1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
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.
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.
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.
22 #include <signal.h> /* FIXME - should be machine dependant version */
29 #undef printf_filtered /* blow away the mapping */
46 #include "remote-sim.h"
49 /* Structures used by the simulator, for gdb just have static structures */
51 static psim *simulator;
52 static device *root_device;
53 static const char *register_names[] = REGISTER_NAMES;
55 /* For communication between sim_load and sim_create_inferior.
56 This can be made to go away, please do. */
57 static unsigned_word entry_point;
60 sim_open (SIM_OPEN_KIND kind, char **argv)
62 /* Note: The simulation is not created by sim_open() because
63 complete information is not yet available */
65 TRACE(trace_gdb, ("sim_open called\n"));
67 if (root_device != NULL)
68 sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
69 root_device = psim_tree();
72 psim_options(root_device, argv + 1);
74 if (ppc_trace[trace_opts])
77 /* fudge our descriptor for now */
83 sim_close (SIM_DESC sd, int quitting)
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]);
92 sim_load (SIM_DESC sd, char *prog, bfd *abfd, int from_tty)
95 TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
99 /* parse the arguments, assume that the file is argument 0 */
100 argv = buildargv(prog);
101 ASSERT(argv != NULL && argv[0] != NULL);
103 /* create the simulator */
104 TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
105 simulator = psim_create(argv[0], root_device);
107 /* bring in all the data section */
108 psim_init(simulator);
110 /* release the arguments */
113 /* get the start address */
115 entry_point = bfd_get_start_address (abfd);
118 abfd = bfd_openr (argv[0], 0);
120 error ("psim: can't open \"%s\": %s\n",
121 argv[0], bfd_errmsg (bfd_get_error ()));
122 if (!bfd_check_format (abfd, bfd_object))
124 const char *errmsg = bfd_errmsg (bfd_get_error ());
126 error ("psim: \"%s\" is not an object file: %s\n",
129 entry_point = bfd_get_start_address (abfd);
138 sim_kill (SIM_DESC sd)
140 TRACE(trace_gdb, ("sim_kill(void) called\n"));
141 /* do nothing, nothing to do */
146 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
148 int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
150 TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
151 (long)mem, (long)buf, length, result));
157 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
159 int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
162 TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
163 (long)mem, (long)buf, length, result));
169 sim_fetch_register (SIM_DESC sd, int regno, unsigned char *buf)
171 if (simulator == NULL) {
174 TRACE(trace_gdb, ("sim_fetch_register(regno=%d(%s), buf=0x%lx)\n",
175 regno, register_names[regno], (long)buf));
176 psim_read_register(simulator, MAX_NR_PROCESSORS,
177 buf, register_names[regno],
183 sim_store_register (SIM_DESC sd, int regno, unsigned char *buf)
185 if (simulator == NULL)
187 TRACE(trace_gdb, ("sim_store_register(regno=%d(%s), buf=0x%lx)\n",
188 regno, register_names[regno], (long)buf));
189 psim_write_register(simulator, MAX_NR_PROCESSORS,
190 buf, register_names[regno],
196 sim_info (SIM_DESC sd, int verbose)
198 TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
199 psim_print_info (simulator, verbose);
204 sim_create_inferior (SIM_DESC sd, char **argv, char **envp)
206 TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
209 psim_init(simulator);
210 psim_stack(simulator, argv, envp);
212 psim_write_register(simulator, -1 /* all start at same PC */,
213 &entry_point, "pc", cooked_transfer);
218 static volatile int sim_should_run;
221 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
223 psim_status status = psim_get_status(simulator);
225 switch (CURRENT_ENVIRONMENT) {
227 case USER_ENVIRONMENT:
228 case VIRTUAL_ENVIRONMENT:
229 switch (status.reason) {
231 *reason = sim_stopped;
233 if (sim_should_run) {
234 error("sim_stop_reason() unknown reason for halt\n");
238 *reason = sim_stopped;
242 *reason = sim_exited;
246 *reason = sim_signalled;
247 *sigrc = status.signal;
252 case OPERATING_ENVIRONMENT:
253 *reason = sim_stopped;
258 error("sim_stop_reason() - unknown environment\n");
262 TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
263 (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
268 /* Run (or resume) the program. */
276 sim_resume (SIM_DESC sd, int step, int siggnal)
278 TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
283 psim_step(simulator);
284 /* sim_stop_reason has a sanity check for stopping while
285 was_continuing. We don't want that here so reset sim_should_run. */
290 RETSIGTYPE (*prev) ();
292 prev = signal(SIGINT, sim_ctrl_c);
294 psim_run_until_stop(simulator, &sim_should_run);
295 signal(SIGINT, prev);
300 sim_do_command (SIM_DESC sd, char *cmd)
302 TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
303 cmd ? cmd : "(null)"));
305 char **argv = buildargv(cmd);
306 psim_command(root_device, argv);
312 /* Map simulator IO operations onto the corresponding GDB I/O
315 NB: Only a limited subset of operations are mapped across. More
316 advanced operations (such as dup or write) must either be mapped to
317 one of the below calls or handled internally */
319 static host_callback *callbacks;
322 sim_io_read_stdin(char *buf,
325 switch (CURRENT_STDIO) {
327 return callbacks->read_stdin(callbacks, buf, sizeof_buf);
330 return callbacks->read(callbacks, 0, buf, sizeof_buf);
333 error("sim_io_read_stdin: unaccounted switch\n");
340 sim_io_write_stdout(const char *buf,
343 switch (CURRENT_STDIO) {
345 return callbacks->write_stdout(callbacks, buf, sizeof_buf);
348 return callbacks->write(callbacks, 1, buf, sizeof_buf);
351 error("sim_io_write_stdout: unaccounted switch\n");
358 sim_io_write_stderr(const char *buf,
361 switch (CURRENT_STDIO) {
363 /* NB: I think there should be an explicit write_stderr callback */
364 return callbacks->write(callbacks, 3, buf, sizeof_buf);
367 return callbacks->write(callbacks, 3, buf, sizeof_buf);
370 error("sim_io_write_stderr: unaccounted switch\n");
378 sim_io_printf_filtered(const char *fmt,
383 /* format the message */
385 vsprintf(message, fmt, ap);
388 if (strlen(message) >= sizeof(message))
389 error("sim_io_printf_filtered: buffer overflow\n");
390 callbacks->printf_filtered(callbacks, "%s", message);
394 sim_io_flush_stdoutput(void)
396 switch (CURRENT_STDIO) {
398 gdb_flush (gdb_stdout);
403 error("sim_io_read_stdin: unaccounted switch\n");
409 sim_set_callbacks (SIM_DESC sd, host_callback *callback)
411 callbacks = callback;
412 TRACE(trace_gdb, ("sim_set_callbacks called\n"));
420 void *memory = (void*)xmalloc(size);
422 error("xmalloc failed\n");
423 memset(memory, 0, size);
427 void zfree(void *data)