1 /* Main simulator entry points specific to the CRIS.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Axis Communications.
5 This file is part of the GNU simulators.
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.
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.
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/>. */
20 /* Based on the fr30 file, mixing in bits from the i960 and pruning of
23 #include "libiberty.h"
30 #include "sim-options.h"
33 /* Apparently the autoconf bits are missing (though HAVE_ENVIRON is used
34 in other dirs; also lacking there). Patch around it for major systems. */
35 #if defined (HAVE_ENVIRON) || defined (__GLIBC__)
36 extern char **environ
;
37 #define GET_ENVIRON() environ
39 char *missing_environ
[] = { "SHELL=/bin/sh", "PATH=/bin:/usr/bin", NULL
};
40 #define GET_ENVIRON() missing_environ
43 /* AUX vector entries. */
44 #define TARGET_AT_NULL 0
45 #define TARGET_AT_PHDR 3
46 #define TARGET_AT_PHENT 4
47 #define TARGET_AT_PHNUM 5
48 #define TARGET_AT_PAGESZ 6
49 #define TARGET_AT_BASE 7
50 #define TARGET_AT_FLAGS 8
51 #define TARGET_AT_ENTRY 9
52 #define TARGET_AT_UID 11
53 #define TARGET_AT_EUID 12
54 #define TARGET_AT_GID 13
55 #define TARGET_AT_EGID 14
56 #define TARGET_AT_HWCAP 16
57 #define TARGET_AT_CLKTCK 17
59 /* Used with get_progbounds to find out how much memory is needed for the
60 program. We don't want to allocate more, since that could mask
61 invalid memory accesses program bugs. */
67 static void free_state (SIM_DESC
);
68 static void get_progbounds (bfd
*, asection
*, void *);
69 static SIM_RC
cris_option_handler (SIM_DESC
, sim_cpu
*, int, char *, int);
71 /* Since we don't build the cgen-opcode table, we use the old
73 static CGEN_DISASSEMBLER cris_disassemble_insn
;
75 /* By default, we set up stack and environment variables like the Linux
77 static char cris_bare_iron
= 0;
79 /* Whether 0x9000000xx have simulator-specific meanings. */
80 char cris_have_900000xxif
= 0;
82 /* What to do when we face a more or less unknown syscall. */
83 enum cris_unknown_syscall_action_type cris_unknown_syscall_action
84 = CRIS_USYSC_MSG_STOP
;
86 /* Records simulator descriptor so utilities like cris_dump_regs can be
88 SIM_DESC current_state
;
90 /* CRIS-specific options. */
92 OPTION_CRIS_STATS
= OPTION_START
,
95 OPTION_CRIS_900000XXIF
,
96 OPTION_CRIS_UNKNOWN_SYSCALL
99 static const OPTION cris_options
[] =
101 { {"cris-cycles", required_argument
, NULL
, OPTION_CRIS_STATS
},
102 '\0', "basic|unaligned|schedulable|all",
103 "Dump execution statistics",
104 cris_option_handler
, NULL
},
105 { {"cris-trace", required_argument
, NULL
, OPTION_CRIS_TRACE
},
107 "Emit trace information while running",
108 cris_option_handler
, NULL
},
109 { {"cris-naked", no_argument
, NULL
, OPTION_CRIS_NAKED
},
110 '\0', NULL
, "Don't set up stack and environment",
111 cris_option_handler
, NULL
},
112 { {"cris-900000xx", no_argument
, NULL
, OPTION_CRIS_900000XXIF
},
113 '\0', NULL
, "Define addresses at 0x900000xx with simulator semantics",
114 cris_option_handler
, NULL
},
115 { {"cris-unknown-syscall", required_argument
, NULL
,
116 OPTION_CRIS_UNKNOWN_SYSCALL
},
117 '\0', "stop|enosys|enosys-quiet", "Action at an unknown system call",
118 cris_option_handler
, NULL
},
119 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
122 /* Add the CRIS-specific option list to the simulator. */
125 cris_option_install (SIM_DESC sd
)
127 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
128 if (sim_add_option_table (sd
, NULL
, cris_options
) != SIM_RC_OK
)
133 /* Handle CRIS-specific options. */
136 cris_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
137 char *arg
, int is_command ATTRIBUTE_UNUSED
)
139 /* The options are CRIS-specific, but cpu-specific option-handling is
140 broken; required to being with "--cpu0-". We store the flags in an
141 unused field in the global state structure and move the flags over
142 to the module-specific CPU data when we store things in the
143 cpu-specific structure. */
144 char *tracefp
= STATE_TRACE_FLAGS (sd
);
146 switch ((CRIS_OPTIONS
) opt
)
148 case OPTION_CRIS_STATS
:
149 if (strcmp (arg
, "basic") == 0)
150 *tracefp
= FLAG_CRIS_MISC_PROFILE_SIMPLE
;
151 else if (strcmp (arg
, "unaligned") == 0)
153 = (FLAG_CRIS_MISC_PROFILE_UNALIGNED
154 | FLAG_CRIS_MISC_PROFILE_SIMPLE
);
155 else if (strcmp (arg
, "schedulable") == 0)
157 = (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
158 | FLAG_CRIS_MISC_PROFILE_SIMPLE
);
159 else if (strcmp (arg
, "all") == 0)
160 *tracefp
= FLAG_CRIS_MISC_PROFILE_ALL
;
163 /* Beware; the framework does not handle the error case;
164 we have to do it ourselves. */
165 sim_io_eprintf (sd
, "Unknown option `--cris-cycles=%s'\n", arg
);
170 case OPTION_CRIS_TRACE
:
171 if (strcmp (arg
, "basic") == 0)
172 *tracefp
|= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE
;
175 sim_io_eprintf (sd
, "Unknown option `--cris-trace=%s'\n", arg
);
180 case OPTION_CRIS_NAKED
:
184 case OPTION_CRIS_900000XXIF
:
185 cris_have_900000xxif
= 1;
188 case OPTION_CRIS_UNKNOWN_SYSCALL
:
189 if (strcmp (arg
, "enosys") == 0)
190 cris_unknown_syscall_action
= CRIS_USYSC_MSG_ENOSYS
;
191 else if (strcmp (arg
, "enosys-quiet") == 0)
192 cris_unknown_syscall_action
= CRIS_USYSC_QUIET_ENOSYS
;
193 else if (strcmp (arg
, "stop") == 0)
194 cris_unknown_syscall_action
= CRIS_USYSC_MSG_STOP
;
197 sim_io_eprintf (sd
, "Unknown option `--cris-unknown-syscall=%s'\n",
204 /* We'll actually never get here; the caller handles the error
206 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
210 /* Imply --profile-model=on. */
211 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, "on");
214 /* Cover function of sim_state_free to free the cpu buffers as well. */
217 free_state (SIM_DESC sd
)
219 if (STATE_MODULES (sd
) != NULL
)
220 sim_module_uninstall (sd
);
221 sim_cpu_free_all (sd
);
225 /* BFD section iterator to find the highest allocated section address
226 (plus one). If we could, we should use the program header table
227 instead, but we can't get to that using bfd. */
230 get_progbounds (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*s
, void *vp
)
232 struct progbounds
*pbp
= (struct progbounds
*) vp
;
234 if ((bfd_get_section_flags (abfd
, s
) & SEC_ALLOC
))
236 bfd_size_type sec_size
= bfd_get_section_size (s
);
237 bfd_size_type sec_start
= bfd_get_section_vma (abfd
, s
);
238 bfd_size_type sec_end
= sec_start
+ sec_size
;
240 if (sec_end
> pbp
->endmem
)
241 pbp
->endmem
= sec_end
;
243 if (sec_start
< pbp
->startmem
)
244 pbp
->startmem
= sec_start
;
248 /* Create an instance of the simulator. */
251 sim_open (SIM_OPEN_KIND kind
, host_callback
*callback
, struct bfd
*abfd
,
257 USI endmem
= CRIS_DEFAULT_MEM_SIZE
;
260 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
262 /* Can't initialize to "" below. It's either a GCC bug in old
263 releases (up to and including 2.95.3 (.4 in debian) or a bug in the
264 standard ;-) that the rest of the elements won't be initialized. */
265 bfd_byte sp_init
[4] = {0, 0, 0, 0};
267 /* The cpu data is kept in a separately allocated chunk of memory. */
268 if (sim_cpu_alloc_all (sd
, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK
)
274 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
280 /* getopt will print the error message so we just have to exit if this fails.
281 FIXME: Hmmm... in the case of gdb we need getopt to call
283 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
289 /* If we have a binary program, endianness-setting would not be taken
290 from elsewhere unfortunately, so set it here. At the time of this
291 writing, it isn't used until sim_config, but that might change so
292 set it here before memory is defined or touched. */
293 current_target_byte_order
= LITTLE_ENDIAN
;
295 /* check for/establish the reference program image */
296 if (sim_analyze_program (sd
,
297 (STATE_PROG_ARGV (sd
) != NULL
298 ? *STATE_PROG_ARGV (sd
)
306 /* For CRIS simulator-specific use, we need to find out the bounds of
307 the program as well, which is not done by sim_analyze_program
309 if (STATE_PROG_BFD (sd
))
311 struct progbounds pb
;
313 /* The sections should now be accessible using bfd functions. */
314 pb
.startmem
= 0x7fffffff;
316 bfd_map_over_sections (STATE_PROG_BFD (sd
), get_progbounds
, &pb
);
318 /* We align the area that the program uses to page boundaries. */
319 startmem
= pb
.startmem
& ~8191;
321 endmem
= (endbrk
+ 8191) & ~8191;
324 /* Find out how much room is needed for the environment and argv, create
325 that memory and fill it. Only do this when there's a program
327 if (STATE_PROG_BFD (sd
) && !cris_bare_iron
)
329 char *name
= bfd_get_filename (STATE_PROG_BFD (sd
));
330 char **my_environ
= GET_ENVIRON ();
331 /* We use these maps to give the same behavior as the old xsim
333 USI envtop
= 0x40000000;
334 USI stacktop
= 0x3e000000;
337 int len
= strlen (name
) + 1;
341 char **prog_argv
= STATE_PROG_ARGV (sd
);
343 /* All CPU:s have the same memory map, apparently. */
344 SIM_CPU
*cpu
= STATE_CPU (sd
, 0);
348 /* Count in the environment as well. */
349 for (envc
= 0; my_environ
[envc
] != NULL
; envc
++)
350 len
+= strlen (my_environ
[envc
]) + 1;
352 for (i
= 0; prog_argv
[i
] != NULL
; my_argc
++, i
++)
353 len
+= strlen (prog_argv
[i
]) + 1;
355 envstart
= (envtop
- len
) & ~8191;
357 /* Create read-only block for the environment strings. */
358 sim_core_attach (sd
, NULL
, 0, access_read
, 0,
359 envstart
, (len
+ 8191) & ~8191,
362 /* This shouldn't happen. */
363 if (envstart
< stacktop
)
364 stacktop
= envstart
- 64 * 8192;
368 /* Note that the linux kernel does not correctly compute the storage
369 needs for the static-exe AUX vector. */
372 csp
-= (envc
+ 1) * 4;
373 csp
-= (my_argc
+ 1) * 4;
376 /* Write the target representation of the start-up-value for the
377 stack-pointer suitable for register initialization below. */
378 bfd_putl32 (csp
, sp_init
);
380 /* If we make this 1M higher; say 8192*1024, we have to take
381 special precautions for pthreads, because pthreads assumes that
382 the memory that low isn't mmapped, and that it can mmap it
383 without fallback in case of failure (and we fail ungracefully
384 long before *that*: the memory isn't accounted for in our mmap
386 stack_low
= (csp
- (7168*1024)) & ~8191;
388 stacklen
= stacktop
- stack_low
;
390 /* Tee hee, we have an executable stack. Well, it's necessary to
391 test GCC trampolines... */
392 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
396 epp
= epp0
= envstart
;
398 /* Can't use sim_core_write_unaligned_4 without everything
399 initialized when tracing, and then these writes would get into
401 #define write_dword(addr, data) \
406 bfd_putl32 (data_, buf); \
407 if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \
412 write_dword (csp
, my_argc
);
415 for (i
= 0; i
< my_argc
; i
++, csp
+= 4)
417 size_t strln
= strlen (prog_argv
[i
]) + 1;
419 if (sim_core_write_buffer (sd
, cpu
, 0, prog_argv
[i
], epp
, strln
)
423 write_dword (csp
, envstart
+ epp
- epp0
);
427 write_dword (csp
, 0);
430 for (i
= 0; i
< envc
; i
++, csp
+= 4)
432 unsigned int strln
= strlen (my_environ
[i
]) + 1;
434 if (sim_core_write_buffer (sd
, cpu
, 0, my_environ
[i
], epp
, strln
)
438 write_dword (csp
, envstart
+ epp
- epp0
);
442 write_dword (csp
, 0);
445 #define NEW_AUX_ENT(nr, id, val) \
448 write_dword (csp + (nr) * 4 * 2, (id)); \
449 write_dword (csp + (nr) * 4 * 2 + 4, (val)); \
453 /* Note that there are some extra AUX entries for a dynlinked
454 program loaded image. */
456 /* AUX entries always present. */
457 NEW_AUX_ENT (0, TARGET_AT_HWCAP
, 0);
458 NEW_AUX_ENT (1, TARGET_AT_PAGESZ
, 8192);
459 NEW_AUX_ENT (2, TARGET_AT_CLKTCK
, 100);
462 NEW_AUX_ENT (0, TARGET_AT_NULL
, 0);
465 /* Register R10 should hold 0 at static start (no initfunc), but
466 that's the default, so don't bother. */
469 /* Allocate core managed memory if none specified by user. */
470 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, startmem
, 1) == 0)
471 sim_do_commandf (sd
, "memory region 0x%lx,0x%lx", startmem
,
474 /* Allocate simulator I/O managed memory if none specified by user. */
475 if (cris_have_900000xxif
)
477 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, 0x90000000, 1) == 0)
478 sim_core_attach (sd
, NULL
, 0, access_write
, 0, 0x90000000, 0x100,
479 0, &cris_devices
, NULL
);
483 printf_filtered
) (callback
,
484 "Seeing --cris-900000xx with memory defined there\n");
489 /* Establish any remaining configuration options. */
490 if (sim_config (sd
) != SIM_RC_OK
)
497 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
503 /* Open a copy of the cpu descriptor table. */
505 CGEN_CPU_DESC cd
= cris_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd
)->printable_name
,
507 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
509 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
510 CPU_CPU_DESC (cpu
) = cd
;
511 CPU_DISASSEMBLER (cpu
) = cris_disassemble_insn
;
513 /* See cris_option_handler for the reason why this is needed. */
514 CPU_CRIS_MISC_PROFILE (cpu
)->flags
= STATE_TRACE_FLAGS (sd
)[0];
516 /* Set SP to the stack we allocated above. */
517 (* CPU_REG_STORE (cpu
)) (cpu
, H_GR_SP
, (char *) sp_init
, 4);
519 /* Set the simulator environment data. */
520 cpu
->highest_mmapped_page
= NULL
;
521 cpu
->endmem
= endmem
;
522 cpu
->endbrk
= endbrk
;
523 cpu
->stack_low
= stack_low
;
527 cpu
->max_threadid
= 0;
528 cpu
->thread_data
= NULL
;
529 memset (cpu
->sighandler
, 0, sizeof (cpu
->sighandler
));
530 cpu
->make_thread_cpu_data
= NULL
;
531 cpu
->thread_cpu_data_size
= 0;
533 cpu
->deliver_interrupt
= NULL
;
537 /* Always be cycle-accurate and call before/after functions if
539 sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, "on");
543 /* Initialize various cgen things not done by common framework.
544 Must be done after cris_cgen_cpu_open. */
547 /* Store in a global so things like cris_dump_regs can be invoked
548 from the gdb command line. */
551 cris_set_callbacks (callback
);
557 sim_close (SIM_DESC sd
, int quitting ATTRIBUTE_UNUSED
)
559 cris_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd
, 0)));
560 sim_module_uninstall (sd
);
564 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
565 char **argv ATTRIBUTE_UNUSED
,
566 char **envp ATTRIBUTE_UNUSED
)
568 SIM_CPU
*current_cpu
= STATE_CPU (sd
, 0);
572 addr
= bfd_get_start_address (abfd
);
575 sim_pc_set (current_cpu
, addr
);
577 /* Other simulators have #if 0:d code that says
578 STATE_ARGV (sd) = sim_copy_argv (argv);
579 STATE_ENVP (sd) = sim_copy_argv (envp);
580 Enabling that gives you not-found link-errors for sim_copy_argv.
581 FIXME: Do archaeology to find out more. */
587 sim_do_command (SIM_DESC sd
, char *cmd
)
589 if (sim_args_command (sd
, cmd
) != SIM_RC_OK
)
590 sim_io_eprintf (sd
, "Unknown command `%s'\n", cmd
);
593 /* Disassemble an instruction. */
596 cris_disassemble_insn (SIM_CPU
*cpu
,
597 const CGEN_INSN
*insn ATTRIBUTE_UNUSED
,
598 const ARGBUF
*abuf ATTRIBUTE_UNUSED
,
601 disassembler_ftype pinsn
;
602 struct disassemble_info disasm_info
;
604 SIM_DESC sd
= CPU_STATE (cpu
);
606 sfile
.buffer
= sfile
.current
= buf
;
607 INIT_DISASSEMBLE_INFO (disasm_info
, (FILE *) &sfile
,
608 (fprintf_ftype
) sim_disasm_sprintf
);
610 (bfd_big_endian (STATE_PROG_BFD (sd
)) ? BFD_ENDIAN_BIG
611 : bfd_little_endian (STATE_PROG_BFD (sd
)) ? BFD_ENDIAN_LITTLE
612 : BFD_ENDIAN_UNKNOWN
);
613 /* We live with the cast until the prototype is fixed, or else we get a
614 warning because the functions differ in the signedness of one parameter. */
615 disasm_info
.read_memory_func
=
616 sim_disasm_read_memory
;
617 disasm_info
.memory_error_func
= sim_disasm_perror_memory
;
618 disasm_info
.application_data
= (PTR
) cpu
;
619 pinsn
= cris_get_disassembler (STATE_PROG_BFD (sd
));
620 (*pinsn
) (pc
, &disasm_info
);