]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/ppc/sim_calls.c
sim: create header namespace
[thirdparty/binutils-gdb.git] / sim / ppc / sim_calls.c
CommitLineData
c906108c
SS
1/* This file is part of the program psim.
2
601cecf0 3 Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
c906108c
SS
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
3fd725ef 7 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
51b318de 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
c906108c
SS
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
c906108c 30#include <stdlib.h>
c906108c 31#include <string.h>
c906108c 32
2b3cc94f 33#include "libiberty.h"
c906108c 34#include "bfd.h"
df68e12b
MF
35#include "sim/callback.h"
36#include "sim/sim.h"
aba6488e 37#include "gdb/signals.h"
c906108c 38
7a292a7a
SS
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
45static int poll_quit_count = POLL_QUIT_INTERVAL;
c906108c
SS
46
47/* Structures used by the simulator, for gdb just have static structures */
48
91cd1042 49psim *simulator;
c906108c
SS
50static device *root_device;
51static host_callback *callbacks;
52
c906108c
SS
53SIM_DESC
54sim_open (SIM_OPEN_KIND kind,
55 host_callback *callback,
6b4a8935 56 struct bfd *abfd,
2e3d4f4d 57 char * const *argv)
c906108c
SS
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
dea827fc
MF
71 if (psim_options (root_device, argv + 1, kind) == NULL)
72 return NULL;
c906108c
SS
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
82void
83sim_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
91SIM_RC
b2b255bd 92sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
c906108c
SS
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
126int
127sim_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
137int
5558e7e6 138sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
c906108c
SS
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
c906108c
SS
148void
149sim_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
156SIM_RC
157sim_create_inferior (SIM_DESC sd,
6b4a8935 158 struct bfd *abfd,
2e3d4f4d
MF
159 char * const *argv,
160 char * const *envp)
c906108c
SS
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
601cecf0
AC
177 ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
178 &entry_point, "pc", cooked_transfer) > 0);
c906108c
SS
179 return SIM_RC_OK;
180}
181
182
183void
184sim_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)
a493e3e2 192 *sigrc = GDB_SIGNAL_TRAP;
c906108c
SS
193 else
194 *sigrc = status.signal;
195 break;
196 case was_trap:
197 *reason = sim_stopped;
a493e3e2 198 *sigrc = GDB_SIGNAL_TRAP;
c906108c
SS
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
218int
219sim_stop (SIM_DESC sd)
220{
221 psim_stop (simulator);
222 return 1;
223}
224
225void
226sim_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
241void
60d847df 242sim_do_command (SIM_DESC sd, const char *cmd)
c906108c
SS
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
af9f7da7 253char **
3cb2ab1a 254sim_complete_command (SIM_DESC sd, const char *text, const char *word)
af9f7da7
MF
255{
256 return NULL;
257}
c906108c 258
f4cfa917
MF
259char *
260sim_memory_map (SIM_DESC sd)
261{
262 return NULL;
263}
264
c906108c
SS
265/* Polling, if required */
266
267void
268sim_io_poll_quit (void)
269{
7a292a7a 270 if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
c906108c 271 {
7a292a7a 272 poll_quit_count = POLL_QUIT_INTERVAL;
c906108c
SS
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
287int
288sim_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
305int
306sim_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
323int
324sim_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
343void
344sim_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
359void
360sim_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
de46f45f
MG
374void
375sim_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
c906108c
SS
384/****/
385
625ce09c
JB
386void NORETURN
387error (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
c906108c
SS
396void *
397zalloc(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}