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