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