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