perf: Fix LLC-* events on Intel Nehalem/Westmere
[linux-2.6-block.git] / arch / x86 / kernel / cpu / perf_event.c
CommitLineData
241771ef 1/*
cdd6c482 2 * Performance events x86 architecture code
241771ef 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>
30dd568c 9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
1da53e02 10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
241771ef
IM
11 *
12 * For licencing details see kernel-base/COPYING
13 */
14
cdd6c482 15#include <linux/perf_event.h>
241771ef
IM
16#include <linux/capability.h>
17#include <linux/notifier.h>
18#include <linux/hardirq.h>
19#include <linux/kprobes.h>
4ac13294 20#include <linux/module.h>
241771ef
IM
21#include <linux/kdebug.h>
22#include <linux/sched.h>
d7d59fb3 23#include <linux/uaccess.h>
5a0e3ad6 24#include <linux/slab.h>
74193ef0 25#include <linux/highmem.h>
30dd568c 26#include <linux/cpu.h>
272d30be 27#include <linux/bitops.h>
241771ef 28
241771ef 29#include <asm/apic.h>
d7d59fb3 30#include <asm/stacktrace.h>
4e935e47 31#include <asm/nmi.h>
257ef9d2 32#include <asm/compat.h>
241771ef 33
7645a24c
PZ
34#if 0
35#undef wrmsrl
36#define wrmsrl(msr, val) \
37do { \
38 trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
39 (unsigned long)(val)); \
40 native_write_msr((msr), (u32)((u64)(val)), \
41 (u32)((u64)(val) >> 32)); \
42} while (0)
43#endif
44
ef21f683
PZ
45/*
46 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
47 */
48static unsigned long
49copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
50{
51 unsigned long offset, addr = (unsigned long)from;
ef21f683
PZ
52 unsigned long size, len = 0;
53 struct page *page;
54 void *map;
55 int ret;
56
57 do {
58 ret = __get_user_pages_fast(addr, 1, 0, &page);
59 if (!ret)
60 break;
61
62 offset = addr & (PAGE_SIZE - 1);
63 size = min(PAGE_SIZE - offset, n - len);
64
7a837d1b 65 map = kmap_atomic(page);
ef21f683 66 memcpy(to, map+offset, size);
7a837d1b 67 kunmap_atomic(map);
ef21f683
PZ
68 put_page(page);
69
70 len += size;
71 to += size;
72 addr += size;
73
74 } while (len < n);
75
76 return len;
77}
78
1da53e02 79struct event_constraint {
c91e0f5d
PZ
80 union {
81 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
b622d644 82 u64 idxmsk64;
c91e0f5d 83 };
b622d644
PZ
84 u64 code;
85 u64 cmask;
272d30be 86 int weight;
1da53e02
SE
87};
88
38331f62
SE
89struct amd_nb {
90 int nb_id; /* NorthBridge id */
91 int refcnt; /* reference count */
92 struct perf_event *owners[X86_PMC_IDX_MAX];
93 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
94};
95
a7e3ed1e
AK
96struct intel_percore;
97
caff2bef
PZ
98#define MAX_LBR_ENTRIES 16
99
cdd6c482 100struct cpu_hw_events {
ca037701
PZ
101 /*
102 * Generic x86 PMC bits
103 */
1da53e02 104 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
43f6201a 105 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
63e6be6d 106 unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
b0f3f28e 107 int enabled;
241771ef 108
1da53e02
SE
109 int n_events;
110 int n_added;
90151c35 111 int n_txn;
1da53e02 112 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
447a194b 113 u64 tags[X86_PMC_IDX_MAX];
1da53e02 114 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
ca037701 115
4d1c52b0
LM
116 unsigned int group_flag;
117
ca037701
PZ
118 /*
119 * Intel DebugStore bits
120 */
121 struct debug_store *ds;
122 u64 pebs_enabled;
123
caff2bef
PZ
124 /*
125 * Intel LBR bits
126 */
127 int lbr_users;
128 void *lbr_context;
129 struct perf_branch_stack lbr_stack;
130 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
131
a7e3ed1e
AK
132 /*
133 * Intel percore register state.
134 * Coordinate shared resources between HT threads.
135 */
136 int percore_used; /* Used by this CPU? */
137 struct intel_percore *per_core;
138
ca037701
PZ
139 /*
140 * AMD specific bits
141 */
38331f62 142 struct amd_nb *amd_nb;
b690081d
SE
143};
144
fce877e3 145#define __EVENT_CONSTRAINT(c, n, m, w) {\
b622d644 146 { .idxmsk64 = (n) }, \
c91e0f5d
PZ
147 .code = (c), \
148 .cmask = (m), \
fce877e3 149 .weight = (w), \
c91e0f5d 150}
b690081d 151
fce877e3
PZ
152#define EVENT_CONSTRAINT(c, n, m) \
153 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
154
ca037701
PZ
155/*
156 * Constraint on the Event code.
157 */
ed8777fc 158#define INTEL_EVENT_CONSTRAINT(c, n) \
a098f448 159 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
8433be11 160
ca037701
PZ
161/*
162 * Constraint on the Event code + UMask + fixed-mask
a098f448
RR
163 *
164 * filter mask to validate fixed counter events.
165 * the following filters disqualify for fixed counters:
166 * - inv
167 * - edge
168 * - cnt-mask
169 * The other filters are supported by fixed counters.
170 * The any-thread option is supported starting with v3.
ca037701 171 */
ed8777fc 172#define FIXED_EVENT_CONSTRAINT(c, n) \
a098f448 173 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
8433be11 174
ca037701
PZ
175/*
176 * Constraint on the Event code + UMask
177 */
b06b3d49 178#define INTEL_UEVENT_CONSTRAINT(c, n) \
ca037701 179 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
b06b3d49
LM
180#define PEBS_EVENT_CONSTRAINT(c, n) \
181 INTEL_UEVENT_CONSTRAINT(c, n)
ca037701 182
ed8777fc
PZ
183#define EVENT_CONSTRAINT_END \
184 EVENT_CONSTRAINT(0, 0, 0)
185
186#define for_each_event_constraint(e, c) \
a1f2b70a 187 for ((e) = (c); (e)->weight; (e)++)
b690081d 188
a7e3ed1e
AK
189/*
190 * Extra registers for specific events.
191 * Some events need large masks and require external MSRs.
192 * Define a mapping to these extra registers.
193 */
194struct extra_reg {
195 unsigned int event;
196 unsigned int msr;
197 u64 config_mask;
198 u64 valid_mask;
199};
200
201#define EVENT_EXTRA_REG(e, ms, m, vm) { \
202 .event = (e), \
203 .msr = (ms), \
204 .config_mask = (m), \
205 .valid_mask = (vm), \
206 }
207#define INTEL_EVENT_EXTRA_REG(event, msr, vm) \
208 EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm)
209#define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0)
210
8db909a7
PZ
211union perf_capabilities {
212 struct {
213 u64 lbr_format : 6;
214 u64 pebs_trap : 1;
215 u64 pebs_arch_reg : 1;
216 u64 pebs_format : 4;
217 u64 smm_freeze : 1;
218 };
219 u64 capabilities;
220};
221
241771ef 222/*
5f4ec28f 223 * struct x86_pmu - generic x86 pmu
241771ef 224 */
5f4ec28f 225struct x86_pmu {
ca037701
PZ
226 /*
227 * Generic x86 PMC bits
228 */
faa28ae0
RR
229 const char *name;
230 int version;
a3288106 231 int (*handle_irq)(struct pt_regs *);
9e35ad38 232 void (*disable_all)(void);
11164cd4 233 void (*enable_all)(int added);
aff3d91a
PZ
234 void (*enable)(struct perf_event *);
235 void (*disable)(struct perf_event *);
b4cdc5c2 236 int (*hw_config)(struct perf_event *event);
a072738e 237 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
169e41eb
JSR
238 unsigned eventsel;
239 unsigned perfctr;
b0f3f28e 240 u64 (*event_map)(int);
169e41eb 241 int max_events;
948b1bb8
RR
242 int num_counters;
243 int num_counters_fixed;
244 int cntval_bits;
245 u64 cntval_mask;
04da8a43 246 int apic;
c619b8ff 247 u64 max_period;
63b14649
PZ
248 struct event_constraint *
249 (*get_event_constraints)(struct cpu_hw_events *cpuc,
250 struct perf_event *event);
251
c91e0f5d
PZ
252 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
253 struct perf_event *event);
63b14649 254 struct event_constraint *event_constraints;
a7e3ed1e 255 struct event_constraint *percore_constraints;
3c44780b 256 void (*quirks)(void);
68aa00ac 257 int perfctr_second_write;
3f6da390 258
b38b24ea 259 int (*cpu_prepare)(int cpu);
3f6da390
PZ
260 void (*cpu_starting)(int cpu);
261 void (*cpu_dying)(int cpu);
262 void (*cpu_dead)(int cpu);
ca037701
PZ
263
264 /*
265 * Intel Arch Perfmon v2+
266 */
8db909a7
PZ
267 u64 intel_ctrl;
268 union perf_capabilities intel_cap;
ca037701
PZ
269
270 /*
271 * Intel DebugStore bits
272 */
273 int bts, pebs;
6809b6ea 274 int bts_active, pebs_active;
ca037701
PZ
275 int pebs_record_size;
276 void (*drain_pebs)(struct pt_regs *regs);
277 struct event_constraint *pebs_constraints;
caff2bef
PZ
278
279 /*
280 * Intel LBR
281 */
282 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
283 int lbr_nr; /* hardware stack size */
a7e3ed1e
AK
284
285 /*
286 * Extra registers for events
287 */
288 struct extra_reg *extra_regs;
b56a3802
JSR
289};
290
4a06bd85 291static struct x86_pmu x86_pmu __read_mostly;
b56a3802 292
cdd6c482 293static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
b0f3f28e
PZ
294 .enabled = 1,
295};
241771ef 296
07088edb 297static int x86_perf_event_set_period(struct perf_event *event);
b690081d 298
8326f44d 299/*
dfc65094 300 * Generalized hw caching related hw_event table, filled
8326f44d 301 * in on a per model basis. A value of 0 means
dfc65094
IM
302 * 'not supported', -1 means 'hw_event makes no sense on
303 * this CPU', any other value means the raw hw_event
8326f44d
IM
304 * ID.
305 */
306
307#define C(x) PERF_COUNT_HW_CACHE_##x
308
309static u64 __read_mostly hw_cache_event_ids
310 [PERF_COUNT_HW_CACHE_MAX]
311 [PERF_COUNT_HW_CACHE_OP_MAX]
312 [PERF_COUNT_HW_CACHE_RESULT_MAX];
e994d7d2
AK
313static u64 __read_mostly hw_cache_extra_regs
314 [PERF_COUNT_HW_CACHE_MAX]
315 [PERF_COUNT_HW_CACHE_OP_MAX]
316 [PERF_COUNT_HW_CACHE_RESULT_MAX];
8326f44d 317
ee06094f 318/*
cdd6c482
IM
319 * Propagate event elapsed time into the generic event.
320 * Can only be executed on the CPU where the event is active.
ee06094f
IM
321 * Returns the delta events processed.
322 */
4b7bfd0d 323static u64
cc2ad4ba 324x86_perf_event_update(struct perf_event *event)
ee06094f 325{
cc2ad4ba 326 struct hw_perf_event *hwc = &event->hw;
948b1bb8 327 int shift = 64 - x86_pmu.cntval_bits;
ec3232bd 328 u64 prev_raw_count, new_raw_count;
cc2ad4ba 329 int idx = hwc->idx;
ec3232bd 330 s64 delta;
ee06094f 331
30dd568c
MM
332 if (idx == X86_PMC_IDX_FIXED_BTS)
333 return 0;
334
ee06094f 335 /*
cdd6c482 336 * Careful: an NMI might modify the previous event value.
ee06094f
IM
337 *
338 * Our tactic to handle this is to first atomically read and
339 * exchange a new raw count - then add that new-prev delta
cdd6c482 340 * count to the generic event atomically:
ee06094f
IM
341 */
342again:
e7850595 343 prev_raw_count = local64_read(&hwc->prev_count);
73d6e522 344 rdmsrl(hwc->event_base, new_raw_count);
ee06094f 345
e7850595 346 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
ee06094f
IM
347 new_raw_count) != prev_raw_count)
348 goto again;
349
350 /*
351 * Now we have the new raw value and have updated the prev
352 * timestamp already. We can now calculate the elapsed delta
cdd6c482 353 * (event-)time and add that to the generic event.
ee06094f
IM
354 *
355 * Careful, not all hw sign-extends above the physical width
ec3232bd 356 * of the count.
ee06094f 357 */
ec3232bd
PZ
358 delta = (new_raw_count << shift) - (prev_raw_count << shift);
359 delta >>= shift;
ee06094f 360
e7850595
PZ
361 local64_add(delta, &event->count);
362 local64_sub(delta, &hwc->period_left);
4b7bfd0d
RR
363
364 return new_raw_count;
ee06094f
IM
365}
366
4979d272
RR
367/* using X86_FEATURE_PERFCTR_CORE to later implement ALTERNATIVE() here */
368static inline int x86_pmu_addr_offset(int index)
369{
370 if (boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
371 return index << 1;
372 return index;
373}
374
41bf4989
RR
375static inline unsigned int x86_pmu_config_addr(int index)
376{
4979d272 377 return x86_pmu.eventsel + x86_pmu_addr_offset(index);
41bf4989
RR
378}
379
380static inline unsigned int x86_pmu_event_addr(int index)
381{
4979d272 382 return x86_pmu.perfctr + x86_pmu_addr_offset(index);
41bf4989
RR
383}
384
a7e3ed1e
AK
385/*
386 * Find and validate any extra registers to set up.
387 */
388static int x86_pmu_extra_regs(u64 config, struct perf_event *event)
389{
390 struct extra_reg *er;
391
392 event->hw.extra_reg = 0;
393 event->hw.extra_config = 0;
394
395 if (!x86_pmu.extra_regs)
396 return 0;
397
398 for (er = x86_pmu.extra_regs; er->msr; er++) {
399 if (er->event != (config & er->config_mask))
400 continue;
401 if (event->attr.config1 & ~er->valid_mask)
402 return -EINVAL;
403 event->hw.extra_reg = er->msr;
404 event->hw.extra_config = event->attr.config1;
405 break;
406 }
407 return 0;
408}
409
cdd6c482 410static atomic_t active_events;
4e935e47
PZ
411static DEFINE_MUTEX(pmc_reserve_mutex);
412
b27ea29c
RR
413#ifdef CONFIG_X86_LOCAL_APIC
414
4e935e47
PZ
415static bool reserve_pmc_hardware(void)
416{
417 int i;
418
948b1bb8 419 for (i = 0; i < x86_pmu.num_counters; i++) {
41bf4989 420 if (!reserve_perfctr_nmi(x86_pmu_event_addr(i)))
4e935e47
PZ
421 goto perfctr_fail;
422 }
423
948b1bb8 424 for (i = 0; i < x86_pmu.num_counters; i++) {
41bf4989 425 if (!reserve_evntsel_nmi(x86_pmu_config_addr(i)))
4e935e47
PZ
426 goto eventsel_fail;
427 }
428
429 return true;
430
431eventsel_fail:
432 for (i--; i >= 0; i--)
41bf4989 433 release_evntsel_nmi(x86_pmu_config_addr(i));
4e935e47 434
948b1bb8 435 i = x86_pmu.num_counters;
4e935e47
PZ
436
437perfctr_fail:
438 for (i--; i >= 0; i--)
41bf4989 439 release_perfctr_nmi(x86_pmu_event_addr(i));
4e935e47 440
4e935e47
PZ
441 return false;
442}
443
444static void release_pmc_hardware(void)
445{
446 int i;
447
948b1bb8 448 for (i = 0; i < x86_pmu.num_counters; i++) {
41bf4989
RR
449 release_perfctr_nmi(x86_pmu_event_addr(i));
450 release_evntsel_nmi(x86_pmu_config_addr(i));
4e935e47 451 }
4e935e47
PZ
452}
453
b27ea29c
RR
454#else
455
456static bool reserve_pmc_hardware(void) { return true; }
457static void release_pmc_hardware(void) {}
458
459#endif
460
33c6d6a7
DZ
461static bool check_hw_exists(void)
462{
463 u64 val, val_new = 0;
4407204c 464 int i, reg, ret = 0;
33c6d6a7 465
4407204c
PZ
466 /*
467 * Check to see if the BIOS enabled any of the counters, if so
468 * complain and bail.
469 */
470 for (i = 0; i < x86_pmu.num_counters; i++) {
41bf4989 471 reg = x86_pmu_config_addr(i);
4407204c
PZ
472 ret = rdmsrl_safe(reg, &val);
473 if (ret)
474 goto msr_fail;
475 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
476 goto bios_fail;
477 }
478
479 if (x86_pmu.num_counters_fixed) {
480 reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
481 ret = rdmsrl_safe(reg, &val);
482 if (ret)
483 goto msr_fail;
484 for (i = 0; i < x86_pmu.num_counters_fixed; i++) {
485 if (val & (0x03 << i*4))
486 goto bios_fail;
487 }
488 }
489
490 /*
491 * Now write a value and read it back to see if it matches,
492 * this is needed to detect certain hardware emulators (qemu/kvm)
493 * that don't trap on the MSR access and always return 0s.
494 */
33c6d6a7 495 val = 0xabcdUL;
41bf4989
RR
496 ret = checking_wrmsrl(x86_pmu_event_addr(0), val);
497 ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new);
33c6d6a7 498 if (ret || val != val_new)
4407204c 499 goto msr_fail;
33c6d6a7
DZ
500
501 return true;
4407204c
PZ
502
503bios_fail:
504 printk(KERN_CONT "Broken BIOS detected, using software events only.\n");
505 printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val);
506 return false;
507
508msr_fail:
509 printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n");
510 return false;
33c6d6a7
DZ
511}
512
f80c9e30 513static void reserve_ds_buffers(void);
ca037701 514static void release_ds_buffers(void);
30dd568c 515
cdd6c482 516static void hw_perf_event_destroy(struct perf_event *event)
4e935e47 517{
cdd6c482 518 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
4e935e47 519 release_pmc_hardware();
ca037701 520 release_ds_buffers();
4e935e47
PZ
521 mutex_unlock(&pmc_reserve_mutex);
522 }
523}
524
85cf9dba
RR
525static inline int x86_pmu_initialized(void)
526{
527 return x86_pmu.handle_irq != NULL;
528}
529
8326f44d 530static inline int
e994d7d2 531set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event)
8326f44d 532{
e994d7d2 533 struct perf_event_attr *attr = &event->attr;
8326f44d
IM
534 unsigned int cache_type, cache_op, cache_result;
535 u64 config, val;
536
537 config = attr->config;
538
539 cache_type = (config >> 0) & 0xff;
540 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
541 return -EINVAL;
542
543 cache_op = (config >> 8) & 0xff;
544 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
545 return -EINVAL;
546
547 cache_result = (config >> 16) & 0xff;
548 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
549 return -EINVAL;
550
551 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
552
553 if (val == 0)
554 return -ENOENT;
555
556 if (val == -1)
557 return -EINVAL;
558
559 hwc->config |= val;
e994d7d2
AK
560 attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result];
561 return x86_pmu_extra_regs(val, event);
8326f44d
IM
562}
563
c1726f34
RR
564static int x86_setup_perfctr(struct perf_event *event)
565{
566 struct perf_event_attr *attr = &event->attr;
567 struct hw_perf_event *hwc = &event->hw;
568 u64 config;
569
6c7e550f 570 if (!is_sampling_event(event)) {
c1726f34
RR
571 hwc->sample_period = x86_pmu.max_period;
572 hwc->last_period = hwc->sample_period;
e7850595 573 local64_set(&hwc->period_left, hwc->sample_period);
c1726f34
RR
574 } else {
575 /*
576 * If we have a PMU initialized but no APIC
577 * interrupts, we cannot sample hardware
578 * events (user-space has to fall back and
579 * sample via a hrtimer based software event):
580 */
581 if (!x86_pmu.apic)
582 return -EOPNOTSUPP;
583 }
584
585 if (attr->type == PERF_TYPE_RAW)
e994d7d2 586 return x86_pmu_extra_regs(event->attr.config, event);
c1726f34
RR
587
588 if (attr->type == PERF_TYPE_HW_CACHE)
e994d7d2 589 return set_ext_hw_attr(hwc, event);
c1726f34
RR
590
591 if (attr->config >= x86_pmu.max_events)
592 return -EINVAL;
593
594 /*
595 * The generic map:
596 */
597 config = x86_pmu.event_map(attr->config);
598
599 if (config == 0)
600 return -ENOENT;
601
602 if (config == -1LL)
603 return -EINVAL;
604
605 /*
606 * Branch tracing:
607 */
608 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
609 (hwc->sample_period == 1)) {
610 /* BTS is not supported by this architecture. */
6809b6ea 611 if (!x86_pmu.bts_active)
c1726f34
RR
612 return -EOPNOTSUPP;
613
614 /* BTS is currently only allowed for user-mode. */
615 if (!attr->exclude_kernel)
616 return -EOPNOTSUPP;
617 }
618
619 hwc->config |= config;
620
621 return 0;
622}
4261e0e0 623
b4cdc5c2 624static int x86_pmu_hw_config(struct perf_event *event)
a072738e 625{
ab608344
PZ
626 if (event->attr.precise_ip) {
627 int precise = 0;
628
629 /* Support for constant skid */
6809b6ea 630 if (x86_pmu.pebs_active) {
ab608344
PZ
631 precise++;
632
5553be26
PZ
633 /* Support for IP fixup */
634 if (x86_pmu.lbr_nr)
635 precise++;
636 }
ab608344
PZ
637
638 if (event->attr.precise_ip > precise)
639 return -EOPNOTSUPP;
640 }
641
a072738e
CG
642 /*
643 * Generate PMC IRQs:
644 * (keep 'enabled' bit clear for now)
645 */
b4cdc5c2 646 event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
a072738e
CG
647
648 /*
649 * Count user and OS events unless requested not to
650 */
b4cdc5c2
PZ
651 if (!event->attr.exclude_user)
652 event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
653 if (!event->attr.exclude_kernel)
654 event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
a072738e 655
b4cdc5c2
PZ
656 if (event->attr.type == PERF_TYPE_RAW)
657 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
a072738e 658
9d0fcba6 659 return x86_setup_perfctr(event);
a098f448
RR
660}
661
241771ef 662/*
0d48696f 663 * Setup the hardware configuration for a given attr_type
241771ef 664 */
b0a873eb 665static int __x86_pmu_event_init(struct perf_event *event)
241771ef 666{
4e935e47 667 int err;
241771ef 668
85cf9dba
RR
669 if (!x86_pmu_initialized())
670 return -ENODEV;
241771ef 671
4e935e47 672 err = 0;
cdd6c482 673 if (!atomic_inc_not_zero(&active_events)) {
4e935e47 674 mutex_lock(&pmc_reserve_mutex);
cdd6c482 675 if (atomic_read(&active_events) == 0) {
30dd568c
MM
676 if (!reserve_pmc_hardware())
677 err = -EBUSY;
f80c9e30
PZ
678 else
679 reserve_ds_buffers();
30dd568c
MM
680 }
681 if (!err)
cdd6c482 682 atomic_inc(&active_events);
4e935e47
PZ
683 mutex_unlock(&pmc_reserve_mutex);
684 }
685 if (err)
686 return err;
687
cdd6c482 688 event->destroy = hw_perf_event_destroy;
a1792cda 689
4261e0e0
RR
690 event->hw.idx = -1;
691 event->hw.last_cpu = -1;
692 event->hw.last_tag = ~0ULL;
b690081d 693
9d0fcba6 694 return x86_pmu.hw_config(event);
4261e0e0
RR
695}
696
8c48e444 697static void x86_pmu_disable_all(void)
f87ad35d 698{
cdd6c482 699 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
9e35ad38
PZ
700 int idx;
701
948b1bb8 702 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
b0f3f28e
PZ
703 u64 val;
704
43f6201a 705 if (!test_bit(idx, cpuc->active_mask))
4295ee62 706 continue;
41bf4989 707 rdmsrl(x86_pmu_config_addr(idx), val);
bb1165d6 708 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
4295ee62 709 continue;
bb1165d6 710 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
41bf4989 711 wrmsrl(x86_pmu_config_addr(idx), val);
f87ad35d 712 }
f87ad35d
JSR
713}
714
a4eaf7f1 715static void x86_pmu_disable(struct pmu *pmu)
b56a3802 716{
1da53e02
SE
717 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
718
85cf9dba 719 if (!x86_pmu_initialized())
9e35ad38 720 return;
1da53e02 721
1a6e21f7
PZ
722 if (!cpuc->enabled)
723 return;
724
725 cpuc->n_added = 0;
726 cpuc->enabled = 0;
727 barrier();
1da53e02
SE
728
729 x86_pmu.disable_all();
b56a3802 730}
241771ef 731
d45dd923
RR
732static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
733 u64 enable_mask)
734{
a7e3ed1e
AK
735 if (hwc->extra_reg)
736 wrmsrl(hwc->extra_reg, hwc->extra_config);
73d6e522 737 wrmsrl(hwc->config_base, hwc->config | enable_mask);
d45dd923
RR
738}
739
11164cd4 740static void x86_pmu_enable_all(int added)
f87ad35d 741{
cdd6c482 742 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
f87ad35d
JSR
743 int idx;
744
948b1bb8 745 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
d45dd923 746 struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
b0f3f28e 747
43f6201a 748 if (!test_bit(idx, cpuc->active_mask))
4295ee62 749 continue;
984b838c 750
d45dd923 751 __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
f87ad35d
JSR
752 }
753}
754
51b0fe39 755static struct pmu pmu;
1da53e02
SE
756
757static inline int is_x86_event(struct perf_event *event)
758{
759 return event->pmu == &pmu;
760}
761
762static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
763{
63b14649 764 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
1da53e02 765 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
c933c1a6 766 int i, j, w, wmax, num = 0;
1da53e02
SE
767 struct hw_perf_event *hwc;
768
769 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
770
771 for (i = 0; i < n; i++) {
b622d644
PZ
772 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
773 constraints[i] = c;
1da53e02
SE
774 }
775
8113070d
SE
776 /*
777 * fastpath, try to reuse previous register
778 */
c933c1a6 779 for (i = 0; i < n; i++) {
8113070d 780 hwc = &cpuc->event_list[i]->hw;
81269a08 781 c = constraints[i];
8113070d
SE
782
783 /* never assigned */
784 if (hwc->idx == -1)
785 break;
786
787 /* constraint still honored */
63b14649 788 if (!test_bit(hwc->idx, c->idxmsk))
8113070d
SE
789 break;
790
791 /* not already used */
792 if (test_bit(hwc->idx, used_mask))
793 break;
794
34538ee7 795 __set_bit(hwc->idx, used_mask);
8113070d
SE
796 if (assign)
797 assign[i] = hwc->idx;
798 }
c933c1a6 799 if (i == n)
8113070d
SE
800 goto done;
801
802 /*
803 * begin slow path
804 */
805
806 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
807
1da53e02
SE
808 /*
809 * weight = number of possible counters
810 *
811 * 1 = most constrained, only works on one counter
812 * wmax = least constrained, works on any counter
813 *
814 * assign events to counters starting with most
815 * constrained events.
816 */
948b1bb8 817 wmax = x86_pmu.num_counters;
1da53e02
SE
818
819 /*
820 * when fixed event counters are present,
821 * wmax is incremented by 1 to account
822 * for one more choice
823 */
948b1bb8 824 if (x86_pmu.num_counters_fixed)
1da53e02
SE
825 wmax++;
826
8113070d 827 for (w = 1, num = n; num && w <= wmax; w++) {
1da53e02 828 /* for each event */
8113070d 829 for (i = 0; num && i < n; i++) {
81269a08 830 c = constraints[i];
1da53e02
SE
831 hwc = &cpuc->event_list[i]->hw;
832
272d30be 833 if (c->weight != w)
1da53e02
SE
834 continue;
835
984b3f57 836 for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
1da53e02
SE
837 if (!test_bit(j, used_mask))
838 break;
839 }
840
841 if (j == X86_PMC_IDX_MAX)
842 break;
1da53e02 843
34538ee7 844 __set_bit(j, used_mask);
8113070d 845
1da53e02
SE
846 if (assign)
847 assign[i] = j;
848 num--;
849 }
850 }
8113070d 851done:
1da53e02
SE
852 /*
853 * scheduling failed or is just a simulation,
854 * free resources if necessary
855 */
856 if (!assign || num) {
857 for (i = 0; i < n; i++) {
858 if (x86_pmu.put_event_constraints)
859 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
860 }
861 }
862 return num ? -ENOSPC : 0;
863}
864
865/*
866 * dogrp: true if must collect siblings events (group)
867 * returns total number of events and error code
868 */
869static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
870{
871 struct perf_event *event;
872 int n, max_count;
873
948b1bb8 874 max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
1da53e02
SE
875
876 /* current number of events already accepted */
877 n = cpuc->n_events;
878
879 if (is_x86_event(leader)) {
880 if (n >= max_count)
881 return -ENOSPC;
882 cpuc->event_list[n] = leader;
883 n++;
884 }
885 if (!dogrp)
886 return n;
887
888 list_for_each_entry(event, &leader->sibling_list, group_entry) {
889 if (!is_x86_event(event) ||
8113070d 890 event->state <= PERF_EVENT_STATE_OFF)
1da53e02
SE
891 continue;
892
893 if (n >= max_count)
894 return -ENOSPC;
895
896 cpuc->event_list[n] = event;
897 n++;
898 }
899 return n;
900}
901
1da53e02 902static inline void x86_assign_hw_event(struct perf_event *event,
447a194b 903 struct cpu_hw_events *cpuc, int i)
1da53e02 904{
447a194b
SE
905 struct hw_perf_event *hwc = &event->hw;
906
907 hwc->idx = cpuc->assign[i];
908 hwc->last_cpu = smp_processor_id();
909 hwc->last_tag = ++cpuc->tags[i];
1da53e02
SE
910
911 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
912 hwc->config_base = 0;
913 hwc->event_base = 0;
914 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
915 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
73d6e522 916 hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0;
1da53e02 917 } else {
73d6e522
RR
918 hwc->config_base = x86_pmu_config_addr(hwc->idx);
919 hwc->event_base = x86_pmu_event_addr(hwc->idx);
1da53e02
SE
920 }
921}
922
447a194b
SE
923static inline int match_prev_assignment(struct hw_perf_event *hwc,
924 struct cpu_hw_events *cpuc,
925 int i)
926{
927 return hwc->idx == cpuc->assign[i] &&
928 hwc->last_cpu == smp_processor_id() &&
929 hwc->last_tag == cpuc->tags[i];
930}
931
a4eaf7f1
PZ
932static void x86_pmu_start(struct perf_event *event, int flags);
933static void x86_pmu_stop(struct perf_event *event, int flags);
2e841873 934
a4eaf7f1 935static void x86_pmu_enable(struct pmu *pmu)
ee06094f 936{
1da53e02
SE
937 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
938 struct perf_event *event;
939 struct hw_perf_event *hwc;
11164cd4 940 int i, added = cpuc->n_added;
1da53e02 941
85cf9dba 942 if (!x86_pmu_initialized())
2b9ff0db 943 return;
1a6e21f7
PZ
944
945 if (cpuc->enabled)
946 return;
947
1da53e02 948 if (cpuc->n_added) {
19925ce7 949 int n_running = cpuc->n_events - cpuc->n_added;
1da53e02
SE
950 /*
951 * apply assignment obtained either from
952 * hw_perf_group_sched_in() or x86_pmu_enable()
953 *
954 * step1: save events moving to new counters
955 * step2: reprogram moved events into new counters
956 */
19925ce7 957 for (i = 0; i < n_running; i++) {
1da53e02
SE
958 event = cpuc->event_list[i];
959 hwc = &event->hw;
960
447a194b
SE
961 /*
962 * we can avoid reprogramming counter if:
963 * - assigned same counter as last time
964 * - running on same CPU as last time
965 * - no other event has used the counter since
966 */
967 if (hwc->idx == -1 ||
968 match_prev_assignment(hwc, cpuc, i))
1da53e02
SE
969 continue;
970
a4eaf7f1
PZ
971 /*
972 * Ensure we don't accidentally enable a stopped
973 * counter simply because we rescheduled.
974 */
975 if (hwc->state & PERF_HES_STOPPED)
976 hwc->state |= PERF_HES_ARCH;
977
978 x86_pmu_stop(event, PERF_EF_UPDATE);
1da53e02
SE
979 }
980
981 for (i = 0; i < cpuc->n_events; i++) {
1da53e02
SE
982 event = cpuc->event_list[i];
983 hwc = &event->hw;
984
45e16a68 985 if (!match_prev_assignment(hwc, cpuc, i))
447a194b 986 x86_assign_hw_event(event, cpuc, i);
45e16a68
PZ
987 else if (i < n_running)
988 continue;
1da53e02 989
a4eaf7f1
PZ
990 if (hwc->state & PERF_HES_ARCH)
991 continue;
992
993 x86_pmu_start(event, PERF_EF_RELOAD);
1da53e02
SE
994 }
995 cpuc->n_added = 0;
996 perf_events_lapic_init();
997 }
1a6e21f7
PZ
998
999 cpuc->enabled = 1;
1000 barrier();
1001
11164cd4 1002 x86_pmu.enable_all(added);
ee06094f 1003}
ee06094f 1004
aff3d91a 1005static inline void x86_pmu_disable_event(struct perf_event *event)
b0f3f28e 1006{
aff3d91a 1007 struct hw_perf_event *hwc = &event->hw;
7645a24c 1008
73d6e522 1009 wrmsrl(hwc->config_base, hwc->config);
b0f3f28e
PZ
1010}
1011
245b2e70 1012static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
241771ef 1013
ee06094f
IM
1014/*
1015 * Set the next IRQ period, based on the hwc->period_left value.
cdd6c482 1016 * To be called with the event disabled in hw:
ee06094f 1017 */
e4abb5d4 1018static int
07088edb 1019x86_perf_event_set_period(struct perf_event *event)
241771ef 1020{
07088edb 1021 struct hw_perf_event *hwc = &event->hw;
e7850595 1022 s64 left = local64_read(&hwc->period_left);
e4abb5d4 1023 s64 period = hwc->sample_period;
7645a24c 1024 int ret = 0, idx = hwc->idx;
ee06094f 1025
30dd568c
MM
1026 if (idx == X86_PMC_IDX_FIXED_BTS)
1027 return 0;
1028
ee06094f 1029 /*
af901ca1 1030 * If we are way outside a reasonable range then just skip forward:
ee06094f
IM
1031 */
1032 if (unlikely(left <= -period)) {
1033 left = period;
e7850595 1034 local64_set(&hwc->period_left, left);
9e350de3 1035 hwc->last_period = period;
e4abb5d4 1036 ret = 1;
ee06094f
IM
1037 }
1038
1039 if (unlikely(left <= 0)) {
1040 left += period;
e7850595 1041 local64_set(&hwc->period_left, left);
9e350de3 1042 hwc->last_period = period;
e4abb5d4 1043 ret = 1;
ee06094f 1044 }
1c80f4b5 1045 /*
dfc65094 1046 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
1c80f4b5
IM
1047 */
1048 if (unlikely(left < 2))
1049 left = 2;
241771ef 1050
e4abb5d4
PZ
1051 if (left > x86_pmu.max_period)
1052 left = x86_pmu.max_period;
1053
245b2e70 1054 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
ee06094f
IM
1055
1056 /*
cdd6c482 1057 * The hw event starts counting from this event offset,
ee06094f
IM
1058 * mark it to be able to extra future deltas:
1059 */
e7850595 1060 local64_set(&hwc->prev_count, (u64)-left);
ee06094f 1061
73d6e522 1062 wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask);
68aa00ac
CG
1063
1064 /*
1065 * Due to erratum on certan cpu we need
1066 * a second write to be sure the register
1067 * is updated properly
1068 */
1069 if (x86_pmu.perfctr_second_write) {
73d6e522 1070 wrmsrl(hwc->event_base,
948b1bb8 1071 (u64)(-left) & x86_pmu.cntval_mask);
68aa00ac 1072 }
e4abb5d4 1073
cdd6c482 1074 perf_event_update_userpage(event);
194002b2 1075
e4abb5d4 1076 return ret;
2f18d1e8
IM
1077}
1078
aff3d91a 1079static void x86_pmu_enable_event(struct perf_event *event)
7c90cc45 1080{
0a3aee0d 1081 if (__this_cpu_read(cpu_hw_events.enabled))
31fa58af
RR
1082 __x86_pmu_enable_event(&event->hw,
1083 ARCH_PERFMON_EVENTSEL_ENABLE);
241771ef
IM
1084}
1085
b690081d 1086/*
a4eaf7f1 1087 * Add a single event to the PMU.
1da53e02
SE
1088 *
1089 * The event is added to the group of enabled events
1090 * but only if it can be scehduled with existing events.
fe9081cc 1091 */
a4eaf7f1 1092static int x86_pmu_add(struct perf_event *event, int flags)
fe9081cc
PZ
1093{
1094 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1da53e02
SE
1095 struct hw_perf_event *hwc;
1096 int assign[X86_PMC_IDX_MAX];
1097 int n, n0, ret;
fe9081cc 1098
1da53e02 1099 hwc = &event->hw;
fe9081cc 1100
33696fc0 1101 perf_pmu_disable(event->pmu);
1da53e02 1102 n0 = cpuc->n_events;
24cd7f54
PZ
1103 ret = n = collect_events(cpuc, event, false);
1104 if (ret < 0)
1105 goto out;
53b441a5 1106
a4eaf7f1
PZ
1107 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
1108 if (!(flags & PERF_EF_START))
1109 hwc->state |= PERF_HES_ARCH;
1110
4d1c52b0
LM
1111 /*
1112 * If group events scheduling transaction was started,
1113 * skip the schedulability test here, it will be peformed
a4eaf7f1 1114 * at commit time (->commit_txn) as a whole
4d1c52b0 1115 */
8d2cacbb 1116 if (cpuc->group_flag & PERF_EVENT_TXN)
24cd7f54 1117 goto done_collect;
4d1c52b0 1118
a072738e 1119 ret = x86_pmu.schedule_events(cpuc, n, assign);
1da53e02 1120 if (ret)
24cd7f54 1121 goto out;
1da53e02
SE
1122 /*
1123 * copy new assignment, now we know it is possible
1124 * will be used by hw_perf_enable()
1125 */
1126 memcpy(cpuc->assign, assign, n*sizeof(int));
7e2ae347 1127
24cd7f54 1128done_collect:
1da53e02 1129 cpuc->n_events = n;
356e1f2e 1130 cpuc->n_added += n - n0;
90151c35 1131 cpuc->n_txn += n - n0;
95cdd2e7 1132
24cd7f54
PZ
1133 ret = 0;
1134out:
33696fc0 1135 perf_pmu_enable(event->pmu);
24cd7f54 1136 return ret;
241771ef
IM
1137}
1138
a4eaf7f1 1139static void x86_pmu_start(struct perf_event *event, int flags)
d76a0812 1140{
c08053e6
PZ
1141 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1142 int idx = event->hw.idx;
1143
a4eaf7f1
PZ
1144 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
1145 return;
1146
1147 if (WARN_ON_ONCE(idx == -1))
1148 return;
1149
1150 if (flags & PERF_EF_RELOAD) {
1151 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
1152 x86_perf_event_set_period(event);
1153 }
1154
1155 event->hw.state = 0;
d76a0812 1156
c08053e6
PZ
1157 cpuc->events[idx] = event;
1158 __set_bit(idx, cpuc->active_mask);
63e6be6d 1159 __set_bit(idx, cpuc->running);
aff3d91a 1160 x86_pmu.enable(event);
c08053e6 1161 perf_event_update_userpage(event);
a78ac325
PZ
1162}
1163
cdd6c482 1164void perf_event_print_debug(void)
241771ef 1165{
2f18d1e8 1166 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
ca037701 1167 u64 pebs;
cdd6c482 1168 struct cpu_hw_events *cpuc;
5bb9efe3 1169 unsigned long flags;
1e125676
IM
1170 int cpu, idx;
1171
948b1bb8 1172 if (!x86_pmu.num_counters)
1e125676 1173 return;
241771ef 1174
5bb9efe3 1175 local_irq_save(flags);
241771ef
IM
1176
1177 cpu = smp_processor_id();
cdd6c482 1178 cpuc = &per_cpu(cpu_hw_events, cpu);
241771ef 1179
faa28ae0 1180 if (x86_pmu.version >= 2) {
a1ef58f4
JSR
1181 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1182 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1183 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1184 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
ca037701 1185 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
a1ef58f4
JSR
1186
1187 pr_info("\n");
1188 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1189 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1190 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1191 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
ca037701 1192 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
f87ad35d 1193 }
7645a24c 1194 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
241771ef 1195
948b1bb8 1196 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
41bf4989
RR
1197 rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl);
1198 rdmsrl(x86_pmu_event_addr(idx), pmc_count);
241771ef 1199
245b2e70 1200 prev_left = per_cpu(pmc_prev_left[idx], cpu);
241771ef 1201
a1ef58f4 1202 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
241771ef 1203 cpu, idx, pmc_ctrl);
a1ef58f4 1204 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
241771ef 1205 cpu, idx, pmc_count);
a1ef58f4 1206 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
ee06094f 1207 cpu, idx, prev_left);
241771ef 1208 }
948b1bb8 1209 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
2f18d1e8
IM
1210 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1211
a1ef58f4 1212 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
2f18d1e8
IM
1213 cpu, idx, pmc_count);
1214 }
5bb9efe3 1215 local_irq_restore(flags);
241771ef
IM
1216}
1217
a4eaf7f1 1218static void x86_pmu_stop(struct perf_event *event, int flags)
241771ef 1219{
d76a0812 1220 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
cdd6c482 1221 struct hw_perf_event *hwc = &event->hw;
241771ef 1222
a4eaf7f1
PZ
1223 if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) {
1224 x86_pmu.disable(event);
1225 cpuc->events[hwc->idx] = NULL;
1226 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
1227 hwc->state |= PERF_HES_STOPPED;
1228 }
30dd568c 1229
a4eaf7f1
PZ
1230 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
1231 /*
1232 * Drain the remaining delta count out of a event
1233 * that we are disabling:
1234 */
1235 x86_perf_event_update(event);
1236 hwc->state |= PERF_HES_UPTODATE;
1237 }
2e841873
PZ
1238}
1239
a4eaf7f1 1240static void x86_pmu_del(struct perf_event *event, int flags)
2e841873
PZ
1241{
1242 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1243 int i;
1244
90151c35
SE
1245 /*
1246 * If we're called during a txn, we don't need to do anything.
1247 * The events never got scheduled and ->cancel_txn will truncate
1248 * the event_list.
1249 */
8d2cacbb 1250 if (cpuc->group_flag & PERF_EVENT_TXN)
90151c35
SE
1251 return;
1252
a4eaf7f1 1253 x86_pmu_stop(event, PERF_EF_UPDATE);
194002b2 1254
1da53e02
SE
1255 for (i = 0; i < cpuc->n_events; i++) {
1256 if (event == cpuc->event_list[i]) {
1257
1258 if (x86_pmu.put_event_constraints)
1259 x86_pmu.put_event_constraints(cpuc, event);
1260
1261 while (++i < cpuc->n_events)
1262 cpuc->event_list[i-1] = cpuc->event_list[i];
1263
1264 --cpuc->n_events;
6c9687ab 1265 break;
1da53e02
SE
1266 }
1267 }
cdd6c482 1268 perf_event_update_userpage(event);
241771ef
IM
1269}
1270
8c48e444 1271static int x86_pmu_handle_irq(struct pt_regs *regs)
a29aa8a7 1272{
df1a132b 1273 struct perf_sample_data data;
cdd6c482
IM
1274 struct cpu_hw_events *cpuc;
1275 struct perf_event *event;
11d1578f 1276 int idx, handled = 0;
9029a5e3
IM
1277 u64 val;
1278
dc1d628a 1279 perf_sample_data_init(&data, 0);
df1a132b 1280
cdd6c482 1281 cpuc = &__get_cpu_var(cpu_hw_events);
962bf7a6 1282
948b1bb8 1283 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
63e6be6d
RR
1284 if (!test_bit(idx, cpuc->active_mask)) {
1285 /*
1286 * Though we deactivated the counter some cpus
1287 * might still deliver spurious interrupts still
1288 * in flight. Catch them:
1289 */
1290 if (__test_and_clear_bit(idx, cpuc->running))
1291 handled++;
a29aa8a7 1292 continue;
63e6be6d 1293 }
962bf7a6 1294
cdd6c482 1295 event = cpuc->events[idx];
a4016a79 1296
cc2ad4ba 1297 val = x86_perf_event_update(event);
948b1bb8 1298 if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
48e22d56 1299 continue;
962bf7a6 1300
9e350de3 1301 /*
cdd6c482 1302 * event overflow
9e350de3 1303 */
4177c42a 1304 handled++;
cdd6c482 1305 data.period = event->hw.last_period;
9e350de3 1306
07088edb 1307 if (!x86_perf_event_set_period(event))
e4abb5d4
PZ
1308 continue;
1309
cdd6c482 1310 if (perf_event_overflow(event, 1, &data, regs))
a4eaf7f1 1311 x86_pmu_stop(event, 0);
a29aa8a7 1312 }
962bf7a6 1313
9e350de3
PZ
1314 if (handled)
1315 inc_irq_stat(apic_perf_irqs);
1316
a29aa8a7
RR
1317 return handled;
1318}
39d81eab 1319
cdd6c482 1320void perf_events_lapic_init(void)
241771ef 1321{
04da8a43 1322 if (!x86_pmu.apic || !x86_pmu_initialized())
241771ef 1323 return;
85cf9dba 1324
241771ef 1325 /*
c323d95f 1326 * Always use NMI for PMU
241771ef 1327 */
c323d95f 1328 apic_write(APIC_LVTPC, APIC_DM_NMI);
241771ef
IM
1329}
1330
4177c42a
RR
1331struct pmu_nmi_state {
1332 unsigned int marked;
1333 int handled;
1334};
1335
1336static DEFINE_PER_CPU(struct pmu_nmi_state, pmu_nmi);
1337
241771ef 1338static int __kprobes
cdd6c482 1339perf_event_nmi_handler(struct notifier_block *self,
241771ef
IM
1340 unsigned long cmd, void *__args)
1341{
1342 struct die_args *args = __args;
4177c42a
RR
1343 unsigned int this_nmi;
1344 int handled;
b0f3f28e 1345
cdd6c482 1346 if (!atomic_read(&active_events))
63a809a2
PZ
1347 return NOTIFY_DONE;
1348
b0f3f28e
PZ
1349 switch (cmd) {
1350 case DIE_NMI:
b0f3f28e 1351 break;
4177c42a
RR
1352 case DIE_NMIUNKNOWN:
1353 this_nmi = percpu_read(irq_stat.__nmi_count);
0a3aee0d 1354 if (this_nmi != __this_cpu_read(pmu_nmi.marked))
4177c42a
RR
1355 /* let the kernel handle the unknown nmi */
1356 return NOTIFY_DONE;
1357 /*
1358 * This one is a PMU back-to-back nmi. Two events
1359 * trigger 'simultaneously' raising two back-to-back
1360 * NMIs. If the first NMI handles both, the latter
1361 * will be empty and daze the CPU. So, we drop it to
1362 * avoid false-positive 'unknown nmi' messages.
1363 */
1364 return NOTIFY_STOP;
b0f3f28e 1365 default:
241771ef 1366 return NOTIFY_DONE;
b0f3f28e 1367 }
241771ef 1368
241771ef 1369 apic_write(APIC_LVTPC, APIC_DM_NMI);
4177c42a
RR
1370
1371 handled = x86_pmu.handle_irq(args->regs);
1372 if (!handled)
1373 return NOTIFY_DONE;
1374
1375 this_nmi = percpu_read(irq_stat.__nmi_count);
1376 if ((handled > 1) ||
1377 /* the next nmi could be a back-to-back nmi */
0a3aee0d
TH
1378 ((__this_cpu_read(pmu_nmi.marked) == this_nmi) &&
1379 (__this_cpu_read(pmu_nmi.handled) > 1))) {
4177c42a
RR
1380 /*
1381 * We could have two subsequent back-to-back nmis: The
1382 * first handles more than one counter, the 2nd
1383 * handles only one counter and the 3rd handles no
1384 * counter.
1385 *
1386 * This is the 2nd nmi because the previous was
1387 * handling more than one counter. We will mark the
1388 * next (3rd) and then drop it if unhandled.
1389 */
0a3aee0d
TH
1390 __this_cpu_write(pmu_nmi.marked, this_nmi + 1);
1391 __this_cpu_write(pmu_nmi.handled, handled);
4177c42a 1392 }
241771ef 1393
a4016a79 1394 return NOTIFY_STOP;
241771ef
IM
1395}
1396
f22f54f4
PZ
1397static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1398 .notifier_call = perf_event_nmi_handler,
1399 .next = NULL,
166d7514 1400 .priority = NMI_LOCAL_LOW_PRIOR,
f22f54f4
PZ
1401};
1402
63b14649 1403static struct event_constraint unconstrained;
38331f62 1404static struct event_constraint emptyconstraint;
63b14649 1405
63b14649 1406static struct event_constraint *
f22f54f4 1407x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
1da53e02 1408{
63b14649 1409 struct event_constraint *c;
1da53e02 1410
1da53e02
SE
1411 if (x86_pmu.event_constraints) {
1412 for_each_event_constraint(c, x86_pmu.event_constraints) {
63b14649
PZ
1413 if ((event->hw.config & c->cmask) == c->code)
1414 return c;
1da53e02
SE
1415 }
1416 }
63b14649
PZ
1417
1418 return &unconstrained;
1da53e02
SE
1419}
1420
f22f54f4
PZ
1421#include "perf_event_amd.c"
1422#include "perf_event_p6.c"
a072738e 1423#include "perf_event_p4.c"
caff2bef 1424#include "perf_event_intel_lbr.c"
ca037701 1425#include "perf_event_intel_ds.c"
f22f54f4 1426#include "perf_event_intel.c"
f87ad35d 1427
3f6da390
PZ
1428static int __cpuinit
1429x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1430{
1431 unsigned int cpu = (long)hcpu;
b38b24ea 1432 int ret = NOTIFY_OK;
3f6da390
PZ
1433
1434 switch (action & ~CPU_TASKS_FROZEN) {
1435 case CPU_UP_PREPARE:
1436 if (x86_pmu.cpu_prepare)
b38b24ea 1437 ret = x86_pmu.cpu_prepare(cpu);
3f6da390
PZ
1438 break;
1439
1440 case CPU_STARTING:
1441 if (x86_pmu.cpu_starting)
1442 x86_pmu.cpu_starting(cpu);
1443 break;
1444
1445 case CPU_DYING:
1446 if (x86_pmu.cpu_dying)
1447 x86_pmu.cpu_dying(cpu);
1448 break;
1449
b38b24ea 1450 case CPU_UP_CANCELED:
3f6da390
PZ
1451 case CPU_DEAD:
1452 if (x86_pmu.cpu_dead)
1453 x86_pmu.cpu_dead(cpu);
1454 break;
1455
1456 default:
1457 break;
1458 }
1459
b38b24ea 1460 return ret;
3f6da390
PZ
1461}
1462
12558038
CG
1463static void __init pmu_check_apic(void)
1464{
1465 if (cpu_has_apic)
1466 return;
1467
1468 x86_pmu.apic = 0;
1469 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1470 pr_info("no hardware sampling interrupt available.\n");
1471}
1472
dda99116 1473static int __init init_hw_perf_events(void)
b56a3802 1474{
b622d644 1475 struct event_constraint *c;
72eae04d
RR
1476 int err;
1477
cdd6c482 1478 pr_info("Performance Events: ");
1123e3ad 1479
b56a3802
JSR
1480 switch (boot_cpu_data.x86_vendor) {
1481 case X86_VENDOR_INTEL:
72eae04d 1482 err = intel_pmu_init();
b56a3802 1483 break;
f87ad35d 1484 case X86_VENDOR_AMD:
72eae04d 1485 err = amd_pmu_init();
f87ad35d 1486 break;
4138960a 1487 default:
004417a6 1488 return 0;
b56a3802 1489 }
1123e3ad 1490 if (err != 0) {
cdd6c482 1491 pr_cont("no PMU driver, software events only.\n");
004417a6 1492 return 0;
1123e3ad 1493 }
b56a3802 1494
12558038
CG
1495 pmu_check_apic();
1496
33c6d6a7 1497 /* sanity check that the hardware exists or is emulated */
4407204c 1498 if (!check_hw_exists())
004417a6 1499 return 0;
33c6d6a7 1500
1123e3ad 1501 pr_cont("%s PMU driver.\n", x86_pmu.name);
faa28ae0 1502
3c44780b
PZ
1503 if (x86_pmu.quirks)
1504 x86_pmu.quirks();
1505
948b1bb8 1506 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
cdd6c482 1507 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
948b1bb8
RR
1508 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
1509 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
241771ef 1510 }
948b1bb8 1511 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
241771ef 1512
948b1bb8 1513 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
cdd6c482 1514 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
948b1bb8
RR
1515 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1516 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
703e937c 1517 }
862a1a5f 1518
d6dc0b4e 1519 x86_pmu.intel_ctrl |=
948b1bb8 1520 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
241771ef 1521
cdd6c482
IM
1522 perf_events_lapic_init();
1523 register_die_notifier(&perf_event_nmi_notifier);
1123e3ad 1524
63b14649 1525 unconstrained = (struct event_constraint)
948b1bb8
RR
1526 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
1527 0, x86_pmu.num_counters);
63b14649 1528
b622d644
PZ
1529 if (x86_pmu.event_constraints) {
1530 for_each_event_constraint(c, x86_pmu.event_constraints) {
a098f448 1531 if (c->cmask != X86_RAW_EVENT_MASK)
b622d644
PZ
1532 continue;
1533
948b1bb8
RR
1534 c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
1535 c->weight += x86_pmu.num_counters;
b622d644
PZ
1536 }
1537 }
1538
57c0c15b 1539 pr_info("... version: %d\n", x86_pmu.version);
948b1bb8
RR
1540 pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
1541 pr_info("... generic registers: %d\n", x86_pmu.num_counters);
1542 pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
57c0c15b 1543 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
948b1bb8 1544 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
d6dc0b4e 1545 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
3f6da390 1546
2e80a82a 1547 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
3f6da390 1548 perf_cpu_notifier(x86_pmu_notifier);
004417a6
PZ
1549
1550 return 0;
241771ef 1551}
004417a6 1552early_initcall(init_hw_perf_events);
621a01ea 1553
cdd6c482 1554static inline void x86_pmu_read(struct perf_event *event)
ee06094f 1555{
cc2ad4ba 1556 x86_perf_event_update(event);
ee06094f
IM
1557}
1558
4d1c52b0
LM
1559/*
1560 * Start group events scheduling transaction
1561 * Set the flag to make pmu::enable() not perform the
1562 * schedulability test, it will be performed at commit time
1563 */
51b0fe39 1564static void x86_pmu_start_txn(struct pmu *pmu)
4d1c52b0 1565{
33696fc0 1566 perf_pmu_disable(pmu);
0a3aee0d
TH
1567 __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN);
1568 __this_cpu_write(cpu_hw_events.n_txn, 0);
4d1c52b0
LM
1569}
1570
1571/*
1572 * Stop group events scheduling transaction
1573 * Clear the flag and pmu::enable() will perform the
1574 * schedulability test.
1575 */
51b0fe39 1576static void x86_pmu_cancel_txn(struct pmu *pmu)
4d1c52b0 1577{
0a3aee0d 1578 __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN);
90151c35
SE
1579 /*
1580 * Truncate the collected events.
1581 */
0a3aee0d
TH
1582 __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn));
1583 __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn));
33696fc0 1584 perf_pmu_enable(pmu);
4d1c52b0
LM
1585}
1586
1587/*
1588 * Commit group events scheduling transaction
1589 * Perform the group schedulability test as a whole
1590 * Return 0 if success
1591 */
51b0fe39 1592static int x86_pmu_commit_txn(struct pmu *pmu)
4d1c52b0
LM
1593{
1594 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1595 int assign[X86_PMC_IDX_MAX];
1596 int n, ret;
1597
1598 n = cpuc->n_events;
1599
1600 if (!x86_pmu_initialized())
1601 return -EAGAIN;
1602
1603 ret = x86_pmu.schedule_events(cpuc, n, assign);
1604 if (ret)
1605 return ret;
1606
1607 /*
1608 * copy new assignment, now we know it is possible
1609 * will be used by hw_perf_enable()
1610 */
1611 memcpy(cpuc->assign, assign, n*sizeof(int));
1612
8d2cacbb 1613 cpuc->group_flag &= ~PERF_EVENT_TXN;
33696fc0 1614 perf_pmu_enable(pmu);
4d1c52b0
LM
1615 return 0;
1616}
1617
ca037701
PZ
1618/*
1619 * validate that we can schedule this event
1620 */
1621static int validate_event(struct perf_event *event)
1622{
1623 struct cpu_hw_events *fake_cpuc;
1624 struct event_constraint *c;
1625 int ret = 0;
1626
1627 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1628 if (!fake_cpuc)
1629 return -ENOMEM;
1630
1631 c = x86_pmu.get_event_constraints(fake_cpuc, event);
1632
1633 if (!c || !c->weight)
1634 ret = -ENOSPC;
1635
1636 if (x86_pmu.put_event_constraints)
1637 x86_pmu.put_event_constraints(fake_cpuc, event);
1638
1639 kfree(fake_cpuc);
1640
1641 return ret;
1642}
1643
1da53e02
SE
1644/*
1645 * validate a single event group
1646 *
1647 * validation include:
184f412c
IM
1648 * - check events are compatible which each other
1649 * - events do not compete for the same counter
1650 * - number of events <= number of counters
1da53e02
SE
1651 *
1652 * validation ensures the group can be loaded onto the
1653 * PMU if it was the only group available.
1654 */
fe9081cc
PZ
1655static int validate_group(struct perf_event *event)
1656{
1da53e02 1657 struct perf_event *leader = event->group_leader;
502568d5
PZ
1658 struct cpu_hw_events *fake_cpuc;
1659 int ret, n;
fe9081cc 1660
502568d5
PZ
1661 ret = -ENOMEM;
1662 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1663 if (!fake_cpuc)
1664 goto out;
fe9081cc 1665
1da53e02
SE
1666 /*
1667 * the event is not yet connected with its
1668 * siblings therefore we must first collect
1669 * existing siblings, then add the new event
1670 * before we can simulate the scheduling
1671 */
502568d5
PZ
1672 ret = -ENOSPC;
1673 n = collect_events(fake_cpuc, leader, true);
1da53e02 1674 if (n < 0)
502568d5 1675 goto out_free;
fe9081cc 1676
502568d5
PZ
1677 fake_cpuc->n_events = n;
1678 n = collect_events(fake_cpuc, event, false);
1da53e02 1679 if (n < 0)
502568d5 1680 goto out_free;
fe9081cc 1681
502568d5 1682 fake_cpuc->n_events = n;
1da53e02 1683
a072738e 1684 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
502568d5
PZ
1685
1686out_free:
1687 kfree(fake_cpuc);
1688out:
1689 return ret;
fe9081cc
PZ
1690}
1691
dda99116 1692static int x86_pmu_event_init(struct perf_event *event)
621a01ea 1693{
51b0fe39 1694 struct pmu *tmp;
621a01ea
IM
1695 int err;
1696
b0a873eb
PZ
1697 switch (event->attr.type) {
1698 case PERF_TYPE_RAW:
1699 case PERF_TYPE_HARDWARE:
1700 case PERF_TYPE_HW_CACHE:
1701 break;
1702
1703 default:
1704 return -ENOENT;
1705 }
1706
1707 err = __x86_pmu_event_init(event);
fe9081cc 1708 if (!err) {
8113070d
SE
1709 /*
1710 * we temporarily connect event to its pmu
1711 * such that validate_group() can classify
1712 * it as an x86 event using is_x86_event()
1713 */
1714 tmp = event->pmu;
1715 event->pmu = &pmu;
1716
fe9081cc
PZ
1717 if (event->group_leader != event)
1718 err = validate_group(event);
ca037701
PZ
1719 else
1720 err = validate_event(event);
8113070d
SE
1721
1722 event->pmu = tmp;
fe9081cc 1723 }
a1792cda 1724 if (err) {
cdd6c482
IM
1725 if (event->destroy)
1726 event->destroy(event);
a1792cda 1727 }
621a01ea 1728
b0a873eb 1729 return err;
621a01ea 1730}
d7d59fb3 1731
b0a873eb 1732static struct pmu pmu = {
a4eaf7f1
PZ
1733 .pmu_enable = x86_pmu_enable,
1734 .pmu_disable = x86_pmu_disable,
1735
b0a873eb 1736 .event_init = x86_pmu_event_init,
a4eaf7f1
PZ
1737
1738 .add = x86_pmu_add,
1739 .del = x86_pmu_del,
b0a873eb
PZ
1740 .start = x86_pmu_start,
1741 .stop = x86_pmu_stop,
1742 .read = x86_pmu_read,
a4eaf7f1 1743
b0a873eb
PZ
1744 .start_txn = x86_pmu_start_txn,
1745 .cancel_txn = x86_pmu_cancel_txn,
1746 .commit_txn = x86_pmu_commit_txn,
1747};
1748
d7d59fb3
PZ
1749/*
1750 * callchain support
1751 */
1752
d7d59fb3
PZ
1753static void
1754backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1755{
1756 /* Ignore warnings */
1757}
1758
1759static void backtrace_warning(void *data, char *msg)
1760{
1761 /* Ignore warnings */
1762}
1763
1764static int backtrace_stack(void *data, char *name)
1765{
038e836e 1766 return 0;
d7d59fb3
PZ
1767}
1768
1769static void backtrace_address(void *data, unsigned long addr, int reliable)
1770{
1771 struct perf_callchain_entry *entry = data;
1772
70791ce9 1773 perf_callchain_store(entry, addr);
d7d59fb3
PZ
1774}
1775
1776static const struct stacktrace_ops backtrace_ops = {
1777 .warning = backtrace_warning,
1778 .warning_symbol = backtrace_warning_symbol,
1779 .stack = backtrace_stack,
1780 .address = backtrace_address,
06d65bda 1781 .walk_stack = print_context_stack_bp,
d7d59fb3
PZ
1782};
1783
56962b44
FW
1784void
1785perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
d7d59fb3 1786{
927c7a9e
FW
1787 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1788 /* TODO: We don't support guest os callchain now */
ed805261 1789 return;
927c7a9e
FW
1790 }
1791
70791ce9 1792 perf_callchain_store(entry, regs->ip);
d7d59fb3 1793
9c0729dc 1794 dump_trace(NULL, regs, NULL, &backtrace_ops, entry);
d7d59fb3
PZ
1795}
1796
257ef9d2
TE
1797#ifdef CONFIG_COMPAT
1798static inline int
1799perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
74193ef0 1800{
257ef9d2
TE
1801 /* 32-bit process in 64-bit kernel. */
1802 struct stack_frame_ia32 frame;
1803 const void __user *fp;
74193ef0 1804
257ef9d2
TE
1805 if (!test_thread_flag(TIF_IA32))
1806 return 0;
1807
1808 fp = compat_ptr(regs->bp);
1809 while (entry->nr < PERF_MAX_STACK_DEPTH) {
1810 unsigned long bytes;
1811 frame.next_frame = 0;
1812 frame.return_address = 0;
1813
1814 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1815 if (bytes != sizeof(frame))
1816 break;
74193ef0 1817
257ef9d2
TE
1818 if (fp < compat_ptr(regs->sp))
1819 break;
74193ef0 1820
70791ce9 1821 perf_callchain_store(entry, frame.return_address);
257ef9d2
TE
1822 fp = compat_ptr(frame.next_frame);
1823 }
1824 return 1;
d7d59fb3 1825}
257ef9d2
TE
1826#else
1827static inline int
1828perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
1829{
1830 return 0;
1831}
1832#endif
d7d59fb3 1833
56962b44
FW
1834void
1835perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
d7d59fb3
PZ
1836{
1837 struct stack_frame frame;
1838 const void __user *fp;
1839
927c7a9e
FW
1840 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1841 /* TODO: We don't support guest os callchain now */
ed805261 1842 return;
927c7a9e 1843 }
5a6cec3a 1844
74193ef0 1845 fp = (void __user *)regs->bp;
d7d59fb3 1846
70791ce9 1847 perf_callchain_store(entry, regs->ip);
d7d59fb3 1848
257ef9d2
TE
1849 if (perf_callchain_user32(regs, entry))
1850 return;
1851
f9188e02 1852 while (entry->nr < PERF_MAX_STACK_DEPTH) {
257ef9d2 1853 unsigned long bytes;
038e836e 1854 frame.next_frame = NULL;
d7d59fb3
PZ
1855 frame.return_address = 0;
1856
257ef9d2
TE
1857 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1858 if (bytes != sizeof(frame))
d7d59fb3
PZ
1859 break;
1860
5a6cec3a 1861 if ((unsigned long)fp < regs->sp)
d7d59fb3
PZ
1862 break;
1863
70791ce9 1864 perf_callchain_store(entry, frame.return_address);
038e836e 1865 fp = frame.next_frame;
d7d59fb3
PZ
1866 }
1867}
1868
39447b38
ZY
1869unsigned long perf_instruction_pointer(struct pt_regs *regs)
1870{
1871 unsigned long ip;
dcf46b94 1872
39447b38
ZY
1873 if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
1874 ip = perf_guest_cbs->get_guest_ip();
1875 else
1876 ip = instruction_pointer(regs);
dcf46b94 1877
39447b38
ZY
1878 return ip;
1879}
1880
1881unsigned long perf_misc_flags(struct pt_regs *regs)
1882{
1883 int misc = 0;
dcf46b94 1884
39447b38 1885 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
dcf46b94
ZY
1886 if (perf_guest_cbs->is_user_mode())
1887 misc |= PERF_RECORD_MISC_GUEST_USER;
1888 else
1889 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
1890 } else {
1891 if (user_mode(regs))
1892 misc |= PERF_RECORD_MISC_USER;
1893 else
1894 misc |= PERF_RECORD_MISC_KERNEL;
1895 }
1896
39447b38 1897 if (regs->flags & PERF_EFLAGS_EXACT)
ab608344 1898 misc |= PERF_RECORD_MISC_EXACT_IP;
39447b38
ZY
1899
1900 return misc;
1901}