1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/regset.h>
4 #include <linux/hw_breakpoint.h>
8 #include "ptrace-decl.h"
10 void user_enable_single_step(struct task_struct *task)
12 struct pt_regs *regs = task->thread.regs;
15 regs_set_return_msr(regs, (regs->msr & ~MSR_BE) | MSR_SE);
16 set_tsk_thread_flag(task, TIF_SINGLESTEP);
19 void user_enable_block_step(struct task_struct *task)
21 struct pt_regs *regs = task->thread.regs;
24 regs_set_return_msr(regs, (regs->msr & ~MSR_SE) | MSR_BE);
25 set_tsk_thread_flag(task, TIF_SINGLESTEP);
28 void user_disable_single_step(struct task_struct *task)
30 struct pt_regs *regs = task->thread.regs;
33 regs_set_return_msr(regs, regs->msr & ~(MSR_SE | MSR_BE));
35 clear_tsk_thread_flag(task, TIF_SINGLESTEP);
38 void ppc_gethwdinfo(struct ppc_debug_info *dbginfo)
41 dbginfo->num_instruction_bps = 0;
42 if (ppc_breakpoint_available())
43 dbginfo->num_data_bps = nr_wp_slots();
45 dbginfo->num_data_bps = 0;
46 dbginfo->num_condition_regs = 0;
47 dbginfo->data_bp_alignment = sizeof(long);
48 dbginfo->sizeof_condition = 0;
49 if (IS_ENABLED(CONFIG_HAVE_HW_BREAKPOINT)) {
50 dbginfo->features = PPC_DEBUG_FEATURE_DATA_BP_RANGE;
52 dbginfo->features |= PPC_DEBUG_FEATURE_DATA_BP_DAWR;
54 dbginfo->features = 0;
56 if (cpu_has_feature(CPU_FTR_ARCH_31))
57 dbginfo->features |= PPC_DEBUG_FEATURE_DATA_BP_ARCH_31;
60 int ptrace_get_debugreg(struct task_struct *child, unsigned long addr,
61 unsigned long __user *datalp)
63 unsigned long dabr_fake;
65 /* We only support one DABR and no IABRS at the moment */
68 dabr_fake = ((child->thread.hw_brk[0].address & (~HW_BRK_TYPE_DABR)) |
69 (child->thread.hw_brk[0].type & HW_BRK_TYPE_DABR));
70 return put_user(dabr_fake, datalp);
74 * ptrace_set_debugreg() fakes DABR and DABR is only one. So even if
75 * internal hw supports more than one watchpoint, we support only one
76 * watchpoint with this interface.
78 int ptrace_set_debugreg(struct task_struct *task, unsigned long addr, unsigned long data)
80 #ifdef CONFIG_HAVE_HW_BREAKPOINT
82 struct thread_struct *thread = &task->thread;
83 struct perf_event *bp;
84 struct perf_event_attr attr;
85 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
87 struct arch_hw_breakpoint hw_brk;
89 /* For ppc64 we support one DABR and no IABR's at the moment (ppc64).
90 * For embedded processors we support one DAC and no IAC's at the
96 /* The bottom 3 bits in dabr are flags */
97 if ((data & ~0x7UL) >= TASK_SIZE)
100 /* For processors using DABR (i.e. 970), the bottom 3 bits are flags.
101 * It was assumed, on previous implementations, that 3 bits were
102 * passed together with the data address, fitting the design of the
103 * DABR register, as follows:
107 * bit 2: Breakpoint translation
109 * Thus, we use them here as so.
112 /* Ensure breakpoint translation bit is set */
113 if (data && !(data & HW_BRK_TYPE_TRANSLATE))
115 hw_brk.address = data & (~HW_BRK_TYPE_DABR);
116 hw_brk.type = (data & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
117 hw_brk.len = DABR_MAX_LEN;
118 hw_brk.hw_len = DABR_MAX_LEN;
119 set_bp = (data) && (hw_brk.type & HW_BRK_TYPE_RDWR);
120 #ifdef CONFIG_HAVE_HW_BREAKPOINT
121 bp = thread->ptrace_bps[0];
124 unregister_hw_breakpoint(bp);
125 thread->ptrace_bps[0] = NULL;
131 attr.bp_addr = hw_brk.address;
132 attr.bp_len = DABR_MAX_LEN;
133 arch_bp_generic_fields(hw_brk.type, &attr.bp_type);
135 /* Enable breakpoint */
136 attr.disabled = false;
138 ret = modify_user_hw_breakpoint(bp, &attr);
142 thread->ptrace_bps[0] = bp;
143 thread->hw_brk[0] = hw_brk;
147 /* Create a new breakpoint request if one doesn't exist already */
148 hw_breakpoint_init(&attr);
149 attr.bp_addr = hw_brk.address;
150 attr.bp_len = DABR_MAX_LEN;
151 arch_bp_generic_fields(hw_brk.type,
154 thread->ptrace_bps[0] = bp = register_user_hw_breakpoint(&attr,
155 ptrace_triggered, NULL, task);
157 thread->ptrace_bps[0] = NULL;
161 #else /* !CONFIG_HAVE_HW_BREAKPOINT */
162 if (set_bp && (!ppc_breakpoint_available()))
164 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
165 task->thread.hw_brk[0] = hw_brk;
169 #ifdef CONFIG_HAVE_HW_BREAKPOINT
170 static int find_empty_ptrace_bp(struct thread_struct *thread)
174 for (i = 0; i < nr_wp_slots(); i++) {
175 if (!thread->ptrace_bps[i])
182 static int find_empty_hw_brk(struct thread_struct *thread)
186 for (i = 0; i < nr_wp_slots(); i++) {
187 if (!thread->hw_brk[i].address)
193 long ppc_set_hwdebug(struct task_struct *child, struct ppc_hw_breakpoint *bp_info)
196 #ifdef CONFIG_HAVE_HW_BREAKPOINT
198 struct thread_struct *thread = &child->thread;
199 struct perf_event *bp;
200 struct perf_event_attr attr;
201 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
202 struct arch_hw_breakpoint brk;
204 if (bp_info->version != 1)
207 * We only support one data breakpoint
209 if ((bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_RW) == 0 ||
210 (bp_info->trigger_type & ~PPC_BREAKPOINT_TRIGGER_RW) != 0 ||
211 bp_info->condition_mode != PPC_BREAKPOINT_CONDITION_NONE)
214 if ((unsigned long)bp_info->addr >= TASK_SIZE)
217 brk.address = ALIGN_DOWN(bp_info->addr, HW_BREAKPOINT_SIZE);
218 brk.type = HW_BRK_TYPE_TRANSLATE | HW_BRK_TYPE_PRIV_ALL;
219 brk.len = DABR_MAX_LEN;
220 brk.hw_len = DABR_MAX_LEN;
221 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_READ)
222 brk.type |= HW_BRK_TYPE_READ;
223 if (bp_info->trigger_type & PPC_BREAKPOINT_TRIGGER_WRITE)
224 brk.type |= HW_BRK_TYPE_WRITE;
225 #ifdef CONFIG_HAVE_HW_BREAKPOINT
226 if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_RANGE_INCLUSIVE)
227 len = bp_info->addr2 - bp_info->addr;
228 else if (bp_info->addr_mode == PPC_BREAKPOINT_MODE_EXACT)
233 i = find_empty_ptrace_bp(thread);
237 /* Create a new breakpoint request if one doesn't exist already */
238 hw_breakpoint_init(&attr);
239 attr.bp_addr = (unsigned long)bp_info->addr;
241 arch_bp_generic_fields(brk.type, &attr.bp_type);
243 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, NULL, child);
244 thread->ptrace_bps[i] = bp;
246 thread->ptrace_bps[i] = NULL;
251 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
253 if (bp_info->addr_mode != PPC_BREAKPOINT_MODE_EXACT)
256 i = find_empty_hw_brk(&child->thread);
260 if (!ppc_breakpoint_available())
263 child->thread.hw_brk[i] = brk;
268 long ppc_del_hwdebug(struct task_struct *child, long data)
270 #ifdef CONFIG_HAVE_HW_BREAKPOINT
272 struct thread_struct *thread = &child->thread;
273 struct perf_event *bp;
274 #endif /* CONFIG_HAVE_HW_BREAKPOINT */
275 if (data < 1 || data > nr_wp_slots())
278 #ifdef CONFIG_HAVE_HW_BREAKPOINT
279 bp = thread->ptrace_bps[data - 1];
281 unregister_hw_breakpoint(bp);
282 thread->ptrace_bps[data - 1] = NULL;
287 #else /* CONFIG_HAVE_HW_BREAKPOINT */
288 if (!(child->thread.hw_brk[data - 1].flags & HW_BRK_FLAG_DISABLED) &&
289 child->thread.hw_brk[data - 1].address == 0)
292 child->thread.hw_brk[data - 1].address = 0;
293 child->thread.hw_brk[data - 1].type = 0;
294 child->thread.hw_brk[data - 1].flags = 0;
295 #endif /* CONFIG_HAVE_HW_BREAKPOINT */