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