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