perf_counter: x86: Allow unpriviliged use of NMIs
[deliverable/linux.git] / arch / x86 / kernel / cpu / perf_counter.c
CommitLineData
241771ef
IM
1/*
2 * Performance counter x86 architecture code
3 *
98144511
IM
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
241771ef
IM
9 *
10 * For licencing details see kernel-base/COPYING
11 */
12
13#include <linux/perf_counter.h>
14#include <linux/capability.h>
15#include <linux/notifier.h>
16#include <linux/hardirq.h>
17#include <linux/kprobes.h>
4ac13294 18#include <linux/module.h>
241771ef
IM
19#include <linux/kdebug.h>
20#include <linux/sched.h>
d7d59fb3 21#include <linux/uaccess.h>
241771ef 22
241771ef 23#include <asm/apic.h>
d7d59fb3 24#include <asm/stacktrace.h>
4e935e47 25#include <asm/nmi.h>
241771ef 26
862a1a5f 27static u64 perf_counter_mask __read_mostly;
703e937c 28
241771ef 29struct cpu_hw_counters {
862a1a5f 30 struct perf_counter *counters[X86_PMC_IDX_MAX];
43f6201a
RR
31 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
32 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
4b39fd96 33 unsigned long interrupts;
b0f3f28e 34 u64 throttle_ctrl;
b0f3f28e 35 int enabled;
241771ef
IM
36};
37
38/*
5f4ec28f 39 * struct x86_pmu - generic x86 pmu
241771ef 40 */
5f4ec28f 41struct x86_pmu {
faa28ae0
RR
42 const char *name;
43 int version;
39d81eab 44 int (*handle_irq)(struct pt_regs *, int);
169e41eb 45 u64 (*save_disable_all)(void);
b0f3f28e 46 void (*restore_all)(u64);
7c90cc45 47 void (*enable)(struct hw_perf_counter *, int);
d4369891 48 void (*disable)(struct hw_perf_counter *, int);
169e41eb
JSR
49 unsigned eventsel;
50 unsigned perfctr;
b0f3f28e
PZ
51 u64 (*event_map)(int);
52 u64 (*raw_event)(u64);
169e41eb 53 int max_events;
0933e5c6
RR
54 int num_counters;
55 int num_counters_fixed;
56 int counter_bits;
57 u64 counter_mask;
c619b8ff 58 u64 max_period;
b56a3802
JSR
59};
60
4a06bd85 61static struct x86_pmu x86_pmu __read_mostly;
b56a3802 62
b0f3f28e
PZ
63static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
64 .enabled = 1,
65};
241771ef 66
b56a3802
JSR
67/*
68 * Intel PerfMon v3. Used on Core2 and later.
69 */
b0f3f28e 70static const u64 intel_perfmon_event_map[] =
241771ef 71{
f650a672 72 [PERF_COUNT_CPU_CYCLES] = 0x003c,
241771ef
IM
73 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
74 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
75 [PERF_COUNT_CACHE_MISSES] = 0x412e,
76 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
77 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
f650a672 78 [PERF_COUNT_BUS_CYCLES] = 0x013c,
241771ef
IM
79};
80
5f4ec28f 81static u64 intel_pmu_event_map(int event)
b56a3802
JSR
82{
83 return intel_perfmon_event_map[event];
84}
241771ef 85
5f4ec28f 86static u64 intel_pmu_raw_event(u64 event)
b0f3f28e 87{
82bae4f8
PZ
88#define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
89#define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
90#define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
b0f3f28e
PZ
91
92#define CORE_EVNTSEL_MASK \
93 (CORE_EVNTSEL_EVENT_MASK | \
94 CORE_EVNTSEL_UNIT_MASK | \
95 CORE_EVNTSEL_COUNTER_MASK)
96
97 return event & CORE_EVNTSEL_MASK;
98}
99
f87ad35d
JSR
100/*
101 * AMD Performance Monitor K7 and later.
102 */
b0f3f28e 103static const u64 amd_perfmon_event_map[] =
f87ad35d
JSR
104{
105 [PERF_COUNT_CPU_CYCLES] = 0x0076,
106 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
107 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
108 [PERF_COUNT_CACHE_MISSES] = 0x0081,
109 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
110 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
111};
112
5f4ec28f 113static u64 amd_pmu_event_map(int event)
f87ad35d
JSR
114{
115 return amd_perfmon_event_map[event];
116}
117
5f4ec28f 118static u64 amd_pmu_raw_event(u64 event)
b0f3f28e 119{
82bae4f8
PZ
120#define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
121#define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
122#define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
b0f3f28e
PZ
123
124#define K7_EVNTSEL_MASK \
125 (K7_EVNTSEL_EVENT_MASK | \
126 K7_EVNTSEL_UNIT_MASK | \
127 K7_EVNTSEL_COUNTER_MASK)
128
129 return event & K7_EVNTSEL_MASK;
130}
131
ee06094f
IM
132/*
133 * Propagate counter elapsed time into the generic counter.
134 * Can only be executed on the CPU where the counter is active.
135 * Returns the delta events processed.
136 */
4b7bfd0d 137static u64
ee06094f
IM
138x86_perf_counter_update(struct perf_counter *counter,
139 struct hw_perf_counter *hwc, int idx)
140{
ec3232bd
PZ
141 int shift = 64 - x86_pmu.counter_bits;
142 u64 prev_raw_count, new_raw_count;
143 s64 delta;
ee06094f 144
ee06094f
IM
145 /*
146 * Careful: an NMI might modify the previous counter value.
147 *
148 * Our tactic to handle this is to first atomically read and
149 * exchange a new raw count - then add that new-prev delta
150 * count to the generic counter atomically:
151 */
152again:
153 prev_raw_count = atomic64_read(&hwc->prev_count);
154 rdmsrl(hwc->counter_base + idx, new_raw_count);
155
156 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
157 new_raw_count) != prev_raw_count)
158 goto again;
159
160 /*
161 * Now we have the new raw value and have updated the prev
162 * timestamp already. We can now calculate the elapsed delta
163 * (counter-)time and add that to the generic counter.
164 *
165 * Careful, not all hw sign-extends above the physical width
ec3232bd 166 * of the count.
ee06094f 167 */
ec3232bd
PZ
168 delta = (new_raw_count << shift) - (prev_raw_count << shift);
169 delta >>= shift;
ee06094f
IM
170
171 atomic64_add(delta, &counter->count);
172 atomic64_sub(delta, &hwc->period_left);
4b7bfd0d
RR
173
174 return new_raw_count;
ee06094f
IM
175}
176
ba77813a 177static atomic_t active_counters;
4e935e47
PZ
178static DEFINE_MUTEX(pmc_reserve_mutex);
179
180static bool reserve_pmc_hardware(void)
181{
182 int i;
183
184 if (nmi_watchdog == NMI_LOCAL_APIC)
185 disable_lapic_nmi_watchdog();
186
0933e5c6 187 for (i = 0; i < x86_pmu.num_counters; i++) {
4a06bd85 188 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
4e935e47
PZ
189 goto perfctr_fail;
190 }
191
0933e5c6 192 for (i = 0; i < x86_pmu.num_counters; i++) {
4a06bd85 193 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
4e935e47
PZ
194 goto eventsel_fail;
195 }
196
197 return true;
198
199eventsel_fail:
200 for (i--; i >= 0; i--)
4a06bd85 201 release_evntsel_nmi(x86_pmu.eventsel + i);
4e935e47 202
0933e5c6 203 i = x86_pmu.num_counters;
4e935e47
PZ
204
205perfctr_fail:
206 for (i--; i >= 0; i--)
4a06bd85 207 release_perfctr_nmi(x86_pmu.perfctr + i);
4e935e47
PZ
208
209 if (nmi_watchdog == NMI_LOCAL_APIC)
210 enable_lapic_nmi_watchdog();
211
212 return false;
213}
214
215static void release_pmc_hardware(void)
216{
217 int i;
218
0933e5c6 219 for (i = 0; i < x86_pmu.num_counters; i++) {
4a06bd85
RR
220 release_perfctr_nmi(x86_pmu.perfctr + i);
221 release_evntsel_nmi(x86_pmu.eventsel + i);
4e935e47
PZ
222 }
223
224 if (nmi_watchdog == NMI_LOCAL_APIC)
225 enable_lapic_nmi_watchdog();
226}
227
228static void hw_perf_counter_destroy(struct perf_counter *counter)
229{
ba77813a 230 if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
4e935e47
PZ
231 release_pmc_hardware();
232 mutex_unlock(&pmc_reserve_mutex);
233 }
234}
235
85cf9dba
RR
236static inline int x86_pmu_initialized(void)
237{
238 return x86_pmu.handle_irq != NULL;
239}
240
241771ef
IM
241/*
242 * Setup the hardware configuration for a given hw_event_type
243 */
621a01ea 244static int __hw_perf_counter_init(struct perf_counter *counter)
241771ef 245{
9f66a381 246 struct perf_counter_hw_event *hw_event = &counter->hw_event;
241771ef 247 struct hw_perf_counter *hwc = &counter->hw;
4e935e47 248 int err;
241771ef 249
85cf9dba
RR
250 if (!x86_pmu_initialized())
251 return -ENODEV;
241771ef 252
4e935e47 253 err = 0;
ba77813a 254 if (!atomic_inc_not_zero(&active_counters)) {
4e935e47 255 mutex_lock(&pmc_reserve_mutex);
ba77813a 256 if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
4e935e47
PZ
257 err = -EBUSY;
258 else
ba77813a 259 atomic_inc(&active_counters);
4e935e47
PZ
260 mutex_unlock(&pmc_reserve_mutex);
261 }
262 if (err)
263 return err;
264
241771ef 265 /*
0475f9ea 266 * Generate PMC IRQs:
241771ef
IM
267 * (keep 'enabled' bit clear for now)
268 */
0475f9ea 269 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
241771ef
IM
270
271 /*
0475f9ea 272 * Count user and OS events unless requested not to.
241771ef 273 */
0475f9ea
PM
274 if (!hw_event->exclude_user)
275 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
276 if (!hw_event->exclude_kernel)
241771ef 277 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
0475f9ea
PM
278
279 /*
280 * If privileged enough, allow NMI events:
281 */
282 hwc->nmi = 0;
a026dfec
PZ
283 if (hw_event->nmi) {
284 if (sysctl_perf_counter_priv && !capable(CAP_SYS_ADMIN))
285 return -EACCES;
0475f9ea 286 hwc->nmi = 1;
a026dfec 287 }
241771ef 288
ba77813a 289 hwc->irq_period = hw_event->irq_period;
c619b8ff
RR
290 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > x86_pmu.max_period)
291 hwc->irq_period = x86_pmu.max_period;
241771ef 292
ee06094f 293 atomic64_set(&hwc->period_left, hwc->irq_period);
241771ef
IM
294
295 /*
dfa7c899 296 * Raw event type provide the config in the event structure
241771ef 297 */
f4a2deb4 298 if (perf_event_raw(hw_event)) {
4a06bd85 299 hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
241771ef 300 } else {
4a06bd85 301 if (perf_event_id(hw_event) >= x86_pmu.max_events)
241771ef
IM
302 return -EINVAL;
303 /*
304 * The generic map:
305 */
4a06bd85 306 hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
241771ef 307 }
241771ef 308
4e935e47
PZ
309 counter->destroy = hw_perf_counter_destroy;
310
241771ef
IM
311 return 0;
312}
313
5f4ec28f 314static u64 intel_pmu_save_disable_all(void)
4ac13294
TG
315{
316 u64 ctrl;
317
318 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
862a1a5f 319 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
2b9ff0db 320
4ac13294 321 return ctrl;
241771ef 322}
b56a3802 323
5f4ec28f 324static u64 amd_pmu_save_disable_all(void)
f87ad35d 325{
b0f3f28e
PZ
326 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
327 int enabled, idx;
328
329 enabled = cpuc->enabled;
330 cpuc->enabled = 0;
60b3df9c
PZ
331 /*
332 * ensure we write the disable before we start disabling the
5f4ec28f
RR
333 * counters proper, so that amd_pmu_enable_counter() does the
334 * right thing.
60b3df9c 335 */
b0f3f28e 336 barrier();
f87ad35d 337
0933e5c6 338 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
b0f3f28e
PZ
339 u64 val;
340
43f6201a 341 if (!test_bit(idx, cpuc->active_mask))
4295ee62 342 continue;
f87ad35d 343 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
4295ee62
RR
344 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
345 continue;
346 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
347 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
f87ad35d
JSR
348 }
349
b0f3f28e 350 return enabled;
f87ad35d
JSR
351}
352
b56a3802
JSR
353u64 hw_perf_save_disable(void)
354{
85cf9dba 355 if (!x86_pmu_initialized())
b56a3802 356 return 0;
4a06bd85 357 return x86_pmu.save_disable_all();
b56a3802 358}
b0f3f28e
PZ
359/*
360 * Exported because of ACPI idle
361 */
01b2838c 362EXPORT_SYMBOL_GPL(hw_perf_save_disable);
241771ef 363
5f4ec28f 364static void intel_pmu_restore_all(u64 ctrl)
b56a3802
JSR
365{
366 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
367}
368
5f4ec28f 369static void amd_pmu_restore_all(u64 ctrl)
f87ad35d 370{
b0f3f28e 371 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
f87ad35d
JSR
372 int idx;
373
b0f3f28e
PZ
374 cpuc->enabled = ctrl;
375 barrier();
376 if (!ctrl)
377 return;
378
0933e5c6 379 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
4295ee62 380 u64 val;
b0f3f28e 381
43f6201a 382 if (!test_bit(idx, cpuc->active_mask))
4295ee62
RR
383 continue;
384 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
385 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
386 continue;
387 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
388 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
f87ad35d
JSR
389 }
390}
391
ee06094f
IM
392void hw_perf_restore(u64 ctrl)
393{
85cf9dba 394 if (!x86_pmu_initialized())
2b9ff0db 395 return;
4a06bd85 396 x86_pmu.restore_all(ctrl);
ee06094f 397}
b0f3f28e
PZ
398/*
399 * Exported because of ACPI idle
400 */
ee06094f
IM
401EXPORT_SYMBOL_GPL(hw_perf_restore);
402
19d84dab 403static inline u64 intel_pmu_get_status(void)
b0f3f28e
PZ
404{
405 u64 status;
406
b7f8859a 407 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
b0f3f28e 408
b7f8859a 409 return status;
b0f3f28e
PZ
410}
411
dee5d906 412static inline void intel_pmu_ack_status(u64 ack)
b0f3f28e
PZ
413{
414 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
415}
416
7c90cc45 417static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
b0f3f28e 418{
7c90cc45 419 int err;
7c90cc45
RR
420 err = checking_wrmsrl(hwc->config_base + idx,
421 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
b0f3f28e
PZ
422}
423
d4369891 424static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
b0f3f28e 425{
d4369891 426 int err;
d4369891
RR
427 err = checking_wrmsrl(hwc->config_base + idx,
428 hwc->config);
b0f3f28e
PZ
429}
430
2f18d1e8 431static inline void
d4369891 432intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
2f18d1e8
IM
433{
434 int idx = __idx - X86_PMC_IDX_FIXED;
435 u64 ctrl_val, mask;
436 int err;
437
438 mask = 0xfULL << (idx * 4);
439
440 rdmsrl(hwc->config_base, ctrl_val);
441 ctrl_val &= ~mask;
442 err = checking_wrmsrl(hwc->config_base, ctrl_val);
443}
444
7e2ae347 445static inline void
d4369891 446intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
7e2ae347 447{
d4369891
RR
448 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
449 intel_pmu_disable_fixed(hwc, idx);
450 return;
451 }
452
453 x86_pmu_disable_counter(hwc, idx);
454}
455
456static inline void
457amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
458{
459 x86_pmu_disable_counter(hwc, idx);
7e2ae347
IM
460}
461
2f18d1e8 462static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
241771ef 463
ee06094f
IM
464/*
465 * Set the next IRQ period, based on the hwc->period_left value.
466 * To be called with the counter disabled in hw:
467 */
468static void
26816c28 469x86_perf_counter_set_period(struct perf_counter *counter,
ee06094f 470 struct hw_perf_counter *hwc, int idx)
241771ef 471{
2f18d1e8 472 s64 left = atomic64_read(&hwc->period_left);
595258aa 473 s64 period = hwc->irq_period;
2f18d1e8 474 int err;
ee06094f 475
ee06094f
IM
476 /*
477 * If we are way outside a reasoable range then just skip forward:
478 */
479 if (unlikely(left <= -period)) {
480 left = period;
481 atomic64_set(&hwc->period_left, left);
482 }
483
484 if (unlikely(left <= 0)) {
485 left += period;
486 atomic64_set(&hwc->period_left, left);
487 }
241771ef 488
ee06094f
IM
489 per_cpu(prev_left[idx], smp_processor_id()) = left;
490
491 /*
492 * The hw counter starts counting from this counter offset,
493 * mark it to be able to extra future deltas:
494 */
2f18d1e8 495 atomic64_set(&hwc->prev_count, (u64)-left);
ee06094f 496
2f18d1e8 497 err = checking_wrmsrl(hwc->counter_base + idx,
0933e5c6 498 (u64)(-left) & x86_pmu.counter_mask);
2f18d1e8
IM
499}
500
501static inline void
7c90cc45 502intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
2f18d1e8
IM
503{
504 int idx = __idx - X86_PMC_IDX_FIXED;
505 u64 ctrl_val, bits, mask;
506 int err;
507
508 /*
0475f9ea
PM
509 * Enable IRQ generation (0x8),
510 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
511 * if requested:
2f18d1e8 512 */
0475f9ea
PM
513 bits = 0x8ULL;
514 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
515 bits |= 0x2;
2f18d1e8
IM
516 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
517 bits |= 0x1;
518 bits <<= (idx * 4);
519 mask = 0xfULL << (idx * 4);
520
521 rdmsrl(hwc->config_base, ctrl_val);
522 ctrl_val &= ~mask;
523 ctrl_val |= bits;
524 err = checking_wrmsrl(hwc->config_base, ctrl_val);
7e2ae347
IM
525}
526
7c90cc45 527static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
7e2ae347 528{
7c90cc45
RR
529 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
530 intel_pmu_enable_fixed(hwc, idx);
531 return;
532 }
533
534 x86_pmu_enable_counter(hwc, idx);
535}
536
537static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
538{
539 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
540
541 if (cpuc->enabled)
542 x86_pmu_enable_counter(hwc, idx);
2b583d8b 543 else
d4369891 544 x86_pmu_disable_counter(hwc, idx);
241771ef
IM
545}
546
2f18d1e8
IM
547static int
548fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
862a1a5f 549{
2f18d1e8
IM
550 unsigned int event;
551
ef7b3e09 552 if (!x86_pmu.num_counters_fixed)
f87ad35d
JSR
553 return -1;
554
2f18d1e8
IM
555 if (unlikely(hwc->nmi))
556 return -1;
557
558 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
559
4a06bd85 560 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
2f18d1e8 561 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
4a06bd85 562 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
2f18d1e8 563 return X86_PMC_IDX_FIXED_CPU_CYCLES;
4a06bd85 564 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
2f18d1e8
IM
565 return X86_PMC_IDX_FIXED_BUS_CYCLES;
566
862a1a5f
IM
567 return -1;
568}
569
ee06094f
IM
570/*
571 * Find a PMC slot for the freshly enabled / scheduled in counter:
572 */
4aeb0b42 573static int x86_pmu_enable(struct perf_counter *counter)
241771ef
IM
574{
575 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
576 struct hw_perf_counter *hwc = &counter->hw;
2f18d1e8 577 int idx;
241771ef 578
2f18d1e8
IM
579 idx = fixed_mode_idx(counter, hwc);
580 if (idx >= 0) {
581 /*
582 * Try to get the fixed counter, if that is already taken
583 * then try to get a generic counter:
584 */
43f6201a 585 if (test_and_set_bit(idx, cpuc->used_mask))
2f18d1e8 586 goto try_generic;
0dff86aa 587
2f18d1e8
IM
588 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
589 /*
590 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
591 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
592 */
593 hwc->counter_base =
594 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
241771ef 595 hwc->idx = idx;
2f18d1e8
IM
596 } else {
597 idx = hwc->idx;
598 /* Try to get the previous generic counter again */
43f6201a 599 if (test_and_set_bit(idx, cpuc->used_mask)) {
2f18d1e8 600try_generic:
43f6201a 601 idx = find_first_zero_bit(cpuc->used_mask,
0933e5c6
RR
602 x86_pmu.num_counters);
603 if (idx == x86_pmu.num_counters)
2f18d1e8
IM
604 return -EAGAIN;
605
43f6201a 606 set_bit(idx, cpuc->used_mask);
2f18d1e8
IM
607 hwc->idx = idx;
608 }
4a06bd85
RR
609 hwc->config_base = x86_pmu.eventsel;
610 hwc->counter_base = x86_pmu.perfctr;
241771ef
IM
611 }
612
613 perf_counters_lapic_init(hwc->nmi);
614
d4369891 615 x86_pmu.disable(hwc, idx);
241771ef 616
862a1a5f 617 cpuc->counters[idx] = counter;
43f6201a 618 set_bit(idx, cpuc->active_mask);
7e2ae347 619
26816c28 620 x86_perf_counter_set_period(counter, hwc, idx);
7c90cc45 621 x86_pmu.enable(hwc, idx);
95cdd2e7
IM
622
623 return 0;
241771ef
IM
624}
625
626void perf_counter_print_debug(void)
627{
2f18d1e8 628 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
0dff86aa 629 struct cpu_hw_counters *cpuc;
5bb9efe3 630 unsigned long flags;
1e125676
IM
631 int cpu, idx;
632
0933e5c6 633 if (!x86_pmu.num_counters)
1e125676 634 return;
241771ef 635
5bb9efe3 636 local_irq_save(flags);
241771ef
IM
637
638 cpu = smp_processor_id();
0dff86aa 639 cpuc = &per_cpu(cpu_hw_counters, cpu);
241771ef 640
faa28ae0 641 if (x86_pmu.version >= 2) {
a1ef58f4
JSR
642 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
643 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
644 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
645 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
646
647 pr_info("\n");
648 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
649 pr_info("CPU#%d: status: %016llx\n", cpu, status);
650 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
651 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
f87ad35d 652 }
43f6201a 653 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
241771ef 654
0933e5c6 655 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
4a06bd85
RR
656 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
657 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
241771ef 658
ee06094f 659 prev_left = per_cpu(prev_left[idx], cpu);
241771ef 660
a1ef58f4 661 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
241771ef 662 cpu, idx, pmc_ctrl);
a1ef58f4 663 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
241771ef 664 cpu, idx, pmc_count);
a1ef58f4 665 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
ee06094f 666 cpu, idx, prev_left);
241771ef 667 }
0933e5c6 668 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
2f18d1e8
IM
669 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
670
a1ef58f4 671 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
2f18d1e8
IM
672 cpu, idx, pmc_count);
673 }
5bb9efe3 674 local_irq_restore(flags);
241771ef
IM
675}
676
4aeb0b42 677static void x86_pmu_disable(struct perf_counter *counter)
241771ef
IM
678{
679 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
680 struct hw_perf_counter *hwc = &counter->hw;
6f00cada 681 int idx = hwc->idx;
241771ef 682
09534238
RR
683 /*
684 * Must be done before we disable, otherwise the nmi handler
685 * could reenable again:
686 */
43f6201a 687 clear_bit(idx, cpuc->active_mask);
d4369891 688 x86_pmu.disable(hwc, idx);
241771ef 689
2f18d1e8
IM
690 /*
691 * Make sure the cleared pointer becomes visible before we
692 * (potentially) free the counter:
693 */
527e26af 694 barrier();
241771ef 695
ee06094f
IM
696 /*
697 * Drain the remaining delta count out of a counter
698 * that we are disabling:
699 */
700 x86_perf_counter_update(counter, hwc, idx);
09534238 701 cpuc->counters[idx] = NULL;
43f6201a 702 clear_bit(idx, cpuc->used_mask);
241771ef
IM
703}
704
7e2ae347 705/*
ee06094f
IM
706 * Save and restart an expired counter. Called by NMI contexts,
707 * so it has to be careful about preempting normal counter ops:
7e2ae347 708 */
55de0f2e 709static void intel_pmu_save_and_restart(struct perf_counter *counter)
241771ef
IM
710{
711 struct hw_perf_counter *hwc = &counter->hw;
712 int idx = hwc->idx;
241771ef 713
ee06094f 714 x86_perf_counter_update(counter, hwc, idx);
26816c28 715 x86_perf_counter_set_period(counter, hwc, idx);
7e2ae347 716
2f18d1e8 717 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
7c90cc45 718 intel_pmu_enable_counter(hwc, idx);
241771ef
IM
719}
720
4b39fd96
MG
721/*
722 * Maximum interrupt frequency of 100KHz per CPU
723 */
169e41eb 724#define PERFMON_MAX_INTERRUPTS (100000/HZ)
4b39fd96 725
241771ef
IM
726/*
727 * This handler is triggered by the local APIC, so the APIC IRQ handling
728 * rules apply:
729 */
39d81eab 730static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
241771ef
IM
731{
732 int bit, cpu = smp_processor_id();
4b39fd96 733 u64 ack, status;
1b023a96 734 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
b0f3f28e 735 int ret = 0;
43874d23 736
55de0f2e 737 cpuc->throttle_ctrl = intel_pmu_save_disable_all();
241771ef 738
19d84dab 739 status = intel_pmu_get_status();
87b9cf46
IM
740 if (!status)
741 goto out;
742
b0f3f28e 743 ret = 1;
241771ef 744again:
d278c484 745 inc_irq_stat(apic_perf_irqs);
241771ef 746 ack = status;
2f18d1e8 747 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
862a1a5f 748 struct perf_counter *counter = cpuc->counters[bit];
241771ef
IM
749
750 clear_bit(bit, (unsigned long *) &status);
43f6201a 751 if (!test_bit(bit, cpuc->active_mask))
241771ef
IM
752 continue;
753
55de0f2e 754 intel_pmu_save_and_restart(counter);
78f13e95 755 if (perf_counter_overflow(counter, nmi, regs, 0))
d4369891 756 intel_pmu_disable_counter(&counter->hw, bit);
241771ef
IM
757 }
758
dee5d906 759 intel_pmu_ack_status(ack);
241771ef
IM
760
761 /*
762 * Repeat if there is more work to be done:
763 */
19d84dab 764 status = intel_pmu_get_status();
241771ef
IM
765 if (status)
766 goto again;
87b9cf46 767out:
241771ef 768 /*
1b023a96 769 * Restore - do not reenable when global enable is off or throttled:
241771ef 770 */
f5a5a2f6
IM
771 if (cpuc->throttle_ctrl) {
772 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS) {
773 intel_pmu_restore_all(cpuc->throttle_ctrl);
774 } else {
775 pr_info("CPU#%d: perfcounters: max interrupt rate exceeded! Throttle on.\n", smp_processor_id());
776 }
777 }
b0f3f28e
PZ
778
779 return ret;
1b023a96
MG
780}
781
a29aa8a7
RR
782static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
783{
784 int cpu = smp_processor_id();
785 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
786 u64 val;
787 int handled = 0;
788 struct perf_counter *counter;
789 struct hw_perf_counter *hwc;
790 int idx;
791
792 ++cpuc->interrupts;
793 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
43f6201a 794 if (!test_bit(idx, cpuc->active_mask))
a29aa8a7
RR
795 continue;
796 counter = cpuc->counters[idx];
797 hwc = &counter->hw;
4b7bfd0d 798 val = x86_perf_counter_update(counter, hwc, idx);
a29aa8a7
RR
799 if (val & (1ULL << (x86_pmu.counter_bits - 1)))
800 continue;
801 /* counter overflow */
802 x86_perf_counter_set_period(counter, hwc, idx);
803 handled = 1;
804 inc_irq_stat(apic_perf_irqs);
805 if (perf_counter_overflow(counter, nmi, regs, 0))
806 amd_pmu_disable_counter(hwc, idx);
807 else if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS)
808 /*
809 * do not reenable when throttled, but reload
810 * the register
811 */
812 amd_pmu_disable_counter(hwc, idx);
813 else if (counter->state == PERF_COUNTER_STATE_ACTIVE)
814 amd_pmu_enable_counter(hwc, idx);
815 }
816 return handled;
817}
39d81eab 818
1b023a96
MG
819void perf_counter_unthrottle(void)
820{
821 struct cpu_hw_counters *cpuc;
822
85cf9dba 823 if (!x86_pmu_initialized())
1b023a96
MG
824 return;
825
b0f3f28e 826 cpuc = &__get_cpu_var(cpu_hw_counters);
4b39fd96 827 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
f5a5a2f6
IM
828 pr_info("CPU#%d: perfcounters: throttle off.\n", smp_processor_id());
829
830 /*
831 * Clear them before re-enabling irqs/NMIs again:
832 */
833 cpuc->interrupts = 0;
b0f3f28e 834 hw_perf_restore(cpuc->throttle_ctrl);
f5a5a2f6
IM
835 } else {
836 cpuc->interrupts = 0;
1b023a96 837 }
241771ef
IM
838}
839
840void smp_perf_counter_interrupt(struct pt_regs *regs)
841{
842 irq_enter();
241771ef 843 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
b0f3f28e 844 ack_APIC_irq();
4a06bd85 845 x86_pmu.handle_irq(regs, 0);
241771ef
IM
846 irq_exit();
847}
848
b6276f35
PZ
849void smp_perf_pending_interrupt(struct pt_regs *regs)
850{
851 irq_enter();
852 ack_APIC_irq();
853 inc_irq_stat(apic_pending_irqs);
854 perf_counter_do_pending();
855 irq_exit();
856}
857
858void set_perf_counter_pending(void)
859{
860 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
861}
862
3415dd91 863void perf_counters_lapic_init(int nmi)
241771ef
IM
864{
865 u32 apic_val;
866
85cf9dba 867 if (!x86_pmu_initialized())
241771ef 868 return;
85cf9dba 869
241771ef
IM
870 /*
871 * Enable the performance counter vector in the APIC LVT:
872 */
873 apic_val = apic_read(APIC_LVTERR);
874
875 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
876 if (nmi)
877 apic_write(APIC_LVTPC, APIC_DM_NMI);
878 else
879 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
880 apic_write(APIC_LVTERR, apic_val);
881}
882
883static int __kprobes
884perf_counter_nmi_handler(struct notifier_block *self,
885 unsigned long cmd, void *__args)
886{
887 struct die_args *args = __args;
888 struct pt_regs *regs;
b0f3f28e
PZ
889 int ret;
890
ba77813a 891 if (!atomic_read(&active_counters))
63a809a2
PZ
892 return NOTIFY_DONE;
893
b0f3f28e
PZ
894 switch (cmd) {
895 case DIE_NMI:
896 case DIE_NMI_IPI:
897 break;
241771ef 898
b0f3f28e 899 default:
241771ef 900 return NOTIFY_DONE;
b0f3f28e 901 }
241771ef
IM
902
903 regs = args->regs;
904
905 apic_write(APIC_LVTPC, APIC_DM_NMI);
4a06bd85 906 ret = x86_pmu.handle_irq(regs, 1);
241771ef 907
b0f3f28e 908 return ret ? NOTIFY_STOP : NOTIFY_OK;
241771ef
IM
909}
910
911static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
5b75af0a
MG
912 .notifier_call = perf_counter_nmi_handler,
913 .next = NULL,
914 .priority = 1
241771ef
IM
915};
916
5f4ec28f 917static struct x86_pmu intel_pmu = {
faa28ae0 918 .name = "Intel",
39d81eab 919 .handle_irq = intel_pmu_handle_irq,
5f4ec28f
RR
920 .save_disable_all = intel_pmu_save_disable_all,
921 .restore_all = intel_pmu_restore_all,
5f4ec28f
RR
922 .enable = intel_pmu_enable_counter,
923 .disable = intel_pmu_disable_counter,
b56a3802
JSR
924 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
925 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
5f4ec28f
RR
926 .event_map = intel_pmu_event_map,
927 .raw_event = intel_pmu_raw_event,
b56a3802 928 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
c619b8ff
RR
929 /*
930 * Intel PMCs cannot be accessed sanely above 32 bit width,
931 * so we install an artificial 1<<31 period regardless of
932 * the generic counter period:
933 */
934 .max_period = (1ULL << 31) - 1,
b56a3802
JSR
935};
936
5f4ec28f 937static struct x86_pmu amd_pmu = {
faa28ae0 938 .name = "AMD",
39d81eab 939 .handle_irq = amd_pmu_handle_irq,
5f4ec28f
RR
940 .save_disable_all = amd_pmu_save_disable_all,
941 .restore_all = amd_pmu_restore_all,
5f4ec28f
RR
942 .enable = amd_pmu_enable_counter,
943 .disable = amd_pmu_disable_counter,
f87ad35d
JSR
944 .eventsel = MSR_K7_EVNTSEL0,
945 .perfctr = MSR_K7_PERFCTR0,
5f4ec28f
RR
946 .event_map = amd_pmu_event_map,
947 .raw_event = amd_pmu_raw_event,
f87ad35d 948 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
0933e5c6
RR
949 .num_counters = 4,
950 .counter_bits = 48,
951 .counter_mask = (1ULL << 48) - 1,
c619b8ff
RR
952 /* use highest bit to detect overflow */
953 .max_period = (1ULL << 47) - 1,
f87ad35d
JSR
954};
955
72eae04d 956static int intel_pmu_init(void)
241771ef 957{
7bb497bd 958 union cpuid10_edx edx;
241771ef 959 union cpuid10_eax eax;
703e937c 960 unsigned int unused;
7bb497bd 961 unsigned int ebx;
faa28ae0 962 int version;
241771ef 963
da1a776b 964 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
72eae04d 965 return -ENODEV;
da1a776b 966
241771ef
IM
967 /*
968 * Check whether the Architectural PerfMon supports
969 * Branch Misses Retired Event or not.
970 */
703e937c 971 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
241771ef 972 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
72eae04d 973 return -ENODEV;
241771ef 974
faa28ae0
RR
975 version = eax.split.version_id;
976 if (version < 2)
72eae04d 977 return -ENODEV;
7bb497bd 978
4a06bd85 979 x86_pmu = intel_pmu;
faa28ae0 980 x86_pmu.version = version;
0933e5c6 981 x86_pmu.num_counters = eax.split.num_counters;
066d7dea
IM
982
983 /*
984 * Quirk: v2 perfmon does not report fixed-purpose counters, so
985 * assume at least 3 counters:
986 */
987 x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
988
0933e5c6
RR
989 x86_pmu.counter_bits = eax.split.bit_width;
990 x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
b56a3802 991
72eae04d 992 return 0;
b56a3802
JSR
993}
994
72eae04d 995static int amd_pmu_init(void)
f87ad35d 996{
4a06bd85 997 x86_pmu = amd_pmu;
72eae04d 998 return 0;
f87ad35d
JSR
999}
1000
b56a3802
JSR
1001void __init init_hw_perf_counters(void)
1002{
72eae04d
RR
1003 int err;
1004
b56a3802
JSR
1005 switch (boot_cpu_data.x86_vendor) {
1006 case X86_VENDOR_INTEL:
72eae04d 1007 err = intel_pmu_init();
b56a3802 1008 break;
f87ad35d 1009 case X86_VENDOR_AMD:
72eae04d 1010 err = amd_pmu_init();
f87ad35d 1011 break;
4138960a
RR
1012 default:
1013 return;
b56a3802 1014 }
72eae04d 1015 if (err != 0)
b56a3802
JSR
1016 return;
1017
faa28ae0
RR
1018 pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
1019 pr_info("... version: %d\n", x86_pmu.version);
1020 pr_info("... bit width: %d\n", x86_pmu.counter_bits);
1021
0933e5c6
RR
1022 pr_info("... num counters: %d\n", x86_pmu.num_counters);
1023 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
1024 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
241771ef 1025 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
0933e5c6 1026 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
241771ef 1027 }
0933e5c6
RR
1028 perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
1029 perf_max_counters = x86_pmu.num_counters;
241771ef 1030
0933e5c6 1031 pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
c619b8ff 1032 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
2f18d1e8 1033
0933e5c6
RR
1034 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
1035 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
703e937c 1036 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
0933e5c6 1037 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
703e937c 1038 }
0933e5c6 1039 pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
862a1a5f 1040
0933e5c6
RR
1041 perf_counter_mask |=
1042 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
241771ef 1043
a1ef58f4 1044 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
75f224cf 1045
241771ef
IM
1046 perf_counters_lapic_init(0);
1047 register_die_notifier(&perf_counter_nmi_notifier);
241771ef 1048}
621a01ea 1049
bb775fc2 1050static inline void x86_pmu_read(struct perf_counter *counter)
ee06094f
IM
1051{
1052 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1053}
1054
4aeb0b42
RR
1055static const struct pmu pmu = {
1056 .enable = x86_pmu_enable,
1057 .disable = x86_pmu_disable,
1058 .read = x86_pmu_read,
621a01ea
IM
1059};
1060
4aeb0b42 1061const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
621a01ea
IM
1062{
1063 int err;
1064
1065 err = __hw_perf_counter_init(counter);
1066 if (err)
9ea98e19 1067 return ERR_PTR(err);
621a01ea 1068
4aeb0b42 1069 return &pmu;
621a01ea 1070}
d7d59fb3
PZ
1071
1072/*
1073 * callchain support
1074 */
1075
1076static inline
1077void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1078{
1079 if (entry->nr < MAX_STACK_DEPTH)
1080 entry->ip[entry->nr++] = ip;
1081}
1082
1083static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1084static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1085
1086
1087static void
1088backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1089{
1090 /* Ignore warnings */
1091}
1092
1093static void backtrace_warning(void *data, char *msg)
1094{
1095 /* Ignore warnings */
1096}
1097
1098static int backtrace_stack(void *data, char *name)
1099{
1100 /* Don't bother with IRQ stacks for now */
1101 return -1;
1102}
1103
1104static void backtrace_address(void *data, unsigned long addr, int reliable)
1105{
1106 struct perf_callchain_entry *entry = data;
1107
1108 if (reliable)
1109 callchain_store(entry, addr);
1110}
1111
1112static const struct stacktrace_ops backtrace_ops = {
1113 .warning = backtrace_warning,
1114 .warning_symbol = backtrace_warning_symbol,
1115 .stack = backtrace_stack,
1116 .address = backtrace_address,
1117};
1118
1119static void
1120perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1121{
1122 unsigned long bp;
1123 char *stack;
5872bdb8 1124 int nr = entry->nr;
d7d59fb3
PZ
1125
1126 callchain_store(entry, instruction_pointer(regs));
1127
1128 stack = ((char *)regs + sizeof(struct pt_regs));
1129#ifdef CONFIG_FRAME_POINTER
1130 bp = frame_pointer(regs);
1131#else
1132 bp = 0;
1133#endif
1134
1135 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
5872bdb8
PZ
1136
1137 entry->kernel = entry->nr - nr;
d7d59fb3
PZ
1138}
1139
1140
1141struct stack_frame {
1142 const void __user *next_fp;
1143 unsigned long return_address;
1144};
1145
1146static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1147{
1148 int ret;
1149
1150 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1151 return 0;
1152
1153 ret = 1;
1154 pagefault_disable();
1155 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1156 ret = 0;
1157 pagefault_enable();
1158
1159 return ret;
1160}
1161
1162static void
1163perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1164{
1165 struct stack_frame frame;
1166 const void __user *fp;
5872bdb8 1167 int nr = entry->nr;
d7d59fb3
PZ
1168
1169 regs = (struct pt_regs *)current->thread.sp0 - 1;
1170 fp = (void __user *)regs->bp;
1171
1172 callchain_store(entry, regs->ip);
1173
1174 while (entry->nr < MAX_STACK_DEPTH) {
1175 frame.next_fp = NULL;
1176 frame.return_address = 0;
1177
1178 if (!copy_stack_frame(fp, &frame))
1179 break;
1180
1181 if ((unsigned long)fp < user_stack_pointer(regs))
1182 break;
1183
1184 callchain_store(entry, frame.return_address);
1185 fp = frame.next_fp;
1186 }
5872bdb8
PZ
1187
1188 entry->user = entry->nr - nr;
d7d59fb3
PZ
1189}
1190
1191static void
1192perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1193{
1194 int is_user;
1195
1196 if (!regs)
1197 return;
1198
1199 is_user = user_mode(regs);
1200
1201 if (!current || current->pid == 0)
1202 return;
1203
1204 if (is_user && current->state != TASK_RUNNING)
1205 return;
1206
1207 if (!is_user)
1208 perf_callchain_kernel(regs, entry);
1209
1210 if (current->mm)
1211 perf_callchain_user(regs, entry);
1212}
1213
1214struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1215{
1216 struct perf_callchain_entry *entry;
1217
1218 if (in_nmi())
1219 entry = &__get_cpu_var(nmi_entry);
1220 else
1221 entry = &__get_cpu_var(irq_entry);
1222
1223 entry->nr = 0;
5872bdb8
PZ
1224 entry->hv = 0;
1225 entry->kernel = 0;
1226 entry->user = 0;
d7d59fb3
PZ
1227
1228 perf_do_callchain(regs, entry);
1229
1230 return entry;
1231}
This page took 0.117655 seconds and 5 git commands to generate.