summaryrefslogtreecommitdiff
path: root/kernel/hw_breakpoint.c
blob: c1f64e65a9f38eb0ccfa3db4a8502115e86aa017 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) 2007 Alan Stern
 * Copyright (C) IBM Corporation, 2009
 */

/*
 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
 * using the CPU's debug registers.
 * This file contains the arch-independent routines.
 */

#include <linux/irqflags.h>
#include <linux/kallsyms.h>
#include <linux/notifier.h>
#include <linux/kprobes.h>
#include <linux/kdebug.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/smp.h>

#include <asm/hw_breakpoint.h>
#include <asm/processor.h>

#ifdef CONFIG_X86
#include <asm/debugreg.h>
#endif
/*
 * Spinlock that protects all (un)register operations over kernel/user-space
 * breakpoint requests
 */
static DEFINE_SPINLOCK(hw_breakpoint_lock);

/* Array of kernel-space breakpoint structures */
struct hw_breakpoint *hbp_kernel[HBP_NUM];

/*
 * Per-processor copy of hbp_kernel[]. Used only when hbp_kernel is being
 * modified but we need the older copy to handle any hbp exceptions. It will
 * sync with hbp_kernel[] value after updation is done through IPIs.
 */
DEFINE_PER_CPU(struct hw_breakpoint*, this_hbp_kernel[HBP_NUM]);

/*
 * Kernel breakpoints grow downwards, starting from HBP_NUM
 * 'hbp_kernel_pos' denotes lowest numbered breakpoint register occupied for
 * kernel-space request. We will initialise it here and not in an __init
 * routine because load_debug_registers(), which uses this variable can be
 * called very early during CPU initialisation.
 */
unsigned int hbp_kernel_pos = HBP_NUM;

/*
 * An array containing refcount of threads using a given bkpt register
 * Accesses are synchronised by acquiring hw_breakpoint_lock
 */
unsigned int hbp_user_refcount[HBP_NUM];

/*
 * Load the debug registers during startup of a CPU.
 */
void load_debug_registers(void)
{
	unsigned long flags;
	struct task_struct *tsk = current;

	spin_lock_bh(&hw_breakpoint_lock);

	/* Prevent IPIs for new kernel breakpoint updates */
	local_irq_save(flags);
	arch_update_kernel_hw_breakpoint(NULL);
	local_irq_restore(flags);

	if (test_tsk_thread_flag(tsk, TIF_DEBUG))
		arch_install_thread_hw_breakpoint(tsk);

	spin_unlock_bh(&hw_breakpoint_lock);
}

/*
 * Erase all the hardware breakpoint info associated with a thread.
 *
 * If tsk != current then tsk must not be usable (for example, a
 * child being cleaned up from a failed fork).
 */
void flush_thread_hw_breakpoint(struct task_struct *tsk)
{
	int i;
	struct thread_struct *thread = &(tsk->thread);

	spin_lock_bh(&hw_breakpoint_lock);

	/* The thread no longer has any breakpoints associated with it */
	clear_tsk_thread_flag(tsk, TIF_DEBUG);
	for (i = 0; i < HBP_NUM; i++) {
		if (thread->hbp[i]) {
			hbp_user_refcount[i]--;
			kfree(thread->hbp[i]);
			thread->hbp[i] = NULL;
		}
	}

	arch_flush_thread_hw_breakpoint(tsk);

	/* Actually uninstall the breakpoints if necessary */
	if (tsk == current)
		arch_uninstall_thread_hw_breakpoint();
	spin_unlock_bh(&hw_breakpoint_lock);
}

/*
 * Copy the hardware breakpoint info from a thread to its cloned child.
 */
int copy_thread_hw_breakpoint(struct task_struct *tsk,
		struct task_struct *child, unsigned long clone_flags)
{
	/*
	 * We will assume that breakpoint settings are not inherited
	 * and the child starts out with no debug registers set.
	 * But what about CLONE_PTRACE?
	 */
	clear_tsk_thread_flag(child, TIF_DEBUG);

	/* We will call flush routine since the debugregs are not inherited */
	arch_flush_thread_hw_breakpoint(child);

	return 0;
}

static int __register_user_hw_breakpoint(int pos, struct task_struct *tsk,
					struct hw_breakpoint *bp)
{
	struct thread_struct *thread = &(tsk->thread);
	int rc;

	/* Do not overcommit. Fail if kernel has used the hbp registers */
	if (pos >= hbp_kernel_pos)
		return -ENOSPC;

	rc = arch_validate_hwbkpt_settings(bp, tsk);
	if (rc)
		return rc;

	thread->hbp[pos] = bp;
	hbp_user_refcount[pos]++;

	arch_update_user_hw_breakpoint(pos, tsk);
	/*
	 * Does it need to be installed right now?
	 * Otherwise it will get installed the next time tsk runs
	 */
	if (tsk == current)
		arch_install_thread_hw_breakpoint(tsk);

	return rc;
}

/*
 * Modify the address of a hbp register already in use by the task
 * Do not invoke this in-lieu of a __unregister_user_hw_breakpoint()
 */
static int __modify_user_hw_breakpoint(int pos, struct task_struct *tsk,
					struct hw_breakpoint *bp)
{
	struct thread_struct *thread = &(tsk->thread);

	if ((pos >= hbp_kernel_pos) || (arch_validate_hwbkpt_settings(bp, tsk)))
		return -EINVAL;

	if (thread->hbp[pos] == NULL)
		return -EINVAL;

	thread->hbp[pos] = bp;
	/*
	 * 'pos' must be that of a hbp register already used by 'tsk'
	 * Otherwise arch_modify_user_hw_breakpoint() will fail
	 */
	arch_update_user_hw_breakpoint(pos, tsk);

	if (tsk == current)
		arch_install_thread_hw_breakpoint(tsk);

	return 0;
}

static void __unregister_user_hw_breakpoint(int pos, struct task_struct *tsk)
{
	hbp_user_refcount[pos]--;
	tsk->thread.hbp[pos] = NULL;

	arch_update_user_hw_breakpoint(pos, tsk);

	if (tsk == current)
		arch_install_thread_hw_breakpoint(tsk);
}

/**
 * register_user_hw_breakpoint - register a hardware breakpoint for user space
 * @tsk: pointer to 'task_struct' of the process to which the address belongs
 * @bp: the breakpoint structure to register
 *
 * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
 * @bp->triggered must be set properly before invocation
 *
 */
int register_user_hw_breakpoint(struct task_struct *tsk,
					struct hw_breakpoint *bp)
{
	struct thread_struct *thread = &(tsk->thread);
	int i, rc = -ENOSPC;

	spin_lock_bh(&hw_breakpoint_lock);

	for (i = 0; i < hbp_kernel_pos; i++) {
		if (!thread->hbp[i]) {
			rc = __register_user_hw_breakpoint(i, tsk, bp);
			break;
		}
	}
	if (!rc)
		set_tsk_thread_flag(tsk, TIF_DEBUG);

	spin_unlock_bh(&hw_breakpoint_lock);
	return rc;
}
EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);

/**
 * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
 * @tsk: pointer to 'task_struct' of the process to which the address belongs
 * @bp: the breakpoint structure to unregister
 *
 */
int modify_user_hw_breakpoint(struct task_struct *tsk, struct hw_breakpoint *bp)
{
	struct thread_struct *thread = &(tsk->thread);
	int i, ret = -ENOENT;

	spin_lock_bh(&hw_breakpoint_lock);
	for (i = 0; i < hbp_kernel_pos; i++) {
		if (bp == thread->hbp[i]) {
			ret = __modify_user_hw_breakpoint(i, tsk, bp);
			break;
		}
	}
	spin_unlock_bh(&hw_breakpoint_lock);
	return ret;
}
EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);

/**
 * unregister_user_hw_breakpoint - unregister a user-space hardware breakpoint
 * @tsk: pointer to 'task_struct' of the process to which the address belongs
 * @bp: the breakpoint structure to unregister
 *
 */
void unregister_user_hw_breakpoint(struct task_struct *tsk,
						struct hw_breakpoint *bp)
{
	struct thread_struct *thread = &(tsk->thread);
	int i, pos = -1, hbp_counter = 0;

	spin_lock_bh(&hw_breakpoint_lock);
	for (i = 0; i < hbp_kernel_pos; i++) {
		if (thread->hbp[i])
			hbp_counter++;
		if (bp == thread->hbp[i])
			pos = i;
	}
	if (pos >= 0) {
		__unregister_user_hw_breakpoint(pos, tsk);
		hbp_counter--;
	}
	if (!hbp_counter)
		clear_tsk_thread_flag(tsk, TIF_DEBUG);

	spin_unlock_bh(&hw_breakpoint_lock);
}
EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);

/**
 * register_kernel_hw_breakpoint - register a hardware breakpoint for kernel space
 * @bp: the breakpoint structure to register
 *
 * @bp.info->name or @bp.info->address, @bp.info->len, @bp.info->type and
 * @bp->triggered must be set properly before invocation
 *
 */
int register_kernel_hw_breakpoint(struct hw_breakpoint *bp)
{
	int rc;

	rc = arch_validate_hwbkpt_settings(bp, NULL);
	if (rc)
		return rc;

	spin_lock_bh(&hw_breakpoint_lock);

	rc = -ENOSPC;
	/* Check if we are over-committing */
	if ((hbp_kernel_pos > 0) && (!hbp_user_refcount[hbp_kernel_pos-1])) {
		hbp_kernel_pos--;
		hbp_kernel[hbp_kernel_pos] = bp;
		on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
		rc = 0;
	}

	spin_unlock_bh(&hw_breakpoint_lock);
	return rc;
}
EXPORT_SYMBOL_GPL(register_kernel_hw_breakpoint);

/**
 * unregister_kernel_hw_breakpoint - unregister a HW breakpoint for kernel space
 * @bp: the breakpoint structure to unregister
 *
 * Uninstalls and unregisters @bp.
 */
void unregister_kernel_hw_breakpoint(struct hw_breakpoint *bp)
{
	int i, j;

	spin_lock_bh(&hw_breakpoint_lock);

	/* Find the 'bp' in our list of breakpoints for kernel */
	for (i = hbp_kernel_pos; i < HBP_NUM; i++)
		if (bp == hbp_kernel[i])
			break;

	/* Check if we did not find a match for 'bp'. If so return early */
	if (i == HBP_NUM) {
		spin_unlock_bh(&hw_breakpoint_lock);
		return;
	}

	/*
	 * We'll shift the breakpoints one-level above to compact if
	 * unregistration creates a hole
	 */
	for (j = i; j > hbp_kernel_pos; j--)
		hbp_kernel[j] = hbp_kernel[j-1];

	hbp_kernel[hbp_kernel_pos] = NULL;
	on_each_cpu(arch_update_kernel_hw_breakpoint, NULL, 1);
	hbp_kernel_pos++;

	spin_unlock_bh(&hw_breakpoint_lock);
}
EXPORT_SYMBOL_GPL(unregister_kernel_hw_breakpoint);

static struct notifier_block hw_breakpoint_exceptions_nb = {
	.notifier_call = hw_breakpoint_exceptions_notify,
	/* we need to be notified first */
	.priority = 0x7fffffff
};

static int __init init_hw_breakpoint(void)
{
	return register_die_notifier(&hw_breakpoint_exceptions_nb);
}

core_initcall(init_hw_breakpoint);