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