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