1 /* Main simulator entry points specific to the CRIS.
2 Copyright (C) 2004-2024 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 /* This must come before any other includes. */
30 #include "libiberty.h"
32 #include "bfd/elf-bfd.h"
34 #include "sim/callback.h"
36 #include "sim-options.h"
41 /* Used with get_progbounds to find out how much memory is needed for the
42 program. We don't want to allocate more, since that could mask
43 invalid memory accesses program bugs. */
51 static void free_state (SIM_DESC
);
52 static void get_progbounds_iterator (bfd
*, asection
*, void *);
53 static SIM_RC
cris_option_handler (SIM_DESC
, sim_cpu
*, int, char *, int);
55 /* Since we don't build the cgen-opcode table, we use the old
57 static CGEN_DISASSEMBLER cris_disassemble_insn
;
59 /* By default, we set up stack and environment variables like the Linux
61 static char cris_bare_iron
= 0;
63 /* Whether 0x9000000xx have simulator-specific meanings. */
64 char cris_have_900000xxif
= 0;
66 /* Used to optionally override the default start address of the
68 static USI cris_start_address
= 0xffffffffu
;
70 /* Used to optionally add offsets to the loaded image and its start
71 address. (Not used for the interpreter of dynamically loaded
72 programs or the DSO:s.) */
73 static int cris_program_offset
= 0;
75 /* What to do when we face a more or less unknown syscall. */
76 enum cris_unknown_syscall_action_type cris_unknown_syscall_action
77 = CRIS_USYSC_MSG_STOP
;
79 /* CRIS-specific options. */
81 OPTION_CRIS_STATS
= OPTION_START
,
84 OPTION_CRIS_PROGRAM_OFFSET
,
85 OPTION_CRIS_STARTADDR
,
86 OPTION_CRIS_900000XXIF
,
87 OPTION_CRIS_UNKNOWN_SYSCALL
90 static const OPTION cris_options
[] =
92 { {"cris-cycles", required_argument
, NULL
, OPTION_CRIS_STATS
},
93 '\0', "basic|unaligned|schedulable|all",
94 "Dump execution statistics",
95 cris_option_handler
, NULL
},
96 { {"cris-trace", required_argument
, NULL
, OPTION_CRIS_TRACE
},
98 "Emit trace information while running",
99 cris_option_handler
, NULL
},
100 { {"cris-naked", no_argument
, NULL
, OPTION_CRIS_NAKED
},
101 '\0', NULL
, "Don't set up stack and environment",
102 cris_option_handler
, NULL
},
104 { {"cris-900000xx", no_argument
, NULL
, OPTION_CRIS_900000XXIF
},
105 '\0', NULL
, "Define addresses at 0x900000xx with simulator semantics",
106 cris_option_handler
, NULL
},
108 { {"cris-unknown-syscall", required_argument
, NULL
,
109 OPTION_CRIS_UNKNOWN_SYSCALL
},
110 '\0', "stop|enosys|enosys-quiet", "Action at an unknown system call",
111 cris_option_handler
, NULL
},
112 { {"cris-program-offset", required_argument
, NULL
,
113 OPTION_CRIS_PROGRAM_OFFSET
},
115 "Offset image addresses and default start address of a program",
116 cris_option_handler
},
117 { {"cris-start-address", required_argument
, NULL
, OPTION_CRIS_STARTADDR
},
118 '\0', "ADDRESS", "Set start address",
119 cris_option_handler
},
120 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
123 /* Handle CRIS-specific options. */
126 cris_option_handler (SIM_DESC sd
, sim_cpu
*cpu ATTRIBUTE_UNUSED
, int opt
,
127 char *arg
, int is_command ATTRIBUTE_UNUSED
)
129 /* The options are CRIS-specific, but cpu-specific option-handling is
130 broken; required to being with "--cpu0-". We store the flags in an
131 unused field in the global state structure and move the flags over
132 to the module-specific CPU data when we store things in the
133 cpu-specific structure. */
134 char *tracefp
= STATE_TRACE_FLAGS (sd
);
137 switch ((CRIS_OPTIONS
) opt
)
139 case OPTION_CRIS_STATS
:
140 if (strcmp (arg
, "basic") == 0)
141 *tracefp
= FLAG_CRIS_MISC_PROFILE_SIMPLE
;
142 else if (strcmp (arg
, "unaligned") == 0)
144 = (FLAG_CRIS_MISC_PROFILE_UNALIGNED
145 | FLAG_CRIS_MISC_PROFILE_SIMPLE
);
146 else if (strcmp (arg
, "schedulable") == 0)
148 = (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
149 | FLAG_CRIS_MISC_PROFILE_SIMPLE
);
150 else if (strcmp (arg
, "all") == 0)
151 *tracefp
= FLAG_CRIS_MISC_PROFILE_ALL
;
154 /* Beware; the framework does not handle the error case;
155 we have to do it ourselves. */
156 sim_io_eprintf (sd
, "Unknown option `--cris-cycles=%s'\n", arg
);
161 case OPTION_CRIS_TRACE
:
162 if (strcmp (arg
, "basic") == 0)
163 *tracefp
|= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE
;
166 sim_io_eprintf (sd
, "Unknown option `--cris-trace=%s'\n", arg
);
171 case OPTION_CRIS_NAKED
:
175 case OPTION_CRIS_900000XXIF
:
176 cris_have_900000xxif
= 1;
179 case OPTION_CRIS_STARTADDR
:
181 cris_start_address
= (USI
) strtoul (chp
, &chp
, 0);
183 if (errno
!= 0 || *chp
!= 0)
185 sim_io_eprintf (sd
, "Invalid option `--cris-start-address=%s'\n",
191 case OPTION_CRIS_PROGRAM_OFFSET
:
193 cris_program_offset
= (int) strtol (chp
, &chp
, 0);
195 if (errno
!= 0 || *chp
!= 0)
197 sim_io_eprintf (sd
, "Invalid option `--cris-program-offset=%s'\n",
203 case OPTION_CRIS_UNKNOWN_SYSCALL
:
204 if (strcmp (arg
, "enosys") == 0)
205 cris_unknown_syscall_action
= CRIS_USYSC_MSG_ENOSYS
;
206 else if (strcmp (arg
, "enosys-quiet") == 0)
207 cris_unknown_syscall_action
= CRIS_USYSC_QUIET_ENOSYS
;
208 else if (strcmp (arg
, "stop") == 0)
209 cris_unknown_syscall_action
= CRIS_USYSC_MSG_STOP
;
212 sim_io_eprintf (sd
, "Unknown option `--cris-unknown-syscall=%s'\n",
219 /* We'll actually never get here; the caller handles the error
221 sim_io_eprintf (sd
, "Unknown option `%s'\n", arg
);
225 /* Imply --profile-model=on. */
226 return sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, "on");
229 /* An ELF-specific simplified ../common/sim-load.c:sim_load_file,
230 using the program headers, not sections, in order to make sure that
231 the program headers themeselves are also loaded. The caller is
232 responsible for asserting that ABFD is an ELF file. */
235 cris_load_elf_file (SIM_DESC sd
, struct bfd
*abfd
, sim_write_fn do_write
)
237 Elf_Internal_Phdr
*phdr
;
240 bfd_boolean verbose
= STATE_OPEN_KIND (sd
) == SIM_OPEN_DEBUG
;
242 phdr
= elf_tdata (abfd
)->phdr
;
243 n_hdrs
= elf_elfheader (abfd
)->e_phnum
;
245 /* We're only interested in PT_LOAD; all necessary information
246 should be covered by that. */
247 for (i
= 0; i
< n_hdrs
; i
++)
250 bfd_vma lma
= STATE_LOAD_AT_LMA_P (sd
)
251 ? phdr
[i
].p_paddr
: phdr
[i
].p_vaddr
;
253 if (phdr
[i
].p_type
!= PT_LOAD
)
256 buf
= xmalloc (phdr
[i
].p_filesz
);
260 "Loading segment at 0x%" PRIx64
", "
261 "size 0x%" PRIx64
"\n",
262 (uint64_t) lma
, (uint64_t) phdr
[i
].p_filesz
);
264 if (bfd_seek (abfd
, phdr
[i
].p_offset
, SEEK_SET
) != 0
265 || (bfd_read (buf
, phdr
[i
].p_filesz
, abfd
) != phdr
[i
].p_filesz
))
268 "%s: could not read segment at 0x%" PRIx64
", "
269 "size 0x%" PRIx64
"\n",
270 STATE_MY_NAME (sd
), (uint64_t) lma
,
271 (uint64_t) phdr
[i
].p_filesz
);
276 if (do_write (sd
, lma
, buf
, phdr
[i
].p_filesz
) != phdr
[i
].p_filesz
)
279 "%s: could not load segment at 0x%" PRIx64
", "
280 "size 0x%" PRIx64
"\n",
281 STATE_MY_NAME (sd
), (uint64_t) lma
,
282 (uint64_t) phdr
[i
].p_filesz
);
293 /* Cover function of sim_state_free to free the cpu buffers as well. */
296 free_state (SIM_DESC sd
)
298 if (STATE_MODULES (sd
) != NULL
)
299 sim_module_uninstall (sd
);
300 sim_cpu_free_all (sd
);
304 /* Helper struct for cris_set_section_offset_iterator. */
312 /* BFD section iterator to offset the LMA and VMA. */
315 cris_set_section_offset_iterator (bfd
*abfd
, asection
*s
, void *vp
)
317 struct offsetinfo
*p
= (struct offsetinfo
*) vp
;
319 int offset
= p
->offset
;
321 if ((bfd_section_flags (s
) & SEC_ALLOC
))
323 bfd_vma vma
= bfd_section_vma (s
);
325 bfd_set_section_vma (s
, vma
+ offset
);
328 /* This seems clumsy and inaccurate, but let's stick to doing it the
329 same way as sim_analyze_program for consistency. */
330 if (strcmp (bfd_section_name (s
), ".text") == 0)
331 STATE_TEXT_START (sd
) = bfd_section_vma (s
);
334 /* Adjust the start-address, LMA and VMA of a SD. Must be called
335 after sim_analyze_program. */
338 cris_offset_sections (SIM_DESC sd
, int offset
)
340 struct bfd
*abfd
= STATE_PROG_BFD (sd
);
341 struct offsetinfo oi
;
343 /* Only happens for usage error. */
350 bfd_map_over_sections (abfd
, cris_set_section_offset_iterator
, &oi
);
351 bfd_set_start_address (abfd
, bfd_get_start_address (abfd
) + offset
);
353 STATE_START_ADDR (sd
) = bfd_get_start_address (abfd
);
356 /* BFD section iterator to find the highest and lowest allocated and
357 non-allocated section addresses (plus one). */
360 get_progbounds_iterator (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*s
, void *vp
)
362 struct progbounds
*pbp
= (struct progbounds
*) vp
;
364 if ((bfd_section_flags (s
) & SEC_ALLOC
))
366 bfd_size_type sec_size
= bfd_section_size (s
);
367 bfd_size_type sec_start
= bfd_section_vma (s
);
368 bfd_size_type sec_end
= sec_start
+ sec_size
;
370 if (sec_end
> pbp
->endmem
)
371 pbp
->endmem
= sec_end
;
373 if (sec_start
< pbp
->startmem
)
374 pbp
->startmem
= sec_start
;
376 if ((bfd_section_flags (s
) & SEC_LOAD
))
378 if (sec_end
> pbp
->end_loadmem
)
379 pbp
->end_loadmem
= sec_end
;
381 else if (sec_start
< pbp
->start_nonloadmem
)
382 pbp
->start_nonloadmem
= sec_start
;
386 /* Get the program boundaries. Because not everything is covered by
387 sections in ELF, notably the program headers, we use the program
391 cris_get_progbounds (struct bfd
*abfd
, struct progbounds
*pbp
)
393 Elf_Internal_Phdr
*phdr
;
397 pbp
->startmem
= 0xffffffff;
399 pbp
->end_loadmem
= 0;
400 pbp
->start_nonloadmem
= 0xffffffff;
402 /* In case we're ever used for something other than ELF, use the
404 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
406 bfd_map_over_sections (abfd
, get_progbounds_iterator
, pbp
);
410 phdr
= elf_tdata (abfd
)->phdr
;
411 n_hdrs
= elf_elfheader (abfd
)->e_phnum
;
413 /* We're only interested in PT_LOAD; all necessary information
414 should be covered by that. */
415 for (i
= 0; i
< n_hdrs
; i
++)
417 if (phdr
[i
].p_type
!= PT_LOAD
)
420 if (phdr
[i
].p_paddr
< pbp
->startmem
)
421 pbp
->startmem
= phdr
[i
].p_paddr
;
423 if (phdr
[i
].p_paddr
+ phdr
[i
].p_memsz
> pbp
->endmem
)
424 pbp
->endmem
= phdr
[i
].p_paddr
+ phdr
[i
].p_memsz
;
426 if (phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
> pbp
->end_loadmem
)
427 pbp
->end_loadmem
= phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
;
429 if (phdr
[i
].p_memsz
> phdr
[i
].p_filesz
430 && phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
< pbp
->start_nonloadmem
)
431 pbp
->start_nonloadmem
= phdr
[i
].p_paddr
+ phdr
[i
].p_filesz
;
435 /* Parameter communication by static variables, hmm... Oh well, for
437 static bfd_vma exec_load_addr
;
438 static bfd_vma interp_load_addr
;
439 static bfd_vma interp_start_addr
;
441 /* Supposed to mimic Linux' "NEW_AUX_ENT (AT_PHDR, load_addr + exec->e_phoff)". */
444 aux_ent_phdr (struct bfd
*ebfd
)
446 return elf_elfheader (ebfd
)->e_phoff
+ exec_load_addr
;
449 /* We just pass on the header info; we don't have our own idea of the
450 program header entry size. */
453 aux_ent_phent (struct bfd
*ebfd
)
455 return elf_elfheader (ebfd
)->e_phentsize
;
458 /* Like "NEW_AUX_ENT(AT_PHNUM, exec->e_phnum)". */
461 aux_ent_phnum (struct bfd
*ebfd
)
463 return elf_elfheader (ebfd
)->e_phnum
;
466 /* Like "NEW_AUX_ENT(AT_BASE, interp_load_addr)". */
469 aux_ent_base (struct bfd
*ebfd
)
471 return interp_load_addr
;
474 /* Like "NEW_AUX_ENT(AT_ENTRY, exec->e_entry)". */
477 aux_ent_entry (struct bfd
*ebfd
)
479 ASSERT (elf_elfheader (ebfd
)->e_entry
== bfd_get_start_address (ebfd
));
480 return elf_elfheader (ebfd
)->e_entry
;
483 /* Helper for cris_handle_interpreter: like sim_write, but load at
484 interp_load_addr offset. */
487 cris_write_interp (SIM_DESC sd
, uint64_t mem
, const void *buf
, uint64_t length
)
489 return sim_write (sd
, mem
+ interp_load_addr
, buf
, length
);
492 /* Cater to the presence of an interpreter: load it and set
493 interp_start_addr. Return FALSE if there was an error, TRUE if
494 everything went fine, including an interpreter being absent and
495 the program being in a non-ELF format. */
498 cris_handle_interpreter (SIM_DESC sd
, struct bfd
*abfd
)
503 bfd_boolean ok
= FALSE
;
504 Elf_Internal_Phdr
*phdr
;
506 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
509 phdr
= elf_tdata (abfd
)->phdr
;
510 n_hdrs
= aux_ent_phnum (abfd
);
512 /* Check the program headers for presence of an interpreter. */
513 for (i
= 0; i
< n_hdrs
; i
++)
516 bfd_size_type interpsiz
;
517 struct progbounds interp_bounds
;
519 if (phdr
[i
].p_type
!= PT_INTERP
)
522 /* Get the name of the interpreter, prepended with the sysroot
523 (empty if absent). */
524 interplen
= phdr
[i
].p_filesz
;
525 interp
= xmalloc (interplen
+ strlen (simulator_sysroot
));
526 strcpy (interp
, simulator_sysroot
);
528 /* Read in the name. */
529 if (bfd_seek (abfd
, phdr
[i
].p_offset
, SEEK_SET
) != 0
530 || (bfd_read (interp
+ strlen (simulator_sysroot
), interplen
, abfd
)
532 goto interpname_failed
;
534 /* Like Linux, require the string to be 0-terminated. */
535 if (interp
[interplen
+ strlen (simulator_sysroot
) - 1] != 0)
536 goto interpname_failed
;
538 /* Inspect the interpreter. */
539 ibfd
= bfd_openr (interp
, STATE_TARGET (sd
));
541 goto interpname_failed
;
543 /* The interpreter is at least something readable to BFD; make
544 sure it's an ELF non-archive file. */
545 if (!bfd_check_format (ibfd
, bfd_object
)
546 || bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
549 /* Check the layout of the interpreter. */
550 cris_get_progbounds (ibfd
, &interp_bounds
);
552 /* Round down to pagesize the start page and up the endpage.
553 Don't round the *load and *nonload members. */
554 interp_bounds
.startmem
&= ~8191;
555 interp_bounds
.endmem
= (interp_bounds
.endmem
+ 8191) & ~8191;
557 /* Until we need a more dynamic solution, assume we can put the
558 interpreter at this fixed location. NB: this is not what
559 happens for Linux 2008-12-28, but it could and might and
561 interp_load_addr
= 0x40000;
562 interpsiz
= interp_bounds
.endmem
- interp_bounds
.startmem
;
563 /* interp_filesiz = interp_bounds.end_loadmem - interp_bounds.startmem; */
565 /* If we have a non-DSO or interpreter starting at the wrong
567 if (interp_bounds
.startmem
!= 0
568 || interpsiz
+ interp_load_addr
>= exec_load_addr
)
571 /* We don't have the API to get the address of a simulator
572 memory area, so we go via a temporary area. Luckily, the
573 interpreter is supposed to be small, less than 0x40000
575 sim_do_commandf (sd
, "memory region 0x%" PRIx64
",0x%" PRIx64
,
576 (uint64_t) interp_load_addr
, (uint64_t) interpsiz
);
578 /* Now that memory for the interpreter is defined, load it. */
579 if (!cris_load_elf_file (sd
, ibfd
, cris_write_interp
))
582 /* It's no use setting STATE_START_ADDR, because it gets
583 overwritten by a sim_analyze_program call in sim_load. Let's
584 just store it locally. */
586 = (bfd_get_start_address (ibfd
)
587 - interp_bounds
.startmem
+ interp_load_addr
);
589 /* Linux cares only about the first PT_INTERP, so let's ignore
594 /* Register R10 should hold 0 at static start (no finifunc), but
595 that's the default, so don't bother. */
607 "%s: could not load ELF interpreter `%s' for program `%s'\n",
609 interp
== NULL
? "(what's-its-name)" : interp
,
610 bfd_get_filename (abfd
));
615 extern const SIM_MACH
* const cris_sim_machs
[];
617 /* Create an instance of the simulator. */
620 sim_open (SIM_OPEN_KIND kind
, host_callback
*callback
, struct bfd
*abfd
,
626 USI endmem
= CRIS_DEFAULT_MEM_SIZE
;
629 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
631 static const struct auxv_entries_s
634 USI (*efn
) (struct bfd
*ebfd
);
638 #define AUX_ENT(a, b) {a, NULL, b}
639 #define AUX_ENTF(a, f) {a, f, 0}
640 AUX_ENT (AT_HWCAP
, 0),
641 AUX_ENT (AT_PAGESZ
, 8192),
642 AUX_ENT (AT_CLKTCK
, 100),
643 AUX_ENTF (AT_PHDR
, aux_ent_phdr
),
644 AUX_ENTF (AT_PHENT
, aux_ent_phent
),
645 AUX_ENTF (AT_PHNUM
, aux_ent_phnum
),
646 AUX_ENTF (AT_BASE
, aux_ent_base
),
647 AUX_ENT (AT_FLAGS
, 0),
648 AUX_ENTF (AT_ENTRY
, aux_ent_entry
),
650 /* Or is root better? Maybe have it settable? */
651 AUX_ENT (AT_UID
, 500),
652 AUX_ENT (AT_EUID
, 500),
653 AUX_ENT (AT_GID
, 500),
654 AUX_ENT (AT_EGID
, 500),
655 AUX_ENT (AT_SECURE
, 0),
659 /* Can't initialize to "" below. It's either a GCC bug in old
660 releases (up to and including 2.95.3 (.4 in debian) or a bug in the
661 standard ;-) that the rest of the elements won't be initialized. */
662 bfd_byte sp_init
[4] = {0, 0, 0, 0};
664 /* Set default options before parsing user options. */
665 STATE_MACHS (sd
) = cris_sim_machs
;
666 STATE_MODEL_NAME (sd
) = "crisv32";
667 current_target_byte_order
= BFD_ENDIAN_LITTLE
;
669 /* The cpu data is kept in a separately allocated chunk of memory. */
670 if (sim_cpu_alloc_all_extra (sd
, 0, sizeof (struct cris_sim_cpu
))
677 if (sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
)
683 /* Add the CRIS-specific option list to the simulator. */
684 if (sim_add_option_table (sd
, NULL
, cris_options
) != SIM_RC_OK
)
690 /* The parser will print an error message for us, so we silently return. */
691 if (sim_parse_args (sd
, argv
) != SIM_RC_OK
)
697 /* check for/establish the reference program image */
698 if (sim_analyze_program (sd
, STATE_PROG_FILE (sd
), abfd
) != SIM_RC_OK
)
700 /* When there's an error, sim_analyze_program has already output
701 a message. Let's just clarify it, as "not an object file"
702 perhaps doesn't ring a bell. */
703 sim_io_eprintf (sd
, "(not a CRIS program)\n");
708 /* We might get called with the caller expecting us to get hold of
709 the bfd for ourselves, which would happen at the
710 sim_analyze_program call above. */
712 abfd
= STATE_PROG_BFD (sd
);
714 /* Adjust the addresses of the program at this point. Unfortunately
715 this does not affect ELF program headers, so we have to handle
717 cris_offset_sections (sd
, cris_program_offset
);
719 if (abfd
!= NULL
&& bfd_get_arch (abfd
) == bfd_arch_unknown
)
721 if (STATE_PROG_FILE (sd
) != NULL
)
722 sim_io_eprintf (sd
, "%s: `%s' is not a CRIS program\n",
723 STATE_MY_NAME (sd
), STATE_PROG_FILE (sd
));
725 sim_io_eprintf (sd
, "%s: program to be run is not a CRIS program\n",
731 /* For CRIS simulator-specific use, we need to find out the bounds of
732 the program as well, which is not done by sim_analyze_program
736 struct progbounds pb
;
738 /* The sections should now be accessible using bfd functions. */
739 cris_get_progbounds (abfd
, &pb
);
741 /* We align the area that the program uses to page boundaries. */
742 startmem
= pb
.startmem
& ~8191;
744 endmem
= (endbrk
+ 8191) & ~8191;
747 /* Find out how much room is needed for the environment and argv, create
748 that memory and fill it. Only do this when there's a program
751 TODO: Move this to sim_create_inferior and use STATE_PROG_ENVP. */
752 if (abfd
!= NULL
&& !cris_bare_iron
)
754 const char *name
= bfd_get_filename (abfd
);
755 /* We use these maps to give the same behavior as the old xsim
757 USI envtop
= 0x40000000;
758 USI stacktop
= 0x3e000000;
761 int len
= strlen (name
) + 1;
764 char **prog_argv
= STATE_PROG_ARGV (sd
);
769 /* Count in the environment as well. */
770 for (envc
= 0; environ
[envc
] != NULL
; envc
++)
771 len
+= strlen (environ
[envc
]) + 1;
773 for (i
= 0; prog_argv
[i
] != NULL
; my_argc
++, i
++)
774 len
+= strlen (prog_argv
[i
]) + 1;
776 envstart
= (envtop
- len
) & ~8191;
778 /* Create read-only block for the environment strings. */
779 sim_core_attach (sd
, NULL
, 0, access_read
, 0,
780 envstart
, (len
+ 8191) & ~8191,
783 /* This shouldn't happen. */
784 if (envstart
< stacktop
)
785 stacktop
= envstart
- 64 * 8192;
789 /* Note that the linux kernel does not correctly compute the storage
790 needs for the static-exe AUX vector. */
792 csp
-= ARRAY_SIZE (auxv_entries
) * 4 * 2;
794 csp
-= (envc
+ 1) * 4;
795 csp
-= (my_argc
+ 1) * 4;
798 /* Write the target representation of the start-up-value for the
799 stack-pointer suitable for register initialization below. */
800 bfd_putl32 (csp
, sp_init
);
802 /* If we make this 1M higher; say 8192*1024, we have to take
803 special precautions for pthreads, because pthreads assumes that
804 the memory that low isn't mmapped, and that it can mmap it
805 without fallback in case of failure (and we fail ungracefully
806 long before *that*: the memory isn't accounted for in our mmap
808 stack_low
= (csp
- (7168*1024)) & ~8191;
810 stacklen
= stacktop
- stack_low
;
812 /* Tee hee, we have an executable stack. Well, it's necessary to
813 test GCC trampolines... */
814 sim_core_attach (sd
, NULL
, 0, access_read_write_exec
, 0,
818 epp
= epp0
= envstart
;
820 /* Can't use sim_core_write_unaligned_4 without everything
821 initialized when tracing, and then these writes would get into
823 #define write_dword(addr, data) \
828 bfd_putl32 (data_, buf); \
829 if (sim_core_write_buffer (sd, NULL, NULL_CIA, buf, addr_, 4) != 4)\
834 write_dword (csp
, my_argc
);
837 for (i
= 0; i
< my_argc
; i
++, csp
+= 4)
839 size_t strln
= strlen (prog_argv
[i
]) + 1;
841 if (sim_core_write_buffer (sd
, NULL
, NULL_CIA
, prog_argv
[i
], epp
,
846 write_dword (csp
, envstart
+ epp
- epp0
);
850 write_dword (csp
, 0);
853 for (i
= 0; i
< envc
; i
++, csp
+= 4)
855 unsigned int strln
= strlen (environ
[i
]) + 1;
857 if (sim_core_write_buffer (sd
, NULL
, NULL_CIA
, environ
[i
], epp
, strln
)
861 write_dword (csp
, envstart
+ epp
- epp0
);
865 write_dword (csp
, 0);
868 /* The load address of the executable could presumably be
869 different than the lowest used memory address, but let's
870 stick to simplicity until needed. And
871 cris_handle_interpreter might change startmem and endmem, so
873 exec_load_addr
= startmem
;
875 if (!cris_handle_interpreter (sd
, abfd
))
878 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
879 for (i
= 0; i
< ARRAY_SIZE (auxv_entries
); i
++)
881 write_dword (csp
, auxv_entries
[i
].id
);
882 write_dword (csp
+ 4,
883 auxv_entries
[i
].efn
!= NULL
884 ? (*auxv_entries
[i
].efn
) (abfd
)
885 : auxv_entries
[i
].val
);
890 /* Allocate core managed memory if none specified by user. */
891 if (sim_core_read_buffer (sd
, NULL
, read_map
, &c
, startmem
, 1) == 0)
892 sim_do_commandf (sd
, "memory region 0x%" PRIx32
",0x%" PRIx32
,
893 startmem
, endmem
- startmem
);
895 /* Allocate simulator I/O managed memory if none specified by user. */
897 if (cris_have_900000xxif
)
898 sim_hw_parse (sd
, "/core/%s/reg %#x %i", "cris_900000xx", 0x90000000, 0x100);
900 /* With the option disabled, nothing should be able to set this variable.
901 We should "use" it, though, and why not assert that it isn't set. */
902 ASSERT (! cris_have_900000xxif
);
905 /* Establish any remaining configuration options. */
906 if (sim_config (sd
) != SIM_RC_OK
)
913 if (sim_post_argv_init (sd
) != SIM_RC_OK
)
919 /* Open a copy of the cpu descriptor table. */
921 CGEN_CPU_DESC cd
= cris_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd
)->printable_name
,
923 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
925 SIM_CPU
*cpu
= STATE_CPU (sd
, i
);
926 struct cris_sim_cpu
*cris_cpu
= CRIS_SIM_CPU (cpu
);
928 CPU_CPU_DESC (cpu
) = cd
;
929 CPU_DISASSEMBLER (cpu
) = cris_disassemble_insn
;
931 /* See cris_option_handler for the reason why this is needed. */
932 CPU_CRIS_MISC_PROFILE (cpu
)->flags
= STATE_TRACE_FLAGS (sd
)[0];
934 /* Set SP to the stack we allocated above. */
935 (* CPU_REG_STORE (cpu
)) (cpu
, H_GR_SP
, (const unsigned char *) sp_init
, 4);
937 /* Set the simulator environment data. */
938 cris_cpu
->highest_mmapped_page
= NULL
;
939 cris_cpu
->endmem
= endmem
;
940 cris_cpu
->endbrk
= endbrk
;
941 cris_cpu
->stack_low
= stack_low
;
942 cris_cpu
->syscalls
= 0;
943 cris_cpu
->m1threads
= 0;
944 cris_cpu
->threadno
= 0;
945 cris_cpu
->max_threadid
= 0;
946 cris_cpu
->thread_data
= NULL
;
947 memset (cris_cpu
->sighandler
, 0, sizeof (cris_cpu
->sighandler
));
948 cris_cpu
->make_thread_cpu_data
= NULL
;
949 cris_cpu
->thread_cpu_data_size
= 0;
951 cris_cpu
->deliver_interrupt
= NULL
;
955 /* Always be cycle-accurate and call before/after functions if
957 sim_profile_set_option (sd
, "-model", PROFILE_MODEL_IDX
, "on");
961 cris_set_callbacks (callback
);
967 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
971 SIM_CPU
*current_cpu
= STATE_CPU (sd
, 0);
972 host_callback
*cb
= STATE_CALLBACK (sd
);
976 addr
= cris_start_address
!= (USI
) -1
978 : (interp_start_addr
!= 0
980 : bfd_get_start_address (abfd
));
983 sim_pc_set (current_cpu
, addr
);
985 /* Standalone mode (i.e. `run`) will take care of the argv for us in
986 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
987 with `gdb`), we need to handle it because the user can change the
988 argv on the fly via gdb's 'run'. */
989 if (STATE_PROG_ARGV (sd
) != argv
)
991 freeargv (STATE_PROG_ARGV (sd
));
992 STATE_PROG_ARGV (sd
) = dupargv (argv
);
995 if (STATE_PROG_ENVP (sd
) != env
)
997 freeargv (STATE_PROG_ENVP (sd
));
998 STATE_PROG_ENVP (sd
) = dupargv (env
);
1001 cb
->argv
= STATE_PROG_ARGV (sd
);
1002 cb
->envp
= STATE_PROG_ENVP (sd
);
1007 /* Disassemble an instruction. */
1010 cris_disassemble_insn (SIM_CPU
*cpu
,
1011 const CGEN_INSN
*insn ATTRIBUTE_UNUSED
,
1012 const ARGBUF
*abuf ATTRIBUTE_UNUSED
,
1013 IADDR pc
, char *buf
)
1015 disassembler_ftype pinsn
;
1016 struct disassemble_info disasm_info
;
1018 SIM_DESC sd
= CPU_STATE (cpu
);
1020 sfile
.buffer
= sfile
.current
= buf
;
1021 INIT_DISASSEMBLE_INFO (disasm_info
, (FILE *) &sfile
,
1022 (fprintf_ftype
) sim_disasm_sprintf
,
1023 (fprintf_styled_ftype
) sim_disasm_styled_sprintf
);
1024 disasm_info
.endian
= BFD_ENDIAN_LITTLE
;
1025 disasm_info
.read_memory_func
= sim_disasm_read_memory
;
1026 disasm_info
.memory_error_func
= sim_disasm_perror_memory
;
1027 disasm_info
.application_data
= cpu
;
1028 pinsn
= cris_get_disassembler (STATE_PROG_BFD (sd
));
1029 (*pinsn
) (pc
, &disasm_info
);