1 /* interp.c -- AArch64 sim interface to GDB.
3 Copyright (C) 2015-2021 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/>. */
22 /* This must come before any other includes. */
34 #include "sim/callback.h"
36 #include "gdb/signals.h"
37 #include "gdb/sim-aarch64.h"
40 #include "sim-options.h"
42 #include "simulator.h"
43 #include "sim-assert.h"
45 /* Filter out (in place) symbols that are useless for disassembly.
46 COUNT is the number of elements in SYMBOLS.
47 Return the number of useful symbols. */
50 remove_useless_symbols (asymbol
**symbols
, long count
)
52 asymbol
**in_ptr
= symbols
;
53 asymbol
**out_ptr
= symbols
;
57 asymbol
*sym
= *in_ptr
++;
59 if (strstr (sym
->name
, "gcc2_compiled"))
61 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
63 if (sym
->flags
& (BSF_DEBUGGING
))
65 if ( bfd_is_und_section (sym
->section
)
66 || bfd_is_com_section (sym
->section
))
68 if (sym
->name
[0] == '$')
73 return out_ptr
- symbols
;
77 compare_symbols (const void *ap
, const void *bp
)
79 const asymbol
*a
= * (const asymbol
**) ap
;
80 const asymbol
*b
= * (const asymbol
**) bp
;
82 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
84 if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
89 /* Find the name of the function at ADDR. */
91 aarch64_get_func (SIM_DESC sd
, uint64_t addr
)
93 long symcount
= STATE_PROG_SYMS_COUNT (sd
);
94 asymbol
**symtab
= STATE_PROG_SYMS (sd
);
104 sym
= (min
+ max
) / 2;
105 sa
= bfd_asymbol_value (symtab
[sym
]);
119 return bfd_asymbol_name (symtab
[min
]);
125 sim_create_inferior (SIM_DESC sd
, struct bfd
*abfd
,
126 char * const *argv
, char * const *env
)
128 sim_cpu
*cpu
= STATE_CPU (sd
, 0);
132 addr
= bfd_get_start_address (abfd
);
134 aarch64_set_next_PC (cpu
, addr
);
135 aarch64_update_PC (cpu
);
137 /* Standalone mode (i.e. `run`) will take care of the argv for us in
138 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
139 with `gdb`), we need to handle it because the user can change the
140 argv on the fly via gdb's 'run'. */
141 if (STATE_PROG_ARGV (sd
) != argv
)
143 freeargv (STATE_PROG_ARGV (sd
));
144 STATE_PROG_ARGV (sd
) = dupargv (argv
);
147 if (trace_load_symbols (sd
))
149 STATE_PROG_SYMS_COUNT (sd
) =
150 remove_useless_symbols (STATE_PROG_SYMS (sd
),
151 STATE_PROG_SYMS_COUNT (sd
));
152 qsort (STATE_PROG_SYMS (sd
), STATE_PROG_SYMS_COUNT (sd
),
153 sizeof (asymbol
*), compare_symbols
);
156 aarch64_init (cpu
, addr
);
161 /* Read the LENGTH bytes at BUF as a little-endian value. */
164 get_le (unsigned char *buf
, unsigned int length
)
168 while (length
-- > 0)
169 acc
= (acc
<< 8) + buf
[length
];
174 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
177 put_le (unsigned char *buf
, unsigned int length
, bfd_vma val
)
181 for (i
= 0; i
< length
; i
++)
189 check_regno (int regno
)
191 return 0 <= regno
&& regno
< AARCH64_MAX_REGNO
;
197 if (regno
== AARCH64_CPSR_REGNO
|| regno
== AARCH64_FPSR_REGNO
)
203 aarch64_reg_get (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int length
)
208 if (!check_regno (regno
))
211 size
= reg_size (regno
);
218 case AARCH64_MIN_GR
... AARCH64_MAX_GR
:
219 val
= aarch64_get_reg_u64 (cpu
, regno
, 0);
222 case AARCH64_MIN_FR
... AARCH64_MAX_FR
:
223 val
= aarch64_get_FP_double (cpu
, regno
- 32);
226 case AARCH64_PC_REGNO
:
227 val
= aarch64_get_PC (cpu
);
230 case AARCH64_CPSR_REGNO
:
231 val
= aarch64_get_CPSR (cpu
);
234 case AARCH64_FPSR_REGNO
:
235 val
= aarch64_get_FPSR (cpu
);
239 sim_io_eprintf (CPU_STATE (cpu
),
240 "sim: unrecognized register number: %d\n", regno
);
244 put_le (buf
, length
, val
);
250 aarch64_reg_set (SIM_CPU
*cpu
, int regno
, unsigned char *buf
, int length
)
255 if (!check_regno (regno
))
258 size
= reg_size (regno
);
263 val
= get_le (buf
, length
);
267 case AARCH64_MIN_GR
... AARCH64_MAX_GR
:
268 aarch64_set_reg_u64 (cpu
, regno
, 1, val
);
271 case AARCH64_MIN_FR
... AARCH64_MAX_FR
:
272 aarch64_set_FP_double (cpu
, regno
- 32, (double) val
);
275 case AARCH64_PC_REGNO
:
276 aarch64_set_next_PC (cpu
, val
);
277 aarch64_update_PC (cpu
);
280 case AARCH64_CPSR_REGNO
:
281 aarch64_set_CPSR (cpu
, val
);
284 case AARCH64_FPSR_REGNO
:
285 aarch64_set_FPSR (cpu
, val
);
289 sim_io_eprintf (CPU_STATE (cpu
),
290 "sim: unrecognized register number: %d\n", regno
);
298 aarch64_pc_get (sim_cpu
*cpu
)
300 return aarch64_get_PC (cpu
);
304 aarch64_pc_set (sim_cpu
*cpu
, sim_cia pc
)
306 aarch64_set_next_PC (cpu
, pc
);
307 aarch64_update_PC (cpu
);
311 free_state (SIM_DESC sd
)
313 if (STATE_MODULES (sd
) != NULL
)
314 sim_module_uninstall (sd
);
315 sim_cpu_free_all (sd
);
320 sim_open (SIM_OPEN_KIND kind
,
321 struct host_callback_struct
* callback
,
326 SIM_DESC sd
= sim_state_alloc (kind
, callback
);
331 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
333 /* We use NONSTRICT_ALIGNMENT as the default because AArch64 only enforces
334 4-byte alignment, even for 8-byte reads/writes. The common core does not
335 support this, so we opt for non-strict alignment instead. */
336 current_alignment
= NONSTRICT_ALIGNMENT
;
338 /* Perform the initialization steps one by one. */
339 if (sim_cpu_alloc_all (sd
, 1) != SIM_RC_OK
340 || sim_pre_argv_init (sd
, argv
[0]) != SIM_RC_OK
341 || sim_parse_args (sd
, argv
) != SIM_RC_OK
342 || sim_analyze_program (sd
,
343 (STATE_PROG_ARGV (sd
) != NULL
344 ? *STATE_PROG_ARGV (sd
)
345 : NULL
), abfd
) != SIM_RC_OK
346 || sim_config (sd
) != SIM_RC_OK
347 || sim_post_argv_init (sd
) != SIM_RC_OK
)
353 aarch64_init_LIT_table ();
355 assert (MAX_NR_PROCESSORS
== 1);
356 cpu
= STATE_CPU (sd
, 0);
357 CPU_PC_FETCH (cpu
) = aarch64_pc_get
;
358 CPU_PC_STORE (cpu
) = aarch64_pc_set
;
359 CPU_REG_FETCH (cpu
) = aarch64_reg_get
;
360 CPU_REG_STORE (cpu
) = aarch64_reg_set
;
362 /* Set SP, FP and PC to 0 and set LR to -1
363 so we can detect a top-level return. */
364 aarch64_set_reg_u64 (cpu
, SP
, 1, 0);
365 aarch64_set_reg_u64 (cpu
, FP
, 1, 0);
366 aarch64_set_reg_u64 (cpu
, LR
, 1, TOP_LEVEL_RETURN_PC
);
367 aarch64_set_next_PC (cpu
, 0);
368 aarch64_update_PC (cpu
);
370 /* Default to a 128 Mbyte (== 2^27) memory space. */
371 sim_do_commandf (sd
, "memory-size 0x8000000");
377 sim_engine_run (SIM_DESC sd
,
378 int next_cpu_nr ATTRIBUTE_UNUSED
,
379 int nr_cpus ATTRIBUTE_UNUSED
,
380 int siggnal ATTRIBUTE_UNUSED
)