| 1 | /* run front end support for arm |
| 2 | Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
| 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> |
| 26 | #include <string.h> |
| 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" |
| 34 | #include "ansidecl.h" |
| 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 | |
| 55 | int stop_simulator; |
| 56 | |
| 57 | static void |
| 58 | init () |
| 59 | { |
| 60 | static int done; |
| 61 | |
| 62 | if (!done) |
| 63 | { |
| 64 | ARMul_EmulateInit (); |
| 65 | state = ARMul_NewState (); |
| 66 | state->bigendSig = (big_endian ? HIGH : LOW); |
| 67 | ARMul_MemoryInit (state, mem_size); |
| 68 | ARMul_OSInit (state); |
| 69 | ARMul_CoProInit (state); |
| 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. |
| 88 | Must be called before initializing simulator. */ |
| 89 | /* FIXME: Rename to sim_set_mem_size. */ |
| 90 | |
| 91 | void |
| 92 | sim_size (size) |
| 93 | int size; |
| 94 | { |
| 95 | mem_size = size; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | ARMul_ConsolePrint (ARMul_State * state, const char *format, ...) |
| 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 | |
| 111 | ARMword |
| 112 | ARMul_Debug (ARMul_State * state ATTRIBUTE_UNUSED, ARMword pc ATTRIBUTE_UNUSED, ARMword instr ATTRIBUTE_UNUSED) |
| 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int |
| 118 | sim_write (sd, addr, buffer, size) |
| 119 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 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 | { |
| 128 | ARMul_WriteByte (state, addr + i, buffer[i]); |
| 129 | } |
| 130 | return size; |
| 131 | } |
| 132 | |
| 133 | int |
| 134 | sim_read (sd, addr, buffer, size) |
| 135 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 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) |
| 151 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 152 | { |
| 153 | (*sim_callback->printf_filtered) (sim_callback, |
| 154 | "This simulator does not support tracing\n"); |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | int |
| 159 | sim_stop (sd) |
| 160 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 161 | { |
| 162 | state->Emulate = STOP; |
| 163 | stop_simulator = 1; |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | sim_resume (sd, step, siggnal) |
| 169 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 170 | int step; |
| 171 | int siggnal ATTRIBUTE_UNUSED; |
| 172 | { |
| 173 | state->EndCondition = 0; |
| 174 | stop_simulator = 0; |
| 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 | { |
| 184 | #if 1 /* JGS */ |
| 185 | state->NextInstr = RESUME; /* treat as PC change */ |
| 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) |
| 195 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 196 | struct _bfd *abfd; |
| 197 | char **argv; |
| 198 | char **env; |
| 199 | { |
| 200 | int argvlen = 0; |
| 201 | char **arg; |
| 202 | |
| 203 | if (abfd != NULL) |
| 204 | ARMul_SetPC (state, bfd_get_start_address (abfd)); |
| 205 | else |
| 206 | ARMul_SetPC (state, 0); /* ??? */ |
| 207 | |
| 208 | #if 1 /* JGS */ |
| 209 | /* We explicitly select a processor capable of supporting the ARM |
| 210 | 32bit mode, and then we force the simulated CPU into the 32bit |
| 211 | User mode: */ |
| 212 | ARMul_SelectProcessor (state, ARM600); |
| 213 | ARMul_SetCPSR (state, USER32MODE); |
| 214 | #endif |
| 215 | |
| 216 | if (argv != NULL) |
| 217 | { |
| 218 | /* |
| 219 | ** Set up the command line (by laboriously stringing together the |
| 220 | ** environment carefully picked apart by our caller...) |
| 221 | */ |
| 222 | /* Free any old stuff */ |
| 223 | if (state->CommandLine != NULL) |
| 224 | { |
| 225 | free (state->CommandLine); |
| 226 | state->CommandLine = NULL; |
| 227 | } |
| 228 | |
| 229 | /* See how much we need */ |
| 230 | for (arg = argv; *arg != NULL; arg++) |
| 231 | argvlen += strlen (*arg) + 1; |
| 232 | |
| 233 | /* allocate it... */ |
| 234 | state->CommandLine = malloc (argvlen + 1); |
| 235 | if (state->CommandLine != NULL) |
| 236 | { |
| 237 | arg = argv; |
| 238 | state->CommandLine[0] = '\0'; |
| 239 | for (arg = argv; *arg != NULL; arg++) |
| 240 | { |
| 241 | strcat (state->CommandLine, *arg); |
| 242 | strcat (state->CommandLine, " "); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (env != NULL) |
| 248 | { |
| 249 | /* Now see if there's a MEMSIZE spec in the environment */ |
| 250 | while (*env) |
| 251 | { |
| 252 | if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0) |
| 253 | { |
| 254 | char *end_of_num; |
| 255 | |
| 256 | /* Set up memory limit */ |
| 257 | state->MemSize = |
| 258 | strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0); |
| 259 | } |
| 260 | env++; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return SIM_RC_OK; |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | sim_info (sd, verbose) |
| 269 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 270 | int verbose ATTRIBUTE_UNUSED; |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static int |
| 276 | frommem (state, memory) |
| 277 | struct ARMul_State *state; |
| 278 | unsigned char *memory; |
| 279 | { |
| 280 | if (state->bigendSig == HIGH) |
| 281 | { |
| 282 | return (memory[0] << 24) |
| 283 | | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | return (memory[3] << 24) |
| 288 | | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | |
| 293 | static void |
| 294 | tomem (state, memory, val) |
| 295 | struct ARMul_State *state; |
| 296 | unsigned char *memory; |
| 297 | int val; |
| 298 | { |
| 299 | if (state->bigendSig == HIGH) |
| 300 | { |
| 301 | memory[0] = val >> 24; |
| 302 | memory[1] = val >> 16; |
| 303 | memory[2] = val >> 8; |
| 304 | memory[3] = val >> 0; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | memory[3] = val >> 24; |
| 309 | memory[2] = val >> 16; |
| 310 | memory[1] = val >> 8; |
| 311 | memory[0] = val >> 0; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | int |
| 316 | sim_store_register (sd, rn, memory, length) |
| 317 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 318 | int rn; |
| 319 | unsigned char *memory; |
| 320 | int length ATTRIBUTE_UNUSED; |
| 321 | { |
| 322 | init (); |
| 323 | ARMul_SetReg (state, state->Mode, rn, frommem (state, memory)); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | int |
| 328 | sim_fetch_register (sd, rn, memory, length) |
| 329 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 330 | int rn; |
| 331 | unsigned char *memory; |
| 332 | int length ATTRIBUTE_UNUSED; |
| 333 | { |
| 334 | ARMword regval; |
| 335 | |
| 336 | init (); |
| 337 | if (rn < 16) |
| 338 | regval = ARMul_GetReg (state, state->Mode, rn); |
| 339 | else if (rn == 25) /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */ |
| 340 | regval = ARMul_GetCPSR (state); |
| 341 | else |
| 342 | regval = 0; /* FIXME: should report an error */ |
| 343 | tomem (state, memory, regval); |
| 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | SIM_DESC |
| 348 | sim_open (kind, ptr, abfd, argv) |
| 349 | SIM_OPEN_KIND kind; |
| 350 | host_callback *ptr; |
| 351 | struct _bfd *abfd; |
| 352 | char **argv; |
| 353 | { |
| 354 | sim_kind = kind; |
| 355 | myname = argv[0]; |
| 356 | sim_callback = ptr; |
| 357 | |
| 358 | /* Decide upon the endian-ness of the processor. |
| 359 | If we can, get the information from the bfd itself. |
| 360 | Otherwise look to see if we have been given a command |
| 361 | line switch that tells us. Otherwise default to little endian. */ |
| 362 | if (abfd != NULL) |
| 363 | big_endian = bfd_big_endian (abfd); |
| 364 | else if (argv[1] != NULL) |
| 365 | { |
| 366 | int i; |
| 367 | |
| 368 | /* Scan for endian-ness switch. */ |
| 369 | for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++) |
| 370 | if (argv[i][0] == '-' && argv[i][1] == 'E') |
| 371 | { |
| 372 | char c; |
| 373 | |
| 374 | if ((c = argv[i][2]) == 0) |
| 375 | { |
| 376 | ++i; |
| 377 | c = argv[i][0]; |
| 378 | } |
| 379 | |
| 380 | switch (c) |
| 381 | { |
| 382 | case 0: |
| 383 | sim_callback->printf_filtered |
| 384 | (sim_callback, "No argument to -E option provided\n"); |
| 385 | break; |
| 386 | |
| 387 | case 'b': |
| 388 | case 'B': |
| 389 | big_endian = 1; |
| 390 | break; |
| 391 | |
| 392 | case 'l': |
| 393 | case 'L': |
| 394 | big_endian = 0; |
| 395 | break; |
| 396 | |
| 397 | default: |
| 398 | sim_callback->printf_filtered |
| 399 | (sim_callback, "Unrecognised argument to -E option\n"); |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return (SIM_DESC) 1; |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | sim_close (sd, quitting) |
| 410 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 411 | int quitting ATTRIBUTE_UNUSED; |
| 412 | { |
| 413 | /* nothing to do */ |
| 414 | } |
| 415 | |
| 416 | SIM_RC |
| 417 | sim_load (sd, prog, abfd, from_tty) |
| 418 | SIM_DESC sd; |
| 419 | char *prog; |
| 420 | bfd *abfd; |
| 421 | int from_tty ATTRIBUTE_UNUSED; |
| 422 | { |
| 423 | extern bfd *sim_load_file (); /* ??? Don't know where this should live. */ |
| 424 | bfd *prog_bfd; |
| 425 | |
| 426 | prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd, |
| 427 | sim_kind == SIM_OPEN_DEBUG, 0, sim_write); |
| 428 | if (prog_bfd == NULL) |
| 429 | return SIM_RC_FAIL; |
| 430 | ARMul_SetPC (state, bfd_get_start_address (prog_bfd)); |
| 431 | if (abfd == NULL) |
| 432 | bfd_close (prog_bfd); |
| 433 | return SIM_RC_OK; |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | sim_stop_reason (sd, reason, sigrc) |
| 438 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 439 | enum sim_stop *reason; |
| 440 | int *sigrc; |
| 441 | { |
| 442 | if (stop_simulator) |
| 443 | { |
| 444 | *reason = sim_stopped; |
| 445 | *sigrc = SIGINT; |
| 446 | } |
| 447 | else if (state->EndCondition == 0) |
| 448 | { |
| 449 | *reason = sim_exited; |
| 450 | *sigrc = state->Reg[0] & 255; |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | *reason = sim_stopped; |
| 455 | if (state->EndCondition == RDIError_BreakpointReached) |
| 456 | *sigrc = SIGTRAP; |
| 457 | else |
| 458 | *sigrc = 0; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | sim_do_command (sd, cmd) |
| 464 | SIM_DESC sd ATTRIBUTE_UNUSED; |
| 465 | char *cmd ATTRIBUTE_UNUSED; |
| 466 | { |
| 467 | (*sim_callback->printf_filtered) (sim_callback, |
| 468 | "This simulator does not accept any commands.\n"); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | void |
| 473 | sim_set_callbacks (ptr) |
| 474 | host_callback *ptr; |
| 475 | { |
| 476 | sim_callback = ptr; |
| 477 | } |