]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/aarch64/interp.c
Add an AArch64 simulator to GDB.
[thirdparty/binutils-gdb.git] / sim / aarch64 / interp.c
1 /* interp.c -- AArch64 sim interface to GDB.
2
3 Copyright (C) 2015 Free Software Foundation, Inc.
4
5 Contributed by Red Hat.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29
30 #include "ansidecl.h"
31 #include "gdb/callback.h"
32 #include "gdb/remote-sim.h"
33 #include "gdb/signals.h"
34 #include "gdb/sim-aarch64.h"
35
36 #include "sim-main.h"
37 #include "sim-options.h"
38 #include "memory.h"
39 #include "simulator.h"
40
41 #include "dis-asm.h"
42
43 static struct disassemble_info info;
44 static unsigned long symcount = 0;
45 static asymbol ** symtab = NULL;
46
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];
52
53 static int op_printf (void *, const char *, ...) ATTRIBUTE_FPTR_PRINTF_2;
54
55 static int
56 op_printf (void *stream ATTRIBUTE_UNUSED, const char *fmt, ...)
57 {
58 size_t space_remaining;
59 int ret;
60 va_list ap;
61
62 space_remaining = sizeof (opbuf) - strlen (opbuf);
63 va_start (ap, fmt);
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);
68 va_end (ap);
69 return ret;
70 }
71
72 void
73 aarch64_print_insn (SIM_DESC sd, uint64_t addr)
74 {
75 int size;
76
77 opbuf[0] = 0;
78 size = print_insn_aarch64 (addr, & info);
79 sim_io_eprintf (sd, " %*s\n", size, opbuf);
80 }
81
82 static int
83 sim_dis_read (bfd_vma memaddr,
84 bfd_byte * ptr,
85 unsigned int length,
86 struct disassemble_info * info)
87 {
88 aarch64_get_mem_blk (info->private_data, memaddr, (char *) ptr, length);
89
90 return 0;
91 }
92
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. */
96
97 static unsigned long
98 remove_useless_symbols (asymbol **symbols, unsigned long count)
99 {
100 asymbol **in_ptr = symbols;
101 asymbol **out_ptr = symbols;
102
103 while (count-- > 0)
104 {
105 asymbol *sym = *in_ptr++;
106
107 if (strstr (sym->name, "gcc2_compiled"))
108 continue;
109 if (sym->name == NULL || sym->name[0] == '\0')
110 continue;
111 if (sym->flags & (BSF_DEBUGGING))
112 continue;
113 if ( bfd_is_und_section (sym->section)
114 || bfd_is_com_section (sym->section))
115 continue;
116 if (sym->name[0] == '$')
117 continue;
118
119 *out_ptr++ = sym;
120 }
121 return out_ptr - symbols;
122 }
123
124 static signed int
125 compare_symbols (const void *ap, const void *bp)
126 {
127 const asymbol *a = * (const asymbol **) ap;
128 const asymbol *b = * (const asymbol **) bp;
129
130 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
131 return 1;
132 if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
133 return -1;
134 return 0;
135 }
136
137 /* Find the name of the function at ADDR. */
138 const char *
139 aarch64_get_func (uint64_t addr)
140 {
141 int min, max;
142
143 min = -1;
144 max = symcount;
145 while (min < max - 1)
146 {
147 int sym;
148 bfd_vma sa;
149
150 sym = (min + max) / 2;
151 sa = bfd_asymbol_value (symtab[sym]);
152
153 if (sa > addr)
154 max = sym;
155 else if (sa < addr)
156 min = sym;
157 else
158 {
159 min = sym;
160 break;
161 }
162 }
163
164 if (min != -1)
165 return bfd_asymbol_name (symtab [min]);
166
167 return "";
168 }
169
170 uint64_t
171 aarch64_get_sym_value (const char *name)
172 {
173 unsigned long i;
174
175 for (i = 0; i < symcount; i++)
176 if (strcmp (bfd_asymbol_name (symtab[i]), name) == 0)
177 return bfd_asymbol_value (symtab[i]);
178
179 return 0;
180 }
181
182 SIM_RC
183 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
184 {
185 sim_cpu *cpu = STATE_CPU (sd, 0);
186 long storage;
187 bfd_vma addr = 0;
188
189 if (abfd != NULL)
190 addr = bfd_get_start_address (abfd);
191
192 aarch64_set_next_PC (cpu, addr);
193 aarch64_update_PC (cpu);
194
195 /* Standalone mode (ie aarch64-elf-run) will take care of the argv
196 for us in sim_open() -> sim_parse_args(). But in debug mode (i.e.
197 'target sim' with `aarch64-...-gdb`), we need to handle it. */
198 if (STATE_OPEN_KIND (sd) == SIM_OPEN_DEBUG)
199 {
200 freeargv (STATE_PROG_ARGV (sd));
201 STATE_PROG_ARGV (sd) = dupargv (argv);
202 }
203
204 memset (& info, 0, sizeof (info));
205 init_disassemble_info (& info, NULL, op_printf);
206 info.read_memory_func = sim_dis_read;
207 info.arch = bfd_get_arch (abfd);
208 info.mach = bfd_get_mach (abfd);
209 info.private_data = cpu;
210 if (info.mach == 0)
211 info.arch = bfd_arch_aarch64;
212 disassemble_init_for_target (& info);
213
214 storage = bfd_get_symtab_upper_bound (abfd);
215 if (storage > 0)
216 {
217 symtab = (asymbol **) xmalloc (storage);
218 symcount = bfd_canonicalize_symtab (abfd, symtab);
219 symcount = remove_useless_symbols (symtab, symcount);
220 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
221 }
222
223 aarch64_init (cpu, bfd_get_start_address (abfd));
224
225 return SIM_RC_OK;
226 }
227
228 /* Read the LENGTH bytes at BUF as a little-endian value. */
229
230 static bfd_vma
231 get_le (unsigned char *buf, unsigned int length)
232 {
233 bfd_vma acc = 0;
234
235 while (length -- > 0)
236 acc = (acc << 8) + buf[length];
237
238 return acc;
239 }
240
241 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
242
243 static void
244 put_le (unsigned char *buf, unsigned int length, bfd_vma val)
245 {
246 int i;
247
248 for (i = 0; i < length; i++)
249 {
250 buf[i] = val & 0xff;
251 val >>= 8;
252 }
253 }
254
255 static int
256 check_regno (int regno)
257 {
258 return 0 <= regno && regno < AARCH64_MAX_REGNO;
259 }
260
261 static size_t
262 reg_size (int regno)
263 {
264 if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
265 return 32;
266 return 64;
267 }
268
269 static int
270 aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
271 {
272 size_t size;
273 bfd_vma val;
274
275 if (!check_regno (regno))
276 return 0;
277
278 size = reg_size (regno);
279
280 if (length != size)
281 return 0;
282
283 switch (regno)
284 {
285 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
286 val = aarch64_get_reg_u64 (cpu, regno, 0);
287 break;
288
289 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
290 val = aarch64_get_FP_double (cpu, regno - 32);
291 break;
292
293 case AARCH64_PC_REGNO:
294 val = aarch64_get_PC (cpu);
295 break;
296
297 case AARCH64_CPSR_REGNO:
298 val = aarch64_get_CPSR (cpu);
299 break;
300
301 case AARCH64_FPSR_REGNO:
302 val = aarch64_get_FPSR (cpu);
303 break;
304
305 default:
306 sim_io_eprintf (CPU_STATE (cpu),
307 "sim: unrecognized register number: %d\n", regno);
308 return -1;
309 }
310
311 put_le (buf, length, val);
312
313 return size;
314 }
315
316 static int
317 aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
318 {
319 size_t size;
320 bfd_vma val;
321
322 if (!check_regno (regno))
323 return -1;
324
325 size = reg_size (regno);
326
327 if (length != size)
328 return -1;
329
330 val = get_le (buf, length);
331
332 switch (regno)
333 {
334 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
335 aarch64_set_reg_u64 (cpu, regno, 1, val);
336 break;
337
338 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
339 aarch64_set_FP_double (cpu, regno - 32, (double) val);
340 break;
341
342 case AARCH64_PC_REGNO:
343 aarch64_set_next_PC (cpu, val);
344 aarch64_update_PC (cpu);
345 break;
346
347 case AARCH64_CPSR_REGNO:
348 aarch64_set_CPSR (cpu, val);
349 break;
350
351 case AARCH64_FPSR_REGNO:
352 aarch64_set_FPSR (cpu, val);
353 break;
354
355 default:
356 sim_io_eprintf (CPU_STATE (cpu),
357 "sim: unrecognized register number: %d\n", regno);
358 return 0;
359 }
360
361 return size;
362 }
363
364 static sim_cia
365 aarch64_pc_get (sim_cpu *cpu)
366 {
367 return aarch64_get_PC (cpu);
368 }
369
370 static void
371 aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
372 {
373 aarch64_set_next_PC (cpu, pc);
374 aarch64_update_PC (cpu);
375 }
376
377 static void
378 free_state (SIM_DESC sd)
379 {
380 if (STATE_MODULES (sd) != NULL)
381 sim_module_uninstall (sd);
382 sim_cpu_free_all (sd);
383 sim_state_free (sd);
384 }
385
386 enum
387 {
388 OPTION_DISAS = OPTION_START,
389 };
390
391 static SIM_RC
392 aarch64_option_handler (SIM_DESC sd ATTRIBUTE_UNUSED,
393 sim_cpu * current_cpu ATTRIBUTE_UNUSED,
394 int opt,
395 char * arg ATTRIBUTE_UNUSED,
396 int is_command ATTRIBUTE_UNUSED)
397 {
398 switch (opt)
399 {
400 case OPTION_DISAS:
401 disas = TRUE;
402 return SIM_RC_OK;
403
404 default:
405 sim_io_eprintf (sd, "Unknown AArch64 option %d\n", opt);
406 return SIM_RC_FAIL;
407 }
408 }
409
410 static DECLARE_OPTION_HANDLER (aarch64_option_handler);
411
412 const OPTION aarch64_options[] =
413 {
414 { {"disas", no_argument, NULL, OPTION_DISAS },
415 '\0', NULL, "Enable instruction disassembly",
416 aarch64_option_handler, NULL },
417
418 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
419 };
420
421 SIM_DESC
422 sim_open (SIM_OPEN_KIND kind,
423 struct host_callback_struct * callback,
424 struct bfd * abfd,
425 char ** argv)
426 {
427 int i;
428 sim_cpu *cpu;
429 SIM_DESC sd = sim_state_alloc (kind, callback);
430
431 if (sd == NULL)
432 return sd;
433
434 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
435
436 sim_add_option_table (sd, NULL, aarch64_options);
437
438 /* Perform the initialization steps one by one. */
439 if (sim_cpu_alloc_all (sd, 1, 0) != SIM_RC_OK
440 || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
441 || sim_parse_args (sd, argv) != SIM_RC_OK
442 || sim_analyze_program (sd,
443 (STATE_PROG_ARGV (sd) != NULL
444 ? *STATE_PROG_ARGV (sd)
445 : NULL), abfd) != SIM_RC_OK
446 || sim_config (sd) != SIM_RC_OK
447 || sim_post_argv_init (sd) != SIM_RC_OK)
448 {
449 free_state (sd);
450 return NULL;
451 }
452
453 aarch64_init_LIT_table ();
454
455 assert (MAX_NR_PROCESSORS == 1);
456 cpu = STATE_CPU (sd, 0);
457 CPU_PC_FETCH (cpu) = aarch64_pc_get;
458 CPU_PC_STORE (cpu) = aarch64_pc_set;
459 CPU_REG_FETCH (cpu) = aarch64_reg_get;
460 CPU_REG_STORE (cpu) = aarch64_reg_set;
461
462 /* Set SP, FP and PC to 0 and set LR to -1
463 so we can detect a top-level return. */
464 aarch64_set_reg_u64 (cpu, SP, 1, 0);
465 aarch64_set_reg_u64 (cpu, FP, 1, 0);
466 aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
467 aarch64_set_next_PC (cpu, 0);
468 aarch64_update_PC (cpu);
469
470 /* Default to a 128 Mbyte (== 2^27) memory space. */
471 sim_do_commandf (sd, "memory-size 0x8000000");
472
473 return sd;
474 }
475
476 void
477 sim_engine_run (SIM_DESC sd,
478 int next_cpu_nr ATTRIBUTE_UNUSED,
479 int nr_cpus ATTRIBUTE_UNUSED,
480 int siggnal ATTRIBUTE_UNUSED)
481 {
482 aarch64_run (sd);
483 }