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