blob: e7855def97c3e62d6a37c504d100c08ddf7005be [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
5 * feature that is used for last branch recording (LBR) and
6 * precise-event based sampling (PEBS).
7 *
8 * Different architectures use a different DS layout/pointer size.
9 * The below functions therefore work on a void*.
10 *
11 *
12 * Since there is no user for PEBS, yet, only LBR (or branch
13 * trace store, BTS) is supported.
14 *
15 *
16 * Copyright (C) 2007 Intel Corporation.
17 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
18 */
19
20#include <asm/ds.h>
21
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/slab.h>
25
26
27/*
28 * Debug Store (DS) save area configuration (see Intel64 and IA32
29 * Architectures Software Developer's Manual, section 18.5)
30 *
31 * The DS configuration consists of the following fields; different
32 * architetures vary in the size of those fields.
33 * - double-word aligned base linear address of the BTS buffer
34 * - write pointer into the BTS buffer
35 * - end linear address of the BTS buffer (one byte beyond the end of
36 * the buffer)
37 * - interrupt pointer into BTS buffer
38 * (interrupt occurs when write pointer passes interrupt pointer)
39 * - double-word aligned base linear address of the PEBS buffer
40 * - write pointer into the PEBS buffer
41 * - end linear address of the PEBS buffer (one byte beyond the end of
42 * the buffer)
43 * - interrupt pointer into PEBS buffer
44 * (interrupt occurs when write pointer passes interrupt pointer)
45 * - value to which counter is reset following counter overflow
46 *
47 * On later architectures, the last branch recording hardware uses
48 * 64bit pointers even in 32bit mode.
49 *
50 *
51 * Branch Trace Store (BTS) records store information about control
52 * flow changes. They at least provide the following information:
53 * - source linear address
54 * - destination linear address
55 *
56 * Netburst supported a predicated bit that had been dropped in later
57 * architectures. We do not suppor it.
58 *
59 *
60 * In order to abstract from the actual DS and BTS layout, we describe
61 * the access to the relevant fields.
62 * Thanks to Andi Kleen for proposing this design.
63 *
64 * The implementation, however, is not as general as it might seem. In
65 * order to stay somewhat simple and efficient, we assume an
66 * underlying unsigned type (mostly a pointer type) and we expect the
67 * field to be at least as big as that type.
68 */
69
70/*
71 * A special from_ip address to indicate that the BTS record is an
72 * info record that needs to be interpreted or skipped.
73 */
74#define BTS_ESCAPE_ADDRESS (-1)
75
76/*
77 * A field access descriptor
78 */
79struct access_desc {
80 unsigned char offset;
81 unsigned char size;
82};
83
84/*
85 * The configuration for a particular DS/BTS hardware implementation.
86 */
87struct ds_configuration {
88 /* the DS configuration */
89 unsigned char sizeof_ds;
90 struct access_desc bts_buffer_base;
91 struct access_desc bts_index;
92 struct access_desc bts_absolute_maximum;
93 struct access_desc bts_interrupt_threshold;
94 /* the BTS configuration */
95 unsigned char sizeof_bts;
96 struct access_desc from_ip;
97 struct access_desc to_ip;
98 /* BTS variants used to store additional information like
99 timestamps */
100 struct access_desc info_type;
101 struct access_desc info_data;
102 unsigned long debugctl_mask;
103};
104
105/*
106 * The global configuration used by the below accessor functions
107 */
108static struct ds_configuration ds_cfg;
109
110/*
111 * Accessor functions for some DS and BTS fields using the above
112 * global ptrace_bts_cfg.
113 */
114static inline void *get_bts_buffer_base(char *base)
115{
116 return *(void **)(base + ds_cfg.bts_buffer_base.offset);
117}
118static inline void set_bts_buffer_base(char *base, void *value)
119{
120 (*(void **)(base + ds_cfg.bts_buffer_base.offset)) = value;
121}
122static inline void *get_bts_index(char *base)
123{
124 return *(void **)(base + ds_cfg.bts_index.offset);
125}
126static inline void set_bts_index(char *base, void *value)
127{
128 (*(void **)(base + ds_cfg.bts_index.offset)) = value;
129}
130static inline void *get_bts_absolute_maximum(char *base)
131{
132 return *(void **)(base + ds_cfg.bts_absolute_maximum.offset);
133}
134static inline void set_bts_absolute_maximum(char *base, void *value)
135{
136 (*(void **)(base + ds_cfg.bts_absolute_maximum.offset)) = value;
137}
138static inline void *get_bts_interrupt_threshold(char *base)
139{
140 return *(void **)(base + ds_cfg.bts_interrupt_threshold.offset);
141}
142static inline void set_bts_interrupt_threshold(char *base, void *value)
143{
144 (*(void **)(base + ds_cfg.bts_interrupt_threshold.offset)) = value;
145}
146static inline long get_from_ip(char *base)
147{
148 return *(long *)(base + ds_cfg.from_ip.offset);
149}
150static inline void set_from_ip(char *base, long value)
151{
152 (*(long *)(base + ds_cfg.from_ip.offset)) = value;
153}
154static inline long get_to_ip(char *base)
155{
156 return *(long *)(base + ds_cfg.to_ip.offset);
157}
158static inline void set_to_ip(char *base, long value)
159{
160 (*(long *)(base + ds_cfg.to_ip.offset)) = value;
161}
162static inline unsigned char get_info_type(char *base)
163{
164 return *(unsigned char *)(base + ds_cfg.info_type.offset);
165}
166static inline void set_info_type(char *base, unsigned char value)
167{
168 (*(unsigned char *)(base + ds_cfg.info_type.offset)) = value;
169}
Markus Metzger3c689042008-01-30 13:31:20 +0100170static inline unsigned long get_info_data(char *base)
Markus Metzgereee3af42008-01-30 13:31:09 +0100171{
Markus Metzger3c689042008-01-30 13:31:20 +0100172 return *(unsigned long *)(base + ds_cfg.info_data.offset);
Markus Metzgereee3af42008-01-30 13:31:09 +0100173}
Markus Metzger3c689042008-01-30 13:31:20 +0100174static inline void set_info_data(char *base, unsigned long value)
Markus Metzgereee3af42008-01-30 13:31:09 +0100175{
Markus Metzger3c689042008-01-30 13:31:20 +0100176 (*(unsigned long *)(base + ds_cfg.info_data.offset)) = value;
Markus Metzgereee3af42008-01-30 13:31:09 +0100177}
178
179
180int ds_allocate(void **dsp, size_t bts_size_in_records)
181{
182 size_t bts_size_in_bytes = 0;
183 void *bts = 0;
184 void *ds = 0;
185
186 if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
187 return -EOPNOTSUPP;
188
189 if (bts_size_in_records < 0)
190 return -EINVAL;
191
192 bts_size_in_bytes =
193 bts_size_in_records * ds_cfg.sizeof_bts;
194
195 if (bts_size_in_bytes <= 0)
196 return -EINVAL;
197
198 bts = kzalloc(bts_size_in_bytes, GFP_KERNEL);
199
200 if (!bts)
201 return -ENOMEM;
202
203 ds = kzalloc(ds_cfg.sizeof_ds, GFP_KERNEL);
204
205 if (!ds) {
206 kfree(bts);
207 return -ENOMEM;
208 }
209
210 set_bts_buffer_base(ds, bts);
211 set_bts_index(ds, bts);
212 set_bts_absolute_maximum(ds, bts + bts_size_in_bytes);
213 set_bts_interrupt_threshold(ds, bts + bts_size_in_bytes + 1);
214
215 *dsp = ds;
216 return 0;
217}
218
219int ds_free(void **dsp)
220{
221 if (*dsp)
222 kfree(get_bts_buffer_base(*dsp));
223 kfree(*dsp);
224 *dsp = 0;
225
226 return 0;
227}
228
229int ds_get_bts_size(void *ds)
230{
231 size_t size_in_bytes;
232
233 if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
234 return -EOPNOTSUPP;
235
236 size_in_bytes =
237 get_bts_absolute_maximum(ds) -
238 get_bts_buffer_base(ds);
239
240 return size_in_bytes / ds_cfg.sizeof_bts;
241}
242
243int ds_get_bts_index(void *ds)
244{
245 size_t index_offset_in_bytes;
246
247 if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
248 return -EOPNOTSUPP;
249
250 index_offset_in_bytes =
251 get_bts_index(ds) -
252 get_bts_buffer_base(ds);
253
254 return index_offset_in_bytes / ds_cfg.sizeof_bts;
255}
256
257int ds_read_bts(void *ds, size_t index, struct bts_struct *out)
258{
259 void *bts;
260
261 if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
262 return -EOPNOTSUPP;
263
264 if (index < 0)
265 return -EINVAL;
266
267 if (index >= ds_get_bts_size(ds))
268 return -EINVAL;
269
270 bts = get_bts_buffer_base(ds);
271 bts = (char *)bts + (index * ds_cfg.sizeof_bts);
272
273 memset(out, 0, sizeof(*out));
274 if (get_from_ip(bts) == BTS_ESCAPE_ADDRESS) {
Markus Metzger3c689042008-01-30 13:31:20 +0100275 out->qualifier = get_info_type(bts);
276 out->variant.jiffies = get_info_data(bts);
Markus Metzgereee3af42008-01-30 13:31:09 +0100277 } else {
278 out->qualifier = BTS_BRANCH;
279 out->variant.lbr.from_ip = get_from_ip(bts);
280 out->variant.lbr.to_ip = get_to_ip(bts);
281 }
282
283 return 0;
284}
285
286int ds_write_bts(void *ds, const struct bts_struct *in)
287{
288 void *bts;
289
290 if (!ds_cfg.sizeof_ds || !ds_cfg.sizeof_bts)
291 return -EOPNOTSUPP;
292
293 if (ds_get_bts_size(ds) <= 0)
294 return -ENXIO;
295
296 bts = get_bts_index(ds);
297
298 memset(bts, 0, ds_cfg.sizeof_bts);
299 switch (in->qualifier) {
300 case BTS_INVALID:
301 break;
302
303 case BTS_BRANCH:
304 set_from_ip(bts, in->variant.lbr.from_ip);
305 set_to_ip(bts, in->variant.lbr.to_ip);
306 break;
307
308 case BTS_TASK_ARRIVES:
309 case BTS_TASK_DEPARTS:
310 set_from_ip(bts, BTS_ESCAPE_ADDRESS);
311 set_info_type(bts, in->qualifier);
Markus Metzger3c689042008-01-30 13:31:20 +0100312 set_info_data(bts, in->variant.jiffies);
Markus Metzgereee3af42008-01-30 13:31:09 +0100313 break;
314
315 default:
316 return -EINVAL;
317 }
318
319 bts = (char *)bts + ds_cfg.sizeof_bts;
320 if (bts >= get_bts_absolute_maximum(ds))
321 bts = get_bts_buffer_base(ds);
322 set_bts_index(ds, bts);
323
324 return 0;
325}
326
327unsigned long ds_debugctl_mask(void)
328{
329 return ds_cfg.debugctl_mask;
330}
331
332#ifdef __i386__
333static const struct ds_configuration ds_cfg_netburst = {
334 .sizeof_ds = 9 * 4,
335 .bts_buffer_base = { 0, 4 },
336 .bts_index = { 4, 4 },
337 .bts_absolute_maximum = { 8, 4 },
338 .bts_interrupt_threshold = { 12, 4 },
339 .sizeof_bts = 3 * 4,
340 .from_ip = { 0, 4 },
341 .to_ip = { 4, 4 },
342 .info_type = { 4, 1 },
Markus Metzger3c689042008-01-30 13:31:20 +0100343 .info_data = { 8, 4 },
Markus Metzgereee3af42008-01-30 13:31:09 +0100344 .debugctl_mask = (1<<2)|(1<<3)
345};
346
347static const struct ds_configuration ds_cfg_pentium_m = {
348 .sizeof_ds = 9 * 4,
349 .bts_buffer_base = { 0, 4 },
350 .bts_index = { 4, 4 },
351 .bts_absolute_maximum = { 8, 4 },
352 .bts_interrupt_threshold = { 12, 4 },
353 .sizeof_bts = 3 * 4,
354 .from_ip = { 0, 4 },
355 .to_ip = { 4, 4 },
356 .info_type = { 4, 1 },
Markus Metzger3c689042008-01-30 13:31:20 +0100357 .info_data = { 8, 4 },
Markus Metzgereee3af42008-01-30 13:31:09 +0100358 .debugctl_mask = (1<<6)|(1<<7)
359};
360#endif /* _i386_ */
361
362static const struct ds_configuration ds_cfg_core2 = {
363 .sizeof_ds = 9 * 8,
364 .bts_buffer_base = { 0, 8 },
365 .bts_index = { 8, 8 },
366 .bts_absolute_maximum = { 16, 8 },
367 .bts_interrupt_threshold = { 24, 8 },
368 .sizeof_bts = 3 * 8,
369 .from_ip = { 0, 8 },
370 .to_ip = { 8, 8 },
371 .info_type = { 8, 1 },
Markus Metzger3c689042008-01-30 13:31:20 +0100372 .info_data = { 16, 8 },
Markus Metzgereee3af42008-01-30 13:31:09 +0100373 .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
374};
375
376static inline void
377ds_configure(const struct ds_configuration *cfg)
378{
379 ds_cfg = *cfg;
380}
381
382void __cpuinit ds_init_intel(struct cpuinfo_x86 *c)
383{
384 switch (c->x86) {
385 case 0x6:
386 switch (c->x86_model) {
387#ifdef __i386__
388 case 0xD:
389 case 0xE: /* Pentium M */
390 ds_configure(&ds_cfg_pentium_m);
391 break;
392#endif /* _i386_ */
393 case 0xF: /* Core2 */
394 ds_configure(&ds_cfg_core2);
395 break;
396 default:
397 /* sorry, don't know about them */
398 break;
399 }
400 break;
401 case 0xF:
402 switch (c->x86_model) {
403#ifdef __i386__
404 case 0x0:
405 case 0x1:
406 case 0x2: /* Netburst */
407 ds_configure(&ds_cfg_netburst);
408 break;
409#endif /* _i386_ */
410 default:
411 /* sorry, don't know about them */
412 break;
413 }
414 break;
415 default:
416 /* sorry, don't know about them */
417 break;
418 }
419}