2 * Performance events x86 architecture code
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>
9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
12 * For licencing details see kernel-base/COPYING
15 #include <linux/perf_event.h>
16 #include <linux/capability.h>
17 #include <linux/notifier.h>
18 #include <linux/hardirq.h>
19 #include <linux/kprobes.h>
20 #include <linux/module.h>
21 #include <linux/kdebug.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/slab.h>
25 #include <linux/highmem.h>
26 #include <linux/cpu.h>
27 #include <linux/bitops.h>
30 #include <asm/stacktrace.h>
32 #include <asm/compat.h>
34 #include <asm/alternative.h>
38 #define wrmsrl(msr, val) \
40 trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
41 (unsigned long)(val)); \
42 native_write_msr((msr), (u32)((u64)(val)), \
43 (u32)((u64)(val) >> 32)); \
48 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
51 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
53 unsigned long offset
, addr
= (unsigned long)from
;
54 unsigned long size
, len
= 0;
60 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
64 offset
= addr
& (PAGE_SIZE
- 1);
65 size
= min(PAGE_SIZE
- offset
, n
- len
);
67 map
= kmap_atomic(page
);
68 memcpy(to
, map
+offset
, size
);
81 struct event_constraint
{
83 unsigned long idxmsk
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
92 int nb_id
; /* NorthBridge id */
93 int refcnt
; /* reference count */
94 struct perf_event
*owners
[X86_PMC_IDX_MAX
];
95 struct event_constraint event_constraints
[X86_PMC_IDX_MAX
];
100 #define MAX_LBR_ENTRIES 16
102 struct cpu_hw_events
{
104 * Generic x86 PMC bits
106 struct perf_event
*events
[X86_PMC_IDX_MAX
]; /* in counter order */
107 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
108 unsigned long running
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
114 int assign
[X86_PMC_IDX_MAX
]; /* event to counter assignment */
115 u64 tags
[X86_PMC_IDX_MAX
];
116 struct perf_event
*event_list
[X86_PMC_IDX_MAX
]; /* in enabled order */
118 unsigned int group_flag
;
121 * Intel DebugStore bits
123 struct debug_store
*ds
;
131 struct perf_branch_stack lbr_stack
;
132 struct perf_branch_entry lbr_entries
[MAX_LBR_ENTRIES
];
135 * Intel percore register state.
136 * Coordinate shared resources between HT threads.
138 int percore_used
; /* Used by this CPU? */
139 struct intel_percore
*per_core
;
144 struct amd_nb
*amd_nb
;
147 #define __EVENT_CONSTRAINT(c, n, m, w) {\
148 { .idxmsk64 = (n) }, \
154 #define EVENT_CONSTRAINT(c, n, m) \
155 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
158 * Constraint on the Event code.
160 #define INTEL_EVENT_CONSTRAINT(c, n) \
161 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
164 * Constraint on the Event code + UMask + fixed-mask
166 * filter mask to validate fixed counter events.
167 * the following filters disqualify for fixed counters:
171 * The other filters are supported by fixed counters.
172 * The any-thread option is supported starting with v3.
174 #define FIXED_EVENT_CONSTRAINT(c, n) \
175 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
178 * Constraint on the Event code + UMask
180 #define INTEL_UEVENT_CONSTRAINT(c, n) \
181 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
183 #define EVENT_CONSTRAINT_END \
184 EVENT_CONSTRAINT(0, 0, 0)
186 #define for_each_event_constraint(e, c) \
187 for ((e) = (c); (e)->weight; (e)++)
190 * Extra registers for specific events.
191 * Some events need large masks and require external MSRs.
192 * Define a mapping to these extra registers.
201 #define EVENT_EXTRA_REG(e, ms, m, vm) { \
204 .config_mask = (m), \
205 .valid_mask = (vm), \
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)
211 union perf_capabilities
{
215 u64 pebs_arch_reg
: 1;
223 * struct x86_pmu - generic x86 pmu
227 * Generic x86 PMC bits
231 int (*handle_irq
)(struct pt_regs
*);
232 void (*disable_all
)(void);
233 void (*enable_all
)(int added
);
234 void (*enable
)(struct perf_event
*);
235 void (*disable
)(struct perf_event
*);
236 int (*hw_config
)(struct perf_event
*event
);
237 int (*schedule_events
)(struct cpu_hw_events
*cpuc
, int n
, int *assign
);
240 u64 (*event_map
)(int);
243 int num_counters_fixed
;
248 struct event_constraint
*
249 (*get_event_constraints
)(struct cpu_hw_events
*cpuc
,
250 struct perf_event
*event
);
252 void (*put_event_constraints
)(struct cpu_hw_events
*cpuc
,
253 struct perf_event
*event
);
254 struct event_constraint
*event_constraints
;
255 struct event_constraint
*percore_constraints
;
256 void (*quirks
)(void);
257 int perfctr_second_write
;
259 int (*cpu_prepare
)(int cpu
);
260 void (*cpu_starting
)(int cpu
);
261 void (*cpu_dying
)(int cpu
);
262 void (*cpu_dead
)(int cpu
);
265 * Intel Arch Perfmon v2+
268 union perf_capabilities intel_cap
;
271 * Intel DebugStore bits
274 int bts_active
, pebs_active
;
275 int pebs_record_size
;
276 void (*drain_pebs
)(struct pt_regs
*regs
);
277 struct event_constraint
*pebs_constraints
;
282 unsigned long lbr_tos
, lbr_from
, lbr_to
; /* MSR base regs */
283 int lbr_nr
; /* hardware stack size */
286 * Extra registers for events
288 struct extra_reg
*extra_regs
;
291 static struct x86_pmu x86_pmu __read_mostly
;
293 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
) = {
297 static int x86_perf_event_set_period(struct perf_event
*event
);
300 * Generalized hw caching related hw_event table, filled
301 * in on a per model basis. A value of 0 means
302 * 'not supported', -1 means 'hw_event makes no sense on
303 * this CPU', any other value means the raw hw_event
307 #define C(x) PERF_COUNT_HW_CACHE_##x
309 static 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
];
313 static 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
];
319 * Propagate event elapsed time into the generic event.
320 * Can only be executed on the CPU where the event is active.
321 * Returns the delta events processed.
324 x86_perf_event_update(struct perf_event
*event
)
326 struct hw_perf_event
*hwc
= &event
->hw
;
327 int shift
= 64 - x86_pmu
.cntval_bits
;
328 u64 prev_raw_count
, new_raw_count
;
332 if (idx
== X86_PMC_IDX_FIXED_BTS
)
336 * Careful: an NMI might modify the previous event value.
338 * Our tactic to handle this is to first atomically read and
339 * exchange a new raw count - then add that new-prev delta
340 * count to the generic event atomically:
343 prev_raw_count
= local64_read(&hwc
->prev_count
);
344 rdmsrl(hwc
->event_base
, new_raw_count
);
346 if (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
347 new_raw_count
) != prev_raw_count
)
351 * Now we have the new raw value and have updated the prev
352 * timestamp already. We can now calculate the elapsed delta
353 * (event-)time and add that to the generic event.
355 * Careful, not all hw sign-extends above the physical width
358 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
361 local64_add(delta
, &event
->count
);
362 local64_sub(delta
, &hwc
->period_left
);
364 return new_raw_count
;
367 static inline int x86_pmu_addr_offset(int index
)
371 /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
372 alternative_io(ASM_NOP2
,
374 X86_FEATURE_PERFCTR_CORE
,
381 static inline unsigned int x86_pmu_config_addr(int index
)
383 return x86_pmu
.eventsel
+ x86_pmu_addr_offset(index
);
386 static inline unsigned int x86_pmu_event_addr(int index
)
388 return x86_pmu
.perfctr
+ x86_pmu_addr_offset(index
);
392 * Find and validate any extra registers to set up.
394 static int x86_pmu_extra_regs(u64 config
, struct perf_event
*event
)
396 struct extra_reg
*er
;
398 event
->hw
.extra_reg
= 0;
399 event
->hw
.extra_config
= 0;
401 if (!x86_pmu
.extra_regs
)
404 for (er
= x86_pmu
.extra_regs
; er
->msr
; er
++) {
405 if (er
->event
!= (config
& er
->config_mask
))
407 if (event
->attr
.config1
& ~er
->valid_mask
)
409 event
->hw
.extra_reg
= er
->msr
;
410 event
->hw
.extra_config
= event
->attr
.config1
;
416 static atomic_t active_events
;
417 static DEFINE_MUTEX(pmc_reserve_mutex
);
419 #ifdef CONFIG_X86_LOCAL_APIC
421 static bool reserve_pmc_hardware(void)
425 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
426 if (!reserve_perfctr_nmi(x86_pmu_event_addr(i
)))
430 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
431 if (!reserve_evntsel_nmi(x86_pmu_config_addr(i
)))
438 for (i
--; i
>= 0; i
--)
439 release_evntsel_nmi(x86_pmu_config_addr(i
));
441 i
= x86_pmu
.num_counters
;
444 for (i
--; i
>= 0; i
--)
445 release_perfctr_nmi(x86_pmu_event_addr(i
));
450 static void release_pmc_hardware(void)
454 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
455 release_perfctr_nmi(x86_pmu_event_addr(i
));
456 release_evntsel_nmi(x86_pmu_config_addr(i
));
462 static bool reserve_pmc_hardware(void) { return true; }
463 static void release_pmc_hardware(void) {}
467 static bool check_hw_exists(void)
469 u64 val
, val_new
= 0;
473 * Check to see if the BIOS enabled any of the counters, if so
476 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
477 reg
= x86_pmu_config_addr(i
);
478 ret
= rdmsrl_safe(reg
, &val
);
481 if (val
& ARCH_PERFMON_EVENTSEL_ENABLE
)
485 if (x86_pmu
.num_counters_fixed
) {
486 reg
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
487 ret
= rdmsrl_safe(reg
, &val
);
490 for (i
= 0; i
< x86_pmu
.num_counters_fixed
; i
++) {
491 if (val
& (0x03 << i
*4))
497 * Now write a value and read it back to see if it matches,
498 * this is needed to detect certain hardware emulators (qemu/kvm)
499 * that don't trap on the MSR access and always return 0s.
502 ret
= checking_wrmsrl(x86_pmu_event_addr(0), val
);
503 ret
|= rdmsrl_safe(x86_pmu_event_addr(0), &val_new
);
504 if (ret
|| val
!= val_new
)
511 * We still allow the PMU driver to operate:
513 printk(KERN_CONT
"Broken BIOS detected, complain to your hardware vendor.\n");
514 printk(KERN_ERR FW_BUG
"the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg
, val
);
519 printk(KERN_CONT
"Broken PMU hardware detected, using software events only.\n");
524 static void reserve_ds_buffers(void);
525 static void release_ds_buffers(void);
527 static void hw_perf_event_destroy(struct perf_event
*event
)
529 if (atomic_dec_and_mutex_lock(&active_events
, &pmc_reserve_mutex
)) {
530 release_pmc_hardware();
531 release_ds_buffers();
532 mutex_unlock(&pmc_reserve_mutex
);
536 static inline int x86_pmu_initialized(void)
538 return x86_pmu
.handle_irq
!= NULL
;
542 set_ext_hw_attr(struct hw_perf_event
*hwc
, struct perf_event
*event
)
544 struct perf_event_attr
*attr
= &event
->attr
;
545 unsigned int cache_type
, cache_op
, cache_result
;
548 config
= attr
->config
;
550 cache_type
= (config
>> 0) & 0xff;
551 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
554 cache_op
= (config
>> 8) & 0xff;
555 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
558 cache_result
= (config
>> 16) & 0xff;
559 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
562 val
= hw_cache_event_ids
[cache_type
][cache_op
][cache_result
];
571 attr
->config1
= hw_cache_extra_regs
[cache_type
][cache_op
][cache_result
];
572 return x86_pmu_extra_regs(val
, event
);
575 static int x86_setup_perfctr(struct perf_event
*event
)
577 struct perf_event_attr
*attr
= &event
->attr
;
578 struct hw_perf_event
*hwc
= &event
->hw
;
581 if (!is_sampling_event(event
)) {
582 hwc
->sample_period
= x86_pmu
.max_period
;
583 hwc
->last_period
= hwc
->sample_period
;
584 local64_set(&hwc
->period_left
, hwc
->sample_period
);
587 * If we have a PMU initialized but no APIC
588 * interrupts, we cannot sample hardware
589 * events (user-space has to fall back and
590 * sample via a hrtimer based software event):
597 * Do not allow config1 (extended registers) to propagate,
598 * there's no sane user-space generalization yet:
600 if (attr
->type
== PERF_TYPE_RAW
)
603 if (attr
->type
== PERF_TYPE_HW_CACHE
)
604 return set_ext_hw_attr(hwc
, event
);
606 if (attr
->config
>= x86_pmu
.max_events
)
612 config
= x86_pmu
.event_map(attr
->config
);
623 if (attr
->config
== PERF_COUNT_HW_BRANCH_INSTRUCTIONS
&&
624 !attr
->freq
&& hwc
->sample_period
== 1) {
625 /* BTS is not supported by this architecture. */
626 if (!x86_pmu
.bts_active
)
629 /* BTS is currently only allowed for user-mode. */
630 if (!attr
->exclude_kernel
)
634 hwc
->config
|= config
;
639 static int x86_pmu_hw_config(struct perf_event
*event
)
641 if (event
->attr
.precise_ip
) {
644 /* Support for constant skid */
645 if (x86_pmu
.pebs_active
) {
648 /* Support for IP fixup */
653 if (event
->attr
.precise_ip
> precise
)
659 * (keep 'enabled' bit clear for now)
661 event
->hw
.config
= ARCH_PERFMON_EVENTSEL_INT
;
664 * Count user and OS events unless requested not to
666 if (!event
->attr
.exclude_user
)
667 event
->hw
.config
|= ARCH_PERFMON_EVENTSEL_USR
;
668 if (!event
->attr
.exclude_kernel
)
669 event
->hw
.config
|= ARCH_PERFMON_EVENTSEL_OS
;
671 if (event
->attr
.type
== PERF_TYPE_RAW
)
672 event
->hw
.config
|= event
->attr
.config
& X86_RAW_EVENT_MASK
;
674 return x86_setup_perfctr(event
);
678 * Setup the hardware configuration for a given attr_type
680 static int __x86_pmu_event_init(struct perf_event
*event
)
684 if (!x86_pmu_initialized())
688 if (!atomic_inc_not_zero(&active_events
)) {
689 mutex_lock(&pmc_reserve_mutex
);
690 if (atomic_read(&active_events
) == 0) {
691 if (!reserve_pmc_hardware())
694 reserve_ds_buffers();
697 atomic_inc(&active_events
);
698 mutex_unlock(&pmc_reserve_mutex
);
703 event
->destroy
= hw_perf_event_destroy
;
706 event
->hw
.last_cpu
= -1;
707 event
->hw
.last_tag
= ~0ULL;
709 return x86_pmu
.hw_config(event
);
712 static void x86_pmu_disable_all(void)
714 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
717 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
720 if (!test_bit(idx
, cpuc
->active_mask
))
722 rdmsrl(x86_pmu_config_addr(idx
), val
);
723 if (!(val
& ARCH_PERFMON_EVENTSEL_ENABLE
))
725 val
&= ~ARCH_PERFMON_EVENTSEL_ENABLE
;
726 wrmsrl(x86_pmu_config_addr(idx
), val
);
730 static void x86_pmu_disable(struct pmu
*pmu
)
732 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
734 if (!x86_pmu_initialized())
744 x86_pmu
.disable_all();
747 static inline void __x86_pmu_enable_event(struct hw_perf_event
*hwc
,
751 wrmsrl(hwc
->extra_reg
, hwc
->extra_config
);
752 wrmsrl(hwc
->config_base
, hwc
->config
| enable_mask
);
755 static void x86_pmu_enable_all(int added
)
757 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
760 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
761 struct hw_perf_event
*hwc
= &cpuc
->events
[idx
]->hw
;
763 if (!test_bit(idx
, cpuc
->active_mask
))
766 __x86_pmu_enable_event(hwc
, ARCH_PERFMON_EVENTSEL_ENABLE
);
770 static struct pmu pmu
;
772 static inline int is_x86_event(struct perf_event
*event
)
774 return event
->pmu
== &pmu
;
777 static int x86_schedule_events(struct cpu_hw_events
*cpuc
, int n
, int *assign
)
779 struct event_constraint
*c
, *constraints
[X86_PMC_IDX_MAX
];
780 unsigned long used_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
781 int i
, j
, w
, wmax
, num
= 0;
782 struct hw_perf_event
*hwc
;
784 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
786 for (i
= 0; i
< n
; i
++) {
787 c
= x86_pmu
.get_event_constraints(cpuc
, cpuc
->event_list
[i
]);
792 * fastpath, try to reuse previous register
794 for (i
= 0; i
< n
; i
++) {
795 hwc
= &cpuc
->event_list
[i
]->hw
;
802 /* constraint still honored */
803 if (!test_bit(hwc
->idx
, c
->idxmsk
))
806 /* not already used */
807 if (test_bit(hwc
->idx
, used_mask
))
810 __set_bit(hwc
->idx
, used_mask
);
812 assign
[i
] = hwc
->idx
;
821 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
824 * weight = number of possible counters
826 * 1 = most constrained, only works on one counter
827 * wmax = least constrained, works on any counter
829 * assign events to counters starting with most
830 * constrained events.
832 wmax
= x86_pmu
.num_counters
;
835 * when fixed event counters are present,
836 * wmax is incremented by 1 to account
837 * for one more choice
839 if (x86_pmu
.num_counters_fixed
)
842 for (w
= 1, num
= n
; num
&& w
<= wmax
; w
++) {
844 for (i
= 0; num
&& i
< n
; i
++) {
846 hwc
= &cpuc
->event_list
[i
]->hw
;
851 for_each_set_bit(j
, c
->idxmsk
, X86_PMC_IDX_MAX
) {
852 if (!test_bit(j
, used_mask
))
856 if (j
== X86_PMC_IDX_MAX
)
859 __set_bit(j
, used_mask
);
868 * scheduling failed or is just a simulation,
869 * free resources if necessary
871 if (!assign
|| num
) {
872 for (i
= 0; i
< n
; i
++) {
873 if (x86_pmu
.put_event_constraints
)
874 x86_pmu
.put_event_constraints(cpuc
, cpuc
->event_list
[i
]);
877 return num
? -ENOSPC
: 0;
881 * dogrp: true if must collect siblings events (group)
882 * returns total number of events and error code
884 static int collect_events(struct cpu_hw_events
*cpuc
, struct perf_event
*leader
, bool dogrp
)
886 struct perf_event
*event
;
889 max_count
= x86_pmu
.num_counters
+ x86_pmu
.num_counters_fixed
;
891 /* current number of events already accepted */
894 if (is_x86_event(leader
)) {
897 cpuc
->event_list
[n
] = leader
;
903 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
) {
904 if (!is_x86_event(event
) ||
905 event
->state
<= PERF_EVENT_STATE_OFF
)
911 cpuc
->event_list
[n
] = event
;
917 static inline void x86_assign_hw_event(struct perf_event
*event
,
918 struct cpu_hw_events
*cpuc
, int i
)
920 struct hw_perf_event
*hwc
= &event
->hw
;
922 hwc
->idx
= cpuc
->assign
[i
];
923 hwc
->last_cpu
= smp_processor_id();
924 hwc
->last_tag
= ++cpuc
->tags
[i
];
926 if (hwc
->idx
== X86_PMC_IDX_FIXED_BTS
) {
927 hwc
->config_base
= 0;
929 } else if (hwc
->idx
>= X86_PMC_IDX_FIXED
) {
930 hwc
->config_base
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
931 hwc
->event_base
= MSR_ARCH_PERFMON_FIXED_CTR0
+ (hwc
->idx
- X86_PMC_IDX_FIXED
);
933 hwc
->config_base
= x86_pmu_config_addr(hwc
->idx
);
934 hwc
->event_base
= x86_pmu_event_addr(hwc
->idx
);
938 static inline int match_prev_assignment(struct hw_perf_event
*hwc
,
939 struct cpu_hw_events
*cpuc
,
942 return hwc
->idx
== cpuc
->assign
[i
] &&
943 hwc
->last_cpu
== smp_processor_id() &&
944 hwc
->last_tag
== cpuc
->tags
[i
];
947 static void x86_pmu_start(struct perf_event
*event
, int flags
);
948 static void x86_pmu_stop(struct perf_event
*event
, int flags
);
950 static void x86_pmu_enable(struct pmu
*pmu
)
952 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
953 struct perf_event
*event
;
954 struct hw_perf_event
*hwc
;
955 int i
, added
= cpuc
->n_added
;
957 if (!x86_pmu_initialized())
964 int n_running
= cpuc
->n_events
- cpuc
->n_added
;
966 * apply assignment obtained either from
967 * hw_perf_group_sched_in() or x86_pmu_enable()
969 * step1: save events moving to new counters
970 * step2: reprogram moved events into new counters
972 for (i
= 0; i
< n_running
; i
++) {
973 event
= cpuc
->event_list
[i
];
977 * we can avoid reprogramming counter if:
978 * - assigned same counter as last time
979 * - running on same CPU as last time
980 * - no other event has used the counter since
982 if (hwc
->idx
== -1 ||
983 match_prev_assignment(hwc
, cpuc
, i
))
987 * Ensure we don't accidentally enable a stopped
988 * counter simply because we rescheduled.
990 if (hwc
->state
& PERF_HES_STOPPED
)
991 hwc
->state
|= PERF_HES_ARCH
;
993 x86_pmu_stop(event
, PERF_EF_UPDATE
);
996 for (i
= 0; i
< cpuc
->n_events
; i
++) {
997 event
= cpuc
->event_list
[i
];
1000 if (!match_prev_assignment(hwc
, cpuc
, i
))
1001 x86_assign_hw_event(event
, cpuc
, i
);
1002 else if (i
< n_running
)
1005 if (hwc
->state
& PERF_HES_ARCH
)
1008 x86_pmu_start(event
, PERF_EF_RELOAD
);
1011 perf_events_lapic_init();
1017 x86_pmu
.enable_all(added
);
1020 static inline void x86_pmu_disable_event(struct perf_event
*event
)
1022 struct hw_perf_event
*hwc
= &event
->hw
;
1024 wrmsrl(hwc
->config_base
, hwc
->config
);
1027 static DEFINE_PER_CPU(u64
[X86_PMC_IDX_MAX
], pmc_prev_left
);
1030 * Set the next IRQ period, based on the hwc->period_left value.
1031 * To be called with the event disabled in hw:
1034 x86_perf_event_set_period(struct perf_event
*event
)
1036 struct hw_perf_event
*hwc
= &event
->hw
;
1037 s64 left
= local64_read(&hwc
->period_left
);
1038 s64 period
= hwc
->sample_period
;
1039 int ret
= 0, idx
= hwc
->idx
;
1041 if (idx
== X86_PMC_IDX_FIXED_BTS
)
1045 * If we are way outside a reasonable range then just skip forward:
1047 if (unlikely(left
<= -period
)) {
1049 local64_set(&hwc
->period_left
, left
);
1050 hwc
->last_period
= period
;
1054 if (unlikely(left
<= 0)) {
1056 local64_set(&hwc
->period_left
, left
);
1057 hwc
->last_period
= period
;
1061 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
1063 if (unlikely(left
< 2))
1066 if (left
> x86_pmu
.max_period
)
1067 left
= x86_pmu
.max_period
;
1069 per_cpu(pmc_prev_left
[idx
], smp_processor_id()) = left
;
1072 * The hw event starts counting from this event offset,
1073 * mark it to be able to extra future deltas:
1075 local64_set(&hwc
->prev_count
, (u64
)-left
);
1077 wrmsrl(hwc
->event_base
, (u64
)(-left
) & x86_pmu
.cntval_mask
);
1080 * Due to erratum on certan cpu we need
1081 * a second write to be sure the register
1082 * is updated properly
1084 if (x86_pmu
.perfctr_second_write
) {
1085 wrmsrl(hwc
->event_base
,
1086 (u64
)(-left
) & x86_pmu
.cntval_mask
);
1089 perf_event_update_userpage(event
);
1094 static void x86_pmu_enable_event(struct perf_event
*event
)
1096 if (__this_cpu_read(cpu_hw_events
.enabled
))
1097 __x86_pmu_enable_event(&event
->hw
,
1098 ARCH_PERFMON_EVENTSEL_ENABLE
);
1102 * Add a single event to the PMU.
1104 * The event is added to the group of enabled events
1105 * but only if it can be scehduled with existing events.
1107 static int x86_pmu_add(struct perf_event
*event
, int flags
)
1109 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1110 struct hw_perf_event
*hwc
;
1111 int assign
[X86_PMC_IDX_MAX
];
1116 perf_pmu_disable(event
->pmu
);
1117 n0
= cpuc
->n_events
;
1118 ret
= n
= collect_events(cpuc
, event
, false);
1122 hwc
->state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
1123 if (!(flags
& PERF_EF_START
))
1124 hwc
->state
|= PERF_HES_ARCH
;
1127 * If group events scheduling transaction was started,
1128 * skip the schedulability test here, it will be performed
1129 * at commit time (->commit_txn) as a whole
1131 if (cpuc
->group_flag
& PERF_EVENT_TXN
)
1134 ret
= x86_pmu
.schedule_events(cpuc
, n
, assign
);
1138 * copy new assignment, now we know it is possible
1139 * will be used by hw_perf_enable()
1141 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
1145 cpuc
->n_added
+= n
- n0
;
1146 cpuc
->n_txn
+= n
- n0
;
1150 perf_pmu_enable(event
->pmu
);
1154 static void x86_pmu_start(struct perf_event
*event
, int flags
)
1156 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1157 int idx
= event
->hw
.idx
;
1159 if (WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_STOPPED
)))
1162 if (WARN_ON_ONCE(idx
== -1))
1165 if (flags
& PERF_EF_RELOAD
) {
1166 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
1167 x86_perf_event_set_period(event
);
1170 event
->hw
.state
= 0;
1172 cpuc
->events
[idx
] = event
;
1173 __set_bit(idx
, cpuc
->active_mask
);
1174 __set_bit(idx
, cpuc
->running
);
1175 x86_pmu
.enable(event
);
1176 perf_event_update_userpage(event
);
1179 void perf_event_print_debug(void)
1181 u64 ctrl
, status
, overflow
, pmc_ctrl
, pmc_count
, prev_left
, fixed
;
1183 struct cpu_hw_events
*cpuc
;
1184 unsigned long flags
;
1187 if (!x86_pmu
.num_counters
)
1190 local_irq_save(flags
);
1192 cpu
= smp_processor_id();
1193 cpuc
= &per_cpu(cpu_hw_events
, cpu
);
1195 if (x86_pmu
.version
>= 2) {
1196 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, ctrl
);
1197 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
1198 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, overflow
);
1199 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL
, fixed
);
1200 rdmsrl(MSR_IA32_PEBS_ENABLE
, pebs
);
1203 pr_info("CPU#%d: ctrl: %016llx\n", cpu
, ctrl
);
1204 pr_info("CPU#%d: status: %016llx\n", cpu
, status
);
1205 pr_info("CPU#%d: overflow: %016llx\n", cpu
, overflow
);
1206 pr_info("CPU#%d: fixed: %016llx\n", cpu
, fixed
);
1207 pr_info("CPU#%d: pebs: %016llx\n", cpu
, pebs
);
1209 pr_info("CPU#%d: active: %016llx\n", cpu
, *(u64
*)cpuc
->active_mask
);
1211 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1212 rdmsrl(x86_pmu_config_addr(idx
), pmc_ctrl
);
1213 rdmsrl(x86_pmu_event_addr(idx
), pmc_count
);
1215 prev_left
= per_cpu(pmc_prev_left
[idx
], cpu
);
1217 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1218 cpu
, idx
, pmc_ctrl
);
1219 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1220 cpu
, idx
, pmc_count
);
1221 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1222 cpu
, idx
, prev_left
);
1224 for (idx
= 0; idx
< x86_pmu
.num_counters_fixed
; idx
++) {
1225 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, pmc_count
);
1227 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1228 cpu
, idx
, pmc_count
);
1230 local_irq_restore(flags
);
1233 static void x86_pmu_stop(struct perf_event
*event
, int flags
)
1235 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1236 struct hw_perf_event
*hwc
= &event
->hw
;
1238 if (__test_and_clear_bit(hwc
->idx
, cpuc
->active_mask
)) {
1239 x86_pmu
.disable(event
);
1240 cpuc
->events
[hwc
->idx
] = NULL
;
1241 WARN_ON_ONCE(hwc
->state
& PERF_HES_STOPPED
);
1242 hwc
->state
|= PERF_HES_STOPPED
;
1245 if ((flags
& PERF_EF_UPDATE
) && !(hwc
->state
& PERF_HES_UPTODATE
)) {
1247 * Drain the remaining delta count out of a event
1248 * that we are disabling:
1250 x86_perf_event_update(event
);
1251 hwc
->state
|= PERF_HES_UPTODATE
;
1255 static void x86_pmu_del(struct perf_event
*event
, int flags
)
1257 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1261 * If we're called during a txn, we don't need to do anything.
1262 * The events never got scheduled and ->cancel_txn will truncate
1265 if (cpuc
->group_flag
& PERF_EVENT_TXN
)
1268 x86_pmu_stop(event
, PERF_EF_UPDATE
);
1270 for (i
= 0; i
< cpuc
->n_events
; i
++) {
1271 if (event
== cpuc
->event_list
[i
]) {
1273 if (x86_pmu
.put_event_constraints
)
1274 x86_pmu
.put_event_constraints(cpuc
, event
);
1276 while (++i
< cpuc
->n_events
)
1277 cpuc
->event_list
[i
-1] = cpuc
->event_list
[i
];
1283 perf_event_update_userpage(event
);
1286 static int x86_pmu_handle_irq(struct pt_regs
*regs
)
1288 struct perf_sample_data data
;
1289 struct cpu_hw_events
*cpuc
;
1290 struct perf_event
*event
;
1291 int idx
, handled
= 0;
1294 perf_sample_data_init(&data
, 0);
1296 cpuc
= &__get_cpu_var(cpu_hw_events
);
1299 * Some chipsets need to unmask the LVTPC in a particular spot
1300 * inside the nmi handler. As a result, the unmasking was pushed
1301 * into all the nmi handlers.
1303 * This generic handler doesn't seem to have any issues where the
1304 * unmasking occurs so it was left at the top.
1306 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1308 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1309 if (!test_bit(idx
, cpuc
->active_mask
)) {
1311 * Though we deactivated the counter some cpus
1312 * might still deliver spurious interrupts still
1313 * in flight. Catch them:
1315 if (__test_and_clear_bit(idx
, cpuc
->running
))
1320 event
= cpuc
->events
[idx
];
1322 val
= x86_perf_event_update(event
);
1323 if (val
& (1ULL << (x86_pmu
.cntval_bits
- 1)))
1330 data
.period
= event
->hw
.last_period
;
1332 if (!x86_perf_event_set_period(event
))
1335 if (perf_event_overflow(event
, 1, &data
, regs
))
1336 x86_pmu_stop(event
, 0);
1340 inc_irq_stat(apic_perf_irqs
);
1345 void perf_events_lapic_init(void)
1347 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1351 * Always use NMI for PMU
1353 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1356 struct pmu_nmi_state
{
1357 unsigned int marked
;
1361 static DEFINE_PER_CPU(struct pmu_nmi_state
, pmu_nmi
);
1363 static int __kprobes
1364 perf_event_nmi_handler(struct notifier_block
*self
,
1365 unsigned long cmd
, void *__args
)
1367 struct die_args
*args
= __args
;
1368 unsigned int this_nmi
;
1371 if (!atomic_read(&active_events
))
1377 case DIE_NMIUNKNOWN
:
1378 this_nmi
= percpu_read(irq_stat
.__nmi_count
);
1379 if (this_nmi
!= __this_cpu_read(pmu_nmi
.marked
))
1380 /* let the kernel handle the unknown nmi */
1383 * This one is a PMU back-to-back nmi. Two events
1384 * trigger 'simultaneously' raising two back-to-back
1385 * NMIs. If the first NMI handles both, the latter
1386 * will be empty and daze the CPU. So, we drop it to
1387 * avoid false-positive 'unknown nmi' messages.
1394 handled
= x86_pmu
.handle_irq(args
->regs
);
1398 this_nmi
= percpu_read(irq_stat
.__nmi_count
);
1399 if ((handled
> 1) ||
1400 /* the next nmi could be a back-to-back nmi */
1401 ((__this_cpu_read(pmu_nmi
.marked
) == this_nmi
) &&
1402 (__this_cpu_read(pmu_nmi
.handled
) > 1))) {
1404 * We could have two subsequent back-to-back nmis: The
1405 * first handles more than one counter, the 2nd
1406 * handles only one counter and the 3rd handles no
1409 * This is the 2nd nmi because the previous was
1410 * handling more than one counter. We will mark the
1411 * next (3rd) and then drop it if unhandled.
1413 __this_cpu_write(pmu_nmi
.marked
, this_nmi
+ 1);
1414 __this_cpu_write(pmu_nmi
.handled
, handled
);
1420 static __read_mostly
struct notifier_block perf_event_nmi_notifier
= {
1421 .notifier_call
= perf_event_nmi_handler
,
1423 .priority
= NMI_LOCAL_LOW_PRIOR
,
1426 static struct event_constraint unconstrained
;
1427 static struct event_constraint emptyconstraint
;
1429 static struct event_constraint
*
1430 x86_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
)
1432 struct event_constraint
*c
;
1434 if (x86_pmu
.event_constraints
) {
1435 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1436 if ((event
->hw
.config
& c
->cmask
) == c
->code
)
1441 return &unconstrained
;
1444 #include "perf_event_amd.c"
1445 #include "perf_event_p6.c"
1446 #include "perf_event_p4.c"
1447 #include "perf_event_intel_lbr.c"
1448 #include "perf_event_intel_ds.c"
1449 #include "perf_event_intel.c"
1451 static int __cpuinit
1452 x86_pmu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1454 unsigned int cpu
= (long)hcpu
;
1455 int ret
= NOTIFY_OK
;
1457 switch (action
& ~CPU_TASKS_FROZEN
) {
1458 case CPU_UP_PREPARE
:
1459 if (x86_pmu
.cpu_prepare
)
1460 ret
= x86_pmu
.cpu_prepare(cpu
);
1464 if (x86_pmu
.cpu_starting
)
1465 x86_pmu
.cpu_starting(cpu
);
1469 if (x86_pmu
.cpu_dying
)
1470 x86_pmu
.cpu_dying(cpu
);
1473 case CPU_UP_CANCELED
:
1475 if (x86_pmu
.cpu_dead
)
1476 x86_pmu
.cpu_dead(cpu
);
1486 static void __init
pmu_check_apic(void)
1492 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1493 pr_info("no hardware sampling interrupt available.\n");
1496 static int __init
init_hw_perf_events(void)
1498 struct event_constraint
*c
;
1501 pr_info("Performance Events: ");
1503 switch (boot_cpu_data
.x86_vendor
) {
1504 case X86_VENDOR_INTEL
:
1505 err
= intel_pmu_init();
1507 case X86_VENDOR_AMD
:
1508 err
= amd_pmu_init();
1514 pr_cont("no PMU driver, software events only.\n");
1520 /* sanity check that the hardware exists or is emulated */
1521 if (!check_hw_exists())
1524 pr_cont("%s PMU driver.\n", x86_pmu
.name
);
1529 if (x86_pmu
.num_counters
> X86_PMC_MAX_GENERIC
) {
1530 WARN(1, KERN_ERR
"hw perf events %d > max(%d), clipping!",
1531 x86_pmu
.num_counters
, X86_PMC_MAX_GENERIC
);
1532 x86_pmu
.num_counters
= X86_PMC_MAX_GENERIC
;
1534 x86_pmu
.intel_ctrl
= (1 << x86_pmu
.num_counters
) - 1;
1536 if (x86_pmu
.num_counters_fixed
> X86_PMC_MAX_FIXED
) {
1537 WARN(1, KERN_ERR
"hw perf events fixed %d > max(%d), clipping!",
1538 x86_pmu
.num_counters_fixed
, X86_PMC_MAX_FIXED
);
1539 x86_pmu
.num_counters_fixed
= X86_PMC_MAX_FIXED
;
1542 x86_pmu
.intel_ctrl
|=
1543 ((1LL << x86_pmu
.num_counters_fixed
)-1) << X86_PMC_IDX_FIXED
;
1545 perf_events_lapic_init();
1546 register_die_notifier(&perf_event_nmi_notifier
);
1548 unconstrained
= (struct event_constraint
)
1549 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu
.num_counters
) - 1,
1550 0, x86_pmu
.num_counters
);
1552 if (x86_pmu
.event_constraints
) {
1553 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1554 if (c
->cmask
!= X86_RAW_EVENT_MASK
)
1557 c
->idxmsk64
|= (1ULL << x86_pmu
.num_counters
) - 1;
1558 c
->weight
+= x86_pmu
.num_counters
;
1562 pr_info("... version: %d\n", x86_pmu
.version
);
1563 pr_info("... bit width: %d\n", x86_pmu
.cntval_bits
);
1564 pr_info("... generic registers: %d\n", x86_pmu
.num_counters
);
1565 pr_info("... value mask: %016Lx\n", x86_pmu
.cntval_mask
);
1566 pr_info("... max period: %016Lx\n", x86_pmu
.max_period
);
1567 pr_info("... fixed-purpose events: %d\n", x86_pmu
.num_counters_fixed
);
1568 pr_info("... event mask: %016Lx\n", x86_pmu
.intel_ctrl
);
1570 perf_pmu_register(&pmu
, "cpu", PERF_TYPE_RAW
);
1571 perf_cpu_notifier(x86_pmu_notifier
);
1575 early_initcall(init_hw_perf_events
);
1577 static inline void x86_pmu_read(struct perf_event
*event
)
1579 x86_perf_event_update(event
);
1583 * Start group events scheduling transaction
1584 * Set the flag to make pmu::enable() not perform the
1585 * schedulability test, it will be performed at commit time
1587 static void x86_pmu_start_txn(struct pmu
*pmu
)
1589 perf_pmu_disable(pmu
);
1590 __this_cpu_or(cpu_hw_events
.group_flag
, PERF_EVENT_TXN
);
1591 __this_cpu_write(cpu_hw_events
.n_txn
, 0);
1595 * Stop group events scheduling transaction
1596 * Clear the flag and pmu::enable() will perform the
1597 * schedulability test.
1599 static void x86_pmu_cancel_txn(struct pmu
*pmu
)
1601 __this_cpu_and(cpu_hw_events
.group_flag
, ~PERF_EVENT_TXN
);
1603 * Truncate the collected events.
1605 __this_cpu_sub(cpu_hw_events
.n_added
, __this_cpu_read(cpu_hw_events
.n_txn
));
1606 __this_cpu_sub(cpu_hw_events
.n_events
, __this_cpu_read(cpu_hw_events
.n_txn
));
1607 perf_pmu_enable(pmu
);
1611 * Commit group events scheduling transaction
1612 * Perform the group schedulability test as a whole
1613 * Return 0 if success
1615 static int x86_pmu_commit_txn(struct pmu
*pmu
)
1617 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1618 int assign
[X86_PMC_IDX_MAX
];
1623 if (!x86_pmu_initialized())
1626 ret
= x86_pmu
.schedule_events(cpuc
, n
, assign
);
1631 * copy new assignment, now we know it is possible
1632 * will be used by hw_perf_enable()
1634 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
1636 cpuc
->group_flag
&= ~PERF_EVENT_TXN
;
1637 perf_pmu_enable(pmu
);
1642 * validate that we can schedule this event
1644 static int validate_event(struct perf_event
*event
)
1646 struct cpu_hw_events
*fake_cpuc
;
1647 struct event_constraint
*c
;
1650 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1654 c
= x86_pmu
.get_event_constraints(fake_cpuc
, event
);
1656 if (!c
|| !c
->weight
)
1659 if (x86_pmu
.put_event_constraints
)
1660 x86_pmu
.put_event_constraints(fake_cpuc
, event
);
1668 * validate a single event group
1670 * validation include:
1671 * - check events are compatible which each other
1672 * - events do not compete for the same counter
1673 * - number of events <= number of counters
1675 * validation ensures the group can be loaded onto the
1676 * PMU if it was the only group available.
1678 static int validate_group(struct perf_event
*event
)
1680 struct perf_event
*leader
= event
->group_leader
;
1681 struct cpu_hw_events
*fake_cpuc
;
1685 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1690 * the event is not yet connected with its
1691 * siblings therefore we must first collect
1692 * existing siblings, then add the new event
1693 * before we can simulate the scheduling
1696 n
= collect_events(fake_cpuc
, leader
, true);
1700 fake_cpuc
->n_events
= n
;
1701 n
= collect_events(fake_cpuc
, event
, false);
1705 fake_cpuc
->n_events
= n
;
1707 ret
= x86_pmu
.schedule_events(fake_cpuc
, n
, NULL
);
1715 static int x86_pmu_event_init(struct perf_event
*event
)
1720 switch (event
->attr
.type
) {
1722 case PERF_TYPE_HARDWARE
:
1723 case PERF_TYPE_HW_CACHE
:
1730 err
= __x86_pmu_event_init(event
);
1733 * we temporarily connect event to its pmu
1734 * such that validate_group() can classify
1735 * it as an x86 event using is_x86_event()
1740 if (event
->group_leader
!= event
)
1741 err
= validate_group(event
);
1743 err
= validate_event(event
);
1749 event
->destroy(event
);
1755 static struct pmu pmu
= {
1756 .pmu_enable
= x86_pmu_enable
,
1757 .pmu_disable
= x86_pmu_disable
,
1759 .event_init
= x86_pmu_event_init
,
1763 .start
= x86_pmu_start
,
1764 .stop
= x86_pmu_stop
,
1765 .read
= x86_pmu_read
,
1767 .start_txn
= x86_pmu_start_txn
,
1768 .cancel_txn
= x86_pmu_cancel_txn
,
1769 .commit_txn
= x86_pmu_commit_txn
,
1776 static int backtrace_stack(void *data
, char *name
)
1781 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
1783 struct perf_callchain_entry
*entry
= data
;
1785 perf_callchain_store(entry
, addr
);
1788 static const struct stacktrace_ops backtrace_ops
= {
1789 .stack
= backtrace_stack
,
1790 .address
= backtrace_address
,
1791 .walk_stack
= print_context_stack_bp
,
1795 perf_callchain_kernel(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
1797 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest()) {
1798 /* TODO: We don't support guest os callchain now */
1802 perf_callchain_store(entry
, regs
->ip
);
1804 dump_trace(NULL
, regs
, NULL
, 0, &backtrace_ops
, entry
);
1807 #ifdef CONFIG_COMPAT
1809 perf_callchain_user32(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1811 /* 32-bit process in 64-bit kernel. */
1812 struct stack_frame_ia32 frame
;
1813 const void __user
*fp
;
1815 if (!test_thread_flag(TIF_IA32
))
1818 fp
= compat_ptr(regs
->bp
);
1819 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1820 unsigned long bytes
;
1821 frame
.next_frame
= 0;
1822 frame
.return_address
= 0;
1824 bytes
= copy_from_user_nmi(&frame
, fp
, sizeof(frame
));
1825 if (bytes
!= sizeof(frame
))
1828 if (fp
< compat_ptr(regs
->sp
))
1831 perf_callchain_store(entry
, frame
.return_address
);
1832 fp
= compat_ptr(frame
.next_frame
);
1838 perf_callchain_user32(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1845 perf_callchain_user(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
1847 struct stack_frame frame
;
1848 const void __user
*fp
;
1850 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest()) {
1851 /* TODO: We don't support guest os callchain now */
1855 fp
= (void __user
*)regs
->bp
;
1857 perf_callchain_store(entry
, regs
->ip
);
1859 if (perf_callchain_user32(regs
, entry
))
1862 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1863 unsigned long bytes
;
1864 frame
.next_frame
= NULL
;
1865 frame
.return_address
= 0;
1867 bytes
= copy_from_user_nmi(&frame
, fp
, sizeof(frame
));
1868 if (bytes
!= sizeof(frame
))
1871 if ((unsigned long)fp
< regs
->sp
)
1874 perf_callchain_store(entry
, frame
.return_address
);
1875 fp
= frame
.next_frame
;
1879 unsigned long perf_instruction_pointer(struct pt_regs
*regs
)
1883 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest())
1884 ip
= perf_guest_cbs
->get_guest_ip();
1886 ip
= instruction_pointer(regs
);
1891 unsigned long perf_misc_flags(struct pt_regs
*regs
)
1895 if (perf_guest_cbs
&& perf_guest_cbs
->is_in_guest()) {
1896 if (perf_guest_cbs
->is_user_mode())
1897 misc
|= PERF_RECORD_MISC_GUEST_USER
;
1899 misc
|= PERF_RECORD_MISC_GUEST_KERNEL
;
1901 if (user_mode(regs
))
1902 misc
|= PERF_RECORD_MISC_USER
;
1904 misc
|= PERF_RECORD_MISC_KERNEL
;
1907 if (regs
->flags
& PERF_EFLAGS_EXACT
)
1908 misc
|= PERF_RECORD_MISC_EXACT_IP
;