hw_breakpoint.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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) 2009 IBM Corporation
18
 * Copyright (C) 2009 Frederic Weisbecker <fweisbec@gmail.com>
19 20 21 22 23 24 25
 */

/*
 * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
 * using the CPU's debug registers.
 */

26 27
#include <linux/perf_event.h>
#include <linux/hw_breakpoint.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <linux/irqflags.h>
#include <linux/notifier.h>
#include <linux/kallsyms.h>
#include <linux/kprobes.h>
#include <linux/percpu.h>
#include <linux/kdebug.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/smp.h>

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

44 45 46 47 48
/* Per cpu debug control register value */
DEFINE_PER_CPU(unsigned long, dr7);

/* Per cpu debug address registers values */
static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]);
49 50

/*
51 52
 * Stores the breakpoints currently in use on each breakpoint address
 * register for each cpus
53
 */
54
static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]);
55 56 57 58 59 60


/*
 * Encode the length, type, Exact, and Enable bits for a particular breakpoint
 * as stored in debug register 7.
 */
61
unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type)
62 63 64 65 66 67 68 69 70 71
{
	unsigned long bp_info;

	bp_info = (len | type) & 0xf;
	bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE);
	bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)) |
				DR_GLOBAL_SLOWDOWN;
	return bp_info;
}

72 73 74 75 76
/*
 * Decode the length and type bits for a particular breakpoint as
 * stored in debug register 7.  Return the "enabled" status.
 */
int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type)
77
{
78
	int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
79

80 81
	*len = (bp_info & 0xc) | 0x40;
	*type = (bp_info & 0x3) | 0x80;
82

83
	return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
84 85 86
}

/*
87 88 89 90 91 92 93
 * Install a perf counter breakpoint.
 *
 * We seek a free debug address register and use it for this
 * breakpoint. Eventually we enable it in the debug control register.
 *
 * Atomic: we hold the counter->ctx->lock and we only handle variables
 * and registers local to this cpu.
94
 */
95
int arch_install_hw_breakpoint(struct perf_event *bp)
96
{
97 98 99 100 101 102 103 104 105 106 107
	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
	unsigned long *dr7;
	int i;

	for (i = 0; i < HBP_NUM; i++) {
		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);

		if (!*slot) {
			*slot = bp;
			break;
		}
108 109
	}

110 111 112 113 114 115 116 117 118 119 120 121
	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
		return -EBUSY;

	set_debugreg(info->address, i);
	__get_cpu_var(cpu_debugreg[i]) = info->address;

	dr7 = &__get_cpu_var(dr7);
	*dr7 |= encode_dr7(i, info->len, info->type);

	set_debugreg(*dr7, 7);

	return 0;
122 123 124
}

/*
125 126 127 128 129 130 131
 * Uninstall the breakpoint contained in the given counter.
 *
 * First we search the debug address register it uses and then we disable
 * it.
 *
 * Atomic: we hold the counter->ctx->lock and we only handle variables
 * and registers local to this cpu.
132
 */
133
void arch_uninstall_hw_breakpoint(struct perf_event *bp)
134
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
	unsigned long *dr7;
	int i;

	for (i = 0; i < HBP_NUM; i++) {
		struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);

		if (*slot == bp) {
			*slot = NULL;
			break;
		}
	}

	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
		return;
150

151 152 153 154
	dr7 = &__get_cpu_var(dr7);
	*dr7 &= ~encode_dr7(i, info->len, info->type);

	set_debugreg(*dr7, 7);
155 156 157 158 159 160 161
}

static int get_hbp_len(u8 hbp_len)
{
	unsigned int len_in_bytes = 0;

	switch (hbp_len) {
162
	case X86_BREAKPOINT_LEN_1:
163 164
		len_in_bytes = 1;
		break;
165
	case X86_BREAKPOINT_LEN_2:
166 167
		len_in_bytes = 2;
		break;
168
	case X86_BREAKPOINT_LEN_4:
169 170 171
		len_in_bytes = 4;
		break;
#ifdef CONFIG_X86_64
172
	case X86_BREAKPOINT_LEN_8:
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		len_in_bytes = 8;
		break;
#endif
	}
	return len_in_bytes;
}

/*
 * Check for virtual address in user space.
 */
int arch_check_va_in_userspace(unsigned long va, u8 hbp_len)
{
	unsigned int len;

	len = get_hbp_len(hbp_len);

	return (va <= TASK_SIZE - len);
}

/*
 * Check for virtual address in kernel space.
 */
195
static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len)
196 197 198 199 200 201 202 203 204 205 206
{
	unsigned int len;

	len = get_hbp_len(hbp_len);

	return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
}

/*
 * Store a breakpoint's encoded address, length, and type.
 */
207
static int arch_store_info(struct perf_event *bp)
208
{
209
	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
210 211 212 213
	/*
	 * For kernel-addresses, either the address or symbol name can be
	 * specified.
	 */
214 215 216 217
	if (info->name)
		info->address = (unsigned long)
				kallsyms_lookup_name(info->name);
	if (info->address)
218
		return 0;
219

220 221 222
	return -EINVAL;
}

223 224
int arch_bp_generic_fields(int x86_len, int x86_type,
			   int *gen_len, int *gen_type)
225
{
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	/* Len */
	switch (x86_len) {
	case X86_BREAKPOINT_LEN_1:
		*gen_len = HW_BREAKPOINT_LEN_1;
		break;
	case X86_BREAKPOINT_LEN_2:
		*gen_len = HW_BREAKPOINT_LEN_2;
		break;
	case X86_BREAKPOINT_LEN_4:
		*gen_len = HW_BREAKPOINT_LEN_4;
		break;
#ifdef CONFIG_X86_64
	case X86_BREAKPOINT_LEN_8:
		*gen_len = HW_BREAKPOINT_LEN_8;
		break;
#endif
	default:
		return -EINVAL;
	}
245

246 247 248 249
	/* Type */
	switch (x86_type) {
	case X86_BREAKPOINT_EXECUTE:
		*gen_type = HW_BREAKPOINT_X;
250
		break;
251 252
	case X86_BREAKPOINT_WRITE:
		*gen_type = HW_BREAKPOINT_W;
253
		break;
254 255
	case X86_BREAKPOINT_RW:
		*gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
256 257
		break;
	default:
258
		return -EINVAL;
259 260
	}

261 262 263 264 265 266 267 268 269 270 271 272
	return 0;
}


static int arch_build_bp_info(struct perf_event *bp)
{
	struct arch_hw_breakpoint *info = counter_arch_bp(bp);

	info->address = bp->attr.bp_addr;

	/* Len */
	switch (bp->attr.bp_len) {
273
	case HW_BREAKPOINT_LEN_1:
274
		info->len = X86_BREAKPOINT_LEN_1;
275 276
		break;
	case HW_BREAKPOINT_LEN_2:
277
		info->len = X86_BREAKPOINT_LEN_2;
278 279
		break;
	case HW_BREAKPOINT_LEN_4:
280
		info->len = X86_BREAKPOINT_LEN_4;
281 282 283
		break;
#ifdef CONFIG_X86_64
	case HW_BREAKPOINT_LEN_8:
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
		info->len = X86_BREAKPOINT_LEN_8;
		break;
#endif
	default:
		return -EINVAL;
	}

	/* Type */
	switch (bp->attr.bp_type) {
	case HW_BREAKPOINT_W:
		info->type = X86_BREAKPOINT_WRITE;
		break;
	case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
		info->type = X86_BREAKPOINT_RW;
		break;
	case HW_BREAKPOINT_X:
		info->type = X86_BREAKPOINT_EXECUTE;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}
/*
 * Validate the arch-specific HW Breakpoint register settings
 */
int arch_validate_hwbkpt_settings(struct perf_event *bp,
				  struct task_struct *tsk)
{
	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
	unsigned int align;
	int ret;


	ret = arch_build_bp_info(bp);
	if (ret)
		return ret;

	ret = -EINVAL;

	if (info->type == X86_BREAKPOINT_EXECUTE)
		/*
		 * Ptrace-refactoring code
		 * For now, we'll allow instruction breakpoint only for user-space
		 * addresses
		 */
		if ((!arch_check_va_in_userspace(info->address, info->len)) &&
			info->len != X86_BREAKPOINT_EXECUTE)
			return ret;

	switch (info->len) {
	case X86_BREAKPOINT_LEN_1:
		align = 0;
		break;
	case X86_BREAKPOINT_LEN_2:
		align = 1;
		break;
	case X86_BREAKPOINT_LEN_4:
		align = 3;
		break;
#ifdef CONFIG_X86_64
	case X86_BREAKPOINT_LEN_8:
347 348 349 350 351 352 353
		align = 7;
		break;
#endif
	default:
		return ret;
	}

354 355
	if (bp->callback)
		ret = arch_store_info(bp);
356 357 358 359 360 361 362

	if (ret < 0)
		return ret;
	/*
	 * Check that the low-order bits of the address are appropriate
	 * for the alignment implied by len.
	 */
363
	if (info->address & align)
364 365 366 367
		return -EINVAL;

	/* Check that the virtual address is in the proper range */
	if (tsk) {
368
		if (!arch_check_va_in_userspace(info->address, info->len))
369 370
			return -EFAULT;
	} else {
371
		if (!arch_check_va_in_kernelspace(info->address, info->len))
372 373
			return -EFAULT;
	}
374

375 376 377
	return 0;
}

378 379 380 381
/*
 * Release the user breakpoints used by ptrace
 */
void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
382
{
383 384 385 386 387 388 389
	int i;
	struct thread_struct *t = &tsk->thread;

	for (i = 0; i < HBP_NUM; i++) {
		unregister_hw_breakpoint(t->ptrace_bps[i]);
		t->ptrace_bps[i] = NULL;
	}
390 391
}

392 393
#ifdef CONFIG_KVM
void hw_breakpoint_restore(void)
394
{
395 396 397 398 399 400
	set_debugreg(__get_cpu_var(cpu_debugreg[0]), 0);
	set_debugreg(__get_cpu_var(cpu_debugreg[1]), 1);
	set_debugreg(__get_cpu_var(cpu_debugreg[2]), 2);
	set_debugreg(__get_cpu_var(cpu_debugreg[3]), 3);
	set_debugreg(current->thread.debugreg6, 6);
	set_debugreg(__get_cpu_var(dr7), 7);
401
}
402 403
EXPORT_SYMBOL_GPL(hw_breakpoint_restore);
#endif
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

/*
 * Handle debug exception notifications.
 *
 * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
 *
 * NOTIFY_DONE returned if one of the following conditions is true.
 * i) When the causative address is from user-space and the exception
 * is a valid one, i.e. not triggered as a result of lazy debug register
 * switching
 * ii) When there are more bits than trap<n> set in DR6 register (such
 * as BD, BS or BT) indicating that more than one debug condition is
 * met and requires some more action in do_debug().
 *
 * NOTIFY_STOP returned for all other cases
 *
 */
421
static int __kprobes hw_breakpoint_handler(struct die_args *args)
422 423
{
	int i, cpu, rc = NOTIFY_STOP;
424
	struct perf_event *bp;
425 426 427 428 429 430
	unsigned long dr7, dr6;
	unsigned long *dr6_p;

	/* The DR6 value is pointed by args->err */
	dr6_p = (unsigned long *)ERR_PTR(args->err);
	dr6 = *dr6_p;
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	/* Do an early return if no trap bits are set in DR6 */
	if ((dr6 & DR_TRAP_BITS) == 0)
		return NOTIFY_DONE;

	get_debugreg(dr7, 7);
	/* Disable breakpoints during exception handling */
	set_debugreg(0UL, 7);
	/*
	 * Assert that local interrupts are disabled
	 * Reset the DRn bits in the virtualized register value.
	 * The ptrace trigger routine will add in whatever is needed.
	 */
	current->thread.debugreg6 &= ~DR_TRAP_BITS;
	cpu = get_cpu();

	/* Handle all the breakpoints that were triggered */
	for (i = 0; i < HBP_NUM; ++i) {
		if (likely(!(dr6 & (DR_TRAP0 << i))))
			continue;
451

452
		/*
453 454 455 456
		 * The counter may be concurrently released but that can only
		 * occur from a call_rcu() path. We can then safely fetch
		 * the breakpoint, use its callback, touch its counter
		 * while we are in an rcu_read_lock() path.
457
		 */
458 459 460 461 462
		rcu_read_lock();

		bp = per_cpu(bp_per_reg[i], cpu);
		if (bp)
			rc = NOTIFY_DONE;
463 464 465 466 467
		/*
		 * Reset the 'i'th TRAP bit in dr6 to denote completion of
		 * exception handling
		 */
		(*dr6_p) &= ~(DR_TRAP0 << i);
468 469
		/*
		 * bp can be NULL due to lazy debug register switching
470
		 * or due to concurrent perf counter removing.
471
		 */
472 473 474 475 476 477
		if (!bp) {
			rcu_read_unlock();
			break;
		}

		(bp->callback)(bp, args->regs);
478

479
		rcu_read_unlock();
480 481 482 483 484
	}
	if (dr6 & (~DR_TRAP_BITS))
		rc = NOTIFY_DONE;

	set_debugreg(dr7, 7);
485
	put_cpu();
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500
	return rc;
}

/*
 * Handle debug exception notifications.
 */
int __kprobes hw_breakpoint_exceptions_notify(
		struct notifier_block *unused, unsigned long val, void *data)
{
	if (val != DIE_DEBUG)
		return NOTIFY_DONE;

	return hw_breakpoint_handler(data);
}
501 502 503 504 505 506 507 508 509 510

void hw_breakpoint_pmu_read(struct perf_event *bp)
{
	/* TODO */
}

void hw_breakpoint_pmu_unthrottle(struct perf_event *bp)
{
	/* TODO */
}