ds.c 20.1 KB
Newer Older
1 2 3 4
/*
 * Debug Store support
 *
 * This provides a low-level interface to the hardware's Debug Store
M
Markus Metzger 已提交
5
 * feature that is used for branch trace store (BTS) and
6 7
 * precise-event based sampling (PEBS).
 *
M
Markus Metzger 已提交
8 9 10 11 12
 * It manages:
 * - per-thread and per-cpu allocation of BTS and PEBS
 * - buffer memory allocation (optional)
 * - buffer overflow handling
 * - buffer access
13
 *
M
Markus Metzger 已提交
14 15 16
 * It assumes:
 * - get_task_struct on all parameter tasks
 * - current is allowed to trace parameter tasks
17 18
 *
 *
M
Markus Metzger 已提交
19 20
 * Copyright (C) 2007-2008 Intel Corporation.
 * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
21 22
 */

M
Markus Metzger 已提交
23 24 25

#ifdef CONFIG_X86_DS

26 27 28 29 30
#include <asm/ds.h>

#include <linux/errno.h>
#include <linux/string.h>
#include <linux/slab.h>
M
Markus Metzger 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <linux/sched.h>


/*
 * The configuration for a particular DS hardware implementation.
 */
struct ds_configuration {
	/* the size of the DS structure in bytes */
	unsigned char  sizeof_ds;
	/* the size of one pointer-typed field in the DS structure in bytes;
	   this covers the first 8 fields related to buffer management. */
	unsigned char  sizeof_field;
	/* the size of a BTS/PEBS record in bytes */
	unsigned char  sizeof_rec[2];
};
static struct ds_configuration ds_cfg;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68


/*
 * Debug Store (DS) save area configuration (see Intel64 and IA32
 * Architectures Software Developer's Manual, section 18.5)
 *
 * The DS configuration consists of the following fields; different
 * architetures vary in the size of those fields.
 * - double-word aligned base linear address of the BTS buffer
 * - write pointer into the BTS buffer
 * - end linear address of the BTS buffer (one byte beyond the end of
 *   the buffer)
 * - interrupt pointer into BTS buffer
 *   (interrupt occurs when write pointer passes interrupt pointer)
 * - double-word aligned base linear address of the PEBS buffer
 * - write pointer into the PEBS buffer
 * - end linear address of the PEBS buffer (one byte beyond the end of
 *   the buffer)
 * - interrupt pointer into PEBS buffer
 *   (interrupt occurs when write pointer passes interrupt pointer)
 * - value to which counter is reset following counter overflow
 *
M
Markus Metzger 已提交
69 70
 * Later architectures use 64bit pointers throughout, whereas earlier
 * architectures use 32bit pointers in 32bit mode.
71 72
 *
 *
M
Markus Metzger 已提交
73 74 75 76
 * We compute the base address for the first 8 fields based on:
 * - the field size stored in the DS configuration
 * - the relative field position
 * - an offset giving the start of the respective region
77
 *
M
Markus Metzger 已提交
78 79
 * This offset is further used to index various arrays holding
 * information for BTS and PEBS at the respective index.
80
 *
M
Markus Metzger 已提交
81 82
 * On later 32bit processors, we only access the lower 32bit of the
 * 64bit pointer fields. The upper halves will be zeroed out.
83 84
 */

M
Markus Metzger 已提交
85 86 87 88 89 90
enum ds_field {
	ds_buffer_base = 0,
	ds_index,
	ds_absolute_maximum,
	ds_interrupt_threshold,
};
91

M
Markus Metzger 已提交
92 93 94
enum ds_qualifier {
	ds_bts  = 0,
	ds_pebs
95 96
};

M
Markus Metzger 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static inline unsigned long ds_get(const unsigned char *base,
				   enum ds_qualifier qual, enum ds_field field)
{
	base += (ds_cfg.sizeof_field * (field + (4 * qual)));
	return *(unsigned long *)base;
}

static inline void ds_set(unsigned char *base, enum ds_qualifier qual,
			  enum ds_field field, unsigned long value)
{
	base += (ds_cfg.sizeof_field * (field + (4 * qual)));
	(*(unsigned long *)base) = value;
}


112
/*
M
Markus Metzger 已提交
113 114 115 116 117 118
 * Locking is done only for allocating BTS or PEBS resources and for
 * guarding context and buffer memory allocation.
 *
 * Most functions require the current task to own the ds context part
 * they are going to access. All the locking is done when validating
 * access to the context.
119
 */
M
Markus Metzger 已提交
120
static spinlock_t ds_lock = __SPIN_LOCK_UNLOCKED(ds_lock);
121 122

/*
M
Markus Metzger 已提交
123 124 125 126
 * Validate that the current task is allowed to access the BTS/PEBS
 * buffer of the parameter task.
 *
 * Returns 0, if access is granted; -Eerrno, otherwise.
127
 */
M
Markus Metzger 已提交
128 129 130 131 132 133 134 135 136 137 138 139
static inline int ds_validate_access(struct ds_context *context,
				     enum ds_qualifier qual)
{
	if (!context)
		return -EPERM;

	if (context->owner[qual] == current)
		return 0;

	return -EPERM;
}

140 141

/*
M
Markus Metzger 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 * We either support (system-wide) per-cpu or per-thread allocation.
 * We distinguish the two based on the task_struct pointer, where a
 * NULL pointer indicates per-cpu allocation for the current cpu.
 *
 * Allocations are use-counted. As soon as resources are allocated,
 * further allocations must be of the same type (per-cpu or
 * per-thread). We model this by counting allocations (i.e. the number
 * of tracers of a certain type) for one type negatively:
 *   =0  no tracers
 *   >0  number of per-thread tracers
 *   <0  number of per-cpu tracers
 *
 * The below functions to get and put tracers and to check the
 * allocation type require the ds_lock to be held by the caller.
 *
 * Tracers essentially gives the number of ds contexts for a certain
 * type of allocation.
159
 */
M
Markus Metzger 已提交
160 161 162
static long tracers;

static inline void get_tracer(struct task_struct *task)
163
{
M
Markus Metzger 已提交
164
	tracers += (task ? 1 : -1);
165
}
M
Markus Metzger 已提交
166 167

static inline void put_tracer(struct task_struct *task)
168
{
M
Markus Metzger 已提交
169
	tracers -= (task ? 1 : -1);
170
}
M
Markus Metzger 已提交
171 172

static inline int check_tracer(struct task_struct *task)
173
{
M
Markus Metzger 已提交
174
	return (task ? (tracers >= 0) : (tracers <= 0));
175
}
M
Markus Metzger 已提交
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


/*
 * The DS context is either attached to a thread or to a cpu:
 * - in the former case, the thread_struct contains a pointer to the
 *   attached context.
 * - in the latter case, we use a static array of per-cpu context
 *   pointers.
 *
 * Contexts are use-counted. They are allocated on first access and
 * deallocated when the last user puts the context.
 *
 * We distinguish between an allocating and a non-allocating get of a
 * context:
 * - the allocating get is used for requesting BTS/PEBS resources. It
 *   requires the caller to hold the global ds_lock.
 * - the non-allocating get is used for all other cases. A
 *   non-existing context indicates an error. It acquires and releases
 *   the ds_lock itself for obtaining the context.
 *
 * A context and its DS configuration are allocated and deallocated
 * together. A context always has a DS configuration of the
 * appropriate size.
 */
static DEFINE_PER_CPU(struct ds_context *, system_context);

#define this_system_context per_cpu(system_context, smp_processor_id())

/*
 * Returns the pointer to the parameter task's context or to the
 * system-wide context, if task is NULL.
 *
 * Increases the use count of the returned context, if not NULL.
 */
static inline struct ds_context *ds_get_context(struct task_struct *task)
211
{
M
Markus Metzger 已提交
212 213 214 215 216 217 218 219 220 221 222
	struct ds_context *context;

	spin_lock(&ds_lock);

	context = (task ? task->thread.ds_ctx : this_system_context);
	if (context)
		context->count++;

	spin_unlock(&ds_lock);

	return context;
223
}
M
Markus Metzger 已提交
224 225 226 227 228 229 230 231

/*
 * Same as ds_get_context, but allocates the context and it's DS
 * structure, if necessary; returns NULL; if out of memory.
 *
 * pre: requires ds_lock to be held
 */
static inline struct ds_context *ds_alloc_context(struct task_struct *task)
232
{
M
Markus Metzger 已提交
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
	struct ds_context **p_context =
		(task ? &task->thread.ds_ctx : &this_system_context);
	struct ds_context *context = *p_context;

	if (!context) {
		context = kzalloc(sizeof(*context), GFP_KERNEL);

		if (!context)
			return 0;

		context->ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
		if (!context->ds) {
			kfree(context);
			return 0;
		}

		*p_context = context;

		context->this = p_context;
		context->task = task;

		if (task)
			set_tsk_thread_flag(task, TIF_DS_AREA_MSR);

		if (!task || (task == current))
			wrmsr(MSR_IA32_DS_AREA, (unsigned long)context->ds, 0);

		get_tracer(task);
	}

	context->count++;

	return context;
266
}
M
Markus Metzger 已提交
267 268 269 270 271 272

/*
 * Decreases the use count of the parameter context, if not NULL.
 * Deallocates the context, if the use count reaches zero.
 */
static inline void ds_put_context(struct ds_context *context)
273
{
M
Markus Metzger 已提交
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
	if (!context)
		return;

	spin_lock(&ds_lock);

	if (--context->count)
		goto out;

	*(context->this) = 0;

	if (context->task)
		clear_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);

	if (!context->task || (context->task == current))
		wrmsrl(MSR_IA32_DS_AREA, 0);

	put_tracer(context->task);

	/* free any leftover buffers from tracers that did not
	 * deallocate them properly. */
	kfree(context->buffer[ds_bts]);
	kfree(context->buffer[ds_pebs]);
	kfree(context->ds);
	kfree(context);
 out:
	spin_unlock(&ds_lock);
300
}
M
Markus Metzger 已提交
301 302 303 304 305 306 307 308 309 310 311 312


/*
 * Handle a buffer overflow
 *
 * task: the task whose buffers are overflowing;
 *       NULL for a buffer overflow on the current cpu
 * context: the ds context
 * qual: the buffer type
 */
static void ds_overflow(struct task_struct *task, struct ds_context *context,
			enum ds_qualifier qual)
313
{
M
Markus Metzger 已提交
314 315 316 317 318 319 320
	if (!context)
		return;

	if (context->callback[qual])
		(*context->callback[qual])(task);

	/* todo: do some more overflow handling */
321
}
M
Markus Metzger 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334


/*
 * Allocate a non-pageable buffer of the parameter size.
 * Checks the memory and the locked memory rlimit.
 *
 * Returns the buffer, if successful;
 *         NULL, if out of memory or rlimit exceeded.
 *
 * size: the requested buffer size in bytes
 * pages (out): if not NULL, contains the number of pages reserved
 */
static inline void *ds_allocate_buffer(size_t size, unsigned int *pages)
335
{
M
Markus Metzger 已提交
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
	unsigned long rlim, vm, pgsz;
	void *buffer;

	pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;

	rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
	vm   = current->mm->total_vm  + pgsz;
	if (rlim < vm)
		return 0;

	rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
	vm   = current->mm->locked_vm  + pgsz;
	if (rlim < vm)
		return 0;

	buffer = kzalloc(size, GFP_KERNEL);
	if (!buffer)
		return 0;

	current->mm->total_vm  += pgsz;
	current->mm->locked_vm += pgsz;

	if (pages)
		*pages = pgsz;

	return buffer;
362
}
M
Markus Metzger 已提交
363 364 365

static int ds_request(struct task_struct *task, void *base, size_t size,
		      ds_ovfl_callback_t ovfl, enum ds_qualifier qual)
366
{
M
Markus Metzger 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	struct ds_context *context;
	unsigned long buffer, adj;
	const unsigned long alignment = (1 << 3);
	int error = 0;

	if (!ds_cfg.sizeof_ds)
		return -EOPNOTSUPP;

	/* we require some space to do alignment adjustments below */
	if (size < (alignment + ds_cfg.sizeof_rec[qual]))
		return -EINVAL;

	/* buffer overflow notification is not yet implemented */
	if (ovfl)
		return -EOPNOTSUPP;


	spin_lock(&ds_lock);

	if (!check_tracer(task))
		return -EPERM;

	error = -ENOMEM;
	context = ds_alloc_context(task);
	if (!context)
		goto out_unlock;

	error = -EALREADY;
	if (context->owner[qual] == current)
		goto out_unlock;
	error = -EPERM;
	if (context->owner[qual] != 0)
		goto out_unlock;
	context->owner[qual] = current;

	spin_unlock(&ds_lock);


	error = -ENOMEM;
	if (!base) {
		base = ds_allocate_buffer(size, &context->pages[qual]);
		if (!base)
			goto out_release;

		context->buffer[qual]   = base;
	}
	error = 0;

	context->callback[qual] = ovfl;

	/* adjust the buffer address and size to meet alignment
	 * constraints:
	 * - buffer is double-word aligned
	 * - size is multiple of record size
	 *
	 * We checked the size at the very beginning; we have enough
	 * space to do the adjustment.
	 */
	buffer = (unsigned long)base;

	adj = ALIGN(buffer, alignment) - buffer;
	buffer += adj;
	size   -= adj;

	size /= ds_cfg.sizeof_rec[qual];
	size *= ds_cfg.sizeof_rec[qual];

	ds_set(context->ds, qual, ds_buffer_base, buffer);
	ds_set(context->ds, qual, ds_index, buffer);
	ds_set(context->ds, qual, ds_absolute_maximum, buffer + size);

	if (ovfl) {
		/* todo: select a suitable interrupt threshold */
	} else
		ds_set(context->ds, qual,
		       ds_interrupt_threshold, buffer + size + 1);

	/* we keep the context until ds_release */
	return error;

 out_release:
	context->owner[qual] = 0;
	ds_put_context(context);
	return error;

 out_unlock:
	spin_unlock(&ds_lock);
	ds_put_context(context);
	return error;
456
}
M
Markus Metzger 已提交
457 458 459

int ds_request_bts(struct task_struct *task, void *base, size_t size,
		   ds_ovfl_callback_t ovfl)
460
{
M
Markus Metzger 已提交
461
	return ds_request(task, base, size, ovfl, ds_bts);
462
}
M
Markus Metzger 已提交
463 464 465

int ds_request_pebs(struct task_struct *task, void *base, size_t size,
		    ds_ovfl_callback_t ovfl)
466
{
M
Markus Metzger 已提交
467
	return ds_request(task, base, size, ovfl, ds_pebs);
468
}
M
Markus Metzger 已提交
469 470

static int ds_release(struct task_struct *task, enum ds_qualifier qual)
471
{
M
Markus Metzger 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	struct ds_context *context;
	int error;

	context = ds_get_context(task);
	error = ds_validate_access(context, qual);
	if (error < 0)
		goto out;

	kfree(context->buffer[qual]);
	context->buffer[qual] = 0;

	current->mm->total_vm  -= context->pages[qual];
	current->mm->locked_vm -= context->pages[qual];
	context->pages[qual] = 0;
	context->owner[qual] = 0;

	/*
	 * we put the context twice:
	 *   once for the ds_get_context
	 *   once for the corresponding ds_request
	 */
	ds_put_context(context);
 out:
	ds_put_context(context);
	return error;
497
}
M
Markus Metzger 已提交
498 499

int ds_release_bts(struct task_struct *task)
500
{
M
Markus Metzger 已提交
501
	return ds_release(task, ds_bts);
502
}
M
Markus Metzger 已提交
503 504

int ds_release_pebs(struct task_struct *task)
505
{
M
Markus Metzger 已提交
506
	return ds_release(task, ds_pebs);
507
}
M
Markus Metzger 已提交
508 509 510

static int ds_get_index(struct task_struct *task, size_t *pos,
			enum ds_qualifier qual)
511
{
M
Markus Metzger 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	struct ds_context *context;
	unsigned long base, index;
	int error;

	context = ds_get_context(task);
	error = ds_validate_access(context, qual);
	if (error < 0)
		goto out;

	base  = ds_get(context->ds, qual, ds_buffer_base);
	index = ds_get(context->ds, qual, ds_index);

	error = ((index - base) / ds_cfg.sizeof_rec[qual]);
	if (pos)
		*pos = error;
 out:
	ds_put_context(context);
	return error;
530
}
M
Markus Metzger 已提交
531 532

int ds_get_bts_index(struct task_struct *task, size_t *pos)
533
{
M
Markus Metzger 已提交
534
	return ds_get_index(task, pos, ds_bts);
535 536
}

M
Markus Metzger 已提交
537 538 539 540
int ds_get_pebs_index(struct task_struct *task, size_t *pos)
{
	return ds_get_index(task, pos, ds_pebs);
}
541

M
Markus Metzger 已提交
542 543
static int ds_get_end(struct task_struct *task, size_t *pos,
		      enum ds_qualifier qual)
544
{
M
Markus Metzger 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	struct ds_context *context;
	unsigned long base, end;
	int error;

	context = ds_get_context(task);
	error = ds_validate_access(context, qual);
	if (error < 0)
		goto out;

	base = ds_get(context->ds, qual, ds_buffer_base);
	end  = ds_get(context->ds, qual, ds_absolute_maximum);

	error = ((end - base) / ds_cfg.sizeof_rec[qual]);
	if (pos)
		*pos = error;
 out:
	ds_put_context(context);
	return error;
}
564

M
Markus Metzger 已提交
565 566 567 568
int ds_get_bts_end(struct task_struct *task, size_t *pos)
{
	return ds_get_end(task, pos, ds_bts);
}
569

M
Markus Metzger 已提交
570 571 572 573
int ds_get_pebs_end(struct task_struct *task, size_t *pos)
{
	return ds_get_end(task, pos, ds_pebs);
}
574

M
Markus Metzger 已提交
575 576 577 578 579 580
static int ds_access(struct task_struct *task, size_t index,
		     const void **record, enum ds_qualifier qual)
{
	struct ds_context *context;
	unsigned long base, idx;
	int error;
581

M
Markus Metzger 已提交
582
	if (!record)
583 584
		return -EINVAL;

M
Markus Metzger 已提交
585 586 587 588
	context = ds_get_context(task);
	error = ds_validate_access(context, qual);
	if (error < 0)
		goto out;
589

M
Markus Metzger 已提交
590 591
	base = ds_get(context->ds, qual, ds_buffer_base);
	idx = base + (index * ds_cfg.sizeof_rec[qual]);
592

M
Markus Metzger 已提交
593 594 595
	error = -EINVAL;
	if (idx > ds_get(context->ds, qual, ds_absolute_maximum))
		goto out;
596

M
Markus Metzger 已提交
597 598 599 600 601
	*record = (const void *)idx;
	error = ds_cfg.sizeof_rec[qual];
 out:
	ds_put_context(context);
	return error;
602 603
}

M
Markus Metzger 已提交
604
int ds_access_bts(struct task_struct *task, size_t index, const void **record)
605
{
M
Markus Metzger 已提交
606
	return ds_access(task, index, record, ds_bts);
607 608
}

M
Markus Metzger 已提交
609
int ds_access_pebs(struct task_struct *task, size_t index, const void **record)
610
{
M
Markus Metzger 已提交
611
	return ds_access(task, index, record, ds_pebs);
M
Markus Metzger 已提交
612 613
}

M
Markus Metzger 已提交
614 615
static int ds_write(struct task_struct *task, const void *record, size_t size,
		    enum ds_qualifier qual, int force)
M
Markus Metzger 已提交
616
{
M
Markus Metzger 已提交
617 618
	struct ds_context *context;
	int error;
619

M
Markus Metzger 已提交
620 621
	if (!record)
		return -EINVAL;
622

M
Markus Metzger 已提交
623 624 625 626
	error = -EPERM;
	context = ds_get_context(task);
	if (!context)
		goto out;
627

M
Markus Metzger 已提交
628 629 630 631 632
	if (!force) {
		error = ds_validate_access(context, qual);
		if (error < 0)
			goto out;
	}
633

M
Markus Metzger 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
	error = 0;
	while (size) {
		unsigned long base, index, end, write_end, int_th;
		unsigned long write_size, adj_write_size;

		/*
		 * write as much as possible without producing an
		 * overflow interrupt.
		 *
		 * interrupt_threshold must either be
		 * - bigger than absolute_maximum or
		 * - point to a record between buffer_base and absolute_maximum
		 *
		 * index points to a valid record.
		 */
		base   = ds_get(context->ds, qual, ds_buffer_base);
		index  = ds_get(context->ds, qual, ds_index);
		end    = ds_get(context->ds, qual, ds_absolute_maximum);
		int_th = ds_get(context->ds, qual, ds_interrupt_threshold);

		write_end = min(end, int_th);

		/* if we are already beyond the interrupt threshold,
		 * we fill the entire buffer */
		if (write_end <= index)
			write_end = end;

		if (write_end <= index)
			goto out;

		write_size = min((unsigned long) size, write_end - index);
		memcpy((void *)index, record, write_size);

		record = (const char *)record + write_size;
		size  -= write_size;
		error += write_size;

		adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
		adj_write_size *= ds_cfg.sizeof_rec[qual];

		/* zero out trailing bytes */
		memset((char *)index + write_size, 0,
		       adj_write_size - write_size);
		index += adj_write_size;

		if (index >= end)
			index = base;
		ds_set(context->ds, qual, ds_index, index);

		if (index >= int_th)
			ds_overflow(task, context, qual);
	}
686

M
Markus Metzger 已提交
687 688 689
 out:
	ds_put_context(context);
	return error;
690 691
}

M
Markus Metzger 已提交
692
int ds_write_bts(struct task_struct *task, const void *record, size_t size)
M
Markus Metzger 已提交
693
{
M
Markus Metzger 已提交
694
	return ds_write(task, record, size, ds_bts, /* force = */ 0);
M
Markus Metzger 已提交
695 696
}

M
Markus Metzger 已提交
697
int ds_write_pebs(struct task_struct *task, const void *record, size_t size)
M
Markus Metzger 已提交
698
{
M
Markus Metzger 已提交
699
	return ds_write(task, record, size, ds_pebs, /* force = */ 0);
M
Markus Metzger 已提交
700 701
}

M
Markus Metzger 已提交
702 703
int ds_unchecked_write_bts(struct task_struct *task,
			   const void *record, size_t size)
M
Markus Metzger 已提交
704
{
M
Markus Metzger 已提交
705
	return ds_write(task, record, size, ds_bts, /* force = */ 1);
M
Markus Metzger 已提交
706 707
}

M
Markus Metzger 已提交
708 709
int ds_unchecked_write_pebs(struct task_struct *task,
			    const void *record, size_t size)
710
{
M
Markus Metzger 已提交
711 712
	return ds_write(task, record, size, ds_pebs, /* force = */ 1);
}
713

M
Markus Metzger 已提交
714 715 716 717 718 719
static int ds_reset_or_clear(struct task_struct *task,
			     enum ds_qualifier qual, int clear)
{
	struct ds_context *context;
	unsigned long base, end;
	int error;
720

M
Markus Metzger 已提交
721 722 723 724
	context = ds_get_context(task);
	error = ds_validate_access(context, qual);
	if (error < 0)
		goto out;
725

M
Markus Metzger 已提交
726 727
	base = ds_get(context->ds, qual, ds_buffer_base);
	end  = ds_get(context->ds, qual, ds_absolute_maximum);
728

M
Markus Metzger 已提交
729 730
	if (clear)
		memset((void *)base, 0, end - base);
731

M
Markus Metzger 已提交
732
	ds_set(context->ds, qual, ds_index, base);
733

M
Markus Metzger 已提交
734 735 736 737
	error = 0;
 out:
	ds_put_context(context);
	return error;
738 739
}

M
Markus Metzger 已提交
740
int ds_reset_bts(struct task_struct *task)
741
{
M
Markus Metzger 已提交
742 743
	return ds_reset_or_clear(task, ds_bts, /* clear = */ 0);
}
744

M
Markus Metzger 已提交
745 746 747 748
int ds_reset_pebs(struct task_struct *task)
{
	return ds_reset_or_clear(task, ds_pebs, /* clear = */ 0);
}
749

M
Markus Metzger 已提交
750 751 752 753
int ds_clear_bts(struct task_struct *task)
{
	return ds_reset_or_clear(task, ds_bts, /* clear = */ 1);
}
754

M
Markus Metzger 已提交
755 756 757 758
int ds_clear_pebs(struct task_struct *task)
{
	return ds_reset_or_clear(task, ds_pebs, /* clear = */ 1);
}
759

M
Markus Metzger 已提交
760 761 762 763
int ds_get_pebs_reset(struct task_struct *task, u64 *value)
{
	struct ds_context *context;
	int error;
764

M
Markus Metzger 已提交
765
	if (!value)
766 767
		return -EINVAL;

M
Markus Metzger 已提交
768 769 770 771
	context = ds_get_context(task);
	error = ds_validate_access(context, ds_pebs);
	if (error < 0)
		goto out;
772

M
Markus Metzger 已提交
773 774 775 776 777 778
	*value = *(u64 *)(context->ds + (ds_cfg.sizeof_field * 8));

	error = 0;
 out:
	ds_put_context(context);
	return error;
779 780
}

M
Markus Metzger 已提交
781
int ds_set_pebs_reset(struct task_struct *task, u64 value)
782
{
M
Markus Metzger 已提交
783 784
	struct ds_context *context;
	int error;
785

M
Markus Metzger 已提交
786 787 788 789
	context = ds_get_context(task);
	error = ds_validate_access(context, ds_pebs);
	if (error < 0)
		goto out;
790

M
Markus Metzger 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803
	*(u64 *)(context->ds + (ds_cfg.sizeof_field * 8)) = value;

	error = 0;
 out:
	ds_put_context(context);
	return error;
}

static const struct ds_configuration ds_cfg_var = {
	.sizeof_ds    = sizeof(long) * 12,
	.sizeof_field = sizeof(long),
	.sizeof_rec[ds_bts]   = sizeof(long) * 3,
	.sizeof_rec[ds_pebs]  = sizeof(long) * 10
804
};
M
Markus Metzger 已提交
805 806 807 808 809
static const struct ds_configuration ds_cfg_64 = {
	.sizeof_ds    = 8 * 12,
	.sizeof_field = 8,
	.sizeof_rec[ds_bts]   = 8 * 3,
	.sizeof_rec[ds_pebs]  = 8 * 10
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
};

static inline void
ds_configure(const struct ds_configuration *cfg)
{
	ds_cfg = *cfg;
}

void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
{
	switch (c->x86) {
	case 0x6:
		switch (c->x86_model) {
		case 0xD:
		case 0xE: /* Pentium M */
M
Markus Metzger 已提交
825
			ds_configure(&ds_cfg_var);
826 827
			break;
		case 0xF: /* Core2 */
M
Markus Metzger 已提交
828 829
        case 0x1C: /* Atom */
			ds_configure(&ds_cfg_64);
830 831 832 833 834 835 836 837 838 839 840
			break;
		default:
			/* sorry, don't know about them */
			break;
		}
		break;
	case 0xF:
		switch (c->x86_model) {
		case 0x0:
		case 0x1:
		case 0x2: /* Netburst */
M
Markus Metzger 已提交
841
			ds_configure(&ds_cfg_var);
842 843 844 845 846 847 848 849 850 851 852
			break;
		default:
			/* sorry, don't know about them */
			break;
		}
		break;
	default:
		/* sorry, don't know about them */
		break;
	}
}
M
Markus Metzger 已提交
853 854 855 856 857 858 859 860 861 862 863

void ds_free(struct ds_context *context)
{
	/* This is called when the task owning the parameter context
	 * is dying. There should not be any user of that context left
	 * to disturb us, anymore. */
	unsigned long leftovers = context->count;
	while (leftovers--)
		ds_put_context(context);
}
#endif /* CONFIG_X86_DS */