1 /* Main simulator entry points specific to the CRIS.
2 Copyright (C) 2004-2014 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
24 #include "libiberty.h"
33 #include "sim-options.h"
36 /* Apparently the autoconf bits are missing (though HAVE_ENVIRON is used
37 in other dirs; also lacking there). Patch around it for major systems. */
38 #if defined (HAVE_ENVIRON) || defined (__GLIBC__)
39 extern char **environ;
40 #define GET_ENVIRON() environ
42 char *missing_environ[] = { "SHELL=/bin/sh", "PATH=/bin:/usr/bin", NULL };
43 #define GET_ENVIRON() missing_environ
46 /* Used with get_progbounds to find out how much memory is needed for the
47 program. We don't want to allocate more, since that could mask
48 invalid memory accesses program bugs. */
56 static void free_state (SIM_DESC);
57 static void get_progbounds_iterator (bfd *, asection *, void *);
58 static SIM_RC cris_option_handler (SIM_DESC, sim_cpu *, int, char *, int);
60 /* Since we don't build the cgen-opcode table, we use the old
62 static CGEN_DISASSEMBLER cris_disassemble_insn;
64 /* By default, we set up stack and environment variables like the Linux
66 static char cris_bare_iron = 0;
68 /* Whether 0x9000000xx have simulator-specific meanings. */
69 char cris_have_900000xxif = 0;
71 /* Used to optionally override the default start address of the
73 static USI cris_start_address = 0xffffffffu;
75 /* Used to optionally add offsets to the loaded image and its start
76 address. (Not used for the interpreter of dynamically loaded
77 programs or the DSO:s.) */
78 static int cris_program_offset = 0;
80 /* What to do when we face a more or less unknown syscall. */
81 enum cris_unknown_syscall_action_type cris_unknown_syscall_action
82 = CRIS_USYSC_MSG_STOP;
84 /* Records simulator descriptor so utilities like cris_dump_regs can be
86 SIM_DESC current_state;
88 /* CRIS-specific options. */
90 OPTION_CRIS_STATS = OPTION_START,
93 OPTION_CRIS_PROGRAM_OFFSET,
94 OPTION_CRIS_STARTADDR,
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 { {"cris-program-offset", required_argument, NULL,
120 OPTION_CRIS_PROGRAM_OFFSET},
122 "Offset image addresses and default start address of a program",
123 cris_option_handler },
124 { {"cris-start-address", required_argument, NULL, OPTION_CRIS_STARTADDR},
125 '\0', "ADDRESS", "Set start address",
126 cris_option_handler },
127 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
130 /* Add the CRIS-specific option list to the simulator. */
133 cris_option_install (SIM_DESC sd)
135 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
136 if (sim_add_option_table (sd, NULL, cris_options) != SIM_RC_OK)
141 /* Handle CRIS-specific options. */
144 cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt,
145 char *arg, int is_command ATTRIBUTE_UNUSED)
147 /* The options are CRIS-specific, but cpu-specific option-handling is
148 broken; required to being with "--cpu0-". We store the flags in an
149 unused field in the global state structure and move the flags over
150 to the module-specific CPU data when we store things in the
151 cpu-specific structure. */
152 char *tracefp = STATE_TRACE_FLAGS (sd);
155 switch ((CRIS_OPTIONS) opt)
157 case OPTION_CRIS_STATS:
158 if (strcmp (arg, "basic") == 0)
159 *tracefp = FLAG_CRIS_MISC_PROFILE_SIMPLE;
160 else if (strcmp (arg, "unaligned") == 0)
162 = (FLAG_CRIS_MISC_PROFILE_UNALIGNED
163 | FLAG_CRIS_MISC_PROFILE_SIMPLE);
164 else if (strcmp (arg, "schedulable") == 0)
166 = (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
167 | FLAG_CRIS_MISC_PROFILE_SIMPLE);
168 else if (strcmp (arg, "all") == 0)
169 *tracefp = FLAG_CRIS_MISC_PROFILE_ALL;
172 /* Beware; the framework does not handle the error case;
173 we have to do it ourselves. */
174 sim_io_eprintf (sd, "Unknown option `--cris-cycles=%s'\n", arg);
179 case OPTION_CRIS_TRACE:
180 if (strcmp (arg, "basic") == 0)
181 *tracefp |= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE;
184 sim_io_eprintf (sd, "Unknown option `--cris-trace=%s'\n", arg);
189 case OPTION_CRIS_NAKED:
193 case OPTION_CRIS_900000XXIF:
194 cris_have_900000xxif = 1;
197 case OPTION_CRIS_STARTADDR:
199 cris_start_address = (USI) strtoul (chp, &chp, 0);
201 if (errno != 0 || *chp != 0)
203 sim_io_eprintf (sd, "Invalid option `--cris-start-address=%s'\n",
209 case OPTION_CRIS_PROGRAM_OFFSET:
211 cris_program_offset = (int) strtol (chp, &chp, 0);
213 if (errno != 0 || *chp != 0)
215 sim_io_eprintf (sd, "Invalid option `--cris-program-offset=%s'\n",
221 case OPTION_CRIS_UNKNOWN_SYSCALL:
222 if (strcmp (arg, "enosys") == 0)
223 cris_unknown_syscall_action = CRIS_USYSC_MSG_ENOSYS;
224 else if (strcmp (arg, "enosys-quiet") == 0)
225 cris_unknown_syscall_action = CRIS_USYSC_QUIET_ENOSYS;
226 else if (strcmp (arg, "stop") == 0)
227 cris_unknown_syscall_action = CRIS_USYSC_MSG_STOP;
230 sim_io_eprintf (sd, "Unknown option `--cris-unknown-syscall=%s'\n",
237 /* We'll actually never get here; the caller handles the error
239 sim_io_eprintf (sd, "Unknown option `%s'\n", arg);
243 /* Imply --profile-model=on. */
244 return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on");
247 /* FIXME: Remove these, globalize those in sim-load.c, move elsewhere. */
250 xprintf (host_callback *callback, const char *fmt, ...)
256 (*callback->vprintf_filtered) (callback, fmt, ap);
262 eprintf (host_callback *callback, const char *fmt, ...)
268 (*callback->evprintf_filtered) (callback, fmt, ap);
273 /* An ELF-specific simplified ../common/sim-load.c:sim_load_file,
274 using the program headers, not sections, in order to make sure that
275 the program headers themeselves are also loaded. The caller is
276 responsible for asserting that ABFD is an ELF file. */
279 cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write)
281 Elf_Internal_Phdr *phdr;
284 bfd_boolean verbose = STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG;
285 host_callback *callback = STATE_CALLBACK (sd);
287 phdr = elf_tdata (abfd)->phdr;
288 n_hdrs = elf_elfheader (abfd)->e_phnum;
290 /* We're only interested in PT_LOAD; all necessary information
291 should be covered by that. */
292 for (i = 0; i < n_hdrs; i++)
295 bfd_vma lma = STATE_LOAD_AT_LMA_P (sd)
296 ? phdr[i].p_paddr : phdr[i].p_vaddr;
298 if (phdr[i].p_type != PT_LOAD)
301 buf = xmalloc (phdr[i].p_filesz);
304 xprintf (callback, "Loading segment at 0x%lx, size 0x%lx\n",
305 lma, phdr[i].p_filesz);
307 if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0
308 || (bfd_bread (buf, phdr[i].p_filesz, abfd) != phdr[i].p_filesz))
311 "%s: could not read segment at 0x%lx, size 0x%lx\n",
312 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
317 if (do_write (sd, lma, buf, phdr[i].p_filesz) != phdr[i].p_filesz)
320 "%s: could not load segment at 0x%lx, size 0x%lx\n",
321 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
332 /* Helper for sim_load (needed just for ELF files): like sim_write,
333 but offset load at cris_program_offset offset. */
336 cris_program_offset_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf,
339 return sim_write (sd, mem + cris_program_offset, buf, length);
342 /* Replacement for ../common/sim-hload.c:sim_load, so we can treat ELF
343 files differently. */
346 sim_load (SIM_DESC sd, char *prog_name, struct bfd *prog_bfd,
347 int from_tty ATTRIBUTE_UNUSED)
351 if (bfd_get_flavour (prog_bfd) != bfd_target_elf_flavour)
353 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
354 if (sim_analyze_program (sd, prog_name, prog_bfd) != SIM_RC_OK)
356 SIM_ASSERT (STATE_PROG_BFD (sd) != NULL);
358 result_bfd = sim_load_file (sd, STATE_MY_NAME (sd),
362 STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG,
363 STATE_LOAD_AT_LMA_P (sd),
365 if (result_bfd == NULL)
367 bfd_close (STATE_PROG_BFD (sd));
368 STATE_PROG_BFD (sd) = NULL;
374 return cris_load_elf_file (sd, prog_bfd, cris_program_offset_write)
375 ? SIM_RC_OK : SIM_RC_FAIL;
378 /* Cover function of sim_state_free to free the cpu buffers as well. */
381 free_state (SIM_DESC sd)
383 if (STATE_MODULES (sd) != NULL)
384 sim_module_uninstall (sd);
385 sim_cpu_free_all (sd);
389 /* Helper struct for cris_set_section_offset_iterator. */
397 /* BFD section iterator to offset the LMA and VMA. */
400 cris_set_section_offset_iterator (bfd *abfd, asection *s, void *vp)
402 struct offsetinfo *p = (struct offsetinfo *) vp;
404 int offset = p->offset;
406 if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC))
408 bfd_vma vma = bfd_get_section_vma (abfd, s);
410 bfd_set_section_vma (abfd, s, vma + offset);
413 /* This seems clumsy and inaccurate, but let's stick to doing it the
414 same way as sim_analyze_program for consistency. */
415 if (strcmp (bfd_get_section_name (abfd, s), ".text") == 0)
416 STATE_TEXT_START (sd) = bfd_get_section_vma (abfd, s);
419 /* Adjust the start-address, LMA and VMA of a SD. Must be called
420 after sim_analyze_program. */
423 cris_offset_sections (SIM_DESC sd, int offset)
426 struct bfd *abfd = STATE_PROG_BFD (sd);
428 struct offsetinfo oi;
430 /* Only happens for usage error. */
437 bfd_map_over_sections (abfd, cris_set_section_offset_iterator, &oi);
438 ret = bfd_set_start_address (abfd, bfd_get_start_address (abfd) + offset);
440 STATE_START_ADDR (sd) = bfd_get_start_address (abfd);
443 /* BFD section iterator to find the highest and lowest allocated and
444 non-allocated section addresses (plus one). */
447 get_progbounds_iterator (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *vp)
449 struct progbounds *pbp = (struct progbounds *) vp;
451 if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC))
453 bfd_size_type sec_size = bfd_get_section_size (s);
454 bfd_size_type sec_start = bfd_get_section_vma (abfd, s);
455 bfd_size_type sec_end = sec_start + sec_size;
457 if (sec_end > pbp->endmem)
458 pbp->endmem = sec_end;
460 if (sec_start < pbp->startmem)
461 pbp->startmem = sec_start;
463 if ((bfd_get_section_flags (abfd, s) & SEC_LOAD))
465 if (sec_end > pbp->end_loadmem)
466 pbp->end_loadmem = sec_end;
468 else if (sec_start < pbp->start_nonloadmem)
469 pbp->start_nonloadmem = sec_start;
473 /* Get the program boundaries. Because not everything is covered by
474 sections in ELF, notably the program headers, we use the program
478 cris_get_progbounds (struct bfd *abfd, struct progbounds *pbp)
480 Elf_Internal_Phdr *phdr;
484 pbp->startmem = 0xffffffff;
486 pbp->end_loadmem = 0;
487 pbp->start_nonloadmem = 0xffffffff;
489 /* In case we're ever used for something other than ELF, use the
491 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
493 bfd_map_over_sections (abfd, get_progbounds_iterator, pbp);
497 phdr = elf_tdata (abfd)->phdr;
498 n_hdrs = elf_elfheader (abfd)->e_phnum;
500 /* We're only interested in PT_LOAD; all necessary information
501 should be covered by that. */
502 for (i = 0; i < n_hdrs; i++)
504 if (phdr[i].p_type != PT_LOAD)
507 if (phdr[i].p_paddr < pbp->startmem)
508 pbp->startmem = phdr[i].p_paddr;
510 if (phdr[i].p_paddr + phdr[i].p_memsz > pbp->endmem)
511 pbp->endmem = phdr[i].p_paddr + phdr[i].p_memsz;
513 if (phdr[i].p_paddr + phdr[i].p_filesz > pbp->end_loadmem)
514 pbp->end_loadmem = phdr[i].p_paddr + phdr[i].p_filesz;
516 if (phdr[i].p_memsz > phdr[i].p_filesz
517 && phdr[i].p_paddr + phdr[i].p_filesz < pbp->start_nonloadmem)
518 pbp->start_nonloadmem = phdr[i].p_paddr + phdr[i].p_filesz;
522 /* Parameter communication by static variables, hmm... Oh well, for
524 static bfd_vma exec_load_addr;
525 static bfd_vma interp_load_addr;
526 static bfd_vma interp_start_addr;
528 /* Supposed to mimic Linux' "NEW_AUX_ENT (AT_PHDR, load_addr + exec->e_phoff)". */
531 aux_ent_phdr (struct bfd *ebfd)
533 return elf_elfheader (ebfd)->e_phoff + exec_load_addr;
536 /* We just pass on the header info; we don't have our own idea of the
537 program header entry size. */
540 aux_ent_phent (struct bfd *ebfd)
542 return elf_elfheader (ebfd)->e_phentsize;
545 /* Like "NEW_AUX_ENT(AT_PHNUM, exec->e_phnum)". */
548 aux_ent_phnum (struct bfd *ebfd)
550 return elf_elfheader (ebfd)->e_phnum;
553 /* Like "NEW_AUX_ENT(AT_BASE, interp_load_addr)". */
556 aux_ent_base (struct bfd *ebfd)
558 return interp_load_addr;
561 /* Like "NEW_AUX_ENT(AT_ENTRY, exec->e_entry)". */
564 aux_ent_entry (struct bfd *ebfd)
566 ASSERT (elf_elfheader (ebfd)->e_entry == bfd_get_start_address (ebfd));
567 return elf_elfheader (ebfd)->e_entry;
570 /* Helper for cris_handle_interpreter: like sim_write, but load at
571 interp_load_addr offset. */
574 cris_write_interp (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
576 return sim_write (sd, mem + interp_load_addr, buf, length);
579 /* Cater to the presence of an interpreter: load it and set
580 interp_start_addr. Return FALSE if there was an error, TRUE if
581 everything went fine, including an interpreter being absent and
582 the program being in a non-ELF format. */
585 cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd)
592 bfd_boolean ok = FALSE;
593 Elf_Internal_Phdr *phdr;
595 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
598 phdr = elf_tdata (abfd)->phdr;
599 n_hdrs = aux_ent_phnum (abfd);
601 /* Check the program headers for presence of an interpreter. */
602 for (i = 0; i < n_hdrs; i++)
605 bfd_size_type interpsiz, interp_filesiz;
606 struct progbounds interp_bounds;
608 if (phdr[i].p_type != PT_INTERP)
611 /* Get the name of the interpreter, prepended with the sysroot
612 (empty if absent). */
613 interplen = phdr[i].p_filesz;
614 interp = xmalloc (interplen + strlen (simulator_sysroot));
615 strcpy (interp, simulator_sysroot);
617 /* Read in the name. */
618 if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0
619 || (bfd_bread (interp + strlen (simulator_sysroot), interplen, abfd)
621 goto interpname_failed;
623 /* Like Linux, require the string to be 0-terminated. */
624 if (interp[interplen + strlen (simulator_sysroot) - 1] != 0)
625 goto interpname_failed;
627 /* Inspect the interpreter. */
628 ibfd = bfd_openr (interp, STATE_TARGET (sd));
630 goto interpname_failed;
632 /* The interpreter is at least something readable to BFD; make
633 sure it's an ELF non-archive file. */
634 if (!bfd_check_format (ibfd, bfd_object)
635 || bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
638 /* Check the layout of the interpreter. */
639 cris_get_progbounds (ibfd, &interp_bounds);
641 /* Round down to pagesize the start page and up the endpage.
642 Don't round the *load and *nonload members. */
643 interp_bounds.startmem &= ~8191;
644 interp_bounds.endmem = (interp_bounds.endmem + 8191) & ~8191;
646 /* Until we need a more dynamic solution, assume we can put the
647 interpreter at this fixed location. NB: this is not what
648 happens for Linux 2008-12-28, but it could and might and
650 interp_load_addr = 0x40000;
651 interpsiz = interp_bounds.endmem - interp_bounds.startmem;
652 interp_filesiz = interp_bounds.end_loadmem - interp_bounds.startmem;
654 /* If we have a non-DSO or interpreter starting at the wrong
656 if (interp_bounds.startmem != 0
657 || interpsiz + interp_load_addr >= exec_load_addr)
660 /* We don't have the API to get the address of a simulator
661 memory area, so we go via a temporary area. Luckily, the
662 interpreter is supposed to be small, less than 0x40000
664 sim_do_commandf (sd, "memory region 0x%lx,0x%lx",
665 interp_load_addr, interpsiz);
667 /* Now that memory for the interpreter is defined, load it. */
668 if (!cris_load_elf_file (sd, ibfd, cris_write_interp))
671 /* It's no use setting STATE_START_ADDR, because it gets
672 overwritten by a sim_analyze_program call in sim_load. Let's
673 just store it locally. */
675 = (bfd_get_start_address (ibfd)
676 - interp_bounds.startmem + interp_load_addr);
678 /* Linux cares only about the first PT_INTERP, so let's ignore
683 /* Register R10 should hold 0 at static start (no finifunc), but
684 that's the default, so don't bother. */
696 "%s: could not load ELF interpreter `%s' for program `%s'\n",
698 interp == NULL ? "(what's-its-name)" : interp,
699 bfd_get_filename (abfd));
704 /* Create an instance of the simulator. */
707 sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
713 USI endmem = CRIS_DEFAULT_MEM_SIZE;
716 SIM_DESC sd = sim_state_alloc (kind, callback);
718 static const struct auxv_entries_s
721 USI (*efn) (struct bfd *ebfd);
725 #define AUX_ENT(a, b) {a, NULL, b}
726 #define AUX_ENTF(a, f) {a, f, 0}
727 AUX_ENT (AT_HWCAP, 0),
728 AUX_ENT (AT_PAGESZ, 8192),
729 AUX_ENT (AT_CLKTCK, 100),
730 AUX_ENTF (AT_PHDR, aux_ent_phdr),
731 AUX_ENTF (AT_PHENT, aux_ent_phent),
732 AUX_ENTF (AT_PHNUM, aux_ent_phnum),
733 AUX_ENTF (AT_BASE, aux_ent_base),
734 AUX_ENT (AT_FLAGS, 0),
735 AUX_ENTF (AT_ENTRY, aux_ent_entry),
737 /* Or is root better? Maybe have it settable? */
738 AUX_ENT (AT_UID, 500),
739 AUX_ENT (AT_EUID, 500),
740 AUX_ENT (AT_GID, 500),
741 AUX_ENT (AT_EGID, 500),
742 AUX_ENT (AT_SECURE, 0),
746 /* Can't initialize to "" below. It's either a GCC bug in old
747 releases (up to and including 2.95.3 (.4 in debian) or a bug in the
748 standard ;-) that the rest of the elements won't be initialized. */
749 bfd_byte sp_init[4] = {0, 0, 0, 0};
751 /* The cpu data is kept in a separately allocated chunk of memory. */
752 if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
758 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
764 /* getopt will print the error message so we just have to exit if this fails.
765 FIXME: Hmmm... in the case of gdb we need getopt to call
767 if (sim_parse_args (sd, argv) != SIM_RC_OK)
773 /* If we have a binary program, endianness-setting would not be taken
774 from elsewhere unfortunately, so set it here. At the time of this
775 writing, it isn't used until sim_config, but that might change so
776 set it here before memory is defined or touched. */
777 current_target_byte_order = LITTLE_ENDIAN;
779 /* check for/establish the reference program image */
780 if (sim_analyze_program (sd,
781 (STATE_PROG_ARGV (sd) != NULL
782 ? *STATE_PROG_ARGV (sd)
786 /* When there's an error, sim_analyze_program has already output
787 a message. Let's just clarify it, as "not an object file"
788 perhaps doesn't ring a bell. */
789 sim_io_eprintf (sd, "(not a CRIS program)\n");
794 /* We might get called with the caller expecting us to get hold of
795 the bfd for ourselves, which would happen at the
796 sim_analyze_program call above. */
798 abfd = STATE_PROG_BFD (sd);
800 /* Adjust the addresses of the program at this point. Unfortunately
801 this does not affect ELF program headers, so we have to handle
803 cris_offset_sections (sd, cris_program_offset);
805 if (abfd != NULL && bfd_get_arch (abfd) == bfd_arch_unknown)
807 if (STATE_PROG_ARGV (sd) != NULL)
808 sim_io_eprintf (sd, "%s: `%s' is not a CRIS program\n",
809 STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd));
811 sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n",
817 /* For CRIS simulator-specific use, we need to find out the bounds of
818 the program as well, which is not done by sim_analyze_program
822 struct progbounds pb;
824 /* The sections should now be accessible using bfd functions. */
825 cris_get_progbounds (abfd, &pb);
827 /* We align the area that the program uses to page boundaries. */
828 startmem = pb.startmem & ~8191;
830 endmem = (endbrk + 8191) & ~8191;
833 /* Find out how much room is needed for the environment and argv, create
834 that memory and fill it. Only do this when there's a program
836 if (abfd != NULL && !cris_bare_iron)
838 char *name = bfd_get_filename (abfd);
839 char **my_environ = GET_ENVIRON ();
840 /* We use these maps to give the same behavior as the old xsim
842 USI envtop = 0x40000000;
843 USI stacktop = 0x3e000000;
846 int len = strlen (name) + 1;
850 char **prog_argv = STATE_PROG_ARGV (sd);
852 /* All CPU:s have the same memory map, apparently. */
853 SIM_CPU *cpu = STATE_CPU (sd, 0);
857 /* Count in the environment as well. */
858 for (envc = 0; my_environ[envc] != NULL; envc++)
859 len += strlen (my_environ[envc]) + 1;
861 for (i = 0; prog_argv[i] != NULL; my_argc++, i++)
862 len += strlen (prog_argv[i]) + 1;
864 envstart = (envtop - len) & ~8191;
866 /* Create read-only block for the environment strings. */
867 sim_core_attach (sd, NULL, 0, access_read, 0,
868 envstart, (len + 8191) & ~8191,
871 /* This shouldn't happen. */
872 if (envstart < stacktop)
873 stacktop = envstart - 64 * 8192;
877 /* Note that the linux kernel does not correctly compute the storage
878 needs for the static-exe AUX vector. */
880 csp -= sizeof (auxv_entries) / sizeof (auxv_entries[0]) * 4 * 2;
882 csp -= (envc + 1) * 4;
883 csp -= (my_argc + 1) * 4;
886 /* Write the target representation of the start-up-value for the
887 stack-pointer suitable for register initialization below. */
888 bfd_putl32 (csp, sp_init);
890 /* If we make this 1M higher; say 8192*1024, we have to take
891 special precautions for pthreads, because pthreads assumes that
892 the memory that low isn't mmapped, and that it can mmap it
893 without fallback in case of failure (and we fail ungracefully
894 long before *that*: the memory isn't accounted for in our mmap
896 stack_low = (csp - (7168*1024)) & ~8191;
898 stacklen = stacktop - stack_low;
900 /* Tee hee, we have an executable stack. Well, it's necessary to
901 test GCC trampolines... */
902 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
906 epp = epp0 = envstart;
908 /* Can't use sim_core_write_unaligned_4 without everything
909 initialized when tracing, and then these writes would get into
911 #define write_dword(addr, data) \
916 bfd_putl32 (data_, buf); \
917 if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \
922 write_dword (csp, my_argc);
925 for (i = 0; i < my_argc; i++, csp += 4)
927 size_t strln = strlen (prog_argv[i]) + 1;
929 if (sim_core_write_buffer (sd, cpu, 0, prog_argv[i], epp, strln)
933 write_dword (csp, envstart + epp - epp0);
937 write_dword (csp, 0);
940 for (i = 0; i < envc; i++, csp += 4)
942 unsigned int strln = strlen (my_environ[i]) + 1;
944 if (sim_core_write_buffer (sd, cpu, 0, my_environ[i], epp, strln)
948 write_dword (csp, envstart + epp - epp0);
952 write_dword (csp, 0);
955 /* The load address of the executable could presumably be
956 different than the lowest used memory address, but let's
957 stick to simplicity until needed. And
958 cris_handle_interpreter might change startmem and endmem, so
960 exec_load_addr = startmem;
962 if (!cris_handle_interpreter (sd, abfd))
965 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
966 for (i = 0; i < sizeof (auxv_entries) / sizeof (auxv_entries[0]); i++)
968 write_dword (csp, auxv_entries[i].id);
969 write_dword (csp + 4,
970 auxv_entries[i].efn != NULL
971 ? (*auxv_entries[i].efn) (abfd)
972 : auxv_entries[i].val);
977 /* Allocate core managed memory if none specified by user. */
978 if (sim_core_read_buffer (sd, NULL, read_map, &c, startmem, 1) == 0)
979 sim_do_commandf (sd, "memory region 0x%lx,0x%lx", startmem,
982 /* Allocate simulator I/O managed memory if none specified by user. */
983 if (cris_have_900000xxif)
985 if (sim_core_read_buffer (sd, NULL, read_map, &c, 0x90000000, 1) == 0)
986 sim_core_attach (sd, NULL, 0, access_write, 0, 0x90000000, 0x100,
987 0, &cris_devices, NULL);
991 printf_filtered) (callback,
992 "Seeing --cris-900000xx with memory defined there\n");
997 /* Establish any remaining configuration options. */
998 if (sim_config (sd) != SIM_RC_OK)
1005 if (sim_post_argv_init (sd) != SIM_RC_OK)
1011 /* Open a copy of the cpu descriptor table. */
1013 CGEN_CPU_DESC cd = cris_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
1014 CGEN_ENDIAN_LITTLE);
1015 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
1017 SIM_CPU *cpu = STATE_CPU (sd, i);
1018 CPU_CPU_DESC (cpu) = cd;
1019 CPU_DISASSEMBLER (cpu) = cris_disassemble_insn;
1021 /* See cris_option_handler for the reason why this is needed. */
1022 CPU_CRIS_MISC_PROFILE (cpu)->flags = STATE_TRACE_FLAGS (sd)[0];
1024 /* Set SP to the stack we allocated above. */
1025 (* CPU_REG_STORE (cpu)) (cpu, H_GR_SP, (char *) sp_init, 4);
1027 /* Set the simulator environment data. */
1028 cpu->highest_mmapped_page = NULL;
1029 cpu->endmem = endmem;
1030 cpu->endbrk = endbrk;
1031 cpu->stack_low = stack_low;
1035 cpu->max_threadid = 0;
1036 cpu->thread_data = NULL;
1037 memset (cpu->sighandler, 0, sizeof (cpu->sighandler));
1038 cpu->make_thread_cpu_data = NULL;
1039 cpu->thread_cpu_data_size = 0;
1041 cpu->deliver_interrupt = NULL;
1045 /* Always be cycle-accurate and call before/after functions if
1047 sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on");
1051 /* Initialize various cgen things not done by common framework.
1052 Must be done after cris_cgen_cpu_open. */
1055 /* Store in a global so things like cris_dump_regs can be invoked
1056 from the gdb command line. */
1059 cris_set_callbacks (callback);
1065 sim_close (SIM_DESC sd, int quitting ATTRIBUTE_UNUSED)
1067 cris_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd, 0)));
1068 sim_module_uninstall (sd);
1072 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
1073 char **argv ATTRIBUTE_UNUSED,
1074 char **envp ATTRIBUTE_UNUSED)
1076 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
1080 addr = cris_start_address != (SIM_ADDR) -1
1081 ? cris_start_address
1082 : (interp_start_addr != 0
1084 : bfd_get_start_address (abfd));
1087 sim_pc_set (current_cpu, addr);
1089 /* Other simulators have #if 0:d code that says
1090 STATE_ARGV (sd) = sim_copy_argv (argv);
1091 STATE_ENVP (sd) = sim_copy_argv (envp);
1092 Enabling that gives you not-found link-errors for sim_copy_argv.
1093 FIXME: Do archaeology to find out more. */
1098 /* Disassemble an instruction. */
1101 cris_disassemble_insn (SIM_CPU *cpu,
1102 const CGEN_INSN *insn ATTRIBUTE_UNUSED,
1103 const ARGBUF *abuf ATTRIBUTE_UNUSED,
1104 IADDR pc, char *buf)
1106 disassembler_ftype pinsn;
1107 struct disassemble_info disasm_info;
1109 SIM_DESC sd = CPU_STATE (cpu);
1111 sfile.buffer = sfile.current = buf;
1112 INIT_DISASSEMBLE_INFO (disasm_info, (FILE *) &sfile,
1113 (fprintf_ftype) sim_disasm_sprintf);
1114 disasm_info.endian = BFD_ENDIAN_LITTLE;
1115 disasm_info.read_memory_func = sim_disasm_read_memory;
1116 disasm_info.memory_error_func = sim_disasm_perror_memory;
1117 disasm_info.application_data = (PTR) cpu;
1118 pinsn = cris_get_disassembler (STATE_PROG_BFD (sd));
1119 (*pinsn) (pc, &disasm_info);