Fix bug detected by GDB testsuite - when fetching registers more than 4
[external/binutils.git] / sim / arm / wrapper.c
1 /* run front end support for arm
2    Copyright (C) 1995, 1996, 1997, 2000, 2001 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
126   init ();
127
128   for (i = 0; i < size; i++)
129     ARMul_SafeWriteByte (state, addr + i, buffer[i]);
130
131   return size;
132 }
133
134 int
135 sim_read (sd, addr, buffer, size)
136      SIM_DESC sd ATTRIBUTE_UNUSED;
137      SIM_ADDR addr;
138      unsigned char *buffer;
139      int size;
140 {
141   int i;
142
143   init ();
144   for (i = 0; i < size; i++)
145     buffer[i] = ARMul_SafeReadByte (state, addr + i);
146
147   return size;
148 }
149
150 int
151 sim_trace (sd)
152      SIM_DESC sd ATTRIBUTE_UNUSED;
153 {  
154   (*sim_callback->printf_filtered) (sim_callback,
155                                     "This simulator does not support tracing\n");
156   return 1;
157 }
158
159 int
160 sim_stop (sd)
161      SIM_DESC sd ATTRIBUTE_UNUSED;
162 {
163   state->Emulate = STOP;
164   stop_simulator = 1;
165   return 1;
166 }
167
168 void
169 sim_resume (sd, step, siggnal)
170      SIM_DESC sd ATTRIBUTE_UNUSED;
171      int step;
172      int siggnal ATTRIBUTE_UNUSED;
173 {
174   state->EndCondition = 0;
175   stop_simulator = 0;
176
177   if (step)
178     {
179       state->Reg[15] = ARMul_DoInstr (state);
180       if (state->EndCondition == 0)
181         state->EndCondition = RDIError_BreakpointReached;
182     }
183   else
184     {
185 #if 1                           /* JGS */
186       state->NextInstr = RESUME;        /* treat as PC change */
187 #endif
188       state->Reg[15] = ARMul_DoProg (state);
189     }
190
191   FLUSHPIPE;
192 }
193
194 SIM_RC
195 sim_create_inferior (sd, abfd, argv, env)
196      SIM_DESC sd ATTRIBUTE_UNUSED;
197      struct _bfd *abfd;
198      char **argv;
199      char **env;
200 {
201   int argvlen = 0;
202   int mach;
203   char **arg;
204
205   if (abfd != NULL)
206     ARMul_SetPC (state, bfd_get_start_address (abfd));
207   else
208     ARMul_SetPC (state, 0);     /* ??? */
209
210   mach = bfd_get_mach (abfd);
211
212   switch (mach)
213     {
214     default:
215       (*sim_callback->printf_filtered) (sim_callback,
216                                         "Unknown machine type; please update sim_create_inferior.\n");
217       /* fall through */
218
219     case 0:
220       /* We wouldn't set the machine type with earlier toolchains, so we
221          explicitly select a processor capable of supporting all ARMs in
222          32bit mode.  */
223     case bfd_mach_arm_XScale:
224       ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop | ARM_XScale_Prop);
225       break;
226
227     case bfd_mach_arm_5:
228     case bfd_mach_arm_5T:
229       ARMul_SelectProcessor (state, ARM_v5_Prop);
230       break;
231
232     case bfd_mach_arm_5TE:
233       ARMul_SelectProcessor (state, ARM_v5_Prop | ARM_v5e_Prop);
234       break;
235
236     case bfd_mach_arm_4:
237     case bfd_mach_arm_4T:
238       ARMul_SelectProcessor (state, ARM_v4_Prop);
239       break;
240
241     case bfd_mach_arm_3:
242     case bfd_mach_arm_3M:
243       ARMul_SelectProcessor (state, ARM_Lock_Prop);
244       break;
245
246     case bfd_mach_arm_2:
247     case bfd_mach_arm_2a:
248       ARMul_SelectProcessor (state, ARM_Fix26_Prop);
249       break;
250     }
251
252   if (   mach != bfd_mach_arm_3
253       && mach != bfd_mach_arm_3M
254       && mach != bfd_mach_arm_2
255       && mach != bfd_mach_arm_2a)
256     {
257       /* Reset mode to ARM.  A gdb user may rerun a program that had entered
258          THUMB mode from the start and cause the ARM-mode startup code to be
259          executed in THUMB mode.  */
260       ARMul_SetCPSR (state, SVC32MODE);
261     }
262   
263   if (argv != NULL)
264     {
265       /* Set up the command line by laboriously stringing together
266          the environment carefully picked apart by our caller.  */
267
268       /* Free any old stuff.  */
269       if (state->CommandLine != NULL)
270         {
271           free (state->CommandLine);
272           state->CommandLine = NULL;
273         }
274
275       /* See how much we need.  */
276       for (arg = argv; *arg != NULL; arg++)
277         argvlen += strlen (*arg) + 1;
278
279       /* Allocate it.  */
280       state->CommandLine = malloc (argvlen + 1);
281       if (state->CommandLine != NULL)
282         {
283           arg = argv;
284           state->CommandLine[0] = '\0';
285
286           for (arg = argv; *arg != NULL; arg++)
287             {
288               strcat (state->CommandLine, *arg);
289               strcat (state->CommandLine, " ");
290             }
291         }
292     }
293
294   if (env != NULL)
295     {
296       /* Now see if there's a MEMSIZE spec in the environment.  */
297       while (*env)
298         {
299           if (strncmp (*env, "MEMSIZE=", sizeof ("MEMSIZE=") - 1) == 0)
300             {
301               char *end_of_num;
302
303               /* Set up memory limit.  */
304               state->MemSize =
305                 strtoul (*env + sizeof ("MEMSIZE=") - 1, &end_of_num, 0);
306             }
307           env++;
308         }
309     }
310
311   return SIM_RC_OK;
312 }
313
314 void
315 sim_info (sd, verbose)
316      SIM_DESC sd ATTRIBUTE_UNUSED;
317      int verbose ATTRIBUTE_UNUSED;
318 {
319 }
320
321
322 static int
323 frommem (state, memory)
324      struct ARMul_State *state;
325      unsigned char *memory;
326 {
327   if (state->bigendSig == HIGH)
328     {
329       return (memory[0] << 24)
330         | (memory[1] << 16) | (memory[2] << 8) | (memory[3] << 0);
331     }
332   else
333     {
334       return (memory[3] << 24)
335         | (memory[2] << 16) | (memory[1] << 8) | (memory[0] << 0);
336     }
337 }
338
339
340 static void
341 tomem (state, memory, val)
342      struct ARMul_State *state;
343      unsigned char *memory;
344      int val;
345 {
346   if (state->bigendSig == HIGH)
347     {
348       memory[0] = val >> 24;
349       memory[1] = val >> 16;
350       memory[2] = val >> 8;
351       memory[3] = val >> 0;
352     }
353   else
354     {
355       memory[3] = val >> 24;
356       memory[2] = val >> 16;
357       memory[1] = val >> 8;
358       memory[0] = val >> 0;
359     }
360 }
361
362 int
363 sim_store_register (sd, rn, memory, length)
364      SIM_DESC sd ATTRIBUTE_UNUSED;
365      int rn;
366      unsigned char *memory;
367      int length ATTRIBUTE_UNUSED;
368 {
369   init ();
370
371   if (rn == 25)
372     {
373       state->Cpsr = frommem (state, memory);
374       ARMul_CPSRAltered (state);             
375     }
376   else
377     ARMul_SetReg (state, state->Mode, rn, frommem (state, memory));
378   return -1;
379 }
380
381 int
382 sim_fetch_register (sd, rn, memory, length)
383      SIM_DESC sd ATTRIBUTE_UNUSED;
384      int rn;
385      unsigned char *memory;
386      int length ATTRIBUTE_UNUSED;
387 {
388   ARMword regval;
389
390   init ();
391
392   if (rn < 16)
393     regval = ARMul_GetReg (state, state->Mode, rn);
394   else if (rn == 25)            /* FIXME: use PS_REGNUM from gdb/config/arm/tm-arm.h */
395     regval = ARMul_GetCPSR (state);
396   else
397     regval = 0;                 /* FIXME: should report an error */
398
399   while (length)
400     {
401       tomem (state, memory, regval);
402
403       length -= 4;
404       memory += 4;
405       regval = 0;
406     }  
407
408   return -1;
409 }
410
411 SIM_DESC
412 sim_open (kind, ptr, abfd, argv)
413      SIM_OPEN_KIND kind;
414      host_callback *ptr;
415      struct _bfd *abfd;
416      char **argv;
417 {
418   sim_kind = kind;
419   if (myname) free (myname);
420   myname = (char *) xstrdup (argv[0]);
421   sim_callback = ptr;
422
423   /* Decide upon the endian-ness of the processor.
424      If we can, get the information from the bfd itself.
425      Otherwise look to see if we have been given a command
426      line switch that tells us.  Otherwise default to little endian.  */
427   if (abfd != NULL)
428     big_endian = bfd_big_endian (abfd);
429   else if (argv[1] != NULL)
430     {
431       int i;
432
433       /* Scan for endian-ness switch.  */
434       for (i = 0; (argv[i] != NULL) && (argv[i][0] != 0); i++)
435         if (argv[i][0] == '-' && argv[i][1] == 'E')
436           {
437             char c;
438
439             if ((c = argv[i][2]) == 0)
440               {
441                 ++i;
442                 c = argv[i][0];
443               }
444
445             switch (c)
446               {
447               case 0:
448                 sim_callback->printf_filtered
449                   (sim_callback, "No argument to -E option provided\n");
450                 break;
451
452               case 'b':
453               case 'B':
454                 big_endian = 1;
455                 break;
456
457               case 'l':
458               case 'L':
459                 big_endian = 0;
460                 break;
461
462               default:
463                 sim_callback->printf_filtered
464                   (sim_callback, "Unrecognised argument to -E option\n");
465                 break;
466               }
467           }
468     }
469
470   return (SIM_DESC) 1;
471 }
472
473 void
474 sim_close (sd, quitting)
475      SIM_DESC sd ATTRIBUTE_UNUSED;
476      int quitting ATTRIBUTE_UNUSED;
477 {
478   if (myname) free (myname);
479   myname = NULL;
480 }
481
482 SIM_RC
483 sim_load (sd, prog, abfd, from_tty)
484      SIM_DESC sd;
485      char *prog;
486      bfd *abfd;
487      int from_tty ATTRIBUTE_UNUSED;
488 {
489   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
490   bfd *prog_bfd;
491
492   prog_bfd = sim_load_file (sd, myname, sim_callback, prog, abfd,
493                             sim_kind == SIM_OPEN_DEBUG, 0, sim_write);
494   if (prog_bfd == NULL)
495     return SIM_RC_FAIL;
496   ARMul_SetPC (state, bfd_get_start_address (prog_bfd));
497   if (abfd == NULL)
498     bfd_close (prog_bfd);
499   return SIM_RC_OK;
500 }
501
502 void
503 sim_stop_reason (sd, reason, sigrc)
504      SIM_DESC sd ATTRIBUTE_UNUSED;
505      enum sim_stop *reason;
506      int *sigrc;
507 {
508   if (stop_simulator)
509     {
510       *reason = sim_stopped;
511       *sigrc = SIGINT;
512     }
513   else if (state->EndCondition == 0)
514     {
515       *reason = sim_exited;
516       *sigrc = state->Reg[0] & 255;
517     }
518   else
519     {
520       *reason = sim_stopped;
521       if (state->EndCondition == RDIError_BreakpointReached)
522         *sigrc = SIGTRAP;
523       else
524         *sigrc = 0;
525     }
526 }
527
528 void
529 sim_do_command (sd, cmd)
530      SIM_DESC sd ATTRIBUTE_UNUSED;
531      char *cmd ATTRIBUTE_UNUSED;
532 {  
533   (*sim_callback->printf_filtered) (sim_callback,
534                                     "This simulator does not accept any commands.\n");
535 }
536
537
538 void
539 sim_set_callbacks (ptr)
540      host_callback *ptr;
541 {
542   sim_callback = ptr;
543 }