* cris/Makefile.in (SIM_OBJS): Remove sim-hload.o.
[deliverable/binutils-gdb.git] / sim / cris / sim-if.c
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.
4
5 This file is part of the GNU simulators.
6
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.
11
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.
16
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/>. */
19
20 /* Based on the fr30 file, mixing in bits from the i960 and pruning of
21 dead code. */
22
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "elf-bfd.h"
26
27 #include "sim-main.h"
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #include "sim-options.h"
32 #include "dis-asm.h"
33
34 /* Apparently the autoconf bits are missing (though HAVE_ENVIRON is used
35 in other dirs; also lacking there). Patch around it for major systems. */
36 #if defined (HAVE_ENVIRON) || defined (__GLIBC__)
37 extern char **environ;
38 #define GET_ENVIRON() environ
39 #else
40 char *missing_environ[] = { "SHELL=/bin/sh", "PATH=/bin:/usr/bin", NULL };
41 #define GET_ENVIRON() missing_environ
42 #endif
43
44 /* AUX vector entries. */
45 #define TARGET_AT_NULL 0
46 #define TARGET_AT_PHDR 3
47 #define TARGET_AT_PHENT 4
48 #define TARGET_AT_PHNUM 5
49 #define TARGET_AT_PAGESZ 6
50 #define TARGET_AT_BASE 7
51 #define TARGET_AT_FLAGS 8
52 #define TARGET_AT_ENTRY 9
53 #define TARGET_AT_UID 11
54 #define TARGET_AT_EUID 12
55 #define TARGET_AT_GID 13
56 #define TARGET_AT_EGID 14
57 #define TARGET_AT_HWCAP 16
58 #define TARGET_AT_CLKTCK 17
59
60 /* Used with get_progbounds to find out how much memory is needed for the
61 program. We don't want to allocate more, since that could mask
62 invalid memory accesses program bugs. */
63 struct progbounds {
64 USI startmem;
65 USI endmem;
66 USI end_loadmem;
67 USI start_nonloadmem;
68 };
69
70 static void free_state (SIM_DESC);
71 static void get_progbounds_iterator (bfd *, asection *, void *);
72 static SIM_RC cris_option_handler (SIM_DESC, sim_cpu *, int, char *, int);
73
74 /* Since we don't build the cgen-opcode table, we use the old
75 disassembler. */
76 static CGEN_DISASSEMBLER cris_disassemble_insn;
77
78 /* By default, we set up stack and environment variables like the Linux
79 kernel. */
80 static char cris_bare_iron = 0;
81
82 /* Whether 0x9000000xx have simulator-specific meanings. */
83 char cris_have_900000xxif = 0;
84
85 /* What to do when we face a more or less unknown syscall. */
86 enum cris_unknown_syscall_action_type cris_unknown_syscall_action
87 = CRIS_USYSC_MSG_STOP;
88
89 /* Records simulator descriptor so utilities like cris_dump_regs can be
90 called from gdb. */
91 SIM_DESC current_state;
92
93 /* CRIS-specific options. */
94 typedef enum {
95 OPTION_CRIS_STATS = OPTION_START,
96 OPTION_CRIS_TRACE,
97 OPTION_CRIS_NAKED,
98 OPTION_CRIS_900000XXIF,
99 OPTION_CRIS_UNKNOWN_SYSCALL
100 } CRIS_OPTIONS;
101
102 static const OPTION cris_options[] =
103 {
104 { {"cris-cycles", required_argument, NULL, OPTION_CRIS_STATS},
105 '\0', "basic|unaligned|schedulable|all",
106 "Dump execution statistics",
107 cris_option_handler, NULL },
108 { {"cris-trace", required_argument, NULL, OPTION_CRIS_TRACE},
109 '\0', "basic",
110 "Emit trace information while running",
111 cris_option_handler, NULL },
112 { {"cris-naked", no_argument, NULL, OPTION_CRIS_NAKED},
113 '\0', NULL, "Don't set up stack and environment",
114 cris_option_handler, NULL },
115 { {"cris-900000xx", no_argument, NULL, OPTION_CRIS_900000XXIF},
116 '\0', NULL, "Define addresses at 0x900000xx with simulator semantics",
117 cris_option_handler, NULL },
118 { {"cris-unknown-syscall", required_argument, NULL,
119 OPTION_CRIS_UNKNOWN_SYSCALL},
120 '\0', "stop|enosys|enosys-quiet", "Action at an unknown system call",
121 cris_option_handler, NULL },
122 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
123 };
124 \f
125 /* Add the CRIS-specific option list to the simulator. */
126
127 SIM_RC
128 cris_option_install (SIM_DESC sd)
129 {
130 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
131 if (sim_add_option_table (sd, NULL, cris_options) != SIM_RC_OK)
132 return SIM_RC_FAIL;
133 return SIM_RC_OK;
134 }
135
136 /* Handle CRIS-specific options. */
137
138 static SIM_RC
139 cris_option_handler (SIM_DESC sd, sim_cpu *cpu ATTRIBUTE_UNUSED, int opt,
140 char *arg, int is_command ATTRIBUTE_UNUSED)
141 {
142 /* The options are CRIS-specific, but cpu-specific option-handling is
143 broken; required to being with "--cpu0-". We store the flags in an
144 unused field in the global state structure and move the flags over
145 to the module-specific CPU data when we store things in the
146 cpu-specific structure. */
147 char *tracefp = STATE_TRACE_FLAGS (sd);
148
149 switch ((CRIS_OPTIONS) opt)
150 {
151 case OPTION_CRIS_STATS:
152 if (strcmp (arg, "basic") == 0)
153 *tracefp = FLAG_CRIS_MISC_PROFILE_SIMPLE;
154 else if (strcmp (arg, "unaligned") == 0)
155 *tracefp
156 = (FLAG_CRIS_MISC_PROFILE_UNALIGNED
157 | FLAG_CRIS_MISC_PROFILE_SIMPLE);
158 else if (strcmp (arg, "schedulable") == 0)
159 *tracefp
160 = (FLAG_CRIS_MISC_PROFILE_SCHEDULABLE
161 | FLAG_CRIS_MISC_PROFILE_SIMPLE);
162 else if (strcmp (arg, "all") == 0)
163 *tracefp = FLAG_CRIS_MISC_PROFILE_ALL;
164 else
165 {
166 /* Beware; the framework does not handle the error case;
167 we have to do it ourselves. */
168 sim_io_eprintf (sd, "Unknown option `--cris-cycles=%s'\n", arg);
169 return SIM_RC_FAIL;
170 }
171 break;
172
173 case OPTION_CRIS_TRACE:
174 if (strcmp (arg, "basic") == 0)
175 *tracefp |= FLAG_CRIS_MISC_PROFILE_XSIM_TRACE;
176 else
177 {
178 sim_io_eprintf (sd, "Unknown option `--cris-trace=%s'\n", arg);
179 return SIM_RC_FAIL;
180 }
181 break;
182
183 case OPTION_CRIS_NAKED:
184 cris_bare_iron = 1;
185 break;
186
187 case OPTION_CRIS_900000XXIF:
188 cris_have_900000xxif = 1;
189 break;
190
191 case OPTION_CRIS_UNKNOWN_SYSCALL:
192 if (strcmp (arg, "enosys") == 0)
193 cris_unknown_syscall_action = CRIS_USYSC_MSG_ENOSYS;
194 else if (strcmp (arg, "enosys-quiet") == 0)
195 cris_unknown_syscall_action = CRIS_USYSC_QUIET_ENOSYS;
196 else if (strcmp (arg, "stop") == 0)
197 cris_unknown_syscall_action = CRIS_USYSC_MSG_STOP;
198 else
199 {
200 sim_io_eprintf (sd, "Unknown option `--cris-unknown-syscall=%s'\n",
201 arg);
202 return SIM_RC_FAIL;
203 }
204 break;
205
206 default:
207 /* We'll actually never get here; the caller handles the error
208 case. */
209 sim_io_eprintf (sd, "Unknown option `%s'\n", arg);
210 return SIM_RC_FAIL;
211 }
212
213 /* Imply --profile-model=on. */
214 return sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on");
215 }
216
217 /* FIXME: Remove these, globalize those in sim-load.c, move elsewhere. */
218
219 static void
220 xprintf (host_callback *callback, const char *fmt, ...)
221 {
222 va_list ap;
223
224 va_start (ap, fmt);
225
226 (*callback->vprintf_filtered) (callback, fmt, ap);
227
228 va_end (ap);
229 }
230
231 static void
232 eprintf (host_callback *callback, const char *fmt, ...)
233 {
234 va_list ap;
235
236 va_start (ap, fmt);
237
238 (*callback->evprintf_filtered) (callback, fmt, ap);
239
240 va_end (ap);
241 }
242
243 /* An ELF-specific simplified ../common/sim-load.c:sim_load_file,
244 using the program headers, not sections, in order to make sure that
245 the program headers themeselves are also loaded. The caller is
246 responsible for asserting that ABFD is an ELF file. */
247
248 static bfd_boolean
249 cris_load_elf_file (SIM_DESC sd, struct bfd *abfd, sim_write_fn do_write)
250 {
251 Elf_Internal_Phdr *phdr;
252 int n_hdrs;
253 int i;
254 bfd_boolean verbose = STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG;
255 host_callback *callback = STATE_CALLBACK (sd);
256
257 phdr = elf_tdata (abfd)->phdr;
258 n_hdrs = elf_elfheader (abfd)->e_phnum;
259
260 /* We're only interested in PT_LOAD; all necessary information
261 should be covered by that. */
262 for (i = 0; i < n_hdrs; i++)
263 {
264 bfd_byte *buf;
265 bfd_vma lma = STATE_LOAD_AT_LMA_P (sd)
266 ? phdr[i].p_paddr : phdr[i].p_vaddr;
267
268 if (phdr[i].p_type != PT_LOAD)
269 continue;
270
271 buf = xmalloc (phdr[i].p_filesz);
272
273 if (verbose)
274 xprintf (callback, "Loading segment at 0x%lx, size 0x%lx\n",
275 lma, phdr[i].p_filesz);
276
277 if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0
278 || (bfd_bread (buf, phdr[i].p_filesz, abfd) != phdr[i].p_filesz))
279 {
280 eprintf (callback,
281 "%s: could not read segment at 0x%lx, size 0x%lx\n",
282 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
283 free (buf);
284 return FALSE;
285 }
286
287 if (do_write (sd, lma, buf, phdr[i].p_filesz) != phdr[i].p_filesz)
288 {
289 eprintf (callback,
290 "%s: could not load segment at 0x%lx, size 0x%lx\n",
291 STATE_MY_NAME (sd), lma, phdr[i].p_filesz);
292 free (buf);
293 return FALSE;
294 }
295
296 free (buf);
297 }
298
299 return TRUE;
300 }
301
302 /* Replacement for ../common/sim-hload.c:sim_load, so we can treat ELF
303 files differently. */
304
305 SIM_RC
306 sim_load (SIM_DESC sd, char *prog_name, struct bfd *prog_bfd,
307 int from_tty ATTRIBUTE_UNUSED)
308 {
309 bfd *result_bfd;
310
311 if (bfd_get_flavour (prog_bfd) != bfd_target_elf_flavour)
312 {
313 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
314 if (sim_analyze_program (sd, prog_name, prog_bfd) != SIM_RC_OK)
315 return SIM_RC_FAIL;
316 SIM_ASSERT (STATE_PROG_BFD (sd) != NULL);
317
318 result_bfd = sim_load_file (sd, STATE_MY_NAME (sd),
319 STATE_CALLBACK (sd),
320 prog_name,
321 STATE_PROG_BFD (sd),
322 STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG,
323 STATE_LOAD_AT_LMA_P (sd),
324 sim_write);
325 if (result_bfd == NULL)
326 {
327 bfd_close (STATE_PROG_BFD (sd));
328 STATE_PROG_BFD (sd) = NULL;
329 return SIM_RC_FAIL;
330 }
331 return SIM_RC_OK;
332 }
333
334 return cris_load_elf_file (sd, prog_bfd, sim_write)
335 ? SIM_RC_OK : SIM_RC_FAIL;
336 }
337
338 /* Cover function of sim_state_free to free the cpu buffers as well. */
339
340 static void
341 free_state (SIM_DESC sd)
342 {
343 if (STATE_MODULES (sd) != NULL)
344 sim_module_uninstall (sd);
345 sim_cpu_free_all (sd);
346 sim_state_free (sd);
347 }
348
349 /* BFD section iterator to find the highest and lowest allocated and
350 non-allocated section addresses (plus one). */
351
352 static void
353 get_progbounds_iterator (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *vp)
354 {
355 struct progbounds *pbp = (struct progbounds *) vp;
356
357 if ((bfd_get_section_flags (abfd, s) & SEC_ALLOC))
358 {
359 bfd_size_type sec_size = bfd_get_section_size (s);
360 bfd_size_type sec_start = bfd_get_section_vma (abfd, s);
361 bfd_size_type sec_end = sec_start + sec_size;
362
363 if (sec_end > pbp->endmem)
364 pbp->endmem = sec_end;
365
366 if (sec_start < pbp->startmem)
367 pbp->startmem = sec_start;
368
369 if ((bfd_get_section_flags (abfd, s) & SEC_LOAD))
370 {
371 if (sec_end > pbp->end_loadmem)
372 pbp->end_loadmem = sec_end;
373 }
374 else if (sec_start < pbp->start_nonloadmem)
375 pbp->start_nonloadmem = sec_start;
376 }
377 }
378
379 /* Get the program boundaries. Because not everything is covered by
380 sections in ELF, notably the program headers, we use the program
381 headers instead. */
382
383 static void
384 cris_get_progbounds (struct bfd *abfd, struct progbounds *pbp)
385 {
386 Elf_Internal_Phdr *phdr;
387 int n_hdrs;
388 int i;
389
390 pbp->startmem = 0xffffffff;
391 pbp->endmem = 0;
392 pbp->end_loadmem = 0;
393 pbp->start_nonloadmem = 0xffffffff;
394
395 /* In case we're ever used for something other than ELF, use the
396 generic method. */
397 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
398 {
399 bfd_map_over_sections (abfd, get_progbounds_iterator, pbp);
400 return;
401 }
402
403 phdr = elf_tdata (abfd)->phdr;
404 n_hdrs = elf_elfheader (abfd)->e_phnum;
405
406 /* We're only interested in PT_LOAD; all necessary information
407 should be covered by that. */
408 for (i = 0; i < n_hdrs; i++)
409 {
410 if (phdr[i].p_type != PT_LOAD)
411 continue;
412
413 if (phdr[i].p_paddr < pbp->startmem)
414 pbp->startmem = phdr[i].p_paddr;
415
416 if (phdr[i].p_paddr + phdr[i].p_memsz > pbp->endmem)
417 pbp->endmem = phdr[i].p_paddr + phdr[i].p_memsz;
418
419 if (phdr[i].p_paddr + phdr[i].p_filesz > pbp->end_loadmem)
420 pbp->end_loadmem = phdr[i].p_paddr + phdr[i].p_filesz;
421
422 if (phdr[i].p_memsz > phdr[i].p_filesz
423 && phdr[i].p_paddr + phdr[i].p_filesz < pbp->start_nonloadmem)
424 pbp->start_nonloadmem = phdr[i].p_paddr + phdr[i].p_filesz;
425 }
426 }
427
428 /* Parameter communication by static variables, hmm... Oh well, for
429 simplicity. */
430 static bfd_vma exec_load_addr;
431 static bfd_vma interp_load_addr;
432 static bfd_vma interp_start_addr;
433
434 /* Supposed to mimic Linux' "NEW_AUX_ENT (AT_PHDR, load_addr + exec->e_phoff)". */
435
436 static USI
437 aux_ent_phdr (struct bfd *ebfd)
438 {
439 return elf_elfheader (ebfd)->e_phoff + exec_load_addr;
440 }
441
442 /* We just pass on the header info; we don't have our own idea of the
443 program header entry size. */
444
445 static USI
446 aux_ent_phent (struct bfd *ebfd)
447 {
448 return elf_elfheader (ebfd)->e_phentsize;
449 }
450
451 /* Like "NEW_AUX_ENT(AT_PHNUM, exec->e_phnum)". */
452
453 static USI
454 aux_ent_phnum (struct bfd *ebfd)
455 {
456 return elf_elfheader (ebfd)->e_phnum;
457 }
458
459 /* Like "NEW_AUX_ENT(AT_BASE, interp_load_addr)". */
460
461 static USI
462 aux_ent_base (struct bfd *ebfd)
463 {
464 return interp_load_addr;
465 }
466
467 /* Like "NEW_AUX_ENT(AT_ENTRY, exec->e_entry)". */
468
469 static USI
470 aux_ent_entry (struct bfd *ebfd)
471 {
472 ASSERT (elf_elfheader (ebfd)->e_entry == bfd_get_start_address (ebfd));
473 return elf_elfheader (ebfd)->e_entry;
474 }
475
476 /* Helper for cris_handle_interpreter: like sim_write, but load at
477 interp_load_addr offset. */
478
479 static int
480 cris_write_interp (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
481 {
482 return sim_write (sd, mem + interp_load_addr, buf, length);
483 }
484
485 /* Cater to the presence of an interpreter: load it and set
486 interp_start_addr. Return FALSE if there was an error, TRUE if
487 everything went fine, including an interpreter being absent and
488 the program being in a non-ELF format. */
489
490 static bfd_boolean
491 cris_handle_interpreter (SIM_DESC sd, struct bfd *abfd)
492 {
493 int i, n_hdrs;
494 bfd_vma phaddr;
495 bfd_byte buf[4];
496 char *interp = NULL;
497 struct bfd *ibfd;
498 bfd_boolean ok = FALSE;
499 Elf_Internal_Phdr *phdr;
500
501 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
502 return TRUE;
503
504 phdr = elf_tdata (abfd)->phdr;
505 n_hdrs = aux_ent_phnum (abfd);
506
507 /* Check the program headers for presence of an interpreter. */
508 for (i = 0; i < n_hdrs; i++)
509 {
510 int interplen;
511 bfd_size_type interpsiz, interp_filesiz;
512 struct progbounds interp_bounds;
513
514 if (phdr[i].p_type != PT_INTERP)
515 continue;
516
517 /* Get the name of the interpreter, prepended with the sysroot
518 (empty if absent). */
519 interplen = phdr[i].p_filesz;
520 interp = xmalloc (interplen + strlen (simulator_sysroot));
521 strcpy (interp, simulator_sysroot);
522
523 /* Read in the name. */
524 if (bfd_seek (abfd, phdr[i].p_offset, SEEK_SET) != 0
525 || (bfd_bread (interp + strlen (simulator_sysroot), interplen, abfd)
526 != interplen))
527 goto interpname_failed;
528
529 /* Like Linux, require the string to be 0-terminated. */
530 if (interp[interplen + strlen (simulator_sysroot) - 1] != 0)
531 goto interpname_failed;
532
533 /* Inspect the interpreter. */
534 ibfd = bfd_openr (interp, STATE_TARGET (sd));
535 if (ibfd == NULL)
536 goto interpname_failed;
537
538 /* The interpreter is at leat something readable to BFD; make
539 sure it's an ELF non-archive file. */
540 if (!bfd_check_format (ibfd, bfd_object)
541 || bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
542 goto interp_failed;
543
544 /* Check the layout of the interpreter. */
545 cris_get_progbounds (ibfd, &interp_bounds);
546
547 /* Round down to pagesize the start page and up the endpage.
548 Don't round the *load and *nonload members. */
549 interp_bounds.startmem &= ~8191;
550 interp_bounds.endmem = (interp_bounds.endmem + 8191) & ~8191;
551
552 /* Until we need a more dynamic solution, assume we can put the
553 interpreter at this fixed location. NB: this is not what
554 happens for Linux 2008-12-28, but it could and might and
555 perhaps should. */
556 interp_load_addr = 0x40000;
557 interpsiz = interp_bounds.endmem - interp_bounds.startmem;
558 interp_filesiz = interp_bounds.end_loadmem - interp_bounds.startmem;
559
560 /* If we have a non-DSO or interpreter starting at the wrong
561 address, bail. */
562 if (interp_bounds.startmem != 0
563 || interpsiz + interp_load_addr >= exec_load_addr)
564 goto interp_failed;
565
566 /* We don't have the API to get the address of a simulator
567 memory area, so we go via a temporary area. Luckily, the
568 interpreter is supposed to be small, less than 0x40000
569 bytes. */
570 sim_do_commandf (sd, "memory region 0x%lx,0x%lx",
571 interp_load_addr, interpsiz);
572
573 /* Now that memory for the interpreter is defined, load it. */
574 if (!cris_load_elf_file (sd, ibfd, cris_write_interp))
575 goto interp_failed;
576
577 /* It's no use setting STATE_START_ADDR, because it gets
578 overwritten by a sim_analyze_program call in sim_load. Let's
579 just store it locally. */
580 interp_start_addr
581 = (bfd_get_start_address (ibfd)
582 - interp_bounds.startmem + interp_load_addr);
583
584 /* Linux cares only about the first PT_INTERP, so let's ignore
585 the rest. */
586 goto all_done;
587 }
588
589 /* Register R10 should hold 0 at static start (no finifunc), but
590 that's the default, so don't bother. */
591 return TRUE;
592
593 all_done:
594 ok = TRUE;
595
596 interp_failed:
597 bfd_close (ibfd);
598
599 interpname_failed:
600 if (!ok)
601 sim_io_eprintf (sd,
602 "%s: could not load ELF interpreter `%s' for program `%s'\n",
603 STATE_MY_NAME (sd),
604 interp == NULL ? "(what's-its-name)" : interp,
605 bfd_get_filename (abfd));
606 free (interp);
607 return ok;
608 }
609
610 /* Create an instance of the simulator. */
611
612 SIM_DESC
613 sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
614 char **argv)
615 {
616 char c;
617 int i;
618 USI startmem = 0;
619 USI endmem = CRIS_DEFAULT_MEM_SIZE;
620 USI endbrk = endmem;
621 USI stack_low = 0;
622 SIM_DESC sd = sim_state_alloc (kind, callback);
623
624 static const struct auxv_entries_s
625 {
626 bfd_byte id;
627 USI (*efn) (struct bfd *ebfd);
628 USI val;
629 } auxv_entries[] =
630 {
631 #define AUX_ENT(a, b) {TARGET_ ## a, NULL, b}
632 #define AUX_ENTF(a, b, f) {TARGET_ ## a, f, b}
633 AUX_ENT (AT_HWCAP, 0),
634 AUX_ENT (AT_PAGESZ, 8192),
635 AUX_ENT (AT_CLKTCK, 100),
636 AUX_ENTF (AT_PHDR, 0, aux_ent_phdr),
637 AUX_ENTF (AT_PHENT, 0, aux_ent_phent),
638 AUX_ENTF (AT_PHNUM, 0, aux_ent_phnum),
639 AUX_ENTF (AT_BASE, 0, aux_ent_base),
640 AUX_ENT (AT_FLAGS, 0),
641 AUX_ENTF (AT_ENTRY, 0, aux_ent_entry),
642
643 /* Or is root better? Maybe have it settable? */
644 AUX_ENT (AT_UID, 500),
645 AUX_ENT (AT_EUID, 500),
646 AUX_ENT (AT_GID, 500),
647 AUX_ENT (AT_EGID, 500),
648 AUX_ENT (AT_NULL, 0)
649 };
650
651 /* Can't initialize to "" below. It's either a GCC bug in old
652 releases (up to and including 2.95.3 (.4 in debian) or a bug in the
653 standard ;-) that the rest of the elements won't be initialized. */
654 bfd_byte sp_init[4] = {0, 0, 0, 0};
655
656 /* The cpu data is kept in a separately allocated chunk of memory. */
657 if (sim_cpu_alloc_all (sd, 1, cgen_cpu_max_extra_bytes ()) != SIM_RC_OK)
658 {
659 free_state (sd);
660 return 0;
661 }
662
663 if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
664 {
665 free_state (sd);
666 return 0;
667 }
668
669 /* getopt will print the error message so we just have to exit if this fails.
670 FIXME: Hmmm... in the case of gdb we need getopt to call
671 print_filtered. */
672 if (sim_parse_args (sd, argv) != SIM_RC_OK)
673 {
674 free_state (sd);
675 return 0;
676 }
677
678 /* If we have a binary program, endianness-setting would not be taken
679 from elsewhere unfortunately, so set it here. At the time of this
680 writing, it isn't used until sim_config, but that might change so
681 set it here before memory is defined or touched. */
682 current_target_byte_order = LITTLE_ENDIAN;
683
684 /* check for/establish the reference program image */
685 if (sim_analyze_program (sd,
686 (STATE_PROG_ARGV (sd) != NULL
687 ? *STATE_PROG_ARGV (sd)
688 : NULL),
689 abfd) != SIM_RC_OK)
690 {
691 if (STATE_PROG_ARGV (sd) != NULL)
692 sim_io_eprintf (sd, "%s: invalid executable `%s'\n",
693 STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd));
694 else
695 sim_io_eprintf (sd, "%s: invalid executable\n",
696 STATE_MY_NAME (sd));
697 free_state (sd);
698 return 0;
699 }
700
701 /* We might get called with the caller expecting us to get hold of
702 the bfd for ourselves, which would happen at the
703 sim_analyze_program call above. */
704 if (abfd == NULL)
705 abfd = STATE_PROG_BFD (sd);
706
707 if (bfd_get_arch (abfd) == bfd_arch_unknown)
708 {
709 if (STATE_PROG_ARGV (sd) != NULL)
710 sim_io_eprintf (sd, "%s: not a CRIS program `%s'\n",
711 STATE_MY_NAME (sd), *STATE_PROG_ARGV (sd));
712 else
713 sim_io_eprintf (sd, "%s: program to be run is not a CRIS program\n",
714 STATE_MY_NAME (sd));
715 free_state (sd);
716 return 0;
717 }
718
719 /* For CRIS simulator-specific use, we need to find out the bounds of
720 the program as well, which is not done by sim_analyze_program
721 above. */
722 if (abfd != NULL)
723 {
724 struct progbounds pb;
725
726 /* The sections should now be accessible using bfd functions. */
727 cris_get_progbounds (abfd, &pb);
728
729 /* We align the area that the program uses to page boundaries. */
730 startmem = pb.startmem & ~8191;
731 endbrk = pb.endmem;
732 endmem = (endbrk + 8191) & ~8191;
733 }
734
735 /* Find out how much room is needed for the environment and argv, create
736 that memory and fill it. Only do this when there's a program
737 specified. */
738 if (abfd != NULL && !cris_bare_iron)
739 {
740 char *name = bfd_get_filename (abfd);
741 char **my_environ = GET_ENVIRON ();
742 /* We use these maps to give the same behavior as the old xsim
743 simulator. */
744 USI envtop = 0x40000000;
745 USI stacktop = 0x3e000000;
746 USI envstart;
747 int envc;
748 int len = strlen (name) + 1;
749 USI epp, epp0;
750 USI stacklen;
751 int i;
752 char **prog_argv = STATE_PROG_ARGV (sd);
753 int my_argc = 0;
754 /* All CPU:s have the same memory map, apparently. */
755 SIM_CPU *cpu = STATE_CPU (sd, 0);
756 USI csp;
757 bfd_byte buf[4];
758
759 /* Count in the environment as well. */
760 for (envc = 0; my_environ[envc] != NULL; envc++)
761 len += strlen (my_environ[envc]) + 1;
762
763 for (i = 0; prog_argv[i] != NULL; my_argc++, i++)
764 len += strlen (prog_argv[i]) + 1;
765
766 envstart = (envtop - len) & ~8191;
767
768 /* Create read-only block for the environment strings. */
769 sim_core_attach (sd, NULL, 0, access_read, 0,
770 envstart, (len + 8191) & ~8191,
771 0, NULL, NULL);
772
773 /* This shouldn't happen. */
774 if (envstart < stacktop)
775 stacktop = envstart - 64 * 8192;
776
777 csp = stacktop;
778
779 /* Note that the linux kernel does not correctly compute the storage
780 needs for the static-exe AUX vector. */
781
782 csp -= sizeof (auxv_entries) / sizeof (auxv_entries[0]) * 4 * 2;
783
784 csp -= (envc + 1) * 4;
785 csp -= (my_argc + 1) * 4;
786 csp -= 4;
787
788 /* Write the target representation of the start-up-value for the
789 stack-pointer suitable for register initialization below. */
790 bfd_putl32 (csp, sp_init);
791
792 /* If we make this 1M higher; say 8192*1024, we have to take
793 special precautions for pthreads, because pthreads assumes that
794 the memory that low isn't mmapped, and that it can mmap it
795 without fallback in case of failure (and we fail ungracefully
796 long before *that*: the memory isn't accounted for in our mmap
797 list). */
798 stack_low = (csp - (7168*1024)) & ~8191;
799
800 stacklen = stacktop - stack_low;
801
802 /* Tee hee, we have an executable stack. Well, it's necessary to
803 test GCC trampolines... */
804 sim_core_attach (sd, NULL, 0, access_read_write_exec, 0,
805 stack_low, stacklen,
806 0, NULL, NULL);
807
808 epp = epp0 = envstart;
809
810 /* Can't use sim_core_write_unaligned_4 without everything
811 initialized when tracing, and then these writes would get into
812 the trace. */
813 #define write_dword(addr, data) \
814 do \
815 { \
816 USI data_ = data; \
817 USI addr_ = addr; \
818 bfd_putl32 (data_, buf); \
819 if (sim_core_write_buffer (sd, cpu, 0, buf, addr_, 4) != 4) \
820 goto abandon_chip; \
821 } \
822 while (0)
823
824 write_dword (csp, my_argc);
825 csp += 4;
826
827 for (i = 0; i < my_argc; i++, csp += 4)
828 {
829 size_t strln = strlen (prog_argv[i]) + 1;
830
831 if (sim_core_write_buffer (sd, cpu, 0, prog_argv[i], epp, strln)
832 != strln)
833 goto abandon_chip;
834
835 write_dword (csp, envstart + epp - epp0);
836 epp += strln;
837 }
838
839 write_dword (csp, 0);
840 csp += 4;
841
842 for (i = 0; i < envc; i++, csp += 4)
843 {
844 unsigned int strln = strlen (my_environ[i]) + 1;
845
846 if (sim_core_write_buffer (sd, cpu, 0, my_environ[i], epp, strln)
847 != strln)
848 goto abandon_chip;
849
850 write_dword (csp, envstart + epp - epp0);
851 epp += strln;
852 }
853
854 write_dword (csp, 0);
855 csp += 4;
856
857 /* The load address of the executable could presumably be
858 different than the lowest used memory address, but let's
859 stick to simplicity until needed. And
860 cris_handle_interpreter might change startmem and endmem, so
861 let's set it now. */
862 exec_load_addr = startmem;
863
864 if (!cris_handle_interpreter (sd, abfd))
865 goto abandon_chip;
866
867 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
868 for (i = 0; i < sizeof (auxv_entries) / sizeof (auxv_entries[0]); i++)
869 {
870 write_dword (csp, auxv_entries[i].id);
871 write_dword (csp + 4,
872 auxv_entries[i].efn != NULL
873 ? (*auxv_entries[i].efn) (abfd)
874 : auxv_entries[i].val);
875 csp += 4 + 4;
876 }
877 }
878
879 /* Allocate core managed memory if none specified by user. */
880 if (sim_core_read_buffer (sd, NULL, read_map, &c, startmem, 1) == 0)
881 sim_do_commandf (sd, "memory region 0x%lx,0x%lx", startmem,
882 endmem - startmem);
883
884 /* Allocate simulator I/O managed memory if none specified by user. */
885 if (cris_have_900000xxif)
886 {
887 if (sim_core_read_buffer (sd, NULL, read_map, &c, 0x90000000, 1) == 0)
888 sim_core_attach (sd, NULL, 0, access_write, 0, 0x90000000, 0x100,
889 0, &cris_devices, NULL);
890 else
891 {
892 (*callback->
893 printf_filtered) (callback,
894 "Seeing --cris-900000xx with memory defined there\n");
895 goto abandon_chip;
896 }
897 }
898
899 /* Establish any remaining configuration options. */
900 if (sim_config (sd) != SIM_RC_OK)
901 {
902 abandon_chip:
903 free_state (sd);
904 return 0;
905 }
906
907 if (sim_post_argv_init (sd) != SIM_RC_OK)
908 {
909 free_state (sd);
910 return 0;
911 }
912
913 /* Open a copy of the cpu descriptor table. */
914 {
915 CGEN_CPU_DESC cd = cris_cgen_cpu_open_1 (STATE_ARCHITECTURE (sd)->printable_name,
916 CGEN_ENDIAN_LITTLE);
917 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
918 {
919 SIM_CPU *cpu = STATE_CPU (sd, i);
920 CPU_CPU_DESC (cpu) = cd;
921 CPU_DISASSEMBLER (cpu) = cris_disassemble_insn;
922
923 /* See cris_option_handler for the reason why this is needed. */
924 CPU_CRIS_MISC_PROFILE (cpu)->flags = STATE_TRACE_FLAGS (sd)[0];
925
926 /* Set SP to the stack we allocated above. */
927 (* CPU_REG_STORE (cpu)) (cpu, H_GR_SP, (char *) sp_init, 4);
928
929 /* Set the simulator environment data. */
930 cpu->highest_mmapped_page = NULL;
931 cpu->endmem = endmem;
932 cpu->endbrk = endbrk;
933 cpu->stack_low = stack_low;
934 cpu->syscalls = 0;
935 cpu->m1threads = 0;
936 cpu->threadno = 0;
937 cpu->max_threadid = 0;
938 cpu->thread_data = NULL;
939 memset (cpu->sighandler, 0, sizeof (cpu->sighandler));
940 cpu->make_thread_cpu_data = NULL;
941 cpu->thread_cpu_data_size = 0;
942 #if WITH_HW
943 cpu->deliver_interrupt = NULL;
944 #endif
945 }
946 #if WITH_HW
947 /* Always be cycle-accurate and call before/after functions if
948 with-hardware. */
949 sim_profile_set_option (sd, "-model", PROFILE_MODEL_IDX, "on");
950 #endif
951 }
952
953 /* Initialize various cgen things not done by common framework.
954 Must be done after cris_cgen_cpu_open. */
955 cgen_init (sd);
956
957 /* Store in a global so things like cris_dump_regs can be invoked
958 from the gdb command line. */
959 current_state = sd;
960
961 cris_set_callbacks (callback);
962
963 return sd;
964 }
965
966 void
967 sim_close (SIM_DESC sd, int quitting ATTRIBUTE_UNUSED)
968 {
969 cris_cgen_cpu_close (CPU_CPU_DESC (STATE_CPU (sd, 0)));
970 sim_module_uninstall (sd);
971 }
972 \f
973 SIM_RC
974 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
975 char **argv ATTRIBUTE_UNUSED,
976 char **envp ATTRIBUTE_UNUSED)
977 {
978 SIM_CPU *current_cpu = STATE_CPU (sd, 0);
979 SIM_ADDR addr;
980
981 if (sd != NULL)
982 addr = interp_start_addr != 0
983 ? interp_start_addr : bfd_get_start_address (abfd);
984 else
985 addr = 0;
986 sim_pc_set (current_cpu, addr);
987
988 /* Other simulators have #if 0:d code that says
989 STATE_ARGV (sd) = sim_copy_argv (argv);
990 STATE_ENVP (sd) = sim_copy_argv (envp);
991 Enabling that gives you not-found link-errors for sim_copy_argv.
992 FIXME: Do archaeology to find out more. */
993
994 return SIM_RC_OK;
995 }
996
997 void
998 sim_do_command (SIM_DESC sd, char *cmd)
999 {
1000 if (sim_args_command (sd, cmd) != SIM_RC_OK)
1001 sim_io_eprintf (sd, "Unknown command `%s'\n", cmd);
1002 }
1003 \f
1004 /* Disassemble an instruction. */
1005
1006 static void
1007 cris_disassemble_insn (SIM_CPU *cpu,
1008 const CGEN_INSN *insn ATTRIBUTE_UNUSED,
1009 const ARGBUF *abuf ATTRIBUTE_UNUSED,
1010 IADDR pc, char *buf)
1011 {
1012 disassembler_ftype pinsn;
1013 struct disassemble_info disasm_info;
1014 SFILE sfile;
1015 SIM_DESC sd = CPU_STATE (cpu);
1016
1017 sfile.buffer = sfile.current = buf;
1018 INIT_DISASSEMBLE_INFO (disasm_info, (FILE *) &sfile,
1019 (fprintf_ftype) sim_disasm_sprintf);
1020 disasm_info.endian = BFD_ENDIAN_LITTLE;
1021 disasm_info.read_memory_func = sim_disasm_read_memory;
1022 disasm_info.memory_error_func = sim_disasm_perror_memory;
1023 disasm_info.application_data = (PTR) cpu;
1024 pinsn = cris_get_disassembler (STATE_PROG_BFD (sd));
1025 (*pinsn) (pc, &disasm_info);
1026 }
This page took 0.051627 seconds and 5 git commands to generate.