Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / sim / common / sim-engine.c
1 /* Generic simulator halt/restart.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
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.
11
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.
16
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/>. */
19
20 /* This must come before any other includes. */
21 #include "defs.h"
22
23 #include "sim-main.h"
24 #include "sim-assert.h"
25 #include "sim-signal.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 /* Get the run state.
31 REASON/SIGRC are the values returned by sim_stop_reason.
32 ??? Should each cpu have its own copy? */
33
34 void
35 sim_engine_get_run_state (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
36 {
37 sim_engine *engine = STATE_ENGINE (sd);
38 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
39 *reason = engine->reason;
40 *sigrc = engine->sigrc;
41 }
42
43 /* Set the run state to REASON/SIGRC.
44 REASON/SIGRC are the values returned by sim_stop_reason.
45 ??? Should each cpu have its own copy? */
46
47 void
48 sim_engine_set_run_state (SIM_DESC sd, enum sim_stop reason, int sigrc)
49 {
50 sim_engine *engine = STATE_ENGINE (sd);
51 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
52 engine->reason = reason;
53 engine->sigrc = sigrc;
54 }
55
56 /* Generic halt */
57
58 void
59 sim_engine_halt (SIM_DESC sd,
60 sim_cpu *last_cpu,
61 sim_cpu *next_cpu, /* NULL - use default */
62 sim_cia cia,
63 enum sim_stop reason,
64 int sigrc)
65 {
66 sim_engine *engine = STATE_ENGINE (sd);
67 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
68 if (engine->jmpbuf != NULL)
69 {
70 jmp_buf *halt_buf = engine->jmpbuf;
71 engine->last_cpu = last_cpu;
72 engine->next_cpu = next_cpu;
73 engine->reason = reason;
74 engine->sigrc = sigrc;
75
76 SIM_ENGINE_HALT_HOOK (sd, last_cpu, cia);
77
78 #ifdef SIM_CPU_EXCEPTION_SUSPEND
79 if (last_cpu != NULL && reason != sim_exited)
80 SIM_CPU_EXCEPTION_SUSPEND (sd, last_cpu, sim_signal_to_host (sd, sigrc));
81 #endif
82
83 longjmp (*halt_buf, sim_engine_halt_jmpval);
84 }
85 else
86 {
87 sim_io_error (sd, "sim_halt - bad long jump");
88 abort ();
89 }
90 }
91
92
93 /* Generic restart */
94
95 void
96 sim_engine_restart (SIM_DESC sd,
97 sim_cpu *last_cpu,
98 sim_cpu *next_cpu,
99 sim_cia cia)
100 {
101 sim_engine *engine = STATE_ENGINE (sd);
102 ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
103 if (engine->jmpbuf != NULL)
104 {
105 jmp_buf *halt_buf = engine->jmpbuf;
106 engine->last_cpu = last_cpu;
107 engine->next_cpu = next_cpu;
108 SIM_ENGINE_RESTART_HOOK (sd, last_cpu, cia);
109 longjmp (*halt_buf, sim_engine_restart_jmpval);
110 }
111 else
112 sim_io_error (sd, "sim_restart - bad long jump");
113 }
114
115
116 /* Generic error code */
117
118 void
119 sim_engine_vabort (SIM_DESC sd,
120 sim_cpu *cpu,
121 sim_cia cia,
122 const char *fmt,
123 va_list ap)
124 {
125 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
126 if (sd == NULL)
127 {
128 vfprintf (stderr, fmt, ap);
129 fprintf (stderr, "\nQuit\n");
130 abort ();
131 }
132 else if (STATE_ENGINE (sd)->jmpbuf == NULL)
133 {
134 sim_io_evprintf (sd, fmt, ap);
135 sim_io_eprintf (sd, "\n");
136 sim_io_error (sd, "Quit Simulator");
137 abort ();
138 }
139 else
140 {
141 sim_io_evprintf (sd, fmt, ap);
142 sim_io_eprintf (sd, "\n");
143 sim_engine_halt (sd, cpu, NULL, cia, sim_stopped, SIM_SIGABRT);
144 }
145 }
146
147 void
148 sim_engine_abort (SIM_DESC sd,
149 sim_cpu *cpu,
150 sim_cia cia,
151 const char *fmt,
152 ...)
153 {
154 va_list ap;
155 ASSERT (sd == NULL || STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
156 va_start (ap, fmt);
157 sim_engine_vabort (sd, cpu, cia, fmt, ap);
158 va_end (ap);
159 }
160
161
162 /* Generic next/last cpu */
163
164 int
165 sim_engine_last_cpu_nr (SIM_DESC sd)
166 {
167 sim_engine *engine = STATE_ENGINE (sd);
168 if (engine->last_cpu != NULL)
169 return engine->last_cpu - STATE_CPU (sd, 0);
170 else
171 return MAX_NR_PROCESSORS;
172 }
173
174 int
175 sim_engine_next_cpu_nr (SIM_DESC sd)
176 {
177 sim_engine *engine = STATE_ENGINE (sd);
178 if (engine->next_cpu != NULL)
179 return engine->next_cpu - STATE_CPU (sd, 0);
180 else
181 return sim_engine_last_cpu_nr (sd) + 1;
182 }
183
184 int
185 sim_engine_nr_cpus (SIM_DESC sd)
186 {
187 sim_engine *engine = STATE_ENGINE (sd);
188 return engine->nr_cpus;
189 }
190
191
192
193
194 /* Initialization */
195
196 static SIM_RC
197 sim_engine_init (SIM_DESC sd)
198 {
199 /* initialize the start/stop/resume engine */
200 sim_engine *engine = STATE_ENGINE (sd);
201 engine->jmpbuf = NULL;
202 engine->last_cpu = NULL;
203 engine->next_cpu = NULL;
204 engine->nr_cpus = MAX_NR_PROCESSORS;
205 engine->reason = sim_running;
206 engine->sigrc = 0;
207 engine->stepper = NULL; /* sim_events_init will clean it up */
208 return SIM_RC_OK;
209 }
210
211 /* Provide a prototype to silence -Wmissing-prototypes. */
212 SIM_RC sim_install_engine (SIM_DESC sd);
213
214 SIM_RC
215 sim_install_engine (SIM_DESC sd)
216 {
217 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
218 sim_module_add_init_fn (sd, sim_engine_init);
219 return SIM_RC_OK;
220 }
This page took 0.037872 seconds and 4 git commands to generate.