Update release README with new version numbers
[binutils-gdb.git] / gdb / aarch64-fbsd-nat.c
blobd8cf6227e73bdc463744a6d74ac224a808afa994
1 /* Native-dependent code for FreeBSD/aarch64.
3 Copyright (C) 2017-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "inferior.h"
23 #include "regcache.h"
24 #include "target.h"
25 #include "nat/aarch64-hw-point.h"
27 #include "elf/common.h"
29 #include <sys/param.h>
30 #include <sys/ptrace.h>
31 #include <machine/armreg.h>
32 #include <machine/reg.h>
34 #include "fbsd-nat.h"
35 #include "aarch64-tdep.h"
36 #include "aarch64-fbsd-tdep.h"
37 #include "aarch64-nat.h"
38 #include "inf-ptrace.h"
40 #if __FreeBSD_version >= 1400005
41 #define HAVE_DBREG
43 #include <unordered_set>
44 #endif
46 #ifdef HAVE_DBREG
47 struct aarch64_fbsd_nat_target final
48 : public aarch64_nat_target<fbsd_nat_target>
49 #else
50 struct aarch64_fbsd_nat_target final : public fbsd_nat_target
51 #endif
53 void fetch_registers (struct regcache *, int) override;
54 void store_registers (struct regcache *, int) override;
56 const struct target_desc *read_description () override;
58 #ifdef HAVE_DBREG
59 /* Hardware breakpoints and watchpoints. */
60 bool stopped_by_watchpoint () override;
61 bool stopped_data_address (CORE_ADDR *) override;
62 bool stopped_by_hw_breakpoint () override;
63 bool supports_stopped_by_hw_breakpoint () override;
65 void post_startup_inferior (ptid_t) override;
66 void post_attach (int pid) override;
68 void low_new_fork (ptid_t parent, pid_t child) override;
69 void low_delete_thread (thread_info *) override;
70 void low_prepare_to_resume (thread_info *) override;
72 private:
73 void probe_debug_regs (int pid);
74 static bool debug_regs_probed;
75 #endif
78 static aarch64_fbsd_nat_target the_aarch64_fbsd_nat_target;
80 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
81 for all registers. */
83 void
84 aarch64_fbsd_nat_target::fetch_registers (struct regcache *regcache,
85 int regnum)
87 fetch_register_set<struct reg> (regcache, regnum, PT_GETREGS,
88 &aarch64_fbsd_gregset);
89 fetch_register_set<struct fpreg> (regcache, regnum, PT_GETFPREGS,
90 &aarch64_fbsd_fpregset);
92 gdbarch *gdbarch = regcache->arch ();
93 aarch64_gdbarch_tdep *tdep = (aarch64_gdbarch_tdep *) gdbarch_tdep (gdbarch);
94 if (tdep->has_tls ())
96 const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
98 { 1, tdep->tls_regnum, 8 },
99 { 0 }
102 const struct regset aarch64_fbsd_tls_regset =
104 aarch64_fbsd_tls_regmap,
105 regcache_supply_regset, regcache_collect_regset
108 fetch_regset<uint64_t> (regcache, regnum, NT_ARM_TLS,
109 &aarch64_fbsd_tls_regset);
113 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
114 this for all registers. */
116 void
117 aarch64_fbsd_nat_target::store_registers (struct regcache *regcache,
118 int regnum)
120 store_register_set<struct reg> (regcache, regnum, PT_GETREGS, PT_SETREGS,
121 &aarch64_fbsd_gregset);
122 store_register_set<struct fpreg> (regcache, regnum, PT_GETFPREGS,
123 PT_SETFPREGS, &aarch64_fbsd_fpregset);
125 gdbarch *gdbarch = regcache->arch ();
126 aarch64_gdbarch_tdep *tdep = (aarch64_gdbarch_tdep *) gdbarch_tdep (gdbarch);
127 if (tdep->has_tls ())
129 const struct regcache_map_entry aarch64_fbsd_tls_regmap[] =
131 { 1, tdep->tls_regnum, 8 },
132 { 0 }
135 const struct regset aarch64_fbsd_tls_regset =
137 aarch64_fbsd_tls_regmap,
138 regcache_supply_regset, regcache_collect_regset
141 store_regset<uint64_t> (regcache, regnum, NT_ARM_TLS,
142 &aarch64_fbsd_tls_regset);
146 /* Implement the target read_description method. */
148 const struct target_desc *
149 aarch64_fbsd_nat_target::read_description ()
151 aarch64_features features;
152 features.tls = have_regset (inferior_ptid, NT_ARM_TLS) != 0;
153 return aarch64_read_description (features);
156 #ifdef HAVE_DBREG
157 bool aarch64_fbsd_nat_target::debug_regs_probed;
159 /* Set of threads which need to update debug registers on next resume. */
161 static std::unordered_set<lwpid_t> aarch64_debug_pending_threads;
163 /* Implement the "stopped_data_address" target_ops method. */
165 bool
166 aarch64_fbsd_nat_target::stopped_data_address (CORE_ADDR *addr_p)
168 siginfo_t siginfo;
169 struct aarch64_debug_reg_state *state;
171 if (!fbsd_nat_get_siginfo (inferior_ptid, &siginfo))
172 return false;
174 /* This must be a hardware breakpoint. */
175 if (siginfo.si_signo != SIGTRAP
176 || siginfo.si_code != TRAP_TRACE
177 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
178 return false;
180 const CORE_ADDR addr_trap = (CORE_ADDR) siginfo.si_addr;
182 /* Check if the address matches any watched address. */
183 state = aarch64_get_debug_reg_state (inferior_ptid.pid ());
184 return aarch64_stopped_data_address (state, addr_trap, addr_p);
187 /* Implement the "stopped_by_watchpoint" target_ops method. */
189 bool
190 aarch64_fbsd_nat_target::stopped_by_watchpoint ()
192 CORE_ADDR addr;
194 return stopped_data_address (&addr);
197 /* Implement the "stopped_by_hw_breakpoint" target_ops method. */
199 bool
200 aarch64_fbsd_nat_target::stopped_by_hw_breakpoint ()
202 siginfo_t siginfo;
203 struct aarch64_debug_reg_state *state;
205 if (!fbsd_nat_get_siginfo (inferior_ptid, &siginfo))
206 return false;
208 /* This must be a hardware breakpoint. */
209 if (siginfo.si_signo != SIGTRAP
210 || siginfo.si_code != TRAP_TRACE
211 || siginfo.si_trapno != EXCP_WATCHPT_EL0)
212 return false;
214 return !stopped_by_watchpoint();
217 /* Implement the "supports_stopped_by_hw_breakpoint" target_ops method. */
219 bool
220 aarch64_fbsd_nat_target::supports_stopped_by_hw_breakpoint ()
222 return true;
225 /* Fetch the hardware debug register capability information. */
227 void
228 aarch64_fbsd_nat_target::probe_debug_regs (int pid)
230 if (!debug_regs_probed)
232 struct dbreg reg;
234 debug_regs_probed = true;
235 aarch64_num_bp_regs = 0;
236 aarch64_num_wp_regs = 0;
238 if (ptrace(PT_GETDBREGS, pid, (PTRACE_TYPE_ARG3) &reg, 0) == 0)
240 switch (reg.db_debug_ver)
242 case AARCH64_DEBUG_ARCH_V8:
243 case AARCH64_DEBUG_ARCH_V8_1:
244 case AARCH64_DEBUG_ARCH_V8_2:
245 case AARCH64_DEBUG_ARCH_V8_4:
246 break;
247 default:
248 return;
251 aarch64_num_bp_regs = reg.db_nbkpts;
252 if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
254 warning (_("Unexpected number of hardware breakpoint registers"
255 " reported by ptrace, got %d, expected %d."),
256 aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
257 aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
259 aarch64_num_wp_regs = reg.db_nwtpts;
260 if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
262 warning (_("Unexpected number of hardware watchpoint registers"
263 " reported by ptrace, got %d, expected %d."),
264 aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
265 aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
271 /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
273 void
274 aarch64_fbsd_nat_target::post_startup_inferior (ptid_t ptid)
276 aarch64_remove_debug_reg_state (ptid.pid ());
277 probe_debug_regs (ptid.pid ());
278 fbsd_nat_target::post_startup_inferior (ptid);
281 /* Implement the "post_attach" target_ops method. */
283 void
284 aarch64_fbsd_nat_target::post_attach (int pid)
286 aarch64_remove_debug_reg_state (pid);
287 probe_debug_regs (pid);
288 fbsd_nat_target::post_attach (pid);
291 /* Implement the virtual fbsd_nat_target::low_new_fork method. */
293 void
294 aarch64_fbsd_nat_target::low_new_fork (ptid_t parent, pid_t child)
296 struct aarch64_debug_reg_state *parent_state, *child_state;
298 /* If there is no parent state, no watchpoints nor breakpoints have
299 been set, so there is nothing to do. */
300 parent_state = aarch64_lookup_debug_reg_state (parent.pid ());
301 if (parent_state == nullptr)
302 return;
304 /* The kernel clears debug registers in the new child process after
305 fork, but GDB core assumes the child inherits the watchpoints/hw
306 breakpoints of the parent, and will remove them all from the
307 forked off process. Copy the debug registers mirrors into the
308 new process so that all breakpoints and watchpoints can be
309 removed together. */
311 child_state = aarch64_get_debug_reg_state (child);
312 *child_state = *parent_state;
315 /* Mark debug register state "dirty" for all threads belonging to the
316 current inferior. */
318 void
319 aarch64_notify_debug_reg_change (ptid_t ptid,
320 int is_watchpoint, unsigned int idx)
322 for (thread_info *tp : current_inferior ()->non_exited_threads ())
324 if (tp->ptid.lwp_p ())
325 aarch64_debug_pending_threads.emplace (tp->ptid.lwp ());
329 /* Implement the virtual fbsd_nat_target::low_delete_thread method. */
331 void
332 aarch64_fbsd_nat_target::low_delete_thread (thread_info *tp)
334 gdb_assert(tp->ptid.lwp_p ());
335 aarch64_debug_pending_threads.erase (tp->ptid.lwp ());
338 /* Implement the virtual fbsd_nat_target::low_prepare_to_resume method. */
340 void
341 aarch64_fbsd_nat_target::low_prepare_to_resume (thread_info *tp)
343 gdb_assert(tp->ptid.lwp_p ());
345 if (aarch64_debug_pending_threads.erase (tp->ptid.lwp ()) == 0)
346 return;
348 struct aarch64_debug_reg_state *state =
349 aarch64_lookup_debug_reg_state (tp->ptid.pid ());
350 gdb_assert(state != nullptr);
352 struct dbreg reg;
353 memset (&reg, 0, sizeof(reg));
354 for (int i = 0; i < aarch64_num_bp_regs; i++)
356 reg.db_breakregs[i].dbr_addr = state->dr_addr_bp[i];
357 reg.db_breakregs[i].dbr_ctrl = state->dr_ctrl_bp[i];
359 for (int i = 0; i < aarch64_num_wp_regs; i++)
361 reg.db_watchregs[i].dbw_addr = state->dr_addr_wp[i];
362 reg.db_watchregs[i].dbw_ctrl = state->dr_ctrl_wp[i];
364 if (ptrace(PT_SETDBREGS, tp->ptid.lwp (), (PTRACE_TYPE_ARG3) &reg, 0) != 0)
365 error (_("Failed to set hardware debug registers"));
367 #else
368 /* A stub that should never be called. */
369 void
370 aarch64_notify_debug_reg_change (ptid_t ptid,
371 int is_watchpoint, unsigned int idx)
373 gdb_assert (true);
375 #endif
377 void _initialize_aarch64_fbsd_nat ();
378 void
379 _initialize_aarch64_fbsd_nat ()
381 #ifdef HAVE_DBREG
382 aarch64_initialize_hw_point ();
383 #endif
384 add_inf_child_target (&the_aarch64_fbsd_nat_target);