66 * precise-event based sampling (PEBS).
77 *
88 * It manages:
9- * - per-thread and per-cpu allocation of BTS and PEBS
9+ * - DS and BTS hardware configuration
1010 * - buffer overflow handling (to be done)
1111 * - buffer access
1212 *
13- * It assumes :
14- * - get_task_struct on all traced tasks
15- * - current is allowed to trace tasks
13+ * It does not do :
14+ * - security checking (is the caller allowed to trace the task)
15+ * - buffer allocation (memory accounting)
1616 *
1717 *
1818 * Copyright (C) 2007-2008 Intel Corporation.
3131#ifdef CONFIG_X86_DS
3232
3333struct task_struct ;
34+ struct ds_context ;
3435struct ds_tracer ;
3536struct bts_tracer ;
3637struct pebs_tracer ;
3738
3839typedef void (* bts_ovfl_callback_t )(struct bts_tracer * );
3940typedef void (* pebs_ovfl_callback_t )(struct pebs_tracer * );
4041
42+
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+ */
50+ enum 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+
4174/*
4275 * Request BTS or PEBS
4376 *
@@ -58,92 +91,135 @@ typedef void (*pebs_ovfl_callback_t)(struct pebs_tracer *);
5891 * NULL if cyclic buffer requested
5992 * th: the interrupt threshold in records from the end of the buffer;
6093 * -1 if no interrupt threshold is requested.
94+ * flags: a bit-mask of the above flags
6195 */
6296extern struct bts_tracer * ds_request_bts (struct task_struct * task ,
6397 void * base , size_t size ,
64- bts_ovfl_callback_t ovfl , size_t th );
98+ bts_ovfl_callback_t ovfl ,
99+ size_t th , unsigned int flags );
65100extern struct pebs_tracer * ds_request_pebs (struct task_struct * task ,
66101 void * base , size_t size ,
67102 pebs_ovfl_callback_t ovfl ,
68- size_t th );
103+ size_t th , unsigned int flags );
69104
70105/*
71106 * Release BTS or PEBS resources
72- *
73- * Returns 0 on success; -Eerrno otherwise
107+ * Suspend and resume BTS or PEBS tracing
74108 *
75109 * tracer: the tracer handle returned from ds_request_~()
76110 */
77- extern int ds_release_bts (struct bts_tracer * tracer );
78- extern int ds_release_pebs (struct pebs_tracer * tracer );
111+ extern void ds_release_bts (struct bts_tracer * tracer );
112+ extern void ds_suspend_bts (struct bts_tracer * tracer );
113+ extern void ds_resume_bts (struct bts_tracer * tracer );
114+ extern void ds_release_pebs (struct pebs_tracer * tracer );
115+ extern void ds_suspend_pebs (struct pebs_tracer * tracer );
116+ extern void ds_resume_pebs (struct pebs_tracer * tracer );
117+
79118
80119/*
81- * Get the (array) index of the write pointer.
82- * (assuming an array of BTS/PEBS records)
83- *
84- * Returns 0 on success; -Eerrno on error
120+ * The raw DS buffer state as it is used for BTS and PEBS recording.
85121 *
86- * tracer: the tracer handle returned from ds_request_~()
87- * pos (out): will hold the result
122+ * This is the low-level, arch-dependent interface for working
123+ * directly on the raw trace data.
88124 */
89- extern int ds_get_bts_index (struct bts_tracer * tracer , size_t * pos );
90- extern int ds_get_pebs_index (struct pebs_tracer * tracer , size_t * pos );
125+ struct 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+ };
91142
92143/*
93- * Get the (array) index one record beyond the end of the array.
94- * (assuming an array of BTS/PEBS records)
95- *
96- * Returns 0 on success; -Eerrno on error
97- *
98- * tracer: the tracer handle returned from ds_request_~()
99- * pos (out): will hold the result
144+ * An arch-independent view on branch trace data.
100145 */
101- extern int ds_get_bts_end (struct bts_tracer * tracer , size_t * pos );
102- extern int ds_get_pebs_end (struct pebs_tracer * tracer , size_t * pos );
146+ enum 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+
163+ struct 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+
103179
104180/*
105- * Provide a pointer to the BTS/PEBS record at parameter index.
106- * (assuming an array of BTS/PEBS records)
107- *
108- * The pointer points directly into the buffer. The user is
109- * responsible for copying the record.
110- *
111- * Returns the size of a single record on success; -Eerrno on error
181+ * The BTS state.
112182 *
113- * tracer: the tracer handle returned from ds_request_~()
114- * index: the index of the requested record
115- * record (out): pointer to the requested record
183+ * This gives access to the raw DS state and adds functions to provide
184+ * an arch-independent view of the BTS data.
116185 */
117- extern int ds_access_bts (struct bts_tracer * tracer ,
118- size_t index , const void * * record );
119- extern int ds_access_pebs (struct pebs_tracer * tracer ,
120- size_t index , const void * * record );
186+ struct 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+
121194
122195/*
123- * Write one or more BTS/PEBS records at the write pointer index and
124- * advance the write pointer.
196+ * The PEBS state.
125197 *
126- * If size is not a multiple of the record size, trailing bytes are
127- * zeroed out.
128- *
129- * May result in one or more overflow notifications.
130- *
131- * If called during overflow handling, that is, with index >=
132- * interrupt threshold, the write will wrap around.
198+ * This gives access to the raw DS state and the PEBS-specific counter
199+ * reset value.
200+ */
201+ struct 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.
133211 *
134- * An overflow notification is given if and when the interrupt
135- * threshold is reached during or after the write.
212+ * Returns a view on the trace collected for the parameter tracer.
136213 *
137- * Returns the number of bytes written or -Eerrno.
214+ * 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.
138217 *
139218 * tracer: the tracer handle returned from ds_request_~()
140- * buffer: the buffer to write
141- * size: the size of the buffer
142219 */
143- extern int ds_write_bts (struct bts_tracer * tracer ,
144- const void * buffer , size_t size );
145- extern int ds_write_pebs (struct pebs_tracer * tracer ,
146- const void * buffer , size_t size );
220+ extern const struct bts_trace * ds_read_bts (struct bts_tracer * tracer );
221+ extern const struct pebs_trace * ds_read_pebs (struct pebs_tracer * tracer );
222+
147223
148224/*
149225 * Reset the write pointer of the BTS/PEBS buffer.
@@ -155,27 +231,6 @@ extern int ds_write_pebs(struct pebs_tracer *tracer,
155231extern int ds_reset_bts (struct bts_tracer * tracer );
156232extern int ds_reset_pebs (struct pebs_tracer * tracer );
157233
158- /*
159- * Clear the BTS/PEBS buffer and reset the write pointer.
160- * The entire buffer will be zeroed out.
161- *
162- * Returns 0 on success; -Eerrno on error
163- *
164- * tracer: the tracer handle returned from ds_request_~()
165- */
166- extern int ds_clear_bts (struct bts_tracer * tracer );
167- extern int ds_clear_pebs (struct pebs_tracer * tracer );
168-
169- /*
170- * Provide the PEBS counter reset value.
171- *
172- * Returns 0 on success; -Eerrno on error
173- *
174- * tracer: the tracer handle returned from ds_request_pebs()
175- * value (out): the counter reset value
176- */
177- extern int ds_get_pebs_reset (struct pebs_tracer * tracer , u64 * value );
178-
179234/*
180235 * Set the PEBS counter reset value.
181236 *
@@ -192,35 +247,17 @@ extern int ds_set_pebs_reset(struct pebs_tracer *tracer, u64 value);
192247struct cpuinfo_x86 ;
193248extern void __cpuinit ds_init_intel (struct cpuinfo_x86 * );
194249
195-
196-
197250/*
198- * The DS context - part of struct thread_struct.
251+ * Context switch work
199252 */
200- #define MAX_SIZEOF_DS (12 * 8)
201-
202- struct ds_context {
203- /* pointer to the DS configuration; goes into MSR_IA32_DS_AREA */
204- unsigned char ds [MAX_SIZEOF_DS ];
205- /* the owner of the BTS and PEBS configuration, respectively */
206- struct ds_tracer * owner [2 ];
207- /* use count */
208- unsigned long count ;
209- /* a pointer to the context location inside the thread_struct
210- * or the per_cpu context array */
211- struct ds_context * * this ;
212- /* a pointer to the task owning this context, or NULL, if the
213- * context is owned by a cpu */
214- struct task_struct * task ;
215- };
216-
217- /* called by exit_thread() to free leftover contexts */
218- extern void ds_free (struct ds_context * context );
253+ extern void ds_switch_to (struct task_struct * prev , struct task_struct * next );
219254
220255#else /* CONFIG_X86_DS */
221256
222257struct cpuinfo_x86 ;
223258static inline void __cpuinit ds_init_intel (struct cpuinfo_x86 * ignored ) {}
259+ static inline void ds_switch_to (struct task_struct * prev ,
260+ struct task_struct * next ) {}
224261
225262#endif /* CONFIG_X86_DS */
226263#endif /* _ASM_X86_DS_H */
0 commit comments