Commit | Line | Data |
---|---|---|
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" | |
24 | #include "elf/common.h" | |
25 | ||
26 | #include <signal.h> | |
27 | #include <sys/user.h> | |
28 | #include <sys/ptrace.h> | |
e9dae05e | 29 | #include <asm/ptrace.h> |
176eb98c MS |
30 | #include <sys/uio.h> |
31 | ||
32 | #include "gdb_proc_service.h" | |
33 | ||
34 | /* Defined in auto-generated files. */ | |
35 | void init_registers_aarch64 (void); | |
3aee8918 | 36 | extern const struct target_desc *tdesc_aarch64; |
176eb98c | 37 | |
176eb98c MS |
38 | #ifdef HAVE_SYS_REG_H |
39 | #include <sys/reg.h> | |
40 | #endif | |
41 | ||
42 | #define AARCH64_X_REGS_NUM 31 | |
43 | #define AARCH64_V_REGS_NUM 32 | |
44 | #define AARCH64_X0_REGNO 0 | |
45 | #define AARCH64_SP_REGNO 31 | |
46 | #define AARCH64_PC_REGNO 32 | |
47 | #define AARCH64_CPSR_REGNO 33 | |
48 | #define AARCH64_V0_REGNO 34 | |
bf330350 CU |
49 | #define AARCH64_FPSR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM) |
50 | #define AARCH64_FPCR_REGNO (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 1) | |
176eb98c | 51 | |
bf330350 | 52 | #define AARCH64_NUM_REGS (AARCH64_V0_REGNO + AARCH64_V_REGS_NUM + 2) |
176eb98c | 53 | |
176eb98c MS |
54 | /* Here starts the macro definitions, data structures, and code for |
55 | the hardware breakpoint and hardware watchpoint support. The | |
56 | following is the abbreviations that are used frequently in the code | |
57 | and comment: | |
58 | ||
59 | hw - hardware | |
60 | bp - breakpoint | |
61 | wp - watchpoint */ | |
62 | ||
63 | /* Maximum number of hardware breakpoint and watchpoint registers. | |
64 | Neither of these values may exceed the width of dr_changed_t | |
65 | measured in bits. */ | |
66 | ||
67 | #define AARCH64_HBP_MAX_NUM 16 | |
68 | #define AARCH64_HWP_MAX_NUM 16 | |
69 | ||
70 | /* Alignment requirement in bytes of hardware breakpoint and | |
71 | watchpoint address. This is the requirement for the addresses that | |
72 | can be written to the hardware breakpoint/watchpoint value | |
73 | registers. The kernel currently does not do any alignment on | |
74 | addresses when receiving a writing request (via ptrace call) to | |
75 | these debug registers, and it will reject any address that is | |
76 | unaligned. | |
77 | Some limited support has been provided in this gdbserver port for | |
78 | unaligned watchpoints, so that from a gdb user point of view, an | |
79 | unaligned watchpoint can still be set. This is achieved by | |
80 | minimally enlarging the watched area to meet the alignment | |
81 | requirement, and if necessary, splitting the watchpoint over | |
82 | several hardware watchpoint registers. */ | |
83 | ||
84 | #define AARCH64_HBP_ALIGNMENT 4 | |
85 | #define AARCH64_HWP_ALIGNMENT 8 | |
86 | ||
87 | /* The maximum length of a memory region that can be watched by one | |
88 | hardware watchpoint register. */ | |
89 | ||
90 | #define AARCH64_HWP_MAX_LEN_PER_REG 8 | |
91 | ||
92 | /* Each bit of a variable of this type is used to indicate whether a | |
93 | hardware breakpoint or watchpoint setting has been changed since | |
94 | the last updating. Bit N corresponds to the Nth hardware | |
95 | breakpoint or watchpoint setting which is managed in | |
96 | aarch64_debug_reg_state. Where N is valid between 0 and the total | |
97 | number of the hardware breakpoint or watchpoint debug registers | |
98 | minus 1. When the bit N is 1, it indicates the corresponding | |
99 | breakpoint or watchpoint setting is changed, and thus the | |
100 | corresponding hardware debug register needs to be updated via the | |
101 | ptrace interface. | |
102 | ||
103 | In the per-thread arch-specific data area, we define two such | |
104 | variables for per-thread hardware breakpoint and watchpoint | |
105 | settings respectively. | |
106 | ||
107 | This type is part of the mechanism which helps reduce the number of | |
108 | ptrace calls to the kernel, i.e. avoid asking the kernel to write | |
109 | to the debug registers with unchanged values. */ | |
110 | ||
111 | typedef unsigned long long dr_changed_t; | |
112 | ||
113 | /* Set each of the lower M bits of X to 1; assert X is wide enough. */ | |
114 | ||
115 | #define DR_MARK_ALL_CHANGED(x, m) \ | |
116 | do \ | |
117 | { \ | |
118 | gdb_assert (sizeof ((x)) * 8 >= (m)); \ | |
119 | (x) = (((dr_changed_t)1 << (m)) - 1); \ | |
120 | } while (0) | |
121 | ||
122 | #define DR_MARK_N_CHANGED(x, n) \ | |
123 | do \ | |
124 | { \ | |
125 | (x) |= ((dr_changed_t)1 << (n)); \ | |
126 | } while (0) | |
127 | ||
128 | #define DR_CLEAR_CHANGED(x) \ | |
129 | do \ | |
130 | { \ | |
131 | (x) = 0; \ | |
132 | } while (0) | |
133 | ||
134 | #define DR_HAS_CHANGED(x) ((x) != 0) | |
135 | #define DR_N_HAS_CHANGED(x, n) ((x) & ((dr_changed_t)1 << (n))) | |
136 | ||
137 | /* Structure for managing the hardware breakpoint/watchpoint resources. | |
138 | DR_ADDR_* stores the address, DR_CTRL_* stores the control register | |
139 | content, and DR_REF_COUNT_* counts the numbers of references to the | |
140 | corresponding bp/wp, by which way the limited hardware resources | |
141 | are not wasted on duplicated bp/wp settings (though so far gdb has | |
142 | done a good job by not sending duplicated bp/wp requests). */ | |
143 | ||
144 | struct aarch64_debug_reg_state | |
145 | { | |
146 | /* hardware breakpoint */ | |
147 | CORE_ADDR dr_addr_bp[AARCH64_HBP_MAX_NUM]; | |
148 | unsigned int dr_ctrl_bp[AARCH64_HBP_MAX_NUM]; | |
149 | unsigned int dr_ref_count_bp[AARCH64_HBP_MAX_NUM]; | |
150 | ||
151 | /* hardware watchpoint */ | |
152 | CORE_ADDR dr_addr_wp[AARCH64_HWP_MAX_NUM]; | |
153 | unsigned int dr_ctrl_wp[AARCH64_HWP_MAX_NUM]; | |
154 | unsigned int dr_ref_count_wp[AARCH64_HWP_MAX_NUM]; | |
155 | }; | |
156 | ||
157 | /* Per-process arch-specific data we want to keep. */ | |
158 | ||
159 | struct arch_process_info | |
160 | { | |
161 | /* Hardware breakpoint/watchpoint data. | |
162 | The reason for them to be per-process rather than per-thread is | |
163 | due to the lack of information in the gdbserver environment; | |
164 | gdbserver is not told that whether a requested hardware | |
165 | breakpoint/watchpoint is thread specific or not, so it has to set | |
166 | each hw bp/wp for every thread in the current process. The | |
167 | higher level bp/wp management in gdb will resume a thread if a hw | |
168 | bp/wp trap is not expected for it. Since the hw bp/wp setting is | |
169 | same for each thread, it is reasonable for the data to live here. | |
170 | */ | |
171 | struct aarch64_debug_reg_state debug_reg_state; | |
172 | }; | |
173 | ||
174 | /* Per-thread arch-specific data we want to keep. */ | |
175 | ||
176 | struct arch_lwp_info | |
177 | { | |
178 | /* When bit N is 1, it indicates the Nth hardware breakpoint or | |
179 | watchpoint register pair needs to be updated when the thread is | |
180 | resumed; see aarch64_linux_prepare_to_resume. */ | |
181 | dr_changed_t dr_changed_bp; | |
182 | dr_changed_t dr_changed_wp; | |
183 | }; | |
184 | ||
185 | /* Number of hardware breakpoints/watchpoints the target supports. | |
186 | They are initialized with values obtained via the ptrace calls | |
187 | with NT_ARM_HW_BREAK and NT_ARM_HW_WATCH respectively. */ | |
188 | ||
189 | static int aarch64_num_bp_regs; | |
190 | static int aarch64_num_wp_regs; | |
191 | ||
176eb98c MS |
192 | static int |
193 | aarch64_cannot_store_register (int regno) | |
194 | { | |
195 | return regno >= AARCH64_NUM_REGS; | |
196 | } | |
197 | ||
198 | static int | |
199 | aarch64_cannot_fetch_register (int regno) | |
200 | { | |
201 | return regno >= AARCH64_NUM_REGS; | |
202 | } | |
203 | ||
204 | static void | |
205 | aarch64_fill_gregset (struct regcache *regcache, void *buf) | |
206 | { | |
207 | struct user_pt_regs *regset = buf; | |
208 | int i; | |
209 | ||
210 | for (i = 0; i < AARCH64_X_REGS_NUM; i++) | |
211 | collect_register (regcache, AARCH64_X0_REGNO + i, ®set->regs[i]); | |
212 | collect_register (regcache, AARCH64_SP_REGNO, ®set->sp); | |
213 | collect_register (regcache, AARCH64_PC_REGNO, ®set->pc); | |
214 | collect_register (regcache, AARCH64_CPSR_REGNO, ®set->pstate); | |
215 | } | |
216 | ||
217 | static void | |
218 | aarch64_store_gregset (struct regcache *regcache, const void *buf) | |
219 | { | |
220 | const struct user_pt_regs *regset = buf; | |
221 | int i; | |
222 | ||
223 | for (i = 0; i < AARCH64_X_REGS_NUM; i++) | |
224 | supply_register (regcache, AARCH64_X0_REGNO + i, ®set->regs[i]); | |
225 | supply_register (regcache, AARCH64_SP_REGNO, ®set->sp); | |
226 | supply_register (regcache, AARCH64_PC_REGNO, ®set->pc); | |
227 | supply_register (regcache, AARCH64_CPSR_REGNO, ®set->pstate); | |
228 | } | |
229 | ||
230 | static void | |
231 | aarch64_fill_fpregset (struct regcache *regcache, void *buf) | |
232 | { | |
233 | struct user_fpsimd_state *regset = buf; | |
234 | int i; | |
235 | ||
236 | for (i = 0; i < AARCH64_V_REGS_NUM; i++) | |
237 | collect_register (regcache, AARCH64_V0_REGNO + i, ®set->vregs[i]); | |
bf330350 CU |
238 | collect_register (regcache, AARCH64_FPSR_REGNO, ®set->fpsr); |
239 | collect_register (regcache, AARCH64_FPCR_REGNO, ®set->fpcr); | |
176eb98c MS |
240 | } |
241 | ||
242 | static void | |
243 | aarch64_store_fpregset (struct regcache *regcache, const void *buf) | |
244 | { | |
245 | const struct user_fpsimd_state *regset = buf; | |
246 | int i; | |
247 | ||
248 | for (i = 0; i < AARCH64_V_REGS_NUM; i++) | |
249 | supply_register (regcache, AARCH64_V0_REGNO + i, ®set->vregs[i]); | |
bf330350 CU |
250 | supply_register (regcache, AARCH64_FPSR_REGNO, ®set->fpsr); |
251 | supply_register (regcache, AARCH64_FPCR_REGNO, ®set->fpcr); | |
176eb98c MS |
252 | } |
253 | ||
176eb98c MS |
254 | /* Enable miscellaneous debugging output. The name is historical - it |
255 | was originally used to debug LinuxThreads support. */ | |
256 | extern int debug_threads; | |
257 | ||
258 | static CORE_ADDR | |
259 | aarch64_get_pc (struct regcache *regcache) | |
260 | { | |
261 | unsigned long pc; | |
262 | ||
263 | collect_register_by_name (regcache, "pc", &pc); | |
264 | if (debug_threads) | |
87ce2a04 | 265 | debug_printf ("stop pc is %08lx\n", pc); |
176eb98c MS |
266 | return pc; |
267 | } | |
268 | ||
269 | static void | |
270 | aarch64_set_pc (struct regcache *regcache, CORE_ADDR pc) | |
271 | { | |
272 | unsigned long newpc = pc; | |
273 | supply_register_by_name (regcache, "pc", &newpc); | |
274 | } | |
275 | ||
176eb98c MS |
276 | #define aarch64_breakpoint_len 4 |
277 | ||
37d66942 PL |
278 | /* AArch64 BRK software debug mode instruction. |
279 | This instruction needs to match gdb/aarch64-tdep.c | |
280 | (aarch64_default_breakpoint). */ | |
281 | static const gdb_byte aarch64_breakpoint[] = {0x00, 0x00, 0x20, 0xd4}; | |
176eb98c MS |
282 | |
283 | static int | |
284 | aarch64_breakpoint_at (CORE_ADDR where) | |
285 | { | |
37d66942 | 286 | gdb_byte insn[aarch64_breakpoint_len]; |
176eb98c | 287 | |
37d66942 PL |
288 | (*the_target->read_memory) (where, (unsigned char *) &insn, |
289 | aarch64_breakpoint_len); | |
290 | if (memcmp (insn, aarch64_breakpoint, aarch64_breakpoint_len) == 0) | |
176eb98c MS |
291 | return 1; |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | /* Print the values of the cached breakpoint/watchpoint registers. | |
297 | This is enabled via the "set debug-hw-points" monitor command. */ | |
298 | ||
299 | static void | |
300 | aarch64_show_debug_reg_state (struct aarch64_debug_reg_state *state, | |
301 | const char *func, CORE_ADDR addr, | |
4ff0d3d8 | 302 | int len, enum target_hw_bp_type type) |
176eb98c MS |
303 | { |
304 | int i; | |
305 | ||
306 | fprintf (stderr, "%s", func); | |
307 | if (addr || len) | |
308 | fprintf (stderr, " (addr=0x%08lx, len=%d, type=%s)", | |
309 | (unsigned long) addr, len, | |
310 | type == hw_write ? "hw-write-watchpoint" | |
311 | : (type == hw_read ? "hw-read-watchpoint" | |
312 | : (type == hw_access ? "hw-access-watchpoint" | |
313 | : (type == hw_execute ? "hw-breakpoint" | |
314 | : "??unknown??")))); | |
315 | fprintf (stderr, ":\n"); | |
316 | ||
317 | fprintf (stderr, "\tBREAKPOINTs:\n"); | |
318 | for (i = 0; i < aarch64_num_bp_regs; i++) | |
319 | fprintf (stderr, "\tBP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n", | |
320 | i, paddress (state->dr_addr_bp[i]), | |
321 | state->dr_ctrl_bp[i], state->dr_ref_count_bp[i]); | |
322 | ||
323 | fprintf (stderr, "\tWATCHPOINTs:\n"); | |
324 | for (i = 0; i < aarch64_num_wp_regs; i++) | |
325 | fprintf (stderr, "\tWP%d: addr=0x%s, ctrl=0x%08x, ref.count=%d\n", | |
326 | i, paddress (state->dr_addr_wp[i]), | |
327 | state->dr_ctrl_wp[i], state->dr_ref_count_wp[i]); | |
328 | } | |
329 | ||
330 | static void | |
331 | aarch64_init_debug_reg_state (struct aarch64_debug_reg_state *state) | |
332 | { | |
333 | int i; | |
334 | ||
335 | for (i = 0; i < AARCH64_HBP_MAX_NUM; ++i) | |
336 | { | |
337 | state->dr_addr_bp[i] = 0; | |
338 | state->dr_ctrl_bp[i] = 0; | |
339 | state->dr_ref_count_bp[i] = 0; | |
340 | } | |
341 | ||
342 | for (i = 0; i < AARCH64_HWP_MAX_NUM; ++i) | |
343 | { | |
344 | state->dr_addr_wp[i] = 0; | |
345 | state->dr_ctrl_wp[i] = 0; | |
346 | state->dr_ref_count_wp[i] = 0; | |
347 | } | |
348 | } | |
349 | ||
350 | /* ptrace expects control registers to be formatted as follows: | |
351 | ||
352 | 31 13 5 3 1 0 | |
353 | +--------------------------------+----------+------+------+----+ | |
354 | | RESERVED (SBZ) | LENGTH | TYPE | PRIV | EN | | |
355 | +--------------------------------+----------+------+------+----+ | |
356 | ||
357 | The TYPE field is ignored for breakpoints. */ | |
358 | ||
359 | #define DR_CONTROL_ENABLED(ctrl) (((ctrl) & 0x1) == 1) | |
360 | #define DR_CONTROL_LENGTH(ctrl) (((ctrl) >> 5) & 0xff) | |
361 | ||
362 | /* Utility function that returns the length in bytes of a watchpoint | |
363 | according to the content of a hardware debug control register CTRL. | |
364 | Note that the kernel currently only supports the following Byte | |
365 | Address Select (BAS) values: 0x1, 0x3, 0xf and 0xff, which means | |
366 | that for a hardware watchpoint, its valid length can only be 1 | |
367 | byte, 2 bytes, 4 bytes or 8 bytes. */ | |
368 | ||
369 | static inline unsigned int | |
370 | aarch64_watchpoint_length (unsigned int ctrl) | |
371 | { | |
372 | switch (DR_CONTROL_LENGTH (ctrl)) | |
373 | { | |
374 | case 0x01: | |
375 | return 1; | |
376 | case 0x03: | |
377 | return 2; | |
378 | case 0x0f: | |
379 | return 4; | |
380 | case 0xff: | |
381 | return 8; | |
382 | default: | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | ||
387 | /* Given the hardware breakpoint or watchpoint type TYPE and its | |
388 | length LEN, return the expected encoding for a hardware | |
389 | breakpoint/watchpoint control register. */ | |
390 | ||
391 | static unsigned int | |
4ff0d3d8 | 392 | aarch64_point_encode_ctrl_reg (enum target_hw_bp_type type, int len) |
176eb98c | 393 | { |
4ff0d3d8 | 394 | unsigned int ctrl, ttype; |
176eb98c MS |
395 | |
396 | /* type */ | |
4ff0d3d8 PA |
397 | switch (type) |
398 | { | |
399 | case hw_write: | |
400 | ttype = 2; | |
401 | break; | |
402 | case hw_read: | |
403 | ttype = 1; | |
404 | break; | |
405 | case hw_access: | |
406 | ttype = 3; | |
407 | break; | |
408 | case hw_execute: | |
409 | ttype = 0; | |
410 | break; | |
411 | default: | |
412 | perror_with_name (_("Unrecognized breakpoint/watchpoint type")); | |
413 | } | |
414 | ||
415 | /* type */ | |
416 | ctrl = ttype << 3; | |
176eb98c MS |
417 | /* length bitmask */ |
418 | ctrl |= ((1 << len) - 1) << 5; | |
419 | /* enabled at el0 */ | |
420 | ctrl |= (2 << 1) | 1; | |
421 | ||
422 | return ctrl; | |
423 | } | |
424 | ||
425 | /* Addresses to be written to the hardware breakpoint and watchpoint | |
426 | value registers need to be aligned; the alignment is 4-byte and | |
427 | 8-type respectively. Linux kernel rejects any non-aligned address | |
428 | it receives from the related ptrace call. Furthermore, the kernel | |
429 | currently only supports the following Byte Address Select (BAS) | |
430 | values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware | |
431 | watchpoint to be accepted by the kernel (via ptrace call), its | |
432 | valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes. | |
433 | Despite these limitations, the unaligned watchpoint is supported in | |
434 | this gdbserver port. | |
435 | ||
436 | Return 0 for any non-compliant ADDR and/or LEN; return 1 otherwise. */ | |
437 | ||
438 | static int | |
439 | aarch64_point_is_aligned (int is_watchpoint, CORE_ADDR addr, int len) | |
440 | { | |
441 | unsigned int alignment = is_watchpoint ? AARCH64_HWP_ALIGNMENT | |
442 | : AARCH64_HBP_ALIGNMENT; | |
443 | ||
444 | if (addr & (alignment - 1)) | |
445 | return 0; | |
446 | ||
447 | if (len != 8 && len != 4 && len != 2 && len != 1) | |
448 | return 0; | |
449 | ||
450 | return 1; | |
451 | } | |
452 | ||
453 | /* Given the (potentially unaligned) watchpoint address in ADDR and | |
454 | length in LEN, return the aligned address and aligned length in | |
455 | *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively. The returned | |
456 | aligned address and length will be valid to be written to the | |
457 | hardware watchpoint value and control registers. See the comment | |
458 | above aarch64_point_is_aligned for the information about the | |
459 | alignment requirement. The given watchpoint may get truncated if | |
460 | more than one hardware register is needed to cover the watched | |
461 | region. *NEXT_ADDR_P and *NEXT_LEN_P, if non-NULL, will return the | |
462 | address and length of the remaining part of the watchpoint (which | |
463 | can be processed by calling this routine again to generate another | |
464 | aligned address and length pair. | |
465 | ||
466 | Essentially, unaligned watchpoint is achieved by minimally | |
467 | enlarging the watched area to meet the alignment requirement, and | |
468 | if necessary, splitting the watchpoint over several hardware | |
469 | watchpoint registers. The trade-off is that there will be | |
470 | false-positive hits for the read-type or the access-type hardware | |
471 | watchpoints; for the write type, which is more commonly used, there | |
472 | will be no such issues, as the higher-level breakpoint management | |
473 | in gdb always examines the exact watched region for any content | |
474 | change, and transparently resumes a thread from a watchpoint trap | |
475 | if there is no change to the watched region. | |
476 | ||
477 | Another limitation is that because the watched region is enlarged, | |
478 | the watchpoint fault address returned by | |
479 | aarch64_stopped_data_address may be outside of the original watched | |
480 | region, especially when the triggering instruction is accessing a | |
481 | larger region. When the fault address is not within any known | |
482 | range, watchpoints_triggered in gdb will get confused, as the | |
483 | higher-level watchpoint management is only aware of original | |
484 | watched regions, and will think that some unknown watchpoint has | |
485 | been triggered. In such a case, gdb may stop without displaying | |
486 | any detailed information. | |
487 | ||
488 | Once the kernel provides the full support for Byte Address Select | |
489 | (BAS) in the hardware watchpoint control register, these | |
490 | limitations can be largely relaxed with some further work. */ | |
491 | ||
492 | static void | |
493 | aarch64_align_watchpoint (CORE_ADDR addr, int len, CORE_ADDR *aligned_addr_p, | |
494 | int *aligned_len_p, CORE_ADDR *next_addr_p, | |
495 | int *next_len_p) | |
496 | { | |
497 | int aligned_len; | |
498 | unsigned int offset; | |
499 | CORE_ADDR aligned_addr; | |
500 | const unsigned int alignment = AARCH64_HWP_ALIGNMENT; | |
501 | const unsigned int max_wp_len = AARCH64_HWP_MAX_LEN_PER_REG; | |
502 | ||
503 | /* As assumed by the algorithm. */ | |
504 | gdb_assert (alignment == max_wp_len); | |
505 | ||
506 | if (len <= 0) | |
507 | return; | |
508 | ||
509 | /* Address to be put into the hardware watchpoint value register | |
510 | must be aligned. */ | |
511 | offset = addr & (alignment - 1); | |
512 | aligned_addr = addr - offset; | |
513 | ||
514 | gdb_assert (offset >= 0 && offset < alignment); | |
515 | gdb_assert (aligned_addr >= 0 && aligned_addr <= addr); | |
516 | gdb_assert ((offset + len) > 0); | |
517 | ||
518 | if (offset + len >= max_wp_len) | |
519 | { | |
520 | /* Need more than one watchpoint registers; truncate it at the | |
521 | alignment boundary. */ | |
522 | aligned_len = max_wp_len; | |
523 | len -= (max_wp_len - offset); | |
524 | addr += (max_wp_len - offset); | |
525 | gdb_assert ((addr & (alignment - 1)) == 0); | |
526 | } | |
527 | else | |
528 | { | |
529 | /* Find the smallest valid length that is large enough to | |
530 | accommodate this watchpoint. */ | |
531 | static const unsigned char | |
532 | aligned_len_array[AARCH64_HWP_MAX_LEN_PER_REG] = | |
533 | { 1, 2, 4, 4, 8, 8, 8, 8 }; | |
534 | ||
535 | aligned_len = aligned_len_array[offset + len - 1]; | |
536 | addr += len; | |
537 | len = 0; | |
538 | } | |
539 | ||
540 | if (aligned_addr_p != NULL) | |
541 | *aligned_addr_p = aligned_addr; | |
542 | if (aligned_len_p != NULL) | |
543 | *aligned_len_p = aligned_len; | |
544 | if (next_addr_p != NULL) | |
545 | *next_addr_p = addr; | |
546 | if (next_len_p != NULL) | |
547 | *next_len_p = len; | |
548 | } | |
549 | ||
550 | /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint | |
551 | registers with data from *STATE. */ | |
552 | ||
553 | static void | |
554 | aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state *state, | |
555 | int tid, int watchpoint) | |
556 | { | |
557 | int i, count; | |
558 | struct iovec iov; | |
559 | struct user_hwdebug_state regs; | |
560 | const CORE_ADDR *addr; | |
561 | const unsigned int *ctrl; | |
562 | ||
c623a6ef | 563 | memset (®s, 0, sizeof (regs)); |
176eb98c | 564 | iov.iov_base = ®s; |
176eb98c MS |
565 | count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs; |
566 | addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp; | |
567 | ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp; | |
f45c82da YZ |
568 | if (count == 0) |
569 | return; | |
570 | iov.iov_len = (offsetof (struct user_hwdebug_state, dbg_regs[count - 1]) | |
571 | + sizeof (regs.dbg_regs [count - 1])); | |
176eb98c MS |
572 | |
573 | for (i = 0; i < count; i++) | |
574 | { | |
575 | regs.dbg_regs[i].addr = addr[i]; | |
576 | regs.dbg_regs[i].ctrl = ctrl[i]; | |
577 | } | |
578 | ||
579 | if (ptrace (PTRACE_SETREGSET, tid, | |
580 | watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, | |
581 | (void *) &iov)) | |
582 | error (_("Unexpected error setting hardware debug registers")); | |
583 | } | |
584 | ||
585 | struct aarch64_dr_update_callback_param | |
586 | { | |
587 | int pid; | |
588 | int is_watchpoint; | |
589 | unsigned int idx; | |
590 | }; | |
591 | ||
592 | /* Callback function which records the information about the change of | |
593 | one hardware breakpoint/watchpoint setting for the thread ENTRY. | |
594 | The information is passed in via PTR. | |
595 | N.B. The actual updating of hardware debug registers is not | |
596 | carried out until the moment the thread is resumed. */ | |
597 | ||
598 | static int | |
599 | debug_reg_change_callback (struct inferior_list_entry *entry, void *ptr) | |
600 | { | |
d86d4aaf DE |
601 | struct thread_info *thread = (struct thread_info *) entry; |
602 | struct lwp_info *lwp = get_thread_lwp (thread); | |
176eb98c MS |
603 | struct aarch64_dr_update_callback_param *param_p |
604 | = (struct aarch64_dr_update_callback_param *) ptr; | |
605 | int pid = param_p->pid; | |
606 | int idx = param_p->idx; | |
607 | int is_watchpoint = param_p->is_watchpoint; | |
608 | struct arch_lwp_info *info = lwp->arch_private; | |
609 | dr_changed_t *dr_changed_ptr; | |
610 | dr_changed_t dr_changed; | |
611 | ||
c5e92cca | 612 | if (show_debug_regs) |
176eb98c MS |
613 | { |
614 | fprintf (stderr, "debug_reg_change_callback: \n\tOn entry:\n"); | |
615 | fprintf (stderr, "\tpid%d, tid: %ld, dr_changed_bp=0x%llx, " | |
616 | "dr_changed_wp=0x%llx\n", | |
d86d4aaf | 617 | pid, lwpid_of (thread), info->dr_changed_bp, |
176eb98c MS |
618 | info->dr_changed_wp); |
619 | } | |
620 | ||
621 | dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp | |
622 | : &info->dr_changed_bp; | |
623 | dr_changed = *dr_changed_ptr; | |
624 | ||
625 | /* Only update the threads of this process. */ | |
d86d4aaf | 626 | if (pid_of (thread) == pid) |
176eb98c MS |
627 | { |
628 | gdb_assert (idx >= 0 | |
629 | && (idx <= (is_watchpoint ? aarch64_num_wp_regs | |
630 | : aarch64_num_bp_regs))); | |
631 | ||
632 | /* The following assertion is not right, as there can be changes | |
633 | that have not been made to the hardware debug registers | |
634 | before new changes overwrite the old ones. This can happen, | |
635 | for instance, when the breakpoint/watchpoint hit one of the | |
636 | threads and the user enters continue; then what happens is: | |
637 | 1) all breakpoints/watchpoints are removed for all threads; | |
638 | 2) a single step is carried out for the thread that was hit; | |
639 | 3) all of the points are inserted again for all threads; | |
640 | 4) all threads are resumed. | |
641 | The 2nd step will only affect the one thread in which the | |
642 | bp/wp was hit, which means only that one thread is resumed; | |
643 | remember that the actual updating only happen in | |
644 | aarch64_linux_prepare_to_resume, so other threads remain | |
645 | stopped during the removal and insertion of bp/wp. Therefore | |
646 | for those threads, the change of insertion of the bp/wp | |
647 | overwrites that of the earlier removals. (The situation may | |
648 | be different when bp/wp is steppable, or in the non-stop | |
649 | mode.) */ | |
650 | /* gdb_assert (DR_N_HAS_CHANGED (dr_changed, idx) == 0); */ | |
651 | ||
652 | /* The actual update is done later just before resuming the lwp, | |
653 | we just mark that one register pair needs updating. */ | |
654 | DR_MARK_N_CHANGED (dr_changed, idx); | |
655 | *dr_changed_ptr = dr_changed; | |
656 | ||
657 | /* If the lwp isn't stopped, force it to momentarily pause, so | |
658 | we can update its debug registers. */ | |
659 | if (!lwp->stopped) | |
660 | linux_stop_lwp (lwp); | |
661 | } | |
662 | ||
c5e92cca | 663 | if (show_debug_regs) |
176eb98c MS |
664 | { |
665 | fprintf (stderr, "\tOn exit:\n\tpid%d, tid: %ld, dr_changed_bp=0x%llx, " | |
666 | "dr_changed_wp=0x%llx\n", | |
d86d4aaf DE |
667 | pid, lwpid_of (thread), info->dr_changed_bp, |
668 | info->dr_changed_wp); | |
176eb98c MS |
669 | } |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
674 | /* Notify each thread that their IDXth breakpoint/watchpoint register | |
675 | pair needs to be updated. The message will be recorded in each | |
676 | thread's arch-specific data area, the actual updating will be done | |
677 | when the thread is resumed. */ | |
678 | ||
679 | void | |
680 | aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state, | |
681 | int is_watchpoint, unsigned int idx) | |
682 | { | |
683 | struct aarch64_dr_update_callback_param param; | |
684 | ||
685 | /* Only update the threads of this process. */ | |
0bfdf32f | 686 | param.pid = pid_of (current_thread); |
176eb98c MS |
687 | |
688 | param.is_watchpoint = is_watchpoint; | |
689 | param.idx = idx; | |
690 | ||
d86d4aaf | 691 | find_inferior (&all_threads, debug_reg_change_callback, (void *) ¶m); |
176eb98c MS |
692 | } |
693 | ||
694 | ||
695 | /* Return the pointer to the debug register state structure in the | |
696 | current process' arch-specific data area. */ | |
697 | ||
698 | static struct aarch64_debug_reg_state * | |
699 | aarch64_get_debug_reg_state () | |
700 | { | |
701 | struct process_info *proc; | |
702 | ||
703 | proc = current_process (); | |
fe978cb0 | 704 | return &proc->priv->arch_private->debug_reg_state; |
176eb98c MS |
705 | } |
706 | ||
707 | /* Record the insertion of one breakpoint/watchpoint, as represented | |
708 | by ADDR and CTRL, in the process' arch-specific data area *STATE. */ | |
709 | ||
710 | static int | |
711 | aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state *state, | |
4ff0d3d8 | 712 | enum target_hw_bp_type type, |
176eb98c MS |
713 | CORE_ADDR addr, int len) |
714 | { | |
715 | int i, idx, num_regs, is_watchpoint; | |
716 | unsigned int ctrl, *dr_ctrl_p, *dr_ref_count; | |
717 | CORE_ADDR *dr_addr_p; | |
718 | ||
719 | /* Set up state pointers. */ | |
720 | is_watchpoint = (type != hw_execute); | |
721 | gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len)); | |
722 | if (is_watchpoint) | |
723 | { | |
724 | num_regs = aarch64_num_wp_regs; | |
725 | dr_addr_p = state->dr_addr_wp; | |
726 | dr_ctrl_p = state->dr_ctrl_wp; | |
727 | dr_ref_count = state->dr_ref_count_wp; | |
728 | } | |
729 | else | |
730 | { | |
731 | num_regs = aarch64_num_bp_regs; | |
732 | dr_addr_p = state->dr_addr_bp; | |
733 | dr_ctrl_p = state->dr_ctrl_bp; | |
734 | dr_ref_count = state->dr_ref_count_bp; | |
735 | } | |
736 | ||
737 | ctrl = aarch64_point_encode_ctrl_reg (type, len); | |
738 | ||
739 | /* Find an existing or free register in our cache. */ | |
740 | idx = -1; | |
741 | for (i = 0; i < num_regs; ++i) | |
742 | { | |
743 | if ((dr_ctrl_p[i] & 1) == 0) | |
744 | { | |
745 | gdb_assert (dr_ref_count[i] == 0); | |
746 | idx = i; | |
747 | /* no break; continue hunting for an exising one. */ | |
748 | } | |
749 | else if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl) | |
750 | { | |
751 | gdb_assert (dr_ref_count[i] != 0); | |
752 | idx = i; | |
753 | break; | |
754 | } | |
755 | } | |
756 | ||
757 | /* No space. */ | |
758 | if (idx == -1) | |
759 | return -1; | |
760 | ||
761 | /* Update our cache. */ | |
762 | if ((dr_ctrl_p[idx] & 1) == 0) | |
763 | { | |
764 | /* new entry */ | |
765 | dr_addr_p[idx] = addr; | |
766 | dr_ctrl_p[idx] = ctrl; | |
767 | dr_ref_count[idx] = 1; | |
768 | /* Notify the change. */ | |
769 | aarch64_notify_debug_reg_change (state, is_watchpoint, idx); | |
770 | } | |
771 | else | |
772 | { | |
773 | /* existing entry */ | |
774 | dr_ref_count[idx]++; | |
775 | } | |
776 | ||
777 | return 0; | |
778 | } | |
779 | ||
780 | /* Record the removal of one breakpoint/watchpoint, as represented by | |
781 | ADDR and CTRL, in the process' arch-specific data area *STATE. */ | |
782 | ||
783 | static int | |
784 | aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state *state, | |
4ff0d3d8 | 785 | enum target_hw_bp_type type, |
176eb98c MS |
786 | CORE_ADDR addr, int len) |
787 | { | |
788 | int i, num_regs, is_watchpoint; | |
789 | unsigned int ctrl, *dr_ctrl_p, *dr_ref_count; | |
790 | CORE_ADDR *dr_addr_p; | |
791 | ||
792 | /* Set up state pointers. */ | |
793 | is_watchpoint = (type != hw_execute); | |
794 | gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len)); | |
795 | if (is_watchpoint) | |
796 | { | |
797 | num_regs = aarch64_num_wp_regs; | |
798 | dr_addr_p = state->dr_addr_wp; | |
799 | dr_ctrl_p = state->dr_ctrl_wp; | |
800 | dr_ref_count = state->dr_ref_count_wp; | |
801 | } | |
802 | else | |
803 | { | |
804 | num_regs = aarch64_num_bp_regs; | |
805 | dr_addr_p = state->dr_addr_bp; | |
806 | dr_ctrl_p = state->dr_ctrl_bp; | |
807 | dr_ref_count = state->dr_ref_count_bp; | |
808 | } | |
809 | ||
810 | ctrl = aarch64_point_encode_ctrl_reg (type, len); | |
811 | ||
812 | /* Find the entry that matches the ADDR and CTRL. */ | |
813 | for (i = 0; i < num_regs; ++i) | |
814 | if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl) | |
815 | { | |
816 | gdb_assert (dr_ref_count[i] != 0); | |
817 | break; | |
818 | } | |
819 | ||
820 | /* Not found. */ | |
821 | if (i == num_regs) | |
822 | return -1; | |
823 | ||
824 | /* Clear our cache. */ | |
825 | if (--dr_ref_count[i] == 0) | |
826 | { | |
827 | /* Clear the enable bit. */ | |
828 | ctrl &= ~1; | |
829 | dr_addr_p[i] = 0; | |
830 | dr_ctrl_p[i] = ctrl; | |
831 | /* Notify the change. */ | |
832 | aarch64_notify_debug_reg_change (state, is_watchpoint, i); | |
833 | } | |
834 | ||
835 | return 0; | |
836 | } | |
837 | ||
838 | static int | |
4ff0d3d8 | 839 | aarch64_handle_breakpoint (enum target_hw_bp_type type, CORE_ADDR addr, |
176eb98c MS |
840 | int len, int is_insert) |
841 | { | |
842 | struct aarch64_debug_reg_state *state; | |
843 | ||
844 | /* The hardware breakpoint on AArch64 should always be 4-byte | |
845 | aligned. */ | |
846 | if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr, len)) | |
847 | return -1; | |
848 | ||
849 | state = aarch64_get_debug_reg_state (); | |
850 | ||
851 | if (is_insert) | |
852 | return aarch64_dr_state_insert_one_point (state, type, addr, len); | |
853 | else | |
854 | return aarch64_dr_state_remove_one_point (state, type, addr, len); | |
855 | } | |
856 | ||
857 | /* This is essentially the same as aarch64_handle_breakpoint, apart | |
858 | from that it is an aligned watchpoint to be handled. */ | |
859 | ||
860 | static int | |
4ff0d3d8 | 861 | aarch64_handle_aligned_watchpoint (enum target_hw_bp_type type, |
176eb98c MS |
862 | CORE_ADDR addr, int len, int is_insert) |
863 | { | |
864 | struct aarch64_debug_reg_state *state; | |
865 | ||
866 | state = aarch64_get_debug_reg_state (); | |
867 | ||
868 | if (is_insert) | |
869 | return aarch64_dr_state_insert_one_point (state, type, addr, len); | |
870 | else | |
871 | return aarch64_dr_state_remove_one_point (state, type, addr, len); | |
872 | } | |
873 | ||
874 | /* Insert/remove unaligned watchpoint by calling | |
875 | aarch64_align_watchpoint repeatedly until the whole watched region, | |
876 | as represented by ADDR and LEN, has been properly aligned and ready | |
877 | to be written to one or more hardware watchpoint registers. | |
878 | IS_INSERT indicates whether this is an insertion or a deletion. | |
879 | Return 0 if succeed. */ | |
880 | ||
881 | static int | |
4ff0d3d8 | 882 | aarch64_handle_unaligned_watchpoint (enum target_hw_bp_type type, |
176eb98c MS |
883 | CORE_ADDR addr, int len, int is_insert) |
884 | { | |
885 | struct aarch64_debug_reg_state *state | |
886 | = aarch64_get_debug_reg_state (); | |
887 | ||
888 | while (len > 0) | |
889 | { | |
890 | CORE_ADDR aligned_addr; | |
891 | int aligned_len, ret; | |
892 | ||
893 | aarch64_align_watchpoint (addr, len, &aligned_addr, &aligned_len, | |
894 | &addr, &len); | |
895 | ||
896 | if (is_insert) | |
897 | ret = aarch64_dr_state_insert_one_point (state, type, aligned_addr, | |
898 | aligned_len); | |
899 | else | |
900 | ret = aarch64_dr_state_remove_one_point (state, type, aligned_addr, | |
901 | aligned_len); | |
902 | ||
c5e92cca | 903 | if (show_debug_regs) |
176eb98c MS |
904 | fprintf (stderr, |
905 | "handle_unaligned_watchpoint: is_insert: %d\n" | |
906 | " aligned_addr: 0x%s, aligned_len: %d\n" | |
907 | " next_addr: 0x%s, next_len: %d\n", | |
908 | is_insert, paddress (aligned_addr), aligned_len, | |
909 | paddress (addr), len); | |
910 | ||
911 | if (ret != 0) | |
912 | return ret; | |
913 | } | |
914 | ||
915 | return 0; | |
916 | } | |
917 | ||
918 | static int | |
4ff0d3d8 | 919 | aarch64_handle_watchpoint (enum target_hw_bp_type type, CORE_ADDR addr, |
176eb98c MS |
920 | int len, int is_insert) |
921 | { | |
922 | if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr, len)) | |
923 | return aarch64_handle_aligned_watchpoint (type, addr, len, is_insert); | |
924 | else | |
925 | return aarch64_handle_unaligned_watchpoint (type, addr, len, is_insert); | |
926 | } | |
927 | ||
4ff0d3d8 PA |
928 | static int |
929 | aarch64_supports_z_point_type (char z_type) | |
930 | { | |
931 | switch (z_type) | |
932 | { | |
96c97461 | 933 | case Z_PACKET_SW_BP: |
4ff0d3d8 PA |
934 | case Z_PACKET_HW_BP: |
935 | case Z_PACKET_WRITE_WP: | |
936 | case Z_PACKET_READ_WP: | |
937 | case Z_PACKET_ACCESS_WP: | |
938 | return 1; | |
939 | default: | |
4ff0d3d8 PA |
940 | return 0; |
941 | } | |
942 | } | |
943 | ||
176eb98c MS |
944 | /* Insert a hardware breakpoint/watchpoint. |
945 | It actually only records the info of the to-be-inserted bp/wp; | |
946 | the actual insertion will happen when threads are resumed. | |
947 | ||
948 | Return 0 if succeed; | |
949 | Return 1 if TYPE is unsupported type; | |
950 | Return -1 if an error occurs. */ | |
951 | ||
952 | static int | |
802e8e6d PA |
953 | aarch64_insert_point (enum raw_bkpt_type type, CORE_ADDR addr, |
954 | int len, struct raw_breakpoint *bp) | |
176eb98c MS |
955 | { |
956 | int ret; | |
4ff0d3d8 PA |
957 | enum target_hw_bp_type targ_type; |
958 | ||
c5e92cca | 959 | if (show_debug_regs) |
176eb98c MS |
960 | fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n", |
961 | (unsigned long) addr, len); | |
962 | ||
802e8e6d PA |
963 | /* Determine the type from the raw breakpoint type. */ |
964 | targ_type = raw_bkpt_type_to_target_hw_bp_type (type); | |
176eb98c MS |
965 | |
966 | if (targ_type != hw_execute) | |
967 | ret = | |
968 | aarch64_handle_watchpoint (targ_type, addr, len, 1 /* is_insert */); | |
969 | else | |
970 | ret = | |
971 | aarch64_handle_breakpoint (targ_type, addr, len, 1 /* is_insert */); | |
972 | ||
60a191ed | 973 | if (show_debug_regs) |
176eb98c MS |
974 | aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (), |
975 | "insert_point", addr, len, targ_type); | |
976 | ||
977 | return ret; | |
978 | } | |
979 | ||
980 | /* Remove a hardware breakpoint/watchpoint. | |
981 | It actually only records the info of the to-be-removed bp/wp, | |
982 | the actual removal will be done when threads are resumed. | |
983 | ||
984 | Return 0 if succeed; | |
985 | Return 1 if TYPE is an unsupported type; | |
986 | Return -1 if an error occurs. */ | |
987 | ||
988 | static int | |
802e8e6d PA |
989 | aarch64_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, |
990 | int len, struct raw_breakpoint *bp) | |
176eb98c MS |
991 | { |
992 | int ret; | |
4ff0d3d8 PA |
993 | enum target_hw_bp_type targ_type; |
994 | ||
c5e92cca | 995 | if (show_debug_regs) |
176eb98c MS |
996 | fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n", |
997 | (unsigned long) addr, len); | |
998 | ||
802e8e6d PA |
999 | /* Determine the type from the raw breakpoint type. */ |
1000 | targ_type = raw_bkpt_type_to_target_hw_bp_type (type); | |
176eb98c MS |
1001 | |
1002 | /* Set up state pointers. */ | |
1003 | if (targ_type != hw_execute) | |
1004 | ret = | |
1005 | aarch64_handle_watchpoint (targ_type, addr, len, 0 /* is_insert */); | |
1006 | else | |
1007 | ret = | |
1008 | aarch64_handle_breakpoint (targ_type, addr, len, 0 /* is_insert */); | |
1009 | ||
60a191ed | 1010 | if (show_debug_regs) |
176eb98c MS |
1011 | aarch64_show_debug_reg_state (aarch64_get_debug_reg_state (), |
1012 | "remove_point", addr, len, targ_type); | |
1013 | ||
1014 | return ret; | |
1015 | } | |
1016 | ||
1017 | /* Returns the address associated with the watchpoint that hit, if | |
1018 | any; returns 0 otherwise. */ | |
1019 | ||
1020 | static CORE_ADDR | |
1021 | aarch64_stopped_data_address (void) | |
1022 | { | |
1023 | siginfo_t siginfo; | |
1024 | int pid, i; | |
1025 | struct aarch64_debug_reg_state *state; | |
1026 | ||
0bfdf32f | 1027 | pid = lwpid_of (current_thread); |
176eb98c MS |
1028 | |
1029 | /* Get the siginfo. */ | |
1030 | if (ptrace (PTRACE_GETSIGINFO, pid, NULL, &siginfo) != 0) | |
1031 | return (CORE_ADDR) 0; | |
1032 | ||
1033 | /* Need to be a hardware breakpoint/watchpoint trap. */ | |
1034 | if (siginfo.si_signo != SIGTRAP | |
1035 | || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */) | |
1036 | return (CORE_ADDR) 0; | |
1037 | ||
1038 | /* Check if the address matches any watched address. */ | |
1039 | state = aarch64_get_debug_reg_state (); | |
1040 | for (i = aarch64_num_wp_regs - 1; i >= 0; --i) | |
1041 | { | |
1042 | const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]); | |
1043 | const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr; | |
1044 | const CORE_ADDR addr_watch = state->dr_addr_wp[i]; | |
1045 | if (state->dr_ref_count_wp[i] | |
1046 | && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i]) | |
1047 | && addr_trap >= addr_watch | |
1048 | && addr_trap < addr_watch + len) | |
1049 | return addr_trap; | |
1050 | } | |
1051 | ||
1052 | return (CORE_ADDR) 0; | |
1053 | } | |
1054 | ||
1055 | /* Returns 1 if target was stopped due to a watchpoint hit, 0 | |
1056 | otherwise. */ | |
1057 | ||
1058 | static int | |
1059 | aarch64_stopped_by_watchpoint (void) | |
1060 | { | |
1061 | if (aarch64_stopped_data_address () != 0) | |
1062 | return 1; | |
1063 | else | |
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | /* Fetch the thread-local storage pointer for libthread_db. */ | |
1068 | ||
1069 | ps_err_e | |
55fac6e0 | 1070 | ps_get_thread_area (const struct ps_prochandle *ph, |
176eb98c MS |
1071 | lwpid_t lwpid, int idx, void **base) |
1072 | { | |
55fac6e0 MS |
1073 | struct iovec iovec; |
1074 | uint64_t reg; | |
1075 | ||
1076 | iovec.iov_base = ® | |
1077 | iovec.iov_len = sizeof (reg); | |
1078 | ||
1079 | if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0) | |
176eb98c MS |
1080 | return PS_ERR; |
1081 | ||
1082 | /* IDX is the bias from the thread pointer to the beginning of the | |
1083 | thread descriptor. It has to be subtracted due to implementation | |
1084 | quirks in libthread_db. */ | |
55fac6e0 | 1085 | *base = (void *) (reg - idx); |
176eb98c MS |
1086 | |
1087 | return PS_OK; | |
1088 | } | |
1089 | ||
1090 | /* Called when a new process is created. */ | |
1091 | ||
1092 | static struct arch_process_info * | |
1093 | aarch64_linux_new_process (void) | |
1094 | { | |
1095 | struct arch_process_info *info = xcalloc (1, sizeof (*info)); | |
1096 | ||
1097 | aarch64_init_debug_reg_state (&info->debug_reg_state); | |
1098 | ||
1099 | return info; | |
1100 | } | |
1101 | ||
1102 | /* Called when a new thread is detected. */ | |
1103 | ||
34c703da GB |
1104 | static void |
1105 | aarch64_linux_new_thread (struct lwp_info *lwp) | |
176eb98c MS |
1106 | { |
1107 | struct arch_lwp_info *info = xcalloc (1, sizeof (*info)); | |
1108 | ||
1109 | /* Mark that all the hardware breakpoint/watchpoint register pairs | |
1110 | for this thread need to be initialized (with data from | |
1111 | aarch_process_info.debug_reg_state). */ | |
1112 | DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs); | |
1113 | DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs); | |
1114 | ||
34c703da | 1115 | lwp->arch_private = info; |
176eb98c MS |
1116 | } |
1117 | ||
3a8a0396 DB |
1118 | static void |
1119 | aarch64_linux_new_fork (struct process_info *parent, | |
1120 | struct process_info *child) | |
1121 | { | |
1122 | /* These are allocated by linux_add_process. */ | |
61a7418c DB |
1123 | gdb_assert (parent->priv != NULL |
1124 | && parent->priv->arch_private != NULL); | |
1125 | gdb_assert (child->priv != NULL | |
1126 | && child->priv->arch_private != NULL); | |
3a8a0396 DB |
1127 | |
1128 | /* Linux kernel before 2.6.33 commit | |
1129 | 72f674d203cd230426437cdcf7dd6f681dad8b0d | |
1130 | will inherit hardware debug registers from parent | |
1131 | on fork/vfork/clone. Newer Linux kernels create such tasks with | |
1132 | zeroed debug registers. | |
1133 | ||
1134 | GDB core assumes the child inherits the watchpoints/hw | |
1135 | breakpoints of the parent, and will remove them all from the | |
1136 | forked off process. Copy the debug registers mirrors into the | |
1137 | new process so that all breakpoints and watchpoints can be | |
1138 | removed together. The debug registers mirror will become zeroed | |
1139 | in the end before detaching the forked off process, thus making | |
1140 | this compatible with older Linux kernels too. */ | |
1141 | ||
61a7418c | 1142 | *child->priv->arch_private = *parent->priv->arch_private; |
3a8a0396 DB |
1143 | } |
1144 | ||
176eb98c MS |
1145 | /* Called when resuming a thread. |
1146 | If the debug regs have changed, update the thread's copies. */ | |
1147 | ||
1148 | static void | |
1149 | aarch64_linux_prepare_to_resume (struct lwp_info *lwp) | |
1150 | { | |
d86d4aaf DE |
1151 | struct thread_info *thread = get_lwp_thread (lwp); |
1152 | ptid_t ptid = ptid_of (thread); | |
176eb98c MS |
1153 | struct arch_lwp_info *info = lwp->arch_private; |
1154 | ||
1155 | if (DR_HAS_CHANGED (info->dr_changed_bp) | |
1156 | || DR_HAS_CHANGED (info->dr_changed_wp)) | |
1157 | { | |
1158 | int tid = ptid_get_lwp (ptid); | |
1159 | struct process_info *proc = find_process_pid (ptid_get_pid (ptid)); | |
1160 | struct aarch64_debug_reg_state *state | |
fe978cb0 | 1161 | = &proc->priv->arch_private->debug_reg_state; |
176eb98c | 1162 | |
c5e92cca | 1163 | if (show_debug_regs) |
d86d4aaf | 1164 | fprintf (stderr, "prepare_to_resume thread %ld\n", lwpid_of (thread)); |
176eb98c MS |
1165 | |
1166 | /* Watchpoints. */ | |
1167 | if (DR_HAS_CHANGED (info->dr_changed_wp)) | |
1168 | { | |
1169 | aarch64_linux_set_debug_regs (state, tid, 1); | |
1170 | DR_CLEAR_CHANGED (info->dr_changed_wp); | |
1171 | } | |
1172 | ||
1173 | /* Breakpoints. */ | |
1174 | if (DR_HAS_CHANGED (info->dr_changed_bp)) | |
1175 | { | |
1176 | aarch64_linux_set_debug_regs (state, tid, 0); | |
1177 | DR_CLEAR_CHANGED (info->dr_changed_bp); | |
1178 | } | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | /* ptrace hardware breakpoint resource info is formatted as follows: | |
1183 | ||
1184 | 31 24 16 8 0 | |
1185 | +---------------+--------------+---------------+---------------+ | |
1186 | | RESERVED | RESERVED | DEBUG_ARCH | NUM_SLOTS | | |
1187 | +---------------+--------------+---------------+---------------+ */ | |
1188 | ||
1189 | #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff) | |
1190 | #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff) | |
1191 | #define AARCH64_DEBUG_ARCH_V8 0x6 | |
1192 | ||
1193 | static void | |
1194 | aarch64_arch_setup (void) | |
1195 | { | |
1196 | int pid; | |
1197 | struct iovec iov; | |
1198 | struct user_hwdebug_state dreg_state; | |
1199 | ||
3aee8918 | 1200 | current_process ()->tdesc = tdesc_aarch64; |
176eb98c | 1201 | |
0bfdf32f | 1202 | pid = lwpid_of (current_thread); |
176eb98c MS |
1203 | iov.iov_base = &dreg_state; |
1204 | iov.iov_len = sizeof (dreg_state); | |
1205 | ||
1206 | /* Get hardware watchpoint register info. */ | |
1207 | if (ptrace (PTRACE_GETREGSET, pid, NT_ARM_HW_WATCH, &iov) == 0 | |
1208 | && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8) | |
1209 | { | |
1210 | aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info); | |
148de6bb MS |
1211 | if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM) |
1212 | { | |
1213 | warning ("Unexpected number of hardware watchpoint registers reported" | |
1214 | " by ptrace, got %d, expected %d.", | |
1215 | aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM); | |
1216 | aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM; | |
1217 | } | |
176eb98c MS |
1218 | } |
1219 | else | |
1220 | { | |
1221 | warning ("Unable to determine the number of hardware watchpoints" | |
1222 | " available."); | |
1223 | aarch64_num_wp_regs = 0; | |
1224 | } | |
1225 | ||
1226 | /* Get hardware breakpoint register info. */ | |
1227 | if (ptrace (PTRACE_GETREGSET, pid, NT_ARM_HW_BREAK, &iov) == 0 | |
1228 | && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8) | |
1229 | { | |
1230 | aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info); | |
1231 | if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM) | |
148de6bb MS |
1232 | { |
1233 | warning ("Unexpected number of hardware breakpoint registers reported" | |
1234 | " by ptrace, got %d, expected %d.", | |
1235 | aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM); | |
1236 | aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM; | |
1237 | } | |
176eb98c MS |
1238 | } |
1239 | else | |
1240 | { | |
1241 | warning ("Unable to determine the number of hardware breakpoints" | |
1242 | " available."); | |
1243 | aarch64_num_bp_regs = 0; | |
1244 | } | |
1245 | } | |
1246 | ||
3aee8918 | 1247 | static struct regset_info aarch64_regsets[] = |
176eb98c MS |
1248 | { |
1249 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, | |
1250 | sizeof (struct user_pt_regs), GENERAL_REGS, | |
1251 | aarch64_fill_gregset, aarch64_store_gregset }, | |
1252 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, | |
1253 | sizeof (struct user_fpsimd_state), FP_REGS, | |
1254 | aarch64_fill_fpregset, aarch64_store_fpregset | |
1255 | }, | |
1256 | { 0, 0, 0, -1, -1, NULL, NULL } | |
1257 | }; | |
1258 | ||
3aee8918 PA |
1259 | static struct regsets_info aarch64_regsets_info = |
1260 | { | |
1261 | aarch64_regsets, /* regsets */ | |
1262 | 0, /* num_regsets */ | |
1263 | NULL, /* disabled_regsets */ | |
1264 | }; | |
1265 | ||
3aee8918 PA |
1266 | static struct regs_info regs_info = |
1267 | { | |
1268 | NULL, /* regset_bitmap */ | |
c2d65f38 | 1269 | NULL, /* usrregs */ |
3aee8918 PA |
1270 | &aarch64_regsets_info, |
1271 | }; | |
1272 | ||
1273 | static const struct regs_info * | |
1274 | aarch64_regs_info (void) | |
1275 | { | |
1276 | return ®s_info; | |
1277 | } | |
1278 | ||
176eb98c MS |
1279 | struct linux_target_ops the_low_target = |
1280 | { | |
1281 | aarch64_arch_setup, | |
3aee8918 | 1282 | aarch64_regs_info, |
176eb98c MS |
1283 | aarch64_cannot_fetch_register, |
1284 | aarch64_cannot_store_register, | |
1285 | NULL, | |
1286 | aarch64_get_pc, | |
1287 | aarch64_set_pc, | |
1288 | (const unsigned char *) &aarch64_breakpoint, | |
1289 | aarch64_breakpoint_len, | |
1290 | NULL, | |
1291 | 0, | |
1292 | aarch64_breakpoint_at, | |
802e8e6d | 1293 | aarch64_supports_z_point_type, |
176eb98c MS |
1294 | aarch64_insert_point, |
1295 | aarch64_remove_point, | |
1296 | aarch64_stopped_by_watchpoint, | |
1297 | aarch64_stopped_data_address, | |
1298 | NULL, | |
1299 | NULL, | |
1300 | NULL, | |
1301 | aarch64_linux_new_process, | |
1302 | aarch64_linux_new_thread, | |
3a8a0396 | 1303 | aarch64_linux_new_fork, |
176eb98c MS |
1304 | aarch64_linux_prepare_to_resume, |
1305 | }; | |
3aee8918 PA |
1306 | |
1307 | void | |
1308 | initialize_low_arch (void) | |
1309 | { | |
1310 | init_registers_aarch64 (); | |
1311 | ||
1312 | initialize_regsets_info (&aarch64_regsets_info); | |
1313 | } |