blob: b600fc27f1613e386ee4397a84e65cb623ea6b26 [file] [log] [blame]
K.Prasad62a038d2009-06-01 23:43:33 +05301/*
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
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020018 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020019 *
20 * Thanks to Ingo Molnar for his many suggestions.
K.Prasadba6909b2009-11-23 21:17:13 +053021 *
22 * Authors: Alan Stern <stern@rowland.harvard.edu>
23 * K.Prasad <prasad@linux.vnet.ibm.com>
24 * Frederic Weisbecker <fweisbec@gmail.com>
K.Prasad62a038d2009-06-01 23:43:33 +053025 */
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>
43#include <linux/smp.h>
44
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020045#include <linux/hw_breakpoint.h>
46
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020047/*
48 * Constraints data
49 */
K.Prasad62a038d2009-06-01 23:43:33 +053050
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020051/* Number of pinned cpu breakpoints in a cpu */
52static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned);
53
54/* Number of pinned task breakpoints in a cpu */
55static DEFINE_PER_CPU(unsigned int, task_bp_pinned[HBP_NUM]);
56
57/* Number of non-pinned cpu/task breakpoints in a cpu */
58static DEFINE_PER_CPU(unsigned int, nr_bp_flexible);
59
60/* Gather the number of total pinned and un-pinned bp in a cpuset */
61struct bp_busy_slots {
62 unsigned int pinned;
63 unsigned int flexible;
64};
65
66/* Serialize accesses to the above constraints */
67static DEFINE_MUTEX(nr_bp_mutex);
68
69/*
70 * Report the maximum number of pinned breakpoints a task
71 * have in this cpu
72 */
73static unsigned int max_task_bp_pinned(int cpu)
K.Prasad62a038d2009-06-01 23:43:33 +053074{
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020075 int i;
76 unsigned int *tsk_pinned = per_cpu(task_bp_pinned, cpu);
K.Prasad62a038d2009-06-01 23:43:33 +053077
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020078 for (i = HBP_NUM -1; i >= 0; i--) {
79 if (tsk_pinned[i] > 0)
80 return i + 1;
K.Prasad62a038d2009-06-01 23:43:33 +053081 }
82
K.Prasad62a038d2009-06-01 23:43:33 +053083 return 0;
84}
85
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020086/*
87 * Report the number of pinned/un-pinned breakpoints we have in
88 * a given cpu (cpu > -1) or in all of them (cpu = -1).
89 */
90static void fetch_bp_busy_slots(struct bp_busy_slots *slots, int cpu)
91{
92 if (cpu >= 0) {
93 slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
94 slots->pinned += max_task_bp_pinned(cpu);
95 slots->flexible = per_cpu(nr_bp_flexible, cpu);
96
97 return;
98 }
99
100 for_each_online_cpu(cpu) {
101 unsigned int nr;
102
103 nr = per_cpu(nr_cpu_bp_pinned, cpu);
104 nr += max_task_bp_pinned(cpu);
105
106 if (nr > slots->pinned)
107 slots->pinned = nr;
108
109 nr = per_cpu(nr_bp_flexible, cpu);
110
111 if (nr > slots->flexible)
112 slots->flexible = nr;
113 }
114}
115
116/*
117 * Add a pinned breakpoint for the given task in our constraint table
118 */
119static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
120{
121 int count = 0;
122 struct perf_event *bp;
123 struct perf_event_context *ctx = tsk->perf_event_ctxp;
Andrew Morton11e66352009-11-25 23:01:50 -0800124 unsigned int *tsk_pinned;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200125 struct list_head *list;
126 unsigned long flags;
127
128 if (WARN_ONCE(!ctx, "No perf context for this task"))
129 return;
130
131 list = &ctx->event_list;
132
133 spin_lock_irqsave(&ctx->lock, flags);
134
135 /*
136 * The current breakpoint counter is not included in the list
137 * at the open() callback time
138 */
139 list_for_each_entry(bp, list, event_entry) {
140 if (bp->attr.type == PERF_TYPE_BREAKPOINT)
141 count++;
142 }
143
144 spin_unlock_irqrestore(&ctx->lock, flags);
145
146 if (WARN_ONCE(count < 0, "No breakpoint counter found in the counter list"))
147 return;
148
Andrew Morton11e66352009-11-25 23:01:50 -0800149 tsk_pinned = per_cpu(task_bp_pinned, cpu);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200150 if (enable) {
Andrew Morton11e66352009-11-25 23:01:50 -0800151 tsk_pinned[count]++;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200152 if (count > 0)
Andrew Morton11e66352009-11-25 23:01:50 -0800153 tsk_pinned[count-1]--;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200154 } else {
Andrew Morton11e66352009-11-25 23:01:50 -0800155 tsk_pinned[count]--;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200156 if (count > 0)
Andrew Morton11e66352009-11-25 23:01:50 -0800157 tsk_pinned[count-1]++;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200158 }
159}
160
161/*
162 * Add/remove the given breakpoint in our constraint table
163 */
164static void toggle_bp_slot(struct perf_event *bp, bool enable)
165{
166 int cpu = bp->cpu;
167 struct task_struct *tsk = bp->ctx->task;
168
169 /* Pinned counter task profiling */
170 if (tsk) {
171 if (cpu >= 0) {
172 toggle_bp_task_slot(tsk, cpu, enable);
173 return;
174 }
175
176 for_each_online_cpu(cpu)
177 toggle_bp_task_slot(tsk, cpu, enable);
178 return;
179 }
180
181 /* Pinned counter cpu profiling */
182 if (enable)
183 per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
184 else
185 per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
186}
187
188/*
189 * Contraints to check before allowing this new breakpoint counter:
190 *
191 * == Non-pinned counter == (Considered as pinned for now)
192 *
193 * - If attached to a single cpu, check:
194 *
195 * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
196 * + max(per_cpu(task_bp_pinned, cpu)))) < HBP_NUM
197 *
198 * -> If there are already non-pinned counters in this cpu, it means
199 * there is already a free slot for them.
200 * Otherwise, we check that the maximum number of per task
201 * breakpoints (for this cpu) plus the number of per cpu breakpoint
202 * (for this cpu) doesn't cover every registers.
203 *
204 * - If attached to every cpus, check:
205 *
206 * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
207 * + max(per_cpu(task_bp_pinned, *)))) < HBP_NUM
208 *
209 * -> This is roughly the same, except we check the number of per cpu
210 * bp for every cpu and we keep the max one. Same for the per tasks
211 * breakpoints.
212 *
213 *
214 * == Pinned counter ==
215 *
216 * - If attached to a single cpu, check:
217 *
218 * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
219 * + max(per_cpu(task_bp_pinned, cpu))) < HBP_NUM
220 *
221 * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
222 * one register at least (or they will never be fed).
223 *
224 * - If attached to every cpus, check:
225 *
226 * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
227 * + max(per_cpu(task_bp_pinned, *))) < HBP_NUM
228 */
229int reserve_bp_slot(struct perf_event *bp)
230{
231 struct bp_busy_slots slots = {0};
232 int ret = 0;
233
234 mutex_lock(&nr_bp_mutex);
235
236 fetch_bp_busy_slots(&slots, bp->cpu);
237
238 /* Flexible counters need to keep at least one slot */
239 if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
240 ret = -ENOSPC;
241 goto end;
242 }
243
244 toggle_bp_slot(bp, true);
245
246end:
247 mutex_unlock(&nr_bp_mutex);
248
249 return ret;
250}
251
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200252void release_bp_slot(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530253{
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200254 mutex_lock(&nr_bp_mutex);
255
256 toggle_bp_slot(bp, false);
257
258 mutex_unlock(&nr_bp_mutex);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200259}
K.Prasad62a038d2009-06-01 23:43:33 +0530260
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200261
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100262int register_perf_hw_breakpoint(struct perf_event *bp)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200263{
264 int ret;
K.Prasad62a038d2009-06-01 23:43:33 +0530265
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200266 ret = reserve_bp_slot(bp);
267 if (ret)
268 return ret;
K.Prasad62a038d2009-06-01 23:43:33 +0530269
Frederic Weisbeckerfdf6bc92009-11-23 15:42:33 +0100270 /*
271 * Ptrace breakpoints can be temporary perf events only
272 * meant to reserve a slot. In this case, it is created disabled and
273 * we don't want to check the params right now (as we put a null addr)
274 * But perf tools create events as disabled and we want to check
275 * the params for them.
276 * This is a quick hack that will be removed soon, once we remove
277 * the tmp breakpoints from ptrace
278 */
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100279 if (!bp->attr.disabled || !bp->overflow_handler)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200280 ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
K.Prasad62a038d2009-06-01 23:43:33 +0530281
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200282 return ret;
283}
K.Prasad62a038d2009-06-01 23:43:33 +0530284
K.Prasad62a038d2009-06-01 23:43:33 +0530285/**
286 * register_user_hw_breakpoint - register a hardware breakpoint for user space
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100287 * @attr: breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200288 * @triggered: callback to trigger when we hit the breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530289 * @tsk: pointer to 'task_struct' of the process to which the address belongs
K.Prasad62a038d2009-06-01 23:43:33 +0530290 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200291struct perf_event *
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100292register_user_hw_breakpoint(struct perf_event_attr *attr,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100293 perf_overflow_handler_t triggered,
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100294 struct task_struct *tsk)
K.Prasad62a038d2009-06-01 23:43:33 +0530295{
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100296 return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
K.Prasad62a038d2009-06-01 23:43:33 +0530297}
298EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
299
300/**
301 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200302 * @bp: the breakpoint structure to modify
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100303 * @attr: new breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200304 * @triggered: callback to trigger when we hit the breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530305 * @tsk: pointer to 'task_struct' of the process to which the address belongs
K.Prasad62a038d2009-06-01 23:43:33 +0530306 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200307struct perf_event *
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100308modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
K.Prasad62a038d2009-06-01 23:43:33 +0530309{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200310 /*
311 * FIXME: do it without unregistering
312 * - We don't want to lose our slot
313 * - If the new bp is incorrect, don't lose the older one
314 */
315 unregister_hw_breakpoint(bp);
K.Prasad62a038d2009-06-01 23:43:33 +0530316
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100317 return perf_event_create_kernel_counter(attr, -1, bp->ctx->task->pid,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100318 bp->overflow_handler);
K.Prasad62a038d2009-06-01 23:43:33 +0530319}
320EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
321
322/**
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200323 * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530324 * @bp: the breakpoint structure to unregister
K.Prasad62a038d2009-06-01 23:43:33 +0530325 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200326void unregister_hw_breakpoint(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530327{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200328 if (!bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530329 return;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200330 perf_event_release_kernel(bp);
331}
332EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
333
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200334/**
335 * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100336 * @attr: breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200337 * @triggered: callback to trigger when we hit the breakpoint
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200338 *
339 * @return a set of per_cpu pointers to perf events
340 */
341struct perf_event **
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100342register_wide_hw_breakpoint(struct perf_event_attr *attr,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100343 perf_overflow_handler_t triggered)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200344{
345 struct perf_event **cpu_events, **pevent, *bp;
346 long err;
347 int cpu;
348
349 cpu_events = alloc_percpu(typeof(*cpu_events));
350 if (!cpu_events)
351 return ERR_PTR(-ENOMEM);
352
353 for_each_possible_cpu(cpu) {
354 pevent = per_cpu_ptr(cpu_events, cpu);
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100355 bp = perf_event_create_kernel_counter(attr, cpu, -1, triggered);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200356
357 *pevent = bp;
358
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100359 if (IS_ERR(bp)) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200360 err = PTR_ERR(bp);
361 goto fail;
362 }
K.Prasad62a038d2009-06-01 23:43:33 +0530363 }
364
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200365 return cpu_events;
K.Prasad62a038d2009-06-01 23:43:33 +0530366
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200367fail:
368 for_each_possible_cpu(cpu) {
369 pevent = per_cpu_ptr(cpu_events, cpu);
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100370 if (IS_ERR(*pevent))
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200371 break;
372 unregister_hw_breakpoint(*pevent);
373 }
374 free_percpu(cpu_events);
375 /* return the error if any */
376 return ERR_PTR(err);
K.Prasad62a038d2009-06-01 23:43:33 +0530377}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100378EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200379
380/**
381 * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
382 * @cpu_events: the per cpu set of events to unregister
383 */
384void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
385{
386 int cpu;
387 struct perf_event **pevent;
388
389 for_each_possible_cpu(cpu) {
390 pevent = per_cpu_ptr(cpu_events, cpu);
391 unregister_hw_breakpoint(*pevent);
392 }
393 free_percpu(cpu_events);
394}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100395EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
K.Prasad62a038d2009-06-01 23:43:33 +0530396
397static struct notifier_block hw_breakpoint_exceptions_nb = {
398 .notifier_call = hw_breakpoint_exceptions_notify,
399 /* we need to be notified first */
400 .priority = 0x7fffffff
401};
402
403static int __init init_hw_breakpoint(void)
404{
405 return register_die_notifier(&hw_breakpoint_exceptions_nb);
406}
K.Prasad62a038d2009-06-01 23:43:33 +0530407core_initcall(init_hw_breakpoint);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200408
409
410struct pmu perf_ops_bp = {
411 .enable = arch_install_hw_breakpoint,
412 .disable = arch_uninstall_hw_breakpoint,
413 .read = hw_breakpoint_pmu_read,
414 .unthrottle = hw_breakpoint_pmu_unthrottle
415};