powerpc/watchpoint: Use loop for thread_struct->ptrace_bps
[linux-2.6-microblaze.git] / arch / powerpc / kernel / hw_breakpoint.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
4  * using the CPU's debug registers. Derived from
5  * "arch/x86/kernel/hw_breakpoint.c"
6  *
7  * Copyright 2010 IBM Corporation
8  * Author: K.Prasad <prasad@linux.vnet.ibm.com>
9  */
10
11 #include <linux/hw_breakpoint.h>
12 #include <linux/notifier.h>
13 #include <linux/kprobes.h>
14 #include <linux/percpu.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
18 #include <linux/debugfs.h>
19 #include <linux/init.h>
20
21 #include <asm/hw_breakpoint.h>
22 #include <asm/processor.h>
23 #include <asm/sstep.h>
24 #include <asm/debug.h>
25 #include <asm/debugfs.h>
26 #include <asm/hvcall.h>
27 #include <asm/inst.h>
28 #include <linux/uaccess.h>
29
30 /*
31  * Stores the breakpoints currently in use on each breakpoint address
32  * register for every cpu
33  */
34 static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
35
36 /*
37  * Returns total number of data or instruction breakpoints available.
38  */
39 int hw_breakpoint_slots(int type)
40 {
41         if (type == TYPE_DATA)
42                 return nr_wp_slots();
43         return 0;               /* no instruction breakpoints available */
44 }
45
46 /*
47  * Install a perf counter breakpoint.
48  *
49  * We seek a free debug address register and use it for this
50  * breakpoint.
51  *
52  * Atomic: we hold the counter->ctx->lock and we only handle variables
53  * and registers local to this cpu.
54  */
55 int arch_install_hw_breakpoint(struct perf_event *bp)
56 {
57         struct arch_hw_breakpoint *info = counter_arch_bp(bp);
58         struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
59
60         *slot = bp;
61
62         /*
63          * Do not install DABR values if the instruction must be single-stepped.
64          * If so, DABR will be populated in single_step_dabr_instruction().
65          */
66         if (current->thread.last_hit_ubp != bp)
67                 __set_breakpoint(0, info);
68
69         return 0;
70 }
71
72 /*
73  * Uninstall the breakpoint contained in the given counter.
74  *
75  * First we search the debug address register it uses and then we disable
76  * it.
77  *
78  * Atomic: we hold the counter->ctx->lock and we only handle variables
79  * and registers local to this cpu.
80  */
81 void arch_uninstall_hw_breakpoint(struct perf_event *bp)
82 {
83         struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
84
85         if (*slot != bp) {
86                 WARN_ONCE(1, "Can't find the breakpoint");
87                 return;
88         }
89
90         *slot = NULL;
91         hw_breakpoint_disable();
92 }
93
94 /*
95  * Perform cleanup of arch-specific counters during unregistration
96  * of the perf-event
97  */
98 void arch_unregister_hw_breakpoint(struct perf_event *bp)
99 {
100         /*
101          * If the breakpoint is unregistered between a hw_breakpoint_handler()
102          * and the single_step_dabr_instruction(), then cleanup the breakpoint
103          * restoration variables to prevent dangling pointers.
104          * FIXME, this should not be using bp->ctx at all! Sayeth peterz.
105          */
106         if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
107                 bp->ctx->task->thread.last_hit_ubp = NULL;
108 }
109
110 /*
111  * Check for virtual address in kernel space.
112  */
113 int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
114 {
115         return is_kernel_addr(hw->address);
116 }
117
118 int arch_bp_generic_fields(int type, int *gen_bp_type)
119 {
120         *gen_bp_type = 0;
121         if (type & HW_BRK_TYPE_READ)
122                 *gen_bp_type |= HW_BREAKPOINT_R;
123         if (type & HW_BRK_TYPE_WRITE)
124                 *gen_bp_type |= HW_BREAKPOINT_W;
125         if (*gen_bp_type == 0)
126                 return -EINVAL;
127         return 0;
128 }
129
130 /*
131  * Watchpoint match range is always doubleword(8 bytes) aligned on
132  * powerpc. If the given range is crossing doubleword boundary, we
133  * need to increase the length such that next doubleword also get
134  * covered. Ex,
135  *
136  *          address   len = 6 bytes
137  *                |=========.
138  *   |------------v--|------v--------|
139  *   | | | | | | | | | | | | | | | | |
140  *   |---------------|---------------|
141  *    <---8 bytes--->
142  *
143  * In this case, we should configure hw as:
144  *   start_addr = address & ~HW_BREAKPOINT_ALIGN
145  *   len = 16 bytes
146  *
147  * @start_addr and @end_addr are inclusive.
148  */
149 static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
150 {
151         u16 max_len = DABR_MAX_LEN;
152         u16 hw_len;
153         unsigned long start_addr, end_addr;
154
155         start_addr = hw->address & ~HW_BREAKPOINT_ALIGN;
156         end_addr = (hw->address + hw->len - 1) | HW_BREAKPOINT_ALIGN;
157         hw_len = end_addr - start_addr + 1;
158
159         if (dawr_enabled()) {
160                 max_len = DAWR_MAX_LEN;
161                 /* DAWR region can't cross 512 bytes boundary */
162                 if ((start_addr >> 9) != (end_addr >> 9))
163                         return -EINVAL;
164         } else if (IS_ENABLED(CONFIG_PPC_8xx)) {
165                 /* 8xx can setup a range without limitation */
166                 max_len = U16_MAX;
167         }
168
169         if (hw_len > max_len)
170                 return -EINVAL;
171
172         hw->hw_len = hw_len;
173         return 0;
174 }
175
176 /*
177  * Validate the arch-specific HW Breakpoint register settings
178  */
179 int hw_breakpoint_arch_parse(struct perf_event *bp,
180                              const struct perf_event_attr *attr,
181                              struct arch_hw_breakpoint *hw)
182 {
183         int ret = -EINVAL;
184
185         if (!bp || !attr->bp_len)
186                 return ret;
187
188         hw->type = HW_BRK_TYPE_TRANSLATE;
189         if (attr->bp_type & HW_BREAKPOINT_R)
190                 hw->type |= HW_BRK_TYPE_READ;
191         if (attr->bp_type & HW_BREAKPOINT_W)
192                 hw->type |= HW_BRK_TYPE_WRITE;
193         if (hw->type == HW_BRK_TYPE_TRANSLATE)
194                 /* must set alteast read or write */
195                 return ret;
196         if (!attr->exclude_user)
197                 hw->type |= HW_BRK_TYPE_USER;
198         if (!attr->exclude_kernel)
199                 hw->type |= HW_BRK_TYPE_KERNEL;
200         if (!attr->exclude_hv)
201                 hw->type |= HW_BRK_TYPE_HYP;
202         hw->address = attr->bp_addr;
203         hw->len = attr->bp_len;
204
205         if (!ppc_breakpoint_available())
206                 return -ENODEV;
207
208         return hw_breakpoint_validate_len(hw);
209 }
210
211 /*
212  * Restores the breakpoint on the debug registers.
213  * Invoke this function if it is known that the execution context is
214  * about to change to cause loss of MSR_SE settings.
215  */
216 void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
217 {
218         struct arch_hw_breakpoint *info;
219
220         if (likely(!tsk->thread.last_hit_ubp))
221                 return;
222
223         info = counter_arch_bp(tsk->thread.last_hit_ubp);
224         regs->msr &= ~MSR_SE;
225         __set_breakpoint(0, info);
226         tsk->thread.last_hit_ubp = NULL;
227 }
228
229 static bool dar_within_range(unsigned long dar, struct arch_hw_breakpoint *info)
230 {
231         return ((info->address <= dar) && (dar - info->address < info->len));
232 }
233
234 static bool
235 dar_range_overlaps(unsigned long dar, int size, struct arch_hw_breakpoint *info)
236 {
237         return ((dar <= info->address + info->len - 1) &&
238                 (dar + size - 1 >= info->address));
239 }
240
241 /*
242  * Handle debug exception notifications.
243  */
244 static bool stepping_handler(struct pt_regs *regs, struct perf_event *bp,
245                              struct arch_hw_breakpoint *info)
246 {
247         struct ppc_inst instr = ppc_inst(0);
248         int ret, type, size;
249         struct instruction_op op;
250         unsigned long addr = info->address;
251
252         if (__get_user_instr_inatomic(instr, (void __user *)regs->nip))
253                 goto fail;
254
255         ret = analyse_instr(&op, regs, instr);
256         type = GETTYPE(op.type);
257         size = GETSIZE(op.type);
258
259         if (!ret && (type == LARX || type == STCX)) {
260                 printk_ratelimited("Breakpoint hit on instruction that can't be emulated."
261                                    " Breakpoint at 0x%lx will be disabled.\n", addr);
262                 goto disable;
263         }
264
265         /*
266          * If it's extraneous event, we still need to emulate/single-
267          * step the instruction, but we don't generate an event.
268          */
269         if (size && !dar_range_overlaps(regs->dar, size, info))
270                 info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
271
272         /* Do not emulate user-space instructions, instead single-step them */
273         if (user_mode(regs)) {
274                 current->thread.last_hit_ubp = bp;
275                 regs->msr |= MSR_SE;
276                 return false;
277         }
278
279         if (!emulate_step(regs, instr))
280                 goto fail;
281
282         return true;
283
284 fail:
285         /*
286          * We've failed in reliably handling the hw-breakpoint. Unregister
287          * it and throw a warning message to let the user know about it.
288          */
289         WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
290                 "0x%lx will be disabled.", addr);
291
292 disable:
293         perf_event_disable_inatomic(bp);
294         return false;
295 }
296
297 int hw_breakpoint_handler(struct die_args *args)
298 {
299         int rc = NOTIFY_STOP;
300         struct perf_event *bp;
301         struct pt_regs *regs = args->regs;
302         struct arch_hw_breakpoint *info;
303
304         /* Disable breakpoints during exception handling */
305         hw_breakpoint_disable();
306
307         /*
308          * The counter may be concurrently released but that can only
309          * occur from a call_rcu() path. We can then safely fetch
310          * the breakpoint, use its callback, touch its counter
311          * while we are in an rcu_read_lock() path.
312          */
313         rcu_read_lock();
314
315         bp = __this_cpu_read(bp_per_reg);
316         if (!bp) {
317                 rc = NOTIFY_DONE;
318                 goto out;
319         }
320         info = counter_arch_bp(bp);
321
322         /*
323          * Return early after invoking user-callback function without restoring
324          * DABR if the breakpoint is from ptrace which always operates in
325          * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
326          * generated in do_dabr().
327          */
328         if (bp->overflow_handler == ptrace_triggered) {
329                 perf_bp_event(bp, regs);
330                 rc = NOTIFY_DONE;
331                 goto out;
332         }
333
334         info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
335         if (IS_ENABLED(CONFIG_PPC_8xx)) {
336                 if (!dar_within_range(regs->dar, info))
337                         info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
338         } else {
339                 if (!stepping_handler(regs, bp, info))
340                         goto out;
341         }
342
343         /*
344          * As a policy, the callback is invoked in a 'trigger-after-execute'
345          * fashion
346          */
347         if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
348                 perf_bp_event(bp, regs);
349
350         __set_breakpoint(0, info);
351 out:
352         rcu_read_unlock();
353         return rc;
354 }
355 NOKPROBE_SYMBOL(hw_breakpoint_handler);
356
357 /*
358  * Handle single-step exceptions following a DABR hit.
359  */
360 static int single_step_dabr_instruction(struct die_args *args)
361 {
362         struct pt_regs *regs = args->regs;
363         struct perf_event *bp = NULL;
364         struct arch_hw_breakpoint *info;
365
366         bp = current->thread.last_hit_ubp;
367         /*
368          * Check if we are single-stepping as a result of a
369          * previous HW Breakpoint exception
370          */
371         if (!bp)
372                 return NOTIFY_DONE;
373
374         info = counter_arch_bp(bp);
375
376         /*
377          * We shall invoke the user-defined callback function in the single
378          * stepping handler to confirm to 'trigger-after-execute' semantics
379          */
380         if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
381                 perf_bp_event(bp, regs);
382
383         __set_breakpoint(0, info);
384         current->thread.last_hit_ubp = NULL;
385
386         /*
387          * If the process was being single-stepped by ptrace, let the
388          * other single-step actions occur (e.g. generate SIGTRAP).
389          */
390         if (test_thread_flag(TIF_SINGLESTEP))
391                 return NOTIFY_DONE;
392
393         return NOTIFY_STOP;
394 }
395 NOKPROBE_SYMBOL(single_step_dabr_instruction);
396
397 /*
398  * Handle debug exception notifications.
399  */
400 int hw_breakpoint_exceptions_notify(
401                 struct notifier_block *unused, unsigned long val, void *data)
402 {
403         int ret = NOTIFY_DONE;
404
405         switch (val) {
406         case DIE_DABR_MATCH:
407                 ret = hw_breakpoint_handler(data);
408                 break;
409         case DIE_SSTEP:
410                 ret = single_step_dabr_instruction(data);
411                 break;
412         }
413
414         return ret;
415 }
416 NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
417
418 /*
419  * Release the user breakpoints used by ptrace
420  */
421 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
422 {
423         int i;
424         struct thread_struct *t = &tsk->thread;
425
426         for (i = 0; i < nr_wp_slots(); i++) {
427                 unregister_hw_breakpoint(t->ptrace_bps[i]);
428                 t->ptrace_bps[i] = NULL;
429         }
430 }
431
432 void hw_breakpoint_pmu_read(struct perf_event *bp)
433 {
434         /* TODO */
435 }
436
437 void ptrace_triggered(struct perf_event *bp,
438                       struct perf_sample_data *data, struct pt_regs *regs)
439 {
440         struct perf_event_attr attr;
441
442         /*
443          * Disable the breakpoint request here since ptrace has defined a
444          * one-shot behaviour for breakpoint exceptions in PPC64.
445          * The SIGTRAP signal is generated automatically for us in do_dabr().
446          * We don't have to do anything about that here
447          */
448         attr = bp->attr;
449         attr.disabled = true;
450         modify_user_hw_breakpoint(bp, &attr);
451 }