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