1 /* interp.c -- AArch64 sim interface to GDB.
3 Copyright (C) 2015 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
->private_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 (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
)
200 freeargv (STATE_PROG_ARGV (sd
));
201 STATE_PROG_ARGV (sd
) = dupargv (argv
);
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
;
211 info
.arch
= bfd_arch_aarch64
;
212 disassemble_init_for_target (& info
);
214 storage
= bfd_get_symtab_upper_bound (abfd
);
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
);
223 aarch64_init (cpu
, bfd_get_start_address (abfd
));
228 /* Read the LENGTH bytes at BUF as a little-endian value. */
231 get_le (unsigned char *buf
, unsigned int length
)
235 while (length
-- > 0)
236 acc
= (acc
<< 8) + buf
[length
];
241 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
244 put_le (unsigned char *buf
, unsigned int length
, bfd_vma val
)
248 for (i
= 0; i
< length
; i
++)
256 check_regno (int regno
)
258 return 0 <= regno
&& regno
< AARCH64_MAX_REGNO
;
264 if (regno
== AARCH64_CPSR_REGNO
|| regno
== AARCH64_FPSR_REGNO
)
270 aarch64_reg_get (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int length
)
275 if (!check_regno (regno
))
278 size
= reg_size (regno
);
285 case AARCH64_MIN_GR
... AARCH64_MAX_GR
:
286 val
= aarch64_get_reg_u64 (cpu
, regno
, 0);
289 case AARCH64_MIN_FR
... AARCH64_MAX_FR
:
290 val
= aarch64_get_FP_double (cpu
, regno
- 32);
293 case AARCH64_PC_REGNO
:
294 val
= aarch64_get_PC (cpu
);
297 case AARCH64_CPSR_REGNO
:
298 val
= aarch64_get_CPSR (cpu
);
301 case AARCH64_FPSR_REGNO
:
302 val
= aarch64_get_FPSR (cpu
);
306 sim_io_eprintf (CPU_STATE (cpu
),
307 "sim: unrecognized register number: %d\n", regno
);
311 put_le (buf
, length
, val
);
317 aarch64_reg_set (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int length
)
322 if (!check_regno (regno
))
325 size
= reg_size (regno
);
330 val
= get_le (buf
, length
);
334 case AARCH64_MIN_GR
... AARCH64_MAX_GR
:
335 aarch64_set_reg_u64 (cpu
, regno
, 1, val
);
338 case AARCH64_MIN_FR
... AARCH64_MAX_FR
:
339 aarch64_set_FP_double (cpu
, regno
- 32, (double) val
);
342 case AARCH64_PC_REGNO
:
343 aarch64_set_next_PC (cpu
, val
);
344 aarch64_update_PC (cpu
);
347 case AARCH64_CPSR_REGNO
:
348 aarch64_set_CPSR (cpu
, val
);
351 case AARCH64_FPSR_REGNO
:
352 aarch64_set_FPSR (cpu
, val
);
356 sim_io_eprintf (CPU_STATE (cpu
),
357 "sim: unrecognized register number: %d\n", regno
);
365 aarch64_pc_get (sim_cpu
*cpu
)
367 return aarch64_get_PC (cpu
);
371 aarch64_pc_set (sim_cpu
*cpu
, sim_cia pc
)
373 aarch64_set_next_PC (cpu
, pc
);
374 aarch64_update_PC (cpu
);
378 free_state (SIM_DESC sd
)
380 if (STATE_MODULES (sd
) != NULL
)
381 sim_module_uninstall (sd
);
382 sim_cpu_free_all (sd
);
388 OPTION_DISAS
= OPTION_START
,
392 aarch64_option_handler (SIM_DESC sd ATTRIBUTE_UNUSED
,
393 sim_cpu
* current_cpu ATTRIBUTE_UNUSED
,
395 char * arg ATTRIBUTE_UNUSED
,
396 int is_command ATTRIBUTE_UNUSED
)
405 sim_io_eprintf (sd
, "Unknown AArch64 option %d\n", opt
);
410 static DECLARE_OPTION_HANDLER (aarch64_option_handler
);
412 const OPTION aarch64_options
[] =
414 { {"disas", no_argument
, NULL
, OPTION_DISAS
},
415 '\0', NULL
, "Enable instruction disassembly",
416 aarch64_option_handler
, NULL
},
418 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
422 sim_open (SIM_OPEN_KIND kind
,
423 struct host_callback_struct
* callback
,
429 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
434 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
436 sim_add_option_table (sd
, NULL
, aarch64_options
);
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
)
453 aarch64_init_LIT_table ();
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
;
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
);
470 /* Default to a 128 Mbyte (== 2^27) memory space. */
471 sim_do_commandf (sd
, "memory-size 0x8000000");
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
)