Commit | Line | Data |
---|---|---|
9d19df75 MS |
1 | /* Native-dependent code for GNU/Linux AArch64. |
2 | ||
3 | Copyright (C) 2011-2013 Free Software Foundation, Inc. | |
4 | Contributed by ARM Ltd. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "defs.h" | |
22 | ||
23 | #include "inferior.h" | |
24 | #include "gdbcore.h" | |
25 | #include "regcache.h" | |
26 | #include "linux-nat.h" | |
27 | #include "target-descriptions.h" | |
28 | #include "auxv.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "aarch64-tdep.h" | |
31 | #include "aarch64-linux-tdep.h" | |
32 | #include "elf/common.h" | |
33 | ||
34 | #include <sys/ptrace.h> | |
35 | #include <sys/utsname.h> | |
36 | ||
37 | #include "gregset.h" | |
38 | ||
39 | #include "features/aarch64.c" | |
40 | ||
41 | /* Defines ps_err_e, struct ps_prochandle. */ | |
42 | #include "gdb_proc_service.h" | |
43 | ||
44 | #ifndef TRAP_HWBKPT | |
45 | #define TRAP_HWBKPT 0x0004 | |
46 | #endif | |
47 | ||
48 | /* On GNU/Linux, threads are implemented as pseudo-processes, in which | |
49 | case we may be tracing more than one process at a time. In that | |
50 | case, inferior_ptid will contain the main process ID and the | |
51 | individual thread (process) ID. get_thread_id () is used to get | |
52 | the thread id if it's available, and the process id otherwise. */ | |
53 | ||
54 | static int | |
55 | get_thread_id (ptid_t ptid) | |
56 | { | |
dfd4cc63 | 57 | int tid = ptid_get_lwp (ptid); |
9d19df75 MS |
58 | |
59 | if (0 == tid) | |
dfd4cc63 | 60 | tid = ptid_get_pid (ptid); |
9d19df75 MS |
61 | return tid; |
62 | } | |
63 | ||
64 | /* Macro definitions, data structures, and code for the hardware | |
65 | breakpoint and hardware watchpoint support follow. We use the | |
66 | following abbreviations throughout the code: | |
67 | ||
68 | hw - hardware | |
69 | bp - breakpoint | |
70 | wp - watchpoint */ | |
71 | ||
72 | /* Maximum number of hardware breakpoint and watchpoint registers. | |
73 | Neither of these values may exceed the width of dr_changed_t | |
74 | measured in bits. */ | |
75 | ||
76 | #define AARCH64_HBP_MAX_NUM 16 | |
77 | #define AARCH64_HWP_MAX_NUM 16 | |
78 | ||
79 | /* Alignment requirement in bytes for addresses written to | |
80 | hardware breakpoint and watchpoint value registers. | |
81 | ||
82 | A ptrace call attempting to set an address that does not meet the | |
83 | alignment criteria will fail. Limited support has been provided in | |
84 | this port for unaligned watchpoints, such that from a GDB user | |
85 | perspective, an unaligned watchpoint may be requested. | |
86 | ||
87 | This is achieved by minimally enlarging the watched area to meet the | |
88 | alignment requirement, and if necessary, splitting the watchpoint | |
89 | over several hardware watchpoint registers. */ | |
90 | ||
91 | #define AARCH64_HBP_ALIGNMENT 4 | |
92 | #define AARCH64_HWP_ALIGNMENT 8 | |
93 | ||
94 | /* The maximum length of a memory region that can be watched by one | |
95 | hardware watchpoint register. */ | |
96 | ||
97 | #define AARCH64_HWP_MAX_LEN_PER_REG 8 | |
98 | ||
99 | /* ptrace hardware breakpoint resource info is formatted as follows: | |
100 | ||
101 | 31 24 16 8 0 | |
102 | +---------------+--------------+---------------+---------------+ | |
103 | | RESERVED | RESERVED | DEBUG_ARCH | NUM_SLOTS | | |
104 | +---------------+--------------+---------------+---------------+ */ | |
105 | ||
106 | ||
107 | /* Macros to extract fields from the hardware debug information word. */ | |
108 | #define AARCH64_DEBUG_NUM_SLOTS(x) ((x) & 0xff) | |
109 | #define AARCH64_DEBUG_ARCH(x) (((x) >> 8) & 0xff) | |
110 | ||
111 | /* Macro for the expected version of the ARMv8-A debug architecture. */ | |
112 | #define AARCH64_DEBUG_ARCH_V8 0x6 | |
113 | ||
114 | /* Number of hardware breakpoints/watchpoints the target supports. | |
115 | They are initialized with values obtained via the ptrace calls | |
116 | with NT_ARM_HW_BREAK and NT_ARM_HW_WATCH respectively. */ | |
117 | ||
118 | static int aarch64_num_bp_regs; | |
119 | static int aarch64_num_wp_regs; | |
120 | ||
121 | /* Debugging of hardware breakpoint/watchpoint support. */ | |
122 | ||
123 | static int debug_hw_points; | |
124 | ||
125 | /* Each bit of a variable of this type is used to indicate whether a | |
126 | hardware breakpoint or watchpoint setting has been changed since | |
127 | the last update. | |
128 | ||
129 | Bit N corresponds to the Nth hardware breakpoint or watchpoint | |
130 | setting which is managed in aarch64_debug_reg_state, where N is | |
131 | valid between 0 and the total number of the hardware breakpoint or | |
132 | watchpoint debug registers minus 1. | |
133 | ||
134 | When bit N is 1, the corresponding breakpoint or watchpoint setting | |
135 | has changed, and therefore the corresponding hardware debug | |
136 | register needs to be updated via the ptrace interface. | |
137 | ||
138 | In the per-thread arch-specific data area, we define two such | |
139 | variables for per-thread hardware breakpoint and watchpoint | |
140 | settings respectively. | |
141 | ||
142 | This type is part of the mechanism which helps reduce the number of | |
143 | ptrace calls to the kernel, i.e. avoid asking the kernel to write | |
144 | to the debug registers with unchanged values. */ | |
145 | ||
6eb04473 | 146 | typedef unsigned LONGEST dr_changed_t; |
9d19df75 MS |
147 | |
148 | /* Set each of the lower M bits of X to 1; assert X is wide enough. */ | |
149 | ||
150 | #define DR_MARK_ALL_CHANGED(x, m) \ | |
151 | do \ | |
152 | { \ | |
153 | gdb_assert (sizeof ((x)) * 8 >= (m)); \ | |
154 | (x) = (((dr_changed_t)1 << (m)) - 1); \ | |
155 | } while (0) | |
156 | ||
157 | #define DR_MARK_N_CHANGED(x, n) \ | |
158 | do \ | |
159 | { \ | |
160 | (x) |= ((dr_changed_t)1 << (n)); \ | |
161 | } while (0) | |
162 | ||
163 | #define DR_CLEAR_CHANGED(x) \ | |
164 | do \ | |
165 | { \ | |
166 | (x) = 0; \ | |
167 | } while (0) | |
168 | ||
169 | #define DR_HAS_CHANGED(x) ((x) != 0) | |
170 | #define DR_N_HAS_CHANGED(x, n) ((x) & ((dr_changed_t)1 << (n))) | |
171 | ||
172 | /* Structure for managing the hardware breakpoint/watchpoint resources. | |
173 | DR_ADDR_* stores the address, DR_CTRL_* stores the control register | |
174 | content, and DR_REF_COUNT_* counts the numbers of references to the | |
175 | corresponding bp/wp, by which way the limited hardware resources | |
176 | are not wasted on duplicated bp/wp settings (though so far gdb has | |
177 | done a good job by not sending duplicated bp/wp requests). */ | |
178 | ||
179 | struct aarch64_debug_reg_state | |
180 | { | |
181 | /* hardware breakpoint */ | |
182 | CORE_ADDR dr_addr_bp[AARCH64_HBP_MAX_NUM]; | |
183 | unsigned int dr_ctrl_bp[AARCH64_HBP_MAX_NUM]; | |
184 | unsigned int dr_ref_count_bp[AARCH64_HBP_MAX_NUM]; | |
185 | ||
186 | /* hardware watchpoint */ | |
187 | CORE_ADDR dr_addr_wp[AARCH64_HWP_MAX_NUM]; | |
188 | unsigned int dr_ctrl_wp[AARCH64_HWP_MAX_NUM]; | |
189 | unsigned int dr_ref_count_wp[AARCH64_HWP_MAX_NUM]; | |
190 | }; | |
191 | ||
d6c44983 YZ |
192 | /* Per-process data. We don't bind this to a per-inferior registry |
193 | because of targets like x86 GNU/Linux that need to keep track of | |
194 | processes that aren't bound to any inferior (e.g., fork children, | |
195 | checkpoints). */ | |
9d19df75 | 196 | |
d6c44983 | 197 | struct aarch64_process_info |
9d19df75 | 198 | { |
d6c44983 YZ |
199 | /* Linked list. */ |
200 | struct aarch64_process_info *next; | |
9d19df75 | 201 | |
d6c44983 YZ |
202 | /* The process identifier. */ |
203 | pid_t pid; | |
9d19df75 | 204 | |
d6c44983 YZ |
205 | /* Copy of aarch64 hardware debug registers. */ |
206 | struct aarch64_debug_reg_state state; | |
207 | }; | |
208 | ||
209 | static struct aarch64_process_info *aarch64_process_list = NULL; | |
210 | ||
211 | /* Find process data for process PID. */ | |
212 | ||
213 | static struct aarch64_process_info * | |
214 | aarch64_find_process_pid (pid_t pid) | |
215 | { | |
216 | struct aarch64_process_info *proc; | |
217 | ||
218 | for (proc = aarch64_process_list; proc; proc = proc->next) | |
219 | if (proc->pid == pid) | |
220 | return proc; | |
221 | ||
222 | return NULL; | |
9d19df75 MS |
223 | } |
224 | ||
d6c44983 YZ |
225 | /* Add process data for process PID. Returns newly allocated info |
226 | object. */ | |
9d19df75 | 227 | |
d6c44983 YZ |
228 | static struct aarch64_process_info * |
229 | aarch64_add_process (pid_t pid) | |
9d19df75 | 230 | { |
d6c44983 | 231 | struct aarch64_process_info *proc; |
9d19df75 | 232 | |
d6c44983 YZ |
233 | proc = xcalloc (1, sizeof (*proc)); |
234 | proc->pid = pid; | |
9d19df75 | 235 | |
d6c44983 YZ |
236 | proc->next = aarch64_process_list; |
237 | aarch64_process_list = proc; | |
238 | ||
239 | return proc; | |
240 | } | |
241 | ||
242 | /* Get data specific info for process PID, creating it if necessary. | |
243 | Never returns NULL. */ | |
244 | ||
245 | static struct aarch64_process_info * | |
246 | aarch64_process_info_get (pid_t pid) | |
9d19df75 | 247 | { |
d6c44983 YZ |
248 | struct aarch64_process_info *proc; |
249 | ||
250 | proc = aarch64_find_process_pid (pid); | |
251 | if (proc == NULL) | |
252 | proc = aarch64_add_process (pid); | |
9d19df75 | 253 | |
d6c44983 | 254 | return proc; |
9d19df75 MS |
255 | } |
256 | ||
d6c44983 YZ |
257 | /* Called whenever GDB is no longer debugging process PID. It deletes |
258 | data structures that keep track of debug register state. */ | |
9d19df75 | 259 | |
d6c44983 YZ |
260 | static void |
261 | aarch64_forget_process (pid_t pid) | |
9d19df75 | 262 | { |
d6c44983 | 263 | struct aarch64_process_info *proc, **proc_link; |
9d19df75 | 264 | |
d6c44983 YZ |
265 | proc = aarch64_process_list; |
266 | proc_link = &aarch64_process_list; | |
267 | ||
268 | while (proc != NULL) | |
9d19df75 | 269 | { |
d6c44983 YZ |
270 | if (proc->pid == pid) |
271 | { | |
272 | *proc_link = proc->next; | |
9d19df75 | 273 | |
d6c44983 YZ |
274 | xfree (proc); |
275 | return; | |
276 | } | |
277 | ||
278 | proc_link = &proc->next; | |
279 | proc = *proc_link; | |
280 | } | |
9d19df75 MS |
281 | } |
282 | ||
d6c44983 | 283 | /* Get debug registers state for process PID. */ |
9d19df75 MS |
284 | |
285 | static struct aarch64_debug_reg_state * | |
d6c44983 | 286 | aarch64_get_debug_reg_state (pid_t pid) |
9d19df75 | 287 | { |
d6c44983 | 288 | return &aarch64_process_info_get (pid)->state; |
9d19df75 MS |
289 | } |
290 | ||
291 | /* Per-thread arch-specific data we want to keep. */ | |
292 | ||
293 | struct arch_lwp_info | |
294 | { | |
295 | /* When bit N is 1, it indicates the Nth hardware breakpoint or | |
296 | watchpoint register pair needs to be updated when the thread is | |
297 | resumed; see aarch64_linux_prepare_to_resume. */ | |
298 | dr_changed_t dr_changed_bp; | |
299 | dr_changed_t dr_changed_wp; | |
300 | }; | |
301 | ||
302 | /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint | |
303 | registers with data from *STATE. */ | |
304 | ||
305 | static void | |
306 | aarch64_linux_set_debug_regs (const struct aarch64_debug_reg_state *state, | |
307 | int tid, int watchpoint) | |
308 | { | |
309 | int i, count; | |
310 | struct iovec iov; | |
311 | struct user_hwdebug_state regs; | |
312 | const CORE_ADDR *addr; | |
313 | const unsigned int *ctrl; | |
314 | ||
1aa4cd77 | 315 | memset (®s, 0, sizeof (regs)); |
9d19df75 MS |
316 | iov.iov_base = ®s; |
317 | iov.iov_len = sizeof (regs); | |
318 | count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs; | |
319 | addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp; | |
320 | ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp; | |
321 | ||
322 | for (i = 0; i < count; i++) | |
323 | { | |
324 | regs.dbg_regs[i].addr = addr[i]; | |
325 | regs.dbg_regs[i].ctrl = ctrl[i]; | |
326 | } | |
327 | ||
328 | if (ptrace (PTRACE_SETREGSET, tid, | |
329 | watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK, | |
330 | (void *) &iov)) | |
331 | error (_("Unexpected error setting hardware debug registers")); | |
332 | } | |
333 | ||
334 | struct aarch64_dr_update_callback_param | |
335 | { | |
336 | int is_watchpoint; | |
337 | unsigned int idx; | |
338 | }; | |
339 | ||
d6c44983 | 340 | /* Callback for iterate_over_lwps. Records the |
9d19df75 MS |
341 | information about the change of one hardware breakpoint/watchpoint |
342 | setting for the thread LWP. | |
343 | The information is passed in via PTR. | |
344 | N.B. The actual updating of hardware debug registers is not | |
345 | carried out until the moment the thread is resumed. */ | |
346 | ||
347 | static int | |
348 | debug_reg_change_callback (struct lwp_info *lwp, void *ptr) | |
349 | { | |
350 | struct aarch64_dr_update_callback_param *param_p | |
351 | = (struct aarch64_dr_update_callback_param *) ptr; | |
352 | int pid = get_thread_id (lwp->ptid); | |
353 | int idx = param_p->idx; | |
354 | int is_watchpoint = param_p->is_watchpoint; | |
355 | struct arch_lwp_info *info = lwp->arch_private; | |
356 | dr_changed_t *dr_changed_ptr; | |
357 | dr_changed_t dr_changed; | |
358 | ||
359 | if (info == NULL) | |
360 | info = lwp->arch_private = XCNEW (struct arch_lwp_info); | |
361 | ||
362 | if (debug_hw_points) | |
363 | { | |
364 | fprintf_unfiltered (gdb_stdlog, | |
365 | "debug_reg_change_callback: \n\tOn entry:\n"); | |
366 | fprintf_unfiltered (gdb_stdlog, | |
1d3ffd6b MS |
367 | "\tpid%d, dr_changed_bp=0x%s, " |
368 | "dr_changed_wp=0x%s\n", | |
369 | pid, phex (info->dr_changed_bp, 8), | |
370 | phex (info->dr_changed_wp, 8)); | |
9d19df75 MS |
371 | } |
372 | ||
373 | dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp | |
374 | : &info->dr_changed_bp; | |
375 | dr_changed = *dr_changed_ptr; | |
376 | ||
377 | gdb_assert (idx >= 0 | |
378 | && (idx <= (is_watchpoint ? aarch64_num_wp_regs | |
379 | : aarch64_num_bp_regs))); | |
380 | ||
381 | /* The actual update is done later just before resuming the lwp, | |
382 | we just mark that one register pair needs updating. */ | |
383 | DR_MARK_N_CHANGED (dr_changed, idx); | |
384 | *dr_changed_ptr = dr_changed; | |
385 | ||
386 | /* If the lwp isn't stopped, force it to momentarily pause, so | |
387 | we can update its debug registers. */ | |
388 | if (!lwp->stopped) | |
389 | linux_stop_lwp (lwp); | |
390 | ||
391 | if (debug_hw_points) | |
392 | { | |
393 | fprintf_unfiltered (gdb_stdlog, | |
1d3ffd6b MS |
394 | "\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, " |
395 | "dr_changed_wp=0x%s\n", | |
396 | pid, phex (info->dr_changed_bp, 8), | |
397 | phex (info->dr_changed_wp, 8)); | |
9d19df75 MS |
398 | } |
399 | ||
400 | /* Continue the iteration. */ | |
401 | return 0; | |
402 | } | |
403 | ||
404 | /* Notify each thread that their IDXth breakpoint/watchpoint register | |
405 | pair needs to be updated. The message will be recorded in each | |
406 | thread's arch-specific data area, the actual updating will be done | |
407 | when the thread is resumed. */ | |
408 | ||
409 | static void | |
410 | aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state, | |
411 | int is_watchpoint, unsigned int idx) | |
412 | { | |
413 | struct aarch64_dr_update_callback_param param; | |
d6c44983 | 414 | ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid)); |
9d19df75 MS |
415 | |
416 | param.is_watchpoint = is_watchpoint; | |
417 | param.idx = idx; | |
418 | ||
d6c44983 | 419 | iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) ¶m); |
9d19df75 MS |
420 | } |
421 | ||
422 | /* Print the values of the cached breakpoint/watchpoint registers. */ | |
423 | ||
424 | static void | |
425 | aarch64_show_debug_reg_state (struct aarch64_debug_reg_state *state, | |
426 | const char *func, CORE_ADDR addr, | |
427 | int len, int type) | |
428 | { | |
429 | int i; | |
430 | ||
431 | fprintf_unfiltered (gdb_stdlog, "%s", func); | |
432 | if (addr || len) | |
433 | fprintf_unfiltered (gdb_stdlog, " (addr=0x%08lx, len=%d, type=%s)", | |
434 | (unsigned long) addr, len, | |
435 | type == hw_write ? "hw-write-watchpoint" | |
436 | : (type == hw_read ? "hw-read-watchpoint" | |
437 | : (type == hw_access ? "hw-access-watchpoint" | |
438 | : (type == hw_execute ? "hw-breakpoint" | |
439 | : "??unknown??")))); | |
440 | fprintf_unfiltered (gdb_stdlog, ":\n"); | |
441 | ||
442 | fprintf_unfiltered (gdb_stdlog, "\tBREAKPOINTs:\n"); | |
443 | for (i = 0; i < aarch64_num_bp_regs; i++) | |
444 | fprintf_unfiltered (gdb_stdlog, | |
445 | "\tBP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n", | |
446 | i, state->dr_addr_bp[i], | |
447 | state->dr_ctrl_bp[i], state->dr_ref_count_bp[i]); | |
448 | ||
449 | fprintf_unfiltered (gdb_stdlog, "\tWATCHPOINTs:\n"); | |
450 | for (i = 0; i < aarch64_num_wp_regs; i++) | |
451 | fprintf_unfiltered (gdb_stdlog, | |
452 | "\tWP%d: addr=0x%08lx, ctrl=0x%08x, ref.count=%d\n", | |
453 | i, state->dr_addr_wp[i], | |
454 | state->dr_ctrl_wp[i], state->dr_ref_count_wp[i]); | |
455 | } | |
456 | ||
457 | /* Fill GDB's register array with the general-purpose register values | |
458 | from the current thread. */ | |
459 | ||
460 | static void | |
461 | fetch_gregs_from_thread (struct regcache *regcache) | |
462 | { | |
463 | int ret, regno, tid; | |
464 | elf_gregset_t regs; | |
465 | struct iovec iovec; | |
466 | ||
467 | tid = get_thread_id (inferior_ptid); | |
468 | ||
469 | iovec.iov_base = ®s; | |
470 | iovec.iov_len = sizeof (regs); | |
471 | ||
472 | ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec); | |
473 | if (ret < 0) | |
474 | perror_with_name (_("Unable to fetch general registers.")); | |
475 | ||
476 | for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++) | |
477 | regcache_raw_supply (regcache, regno, | |
478 | (char *) ®s[regno - AARCH64_X0_REGNUM]); | |
479 | } | |
480 | ||
481 | /* Store to the current thread the valid general-purpose register | |
482 | values in the GDB's register array. */ | |
483 | ||
484 | static void | |
485 | store_gregs_to_thread (const struct regcache *regcache) | |
486 | { | |
487 | int ret, regno, tid; | |
488 | elf_gregset_t regs; | |
489 | struct iovec iovec; | |
490 | ||
491 | tid = get_thread_id (inferior_ptid); | |
492 | ||
493 | iovec.iov_base = ®s; | |
494 | iovec.iov_len = sizeof (regs); | |
495 | ||
496 | ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec); | |
497 | if (ret < 0) | |
498 | perror_with_name (_("Unable to fetch general registers.")); | |
499 | ||
500 | for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++) | |
501 | if (REG_VALID == regcache_register_status (regcache, regno)) | |
502 | regcache_raw_collect (regcache, regno, | |
503 | (char *) ®s[regno - AARCH64_X0_REGNUM]); | |
504 | ||
505 | ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec); | |
506 | if (ret < 0) | |
507 | perror_with_name (_("Unable to store general registers.")); | |
508 | } | |
509 | ||
510 | /* Fill GDB's register array with the fp/simd register values | |
511 | from the current thread. */ | |
512 | ||
513 | static void | |
514 | fetch_fpregs_from_thread (struct regcache *regcache) | |
515 | { | |
516 | int ret, regno, tid; | |
517 | elf_fpregset_t regs; | |
518 | struct iovec iovec; | |
519 | ||
520 | tid = get_thread_id (inferior_ptid); | |
521 | ||
522 | iovec.iov_base = ®s; | |
523 | iovec.iov_len = sizeof (regs); | |
524 | ||
525 | ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec); | |
526 | if (ret < 0) | |
527 | perror_with_name (_("Unable to fetch FP/SIMD registers.")); | |
528 | ||
529 | for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++) | |
530 | regcache_raw_supply (regcache, regno, | |
531 | (char *) ®s.vregs[regno - AARCH64_V0_REGNUM]); | |
532 | ||
533 | regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, (char *) ®s.fpsr); | |
534 | regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, (char *) ®s.fpcr); | |
535 | } | |
536 | ||
537 | /* Store to the current thread the valid fp/simd register | |
538 | values in the GDB's register array. */ | |
539 | ||
540 | static void | |
541 | store_fpregs_to_thread (const struct regcache *regcache) | |
542 | { | |
543 | int ret, regno, tid; | |
544 | elf_fpregset_t regs; | |
545 | struct iovec iovec; | |
546 | ||
547 | tid = get_thread_id (inferior_ptid); | |
548 | ||
549 | iovec.iov_base = ®s; | |
550 | iovec.iov_len = sizeof (regs); | |
551 | ||
552 | ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec); | |
553 | if (ret < 0) | |
554 | perror_with_name (_("Unable to fetch FP/SIMD registers.")); | |
555 | ||
556 | for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++) | |
557 | if (REG_VALID == regcache_register_status (regcache, regno)) | |
558 | regcache_raw_collect (regcache, regno, | |
559 | (char *) ®s.vregs[regno - AARCH64_V0_REGNUM]); | |
560 | ||
561 | if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM)) | |
562 | regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM, (char *) ®s.fpsr); | |
563 | if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM)) | |
564 | regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM, (char *) ®s.fpcr); | |
565 | ||
566 | ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec); | |
567 | if (ret < 0) | |
568 | perror_with_name (_("Unable to store FP/SIMD registers.")); | |
569 | } | |
570 | ||
571 | /* Implement the "to_fetch_register" target_ops method. */ | |
572 | ||
573 | static void | |
574 | aarch64_linux_fetch_inferior_registers (struct target_ops *ops, | |
575 | struct regcache *regcache, | |
576 | int regno) | |
577 | { | |
578 | if (regno == -1) | |
579 | { | |
580 | fetch_gregs_from_thread (regcache); | |
581 | fetch_fpregs_from_thread (regcache); | |
582 | } | |
583 | else if (regno < AARCH64_V0_REGNUM) | |
584 | fetch_gregs_from_thread (regcache); | |
585 | else | |
586 | fetch_fpregs_from_thread (regcache); | |
587 | } | |
588 | ||
589 | /* Implement the "to_store_register" target_ops method. */ | |
590 | ||
591 | static void | |
592 | aarch64_linux_store_inferior_registers (struct target_ops *ops, | |
593 | struct regcache *regcache, | |
594 | int regno) | |
595 | { | |
596 | if (regno == -1) | |
597 | { | |
598 | store_gregs_to_thread (regcache); | |
599 | store_fpregs_to_thread (regcache); | |
600 | } | |
601 | else if (regno < AARCH64_V0_REGNUM) | |
602 | store_gregs_to_thread (regcache); | |
603 | else | |
604 | store_fpregs_to_thread (regcache); | |
605 | } | |
606 | ||
607 | /* Fill register REGNO (if it is a general-purpose register) in | |
608 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
609 | do this for all registers. */ | |
610 | ||
611 | void | |
612 | fill_gregset (const struct regcache *regcache, | |
613 | gdb_gregset_t *gregsetp, int regno) | |
614 | { | |
615 | gdb_byte *gregs_buf = (gdb_byte *) gregsetp; | |
616 | int i; | |
617 | ||
618 | for (i = AARCH64_X0_REGNUM; i <= AARCH64_CPSR_REGNUM; i++) | |
619 | if (regno == -1 || regno == i) | |
620 | regcache_raw_collect (regcache, i, | |
621 | gregs_buf + X_REGISTER_SIZE | |
622 | * (i - AARCH64_X0_REGNUM)); | |
623 | } | |
624 | ||
625 | /* Fill GDB's register array with the general-purpose register values | |
626 | in *GREGSETP. */ | |
627 | ||
628 | void | |
629 | supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp) | |
630 | { | |
631 | aarch64_linux_supply_gregset (regcache, (const gdb_byte *) gregsetp); | |
632 | } | |
633 | ||
634 | /* Fill register REGNO (if it is a floating-point register) in | |
635 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
636 | do this for all registers. */ | |
637 | ||
638 | void | |
639 | fill_fpregset (const struct regcache *regcache, | |
640 | gdb_fpregset_t *fpregsetp, int regno) | |
641 | { | |
642 | gdb_byte *fpregs_buf = (gdb_byte *) fpregsetp; | |
643 | int i; | |
644 | ||
645 | for (i = AARCH64_V0_REGNUM; i <= AARCH64_V31_REGNUM; i++) | |
646 | if (regno == -1 || regno == i) | |
647 | regcache_raw_collect (regcache, i, | |
648 | fpregs_buf + V_REGISTER_SIZE | |
649 | * (i - AARCH64_V0_REGNUM)); | |
650 | ||
651 | if (regno == -1 || regno == AARCH64_FPSR_REGNUM) | |
652 | regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM, | |
653 | fpregs_buf + V_REGISTER_SIZE * 32); | |
654 | ||
655 | if (regno == -1 || regno == AARCH64_FPCR_REGNUM) | |
656 | regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM, | |
657 | fpregs_buf + V_REGISTER_SIZE * 32 + 4); | |
658 | } | |
659 | ||
660 | /* Fill GDB's register array with the floating-point register values | |
661 | in *FPREGSETP. */ | |
662 | ||
663 | void | |
664 | supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp) | |
665 | { | |
666 | aarch64_linux_supply_fpregset (regcache, (const gdb_byte *) fpregsetp); | |
667 | } | |
668 | ||
669 | /* Called when resuming a thread. | |
670 | The hardware debug registers are updated when there is any change. */ | |
671 | ||
672 | static void | |
673 | aarch64_linux_prepare_to_resume (struct lwp_info *lwp) | |
674 | { | |
675 | struct arch_lwp_info *info = lwp->arch_private; | |
676 | ||
677 | /* NULL means this is the main thread still going through the shell, | |
678 | or, no watchpoint has been set yet. In that case, there's | |
679 | nothing to do. */ | |
680 | if (info == NULL) | |
681 | return; | |
682 | ||
683 | if (DR_HAS_CHANGED (info->dr_changed_bp) | |
684 | || DR_HAS_CHANGED (info->dr_changed_wp)) | |
685 | { | |
dfd4cc63 | 686 | int tid = ptid_get_lwp (lwp->ptid); |
d6c44983 YZ |
687 | struct aarch64_debug_reg_state *state |
688 | = aarch64_get_debug_reg_state (ptid_get_pid (lwp->ptid)); | |
9d19df75 MS |
689 | |
690 | if (debug_hw_points) | |
691 | fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid); | |
692 | ||
693 | /* Watchpoints. */ | |
694 | if (DR_HAS_CHANGED (info->dr_changed_wp)) | |
695 | { | |
696 | aarch64_linux_set_debug_regs (state, tid, 1); | |
697 | DR_CLEAR_CHANGED (info->dr_changed_wp); | |
698 | } | |
699 | ||
700 | /* Breakpoints. */ | |
701 | if (DR_HAS_CHANGED (info->dr_changed_bp)) | |
702 | { | |
703 | aarch64_linux_set_debug_regs (state, tid, 0); | |
704 | DR_CLEAR_CHANGED (info->dr_changed_bp); | |
705 | } | |
706 | } | |
707 | } | |
708 | ||
709 | static void | |
710 | aarch64_linux_new_thread (struct lwp_info *lp) | |
711 | { | |
712 | struct arch_lwp_info *info = XCNEW (struct arch_lwp_info); | |
713 | ||
714 | /* Mark that all the hardware breakpoint/watchpoint register pairs | |
715 | for this thread need to be initialized. */ | |
716 | DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs); | |
717 | DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs); | |
718 | ||
719 | lp->arch_private = info; | |
720 | } | |
d6c44983 YZ |
721 | |
722 | /* linux_nat_new_fork hook. */ | |
723 | ||
724 | static void | |
725 | aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid) | |
726 | { | |
727 | pid_t parent_pid; | |
728 | struct aarch64_debug_reg_state *parent_state; | |
729 | struct aarch64_debug_reg_state *child_state; | |
730 | ||
731 | /* NULL means no watchpoint has ever been set in the parent. In | |
732 | that case, there's nothing to do. */ | |
733 | if (parent->arch_private == NULL) | |
734 | return; | |
735 | ||
736 | /* GDB core assumes the child inherits the watchpoints/hw | |
737 | breakpoints of the parent, and will remove them all from the | |
738 | forked off process. Copy the debug registers mirrors into the | |
739 | new process so that all breakpoints and watchpoints can be | |
740 | removed together. */ | |
741 | ||
742 | parent_pid = ptid_get_pid (parent->ptid); | |
743 | parent_state = aarch64_get_debug_reg_state (parent_pid); | |
744 | child_state = aarch64_get_debug_reg_state (child_pid); | |
745 | *child_state = *parent_state; | |
746 | } | |
9d19df75 MS |
747 | \f |
748 | ||
749 | /* Called by libthread_db. Returns a pointer to the thread local | |
750 | storage (or its descriptor). */ | |
751 | ||
752 | ps_err_e | |
753 | ps_get_thread_area (const struct ps_prochandle *ph, | |
754 | lwpid_t lwpid, int idx, void **base) | |
755 | { | |
756 | struct iovec iovec; | |
757 | uint64_t reg; | |
758 | ||
759 | iovec.iov_base = ® | |
760 | iovec.iov_len = sizeof (reg); | |
761 | ||
762 | if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0) | |
763 | return PS_ERR; | |
764 | ||
765 | /* IDX is the bias from the thread pointer to the beginning of the | |
766 | thread descriptor. It has to be subtracted due to implementation | |
767 | quirks in libthread_db. */ | |
768 | *base = (void *) (reg - idx); | |
769 | ||
770 | return PS_OK; | |
771 | } | |
772 | \f | |
773 | ||
774 | /* Get the hardware debug register capacity information. */ | |
775 | ||
776 | static void | |
777 | aarch64_linux_get_debug_reg_capacity (void) | |
778 | { | |
779 | int tid; | |
780 | struct iovec iov; | |
781 | struct user_hwdebug_state dreg_state; | |
782 | ||
783 | tid = get_thread_id (inferior_ptid); | |
784 | iov.iov_base = &dreg_state; | |
785 | iov.iov_len = sizeof (dreg_state); | |
786 | ||
787 | /* Get hardware watchpoint register info. */ | |
788 | if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_WATCH, &iov) == 0 | |
789 | && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8) | |
790 | { | |
791 | aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info); | |
792 | if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM) | |
793 | { | |
1d3ffd6b MS |
794 | warning (_("Unexpected number of hardware watchpoint registers" |
795 | " reported by ptrace, got %d, expected %d."), | |
9d19df75 MS |
796 | aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM); |
797 | aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM; | |
798 | } | |
799 | } | |
800 | else | |
801 | { | |
1d3ffd6b MS |
802 | warning (_("Unable to determine the number of hardware watchpoints" |
803 | " available.")); | |
9d19df75 MS |
804 | aarch64_num_wp_regs = 0; |
805 | } | |
806 | ||
807 | /* Get hardware breakpoint register info. */ | |
808 | if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_BREAK, &iov) == 0 | |
809 | && AARCH64_DEBUG_ARCH (dreg_state.dbg_info) == AARCH64_DEBUG_ARCH_V8) | |
810 | { | |
811 | aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info); | |
812 | if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM) | |
813 | { | |
1d3ffd6b MS |
814 | warning (_("Unexpected number of hardware breakpoint registers" |
815 | " reported by ptrace, got %d, expected %d."), | |
9d19df75 MS |
816 | aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM); |
817 | aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM; | |
818 | } | |
819 | } | |
820 | else | |
821 | { | |
1d3ffd6b MS |
822 | warning (_("Unable to determine the number of hardware breakpoints" |
823 | " available.")); | |
9d19df75 MS |
824 | aarch64_num_bp_regs = 0; |
825 | } | |
826 | } | |
827 | ||
828 | static void (*super_post_startup_inferior) (ptid_t ptid); | |
829 | ||
830 | /* Implement the "to_post_startup_inferior" target_ops method. */ | |
831 | ||
832 | static void | |
833 | aarch64_linux_child_post_startup_inferior (ptid_t ptid) | |
834 | { | |
d6c44983 | 835 | aarch64_forget_process (ptid_get_pid (ptid)); |
9d19df75 MS |
836 | aarch64_linux_get_debug_reg_capacity (); |
837 | super_post_startup_inferior (ptid); | |
838 | } | |
839 | ||
840 | /* Implement the "to_read_description" target_ops method. */ | |
841 | ||
842 | static const struct target_desc * | |
843 | aarch64_linux_read_description (struct target_ops *ops) | |
844 | { | |
845 | initialize_tdesc_aarch64 (); | |
846 | return tdesc_aarch64; | |
847 | } | |
848 | ||
849 | /* Given the (potentially unaligned) watchpoint address in ADDR and | |
850 | length in LEN, return the aligned address and aligned length in | |
851 | *ALIGNED_ADDR_P and *ALIGNED_LEN_P, respectively. The returned | |
852 | aligned address and length will be valid values to write to the | |
853 | hardware watchpoint value and control registers. | |
854 | ||
855 | The given watchpoint may get truncated if more than one hardware | |
856 | register is needed to cover the watched region. *NEXT_ADDR_P | |
857 | and *NEXT_LEN_P, if non-NULL, will return the address and length | |
858 | of the remaining part of the watchpoint (which can be processed | |
859 | by calling this routine again to generate another aligned address | |
860 | and length pair. | |
861 | ||
862 | See the comment above the function of the same name in | |
863 | gdbserver/linux-aarch64-low.c for more information. */ | |
864 | ||
865 | static void | |
866 | aarch64_align_watchpoint (CORE_ADDR addr, int len, CORE_ADDR *aligned_addr_p, | |
867 | int *aligned_len_p, CORE_ADDR *next_addr_p, | |
868 | int *next_len_p) | |
869 | { | |
870 | int aligned_len; | |
871 | unsigned int offset; | |
872 | CORE_ADDR aligned_addr; | |
873 | const unsigned int alignment = AARCH64_HWP_ALIGNMENT; | |
874 | const unsigned int max_wp_len = AARCH64_HWP_MAX_LEN_PER_REG; | |
875 | ||
876 | /* As assumed by the algorithm. */ | |
877 | gdb_assert (alignment == max_wp_len); | |
878 | ||
879 | if (len <= 0) | |
880 | return; | |
881 | ||
882 | /* Address to be put into the hardware watchpoint value register | |
883 | must be aligned. */ | |
884 | offset = addr & (alignment - 1); | |
885 | aligned_addr = addr - offset; | |
886 | ||
887 | gdb_assert (offset >= 0 && offset < alignment); | |
888 | gdb_assert (aligned_addr >= 0 && aligned_addr <= addr); | |
889 | gdb_assert (offset + len > 0); | |
890 | ||
891 | if (offset + len >= max_wp_len) | |
892 | { | |
893 | /* Need more than one watchpoint registers; truncate it at the | |
894 | alignment boundary. */ | |
895 | aligned_len = max_wp_len; | |
896 | len -= (max_wp_len - offset); | |
897 | addr += (max_wp_len - offset); | |
898 | gdb_assert ((addr & (alignment - 1)) == 0); | |
899 | } | |
900 | else | |
901 | { | |
902 | /* Find the smallest valid length that is large enough to | |
903 | accommodate this watchpoint. */ | |
904 | static const unsigned char | |
905 | aligned_len_array[AARCH64_HWP_MAX_LEN_PER_REG] = | |
906 | { 1, 2, 4, 4, 8, 8, 8, 8 }; | |
907 | ||
908 | aligned_len = aligned_len_array[offset + len - 1]; | |
909 | addr += len; | |
910 | len = 0; | |
911 | } | |
912 | ||
913 | if (aligned_addr_p) | |
914 | *aligned_addr_p = aligned_addr; | |
915 | if (aligned_len_p) | |
916 | *aligned_len_p = aligned_len; | |
917 | if (next_addr_p) | |
918 | *next_addr_p = addr; | |
919 | if (next_len_p) | |
920 | *next_len_p = len; | |
921 | } | |
922 | ||
923 | /* Returns the number of hardware watchpoints of type TYPE that we can | |
924 | set. Value is positive if we can set CNT watchpoints, zero if | |
925 | setting watchpoints of type TYPE is not supported, and negative if | |
926 | CNT is more than the maximum number of watchpoints of type TYPE | |
927 | that we can support. TYPE is one of bp_hardware_watchpoint, | |
928 | bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. | |
929 | CNT is the number of such watchpoints used so far (including this | |
930 | one). OTHERTYPE is non-zero if other types of watchpoints are | |
931 | currently enabled. | |
932 | ||
933 | We always return 1 here because we don't have enough information | |
934 | about possible overlap of addresses that they want to watch. As an | |
935 | extreme example, consider the case where all the watchpoints watch | |
936 | the same address and the same region length: then we can handle a | |
937 | virtually unlimited number of watchpoints, due to debug register | |
938 | sharing implemented via reference counts. */ | |
939 | ||
940 | static int | |
941 | aarch64_linux_can_use_hw_breakpoint (int type, int cnt, int othertype) | |
942 | { | |
943 | return 1; | |
944 | } | |
945 | ||
946 | /* ptrace expects control registers to be formatted as follows: | |
947 | ||
948 | 31 13 5 3 1 0 | |
949 | +--------------------------------+----------+------+------+----+ | |
950 | | RESERVED (SBZ) | LENGTH | TYPE | PRIV | EN | | |
951 | +--------------------------------+----------+------+------+----+ | |
952 | ||
953 | The TYPE field is ignored for breakpoints. */ | |
954 | ||
955 | #define DR_CONTROL_ENABLED(ctrl) (((ctrl) & 0x1) == 1) | |
956 | #define DR_CONTROL_LENGTH(ctrl) (((ctrl) >> 5) & 0xff) | |
957 | ||
958 | /* Utility function that returns the length in bytes of a watchpoint | |
959 | according to the content of a hardware debug control register CTRL. | |
960 | Note that the kernel currently only supports the following Byte | |
961 | Address Select (BAS) values: 0x1, 0x3, 0xf and 0xff, which means | |
962 | that for a hardware watchpoint, its valid length can only be 1 | |
963 | byte, 2 bytes, 4 bytes or 8 bytes. */ | |
964 | ||
965 | static inline unsigned int | |
966 | aarch64_watchpoint_length (unsigned int ctrl) | |
967 | { | |
968 | switch (DR_CONTROL_LENGTH (ctrl)) | |
969 | { | |
970 | case 0x01: | |
971 | return 1; | |
972 | case 0x03: | |
973 | return 2; | |
974 | case 0x0f: | |
975 | return 4; | |
976 | case 0xff: | |
977 | return 8; | |
978 | default: | |
979 | return 0; | |
980 | } | |
981 | } | |
982 | ||
983 | /* Given the hardware breakpoint or watchpoint type TYPE and its | |
984 | length LEN, return the expected encoding for a hardware | |
985 | breakpoint/watchpoint control register. */ | |
986 | ||
987 | static unsigned int | |
988 | aarch64_point_encode_ctrl_reg (int type, int len) | |
989 | { | |
990 | unsigned int ctrl, ttype; | |
991 | ||
992 | /* type */ | |
993 | switch (type) | |
994 | { | |
995 | case hw_write: | |
996 | ttype = 2; | |
997 | break; | |
998 | case hw_read: | |
999 | ttype = 1; | |
1000 | break; | |
1001 | case hw_access: | |
1002 | ttype = 3; | |
1003 | break; | |
1004 | case hw_execute: | |
1005 | ttype = 0; | |
1006 | break; | |
1007 | default: | |
1008 | perror_with_name (_("Unrecognized breakpoint/watchpoint type")); | |
1009 | } | |
1010 | ctrl = ttype << 3; | |
1011 | ||
1012 | /* length bitmask */ | |
1013 | ctrl |= ((1 << len) - 1) << 5; | |
1014 | /* enabled at el0 */ | |
1015 | ctrl |= (2 << 1) | 1; | |
1016 | ||
1017 | return ctrl; | |
1018 | } | |
1019 | ||
1020 | /* Addresses to be written to the hardware breakpoint and watchpoint | |
1021 | value registers need to be aligned; the alignment is 4-byte and | |
1022 | 8-type respectively. Linux kernel rejects any non-aligned address | |
1023 | it receives from the related ptrace call. Furthermore, the kernel | |
1024 | currently only supports the following Byte Address Select (BAS) | |
1025 | values: 0x1, 0x3, 0xf and 0xff, which means that for a hardware | |
1026 | watchpoint to be accepted by the kernel (via ptrace call), its | |
1027 | valid length can only be 1 byte, 2 bytes, 4 bytes or 8 bytes. | |
1028 | Despite these limitations, the unaligned watchpoint is supported in | |
1029 | this port. | |
1030 | ||
1031 | Return 0 for any non-compliant ADDR and/or LEN; return 1 otherwise. */ | |
1032 | ||
1033 | static int | |
1034 | aarch64_point_is_aligned (int is_watchpoint, CORE_ADDR addr, int len) | |
1035 | { | |
1036 | unsigned int alignment = is_watchpoint ? AARCH64_HWP_ALIGNMENT | |
1037 | : AARCH64_HBP_ALIGNMENT; | |
1038 | ||
1039 | if (addr & (alignment - 1)) | |
1040 | return 0; | |
1041 | ||
1042 | if (len != 8 && len != 4 && len != 2 && len != 1) | |
1043 | return 0; | |
1044 | ||
1045 | return 1; | |
1046 | } | |
1047 | ||
1048 | /* Record the insertion of one breakpoint/watchpoint, as represented | |
1049 | by ADDR and CTRL, in the cached debug register state area *STATE. */ | |
1050 | ||
1051 | static int | |
1052 | aarch64_dr_state_insert_one_point (struct aarch64_debug_reg_state *state, | |
1053 | int type, CORE_ADDR addr, int len) | |
1054 | { | |
1055 | int i, idx, num_regs, is_watchpoint; | |
1056 | unsigned int ctrl, *dr_ctrl_p, *dr_ref_count; | |
1057 | CORE_ADDR *dr_addr_p; | |
1058 | ||
1059 | /* Set up state pointers. */ | |
1060 | is_watchpoint = (type != hw_execute); | |
1061 | gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len)); | |
1062 | if (is_watchpoint) | |
1063 | { | |
1064 | num_regs = aarch64_num_wp_regs; | |
1065 | dr_addr_p = state->dr_addr_wp; | |
1066 | dr_ctrl_p = state->dr_ctrl_wp; | |
1067 | dr_ref_count = state->dr_ref_count_wp; | |
1068 | } | |
1069 | else | |
1070 | { | |
1071 | num_regs = aarch64_num_bp_regs; | |
1072 | dr_addr_p = state->dr_addr_bp; | |
1073 | dr_ctrl_p = state->dr_ctrl_bp; | |
1074 | dr_ref_count = state->dr_ref_count_bp; | |
1075 | } | |
1076 | ||
1077 | ctrl = aarch64_point_encode_ctrl_reg (type, len); | |
1078 | ||
1079 | /* Find an existing or free register in our cache. */ | |
1080 | idx = -1; | |
1081 | for (i = 0; i < num_regs; ++i) | |
1082 | { | |
1083 | if ((dr_ctrl_p[i] & 1) == 0) | |
1084 | { | |
1085 | gdb_assert (dr_ref_count[i] == 0); | |
1086 | idx = i; | |
1087 | /* no break; continue hunting for an existing one. */ | |
1088 | } | |
1089 | else if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl) | |
1090 | { | |
1091 | gdb_assert (dr_ref_count[i] != 0); | |
1092 | idx = i; | |
1093 | break; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | /* No space. */ | |
1098 | if (idx == -1) | |
1099 | return -1; | |
1100 | ||
1101 | /* Update our cache. */ | |
1102 | if ((dr_ctrl_p[idx] & 1) == 0) | |
1103 | { | |
1104 | /* new entry */ | |
1105 | dr_addr_p[idx] = addr; | |
1106 | dr_ctrl_p[idx] = ctrl; | |
1107 | dr_ref_count[idx] = 1; | |
1108 | /* Notify the change. */ | |
1109 | aarch64_notify_debug_reg_change (state, is_watchpoint, idx); | |
1110 | } | |
1111 | else | |
1112 | { | |
1113 | /* existing entry */ | |
1114 | dr_ref_count[idx]++; | |
1115 | } | |
1116 | ||
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | /* Record the removal of one breakpoint/watchpoint, as represented by | |
1121 | ADDR and CTRL, in the cached debug register state area *STATE. */ | |
1122 | ||
1123 | static int | |
1124 | aarch64_dr_state_remove_one_point (struct aarch64_debug_reg_state *state, | |
1125 | int type, CORE_ADDR addr, int len) | |
1126 | { | |
1127 | int i, num_regs, is_watchpoint; | |
1128 | unsigned int ctrl, *dr_ctrl_p, *dr_ref_count; | |
1129 | CORE_ADDR *dr_addr_p; | |
1130 | ||
1131 | /* Set up state pointers. */ | |
1132 | is_watchpoint = (type != hw_execute); | |
1133 | gdb_assert (aarch64_point_is_aligned (is_watchpoint, addr, len)); | |
1134 | if (is_watchpoint) | |
1135 | { | |
1136 | num_regs = aarch64_num_wp_regs; | |
1137 | dr_addr_p = state->dr_addr_wp; | |
1138 | dr_ctrl_p = state->dr_ctrl_wp; | |
1139 | dr_ref_count = state->dr_ref_count_wp; | |
1140 | } | |
1141 | else | |
1142 | { | |
1143 | num_regs = aarch64_num_bp_regs; | |
1144 | dr_addr_p = state->dr_addr_bp; | |
1145 | dr_ctrl_p = state->dr_ctrl_bp; | |
1146 | dr_ref_count = state->dr_ref_count_bp; | |
1147 | } | |
1148 | ||
1149 | ctrl = aarch64_point_encode_ctrl_reg (type, len); | |
1150 | ||
1151 | /* Find the entry that matches the ADDR and CTRL. */ | |
1152 | for (i = 0; i < num_regs; ++i) | |
1153 | if (dr_addr_p[i] == addr && dr_ctrl_p[i] == ctrl) | |
1154 | { | |
1155 | gdb_assert (dr_ref_count[i] != 0); | |
1156 | break; | |
1157 | } | |
1158 | ||
1159 | /* Not found. */ | |
1160 | if (i == num_regs) | |
1161 | return -1; | |
1162 | ||
1163 | /* Clear our cache. */ | |
1164 | if (--dr_ref_count[i] == 0) | |
1165 | { | |
1166 | /* Clear the enable bit. */ | |
1167 | ctrl &= ~1; | |
1168 | dr_addr_p[i] = 0; | |
1169 | dr_ctrl_p[i] = ctrl; | |
1170 | /* Notify the change. */ | |
1171 | aarch64_notify_debug_reg_change (state, is_watchpoint, i); | |
1172 | } | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | /* Implement insertion and removal of a single breakpoint. */ | |
1178 | ||
1179 | static int | |
1180 | aarch64_handle_breakpoint (int type, CORE_ADDR addr, int len, int is_insert) | |
1181 | { | |
1182 | struct aarch64_debug_reg_state *state; | |
1183 | ||
1184 | /* The hardware breakpoint on AArch64 should always be 4-byte | |
1185 | aligned. */ | |
1186 | if (!aarch64_point_is_aligned (0 /* is_watchpoint */ , addr, len)) | |
1187 | return -1; | |
1188 | ||
d6c44983 | 1189 | state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
9d19df75 MS |
1190 | |
1191 | if (is_insert) | |
1192 | return aarch64_dr_state_insert_one_point (state, type, addr, len); | |
1193 | else | |
1194 | return aarch64_dr_state_remove_one_point (state, type, addr, len); | |
1195 | } | |
1196 | ||
1197 | /* Insert a hardware-assisted breakpoint at BP_TGT->placed_address. | |
1198 | Return 0 on success, -1 on failure. */ | |
1199 | ||
1200 | static int | |
1201 | aarch64_linux_insert_hw_breakpoint (struct gdbarch *gdbarch, | |
1202 | struct bp_target_info *bp_tgt) | |
1203 | { | |
1204 | int ret; | |
1205 | CORE_ADDR addr = bp_tgt->placed_address; | |
1206 | const int len = 4; | |
1207 | const int type = hw_execute; | |
1208 | ||
1209 | if (debug_hw_points) | |
1210 | fprintf_unfiltered | |
1211 | (gdb_stdlog, | |
1212 | "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n", | |
1213 | (unsigned long) addr, len); | |
1214 | ||
1215 | ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */); | |
1216 | ||
1217 | if (debug_hw_points > 1) | |
d6c44983 YZ |
1218 | { |
1219 | struct aarch64_debug_reg_state *state | |
1220 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
1221 | ||
1222 | aarch64_show_debug_reg_state (state, | |
1223 | "insert_hw_watchpoint", addr, len, type); | |
1224 | } | |
9d19df75 MS |
1225 | |
1226 | return ret; | |
1227 | } | |
1228 | ||
1229 | /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. | |
1230 | Return 0 on success, -1 on failure. */ | |
1231 | ||
1232 | static int | |
1233 | aarch64_linux_remove_hw_breakpoint (struct gdbarch *gdbarch, | |
1234 | struct bp_target_info *bp_tgt) | |
1235 | { | |
1236 | int ret; | |
1237 | CORE_ADDR addr = bp_tgt->placed_address; | |
1238 | const int len = 4; | |
1239 | const int type = hw_execute; | |
1240 | ||
1241 | if (debug_hw_points) | |
1242 | fprintf_unfiltered | |
1243 | (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n", | |
1244 | (unsigned long) addr, len); | |
1245 | ||
1246 | ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */); | |
1247 | ||
1248 | if (debug_hw_points > 1) | |
d6c44983 YZ |
1249 | { |
1250 | struct aarch64_debug_reg_state *state | |
1251 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
1252 | ||
1253 | aarch64_show_debug_reg_state (state, | |
1254 | "remove_hw_watchpoint", addr, len, type); | |
1255 | } | |
9d19df75 MS |
1256 | |
1257 | return ret; | |
1258 | } | |
1259 | ||
1260 | /* This is essentially the same as aarch64_handle_breakpoint, apart | |
1261 | from that it is an aligned watchpoint to be handled. */ | |
1262 | ||
1263 | static int | |
1264 | aarch64_handle_aligned_watchpoint (int type, CORE_ADDR addr, int len, | |
1265 | int is_insert) | |
1266 | { | |
d6c44983 YZ |
1267 | struct aarch64_debug_reg_state *state |
1268 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
9d19df75 MS |
1269 | |
1270 | if (is_insert) | |
1271 | return aarch64_dr_state_insert_one_point (state, type, addr, len); | |
1272 | else | |
1273 | return aarch64_dr_state_remove_one_point (state, type, addr, len); | |
1274 | } | |
1275 | ||
1276 | /* Insert/remove unaligned watchpoint by calling | |
1277 | aarch64_align_watchpoint repeatedly until the whole watched region, | |
1278 | as represented by ADDR and LEN, has been properly aligned and ready | |
1279 | to be written to one or more hardware watchpoint registers. | |
1280 | IS_INSERT indicates whether this is an insertion or a deletion. | |
1281 | Return 0 if succeed. */ | |
1282 | ||
1283 | static int | |
1284 | aarch64_handle_unaligned_watchpoint (int type, CORE_ADDR addr, int len, | |
1285 | int is_insert) | |
1286 | { | |
d6c44983 YZ |
1287 | struct aarch64_debug_reg_state *state |
1288 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
9d19df75 MS |
1289 | |
1290 | while (len > 0) | |
1291 | { | |
1292 | CORE_ADDR aligned_addr; | |
1293 | int aligned_len, ret; | |
1294 | ||
1295 | aarch64_align_watchpoint (addr, len, &aligned_addr, &aligned_len, | |
1296 | &addr, &len); | |
1297 | ||
1298 | if (is_insert) | |
1299 | ret = aarch64_dr_state_insert_one_point (state, type, aligned_addr, | |
1300 | aligned_len); | |
1301 | else | |
1302 | ret = aarch64_dr_state_remove_one_point (state, type, aligned_addr, | |
1303 | aligned_len); | |
1304 | ||
1305 | if (debug_hw_points) | |
1306 | fprintf_unfiltered (gdb_stdlog, | |
1307 | "handle_unaligned_watchpoint: is_insert: %d\n" | |
1308 | " aligned_addr: 0x%08lx, aligned_len: %d\n" | |
1309 | " next_addr: 0x%08lx, next_len: %d\n", | |
1310 | is_insert, aligned_addr, aligned_len, addr, len); | |
1311 | ||
1312 | if (ret != 0) | |
1313 | return ret; | |
1314 | } | |
1315 | ||
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | /* Implements insertion and removal of a single watchpoint. */ | |
1320 | ||
1321 | static int | |
1322 | aarch64_handle_watchpoint (int type, CORE_ADDR addr, int len, int is_insert) | |
1323 | { | |
1324 | if (aarch64_point_is_aligned (1 /* is_watchpoint */ , addr, len)) | |
1325 | return aarch64_handle_aligned_watchpoint (type, addr, len, is_insert); | |
1326 | else | |
1327 | return aarch64_handle_unaligned_watchpoint (type, addr, len, is_insert); | |
1328 | } | |
1329 | ||
1330 | /* Implement the "to_insert_watchpoint" target_ops method. | |
1331 | ||
1332 | Insert a watchpoint to watch a memory region which starts at | |
1333 | address ADDR and whose length is LEN bytes. Watch memory accesses | |
1334 | of the type TYPE. Return 0 on success, -1 on failure. */ | |
1335 | ||
1336 | static int | |
1337 | aarch64_linux_insert_watchpoint (CORE_ADDR addr, int len, int type, | |
1338 | struct expression *cond) | |
1339 | { | |
1340 | int ret; | |
1341 | ||
1342 | if (debug_hw_points) | |
1343 | fprintf_unfiltered (gdb_stdlog, | |
1344 | "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n", | |
1345 | (unsigned long) addr, len); | |
1346 | ||
1347 | gdb_assert (type != hw_execute); | |
1348 | ||
1349 | ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */); | |
1350 | ||
1351 | if (debug_hw_points > 1) | |
d6c44983 YZ |
1352 | { |
1353 | struct aarch64_debug_reg_state *state | |
1354 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
1355 | ||
1356 | aarch64_show_debug_reg_state (state, | |
1357 | "insert_watchpoint", addr, len, type); | |
1358 | } | |
9d19df75 MS |
1359 | |
1360 | return ret; | |
1361 | } | |
1362 | ||
1363 | /* Implement the "to_remove_watchpoint" target_ops method. | |
1364 | Remove a watchpoint that watched the memory region which starts at | |
1365 | address ADDR, whose length is LEN bytes, and for accesses of the | |
1366 | type TYPE. Return 0 on success, -1 on failure. */ | |
1367 | ||
1368 | static int | |
1369 | aarch64_linux_remove_watchpoint (CORE_ADDR addr, int len, int type, | |
1370 | struct expression *cond) | |
1371 | { | |
1372 | int ret; | |
1373 | ||
1374 | if (debug_hw_points) | |
1375 | fprintf_unfiltered (gdb_stdlog, | |
1376 | "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n", | |
1377 | (unsigned long) addr, len); | |
1378 | ||
1379 | gdb_assert (type != hw_execute); | |
1380 | ||
1381 | ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */); | |
1382 | ||
1383 | if (debug_hw_points > 1) | |
d6c44983 YZ |
1384 | { |
1385 | struct aarch64_debug_reg_state *state | |
1386 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); | |
1387 | ||
1388 | aarch64_show_debug_reg_state (state, | |
1389 | "remove_watchpoint", addr, len, type); | |
1390 | } | |
9d19df75 MS |
1391 | |
1392 | return ret; | |
1393 | } | |
1394 | ||
1395 | /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */ | |
1396 | ||
1397 | static int | |
1398 | aarch64_linux_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) | |
1399 | { | |
1400 | CORE_ADDR aligned_addr; | |
1401 | ||
1402 | /* Can not set watchpoints for zero or negative lengths. */ | |
1403 | if (len <= 0) | |
1404 | return 0; | |
1405 | ||
1406 | /* Must have hardware watchpoint debug register(s). */ | |
1407 | if (aarch64_num_wp_regs == 0) | |
1408 | return 0; | |
1409 | ||
1410 | /* We support unaligned watchpoint address and arbitrary length, | |
1411 | as long as the size of the whole watched area after alignment | |
1412 | doesn't exceed size of the total area that all watchpoint debug | |
1413 | registers can watch cooperatively. | |
1414 | ||
1415 | This is a very relaxed rule, but unfortunately there are | |
1416 | limitations, e.g. false-positive hits, due to limited support of | |
1417 | hardware debug registers in the kernel. See comment above | |
1418 | aarch64_align_watchpoint for more information. */ | |
1419 | ||
1420 | aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1); | |
1421 | if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG | |
1422 | < addr + len) | |
1423 | return 0; | |
1424 | ||
1425 | /* All tests passed so we are likely to be able to set the watchpoint. | |
1426 | The reason that it is 'likely' rather than 'must' is because | |
1427 | we don't check the current usage of the watchpoint registers, and | |
1428 | there may not be enough registers available for this watchpoint. | |
1429 | Ideally we should check the cached debug register state, however | |
1430 | the checking is costly. */ | |
1431 | return 1; | |
1432 | } | |
1433 | ||
1434 | /* Implement the "to_stopped_data_address" target_ops method. */ | |
1435 | ||
1436 | static int | |
1437 | aarch64_linux_stopped_data_address (struct target_ops *target, | |
1438 | CORE_ADDR *addr_p) | |
1439 | { | |
1440 | siginfo_t siginfo; | |
1441 | int i, tid; | |
1442 | struct aarch64_debug_reg_state *state; | |
1443 | ||
1444 | if (!linux_nat_get_siginfo (inferior_ptid, &siginfo)) | |
1445 | return 0; | |
1446 | ||
1447 | /* This must be a hardware breakpoint. */ | |
1448 | if (siginfo.si_signo != SIGTRAP | |
1449 | || (siginfo.si_code & 0xffff) != TRAP_HWBKPT) | |
1450 | return 0; | |
1451 | ||
1452 | /* Check if the address matches any watched address. */ | |
d6c44983 | 1453 | state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
9d19df75 MS |
1454 | for (i = aarch64_num_wp_regs - 1; i >= 0; --i) |
1455 | { | |
1456 | const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]); | |
1457 | const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr; | |
1458 | const CORE_ADDR addr_watch = state->dr_addr_wp[i]; | |
1459 | ||
1460 | if (state->dr_ref_count_wp[i] | |
1461 | && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i]) | |
1462 | && addr_trap >= addr_watch | |
1463 | && addr_trap < addr_watch + len) | |
1464 | { | |
1465 | *addr_p = addr_trap; | |
1466 | return 1; | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | return 0; | |
1471 | } | |
1472 | ||
1473 | /* Implement the "to_stopped_by_watchpoint" target_ops method. */ | |
1474 | ||
1475 | static int | |
1476 | aarch64_linux_stopped_by_watchpoint (void) | |
1477 | { | |
1478 | CORE_ADDR addr; | |
1479 | ||
1480 | return aarch64_linux_stopped_data_address (¤t_target, &addr); | |
1481 | } | |
1482 | ||
1483 | /* Implement the "to_watchpoint_addr_within_range" target_ops method. */ | |
1484 | ||
1485 | static int | |
1486 | aarch64_linux_watchpoint_addr_within_range (struct target_ops *target, | |
1487 | CORE_ADDR addr, | |
1488 | CORE_ADDR start, int length) | |
1489 | { | |
1490 | return start <= addr && start + length - 1 >= addr; | |
1491 | } | |
1492 | ||
1493 | /* Define AArch64 maintenance commands. */ | |
1494 | ||
1495 | static void | |
1496 | add_show_debug_regs_command (void) | |
1497 | { | |
1498 | /* A maintenance command to enable printing the internal DRi mirror | |
1499 | variables. */ | |
1500 | add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, | |
1501 | &debug_hw_points, _("\ | |
1502 | Set whether to show variables that mirror the AArch64 debug registers."), _("\ | |
1503 | Show whether to show variables that mirror the AArch64 debug registers."), _("\ | |
1504 | Use \"on\" to enable, \"off\" to disable.\n\ | |
1505 | If enabled, the debug registers values are shown when GDB inserts\n\ | |
1506 | or removes a hardware breakpoint or watchpoint, and when the inferior\n\ | |
1507 | triggers a breakpoint or watchpoint."), | |
1508 | NULL, | |
1509 | NULL, | |
1510 | &maintenance_set_cmdlist, | |
1511 | &maintenance_show_cmdlist); | |
1512 | } | |
1513 | ||
1514 | /* -Wmissing-prototypes. */ | |
1515 | void _initialize_aarch64_linux_nat (void); | |
1516 | ||
1517 | void | |
1518 | _initialize_aarch64_linux_nat (void) | |
1519 | { | |
1520 | struct target_ops *t; | |
1521 | ||
1522 | /* Fill in the generic GNU/Linux methods. */ | |
1523 | t = linux_target (); | |
1524 | ||
1525 | add_show_debug_regs_command (); | |
1526 | ||
1527 | /* Add our register access methods. */ | |
1528 | t->to_fetch_registers = aarch64_linux_fetch_inferior_registers; | |
1529 | t->to_store_registers = aarch64_linux_store_inferior_registers; | |
1530 | ||
1531 | t->to_read_description = aarch64_linux_read_description; | |
1532 | ||
1533 | t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint; | |
1534 | t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint; | |
1535 | t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint; | |
1536 | t->to_region_ok_for_hw_watchpoint = | |
1537 | aarch64_linux_region_ok_for_hw_watchpoint; | |
1538 | t->to_insert_watchpoint = aarch64_linux_insert_watchpoint; | |
1539 | t->to_remove_watchpoint = aarch64_linux_remove_watchpoint; | |
1540 | t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint; | |
1541 | t->to_stopped_data_address = aarch64_linux_stopped_data_address; | |
1542 | t->to_watchpoint_addr_within_range = | |
1543 | aarch64_linux_watchpoint_addr_within_range; | |
9d19df75 MS |
1544 | |
1545 | /* Override the GNU/Linux inferior startup hook. */ | |
1546 | super_post_startup_inferior = t->to_post_startup_inferior; | |
1547 | t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior; | |
1548 | ||
1549 | /* Register the target. */ | |
1550 | linux_nat_add_target (t); | |
1551 | linux_nat_set_new_thread (t, aarch64_linux_new_thread); | |
d6c44983 YZ |
1552 | linux_nat_set_new_fork (t, aarch64_linux_new_fork); |
1553 | linux_nat_set_forget_process (t, aarch64_forget_process); | |
9d19df75 MS |
1554 | linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume); |
1555 | } |