Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* This file is part of the program psim. |
2 | ||
3 | Copyright (C) 1994-1996,1998, Andrew Cagney <cagney@highland.com.au> | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | ||
19 | */ | |
20 | ||
21 | ||
22 | #include <signal.h> /* FIXME - should be machine dependant version */ | |
23 | #include <stdarg.h> | |
24 | #include <ctype.h> | |
25 | ||
26 | #include "psim.h" | |
27 | #include "options.h" | |
28 | ||
29 | #undef printf_filtered /* blow away the mapping */ | |
30 | ||
31 | #ifdef HAVE_STDLIB_H | |
32 | #include <stdlib.h> | |
33 | #endif | |
34 | ||
35 | #ifdef HAVE_STRING_H | |
36 | #include <string.h> | |
37 | #else | |
38 | #ifdef HAVE_STRINGS_H | |
39 | #include <strings.h> | |
40 | #endif | |
41 | #endif | |
42 | ||
43 | #include "defs.h" | |
44 | #include "bfd.h" | |
45 | #include "callback.h" | |
46 | #include "remote-sim.h" | |
47 | ||
48 | ||
49 | /* Structures used by the simulator, for gdb just have static structures */ | |
50 | ||
51 | static psim *simulator; | |
52 | static device *root_device; | |
53 | static host_callback *callbacks; | |
54 | ||
55 | /* We use GDB's reg_names array to map GDB register numbers onto | |
56 | names, which we can then look up in the register table. | |
57 | ||
58 | We used to just use the REGISTER_NAMES macro, from GDB's | |
59 | target-dependent header files. That was kind of nice, because it | |
60 | meant that libsim.a had only a compile-time dependency on GDB; | |
61 | using reg_names directly means that there are now link-time and | |
62 | run-time dependencies too. | |
63 | ||
64 | However, the GDB PPC back-end now modifies the reg_names array when | |
65 | the user runs the `set processor' command, which affects the | |
66 | meanings of the register numbers. So the sim needs to see the | |
67 | register names GDB is actually using. | |
68 | ||
69 | Perhaps the host_callback structure could contain a pointer to the | |
70 | register name table; that would be cleaner. */ | |
71 | ||
72 | SIM_DESC | |
73 | sim_open (SIM_OPEN_KIND kind, | |
74 | host_callback *callback, | |
75 | struct _bfd *abfd, | |
76 | char **argv) | |
77 | { | |
78 | callbacks = callback; | |
79 | ||
80 | /* Note: The simulation is not created by sim_open() because | |
81 | complete information is not yet available */ | |
82 | /* trace the call */ | |
83 | TRACE(trace_gdb, ("sim_open called\n")); | |
84 | ||
85 | if (root_device != NULL) | |
86 | sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n"); | |
87 | root_device = psim_tree(); | |
88 | simulator = NULL; | |
89 | ||
90 | psim_options(root_device, argv + 1); | |
91 | ||
92 | if (ppc_trace[trace_opts]) | |
93 | print_options (); | |
94 | ||
95 | /* fudge our descriptor for now */ | |
96 | return (SIM_DESC) 1; | |
97 | } | |
98 | ||
99 | ||
100 | void | |
101 | sim_close (SIM_DESC sd, int quitting) | |
102 | { | |
103 | TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting)); | |
104 | if (ppc_trace[trace_print_info] && simulator != NULL) | |
105 | psim_print_info (simulator, ppc_trace[trace_print_info]); | |
106 | } | |
107 | ||
108 | ||
109 | SIM_RC | |
110 | sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty) | |
111 | { | |
112 | TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n", | |
113 | prog, from_tty)); | |
114 | ASSERT(prog != NULL); | |
115 | ||
116 | /* create the simulator */ | |
117 | TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n")); | |
118 | simulator = psim_create(prog, root_device); | |
119 | ||
120 | /* bring in all the data section */ | |
121 | psim_init(simulator); | |
122 | ||
123 | /* get the start address */ | |
124 | if (abfd == NULL) | |
125 | { | |
126 | abfd = bfd_openr (prog, 0); | |
127 | if (abfd == NULL) | |
128 | error ("psim: can't open \"%s\": %s\n", | |
129 | prog, bfd_errmsg (bfd_get_error ())); | |
130 | if (!bfd_check_format (abfd, bfd_object)) | |
131 | { | |
132 | const char *errmsg = bfd_errmsg (bfd_get_error ()); | |
133 | bfd_close (abfd); | |
134 | error ("psim: \"%s\" is not an object file: %s\n", | |
135 | prog, errmsg); | |
136 | } | |
137 | bfd_close (abfd); | |
138 | } | |
139 | ||
140 | return SIM_RC_OK; | |
141 | } | |
142 | ||
143 | ||
144 | int | |
145 | sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) | |
146 | { | |
147 | int result = psim_read_memory(simulator, MAX_NR_PROCESSORS, | |
148 | buf, mem, length); | |
149 | TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", | |
150 | (long)mem, (long)buf, length, result)); | |
151 | return result; | |
152 | } | |
153 | ||
154 | ||
155 | int | |
156 | sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length) | |
157 | { | |
158 | int result = psim_write_memory(simulator, MAX_NR_PROCESSORS, | |
159 | buf, mem, length, | |
160 | 1/*violate_ro*/); | |
161 | TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", | |
162 | (long)mem, (long)buf, length, result)); | |
163 | return result; | |
164 | } | |
165 | ||
166 | ||
167 | int | |
168 | sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length) | |
169 | { | |
170 | char *regname; | |
171 | ||
172 | if (simulator == NULL) { | |
173 | return 0; | |
174 | } | |
175 | ||
176 | /* GDB will sometimes ask for the contents of a register named ""; | |
177 | we ignore such requests, and leave garbage in *BUF. In | |
178 | REG_NAMES, the empty string means "the register with this | |
179 | number is not present in the currently selected architecture | |
180 | variant." That's following the kludge we're using for the MIPS | |
181 | processors. But there are loops that just walk through the | |
182 | entire list of names and try to get everything. */ | |
183 | regname = REGISTER_NAME (regno); | |
184 | if (! regname || regname[0] == '\0') | |
185 | return -1; | |
186 | ||
187 | TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n", | |
188 | regno, regname, (long)buf)); | |
189 | psim_read_register(simulator, MAX_NR_PROCESSORS, | |
190 | buf, regname, raw_transfer); | |
191 | return -1; | |
192 | } | |
193 | ||
194 | ||
195 | int | |
196 | sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length) | |
197 | { | |
198 | char *regname; | |
199 | ||
200 | if (simulator == NULL) | |
201 | return 0; | |
202 | ||
203 | /* See comments in sim_fetch_register, above. */ | |
204 | regname = REGISTER_NAME (regno); | |
205 | if (! regname || regname[0] == '\0') | |
206 | return -1; | |
207 | ||
208 | TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n", | |
209 | regno, regname, (long)buf)); | |
210 | psim_write_register(simulator, MAX_NR_PROCESSORS, | |
211 | buf, regname, raw_transfer); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | ||
216 | void | |
217 | sim_info (SIM_DESC sd, int verbose) | |
218 | { | |
219 | TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose)); | |
220 | psim_print_info (simulator, verbose); | |
221 | } | |
222 | ||
223 | ||
224 | SIM_RC | |
225 | sim_create_inferior (SIM_DESC sd, | |
226 | struct _bfd *abfd, | |
227 | char **argv, | |
228 | char **envp) | |
229 | { | |
230 | unsigned_word entry_point; | |
231 | TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n", | |
232 | entry_point)); | |
233 | ||
234 | if (simulator == NULL) | |
235 | error ("No program loaded"); | |
236 | ||
237 | if (abfd != NULL) | |
238 | entry_point = bfd_get_start_address (abfd); | |
239 | else | |
240 | entry_point = 0xfff00000; /* ??? */ | |
241 | ||
242 | psim_init(simulator); | |
243 | psim_stack(simulator, argv, envp); | |
244 | ||
245 | psim_write_register(simulator, -1 /* all start at same PC */, | |
246 | &entry_point, "pc", cooked_transfer); | |
247 | return SIM_RC_OK; | |
248 | } | |
249 | ||
250 | ||
251 | void | |
252 | sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc) | |
253 | { | |
254 | psim_status status = psim_get_status(simulator); | |
255 | ||
256 | switch (status.reason) { | |
257 | case was_continuing: | |
258 | *reason = sim_stopped; | |
259 | if (status.signal == 0) | |
260 | *sigrc = SIGTRAP; | |
261 | else | |
262 | *sigrc = status.signal; | |
263 | break; | |
264 | case was_trap: | |
265 | *reason = sim_stopped; | |
266 | *sigrc = SIGTRAP; | |
267 | break; | |
268 | case was_exited: | |
269 | *reason = sim_exited; | |
270 | *sigrc = status.signal; | |
271 | break; | |
272 | case was_signalled: | |
273 | *reason = sim_signalled; | |
274 | *sigrc = status.signal; | |
275 | break; | |
276 | } | |
277 | ||
278 | TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n", | |
279 | (long)reason, (long)*reason, (long)sigrc, (long)*sigrc)); | |
280 | } | |
281 | ||
282 | ||
283 | ||
284 | /* Run (or resume) the program. */ | |
285 | ||
286 | int | |
287 | sim_stop (SIM_DESC sd) | |
288 | { | |
289 | psim_stop (simulator); | |
290 | return 1; | |
291 | } | |
292 | ||
293 | void | |
294 | sim_resume (SIM_DESC sd, int step, int siggnal) | |
295 | { | |
296 | TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n", | |
297 | step, siggnal)); | |
298 | ||
299 | if (step) | |
300 | { | |
301 | psim_step (simulator); | |
302 | } | |
303 | else | |
304 | { | |
305 | psim_run (simulator); | |
306 | } | |
307 | } | |
308 | ||
309 | void | |
310 | sim_do_command (SIM_DESC sd, char *cmd) | |
311 | { | |
312 | TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", | |
313 | cmd ? cmd : "(null)")); | |
314 | if (cmd != NULL) { | |
315 | char **argv = buildargv(cmd); | |
316 | psim_command(root_device, argv); | |
317 | freeargv(argv); | |
318 | } | |
319 | } | |
320 | ||
321 | ||
322 | /* Polling, if required */ | |
323 | ||
324 | void | |
325 | sim_io_poll_quit (void) | |
326 | { | |
327 | if (callbacks->poll_quit != NULL) | |
328 | { | |
329 | if (callbacks->poll_quit (callbacks)) | |
330 | psim_stop (simulator); | |
331 | } | |
332 | } | |
333 | ||
334 | ||
335 | ||
336 | /* Map simulator IO operations onto the corresponding GDB I/O | |
337 | functions. | |
338 | ||
339 | NB: Only a limited subset of operations are mapped across. More | |
340 | advanced operations (such as dup or write) must either be mapped to | |
341 | one of the below calls or handled internally */ | |
342 | ||
343 | int | |
344 | sim_io_read_stdin(char *buf, | |
345 | int sizeof_buf) | |
346 | { | |
347 | switch (CURRENT_STDIO) { | |
348 | case DO_USE_STDIO: | |
349 | return callbacks->read_stdin(callbacks, buf, sizeof_buf); | |
350 | break; | |
351 | case DONT_USE_STDIO: | |
352 | return callbacks->read(callbacks, 0, buf, sizeof_buf); | |
353 | break; | |
354 | default: | |
355 | error("sim_io_read_stdin: unaccounted switch\n"); | |
356 | break; | |
357 | } | |
358 | return 0; | |
359 | } | |
360 | ||
361 | int | |
362 | sim_io_write_stdout(const char *buf, | |
363 | int sizeof_buf) | |
364 | { | |
365 | switch (CURRENT_STDIO) { | |
366 | case DO_USE_STDIO: | |
367 | return callbacks->write_stdout(callbacks, buf, sizeof_buf); | |
368 | break; | |
369 | case DONT_USE_STDIO: | |
370 | return callbacks->write(callbacks, 1, buf, sizeof_buf); | |
371 | break; | |
372 | default: | |
373 | error("sim_io_write_stdout: unaccounted switch\n"); | |
374 | break; | |
375 | } | |
376 | return 0; | |
377 | } | |
378 | ||
379 | int | |
380 | sim_io_write_stderr(const char *buf, | |
381 | int sizeof_buf) | |
382 | { | |
383 | switch (CURRENT_STDIO) { | |
384 | case DO_USE_STDIO: | |
385 | /* NB: I think there should be an explicit write_stderr callback */ | |
386 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
387 | break; | |
388 | case DONT_USE_STDIO: | |
389 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
390 | break; | |
391 | default: | |
392 | error("sim_io_write_stderr: unaccounted switch\n"); | |
393 | break; | |
394 | } | |
395 | return 0; | |
396 | } | |
397 | ||
398 | ||
399 | void | |
400 | sim_io_printf_filtered(const char *fmt, | |
401 | ...) | |
402 | { | |
403 | char message[1024]; | |
404 | va_list ap; | |
405 | /* format the message */ | |
406 | va_start(ap, fmt); | |
407 | vsprintf(message, fmt, ap); | |
408 | va_end(ap); | |
409 | /* sanity check */ | |
410 | if (strlen(message) >= sizeof(message)) | |
411 | error("sim_io_printf_filtered: buffer overflow\n"); | |
412 | callbacks->printf_filtered(callbacks, "%s", message); | |
413 | } | |
414 | ||
415 | void | |
416 | sim_io_flush_stdoutput(void) | |
417 | { | |
418 | switch (CURRENT_STDIO) { | |
419 | case DO_USE_STDIO: | |
420 | callbacks->flush_stdout (callbacks); | |
421 | break; | |
422 | case DONT_USE_STDIO: | |
423 | break; | |
424 | default: | |
425 | error("sim_io_read_stdin: unaccounted switch\n"); | |
426 | break; | |
427 | } | |
428 | } | |
429 | ||
430 | /****/ | |
431 | ||
432 | void * | |
433 | zalloc(long size) | |
434 | { | |
435 | void *memory = (void*)xmalloc(size); | |
436 | if (memory == NULL) | |
437 | error("xmalloc failed\n"); | |
438 | memset(memory, 0, size); | |
439 | return memory; | |
440 | } | |
441 | ||
442 | void zfree(void *data) | |
443 | { | |
444 | free(data); | |
445 | } |