| 1 | /* Native-dependent code for GNU/Linux AArch64. |
| 2 | |
| 3 | Copyright (C) 2011-2015 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 "aarch32-linux-nat.h" |
| 33 | #include "nat/aarch64-linux-hw-point.h" |
| 34 | |
| 35 | #include "elf/external.h" |
| 36 | #include "elf/common.h" |
| 37 | |
| 38 | #include "nat/gdb_ptrace.h" |
| 39 | #include <sys/utsname.h> |
| 40 | #include <asm/ptrace.h> |
| 41 | |
| 42 | #include "gregset.h" |
| 43 | |
| 44 | /* Defines ps_err_e, struct ps_prochandle. */ |
| 45 | #include "gdb_proc_service.h" |
| 46 | |
| 47 | #ifndef TRAP_HWBKPT |
| 48 | #define TRAP_HWBKPT 0x0004 |
| 49 | #endif |
| 50 | |
| 51 | /* On GNU/Linux, threads are implemented as pseudo-processes, in which |
| 52 | case we may be tracing more than one process at a time. In that |
| 53 | case, inferior_ptid will contain the main process ID and the |
| 54 | individual thread (process) ID. get_thread_id () is used to get |
| 55 | the thread id if it's available, and the process id otherwise. */ |
| 56 | |
| 57 | static int |
| 58 | get_thread_id (ptid_t ptid) |
| 59 | { |
| 60 | int tid = ptid_get_lwp (ptid); |
| 61 | |
| 62 | if (0 == tid) |
| 63 | tid = ptid_get_pid (ptid); |
| 64 | return tid; |
| 65 | } |
| 66 | |
| 67 | /* Per-process data. We don't bind this to a per-inferior registry |
| 68 | because of targets like x86 GNU/Linux that need to keep track of |
| 69 | processes that aren't bound to any inferior (e.g., fork children, |
| 70 | checkpoints). */ |
| 71 | |
| 72 | struct aarch64_process_info |
| 73 | { |
| 74 | /* Linked list. */ |
| 75 | struct aarch64_process_info *next; |
| 76 | |
| 77 | /* The process identifier. */ |
| 78 | pid_t pid; |
| 79 | |
| 80 | /* Copy of aarch64 hardware debug registers. */ |
| 81 | struct aarch64_debug_reg_state state; |
| 82 | }; |
| 83 | |
| 84 | static struct aarch64_process_info *aarch64_process_list = NULL; |
| 85 | |
| 86 | /* Find process data for process PID. */ |
| 87 | |
| 88 | static struct aarch64_process_info * |
| 89 | aarch64_find_process_pid (pid_t pid) |
| 90 | { |
| 91 | struct aarch64_process_info *proc; |
| 92 | |
| 93 | for (proc = aarch64_process_list; proc; proc = proc->next) |
| 94 | if (proc->pid == pid) |
| 95 | return proc; |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /* Add process data for process PID. Returns newly allocated info |
| 101 | object. */ |
| 102 | |
| 103 | static struct aarch64_process_info * |
| 104 | aarch64_add_process (pid_t pid) |
| 105 | { |
| 106 | struct aarch64_process_info *proc; |
| 107 | |
| 108 | proc = xcalloc (1, sizeof (*proc)); |
| 109 | proc->pid = pid; |
| 110 | |
| 111 | proc->next = aarch64_process_list; |
| 112 | aarch64_process_list = proc; |
| 113 | |
| 114 | return proc; |
| 115 | } |
| 116 | |
| 117 | /* Get data specific info for process PID, creating it if necessary. |
| 118 | Never returns NULL. */ |
| 119 | |
| 120 | static struct aarch64_process_info * |
| 121 | aarch64_process_info_get (pid_t pid) |
| 122 | { |
| 123 | struct aarch64_process_info *proc; |
| 124 | |
| 125 | proc = aarch64_find_process_pid (pid); |
| 126 | if (proc == NULL) |
| 127 | proc = aarch64_add_process (pid); |
| 128 | |
| 129 | return proc; |
| 130 | } |
| 131 | |
| 132 | /* Called whenever GDB is no longer debugging process PID. It deletes |
| 133 | data structures that keep track of debug register state. */ |
| 134 | |
| 135 | static void |
| 136 | aarch64_forget_process (pid_t pid) |
| 137 | { |
| 138 | struct aarch64_process_info *proc, **proc_link; |
| 139 | |
| 140 | proc = aarch64_process_list; |
| 141 | proc_link = &aarch64_process_list; |
| 142 | |
| 143 | while (proc != NULL) |
| 144 | { |
| 145 | if (proc->pid == pid) |
| 146 | { |
| 147 | *proc_link = proc->next; |
| 148 | |
| 149 | xfree (proc); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | proc_link = &proc->next; |
| 154 | proc = *proc_link; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* Get debug registers state for process PID. */ |
| 159 | |
| 160 | static struct aarch64_debug_reg_state * |
| 161 | aarch64_get_debug_reg_state (pid_t pid) |
| 162 | { |
| 163 | return &aarch64_process_info_get (pid)->state; |
| 164 | } |
| 165 | |
| 166 | struct aarch64_dr_update_callback_param |
| 167 | { |
| 168 | int is_watchpoint; |
| 169 | unsigned int idx; |
| 170 | }; |
| 171 | |
| 172 | /* Callback for iterate_over_lwps. Records the |
| 173 | information about the change of one hardware breakpoint/watchpoint |
| 174 | setting for the thread LWP. |
| 175 | The information is passed in via PTR. |
| 176 | N.B. The actual updating of hardware debug registers is not |
| 177 | carried out until the moment the thread is resumed. */ |
| 178 | |
| 179 | static int |
| 180 | debug_reg_change_callback (struct lwp_info *lwp, void *ptr) |
| 181 | { |
| 182 | struct aarch64_dr_update_callback_param *param_p |
| 183 | = (struct aarch64_dr_update_callback_param *) ptr; |
| 184 | int pid = get_thread_id (lwp->ptid); |
| 185 | int idx = param_p->idx; |
| 186 | int is_watchpoint = param_p->is_watchpoint; |
| 187 | struct arch_lwp_info *info = lwp->arch_private; |
| 188 | dr_changed_t *dr_changed_ptr; |
| 189 | dr_changed_t dr_changed; |
| 190 | |
| 191 | if (info == NULL) |
| 192 | info = lwp->arch_private = XCNEW (struct arch_lwp_info); |
| 193 | |
| 194 | if (show_debug_regs) |
| 195 | { |
| 196 | fprintf_unfiltered (gdb_stdlog, |
| 197 | "debug_reg_change_callback: \n\tOn entry:\n"); |
| 198 | fprintf_unfiltered (gdb_stdlog, |
| 199 | "\tpid%d, dr_changed_bp=0x%s, " |
| 200 | "dr_changed_wp=0x%s\n", |
| 201 | pid, phex (info->dr_changed_bp, 8), |
| 202 | phex (info->dr_changed_wp, 8)); |
| 203 | } |
| 204 | |
| 205 | dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp |
| 206 | : &info->dr_changed_bp; |
| 207 | dr_changed = *dr_changed_ptr; |
| 208 | |
| 209 | gdb_assert (idx >= 0 |
| 210 | && (idx <= (is_watchpoint ? aarch64_num_wp_regs |
| 211 | : aarch64_num_bp_regs))); |
| 212 | |
| 213 | /* The actual update is done later just before resuming the lwp, |
| 214 | we just mark that one register pair needs updating. */ |
| 215 | DR_MARK_N_CHANGED (dr_changed, idx); |
| 216 | *dr_changed_ptr = dr_changed; |
| 217 | |
| 218 | /* If the lwp isn't stopped, force it to momentarily pause, so |
| 219 | we can update its debug registers. */ |
| 220 | if (!lwp->stopped) |
| 221 | linux_stop_lwp (lwp); |
| 222 | |
| 223 | if (show_debug_regs) |
| 224 | { |
| 225 | fprintf_unfiltered (gdb_stdlog, |
| 226 | "\tOn exit:\n\tpid%d, dr_changed_bp=0x%s, " |
| 227 | "dr_changed_wp=0x%s\n", |
| 228 | pid, phex (info->dr_changed_bp, 8), |
| 229 | phex (info->dr_changed_wp, 8)); |
| 230 | } |
| 231 | |
| 232 | /* Continue the iteration. */ |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* Notify each thread that their IDXth breakpoint/watchpoint register |
| 237 | pair needs to be updated. The message will be recorded in each |
| 238 | thread's arch-specific data area, the actual updating will be done |
| 239 | when the thread is resumed. */ |
| 240 | |
| 241 | void |
| 242 | aarch64_notify_debug_reg_change (const struct aarch64_debug_reg_state *state, |
| 243 | int is_watchpoint, unsigned int idx) |
| 244 | { |
| 245 | struct aarch64_dr_update_callback_param param; |
| 246 | ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid)); |
| 247 | |
| 248 | param.is_watchpoint = is_watchpoint; |
| 249 | param.idx = idx; |
| 250 | |
| 251 | iterate_over_lwps (pid_ptid, debug_reg_change_callback, (void *) ¶m); |
| 252 | } |
| 253 | |
| 254 | /* Fill GDB's register array with the general-purpose register values |
| 255 | from the current thread. */ |
| 256 | |
| 257 | static void |
| 258 | fetch_gregs_from_thread (struct regcache *regcache) |
| 259 | { |
| 260 | int ret, tid; |
| 261 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 262 | elf_gregset_t regs; |
| 263 | struct iovec iovec; |
| 264 | |
| 265 | /* Make sure REGS can hold all registers contents on both aarch64 |
| 266 | and arm. */ |
| 267 | gdb_static_assert (sizeof (regs) >= 18 * 4); |
| 268 | |
| 269 | tid = get_thread_id (inferior_ptid); |
| 270 | |
| 271 | iovec.iov_base = ®s; |
| 272 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 273 | iovec.iov_len = 18 * 4; |
| 274 | else |
| 275 | iovec.iov_len = sizeof (regs); |
| 276 | |
| 277 | ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec); |
| 278 | if (ret < 0) |
| 279 | perror_with_name (_("Unable to fetch general registers.")); |
| 280 | |
| 281 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 282 | aarch32_gp_regcache_supply (regcache, (uint32_t *) regs, 1); |
| 283 | else |
| 284 | { |
| 285 | int regno; |
| 286 | |
| 287 | for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++) |
| 288 | regcache_raw_supply (regcache, regno, ®s[regno - AARCH64_X0_REGNUM]); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* Store to the current thread the valid general-purpose register |
| 293 | values in the GDB's register array. */ |
| 294 | |
| 295 | static void |
| 296 | store_gregs_to_thread (const struct regcache *regcache) |
| 297 | { |
| 298 | int ret, tid; |
| 299 | elf_gregset_t regs; |
| 300 | struct iovec iovec; |
| 301 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 302 | |
| 303 | /* Make sure REGS can hold all registers contents on both aarch64 |
| 304 | and arm. */ |
| 305 | gdb_static_assert (sizeof (regs) >= 18 * 4); |
| 306 | tid = get_thread_id (inferior_ptid); |
| 307 | |
| 308 | iovec.iov_base = ®s; |
| 309 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 310 | iovec.iov_len = 18 * 4; |
| 311 | else |
| 312 | iovec.iov_len = sizeof (regs); |
| 313 | |
| 314 | ret = ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec); |
| 315 | if (ret < 0) |
| 316 | perror_with_name (_("Unable to fetch general registers.")); |
| 317 | |
| 318 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 319 | aarch32_gp_regcache_collect (regcache, (uint32_t *) regs, 1); |
| 320 | else |
| 321 | { |
| 322 | int regno; |
| 323 | |
| 324 | for (regno = AARCH64_X0_REGNUM; regno <= AARCH64_CPSR_REGNUM; regno++) |
| 325 | if (REG_VALID == regcache_register_status (regcache, regno)) |
| 326 | regcache_raw_collect (regcache, regno, |
| 327 | ®s[regno - AARCH64_X0_REGNUM]); |
| 328 | } |
| 329 | |
| 330 | ret = ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, &iovec); |
| 331 | if (ret < 0) |
| 332 | perror_with_name (_("Unable to store general registers.")); |
| 333 | } |
| 334 | |
| 335 | /* Fill GDB's register array with the fp/simd register values |
| 336 | from the current thread. */ |
| 337 | |
| 338 | static void |
| 339 | fetch_fpregs_from_thread (struct regcache *regcache) |
| 340 | { |
| 341 | int ret, tid; |
| 342 | elf_fpregset_t regs; |
| 343 | struct iovec iovec; |
| 344 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 345 | |
| 346 | /* Make sure REGS can hold all VFP registers contents on both aarch64 |
| 347 | and arm. */ |
| 348 | gdb_static_assert (sizeof regs >= VFP_REGS_SIZE); |
| 349 | |
| 350 | tid = get_thread_id (inferior_ptid); |
| 351 | |
| 352 | iovec.iov_base = ®s; |
| 353 | |
| 354 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 355 | { |
| 356 | iovec.iov_len = VFP_REGS_SIZE; |
| 357 | |
| 358 | ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec); |
| 359 | if (ret < 0) |
| 360 | perror_with_name (_("Unable to fetch VFP registers.")); |
| 361 | |
| 362 | aarch32_vfp_regcache_supply (regcache, (gdb_byte *) ®s, 32); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | int regno; |
| 367 | |
| 368 | iovec.iov_len = sizeof (regs); |
| 369 | |
| 370 | ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec); |
| 371 | if (ret < 0) |
| 372 | perror_with_name (_("Unable to fetch vFP/SIMD registers.")); |
| 373 | |
| 374 | for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++) |
| 375 | regcache_raw_supply (regcache, regno, |
| 376 | ®s.vregs[regno - AARCH64_V0_REGNUM]); |
| 377 | |
| 378 | regcache_raw_supply (regcache, AARCH64_FPSR_REGNUM, ®s.fpsr); |
| 379 | regcache_raw_supply (regcache, AARCH64_FPCR_REGNUM, ®s.fpcr); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* Store to the current thread the valid fp/simd register |
| 384 | values in the GDB's register array. */ |
| 385 | |
| 386 | static void |
| 387 | store_fpregs_to_thread (const struct regcache *regcache) |
| 388 | { |
| 389 | int ret, tid; |
| 390 | elf_fpregset_t regs; |
| 391 | struct iovec iovec; |
| 392 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
| 393 | |
| 394 | /* Make sure REGS can hold all VFP registers contents on both aarch64 |
| 395 | and arm. */ |
| 396 | gdb_static_assert (sizeof regs >= VFP_REGS_SIZE); |
| 397 | tid = get_thread_id (inferior_ptid); |
| 398 | |
| 399 | iovec.iov_base = ®s; |
| 400 | |
| 401 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 402 | { |
| 403 | iovec.iov_len = VFP_REGS_SIZE; |
| 404 | |
| 405 | ret = ptrace (PTRACE_GETREGSET, tid, NT_ARM_VFP, &iovec); |
| 406 | if (ret < 0) |
| 407 | perror_with_name (_("Unable to fetch VFP registers.")); |
| 408 | |
| 409 | aarch32_vfp_regcache_collect (regcache, (gdb_byte *) ®s, 32); |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | int regno; |
| 414 | |
| 415 | iovec.iov_len = sizeof (regs); |
| 416 | |
| 417 | ret = ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec); |
| 418 | if (ret < 0) |
| 419 | perror_with_name (_("Unable to fetch FP/SIMD registers.")); |
| 420 | |
| 421 | for (regno = AARCH64_V0_REGNUM; regno <= AARCH64_V31_REGNUM; regno++) |
| 422 | if (REG_VALID == regcache_register_status (regcache, regno)) |
| 423 | regcache_raw_collect (regcache, regno, |
| 424 | (char *) ®s.vregs[regno - AARCH64_V0_REGNUM]); |
| 425 | |
| 426 | if (REG_VALID == regcache_register_status (regcache, AARCH64_FPSR_REGNUM)) |
| 427 | regcache_raw_collect (regcache, AARCH64_FPSR_REGNUM, |
| 428 | (char *) ®s.fpsr); |
| 429 | if (REG_VALID == regcache_register_status (regcache, AARCH64_FPCR_REGNUM)) |
| 430 | regcache_raw_collect (regcache, AARCH64_FPCR_REGNUM, |
| 431 | (char *) ®s.fpcr); |
| 432 | } |
| 433 | |
| 434 | if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32) |
| 435 | { |
| 436 | ret = ptrace (PTRACE_SETREGSET, tid, NT_ARM_VFP, &iovec); |
| 437 | if (ret < 0) |
| 438 | perror_with_name (_("Unable to store VFP registers.")); |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | ret = ptrace (PTRACE_SETREGSET, tid, NT_FPREGSET, &iovec); |
| 443 | if (ret < 0) |
| 444 | perror_with_name (_("Unable to store FP/SIMD registers.")); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* Implement the "to_fetch_register" target_ops method. */ |
| 449 | |
| 450 | static void |
| 451 | aarch64_linux_fetch_inferior_registers (struct target_ops *ops, |
| 452 | struct regcache *regcache, |
| 453 | int regno) |
| 454 | { |
| 455 | if (regno == -1) |
| 456 | { |
| 457 | fetch_gregs_from_thread (regcache); |
| 458 | fetch_fpregs_from_thread (regcache); |
| 459 | } |
| 460 | else if (regno < AARCH64_V0_REGNUM) |
| 461 | fetch_gregs_from_thread (regcache); |
| 462 | else |
| 463 | fetch_fpregs_from_thread (regcache); |
| 464 | } |
| 465 | |
| 466 | /* Implement the "to_store_register" target_ops method. */ |
| 467 | |
| 468 | static void |
| 469 | aarch64_linux_store_inferior_registers (struct target_ops *ops, |
| 470 | struct regcache *regcache, |
| 471 | int regno) |
| 472 | { |
| 473 | if (regno == -1) |
| 474 | { |
| 475 | store_gregs_to_thread (regcache); |
| 476 | store_fpregs_to_thread (regcache); |
| 477 | } |
| 478 | else if (regno < AARCH64_V0_REGNUM) |
| 479 | store_gregs_to_thread (regcache); |
| 480 | else |
| 481 | store_fpregs_to_thread (regcache); |
| 482 | } |
| 483 | |
| 484 | /* Fill register REGNO (if it is a general-purpose register) in |
| 485 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, |
| 486 | do this for all registers. */ |
| 487 | |
| 488 | void |
| 489 | fill_gregset (const struct regcache *regcache, |
| 490 | gdb_gregset_t *gregsetp, int regno) |
| 491 | { |
| 492 | regcache_collect_regset (&aarch64_linux_gregset, regcache, |
| 493 | regno, (gdb_byte *) gregsetp, |
| 494 | AARCH64_LINUX_SIZEOF_GREGSET); |
| 495 | } |
| 496 | |
| 497 | /* Fill GDB's register array with the general-purpose register values |
| 498 | in *GREGSETP. */ |
| 499 | |
| 500 | void |
| 501 | supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp) |
| 502 | { |
| 503 | regcache_supply_regset (&aarch64_linux_gregset, regcache, -1, |
| 504 | (const gdb_byte *) gregsetp, |
| 505 | AARCH64_LINUX_SIZEOF_GREGSET); |
| 506 | } |
| 507 | |
| 508 | /* Fill register REGNO (if it is a floating-point register) in |
| 509 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, |
| 510 | do this for all registers. */ |
| 511 | |
| 512 | void |
| 513 | fill_fpregset (const struct regcache *regcache, |
| 514 | gdb_fpregset_t *fpregsetp, int regno) |
| 515 | { |
| 516 | regcache_collect_regset (&aarch64_linux_fpregset, regcache, |
| 517 | regno, (gdb_byte *) fpregsetp, |
| 518 | AARCH64_LINUX_SIZEOF_FPREGSET); |
| 519 | } |
| 520 | |
| 521 | /* Fill GDB's register array with the floating-point register values |
| 522 | in *FPREGSETP. */ |
| 523 | |
| 524 | void |
| 525 | supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregsetp) |
| 526 | { |
| 527 | regcache_supply_regset (&aarch64_linux_fpregset, regcache, -1, |
| 528 | (const gdb_byte *) fpregsetp, |
| 529 | AARCH64_LINUX_SIZEOF_FPREGSET); |
| 530 | } |
| 531 | |
| 532 | /* Called when resuming a thread. |
| 533 | The hardware debug registers are updated when there is any change. */ |
| 534 | |
| 535 | static void |
| 536 | aarch64_linux_prepare_to_resume (struct lwp_info *lwp) |
| 537 | { |
| 538 | struct arch_lwp_info *info = lwp->arch_private; |
| 539 | |
| 540 | /* NULL means this is the main thread still going through the shell, |
| 541 | or, no watchpoint has been set yet. In that case, there's |
| 542 | nothing to do. */ |
| 543 | if (info == NULL) |
| 544 | return; |
| 545 | |
| 546 | if (DR_HAS_CHANGED (info->dr_changed_bp) |
| 547 | || DR_HAS_CHANGED (info->dr_changed_wp)) |
| 548 | { |
| 549 | int tid = ptid_get_lwp (lwp->ptid); |
| 550 | struct aarch64_debug_reg_state *state |
| 551 | = aarch64_get_debug_reg_state (ptid_get_pid (lwp->ptid)); |
| 552 | |
| 553 | if (show_debug_regs) |
| 554 | fprintf_unfiltered (gdb_stdlog, "prepare_to_resume thread %d\n", tid); |
| 555 | |
| 556 | /* Watchpoints. */ |
| 557 | if (DR_HAS_CHANGED (info->dr_changed_wp)) |
| 558 | { |
| 559 | aarch64_linux_set_debug_regs (state, tid, 1); |
| 560 | DR_CLEAR_CHANGED (info->dr_changed_wp); |
| 561 | } |
| 562 | |
| 563 | /* Breakpoints. */ |
| 564 | if (DR_HAS_CHANGED (info->dr_changed_bp)) |
| 565 | { |
| 566 | aarch64_linux_set_debug_regs (state, tid, 0); |
| 567 | DR_CLEAR_CHANGED (info->dr_changed_bp); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | static void |
| 573 | aarch64_linux_new_thread (struct lwp_info *lp) |
| 574 | { |
| 575 | struct arch_lwp_info *info = XCNEW (struct arch_lwp_info); |
| 576 | |
| 577 | /* Mark that all the hardware breakpoint/watchpoint register pairs |
| 578 | for this thread need to be initialized. */ |
| 579 | DR_MARK_ALL_CHANGED (info->dr_changed_bp, aarch64_num_bp_regs); |
| 580 | DR_MARK_ALL_CHANGED (info->dr_changed_wp, aarch64_num_wp_regs); |
| 581 | |
| 582 | lp->arch_private = info; |
| 583 | } |
| 584 | |
| 585 | /* linux_nat_new_fork hook. */ |
| 586 | |
| 587 | static void |
| 588 | aarch64_linux_new_fork (struct lwp_info *parent, pid_t child_pid) |
| 589 | { |
| 590 | pid_t parent_pid; |
| 591 | struct aarch64_debug_reg_state *parent_state; |
| 592 | struct aarch64_debug_reg_state *child_state; |
| 593 | |
| 594 | /* NULL means no watchpoint has ever been set in the parent. In |
| 595 | that case, there's nothing to do. */ |
| 596 | if (parent->arch_private == NULL) |
| 597 | return; |
| 598 | |
| 599 | /* GDB core assumes the child inherits the watchpoints/hw |
| 600 | breakpoints of the parent, and will remove them all from the |
| 601 | forked off process. Copy the debug registers mirrors into the |
| 602 | new process so that all breakpoints and watchpoints can be |
| 603 | removed together. */ |
| 604 | |
| 605 | parent_pid = ptid_get_pid (parent->ptid); |
| 606 | parent_state = aarch64_get_debug_reg_state (parent_pid); |
| 607 | child_state = aarch64_get_debug_reg_state (child_pid); |
| 608 | *child_state = *parent_state; |
| 609 | } |
| 610 | \f |
| 611 | |
| 612 | /* Called by libthread_db. Returns a pointer to the thread local |
| 613 | storage (or its descriptor). */ |
| 614 | |
| 615 | ps_err_e |
| 616 | ps_get_thread_area (const struct ps_prochandle *ph, |
| 617 | lwpid_t lwpid, int idx, void **base) |
| 618 | { |
| 619 | struct iovec iovec; |
| 620 | uint64_t reg; |
| 621 | |
| 622 | iovec.iov_base = ® |
| 623 | iovec.iov_len = sizeof (reg); |
| 624 | |
| 625 | if (ptrace (PTRACE_GETREGSET, lwpid, NT_ARM_TLS, &iovec) != 0) |
| 626 | return PS_ERR; |
| 627 | |
| 628 | /* IDX is the bias from the thread pointer to the beginning of the |
| 629 | thread descriptor. It has to be subtracted due to implementation |
| 630 | quirks in libthread_db. */ |
| 631 | *base = (void *) (reg - idx); |
| 632 | |
| 633 | return PS_OK; |
| 634 | } |
| 635 | \f |
| 636 | |
| 637 | static void (*super_post_startup_inferior) (struct target_ops *self, |
| 638 | ptid_t ptid); |
| 639 | |
| 640 | /* Implement the "to_post_startup_inferior" target_ops method. */ |
| 641 | |
| 642 | static void |
| 643 | aarch64_linux_child_post_startup_inferior (struct target_ops *self, |
| 644 | ptid_t ptid) |
| 645 | { |
| 646 | aarch64_forget_process (ptid_get_pid (ptid)); |
| 647 | aarch64_linux_get_debug_reg_capacity (ptid_get_pid (ptid)); |
| 648 | super_post_startup_inferior (self, ptid); |
| 649 | } |
| 650 | |
| 651 | extern struct target_desc *tdesc_arm_with_vfpv3; |
| 652 | extern struct target_desc *tdesc_arm_with_neon; |
| 653 | |
| 654 | /* Implement the "to_read_description" target_ops method. */ |
| 655 | |
| 656 | static const struct target_desc * |
| 657 | aarch64_linux_read_description (struct target_ops *ops) |
| 658 | { |
| 659 | CORE_ADDR at_phent; |
| 660 | |
| 661 | if (target_auxv_search (ops, AT_PHENT, &at_phent) == 1) |
| 662 | { |
| 663 | if (at_phent == sizeof (Elf64_External_Phdr)) |
| 664 | return tdesc_aarch64; |
| 665 | else |
| 666 | { |
| 667 | CORE_ADDR arm_hwcap = 0; |
| 668 | |
| 669 | if (target_auxv_search (ops, AT_HWCAP, &arm_hwcap) != 1) |
| 670 | return ops->beneath->to_read_description (ops->beneath); |
| 671 | |
| 672 | #ifndef COMPAT_HWCAP_VFP |
| 673 | #define COMPAT_HWCAP_VFP (1 << 6) |
| 674 | #endif |
| 675 | #ifndef COMPAT_HWCAP_NEON |
| 676 | #define COMPAT_HWCAP_NEON (1 << 12) |
| 677 | #endif |
| 678 | #ifndef COMPAT_HWCAP_VFPv3 |
| 679 | #define COMPAT_HWCAP_VFPv3 (1 << 13) |
| 680 | #endif |
| 681 | |
| 682 | if (arm_hwcap & COMPAT_HWCAP_VFP) |
| 683 | { |
| 684 | char *buf; |
| 685 | const struct target_desc *result = NULL; |
| 686 | |
| 687 | if (arm_hwcap & COMPAT_HWCAP_NEON) |
| 688 | result = tdesc_arm_with_neon; |
| 689 | else if (arm_hwcap & COMPAT_HWCAP_VFPv3) |
| 690 | result = tdesc_arm_with_vfpv3; |
| 691 | |
| 692 | return result; |
| 693 | } |
| 694 | |
| 695 | return NULL; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return tdesc_aarch64; |
| 700 | } |
| 701 | |
| 702 | /* Returns the number of hardware watchpoints of type TYPE that we can |
| 703 | set. Value is positive if we can set CNT watchpoints, zero if |
| 704 | setting watchpoints of type TYPE is not supported, and negative if |
| 705 | CNT is more than the maximum number of watchpoints of type TYPE |
| 706 | that we can support. TYPE is one of bp_hardware_watchpoint, |
| 707 | bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint. |
| 708 | CNT is the number of such watchpoints used so far (including this |
| 709 | one). OTHERTYPE is non-zero if other types of watchpoints are |
| 710 | currently enabled. */ |
| 711 | |
| 712 | static int |
| 713 | aarch64_linux_can_use_hw_breakpoint (struct target_ops *self, |
| 714 | int type, int cnt, int othertype) |
| 715 | { |
| 716 | if (type == bp_hardware_watchpoint || type == bp_read_watchpoint |
| 717 | || type == bp_access_watchpoint || type == bp_watchpoint) |
| 718 | { |
| 719 | if (aarch64_num_wp_regs == 0) |
| 720 | return 0; |
| 721 | } |
| 722 | else if (type == bp_hardware_breakpoint) |
| 723 | { |
| 724 | if (aarch64_num_bp_regs == 0) |
| 725 | return 0; |
| 726 | } |
| 727 | else |
| 728 | gdb_assert_not_reached ("unexpected breakpoint type"); |
| 729 | |
| 730 | /* We always return 1 here because we don't have enough information |
| 731 | about possible overlap of addresses that they want to watch. As an |
| 732 | extreme example, consider the case where all the watchpoints watch |
| 733 | the same address and the same region length: then we can handle a |
| 734 | virtually unlimited number of watchpoints, due to debug register |
| 735 | sharing implemented via reference counts. */ |
| 736 | return 1; |
| 737 | } |
| 738 | |
| 739 | /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address. |
| 740 | Return 0 on success, -1 on failure. */ |
| 741 | |
| 742 | static int |
| 743 | aarch64_linux_insert_hw_breakpoint (struct target_ops *self, |
| 744 | struct gdbarch *gdbarch, |
| 745 | struct bp_target_info *bp_tgt) |
| 746 | { |
| 747 | int ret; |
| 748 | CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address; |
| 749 | const int len = 4; |
| 750 | const enum target_hw_bp_type type = hw_execute; |
| 751 | struct aarch64_debug_reg_state *state |
| 752 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
| 753 | |
| 754 | if (show_debug_regs) |
| 755 | fprintf_unfiltered |
| 756 | (gdb_stdlog, |
| 757 | "insert_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n", |
| 758 | (unsigned long) addr, len); |
| 759 | |
| 760 | ret = aarch64_handle_breakpoint (type, addr, len, 1 /* is_insert */, state); |
| 761 | |
| 762 | if (show_debug_regs) |
| 763 | { |
| 764 | aarch64_show_debug_reg_state (state, |
| 765 | "insert_hw_breakpoint", addr, len, type); |
| 766 | } |
| 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address. |
| 772 | Return 0 on success, -1 on failure. */ |
| 773 | |
| 774 | static int |
| 775 | aarch64_linux_remove_hw_breakpoint (struct target_ops *self, |
| 776 | struct gdbarch *gdbarch, |
| 777 | struct bp_target_info *bp_tgt) |
| 778 | { |
| 779 | int ret; |
| 780 | CORE_ADDR addr = bp_tgt->placed_address; |
| 781 | const int len = 4; |
| 782 | const enum target_hw_bp_type type = hw_execute; |
| 783 | struct aarch64_debug_reg_state *state |
| 784 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
| 785 | |
| 786 | if (show_debug_regs) |
| 787 | fprintf_unfiltered |
| 788 | (gdb_stdlog, "remove_hw_breakpoint on entry (addr=0x%08lx, len=%d))\n", |
| 789 | (unsigned long) addr, len); |
| 790 | |
| 791 | ret = aarch64_handle_breakpoint (type, addr, len, 0 /* is_insert */, state); |
| 792 | |
| 793 | if (show_debug_regs) |
| 794 | { |
| 795 | aarch64_show_debug_reg_state (state, |
| 796 | "remove_hw_watchpoint", addr, len, type); |
| 797 | } |
| 798 | |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | /* Implement the "to_insert_watchpoint" target_ops method. |
| 803 | |
| 804 | Insert a watchpoint to watch a memory region which starts at |
| 805 | address ADDR and whose length is LEN bytes. Watch memory accesses |
| 806 | of the type TYPE. Return 0 on success, -1 on failure. */ |
| 807 | |
| 808 | static int |
| 809 | aarch64_linux_insert_watchpoint (struct target_ops *self, |
| 810 | CORE_ADDR addr, int len, int type, |
| 811 | struct expression *cond) |
| 812 | { |
| 813 | int ret; |
| 814 | struct aarch64_debug_reg_state *state |
| 815 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
| 816 | |
| 817 | if (show_debug_regs) |
| 818 | fprintf_unfiltered (gdb_stdlog, |
| 819 | "insert_watchpoint on entry (addr=0x%08lx, len=%d)\n", |
| 820 | (unsigned long) addr, len); |
| 821 | |
| 822 | gdb_assert (type != hw_execute); |
| 823 | |
| 824 | ret = aarch64_handle_watchpoint (type, addr, len, 1 /* is_insert */, state); |
| 825 | |
| 826 | if (show_debug_regs) |
| 827 | { |
| 828 | aarch64_show_debug_reg_state (state, |
| 829 | "insert_watchpoint", addr, len, type); |
| 830 | } |
| 831 | |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | /* Implement the "to_remove_watchpoint" target_ops method. |
| 836 | Remove a watchpoint that watched the memory region which starts at |
| 837 | address ADDR, whose length is LEN bytes, and for accesses of the |
| 838 | type TYPE. Return 0 on success, -1 on failure. */ |
| 839 | |
| 840 | static int |
| 841 | aarch64_linux_remove_watchpoint (struct target_ops *self, |
| 842 | CORE_ADDR addr, int len, int type, |
| 843 | struct expression *cond) |
| 844 | { |
| 845 | int ret; |
| 846 | struct aarch64_debug_reg_state *state |
| 847 | = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
| 848 | |
| 849 | if (show_debug_regs) |
| 850 | fprintf_unfiltered (gdb_stdlog, |
| 851 | "remove_watchpoint on entry (addr=0x%08lx, len=%d)\n", |
| 852 | (unsigned long) addr, len); |
| 853 | |
| 854 | gdb_assert (type != hw_execute); |
| 855 | |
| 856 | ret = aarch64_handle_watchpoint (type, addr, len, 0 /* is_insert */, state); |
| 857 | |
| 858 | if (show_debug_regs) |
| 859 | { |
| 860 | aarch64_show_debug_reg_state (state, |
| 861 | "remove_watchpoint", addr, len, type); |
| 862 | } |
| 863 | |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | /* Implement the "to_region_ok_for_hw_watchpoint" target_ops method. */ |
| 868 | |
| 869 | static int |
| 870 | aarch64_linux_region_ok_for_hw_watchpoint (struct target_ops *self, |
| 871 | CORE_ADDR addr, int len) |
| 872 | { |
| 873 | CORE_ADDR aligned_addr; |
| 874 | |
| 875 | /* Can not set watchpoints for zero or negative lengths. */ |
| 876 | if (len <= 0) |
| 877 | return 0; |
| 878 | |
| 879 | /* Must have hardware watchpoint debug register(s). */ |
| 880 | if (aarch64_num_wp_regs == 0) |
| 881 | return 0; |
| 882 | |
| 883 | /* We support unaligned watchpoint address and arbitrary length, |
| 884 | as long as the size of the whole watched area after alignment |
| 885 | doesn't exceed size of the total area that all watchpoint debug |
| 886 | registers can watch cooperatively. |
| 887 | |
| 888 | This is a very relaxed rule, but unfortunately there are |
| 889 | limitations, e.g. false-positive hits, due to limited support of |
| 890 | hardware debug registers in the kernel. See comment above |
| 891 | aarch64_align_watchpoint for more information. */ |
| 892 | |
| 893 | aligned_addr = addr & ~(AARCH64_HWP_MAX_LEN_PER_REG - 1); |
| 894 | if (aligned_addr + aarch64_num_wp_regs * AARCH64_HWP_MAX_LEN_PER_REG |
| 895 | < addr + len) |
| 896 | return 0; |
| 897 | |
| 898 | /* All tests passed so we are likely to be able to set the watchpoint. |
| 899 | The reason that it is 'likely' rather than 'must' is because |
| 900 | we don't check the current usage of the watchpoint registers, and |
| 901 | there may not be enough registers available for this watchpoint. |
| 902 | Ideally we should check the cached debug register state, however |
| 903 | the checking is costly. */ |
| 904 | return 1; |
| 905 | } |
| 906 | |
| 907 | /* Implement the "to_stopped_data_address" target_ops method. */ |
| 908 | |
| 909 | static int |
| 910 | aarch64_linux_stopped_data_address (struct target_ops *target, |
| 911 | CORE_ADDR *addr_p) |
| 912 | { |
| 913 | siginfo_t siginfo; |
| 914 | int i, tid; |
| 915 | struct aarch64_debug_reg_state *state; |
| 916 | |
| 917 | if (!linux_nat_get_siginfo (inferior_ptid, &siginfo)) |
| 918 | return 0; |
| 919 | |
| 920 | /* This must be a hardware breakpoint. */ |
| 921 | if (siginfo.si_signo != SIGTRAP |
| 922 | || (siginfo.si_code & 0xffff) != TRAP_HWBKPT) |
| 923 | return 0; |
| 924 | |
| 925 | /* Check if the address matches any watched address. */ |
| 926 | state = aarch64_get_debug_reg_state (ptid_get_pid (inferior_ptid)); |
| 927 | for (i = aarch64_num_wp_regs - 1; i >= 0; --i) |
| 928 | { |
| 929 | const unsigned int len = aarch64_watchpoint_length (state->dr_ctrl_wp[i]); |
| 930 | const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr; |
| 931 | const CORE_ADDR addr_watch = state->dr_addr_wp[i]; |
| 932 | |
| 933 | if (state->dr_ref_count_wp[i] |
| 934 | && DR_CONTROL_ENABLED (state->dr_ctrl_wp[i]) |
| 935 | && addr_trap >= addr_watch |
| 936 | && addr_trap < addr_watch + len) |
| 937 | { |
| 938 | *addr_p = addr_trap; |
| 939 | return 1; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | /* Implement the "to_stopped_by_watchpoint" target_ops method. */ |
| 947 | |
| 948 | static int |
| 949 | aarch64_linux_stopped_by_watchpoint (struct target_ops *ops) |
| 950 | { |
| 951 | CORE_ADDR addr; |
| 952 | |
| 953 | return aarch64_linux_stopped_data_address (ops, &addr); |
| 954 | } |
| 955 | |
| 956 | /* Implement the "to_watchpoint_addr_within_range" target_ops method. */ |
| 957 | |
| 958 | static int |
| 959 | aarch64_linux_watchpoint_addr_within_range (struct target_ops *target, |
| 960 | CORE_ADDR addr, |
| 961 | CORE_ADDR start, int length) |
| 962 | { |
| 963 | return start <= addr && start + length - 1 >= addr; |
| 964 | } |
| 965 | |
| 966 | /* Define AArch64 maintenance commands. */ |
| 967 | |
| 968 | static void |
| 969 | add_show_debug_regs_command (void) |
| 970 | { |
| 971 | /* A maintenance command to enable printing the internal DRi mirror |
| 972 | variables. */ |
| 973 | add_setshow_boolean_cmd ("show-debug-regs", class_maintenance, |
| 974 | &show_debug_regs, _("\ |
| 975 | Set whether to show variables that mirror the AArch64 debug registers."), _("\ |
| 976 | Show whether to show variables that mirror the AArch64 debug registers."), _("\ |
| 977 | Use \"on\" to enable, \"off\" to disable.\n\ |
| 978 | If enabled, the debug registers values are shown when GDB inserts\n\ |
| 979 | or removes a hardware breakpoint or watchpoint, and when the inferior\n\ |
| 980 | triggers a breakpoint or watchpoint."), |
| 981 | NULL, |
| 982 | NULL, |
| 983 | &maintenance_set_cmdlist, |
| 984 | &maintenance_show_cmdlist); |
| 985 | } |
| 986 | |
| 987 | /* -Wmissing-prototypes. */ |
| 988 | void _initialize_aarch64_linux_nat (void); |
| 989 | |
| 990 | void |
| 991 | _initialize_aarch64_linux_nat (void) |
| 992 | { |
| 993 | struct target_ops *t; |
| 994 | |
| 995 | /* Fill in the generic GNU/Linux methods. */ |
| 996 | t = linux_target (); |
| 997 | |
| 998 | add_show_debug_regs_command (); |
| 999 | |
| 1000 | /* Add our register access methods. */ |
| 1001 | t->to_fetch_registers = aarch64_linux_fetch_inferior_registers; |
| 1002 | t->to_store_registers = aarch64_linux_store_inferior_registers; |
| 1003 | |
| 1004 | t->to_read_description = aarch64_linux_read_description; |
| 1005 | |
| 1006 | t->to_can_use_hw_breakpoint = aarch64_linux_can_use_hw_breakpoint; |
| 1007 | t->to_insert_hw_breakpoint = aarch64_linux_insert_hw_breakpoint; |
| 1008 | t->to_remove_hw_breakpoint = aarch64_linux_remove_hw_breakpoint; |
| 1009 | t->to_region_ok_for_hw_watchpoint = |
| 1010 | aarch64_linux_region_ok_for_hw_watchpoint; |
| 1011 | t->to_insert_watchpoint = aarch64_linux_insert_watchpoint; |
| 1012 | t->to_remove_watchpoint = aarch64_linux_remove_watchpoint; |
| 1013 | t->to_stopped_by_watchpoint = aarch64_linux_stopped_by_watchpoint; |
| 1014 | t->to_stopped_data_address = aarch64_linux_stopped_data_address; |
| 1015 | t->to_watchpoint_addr_within_range = |
| 1016 | aarch64_linux_watchpoint_addr_within_range; |
| 1017 | |
| 1018 | /* Override the GNU/Linux inferior startup hook. */ |
| 1019 | super_post_startup_inferior = t->to_post_startup_inferior; |
| 1020 | t->to_post_startup_inferior = aarch64_linux_child_post_startup_inferior; |
| 1021 | |
| 1022 | /* Register the target. */ |
| 1023 | linux_nat_add_target (t); |
| 1024 | linux_nat_set_new_thread (t, aarch64_linux_new_thread); |
| 1025 | linux_nat_set_new_fork (t, aarch64_linux_new_fork); |
| 1026 | linux_nat_set_forget_process (t, aarch64_forget_process); |
| 1027 | linux_nat_set_prepare_to_resume (t, aarch64_linux_prepare_to_resume); |
| 1028 | } |