Commit | Line | Data |
---|---|---|
1e9b51c2 | 1 | /* |
a93751ca | 2 | * h/w branch tracer for x86 based on bts |
1e9b51c2 | 3 | * |
5c5317de MM |
4 | * Copyright (C) 2008-2009 Intel Corporation. |
5 | * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009 | |
1e9b51c2 MM |
6 | * |
7 | */ | |
8 | ||
9 | #include <linux/module.h> | |
10 | #include <linux/fs.h> | |
11 | #include <linux/debugfs.h> | |
12 | #include <linux/ftrace.h> | |
13 | #include <linux/kallsyms.h> | |
5c5317de MM |
14 | #include <linux/mutex.h> |
15 | #include <linux/cpu.h> | |
16 | #include <linux/smp.h> | |
1e9b51c2 MM |
17 | |
18 | #include <asm/ds.h> | |
19 | ||
20 | #include "trace.h" | |
f0868d1e | 21 | #include "trace_output.h" |
1e9b51c2 MM |
22 | |
23 | ||
24 | #define SIZEOF_BTS (1 << 13) | |
25 | ||
5c5317de MM |
26 | /* The tracer mutex protects the below per-cpu tracer array. |
27 | It needs to be held to: | |
28 | - start tracing on all cpus | |
29 | - stop tracing on all cpus | |
30 | - start tracing on a single hotplug cpu | |
31 | - stop tracing on a single hotplug cpu | |
32 | - read the trace from all cpus | |
33 | - read the trace from a single cpu | |
34 | */ | |
35 | static DEFINE_MUTEX(bts_tracer_mutex); | |
1e9b51c2 MM |
36 | static DEFINE_PER_CPU(struct bts_tracer *, tracer); |
37 | static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer); | |
38 | ||
39 | #define this_tracer per_cpu(tracer, smp_processor_id()) | |
40 | #define this_buffer per_cpu(buffer, smp_processor_id()) | |
41 | ||
5c5317de | 42 | static int __read_mostly trace_hw_branches_enabled; |
b1818748 | 43 | static struct trace_array *hw_branch_trace __read_mostly; |
1e9b51c2 | 44 | |
5c5317de MM |
45 | |
46 | /* | |
47 | * Start tracing on the current cpu. | |
48 | * The argument is ignored. | |
49 | * | |
50 | * pre: bts_tracer_mutex must be locked. | |
51 | */ | |
1e9b51c2 MM |
52 | static void bts_trace_start_cpu(void *arg) |
53 | { | |
a93751ca MM |
54 | if (this_tracer) |
55 | ds_release_bts(this_tracer); | |
56 | ||
1e9b51c2 MM |
57 | this_tracer = |
58 | ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS, | |
a93751ca MM |
59 | /* ovfl = */ NULL, /* th = */ (size_t)-1, |
60 | BTS_KERNEL); | |
1e9b51c2 MM |
61 | if (IS_ERR(this_tracer)) { |
62 | this_tracer = NULL; | |
63 | return; | |
64 | } | |
1e9b51c2 MM |
65 | } |
66 | ||
67 | static void bts_trace_start(struct trace_array *tr) | |
68 | { | |
5c5317de | 69 | mutex_lock(&bts_tracer_mutex); |
1e9b51c2 | 70 | |
5c5317de MM |
71 | on_each_cpu(bts_trace_start_cpu, NULL, 1); |
72 | trace_hw_branches_enabled = 1; | |
1e9b51c2 | 73 | |
5c5317de | 74 | mutex_unlock(&bts_tracer_mutex); |
1e9b51c2 MM |
75 | } |
76 | ||
5c5317de | 77 | /* |
c3706f00 | 78 | * Stop tracing on the current cpu. |
5c5317de MM |
79 | * The argument is ignored. |
80 | * | |
81 | * pre: bts_tracer_mutex must be locked. | |
82 | */ | |
1e9b51c2 MM |
83 | static void bts_trace_stop_cpu(void *arg) |
84 | { | |
85 | if (this_tracer) { | |
1e9b51c2 MM |
86 | ds_release_bts(this_tracer); |
87 | this_tracer = NULL; | |
88 | } | |
89 | } | |
90 | ||
91 | static void bts_trace_stop(struct trace_array *tr) | |
92 | { | |
5c5317de MM |
93 | mutex_lock(&bts_tracer_mutex); |
94 | ||
95 | trace_hw_branches_enabled = 0; | |
96 | on_each_cpu(bts_trace_stop_cpu, NULL, 1); | |
1e9b51c2 | 97 | |
5c5317de MM |
98 | mutex_unlock(&bts_tracer_mutex); |
99 | } | |
100 | ||
101 | static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb, | |
102 | unsigned long action, void *hcpu) | |
103 | { | |
104 | unsigned int cpu = (unsigned long)hcpu; | |
105 | ||
106 | mutex_lock(&bts_tracer_mutex); | |
107 | ||
108 | if (!trace_hw_branches_enabled) | |
109 | goto out; | |
110 | ||
111 | switch (action) { | |
112 | case CPU_ONLINE: | |
113 | case CPU_DOWN_FAILED: | |
114 | smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1); | |
115 | break; | |
116 | case CPU_DOWN_PREPARE: | |
1e9b51c2 | 117 | smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1); |
5c5317de MM |
118 | break; |
119 | } | |
120 | ||
121 | out: | |
122 | mutex_unlock(&bts_tracer_mutex); | |
123 | return NOTIFY_DONE; | |
1e9b51c2 MM |
124 | } |
125 | ||
5c5317de MM |
126 | static struct notifier_block bts_hotcpu_notifier __cpuinitdata = { |
127 | .notifier_call = bts_hotcpu_handler | |
128 | }; | |
129 | ||
5e01cb69 | 130 | static int bts_trace_init(struct trace_array *tr) |
1e9b51c2 | 131 | { |
b1818748 MM |
132 | hw_branch_trace = tr; |
133 | ||
1e9b51c2 MM |
134 | bts_trace_start(tr); |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
5e01cb69 | 139 | static void bts_trace_reset(struct trace_array *tr) |
5c5317de MM |
140 | { |
141 | bts_trace_stop(tr); | |
5c5317de MM |
142 | } |
143 | ||
1e9b51c2 MM |
144 | static void bts_trace_print_header(struct seq_file *m) |
145 | { | |
11edda06 | 146 | seq_puts(m, "# CPU# TO <- FROM\n"); |
1e9b51c2 MM |
147 | } |
148 | ||
149 | static enum print_line_t bts_trace_print_line(struct trace_iterator *iter) | |
150 | { | |
151 | struct trace_entry *entry = iter->ent; | |
152 | struct trace_seq *seq = &iter->seq; | |
a93751ca | 153 | struct hw_branch_entry *it; |
11edda06 | 154 | unsigned long symflags = TRACE_ITER_SYM_OFFSET; |
1e9b51c2 MM |
155 | |
156 | trace_assign_type(it, entry); | |
157 | ||
a93751ca | 158 | if (entry->type == TRACE_HW_BRANCHES) { |
1830b52d | 159 | if (trace_seq_printf(seq, "%4d ", iter->cpu) && |
11edda06 MM |
160 | seq_print_ip_sym(seq, it->to, symflags) && |
161 | trace_seq_printf(seq, "\t <- ") && | |
162 | seq_print_ip_sym(seq, it->from, symflags) && | |
a93751ca MM |
163 | trace_seq_printf(seq, "\n")) |
164 | return TRACE_TYPE_HANDLED; | |
165 | return TRACE_TYPE_PARTIAL_LINE;; | |
1e9b51c2 MM |
166 | } |
167 | return TRACE_TYPE_UNHANDLED; | |
168 | } | |
169 | ||
b1818748 | 170 | void trace_hw_branch(u64 from, u64 to) |
1e9b51c2 | 171 | { |
b1818748 | 172 | struct trace_array *tr = hw_branch_trace; |
1e9b51c2 | 173 | struct ring_buffer_event *event; |
a93751ca | 174 | struct hw_branch_entry *entry; |
0a987751 | 175 | unsigned long irq1; |
5c5317de | 176 | int cpu; |
1e9b51c2 | 177 | |
5c5317de MM |
178 | if (unlikely(!tr)) |
179 | return; | |
180 | ||
181 | if (unlikely(!trace_hw_branches_enabled)) | |
1e9b51c2 | 182 | return; |
5c5317de MM |
183 | |
184 | local_irq_save(irq1); | |
185 | cpu = raw_smp_processor_id(); | |
186 | if (atomic_inc_return(&tr->data[cpu]->disabled) != 1) | |
187 | goto out; | |
188 | ||
51a763dd ACM |
189 | event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES, |
190 | sizeof(*entry), 0, 0); | |
5c5317de MM |
191 | if (!event) |
192 | goto out; | |
1e9b51c2 MM |
193 | entry = ring_buffer_event_data(event); |
194 | tracing_generic_entry_update(&entry->ent, 0, from); | |
a93751ca | 195 | entry->ent.type = TRACE_HW_BRANCHES; |
1e9b51c2 MM |
196 | entry->from = from; |
197 | entry->to = to; | |
51a763dd | 198 | trace_buffer_unlock_commit(tr, event, 0, 0); |
5c5317de MM |
199 | |
200 | out: | |
201 | atomic_dec(&tr->data[cpu]->disabled); | |
202 | local_irq_restore(irq1); | |
1e9b51c2 MM |
203 | } |
204 | ||
b1818748 | 205 | static void trace_bts_at(const struct bts_trace *trace, void *at) |
1e9b51c2 | 206 | { |
a93751ca MM |
207 | struct bts_struct bts; |
208 | int err = 0; | |
1e9b51c2 | 209 | |
a93751ca MM |
210 | WARN_ON_ONCE(!trace->read); |
211 | if (!trace->read) | |
1e9b51c2 MM |
212 | return; |
213 | ||
a93751ca MM |
214 | err = trace->read(this_tracer, at, &bts); |
215 | if (err < 0) | |
216 | return; | |
1e9b51c2 | 217 | |
a93751ca MM |
218 | switch (bts.qualifier) { |
219 | case BTS_BRANCH: | |
b1818748 | 220 | trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to); |
a93751ca MM |
221 | break; |
222 | } | |
1e9b51c2 MM |
223 | } |
224 | ||
5c5317de MM |
225 | /* |
226 | * Collect the trace on the current cpu and write it into the ftrace buffer. | |
227 | * | |
228 | * pre: bts_tracer_mutex must be locked | |
229 | */ | |
1e9b51c2 MM |
230 | static void trace_bts_cpu(void *arg) |
231 | { | |
232 | struct trace_array *tr = (struct trace_array *) arg; | |
a93751ca MM |
233 | const struct bts_trace *trace; |
234 | unsigned char *at; | |
1e9b51c2 | 235 | |
b1818748 | 236 | if (unlikely(!tr)) |
1e9b51c2 MM |
237 | return; |
238 | ||
5c5317de MM |
239 | if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled))) |
240 | return; | |
241 | ||
b1818748 MM |
242 | if (unlikely(!this_tracer)) |
243 | return; | |
244 | ||
a93751ca MM |
245 | ds_suspend_bts(this_tracer); |
246 | trace = ds_read_bts(this_tracer); | |
247 | if (!trace) | |
1e9b51c2 MM |
248 | goto out; |
249 | ||
a93751ca MM |
250 | for (at = trace->ds.top; (void *)at < trace->ds.end; |
251 | at += trace->ds.size) | |
b1818748 | 252 | trace_bts_at(trace, at); |
1e9b51c2 | 253 | |
a93751ca MM |
254 | for (at = trace->ds.begin; (void *)at < trace->ds.top; |
255 | at += trace->ds.size) | |
b1818748 | 256 | trace_bts_at(trace, at); |
1e9b51c2 MM |
257 | |
258 | out: | |
a93751ca | 259 | ds_resume_bts(this_tracer); |
1e9b51c2 MM |
260 | } |
261 | ||
262 | static void trace_bts_prepare(struct trace_iterator *iter) | |
263 | { | |
5c5317de MM |
264 | mutex_lock(&bts_tracer_mutex); |
265 | ||
266 | on_each_cpu(trace_bts_cpu, iter->tr, 1); | |
1e9b51c2 | 267 | |
5c5317de | 268 | mutex_unlock(&bts_tracer_mutex); |
1e9b51c2 MM |
269 | } |
270 | ||
e23b8ad8 MM |
271 | static void trace_bts_close(struct trace_iterator *iter) |
272 | { | |
273 | tracing_reset_online_cpus(iter->tr); | |
274 | } | |
275 | ||
b1818748 MM |
276 | void trace_hw_branch_oops(void) |
277 | { | |
278 | mutex_lock(&bts_tracer_mutex); | |
279 | ||
280 | trace_bts_cpu(hw_branch_trace); | |
281 | ||
282 | mutex_unlock(&bts_tracer_mutex); | |
283 | } | |
284 | ||
1e9b51c2 MM |
285 | struct tracer bts_tracer __read_mostly = |
286 | { | |
a93751ca | 287 | .name = "hw-branch-tracer", |
1e9b51c2 | 288 | .init = bts_trace_init, |
5c5317de | 289 | .reset = bts_trace_reset, |
1e9b51c2 MM |
290 | .print_header = bts_trace_print_header, |
291 | .print_line = bts_trace_print_line, | |
292 | .start = bts_trace_start, | |
293 | .stop = bts_trace_stop, | |
e23b8ad8 MM |
294 | .open = trace_bts_prepare, |
295 | .close = trace_bts_close | |
1e9b51c2 MM |
296 | }; |
297 | ||
298 | __init static int init_bts_trace(void) | |
299 | { | |
5e01cb69 | 300 | register_hotcpu_notifier(&bts_hotcpu_notifier); |
1e9b51c2 MM |
301 | return register_tracer(&bts_tracer); |
302 | } | |
303 | device_initcall(init_bts_trace); |