1 /* Copyright (C) 1995-2016 Free Software Foundation, Inc.
3 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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch/arm-linux.h"
21 #include "linux-low.h"
22 #include "linux-aarch32-low.h"
24 #include <sys/ptrace.h>
25 /* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
26 On Bionic elf.h and linux/elf.h have conflicting definitions. */
31 /* Correct in either endianness. */
32 #define arm_abi_breakpoint 0xef9f0001UL
34 /* For new EABI binaries. We recognize it regardless of which ABI
35 is used for gdbserver, so single threaded debugging should work
36 OK, but for multi-threaded debugging we only insert the current
37 ABI's breakpoint instruction. For now at least. */
38 #define arm_eabi_breakpoint 0xe7f001f0UL
40 #if (defined __ARM_EABI__ || defined __aarch64__)
41 static const unsigned long arm_breakpoint
= arm_eabi_breakpoint
;
43 static const unsigned long arm_breakpoint
= arm_abi_breakpoint
;
46 #define arm_breakpoint_len 4
47 static const unsigned short thumb_breakpoint
= 0xde01;
48 #define thumb_breakpoint_len 2
49 static const unsigned short thumb2_breakpoint
[] = { 0xf7f0, 0xa000 };
50 #define thumb2_breakpoint_len 4
52 /* Some older versions of GNU/Linux and Android do not define
53 the following macros. */
55 #define NT_ARM_VFP 0x400
58 /* Collect GP registers from REGCACHE to buffer BUF. */
61 arm_fill_gregset (struct regcache
*regcache
, void *buf
)
64 uint32_t *regs
= (uint32_t *) buf
;
66 for (i
= ARM_A1_REGNUM
; i
<= ARM_PC_REGNUM
; i
++)
67 collect_register (regcache
, i
, ®s
[i
]);
69 collect_register (regcache
, ARM_PS_REGNUM
, ®s
[ARM_CPSR_GREGNUM
]);
72 /* Supply GP registers contents, stored in BUF, to REGCACHE. */
75 arm_store_gregset (struct regcache
*regcache
, const void *buf
)
79 const uint32_t *regs
= (const uint32_t *) buf
;
81 memset (zerobuf
, 0, 8);
82 for (i
= ARM_A1_REGNUM
; i
<= ARM_PC_REGNUM
; i
++)
83 supply_register (regcache
, i
, ®s
[i
]);
85 for (; i
< ARM_PS_REGNUM
; i
++)
86 supply_register (regcache
, i
, zerobuf
);
88 supply_register (regcache
, ARM_PS_REGNUM
, ®s
[ARM_CPSR_GREGNUM
]);
91 /* Collect NUM number of VFP registers from REGCACHE to buffer BUF. */
94 arm_fill_vfpregset_num (struct regcache
*regcache
, void *buf
, int num
)
98 gdb_assert (num
== 16 || num
== 32);
100 base
= find_regno (regcache
->tdesc
, "d0");
101 for (i
= 0; i
< num
; i
++)
102 collect_register (regcache
, base
+ i
, (char *) buf
+ i
* 8);
104 collect_register_by_name (regcache
, "fpscr", (char *) buf
+ 32 * 8);
107 /* Supply NUM number of VFP registers contents, stored in BUF, to
111 arm_store_vfpregset_num (struct regcache
*regcache
, const void *buf
, int num
)
115 gdb_assert (num
== 16 || num
== 32);
117 base
= find_regno (regcache
->tdesc
, "d0");
118 for (i
= 0; i
< num
; i
++)
119 supply_register (regcache
, base
+ i
, (char *) buf
+ i
* 8);
121 supply_register_by_name (regcache
, "fpscr", (char *) buf
+ 32 * 8);
125 arm_fill_vfpregset (struct regcache
*regcache
, void *buf
)
127 arm_fill_vfpregset_num (regcache
, buf
, 32);
131 arm_store_vfpregset (struct regcache
*regcache
, const void *buf
)
133 arm_store_vfpregset_num (regcache
, buf
, 32);
136 /* Register sets with using PTRACE_GETREGSET. */
138 static struct regset_info aarch32_regsets
[] = {
139 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
, 18 * 4,
141 arm_fill_gregset
, arm_store_gregset
},
142 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_ARM_VFP
, 32 * 8 + 4,
144 arm_fill_vfpregset
, arm_store_vfpregset
},
148 static struct regsets_info aarch32_regsets_info
=
150 aarch32_regsets
, /* regsets */
152 NULL
, /* disabled_regsets */
155 struct regs_info regs_info_aarch32
=
157 NULL
, /* regset_bitmap */
159 &aarch32_regsets_info
162 /* Returns 1 if the current instruction set is thumb, 0 otherwise. */
165 arm_is_thumb_mode (void)
167 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
170 collect_register_by_name (regcache
, "cpsr", &cpsr
);
178 /* Returns 1 if there is a software breakpoint at location. */
181 arm_breakpoint_at (CORE_ADDR where
)
183 if (arm_is_thumb_mode ())
188 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
, 2);
189 if (insn
== thumb_breakpoint
)
192 if (insn
== thumb2_breakpoint
[0])
194 (*the_target
->read_memory
) (where
+ 2, (unsigned char *) &insn
, 2);
195 if (insn
== thumb2_breakpoint
[1])
204 (*the_target
->read_memory
) (where
, (unsigned char *) &insn
, 4);
205 if (insn
== arm_abi_breakpoint
)
208 if (insn
== arm_eabi_breakpoint
)
215 /* Enum describing the different kinds of breakpoints. */
216 enum arm_breakpoint_kinds
218 ARM_BP_KIND_THUMB
= 2,
219 ARM_BP_KIND_THUMB2
= 3,
223 /* Implementation of linux_target_ops method "breakpoint_kind_from_pc".
225 Determine the type and size of breakpoint to insert at PCPTR. Uses the
226 program counter value to determine whether a 16-bit or 32-bit breakpoint
227 should be used. It returns the breakpoint's kind, and adjusts the program
228 counter (if necessary) to point to the actual memory location where the
229 breakpoint should be inserted. */
232 arm_breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
234 if (IS_THUMB_ADDR (*pcptr
))
238 *pcptr
= UNMAKE_THUMB_ADDR (*pcptr
);
240 /* Check whether we are replacing a thumb2 32-bit instruction. */
241 if ((*the_target
->read_memory
) (*pcptr
, buf
, 2) == 0)
243 unsigned short inst1
= 0;
245 (*the_target
->read_memory
) (*pcptr
, (gdb_byte
*) &inst1
, 2);
246 if (thumb_insn_size (inst1
) == 4)
247 return ARM_BP_KIND_THUMB2
;
249 return ARM_BP_KIND_THUMB
;
252 return ARM_BP_KIND_ARM
;
255 /* Implementation of the linux_target_ops method "sw_breakpoint_from_kind". */
258 arm_sw_breakpoint_from_kind (int kind
, int *size
)
260 *size
= arm_breakpoint_len
;
261 /* Define an ARM-mode breakpoint; we only set breakpoints in the C
262 library, which is most likely to be ARM. If the kernel supports
263 clone events, we will never insert a breakpoint, so even a Thumb
264 C library will work; so will mixing EABI/non-EABI gdbserver and
268 case ARM_BP_KIND_THUMB
:
269 *size
= thumb_breakpoint_len
;
270 return (gdb_byte
*) &thumb_breakpoint
;
271 case ARM_BP_KIND_THUMB2
:
272 *size
= thumb2_breakpoint_len
;
273 return (gdb_byte
*) &thumb2_breakpoint
;
274 case ARM_BP_KIND_ARM
:
275 *size
= arm_breakpoint_len
;
276 return (const gdb_byte
*) &arm_breakpoint
;
283 /* Implementation of the linux_target_ops method
284 "breakpoint_kind_from_current_state". */
287 arm_breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
289 if (arm_is_thumb_mode ())
291 *pcptr
= MAKE_THUMB_ADDR (*pcptr
);
292 return arm_breakpoint_kind_from_pc (pcptr
);
296 return arm_breakpoint_kind_from_pc (pcptr
);
301 initialize_low_arch_aarch32 (void)
303 init_registers_arm_with_neon ();
305 initialize_regsets_info (&aarch32_regsets_info
);