1 /* interp.c -- AArch64 sim interface to GDB.
3 Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 Contributed by Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include "gdb/callback.h"
32 #include "gdb/remote-sim.h"
33 #include "gdb/signals.h"
34 #include "gdb/sim-aarch64.h"
37 #include "sim-options.h"
39 #include "simulator.h"
43 static struct disassemble_info info;
44 static unsigned long symcount = 0;
45 static asymbol ** symtab = NULL;
47 /* FIXME: 1000 characters should be enough to hold the disassembled
48 instruction plus any comments that come after it. But maybe with
49 C++ programs this might not be enough. Not sure if it is worth
50 adding logic to dynamically grow the buffer though. */
51 static char opbuf[1000];
53 static int op_printf (void *, const char *, ...) ATTRIBUTE_FPTR_PRINTF_2;
56 op_printf (void *stream ATTRIBUTE_UNUSED, const char *fmt, ...)
58 size_t space_remaining;
62 space_remaining = sizeof (opbuf) - strlen (opbuf);
64 /* Instead of printing to stream we store the text in opbuf.
65 This allows us to use the sim_io_eprintf routine to output
66 the text in aarch64_print_insn. */
67 ret = vsnprintf (opbuf + strlen (opbuf), space_remaining, fmt, ap);
73 aarch64_print_insn (SIM_DESC sd, uint64_t addr)
78 size = print_insn_aarch64 (addr, & info);
79 sim_io_eprintf (sd, " %*s\n", size, opbuf);
83 sim_dis_read (bfd_vma memaddr,
86 struct disassemble_info * info)
88 aarch64_get_mem_blk (info->application_data, memaddr, (char *) ptr, length);
93 /* Filter out (in place) symbols that are useless for disassembly.
94 COUNT is the number of elements in SYMBOLS.
95 Return the number of useful symbols. */
98 remove_useless_symbols (asymbol **symbols, unsigned long count)
100 asymbol **in_ptr = symbols;
101 asymbol **out_ptr = symbols;
105 asymbol *sym = *in_ptr++;
107 if (strstr (sym->name, "gcc2_compiled"))
109 if (sym->name == NULL || sym->name[0] == '\0')
111 if (sym->flags & (BSF_DEBUGGING))
113 if ( bfd_is_und_section (sym->section)
114 || bfd_is_com_section (sym->section))
116 if (sym->name[0] == '$')
121 return out_ptr - symbols;
125 compare_symbols (const void *ap, const void *bp)
127 const asymbol *a = * (const asymbol **) ap;
128 const asymbol *b = * (const asymbol **) bp;
130 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
132 if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
137 /* Find the name of the function at ADDR. */
139 aarch64_get_func (uint64_t addr)
145 while (min < max - 1)
150 sym = (min + max) / 2;
151 sa = bfd_asymbol_value (symtab[sym]);
165 return bfd_asymbol_name (symtab [min]);
171 aarch64_get_sym_value (const char *name)
175 for (i = 0; i < symcount; i++)
176 if (strcmp (bfd_asymbol_name (symtab[i]), name) == 0)
177 return bfd_asymbol_value (symtab[i]);
183 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
185 sim_cpu *cpu = STATE_CPU (sd, 0);
190 addr = bfd_get_start_address (abfd);
192 aarch64_set_next_PC (cpu, addr);
193 aarch64_update_PC (cpu);
195 /* Standalone mode (i.e. `run`) will take care of the argv for us in
196 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
197 with `gdb`), we need to handle it because the user can change the
198 argv on the fly via gdb's 'run'. */
199 if (STATE_PROG_ARGV (sd) != argv)
201 freeargv (STATE_PROG_ARGV (sd));
202 STATE_PROG_ARGV (sd) = dupargv (argv);
205 memset (& info, 0, sizeof (info));
206 init_disassemble_info (& info, NULL, op_printf);
207 info.read_memory_func = sim_dis_read;
208 info.arch = bfd_get_arch (abfd);
209 info.mach = bfd_get_mach (abfd);
210 info.application_data = cpu;
212 info.arch = bfd_arch_aarch64;
213 disassemble_init_for_target (& info);
215 storage = bfd_get_symtab_upper_bound (abfd);
218 symtab = (asymbol **) xmalloc (storage);
219 symcount = bfd_canonicalize_symtab (abfd, symtab);
220 symcount = remove_useless_symbols (symtab, symcount);
221 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
224 aarch64_init (cpu, bfd_get_start_address (abfd));
229 /* Read the LENGTH bytes at BUF as a little-endian value. */
232 get_le (unsigned char *buf, unsigned int length)
236 while (length -- > 0)
237 acc = (acc << 8) + buf[length];
242 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
245 put_le (unsigned char *buf, unsigned int length, bfd_vma val)
249 for (i = 0; i < length; i++)
257 check_regno (int regno)
259 return 0 <= regno && regno < AARCH64_MAX_REGNO;
265 if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
271 aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
276 if (!check_regno (regno))
279 size = reg_size (regno);
286 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
287 val = aarch64_get_reg_u64 (cpu, regno, 0);
290 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
291 val = aarch64_get_FP_double (cpu, regno - 32);
294 case AARCH64_PC_REGNO:
295 val = aarch64_get_PC (cpu);
298 case AARCH64_CPSR_REGNO:
299 val = aarch64_get_CPSR (cpu);
302 case AARCH64_FPSR_REGNO:
303 val = aarch64_get_FPSR (cpu);
307 sim_io_eprintf (CPU_STATE (cpu),
308 "sim: unrecognized register number: %d\n", regno);
312 put_le (buf, length, val);
318 aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
323 if (!check_regno (regno))
326 size = reg_size (regno);
331 val = get_le (buf, length);
335 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
336 aarch64_set_reg_u64 (cpu, regno, 1, val);
339 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
340 aarch64_set_FP_double (cpu, regno - 32, (double) val);
343 case AARCH64_PC_REGNO:
344 aarch64_set_next_PC (cpu, val);
345 aarch64_update_PC (cpu);
348 case AARCH64_CPSR_REGNO:
349 aarch64_set_CPSR (cpu, val);
352 case AARCH64_FPSR_REGNO:
353 aarch64_set_FPSR (cpu, val);
357 sim_io_eprintf (CPU_STATE (cpu),
358 "sim: unrecognized register number: %d\n", regno);
366 aarch64_pc_get (sim_cpu *cpu)
368 return aarch64_get_PC (cpu);
372 aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
374 aarch64_set_next_PC (cpu, pc);
375 aarch64_update_PC (cpu);
379 free_state (SIM_DESC sd)
381 if (STATE_MODULES (sd) != NULL)
382 sim_module_uninstall (sd);
383 sim_cpu_free_all (sd);
389 OPTION_DISAS = OPTION_START,
393 aarch64_option_handler (SIM_DESC sd ATTRIBUTE_UNUSED,
394 sim_cpu * current_cpu ATTRIBUTE_UNUSED,
396 char * arg ATTRIBUTE_UNUSED,
397 int is_command ATTRIBUTE_UNUSED)
406 sim_io_eprintf (sd, "Unknown AArch64 option %d\n", opt);
411 static DECLARE_OPTION_HANDLER (aarch64_option_handler);
413 const OPTION aarch64_options[] =
415 { {"disas", no_argument, NULL, OPTION_DISAS },
416 '\0', NULL, "Enable instruction disassembly",
417 aarch64_option_handler, NULL },
419 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
423 sim_open (SIM_OPEN_KIND kind,
424 struct host_callback_struct * callback,
430 SIM_DESC sd = sim_state_alloc (kind, callback);
435 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
437 sim_add_option_table (sd, NULL, aarch64_options);
439 /* Perform the initialization steps one by one. */
440 if (sim_cpu_alloc_all (sd, 1, 0) != SIM_RC_OK
441 || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
442 || sim_parse_args (sd, argv) != SIM_RC_OK
443 || sim_analyze_program (sd,
444 (STATE_PROG_ARGV (sd) != NULL
445 ? *STATE_PROG_ARGV (sd)
446 : NULL), abfd) != SIM_RC_OK
447 || sim_config (sd) != SIM_RC_OK
448 || sim_post_argv_init (sd) != SIM_RC_OK)
454 aarch64_init_LIT_table ();
456 assert (MAX_NR_PROCESSORS == 1);
457 cpu = STATE_CPU (sd, 0);
458 CPU_PC_FETCH (cpu) = aarch64_pc_get;
459 CPU_PC_STORE (cpu) = aarch64_pc_set;
460 CPU_REG_FETCH (cpu) = aarch64_reg_get;
461 CPU_REG_STORE (cpu) = aarch64_reg_set;
463 /* Set SP, FP and PC to 0 and set LR to -1
464 so we can detect a top-level return. */
465 aarch64_set_reg_u64 (cpu, SP, 1, 0);
466 aarch64_set_reg_u64 (cpu, FP, 1, 0);
467 aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
468 aarch64_set_next_PC (cpu, 0);
469 aarch64_update_PC (cpu);
471 /* Default to a 128 Mbyte (== 2^27) memory space. */
472 sim_do_commandf (sd, "memory-size 0x8000000");
478 sim_engine_run (SIM_DESC sd,
479 int next_cpu_nr ATTRIBUTE_UNUSED,
480 int nr_cpus ATTRIBUTE_UNUSED,
481 int siggnal ATTRIBUTE_UNUSED)