blob: 0af997de5f01e834213dc3063a9d02fd52460a76 [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:
9 * - per-thread and per-cpu allocation of BTS and PEBS
10 * - buffer memory allocation (optional)
11 * - buffer overflow handling
12 * - buffer access
13 *
14 * It assumes:
15 * - get_task_struct on all parameter tasks
16 * - current is allowed to trace parameter tasks
Markus Metzgereee3af42008-01-30 13:31:09 +010017 *
18 *
Markus Metzger93fa7632008-04-08 11:01:58 +020019 * Copyright (C) 2007-2008 Intel Corporation.
20 * Markus Metzger <markus.t.metzger@intel.com>, 2007-2008
Markus Metzgereee3af42008-01-30 13:31:09 +010021 */
22
H. Peter Anvin1965aae2008-10-22 22:26:29 -070023#ifndef _ASM_X86_DS_H
24#define _ASM_X86_DS_H
Markus Metzgereee3af42008-01-30 13:31:09 +010025
Markus Metzger93fa7632008-04-08 11:01:58 +020026
Markus Metzgereee3af42008-01-30 13:31:09 +010027#include <linux/types.h>
28#include <linux/init.h>
Markus Metzgerca0002a2008-11-25 09:01:25 +010029#include <linux/err.h>
Markus Metzgereee3af42008-01-30 13:31:09 +010030
Markus Metzgereee3af42008-01-30 13:31:09 +010031
Markus Metzgere5e8ca62008-11-25 08:47:19 +010032#ifdef CONFIG_X86_DS
33
Markus Metzger93fa7632008-04-08 11:01:58 +020034struct task_struct;
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 Metzger93fa7632008-04-08 11:01:58 +020042/*
43 * Request BTS or PEBS
Markus Metzgereee3af42008-01-30 13:31:09 +010044 *
Markus Metzger93fa7632008-04-08 11:01:58 +020045 * Due to alignement constraints, the actual buffer may be slightly
46 * smaller than the requested or provided buffer.
47 *
Markus Metzgerca0002a2008-11-25 09:01:25 +010048 * Returns a pointer to a tracer structure on success, or
49 * ERR_PTR(errcode) on failure.
50 *
51 * The interrupt threshold is independent from the overflow callback
52 * to allow users to use their own overflow interrupt handling mechanism.
Markus Metzger93fa7632008-04-08 11:01:58 +020053 *
54 * task: the task to request recording for;
55 * NULL for per-cpu recording on the current cpu
56 * base: the base pointer for the (non-pageable) buffer;
57 * NULL if buffer allocation requested
Markus Metzgerca0002a2008-11-25 09:01:25 +010058 * size: the size of the requested or provided buffer in bytes
Markus Metzger93fa7632008-04-08 11:01:58 +020059 * ovfl: pointer to a function to be called on buffer overflow;
60 * NULL if cyclic buffer requested
Markus Metzgerca0002a2008-11-25 09:01:25 +010061 * th: the interrupt threshold in records from the end of the buffer;
62 * -1 if no interrupt threshold is requested.
Markus Metzgereee3af42008-01-30 13:31:09 +010063 */
Markus Metzgerca0002a2008-11-25 09:01:25 +010064extern struct bts_tracer *ds_request_bts(struct task_struct *task,
65 void *base, size_t size,
66 bts_ovfl_callback_t ovfl, size_t th);
67extern struct pebs_tracer *ds_request_pebs(struct task_struct *task,
68 void *base, size_t size,
69 pebs_ovfl_callback_t ovfl,
70 size_t th);
Markus Metzger93fa7632008-04-08 11:01:58 +020071
72/*
73 * Release BTS or PEBS resources
74 *
75 * Frees buffers allocated on ds_request.
76 *
77 * Returns 0 on success; -Eerrno otherwise
78 *
Markus Metzgerca0002a2008-11-25 09:01:25 +010079 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +020080 */
Markus Metzgerca0002a2008-11-25 09:01:25 +010081extern int ds_release_bts(struct bts_tracer *tracer);
82extern int ds_release_pebs(struct pebs_tracer *tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +020083
84/*
Markus Metzgerca0002a2008-11-25 09:01:25 +010085 * Get the (array) index of the write pointer.
Markus Metzger93fa7632008-04-08 11:01:58 +020086 * (assuming an array of BTS/PEBS records)
87 *
Markus Metzgerca0002a2008-11-25 09:01:25 +010088 * Returns 0 on success; -Eerrno on error
Markus Metzger93fa7632008-04-08 11:01:58 +020089 *
Markus Metzgerca0002a2008-11-25 09:01:25 +010090 * tracer: the tracer handle returned from ds_request_~()
91 * pos (out): will hold the result
Markus Metzger93fa7632008-04-08 11:01:58 +020092 */
Markus Metzgerca0002a2008-11-25 09:01:25 +010093extern int ds_get_bts_index(struct bts_tracer *tracer, size_t *pos);
94extern int ds_get_pebs_index(struct pebs_tracer *tracer, size_t *pos);
Markus Metzger93fa7632008-04-08 11:01:58 +020095
96/*
Markus Metzgerca0002a2008-11-25 09:01:25 +010097 * Get the (array) index one record beyond the end of the array.
Markus Metzger93fa7632008-04-08 11:01:58 +020098 * (assuming an array of BTS/PEBS records)
99 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100100 * Returns 0 on success; -Eerrno on error
Markus Metzger93fa7632008-04-08 11:01:58 +0200101 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100102 * tracer: the tracer handle returned from ds_request_~()
103 * pos (out): will hold the result
Markus Metzger93fa7632008-04-08 11:01:58 +0200104 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100105extern int ds_get_bts_end(struct bts_tracer *tracer, size_t *pos);
106extern int ds_get_pebs_end(struct pebs_tracer *tracer, size_t *pos);
Markus Metzger93fa7632008-04-08 11:01:58 +0200107
108/*
109 * Provide a pointer to the BTS/PEBS record at parameter index.
110 * (assuming an array of BTS/PEBS records)
111 *
112 * The pointer points directly into the buffer. The user is
113 * responsible for copying the record.
114 *
115 * Returns the size of a single record on success; -Eerrno on error
116 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100117 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200118 * index: the index of the requested record
119 * record (out): pointer to the requested record
120 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100121extern int ds_access_bts(struct bts_tracer *tracer,
Markus Metzger93fa7632008-04-08 11:01:58 +0200122 size_t index, const void **record);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100123extern int ds_access_pebs(struct pebs_tracer *tracer,
Markus Metzger93fa7632008-04-08 11:01:58 +0200124 size_t index, const void **record);
125
126/*
127 * Write one or more BTS/PEBS records at the write pointer index and
128 * advance the write pointer.
129 *
130 * If size is not a multiple of the record size, trailing bytes are
131 * zeroed out.
132 *
133 * May result in one or more overflow notifications.
134 *
135 * If called during overflow handling, that is, with index >=
136 * interrupt threshold, the write will wrap around.
137 *
138 * An overflow notification is given if and when the interrupt
139 * threshold is reached during or after the write.
140 *
141 * Returns the number of bytes written or -Eerrno.
142 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100143 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200144 * buffer: the buffer to write
145 * size: the size of the buffer
146 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100147extern int ds_write_bts(struct bts_tracer *tracer,
Markus Metzger93fa7632008-04-08 11:01:58 +0200148 const void *buffer, size_t size);
Markus Metzgerca0002a2008-11-25 09:01:25 +0100149extern int ds_write_pebs(struct pebs_tracer *tracer,
Markus Metzger93fa7632008-04-08 11:01:58 +0200150 const void *buffer, size_t size);
151
152/*
Markus Metzger93fa7632008-04-08 11:01:58 +0200153 * Reset the write pointer of the BTS/PEBS buffer.
154 *
155 * Returns 0 on success; -Eerrno on error
156 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100157 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200158 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100159extern int ds_reset_bts(struct bts_tracer *tracer);
160extern int ds_reset_pebs(struct pebs_tracer *tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200161
162/*
163 * Clear the BTS/PEBS buffer and reset the write pointer.
164 * The entire buffer will be zeroed out.
165 *
166 * Returns 0 on success; -Eerrno on error
167 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100168 * tracer: the tracer handle returned from ds_request_~()
Markus Metzger93fa7632008-04-08 11:01:58 +0200169 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100170extern int ds_clear_bts(struct bts_tracer *tracer);
171extern int ds_clear_pebs(struct pebs_tracer *tracer);
Markus Metzger93fa7632008-04-08 11:01:58 +0200172
173/*
174 * Provide the PEBS counter reset value.
175 *
176 * Returns 0 on success; -Eerrno on error
177 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100178 * tracer: the tracer handle returned from ds_request_pebs()
Markus Metzger93fa7632008-04-08 11:01:58 +0200179 * value (out): the counter reset value
180 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100181extern int ds_get_pebs_reset(struct pebs_tracer *tracer, u64 *value);
Markus Metzger93fa7632008-04-08 11:01:58 +0200182
183/*
184 * Set the PEBS counter reset value.
185 *
186 * Returns 0 on success; -Eerrno on error
187 *
Markus Metzgerca0002a2008-11-25 09:01:25 +0100188 * tracer: the tracer handle returned from ds_request_pebs()
Markus Metzger93fa7632008-04-08 11:01:58 +0200189 * value: the new counter reset value
190 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100191extern int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value);
Markus Metzger93fa7632008-04-08 11:01:58 +0200192
193/*
194 * Initialization
195 */
196struct cpuinfo_x86;
197extern void __cpuinit ds_init_intel(struct cpuinfo_x86 *);
198
199
200
201/*
202 * The DS context - part of struct thread_struct.
203 */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100204#define MAX_SIZEOF_DS (12 * 8)
205
Markus Metzger93fa7632008-04-08 11:01:58 +0200206struct ds_context {
207 /* pointer to the DS configuration; goes into MSR_IA32_DS_AREA */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100208 unsigned char ds[MAX_SIZEOF_DS];
Markus Metzger93fa7632008-04-08 11:01:58 +0200209 /* the owner of the BTS and PEBS configuration, respectively */
Markus Metzgerca0002a2008-11-25 09:01:25 +0100210 struct ds_tracer *owner[2];
Markus Metzger93fa7632008-04-08 11:01:58 +0200211 /* use count */
212 unsigned long count;
213 /* a pointer to the context location inside the thread_struct
214 * or the per_cpu context array */
215 struct ds_context **this;
216 /* a pointer to the task owning this context, or NULL, if the
217 * context is owned by a cpu */
218 struct task_struct *task;
Markus Metzgereee3af42008-01-30 13:31:09 +0100219};
220
Markus Metzger93fa7632008-04-08 11:01:58 +0200221/* called by exit_thread() to free leftover contexts */
222extern void ds_free(struct ds_context *context);
Markus Metzgereee3af42008-01-30 13:31:09 +0100223
Markus Metzger93fa7632008-04-08 11:01:58 +0200224#else /* CONFIG_X86_DS */
Markus Metzgereee3af42008-01-30 13:31:09 +0100225
Markus Metzgere5e8ca62008-11-25 08:47:19 +0100226struct cpuinfo_x86;
227static inline void __cpuinit ds_init_intel(struct cpuinfo_x86 *ignored) {}
Markus Metzgereee3af42008-01-30 13:31:09 +0100228
Markus Metzger93fa7632008-04-08 11:01:58 +0200229#endif /* CONFIG_X86_DS */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700230#endif /* _ASM_X86_DS_H */