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.
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.
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.
16 * Copyright (C) 2007 Alan Stern
17 * Copyright (C) IBM Corporation, 2009
21 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
22 * using the CPU's debug registers.
23 * This file contains the arch-independent routines.
26 #include <linux/irqflags.h>
27 #include <linux/kallsyms.h>
28 #include <linux/notifier.h>
29 #include <linux/kprobes.h>
30 #include <linux/kdebug.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/percpu.h>
34 #include <linux/sched.h>
35 #include <linux/init.h>
36 #include <linux/smp.h>
38 #include <asm/hw_breakpoint.h>
39 #include <asm/processor.h>
42 #include <asm/debugreg.h>
45 * Spinlock that protects all (un)register operations over kernel/user-space
48 static DEFINE_SPINLOCK(hw_breakpoint_lock);
50 /* Array of kernel-space breakpoint structures */
51 struct hw_breakpoint *hbp_kernel[HBP_NUM];
54 * Per-processor copy of hbp_kernel[]. Used only when hbp_kernel is being
55 * modified but we need the older copy to handle any hbp exceptions. It will
56 * sync with hbp_kernel[] value after updation is done through IPIs.
58 DEFINE_PER_CPU(struct hw_breakpoint*, this_hbp_kernel[HBP_NUM]);
61 * Kernel breakpoints grow downwards, starting from HBP_NUM
62 * 'hbp_kernel_pos' denotes lowest numbered breakpoint register occupied for
63 * kernel-space request. We will initialise it here and not in an __init
64 * routine because load_debug_registers(), which uses this variable can be
65 * called very early during CPU initialisation.
67 unsigned int hbp_kernel_pos = HBP_NUM;
70 * An array containing refcount of threads using a given bkpt register
71 * Accesses are synchronised by acquiring hw_breakpoint_lock
73 unsigned int hbp_user_refcount[HBP_NUM];
76 * Load the debug registers during startup of a CPU.
78 void load_debug_registers(void)
81 struct task_struct *tsk = current;
83 spin_lock_bh(&hw_breakpoint_lock);
85 /* Prevent IPIs for new kernel breakpoint updates */
86 local_irq_save(flags);
87 arch_update_kernel_hw_breakpoint(NULL);
88 local_irq_restore(flags);
90 if (test_tsk_thread_flag(tsk, TIF_DEBUG))
91 arch_install_thread_hw_breakpoint(tsk);
93 spin_unlock_bh(&hw_breakpoint_lock);
97 * Erase all the hardware breakpoint info associated with a thread.
99 * If tsk != current then tsk must not be usable (for example, a
100 * child being cleaned up from a failed fork).
102 void flush_thread_hw_breakpoint(struct task_struct *tsk)
105 struct thread_struct *thread = &(tsk->thread);
107 spin_lock_bh(&hw_breakpoint_lock);
109 /* The thread no longer has any breakpoints associated with it */
110 clear_tsk_thread_flag(tsk, TIF_DEBUG);
111 for (i = 0; i < HBP_NUM; i++) {
112 if (thread->hbp[i]) {
113 hbp_user_refcount[i]--;
114 kfree(thread->hbp[i]);
115 thread->hbp[i] = NULL;
119 arch_flush_thread_hw_breakpoint(tsk);
121 /* Actually uninstall the breakpoints if necessary */
123 arch_uninstall_thread_hw_breakpoint();
124 spin_unlock_bh(&hw_breakpoint_lock);
128 * Copy the hardware breakpoint info from a thread to its cloned child.
130 int copy_thread_hw_breakpoint(struct task_struct *tsk,
131 struct task_struct *child, unsigned long clone_flags)
134 * We will assume that breakpoint settings are not inherited
135 * and the child starts out with no debug registers set.
136 * But what about CLONE_PTRACE?
138 clear_tsk_thread_flag(child, TIF_DEBUG);
140 /* We will call flush routine since the debugregs are not inherited */
141 arch_flush_thread_hw_breakpoint(child);
146 static int __register_user_hw_breakpoint(int pos, struct task_struct *tsk,
147 struct hw_breakpoint *bp)
149 struct thread_struct *thread = &(tsk->thread);
152 /* Do not overcommit. Fail if kernel has used the hbp registers */
153 if (pos >= hbp_kernel_pos)
156 rc = arch_validate_hwbkpt_settings(bp, tsk);
160 thread->hbp[pos] = bp;
161 hbp_user_refcount[pos]++;
163 arch_update_user_hw_breakpoint(pos, tsk);
165 * Does it need to be installed right now?
166 * Otherwise it will get installed the next time tsk runs
169 arch_install_thread_hw_breakpoint(tsk);
175 * Modify the address of a hbp register already in use by the task
176 * Do not invoke this in-lieu of a __unregister_user_hw_breakpoint()
178 static int __modify_user_hw_breakpoint(int pos, struct task_struct *tsk,
179 struct hw_breakpoint *bp)
181 struct thread_struct *thread = &(tsk->thread);
183 if ((pos >= hbp_kernel_pos) || (arch_validate_hwbkpt_settings(bp, tsk)))
186 if (thread->hbp[pos] == NULL)
189 thread->hbp[pos] = bp;
191 * 'pos' must be that of a hbp register already used by 'tsk'
192 * Otherwise arch_modify_user_hw_breakpoint() will fail
194 arch_update_user_hw_breakpoint(pos, tsk);
197 arch_install_thread_hw_breakpoint(tsk);
202 static void __unregister_user_hw_breakpoint(int pos, struct task_struct *tsk)
204 hbp_user_refcount[pos]--;
205 tsk->thread.hbp[pos] = NULL;
207 arch_update_user_hw_breakpoint(pos, tsk);
210 arch_install_thread_hw_breakpoint(tsk);
214 * register_user_hw_breakpoint - register a hardware breakpoint for user space
215 * @tsk: pointer to 'task_struct' of the process to which the address belongs
216 * @bp: the breakpoint structure to register
218 * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
219 * @bp->triggered must be set properly before invocation
222 int register_user_hw_breakpoint(struct task_struct *tsk,
223 struct hw_breakpoint *bp)
225 struct thread_struct *thread = &(tsk->thread);
228 spin_lock_bh(&hw_breakpoint_lock);
230 for (i = 0; i < hbp_kernel_pos; i++) {
231 if (!thread->hbp[i]) {
232 rc = __register_user_hw_breakpoint(i, tsk, bp);
237 set_tsk_thread_flag(tsk, TIF_DEBUG);
239 spin_unlock_bh(&hw_breakpoint_lock);
242 EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
245 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
246 * @tsk: pointer to 'task_struct' of the process to which the address belongs
247 * @bp: the breakpoint structure to unregister
250 int modify_user_hw_breakpoint(struct task_struct *tsk, struct hw_breakpoint *bp)
252 struct thread_struct *thread = &(tsk->thread);
253 int i, ret = -ENOENT;
255 spin_lock_bh(&hw_breakpoint_lock);
256 for (i = 0; i < hbp_kernel_pos; i++) {
257 if (bp == thread->hbp[i]) {
258 ret = __modify_user_hw_breakpoint(i, tsk, bp);
262 spin_unlock_bh(&hw_breakpoint_lock);
265 EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
268 * unregister_user_hw_breakpoint - unregister a user-space hardware breakpoint
269 * @tsk: pointer to 'task_struct' of the process to which the address belongs
270 * @bp: the breakpoint structure to unregister
273 void unregister_user_hw_breakpoint(struct task_struct *tsk,
274 struct hw_breakpoint *bp)
276 struct thread_struct *thread = &(tsk->thread);
277 int i, pos = -1, hbp_counter = 0;
279 spin_lock_bh(&hw_breakpoint_lock);
280 for (i = 0; i < hbp_kernel_pos; i++) {
283 if (bp == thread->hbp[i])
287 __unregister_user_hw_breakpoint(pos, tsk);
291 clear_tsk_thread_flag(tsk, TIF_DEBUG);
293 spin_unlock_bh(&hw_breakpoint_lock);
295 EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);
298 * register_kernel_hw_breakpoint - register a hardware breakpoint for kernel space
299 * @bp: the breakpoint structure to register
301 * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
302 * @bp->triggered must be set properly before invocation
305 int register_kernel_hw_breakpoint(struct hw_breakpoint *bp)
309 rc = arch_validate_hwbkpt_settings(bp, NULL);
313 spin_lock_bh(&hw_breakpoint_lock);
316 /* Check if we are over-committing */
317 if ((hbp_kernel_pos > 0) && (!hbp_user_refcount[hbp_kernel_pos-1])) {
319 hbp_kernel[hbp_kernel_pos] = bp;
320 on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
324 spin_unlock_bh(&hw_breakpoint_lock);
327 EXPORT_SYMBOL_GPL(register_kernel_hw_breakpoint);
330 * unregister_kernel_hw_breakpoint - unregister a HW breakpoint for kernel space
331 * @bp: the breakpoint structure to unregister
333 * Uninstalls and unregisters @bp.
335 void unregister_kernel_hw_breakpoint(struct hw_breakpoint *bp)
339 spin_lock_bh(&hw_breakpoint_lock);
341 /* Find the 'bp' in our list of breakpoints for kernel */
342 for (i = hbp_kernel_pos; i < HBP_NUM; i++)
343 if (bp == hbp_kernel[i])
346 /* Check if we did not find a match for 'bp'. If so return early */
348 spin_unlock_bh(&hw_breakpoint_lock);
353 * We'll shift the breakpoints one-level above to compact if
354 * unregistration creates a hole
356 for (j = i; j > hbp_kernel_pos; j--)
357 hbp_kernel[j] = hbp_kernel[j-1];
359 hbp_kernel[hbp_kernel_pos] = NULL;
360 on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
363 spin_unlock_bh(&hw_breakpoint_lock);
365 EXPORT_SYMBOL_GPL(unregister_kernel_hw_breakpoint);
367 static struct notifier_block hw_breakpoint_exceptions_nb = {
368 .notifier_call = hw_breakpoint_exceptions_notify,
369 /* we need to be notified first */
370 .priority = 0x7fffffff
373 static int __init init_hw_breakpoint(void)
375 return register_die_notifier(&hw_breakpoint_exceptions_nb);
378 core_initcall(init_hw_breakpoint);