]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/sim_calls.c
3dcce19f6283d05065a48f46fab207b0bf6046e0
[thirdparty/binutils-gdb.git] / sim / ppc / sim_calls.c
1 /* This file is part of the program psim.
2
3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
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 3 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, see <http://www.gnu.org/licenses/>.
17
18 */
19
20
21 #include <signal.h> /* FIXME - should be machine dependant version */
22 #include <stdarg.h>
23 #include <ctype.h>
24
25 #include "psim.h"
26 #include "options.h"
27
28 #undef printf_filtered /* blow away the mapping */
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "ansidecl.h"
34 #include "libiberty.h"
35 #include "bfd.h"
36 #include "sim/callback.h"
37 #include "sim/sim.h"
38 #include "gdb/signals.h"
39
40 /* Define the rate at which the simulator should poll the host
41 for a quit. */
42 #ifndef POLL_QUIT_INTERVAL
43 #define POLL_QUIT_INTERVAL 0x20
44 #endif
45
46 static int poll_quit_count = POLL_QUIT_INTERVAL;
47
48 /* Structures used by the simulator, for gdb just have static structures */
49
50 psim *simulator;
51 static device *root_device;
52 static host_callback *callbacks;
53
54 SIM_DESC
55 sim_open (SIM_OPEN_KIND kind,
56 host_callback *callback,
57 struct bfd *abfd,
58 char * const *argv)
59 {
60 callbacks = callback;
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 if (psim_options (root_device, argv + 1, kind) == NULL)
73 return NULL;
74
75 if (ppc_trace[trace_opts])
76 print_options ();
77
78 /* fudge our descriptor for now */
79 return (SIM_DESC) 1;
80 }
81
82
83 void
84 sim_close (SIM_DESC sd, int quitting)
85 {
86 TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
87 if (ppc_trace[trace_print_info] && simulator != NULL)
88 psim_print_info (simulator, ppc_trace[trace_print_info]);
89 }
90
91
92 SIM_RC
93 sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
94 {
95 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
96 prog, from_tty));
97 ASSERT(prog != NULL);
98
99 /* create the simulator */
100 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
101 simulator = psim_create(prog, root_device);
102
103 /* bring in all the data section */
104 psim_init(simulator);
105
106 /* get the start address */
107 if (abfd == NULL)
108 {
109 abfd = bfd_openr (prog, 0);
110 if (abfd == NULL)
111 error ("psim: can't open \"%s\": %s\n",
112 prog, bfd_errmsg (bfd_get_error ()));
113 if (!bfd_check_format (abfd, bfd_object))
114 {
115 const char *errmsg = bfd_errmsg (bfd_get_error ());
116 bfd_close (abfd);
117 error ("psim: \"%s\" is not an object file: %s\n",
118 prog, errmsg);
119 }
120 bfd_close (abfd);
121 }
122
123 return SIM_RC_OK;
124 }
125
126
127 int
128 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
129 {
130 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
131 buf, mem, length);
132 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=%p, length=%d) = %d\n",
133 (long)mem, buf, length, result));
134 return result;
135 }
136
137
138 int
139 sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
140 {
141 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
142 buf, mem, length,
143 1/*violate_ro*/);
144 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=%p, length=%d) = %d\n",
145 (long)mem, buf, length, result));
146 return result;
147 }
148
149 void
150 sim_info (SIM_DESC sd, int verbose)
151 {
152 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
153 psim_print_info (simulator, verbose);
154 }
155
156
157 SIM_RC
158 sim_create_inferior (SIM_DESC sd,
159 struct bfd *abfd,
160 char * const *argv,
161 char * const *envp)
162 {
163 unsigned_word entry_point;
164
165 if (simulator == NULL)
166 error ("No program loaded");
167
168 if (abfd != NULL)
169 entry_point = bfd_get_start_address (abfd);
170 else
171 entry_point = 0xfff00000; /* ??? */
172
173 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
174 entry_point));
175
176 psim_init(simulator);
177 psim_stack(simulator, argv, envp);
178
179 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
180 &entry_point, "pc", cooked_transfer) > 0);
181 return SIM_RC_OK;
182 }
183
184
185 void
186 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
187 {
188 psim_status status = psim_get_status(simulator);
189
190 switch (status.reason) {
191 case was_continuing:
192 *reason = sim_stopped;
193 if (status.signal == 0)
194 *sigrc = GDB_SIGNAL_TRAP;
195 else
196 *sigrc = status.signal;
197 break;
198 case was_trap:
199 *reason = sim_stopped;
200 *sigrc = GDB_SIGNAL_TRAP;
201 break;
202 case was_exited:
203 *reason = sim_exited;
204 *sigrc = status.signal;
205 break;
206 case was_signalled:
207 *reason = sim_signalled;
208 *sigrc = status.signal;
209 break;
210 }
211
212 TRACE(trace_gdb, ("sim_stop_reason(reason=%p(%ld), sigrc=%p(%ld))\n",
213 reason, (long)*reason, sigrc, (long)*sigrc));
214 }
215
216
217
218 /* Run (or resume) the program. */
219
220 int
221 sim_stop (SIM_DESC sd)
222 {
223 psim_stop (simulator);
224 return 1;
225 }
226
227 void
228 sim_resume (SIM_DESC sd, int step, int siggnal)
229 {
230 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
231 step, siggnal));
232
233 if (step)
234 {
235 psim_step (simulator);
236 }
237 else
238 {
239 psim_run (simulator);
240 }
241 }
242
243 void
244 sim_do_command (SIM_DESC sd, const char *cmd)
245 {
246 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
247 cmd ? cmd : "(null)"));
248 if (cmd != NULL) {
249 char **argv = buildargv(cmd);
250 psim_command(root_device, argv);
251 freeargv(argv);
252 }
253 }
254
255 char **
256 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
257 {
258 return NULL;
259 }
260
261 char *
262 sim_memory_map (SIM_DESC sd)
263 {
264 return NULL;
265 }
266
267 /* Polling, if required */
268
269 void
270 sim_io_poll_quit (void)
271 {
272 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
273 {
274 poll_quit_count = POLL_QUIT_INTERVAL;
275 if (callbacks->poll_quit (callbacks))
276 psim_stop (simulator);
277 }
278 }
279
280
281
282 /* Map simulator IO operations onto the corresponding GDB I/O
283 functions.
284
285 NB: Only a limited subset of operations are mapped across. More
286 advanced operations (such as dup or write) must either be mapped to
287 one of the below calls or handled internally */
288
289 int
290 sim_io_read_stdin(char *buf,
291 int sizeof_buf)
292 {
293 switch (CURRENT_STDIO) {
294 case DO_USE_STDIO:
295 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
296 break;
297 case DONT_USE_STDIO:
298 return callbacks->read(callbacks, 0, buf, sizeof_buf);
299 break;
300 default:
301 error("sim_io_read_stdin: unaccounted switch\n");
302 break;
303 }
304 return 0;
305 }
306
307 int
308 sim_io_write_stdout(const char *buf,
309 int sizeof_buf)
310 {
311 switch (CURRENT_STDIO) {
312 case DO_USE_STDIO:
313 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
314 break;
315 case DONT_USE_STDIO:
316 return callbacks->write(callbacks, 1, buf, sizeof_buf);
317 break;
318 default:
319 error("sim_io_write_stdout: unaccounted switch\n");
320 break;
321 }
322 return 0;
323 }
324
325 int
326 sim_io_write_stderr(const char *buf,
327 int sizeof_buf)
328 {
329 switch (CURRENT_STDIO) {
330 case DO_USE_STDIO:
331 /* NB: I think there should be an explicit write_stderr callback */
332 return callbacks->write(callbacks, 3, buf, sizeof_buf);
333 break;
334 case DONT_USE_STDIO:
335 return callbacks->write(callbacks, 3, buf, sizeof_buf);
336 break;
337 default:
338 error("sim_io_write_stderr: unaccounted switch\n");
339 break;
340 }
341 return 0;
342 }
343
344
345 void
346 sim_io_printf_filtered(const char *fmt,
347 ...)
348 {
349 char message[1024];
350 va_list ap;
351 /* format the message */
352 va_start(ap, fmt);
353 vsprintf(message, fmt, ap);
354 va_end(ap);
355 /* sanity check */
356 if (strlen(message) >= sizeof(message))
357 error("sim_io_printf_filtered: buffer overflow\n");
358 callbacks->printf_filtered(callbacks, "%s", message);
359 }
360
361 void
362 sim_io_flush_stdoutput(void)
363 {
364 switch (CURRENT_STDIO) {
365 case DO_USE_STDIO:
366 callbacks->flush_stdout (callbacks);
367 break;
368 case DONT_USE_STDIO:
369 break;
370 default:
371 error("sim_io_read_stdin: unaccounted switch\n");
372 break;
373 }
374 }
375
376 /* Glue to use sim-fpu module. */
377
378 void
379 sim_io_error (SIM_DESC sd, const char *fmt, ...)
380 {
381 va_list ap;
382 va_start(ap, fmt);
383 callbacks->evprintf_filtered (callbacks, fmt, ap);
384 va_end(ap);
385 /* Printing a space here avoids empty printf compiler warnings. Not ideal,
386 but we want error's side-effect where it halts processing. */
387 callbacks->error (callbacks, " ");
388 }
389
390 /****/
391
392 void
393 error (const char *msg, ...)
394 {
395 va_list ap;
396 va_start(ap, msg);
397 callbacks->evprintf_filtered (callbacks, msg, ap);
398 va_end(ap);
399 /* Printing a space here avoids empty printf compiler warnings. Not ideal,
400 but we want error's side-effect where it halts processing. */
401 callbacks->error (callbacks, " ");
402 }
403
404 void *
405 zalloc(long size)
406 {
407 void *memory = (void*)xmalloc(size);
408 if (memory == NULL)
409 error("xmalloc failed\n");
410 memset(memory, 0, size);
411 return memory;
412 }