blob: a8f672ba100c541fa893c4167dffabfab1c97561 [file] [log] [blame]
Markus Metzgereee3af42008-01-30 13:31:09 +01001/*
2 * Debug Store (DS) 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
H. Peter Anvin1965aae2008-10-22 22:26:29 -070022#ifndef _ASM_X86_DS_H
23#define _ASM_X86_DS_H
Markus Metzgereee3af42008-01-30 13:31:09 +010024
Markus Metzger93fa7632008-04-08 11:01:58 +020025
Markus Metzgereee3af42008-01-30 13:31:09 +010026#include <linux/types.h>
27#include <linux/init.h>
Markus Metzgerca0002a2008-11-25 09:01:25 +010028#include <linux/err.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010029
Markus Metzgereee3af42008-01-30 13:31:09 +010030
Markus Metzgere5e8ca62008-11-25 08:47:19 +010031#ifdef CONFIG_X86_DS
32
Markus Metzger93fa7632008-04-08 11:01:58 +020033struct task_struct;
Markus Metzgerc2724772008-12-11 13:49:59 +010034struct ds_context;
Markus Metzgerca0002a2008-11-25 09:01:25 +010035struct ds_tracer;
36struct bts_tracer;
37struct pebs_tracer;
38
39typedef void (*bts_ovfl_callback_t)(struct bts_tracer *);
40typedef void (*pebs_ovfl_callback_t)(struct pebs_tracer *);
Markus Metzgereee3af42008-01-30 13:31:09 +010041
Markus Metzgerc2724772008-12-11 13:49:59 +010042
43/*
44 * A list of features plus corresponding macros to talk about them in
45 * the ds_request function's flags parameter.
46 *
47 * We use the enum to index an array of corresponding control bits;
48 * we use the macro to index a flags bit-vector.
49 */
50enum ds_feature {
51 dsf_bts = 0,
52 dsf_bts_kernel,
53#define BTS_KERNEL (1 << dsf_bts_kernel)
54 /* trace kernel-mode branches */
55
56 dsf_bts_user,
57#define BTS_USER (1 << dsf_bts_user)
58 /* trace user-mode branches */
59
60 dsf_bts_overflow,
61 dsf_bts_max,
62 dsf_pebs = dsf_bts_max,
63
64 dsf_pebs_max,
65 dsf_ctl_max = dsf_pebs_max,
66 dsf_bts_timestamps = dsf_ctl_max,
67#define BTS_TIMESTAMPS (1 << dsf_bts_timestamps)
68 /* add timestamps into BTS trace */
69
70#define BTS_USER_FLAGS (BTS_KERNEL | BTS_USER | BTS_TIMESTAMPS)
71};
72
73
Markus Metzger93fa7632008-04-08 11:01:58 +020074/*
75 * Request BTS or PEBS
Markus Metzgereee3af42008-01-30 13:31:09 +010076 *
Markus Metzger93fa7632008-04-08 11:01:58 +020077 * Due to alignement constraints, the actual buffer may be slightly
78 * smaller than the requested or provided buffer.
79 *
Markus Metzgerca0002a2008-11-25 09:01:25 +010080 * Returns a pointer to a tracer structure on success, or
81 * ERR_PTR(errcode) on failure.
82 *
83 * The interrupt threshold is independent from the overflow callback
84 * to allow users to use their own overflow interrupt handling mechanism.
Markus Metzger93fa7632008-04-08 11:01:58 +020085 *
86 * task: the task to request recording for;
87 * NULL for per-cpu recording on the current cpu
88 * base: the base pointer for the (non-pageable) buffer;
Markus Metzger6abb11a2008-11-25 09:05:27 +010089 * size: the size of the provided buffer in bytes
Markus Metzger93fa7632008-04-08 11:01:58 +020090 * ovfl: pointer to a function to be called on buffer overflow;
91 * NULL if cyclic buffer requested
Markus Metzgerca0002a2008-11-25 09:01:25 +010092 * th: the interrupt threshold in records from the end of the buffer;
93 * -1 if no interrupt threshold is requested.
Markus Metzgerc2724772008-12-11 13:49:59 +010094 * flags: a bit-mask of the above flags
Markus Metzgereee3af42008-01-30 13:31:09 +010095 */
Markus Metzgerca0002a2008-11-25 09:01:25 +010096extern struct bts_tracer *ds_request_bts(struct task_struct *task,
97 void *base, size_t size,
Markus Metzgerc2724772008-12-11 13:49:59 +010098 bts_ovfl_callback_t ovfl,
99 size_t th, unsigned int flags);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100100extern struct pebs_tracer *ds_request_pebs(struct task_struct *task,
101 void *base, size_t size,
102 pebs_ovfl_callback_t ovfl,
Markus Metzgerc2724772008-12-11 13:49:59 +0100103 size_t th, unsigned int flags);
Markus Metzger93fa7632008-04-08 11:01:58 +0200104
105/*
106 * Release BTS or PEBS resources
Markus Metzgerc2724772008-12-11 13:49:59 +0100107 * Suspend and resume BTS or PEBS tracing
Markus Metzger93fa7632008-04-08 11:01:58 +0200108 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100109 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200110 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100111extern void ds_release_bts(struct bts_tracer *tracer);
112extern void ds_suspend_bts(struct bts_tracer *tracer);
113extern void ds_resume_bts(struct bts_tracer *tracer);
114extern void ds_release_pebs(struct pebs_tracer *tracer);
115extern void ds_suspend_pebs(struct pebs_tracer *tracer);
116extern void ds_resume_pebs(struct pebs_tracer *tracer);
117
Markus Metzger93fa7632008-04-08 11:01:58 +0200118
119/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100120 * The raw DS buffer state as it is used for BTS and PEBS recording.
Markus Metzger93fa7632008-04-08 11:01:58 +0200121 *
Markus Metzgerc2724772008-12-11 13:49:59 +0100122 * This is the low-level, arch-dependent interface for working
123 * directly on the raw trace data.
Markus Metzger93fa7632008-04-08 11:01:58 +0200124 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100125struct ds_trace {
126 /* the number of bts/pebs records */
127 size_t n;
128 /* the size of a bts/pebs record in bytes */
129 size_t size;
130 /* pointers into the raw buffer:
131 - to the first entry */
132 void *begin;
133 /* - one beyond the last entry */
134 void *end;
135 /* - one beyond the newest entry */
136 void *top;
137 /* - the interrupt threshold */
138 void *ith;
139 /* flags given on ds_request() */
140 unsigned int flags;
141};
Markus Metzger93fa7632008-04-08 11:01:58 +0200142
143/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100144 * An arch-independent view on branch trace data.
Markus Metzger93fa7632008-04-08 11:01:58 +0200145 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100146enum bts_qualifier {
147 bts_invalid,
148#define BTS_INVALID bts_invalid
149
150 bts_branch,
151#define BTS_BRANCH bts_branch
152
153 bts_task_arrives,
154#define BTS_TASK_ARRIVES bts_task_arrives
155
156 bts_task_departs,
157#define BTS_TASK_DEPARTS bts_task_departs
158
159 bts_qual_bit_size = 4,
160 bts_qual_max = (1 << bts_qual_bit_size),
161};
162
163struct bts_struct {
164 __u64 qualifier;
165 union {
166 /* BTS_BRANCH */
167 struct {
168 __u64 from;
169 __u64 to;
170 } lbr;
171 /* BTS_TASK_ARRIVES or BTS_TASK_DEPARTS */
172 struct {
173 __u64 jiffies;
174 pid_t pid;
175 } timestamp;
176 } variant;
177};
178
Markus Metzger93fa7632008-04-08 11:01:58 +0200179
180/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100181 * The BTS state.
Markus Metzger93fa7632008-04-08 11:01:58 +0200182 *
Markus Metzgerc2724772008-12-11 13:49:59 +0100183 * This gives access to the raw DS state and adds functions to provide
184 * an arch-independent view of the BTS data.
Markus Metzger93fa7632008-04-08 11:01:58 +0200185 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100186struct bts_trace {
187 struct ds_trace ds;
188
189 int (*read)(struct bts_tracer *tracer, const void *at,
190 struct bts_struct *out);
191 int (*write)(struct bts_tracer *tracer, const struct bts_struct *in);
192};
193
Markus Metzger93fa7632008-04-08 11:01:58 +0200194
195/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100196 * The PEBS state.
Markus Metzger93fa7632008-04-08 11:01:58 +0200197 *
Markus Metzgerc2724772008-12-11 13:49:59 +0100198 * This gives access to the raw DS state and the PEBS-specific counter
199 * reset value.
200 */
201struct pebs_trace {
202 struct ds_trace ds;
203
204 /* the PEBS reset value */
205 unsigned long long reset_value;
206};
207
208
209/*
210 * Read the BTS or PEBS trace.
Markus Metzger93fa7632008-04-08 11:01:58 +0200211 *
Markus Metzgerc2724772008-12-11 13:49:59 +0100212 * Returns a view on the trace collected for the parameter tracer.
Markus Metzger93fa7632008-04-08 11:01:58 +0200213 *
Markus Metzgerc2724772008-12-11 13:49:59 +0100214 * The view remains valid as long as the traced task is not running or
215 * the tracer is suspended.
216 * Writes into the trace buffer are not reflected.
Markus Metzger93fa7632008-04-08 11:01:58 +0200217 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100218 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200219 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100220extern const struct bts_trace *ds_read_bts(struct bts_tracer *tracer);
221extern const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer);
222
Markus Metzger93fa7632008-04-08 11:01:58 +0200223
224/*
Markus Metzger93fa7632008-04-08 11:01:58 +0200225 * Reset the write pointer of the BTS/PEBS buffer.
226 *
227 * Returns 0 on success; -Eerrno on error
228 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100229 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200230 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100231extern int ds_reset_bts(struct bts_tracer *tracer);
232extern int ds_reset_pebs(struct pebs_tracer *tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200233
234/*
Markus Metzger93fa7632008-04-08 11:01:58 +0200235 * Set the PEBS counter reset value.
236 *
237 * Returns 0 on success; -Eerrno on error
238 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100239 * tracer: the tracer handle returned from ds_request_pebs()
Markus Metzger93fa7632008-04-08 11:01:58 +0200240 * value: the new counter reset value
241 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100242extern int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value);
Markus Metzger93fa7632008-04-08 11:01:58 +0200243
244/*
245 * Initialization
246 */
247struct cpuinfo_x86;
248extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
249
Markus Metzger93fa7632008-04-08 11:01:58 +0200250/*
Markus Metzgerc2724772008-12-11 13:49:59 +0100251 * Context switch work
Markus Metzger93fa7632008-04-08 11:01:58 +0200252 */
Markus Metzgerc2724772008-12-11 13:49:59 +0100253extern void ds_switch_to(struct task_struct *prev, struct task_struct *next);
Markus Metzgereee3af42008-01-30 13:31:09 +0100254
Markus Metzgerbf53de92008-12-19 15:10:24 +0100255/*
256 * Task clone/init and cleanup work
257 */
258extern void ds_copy_thread(struct task_struct *tsk, struct task_struct *father);
259extern void ds_exit_thread(struct task_struct *tsk);
260
Markus Metzger93fa7632008-04-08 11:01:58 +0200261#else /* CONFIG_X86_DS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100262
Markus Metzgere5e8ca62008-11-25 08:47:19 +0100263struct cpuinfo_x86;
264static inline void __cpuinit ds_init_intel(struct cpuinfo_x86 *ignored) {}
Markus Metzgerc2724772008-12-11 13:49:59 +0100265static inline void ds_switch_to(struct task_struct *prev,
266 struct task_struct *next) {}
Markus Metzgerbf53de92008-12-19 15:10:24 +0100267static inline void ds_copy_thread(struct task_struct *tsk,
268 struct task_struct *father) {}
269static inline void ds_exit_thread(struct task_struct *tsk) {}
Markus Metzgereee3af42008-01-30 13:31:09 +0100270
Markus Metzger93fa7632008-04-08 11:01:58 +0200271#endif /* CONFIG_X86_DS */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700272#endif /* _ASM_X86_DS_H */