Commit | Line | Data |
---|---|---|
c906108c | 1 | /* run front end support for arm |
1e6b544a | 2 | Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc. |
c906108c SS |
3 | |
4 | This file is part of ARM SIM. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | /* This file provides the interface between the simulator and run.c and gdb | |
21 | (when the simulator is linked with gdb). | |
22 | All simulator interaction should go through this file. */ | |
23 | ||
24 | #include <stdio.h> | |
25 | #include <stdarg.h> | |
6d358e86 | 26 | #include <string.h> |
c906108c SS |
27 | #include <bfd.h> |
28 | #include <signal.h> | |
29 | #include "callback.h" | |
30 | #include "remote-sim.h" | |
31 | #include "armdefs.h" | |
32 | #include "armemu.h" | |
33 | #include "dbg_rdi.h" | |
6d358e86 | 34 | #include "ansidecl.h" |
c906108c SS |
35 | |
36 | host_callback *sim_callback; | |
37 | ||
38 | static struct ARMul_State *state; | |
39 | ||
40 | /* Who is using the simulator. */ | |
41 | static SIM_OPEN_KIND sim_kind; | |
42 | ||
43 | /* argv[0] */ | |
44 | static char *myname; | |
45 | ||
46 | /* Memory size in bytes. */ | |
47 | static int mem_size = (1 << 21); | |
48 | ||
49 | /* Non-zero to display start up banner, and maybe other things. */ | |
50 | static int verbosity; | |
51 | ||
52 | /* Non-zero to set big endian mode. */ | |
53 | static int big_endian; | |
54 | ||
7a292a7a SS |
55 | int stop_simulator; |
56 | ||
dfcd3bfb | 57 | static void |
c906108c SS |
58 | init () |
59 | { | |
60 | static int done; | |
61 | ||
62 | if (!done) | |
63 | { | |
dfcd3bfb | 64 | ARMul_EmulateInit (); |
c906108c SS |
65 | state = ARMul_NewState (); |
66 | state->bigendSig = (big_endian ? HIGH : LOW); | |
dfcd3bfb JM |
67 | ARMul_MemoryInit (state, mem_size); |
68 | ARMul_OSInit (state); | |
69 | ARMul_CoProInit (state); | |
c906108c SS |
70 | state->verbose = verbosity; |
71 | done = 1; | |
72 | } | |
73 | } | |
74 | ||
75 | /* Set verbosity level of simulator. | |
76 | This is not intended to produce detailed tracing or debugging information. | |
77 | Just summaries. */ | |
78 | /* FIXME: common/run.c doesn't do this yet. */ | |
79 | ||
80 | void | |
81 | sim_set_verbose (v) | |
82 | int v; | |
83 | { | |
84 | verbosity = v; | |
85 | } | |
86 | ||
87 | /* Set the memory size to SIZE bytes. | |
dfcd3bfb | 88 | Must be called before initializing simulator. */ |
c906108c SS |
89 | /* FIXME: Rename to sim_set_mem_size. */ |
90 | ||
dfcd3bfb | 91 | void |
c906108c SS |
92 | sim_size (size) |
93 | int size; | |
94 | { | |
95 | mem_size = size; | |
96 | } | |
97 | ||
dfcd3bfb JM |
98 | void |
99 | ARMul_ConsolePrint (ARMul_State * state, const char *format, ...) | |
c906108c SS |
100 | { |
101 | va_list ap; | |
102 | ||
103 | if (state->verbose) | |
104 | { | |
105 | va_start (ap, format); | |
106 | vprintf (format, ap); | |
107 | va_end (ap); | |
108 | } | |
109 | } | |
110 | ||
6d358e86 NC |
111 | ARMword |
112 | ARMul_Debug (ARMul_State * state ATTRIBUTE_UNUSED, ARMword pc ATTRIBUTE_UNUSED, ARMword instr ATTRIBUTE_UNUSED) | |
c906108c | 113 | { |
6d358e86 | 114 | return 0; |
c906108c SS |
115 | } |
116 | ||
117 | int | |
118 | sim_write (sd, addr, buffer, size) | |
6d358e86 | 119 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
120 | SIM_ADDR addr; |
121 | unsigned char *buffer; | |
122 | int size; | |
123 | { | |
124 | int i; | |
125 | init (); | |
126 | for (i = 0; i < size; i++) | |
127 | { | |
dfcd3bfb | 128 | ARMul_WriteByte (state, addr + i, buffer[i]); |
c906108c SS |
129 | } |
130 | return size; | |
131 | } | |
132 | ||
133 | int | |
134 | sim_read (sd, addr, buffer, size) | |
6d358e86 | 135 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
136 | SIM_ADDR addr; |
137 | unsigned char *buffer; | |
138 | int size; | |
139 | { | |
140 | int i; | |
141 | init (); | |
142 | for (i = 0; i < size; i++) | |
143 | { | |
144 | buffer[i] = ARMul_ReadByte (state, addr + i); | |
145 | } | |
146 | return size; | |
147 | } | |
148 | ||
149 | int | |
150 | sim_trace (sd) | |
6d358e86 | 151 | SIM_DESC sd ATTRIBUTE_UNUSED; |
dfcd3bfb JM |
152 | { |
153 | (*sim_callback->printf_filtered) (sim_callback, | |
154 | "This simulator does not support tracing\n"); | |
c906108c SS |
155 | return 1; |
156 | } | |
157 | ||
158 | int | |
159 | sim_stop (sd) | |
6d358e86 | 160 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c | 161 | { |
7a292a7a SS |
162 | state->Emulate = STOP; |
163 | stop_simulator = 1; | |
164 | return 1; | |
c906108c SS |
165 | } |
166 | ||
167 | void | |
168 | sim_resume (sd, step, siggnal) | |
6d358e86 NC |
169 | SIM_DESC sd ATTRIBUTE_UNUSED; |
170 | int step; | |
171 | int siggnal ATTRIBUTE_UNUSED; | |
c906108c SS |
172 | { |
173 | state->EndCondition = 0; | |
7a292a7a | 174 | stop_simulator = 0; |
c906108c SS |
175 | |
176 | if (step) | |
177 | { | |
178 | state->Reg[15] = ARMul_DoInstr (state); | |
179 | if (state->EndCondition == 0) | |
180 | state->EndCondition = RDIError_BreakpointReached; | |
181 | } | |
182 | else | |
183 | { | |
dfcd3bfb JM |
184 | #if 1 /* JGS */ |
185 | state->NextInstr = RESUME; /* treat as PC change */ | |
c906108c SS |
186 | #endif |
187 | state->Reg[15] = ARMul_DoProg (state); | |
188 | } | |
189 | ||
190 | FLUSHPIPE; | |
191 | } | |
192 | ||
193 | SIM_RC | |
194 | sim_create_inferior (sd, abfd, argv, env) | |
6d358e86 | 195 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
196 | struct _bfd *abfd; |
197 | char **argv; | |
198 | char **env; | |
199 | { | |
dfcd3bfb | 200 | int argvlen = 0; |
1e6b544a | 201 | int mach; |
c906108c SS |
202 | char **arg; |
203 | ||
204 | if (abfd != NULL) | |
205 | ARMul_SetPC (state, bfd_get_start_address (abfd)); | |
206 | else | |
dfcd3bfb | 207 | ARMul_SetPC (state, 0); /* ??? */ |
c906108c | 208 | |
1e6b544a AO |
209 | mach = bfd_get_mach (abfd); |
210 | ||
211 | switch (mach) { | |
212 | default: | |
213 | (*sim_callback->printf_filtered) (sim_callback, | |
214 | "Unknown machine type; please update sim_create_inferior.\n"); | |
215 | /* fall through */ | |
216 | ||
217 | case 0: /* arm */ | |
218 | /* We wouldn't set the machine type with earlier toolchains, so we | |
219 | explicitly select a processor capable of supporting all ARM | |
220 | 32bit mode. */ | |
221 | /* fall through */ | |
222 | ||
223 | case 5: /* armv4 */ | |
224 | case 6: /* armv4t */ | |
225 | case 7: /* armv5 */ | |
226 | case 8: /* armv5t */ | |
227 | ARMul_SelectProcessor (state, STRONGARM); | |
64a10675 FN |
228 | /* Reset mode to ARM. A gdb user may rerun a program that had entered |
229 | THUMB mode from the start and cause the ARM-mode startup code to be | |
230 | executed in THUMB mode. */ | |
0a4321b9 | 231 | ARMul_SetCPSR (state, USER32MODE); |
1e6b544a AO |
232 | break; |
233 | ||
234 | case 3: /* armv3 */ | |
235 | case 4: /* armv3m */ | |
236 | ARMul_SelectProcessor (state, ARM600); | |
237 | break; | |
238 | ||
239 | case 1: /* armv2 */ | |
240 | case 2: /* armv2a */ | |
241 | ARMul_SelectProcessor (state, ARM2); | |
242 | break; | |
243 | } | |
244 | ||
c906108c SS |
245 | if (argv != NULL) |
246 | { | |
247 | /* | |
dfcd3bfb JM |
248 | ** Set up the command line (by laboriously stringing together the |
249 | ** environment carefully picked apart by our caller...) | |
250 | */ | |
c906108c SS |
251 | /* Free any old stuff */ |
252 | if (state->CommandLine != NULL) | |
253 | { | |
dfcd3bfb | 254 | free (state->CommandLine); |
c906108c SS |
255 | state->CommandLine = NULL; |
256 | } | |
dfcd3bfb | 257 | |
c906108c SS |
258 | /* See how much we need */ |
259 | for (arg = argv; *arg != NULL; arg++) | |
dfcd3bfb JM |
260 | argvlen += strlen (*arg) + 1; |
261 | ||
c906108c | 262 | /* allocate it... */ |
dfcd3bfb | 263 | state->CommandLine = malloc (argvlen + 1); |
c906108c SS |
264 | if (state->CommandLine != NULL) |
265 | { | |
266 | arg = argv; | |
dfcd3bfb | 267 | state->CommandLine[0] = '\0'; |
c906108c SS |
268 | for (arg = argv; *arg != NULL; arg++) |
269 | { | |
dfcd3bfb JM |
270 | strcat (state->CommandLine, *arg); |
271 | strcat (state->CommandLine, " "); | |
c906108c SS |
272 | } |
273 | } | |
274 | } | |
275 | ||
276 | if (env != NULL) | |
277 | { | |
278 | /* Now see if there's a MEMSIZE spec in the environment */ | |
279 | while (*env) | |
280 | { | |
dfcd3bfb | 281 | if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0) |
c906108c | 282 | { |
c906108c | 283 | char *end_of_num; |
dfcd3bfb | 284 | |
c906108c | 285 | /* Set up memory limit */ |
dfcd3bfb JM |
286 | state->MemSize = |
287 | strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0); | |
c906108c SS |
288 | } |
289 | env++; | |
290 | } | |
291 | } | |
292 | ||
293 | return SIM_RC_OK; | |
294 | } | |
295 | ||
296 | void | |
297 | sim_info (sd, verbose) | |
6d358e86 NC |
298 | SIM_DESC sd ATTRIBUTE_UNUSED; |
299 | int verbose ATTRIBUTE_UNUSED; | |
c906108c SS |
300 | { |
301 | } | |
302 | ||
303 | ||
dfcd3bfb | 304 | static int |
c906108c SS |
305 | frommem (state, memory) |
306 | struct ARMul_State *state; | |
307 | unsigned char *memory; | |
308 | { | |
309 | if (state->bigendSig == HIGH) | |
310 | { | |
311 | return (memory[0] << 24) | |
dfcd3bfb | 312 | | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0); |
c906108c SS |
313 | } |
314 | else | |
315 | { | |
316 | return (memory[3] << 24) | |
dfcd3bfb | 317 | | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0); |
c906108c SS |
318 | } |
319 | } | |
320 | ||
321 | ||
322 | static void | |
dfcd3bfb | 323 | tomem (state, memory, val) |
c906108c SS |
324 | struct ARMul_State *state; |
325 | unsigned char *memory; | |
326 | int val; | |
327 | { | |
328 | if (state->bigendSig == HIGH) | |
329 | { | |
330 | memory[0] = val >> 24; | |
331 | memory[1] = val >> 16; | |
332 | memory[2] = val >> 8; | |
333 | memory[3] = val >> 0; | |
334 | } | |
335 | else | |
336 | { | |
337 | memory[3] = val >> 24; | |
338 | memory[2] = val >> 16; | |
339 | memory[1] = val >> 8; | |
340 | memory[0] = val >> 0; | |
341 | } | |
342 | } | |
343 | ||
344 | int | |
345 | sim_store_register (sd, rn, memory, length) | |
6d358e86 | 346 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
347 | int rn; |
348 | unsigned char *memory; | |
6d358e86 | 349 | int length ATTRIBUTE_UNUSED; |
c906108c SS |
350 | { |
351 | init (); | |
3463c3fb NC |
352 | if (rn == 25) |
353 | { | |
354 | state->Cpsr = frommem (state, memory); | |
355 | ARMul_CPSRAltered (state); | |
356 | } | |
357 | else | |
358 | ARMul_SetReg (state, state->Mode, rn, frommem (state, memory)); | |
c906108c SS |
359 | return -1; |
360 | } | |
361 | ||
362 | int | |
363 | sim_fetch_register (sd, rn, memory, length) | |
6d358e86 | 364 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
365 | int rn; |
366 | unsigned char *memory; | |
6d358e86 | 367 | int length ATTRIBUTE_UNUSED; |
c906108c SS |
368 | { |
369 | ARMword regval; | |
370 | ||
371 | init (); | |
372 | if (rn < 16) | |
dfcd3bfb JM |
373 | regval = ARMul_GetReg (state, state->Mode, rn); |
374 | else if (rn == 25) /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */ | |
375 | regval = ARMul_GetCPSR (state); | |
c906108c | 376 | else |
dfcd3bfb | 377 | regval = 0; /* FIXME: should report an error */ |
c906108c SS |
378 | tomem (state, memory, regval); |
379 | return -1; | |
380 | } | |
381 | ||
382 | SIM_DESC | |
383 | sim_open (kind, ptr, abfd, argv) | |
384 | SIM_OPEN_KIND kind; | |
385 | host_callback *ptr; | |
386 | struct _bfd *abfd; | |
387 | char **argv; | |
388 | { | |
389 | sim_kind = kind; | |
6c9e0292 | 390 | if (myname) free (myname); |
c1a72ffd | 391 | myname = (char *) xstrdup (argv[0]); |
c906108c | 392 | sim_callback = ptr; |
dfcd3bfb | 393 | |
c906108c SS |
394 | /* Decide upon the endian-ness of the processor. |
395 | If we can, get the information from the bfd itself. | |
396 | Otherwise look to see if we have been given a command | |
397 | line switch that tells us. Otherwise default to little endian. */ | |
398 | if (abfd != NULL) | |
399 | big_endian = bfd_big_endian (abfd); | |
400 | else if (argv[1] != NULL) | |
401 | { | |
402 | int i; | |
dfcd3bfb | 403 | |
c906108c SS |
404 | /* Scan for endian-ness switch. */ |
405 | for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++) | |
dfcd3bfb JM |
406 | if (argv[i][0] == '-' && argv[i][1] == 'E') |
407 | { | |
408 | char c; | |
409 | ||
410 | if ((c = argv[i][2]) == 0) | |
411 | { | |
412 | ++i; | |
413 | c = argv[i][0]; | |
414 | } | |
415 | ||
416 | switch (c) | |
417 | { | |
418 | case 0: | |
419 | sim_callback->printf_filtered | |
420 | (sim_callback, "No argument to -E option provided\n"); | |
421 | break; | |
422 | ||
423 | case 'b': | |
424 | case 'B': | |
425 | big_endian = 1; | |
426 | break; | |
427 | ||
428 | case 'l': | |
429 | case 'L': | |
430 | big_endian = 0; | |
431 | break; | |
432 | ||
433 | default: | |
434 | sim_callback->printf_filtered | |
435 | (sim_callback, "Unrecognised argument to -E option\n"); | |
436 | break; | |
437 | } | |
438 | } | |
c906108c SS |
439 | } |
440 | ||
441 | return (SIM_DESC) 1; | |
442 | } | |
443 | ||
444 | void | |
445 | sim_close (sd, quitting) | |
6d358e86 NC |
446 | SIM_DESC sd ATTRIBUTE_UNUSED; |
447 | int quitting ATTRIBUTE_UNUSED; | |
c906108c | 448 | { |
6c9e0292 FCE |
449 | if (myname) free (myname); |
450 | myname = NULL; | |
c906108c SS |
451 | } |
452 | ||
453 | SIM_RC | |
454 | sim_load (sd, prog, abfd, from_tty) | |
455 | SIM_DESC sd; | |
456 | char *prog; | |
457 | bfd *abfd; | |
6d358e86 | 458 | int from_tty ATTRIBUTE_UNUSED; |
c906108c | 459 | { |
dfcd3bfb | 460 | extern bfd *sim_load_file (); /* ??? Don't know where this should live. */ |
c906108c SS |
461 | bfd *prog_bfd; |
462 | ||
463 | prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd, | |
dfcd3bfb | 464 | sim_kind == SIM_OPEN_DEBUG, 0, sim_write); |
c906108c SS |
465 | if (prog_bfd == NULL) |
466 | return SIM_RC_FAIL; | |
467 | ARMul_SetPC (state, bfd_get_start_address (prog_bfd)); | |
468 | if (abfd == NULL) | |
469 | bfd_close (prog_bfd); | |
470 | return SIM_RC_OK; | |
471 | } | |
472 | ||
473 | void | |
474 | sim_stop_reason (sd, reason, sigrc) | |
6d358e86 | 475 | SIM_DESC sd ATTRIBUTE_UNUSED; |
c906108c SS |
476 | enum sim_stop *reason; |
477 | int *sigrc; | |
478 | { | |
7a292a7a SS |
479 | if (stop_simulator) |
480 | { | |
481 | *reason = sim_stopped; | |
482 | *sigrc = SIGINT; | |
483 | } | |
484 | else if (state->EndCondition == 0) | |
c906108c SS |
485 | { |
486 | *reason = sim_exited; | |
487 | *sigrc = state->Reg[0] & 255; | |
488 | } | |
489 | else | |
490 | { | |
491 | *reason = sim_stopped; | |
492 | if (state->EndCondition == RDIError_BreakpointReached) | |
493 | *sigrc = SIGTRAP; | |
494 | else | |
495 | *sigrc = 0; | |
496 | } | |
497 | } | |
498 | ||
499 | void | |
500 | sim_do_command (sd, cmd) | |
6d358e86 NC |
501 | SIM_DESC sd ATTRIBUTE_UNUSED; |
502 | char *cmd ATTRIBUTE_UNUSED; | |
dfcd3bfb JM |
503 | { |
504 | (*sim_callback->printf_filtered) (sim_callback, | |
505 | "This simulator does not accept any commands.\n"); | |
c906108c SS |
506 | } |
507 | ||
508 | ||
509 | void | |
510 | sim_set_callbacks (ptr) | |
511 | host_callback *ptr; | |
512 | { | |
513 | sim_callback = ptr; | |
514 | } |