Index: arm/ChangeLog
[external/binutils.git] / sim / ppc / sim_calls.c
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 "gdb/callback.h"
46 #include "gdb/remote-sim.h"
47
48 /* Define the rate at which the simulator should poll the host
49    for a quit. */
50 #ifndef POLL_QUIT_INTERVAL
51 #define POLL_QUIT_INTERVAL 0x20
52 #endif
53
54 static int poll_quit_count = POLL_QUIT_INTERVAL;
55
56 /* Structures used by the simulator, for gdb just have static structures */
57
58 static psim *simulator;
59 static device *root_device;
60 static host_callback *callbacks;
61
62 /* We use GDB's gdbarch_register_name function to map GDB register
63    numbers onto names, which we can then look up in the register
64    table.  Since the `set architecture' command can select a new
65    processor variant at run-time, the meanings of the register numbers
66    can change, so we need to make sure the sim uses the same
67    name/number mapping that GDB uses.
68
69    (We don't use the REGISTER_NAME macro, which is a wrapper for
70    gdbarch_register_name.  We #include GDB's "defs.h", which tries to
71    #include GDB's "config.h", but gets ours instead, and REGISTER_NAME
72    ends up not getting defined.  Simpler to just use
73    gdbarch_register_name directly.)
74
75    We used to just use the REGISTER_NAMES macro from GDB's
76    target-dependent header files, which expanded into an initializer
77    for an array of strings.  That was kind of nice, because it meant
78    that libsim.a had only a compile-time dependency on GDB; using
79    gdbarch_register_name directly means that there are now link-time
80    and run-time dependencies too.
81
82    Perhaps the host_callback structure could provide a function for
83    retrieving register names; that would be cleaner.  */
84
85 SIM_DESC
86 sim_open (SIM_OPEN_KIND kind,
87           host_callback *callback,
88           struct bfd *abfd,
89           char **argv)
90 {
91   callbacks = callback;
92
93   /* Note: The simulation is not created by sim_open() because
94      complete information is not yet available */
95   /* trace the call */
96   TRACE(trace_gdb, ("sim_open called\n"));
97
98   if (root_device != NULL)
99     sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
100   root_device = psim_tree();
101   simulator = NULL;
102
103   psim_options(root_device, argv + 1);
104
105   if (ppc_trace[trace_opts])
106     print_options ();
107
108   /* fudge our descriptor for now */
109   return (SIM_DESC) 1;
110 }
111
112
113 void
114 sim_close (SIM_DESC sd, int quitting)
115 {
116   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
117   if (ppc_trace[trace_print_info] && simulator != NULL)
118     psim_print_info (simulator, ppc_trace[trace_print_info]);
119 }
120
121
122 SIM_RC
123 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
124 {
125   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
126                     prog, from_tty));
127   ASSERT(prog != NULL);
128
129   /* create the simulator */
130   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
131   simulator = psim_create(prog, root_device);
132
133   /* bring in all the data section */
134   psim_init(simulator);
135
136   /* get the start address */
137   if (abfd == NULL)
138     {
139       abfd = bfd_openr (prog, 0);
140       if (abfd == NULL)
141         error ("psim: can't open \"%s\": %s\n", 
142                prog, bfd_errmsg (bfd_get_error ()));
143       if (!bfd_check_format (abfd, bfd_object)) 
144         {
145           const char *errmsg = bfd_errmsg (bfd_get_error ());
146           bfd_close (abfd);
147           error ("psim: \"%s\" is not an object file: %s\n",
148                  prog, errmsg);
149         }
150       bfd_close (abfd);
151     }
152
153   return SIM_RC_OK;
154 }
155
156
157 int
158 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
159 {
160   int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
161                                 buf, mem, length);
162   TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
163                     (long)mem, (long)buf, length, result));
164   return result;
165 }
166
167
168 int
169 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
170 {
171   int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
172                                  buf, mem, length,
173                                  1/*violate_ro*/);
174   TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
175                     (long)mem, (long)buf, length, result));
176   return result;
177 }
178
179
180 int
181 sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
182 {
183   char *regname;
184
185   if (simulator == NULL) {
186     return 0;
187   }
188
189   /* GDB will sometimes ask for the contents of a register named "";
190      we ignore such requests, and leave garbage in *BUF.  In GDB
191      terms, the empty string means "the register with this number is
192      not present in the currently selected architecture variant."
193      That's following the kludge we're using for the MIPS processors.
194      But there are loops that just walk through the entire list of
195      names and try to get everything.  */
196   regname = gdbarch_register_name (current_gdbarch, regno);
197   /* FIXME: ezannoni 2002/04/15 Remove the 'vr' and 'vscr' check
198      once AltiVec support is committed.  */
199   if (! regname || regname[0] == '\0'
200       || (regname[0] == 'v' && regname[1] == 'r')
201       || (strcmp (regname, "vscr") == 0))
202     return -1;
203
204   TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
205                     regno, regname, (long)buf));
206   psim_read_register(simulator, MAX_NR_PROCESSORS,
207                      buf, regname, raw_transfer);
208   return -1;
209 }
210
211
212 int
213 sim_store_register (SIM_DESC sd, int regno, unsigned char *buf, int length)
214 {
215   char *regname;
216
217   if (simulator == NULL)
218     return 0;
219
220   /* See comments in sim_fetch_register, above.  */
221   regname = gdbarch_register_name (current_gdbarch, regno);
222   /* FIXME: ezannoni 2002/04/15 Remove the 'vr' and 'vscr' check
223      once AltiVec support is committed.  */
224   if (! regname || regname[0] == '\0'
225       || (regname[0] == 'v' && regname[1] == 'r')
226       || (strcmp (regname, "vscr") == 0))
227     return -1;
228
229   TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
230                     regno, regname, (long)buf));
231   psim_write_register(simulator, MAX_NR_PROCESSORS,
232                       buf, regname, raw_transfer);
233   return -1;
234 }
235
236
237 void
238 sim_info (SIM_DESC sd, int verbose)
239 {
240   TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
241   psim_print_info (simulator, verbose);
242 }
243
244
245 SIM_RC
246 sim_create_inferior (SIM_DESC sd,
247                      struct bfd *abfd,
248                      char **argv,
249                      char **envp)
250 {
251   unsigned_word entry_point;
252   TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
253                     entry_point));
254
255   if (simulator == NULL)
256     error ("No program loaded");
257
258   if (abfd != NULL)
259     entry_point = bfd_get_start_address (abfd);
260   else
261     entry_point = 0xfff00000; /* ??? */
262
263   psim_init(simulator);
264   psim_stack(simulator, argv, envp);
265
266   psim_write_register(simulator, -1 /* all start at same PC */,
267                       &entry_point, "pc", cooked_transfer);
268   return SIM_RC_OK;
269 }
270
271
272 void
273 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
274 {
275   psim_status status = psim_get_status(simulator);
276
277   switch (status.reason) {
278   case was_continuing:
279     *reason = sim_stopped;
280     if (status.signal == 0)
281       *sigrc = SIGTRAP;
282     else
283       *sigrc = status.signal;
284     break;
285   case was_trap:
286     *reason = sim_stopped;
287     *sigrc = SIGTRAP;
288     break;
289   case was_exited:
290     *reason = sim_exited;
291     *sigrc = status.signal;
292     break;
293   case was_signalled:
294     *reason = sim_signalled;
295     *sigrc = status.signal;
296     break;
297   }
298
299   TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
300                     (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
301 }
302
303
304
305 /* Run (or resume) the program.  */
306
307 int
308 sim_stop (SIM_DESC sd)
309 {
310   psim_stop (simulator);
311   return 1;
312 }
313
314 void
315 sim_resume (SIM_DESC sd, int step, int siggnal)
316 {
317   TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
318                     step, siggnal));
319
320   if (step)
321     {
322       psim_step (simulator);
323     }
324   else
325     {
326       psim_run (simulator);
327     }
328 }
329
330 void
331 sim_do_command (SIM_DESC sd, char *cmd)
332 {
333   TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
334                     cmd ? cmd : "(null)"));
335   if (cmd != NULL) {
336     char **argv = buildargv(cmd);
337     psim_command(root_device, argv);
338     freeargv(argv);
339   }
340 }
341
342
343 /* Polling, if required */
344
345 void
346 sim_io_poll_quit (void)
347 {
348   if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
349     {
350       poll_quit_count = POLL_QUIT_INTERVAL;
351       if (callbacks->poll_quit (callbacks))
352         psim_stop (simulator);
353     }
354 }
355
356
357
358 /* Map simulator IO operations onto the corresponding GDB I/O
359    functions.
360    
361    NB: Only a limited subset of operations are mapped across.  More
362    advanced operations (such as dup or write) must either be mapped to
363    one of the below calls or handled internally */
364
365 int
366 sim_io_read_stdin(char *buf,
367                   int sizeof_buf)
368 {
369   switch (CURRENT_STDIO) {
370   case DO_USE_STDIO:
371     return callbacks->read_stdin(callbacks, buf, sizeof_buf);
372     break;
373   case DONT_USE_STDIO:
374     return callbacks->read(callbacks, 0, buf, sizeof_buf);
375     break;
376   default:
377     error("sim_io_read_stdin: unaccounted switch\n");
378     break;
379   }
380   return 0;
381 }
382
383 int
384 sim_io_write_stdout(const char *buf,
385                     int sizeof_buf)
386 {
387   switch (CURRENT_STDIO) {
388   case DO_USE_STDIO:
389     return callbacks->write_stdout(callbacks, buf, sizeof_buf);
390     break;
391   case DONT_USE_STDIO:
392     return callbacks->write(callbacks, 1, buf, sizeof_buf);
393     break;
394   default:
395     error("sim_io_write_stdout: unaccounted switch\n");
396     break;
397   }
398   return 0;
399 }
400
401 int
402 sim_io_write_stderr(const char *buf,
403                     int sizeof_buf)
404 {
405   switch (CURRENT_STDIO) {
406   case DO_USE_STDIO:
407     /* NB: I think there should be an explicit write_stderr callback */
408     return callbacks->write(callbacks, 3, buf, sizeof_buf);
409     break;
410   case DONT_USE_STDIO:
411     return callbacks->write(callbacks, 3, buf, sizeof_buf);
412     break;
413   default:
414     error("sim_io_write_stderr: unaccounted switch\n");
415     break;
416   }
417   return 0;
418 }
419
420
421 void
422 sim_io_printf_filtered(const char *fmt,
423                        ...)
424 {
425   char message[1024];
426   va_list ap;
427   /* format the message */
428   va_start(ap, fmt);
429   vsprintf(message, fmt, ap);
430   va_end(ap);
431   /* sanity check */
432   if (strlen(message) >= sizeof(message))
433     error("sim_io_printf_filtered: buffer overflow\n");
434   callbacks->printf_filtered(callbacks, "%s", message);
435 }
436
437 void
438 sim_io_flush_stdoutput(void)
439 {
440   switch (CURRENT_STDIO) {
441   case DO_USE_STDIO:
442     callbacks->flush_stdout (callbacks);
443     break;
444   case DONT_USE_STDIO:
445     break;
446   default:
447     error("sim_io_read_stdin: unaccounted switch\n");
448     break;
449   }
450 }
451
452 void
453 sim_io_error (SIM_DESC sd, const char *fmt, ...)
454 {
455   va_list ap;
456   va_start(ap, fmt);
457   callbacks->evprintf_filtered (callbacks, fmt, ap);
458   va_end(ap);
459   callbacks->error (callbacks, "");
460 }
461
462 /****/
463
464 void *
465 zalloc(long size)
466 {
467   void *memory = (void*)xmalloc(size);
468   if (memory == NULL)
469     error("xmalloc failed\n");
470   memset(memory, 0, size);
471   return memory;
472 }
473
474 void zfree(void *data)
475 {
476   free(data);
477 }