interp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* interp.c -- AArch64 sim interface to GDB.
  2. Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include <assert.h>
  19. #include <signal.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include "ansidecl.h"
  24. #include "bfd.h"
  25. #include "sim/callback.h"
  26. #include "sim/sim.h"
  27. #include "gdb/signals.h"
  28. #include "gdb/sim-aarch64.h"
  29. #include "sim-main.h"
  30. #include "sim-options.h"
  31. #include "memory.h"
  32. #include "simulator.h"
  33. #include "sim-assert.h"
  34. /* Filter out (in place) symbols that are useless for disassembly.
  35. COUNT is the number of elements in SYMBOLS.
  36. Return the number of useful symbols. */
  37. static long
  38. remove_useless_symbols (asymbol **symbols, long count)
  39. {
  40. asymbol **in_ptr = symbols;
  41. asymbol **out_ptr = symbols;
  42. while (count-- > 0)
  43. {
  44. asymbol *sym = *in_ptr++;
  45. if (strstr (sym->name, "gcc2_compiled"))
  46. continue;
  47. if (sym->name == NULL || sym->name[0] == '\0')
  48. continue;
  49. if (sym->flags & (BSF_DEBUGGING))
  50. continue;
  51. if ( bfd_is_und_section (sym->section)
  52. || bfd_is_com_section (sym->section))
  53. continue;
  54. if (sym->name[0] == '$')
  55. continue;
  56. *out_ptr++ = sym;
  57. }
  58. return out_ptr - symbols;
  59. }
  60. static signed int
  61. compare_symbols (const void *ap, const void *bp)
  62. {
  63. const asymbol *a = * (const asymbol **) ap;
  64. const asymbol *b = * (const asymbol **) bp;
  65. if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
  66. return 1;
  67. if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
  68. return -1;
  69. return 0;
  70. }
  71. /* Find the name of the function at ADDR. */
  72. const char *
  73. aarch64_get_func (SIM_DESC sd, uint64_t addr)
  74. {
  75. long symcount = STATE_PROG_SYMS_COUNT (sd);
  76. asymbol **symtab = STATE_PROG_SYMS (sd);
  77. int min, max;
  78. min = -1;
  79. max = symcount;
  80. while (min < max - 1)
  81. {
  82. int sym;
  83. bfd_vma sa;
  84. sym = (min + max) / 2;
  85. sa = bfd_asymbol_value (symtab[sym]);
  86. if (sa > addr)
  87. max = sym;
  88. else if (sa < addr)
  89. min = sym;
  90. else
  91. {
  92. min = sym;
  93. break;
  94. }
  95. }
  96. if (min != -1)
  97. return bfd_asymbol_name (symtab [min]);
  98. return "";
  99. }
  100. SIM_RC
  101. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  102. char * const *argv, char * const *env)
  103. {
  104. sim_cpu *cpu = STATE_CPU (sd, 0);
  105. host_callback *cb = STATE_CALLBACK (sd);
  106. bfd_vma addr = 0;
  107. if (abfd != NULL)
  108. addr = bfd_get_start_address (abfd);
  109. aarch64_set_next_PC (cpu, addr);
  110. aarch64_update_PC (cpu);
  111. /* Standalone mode (i.e. `run`) will take care of the argv for us in
  112. sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
  113. with `gdb`), we need to handle it because the user can change the
  114. argv on the fly via gdb's 'run'. */
  115. if (STATE_PROG_ARGV (sd) != argv)
  116. {
  117. freeargv (STATE_PROG_ARGV (sd));
  118. STATE_PROG_ARGV (sd) = dupargv (argv);
  119. }
  120. if (STATE_PROG_ENVP (sd) != env)
  121. {
  122. freeargv (STATE_PROG_ENVP (sd));
  123. STATE_PROG_ENVP (sd) = dupargv (env);
  124. }
  125. cb->argv = STATE_PROG_ARGV (sd);
  126. cb->envp = STATE_PROG_ENVP (sd);
  127. if (trace_load_symbols (sd))
  128. {
  129. STATE_PROG_SYMS_COUNT (sd) =
  130. remove_useless_symbols (STATE_PROG_SYMS (sd),
  131. STATE_PROG_SYMS_COUNT (sd));
  132. qsort (STATE_PROG_SYMS (sd), STATE_PROG_SYMS_COUNT (sd),
  133. sizeof (asymbol *), compare_symbols);
  134. }
  135. aarch64_init (cpu, addr);
  136. return SIM_RC_OK;
  137. }
  138. /* Read the LENGTH bytes at BUF as a little-endian value. */
  139. static bfd_vma
  140. get_le (unsigned char *buf, unsigned int length)
  141. {
  142. bfd_vma acc = 0;
  143. while (length -- > 0)
  144. acc = (acc << 8) + buf[length];
  145. return acc;
  146. }
  147. /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
  148. static void
  149. put_le (unsigned char *buf, unsigned int length, bfd_vma val)
  150. {
  151. int i;
  152. for (i = 0; i < length; i++)
  153. {
  154. buf[i] = val & 0xff;
  155. val >>= 8;
  156. }
  157. }
  158. static int
  159. check_regno (int regno)
  160. {
  161. return 0 <= regno && regno < AARCH64_MAX_REGNO;
  162. }
  163. static size_t
  164. reg_size (int regno)
  165. {
  166. if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
  167. return 32;
  168. return 64;
  169. }
  170. static int
  171. aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
  172. {
  173. size_t size;
  174. bfd_vma val;
  175. if (!check_regno (regno))
  176. return 0;
  177. size = reg_size (regno);
  178. if (length != size)
  179. return 0;
  180. switch (regno)
  181. {
  182. case AARCH64_MIN_GR ... AARCH64_MAX_GR:
  183. val = aarch64_get_reg_u64 (cpu, regno, 0);
  184. break;
  185. case AARCH64_MIN_FR ... AARCH64_MAX_FR:
  186. val = aarch64_get_FP_double (cpu, regno - 32);
  187. break;
  188. case AARCH64_PC_REGNO:
  189. val = aarch64_get_PC (cpu);
  190. break;
  191. case AARCH64_CPSR_REGNO:
  192. val = aarch64_get_CPSR (cpu);
  193. break;
  194. case AARCH64_FPSR_REGNO:
  195. val = aarch64_get_FPSR (cpu);
  196. break;
  197. default:
  198. sim_io_eprintf (CPU_STATE (cpu),
  199. "sim: unrecognized register number: %d\n", regno);
  200. return -1;
  201. }
  202. put_le (buf, length, val);
  203. return size;
  204. }
  205. static int
  206. aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
  207. {
  208. size_t size;
  209. bfd_vma val;
  210. if (!check_regno (regno))
  211. return -1;
  212. size = reg_size (regno);
  213. if (length != size)
  214. return -1;
  215. val = get_le (buf, length);
  216. switch (regno)
  217. {
  218. case AARCH64_MIN_GR ... AARCH64_MAX_GR:
  219. aarch64_set_reg_u64 (cpu, regno, 1, val);
  220. break;
  221. case AARCH64_MIN_FR ... AARCH64_MAX_FR:
  222. aarch64_set_FP_double (cpu, regno - 32, (double) val);
  223. break;
  224. case AARCH64_PC_REGNO:
  225. aarch64_set_next_PC (cpu, val);
  226. aarch64_update_PC (cpu);
  227. break;
  228. case AARCH64_CPSR_REGNO:
  229. aarch64_set_CPSR (cpu, val);
  230. break;
  231. case AARCH64_FPSR_REGNO:
  232. aarch64_set_FPSR (cpu, val);
  233. break;
  234. default:
  235. sim_io_eprintf (CPU_STATE (cpu),
  236. "sim: unrecognized register number: %d\n", regno);
  237. return 0;
  238. }
  239. return size;
  240. }
  241. static sim_cia
  242. aarch64_pc_get (sim_cpu *cpu)
  243. {
  244. return aarch64_get_PC (cpu);
  245. }
  246. static void
  247. aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
  248. {
  249. aarch64_set_next_PC (cpu, pc);
  250. aarch64_update_PC (cpu);
  251. }
  252. static void
  253. free_state (SIM_DESC sd)
  254. {
  255. if (STATE_MODULES (sd) != NULL)
  256. sim_module_uninstall (sd);
  257. sim_cpu_free_all (sd);
  258. sim_state_free (sd);
  259. }
  260. SIM_DESC
  261. sim_open (SIM_OPEN_KIND kind,
  262. struct host_callback_struct * callback,
  263. struct bfd * abfd,
  264. char * const * argv)
  265. {
  266. sim_cpu *cpu;
  267. SIM_DESC sd = sim_state_alloc (kind, callback);
  268. if (sd == NULL)
  269. return sd;
  270. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  271. /* We use NONSTRICT_ALIGNMENT as the default because AArch64 only enforces
  272. 4-byte alignment, even for 8-byte reads/writes. The common core does not
  273. support this, so we opt for non-strict alignment instead. */
  274. current_alignment = NONSTRICT_ALIGNMENT;
  275. /* Perform the initialization steps one by one. */
  276. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK
  277. || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
  278. || sim_parse_args (sd, argv) != SIM_RC_OK
  279. || sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK
  280. || sim_config (sd) != SIM_RC_OK
  281. || sim_post_argv_init (sd) != SIM_RC_OK)
  282. {
  283. free_state (sd);
  284. return NULL;
  285. }
  286. aarch64_init_LIT_table ();
  287. assert (MAX_NR_PROCESSORS == 1);
  288. cpu = STATE_CPU (sd, 0);
  289. CPU_PC_FETCH (cpu) = aarch64_pc_get;
  290. CPU_PC_STORE (cpu) = aarch64_pc_set;
  291. CPU_REG_FETCH (cpu) = aarch64_reg_get;
  292. CPU_REG_STORE (cpu) = aarch64_reg_set;
  293. /* Set SP, FP and PC to 0 and set LR to -1
  294. so we can detect a top-level return. */
  295. aarch64_set_reg_u64 (cpu, SP, 1, 0);
  296. aarch64_set_reg_u64 (cpu, FP, 1, 0);
  297. aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
  298. aarch64_set_next_PC (cpu, 0);
  299. aarch64_update_PC (cpu);
  300. /* Default to a 128 Mbyte (== 2^27) memory space. */
  301. sim_do_commandf (sd, "memory-size 0x8000000");
  302. return sd;
  303. }
  304. void
  305. sim_engine_run (SIM_DESC sd,
  306. int next_cpu_nr ATTRIBUTE_UNUSED,
  307. int nr_cpus ATTRIBUTE_UNUSED,
  308. int siggnal ATTRIBUTE_UNUSED)
  309. {
  310. aarch64_run (sd);
  311. }