Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Model support. |
88b9d363 | 2 | Copyright (C) 1996-2022 Free Software Foundation, Inc. |
c906108c SS |
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 | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
c906108c SS |
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 | ||
4744ac1b JB |
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/>. */ | |
c906108c | 19 | |
6df01ab8 MF |
20 | /* This must come before any other includes. */ |
21 | #include "defs.h" | |
22 | ||
c906108c | 23 | #include "sim-main.h" |
fd87baa9 | 24 | #include "sim-model.h" |
c906108c SS |
25 | #include "libiberty.h" |
26 | #include "sim-options.h" | |
27 | #include "sim-io.h" | |
28 | #include "sim-assert.h" | |
29 | #include "bfd.h" | |
30 | ||
8a0ebee6 | 31 | static void model_set (sim_cpu *, const SIM_MODEL *); |
c906108c SS |
32 | |
33 | static DECLARE_OPTION_HANDLER (model_option_handler); | |
34 | ||
35 | static MODULE_INIT_FN sim_model_init; | |
36 | ||
bd0bd508 MF |
37 | enum { |
38 | OPTION_MODEL = OPTION_START, | |
39 | OPTION_MODEL_INFO, | |
40 | }; | |
c906108c SS |
41 | |
42 | static const OPTION model_options[] = { | |
43 | { {"model", required_argument, NULL, OPTION_MODEL}, | |
44 | '\0', "MODEL", "Specify model to simulate", | |
21cf617c | 45 | model_option_handler, NULL }, |
bd0bd508 MF |
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 | ||
21cf617c | 54 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL } |
c906108c SS |
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 | { | |
1c636da0 | 65 | const SIM_MODEL *model = sim_model_lookup (sd, arg); |
c906108c SS |
66 | if (! model) |
67 | { | |
53a5351d | 68 | sim_io_eprintf (sd, "unknown model `%s'\n", arg); |
c906108c SS |
69 | return SIM_RC_FAIL; |
70 | } | |
d414eb3e | 71 | STATE_MODEL_NAME (sd) = arg; |
c906108c SS |
72 | sim_model_set (sd, cpu, model); |
73 | break; | |
74 | } | |
bd0bd508 MF |
75 | |
76 | case OPTION_MODEL_INFO : | |
77 | { | |
ba966652 | 78 | const SIM_MACH * const *machp; |
8a0ebee6 | 79 | const SIM_MODEL *model; |
1c636da0 MF |
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) | |
bd0bd508 MF |
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 | } | |
c906108c SS |
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 | |
8a0ebee6 | 117 | model_set (sim_cpu *cpu, const SIM_MODEL *model) |
c906108c SS |
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 | |
8a0ebee6 | 129 | sim_model_set (SIM_DESC sd, sim_cpu *cpu, const SIM_MODEL *model) |
c906108c SS |
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 | ||
8a0ebee6 | 148 | const SIM_MODEL * |
1c636da0 | 149 | sim_model_lookup (SIM_DESC sd, const char *name) |
c906108c | 150 | { |
ba966652 | 151 | const SIM_MACH * const *machp; |
8a0ebee6 | 152 | const SIM_MODEL *model; |
c906108c | 153 | |
1c636da0 MF |
154 | if (STATE_MACHS (sd) == NULL) |
155 | return NULL; | |
156 | ||
157 | for (machp = STATE_MACHS (sd); *machp != NULL; ++machp) | |
c906108c SS |
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 | ||
8a0ebee6 | 171 | const SIM_MACH * |
1c636da0 | 172 | sim_mach_lookup (SIM_DESC sd, const char *name) |
c906108c | 173 | { |
ba966652 | 174 | const SIM_MACH * const *machp; |
c906108c | 175 | |
1c636da0 MF |
176 | if (STATE_MACHS (sd) == NULL) |
177 | return NULL; | |
178 | ||
179 | for (machp = STATE_MACHS (sd); *machp != NULL; ++machp) | |
c906108c SS |
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 | ||
8a0ebee6 | 190 | const SIM_MACH * |
1c636da0 | 191 | sim_mach_lookup_bfd_name (SIM_DESC sd, const char *name) |
c906108c | 192 | { |
ba966652 | 193 | const SIM_MACH * const *machp; |
c906108c | 194 | |
1c636da0 MF |
195 | if (STATE_MACHS (sd) == NULL) |
196 | return NULL; | |
197 | ||
198 | for (machp = STATE_MACHS (sd); *machp != NULL; ++machp) | |
c906108c SS |
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) | |
d414eb3e MF |
223 | && ! CPU_MACH (cpu) |
224 | && STATE_MODEL_NAME (sd)) | |
c906108c SS |
225 | { |
226 | /* Set the default model. */ | |
d414eb3e | 227 | const SIM_MODEL *model = sim_model_lookup (sd, STATE_MODEL_NAME (sd)); |
ccd4b295 | 228 | SIM_ASSERT (model != NULL); |
c906108c SS |
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 | } | |
d414eb3e | 244 | else if (STATE_ARCHITECTURE (sd) && STATE_MACHS (sd)) |
c906108c SS |
245 | { |
246 | /* Use the default model for the selected machine. | |
247 | The default model is the first one in the list. */ | |
1c636da0 MF |
248 | const SIM_MACH *mach = |
249 | sim_mach_lookup_bfd_name (sd, STATE_ARCHITECTURE (sd)->printable_name); | |
9846de1b JM |
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 | } | |
c906108c SS |
257 | sim_model_set (sd, NULL, MACH_MODELS (mach)); |
258 | } | |
d414eb3e | 259 | else if (CPU_MACH (cpu)) |
c906108c SS |
260 | { |
261 | STATE_ARCHITECTURE (sd) = bfd_scan_arch (MACH_BFD_NAME (CPU_MACH (cpu))); | |
262 | } | |
263 | ||
264 | return SIM_RC_OK; | |
265 | } |