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"
41 static unsigned long symcount = 0;
42 static asymbol ** symtab = NULL;
44 /* Filter out (in place) symbols that are useless for disassembly.
45 COUNT is the number of elements in SYMBOLS.
46 Return the number of useful symbols. */
49 remove_useless_symbols (asymbol **symbols, unsigned long count)
51 asymbol **in_ptr = symbols;
52 asymbol **out_ptr = symbols;
56 asymbol *sym = *in_ptr++;
58 if (strstr (sym->name, "gcc2_compiled"))
60 if (sym->name == NULL || sym->name[0] == '\0')
62 if (sym->flags & (BSF_DEBUGGING))
64 if ( bfd_is_und_section (sym->section)
65 || bfd_is_com_section (sym->section))
67 if (sym->name[0] == '$')
72 return out_ptr - symbols;
76 compare_symbols (const void *ap, const void *bp)
78 const asymbol *a = * (const asymbol **) ap;
79 const asymbol *b = * (const asymbol **) bp;
81 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
83 if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
88 /* Find the name of the function at ADDR. */
90 aarch64_get_func (uint64_t addr)
101 sym = (min + max) / 2;
102 sa = bfd_asymbol_value (symtab[sym]);
116 return bfd_asymbol_name (symtab [min]);
122 aarch64_get_sym_value (const char *name)
126 for (i = 0; i < symcount; i++)
127 if (strcmp (bfd_asymbol_name (symtab[i]), name) == 0)
128 return bfd_asymbol_value (symtab[i]);
134 sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
135 char * const *argv, char * const *env)
137 sim_cpu *cpu = STATE_CPU (sd, 0);
142 addr = bfd_get_start_address (abfd);
144 aarch64_set_next_PC (cpu, addr);
145 aarch64_update_PC (cpu);
147 /* Standalone mode (i.e. `run`) will take care of the argv for us in
148 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
149 with `gdb`), we need to handle it because the user can change the
150 argv on the fly via gdb's 'run'. */
151 if (STATE_PROG_ARGV (sd) != argv)
153 freeargv (STATE_PROG_ARGV (sd));
154 STATE_PROG_ARGV (sd) = dupargv (argv);
157 storage = bfd_get_symtab_upper_bound (abfd);
160 symtab = (asymbol **) xmalloc (storage);
161 symcount = bfd_canonicalize_symtab (abfd, symtab);
162 symcount = remove_useless_symbols (symtab, symcount);
163 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
166 aarch64_init (cpu, bfd_get_start_address (abfd));
171 /* Read the LENGTH bytes at BUF as a little-endian value. */
174 get_le (unsigned char *buf, unsigned int length)
178 while (length -- > 0)
179 acc = (acc << 8) + buf[length];
184 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
187 put_le (unsigned char *buf, unsigned int length, bfd_vma val)
191 for (i = 0; i < length; i++)
199 check_regno (int regno)
201 return 0 <= regno && regno < AARCH64_MAX_REGNO;
207 if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
213 aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
218 if (!check_regno (regno))
221 size = reg_size (regno);
228 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
229 val = aarch64_get_reg_u64 (cpu, regno, 0);
232 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
233 val = aarch64_get_FP_double (cpu, regno - 32);
236 case AARCH64_PC_REGNO:
237 val = aarch64_get_PC (cpu);
240 case AARCH64_CPSR_REGNO:
241 val = aarch64_get_CPSR (cpu);
244 case AARCH64_FPSR_REGNO:
245 val = aarch64_get_FPSR (cpu);
249 sim_io_eprintf (CPU_STATE (cpu),
250 "sim: unrecognized register number: %d\n", regno);
254 put_le (buf, length, val);
260 aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
265 if (!check_regno (regno))
268 size = reg_size (regno);
273 val = get_le (buf, length);
277 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
278 aarch64_set_reg_u64 (cpu, regno, 1, val);
281 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
282 aarch64_set_FP_double (cpu, regno - 32, (double) val);
285 case AARCH64_PC_REGNO:
286 aarch64_set_next_PC (cpu, val);
287 aarch64_update_PC (cpu);
290 case AARCH64_CPSR_REGNO:
291 aarch64_set_CPSR (cpu, val);
294 case AARCH64_FPSR_REGNO:
295 aarch64_set_FPSR (cpu, val);
299 sim_io_eprintf (CPU_STATE (cpu),
300 "sim: unrecognized register number: %d\n", regno);
308 aarch64_pc_get (sim_cpu *cpu)
310 return aarch64_get_PC (cpu);
314 aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
316 aarch64_set_next_PC (cpu, pc);
317 aarch64_update_PC (cpu);
321 free_state (SIM_DESC sd)
323 if (STATE_MODULES (sd) != NULL)
324 sim_module_uninstall (sd);
325 sim_cpu_free_all (sd);
330 sim_open (SIM_OPEN_KIND kind,
331 struct host_callback_struct * callback,
337 SIM_DESC sd = sim_state_alloc (kind, callback);
342 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
344 /* Perform the initialization steps one by one. */
345 if (sim_cpu_alloc_all (sd, 1, 0) != SIM_RC_OK
346 || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
347 || sim_parse_args (sd, argv) != SIM_RC_OK
348 || sim_analyze_program (sd,
349 (STATE_PROG_ARGV (sd) != NULL
350 ? *STATE_PROG_ARGV (sd)
351 : NULL), abfd) != SIM_RC_OK
352 || sim_config (sd) != SIM_RC_OK
353 || sim_post_argv_init (sd) != SIM_RC_OK)
359 aarch64_init_LIT_table ();
361 assert (MAX_NR_PROCESSORS == 1);
362 cpu = STATE_CPU (sd, 0);
363 CPU_PC_FETCH (cpu) = aarch64_pc_get;
364 CPU_PC_STORE (cpu) = aarch64_pc_set;
365 CPU_REG_FETCH (cpu) = aarch64_reg_get;
366 CPU_REG_STORE (cpu) = aarch64_reg_set;
368 /* Set SP, FP and PC to 0 and set LR to -1
369 so we can detect a top-level return. */
370 aarch64_set_reg_u64 (cpu, SP, 1, 0);
371 aarch64_set_reg_u64 (cpu, FP, 1, 0);
372 aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
373 aarch64_set_next_PC (cpu, 0);
374 aarch64_update_PC (cpu);
376 /* Default to a 128 Mbyte (== 2^27) memory space. */
377 sim_do_commandf (sd, "memory-size 0x8000000");
383 sim_engine_run (SIM_DESC sd,
384 int next_cpu_nr ATTRIBUTE_UNUSED,
385 int nr_cpus ATTRIBUTE_UNUSED,
386 int siggnal ATTRIBUTE_UNUSED)