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