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