* ppc-linux-nat.c (have_ptrace_getsetregs): New variable.
[gdb/SamB.git] / sim / cris / sim-if.c
blob9135ccbc2d2f64891f57aad02820d5c9b3b900c8
1 /* Main simulator entry points specific to the CRIS.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Axis Communications.
6 This file is part of the GNU simulators.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Based on the fr30 file, mixing in bits from the i960 and pruning of
22 dead code. */
24 #include "libiberty.h"
25 #include "bfd.h"
26 #include "elf-bfd.h"
28 #include "sim-main.h"
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #include <errno.h>
33 #include "sim-options.h"
34 #include "dis-asm.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
41 #else
42 char *missing_environ[] = { "SHELL=/bin/sh", "PATH=/bin:/usr/bin", NULL };
43 #define GET_ENVIRON() missing_environ
44 #endif
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. */
49 struct progbounds {
50 USI startmem;
51 USI endmem;
52 USI end_loadmem;
53 USI start_nonloadmem;
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
61 disassembler. */
62 static CGEN_DISASSEMBLER cris_disassemble_insn;
64 /* By default, we set up stack and environment variables like the Linux
65 kernel. */
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
72 simulation. */
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
85 called from gdb. */
86 SIM_DESC current_state;
88 /* CRIS-specific options. */
89 typedef enum {
90 OPTION_CRIS_STATS = OPTION_START,
91 OPTION_CRIS_TRACE,
92 OPTION_CRIS_NAKED,
93 OPTION_CRIS_PROGRAM_OFFSET,
94 OPTION_CRIS_STARTADDR,
95 OPTION_CRIS_900000XXIF,
96 OPTION_CRIS_UNKNOWN_SYSCALL
97 } CRIS_OPTIONS;
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},
106 '\0', "basic",
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},
121 '\0', "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. */
132 SIM_RC
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)
137 return SIM_RC_FAIL;
138 return SIM_RC_OK;
141 /* Handle CRIS-specific options. */
143 static SIM_RC
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);
153 char *chp = arg;
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)
161 *tracefp
162 = (FLAG_CRIS_MISC_PROFILE_UNALIGNED
163 | FLAG_CRIS_MISC_PROFILE_SIMPLE);
164 else if (strcmp (arg, "schedulable") == 0)
165 *tracefp
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;
170 else
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);
175 return SIM_RC_FAIL;
177 break;
179 case OPTION_CRIS_TRACE:
180 if (strcmp (arg, "basic") == 0)
181 *tracefp |= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE;
182 else
184 sim_io_eprintf (sd, "Unknown option `--cris-trace=%s'\n", arg);
185 return SIM_RC_FAIL;
187 break;
189 case OPTION_CRIS_NAKED:
190 cris_bare_iron = 1;
191 break;
193 case OPTION_CRIS_900000XXIF:
194 cris_have_900000xxif = 1;
195 break;
197 case OPTION_CRIS_STARTADDR:
198 errno = 0;
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",
204 arg);
205 return SIM_RC_FAIL;
207 break;
209 case OPTION_CRIS_PROGRAM_OFFSET:
210 errno = 0;
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",
216 arg);
217 return SIM_RC_FAIL;
219 break;
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;
228 else
230 sim_io_eprintf (sd, "Unknown option `--cris-unknown-syscall=%s'\n",
231 arg);
232 return SIM_RC_FAIL;
234 break;
236 default:
237 /* We'll actually never get here; the caller handles the error
238 case. */
239 sim_io_eprintf (sd, "Unknown option `%s'\n", arg);
240 return SIM_RC_FAIL;
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. */
249 static void
250 xprintf (host_callback *callback, const char *fmt, ...)
252 va_list ap;
254 va_start (ap, fmt);
256 (*callback->vprintf_filtered) (callback, fmt, ap);
258 va_end (ap);
261 static void
262 eprintf (host_callback *callback, const char *fmt, ...)
264 va_list ap;
266 va_start (ap, fmt);
268 (*callback->evprintf_filtered) (callback, fmt, ap);
270 va_end (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. */
278 static bfd_boolean
279 cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write)
281 Elf_Internal_Phdr *phdr;
282 int n_hdrs;
283 int i;
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++)
294 bfd_byte *buf;
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)
299 continue;
301 buf = xmalloc (phdr[i].p_filesz);
303 if (verbose)
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))
310 eprintf (callback,
311 "%s: could not read segment at 0x%lx, size 0x%lx\n",
312 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
313 free (buf);
314 return FALSE;
317 if (do_write (sd, lma, buf, phdr[i].p_filesz) != phdr[i].p_filesz)
319 eprintf (callback,
320 "%s: could not load segment at 0x%lx, size 0x%lx\n",
321 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
322 free (buf);
323 return FALSE;
326 free (buf);
329 return TRUE;
332 /* Helper for sim_load (needed just for ELF files): like sim_write,
333 but offset load at cris_program_offset offset. */
335 static int
336 cris_program_offset_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf,
337 int length)
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. */
345 SIM_RC
346 sim_load (SIM_DESC sd, char *prog_name, struct bfd *prog_bfd,
347 int from_tty ATTRIBUTE_UNUSED)
349 bfd *result_bfd;
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)
355 return SIM_RC_FAIL;
356 SIM_ASSERT (STATE_PROG_BFD (sd) != NULL);
358 result_bfd = sim_load_file (sd, STATE_MY_NAME (sd),
359 STATE_CALLBACK (sd),
360 prog_name,
361 STATE_PROG_BFD (sd),
362 STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG,
363 STATE_LOAD_AT_LMA_P (sd),
364 sim_write);
365 if (result_bfd == NULL)
367 bfd_close (STATE_PROG_BFD (sd));
368 STATE_PROG_BFD (sd) = NULL;
369 return SIM_RC_FAIL;
371 return SIM_RC_OK;
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. */
380 static void
381 free_state (SIM_DESC sd)
383 if (STATE_MODULES (sd) != NULL)
384 sim_module_uninstall (sd);
385 sim_cpu_free_all (sd);
386 sim_state_free (sd);
389 /* Helper struct for cris_set_section_offset_iterator. */
391 struct offsetinfo
393 SIM_DESC sd;
394 int offset;
397 /* BFD section iterator to offset the LMA and VMA. */
399 static void
400 cris_set_section_offset_iterator (bfd *abfd, asection *s, void *vp)
402 struct offsetinfo *p = (struct offsetinfo *) vp;
403 SIM_DESC sd = p->sd;
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. */
422 static void
423 cris_offset_sections (SIM_DESC sd, int offset)
425 bfd_boolean ret;
426 struct bfd *abfd = STATE_PROG_BFD (sd);
427 asection *text;
428 struct offsetinfo oi;
430 /* Only happens for usage error. */
431 if (abfd == NULL)
432 return;
434 oi.sd = sd;
435 oi.offset = offset;
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). */
446 static void
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
475 headers instead. */
477 static void
478 cris_get_progbounds (struct bfd *abfd, struct progbounds *pbp)
480 Elf_Internal_Phdr *phdr;
481 int n_hdrs;
482 int i;
484 pbp->startmem = 0xffffffff;
485 pbp->endmem = 0;
486 pbp->end_loadmem = 0;
487 pbp->start_nonloadmem = 0xffffffff;
489 /* In case we're ever used for something other than ELF, use the
490 generic method. */
491 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
493 bfd_map_over_sections (abfd, get_progbounds_iterator, pbp);
494 return;
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)
505 continue;
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
523 simplicity. */
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)". */
530 static USI
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. */
539 static USI
540 aux_ent_phent (struct bfd *ebfd)
542 return elf_elfheader (ebfd)->e_phentsize;
545 /* Like "NEW_AUX_ENT(AT_PHNUM, exec->e_phnum)". */
547 static USI
548 aux_ent_phnum (struct bfd *ebfd)
550 return elf_elfheader (ebfd)->e_phnum;
553 /* Like "NEW_AUX_ENT(AT_BASE, interp_load_addr)". */
555 static USI
556 aux_ent_base (struct bfd *ebfd)
558 return interp_load_addr;
561 /* Like "NEW_AUX_ENT(AT_ENTRY, exec->e_entry)". */
563 static USI
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. */
573 static int
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. */
584 static bfd_boolean
585 cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd)
587 int i, n_hdrs;
588 bfd_vma phaddr;
589 bfd_byte buf[4];
590 char *interp = NULL;
591 struct bfd *ibfd;
592 bfd_boolean ok = FALSE;
593 Elf_Internal_Phdr *phdr;
595 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
596 return TRUE;
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++)
604 int interplen;
605 bfd_size_type interpsiz, interp_filesiz;
606 struct progbounds interp_bounds;
608 if (phdr[i].p_type != PT_INTERP)
609 continue;
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)
620 != interplen))
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));
629 if (ibfd == NULL)
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)
636 goto interp_failed;
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
649 perhaps should. */
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
655 address, bail. */
656 if (interp_bounds.startmem != 0
657 || interpsiz + interp_load_addr >= exec_load_addr)
658 goto interp_failed;
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
663 bytes. */
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))
669 goto interp_failed;
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. */
674 interp_start_addr
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
679 the rest. */
680 goto all_done;
683 /* Register R10 should hold 0 at static start (no finifunc), but
684 that's the default, so don't bother. */
685 return TRUE;
687 all_done:
688 ok = TRUE;
690 interp_failed:
691 bfd_close (ibfd);
693 interpname_failed:
694 if (!ok)
695 sim_io_eprintf (sd,
696 "%s: could not load ELF interpreter `%s' for program `%s'\n",
697 STATE_MY_NAME (sd),
698 interp == NULL ? "(what's-its-name)" : interp,
699 bfd_get_filename (abfd));
700 free (interp);
701 return ok;
704 /* Create an instance of the simulator. */
706 SIM_DESC
707 sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
708 char **argv)
710 char c;
711 int i;
712 USI startmem = 0;
713 USI endmem = CRIS_DEFAULT_MEM_SIZE;
714 USI endbrk = endmem;
715 USI stack_low = 0;
716 SIM_DESC sd = sim_state_alloc (kind, callback);
718 static const struct auxv_entries_s
720 bfd_byte id;
721 USI (*efn) (struct bfd *ebfd);
722 USI val;
723 } auxv_entries[] =
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),
743 AUX_ENT (AT_NULL, 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)
754 free_state (sd);
755 return 0;
758 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
760 free_state (sd);
761 return 0;
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
766 print_filtered. */
767 if (sim_parse_args (sd, argv) != SIM_RC_OK)
769 free_state (sd);
770 return 0;
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)
783 : NULL),
784 abfd) != SIM_RC_OK)
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");
790 free_state (sd);
791 return 0;
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. */
797 if (abfd == NULL)
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
802 that separately. */
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));
810 else
811 sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n",
812 STATE_MY_NAME (sd));
813 free_state (sd);
814 return 0;
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
819 above. */
820 if (abfd != NULL)
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;
829 endbrk = pb.endmem;
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
835 specified. */
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
841 simulator. */
842 USI envtop = 0x40000000;
843 USI stacktop = 0x3e000000;
844 USI envstart;
845 int envc;
846 int len = strlen (name) + 1;
847 USI epp, epp0;
848 USI stacklen;
849 int i;
850 char **prog_argv = STATE_PROG_ARGV (sd);
851 int my_argc = 0;
852 /* All CPU:s have the same memory map, apparently. */
853 SIM_CPU *cpu = STATE_CPU (sd, 0);
854 USI csp;
855 bfd_byte buf[4];
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,
869 0, NULL, NULL);
871 /* This shouldn't happen. */
872 if (envstart < stacktop)
873 stacktop = envstart - 64 * 8192;
875 csp = stacktop;
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;
884 csp -= 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
895 list). */
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,
903 stack_low, stacklen,
904 0, NULL, NULL);
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
910 the trace. */
911 #define write_dword(addr, data) \
912 do \
914 USI data_ = data; \
915 USI addr_ = addr; \
916 bfd_putl32 (data_, buf); \
917 if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \
918 goto abandon_chip; \
920 while (0)
922 write_dword (csp, my_argc);
923 csp += 4;
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)
930 != strln)
931 goto abandon_chip;
933 write_dword (csp, envstart + epp - epp0);
934 epp += strln;
937 write_dword (csp, 0);
938 csp += 4;
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)
945 != strln)
946 goto abandon_chip;
948 write_dword (csp, envstart + epp - epp0);
949 epp += strln;
952 write_dword (csp, 0);
953 csp += 4;
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
959 let's set it now. */
960 exec_load_addr = startmem;
962 if (!cris_handle_interpreter (sd, abfd))
963 goto abandon_chip;
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);
973 csp += 4 + 4;
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,
980 endmem - 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);
988 else
990 (*callback->
991 printf_filtered) (callback,
992 "Seeing --cris-900000xx with memory defined there\n");
993 goto abandon_chip;
997 /* Establish any remaining configuration options. */
998 if (sim_config (sd) != SIM_RC_OK)
1000 abandon_chip:
1001 free_state (sd);
1002 return 0;
1005 if (sim_post_argv_init (sd) != SIM_RC_OK)
1007 free_state (sd);
1008 return 0;
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;
1032 cpu->syscalls = 0;
1033 cpu->m1threads = 0;
1034 cpu->threadno = 0;
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;
1040 #if WITH_HW
1041 cpu->deliver_interrupt = NULL;
1042 #endif
1044 #if WITH_HW
1045 /* Always be cycle-accurate and call before/after functions if
1046 with-hardware. */
1047 sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on");
1048 #endif
1051 /* Initialize various cgen things not done by common framework.
1052 Must be done after cris_cgen_cpu_open. */
1053 cgen_init (sd);
1055 /* Store in a global so things like cris_dump_regs can be invoked
1056 from the gdb command line. */
1057 current_state = sd;
1059 cris_set_callbacks (callback);
1061 return sd;
1064 void
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);
1071 SIM_RC
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);
1077 SIM_ADDR addr;
1079 if (sd != NULL)
1080 addr = cris_start_address != (SIM_ADDR) -1
1081 ? cris_start_address
1082 : (interp_start_addr != 0
1083 ? interp_start_addr
1084 : bfd_get_start_address (abfd));
1085 else
1086 addr = 0;
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. */
1095 return SIM_RC_OK;
1098 void
1099 sim_do_command (SIM_DESC sd, char *cmd)
1101 if (sim_args_command (sd, cmd) != SIM_RC_OK)
1102 sim_io_eprintf (sd, "Unknown command `%s'\n", cmd);
1105 /* Disassemble an instruction. */
1107 static void
1108 cris_disassemble_insn (SIM_CPU *cpu,
1109 const CGEN_INSN *insn ATTRIBUTE_UNUSED,
1110 const ARGBUF *abuf ATTRIBUTE_UNUSED,
1111 IADDR pc, char *buf)
1113 disassembler_ftype pinsn;
1114 struct disassemble_info disasm_info;
1115 SFILE sfile;
1116 SIM_DESC sd = CPU_STATE (cpu);
1118 sfile.buffer = sfile.current = buf;
1119 INIT_DISASSEMBLE_INFO (disasm_info, (FILE *) &sfile,
1120 (fprintf_ftype) sim_disasm_sprintf);
1121 disasm_info.endian = BFD_ENDIAN_LITTLE;
1122 disasm_info.read_memory_func = sim_disasm_read_memory;
1123 disasm_info.memory_error_func = sim_disasm_perror_memory;
1124 disasm_info.application_data = (PTR) cpu;
1125 pinsn = cris_get_disassembler (STATE_PROG_BFD (sd));
1126 (*pinsn) (pc, &disasm_info);