1 /* Generic simulator halt/restart.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
28 #include "sim-assert.h"
29 #include "sim-signal.h"
32 REASON/SIGRC are the values returned by sim_stop_reason.
33 ??? Should each cpu have its own copy? */
36 sim_engine_get_run_state (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
38 sim_engine *engine = STATE_ENGINE (sd);
39 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
40 *reason = engine->reason;
41 *sigrc = engine->sigrc;
44 /* Set the run state to REASON/SIGRC.
45 REASON/SIGRC are the values returned by sim_stop_reason.
46 ??? Should each cpu have its own copy? */
49 sim_engine_set_run_state (SIM_DESC sd, enum sim_stop reason, int sigrc)
51 sim_engine *engine = STATE_ENGINE (sd);
52 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
53 engine->reason = reason;
54 engine->sigrc = sigrc;
60 sim_engine_halt (SIM_DESC sd,
62 sim_cpu *next_cpu, /* NULL - use default */
67 sim_engine *engine = STATE_ENGINE (sd);
68 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
69 if (engine->jmpbuf != NULL)
71 jmp_buf *halt_buf = engine->jmpbuf;
72 engine->last_cpu = last_cpu;
73 engine->next_cpu = next_cpu;
74 engine->reason = reason;
75 engine->sigrc = sigrc;
77 SIM_ENGINE_HALT_HOOK (sd, last_cpu, cia);
79 #ifdef SIM_CPU_EXCEPTION_SUSPEND
80 if (last_cpu != NULL && reason != sim_exited)
81 SIM_CPU_EXCEPTION_SUSPEND (sd, last_cpu, sim_signal_to_host (sd, sigrc));
84 longjmp (*halt_buf, sim_engine_halt_jmpval);
88 sim_io_error (sd, "sim_halt - bad long jump");
97 sim_engine_restart (SIM_DESC sd,
102 sim_engine *engine = STATE_ENGINE (sd);
103 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
104 if (engine->jmpbuf != NULL)
106 jmp_buf *halt_buf = engine->jmpbuf;
107 engine->last_cpu = last_cpu;
108 engine->next_cpu = next_cpu;
109 SIM_ENGINE_RESTART_HOOK (sd, last_cpu, cia);
110 longjmp (*halt_buf, sim_engine_restart_jmpval);
113 sim_io_error (sd, "sim_restart - bad long jump");
117 /* Generic error code */
120 sim_engine_vabort (SIM_DESC sd,
126 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
129 vfprintf (stderr, fmt, ap);
130 fprintf (stderr, "\nQuit\n");
133 else if (STATE_ENGINE (sd)->jmpbuf == NULL)
135 sim_io_evprintf (sd, fmt, ap);
136 sim_io_eprintf (sd, "\n");
137 sim_io_error (sd, "Quit Simulator");
142 sim_io_evprintf (sd, fmt, ap);
143 sim_io_eprintf (sd, "\n");
144 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGABRT);
149 sim_engine_abort (SIM_DESC sd,
156 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
158 sim_engine_vabort (sd, cpu, cia, fmt, ap);
163 /* Generic next/last cpu */
166 sim_engine_last_cpu_nr (SIM_DESC sd)
168 sim_engine *engine = STATE_ENGINE (sd);
169 if (engine->last_cpu != NULL)
170 return engine->last_cpu - STATE_CPU (sd, 0);
172 return MAX_NR_PROCESSORS;
176 sim_engine_next_cpu_nr (SIM_DESC sd)
178 sim_engine *engine = STATE_ENGINE (sd);
179 if (engine->next_cpu != NULL)
180 return engine->next_cpu - STATE_CPU (sd, 0);
182 return sim_engine_last_cpu_nr (sd) + 1;
186 sim_engine_nr_cpus (SIM_DESC sd)
188 sim_engine *engine = STATE_ENGINE (sd);
189 return engine->nr_cpus;
198 sim_engine_init (SIM_DESC sd)
200 /* initialize the start/stop/resume engine */
201 sim_engine *engine = STATE_ENGINE (sd);
202 engine->jmpbuf = NULL;
203 engine->last_cpu = NULL;
204 engine->next_cpu = NULL;
205 engine->nr_cpus = MAX_NR_PROCESSORS;
206 engine->reason = sim_running;
208 engine->stepper = NULL; /* sim_events_init will clean it up */
212 /* Provide a prototype to silence -Wmissing-prototypes. */
213 SIM_RC sim_install_engine (SIM_DESC sd);
216 sim_install_engine (SIM_DESC sd)
218 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
219 sim_module_add_init_fn (sd, sim_engine_init);