4 * This provides a low-level interface to the hardware's Debug Store
5 * feature that is used for branch trace store (BTS) and
6 * precise-event based sampling (PEBS).
9 * - per-thread and per-cpu allocation of BTS and PEBS
10 * - buffer overflow handling (to be done)
14 * - get_task_struct on all traced tasks
15 * - current is allowed to trace tasks
18 * Copyright (C) 2007-2008 Intel Corporation.
19 * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
30 #include <linux/kernel.h>
34 * The configuration for a particular DS hardware implementation.
36 struct ds_configuration {
37 /* the size of the DS structure in bytes */
38 unsigned char sizeof_ds;
39 /* the size of one pointer-typed field in the DS structure in bytes;
40 this covers the first 8 fields related to buffer management. */
41 unsigned char sizeof_field;
42 /* the size of a BTS/PEBS record in bytes */
43 unsigned char sizeof_rec[2];
45 static struct ds_configuration ds_cfg;
48 * A BTS or PEBS tracer.
50 * This holds the configuration of the tracer and serves as a handle
51 * to identify tracers.
54 /* the DS context (partially) owned by this tracer */
55 struct ds_context *context;
56 /* the buffer provided on ds_request() and its size in bytes */
62 /* the common DS part */
64 /* buffer overflow notification function */
65 bts_ovfl_callback_t ovfl;
69 /* the common DS part */
71 /* buffer overflow notification function */
72 pebs_ovfl_callback_t ovfl;
76 * Debug Store (DS) save area configuration (see Intel64 and IA32
77 * Architectures Software Developer's Manual, section 18.5)
79 * The DS configuration consists of the following fields; different
80 * architetures vary in the size of those fields.
81 * - double-word aligned base linear address of the BTS buffer
82 * - write pointer into the BTS buffer
83 * - end linear address of the BTS buffer (one byte beyond the end of
85 * - interrupt pointer into BTS buffer
86 * (interrupt occurs when write pointer passes interrupt pointer)
87 * - double-word aligned base linear address of the PEBS buffer
88 * - write pointer into the PEBS buffer
89 * - end linear address of the PEBS buffer (one byte beyond the end of
91 * - interrupt pointer into PEBS buffer
92 * (interrupt occurs when write pointer passes interrupt pointer)
93 * - value to which counter is reset following counter overflow
95 * Later architectures use 64bit pointers throughout, whereas earlier
96 * architectures use 32bit pointers in 32bit mode.
99 * We compute the base address for the first 8 fields based on:
100 * - the field size stored in the DS configuration
101 * - the relative field position
102 * - an offset giving the start of the respective region
104 * This offset is further used to index various arrays holding
105 * information for BTS and PEBS at the respective index.
107 * On later 32bit processors, we only access the lower 32bit of the
108 * 64bit pointer fields. The upper halves will be zeroed out.
115 ds_interrupt_threshold,
123 static inline unsigned long ds_get(const unsigned char *base,
124 enum ds_qualifier qual, enum ds_field field)
126 base += (ds_cfg.sizeof_field * (field + (4 * qual)));
127 return *(unsigned long *)base;
130 static inline void ds_set(unsigned char *base, enum ds_qualifier qual,
131 enum ds_field field, unsigned long value)
133 base += (ds_cfg.sizeof_field * (field + (4 * qual)));
134 (*(unsigned long *)base) = value;
137 #define DS_ALIGNMENT (1 << 3) /* BTS and PEBS buffer alignment */
141 * Locking is done only for allocating BTS or PEBS resources.
143 static spinlock_t ds_lock = __SPIN_LOCK_UNLOCKED(ds_lock);
147 * We either support (system-wide) per-cpu or per-thread allocation.
148 * We distinguish the two based on the task_struct pointer, where a
149 * NULL pointer indicates per-cpu allocation for the current cpu.
151 * Allocations are use-counted. As soon as resources are allocated,
152 * further allocations must be of the same type (per-cpu or
153 * per-thread). We model this by counting allocations (i.e. the number
154 * of tracers of a certain type) for one type negatively:
156 * >0 number of per-thread tracers
157 * <0 number of per-cpu tracers
159 * The below functions to get and put tracers and to check the
160 * allocation type require the ds_lock to be held by the caller.
162 * Tracers essentially gives the number of ds contexts for a certain
163 * type of allocation.
167 static inline void get_tracer(struct task_struct *task)
169 tracers += (task ? 1 : -1);
172 static inline void put_tracer(struct task_struct *task)
174 tracers -= (task ? 1 : -1);
177 static inline int check_tracer(struct task_struct *task)
179 return (task ? (tracers >= 0) : (tracers <= 0));
184 * The DS context is either attached to a thread or to a cpu:
185 * - in the former case, the thread_struct contains a pointer to the
187 * - in the latter case, we use a static array of per-cpu context
190 * Contexts are use-counted. They are allocated on first access and
191 * deallocated when the last user puts the context.
193 static DEFINE_PER_CPU(struct ds_context *, system_context);
195 #define this_system_context per_cpu(system_context, smp_processor_id())
197 static inline struct ds_context *ds_get_context(struct task_struct *task)
199 struct ds_context **p_context =
200 (task ? &task->thread.ds_ctx : &this_system_context);
201 struct ds_context *context = *p_context;
205 context = kzalloc(sizeof(*context), GFP_KERNEL);
209 spin_lock_irqsave(&ds_lock, irq);
214 context = *p_context;
216 *p_context = context;
218 context->this = p_context;
219 context->task = task;
222 set_tsk_thread_flag(task, TIF_DS_AREA_MSR);
224 if (!task || (task == current))
225 wrmsrl(MSR_IA32_DS_AREA,
226 (unsigned long)context->ds);
228 spin_unlock_irqrestore(&ds_lock, irq);
236 static inline void ds_put_context(struct ds_context *context)
243 spin_lock_irqsave(&ds_lock, irq);
245 if (--context->count)
248 *(context->this) = NULL;
251 clear_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);
253 if (!context->task || (context->task == current))
254 wrmsrl(MSR_IA32_DS_AREA, 0);
258 spin_unlock_irqrestore(&ds_lock, irq);
263 * Handle a buffer overflow
265 * context: the ds context
266 * qual: the buffer type
268 static void ds_overflow(struct ds_context *context, enum ds_qualifier qual)
272 struct bts_tracer *tracer =
273 container_of(context->owner[qual],
274 struct bts_tracer, ds);
276 tracer->ovfl(tracer);
280 struct pebs_tracer *tracer =
281 container_of(context->owner[qual],
282 struct pebs_tracer, ds);
284 tracer->ovfl(tracer);
291 static void ds_install_ds_config(struct ds_context *context,
292 enum ds_qualifier qual,
293 void *base, size_t size, size_t ith)
295 unsigned long buffer, adj;
297 /* adjust the buffer address and size to meet alignment
299 * - buffer is double-word aligned
300 * - size is multiple of record size
302 * We checked the size at the very beginning; we have enough
303 * space to do the adjustment.
305 buffer = (unsigned long)base;
307 adj = ALIGN(buffer, DS_ALIGNMENT) - buffer;
311 size /= ds_cfg.sizeof_rec[qual];
312 size *= ds_cfg.sizeof_rec[qual];
314 ds_set(context->ds, qual, ds_buffer_base, buffer);
315 ds_set(context->ds, qual, ds_index, buffer);
316 ds_set(context->ds, qual, ds_absolute_maximum, buffer + size);
318 /* The value for 'no threshold' is -1, which will set the
319 * threshold outside of the buffer, just like we want it.
321 ds_set(context->ds, qual,
322 ds_interrupt_threshold, buffer + size - ith);
325 static int ds_request(struct ds_tracer *tracer, enum ds_qualifier qual,
326 struct task_struct *task,
327 void *base, size_t size, size_t th)
329 struct ds_context *context;
334 if (!ds_cfg.sizeof_ds)
341 /* we require some space to do alignment adjustments below */
343 if (size < (DS_ALIGNMENT + ds_cfg.sizeof_rec[qual]))
346 if (th != (size_t)-1) {
347 th *= ds_cfg.sizeof_rec[qual];
354 tracer->buffer = base;
358 context = ds_get_context(task);
361 tracer->context = context;
364 spin_lock_irqsave(&ds_lock, irq);
367 if (!check_tracer(task))
372 if (context->owner[qual])
374 context->owner[qual] = tracer;
376 spin_unlock_irqrestore(&ds_lock, irq);
379 ds_install_ds_config(context, qual, base, size, th);
386 spin_unlock_irqrestore(&ds_lock, irq);
387 ds_put_context(context);
388 tracer->context = NULL;
393 struct bts_tracer *ds_request_bts(struct task_struct *task,
394 void *base, size_t size,
395 bts_ovfl_callback_t ovfl, size_t th)
397 struct bts_tracer *tracer;
400 /* buffer overflow notification is not yet implemented */
406 tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
411 error = ds_request(&tracer->ds, ds_bts, task, base, size, th);
420 return ERR_PTR(error);
423 struct pebs_tracer *ds_request_pebs(struct task_struct *task,
424 void *base, size_t size,
425 pebs_ovfl_callback_t ovfl, size_t th)
427 struct pebs_tracer *tracer;
430 /* buffer overflow notification is not yet implemented */
436 tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
441 error = ds_request(&tracer->ds, ds_pebs, task, base, size, th);
450 return ERR_PTR(error);
453 static void ds_release(struct ds_tracer *tracer, enum ds_qualifier qual)
455 BUG_ON(tracer->context->owner[qual] != tracer);
456 tracer->context->owner[qual] = NULL;
458 put_tracer(tracer->context->task);
459 ds_put_context(tracer->context);
462 int ds_release_bts(struct bts_tracer *tracer)
467 ds_release(&tracer->ds, ds_bts);
473 int ds_release_pebs(struct pebs_tracer *tracer)
478 ds_release(&tracer->ds, ds_pebs);
484 static size_t ds_get_index(struct ds_context *context, enum ds_qualifier qual)
486 unsigned long base, index;
488 base = ds_get(context->ds, qual, ds_buffer_base);
489 index = ds_get(context->ds, qual, ds_index);
491 return (index - base) / ds_cfg.sizeof_rec[qual];
494 int ds_get_bts_index(struct bts_tracer *tracer, size_t *pos)
502 *pos = ds_get_index(tracer->ds.context, ds_bts);
507 int ds_get_pebs_index(struct pebs_tracer *tracer, size_t *pos)
515 *pos = ds_get_index(tracer->ds.context, ds_pebs);
520 static size_t ds_get_end(struct ds_context *context, enum ds_qualifier qual)
522 unsigned long base, max;
524 base = ds_get(context->ds, qual, ds_buffer_base);
525 max = ds_get(context->ds, qual, ds_absolute_maximum);
527 return (max - base) / ds_cfg.sizeof_rec[qual];
530 int ds_get_bts_end(struct bts_tracer *tracer, size_t *pos)
538 *pos = ds_get_end(tracer->ds.context, ds_bts);
543 int ds_get_pebs_end(struct pebs_tracer *tracer, size_t *pos)
551 *pos = ds_get_end(tracer->ds.context, ds_pebs);
556 static int ds_access(struct ds_context *context, enum ds_qualifier qual,
557 size_t index, const void **record)
559 unsigned long base, idx;
564 base = ds_get(context->ds, qual, ds_buffer_base);
565 idx = base + (index * ds_cfg.sizeof_rec[qual]);
567 if (idx > ds_get(context->ds, qual, ds_absolute_maximum))
570 *record = (const void *)idx;
572 return ds_cfg.sizeof_rec[qual];
575 int ds_access_bts(struct bts_tracer *tracer, size_t index,
581 return ds_access(tracer->ds.context, ds_bts, index, record);
584 int ds_access_pebs(struct pebs_tracer *tracer, size_t index,
590 return ds_access(tracer->ds.context, ds_pebs, index, record);
593 static int ds_write(struct ds_context *context, enum ds_qualifier qual,
594 const void *record, size_t size)
596 int bytes_written = 0;
602 unsigned long base, index, end, write_end, int_th;
603 unsigned long write_size, adj_write_size;
606 * write as much as possible without producing an
607 * overflow interrupt.
609 * interrupt_threshold must either be
610 * - bigger than absolute_maximum or
611 * - point to a record between buffer_base and absolute_maximum
613 * index points to a valid record.
615 base = ds_get(context->ds, qual, ds_buffer_base);
616 index = ds_get(context->ds, qual, ds_index);
617 end = ds_get(context->ds, qual, ds_absolute_maximum);
618 int_th = ds_get(context->ds, qual, ds_interrupt_threshold);
620 write_end = min(end, int_th);
622 /* if we are already beyond the interrupt threshold,
623 * we fill the entire buffer */
624 if (write_end <= index)
627 if (write_end <= index)
630 write_size = min((unsigned long) size, write_end - index);
631 memcpy((void *)index, record, write_size);
633 record = (const char *)record + write_size;
635 bytes_written += write_size;
637 adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
638 adj_write_size *= ds_cfg.sizeof_rec[qual];
640 /* zero out trailing bytes */
641 memset((char *)index + write_size, 0,
642 adj_write_size - write_size);
643 index += adj_write_size;
647 ds_set(context->ds, qual, ds_index, index);
650 ds_overflow(context, qual);
653 return bytes_written;
656 int ds_write_bts(struct bts_tracer *tracer, const void *record, size_t size)
661 return ds_write(tracer->ds.context, ds_bts, record, size);
664 int ds_write_pebs(struct pebs_tracer *tracer, const void *record, size_t size)
669 return ds_write(tracer->ds.context, ds_pebs, record, size);
672 static void ds_reset_or_clear(struct ds_context *context,
673 enum ds_qualifier qual, int clear)
675 unsigned long base, end;
677 base = ds_get(context->ds, qual, ds_buffer_base);
678 end = ds_get(context->ds, qual, ds_absolute_maximum);
681 memset((void *)base, 0, end - base);
683 ds_set(context->ds, qual, ds_index, base);
686 int ds_reset_bts(struct bts_tracer *tracer)
691 ds_reset_or_clear(tracer->ds.context, ds_bts, /* clear = */ 0);
696 int ds_reset_pebs(struct pebs_tracer *tracer)
701 ds_reset_or_clear(tracer->ds.context, ds_pebs, /* clear = */ 0);
706 int ds_clear_bts(struct bts_tracer *tracer)
711 ds_reset_or_clear(tracer->ds.context, ds_bts, /* clear = */ 1);
716 int ds_clear_pebs(struct pebs_tracer *tracer)
721 ds_reset_or_clear(tracer->ds.context, ds_pebs, /* clear = */ 1);
726 int ds_get_pebs_reset(struct pebs_tracer *tracer, u64 *value)
734 *value = *(u64 *)(tracer->ds.context->ds + (ds_cfg.sizeof_field * 8));
739 int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value)
744 *(u64 *)(tracer->ds.context->ds + (ds_cfg.sizeof_field * 8)) = value;
749 static const struct ds_configuration ds_cfg_var = {
750 .sizeof_ds = sizeof(long) * 12,
751 .sizeof_field = sizeof(long),
752 .sizeof_rec[ds_bts] = sizeof(long) * 3,
754 .sizeof_rec[ds_pebs] = sizeof(long) * 10
756 .sizeof_rec[ds_pebs] = sizeof(long) * 18
759 static const struct ds_configuration ds_cfg_64 = {
762 .sizeof_rec[ds_bts] = 8 * 3,
764 .sizeof_rec[ds_pebs] = 8 * 10
766 .sizeof_rec[ds_pebs] = 8 * 18
771 ds_configure(const struct ds_configuration *cfg)
775 printk(KERN_INFO "DS available\n");
777 BUG_ON(MAX_SIZEOF_DS < ds_cfg.sizeof_ds);
780 void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
784 switch (c->x86_model) {
786 /* sorry, don't know about them */
789 case 0xE: /* Pentium M */
790 ds_configure(&ds_cfg_var);
792 default: /* Core2, Atom, ... */
793 ds_configure(&ds_cfg_64);
798 switch (c->x86_model) {
801 case 0x2: /* Netburst */
802 ds_configure(&ds_cfg_var);
805 /* sorry, don't know about them */
810 /* sorry, don't know about them */
815 void ds_free(struct ds_context *context)
817 /* This is called when the task owning the parameter context
818 * is dying. There should not be any user of that context left
819 * to disturb us, anymore. */
820 unsigned long leftovers = context->count;
821 while (leftovers--) {
822 put_tracer(context->task);
823 ds_put_context(context);