Merge tag 'for-linus' of git://github.com/openrisc/linux
[linux-2.6-block.git] / kernel / events / hw_breakpoint.c
CommitLineData
62a038d3
P
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2007 Alan Stern
17 * Copyright (C) IBM Corporation, 2009
24f1e32c 18 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
ba1c813a
FW
19 *
20 * Thanks to Ingo Molnar for his many suggestions.
ba6909b7
P
21 *
22 * Authors: Alan Stern <stern@rowland.harvard.edu>
23 * K.Prasad <prasad@linux.vnet.ibm.com>
24 * Frederic Weisbecker <fweisbec@gmail.com>
62a038d3
P
25 */
26
27/*
28 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
29 * using the CPU's debug registers.
30 * This file contains the arch-independent routines.
31 */
32
33#include <linux/irqflags.h>
34#include <linux/kallsyms.h>
35#include <linux/notifier.h>
36#include <linux/kprobes.h>
37#include <linux/kdebug.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/percpu.h>
41#include <linux/sched.h>
42#include <linux/init.h>
feef47d0 43#include <linux/slab.h>
45a73372 44#include <linux/list.h>
88f7a890 45#include <linux/cpu.h>
62a038d3 46#include <linux/smp.h>
ea6a9d53 47#include <linux/bug.h>
62a038d3 48
24f1e32c 49#include <linux/hw_breakpoint.h>
ba1c813a
FW
50/*
51 * Constraints data
52 */
bde96030
ON
53struct bp_cpuinfo {
54 /* Number of pinned cpu breakpoints in a cpu */
55 unsigned int cpu_pinned;
56 /* tsk_pinned[n] is the number of tasks having n+1 breakpoints */
57 unsigned int *tsk_pinned;
58 /* Number of non-pinned cpu/task breakpoints in a cpu */
59 unsigned int flexible; /* XXX: placeholder, see fetch_this_slot() */
60};
62a038d3 61
bde96030 62static DEFINE_PER_CPU(struct bp_cpuinfo, bp_cpuinfo[TYPE_MAX]);
feef47d0
FW
63static int nr_slots[TYPE_MAX];
64
bde96030
ON
65static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type)
66{
67 return per_cpu_ptr(bp_cpuinfo + type, cpu);
68}
69
45a73372
FW
70/* Keep track of the breakpoints attached to tasks */
71static LIST_HEAD(bp_task_head);
72
feef47d0
FW
73static int constraints_initialized;
74
ba1c813a
FW
75/* Gather the number of total pinned and un-pinned bp in a cpuset */
76struct bp_busy_slots {
77 unsigned int pinned;
78 unsigned int flexible;
79};
80
81/* Serialize accesses to the above constraints */
82static DEFINE_MUTEX(nr_bp_mutex);
83
f93a2054
FW
84__weak int hw_breakpoint_weight(struct perf_event *bp)
85{
86 return 1;
87}
88
cbd9d9f1 89static inline enum bp_type_idx find_slot_idx(u64 bp_type)
0102752e 90{
cbd9d9f1 91 if (bp_type & HW_BREAKPOINT_RW)
0102752e
FW
92 return TYPE_DATA;
93
94 return TYPE_INST;
95}
96
ba1c813a
FW
97/*
98 * Report the maximum number of pinned breakpoints a task
99 * have in this cpu
100 */
0102752e 101static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type)
62a038d3 102{
bde96030 103 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
ba1c813a 104 int i;
62a038d3 105
feef47d0 106 for (i = nr_slots[type] - 1; i >= 0; i--) {
ba1c813a
FW
107 if (tsk_pinned[i] > 0)
108 return i + 1;
62a038d3
P
109 }
110
24f1e32c 111 return 0;
62a038d3
P
112}
113
45a73372
FW
114/*
115 * Count the number of breakpoints of the same type and same task.
116 * The given event must be not on the list.
117 */
0d855354 118static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type)
56053170 119{
50f16a8b 120 struct task_struct *tsk = bp->hw.target;
45a73372 121 struct perf_event *iter;
56053170
FW
122 int count = 0;
123
45a73372 124 list_for_each_entry(iter, &bp_task_head, hw.bp_list) {
50f16a8b 125 if (iter->hw.target == tsk &&
cbd9d9f1 126 find_slot_idx(iter->attr.bp_type) == type &&
8b4d801b 127 (iter->cpu < 0 || cpu == iter->cpu))
45a73372 128 count += hw_breakpoint_weight(iter);
56053170
FW
129 }
130
56053170
FW
131 return count;
132}
133
1c10adbb
ON
134static const struct cpumask *cpumask_of_bp(struct perf_event *bp)
135{
136 if (bp->cpu >= 0)
137 return cpumask_of(bp->cpu);
138 return cpu_possible_mask;
139}
140
ba1c813a
FW
141/*
142 * Report the number of pinned/un-pinned breakpoints we have in
143 * a given cpu (cpu > -1) or in all of them (cpu = -1).
144 */
56053170 145static void
0102752e
FW
146fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp,
147 enum bp_type_idx type)
ba1c813a 148{
1c10adbb
ON
149 const struct cpumask *cpumask = cpumask_of_bp(bp);
150 int cpu;
56053170 151
1c10adbb 152 for_each_cpu(cpu, cpumask) {
bde96030
ON
153 struct bp_cpuinfo *info = get_bp_info(cpu, type);
154 int nr;
ba1c813a 155
bde96030 156 nr = info->cpu_pinned;
50f16a8b 157 if (!bp->hw.target)
0102752e 158 nr += max_task_bp_pinned(cpu, type);
56053170 159 else
0d855354 160 nr += task_bp_pinned(cpu, bp, type);
ba1c813a
FW
161
162 if (nr > slots->pinned)
163 slots->pinned = nr;
164
bde96030 165 nr = info->flexible;
ba1c813a
FW
166 if (nr > slots->flexible)
167 slots->flexible = nr;
168 }
169}
170
f93a2054
FW
171/*
172 * For now, continue to consider flexible as pinned, until we can
173 * ensure no flexible event can ever be scheduled before a pinned event
174 * in a same cpu.
175 */
176static void
177fetch_this_slot(struct bp_busy_slots *slots, int weight)
178{
179 slots->pinned += weight;
180}
181
ba1c813a
FW
182/*
183 * Add a pinned breakpoint for the given task in our constraint table
184 */
7ab71f32 185static void toggle_bp_task_slot(struct perf_event *bp, int cpu,
f93a2054 186 enum bp_type_idx type, int weight)
ba1c813a 187{
bde96030 188 unsigned int *tsk_pinned = get_bp_info(cpu, type)->tsk_pinned;
e1ebe862
ON
189 int old_idx, new_idx;
190
191 old_idx = task_bp_pinned(cpu, bp, type) - 1;
7ab71f32 192 new_idx = old_idx + weight;
e1ebe862
ON
193
194 if (old_idx >= 0)
195 tsk_pinned[old_idx]--;
196 if (new_idx >= 0)
197 tsk_pinned[new_idx]++;
ba1c813a
FW
198}
199
200/*
201 * Add/remove the given breakpoint in our constraint table
202 */
0102752e 203static void
f93a2054
FW
204toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type,
205 int weight)
ba1c813a 206{
1c10adbb
ON
207 const struct cpumask *cpumask = cpumask_of_bp(bp);
208 int cpu;
ba1c813a 209
7ab71f32
ON
210 if (!enable)
211 weight = -weight;
212
45a73372 213 /* Pinned counter cpu profiling */
50f16a8b 214 if (!bp->hw.target) {
bde96030 215 get_bp_info(bp->cpu, type)->cpu_pinned += weight;
45a73372
FW
216 return;
217 }
218
ba1c813a 219 /* Pinned counter task profiling */
1c10adbb 220 for_each_cpu(cpu, cpumask)
7ab71f32 221 toggle_bp_task_slot(bp, cpu, type, weight);
ba1c813a 222
ba1c813a 223 if (enable)
45a73372 224 list_add_tail(&bp->hw.bp_list, &bp_task_head);
e1ebe862
ON
225 else
226 list_del(&bp->hw.bp_list);
ba1c813a
FW
227}
228
f7136c51
P
229/*
230 * Function to perform processor-specific cleanup during unregistration
231 */
232__weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
233{
234 /*
235 * A weak stub function here for those archs that don't define
236 * it inside arch/.../kernel/hw_breakpoint.c
237 */
238}
239
ba1c813a
FW
240/*
241 * Contraints to check before allowing this new breakpoint counter:
242 *
243 * == Non-pinned counter == (Considered as pinned for now)
244 *
245 * - If attached to a single cpu, check:
246 *
bde96030
ON
247 * (per_cpu(info->flexible, cpu) || (per_cpu(info->cpu_pinned, cpu)
248 * + max(per_cpu(info->tsk_pinned, cpu)))) < HBP_NUM
ba1c813a
FW
249 *
250 * -> If there are already non-pinned counters in this cpu, it means
251 * there is already a free slot for them.
252 * Otherwise, we check that the maximum number of per task
253 * breakpoints (for this cpu) plus the number of per cpu breakpoint
254 * (for this cpu) doesn't cover every registers.
255 *
256 * - If attached to every cpus, check:
257 *
bde96030
ON
258 * (per_cpu(info->flexible, *) || (max(per_cpu(info->cpu_pinned, *))
259 * + max(per_cpu(info->tsk_pinned, *)))) < HBP_NUM
ba1c813a
FW
260 *
261 * -> This is roughly the same, except we check the number of per cpu
262 * bp for every cpu and we keep the max one. Same for the per tasks
263 * breakpoints.
264 *
265 *
266 * == Pinned counter ==
267 *
268 * - If attached to a single cpu, check:
269 *
bde96030
ON
270 * ((per_cpu(info->flexible, cpu) > 1) + per_cpu(info->cpu_pinned, cpu)
271 * + max(per_cpu(info->tsk_pinned, cpu))) < HBP_NUM
ba1c813a 272 *
bde96030 273 * -> Same checks as before. But now the info->flexible, if any, must keep
ba1c813a
FW
274 * one register at least (or they will never be fed).
275 *
276 * - If attached to every cpus, check:
277 *
bde96030
ON
278 * ((per_cpu(info->flexible, *) > 1) + max(per_cpu(info->cpu_pinned, *))
279 * + max(per_cpu(info->tsk_pinned, *))) < HBP_NUM
ba1c813a 280 */
1ad9ff7d 281static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type)
ba1c813a
FW
282{
283 struct bp_busy_slots slots = {0};
0102752e 284 enum bp_type_idx type;
f93a2054 285 int weight;
ba1c813a 286
feef47d0
FW
287 /* We couldn't initialize breakpoint constraints on boot */
288 if (!constraints_initialized)
289 return -ENOMEM;
290
0102752e 291 /* Basic checks */
1ad9ff7d
JO
292 if (bp_type == HW_BREAKPOINT_EMPTY ||
293 bp_type == HW_BREAKPOINT_INVALID)
0102752e
FW
294 return -EINVAL;
295
1ad9ff7d 296 type = find_slot_idx(bp_type);
f93a2054
FW
297 weight = hw_breakpoint_weight(bp);
298
0102752e 299 fetch_bp_busy_slots(&slots, bp, type);
45a73372
FW
300 /*
301 * Simulate the addition of this breakpoint to the constraints
302 * and see the result.
303 */
f93a2054 304 fetch_this_slot(&slots, weight);
ba1c813a
FW
305
306 /* Flexible counters need to keep at least one slot */
feef47d0 307 if (slots.pinned + (!!slots.flexible) > nr_slots[type])
5352ae63 308 return -ENOSPC;
ba1c813a 309
f93a2054 310 toggle_bp_slot(bp, true, type, weight);
ba1c813a 311
5352ae63
JW
312 return 0;
313}
314
315int reserve_bp_slot(struct perf_event *bp)
316{
317 int ret;
318
319 mutex_lock(&nr_bp_mutex);
320
1ad9ff7d 321 ret = __reserve_bp_slot(bp, bp->attr.bp_type);
5352ae63 322
ba1c813a
FW
323 mutex_unlock(&nr_bp_mutex);
324
325 return ret;
326}
327
1ad9ff7d 328static void __release_bp_slot(struct perf_event *bp, u64 bp_type)
5352ae63 329{
0102752e 330 enum bp_type_idx type;
f93a2054 331 int weight;
0102752e 332
1ad9ff7d 333 type = find_slot_idx(bp_type);
f93a2054
FW
334 weight = hw_breakpoint_weight(bp);
335 toggle_bp_slot(bp, false, type, weight);
5352ae63
JW
336}
337
24f1e32c 338void release_bp_slot(struct perf_event *bp)
62a038d3 339{
ba1c813a
FW
340 mutex_lock(&nr_bp_mutex);
341
f7136c51 342 arch_unregister_hw_breakpoint(bp);
1ad9ff7d 343 __release_bp_slot(bp, bp->attr.bp_type);
ba1c813a
FW
344
345 mutex_unlock(&nr_bp_mutex);
62a038d3
P
346}
347
cb8b7881 348static int __modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
ea6a9d53
JO
349{
350 int err;
351
352 __release_bp_slot(bp, old_type);
353
cb8b7881 354 err = __reserve_bp_slot(bp, new_type);
ea6a9d53
JO
355 if (err) {
356 /*
357 * Reserve the old_type slot back in case
358 * there's no space for the new type.
359 *
360 * This must succeed, because we just released
361 * the old_type slot in the __release_bp_slot
362 * call above. If not, something is broken.
363 */
364 WARN_ON(__reserve_bp_slot(bp, old_type));
365 }
366
367 return err;
368}
369
cb8b7881 370static int modify_bp_slot(struct perf_event *bp, u64 old_type, u64 new_type)
ea6a9d53
JO
371{
372 int ret;
373
374 mutex_lock(&nr_bp_mutex);
cb8b7881 375 ret = __modify_bp_slot(bp, old_type, new_type);
ea6a9d53
JO
376 mutex_unlock(&nr_bp_mutex);
377 return ret;
378}
379
5352ae63
JW
380/*
381 * Allow the kernel debugger to reserve breakpoint slots without
382 * taking a lock using the dbg_* variant of for the reserve and
383 * release breakpoint slots.
384 */
385int dbg_reserve_bp_slot(struct perf_event *bp)
386{
387 if (mutex_is_locked(&nr_bp_mutex))
388 return -1;
389
1ad9ff7d 390 return __reserve_bp_slot(bp, bp->attr.bp_type);
5352ae63
JW
391}
392
393int dbg_release_bp_slot(struct perf_event *bp)
394{
395 if (mutex_is_locked(&nr_bp_mutex))
396 return -1;
397
1ad9ff7d 398 __release_bp_slot(bp, bp->attr.bp_type);
5352ae63
JW
399
400 return 0;
401}
ba1c813a 402
9a4903dd
FW
403static int hw_breakpoint_parse(struct perf_event *bp,
404 const struct perf_event_attr *attr,
405 struct arch_hw_breakpoint *hw)
406{
407 int err;
b2812d03 408
9a4903dd
FW
409 err = hw_breakpoint_arch_parse(bp, attr, hw);
410 if (err)
411 return err;
b2812d03 412
8e983ff9 413 if (arch_check_bp_in_kernelspace(hw)) {
9a4903dd 414 if (attr->exclude_kernel)
b2812d03
FW
415 return -EINVAL;
416 /*
417 * Don't let unprivileged users set a breakpoint in the trap
418 * path to avoid trap recursion attacks.
419 */
420 if (!capable(CAP_SYS_ADMIN))
421 return -EPERM;
422 }
423
424 return 0;
425}
426
b326e956 427int register_perf_hw_breakpoint(struct perf_event *bp)
62a038d3 428{
9a4903dd
FW
429 struct arch_hw_breakpoint hw;
430 int err;
62a038d3 431
9a4903dd
FW
432 err = reserve_bp_slot(bp);
433 if (err)
434 return err;
62a038d3 435
9a4903dd
FW
436 err = hw_breakpoint_parse(bp, &bp->attr, &hw);
437 if (err) {
b23ff0e9 438 release_bp_slot(bp);
9a4903dd
FW
439 return err;
440 }
b23ff0e9 441
9a4903dd
FW
442 bp->hw.info = hw;
443
444 return 0;
24f1e32c 445}
62a038d3 446
62a038d3
P
447/**
448 * register_user_hw_breakpoint - register a hardware breakpoint for user space
5fa10b28 449 * @attr: breakpoint attributes
24f1e32c 450 * @triggered: callback to trigger when we hit the breakpoint
62a038d3 451 * @tsk: pointer to 'task_struct' of the process to which the address belongs
62a038d3 452 */
24f1e32c 453struct perf_event *
5fa10b28 454register_user_hw_breakpoint(struct perf_event_attr *attr,
b326e956 455 perf_overflow_handler_t triggered,
4dc0da86 456 void *context,
5fa10b28 457 struct task_struct *tsk)
62a038d3 458{
4dc0da86
AK
459 return perf_event_create_kernel_counter(attr, -1, tsk, triggered,
460 context);
62a038d3
P
461}
462EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
463
26c6ccdf
FW
464static void hw_breakpoint_copy_attr(struct perf_event_attr *to,
465 struct perf_event_attr *from)
466{
467 to->bp_addr = from->bp_addr;
468 to->bp_type = from->bp_type;
469 to->bp_len = from->bp_len;
470 to->disabled = from->disabled;
471}
472
32ff77e8 473int
705feaf3
JO
474modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_attr *attr,
475 bool check)
18ff57b2 476{
9a4903dd 477 struct arch_hw_breakpoint hw;
26c6ccdf 478 int err;
18ff57b2 479
26c6ccdf
FW
480 err = hw_breakpoint_parse(bp, attr, &hw);
481 if (err)
482 return err;
18ff57b2 483
26c6ccdf
FW
484 if (check) {
485 struct perf_event_attr old_attr;
705feaf3 486
26c6ccdf
FW
487 old_attr = bp->attr;
488 hw_breakpoint_copy_attr(&old_attr, attr);
489 if (memcmp(&old_attr, attr, sizeof(*attr)))
490 return -EINVAL;
491 }
18ff57b2 492
26c6ccdf
FW
493 if (bp->attr.bp_type != attr->bp_type) {
494 err = modify_bp_slot(bp, bp->attr.bp_type, attr->bp_type);
495 if (err)
496 return err;
18ff57b2
JO
497 }
498
26c6ccdf 499 hw_breakpoint_copy_attr(&bp->attr, attr);
9a4903dd 500 bp->hw.info = hw;
9a4903dd 501
18ff57b2
JO
502 return 0;
503}
504
62a038d3
P
505/**
506 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
24f1e32c 507 * @bp: the breakpoint structure to modify
5fa10b28 508 * @attr: new breakpoint attributes
62a038d3 509 */
44234adc 510int modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
62a038d3 511{
500ad2d8
P
512 /*
513 * modify_user_hw_breakpoint can be invoked with IRQs disabled and hence it
514 * will not be possible to raise IPIs that invoke __perf_event_disable.
515 * So call the function directly after making sure we are targeting the
516 * current task.
517 */
518 if (irqs_disabled() && bp->ctx && bp->ctx->task == current)
fae3fde6 519 perf_event_disable_local(bp);
500ad2d8
P
520 else
521 perf_event_disable(bp);
44234adc 522
f67b1503 523 if (!attr->disabled) {
2d074918 524 int err = modify_user_hw_breakpoint_check(bp, attr, false);
44234adc 525
f67b1503
LT
526 if (err)
527 return err;
ea6a9d53 528 perf_event_enable(bp);
f67b1503 529 bp->attr.disabled = 0;
44234adc 530 }
44234adc 531 return 0;
62a038d3
P
532}
533EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
534
535/**
24f1e32c 536 * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
62a038d3 537 * @bp: the breakpoint structure to unregister
62a038d3 538 */
24f1e32c 539void unregister_hw_breakpoint(struct perf_event *bp)
62a038d3 540{
24f1e32c
FW
541 if (!bp)
542 return;
543 perf_event_release_kernel(bp);
544}
545EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
546
62a038d3 547/**
24f1e32c 548 * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
dd1853c3 549 * @attr: breakpoint attributes
24f1e32c 550 * @triggered: callback to trigger when we hit the breakpoint
62a038d3 551 *
24f1e32c 552 * @return a set of per_cpu pointers to perf events
62a038d3 553 */
44ee6358 554struct perf_event * __percpu *
dd1853c3 555register_wide_hw_breakpoint(struct perf_event_attr *attr,
4dc0da86
AK
556 perf_overflow_handler_t triggered,
557 void *context)
62a038d3 558{
e12cbc10
ON
559 struct perf_event * __percpu *cpu_events, *bp;
560 long err = 0;
24f1e32c
FW
561 int cpu;
562
563 cpu_events = alloc_percpu(typeof(*cpu_events));
564 if (!cpu_events)
44ee6358 565 return (void __percpu __force *)ERR_PTR(-ENOMEM);
62a038d3 566
88f7a890
LZ
567 get_online_cpus();
568 for_each_online_cpu(cpu) {
4dc0da86
AK
569 bp = perf_event_create_kernel_counter(attr, cpu, NULL,
570 triggered, context);
605bfaee 571 if (IS_ERR(bp)) {
24f1e32c 572 err = PTR_ERR(bp);
e12cbc10 573 break;
24f1e32c 574 }
24f1e32c 575
e12cbc10 576 per_cpu(*cpu_events, cpu) = bp;
24f1e32c 577 }
88f7a890
LZ
578 put_online_cpus();
579
e12cbc10
ON
580 if (likely(!err))
581 return cpu_events;
582
583 unregister_wide_hw_breakpoint(cpu_events);
44ee6358 584 return (void __percpu __force *)ERR_PTR(err);
62a038d3 585}
f60d24d2 586EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
62a038d3
P
587
588/**
24f1e32c
FW
589 * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
590 * @cpu_events: the per cpu set of events to unregister
62a038d3 591 */
44ee6358 592void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events)
62a038d3 593{
24f1e32c 594 int cpu;
62a038d3 595
e12cbc10
ON
596 for_each_possible_cpu(cpu)
597 unregister_hw_breakpoint(per_cpu(*cpu_events, cpu));
598
24f1e32c 599 free_percpu(cpu_events);
62a038d3 600}
f60d24d2 601EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
62a038d3
P
602
603static struct notifier_block hw_breakpoint_exceptions_nb = {
604 .notifier_call = hw_breakpoint_exceptions_notify,
605 /* we need to be notified first */
606 .priority = 0x7fffffff
607};
608
b0a873eb
PZ
609static void bp_perf_event_destroy(struct perf_event *event)
610{
611 release_bp_slot(event);
612}
613
614static int hw_breakpoint_event_init(struct perf_event *bp)
615{
616 int err;
617
618 if (bp->attr.type != PERF_TYPE_BREAKPOINT)
619 return -ENOENT;
620
2481c5fa
SE
621 /*
622 * no branch sampling for breakpoint events
623 */
624 if (has_branch_stack(bp))
625 return -EOPNOTSUPP;
626
b0a873eb
PZ
627 err = register_perf_hw_breakpoint(bp);
628 if (err)
629 return err;
630
631 bp->destroy = bp_perf_event_destroy;
632
633 return 0;
634}
635
a4eaf7f1
PZ
636static int hw_breakpoint_add(struct perf_event *bp, int flags)
637{
638 if (!(flags & PERF_EF_START))
639 bp->hw.state = PERF_HES_STOPPED;
640
ab573844
JO
641 if (is_sampling_event(bp)) {
642 bp->hw.last_period = bp->hw.sample_period;
643 perf_swevent_set_period(bp);
644 }
645
a4eaf7f1
PZ
646 return arch_install_hw_breakpoint(bp);
647}
648
649static void hw_breakpoint_del(struct perf_event *bp, int flags)
650{
651 arch_uninstall_hw_breakpoint(bp);
652}
653
654static void hw_breakpoint_start(struct perf_event *bp, int flags)
655{
656 bp->hw.state = 0;
657}
658
659static void hw_breakpoint_stop(struct perf_event *bp, int flags)
660{
661 bp->hw.state = PERF_HES_STOPPED;
662}
663
b0a873eb 664static struct pmu perf_breakpoint = {
89a1e187
PZ
665 .task_ctx_nr = perf_sw_context, /* could eventually get its own */
666
b0a873eb 667 .event_init = hw_breakpoint_event_init,
a4eaf7f1
PZ
668 .add = hw_breakpoint_add,
669 .del = hw_breakpoint_del,
670 .start = hw_breakpoint_start,
671 .stop = hw_breakpoint_stop,
b0a873eb
PZ
672 .read = hw_breakpoint_pmu_read,
673};
674
3c502e7a 675int __init init_hw_breakpoint(void)
62a038d3 676{
feef47d0
FW
677 int cpu, err_cpu;
678 int i;
679
680 for (i = 0; i < TYPE_MAX; i++)
681 nr_slots[i] = hw_breakpoint_slots(i);
682
683 for_each_possible_cpu(cpu) {
684 for (i = 0; i < TYPE_MAX; i++) {
bde96030
ON
685 struct bp_cpuinfo *info = get_bp_info(cpu, i);
686
687 info->tsk_pinned = kcalloc(nr_slots[i], sizeof(int),
688 GFP_KERNEL);
689 if (!info->tsk_pinned)
feef47d0
FW
690 goto err_alloc;
691 }
692 }
693
694 constraints_initialized = 1;
695
2e80a82a 696 perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT);
b0a873eb 697
62a038d3 698 return register_die_notifier(&hw_breakpoint_exceptions_nb);
feef47d0
FW
699
700 err_alloc:
701 for_each_possible_cpu(err_cpu) {
feef47d0 702 for (i = 0; i < TYPE_MAX; i++)
bde96030 703 kfree(get_bp_info(err_cpu, i)->tsk_pinned);
30ce2f7e
NK
704 if (err_cpu == cpu)
705 break;
feef47d0
FW
706 }
707
708 return -ENOMEM;
62a038d3 709}
24f1e32c
FW
710
711