Loading...
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/spinlock.h>
19#include <linux/debugfs.h>
20#include <linux/init.h>
21
22#include <asm/hw_breakpoint.h>
23#include <asm/processor.h>
24#include <asm/sstep.h>
25#include <asm/debug.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 */
34static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM_MAX]);
35
36/*
37 * Returns total number of data or instruction breakpoints available.
38 */
39int 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/*
48 * Install a perf counter breakpoint.
49 *
50 * We seek a free debug address register and use it for this
51 * breakpoint.
52 *
53 * Atomic: we hold the counter->ctx->lock and we only handle variables
54 * and registers local to this cpu.
55 */
56int arch_install_hw_breakpoint(struct perf_event *bp)
57{
58 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
59 struct perf_event **slot;
60 int i;
61
62 for (i = 0; i < nr_wp_slots(); i++) {
63 slot = this_cpu_ptr(&bp_per_reg[i]);
64 if (!*slot) {
65 *slot = bp;
66 break;
67 }
68 }
69
70 if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
71 return -EBUSY;
72
73 /*
74 * Do not install DABR values if the instruction must be single-stepped.
75 * If so, DABR will be populated in single_step_dabr_instruction().
76 */
77 if (!info->perf_single_step)
78 __set_breakpoint(i, info);
79
80 return 0;
81}
82
83/*
84 * Uninstall the breakpoint contained in the given counter.
85 *
86 * First we search the debug address register it uses and then we disable
87 * it.
88 *
89 * Atomic: we hold the counter->ctx->lock and we only handle variables
90 * and registers local to this cpu.
91 */
92void arch_uninstall_hw_breakpoint(struct perf_event *bp)
93{
94 struct arch_hw_breakpoint null_brk = {0};
95 struct perf_event **slot;
96 int i;
97
98 for (i = 0; i < nr_wp_slots(); i++) {
99 slot = this_cpu_ptr(&bp_per_reg[i]);
100 if (*slot == bp) {
101 *slot = NULL;
102 break;
103 }
104 }
105
106 if (WARN_ONCE(i == nr_wp_slots(), "Can't find any breakpoint slot"))
107 return;
108
109 __set_breakpoint(i, &null_brk);
110}
111
112static bool is_ptrace_bp(struct perf_event *bp)
113{
114 return bp->overflow_handler == ptrace_triggered;
115}
116
117/*
118 * Check for virtual address in kernel space.
119 */
120int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
121{
122 return is_kernel_addr(hw->address);
123}
124
125int arch_bp_generic_fields(int type, int *gen_bp_type)
126{
127 *gen_bp_type = 0;
128 if (type & HW_BRK_TYPE_READ)
129 *gen_bp_type |= HW_BREAKPOINT_R;
130 if (type & HW_BRK_TYPE_WRITE)
131 *gen_bp_type |= HW_BREAKPOINT_W;
132 if (*gen_bp_type == 0)
133 return -EINVAL;
134 return 0;
135}
136
137/*
138 * Watchpoint match range is always doubleword(8 bytes) aligned on
139 * powerpc. If the given range is crossing doubleword boundary, we
140 * need to increase the length such that next doubleword also get
141 * covered. Ex,
142 *
143 * address len = 6 bytes
144 * |=========.
145 * |------------v--|------v--------|
146 * | | | | | | | | | | | | | | | | |
147 * |---------------|---------------|
148 * <---8 bytes--->
149 *
150 * In this case, we should configure hw as:
151 * start_addr = address & ~(HW_BREAKPOINT_SIZE - 1)
152 * len = 16 bytes
153 *
154 * @start_addr is inclusive but @end_addr is exclusive.
155 */
156static int hw_breakpoint_validate_len(struct arch_hw_breakpoint *hw)
157{
158 u16 max_len = DABR_MAX_LEN;
159 u16 hw_len;
160 unsigned long start_addr, end_addr;
161
162 start_addr = ALIGN_DOWN(hw->address, HW_BREAKPOINT_SIZE);
163 end_addr = ALIGN(hw->address + hw->len, HW_BREAKPOINT_SIZE);
164 hw_len = end_addr - start_addr;
165
166 if (dawr_enabled()) {
167 max_len = DAWR_MAX_LEN;
168 /* DAWR region can't cross 512 bytes boundary on p10 predecessors */
169 if (!cpu_has_feature(CPU_FTR_ARCH_31) &&
170 (ALIGN_DOWN(start_addr, SZ_512) != ALIGN_DOWN(end_addr - 1, SZ_512)))
171 return -EINVAL;
172 } else if (IS_ENABLED(CONFIG_PPC_8xx)) {
173 /* 8xx can setup a range without limitation */
174 max_len = U16_MAX;
175 }
176
177 if (hw_len > max_len)
178 return -EINVAL;
179
180 hw->hw_len = hw_len;
181 return 0;
182}
183
184/*
185 * Validate the arch-specific HW Breakpoint register settings
186 */
187int hw_breakpoint_arch_parse(struct perf_event *bp,
188 const struct perf_event_attr *attr,
189 struct arch_hw_breakpoint *hw)
190{
191 int ret = -EINVAL;
192
193 if (!bp || !attr->bp_len)
194 return ret;
195
196 hw->type = HW_BRK_TYPE_TRANSLATE;
197 if (attr->bp_type & HW_BREAKPOINT_R)
198 hw->type |= HW_BRK_TYPE_READ;
199 if (attr->bp_type & HW_BREAKPOINT_W)
200 hw->type |= HW_BRK_TYPE_WRITE;
201 if (hw->type == HW_BRK_TYPE_TRANSLATE)
202 /* must set alteast read or write */
203 return ret;
204 if (!attr->exclude_user)
205 hw->type |= HW_BRK_TYPE_USER;
206 if (!attr->exclude_kernel)
207 hw->type |= HW_BRK_TYPE_KERNEL;
208 if (!attr->exclude_hv)
209 hw->type |= HW_BRK_TYPE_HYP;
210 hw->address = attr->bp_addr;
211 hw->len = attr->bp_len;
212
213 if (!ppc_breakpoint_available())
214 return -ENODEV;
215
216 return hw_breakpoint_validate_len(hw);
217}
218
219/*
220 * Restores the breakpoint on the debug registers.
221 * Invoke this function if it is known that the execution context is
222 * about to change to cause loss of MSR_SE settings.
223 *
224 * The perf watchpoint will simply re-trigger once the thread is started again,
225 * and the watchpoint handler will set up MSR_SE and perf_single_step as
226 * needed.
227 */
228void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
229{
230 struct arch_hw_breakpoint *info;
231 int i;
232
233 preempt_disable();
234
235 for (i = 0; i < nr_wp_slots(); i++) {
236 struct perf_event *bp = __this_cpu_read(bp_per_reg[i]);
237
238 if (unlikely(bp && counter_arch_bp(bp)->perf_single_step))
239 goto reset;
240 }
241 goto out;
242
243reset:
244 regs_set_return_msr(regs, regs->msr & ~MSR_SE);
245 for (i = 0; i < nr_wp_slots(); i++) {
246 info = counter_arch_bp(__this_cpu_read(bp_per_reg[i]));
247 __set_breakpoint(i, info);
248 info->perf_single_step = false;
249 }
250
251out:
252 preempt_enable();
253}
254
255static bool is_larx_stcx_instr(int type)
256{
257 return type == LARX || type == STCX;
258}
259
260static bool is_octword_vsx_instr(int type, int size)
261{
262 return ((type == LOAD_VSX || type == STORE_VSX) && size == 32);
263}
264
265/*
266 * We've failed in reliably handling the hw-breakpoint. Unregister
267 * it and throw a warning message to let the user know about it.
268 */
269static void handler_error(struct perf_event *bp)
270{
271 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at 0x%lx will be disabled.",
272 counter_arch_bp(bp)->address);
273 perf_event_disable_inatomic(bp);
274}
275
276static void larx_stcx_err(struct perf_event *bp)
277{
278 printk_ratelimited("Breakpoint hit on instruction that can't be emulated. Breakpoint at 0x%lx will be disabled.\n",
279 counter_arch_bp(bp)->address);
280 perf_event_disable_inatomic(bp);
281}
282
283static bool stepping_handler(struct pt_regs *regs, struct perf_event **bp,
284 int *hit, ppc_inst_t instr)
285{
286 int i;
287 int stepped;
288
289 /* Do not emulate user-space instructions, instead single-step them */
290 if (user_mode(regs)) {
291 for (i = 0; i < nr_wp_slots(); i++) {
292 if (!hit[i])
293 continue;
294
295 counter_arch_bp(bp[i])->perf_single_step = true;
296 bp[i] = NULL;
297 }
298 regs_set_return_msr(regs, regs->msr | MSR_SE);
299 return false;
300 }
301
302 stepped = emulate_step(regs, instr);
303 if (!stepped) {
304 for (i = 0; i < nr_wp_slots(); i++) {
305 if (!hit[i])
306 continue;
307 handler_error(bp[i]);
308 bp[i] = NULL;
309 }
310 return false;
311 }
312 return true;
313}
314
315static void handle_p10dd1_spurious_exception(struct perf_event **bp,
316 int *hit, unsigned long ea)
317{
318 int i;
319 unsigned long hw_end_addr;
320
321 /*
322 * Handle spurious exception only when any bp_per_reg is set.
323 * Otherwise this might be created by xmon and not actually a
324 * spurious exception.
325 */
326 for (i = 0; i < nr_wp_slots(); i++) {
327 struct arch_hw_breakpoint *info;
328
329 if (!bp[i])
330 continue;
331
332 info = counter_arch_bp(bp[i]);
333
334 hw_end_addr = ALIGN(info->address + info->len, HW_BREAKPOINT_SIZE);
335
336 /*
337 * Ending address of DAWR range is less than starting
338 * address of op.
339 */
340 if ((hw_end_addr - 1) >= ea)
341 continue;
342
343 /*
344 * Those addresses need to be in the same or in two
345 * consecutive 512B blocks;
346 */
347 if (((hw_end_addr - 1) >> 10) != (ea >> 10))
348 continue;
349
350 /*
351 * 'op address + 64B' generates an address that has a
352 * carry into bit 52 (crosses 2K boundary).
353 */
354 if ((ea & 0x800) == ((ea + 64) & 0x800))
355 continue;
356
357 break;
358 }
359
360 if (i == nr_wp_slots())
361 return;
362
363 for (i = 0; i < nr_wp_slots(); i++) {
364 if (bp[i]) {
365 hit[i] = 1;
366 counter_arch_bp(bp[i])->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
367 }
368 }
369}
370
371/*
372 * Handle a DABR or DAWR exception.
373 *
374 * Called in atomic context.
375 */
376int hw_breakpoint_handler(struct die_args *args)
377{
378 bool err = false;
379 int rc = NOTIFY_STOP;
380 struct perf_event *bp[HBP_NUM_MAX] = { NULL };
381 struct pt_regs *regs = args->regs;
382 int i;
383 int hit[HBP_NUM_MAX] = {0};
384 int nr_hit = 0;
385 bool ptrace_bp = false;
386 ppc_inst_t instr = ppc_inst(0);
387 int type = 0;
388 int size = 0;
389 unsigned long ea = 0;
390
391 /* Disable breakpoints during exception handling */
392 hw_breakpoint_disable();
393
394 /*
395 * The counter may be concurrently released but that can only
396 * occur from a call_rcu() path. We can then safely fetch
397 * the breakpoint, use its callback, touch its counter
398 * while we are in an rcu_read_lock() path.
399 */
400 rcu_read_lock();
401
402 if (!IS_ENABLED(CONFIG_PPC_8xx))
403 wp_get_instr_detail(regs, &instr, &type, &size, &ea);
404
405 for (i = 0; i < nr_wp_slots(); i++) {
406 struct arch_hw_breakpoint *info;
407
408 bp[i] = __this_cpu_read(bp_per_reg[i]);
409 if (!bp[i])
410 continue;
411
412 info = counter_arch_bp(bp[i]);
413 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
414
415 if (wp_check_constraints(regs, instr, ea, type, size, info)) {
416 if (!IS_ENABLED(CONFIG_PPC_8xx) &&
417 ppc_inst_equal(instr, ppc_inst(0))) {
418 handler_error(bp[i]);
419 bp[i] = NULL;
420 err = 1;
421 continue;
422 }
423
424 if (is_ptrace_bp(bp[i]))
425 ptrace_bp = true;
426 hit[i] = 1;
427 nr_hit++;
428 }
429 }
430
431 if (err)
432 goto reset;
433
434 if (!nr_hit) {
435 /* Workaround for Power10 DD1 */
436 if (!IS_ENABLED(CONFIG_PPC_8xx) && mfspr(SPRN_PVR) == 0x800100 &&
437 is_octword_vsx_instr(type, size)) {
438 handle_p10dd1_spurious_exception(bp, hit, ea);
439 } else {
440 rc = NOTIFY_DONE;
441 goto out;
442 }
443 }
444
445 /*
446 * Return early after invoking user-callback function without restoring
447 * DABR if the breakpoint is from ptrace which always operates in
448 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
449 * generated in do_dabr().
450 */
451 if (ptrace_bp) {
452 for (i = 0; i < nr_wp_slots(); i++) {
453 if (!hit[i] || !is_ptrace_bp(bp[i]))
454 continue;
455 perf_bp_event(bp[i], regs);
456 bp[i] = NULL;
457 }
458 rc = NOTIFY_DONE;
459 goto reset;
460 }
461
462 if (!IS_ENABLED(CONFIG_PPC_8xx)) {
463 if (is_larx_stcx_instr(type)) {
464 for (i = 0; i < nr_wp_slots(); i++) {
465 if (!hit[i])
466 continue;
467 larx_stcx_err(bp[i]);
468 bp[i] = NULL;
469 }
470 goto reset;
471 }
472
473 if (!stepping_handler(regs, bp, hit, instr))
474 goto reset;
475 }
476
477 /*
478 * As a policy, the callback is invoked in a 'trigger-after-execute'
479 * fashion
480 */
481 for (i = 0; i < nr_wp_slots(); i++) {
482 if (!hit[i])
483 continue;
484 if (!(counter_arch_bp(bp[i])->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
485 perf_bp_event(bp[i], regs);
486 }
487
488reset:
489 for (i = 0; i < nr_wp_slots(); i++) {
490 if (!bp[i])
491 continue;
492 __set_breakpoint(i, counter_arch_bp(bp[i]));
493 }
494
495out:
496 rcu_read_unlock();
497 return rc;
498}
499NOKPROBE_SYMBOL(hw_breakpoint_handler);
500
501/*
502 * Handle single-step exceptions following a DABR hit.
503 *
504 * Called in atomic context.
505 */
506static int single_step_dabr_instruction(struct die_args *args)
507{
508 struct pt_regs *regs = args->regs;
509 bool found = false;
510
511 /*
512 * Check if we are single-stepping as a result of a
513 * previous HW Breakpoint exception
514 */
515 for (int i = 0; i < nr_wp_slots(); i++) {
516 struct perf_event *bp;
517 struct arch_hw_breakpoint *info;
518
519 bp = __this_cpu_read(bp_per_reg[i]);
520
521 if (!bp)
522 continue;
523
524 info = counter_arch_bp(bp);
525
526 if (!info->perf_single_step)
527 continue;
528
529 found = true;
530
531 /*
532 * We shall invoke the user-defined callback function in the
533 * single stepping handler to confirm to 'trigger-after-execute'
534 * semantics
535 */
536 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
537 perf_bp_event(bp, regs);
538
539 info->perf_single_step = false;
540 __set_breakpoint(i, counter_arch_bp(bp));
541 }
542
543 /*
544 * If the process was being single-stepped by ptrace, let the
545 * other single-step actions occur (e.g. generate SIGTRAP).
546 */
547 if (!found || test_thread_flag(TIF_SINGLESTEP))
548 return NOTIFY_DONE;
549
550 return NOTIFY_STOP;
551}
552NOKPROBE_SYMBOL(single_step_dabr_instruction);
553
554/*
555 * Handle debug exception notifications.
556 *
557 * Called in atomic context.
558 */
559int hw_breakpoint_exceptions_notify(
560 struct notifier_block *unused, unsigned long val, void *data)
561{
562 int ret = NOTIFY_DONE;
563
564 switch (val) {
565 case DIE_DABR_MATCH:
566 ret = hw_breakpoint_handler(data);
567 break;
568 case DIE_SSTEP:
569 ret = single_step_dabr_instruction(data);
570 break;
571 }
572
573 return ret;
574}
575NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
576
577/*
578 * Release the user breakpoints used by ptrace
579 */
580void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
581{
582 int i;
583 struct thread_struct *t = &tsk->thread;
584
585 for (i = 0; i < nr_wp_slots(); i++) {
586 unregister_hw_breakpoint(t->ptrace_bps[i]);
587 t->ptrace_bps[i] = NULL;
588 }
589}
590
591void hw_breakpoint_pmu_read(struct perf_event *bp)
592{
593 /* TODO */
594}
595
596void ptrace_triggered(struct perf_event *bp,
597 struct perf_sample_data *data, struct pt_regs *regs)
598{
599 struct perf_event_attr attr;
600
601 /*
602 * Disable the breakpoint request here since ptrace has defined a
603 * one-shot behaviour for breakpoint exceptions in PPC64.
604 * The SIGTRAP signal is generated automatically for us in do_dabr().
605 * We don't have to do anything about that here
606 */
607 attr = bp->attr;
608 attr.disabled = true;
609 modify_user_hw_breakpoint(bp, &attr);
610}
1/*
2 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
3 * using the CPU's debug registers. Derived from
4 * "arch/x86/kernel/hw_breakpoint.c"
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright 2010 IBM Corporation
21 * Author: K.Prasad <prasad@linux.vnet.ibm.com>
22 *
23 */
24
25#include <linux/hw_breakpoint.h>
26#include <linux/notifier.h>
27#include <linux/kprobes.h>
28#include <linux/percpu.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/smp.h>
32
33#include <asm/hw_breakpoint.h>
34#include <asm/processor.h>
35#include <asm/sstep.h>
36#include <asm/uaccess.h>
37
38/*
39 * Stores the breakpoints currently in use on each breakpoint address
40 * register for every cpu
41 */
42static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
43
44/*
45 * Returns total number of data or instruction breakpoints available.
46 */
47int hw_breakpoint_slots(int type)
48{
49 if (type == TYPE_DATA)
50 return HBP_NUM;
51 return 0; /* no instruction breakpoints available */
52}
53
54/*
55 * Install a perf counter breakpoint.
56 *
57 * We seek a free debug address register and use it for this
58 * breakpoint.
59 *
60 * Atomic: we hold the counter->ctx->lock and we only handle variables
61 * and registers local to this cpu.
62 */
63int arch_install_hw_breakpoint(struct perf_event *bp)
64{
65 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
66 struct perf_event **slot = &__get_cpu_var(bp_per_reg);
67
68 *slot = bp;
69
70 /*
71 * Do not install DABR values if the instruction must be single-stepped.
72 * If so, DABR will be populated in single_step_dabr_instruction().
73 */
74 if (current->thread.last_hit_ubp != bp)
75 set_breakpoint(info);
76
77 return 0;
78}
79
80/*
81 * Uninstall the breakpoint contained in the given counter.
82 *
83 * First we search the debug address register it uses and then we disable
84 * it.
85 *
86 * Atomic: we hold the counter->ctx->lock and we only handle variables
87 * and registers local to this cpu.
88 */
89void arch_uninstall_hw_breakpoint(struct perf_event *bp)
90{
91 struct perf_event **slot = &__get_cpu_var(bp_per_reg);
92
93 if (*slot != bp) {
94 WARN_ONCE(1, "Can't find the breakpoint");
95 return;
96 }
97
98 *slot = NULL;
99 hw_breakpoint_disable();
100}
101
102/*
103 * Perform cleanup of arch-specific counters during unregistration
104 * of the perf-event
105 */
106void arch_unregister_hw_breakpoint(struct perf_event *bp)
107{
108 /*
109 * If the breakpoint is unregistered between a hw_breakpoint_handler()
110 * and the single_step_dabr_instruction(), then cleanup the breakpoint
111 * restoration variables to prevent dangling pointers.
112 */
113 if (bp->ctx && bp->ctx->task)
114 bp->ctx->task->thread.last_hit_ubp = NULL;
115}
116
117/*
118 * Check for virtual address in kernel space.
119 */
120int arch_check_bp_in_kernelspace(struct perf_event *bp)
121{
122 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
123
124 return is_kernel_addr(info->address);
125}
126
127int arch_bp_generic_fields(int type, int *gen_bp_type)
128{
129 *gen_bp_type = 0;
130 if (type & HW_BRK_TYPE_READ)
131 *gen_bp_type |= HW_BREAKPOINT_R;
132 if (type & HW_BRK_TYPE_WRITE)
133 *gen_bp_type |= HW_BREAKPOINT_W;
134 if (*gen_bp_type == 0)
135 return -EINVAL;
136 return 0;
137}
138
139/*
140 * Validate the arch-specific HW Breakpoint register settings
141 */
142int arch_validate_hwbkpt_settings(struct perf_event *bp)
143{
144 int ret = -EINVAL, length_max;
145 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
146
147 if (!bp)
148 return ret;
149
150 info->type = HW_BRK_TYPE_TRANSLATE;
151 if (bp->attr.bp_type & HW_BREAKPOINT_R)
152 info->type |= HW_BRK_TYPE_READ;
153 if (bp->attr.bp_type & HW_BREAKPOINT_W)
154 info->type |= HW_BRK_TYPE_WRITE;
155 if (info->type == HW_BRK_TYPE_TRANSLATE)
156 /* must set alteast read or write */
157 return ret;
158 if (!(bp->attr.exclude_user))
159 info->type |= HW_BRK_TYPE_USER;
160 if (!(bp->attr.exclude_kernel))
161 info->type |= HW_BRK_TYPE_KERNEL;
162 if (!(bp->attr.exclude_hv))
163 info->type |= HW_BRK_TYPE_HYP;
164 info->address = bp->attr.bp_addr;
165 info->len = bp->attr.bp_len;
166
167 /*
168 * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
169 * and breakpoint addresses are aligned to nearest double-word
170 * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
171 * 'symbolsize' should satisfy the check below.
172 */
173 length_max = 8; /* DABR */
174 if (cpu_has_feature(CPU_FTR_DAWR)) {
175 length_max = 512 ; /* 64 doublewords */
176 /* DAWR region can't cross 512 boundary */
177 if ((bp->attr.bp_addr >> 10) !=
178 ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 10))
179 return -EINVAL;
180 }
181 if (info->len >
182 (length_max - (info->address & HW_BREAKPOINT_ALIGN)))
183 return -EINVAL;
184 return 0;
185}
186
187/*
188 * Restores the breakpoint on the debug registers.
189 * Invoke this function if it is known that the execution context is
190 * about to change to cause loss of MSR_SE settings.
191 */
192void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
193{
194 struct arch_hw_breakpoint *info;
195
196 if (likely(!tsk->thread.last_hit_ubp))
197 return;
198
199 info = counter_arch_bp(tsk->thread.last_hit_ubp);
200 regs->msr &= ~MSR_SE;
201 set_breakpoint(info);
202 tsk->thread.last_hit_ubp = NULL;
203}
204
205/*
206 * Handle debug exception notifications.
207 */
208int __kprobes hw_breakpoint_handler(struct die_args *args)
209{
210 int rc = NOTIFY_STOP;
211 struct perf_event *bp;
212 struct pt_regs *regs = args->regs;
213 int stepped = 1;
214 struct arch_hw_breakpoint *info;
215 unsigned int instr;
216 unsigned long dar = regs->dar;
217
218 /* Disable breakpoints during exception handling */
219 hw_breakpoint_disable();
220
221 /*
222 * The counter may be concurrently released but that can only
223 * occur from a call_rcu() path. We can then safely fetch
224 * the breakpoint, use its callback, touch its counter
225 * while we are in an rcu_read_lock() path.
226 */
227 rcu_read_lock();
228
229 bp = __get_cpu_var(bp_per_reg);
230 if (!bp)
231 goto out;
232 info = counter_arch_bp(bp);
233
234 /*
235 * Return early after invoking user-callback function without restoring
236 * DABR if the breakpoint is from ptrace which always operates in
237 * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
238 * generated in do_dabr().
239 */
240 if (bp->overflow_handler == ptrace_triggered) {
241 perf_bp_event(bp, regs);
242 rc = NOTIFY_DONE;
243 goto out;
244 }
245
246 /*
247 * Verify if dar lies within the address range occupied by the symbol
248 * being watched to filter extraneous exceptions. If it doesn't,
249 * we still need to single-step the instruction, but we don't
250 * generate an event.
251 */
252 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
253 if (!((bp->attr.bp_addr <= dar) &&
254 (dar - bp->attr.bp_addr < bp->attr.bp_len)))
255 info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
256
257 /* Do not emulate user-space instructions, instead single-step them */
258 if (user_mode(regs)) {
259 current->thread.last_hit_ubp = bp;
260 regs->msr |= MSR_SE;
261 goto out;
262 }
263
264 stepped = 0;
265 instr = 0;
266 if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
267 stepped = emulate_step(regs, instr);
268
269 /*
270 * emulate_step() could not execute it. We've failed in reliably
271 * handling the hw-breakpoint. Unregister it and throw a warning
272 * message to let the user know about it.
273 */
274 if (!stepped) {
275 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
276 "0x%lx will be disabled.", info->address);
277 perf_event_disable(bp);
278 goto out;
279 }
280 /*
281 * As a policy, the callback is invoked in a 'trigger-after-execute'
282 * fashion
283 */
284 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
285 perf_bp_event(bp, regs);
286
287 set_breakpoint(info);
288out:
289 rcu_read_unlock();
290 return rc;
291}
292
293/*
294 * Handle single-step exceptions following a DABR hit.
295 */
296int __kprobes single_step_dabr_instruction(struct die_args *args)
297{
298 struct pt_regs *regs = args->regs;
299 struct perf_event *bp = NULL;
300 struct arch_hw_breakpoint *info;
301
302 bp = current->thread.last_hit_ubp;
303 /*
304 * Check if we are single-stepping as a result of a
305 * previous HW Breakpoint exception
306 */
307 if (!bp)
308 return NOTIFY_DONE;
309
310 info = counter_arch_bp(bp);
311
312 /*
313 * We shall invoke the user-defined callback function in the single
314 * stepping handler to confirm to 'trigger-after-execute' semantics
315 */
316 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
317 perf_bp_event(bp, regs);
318
319 set_breakpoint(info);
320 current->thread.last_hit_ubp = NULL;
321
322 /*
323 * If the process was being single-stepped by ptrace, let the
324 * other single-step actions occur (e.g. generate SIGTRAP).
325 */
326 if (test_thread_flag(TIF_SINGLESTEP))
327 return NOTIFY_DONE;
328
329 return NOTIFY_STOP;
330}
331
332/*
333 * Handle debug exception notifications.
334 */
335int __kprobes hw_breakpoint_exceptions_notify(
336 struct notifier_block *unused, unsigned long val, void *data)
337{
338 int ret = NOTIFY_DONE;
339
340 switch (val) {
341 case DIE_DABR_MATCH:
342 ret = hw_breakpoint_handler(data);
343 break;
344 case DIE_SSTEP:
345 ret = single_step_dabr_instruction(data);
346 break;
347 }
348
349 return ret;
350}
351
352/*
353 * Release the user breakpoints used by ptrace
354 */
355void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
356{
357 struct thread_struct *t = &tsk->thread;
358
359 unregister_hw_breakpoint(t->ptrace_bps[0]);
360 t->ptrace_bps[0] = NULL;
361}
362
363void hw_breakpoint_pmu_read(struct perf_event *bp)
364{
365 /* TODO */
366}