blob: 02b492504a5ae57a393eaeb4ec1e799e9a47c689 [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 Weisbecker56053172009-12-07 06:46:48 +010086static int task_bp_pinned(struct task_struct *tsk)
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020087{
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020088 struct perf_event_context *ctx = tsk->perf_event_ctxp;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020089 struct list_head *list;
Frederic Weisbecker56053172009-12-07 06:46:48 +010090 struct perf_event *bp;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020091 unsigned long flags;
Frederic Weisbecker56053172009-12-07 06:46:48 +010092 int count = 0;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020093
94 if (WARN_ONCE(!ctx, "No perf context for this task"))
Frederic Weisbecker56053172009-12-07 06:46:48 +010095 return 0;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +020096
97 list = &ctx->event_list;
98
99 spin_lock_irqsave(&ctx->lock, flags);
100
101 /*
102 * The current breakpoint counter is not included in the list
103 * at the open() callback time
104 */
105 list_for_each_entry(bp, list, event_entry) {
106 if (bp->attr.type == PERF_TYPE_BREAKPOINT)
107 count++;
108 }
109
110 spin_unlock_irqrestore(&ctx->lock, flags);
111
Frederic Weisbecker56053172009-12-07 06:46:48 +0100112 return count;
113}
114
115/*
116 * Report the number of pinned/un-pinned breakpoints we have in
117 * a given cpu (cpu > -1) or in all of them (cpu = -1).
118 */
119static void
120fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp)
121{
122 int cpu = bp->cpu;
123 struct task_struct *tsk = bp->ctx->task;
124
125 if (cpu >= 0) {
126 slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
127 if (!tsk)
128 slots->pinned += max_task_bp_pinned(cpu);
129 else
130 slots->pinned += task_bp_pinned(tsk);
131 slots->flexible = per_cpu(nr_bp_flexible, cpu);
132
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200133 return;
Frederic Weisbecker56053172009-12-07 06:46:48 +0100134 }
135
136 for_each_online_cpu(cpu) {
137 unsigned int nr;
138
139 nr = per_cpu(nr_cpu_bp_pinned, cpu);
140 if (!tsk)
141 nr += max_task_bp_pinned(cpu);
142 else
143 nr += task_bp_pinned(tsk);
144
145 if (nr > slots->pinned)
146 slots->pinned = nr;
147
148 nr = per_cpu(nr_bp_flexible, cpu);
149
150 if (nr > slots->flexible)
151 slots->flexible = nr;
152 }
153}
154
155/*
156 * Add a pinned breakpoint for the given task in our constraint table
157 */
158static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
159{
160 unsigned int *tsk_pinned;
161 int count = 0;
162
163 count = task_bp_pinned(tsk);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200164
Andrew Morton11e66352009-11-25 23:01:50 -0800165 tsk_pinned = per_cpu(task_bp_pinned, cpu);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200166 if (enable) {
Andrew Morton11e66352009-11-25 23:01:50 -0800167 tsk_pinned[count]++;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200168 if (count > 0)
Andrew Morton11e66352009-11-25 23:01:50 -0800169 tsk_pinned[count-1]--;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200170 } else {
Andrew Morton11e66352009-11-25 23:01:50 -0800171 tsk_pinned[count]--;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200172 if (count > 0)
Andrew Morton11e66352009-11-25 23:01:50 -0800173 tsk_pinned[count-1]++;
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200174 }
175}
176
177/*
178 * Add/remove the given breakpoint in our constraint table
179 */
180static void toggle_bp_slot(struct perf_event *bp, bool enable)
181{
182 int cpu = bp->cpu;
183 struct task_struct *tsk = bp->ctx->task;
184
185 /* Pinned counter task profiling */
186 if (tsk) {
187 if (cpu >= 0) {
188 toggle_bp_task_slot(tsk, cpu, enable);
189 return;
190 }
191
192 for_each_online_cpu(cpu)
193 toggle_bp_task_slot(tsk, cpu, enable);
194 return;
195 }
196
197 /* Pinned counter cpu profiling */
198 if (enable)
199 per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
200 else
201 per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
202}
203
204/*
205 * Contraints to check before allowing this new breakpoint counter:
206 *
207 * == Non-pinned counter == (Considered as pinned for now)
208 *
209 * - If attached to a single cpu, check:
210 *
211 * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
212 * + max(per_cpu(task_bp_pinned, cpu)))) < HBP_NUM
213 *
214 * -> If there are already non-pinned counters in this cpu, it means
215 * there is already a free slot for them.
216 * Otherwise, we check that the maximum number of per task
217 * breakpoints (for this cpu) plus the number of per cpu breakpoint
218 * (for this cpu) doesn't cover every registers.
219 *
220 * - If attached to every cpus, check:
221 *
222 * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
223 * + max(per_cpu(task_bp_pinned, *)))) < HBP_NUM
224 *
225 * -> This is roughly the same, except we check the number of per cpu
226 * bp for every cpu and we keep the max one. Same for the per tasks
227 * breakpoints.
228 *
229 *
230 * == Pinned counter ==
231 *
232 * - If attached to a single cpu, check:
233 *
234 * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
235 * + max(per_cpu(task_bp_pinned, cpu))) < HBP_NUM
236 *
237 * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
238 * one register at least (or they will never be fed).
239 *
240 * - If attached to every cpus, check:
241 *
242 * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
243 * + max(per_cpu(task_bp_pinned, *))) < HBP_NUM
244 */
245int reserve_bp_slot(struct perf_event *bp)
246{
247 struct bp_busy_slots slots = {0};
248 int ret = 0;
249
250 mutex_lock(&nr_bp_mutex);
251
Frederic Weisbecker56053172009-12-07 06:46:48 +0100252 fetch_bp_busy_slots(&slots, bp);
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200253
254 /* Flexible counters need to keep at least one slot */
255 if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
256 ret = -ENOSPC;
257 goto end;
258 }
259
260 toggle_bp_slot(bp, true);
261
262end:
263 mutex_unlock(&nr_bp_mutex);
264
265 return ret;
266}
267
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200268void release_bp_slot(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530269{
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200270 mutex_lock(&nr_bp_mutex);
271
272 toggle_bp_slot(bp, false);
273
274 mutex_unlock(&nr_bp_mutex);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200275}
K.Prasad62a038d2009-06-01 23:43:33 +0530276
Frederic Weisbeckerba1c8132009-09-10 09:26:21 +0200277
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100278int register_perf_hw_breakpoint(struct perf_event *bp)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200279{
280 int ret;
K.Prasad62a038d2009-06-01 23:43:33 +0530281
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200282 ret = reserve_bp_slot(bp);
283 if (ret)
284 return ret;
K.Prasad62a038d2009-06-01 23:43:33 +0530285
Frederic Weisbeckerfdf6bc92009-11-23 15:42:33 +0100286 /*
287 * Ptrace breakpoints can be temporary perf events only
288 * meant to reserve a slot. In this case, it is created disabled and
289 * we don't want to check the params right now (as we put a null addr)
290 * But perf tools create events as disabled and we want to check
291 * the params for them.
292 * This is a quick hack that will be removed soon, once we remove
293 * the tmp breakpoints from ptrace
294 */
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100295 if (!bp->attr.disabled || !bp->overflow_handler)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200296 ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
K.Prasad62a038d2009-06-01 23:43:33 +0530297
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200298 return ret;
299}
K.Prasad62a038d2009-06-01 23:43:33 +0530300
K.Prasad62a038d2009-06-01 23:43:33 +0530301/**
302 * register_user_hw_breakpoint - register a hardware breakpoint for user space
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100303 * @attr: 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 Weisbecker5fa10b22009-11-27 04:55:53 +0100308register_user_hw_breakpoint(struct perf_event_attr *attr,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100309 perf_overflow_handler_t triggered,
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100310 struct task_struct *tsk)
K.Prasad62a038d2009-06-01 23:43:33 +0530311{
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100312 return perf_event_create_kernel_counter(attr, -1, tsk->pid, triggered);
K.Prasad62a038d2009-06-01 23:43:33 +0530313}
314EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
315
316/**
317 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200318 * @bp: the breakpoint structure to modify
Frederic Weisbecker5fa10b22009-11-27 04:55:53 +0100319 * @attr: new breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200320 * @triggered: callback to trigger when we hit the breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530321 * @tsk: pointer to 'task_struct' of the process to which the address belongs
K.Prasad62a038d2009-06-01 23:43:33 +0530322 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200323struct perf_event *
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100324modify_user_hw_breakpoint(struct perf_event *bp, struct perf_event_attr *attr)
K.Prasad62a038d2009-06-01 23:43:33 +0530325{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200326 /*
327 * FIXME: do it without unregistering
328 * - We don't want to lose our slot
329 * - If the new bp is incorrect, don't lose the older one
330 */
331 unregister_hw_breakpoint(bp);
K.Prasad62a038d2009-06-01 23:43:33 +0530332
Frederic Weisbecker2f0993e2009-12-05 07:06:10 +0100333 return perf_event_create_kernel_counter(attr, -1, bp->ctx->task->pid,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100334 bp->overflow_handler);
K.Prasad62a038d2009-06-01 23:43:33 +0530335}
336EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
337
338/**
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200339 * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
K.Prasad62a038d2009-06-01 23:43:33 +0530340 * @bp: the breakpoint structure to unregister
K.Prasad62a038d2009-06-01 23:43:33 +0530341 */
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200342void unregister_hw_breakpoint(struct perf_event *bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530343{
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200344 if (!bp)
K.Prasad62a038d2009-06-01 23:43:33 +0530345 return;
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200346 perf_event_release_kernel(bp);
347}
348EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
349
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200350/**
351 * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100352 * @attr: breakpoint attributes
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200353 * @triggered: callback to trigger when we hit the breakpoint
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200354 *
355 * @return a set of per_cpu pointers to perf events
356 */
357struct perf_event **
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100358register_wide_hw_breakpoint(struct perf_event_attr *attr,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +0100359 perf_overflow_handler_t triggered)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200360{
361 struct perf_event **cpu_events, **pevent, *bp;
362 long err;
363 int cpu;
364
365 cpu_events = alloc_percpu(typeof(*cpu_events));
366 if (!cpu_events)
367 return ERR_PTR(-ENOMEM);
368
369 for_each_possible_cpu(cpu) {
370 pevent = per_cpu_ptr(cpu_events, cpu);
Frederic Weisbeckerdd1853c2009-11-27 04:55:54 +0100371 bp = perf_event_create_kernel_counter(attr, cpu, -1, triggered);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200372
373 *pevent = bp;
374
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100375 if (IS_ERR(bp)) {
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200376 err = PTR_ERR(bp);
377 goto fail;
378 }
K.Prasad62a038d2009-06-01 23:43:33 +0530379 }
380
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200381 return cpu_events;
K.Prasad62a038d2009-06-01 23:43:33 +0530382
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200383fail:
384 for_each_possible_cpu(cpu) {
385 pevent = per_cpu_ptr(cpu_events, cpu);
Frederic Weisbecker605bfae2009-11-26 05:35:42 +0100386 if (IS_ERR(*pevent))
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200387 break;
388 unregister_hw_breakpoint(*pevent);
389 }
390 free_percpu(cpu_events);
391 /* return the error if any */
392 return ERR_PTR(err);
K.Prasad62a038d2009-06-01 23:43:33 +0530393}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100394EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200395
396/**
397 * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
398 * @cpu_events: the per cpu set of events to unregister
399 */
400void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
401{
402 int cpu;
403 struct perf_event **pevent;
404
405 for_each_possible_cpu(cpu) {
406 pevent = per_cpu_ptr(cpu_events, cpu);
407 unregister_hw_breakpoint(*pevent);
408 }
409 free_percpu(cpu_events);
410}
Frederic Weisbeckerf60d24d2009-11-10 10:17:07 +0100411EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
K.Prasad62a038d2009-06-01 23:43:33 +0530412
413static struct notifier_block hw_breakpoint_exceptions_nb = {
414 .notifier_call = hw_breakpoint_exceptions_notify,
415 /* we need to be notified first */
416 .priority = 0x7fffffff
417};
418
419static int __init init_hw_breakpoint(void)
420{
421 return register_die_notifier(&hw_breakpoint_exceptions_nb);
422}
K.Prasad62a038d2009-06-01 23:43:33 +0530423core_initcall(init_hw_breakpoint);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200424
425
426struct pmu perf_ops_bp = {
427 .enable = arch_install_hw_breakpoint,
428 .disable = arch_uninstall_hw_breakpoint,
429 .read = hw_breakpoint_pmu_read,
430 .unthrottle = hw_breakpoint_pmu_unthrottle
431};