Commit | Line | Data |
---|---|---|
8eab189b MM |
1 | /* This file is part of the program psim. |
2 | ||
30c87b55 | 3 | Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au> |
8eab189b MM |
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> | |
8eab189b MM |
24 | #include <ctype.h> |
25 | ||
8eab189b | 26 | #include "psim.h" |
28816f45 | 27 | #include "options.h" |
8eab189b | 28 | |
939b233a DE |
29 | #undef printf_filtered /* blow away the mapping */ |
30 | ||
c494cadd MM |
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 | ||
5c04f4f7 MM |
43 | #include "defs.h" |
44 | #include "callback.h" | |
45 | #include "remote-sim.h" | |
8eab189b MM |
46 | |
47 | ||
48 | /* Structures used by the simulator, for gdb just have static structures */ | |
49 | ||
50 | static psim *simulator; | |
30c87b55 MM |
51 | static device *root_device; |
52 | static const char *register_names[] = REGISTER_NAMES; | |
8eab189b MM |
53 | |
54 | void | |
55 | sim_open (char *args) | |
56 | { | |
30c87b55 MM |
57 | /* Note: The simulation is not created by sim_open() because |
58 | complete information is not yet available */ | |
8eab189b MM |
59 | /* trace the call */ |
60 | TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)")); | |
61 | ||
30c87b55 | 62 | if (root_device != NULL) |
939b233a | 63 | sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n"); |
30c87b55 MM |
64 | root_device = psim_tree(); |
65 | simulator = NULL; | |
66 | ||
8eab189b | 67 | if (args) { |
a983c8f0 | 68 | char **argv = buildargv(args); |
30c87b55 MM |
69 | psim_options(root_device, argv); |
70 | freeargv(argv); | |
8eab189b MM |
71 | } |
72 | ||
28816f45 MM |
73 | if (ppc_trace[trace_opts]) |
74 | print_options (); | |
8eab189b MM |
75 | } |
76 | ||
77 | ||
78 | void | |
79 | sim_close (int quitting) | |
80 | { | |
81 | TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting)); | |
30c87b55 MM |
82 | if (ppc_trace[trace_print_info] && simulator != NULL) |
83 | psim_print_info (simulator, ppc_trace[trace_print_info]); | |
8eab189b MM |
84 | } |
85 | ||
86 | ||
87 | int | |
88 | sim_load (char *prog, int from_tty) | |
89 | { | |
a983c8f0 | 90 | char **argv; |
8eab189b MM |
91 | TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n", |
92 | prog, from_tty)); | |
a983c8f0 | 93 | ASSERT(prog != NULL); |
8eab189b | 94 | |
a983c8f0 MM |
95 | /* parse the arguments, assume that the file is argument 0 */ |
96 | argv = buildargv(prog); | |
97 | ASSERT(argv != NULL && argv[0] != NULL); | |
8eab189b MM |
98 | |
99 | /* create the simulator */ | |
100 | TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n")); | |
30c87b55 | 101 | simulator = psim_create(argv[0], root_device); |
8eab189b MM |
102 | |
103 | /* bring in all the data section */ | |
a983c8f0 | 104 | psim_init(simulator); |
8eab189b | 105 | |
a983c8f0 MM |
106 | /* release the arguments */ |
107 | freeargv(argv); | |
108 | ||
109 | /* `I did it my way' */ | |
8eab189b MM |
110 | return 0; |
111 | } | |
112 | ||
113 | ||
114 | void | |
115 | sim_kill (void) | |
116 | { | |
117 | TRACE(trace_gdb, ("sim_kill(void) called\n")); | |
118 | /* do nothing, nothing to do */ | |
119 | } | |
120 | ||
121 | ||
122 | int | |
123 | sim_read (SIM_ADDR mem, unsigned char *buf, int length) | |
124 | { | |
a983c8f0 MM |
125 | int result = psim_read_memory(simulator, MAX_NR_PROCESSORS, |
126 | buf, mem, length); | |
28816f45 MM |
127 | TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", |
128 | (long)mem, (long)buf, length, result)); | |
a983c8f0 | 129 | return result; |
8eab189b MM |
130 | } |
131 | ||
132 | ||
133 | int | |
134 | sim_write (SIM_ADDR mem, unsigned char *buf, int length) | |
135 | { | |
a983c8f0 MM |
136 | int result = psim_write_memory(simulator, MAX_NR_PROCESSORS, |
137 | buf, mem, length, | |
138 | 1/*violate_ro*/); | |
28816f45 MM |
139 | TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n", |
140 | (long)mem, (long)buf, length, result)); | |
a983c8f0 | 141 | return result; |
8eab189b MM |
142 | } |
143 | ||
144 | ||
145 | void | |
146 | sim_fetch_register (int regno, unsigned char *buf) | |
147 | { | |
148 | if (simulator == NULL) { | |
149 | return; | |
150 | } | |
28816f45 MM |
151 | TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n", |
152 | regno, register_names[regno], (long)buf)); | |
a983c8f0 MM |
153 | psim_read_register(simulator, MAX_NR_PROCESSORS, |
154 | buf, register_names[regno], | |
8eab189b MM |
155 | raw_transfer); |
156 | } | |
157 | ||
158 | ||
159 | void | |
160 | sim_store_register (int regno, unsigned char *buf) | |
161 | { | |
162 | if (simulator == NULL) | |
163 | return; | |
28816f45 MM |
164 | TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n", |
165 | regno, register_names[regno], (long)buf)); | |
a983c8f0 MM |
166 | psim_write_register(simulator, MAX_NR_PROCESSORS, |
167 | buf, register_names[regno], | |
8eab189b MM |
168 | raw_transfer); |
169 | } | |
170 | ||
171 | ||
172 | void | |
173 | sim_info (int verbose) | |
174 | { | |
175 | TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose)); | |
83d96c6e | 176 | psim_print_info (simulator, verbose); |
8eab189b MM |
177 | } |
178 | ||
179 | ||
180 | void | |
181 | sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp) | |
182 | { | |
183 | unsigned_word entry_point = start_address; | |
184 | ||
185 | TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n", | |
186 | start_address)); | |
187 | ||
a983c8f0 | 188 | psim_init(simulator); |
8eab189b MM |
189 | psim_stack(simulator, argv, envp); |
190 | ||
191 | psim_write_register(simulator, -1 /* all start at same PC */, | |
192 | &entry_point, "pc", cooked_transfer); | |
193 | } | |
194 | ||
195 | ||
196 | static volatile int sim_should_run; | |
197 | ||
198 | void | |
199 | sim_stop_reason (enum sim_stop *reason, int *sigrc) | |
200 | { | |
201 | psim_status status = psim_get_status(simulator); | |
202 | ||
203 | switch (CURRENT_ENVIRONMENT) { | |
204 | ||
a983c8f0 | 205 | case USER_ENVIRONMENT: |
8eab189b MM |
206 | case VIRTUAL_ENVIRONMENT: |
207 | switch (status.reason) { | |
208 | case was_continuing: | |
209 | *reason = sim_stopped; | |
210 | *sigrc = SIGTRAP; | |
211 | if (sim_should_run) { | |
212 | error("sim_stop_reason() unknown reason for halt\n"); | |
213 | } | |
214 | break; | |
215 | case was_trap: | |
216 | *reason = sim_stopped; | |
217 | *sigrc = SIGTRAP; | |
218 | break; | |
219 | case was_exited: | |
220 | *reason = sim_exited; | |
221 | *sigrc = 0; | |
222 | break; | |
223 | case was_signalled: | |
224 | *reason = sim_signalled; | |
225 | *sigrc = status.signal; | |
226 | break; | |
227 | } | |
228 | break; | |
229 | ||
230 | case OPERATING_ENVIRONMENT: | |
231 | *reason = sim_stopped; | |
232 | *sigrc = SIGTRAP; | |
233 | break; | |
234 | ||
235 | default: | |
236 | error("sim_stop_reason() - unknown environment\n"); | |
237 | ||
238 | } | |
a983c8f0 | 239 | |
28816f45 MM |
240 | TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n", |
241 | (long)reason, (long)*reason, (long)sigrc, (long)*sigrc)); | |
8eab189b MM |
242 | } |
243 | ||
244 | ||
245 | ||
246 | /* Run (or resume) the program. */ | |
247 | static void | |
248 | sim_ctrl_c() | |
249 | { | |
250 | sim_should_run = 0; | |
251 | } | |
252 | ||
253 | void | |
254 | sim_resume (int step, int siggnal) | |
255 | { | |
a983c8f0 MM |
256 | TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n", |
257 | step, siggnal)); | |
258 | ||
8eab189b | 259 | if (step) |
939b233a DE |
260 | { |
261 | psim_step(simulator); | |
262 | /* sim_stop_reason has a sanity check for stopping while | |
263 | was_continuing. We don't want that here so reset sim_should_run. */ | |
264 | sim_should_run = 0; | |
265 | } | |
8eab189b | 266 | else |
939b233a DE |
267 | { |
268 | void (*prev) (); | |
8eab189b | 269 | |
939b233a DE |
270 | prev = signal(SIGINT, sim_ctrl_c); |
271 | sim_should_run = 1; | |
272 | psim_run_until_stop(simulator, &sim_should_run); | |
273 | signal(SIGINT, prev); | |
274 | } | |
8eab189b MM |
275 | } |
276 | ||
277 | void | |
30c87b55 | 278 | sim_do_command (char *cmd) |
8eab189b | 279 | { |
30c87b55 MM |
280 | TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", |
281 | cmd ? cmd : "(null)")); | |
5c04f4f7 | 282 | if (cmd != NULL) { |
30c87b55 | 283 | char **argv = buildargv(cmd); |
5c04f4f7 | 284 | psim_command(root_device, argv); |
30c87b55 MM |
285 | freeargv(argv); |
286 | } | |
8eab189b MM |
287 | } |
288 | ||
939b233a DE |
289 | |
290 | /* Map simulator IO operations onto the corresponding GDB I/O | |
291 | functions. | |
292 | ||
293 | NB: Only a limited subset of operations are mapped across. More | |
294 | advanced operations (such as dup or write) must either be mapped to | |
295 | one of the below calls or handled internally */ | |
296 | ||
297 | static host_callback *callbacks; | |
298 | ||
299 | int | |
300 | sim_io_read_stdin(char *buf, | |
301 | int sizeof_buf) | |
302 | { | |
303 | switch (CURRENT_STDIO) { | |
304 | case DO_USE_STDIO: | |
305 | return callbacks->read_stdin(callbacks, buf, sizeof_buf); | |
306 | break; | |
307 | case DONT_USE_STDIO: | |
308 | return callbacks->read(callbacks, 0, buf, sizeof_buf); | |
309 | break; | |
310 | default: | |
311 | error("sim_io_read_stdin: unaccounted switch\n"); | |
312 | break; | |
313 | } | |
5c04f4f7 | 314 | return 0; |
939b233a DE |
315 | } |
316 | ||
317 | int | |
318 | sim_io_write_stdout(const char *buf, | |
319 | int sizeof_buf) | |
320 | { | |
321 | switch (CURRENT_STDIO) { | |
322 | case DO_USE_STDIO: | |
323 | return callbacks->write_stdout(callbacks, buf, sizeof_buf); | |
324 | break; | |
325 | case DONT_USE_STDIO: | |
326 | return callbacks->write(callbacks, 1, buf, sizeof_buf); | |
327 | break; | |
328 | default: | |
329 | error("sim_io_write_stdout: unaccounted switch\n"); | |
330 | break; | |
331 | } | |
5c04f4f7 | 332 | return 0; |
939b233a DE |
333 | } |
334 | ||
335 | int | |
336 | sim_io_write_stderr(const char *buf, | |
337 | int sizeof_buf) | |
338 | { | |
339 | switch (CURRENT_STDIO) { | |
340 | case DO_USE_STDIO: | |
341 | /* NB: I think there should be an explicit write_stderr callback */ | |
342 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
343 | break; | |
344 | case DONT_USE_STDIO: | |
345 | return callbacks->write(callbacks, 3, buf, sizeof_buf); | |
346 | break; | |
347 | default: | |
348 | error("sim_io_write_stderr: unaccounted switch\n"); | |
349 | break; | |
350 | } | |
5c04f4f7 | 351 | return 0; |
939b233a DE |
352 | } |
353 | ||
354 | ||
355 | void | |
356 | sim_io_printf_filtered(const char *fmt, | |
357 | ...) | |
358 | { | |
359 | char message[1024]; | |
360 | va_list ap; | |
361 | /* format the message */ | |
362 | va_start(ap, fmt); | |
363 | vsprintf(message, fmt, ap); | |
364 | va_end(ap); | |
365 | /* sanity check */ | |
366 | if (strlen(message) >= sizeof(message)) | |
367 | error("sim_io_printf_filtered: buffer overflow\n"); | |
368 | callbacks->printf_filtered(callbacks, "%s", message); | |
369 | } | |
370 | ||
371 | void | |
372 | sim_io_flush_stdoutput(void) | |
373 | { | |
374 | switch (CURRENT_STDIO) { | |
375 | case DO_USE_STDIO: | |
376 | gdb_flush (gdb_stdout); | |
377 | break; | |
378 | case DONT_USE_STDIO: | |
379 | break; | |
380 | default: | |
381 | error("sim_io_read_stdin: unaccounted switch\n"); | |
382 | break; | |
383 | } | |
384 | } | |
385 | ||
f46f3807 MM |
386 | void |
387 | sim_set_callbacks (host_callback *callback) | |
388 | { | |
939b233a | 389 | callbacks = callback; |
f46f3807 MM |
390 | TRACE(trace_gdb, ("sim_set_callbacks called\n")); |
391 | } | |
392 | ||
8eab189b MM |
393 | /****/ |
394 | ||
395 | void * | |
396 | zalloc(long size) | |
397 | { | |
398 | void *memory = (void*)xmalloc(size); | |
399 | if (memory == NULL) | |
400 | error("xmalloc failed\n"); | |
290ad14a | 401 | memset(memory, 0, size); |
8eab189b MM |
402 | return memory; |
403 | } | |
404 | ||
405 | void zfree(void *data) | |
406 | { | |
407 | mfree(NULL, data); | |
408 | } |