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