2008-11-25 03:24:15 -05:00
|
|
|
/*
|
2008-12-11 07:53:26 -05:00
|
|
|
* h/w branch tracer for x86 based on bts
|
2008-11-25 03:24:15 -05:00
|
|
|
*
|
2009-01-19 04:26:53 -05:00
|
|
|
* Copyright (C) 2008-2009 Intel Corporation.
|
|
|
|
* Markus Metzger <markus.t.metzger@gmail.com>, 2008-2009
|
2008-11-25 03:24:15 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/ftrace.h>
|
|
|
|
#include <linux/kallsyms.h>
|
2009-01-19 04:26:53 -05:00
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/smp.h>
|
2008-11-25 03:24:15 -05:00
|
|
|
|
|
|
|
#include <asm/ds.h>
|
|
|
|
|
|
|
|
#include "trace.h"
|
2008-12-23 23:24:12 -05:00
|
|
|
#include "trace_output.h"
|
2008-11-25 03:24:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
#define SIZEOF_BTS (1 << 13)
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
/* The tracer mutex protects the below per-cpu tracer array.
|
|
|
|
It needs to be held to:
|
|
|
|
- start tracing on all cpus
|
|
|
|
- stop tracing on all cpus
|
|
|
|
- start tracing on a single hotplug cpu
|
|
|
|
- stop tracing on a single hotplug cpu
|
|
|
|
- read the trace from all cpus
|
|
|
|
- read the trace from a single cpu
|
|
|
|
*/
|
|
|
|
static DEFINE_MUTEX(bts_tracer_mutex);
|
2008-11-25 03:24:15 -05:00
|
|
|
static DEFINE_PER_CPU(struct bts_tracer *, tracer);
|
|
|
|
static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
|
|
|
|
|
|
|
|
#define this_tracer per_cpu(tracer, smp_processor_id())
|
|
|
|
#define this_buffer per_cpu(buffer, smp_processor_id())
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
static int __read_mostly trace_hw_branches_enabled;
|
2009-01-19 04:31:01 -05:00
|
|
|
static struct trace_array *hw_branch_trace __read_mostly;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start tracing on the current cpu.
|
|
|
|
* The argument is ignored.
|
|
|
|
*
|
|
|
|
* pre: bts_tracer_mutex must be locked.
|
|
|
|
*/
|
2008-11-25 03:24:15 -05:00
|
|
|
static void bts_trace_start_cpu(void *arg)
|
|
|
|
{
|
2008-12-11 07:53:26 -05:00
|
|
|
if (this_tracer)
|
|
|
|
ds_release_bts(this_tracer);
|
|
|
|
|
2008-11-25 03:24:15 -05:00
|
|
|
this_tracer =
|
|
|
|
ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
|
2008-12-11 07:53:26 -05:00
|
|
|
/* ovfl = */ NULL, /* th = */ (size_t)-1,
|
|
|
|
BTS_KERNEL);
|
2008-11-25 03:24:15 -05:00
|
|
|
if (IS_ERR(this_tracer)) {
|
|
|
|
this_tracer = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bts_trace_start(struct trace_array *tr)
|
|
|
|
{
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_lock(&bts_tracer_mutex);
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
on_each_cpu(bts_trace_start_cpu, NULL, 1);
|
|
|
|
trace_hw_branches_enabled = 1;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_unlock(&bts_tracer_mutex);
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
/*
|
|
|
|
* Start tracing on the current cpu.
|
|
|
|
* The argument is ignored.
|
|
|
|
*
|
|
|
|
* pre: bts_tracer_mutex must be locked.
|
|
|
|
*/
|
2008-11-25 03:24:15 -05:00
|
|
|
static void bts_trace_stop_cpu(void *arg)
|
|
|
|
{
|
|
|
|
if (this_tracer) {
|
|
|
|
ds_release_bts(this_tracer);
|
|
|
|
this_tracer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bts_trace_stop(struct trace_array *tr)
|
|
|
|
{
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_lock(&bts_tracer_mutex);
|
|
|
|
|
|
|
|
trace_hw_branches_enabled = 0;
|
|
|
|
on_each_cpu(bts_trace_stop_cpu, NULL, 1);
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_unlock(&bts_tracer_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
|
|
|
|
unsigned long action, void *hcpu)
|
|
|
|
{
|
|
|
|
unsigned int cpu = (unsigned long)hcpu;
|
|
|
|
|
|
|
|
mutex_lock(&bts_tracer_mutex);
|
|
|
|
|
|
|
|
if (!trace_hw_branches_enabled)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case CPU_ONLINE:
|
|
|
|
case CPU_DOWN_FAILED:
|
|
|
|
smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
|
|
|
|
break;
|
|
|
|
case CPU_DOWN_PREPARE:
|
2008-11-25 03:24:15 -05:00
|
|
|
smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
|
2009-01-19 04:26:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&bts_tracer_mutex);
|
|
|
|
return NOTIFY_DONE;
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
|
|
|
|
.notifier_call = bts_hotcpu_handler
|
|
|
|
};
|
|
|
|
|
2008-11-25 03:24:15 -05:00
|
|
|
static int bts_trace_init(struct trace_array *tr)
|
|
|
|
{
|
2009-01-19 04:31:01 -05:00
|
|
|
hw_branch_trace = tr;
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
register_hotcpu_notifier(&bts_hotcpu_notifier);
|
2008-12-19 05:08:39 -05:00
|
|
|
tracing_reset_online_cpus(tr);
|
2008-11-25 03:24:15 -05:00
|
|
|
bts_trace_start(tr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
static void bts_trace_reset(struct trace_array *tr)
|
|
|
|
{
|
|
|
|
bts_trace_stop(tr);
|
|
|
|
unregister_hotcpu_notifier(&bts_hotcpu_notifier);
|
|
|
|
}
|
|
|
|
|
2008-11-25 03:24:15 -05:00
|
|
|
static void bts_trace_print_header(struct seq_file *m)
|
|
|
|
{
|
2009-01-19 04:29:16 -05:00
|
|
|
seq_puts(m, "# CPU# TO <- FROM\n");
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum print_line_t bts_trace_print_line(struct trace_iterator *iter)
|
|
|
|
{
|
|
|
|
struct trace_entry *entry = iter->ent;
|
|
|
|
struct trace_seq *seq = &iter->seq;
|
2008-12-11 07:53:26 -05:00
|
|
|
struct hw_branch_entry *it;
|
2009-01-19 04:29:16 -05:00
|
|
|
unsigned long symflags = TRACE_ITER_SYM_OFFSET;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
|
|
|
trace_assign_type(it, entry);
|
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
if (entry->type == TRACE_HW_BRANCHES) {
|
|
|
|
if (trace_seq_printf(seq, "%4d ", entry->cpu) &&
|
2009-01-19 04:29:16 -05:00
|
|
|
seq_print_ip_sym(seq, it->to, symflags) &&
|
|
|
|
trace_seq_printf(seq, "\t <- ") &&
|
|
|
|
seq_print_ip_sym(seq, it->from, symflags) &&
|
2008-12-11 07:53:26 -05:00
|
|
|
trace_seq_printf(seq, "\n"))
|
|
|
|
return TRACE_TYPE_HANDLED;
|
|
|
|
return TRACE_TYPE_PARTIAL_LINE;;
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
return TRACE_TYPE_UNHANDLED;
|
|
|
|
}
|
|
|
|
|
2009-01-19 04:31:01 -05:00
|
|
|
void trace_hw_branch(u64 from, u64 to)
|
2008-11-25 03:24:15 -05:00
|
|
|
{
|
2009-01-19 04:31:01 -05:00
|
|
|
struct trace_array *tr = hw_branch_trace;
|
2008-11-25 03:24:15 -05:00
|
|
|
struct ring_buffer_event *event;
|
2008-12-11 07:53:26 -05:00
|
|
|
struct hw_branch_entry *entry;
|
2009-01-19 04:26:53 -05:00
|
|
|
unsigned long irq1, irq2;
|
|
|
|
int cpu;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
if (unlikely(!tr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (unlikely(!trace_hw_branches_enabled))
|
2008-11-25 03:24:15 -05:00
|
|
|
return;
|
2009-01-19 04:26:53 -05:00
|
|
|
|
|
|
|
local_irq_save(irq1);
|
|
|
|
cpu = raw_smp_processor_id();
|
|
|
|
if (atomic_inc_return(&tr->data[cpu]->disabled) != 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry), &irq2);
|
|
|
|
if (!event)
|
|
|
|
goto out;
|
2008-11-25 03:24:15 -05:00
|
|
|
entry = ring_buffer_event_data(event);
|
|
|
|
tracing_generic_entry_update(&entry->ent, 0, from);
|
2008-12-11 07:53:26 -05:00
|
|
|
entry->ent.type = TRACE_HW_BRANCHES;
|
2009-01-19 04:26:53 -05:00
|
|
|
entry->ent.cpu = cpu;
|
2008-11-25 03:24:15 -05:00
|
|
|
entry->from = from;
|
|
|
|
entry->to = to;
|
2009-01-19 04:26:53 -05:00
|
|
|
ring_buffer_unlock_commit(tr->buffer, event, irq2);
|
|
|
|
|
|
|
|
out:
|
|
|
|
atomic_dec(&tr->data[cpu]->disabled);
|
|
|
|
local_irq_restore(irq1);
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:31:01 -05:00
|
|
|
static void trace_bts_at(const struct bts_trace *trace, void *at)
|
2008-11-25 03:24:15 -05:00
|
|
|
{
|
2008-12-11 07:53:26 -05:00
|
|
|
struct bts_struct bts;
|
|
|
|
int err = 0;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
WARN_ON_ONCE(!trace->read);
|
|
|
|
if (!trace->read)
|
2008-11-25 03:24:15 -05:00
|
|
|
return;
|
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
err = trace->read(this_tracer, at, &bts);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
switch (bts.qualifier) {
|
|
|
|
case BTS_BRANCH:
|
2009-01-19 04:31:01 -05:00
|
|
|
trace_hw_branch(bts.variant.lbr.from, bts.variant.lbr.to);
|
2008-12-11 07:53:26 -05:00
|
|
|
break;
|
|
|
|
}
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
/*
|
|
|
|
* Collect the trace on the current cpu and write it into the ftrace buffer.
|
|
|
|
*
|
|
|
|
* pre: bts_tracer_mutex must be locked
|
|
|
|
*/
|
2008-11-25 03:24:15 -05:00
|
|
|
static void trace_bts_cpu(void *arg)
|
|
|
|
{
|
|
|
|
struct trace_array *tr = (struct trace_array *) arg;
|
2008-12-11 07:53:26 -05:00
|
|
|
const struct bts_trace *trace;
|
|
|
|
unsigned char *at;
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:31:01 -05:00
|
|
|
if (unlikely(!tr))
|
2008-11-25 03:24:15 -05:00
|
|
|
return;
|
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
if (unlikely(atomic_read(&tr->data[raw_smp_processor_id()]->disabled)))
|
|
|
|
return;
|
|
|
|
|
2009-01-19 04:31:01 -05:00
|
|
|
if (unlikely(!this_tracer))
|
|
|
|
return;
|
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
ds_suspend_bts(this_tracer);
|
|
|
|
trace = ds_read_bts(this_tracer);
|
|
|
|
if (!trace)
|
2008-11-25 03:24:15 -05:00
|
|
|
goto out;
|
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
for (at = trace->ds.top; (void *)at < trace->ds.end;
|
|
|
|
at += trace->ds.size)
|
2009-01-19 04:31:01 -05:00
|
|
|
trace_bts_at(trace, at);
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2008-12-11 07:53:26 -05:00
|
|
|
for (at = trace->ds.begin; (void *)at < trace->ds.top;
|
|
|
|
at += trace->ds.size)
|
2009-01-19 04:31:01 -05:00
|
|
|
trace_bts_at(trace, at);
|
2008-11-25 03:24:15 -05:00
|
|
|
|
|
|
|
out:
|
2008-12-11 07:53:26 -05:00
|
|
|
ds_resume_bts(this_tracer);
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void trace_bts_prepare(struct trace_iterator *iter)
|
|
|
|
{
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_lock(&bts_tracer_mutex);
|
|
|
|
|
|
|
|
on_each_cpu(trace_bts_cpu, iter->tr, 1);
|
2008-11-25 03:24:15 -05:00
|
|
|
|
2009-01-19 04:26:53 -05:00
|
|
|
mutex_unlock(&bts_tracer_mutex);
|
2008-11-25 03:24:15 -05:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:33:31 -05:00
|
|
|
static void trace_bts_close(struct trace_iterator *iter)
|
|
|
|
{
|
|
|
|
tracing_reset_online_cpus(iter->tr);
|
|
|
|
}
|
|
|
|
|
2009-01-19 04:31:01 -05:00
|
|
|
void trace_hw_branch_oops(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&bts_tracer_mutex);
|
|
|
|
|
|
|
|
trace_bts_cpu(hw_branch_trace);
|
|
|
|
|
|
|
|
mutex_unlock(&bts_tracer_mutex);
|
|
|
|
}
|
|
|
|
|
2008-11-25 03:24:15 -05:00
|
|
|
struct tracer bts_tracer __read_mostly =
|
|
|
|
{
|
2008-12-11 07:53:26 -05:00
|
|
|
.name = "hw-branch-tracer",
|
2008-11-25 03:24:15 -05:00
|
|
|
.init = bts_trace_init,
|
2009-01-19 04:26:53 -05:00
|
|
|
.reset = bts_trace_reset,
|
2008-11-25 03:24:15 -05:00
|
|
|
.print_header = bts_trace_print_header,
|
|
|
|
.print_line = bts_trace_print_line,
|
|
|
|
.start = bts_trace_start,
|
|
|
|
.stop = bts_trace_stop,
|
2009-01-19 04:33:31 -05:00
|
|
|
.open = trace_bts_prepare,
|
|
|
|
.close = trace_bts_close
|
2008-11-25 03:24:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
__init static int init_bts_trace(void)
|
|
|
|
{
|
|
|
|
return register_tracer(&bts_tracer);
|
|
|
|
}
|
|
|
|
device_initcall(init_bts_trace);
|