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