sim: namespace sim_machs
[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 sim_model_set (sd, cpu, model);
72 break;
73 }
74
75 case OPTION_MODEL_INFO :
76 {
77 const SIM_MACH * const *machp;
78 const SIM_MODEL *model;
79
80 if (STATE_MACHS (sd) == NULL)
81 {
82 sim_io_printf (sd, "This target does not support any models\n");
83 return SIM_RC_FAIL;
84 }
85
86 for (machp = STATE_MACHS(sd); *machp != NULL; ++machp)
87 {
88 sim_io_printf (sd, "Models for architecture `%s':\n",
89 MACH_NAME (*machp));
90 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL;
91 ++model)
92 sim_io_printf (sd, " %s", MODEL_NAME (model));
93 sim_io_printf (sd, "\n");
94 }
95 break;
96 }
97 }
98
99 return SIM_RC_OK;
100 }
101
102 SIM_RC
103 sim_model_install (SIM_DESC sd)
104 {
105 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
106
107 sim_add_option_table (sd, NULL, model_options);
108 sim_module_add_init_fn (sd, sim_model_init);
109
110 return SIM_RC_OK;
111 }
112
113 /* Subroutine of sim_model_set to set the model for one cpu. */
114
115 static void
116 model_set (sim_cpu *cpu, const SIM_MODEL *model)
117 {
118 CPU_MACH (cpu) = MODEL_MACH (model);
119 CPU_MODEL (cpu) = model;
120 (* MACH_INIT_CPU (MODEL_MACH (model))) (cpu);
121 (* MODEL_INIT (model)) (cpu);
122 }
123
124 /* Set the current model of CPU to MODEL.
125 If CPU is NULL, all cpus are set to MODEL. */
126
127 void
128 sim_model_set (SIM_DESC sd, sim_cpu *cpu, const SIM_MODEL *model)
129 {
130 if (! cpu)
131 {
132 int c;
133
134 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
135 if (STATE_CPU (sd, c))
136 model_set (STATE_CPU (sd, c), model);
137 }
138 else
139 {
140 model_set (cpu, model);
141 }
142 }
143
144 /* Look up model named NAME.
145 Result is pointer to MODEL entry or NULL if not found. */
146
147 const SIM_MODEL *
148 sim_model_lookup (SIM_DESC sd, const char *name)
149 {
150 const SIM_MACH * const *machp;
151 const SIM_MODEL *model;
152
153 if (STATE_MACHS (sd) == NULL)
154 return NULL;
155
156 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
157 {
158 for (model = MACH_MODELS (*machp); MODEL_NAME (model) != NULL; ++model)
159 {
160 if (strcmp (MODEL_NAME (model), name) == 0)
161 return model;
162 }
163 }
164 return NULL;
165 }
166
167 /* Look up machine named NAME.
168 Result is pointer to MACH entry or NULL if not found. */
169
170 const SIM_MACH *
171 sim_mach_lookup (SIM_DESC sd, const char *name)
172 {
173 const SIM_MACH * const *machp;
174
175 if (STATE_MACHS (sd) == NULL)
176 return NULL;
177
178 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
179 {
180 if (strcmp (MACH_NAME (*machp), name) == 0)
181 return *machp;
182 }
183 return NULL;
184 }
185
186 /* Look up a machine via its bfd name.
187 Result is pointer to MACH entry or NULL if not found. */
188
189 const SIM_MACH *
190 sim_mach_lookup_bfd_name (SIM_DESC sd, const char *name)
191 {
192 const SIM_MACH * const *machp;
193
194 if (STATE_MACHS (sd) == NULL)
195 return NULL;
196
197 for (machp = STATE_MACHS (sd); *machp != NULL; ++machp)
198 {
199 if (strcmp (MACH_BFD_NAME (*machp), name) == 0)
200 return *machp;
201 }
202 return NULL;
203 }
204
205 /* Initialize model support. */
206
207 static SIM_RC
208 sim_model_init (SIM_DESC sd)
209 {
210 SIM_CPU *cpu;
211
212 if (!WITH_MODEL_P)
213 return SIM_RC_OK;
214
215 /* If both cpu model and state architecture are set, ensure they're
216 compatible. If only one is set, set the other. If neither are set,
217 use the default model. STATE_ARCHITECTURE is the bfd_arch_info data
218 for the selected "mach" (bfd terminology). */
219
220 /* Only check cpu 0. STATE_ARCHITECTURE is for that one only. */
221 /* ??? At present this only supports homogeneous multiprocessors. */
222 cpu = STATE_CPU (sd, 0);
223
224 if (! STATE_ARCHITECTURE (sd)
225 && ! CPU_MACH (cpu))
226 {
227 /* Set the default model. */
228 const SIM_MODEL *model = sim_model_lookup (sd, WITH_DEFAULT_MODEL);
229 SIM_ASSERT (model != NULL);
230 sim_model_set (sd, NULL, model);
231 }
232
233 if (STATE_ARCHITECTURE (sd)
234 && CPU_MACH (cpu))
235 {
236 if (strcmp (STATE_ARCHITECTURE (sd)->printable_name,
237 MACH_BFD_NAME (CPU_MACH (cpu))) != 0)
238 {
239 sim_io_eprintf (sd, "invalid model `%s' for `%s'\n",
240 MODEL_NAME (CPU_MODEL (cpu)),
241 STATE_ARCHITECTURE (sd)->printable_name);
242 return SIM_RC_FAIL;
243 }
244 }
245 else if (STATE_ARCHITECTURE (sd))
246 {
247 /* Use the default model for the selected machine.
248 The default model is the first one in the list. */
249 const SIM_MACH *mach =
250 sim_mach_lookup_bfd_name (sd, STATE_ARCHITECTURE (sd)->printable_name);
251
252 if (mach == NULL)
253 {
254 sim_io_eprintf (sd, "unsupported machine `%s'\n",
255 STATE_ARCHITECTURE (sd)->printable_name);
256 return SIM_RC_FAIL;
257 }
258 sim_model_set (sd, NULL, MACH_MODELS (mach));
259 }
260 else
261 {
262 STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu)));
263 }
264
265 return SIM_RC_OK;
266 }