Fix whitespace snafu in tc-riscv.c
[binutils-gdb.git] / sim / common / sim-engine.c
blobfbdc6ebc5d2a52db7302c35c96999c1abd173571
1 /* Generic simulator halt/restart.
2 Copyright (C) 1997-2023 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. */
21 #include "defs.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "sim-main.h"
28 #include "sim-assert.h"
29 #include "sim-signal.h"
31 /* Get the run state.
32 REASON/SIGRC are the values returned by sim_stop_reason.
33 ??? Should each cpu have its own copy? */
35 void
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? */
48 void
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;
57 /* Generic halt */
59 void
60 sim_engine_halt (SIM_DESC sd,
61 sim_cpu *last_cpu,
62 sim_cpu *next_cpu, /* NULL - use default */
63 sim_cia cia,
64 enum sim_stop reason,
65 int sigrc)
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));
82 #endif
84 longjmp (*halt_buf, sim_engine_halt_jmpval);
86 else
88 sim_io_error (sd, "sim_halt - bad long jump");
89 abort ();
94 /* Generic restart */
96 void
97 sim_engine_restart (SIM_DESC sd,
98 sim_cpu *last_cpu,
99 sim_cpu *next_cpu,
100 sim_cia cia)
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);
112 else
113 sim_io_error (sd, "sim_restart - bad long jump");
117 /* Generic error code */
119 void
120 sim_engine_vabort (SIM_DESC sd,
121 sim_cpu *cpu,
122 sim_cia cia,
123 const char *fmt,
124 va_list ap)
126 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
127 if (sd == NULL)
129 vfprintf (stderr, fmt, ap);
130 fprintf (stderr, "\nQuit\n");
131 abort ();
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");
138 abort ();
140 else
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);
148 void
149 sim_engine_abort (SIM_DESC sd,
150 sim_cpu *cpu,
151 sim_cia cia,
152 const char *fmt,
153 ...)
155 va_list ap;
156 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
157 va_start (ap, fmt);
158 sim_engine_vabort (sd, cpu, cia, fmt, ap);
159 va_end (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);
171 else
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);
181 else
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;
195 /* Initialization */
197 static SIM_RC
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;
207 engine->sigrc = 0;
208 engine->stepper = NULL; /* sim_events_init will clean it up */
209 return SIM_RC_OK;
212 /* Provide a prototype to silence -Wmissing-prototypes. */
213 SIM_RC sim_install_engine (SIM_DESC sd);
215 SIM_RC
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);
220 return SIM_RC_OK;