aarch64 multi-arch (part 3): get thread area
[external/binutils.git] / gdb / gdbserver / linux-aarch64-low.c
1 /* GNU/Linux/AArch64 specific low level interface, for the remote server for
2    GDB.
3
4    Copyright (C) 2009-2015 Free Software Foundation, Inc.
5    Contributed by ARM Ltd.
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 "server.h"
23 #include "linux-low.h"
24 #include "nat/aarch64-linux.h"
25 #include "nat/aarch64-linux-hw-point.h"
26 #include "linux-aarch32-low.h"
27 #include "elf/common.h"
28
29 #include <signal.h>
30 #include <sys/user.h>
31 #include "nat/gdb_ptrace.h"
32 #include <asm/ptrace.h>
33
34 #include "gdb_proc_service.h"
35
36 /* Defined in auto-generated files.  */
37 void init_registers_aarch64 (void);
38 extern const struct target_desc *tdesc_aarch64;
39
40 #ifdef HAVE_SYS_REG_H
41 #include <sys/reg.h>
42 #endif
43
44 #define AARCH64_X_REGS_NUM 31
45 #define AARCH64_V_REGS_NUM 32
46 #define AARCH64_X0_REGNO    0
47 #define AARCH64_SP_REGNO   31
48 #define AARCH64_PC_REGNO   32
49 #define AARCH64_CPSR_REGNO 33
50 #define AARCH64_V0_REGNO   34
51 #define AARCH64_FPSR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM)
52 #define AARCH64_FPCR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 1)
53
54 #define AARCH64_NUM_REGS (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 2)
55
56 /* Per-process arch-specific data we want to keep.  */
57
58 struct arch_process_info
59 {
60   /* Hardware breakpoint/watchpoint data.
61      The reason for them to be per-process rather than per-thread is
62      due to the lack of information in the gdbserver environment;
63      gdbserver is not told that whether a requested hardware
64      breakpoint/watchpoint is thread specific or not, so it has to set
65      each hw bp/wp for every thread in the current process.  The
66      higher level bp/wp management in gdb will resume a thread if a hw
67      bp/wp trap is not expected for it.  Since the hw bp/wp setting is
68      same for each thread, it is reasonable for the data to live here.
69      */
70   struct aarch64_debug_reg_state debug_reg_state;
71 };
72
73 /* Return true if the size of register 0 is 8 byte.  */
74
75 static int
76 is_64bit_tdesc (void)
77 {
78   struct regcache *regcache = get_thread_regcache (current_thread, 0);
79
80   return register_size (regcache->tdesc, 0) == 8;
81 }
82
83 /* Implementation of linux_target_ops method "cannot_store_register".  */
84
85 static int
86 aarch64_cannot_store_register (int regno)
87 {
88   return regno >= AARCH64_NUM_REGS;
89 }
90
91 /* Implementation of linux_target_ops method "cannot_fetch_register".  */
92
93 static int
94 aarch64_cannot_fetch_register (int regno)
95 {
96   return regno >= AARCH64_NUM_REGS;
97 }
98
99 static void
100 aarch64_fill_gregset (struct regcache *regcache, void *buf)
101 {
102   struct user_pt_regs *regset = buf;
103   int i;
104
105   for (i = 0; i < AARCH64_X_REGS_NUM; i++)
106     collect_register (regcache, AARCH64_X0_REGNO + i, &regset->regs[i]);
107   collect_register (regcache, AARCH64_SP_REGNO, &regset->sp);
108   collect_register (regcache, AARCH64_PC_REGNO, &regset->pc);
109   collect_register (regcache, AARCH64_CPSR_REGNO, &regset->pstate);
110 }
111
112 static void
113 aarch64_store_gregset (struct regcache *regcache, const void *buf)
114 {
115   const struct user_pt_regs *regset = buf;
116   int i;
117
118   for (i = 0; i < AARCH64_X_REGS_NUM; i++)
119     supply_register (regcache, AARCH64_X0_REGNO + i, &regset->regs[i]);
120   supply_register (regcache, AARCH64_SP_REGNO, &regset->sp);
121   supply_register (regcache, AARCH64_PC_REGNO, &regset->pc);
122   supply_register (regcache, AARCH64_CPSR_REGNO, &regset->pstate);
123 }
124
125 static void
126 aarch64_fill_fpregset (struct regcache *regcache, void *buf)
127 {
128   struct user_fpsimd_state *regset = buf;
129   int i;
130
131   for (i = 0; i < AARCH64_V_REGS_NUM; i++)
132     collect_register (regcache, AARCH64_V0_REGNO + i, &regset->vregs[i]);
133   collect_register (regcache, AARCH64_FPSR_REGNO, &regset->fpsr);
134   collect_register (regcache, AARCH64_FPCR_REGNO, &regset->fpcr);
135 }
136
137 static void
138 aarch64_store_fpregset (struct regcache *regcache, const void *buf)
139 {
140   const struct user_fpsimd_state *regset = buf;
141   int i;
142
143   for (i = 0; i < AARCH64_V_REGS_NUM; i++)
144     supply_register (regcache, AARCH64_V0_REGNO + i, &regset->vregs[i]);
145   supply_register (regcache, AARCH64_FPSR_REGNO, &regset->fpsr);
146   supply_register (regcache, AARCH64_FPCR_REGNO, &regset->fpcr);
147 }
148
149 /* Enable miscellaneous debugging output.  The name is historical - it
150    was originally used to debug LinuxThreads support.  */
151 extern int debug_threads;
152
153 /* Implementation of linux_target_ops method "get_pc".  */
154
155 static CORE_ADDR
156 aarch64_get_pc (struct regcache *regcache)
157 {
158   if (register_size (regcache->tdesc, 0) == 8)
159     {
160       unsigned long pc;
161
162       collect_register_by_name (regcache, "pc", &pc);
163       if (debug_threads)
164         debug_printf ("stop pc is %08lx\n", pc);
165       return pc;
166     }
167   else
168     {
169       unsigned int pc;
170
171       collect_register_by_name (regcache, "pc", &pc);
172       if (debug_threads)
173         debug_printf ("stop pc is %04x\n", pc);
174       return pc;
175     }
176 }
177
178 /* Implementation of linux_target_ops method "set_pc".  */
179
180 static void
181 aarch64_set_pc (struct regcache *regcache, CORE_ADDR pc)
182 {
183   if (register_size (regcache->tdesc, 0) == 8)
184     {
185       unsigned long newpc = pc;
186       supply_register_by_name (regcache, "pc", &newpc);
187     }
188   else
189     {
190       unsigned int newpc = pc;
191       supply_register_by_name (regcache, "pc", &newpc);
192     }
193 }
194
195 #define aarch64_breakpoint_len 4
196
197 /* AArch64 BRK software debug mode instruction.
198    This instruction needs to match gdb/aarch64-tdep.c
199    (aarch64_default_breakpoint).  */
200 static const gdb_byte aarch64_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
201
202 /* Implementation of linux_target_ops method "breakpoint_at".  */
203
204 static int
205 aarch64_breakpoint_at (CORE_ADDR where)
206 {
207   gdb_byte insn[aarch64_breakpoint_len];
208
209   (*the_target->read_memory) (where, (unsigned char *) &insn,
210                               aarch64_breakpoint_len);
211   if (memcmp (insn, aarch64_breakpoint, aarch64_breakpoint_len) == 0)
212     return 1;
213
214   return 0;
215 }
216
217 static void
218 aarch64_init_debug_reg_state (struct aarch64_debug_reg_state *state)
219 {
220   int i;
221
222   for (i = 0; i < AARCH64_HBP_MAX_NUM; ++i)
223     {
224       state->dr_addr_bp[i] = 0;
225       state->dr_ctrl_bp[i] = 0;
226       state->dr_ref_count_bp[i] = 0;
227     }
228
229   for (i = 0; i < AARCH64_HWP_MAX_NUM; ++i)
230     {
231       state->dr_addr_wp[i] = 0;
232       state->dr_ctrl_wp[i] = 0;
233       state->dr_ref_count_wp[i] = 0;
234     }
235 }
236
237 /* Return the pointer to the debug register state structure in the
238    current process' arch-specific data area.  */
239
240 struct aarch64_debug_reg_state *
241 aarch64_get_debug_reg_state (pid_t pid)
242 {
243   struct process_info *proc = find_process_pid (pid);
244
245   return &proc->priv->arch_private->debug_reg_state;
246 }
247
248 /* Implementation of linux_target_ops method "supports_z_point_type".  */
249
250 static int
251 aarch64_supports_z_point_type (char z_type)
252 {
253   switch (z_type)
254     {
255     case Z_PACKET_SW_BP:
256       {
257         if (!extended_protocol && is_64bit_tdesc ())
258           {
259             /* Only enable Z0 packet in non-multi-arch debugging.  If
260                extended protocol is used, don't enable Z0 packet because
261                GDBserver may attach to 32-bit process.  */
262             return 1;
263           }
264         else
265           {
266             /* Disable Z0 packet so that GDBserver doesn't have to handle
267                different breakpoint instructions (aarch64, arm, thumb etc)
268                in multi-arch debugging.  */
269             return 0;
270           }
271       }
272     case Z_PACKET_HW_BP:
273     case Z_PACKET_WRITE_WP:
274     case Z_PACKET_READ_WP:
275     case Z_PACKET_ACCESS_WP:
276       return 1;
277     default:
278       return 0;
279     }
280 }
281
282 /* Implementation of linux_target_ops method "insert_point".
283
284    It actually only records the info of the to-be-inserted bp/wp;
285    the actual insertion will happen when threads are resumed.  */
286
287 static int
288 aarch64_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
289                       int len, struct raw_breakpoint *bp)
290 {
291   int ret;
292   enum target_hw_bp_type targ_type;
293   struct aarch64_debug_reg_state *state
294     = aarch64_get_debug_reg_state (pid_of (current_thread));
295
296   if (show_debug_regs)
297     fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n",
298              (unsigned long) addr, len);
299
300   /* Determine the type from the raw breakpoint type.  */
301   targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
302
303   if (targ_type != hw_execute)
304     {
305       if (aarch64_linux_region_ok_for_watchpoint (addr, len))
306         ret = aarch64_handle_watchpoint (targ_type, addr, len,
307                                          1 /* is_insert */, state);
308       else
309         ret = -1;
310     }
311   else
312     ret =
313       aarch64_handle_breakpoint (targ_type, addr, len, 1 /* is_insert */,
314                                  state);
315
316   if (show_debug_regs)
317     aarch64_show_debug_reg_state (state, "insert_point", addr, len,
318                                   targ_type);
319
320   return ret;
321 }
322
323 /* Implementation of linux_target_ops method "remove_point".
324
325    It actually only records the info of the to-be-removed bp/wp,
326    the actual removal will be done when threads are resumed.  */
327
328 static int
329 aarch64_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
330                       int len, struct raw_breakpoint *bp)
331 {
332   int ret;
333   enum target_hw_bp_type targ_type;
334   struct aarch64_debug_reg_state *state
335     = aarch64_get_debug_reg_state (pid_of (current_thread));
336
337   if (show_debug_regs)
338     fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n",
339              (unsigned long) addr, len);
340
341   /* Determine the type from the raw breakpoint type.  */
342   targ_type = raw_bkpt_type_to_target_hw_bp_type (type);
343
344   /* Set up state pointers.  */
345   if (targ_type != hw_execute)
346     ret =
347       aarch64_handle_watchpoint (targ_type, addr, len, 0 /* is_insert */,
348                                  state);
349   else
350     ret =
351       aarch64_handle_breakpoint (targ_type, addr, len, 0 /* is_insert */,
352                                  state);
353
354   if (show_debug_regs)
355     aarch64_show_debug_reg_state (state, "remove_point", addr, len,
356                                   targ_type);
357
358   return ret;
359 }
360
361 /* Implementation of linux_target_ops method "stopped_data_address".  */
362
363 static CORE_ADDR
364 aarch64_stopped_data_address (void)
365 {
366   siginfo_t siginfo;
367   int pid, i;
368   struct aarch64_debug_reg_state *state;
369
370   pid = lwpid_of (current_thread);
371
372   /* Get the siginfo.  */
373   if (ptrace (PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0)
374     return (CORE_ADDR) 0;
375
376   /* Need to be a hardware breakpoint/watchpoint trap.  */
377   if (siginfo.si_signo != SIGTRAP
378       || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
379     return (CORE_ADDR) 0;
380
381   /* Check if the address matches any watched address.  */
382   state = aarch64_get_debug_reg_state (pid_of (current_thread));
383   for (i = aarch64_num_wp_regs - 1; i >= 0; --i)
384     {
385       const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]);
386       const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
387       const CORE_ADDR addr_watch = state->dr_addr_wp[i];
388       if (state->dr_ref_count_wp[i]
389           && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i])
390           && addr_trap >= addr_watch
391           && addr_trap < addr_watch + len)
392         return addr_trap;
393     }
394
395   return (CORE_ADDR) 0;
396 }
397
398 /* Implementation of linux_target_ops method "stopped_by_watchpoint".  */
399
400 static int
401 aarch64_stopped_by_watchpoint (void)
402 {
403   if (aarch64_stopped_data_address () != 0)
404     return 1;
405   else
406     return 0;
407 }
408
409 /* Fetch the thread-local storage pointer for libthread_db.  */
410
411 ps_err_e
412 ps_get_thread_area (const struct ps_prochandle *ph,
413                     lwpid_t lwpid, int idx, void **base)
414 {
415   return aarch64_ps_get_thread_area (ph, lwpid, idx, base,
416                                      is_64bit_tdesc ());
417 }
418
419 /* Implementation of linux_target_ops method "siginfo_fixup".  */
420
421 static int
422 aarch64_linux_siginfo_fixup (siginfo_t *native, void *inf, int direction)
423 {
424   /* Is the inferior 32-bit?  If so, then fixup the siginfo object.  */
425   if (!is_64bit_tdesc ())
426     {
427       if (direction == 0)
428         aarch64_compat_siginfo_from_siginfo ((struct compat_siginfo *) inf,
429                                              native);
430       else
431         aarch64_siginfo_from_compat_siginfo (native,
432                                              (struct compat_siginfo *) inf);
433
434       return 1;
435     }
436
437   return 0;
438 }
439
440 /* Implementation of linux_target_ops method "linux_new_process".  */
441
442 static struct arch_process_info *
443 aarch64_linux_new_process (void)
444 {
445   struct arch_process_info *info = XCNEW (struct arch_process_info);
446
447   aarch64_init_debug_reg_state (&info->debug_reg_state);
448
449   return info;
450 }
451
452 /* Implementation of linux_target_ops method "linux_new_fork".  */
453
454 static void
455 aarch64_linux_new_fork (struct process_info *parent,
456                         struct process_info *child)
457 {
458   /* These are allocated by linux_add_process.  */
459   gdb_assert (parent->priv != NULL
460               && parent->priv->arch_private != NULL);
461   gdb_assert (child->priv != NULL
462               && child->priv->arch_private != NULL);
463
464   /* Linux kernel before 2.6.33 commit
465      72f674d203cd230426437cdcf7dd6f681dad8b0d
466      will inherit hardware debug registers from parent
467      on fork/vfork/clone.  Newer Linux kernels create such tasks with
468      zeroed debug registers.
469
470      GDB core assumes the child inherits the watchpoints/hw
471      breakpoints of the parent, and will remove them all from the
472      forked off process.  Copy the debug registers mirrors into the
473      new process so that all breakpoints and watchpoints can be
474      removed together.  The debug registers mirror will become zeroed
475      in the end before detaching the forked off process, thus making
476      this compatible with older Linux kernels too.  */
477
478   *child->priv->arch_private = *parent->priv->arch_private;
479 }
480
481 /* Return the right target description according to the ELF file of
482    current thread.  */
483
484 static const struct target_desc *
485 aarch64_linux_read_description (void)
486 {
487   unsigned int machine;
488   int is_elf64;
489   int tid;
490
491   tid = lwpid_of (current_thread);
492
493   is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine);
494
495   if (is_elf64)
496     return tdesc_aarch64;
497   else
498     return tdesc_arm_with_neon;
499 }
500
501 /* Implementation of linux_target_ops method "arch_setup".  */
502
503 static void
504 aarch64_arch_setup (void)
505 {
506   current_process ()->tdesc = aarch64_linux_read_description ();
507
508   aarch64_linux_get_debug_reg_capacity (lwpid_of (current_thread));
509 }
510
511 static struct regset_info aarch64_regsets[] =
512 {
513   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
514     sizeof (struct user_pt_regs), GENERAL_REGS,
515     aarch64_fill_gregset, aarch64_store_gregset },
516   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET,
517     sizeof (struct user_fpsimd_state), FP_REGS,
518     aarch64_fill_fpregset, aarch64_store_fpregset
519   },
520   { 0, 0, 0, -1, -1, NULL, NULL }
521 };
522
523 static struct regsets_info aarch64_regsets_info =
524   {
525     aarch64_regsets, /* regsets */
526     0, /* num_regsets */
527     NULL, /* disabled_regsets */
528   };
529
530 static struct regs_info regs_info_aarch64 =
531   {
532     NULL, /* regset_bitmap */
533     NULL, /* usrregs */
534     &aarch64_regsets_info,
535   };
536
537 /* Implementation of linux_target_ops method "regs_info".  */
538
539 static const struct regs_info *
540 aarch64_regs_info (void)
541 {
542   if (is_64bit_tdesc ())
543     return &regs_info_aarch64;
544   else
545     return &regs_info_aarch32;
546 }
547
548 /* Implementation of linux_target_ops method "supports_tracepoints".  */
549
550 static int
551 aarch64_supports_tracepoints (void)
552 {
553   if (current_thread == NULL)
554     return 1;
555   else
556     {
557       /* We don't support tracepoints on aarch32 now.  */
558       return is_64bit_tdesc ();
559     }
560 }
561
562 /* Implementation of linux_target_ops method "supports_range_stepping".  */
563
564 static int
565 aarch64_supports_range_stepping (void)
566 {
567   return 1;
568 }
569
570 struct linux_target_ops the_low_target =
571 {
572   aarch64_arch_setup,
573   aarch64_regs_info,
574   aarch64_cannot_fetch_register,
575   aarch64_cannot_store_register,
576   NULL, /* fetch_register */
577   aarch64_get_pc,
578   aarch64_set_pc,
579   (const unsigned char *) &aarch64_breakpoint,
580   aarch64_breakpoint_len,
581   NULL, /* breakpoint_reinsert_addr */
582   0,    /* decr_pc_after_break */
583   aarch64_breakpoint_at,
584   aarch64_supports_z_point_type,
585   aarch64_insert_point,
586   aarch64_remove_point,
587   aarch64_stopped_by_watchpoint,
588   aarch64_stopped_data_address,
589   NULL, /* collect_ptrace_register */
590   NULL, /* supply_ptrace_register */
591   aarch64_linux_siginfo_fixup,
592   aarch64_linux_new_process,
593   aarch64_linux_new_thread,
594   aarch64_linux_new_fork,
595   aarch64_linux_prepare_to_resume,
596   NULL, /* process_qsupported */
597   aarch64_supports_tracepoints,
598   NULL, /* get_thread_area */
599   NULL, /* install_fast_tracepoint_jump_pad */
600   NULL, /* emit_ops */
601   NULL, /* get_min_fast_tracepoint_insn_len */
602   aarch64_supports_range_stepping,
603 };
604
605 void
606 initialize_low_arch (void)
607 {
608   init_registers_aarch64 ();
609
610   initialize_low_arch_aarch32 ();
611
612   initialize_regsets_info (&aarch64_regsets_info);
613 }