1 /* GNU/Linux/AArch64 specific low level interface, for the remote server for
4 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 Contributed by ARM Ltd.
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/>. */
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"
31 #include "nat/gdb_ptrace.h"
32 #include <asm/ptrace.h>
35 #include "gdb_proc_service.h"
37 /* Defined in auto-generated files. */
38 void init_registers_aarch64 (void);
39 extern const struct target_desc
*tdesc_aarch64
;
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
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)
55 #define AARCH64_NUM_REGS (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 2)
57 /* Per-process arch-specific data we want to keep. */
59 struct arch_process_info
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.
71 struct aarch64_debug_reg_state debug_reg_state
;
74 /* Return true if the size of register 0 is 8 byte. */
79 struct regcache
*regcache
= get_thread_regcache (current_thread
, 0);
81 return register_size (regcache
->tdesc
, 0) == 8;
84 /* Implementation of linux_target_ops method "cannot_store_register". */
87 aarch64_cannot_store_register (int regno
)
89 return regno
>= AARCH64_NUM_REGS
;
92 /* Implementation of linux_target_ops method "cannot_fetch_register". */
95 aarch64_cannot_fetch_register (int regno
)
97 return regno
>= AARCH64_NUM_REGS
;
101 aarch64_fill_gregset (struct regcache
*regcache
, void *buf
)
103 struct user_pt_regs
*regset
= buf
;
106 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
107 collect_register (regcache
, AARCH64_X0_REGNO
+ i
, ®set
->regs
[i
]);
108 collect_register (regcache
, AARCH64_SP_REGNO
, ®set
->sp
);
109 collect_register (regcache
, AARCH64_PC_REGNO
, ®set
->pc
);
110 collect_register (regcache
, AARCH64_CPSR_REGNO
, ®set
->pstate
);
114 aarch64_store_gregset (struct regcache
*regcache
, const void *buf
)
116 const struct user_pt_regs
*regset
= buf
;
119 for (i
= 0; i
< AARCH64_X_REGS_NUM
; i
++)
120 supply_register (regcache
, AARCH64_X0_REGNO
+ i
, ®set
->regs
[i
]);
121 supply_register (regcache
, AARCH64_SP_REGNO
, ®set
->sp
);
122 supply_register (regcache
, AARCH64_PC_REGNO
, ®set
->pc
);
123 supply_register (regcache
, AARCH64_CPSR_REGNO
, ®set
->pstate
);
127 aarch64_fill_fpregset (struct regcache
*regcache
, void *buf
)
129 struct user_fpsimd_state
*regset
= buf
;
132 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
133 collect_register (regcache
, AARCH64_V0_REGNO
+ i
, ®set
->vregs
[i
]);
134 collect_register (regcache
, AARCH64_FPSR_REGNO
, ®set
->fpsr
);
135 collect_register (regcache
, AARCH64_FPCR_REGNO
, ®set
->fpcr
);
139 aarch64_store_fpregset (struct regcache
*regcache
, const void *buf
)
141 const struct user_fpsimd_state
*regset
= buf
;
144 for (i
= 0; i
< AARCH64_V_REGS_NUM
; i
++)
145 supply_register (regcache
, AARCH64_V0_REGNO
+ i
, ®set
->vregs
[i
]);
146 supply_register (regcache
, AARCH64_FPSR_REGNO
, ®set
->fpsr
);
147 supply_register (regcache
, AARCH64_FPCR_REGNO
, ®set
->fpcr
);
150 /* Enable miscellaneous debugging output. The name is historical - it
151 was originally used to debug LinuxThreads support. */
152 extern int debug_threads
;
154 /* Implementation of linux_target_ops method "get_pc". */
157 aarch64_get_pc (struct regcache
*regcache
)
159 if (register_size (regcache
->tdesc
, 0) == 8)
163 collect_register_by_name (regcache
, "pc", &pc
);
165 debug_printf ("stop pc is %08lx\n", pc
);
172 collect_register_by_name (regcache
, "pc", &pc
);
174 debug_printf ("stop pc is %04x\n", pc
);
179 /* Implementation of linux_target_ops method "set_pc". */
182 aarch64_set_pc (struct regcache
*regcache
, CORE_ADDR pc
)
184 if (register_size (regcache
->tdesc
, 0) == 8)
186 unsigned long newpc
= pc
;
187 supply_register_by_name (regcache
, "pc", &newpc
);
191 unsigned int newpc
= pc
;
192 supply_register_by_name (regcache
, "pc", &newpc
);
196 #define aarch64_breakpoint_len 4
198 /* AArch64 BRK software debug mode instruction.
199 This instruction needs to match gdb/aarch64-tdep.c
200 (aarch64_default_breakpoint). */
201 static const gdb_byte aarch64_breakpoint
[] = {0x00, 0x00, 0x20, 0xd4};
203 /* Implementation of linux_target_ops method "breakpoint_at". */
206 aarch64_breakpoint_at (CORE_ADDR where
)
208 gdb_byte insn
[aarch64_breakpoint_len
];
210 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
,
211 aarch64_breakpoint_len
);
212 if (memcmp (insn
, aarch64_breakpoint
, aarch64_breakpoint_len
) == 0)
219 aarch64_init_debug_reg_state (struct aarch64_debug_reg_state
*state
)
223 for (i
= 0; i
< AARCH64_HBP_MAX_NUM
; ++i
)
225 state
->dr_addr_bp
[i
] = 0;
226 state
->dr_ctrl_bp
[i
] = 0;
227 state
->dr_ref_count_bp
[i
] = 0;
230 for (i
= 0; i
< AARCH64_HWP_MAX_NUM
; ++i
)
232 state
->dr_addr_wp
[i
] = 0;
233 state
->dr_ctrl_wp
[i
] = 0;
234 state
->dr_ref_count_wp
[i
] = 0;
238 /* Return the pointer to the debug register state structure in the
239 current process' arch-specific data area. */
241 struct aarch64_debug_reg_state
*
242 aarch64_get_debug_reg_state (pid_t pid
)
244 struct process_info
*proc
= find_process_pid (pid
);
246 return &proc
->priv
->arch_private
->debug_reg_state
;
249 /* Implementation of linux_target_ops method "supports_z_point_type". */
252 aarch64_supports_z_point_type (char z_type
)
258 if (!extended_protocol
&& is_64bit_tdesc ())
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. */
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. */
274 case Z_PACKET_WRITE_WP
:
275 case Z_PACKET_READ_WP
:
276 case Z_PACKET_ACCESS_WP
:
283 /* Implementation of linux_target_ops method "insert_point".
285 It actually only records the info of the to-be-inserted bp/wp;
286 the actual insertion will happen when threads are resumed. */
289 aarch64_insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
290 int len
, struct raw_breakpoint
*bp
)
293 enum target_hw_bp_type targ_type
;
294 struct aarch64_debug_reg_state
*state
295 = aarch64_get_debug_reg_state (pid_of (current_thread
));
298 fprintf (stderr
, "insert_point on entry (addr=0x%08lx, len=%d)\n",
299 (unsigned long) addr
, len
);
301 /* Determine the type from the raw breakpoint type. */
302 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
304 if (targ_type
!= hw_execute
)
306 aarch64_handle_watchpoint (targ_type
, addr
, len
, 1 /* is_insert */,
310 aarch64_handle_breakpoint (targ_type
, addr
, len
, 1 /* is_insert */,
314 aarch64_show_debug_reg_state (state
, "insert_point", addr
, len
,
320 /* Implementation of linux_target_ops method "remove_point".
322 It actually only records the info of the to-be-removed bp/wp,
323 the actual removal will be done when threads are resumed. */
326 aarch64_remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
327 int len
, struct raw_breakpoint
*bp
)
330 enum target_hw_bp_type targ_type
;
331 struct aarch64_debug_reg_state
*state
332 = aarch64_get_debug_reg_state (pid_of (current_thread
));
335 fprintf (stderr
, "remove_point on entry (addr=0x%08lx, len=%d)\n",
336 (unsigned long) addr
, len
);
338 /* Determine the type from the raw breakpoint type. */
339 targ_type
= raw_bkpt_type_to_target_hw_bp_type (type
);
341 /* Set up state pointers. */
342 if (targ_type
!= hw_execute
)
344 aarch64_handle_watchpoint (targ_type
, addr
, len
, 0 /* is_insert */,
348 aarch64_handle_breakpoint (targ_type
, addr
, len
, 0 /* is_insert */,
352 aarch64_show_debug_reg_state (state
, "remove_point", addr
, len
,
358 /* Implementation of linux_target_ops method "stopped_data_address". */
361 aarch64_stopped_data_address (void)
365 struct aarch64_debug_reg_state
*state
;
367 pid
= lwpid_of (current_thread
);
369 /* Get the siginfo. */
370 if (ptrace (PTRACE_GETSIGINFO
, pid
, NULL
, &siginfo
) != 0)
371 return (CORE_ADDR
) 0;
373 /* Need to be a hardware breakpoint/watchpoint trap. */
374 if (siginfo
.si_signo
!= SIGTRAP
375 || (siginfo
.si_code
& 0xffff) != 0x0004 /* TRAP_HWBKPT */)
376 return (CORE_ADDR
) 0;
378 /* Check if the address matches any watched address. */
379 state
= aarch64_get_debug_reg_state (pid_of (current_thread
));
380 for (i
= aarch64_num_wp_regs
- 1; i
>= 0; --i
)
382 const unsigned int len
= aarch64_watchpoint_length (state
->dr_ctrl_wp
[i
]);
383 const CORE_ADDR addr_trap
= (CORE_ADDR
) siginfo
.si_addr
;
384 const CORE_ADDR addr_watch
= state
->dr_addr_wp
[i
];
385 if (state
->dr_ref_count_wp
[i
]
386 && DR_CONTROL_ENABLED (state
->dr_ctrl_wp
[i
])
387 && addr_trap
>= addr_watch
388 && addr_trap
< addr_watch
+ len
)
392 return (CORE_ADDR
) 0;
395 /* Implementation of linux_target_ops method "stopped_by_watchpoint". */
398 aarch64_stopped_by_watchpoint (void)
400 if (aarch64_stopped_data_address () != 0)
406 /* Fetch the thread-local storage pointer for libthread_db. */
409 ps_get_thread_area (const struct ps_prochandle
*ph
,
410 lwpid_t lwpid
, int idx
, void **base
)
415 iovec
.iov_base
= ®
;
416 iovec
.iov_len
= sizeof (reg
);
418 if (ptrace (PTRACE_GETREGSET
, lwpid
, NT_ARM_TLS
, &iovec
) != 0)
421 /* IDX is the bias from the thread pointer to the beginning of the
422 thread descriptor. It has to be subtracted due to implementation
423 quirks in libthread_db. */
424 *base
= (void *) (reg
- idx
);
429 /* Implementation of linux_target_ops method "linux_new_process". */
431 static struct arch_process_info
*
432 aarch64_linux_new_process (void)
434 struct arch_process_info
*info
= xcalloc (1, sizeof (*info
));
436 aarch64_init_debug_reg_state (&info
->debug_reg_state
);
441 /* Implementation of linux_target_ops method "linux_new_thread". */
444 aarch64_linux_new_thread (struct lwp_info
*lwp
)
446 struct arch_lwp_info
*info
= xcalloc (1, sizeof (*info
));
448 /* Mark that all the hardware breakpoint/watchpoint register pairs
449 for this thread need to be initialized (with data from
450 aarch_process_info.debug_reg_state). */
451 DR_MARK_ALL_CHANGED (info
->dr_changed_bp
, aarch64_num_bp_regs
);
452 DR_MARK_ALL_CHANGED (info
->dr_changed_wp
, aarch64_num_wp_regs
);
454 lwp
->arch_private
= info
;
457 /* Implementation of linux_target_ops method "linux_new_fork". */
460 aarch64_linux_new_fork (struct process_info
*parent
,
461 struct process_info
*child
)
463 /* These are allocated by linux_add_process. */
464 gdb_assert (parent
->priv
!= NULL
465 && parent
->priv
->arch_private
!= NULL
);
466 gdb_assert (child
->priv
!= NULL
467 && child
->priv
->arch_private
!= NULL
);
469 /* Linux kernel before 2.6.33 commit
470 72f674d203cd230426437cdcf7dd6f681dad8b0d
471 will inherit hardware debug registers from parent
472 on fork/vfork/clone. Newer Linux kernels create such tasks with
473 zeroed debug registers.
475 GDB core assumes the child inherits the watchpoints/hw
476 breakpoints of the parent, and will remove them all from the
477 forked off process. Copy the debug registers mirrors into the
478 new process so that all breakpoints and watchpoints can be
479 removed together. The debug registers mirror will become zeroed
480 in the end before detaching the forked off process, thus making
481 this compatible with older Linux kernels too. */
483 *child
->priv
->arch_private
= *parent
->priv
->arch_private
;
486 /* Return the right target description according to the ELF file of
489 static const struct target_desc
*
490 aarch64_linux_read_description (void)
492 unsigned int machine
;
496 tid
= lwpid_of (current_thread
);
498 is_elf64
= linux_pid_exe_is_elf_64_file (tid
, &machine
);
501 return tdesc_aarch64
;
503 return tdesc_arm_with_neon
;
506 /* Implementation of linux_target_ops method "arch_setup". */
509 aarch64_arch_setup (void)
511 current_process ()->tdesc
= aarch64_linux_read_description ();
513 aarch64_linux_get_debug_reg_capacity (lwpid_of (current_thread
));
516 static struct regset_info aarch64_regsets
[] =
518 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
519 sizeof (struct user_pt_regs
), GENERAL_REGS
,
520 aarch64_fill_gregset
, aarch64_store_gregset
},
521 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_FPREGSET
,
522 sizeof (struct user_fpsimd_state
), FP_REGS
,
523 aarch64_fill_fpregset
, aarch64_store_fpregset
525 { 0, 0, 0, -1, -1, NULL
, NULL
}
528 static struct regsets_info aarch64_regsets_info
=
530 aarch64_regsets
, /* regsets */
532 NULL
, /* disabled_regsets */
535 static struct regs_info regs_info_aarch64
=
537 NULL
, /* regset_bitmap */
539 &aarch64_regsets_info
,
542 /* Implementation of linux_target_ops method "regs_info". */
544 static const struct regs_info
*
545 aarch64_regs_info (void)
547 if (is_64bit_tdesc ())
548 return ®s_info_aarch64
;
550 return ®s_info_aarch32
;
553 /* Implementation of linux_target_ops method "supports_tracepoints". */
556 aarch64_supports_tracepoints (void)
558 if (current_thread
== NULL
)
562 /* We don't support tracepoints on aarch32 now. */
563 return is_64bit_tdesc ();
567 /* Implementation of linux_target_ops method "supports_range_stepping". */
570 aarch64_supports_range_stepping (void)
575 struct linux_target_ops the_low_target
=
579 aarch64_cannot_fetch_register
,
580 aarch64_cannot_store_register
,
581 NULL
, /* fetch_register */
584 (const unsigned char *) &aarch64_breakpoint
,
585 aarch64_breakpoint_len
,
586 NULL
, /* breakpoint_reinsert_addr */
587 0, /* decr_pc_after_break */
588 aarch64_breakpoint_at
,
589 aarch64_supports_z_point_type
,
590 aarch64_insert_point
,
591 aarch64_remove_point
,
592 aarch64_stopped_by_watchpoint
,
593 aarch64_stopped_data_address
,
594 NULL
, /* collect_ptrace_register */
595 NULL
, /* supply_ptrace_register */
596 NULL
, /* siginfo_fixup */
597 aarch64_linux_new_process
,
598 aarch64_linux_new_thread
,
599 aarch64_linux_new_fork
,
600 aarch64_linux_prepare_to_resume
,
601 NULL
, /* process_qsupported */
602 aarch64_supports_tracepoints
,
603 NULL
, /* get_thread_area */
604 NULL
, /* install_fast_tracepoint_jump_pad */
606 NULL
, /* get_min_fast_tracepoint_insn_len */
607 aarch64_supports_range_stepping
,
611 initialize_low_arch (void)
613 init_registers_aarch64 ();
615 initialize_low_arch_aarch32 ();
617 initialize_regsets_info (&aarch64_regsets_info
);