blob: 0dc795951d73f0246ee96479eb6464ad4e585cf4 [file] [log] [blame]
Markus Metzgereee3af42008-01-30 13:31:09 +01001/*
2 * Debug Store support
3 *
4 * This provides a low-level interface to the hardware's Debug Store
Markus Metzger93fa7632008-04-08 11:01:58 +02005 * feature that is used for branch trace store (BTS) and
Markus Metzgereee3af42008-01-30 13:31:09 +01006 * precise-event based sampling (PEBS).
7 *
Markus Metzger93fa7632008-04-08 11:01:58 +02008 * It manages:
Markus Metzgerc2724772008-12-11 13:49:59 +01009 * - DS and BTS hardware configuration
Markus Metzger6abb11a2008-11-25 09:05:27 +010010 * - buffer overflow handling (to be done)
Markus Metzger93fa7632008-04-08 11:01:58 +020011 * - buffer access
12 *
Markus Metzgerc2724772008-12-11 13:49:59 +010013 * It does not do:
14 * - security checking (is the caller allowed to trace the task)
15 * - buffer allocation (memory accounting)
Markus Metzgereee3af42008-01-30 13:31:09 +010016 *
17 *
Markus Metzger93fa7632008-04-08 11:01:58 +020018 * Copyright (C) 2007-2008 Intel Corporation.
19 * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
Markus Metzgereee3af42008-01-30 13:31:09 +010020 */
21
Markus Metzger93fa7632008-04-08 11:01:58 +020022
Markus Metzgereee3af42008-01-30 13:31:09 +010023#include <asm/ds.h>
24
25#include <linux/errno.h>
26#include <linux/string.h>
27#include <linux/slab.h>
Markus Metzger93fa7632008-04-08 11:01:58 +020028#include <linux/sched.h>
Ingo Molnar3c9339042008-07-25 11:32:36 +020029#include <linux/mm.h>
Markus Metzgerca0002a2008-11-25 09:01:25 +010030#include <linux/kernel.h>
Markus Metzger93fa7632008-04-08 11:01:58 +020031
32
33/*
34 * The configuration for a particular DS hardware implementation.
35 */
36struct ds_configuration {
Markus Metzgerc2724772008-12-11 13:49:59 +010037 /* the name of the configuration */
38 const char *name;
39 /* the size of one pointer-typed field in the DS structure and
40 in the BTS and PEBS buffers in bytes;
41 this covers the first 8 DS fields related to buffer management. */
Markus Metzger93fa7632008-04-08 11:01:58 +020042 unsigned char sizeof_field;
43 /* the size of a BTS/PEBS record in bytes */
44 unsigned char sizeof_rec[2];
Markus Metzgerc2724772008-12-11 13:49:59 +010045 /* a series of bit-masks to control various features indexed
46 * by enum ds_feature */
47 unsigned long ctl[dsf_ctl_max];
Markus Metzger93fa7632008-04-08 11:01:58 +020048};
Markus Metzgerc2724772008-12-11 13:49:59 +010049static DEFINE_PER_CPU(struct ds_configuration, ds_cfg_array);
50
51#define ds_cfg per_cpu(ds_cfg_array, smp_processor_id())
52
53#define MAX_SIZEOF_DS (12 * 8) /* maximal size of a DS configuration */
54#define MAX_SIZEOF_BTS (3 * 8) /* maximal size of a BTS record */
55#define DS_ALIGNMENT (1 << 3) /* BTS and PEBS buffer alignment */
56
57#define BTS_CONTROL \
58 (ds_cfg.ctl[dsf_bts] | ds_cfg.ctl[dsf_bts_kernel] | ds_cfg.ctl[dsf_bts_user] |\
59 ds_cfg.ctl[dsf_bts_overflow])
60
Markus Metzgereee3af42008-01-30 13:31:09 +010061
Markus Metzgerca0002a2008-11-25 09:01:25 +010062/*
63 * A BTS or PEBS tracer.
64 *
65 * This holds the configuration of the tracer and serves as a handle
66 * to identify tracers.
67 */
68struct ds_tracer {
69 /* the DS context (partially) owned by this tracer */
70 struct ds_context *context;
71 /* the buffer provided on ds_request() and its size in bytes */
72 void *buffer;
73 size_t size;
Markus Metzgerca0002a2008-11-25 09:01:25 +010074};
75
76struct bts_tracer {
77 /* the common DS part */
78 struct ds_tracer ds;
Markus Metzgerc2724772008-12-11 13:49:59 +010079 /* the trace including the DS configuration */
80 struct bts_trace trace;
Markus Metzgerca0002a2008-11-25 09:01:25 +010081 /* buffer overflow notification function */
82 bts_ovfl_callback_t ovfl;
83};
84
85struct pebs_tracer {
86 /* the common DS part */
87 struct ds_tracer ds;
Markus Metzgerc2724772008-12-11 13:49:59 +010088 /* the trace including the DS configuration */
89 struct pebs_trace trace;
Markus Metzgerca0002a2008-11-25 09:01:25 +010090 /* buffer overflow notification function */
91 pebs_ovfl_callback_t ovfl;
92};
Markus Metzgereee3af42008-01-30 13:31:09 +010093
94/*
95 * Debug Store (DS) save area configuration (see Intel64 and IA32
96 * Architectures Software Developer's Manual, section 18.5)
97 *
98 * The DS configuration consists of the following fields; different
99 * architetures vary in the size of those fields.
100 * - double-word aligned base linear address of the BTS buffer
101 * - write pointer into the BTS buffer
102 * - end linear address of the BTS buffer (one byte beyond the end of
103 * the buffer)
104 * - interrupt pointer into BTS buffer
105 * (interrupt occurs when write pointer passes interrupt pointer)
106 * - double-word aligned base linear address of the PEBS buffer
107 * - write pointer into the PEBS buffer
108 * - end linear address of the PEBS buffer (one byte beyond the end of
109 * the buffer)
110 * - interrupt pointer into PEBS buffer
111 * (interrupt occurs when write pointer passes interrupt pointer)
112 * - value to which counter is reset following counter overflow
113 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200114 * Later architectures use 64bit pointers throughout, whereas earlier
115 * architectures use 32bit pointers in 32bit mode.
Markus Metzgereee3af42008-01-30 13:31:09 +0100116 *
117 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200118 * We compute the base address for the first 8 fields based on:
119 * - the field size stored in the DS configuration
120 * - the relative field position
121 * - an offset giving the start of the respective region
Markus Metzgereee3af42008-01-30 13:31:09 +0100122 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200123 * This offset is further used to index various arrays holding
124 * information for BTS and PEBS at the respective index.
Markus Metzgereee3af42008-01-30 13:31:09 +0100125 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200126 * On later 32bit processors, we only access the lower 32bit of the
127 * 64bit pointer fields. The upper halves will be zeroed out.
Markus Metzgereee3af42008-01-30 13:31:09 +0100128 */
129
Markus Metzger93fa7632008-04-08 11:01:58 +0200130enum ds_field {
131 ds_buffer_base = 0,
132 ds_index,
133 ds_absolute_maximum,
134 ds_interrupt_threshold,
Markus Metzgereee3af42008-01-30 13:31:09 +0100135};
136
Markus Metzger93fa7632008-04-08 11:01:58 +0200137enum ds_qualifier {
138 ds_bts = 0,
139 ds_pebs
Markus Metzgereee3af42008-01-30 13:31:09 +0100140};
141
Markus Metzger93fa7632008-04-08 11:01:58 +0200142static inline unsigned long ds_get(const unsigned char *base,
143 enum ds_qualifier qual, enum ds_field field)
144{
145 base += (ds_cfg.sizeof_field * (field + (4 * qual)));
146 return *(unsigned long *)base;
147}
148
149static inline void ds_set(unsigned char *base, enum ds_qualifier qual,
150 enum ds_field field, unsigned long value)
151{
152 base += (ds_cfg.sizeof_field * (field + (4 * qual)));
153 (*(unsigned long *)base) = value;
154}
155
Markus Metzgereee3af42008-01-30 13:31:09 +0100156
157/*
Markus Metzger6abb11a2008-11-25 09:05:27 +0100158 * Locking is done only for allocating BTS or PEBS resources.
Markus Metzgereee3af42008-01-30 13:31:09 +0100159 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100160static DEFINE_SPINLOCK(ds_lock);
Markus Metzgereee3af42008-01-30 13:31:09 +0100161
Markus Metzger93fa7632008-04-08 11:01:58 +0200162
163/*
164 * We either support (system-wide) per-cpu or per-thread allocation.
165 * We distinguish the two based on the task_struct pointer, where a
166 * NULL pointer indicates per-cpu allocation for the current cpu.
167 *
168 * Allocations are use-counted. As soon as resources are allocated,
169 * further allocations must be of the same type (per-cpu or
170 * per-thread). We model this by counting allocations (i.e. the number
171 * of tracers of a certain type) for one type negatively:
172 * =0 no tracers
173 * >0 number of per-thread tracers
174 * <0 number of per-cpu tracers
175 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200176 * Tracers essentially gives the number of ds contexts for a certain
177 * type of allocation.
178 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100179static atomic_t tracers = ATOMIC_INIT(0);
Markus Metzger93fa7632008-04-08 11:01:58 +0200180
181static inline void get_tracer(struct task_struct *task)
Markus Metzgera95d67f2008-01-30 13:31:20 +0100182{
Markus Metzgerc2724772008-12-11 13:49:59 +0100183 if (task)
184 atomic_inc(&tracers);
185 else
186 atomic_dec(&tracers);
Markus Metzgereee3af42008-01-30 13:31:09 +0100187}
188
Markus Metzger93fa7632008-04-08 11:01:58 +0200189static inline void put_tracer(struct task_struct *task)
Markus Metzgereee3af42008-01-30 13:31:09 +0100190{
Markus Metzgerc2724772008-12-11 13:49:59 +0100191 if (task)
192 atomic_dec(&tracers);
193 else
194 atomic_inc(&tracers);
Markus Metzgereee3af42008-01-30 13:31:09 +0100195}
196
Markus Metzger93fa7632008-04-08 11:01:58 +0200197static inline int check_tracer(struct task_struct *task)
Markus Metzgera95d67f2008-01-30 13:31:20 +0100198{
Markus Metzgerc2724772008-12-11 13:49:59 +0100199 return task ?
200 (atomic_read(&tracers) >= 0) :
201 (atomic_read(&tracers) <= 0);
Markus Metzger93fa7632008-04-08 11:01:58 +0200202}
203
204
205/*
206 * The DS context is either attached to a thread or to a cpu:
207 * - in the former case, the thread_struct contains a pointer to the
208 * attached context.
209 * - in the latter case, we use a static array of per-cpu context
210 * pointers.
211 *
212 * Contexts are use-counted. They are allocated on first access and
213 * deallocated when the last user puts the context.
Markus Metzger93fa7632008-04-08 11:01:58 +0200214 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100215struct ds_context {
216 /* pointer to the DS configuration; goes into MSR_IA32_DS_AREA */
217 unsigned char ds[MAX_SIZEOF_DS];
218 /* the owner of the BTS and PEBS configuration, respectively */
219 struct bts_tracer *bts_master;
220 struct pebs_tracer *pebs_master;
221 /* use count */
222 unsigned long count;
223 /* a pointer to the context location inside the thread_struct
224 * or the per_cpu context array */
225 struct ds_context **this;
226 /* a pointer to the task owning this context, or NULL, if the
227 * context is owned by a cpu */
228 struct task_struct *task;
229};
Markus Metzger93fa7632008-04-08 11:01:58 +0200230
Markus Metzgerc2724772008-12-11 13:49:59 +0100231static DEFINE_PER_CPU(struct ds_context *, system_context_array);
232
233#define system_context per_cpu(system_context_array, smp_processor_id())
Markus Metzger93fa7632008-04-08 11:01:58 +0200234
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100235
236static inline struct ds_context *ds_get_context(struct task_struct *task)
Markus Metzger93fa7632008-04-08 11:01:58 +0200237{
Markus Metzger93fa7632008-04-08 11:01:58 +0200238 struct ds_context **p_context =
Markus Metzgerc2724772008-12-11 13:49:59 +0100239 (task ? &task->thread.ds_ctx : &system_context);
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100240 struct ds_context *context = NULL;
241 struct ds_context *new_context = NULL;
Markus Metzgerde90add2008-11-25 08:52:56 +0100242 unsigned long irq;
Markus Metzger93fa7632008-04-08 11:01:58 +0200243
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100244 /* Chances are small that we already have a context. */
245 new_context = kzalloc(sizeof(*new_context), GFP_KERNEL);
246 if (!new_context)
247 return NULL;
248
249 spin_lock_irqsave(&ds_lock, irq);
250
251 context = *p_context;
Markus Metzger93fa7632008-04-08 11:01:58 +0200252 if (!context) {
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100253 context = new_context;
Markus Metzger93fa7632008-04-08 11:01:58 +0200254
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100255 context->this = p_context;
256 context->task = task;
257 context->count = 0;
Markus Metzgerde90add2008-11-25 08:52:56 +0100258
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100259 if (task)
260 set_tsk_thread_flag(task, TIF_DS_AREA_MSR);
Markus Metzgerde90add2008-11-25 08:52:56 +0100261
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100262 if (!task || (task == current))
263 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)context->ds);
Markus Metzgerde90add2008-11-25 08:52:56 +0100264
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100265 *p_context = context;
Markus Metzgerc2724772008-12-11 13:49:59 +0100266 }
Markus Metzger93fa7632008-04-08 11:01:58 +0200267
Markus Metzgercc1dc6d2008-12-16 15:51:03 +0100268 context->count++;
269
270 spin_unlock_irqrestore(&ds_lock, irq);
271
272 if (context != new_context)
273 kfree(new_context);
274
Markus Metzger93fa7632008-04-08 11:01:58 +0200275 return context;
276}
277
Markus Metzger93fa7632008-04-08 11:01:58 +0200278static inline void ds_put_context(struct ds_context *context)
279{
Markus Metzgerde90add2008-11-25 08:52:56 +0100280 unsigned long irq;
281
Markus Metzger93fa7632008-04-08 11:01:58 +0200282 if (!context)
283 return;
284
Markus Metzgerde90add2008-11-25 08:52:56 +0100285 spin_lock_irqsave(&ds_lock, irq);
Markus Metzger93fa7632008-04-08 11:01:58 +0200286
Markus Metzgerc2724772008-12-11 13:49:59 +0100287 if (--context->count) {
288 spin_unlock_irqrestore(&ds_lock, irq);
289 return;
290 }
Markus Metzger93fa7632008-04-08 11:01:58 +0200291
Cyrill Gorcunov573da422008-04-28 23:15:04 +0400292 *(context->this) = NULL;
Markus Metzger93fa7632008-04-08 11:01:58 +0200293
294 if (context->task)
295 clear_tsk_thread_flag(context->task, TIF_DS_AREA_MSR);
296
297 if (!context->task || (context->task == current))
298 wrmsrl(MSR_IA32_DS_AREA, 0);
299
Markus Metzgerde90add2008-11-25 08:52:56 +0100300 spin_unlock_irqrestore(&ds_lock, irq);
Markus Metzgerc2724772008-12-11 13:49:59 +0100301
302 kfree(context);
Markus Metzger93fa7632008-04-08 11:01:58 +0200303}
304
305
306/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100307 * Call the tracer's callback on a buffer overflow.
Markus Metzger93fa7632008-04-08 11:01:58 +0200308 *
Markus Metzger93fa7632008-04-08 11:01:58 +0200309 * context: the ds context
310 * qual: the buffer type
311 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100312static void ds_overflow(struct ds_context *context, enum ds_qualifier qual)
Markus Metzger93fa7632008-04-08 11:01:58 +0200313{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100314 switch (qual) {
Markus Metzgerc2724772008-12-11 13:49:59 +0100315 case ds_bts:
316 if (context->bts_master &&
317 context->bts_master->ovfl)
318 context->bts_master->ovfl(context->bts_master);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100319 break;
Markus Metzgerc2724772008-12-11 13:49:59 +0100320 case ds_pebs:
321 if (context->pebs_master &&
322 context->pebs_master->ovfl)
323 context->pebs_master->ovfl(context->pebs_master);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100324 break;
325 }
Markus Metzger93fa7632008-04-08 11:01:58 +0200326}
327
328
Markus Metzgerc2724772008-12-11 13:49:59 +0100329/*
330 * Write raw data into the BTS or PEBS buffer.
331 *
332 * The remainder of any partially written record is zeroed out.
333 *
334 * context: the DS context
335 * qual: the buffer type
336 * record: the data to write
337 * size: the size of the data
338 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100339static int ds_write(struct ds_context *context, enum ds_qualifier qual,
340 const void *record, size_t size)
Markus Metzger93fa7632008-04-08 11:01:58 +0200341{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100342 int bytes_written = 0;
Markus Metzger93fa7632008-04-08 11:01:58 +0200343
344 if (!record)
345 return -EINVAL;
346
Markus Metzger93fa7632008-04-08 11:01:58 +0200347 while (size) {
348 unsigned long base, index, end, write_end, int_th;
349 unsigned long write_size, adj_write_size;
Markus Metzgereee3af42008-01-30 13:31:09 +0100350
Markus Metzger93fa7632008-04-08 11:01:58 +0200351 /*
352 * write as much as possible without producing an
353 * overflow interrupt.
354 *
355 * interrupt_threshold must either be
356 * - bigger than absolute_maximum or
357 * - point to a record between buffer_base and absolute_maximum
358 *
359 * index points to a valid record.
360 */
361 base = ds_get(context->ds, qual, ds_buffer_base);
362 index = ds_get(context->ds, qual, ds_index);
363 end = ds_get(context->ds, qual, ds_absolute_maximum);
364 int_th = ds_get(context->ds, qual, ds_interrupt_threshold);
365
366 write_end = min(end, int_th);
367
368 /* if we are already beyond the interrupt threshold,
369 * we fill the entire buffer */
370 if (write_end <= index)
371 write_end = end;
372
373 if (write_end <= index)
Markus Metzgerca0002a2008-11-25 09:01:25 +0100374 break;
Markus Metzger93fa7632008-04-08 11:01:58 +0200375
376 write_size = min((unsigned long) size, write_end - index);
377 memcpy((void *)index, record, write_size);
378
379 record = (const char *)record + write_size;
Markus Metzgerca0002a2008-11-25 09:01:25 +0100380 size -= write_size;
381 bytes_written += write_size;
Markus Metzger93fa7632008-04-08 11:01:58 +0200382
383 adj_write_size = write_size / ds_cfg.sizeof_rec[qual];
384 adj_write_size *= ds_cfg.sizeof_rec[qual];
385
386 /* zero out trailing bytes */
387 memset((char *)index + write_size, 0,
388 adj_write_size - write_size);
389 index += adj_write_size;
390
391 if (index >= end)
392 index = base;
393 ds_set(context->ds, qual, ds_index, index);
394
395 if (index >= int_th)
Markus Metzgerca0002a2008-11-25 09:01:25 +0100396 ds_overflow(context, qual);
Markus Metzger93fa7632008-04-08 11:01:58 +0200397 }
398
Markus Metzgerca0002a2008-11-25 09:01:25 +0100399 return bytes_written;
Markus Metzgereee3af42008-01-30 13:31:09 +0100400}
401
Markus Metzgerc2724772008-12-11 13:49:59 +0100402
403/*
404 * Branch Trace Store (BTS) uses the following format. Different
405 * architectures vary in the size of those fields.
406 * - source linear address
407 * - destination linear address
408 * - flags
409 *
410 * Later architectures use 64bit pointers throughout, whereas earlier
411 * architectures use 32bit pointers in 32bit mode.
412 *
413 * We compute the base address for the first 8 fields based on:
414 * - the field size stored in the DS configuration
415 * - the relative field position
416 *
417 * In order to store additional information in the BTS buffer, we use
418 * a special source address to indicate that the record requires
419 * special interpretation.
420 *
421 * Netburst indicated via a bit in the flags field whether the branch
422 * was predicted; this is ignored.
423 *
424 * We use two levels of abstraction:
425 * - the raw data level defined here
426 * - an arch-independent level defined in ds.h
427 */
428
429enum bts_field {
430 bts_from,
431 bts_to,
432 bts_flags,
433
434 bts_qual = bts_from,
435 bts_jiffies = bts_to,
436 bts_pid = bts_flags,
437
438 bts_qual_mask = (bts_qual_max - 1),
439 bts_escape = ((unsigned long)-1 & ~bts_qual_mask)
440};
441
442static inline unsigned long bts_get(const char *base, enum bts_field field)
443{
444 base += (ds_cfg.sizeof_field * field);
445 return *(unsigned long *)base;
446}
447
448static inline void bts_set(char *base, enum bts_field field, unsigned long val)
449{
450 base += (ds_cfg.sizeof_field * field);;
451 (*(unsigned long *)base) = val;
452}
453
454
455/*
456 * The raw BTS data is architecture dependent.
457 *
458 * For higher-level users, we give an arch-independent view.
459 * - ds.h defines struct bts_struct
460 * - bts_read translates one raw bts record into a bts_struct
461 * - bts_write translates one bts_struct into the raw format and
462 * writes it into the top of the parameter tracer's buffer.
463 *
464 * return: bytes read/written on success; -Eerrno, otherwise
465 */
466static int bts_read(struct bts_tracer *tracer, const void *at,
467 struct bts_struct *out)
Markus Metzgereee3af42008-01-30 13:31:09 +0100468{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100469 if (!tracer)
470 return -EINVAL;
471
Markus Metzgerc2724772008-12-11 13:49:59 +0100472 if (at < tracer->trace.ds.begin)
473 return -EINVAL;
474
475 if (tracer->trace.ds.end < (at + tracer->trace.ds.size))
476 return -EINVAL;
477
478 memset(out, 0, sizeof(*out));
479 if ((bts_get(at, bts_qual) & ~bts_qual_mask) == bts_escape) {
480 out->qualifier = (bts_get(at, bts_qual) & bts_qual_mask);
481 out->variant.timestamp.jiffies = bts_get(at, bts_jiffies);
482 out->variant.timestamp.pid = bts_get(at, bts_pid);
483 } else {
484 out->qualifier = bts_branch;
485 out->variant.lbr.from = bts_get(at, bts_from);
486 out->variant.lbr.to = bts_get(at, bts_to);
487 }
488
489 return ds_cfg.sizeof_rec[ds_bts];
Markus Metzgereee3af42008-01-30 13:31:09 +0100490}
491
Markus Metzgerc2724772008-12-11 13:49:59 +0100492static int bts_write(struct bts_tracer *tracer, const struct bts_struct *in)
Markus Metzger93fa7632008-04-08 11:01:58 +0200493{
Markus Metzgerc2724772008-12-11 13:49:59 +0100494 unsigned char raw[MAX_SIZEOF_BTS];
495
Markus Metzgerca0002a2008-11-25 09:01:25 +0100496 if (!tracer)
497 return -EINVAL;
498
Markus Metzgerc2724772008-12-11 13:49:59 +0100499 if (MAX_SIZEOF_BTS < ds_cfg.sizeof_rec[ds_bts])
500 return -EOVERFLOW;
501
502 switch (in->qualifier) {
503 case bts_invalid:
504 bts_set(raw, bts_from, 0);
505 bts_set(raw, bts_to, 0);
506 bts_set(raw, bts_flags, 0);
507 break;
508 case bts_branch:
509 bts_set(raw, bts_from, in->variant.lbr.from);
510 bts_set(raw, bts_to, in->variant.lbr.to);
511 bts_set(raw, bts_flags, 0);
512 break;
513 case bts_task_arrives:
514 case bts_task_departs:
515 bts_set(raw, bts_qual, (bts_escape | in->qualifier));
516 bts_set(raw, bts_jiffies, in->variant.timestamp.jiffies);
517 bts_set(raw, bts_pid, in->variant.timestamp.pid);
518 break;
519 default:
520 return -EINVAL;
521 }
522
523 return ds_write(tracer->ds.context, ds_bts, raw,
524 ds_cfg.sizeof_rec[ds_bts]);
Markus Metzger93fa7632008-04-08 11:01:58 +0200525}
Markus Metzgereee3af42008-01-30 13:31:09 +0100526
Markus Metzgerc2724772008-12-11 13:49:59 +0100527
528static void ds_write_config(struct ds_context *context,
529 struct ds_trace *cfg, enum ds_qualifier qual)
Markus Metzger93fa7632008-04-08 11:01:58 +0200530{
Markus Metzgerc2724772008-12-11 13:49:59 +0100531 unsigned char *ds = context->ds;
Markus Metzger93fa7632008-04-08 11:01:58 +0200532
Markus Metzgerc2724772008-12-11 13:49:59 +0100533 ds_set(ds, qual, ds_buffer_base, (unsigned long)cfg->begin);
534 ds_set(ds, qual, ds_index, (unsigned long)cfg->top);
535 ds_set(ds, qual, ds_absolute_maximum, (unsigned long)cfg->end);
536 ds_set(ds, qual, ds_interrupt_threshold, (unsigned long)cfg->ith);
537}
Markus Metzger93fa7632008-04-08 11:01:58 +0200538
Markus Metzgerc2724772008-12-11 13:49:59 +0100539static void ds_read_config(struct ds_context *context,
540 struct ds_trace *cfg, enum ds_qualifier qual)
541{
542 unsigned char *ds = context->ds;
Markus Metzger93fa7632008-04-08 11:01:58 +0200543
Markus Metzgerc2724772008-12-11 13:49:59 +0100544 cfg->begin = (void *)ds_get(ds, qual, ds_buffer_base);
545 cfg->top = (void *)ds_get(ds, qual, ds_index);
546 cfg->end = (void *)ds_get(ds, qual, ds_absolute_maximum);
547 cfg->ith = (void *)ds_get(ds, qual, ds_interrupt_threshold);
548}
549
550static void ds_init_ds_trace(struct ds_trace *trace, enum ds_qualifier qual,
551 void *base, size_t size, size_t ith,
552 unsigned int flags) {
553 unsigned long buffer, adj;
554
555 /* adjust the buffer address and size to meet alignment
556 * constraints:
557 * - buffer is double-word aligned
558 * - size is multiple of record size
559 *
560 * We checked the size at the very beginning; we have enough
561 * space to do the adjustment.
562 */
563 buffer = (unsigned long)base;
564
565 adj = ALIGN(buffer, DS_ALIGNMENT) - buffer;
566 buffer += adj;
567 size -= adj;
568
569 trace->n = size / ds_cfg.sizeof_rec[qual];
570 trace->size = ds_cfg.sizeof_rec[qual];
571
572 size = (trace->n * trace->size);
573
574 trace->begin = (void *)buffer;
575 trace->top = trace->begin;
576 trace->end = (void *)(buffer + size);
577 /* The value for 'no threshold' is -1, which will set the
578 * threshold outside of the buffer, just like we want it.
579 */
580 trace->ith = (void *)(buffer + size - ith);
581
582 trace->flags = flags;
583}
584
585
586static int ds_request(struct ds_tracer *tracer, struct ds_trace *trace,
587 enum ds_qualifier qual, struct task_struct *task,
588 void *base, size_t size, size_t th, unsigned int flags)
589{
590 struct ds_context *context;
591 int error;
592
593 error = -EINVAL;
594 if (!base)
595 goto out;
596
597 /* we require some space to do alignment adjustments below */
598 error = -EINVAL;
599 if (size < (DS_ALIGNMENT + ds_cfg.sizeof_rec[qual]))
600 goto out;
601
602 if (th != (size_t)-1) {
603 th *= ds_cfg.sizeof_rec[qual];
604
605 error = -EINVAL;
606 if (size <= th)
607 goto out;
608 }
609
610 tracer->buffer = base;
611 tracer->size = size;
612
613 error = -ENOMEM;
614 context = ds_get_context(task);
615 if (!context)
616 goto out;
617 tracer->context = context;
618
619 ds_init_ds_trace(trace, qual, base, size, th, flags);
620
621 error = 0;
622 out:
623 return error;
624}
625
626struct bts_tracer *ds_request_bts(struct task_struct *task,
627 void *base, size_t size,
628 bts_ovfl_callback_t ovfl, size_t th,
629 unsigned int flags)
630{
631 struct bts_tracer *tracer;
632 unsigned long irq;
633 int error;
634
635 error = -EOPNOTSUPP;
636 if (!ds_cfg.ctl[dsf_bts])
637 goto out;
638
639 /* buffer overflow notification is not yet implemented */
640 error = -EOPNOTSUPP;
641 if (ovfl)
642 goto out;
643
644 error = -ENOMEM;
645 tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
646 if (!tracer)
647 goto out;
648 tracer->ovfl = ovfl;
649
650 error = ds_request(&tracer->ds, &tracer->trace.ds,
651 ds_bts, task, base, size, th, flags);
652 if (error < 0)
653 goto out_tracer;
654
655
656 spin_lock_irqsave(&ds_lock, irq);
657
658 error = -EPERM;
659 if (!check_tracer(task))
660 goto out_unlock;
661 get_tracer(task);
662
663 error = -EPERM;
664 if (tracer->ds.context->bts_master)
665 goto out_put_tracer;
666 tracer->ds.context->bts_master = tracer;
667
668 spin_unlock_irqrestore(&ds_lock, irq);
669
670
671 tracer->trace.read = bts_read;
672 tracer->trace.write = bts_write;
673
674 ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_bts);
675 ds_resume_bts(tracer);
676
677 return tracer;
678
679 out_put_tracer:
680 put_tracer(task);
681 out_unlock:
682 spin_unlock_irqrestore(&ds_lock, irq);
683 ds_put_context(tracer->ds.context);
684 out_tracer:
685 kfree(tracer);
686 out:
687 return ERR_PTR(error);
688}
689
690struct pebs_tracer *ds_request_pebs(struct task_struct *task,
691 void *base, size_t size,
692 pebs_ovfl_callback_t ovfl, size_t th,
693 unsigned int flags)
694{
695 struct pebs_tracer *tracer;
696 unsigned long irq;
697 int error;
698
699 /* buffer overflow notification is not yet implemented */
700 error = -EOPNOTSUPP;
701 if (ovfl)
702 goto out;
703
704 error = -ENOMEM;
705 tracer = kzalloc(sizeof(*tracer), GFP_KERNEL);
706 if (!tracer)
707 goto out;
708 tracer->ovfl = ovfl;
709
710 error = ds_request(&tracer->ds, &tracer->trace.ds,
711 ds_pebs, task, base, size, th, flags);
712 if (error < 0)
713 goto out_tracer;
714
715 spin_lock_irqsave(&ds_lock, irq);
716
717 error = -EPERM;
718 if (!check_tracer(task))
719 goto out_unlock;
720 get_tracer(task);
721
722 error = -EPERM;
723 if (tracer->ds.context->pebs_master)
724 goto out_put_tracer;
725 tracer->ds.context->pebs_master = tracer;
726
727 spin_unlock_irqrestore(&ds_lock, irq);
728
729 ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_bts);
730 ds_resume_pebs(tracer);
731
732 return tracer;
733
734 out_put_tracer:
735 put_tracer(task);
736 out_unlock:
737 spin_unlock_irqrestore(&ds_lock, irq);
738 ds_put_context(tracer->ds.context);
739 out_tracer:
740 kfree(tracer);
741 out:
742 return ERR_PTR(error);
743}
744
745void ds_release_bts(struct bts_tracer *tracer)
746{
747 if (!tracer)
748 return;
749
750 ds_suspend_bts(tracer);
751
752 WARN_ON_ONCE(tracer->ds.context->bts_master != tracer);
753 tracer->ds.context->bts_master = NULL;
754
755 put_tracer(tracer->ds.context->task);
756 ds_put_context(tracer->ds.context);
757
758 kfree(tracer);
759}
760
761void ds_suspend_bts(struct bts_tracer *tracer)
762{
763 struct task_struct *task;
764
765 if (!tracer)
766 return;
767
768 task = tracer->ds.context->task;
769
770 if (!task || (task == current))
771 update_debugctlmsr(get_debugctlmsr() & ~BTS_CONTROL);
772
773 if (task) {
774 task->thread.debugctlmsr &= ~BTS_CONTROL;
775
776 if (!task->thread.debugctlmsr)
777 clear_tsk_thread_flag(task, TIF_DEBUGCTLMSR);
778 }
779}
780
781void ds_resume_bts(struct bts_tracer *tracer)
782{
783 struct task_struct *task;
784 unsigned long control;
785
786 if (!tracer)
787 return;
788
789 task = tracer->ds.context->task;
790
791 control = ds_cfg.ctl[dsf_bts];
792 if (!(tracer->trace.ds.flags & BTS_KERNEL))
793 control |= ds_cfg.ctl[dsf_bts_kernel];
794 if (!(tracer->trace.ds.flags & BTS_USER))
795 control |= ds_cfg.ctl[dsf_bts_user];
796
797 if (task) {
798 task->thread.debugctlmsr |= control;
799 set_tsk_thread_flag(task, TIF_DEBUGCTLMSR);
800 }
801
802 if (!task || (task == current))
803 update_debugctlmsr(get_debugctlmsr() | control);
804}
805
806void ds_release_pebs(struct pebs_tracer *tracer)
807{
808 if (!tracer)
809 return;
810
811 ds_suspend_pebs(tracer);
812
813 WARN_ON_ONCE(tracer->ds.context->pebs_master != tracer);
814 tracer->ds.context->pebs_master = NULL;
815
816 put_tracer(tracer->ds.context->task);
817 ds_put_context(tracer->ds.context);
818
819 kfree(tracer);
820}
821
822void ds_suspend_pebs(struct pebs_tracer *tracer)
823{
824
825}
826
827void ds_resume_pebs(struct pebs_tracer *tracer)
828{
829
830}
831
832const struct bts_trace *ds_read_bts(struct bts_tracer *tracer)
833{
834 if (!tracer)
835 return NULL;
836
837 ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_bts);
838 return &tracer->trace;
839}
840
841const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer)
842{
843 if (!tracer)
844 return NULL;
845
846 ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_pebs);
847 tracer->trace.reset_value =
848 *(u64 *)(tracer->ds.context->ds + (ds_cfg.sizeof_field * 8));
849
850 return &tracer->trace;
Markus Metzger93fa7632008-04-08 11:01:58 +0200851}
852
Markus Metzgerca0002a2008-11-25 09:01:25 +0100853int ds_reset_bts(struct bts_tracer *tracer)
Markus Metzger93fa7632008-04-08 11:01:58 +0200854{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100855 if (!tracer)
856 return -EINVAL;
857
Markus Metzgerc2724772008-12-11 13:49:59 +0100858 tracer->trace.ds.top = tracer->trace.ds.begin;
859
860 ds_set(tracer->ds.context->ds, ds_bts, ds_index,
861 (unsigned long)tracer->trace.ds.top);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100862
863 return 0;
Markus Metzger93fa7632008-04-08 11:01:58 +0200864}
865
Markus Metzgerca0002a2008-11-25 09:01:25 +0100866int ds_reset_pebs(struct pebs_tracer *tracer)
Markus Metzger93fa7632008-04-08 11:01:58 +0200867{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100868 if (!tracer)
869 return -EINVAL;
870
Markus Metzgerc2724772008-12-11 13:49:59 +0100871 tracer->trace.ds.top = tracer->trace.ds.begin;
Markus Metzgerca0002a2008-11-25 09:01:25 +0100872
Markus Metzgerc2724772008-12-11 13:49:59 +0100873 ds_set(tracer->ds.context->ds, ds_bts, ds_index,
874 (unsigned long)tracer->trace.ds.top);
Markus Metzger93fa7632008-04-08 11:01:58 +0200875
Markus Metzgerca0002a2008-11-25 09:01:25 +0100876 return 0;
Markus Metzger93fa7632008-04-08 11:01:58 +0200877}
878
Markus Metzgerca0002a2008-11-25 09:01:25 +0100879int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value)
Markus Metzger93fa7632008-04-08 11:01:58 +0200880{
Markus Metzgerca0002a2008-11-25 09:01:25 +0100881 if (!tracer)
882 return -EINVAL;
Markus Metzger93fa7632008-04-08 11:01:58 +0200883
Markus Metzgerca0002a2008-11-25 09:01:25 +0100884 *(u64 *)(tracer->ds.context->ds + (ds_cfg.sizeof_field * 8)) = value;
Markus Metzger93fa7632008-04-08 11:01:58 +0200885
Markus Metzgerca0002a2008-11-25 09:01:25 +0100886 return 0;
Markus Metzger93fa7632008-04-08 11:01:58 +0200887}
888
Markus Metzgerc2724772008-12-11 13:49:59 +0100889static const struct ds_configuration ds_cfg_netburst = {
890 .name = "netburst",
891 .ctl[dsf_bts] = (1 << 2) | (1 << 3),
892 .ctl[dsf_bts_kernel] = (1 << 5),
893 .ctl[dsf_bts_user] = (1 << 6),
894
895 .sizeof_field = sizeof(long),
896 .sizeof_rec[ds_bts] = sizeof(long) * 3,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100897#ifdef __i386__
Markus Metzgerc2724772008-12-11 13:49:59 +0100898 .sizeof_rec[ds_pebs] = sizeof(long) * 10,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100899#else
Markus Metzgerc2724772008-12-11 13:49:59 +0100900 .sizeof_rec[ds_pebs] = sizeof(long) * 18,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100901#endif
Markus Metzger93fa7632008-04-08 11:01:58 +0200902};
Markus Metzgerc2724772008-12-11 13:49:59 +0100903static const struct ds_configuration ds_cfg_pentium_m = {
904 .name = "pentium m",
905 .ctl[dsf_bts] = (1 << 6) | (1 << 7),
906
907 .sizeof_field = sizeof(long),
908 .sizeof_rec[ds_bts] = sizeof(long) * 3,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100909#ifdef __i386__
Markus Metzgerc2724772008-12-11 13:49:59 +0100910 .sizeof_rec[ds_pebs] = sizeof(long) * 10,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100911#else
Markus Metzgerc2724772008-12-11 13:49:59 +0100912 .sizeof_rec[ds_pebs] = sizeof(long) * 18,
Markus Metzgerc4858ff2008-11-25 08:49:06 +0100913#endif
Markus Metzgereee3af42008-01-30 13:31:09 +0100914};
Markus Metzgerc2724772008-12-11 13:49:59 +0100915static const struct ds_configuration ds_cfg_core2 = {
916 .name = "core 2",
917 .ctl[dsf_bts] = (1 << 6) | (1 << 7),
918 .ctl[dsf_bts_kernel] = (1 << 9),
919 .ctl[dsf_bts_user] = (1 << 10),
920
921 .sizeof_field = 8,
922 .sizeof_rec[ds_bts] = 8 * 3,
923 .sizeof_rec[ds_pebs] = 8 * 18,
924};
Markus Metzgereee3af42008-01-30 13:31:09 +0100925
Markus Metzgerc2724772008-12-11 13:49:59 +0100926static void
Markus Metzgereee3af42008-01-30 13:31:09 +0100927ds_configure(const struct ds_configuration *cfg)
928{
Markus Metzgerc2724772008-12-11 13:49:59 +0100929 memset(&ds_cfg, 0, sizeof(ds_cfg));
Markus Metzgereee3af42008-01-30 13:31:09 +0100930 ds_cfg = *cfg;
Markus Metzgerca0002a2008-11-25 09:01:25 +0100931
Markus Metzgerc2724772008-12-11 13:49:59 +0100932 printk(KERN_INFO "[ds] using %s configuration\n", ds_cfg.name);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100933
Markus Metzgerc2724772008-12-11 13:49:59 +0100934 if (!cpu_has_bts) {
935 ds_cfg.ctl[dsf_bts] = 0;
936 printk(KERN_INFO "[ds] bts not available\n");
937 }
938 if (!cpu_has_pebs)
939 printk(KERN_INFO "[ds] pebs not available\n");
940
941 WARN_ON_ONCE(MAX_SIZEOF_DS < (12 * ds_cfg.sizeof_field));
Markus Metzgereee3af42008-01-30 13:31:09 +0100942}
943
944void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
945{
946 switch (c->x86) {
947 case 0x6:
948 switch (c->x86_model) {
Markus Metzgerf4166c52008-11-09 14:29:21 +0100949 case 0 ... 0xC:
950 /* sorry, don't know about them */
951 break;
Markus Metzgereee3af42008-01-30 13:31:09 +0100952 case 0xD:
953 case 0xE: /* Pentium M */
Markus Metzgerc2724772008-12-11 13:49:59 +0100954 ds_configure(&ds_cfg_pentium_m);
Markus Metzgereee3af42008-01-30 13:31:09 +0100955 break;
Markus Metzgerf4166c52008-11-09 14:29:21 +0100956 default: /* Core2, Atom, ... */
Markus Metzgerc2724772008-12-11 13:49:59 +0100957 ds_configure(&ds_cfg_core2);
Markus Metzgereee3af42008-01-30 13:31:09 +0100958 break;
Markus Metzgereee3af42008-01-30 13:31:09 +0100959 }
960 break;
961 case 0xF:
962 switch (c->x86_model) {
Markus Metzgereee3af42008-01-30 13:31:09 +0100963 case 0x0:
964 case 0x1:
965 case 0x2: /* Netburst */
Markus Metzgerc2724772008-12-11 13:49:59 +0100966 ds_configure(&ds_cfg_netburst);
Markus Metzgereee3af42008-01-30 13:31:09 +0100967 break;
Markus Metzgereee3af42008-01-30 13:31:09 +0100968 default:
969 /* sorry, don't know about them */
970 break;
971 }
972 break;
973 default:
974 /* sorry, don't know about them */
975 break;
976 }
977}
Markus Metzger93fa7632008-04-08 11:01:58 +0200978
Markus Metzgerc2724772008-12-11 13:49:59 +0100979/*
980 * Change the DS configuration from tracing prev to tracing next.
981 */
982void ds_switch_to(struct task_struct *prev, struct task_struct *next)
Markus Metzger93fa7632008-04-08 11:01:58 +0200983{
Markus Metzgerc2724772008-12-11 13:49:59 +0100984 struct ds_context *prev_ctx = prev->thread.ds_ctx;
985 struct ds_context *next_ctx = next->thread.ds_ctx;
986
987 if (prev_ctx) {
988 update_debugctlmsr(0);
989
990 if (prev_ctx->bts_master &&
991 (prev_ctx->bts_master->trace.ds.flags & BTS_TIMESTAMPS)) {
992 struct bts_struct ts = {
993 .qualifier = bts_task_departs,
994 .variant.timestamp.jiffies = jiffies_64,
995 .variant.timestamp.pid = prev->pid
996 };
997 bts_write(prev_ctx->bts_master, &ts);
998 }
Markus Metzgerca0002a2008-11-25 09:01:25 +0100999 }
Markus Metzgerc2724772008-12-11 13:49:59 +01001000
1001 if (next_ctx) {
1002 if (next_ctx->bts_master &&
1003 (next_ctx->bts_master->trace.ds.flags & BTS_TIMESTAMPS)) {
1004 struct bts_struct ts = {
1005 .qualifier = bts_task_arrives,
1006 .variant.timestamp.jiffies = jiffies_64,
1007 .variant.timestamp.pid = next->pid
1008 };
1009 bts_write(next_ctx->bts_master, &ts);
1010 }
1011
1012 wrmsrl(MSR_IA32_DS_AREA, (unsigned long)next_ctx->ds);
1013 }
1014
1015 update_debugctlmsr(next->thread.debugctlmsr);
Markus Metzger93fa7632008-04-08 11:01:58 +02001016}