blob: 50565d8cd2ed9f79e937775886020ff91db3ee06 [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/kallsyms.h>
Markus Metzgera93751c2008-12-11 13:53:26 +01008#include <linux/debugfs.h>
9#include <linux/ftrace.h>
Ingo Molnar2d542cf2009-02-25 08:40:09 +010010#include <linux/module.h>
Markus Metzger5c5317d2009-01-19 10:26:53 +010011#include <linux/cpu.h>
12#include <linux/smp.h>
Ingo Molnar2d542cf2009-02-25 08:40:09 +010013#include <linux/fs.h>
Markus Metzgera93751c2008-12-11 13:53:26 +010014
15#include <asm/ds.h>
16
Steven Rostedtf0868d12008-12-23 23:24:12 -050017#include "trace_output.h"
Ingo Molnare9a22d12009-03-13 11:54:40 +010018#include "trace.h"
Markus Metzgera93751c2008-12-11 13:53:26 +010019
20
Markus Metzgerba9372a2009-03-13 10:48:52 +010021#define BTS_BUFFER_SIZE (1 << 13)
Markus Metzgera93751c2008-12-11 13:53:26 +010022
23static DEFINE_PER_CPU(struct bts_tracer *, tracer);
Markus Metzgerba9372a2009-03-13 10:48:52 +010024static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
Markus Metzgera93751c2008-12-11 13:53:26 +010025
26#define this_tracer per_cpu(tracer, smp_processor_id())
Markus Metzgera93751c2008-12-11 13:53:26 +010027
Markus Metzgerba9372a2009-03-13 10:48:52 +010028static int trace_hw_branches_enabled __read_mostly;
29static int trace_hw_branches_suspended __read_mostly;
Markus Metzgerb1818742009-01-19 10:31:01 +010030static struct trace_array *hw_branch_trace __read_mostly;
Markus Metzgera93751c2008-12-11 13:53:26 +010031
Markus Metzger5c5317d2009-01-19 10:26:53 +010032
Markus Metzgerde79f542009-04-03 16:43:40 +020033static void bts_trace_init_cpu(int cpu)
Markus Metzgera93751c2008-12-11 13:53:26 +010034{
Markus Metzgerde79f542009-04-03 16:43:40 +020035 per_cpu(tracer, cpu) =
36 ds_request_bts_cpu(cpu, per_cpu(buffer, cpu), BTS_BUFFER_SIZE,
37 NULL, (size_t)-1, BTS_KERNEL);
Markus Metzgera93751c2008-12-11 13:53:26 +010038
Markus Metzgerde79f542009-04-03 16:43:40 +020039 if (IS_ERR(per_cpu(tracer, cpu)))
40 per_cpu(tracer, cpu) = NULL;
Markus Metzgera93751c2008-12-11 13:53:26 +010041}
42
Markus Metzgerba9372a2009-03-13 10:48:52 +010043static int bts_trace_init(struct trace_array *tr)
Markus Metzgera93751c2008-12-11 13:53:26 +010044{
Markus Metzgerde79f542009-04-03 16:43:40 +020045 int cpu;
Markus Metzgera93751c2008-12-11 13:53:26 +010046
Markus Metzgerba9372a2009-03-13 10:48:52 +010047 hw_branch_trace = tr;
Markus Metzgerde79f542009-04-03 16:43:40 +020048 trace_hw_branches_enabled = 0;
Markus Metzgerba9372a2009-03-13 10:48:52 +010049
Markus Metzgerde79f542009-04-03 16:43:40 +020050 get_online_cpus();
51 for_each_online_cpu(cpu) {
52 bts_trace_init_cpu(cpu);
Markus Metzgerba9372a2009-03-13 10:48:52 +010053
Markus Metzgerde79f542009-04-03 16:43:40 +020054 if (likely(per_cpu(tracer, cpu)))
55 trace_hw_branches_enabled = 1;
56 }
Markus Metzgerba9372a2009-03-13 10:48:52 +010057 trace_hw_branches_suspended = 0;
Markus Metzgerde79f542009-04-03 16:43:40 +020058 put_online_cpus();
Markus Metzgerba9372a2009-03-13 10:48:52 +010059
60 /* If we could not enable tracing on a single cpu, we fail. */
Markus Metzgerde79f542009-04-03 16:43:40 +020061 return trace_hw_branches_enabled ? 0 : -EOPNOTSUPP;
Markus Metzgera93751c2008-12-11 13:53:26 +010062}
63
Markus Metzgerba9372a2009-03-13 10:48:52 +010064static void bts_trace_reset(struct trace_array *tr)
65{
Markus Metzgerde79f542009-04-03 16:43:40 +020066 int cpu;
Markus Metzgerba9372a2009-03-13 10:48:52 +010067
Markus Metzgerde79f542009-04-03 16:43:40 +020068 get_online_cpus();
69 for_each_online_cpu(cpu) {
70 if (likely(per_cpu(tracer, cpu))) {
71 ds_release_bts(per_cpu(tracer, cpu));
72 per_cpu(tracer, cpu) = NULL;
73 }
74 }
Markus Metzgerba9372a2009-03-13 10:48:52 +010075 trace_hw_branches_enabled = 0;
76 trace_hw_branches_suspended = 0;
Markus Metzgerde79f542009-04-03 16:43:40 +020077 put_online_cpus();
Markus Metzgerba9372a2009-03-13 10:48:52 +010078}
79
80static void bts_trace_start(struct trace_array *tr)
81{
Markus Metzgerde79f542009-04-03 16:43:40 +020082 int cpu;
Markus Metzgerba9372a2009-03-13 10:48:52 +010083
Markus Metzgerde79f542009-04-03 16:43:40 +020084 get_online_cpus();
85 for_each_online_cpu(cpu)
86 if (likely(per_cpu(tracer, cpu)))
87 ds_resume_bts(per_cpu(tracer, cpu));
Markus Metzgerba9372a2009-03-13 10:48:52 +010088 trace_hw_branches_suspended = 0;
Markus Metzgerde79f542009-04-03 16:43:40 +020089 put_online_cpus();
Markus Metzgerba9372a2009-03-13 10:48:52 +010090}
91
Markus Metzgera93751c2008-12-11 13:53:26 +010092static void bts_trace_stop(struct trace_array *tr)
93{
Markus Metzgerde79f542009-04-03 16:43:40 +020094 int cpu;
Markus Metzgera93751c2008-12-11 13:53:26 +010095
Markus Metzgerde79f542009-04-03 16:43:40 +020096 get_online_cpus();
97 for_each_online_cpu(cpu)
98 if (likely(per_cpu(tracer, cpu)))
99 ds_suspend_bts(per_cpu(tracer, cpu));
Markus Metzgerba9372a2009-03-13 10:48:52 +0100100 trace_hw_branches_suspended = 1;
Markus Metzgerde79f542009-04-03 16:43:40 +0200101 put_online_cpus();
Markus Metzgera93751c2008-12-11 13:53:26 +0100102}
103
Markus Metzger5c5317d2009-01-19 10:26:53 +0100104static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
105 unsigned long action, void *hcpu)
106{
Markus Metzgerde79f542009-04-03 16:43:40 +0200107 int cpu = (long)hcpu;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100108
109 switch (action) {
110 case CPU_ONLINE:
111 case CPU_DOWN_FAILED:
Markus Metzgerde79f542009-04-03 16:43:40 +0200112 /* The notification is sent with interrupts enabled. */
113 if (trace_hw_branches_enabled) {
114 bts_trace_init_cpu(cpu);
Markus Metzgerba9372a2009-03-13 10:48:52 +0100115
Markus Metzgerde79f542009-04-03 16:43:40 +0200116 if (trace_hw_branches_suspended &&
117 likely(per_cpu(tracer, cpu)))
118 ds_suspend_bts(per_cpu(tracer, cpu));
119 }
Markus Metzger5c5317d2009-01-19 10:26:53 +0100120 break;
Markus Metzgerde79f542009-04-03 16:43:40 +0200121
Markus Metzger5c5317d2009-01-19 10:26:53 +0100122 case CPU_DOWN_PREPARE:
Markus Metzgerde79f542009-04-03 16:43:40 +0200123 /* The notification is sent with interrupts enabled. */
124 if (likely(per_cpu(tracer, cpu))) {
125 ds_release_bts(per_cpu(tracer, cpu));
126 per_cpu(tracer, cpu) = NULL;
127 }
Markus Metzger5c5317d2009-01-19 10:26:53 +0100128 }
129
Markus Metzger5c5317d2009-01-19 10:26:53 +0100130 return NOTIFY_DONE;
131}
132
133static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
134 .notifier_call = bts_hotcpu_handler
135};
136
Markus Metzgera93751c2008-12-11 13:53:26 +0100137static void bts_trace_print_header(struct seq_file *m)
138{
Markus Metzger11edda02009-01-19 10:29:16 +0100139 seq_puts(m, "# CPU# TO <- FROM\n");
Markus Metzgera93751c2008-12-11 13:53:26 +0100140}
141
142static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
143{
Ingo Molnare9a22d12009-03-13 11:54:40 +0100144 unsigned long symflags = TRACE_ITER_SYM_OFFSET;
Markus Metzgera93751c2008-12-11 13:53:26 +0100145 struct trace_entry *entry = iter->ent;
146 struct trace_seq *seq = &iter->seq;
147 struct hw_branch_entry *it;
148
149 trace_assign_type(it, entry);
150
151 if (entry->type == TRACE_HW_BRANCHES) {
Steven Rostedt1830b522009-02-07 19:38:43 -0500152 if (trace_seq_printf(seq, "%4d ", iter->cpu) &&
Markus Metzger11edda02009-01-19 10:29:16 +0100153 seq_print_ip_sym(seq, it->to, symflags) &&
154 trace_seq_printf(seq, "\t <- ") &&
155 seq_print_ip_sym(seq, it->from, symflags) &&
Markus Metzgera93751c2008-12-11 13:53:26 +0100156 trace_seq_printf(seq, "\n"))
157 return TRACE_TYPE_HANDLED;
158 return TRACE_TYPE_PARTIAL_LINE;;
159 }
160 return TRACE_TYPE_UNHANDLED;
161}
162
Markus Metzgerb1818742009-01-19 10:31:01 +0100163void trace_hw_branch(u64 from, u64 to)
Markus Metzgera93751c2008-12-11 13:53:26 +0100164{
Markus Metzgerb1818742009-01-19 10:31:01 +0100165 struct trace_array *tr = hw_branch_trace;
Markus Metzgera93751c2008-12-11 13:53:26 +0100166 struct ring_buffer_event *event;
167 struct hw_branch_entry *entry;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200168 unsigned long irq1;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100169 int cpu;
Markus Metzgera93751c2008-12-11 13:53:26 +0100170
Markus Metzger5c5317d2009-01-19 10:26:53 +0100171 if (unlikely(!tr))
Markus Metzgera93751c2008-12-11 13:53:26 +0100172 return;
Markus Metzger5c5317d2009-01-19 10:26:53 +0100173
174 if (unlikely(!trace_hw_branches_enabled))
175 return;
176
177 local_irq_save(irq1);
178 cpu = raw_smp_processor_id();
179 if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
180 goto out;
181
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200182 event = trace_buffer_lock_reserve(tr, TRACE_HW_BRANCHES,
183 sizeof(*entry), 0, 0);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100184 if (!event)
185 goto out;
Markus Metzgera93751c2008-12-11 13:53:26 +0100186 entry = ring_buffer_event_data(event);
187 tracing_generic_entry_update(&entry->ent, 0, from);
188 entry->ent.type = TRACE_HW_BRANCHES;
Markus Metzgera93751c2008-12-11 13:53:26 +0100189 entry->from = from;
190 entry->to = to;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200191 trace_buffer_unlock_commit(tr, event, 0, 0);
Markus Metzger5c5317d2009-01-19 10:26:53 +0100192
193 out:
194 atomic_dec(&tr->data[cpu]->disabled);
195 local_irq_restore(irq1);
Markus Metzgera93751c2008-12-11 13:53:26 +0100196}
197
Markus Metzgerb1818742009-01-19 10:31:01 +0100198static void trace_bts_at(const struct bts_trace *trace, void *at)
Markus Metzgera93751c2008-12-11 13:53:26 +0100199{
200 struct bts_struct bts;
201 int err = 0;
202
203 WARN_ON_ONCE(!trace->read);
204 if (!trace->read)
205 return;
206
207 err = trace->read(this_tracer, at, &bts);
208 if (err < 0)
209 return;
210
211 switch (bts.qualifier) {
212 case BTS_BRANCH:
Markus Metzgerb1818742009-01-19 10:31:01 +0100213 trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
Markus Metzgera93751c2008-12-11 13:53:26 +0100214 break;
215 }
216}
217
Markus Metzger5c5317d2009-01-19 10:26:53 +0100218/*
219 * Collect the trace on the current cpu and write it into the ftrace buffer.
220 *
Markus Metzgerde79f542009-04-03 16:43:40 +0200221 * pre: tracing must be suspended on the current cpu
Markus Metzger5c5317d2009-01-19 10:26:53 +0100222 */
Markus Metzgera93751c2008-12-11 13:53:26 +0100223static void trace_bts_cpu(void *arg)
224{
Markus Metzgerba9372a2009-03-13 10:48:52 +0100225 struct trace_array *tr = (struct trace_array *)arg;
Markus Metzgera93751c2008-12-11 13:53:26 +0100226 const struct bts_trace *trace;
227 unsigned char *at;
228
Markus Metzgerb1818742009-01-19 10:31:01 +0100229 if (unlikely(!tr))
Markus Metzgera93751c2008-12-11 13:53:26 +0100230 return;
231
Markus Metzger5c5317d2009-01-19 10:26:53 +0100232 if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
233 return;
234
Markus Metzgerb1818742009-01-19 10:31:01 +0100235 if (unlikely(!this_tracer))
236 return;
237
Markus Metzgera93751c2008-12-11 13:53:26 +0100238 trace = ds_read_bts(this_tracer);
239 if (!trace)
Markus Metzgerde79f542009-04-03 16:43:40 +0200240 return;
Markus Metzgera93751c2008-12-11 13:53:26 +0100241
242 for (at = trace->ds.top; (void *)at < trace->ds.end;
243 at += trace->ds.size)
Markus Metzgerb1818742009-01-19 10:31:01 +0100244 trace_bts_at(trace, at);
Markus Metzgera93751c2008-12-11 13:53:26 +0100245
246 for (at = trace->ds.begin; (void *)at < trace->ds.top;
247 at += trace->ds.size)
Markus Metzgerb1818742009-01-19 10:31:01 +0100248 trace_bts_at(trace, at);
Markus Metzgera93751c2008-12-11 13:53:26 +0100249}
250
251static void trace_bts_prepare(struct trace_iterator *iter)
252{
Markus Metzgerde79f542009-04-03 16:43:40 +0200253 int cpu;
Markus Metzgera93751c2008-12-11 13:53:26 +0100254
Markus Metzgerde79f542009-04-03 16:43:40 +0200255 get_online_cpus();
256 for_each_online_cpu(cpu)
257 if (likely(per_cpu(tracer, cpu)))
258 ds_suspend_bts(per_cpu(tracer, cpu));
259 /*
260 * We need to collect the trace on the respective cpu since ftrace
261 * implicitly adds the record for the current cpu.
262 * Once that is more flexible, we could collect the data from any cpu.
263 */
Markus Metzger5c5317d2009-01-19 10:26:53 +0100264 on_each_cpu(trace_bts_cpu, iter->tr, 1);
265
Markus Metzgerde79f542009-04-03 16:43:40 +0200266 for_each_online_cpu(cpu)
267 if (likely(per_cpu(tracer, cpu)))
268 ds_resume_bts(per_cpu(tracer, cpu));
269 put_online_cpus();
Markus Metzgera93751c2008-12-11 13:53:26 +0100270}
271
Markus Metzgere23b8ad2009-01-19 10:33:31 +0100272static void trace_bts_close(struct trace_iterator *iter)
273{
274 tracing_reset_online_cpus(iter->tr);
275}
276
Markus Metzgerb1818742009-01-19 10:31:01 +0100277void trace_hw_branch_oops(void)
278{
Markus Metzgerde79f542009-04-03 16:43:40 +0200279 if (this_tracer) {
280 ds_suspend_bts_noirq(this_tracer);
Markus Metzgerba9372a2009-03-13 10:48:52 +0100281 trace_bts_cpu(hw_branch_trace);
Markus Metzgerde79f542009-04-03 16:43:40 +0200282 ds_resume_bts_noirq(this_tracer);
283 }
Markus Metzgerb1818742009-01-19 10:31:01 +0100284}
285
Markus Metzgera93751c2008-12-11 13:53:26 +0100286struct tracer bts_tracer __read_mostly =
287{
288 .name = "hw-branch-tracer",
289 .init = bts_trace_init,
Markus Metzger5c5317d2009-01-19 10:26:53 +0100290 .reset = bts_trace_reset,
Markus Metzgera93751c2008-12-11 13:53:26 +0100291 .print_header = bts_trace_print_header,
292 .print_line = bts_trace_print_line,
293 .start = bts_trace_start,
294 .stop = bts_trace_stop,
Markus Metzgere23b8ad2009-01-19 10:33:31 +0100295 .open = trace_bts_prepare,
Markus Metzger321bb5e2009-03-13 10:50:27 +0100296 .close = trace_bts_close,
297#ifdef CONFIG_FTRACE_SELFTEST
298 .selftest = trace_selftest_startup_hw_branches,
299#endif /* CONFIG_FTRACE_SELFTEST */
Markus Metzgera93751c2008-12-11 13:53:26 +0100300};
301
302__init static int init_bts_trace(void)
303{
Markus Metzger5e01cb62009-02-24 13:55:18 +0100304 register_hotcpu_notifier(&bts_hotcpu_notifier);
Markus Metzgera93751c2008-12-11 13:53:26 +0100305 return register_tracer(&bts_tracer);
306}
307device_initcall(init_bts_trace);