1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 <linux/uaccess.h>
37
38
39
40
41
42static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
43
44
45
46
47int hw_breakpoint_slots(int type)
48{
49 if (type == TYPE_DATA)
50 return HBP_NUM;
51 return 0;
52}
53
54
55
56
57
58
59
60
61
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 = this_cpu_ptr(&bp_per_reg);
67
68 *slot = bp;
69
70
71
72
73
74 if (current->thread.last_hit_ubp != bp)
75 __set_breakpoint(info);
76
77 return 0;
78}
79
80
81
82
83
84
85
86
87
88
89void arch_uninstall_hw_breakpoint(struct perf_event *bp)
90{
91 struct perf_event **slot = this_cpu_ptr(&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
104
105
106void arch_unregister_hw_breakpoint(struct perf_event *bp)
107{
108
109
110
111
112
113
114 if (bp->ctx && bp->ctx->task && bp->ctx->task != ((void *)-1L))
115 bp->ctx->task->thread.last_hit_ubp = NULL;
116}
117
118
119
120
121int arch_check_bp_in_kernelspace(struct perf_event *bp)
122{
123 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
124
125 return is_kernel_addr(info->address);
126}
127
128int arch_bp_generic_fields(int type, int *gen_bp_type)
129{
130 *gen_bp_type = 0;
131 if (type & HW_BRK_TYPE_READ)
132 *gen_bp_type |= HW_BREAKPOINT_R;
133 if (type & HW_BRK_TYPE_WRITE)
134 *gen_bp_type |= HW_BREAKPOINT_W;
135 if (*gen_bp_type == 0)
136 return -EINVAL;
137 return 0;
138}
139
140
141
142
143int arch_validate_hwbkpt_settings(struct perf_event *bp)
144{
145 int ret = -EINVAL, length_max;
146 struct arch_hw_breakpoint *info = counter_arch_bp(bp);
147
148 if (!bp)
149 return ret;
150
151 info->type = HW_BRK_TYPE_TRANSLATE;
152 if (bp->attr.bp_type & HW_BREAKPOINT_R)
153 info->type |= HW_BRK_TYPE_READ;
154 if (bp->attr.bp_type & HW_BREAKPOINT_W)
155 info->type |= HW_BRK_TYPE_WRITE;
156 if (info->type == HW_BRK_TYPE_TRANSLATE)
157
158 return ret;
159 if (!(bp->attr.exclude_user))
160 info->type |= HW_BRK_TYPE_USER;
161 if (!(bp->attr.exclude_kernel))
162 info->type |= HW_BRK_TYPE_KERNEL;
163 if (!(bp->attr.exclude_hv))
164 info->type |= HW_BRK_TYPE_HYP;
165 info->address = bp->attr.bp_addr;
166 info->len = bp->attr.bp_len;
167
168
169
170
171
172
173
174 length_max = 8;
175 if (cpu_has_feature(CPU_FTR_DAWR)) {
176 length_max = 512 ;
177
178 if ((bp->attr.bp_addr >> 10) !=
179 ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 10))
180 return -EINVAL;
181 }
182 if (info->len >
183 (length_max - (info->address & HW_BREAKPOINT_ALIGN)))
184 return -EINVAL;
185 return 0;
186}
187
188
189
190
191
192
193void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
194{
195 struct arch_hw_breakpoint *info;
196
197 if (likely(!tsk->thread.last_hit_ubp))
198 return;
199
200 info = counter_arch_bp(tsk->thread.last_hit_ubp);
201 regs->msr &= ~MSR_SE;
202 __set_breakpoint(info);
203 tsk->thread.last_hit_ubp = NULL;
204}
205
206
207
208
209int hw_breakpoint_handler(struct die_args *args)
210{
211 int rc = NOTIFY_STOP;
212 struct perf_event *bp;
213 struct pt_regs *regs = args->regs;
214#ifndef CONFIG_PPC_8xx
215 int stepped = 1;
216 unsigned int instr;
217#endif
218 struct arch_hw_breakpoint *info;
219 unsigned long dar = regs->dar;
220
221
222 hw_breakpoint_disable();
223
224
225
226
227
228
229
230 rcu_read_lock();
231
232 bp = __this_cpu_read(bp_per_reg);
233 if (!bp) {
234 rc = NOTIFY_DONE;
235 goto out;
236 }
237 info = counter_arch_bp(bp);
238
239
240
241
242
243
244
245 if (bp->overflow_handler == ptrace_triggered) {
246 perf_bp_event(bp, regs);
247 rc = NOTIFY_DONE;
248 goto out;
249 }
250
251
252
253
254
255
256
257 info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
258 if (!((bp->attr.bp_addr <= dar) &&
259 (dar - bp->attr.bp_addr < bp->attr.bp_len)))
260 info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
261
262#ifndef CONFIG_PPC_8xx
263
264 if (user_mode(regs)) {
265 current->thread.last_hit_ubp = bp;
266 regs->msr |= MSR_SE;
267 goto out;
268 }
269
270 stepped = 0;
271 instr = 0;
272 if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
273 stepped = emulate_step(regs, instr);
274
275
276
277
278
279
280 if (!stepped) {
281 WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
282 "0x%lx will be disabled.", info->address);
283 perf_event_disable_inatomic(bp);
284 goto out;
285 }
286#endif
287
288
289
290
291 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
292 perf_bp_event(bp, regs);
293
294 __set_breakpoint(info);
295out:
296 rcu_read_unlock();
297 return rc;
298}
299NOKPROBE_SYMBOL(hw_breakpoint_handler);
300
301
302
303
304static int single_step_dabr_instruction(struct die_args *args)
305{
306 struct pt_regs *regs = args->regs;
307 struct perf_event *bp = NULL;
308 struct arch_hw_breakpoint *info;
309
310 bp = current->thread.last_hit_ubp;
311
312
313
314
315 if (!bp)
316 return NOTIFY_DONE;
317
318 info = counter_arch_bp(bp);
319
320
321
322
323
324 if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
325 perf_bp_event(bp, regs);
326
327 __set_breakpoint(info);
328 current->thread.last_hit_ubp = NULL;
329
330
331
332
333
334 if (test_thread_flag(TIF_SINGLESTEP))
335 return NOTIFY_DONE;
336
337 return NOTIFY_STOP;
338}
339NOKPROBE_SYMBOL(single_step_dabr_instruction);
340
341
342
343
344int hw_breakpoint_exceptions_notify(
345 struct notifier_block *unused, unsigned long val, void *data)
346{
347 int ret = NOTIFY_DONE;
348
349 switch (val) {
350 case DIE_DABR_MATCH:
351 ret = hw_breakpoint_handler(data);
352 break;
353 case DIE_SSTEP:
354 ret = single_step_dabr_instruction(data);
355 break;
356 }
357
358 return ret;
359}
360NOKPROBE_SYMBOL(hw_breakpoint_exceptions_notify);
361
362
363
364
365void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
366{
367 struct thread_struct *t = &tsk->thread;
368
369 unregister_hw_breakpoint(t->ptrace_bps[0]);
370 t->ptrace_bps[0] = NULL;
371}
372
373void hw_breakpoint_pmu_read(struct perf_event *bp)
374{
375
376}
377