GDB copyright headers update after running GDB's copyright.py script.
[external/binutils.git] / sim / aarch64 / interp.c
1 /* interp.c -- AArch64 sim interface to GDB.
2
3    Copyright (C) 2015-2016 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->application_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 (i.e. `run`) will take care of the argv for us in
196      sim_open() -> sim_parse_args().  But in debug mode (i.e. 'target sim'
197      with `gdb`), we need to handle it because the user can change the
198      argv on the fly via gdb's 'run'.  */
199   if (STATE_PROG_ARGV (sd) != argv)
200     {
201       freeargv (STATE_PROG_ARGV (sd));
202       STATE_PROG_ARGV (sd) = dupargv (argv);
203     }
204
205   memset (& info, 0, sizeof (info));
206   init_disassemble_info (& info, NULL, op_printf);
207   info.read_memory_func = sim_dis_read;
208   info.arch = bfd_get_arch (abfd);
209   info.mach = bfd_get_mach (abfd);
210   info.application_data = cpu;
211   if (info.mach == 0)
212     info.arch = bfd_arch_aarch64;
213   disassemble_init_for_target (& info);
214
215   storage = bfd_get_symtab_upper_bound (abfd);
216   if (storage > 0)
217     {
218       symtab = (asymbol **) xmalloc (storage);
219       symcount = bfd_canonicalize_symtab (abfd, symtab);
220       symcount = remove_useless_symbols (symtab, symcount);
221       qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
222     }
223
224   aarch64_init (cpu, bfd_get_start_address (abfd));
225
226   return SIM_RC_OK;
227 }
228
229 /* Read the LENGTH bytes at BUF as a little-endian value.  */
230
231 static bfd_vma
232 get_le (unsigned char *buf, unsigned int length)
233 {
234   bfd_vma acc = 0;
235
236   while (length -- > 0)
237     acc = (acc << 8) + buf[length];
238
239   return acc;
240 }
241
242 /* Store VAL as a little-endian value in the LENGTH bytes at BUF.  */
243
244 static void
245 put_le (unsigned char *buf, unsigned int length, bfd_vma val)
246 {
247   int i;
248
249   for (i = 0; i < length; i++)
250     {
251       buf[i] = val & 0xff;
252       val >>= 8;
253     }
254 }
255
256 static int
257 check_regno (int regno)
258 {
259   return 0 <= regno && regno < AARCH64_MAX_REGNO;
260 }
261
262 static size_t
263 reg_size (int regno)
264 {
265   if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
266     return 32;
267   return 64;
268 }
269
270 static int
271 aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
272 {
273   size_t size;
274   bfd_vma val;
275
276   if (!check_regno (regno))
277     return 0;
278
279   size = reg_size (regno);
280
281   if (length != size)
282     return 0;
283
284   switch (regno)
285     {
286     case AARCH64_MIN_GR ... AARCH64_MAX_GR:
287       val = aarch64_get_reg_u64 (cpu, regno, 0);
288       break;
289
290     case AARCH64_MIN_FR ... AARCH64_MAX_FR:
291       val = aarch64_get_FP_double (cpu, regno - 32);
292       break;
293
294     case AARCH64_PC_REGNO:
295       val = aarch64_get_PC (cpu);
296       break;
297
298     case AARCH64_CPSR_REGNO:
299       val = aarch64_get_CPSR (cpu);
300       break;
301
302     case AARCH64_FPSR_REGNO:
303       val = aarch64_get_FPSR (cpu);
304       break;
305
306     default:
307       sim_io_eprintf (CPU_STATE (cpu),
308                       "sim: unrecognized register number: %d\n", regno);
309       return -1;
310     }
311
312   put_le (buf, length, val);
313
314   return size;
315 }
316
317 static int
318 aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
319 {
320   size_t size;
321   bfd_vma val;
322
323   if (!check_regno (regno))
324     return -1;
325
326   size = reg_size (regno);
327
328   if (length != size)
329     return -1;
330
331   val = get_le (buf, length);
332
333   switch (regno)
334     {
335     case AARCH64_MIN_GR ... AARCH64_MAX_GR:
336       aarch64_set_reg_u64 (cpu, regno, 1, val);
337       break;
338
339     case AARCH64_MIN_FR ... AARCH64_MAX_FR:
340       aarch64_set_FP_double (cpu, regno - 32, (double) val);
341       break;
342
343     case AARCH64_PC_REGNO:
344       aarch64_set_next_PC (cpu, val);
345       aarch64_update_PC (cpu);
346       break;
347
348     case AARCH64_CPSR_REGNO:
349       aarch64_set_CPSR (cpu, val);
350       break;
351
352     case AARCH64_FPSR_REGNO:
353       aarch64_set_FPSR (cpu, val);
354       break;
355
356     default:
357       sim_io_eprintf (CPU_STATE (cpu),
358                       "sim: unrecognized register number: %d\n", regno);
359       return 0;
360     }
361
362   return size;
363 }
364
365 static sim_cia
366 aarch64_pc_get (sim_cpu *cpu)
367 {
368   return aarch64_get_PC (cpu);
369 }
370
371 static void
372 aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
373 {
374   aarch64_set_next_PC (cpu, pc);
375   aarch64_update_PC (cpu);
376 }
377
378 static void
379 free_state (SIM_DESC sd)
380 {
381   if (STATE_MODULES (sd) != NULL)
382     sim_module_uninstall (sd);
383   sim_cpu_free_all (sd);
384   sim_state_free (sd);
385 }
386
387 enum
388 {
389   OPTION_DISAS = OPTION_START,
390 };
391
392 static SIM_RC
393 aarch64_option_handler (SIM_DESC  sd ATTRIBUTE_UNUSED,
394                         sim_cpu * current_cpu ATTRIBUTE_UNUSED,
395                         int       opt,
396                         char *    arg ATTRIBUTE_UNUSED,
397                         int       is_command ATTRIBUTE_UNUSED)
398 {
399   switch (opt)
400     {
401     case OPTION_DISAS:
402       disas = TRUE;
403       return SIM_RC_OK;
404
405     default:
406       sim_io_eprintf (sd, "Unknown AArch64 option %d\n", opt);
407       return SIM_RC_FAIL;
408     }
409 }
410
411 static DECLARE_OPTION_HANDLER (aarch64_option_handler);
412
413 const OPTION aarch64_options[] =
414 {
415   { {"disas", no_argument, NULL, OPTION_DISAS },
416       '\0', NULL, "Enable instruction disassembly",
417       aarch64_option_handler, NULL },
418
419   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
420 };
421
422 SIM_DESC
423 sim_open (SIM_OPEN_KIND                  kind,
424           struct host_callback_struct *  callback,
425           struct bfd *                   abfd,
426           char **                        argv)
427 {
428   int i;
429   sim_cpu *cpu;
430   SIM_DESC sd = sim_state_alloc (kind, callback);
431
432   if (sd == NULL)
433     return sd;
434
435   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
436
437   sim_add_option_table (sd, NULL, aarch64_options);
438
439   /* Perform the initialization steps one by one.  */
440   if (sim_cpu_alloc_all (sd, 1, 0) != SIM_RC_OK
441       || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
442       || sim_parse_args (sd, argv) != SIM_RC_OK
443       || sim_analyze_program (sd,
444                               (STATE_PROG_ARGV (sd) != NULL
445                                ? *STATE_PROG_ARGV (sd)
446                                : NULL), abfd) != SIM_RC_OK
447       || sim_config (sd) != SIM_RC_OK
448       || sim_post_argv_init (sd) != SIM_RC_OK)
449     {
450       free_state (sd);
451       return NULL;
452     }
453
454   aarch64_init_LIT_table ();
455
456   assert (MAX_NR_PROCESSORS == 1);
457   cpu = STATE_CPU (sd, 0);
458   CPU_PC_FETCH (cpu) = aarch64_pc_get;
459   CPU_PC_STORE (cpu) = aarch64_pc_set;
460   CPU_REG_FETCH (cpu) = aarch64_reg_get;
461   CPU_REG_STORE (cpu) = aarch64_reg_set;
462
463   /* Set SP, FP and PC to 0 and set LR to -1
464      so we can detect a top-level return.  */
465   aarch64_set_reg_u64 (cpu, SP, 1, 0);
466   aarch64_set_reg_u64 (cpu, FP, 1, 0);
467   aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
468   aarch64_set_next_PC (cpu, 0);
469   aarch64_update_PC (cpu);
470
471   /* Default to a 128 Mbyte (== 2^27) memory space.  */
472   sim_do_commandf (sd, "memory-size 0x8000000");
473
474   return sd;
475 }
476
477 void
478 sim_engine_run (SIM_DESC sd,
479                 int next_cpu_nr ATTRIBUTE_UNUSED,
480                 int nr_cpus ATTRIBUTE_UNUSED,
481                 int siggnal ATTRIBUTE_UNUSED)
482 {
483   aarch64_run (sd);
484 }