blob: 8b2109a6c61cc24b2838dfd0b7e7a91b0b1cce35 [file] [log] [blame]
Markus Metzgera93751c2008-12-11 13:53:26 +01001/*
Ingo Molnare9a22d12009-03-13 11:54:40 +01002 * h/w branch tracer for x86 based on BTS
Markus Metzgera93751c2008-12-11 13:53:26 +01003 *
Markus Metzger5c5317d2009-01-19 10:26:53 +01004 * Copyright (C) 2008-2009 Intel Corporation.
5 * Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
Markus Metzgera93751c2008-12-11 13:53:26 +01006 */
Ingo Molnar2d542cf2009-02-25 08:40:09 +01007#include <linux/spinlock.h>
8#include <linux/kallsyms.h>
Markus Metzgera93751c2008-12-11 13:53:26 +01009#include <linux/debugfs.h>
10#include <linux/ftrace.h>
Ingo Molnar2d542cf2009-02-25 08:40:09 +010011#include <linux/module.h>
Markus Metzger5c5317d2009-01-19 10:26:53 +010012#include <linux/cpu.h>
13#include <linux/smp.h>
Ingo Molnar2d542cf2009-02-25 08:40:09 +010014#include <linux/fs.h>
Markus Metzgera93751c2008-12-11 13:53:26 +010015
16#include <asm/ds.h>
17
Steven Rostedtf0868d12008-12-23 23:24:12 -050018#include "trace_output.h"
Ingo Molnare9a22d12009-03-13 11:54:40 +010019#include "trace.h"
Markus Metzgera93751c2008-12-11 13:53:26 +010020
21
Markus Metzgerba9372a2009-03-13 10:48:52 +010022#define BTS_BUFFER_SIZE (1 << 13)
Markus Metzgera93751c2008-12-11 13:53:26 +010023
Ingo Molnar2d542cf2009-02-25 08:40:09 +010024/*
25 * The tracer lock protects the below per-cpu tracer array.
26 * It needs to be held to:
27 * - start tracing on all cpus
28 * - stop tracing on all cpus
29 * - start tracing on a single hotplug cpu
30 * - stop tracing on a single hotplug cpu
31 * - read the trace from all cpus
32 * - read the trace from a single cpu
33 */
34static DEFINE_SPINLOCK(bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +010035static DEFINE_PER_CPU(struct bts_tracer *, tracer);
Markus Metzgerba9372a2009-03-13 10:48:52 +010036static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
Markus Metzgera93751c2008-12-11 13:53:26 +010037
38#define this_tracer per_cpu(tracer, smp_processor_id())
39#define this_buffer per_cpu(buffer, smp_processor_id())
40
Markus Metzgerba9372a2009-03-13 10:48:52 +010041static int trace_hw_branches_enabled __read_mostly;
42static int trace_hw_branches_suspended __read_mostly;
Markus Metzgerb1818742009-01-19 10:31:01 +010043static struct trace_array *hw_branch_trace __read_mostly;
Markus Metzgera93751c2008-12-11 13:53:26 +010044
Markus Metzger5c5317d2009-01-19 10:26:53 +010045
46/*
Markus Metzgerba9372a2009-03-13 10:48:52 +010047 * Initialize the tracer for the current cpu.
Markus Metzger5c5317d2009-01-19 10:26:53 +010048 * The argument is ignored.
49 *
Ingo Molnar2d542cf2009-02-25 08:40:09 +010050 * pre: bts_tracer_lock must be locked.
Markus Metzger5c5317d2009-01-19 10:26:53 +010051 */
Markus Metzgerba9372a2009-03-13 10:48:52 +010052static void bts_trace_init_cpu(void *arg)
Markus Metzgera93751c2008-12-11 13:53:26 +010053{
54 if (this_tracer)
55 ds_release_bts(this_tracer);
56
Markus Metzgerba9372a2009-03-13 10:48:52 +010057 this_tracer = ds_request_bts(NULL, this_buffer, BTS_BUFFER_SIZE,
58 NULL, (size_t)-1, BTS_KERNEL);
Markus Metzgera93751c2008-12-11 13:53:26 +010059 if (IS_ERR(this_tracer)) {
60 this_tracer = NULL;
61 return;
62 }
63}
64
Markus Metzgerba9372a2009-03-13 10:48:52 +010065static int bts_trace_init(struct trace_array *tr)
Markus Metzgera93751c2008-12-11 13:53:26 +010066{
Markus Metzgerba9372a2009-03-13 10:48:52 +010067 int cpu, avail;
68
Ingo Molnar2d542cf2009-02-25 08:40:09 +010069 spin_lock(&bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +010070
Markus Metzgerba9372a2009-03-13 10:48:52 +010071 hw_branch_trace = tr;
72
73 on_each_cpu(bts_trace_init_cpu, NULL, 1);
74
75 /* Check on how many cpus we could enable tracing */
76 avail = 0;
77 for_each_online_cpu(cpu)
78 if (per_cpu(tracer, cpu))
79 avail++;
80
81 trace_hw_branches_enabled = (avail ? 1 : 0);
82 trace_hw_branches_suspended = 0;
Markus Metzgera93751c2008-12-11 13:53:26 +010083
Ingo Molnar2d542cf2009-02-25 08:40:09 +010084 spin_unlock(&bts_tracer_lock);
Markus Metzgerba9372a2009-03-13 10:48:52 +010085
86
87 /* If we could not enable tracing on a single cpu, we fail. */
88 return avail ? 0 : -EOPNOTSUPP;
Markus Metzgera93751c2008-12-11 13:53:26 +010089}
90
Markus Metzger5c5317d2009-01-19 10:26:53 +010091/*
Markus Metzgerba9372a2009-03-13 10:48:52 +010092 * Release the tracer for the current cpu.
Markus Metzger5c5317d2009-01-19 10:26:53 +010093 * The argument is ignored.
94 *
Ingo Molnar2d542cf2009-02-25 08:40:09 +010095 * pre: bts_tracer_lock must be locked.
Markus Metzger5c5317d2009-01-19 10:26:53 +010096 */
Markus Metzgerba9372a2009-03-13 10:48:52 +010097static void bts_trace_release_cpu(void *arg)
Markus Metzgera93751c2008-12-11 13:53:26 +010098{
99 if (this_tracer) {
100 ds_release_bts(this_tracer);
101 this_tracer = NULL;
102 }
103}
104
Markus Metzgerba9372a2009-03-13 10:48:52 +0100105static void bts_trace_reset(struct trace_array *tr)
106{
107 spin_lock(&bts_tracer_lock);
108
109 on_each_cpu(bts_trace_release_cpu, NULL, 1);
110 trace_hw_branches_enabled = 0;
111 trace_hw_branches_suspended = 0;
112
113 spin_unlock(&bts_tracer_lock);
114}
115
116/*
117 * Resume tracing on the current cpu.
118 * The argument is ignored.
119 *
120 * pre: bts_tracer_lock must be locked.
121 */
122static void bts_trace_resume_cpu(void *arg)
123{
124 if (this_tracer)
125 ds_resume_bts(this_tracer);
126}
127
128static void bts_trace_start(struct trace_array *tr)
129{
130 spin_lock(&bts_tracer_lock);
131
132 on_each_cpu(bts_trace_resume_cpu, NULL, 1);
133 trace_hw_branches_suspended = 0;
134
135 spin_unlock(&bts_tracer_lock);
136}
137
138/*
139 * Suspend tracing on the current cpu.
140 * The argument is ignored.
141 *
142 * pre: bts_tracer_lock must be locked.
143 */
144static void bts_trace_suspend_cpu(void *arg)
145{
146 if (this_tracer)
147 ds_suspend_bts(this_tracer);
148}
149
Markus Metzgera93751c2008-12-11 13:53:26 +0100150static void bts_trace_stop(struct trace_array *tr)
151{
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100152 spin_lock(&bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +0100153
Markus Metzgerba9372a2009-03-13 10:48:52 +0100154 on_each_cpu(bts_trace_suspend_cpu, NULL, 1);
155 trace_hw_branches_suspended = 1;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100156
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100157 spin_unlock(&bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +0100158}
159
Markus Metzger5c5317d2009-01-19 10:26:53 +0100160static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
161 unsigned long action, void *hcpu)
162{
163 unsigned int cpu = (unsigned long)hcpu;
164
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100165 spin_lock(&bts_tracer_lock);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100166
167 if (!trace_hw_branches_enabled)
168 goto out;
169
170 switch (action) {
171 case CPU_ONLINE:
172 case CPU_DOWN_FAILED:
Markus Metzgerba9372a2009-03-13 10:48:52 +0100173 smp_call_function_single(cpu, bts_trace_init_cpu, NULL, 1);
174
175 if (trace_hw_branches_suspended)
176 smp_call_function_single(cpu, bts_trace_suspend_cpu,
177 NULL, 1);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100178 break;
179 case CPU_DOWN_PREPARE:
Markus Metzgerba9372a2009-03-13 10:48:52 +0100180 smp_call_function_single(cpu, bts_trace_release_cpu, NULL, 1);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100181 break;
182 }
183
184 out:
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100185 spin_unlock(&bts_tracer_lock);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100186 return NOTIFY_DONE;
187}
188
189static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
190 .notifier_call = bts_hotcpu_handler
191};
192
Markus Metzgera93751c2008-12-11 13:53:26 +0100193static void bts_trace_print_header(struct seq_file *m)
194{
Markus Metzger11edda02009-01-19 10:29:16 +0100195 seq_puts(m, "# CPU# TO <- FROM\n");
Markus Metzgera93751c2008-12-11 13:53:26 +0100196}
197
198static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
199{
Ingo Molnare9a22d12009-03-13 11:54:40 +0100200 unsigned long symflags = TRACE_ITER_SYM_OFFSET;
Markus Metzgera93751c2008-12-11 13:53:26 +0100201 struct trace_entry *entry = iter->ent;
202 struct trace_seq *seq = &iter->seq;
203 struct hw_branch_entry *it;
204
205 trace_assign_type(it, entry);
206
207 if (entry->type == TRACE_HW_BRANCHES) {
Steven Rostedt1830b522009-02-07 19:38:43 -0500208 if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
Markus Metzger11edda02009-01-19 10:29:16 +0100209 seq_print_ip_sym(seq, it->to, symflags) &&
210 trace_seq_printf(seq, "\t <- ") &&
211 seq_print_ip_sym(seq, it->from, symflags) &&
Markus Metzgera93751c2008-12-11 13:53:26 +0100212 trace_seq_printf(seq, "\n"))
213 return TRACE_TYPE_HANDLED;
214 return TRACE_TYPE_PARTIAL_LINE;;
215 }
216 return TRACE_TYPE_UNHANDLED;
217}
218
Markus Metzgerb1818742009-01-19 10:31:01 +0100219void trace_hw_branch(u64 from, u64 to)
Markus Metzgera93751c2008-12-11 13:53:26 +0100220{
Markus Metzgerb1818742009-01-19 10:31:01 +0100221 struct trace_array *tr = hw_branch_trace;
Markus Metzgera93751c2008-12-11 13:53:26 +0100222 struct ring_buffer_event *event;
223 struct hw_branch_entry *entry;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200224 unsigned long irq1;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100225 int cpu;
Markus Metzgera93751c2008-12-11 13:53:26 +0100226
Markus Metzger5c5317d2009-01-19 10:26:53 +0100227 if (unlikely(!tr))
Markus Metzgera93751c2008-12-11 13:53:26 +0100228 return;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100229
230 if (unlikely(!trace_hw_branches_enabled))
231 return;
232
233 local_irq_save(irq1);
234 cpu = raw_smp_processor_id();
235 if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
236 goto out;
237
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200238 event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
239 sizeof(*entry), 0, 0);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100240 if (!event)
241 goto out;
Markus Metzgera93751c2008-12-11 13:53:26 +0100242 entry = ring_buffer_event_data(event);
243 tracing_generic_entry_update(&entry->ent, 0, from);
244 entry->ent.type = TRACE_HW_BRANCHES;
Markus Metzgera93751c2008-12-11 13:53:26 +0100245 entry->from = from;
246 entry->to = to;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200247 trace_buffer_unlock_commit(tr, event, 0, 0);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100248
249 out:
250 atomic_dec(&tr->data[cpu]->disabled);
251 local_irq_restore(irq1);
Markus Metzgera93751c2008-12-11 13:53:26 +0100252}
253
Markus Metzgerb1818742009-01-19 10:31:01 +0100254static void trace_bts_at(const struct bts_trace *trace, void *at)
Markus Metzgera93751c2008-12-11 13:53:26 +0100255{
256 struct bts_struct bts;
257 int err = 0;
258
259 WARN_ON_ONCE(!trace->read);
260 if (!trace->read)
261 return;
262
263 err = trace->read(this_tracer, at, &bts);
264 if (err < 0)
265 return;
266
267 switch (bts.qualifier) {
268 case BTS_BRANCH:
Markus Metzgerb1818742009-01-19 10:31:01 +0100269 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
Markus Metzgera93751c2008-12-11 13:53:26 +0100270 break;
271 }
272}
273
Markus Metzger5c5317d2009-01-19 10:26:53 +0100274/*
275 * Collect the trace on the current cpu and write it into the ftrace buffer.
276 *
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100277 * pre: bts_tracer_lock must be locked
Markus Metzger5c5317d2009-01-19 10:26:53 +0100278 */
Markus Metzgera93751c2008-12-11 13:53:26 +0100279static void trace_bts_cpu(void *arg)
280{
Markus Metzgerba9372a2009-03-13 10:48:52 +0100281 struct trace_array *tr = (struct trace_array *)arg;
Markus Metzgera93751c2008-12-11 13:53:26 +0100282 const struct bts_trace *trace;
283 unsigned char *at;
284
Markus Metzgerb1818742009-01-19 10:31:01 +0100285 if (unlikely(!tr))
Markus Metzgera93751c2008-12-11 13:53:26 +0100286 return;
287
Markus Metzger5c5317d2009-01-19 10:26:53 +0100288 if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
289 return;
290
Markus Metzgerb1818742009-01-19 10:31:01 +0100291 if (unlikely(!this_tracer))
292 return;
293
Markus Metzgera93751c2008-12-11 13:53:26 +0100294 ds_suspend_bts(this_tracer);
295 trace = ds_read_bts(this_tracer);
296 if (!trace)
297 goto out;
298
299 for (at = trace->ds.top; (void *)at < trace->ds.end;
300 at += trace->ds.size)
Markus Metzgerb1818742009-01-19 10:31:01 +0100301 trace_bts_at(trace, at);
Markus Metzgera93751c2008-12-11 13:53:26 +0100302
303 for (at = trace->ds.begin; (void *)at < trace->ds.top;
304 at += trace->ds.size)
Markus Metzgerb1818742009-01-19 10:31:01 +0100305 trace_bts_at(trace, at);
Markus Metzgera93751c2008-12-11 13:53:26 +0100306
307out:
308 ds_resume_bts(this_tracer);
309}
310
311static void trace_bts_prepare(struct trace_iterator *iter)
312{
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100313 spin_lock(&bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +0100314
Markus Metzger5c5317d2009-01-19 10:26:53 +0100315 on_each_cpu(trace_bts_cpu, iter->tr, 1);
316
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100317 spin_unlock(&bts_tracer_lock);
Markus Metzgera93751c2008-12-11 13:53:26 +0100318}
319
Markus Metzgere23b8ad2009-01-19 10:33:31 +0100320static void trace_bts_close(struct trace_iterator *iter)
321{
322 tracing_reset_online_cpus(iter->tr);
323}
324
Markus Metzgerb1818742009-01-19 10:31:01 +0100325void trace_hw_branch_oops(void)
326{
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100327 spin_lock(&bts_tracer_lock);
Markus Metzgerb1818742009-01-19 10:31:01 +0100328
Markus Metzgerba9372a2009-03-13 10:48:52 +0100329 if (trace_hw_branches_enabled)
330 trace_bts_cpu(hw_branch_trace);
Markus Metzgerb1818742009-01-19 10:31:01 +0100331
Ingo Molnar2d542cf2009-02-25 08:40:09 +0100332 spin_unlock(&bts_tracer_lock);
Markus Metzgerb1818742009-01-19 10:31:01 +0100333}
334
Markus Metzgera93751c2008-12-11 13:53:26 +0100335struct tracer bts_tracer __read_mostly =
336{
337 .name = "hw-branch-tracer",
338 .init = bts_trace_init,
Markus Metzger5c5317d2009-01-19 10:26:53 +0100339 .reset = bts_trace_reset,
Markus Metzgera93751c2008-12-11 13:53:26 +0100340 .print_header = bts_trace_print_header,
341 .print_line = bts_trace_print_line,
342 .start = bts_trace_start,
343 .stop = bts_trace_stop,
Markus Metzgere23b8ad2009-01-19 10:33:31 +0100344 .open = trace_bts_prepare,
Markus Metzger321bb5e2009-03-13 10:50:27 +0100345 .close = trace_bts_close,
346#ifdef CONFIG_FTRACE_SELFTEST
347 .selftest = trace_selftest_startup_hw_branches,
348#endif /* CONFIG_FTRACE_SELFTEST */
Markus Metzgera93751c2008-12-11 13:53:26 +0100349};
350
351__init static int init_bts_trace(void)
352{
Markus Metzger5e01cb62009-02-24 13:55:18 +0100353 register_hotcpu_notifier(&bts_hotcpu_notifier);
Markus Metzgera93751c2008-12-11 13:53:26 +0100354 return register_tracer(&bts_tracer);
355}
356device_initcall(init_bts_trace);