4 * Copyright (C) 2008 Markus Metzger <markus.t.metzger@gmail.com>
8 #include <linux/module.h>
10 #include <linux/debugfs.h>
11 #include <linux/ftrace.h>
12 #include <linux/kallsyms.h>
19 #define SIZEOF_BTS (1 << 13)
21 static DEFINE_PER_CPU(struct bts_tracer
*, tracer
);
22 static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS
], buffer
);
24 #define this_tracer per_cpu(tracer, smp_processor_id())
25 #define this_buffer per_cpu(buffer, smp_processor_id())
29 * Information to interpret a BTS record.
30 * This will go into an in-kernel BTS interface.
32 static unsigned char sizeof_field
;
33 static unsigned long debugctl_mask
;
35 #define sizeof_bts (3 * sizeof_field)
37 static void bts_trace_cpuinit(struct cpuinfo_x86
*c
)
41 switch (c
->x86_model
) {
45 case 0xE: /* Pentium M */
46 sizeof_field
= sizeof(long);
47 debugctl_mask
= (1<<6)|(1<<7);
51 debugctl_mask
= (1<<6)|(1<<7);
56 switch (c
->x86_model
) {
59 case 0x2: /* Netburst */
60 sizeof_field
= sizeof(long);
61 debugctl_mask
= (1<<2)|(1<<3);
64 /* sorry, don't know about them */
69 /* sorry, don't know about them */
74 static inline void bts_enable(void)
76 unsigned long debugctl
;
78 rdmsrl(MSR_IA32_DEBUGCTLMSR
, debugctl
);
79 wrmsrl(MSR_IA32_DEBUGCTLMSR
, debugctl
| debugctl_mask
);
82 static inline void bts_disable(void)
84 unsigned long debugctl
;
86 rdmsrl(MSR_IA32_DEBUGCTLMSR
, debugctl
);
87 wrmsrl(MSR_IA32_DEBUGCTLMSR
, debugctl
& ~debugctl_mask
);
90 static void bts_trace_reset(struct trace_array
*tr
)
94 tr
->time_start
= ftrace_now(tr
->cpu
);
96 for_each_online_cpu(cpu
)
97 tracing_reset(tr
, cpu
);
100 static void bts_trace_start_cpu(void *arg
)
103 ds_request_bts(/* task = */ NULL
, this_buffer
, SIZEOF_BTS
,
104 /* ovfl = */ NULL
, /* th = */ (size_t)-1);
105 if (IS_ERR(this_tracer
)) {
113 static void bts_trace_start(struct trace_array
*tr
)
119 for_each_cpu_mask(cpu
, cpu_possible_map
)
120 smp_call_function_single(cpu
, bts_trace_start_cpu
, NULL
, 1);
123 static void bts_trace_stop_cpu(void *arg
)
128 ds_release_bts(this_tracer
);
133 static void bts_trace_stop(struct trace_array
*tr
)
137 for_each_cpu_mask(cpu
, cpu_possible_map
)
138 smp_call_function_single(cpu
, bts_trace_stop_cpu
, NULL
, 1);
141 static int bts_trace_init(struct trace_array
*tr
)
143 bts_trace_cpuinit(&boot_cpu_data
);
150 static void bts_trace_print_header(struct seq_file
*m
)
153 seq_puts(m
, "# CPU# FROM TO FUNCTION\n");
154 seq_puts(m
, "# | | | |\n");
157 "# CPU# FROM TO FUNCTION\n");
163 static enum print_line_t
bts_trace_print_line(struct trace_iterator
*iter
)
165 struct trace_entry
*entry
= iter
->ent
;
166 struct trace_seq
*seq
= &iter
->seq
;
167 struct bts_entry
*it
;
169 trace_assign_type(it
, entry
);
171 if (entry
->type
== TRACE_BTS
) {
173 #ifdef CONFIG_KALLSYMS
174 char function
[KSYM_SYMBOL_LEN
];
175 sprint_symbol(function
, it
->from
);
177 char *function
= "<unknown>";
180 ret
= trace_seq_printf(seq
, "%4d 0x%lx -> 0x%lx [%s]\n",
181 entry
->cpu
, it
->from
, it
->to
, function
);
183 return TRACE_TYPE_PARTIAL_LINE
;;
184 return TRACE_TYPE_HANDLED
;
186 return TRACE_TYPE_UNHANDLED
;
189 void trace_bts(struct trace_array
*tr
, unsigned long from
, unsigned long to
)
191 struct ring_buffer_event
*event
;
192 struct bts_entry
*entry
;
195 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
), &irq
);
198 entry
= ring_buffer_event_data(event
);
199 tracing_generic_entry_update(&entry
->ent
, 0, from
);
200 entry
->ent
.type
= TRACE_BTS
;
201 entry
->ent
.cpu
= smp_processor_id();
204 ring_buffer_unlock_commit(tr
->buffer
, event
, irq
);
207 static void trace_bts_at(struct trace_array
*tr
, size_t index
)
209 const void *raw
= NULL
;
210 unsigned long from
, to
;
213 err
= ds_access_bts(this_tracer
, index
, &raw
);
217 from
= *(const unsigned long *)raw
;
218 to
= *(const unsigned long *)((const char *)raw
+ sizeof_field
);
220 trace_bts(tr
, from
, to
);
223 static void trace_bts_cpu(void *arg
)
225 struct trace_array
*tr
= (struct trace_array
*) arg
;
226 size_t index
= 0, end
= 0, i
;
234 err
= ds_get_bts_index(this_tracer
, &index
);
238 err
= ds_get_bts_end(this_tracer
, &end
);
242 for (i
= index
; i
< end
; i
++)
245 for (i
= 0; i
< index
; i
++)
252 static void trace_bts_prepare(struct trace_iterator
*iter
)
256 for_each_cpu_mask(cpu
, cpu_possible_map
)
257 smp_call_function_single(cpu
, trace_bts_cpu
, iter
->tr
, 1);
260 struct tracer bts_tracer __read_mostly
=
263 .init
= bts_trace_init
,
264 .reset
= bts_trace_stop
,
265 .print_header
= bts_trace_print_header
,
266 .print_line
= bts_trace_print_line
,
267 .start
= bts_trace_start
,
268 .stop
= bts_trace_stop
,
269 .open
= trace_bts_prepare
272 __init
static int init_bts_trace(void)
274 return register_tracer(&bts_tracer
);
276 device_initcall(init_bts_trace
);