January 23rd merge
[external/binutils.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 "callback.h"
45 #include "remote-sim.h"
46
47
48 /* Structures used by the simulator, for gdb just have static structures */
49
50 static psim *simulator;
51 static device *root_device;
52 static const char *register_names[] = REGISTER_NAMES;
53
54 void
55 sim_open (char *args)
56 {
57   /* Note: The simulation is not created by sim_open() because
58      complete information is not yet available */
59   /* trace the call */
60   TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
61
62   if (root_device != NULL)
63     sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
64   root_device = psim_tree();
65   simulator = NULL;
66
67   if (args) {
68     char **argv = buildargv(args);
69     psim_options(root_device, argv);
70     freeargv(argv);
71   }
72
73   if (ppc_trace[trace_opts])
74     print_options ();
75 }
76
77
78 void
79 sim_close (int quitting)
80 {
81   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
82   if (ppc_trace[trace_print_info] && simulator != NULL)
83     psim_print_info (simulator, ppc_trace[trace_print_info]);
84 }
85
86
87 int
88 sim_load (char *prog, int from_tty)
89 {
90   char **argv;
91   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
92                     prog, from_tty));
93   ASSERT(prog != NULL);
94
95   /* parse the arguments, assume that the file is argument 0 */
96   argv = buildargv(prog);
97   ASSERT(argv != NULL && argv[0] != NULL);
98
99   /* create the simulator */
100   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
101   simulator = psim_create(argv[0], root_device);
102
103   /* bring in all the data section */
104   psim_init(simulator);
105
106   /* release the arguments */
107   freeargv(argv);
108
109   /* `I did it my way' */
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 {
125   int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
126                                 buf, mem, length);
127   TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
128                     (long)mem, (long)buf, length, result));
129   return result;
130 }
131
132
133 int
134 sim_write (SIM_ADDR mem, unsigned char *buf, int length)
135 {
136   int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
137                                  buf, mem, length,
138                                  1/*violate_ro*/);
139   TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
140                     (long)mem, (long)buf, length, result));
141   return result;
142 }
143
144
145 void
146 sim_fetch_register (int regno, unsigned char *buf)
147 {
148   if (simulator == NULL) {
149     return;
150   }
151   TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
152                     regno, register_names[regno], (long)buf));
153   psim_read_register(simulator, MAX_NR_PROCESSORS,
154                      buf, register_names[regno],
155                      raw_transfer);
156 }
157
158
159 void
160 sim_store_register (int regno, unsigned char *buf)
161 {
162   if (simulator == NULL)
163     return;
164   TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
165                     regno, register_names[regno], (long)buf));
166   psim_write_register(simulator, MAX_NR_PROCESSORS,
167                       buf, register_names[regno],
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));
176   psim_print_info (simulator, verbose);
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
188   psim_init(simulator);
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
205   case USER_ENVIRONMENT:
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   }
239
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));
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 {
256   TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
257                     step, siggnal));
258
259   if (step)
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     }
266   else
267     {
268       void (*prev) ();
269
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     }
275 }
276
277 void
278 sim_do_command (char *cmd)
279 {
280   TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
281                     cmd ? cmd : "(null)"));
282   if (cmd != NULL) {
283     char **argv = buildargv(cmd);
284     psim_command(root_device, argv);
285     freeargv(argv);
286   }
287 }
288
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   }
314   return 0;
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   }
332   return 0;
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   }
351   return 0;
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
386 void
387 sim_set_callbacks (host_callback *callback)
388 {
389   callbacks = callback;
390   TRACE(trace_gdb, ("sim_set_callbacks called\n"));
391 }
392
393 /****/
394
395 void *
396 zalloc(long size)
397 {
398   void *memory = (void*)xmalloc(size);
399   if (memory == NULL)
400     error("xmalloc failed\n");
401   memset(memory, 0, size);
402   return memory;
403 }
404
405 void zfree(void *data)
406 {
407   mfree(NULL, data);
408 }