]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/sim-model.c
sim: move default model to the runtime sim state
[thirdparty/binutils-gdb.git] / sim / common / sim-model.c
1 /* Model support.
2 Copyright (C) 1996-2021 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-model.h"
25 #include "libiberty.h"
26 #include "sim-options.h"
27 #include "sim-io.h"
28 #include "sim-assert.h"
29 #include "bfd.h"
30
31 static void model_set (sim_cpu *, const SIM_MODEL *);
32
33 static DECLARE_OPTION_HANDLER (model_option_handler);
34
35 static MODULE_INIT_FN sim_model_init;
36
37 enum {
38 OPTION_MODEL = OPTION_START,
39 OPTION_MODEL_INFO,
40 };
41
42 static const OPTION model_options[] = {
43 { {"model", required_argument, NULL, OPTION_MODEL},
44 '\0', "MODEL", "Specify model to simulate",
45 model_option_handler, NULL },
46
47 { {"model-info", no_argument, NULL, OPTION_MODEL_INFO},
48 '\0', NULL, "List selectable models",
49 model_option_handler, NULL },
50 { {"info-model", no_argument, NULL, OPTION_MODEL_INFO},
51 '\0', NULL, NULL,
52 model_option_handler, NULL },
53
54 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
55 };
56
57 static SIM_RC
58 model_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
59 char *arg, int is_command)
60 {
61 switch (opt)
62 {
63 case OPTION_MODEL :
64 {
65 const SIM_MODEL *model = sim_model_lookup (sd, arg);
66 if (! model)
67 {
68 sim_io_eprintf (sd, "unknown model `%s'\n", arg);
69 return SIM_RC_FAIL;
70 }
71 STATE_MODEL_NAME (sd) = arg;
72 sim_model_set (sd, cpu, model);
73 break;
74 }
75
76 case OPTION_MODEL_INFO :
77 {
78 const SIM_MACH * const *machp;
79 const SIM_MODEL *model;
80
81 if (STATE_MACHS (sd) == NULL)
82 {
83 sim_io_printf (sd, "This target does not support any models\n");
84 return SIM_RC_FAIL;
85 }
86
87 for (machp = STATE_MACHS(sd); *machp != NULL; ++machp)
88 {
89 sim_io_printf (sd, "Models for architecture `%s':\n",
90 MACH_NAME (*machp));
91 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL;
92 ++model)
93 sim_io_printf (sd, " %s", MODEL_NAME (model));
94 sim_io_printf (sd, "\n");
95 }
96 break;
97 }
98 }
99
100 return SIM_RC_OK;
101 }
102
103 SIM_RC
104 sim_model_install (SIM_DESC sd)
105 {
106 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
107
108 sim_add_option_table (sd, NULL, model_options);
109 sim_module_add_init_fn (sd, sim_model_init);
110
111 return SIM_RC_OK;
112 }
113
114 /* Subroutine of sim_model_set to set the model for one cpu. */
115
116 static void
117 model_set (sim_cpu *cpu, const SIM_MODEL *model)
118 {
119 CPU_MACH (cpu) = MODEL_MACH (model);
120 CPU_MODEL (cpu) = model;
121 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
122 (* MODEL_INIT (model)) (cpu);
123 }
124
125 /* Set the current model of CPU to MODEL.
126 If CPU is NULL, all cpus are set to MODEL. */
127
128 void
129 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const SIM_MODEL *model)
130 {
131 if (! cpu)
132 {
133 int c;
134
135 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
136 if (STATE_CPU (sd, c))
137 model_set (STATE_CPU (sd, c), model);
138 }
139 else
140 {
141 model_set (cpu, model);
142 }
143 }
144
145 /* Look up model named NAME.
146 Result is pointer to MODEL entry or NULL if not found. */
147
148 const SIM_MODEL *
149 sim_model_lookup (SIM_DESC sd, const char *name)
150 {
151 const SIM_MACH * const *machp;
152 const SIM_MODEL *model;
153
154 if (STATE_MACHS (sd) == NULL)
155 return NULL;
156
157 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
158 {
159 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
160 {
161 if (strcmp (MODEL_NAME (model), name) == 0)
162 return model;
163 }
164 }
165 return NULL;
166 }
167
168 /* Look up machine named NAME.
169 Result is pointer to MACH entry or NULL if not found. */
170
171 const SIM_MACH *
172 sim_mach_lookup (SIM_DESC sd, const char *name)
173 {
174 const SIM_MACH * const *machp;
175
176 if (STATE_MACHS (sd) == NULL)
177 return NULL;
178
179 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
180 {
181 if (strcmp (MACH_NAME (*machp), name) == 0)
182 return *machp;
183 }
184 return NULL;
185 }
186
187 /* Look up a machine via its bfd name.
188 Result is pointer to MACH entry or NULL if not found. */
189
190 const SIM_MACH *
191 sim_mach_lookup_bfd_name (SIM_DESC sd, const char *name)
192 {
193 const SIM_MACH * const *machp;
194
195 if (STATE_MACHS (sd) == NULL)
196 return NULL;
197
198 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
199 {
200 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
201 return *machp;
202 }
203 return NULL;
204 }
205
206 /* Initialize model support. */
207
208 static SIM_RC
209 sim_model_init (SIM_DESC sd)
210 {
211 SIM_CPU *cpu;
212
213 /* If both cpu model and state architecture are set, ensure they're
214 compatible. If only one is set, set the other. If neither are set,
215 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
216 for the selected "mach" (bfd terminology). */
217
218 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
219 /* ??? At present this only supports homogeneous multiprocessors. */
220 cpu = STATE_CPU (sd, 0);
221
222 if (! STATE_ARCHITECTURE (sd)
223 && ! CPU_MACH (cpu)
224 && STATE_MODEL_NAME (sd))
225 {
226 /* Set the default model. */
227 const SIM_MODEL *model = sim_model_lookup (sd, STATE_MODEL_NAME (sd));
228 SIM_ASSERT (model != NULL);
229 sim_model_set (sd, NULL, model);
230 }
231
232 if (STATE_ARCHITECTURE (sd)
233 && CPU_MACH (cpu))
234 {
235 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
236 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
237 {
238 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
239 MODEL_NAME (CPU_MODEL (cpu)),
240 STATE_ARCHITECTURE (sd)->printable_name);
241 return SIM_RC_FAIL;
242 }
243 }
244 else if (STATE_ARCHITECTURE (sd) && STATE_MACHS (sd))
245 {
246 /* Use the default model for the selected machine.
247 The default model is the first one in the list. */
248 const SIM_MACH *mach =
249 sim_mach_lookup_bfd_name (sd, STATE_ARCHITECTURE (sd)->printable_name);
250
251 if (mach == NULL)
252 {
253 sim_io_eprintf (sd, "unsupported machine `%s'\n",
254 STATE_ARCHITECTURE (sd)->printable_name);
255 return SIM_RC_FAIL;
256 }
257 sim_model_set (sd, NULL, MACH_MODELS (mach));
258 }
259 else if (CPU_MACH (cpu))
260 {
261 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
262 }
263
264 return SIM_RC_OK;
265 }