perf_events: Fix event scheduling issues introduced by transactional API
[linux-2.6/x86.git] / arch / x86 / kernel / cpu / perf_event.c
blob5db5b7d65a180f6a7f0c2cb970d63e04129add2a
1 /*
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>
29 #include <asm/apic.h>
30 #include <asm/stacktrace.h>
31 #include <asm/nmi.h>
32 #include <asm/compat.h>
34 #if 0
35 #undef wrmsrl
36 #define wrmsrl(msr, val) \
37 do { \
38 trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\
39 (unsigned long)(val)); \
40 native_write_msr((msr), (u32)((u64)(val)), \
41 (u32)((u64)(val) >> 32)); \
42 } while (0)
43 #endif
46 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
48 static unsigned long
49 copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
51 unsigned long offset, addr = (unsigned long)from;
52 int type = in_nmi() ? KM_NMI : KM_IRQ0;
53 unsigned long size, len = 0;
54 struct page *page;
55 void *map;
56 int ret;
58 do {
59 ret = __get_user_pages_fast(addr, 1, 0, &page);
60 if (!ret)
61 break;
63 offset = addr & (PAGE_SIZE - 1);
64 size = min(PAGE_SIZE - offset, n - len);
66 map = kmap_atomic(page, type);
67 memcpy(to, map+offset, size);
68 kunmap_atomic(map, type);
69 put_page(page);
71 len += size;
72 to += size;
73 addr += size;
75 } while (len < n);
77 return len;
80 struct event_constraint {
81 union {
82 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
83 u64 idxmsk64;
85 u64 code;
86 u64 cmask;
87 int weight;
90 struct amd_nb {
91 int nb_id; /* NorthBridge id */
92 int refcnt; /* reference count */
93 struct perf_event *owners[X86_PMC_IDX_MAX];
94 struct event_constraint event_constraints[X86_PMC_IDX_MAX];
97 #define MAX_LBR_ENTRIES 16
99 struct cpu_hw_events {
101 * Generic x86 PMC bits
103 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
104 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
105 int enabled;
107 int n_events;
108 int n_added;
109 int n_txn;
110 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
111 u64 tags[X86_PMC_IDX_MAX];
112 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
114 unsigned int group_flag;
117 * Intel DebugStore bits
119 struct debug_store *ds;
120 u64 pebs_enabled;
123 * Intel LBR bits
125 int lbr_users;
126 void *lbr_context;
127 struct perf_branch_stack lbr_stack;
128 struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES];
131 * AMD specific bits
133 struct amd_nb *amd_nb;
136 #define __EVENT_CONSTRAINT(c, n, m, w) {\
137 { .idxmsk64 = (n) }, \
138 .code = (c), \
139 .cmask = (m), \
140 .weight = (w), \
143 #define EVENT_CONSTRAINT(c, n, m) \
144 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
147 * Constraint on the Event code.
149 #define INTEL_EVENT_CONSTRAINT(c, n) \
150 EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
153 * Constraint on the Event code + UMask + fixed-mask
155 * filter mask to validate fixed counter events.
156 * the following filters disqualify for fixed counters:
157 * - inv
158 * - edge
159 * - cnt-mask
160 * The other filters are supported by fixed counters.
161 * The any-thread option is supported starting with v3.
163 #define FIXED_EVENT_CONSTRAINT(c, n) \
164 EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
167 * Constraint on the Event code + UMask
169 #define PEBS_EVENT_CONSTRAINT(c, n) \
170 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
172 #define EVENT_CONSTRAINT_END \
173 EVENT_CONSTRAINT(0, 0, 0)
175 #define for_each_event_constraint(e, c) \
176 for ((e) = (c); (e)->weight; (e)++)
178 union perf_capabilities {
179 struct {
180 u64 lbr_format : 6;
181 u64 pebs_trap : 1;
182 u64 pebs_arch_reg : 1;
183 u64 pebs_format : 4;
184 u64 smm_freeze : 1;
186 u64 capabilities;
190 * struct x86_pmu - generic x86 pmu
192 struct x86_pmu {
194 * Generic x86 PMC bits
196 const char *name;
197 int version;
198 int (*handle_irq)(struct pt_regs *);
199 void (*disable_all)(void);
200 void (*enable_all)(int added);
201 void (*enable)(struct perf_event *);
202 void (*disable)(struct perf_event *);
203 int (*hw_config)(struct perf_event *event);
204 int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
205 unsigned eventsel;
206 unsigned perfctr;
207 u64 (*event_map)(int);
208 int max_events;
209 int num_counters;
210 int num_counters_fixed;
211 int cntval_bits;
212 u64 cntval_mask;
213 int apic;
214 u64 max_period;
215 struct event_constraint *
216 (*get_event_constraints)(struct cpu_hw_events *cpuc,
217 struct perf_event *event);
219 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
220 struct perf_event *event);
221 struct event_constraint *event_constraints;
222 void (*quirks)(void);
224 int (*cpu_prepare)(int cpu);
225 void (*cpu_starting)(int cpu);
226 void (*cpu_dying)(int cpu);
227 void (*cpu_dead)(int cpu);
230 * Intel Arch Perfmon v2+
232 u64 intel_ctrl;
233 union perf_capabilities intel_cap;
236 * Intel DebugStore bits
238 int bts, pebs;
239 int pebs_record_size;
240 void (*drain_pebs)(struct pt_regs *regs);
241 struct event_constraint *pebs_constraints;
244 * Intel LBR
246 unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */
247 int lbr_nr; /* hardware stack size */
250 static struct x86_pmu x86_pmu __read_mostly;
252 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
253 .enabled = 1,
256 static int x86_perf_event_set_period(struct perf_event *event);
259 * Generalized hw caching related hw_event table, filled
260 * in on a per model basis. A value of 0 means
261 * 'not supported', -1 means 'hw_event makes no sense on
262 * this CPU', any other value means the raw hw_event
263 * ID.
266 #define C(x) PERF_COUNT_HW_CACHE_##x
268 static u64 __read_mostly hw_cache_event_ids
269 [PERF_COUNT_HW_CACHE_MAX]
270 [PERF_COUNT_HW_CACHE_OP_MAX]
271 [PERF_COUNT_HW_CACHE_RESULT_MAX];
274 * Propagate event elapsed time into the generic event.
275 * Can only be executed on the CPU where the event is active.
276 * Returns the delta events processed.
278 static u64
279 x86_perf_event_update(struct perf_event *event)
281 struct hw_perf_event *hwc = &event->hw;
282 int shift = 64 - x86_pmu.cntval_bits;
283 u64 prev_raw_count, new_raw_count;
284 int idx = hwc->idx;
285 s64 delta;
287 if (idx == X86_PMC_IDX_FIXED_BTS)
288 return 0;
291 * Careful: an NMI might modify the previous event value.
293 * Our tactic to handle this is to first atomically read and
294 * exchange a new raw count - then add that new-prev delta
295 * count to the generic event atomically:
297 again:
298 prev_raw_count = atomic64_read(&hwc->prev_count);
299 rdmsrl(hwc->event_base + idx, new_raw_count);
301 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
302 new_raw_count) != prev_raw_count)
303 goto again;
306 * Now we have the new raw value and have updated the prev
307 * timestamp already. We can now calculate the elapsed delta
308 * (event-)time and add that to the generic event.
310 * Careful, not all hw sign-extends above the physical width
311 * of the count.
313 delta = (new_raw_count << shift) - (prev_raw_count << shift);
314 delta >>= shift;
316 atomic64_add(delta, &event->count);
317 atomic64_sub(delta, &hwc->period_left);
319 return new_raw_count;
322 static atomic_t active_events;
323 static DEFINE_MUTEX(pmc_reserve_mutex);
325 #ifdef CONFIG_X86_LOCAL_APIC
327 static bool reserve_pmc_hardware(void)
329 int i;
331 if (nmi_watchdog == NMI_LOCAL_APIC)
332 disable_lapic_nmi_watchdog();
334 for (i = 0; i < x86_pmu.num_counters; i++) {
335 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
336 goto perfctr_fail;
339 for (i = 0; i < x86_pmu.num_counters; i++) {
340 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
341 goto eventsel_fail;
344 return true;
346 eventsel_fail:
347 for (i--; i >= 0; i--)
348 release_evntsel_nmi(x86_pmu.eventsel + i);
350 i = x86_pmu.num_counters;
352 perfctr_fail:
353 for (i--; i >= 0; i--)
354 release_perfctr_nmi(x86_pmu.perfctr + i);
356 if (nmi_watchdog == NMI_LOCAL_APIC)
357 enable_lapic_nmi_watchdog();
359 return false;
362 static void release_pmc_hardware(void)
364 int i;
366 for (i = 0; i < x86_pmu.num_counters; i++) {
367 release_perfctr_nmi(x86_pmu.perfctr + i);
368 release_evntsel_nmi(x86_pmu.eventsel + i);
371 if (nmi_watchdog == NMI_LOCAL_APIC)
372 enable_lapic_nmi_watchdog();
375 #else
377 static bool reserve_pmc_hardware(void) { return true; }
378 static void release_pmc_hardware(void) {}
380 #endif
382 static int reserve_ds_buffers(void);
383 static void release_ds_buffers(void);
385 static void hw_perf_event_destroy(struct perf_event *event)
387 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
388 release_pmc_hardware();
389 release_ds_buffers();
390 mutex_unlock(&pmc_reserve_mutex);
394 static inline int x86_pmu_initialized(void)
396 return x86_pmu.handle_irq != NULL;
399 static inline int
400 set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
402 unsigned int cache_type, cache_op, cache_result;
403 u64 config, val;
405 config = attr->config;
407 cache_type = (config >> 0) & 0xff;
408 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
409 return -EINVAL;
411 cache_op = (config >> 8) & 0xff;
412 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
413 return -EINVAL;
415 cache_result = (config >> 16) & 0xff;
416 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
417 return -EINVAL;
419 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
421 if (val == 0)
422 return -ENOENT;
424 if (val == -1)
425 return -EINVAL;
427 hwc->config |= val;
429 return 0;
432 static int x86_setup_perfctr(struct perf_event *event)
434 struct perf_event_attr *attr = &event->attr;
435 struct hw_perf_event *hwc = &event->hw;
436 u64 config;
438 if (!hwc->sample_period) {
439 hwc->sample_period = x86_pmu.max_period;
440 hwc->last_period = hwc->sample_period;
441 atomic64_set(&hwc->period_left, hwc->sample_period);
442 } else {
444 * If we have a PMU initialized but no APIC
445 * interrupts, we cannot sample hardware
446 * events (user-space has to fall back and
447 * sample via a hrtimer based software event):
449 if (!x86_pmu.apic)
450 return -EOPNOTSUPP;
453 if (attr->type == PERF_TYPE_RAW)
454 return 0;
456 if (attr->type == PERF_TYPE_HW_CACHE)
457 return set_ext_hw_attr(hwc, attr);
459 if (attr->config >= x86_pmu.max_events)
460 return -EINVAL;
463 * The generic map:
465 config = x86_pmu.event_map(attr->config);
467 if (config == 0)
468 return -ENOENT;
470 if (config == -1LL)
471 return -EINVAL;
474 * Branch tracing:
476 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
477 (hwc->sample_period == 1)) {
478 /* BTS is not supported by this architecture. */
479 if (!x86_pmu.bts)
480 return -EOPNOTSUPP;
482 /* BTS is currently only allowed for user-mode. */
483 if (!attr->exclude_kernel)
484 return -EOPNOTSUPP;
487 hwc->config |= config;
489 return 0;
492 static int x86_pmu_hw_config(struct perf_event *event)
494 if (event->attr.precise_ip) {
495 int precise = 0;
497 /* Support for constant skid */
498 if (x86_pmu.pebs)
499 precise++;
501 /* Support for IP fixup */
502 if (x86_pmu.lbr_nr)
503 precise++;
505 if (event->attr.precise_ip > precise)
506 return -EOPNOTSUPP;
510 * Generate PMC IRQs:
511 * (keep 'enabled' bit clear for now)
513 event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
516 * Count user and OS events unless requested not to
518 if (!event->attr.exclude_user)
519 event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
520 if (!event->attr.exclude_kernel)
521 event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
523 if (event->attr.type == PERF_TYPE_RAW)
524 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
526 return x86_setup_perfctr(event);
530 * Setup the hardware configuration for a given attr_type
532 static int __hw_perf_event_init(struct perf_event *event)
534 int err;
536 if (!x86_pmu_initialized())
537 return -ENODEV;
539 err = 0;
540 if (!atomic_inc_not_zero(&active_events)) {
541 mutex_lock(&pmc_reserve_mutex);
542 if (atomic_read(&active_events) == 0) {
543 if (!reserve_pmc_hardware())
544 err = -EBUSY;
545 else {
546 err = reserve_ds_buffers();
547 if (err)
548 release_pmc_hardware();
551 if (!err)
552 atomic_inc(&active_events);
553 mutex_unlock(&pmc_reserve_mutex);
555 if (err)
556 return err;
558 event->destroy = hw_perf_event_destroy;
560 event->hw.idx = -1;
561 event->hw.last_cpu = -1;
562 event->hw.last_tag = ~0ULL;
564 return x86_pmu.hw_config(event);
567 static void x86_pmu_disable_all(void)
569 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
570 int idx;
572 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
573 u64 val;
575 if (!test_bit(idx, cpuc->active_mask))
576 continue;
577 rdmsrl(x86_pmu.eventsel + idx, val);
578 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
579 continue;
580 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
581 wrmsrl(x86_pmu.eventsel + idx, val);
585 void hw_perf_disable(void)
587 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
589 if (!x86_pmu_initialized())
590 return;
592 if (!cpuc->enabled)
593 return;
595 cpuc->n_added = 0;
596 cpuc->enabled = 0;
597 barrier();
599 x86_pmu.disable_all();
602 static void x86_pmu_enable_all(int added)
604 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
605 int idx;
607 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
608 struct perf_event *event = cpuc->events[idx];
609 u64 val;
611 if (!test_bit(idx, cpuc->active_mask))
612 continue;
614 val = event->hw.config;
615 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
616 wrmsrl(x86_pmu.eventsel + idx, val);
620 static const struct pmu pmu;
622 static inline int is_x86_event(struct perf_event *event)
624 return event->pmu == &pmu;
627 static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
629 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
630 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
631 int i, j, w, wmax, num = 0;
632 struct hw_perf_event *hwc;
634 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
636 for (i = 0; i < n; i++) {
637 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
638 constraints[i] = c;
642 * fastpath, try to reuse previous register
644 for (i = 0; i < n; i++) {
645 hwc = &cpuc->event_list[i]->hw;
646 c = constraints[i];
648 /* never assigned */
649 if (hwc->idx == -1)
650 break;
652 /* constraint still honored */
653 if (!test_bit(hwc->idx, c->idxmsk))
654 break;
656 /* not already used */
657 if (test_bit(hwc->idx, used_mask))
658 break;
660 __set_bit(hwc->idx, used_mask);
661 if (assign)
662 assign[i] = hwc->idx;
664 if (i == n)
665 goto done;
668 * begin slow path
671 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
674 * weight = number of possible counters
676 * 1 = most constrained, only works on one counter
677 * wmax = least constrained, works on any counter
679 * assign events to counters starting with most
680 * constrained events.
682 wmax = x86_pmu.num_counters;
685 * when fixed event counters are present,
686 * wmax is incremented by 1 to account
687 * for one more choice
689 if (x86_pmu.num_counters_fixed)
690 wmax++;
692 for (w = 1, num = n; num && w <= wmax; w++) {
693 /* for each event */
694 for (i = 0; num && i < n; i++) {
695 c = constraints[i];
696 hwc = &cpuc->event_list[i]->hw;
698 if (c->weight != w)
699 continue;
701 for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
702 if (!test_bit(j, used_mask))
703 break;
706 if (j == X86_PMC_IDX_MAX)
707 break;
709 __set_bit(j, used_mask);
711 if (assign)
712 assign[i] = j;
713 num--;
716 done:
718 * scheduling failed or is just a simulation,
719 * free resources if necessary
721 if (!assign || num) {
722 for (i = 0; i < n; i++) {
723 if (x86_pmu.put_event_constraints)
724 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
727 return num ? -ENOSPC : 0;
731 * dogrp: true if must collect siblings events (group)
732 * returns total number of events and error code
734 static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
736 struct perf_event *event;
737 int n, max_count;
739 max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
741 /* current number of events already accepted */
742 n = cpuc->n_events;
744 if (is_x86_event(leader)) {
745 if (n >= max_count)
746 return -ENOSPC;
747 cpuc->event_list[n] = leader;
748 n++;
750 if (!dogrp)
751 return n;
753 list_for_each_entry(event, &leader->sibling_list, group_entry) {
754 if (!is_x86_event(event) ||
755 event->state <= PERF_EVENT_STATE_OFF)
756 continue;
758 if (n >= max_count)
759 return -ENOSPC;
761 cpuc->event_list[n] = event;
762 n++;
764 return n;
767 static inline void x86_assign_hw_event(struct perf_event *event,
768 struct cpu_hw_events *cpuc, int i)
770 struct hw_perf_event *hwc = &event->hw;
772 hwc->idx = cpuc->assign[i];
773 hwc->last_cpu = smp_processor_id();
774 hwc->last_tag = ++cpuc->tags[i];
776 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
777 hwc->config_base = 0;
778 hwc->event_base = 0;
779 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
780 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
782 * We set it so that event_base + idx in wrmsr/rdmsr maps to
783 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
785 hwc->event_base =
786 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
787 } else {
788 hwc->config_base = x86_pmu.eventsel;
789 hwc->event_base = x86_pmu.perfctr;
793 static inline int match_prev_assignment(struct hw_perf_event *hwc,
794 struct cpu_hw_events *cpuc,
795 int i)
797 return hwc->idx == cpuc->assign[i] &&
798 hwc->last_cpu == smp_processor_id() &&
799 hwc->last_tag == cpuc->tags[i];
802 static int x86_pmu_start(struct perf_event *event);
803 static void x86_pmu_stop(struct perf_event *event);
805 void hw_perf_enable(void)
807 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
808 struct perf_event *event;
809 struct hw_perf_event *hwc;
810 int i, added = cpuc->n_added;
812 if (!x86_pmu_initialized())
813 return;
815 if (cpuc->enabled)
816 return;
818 if (cpuc->n_added) {
819 int n_running = cpuc->n_events - cpuc->n_added;
821 * apply assignment obtained either from
822 * hw_perf_group_sched_in() or x86_pmu_enable()
824 * step1: save events moving to new counters
825 * step2: reprogram moved events into new counters
827 for (i = 0; i < n_running; i++) {
828 event = cpuc->event_list[i];
829 hwc = &event->hw;
832 * we can avoid reprogramming counter if:
833 * - assigned same counter as last time
834 * - running on same CPU as last time
835 * - no other event has used the counter since
837 if (hwc->idx == -1 ||
838 match_prev_assignment(hwc, cpuc, i))
839 continue;
841 x86_pmu_stop(event);
844 for (i = 0; i < cpuc->n_events; i++) {
845 event = cpuc->event_list[i];
846 hwc = &event->hw;
848 if (!match_prev_assignment(hwc, cpuc, i))
849 x86_assign_hw_event(event, cpuc, i);
850 else if (i < n_running)
851 continue;
853 x86_pmu_start(event);
855 cpuc->n_added = 0;
856 perf_events_lapic_init();
859 cpuc->enabled = 1;
860 barrier();
862 x86_pmu.enable_all(added);
865 static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
866 u64 enable_mask)
868 wrmsrl(hwc->config_base + hwc->idx, hwc->config | enable_mask);
871 static inline void x86_pmu_disable_event(struct perf_event *event)
873 struct hw_perf_event *hwc = &event->hw;
875 wrmsrl(hwc->config_base + hwc->idx, hwc->config);
878 static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
881 * Set the next IRQ period, based on the hwc->period_left value.
882 * To be called with the event disabled in hw:
884 static int
885 x86_perf_event_set_period(struct perf_event *event)
887 struct hw_perf_event *hwc = &event->hw;
888 s64 left = atomic64_read(&hwc->period_left);
889 s64 period = hwc->sample_period;
890 int ret = 0, idx = hwc->idx;
892 if (idx == X86_PMC_IDX_FIXED_BTS)
893 return 0;
896 * If we are way outside a reasonable range then just skip forward:
898 if (unlikely(left <= -period)) {
899 left = period;
900 atomic64_set(&hwc->period_left, left);
901 hwc->last_period = period;
902 ret = 1;
905 if (unlikely(left <= 0)) {
906 left += period;
907 atomic64_set(&hwc->period_left, left);
908 hwc->last_period = period;
909 ret = 1;
912 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
914 if (unlikely(left < 2))
915 left = 2;
917 if (left > x86_pmu.max_period)
918 left = x86_pmu.max_period;
920 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
923 * The hw event starts counting from this event offset,
924 * mark it to be able to extra future deltas:
926 atomic64_set(&hwc->prev_count, (u64)-left);
928 wrmsrl(hwc->event_base + idx,
929 (u64)(-left) & x86_pmu.cntval_mask);
931 perf_event_update_userpage(event);
933 return ret;
936 static void x86_pmu_enable_event(struct perf_event *event)
938 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
939 if (cpuc->enabled)
940 __x86_pmu_enable_event(&event->hw,
941 ARCH_PERFMON_EVENTSEL_ENABLE);
945 * activate a single event
947 * The event is added to the group of enabled events
948 * but only if it can be scehduled with existing events.
950 * Called with PMU disabled. If successful and return value 1,
951 * then guaranteed to call perf_enable() and hw_perf_enable()
953 static int x86_pmu_enable(struct perf_event *event)
955 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
956 struct hw_perf_event *hwc;
957 int assign[X86_PMC_IDX_MAX];
958 int n, n0, ret;
960 hwc = &event->hw;
962 n0 = cpuc->n_events;
963 n = collect_events(cpuc, event, false);
964 if (n < 0)
965 return n;
968 * If group events scheduling transaction was started,
969 * skip the schedulability test here, it will be peformed
970 * at commit time(->commit_txn) as a whole
972 if (cpuc->group_flag & PERF_EVENT_TXN_STARTED)
973 goto out;
975 ret = x86_pmu.schedule_events(cpuc, n, assign);
976 if (ret)
977 return ret;
979 * copy new assignment, now we know it is possible
980 * will be used by hw_perf_enable()
982 memcpy(cpuc->assign, assign, n*sizeof(int));
984 out:
985 cpuc->n_events = n;
986 cpuc->n_added += n - n0;
987 cpuc->n_txn += n - n0;
989 return 0;
992 static int x86_pmu_start(struct perf_event *event)
994 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
995 int idx = event->hw.idx;
997 if (idx == -1)
998 return -EAGAIN;
1000 x86_perf_event_set_period(event);
1001 cpuc->events[idx] = event;
1002 __set_bit(idx, cpuc->active_mask);
1003 x86_pmu.enable(event);
1004 perf_event_update_userpage(event);
1006 return 0;
1009 static void x86_pmu_unthrottle(struct perf_event *event)
1011 int ret = x86_pmu_start(event);
1012 WARN_ON_ONCE(ret);
1015 void perf_event_print_debug(void)
1017 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
1018 u64 pebs;
1019 struct cpu_hw_events *cpuc;
1020 unsigned long flags;
1021 int cpu, idx;
1023 if (!x86_pmu.num_counters)
1024 return;
1026 local_irq_save(flags);
1028 cpu = smp_processor_id();
1029 cpuc = &per_cpu(cpu_hw_events, cpu);
1031 if (x86_pmu.version >= 2) {
1032 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1033 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1034 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1035 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
1036 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
1038 pr_info("\n");
1039 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1040 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1041 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1042 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
1043 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
1045 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
1047 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
1048 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
1049 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
1051 prev_left = per_cpu(pmc_prev_left[idx], cpu);
1053 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1054 cpu, idx, pmc_ctrl);
1055 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1056 cpu, idx, pmc_count);
1057 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1058 cpu, idx, prev_left);
1060 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
1061 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1063 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1064 cpu, idx, pmc_count);
1066 local_irq_restore(flags);
1069 static void x86_pmu_stop(struct perf_event *event)
1071 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1072 struct hw_perf_event *hwc = &event->hw;
1073 int idx = hwc->idx;
1075 if (!__test_and_clear_bit(idx, cpuc->active_mask))
1076 return;
1078 x86_pmu.disable(event);
1081 * Drain the remaining delta count out of a event
1082 * that we are disabling:
1084 x86_perf_event_update(event);
1086 cpuc->events[idx] = NULL;
1089 static void x86_pmu_disable(struct perf_event *event)
1091 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1092 int i;
1095 * If we're called during a txn, we don't need to do anything.
1096 * The events never got scheduled and ->cancel_txn will truncate
1097 * the event_list.
1099 if (cpuc->group_flag & PERF_EVENT_TXN_STARTED)
1100 return;
1102 x86_pmu_stop(event);
1104 for (i = 0; i < cpuc->n_events; i++) {
1105 if (event == cpuc->event_list[i]) {
1107 if (x86_pmu.put_event_constraints)
1108 x86_pmu.put_event_constraints(cpuc, event);
1110 while (++i < cpuc->n_events)
1111 cpuc->event_list[i-1] = cpuc->event_list[i];
1113 --cpuc->n_events;
1114 break;
1117 perf_event_update_userpage(event);
1120 static int x86_pmu_handle_irq(struct pt_regs *regs)
1122 struct perf_sample_data data;
1123 struct cpu_hw_events *cpuc;
1124 struct perf_event *event;
1125 struct hw_perf_event *hwc;
1126 int idx, handled = 0;
1127 u64 val;
1129 perf_sample_data_init(&data, 0);
1131 cpuc = &__get_cpu_var(cpu_hw_events);
1133 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
1134 if (!test_bit(idx, cpuc->active_mask))
1135 continue;
1137 event = cpuc->events[idx];
1138 hwc = &event->hw;
1140 val = x86_perf_event_update(event);
1141 if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
1142 continue;
1145 * event overflow
1147 handled = 1;
1148 data.period = event->hw.last_period;
1150 if (!x86_perf_event_set_period(event))
1151 continue;
1153 if (perf_event_overflow(event, 1, &data, regs))
1154 x86_pmu_stop(event);
1157 if (handled)
1158 inc_irq_stat(apic_perf_irqs);
1160 return handled;
1163 void smp_perf_pending_interrupt(struct pt_regs *regs)
1165 irq_enter();
1166 ack_APIC_irq();
1167 inc_irq_stat(apic_pending_irqs);
1168 perf_event_do_pending();
1169 irq_exit();
1172 void set_perf_event_pending(void)
1174 #ifdef CONFIG_X86_LOCAL_APIC
1175 if (!x86_pmu.apic || !x86_pmu_initialized())
1176 return;
1178 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
1179 #endif
1182 void perf_events_lapic_init(void)
1184 if (!x86_pmu.apic || !x86_pmu_initialized())
1185 return;
1188 * Always use NMI for PMU
1190 apic_write(APIC_LVTPC, APIC_DM_NMI);
1193 static int __kprobes
1194 perf_event_nmi_handler(struct notifier_block *self,
1195 unsigned long cmd, void *__args)
1197 struct die_args *args = __args;
1198 struct pt_regs *regs;
1200 if (!atomic_read(&active_events))
1201 return NOTIFY_DONE;
1203 switch (cmd) {
1204 case DIE_NMI:
1205 case DIE_NMI_IPI:
1206 break;
1208 default:
1209 return NOTIFY_DONE;
1212 regs = args->regs;
1214 apic_write(APIC_LVTPC, APIC_DM_NMI);
1216 * Can't rely on the handled return value to say it was our NMI, two
1217 * events could trigger 'simultaneously' raising two back-to-back NMIs.
1219 * If the first NMI handles both, the latter will be empty and daze
1220 * the CPU.
1222 x86_pmu.handle_irq(regs);
1224 return NOTIFY_STOP;
1227 static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1228 .notifier_call = perf_event_nmi_handler,
1229 .next = NULL,
1230 .priority = 1
1233 static struct event_constraint unconstrained;
1234 static struct event_constraint emptyconstraint;
1236 static struct event_constraint *
1237 x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
1239 struct event_constraint *c;
1241 if (x86_pmu.event_constraints) {
1242 for_each_event_constraint(c, x86_pmu.event_constraints) {
1243 if ((event->hw.config & c->cmask) == c->code)
1244 return c;
1248 return &unconstrained;
1251 #include "perf_event_amd.c"
1252 #include "perf_event_p6.c"
1253 #include "perf_event_p4.c"
1254 #include "perf_event_intel_lbr.c"
1255 #include "perf_event_intel_ds.c"
1256 #include "perf_event_intel.c"
1258 static int __cpuinit
1259 x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1261 unsigned int cpu = (long)hcpu;
1262 int ret = NOTIFY_OK;
1264 switch (action & ~CPU_TASKS_FROZEN) {
1265 case CPU_UP_PREPARE:
1266 if (x86_pmu.cpu_prepare)
1267 ret = x86_pmu.cpu_prepare(cpu);
1268 break;
1270 case CPU_STARTING:
1271 if (x86_pmu.cpu_starting)
1272 x86_pmu.cpu_starting(cpu);
1273 break;
1275 case CPU_DYING:
1276 if (x86_pmu.cpu_dying)
1277 x86_pmu.cpu_dying(cpu);
1278 break;
1280 case CPU_UP_CANCELED:
1281 case CPU_DEAD:
1282 if (x86_pmu.cpu_dead)
1283 x86_pmu.cpu_dead(cpu);
1284 break;
1286 default:
1287 break;
1290 return ret;
1293 static void __init pmu_check_apic(void)
1295 if (cpu_has_apic)
1296 return;
1298 x86_pmu.apic = 0;
1299 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1300 pr_info("no hardware sampling interrupt available.\n");
1303 void __init init_hw_perf_events(void)
1305 struct event_constraint *c;
1306 int err;
1308 pr_info("Performance Events: ");
1310 switch (boot_cpu_data.x86_vendor) {
1311 case X86_VENDOR_INTEL:
1312 err = intel_pmu_init();
1313 break;
1314 case X86_VENDOR_AMD:
1315 err = amd_pmu_init();
1316 break;
1317 default:
1318 return;
1320 if (err != 0) {
1321 pr_cont("no PMU driver, software events only.\n");
1322 return;
1325 pmu_check_apic();
1327 pr_cont("%s PMU driver.\n", x86_pmu.name);
1329 if (x86_pmu.quirks)
1330 x86_pmu.quirks();
1332 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
1333 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
1334 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
1335 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
1337 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
1338 perf_max_events = x86_pmu.num_counters;
1340 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
1341 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
1342 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1343 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
1346 x86_pmu.intel_ctrl |=
1347 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1349 perf_events_lapic_init();
1350 register_die_notifier(&perf_event_nmi_notifier);
1352 unconstrained = (struct event_constraint)
1353 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
1354 0, x86_pmu.num_counters);
1356 if (x86_pmu.event_constraints) {
1357 for_each_event_constraint(c, x86_pmu.event_constraints) {
1358 if (c->cmask != X86_RAW_EVENT_MASK)
1359 continue;
1361 c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
1362 c->weight += x86_pmu.num_counters;
1366 pr_info("... version: %d\n", x86_pmu.version);
1367 pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
1368 pr_info("... generic registers: %d\n", x86_pmu.num_counters);
1369 pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
1370 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
1371 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
1372 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
1374 perf_cpu_notifier(x86_pmu_notifier);
1377 static inline void x86_pmu_read(struct perf_event *event)
1379 x86_perf_event_update(event);
1383 * Start group events scheduling transaction
1384 * Set the flag to make pmu::enable() not perform the
1385 * schedulability test, it will be performed at commit time
1387 static void x86_pmu_start_txn(const struct pmu *pmu)
1389 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1391 cpuc->group_flag |= PERF_EVENT_TXN_STARTED;
1392 cpuc->n_txn = 0;
1396 * Stop group events scheduling transaction
1397 * Clear the flag and pmu::enable() will perform the
1398 * schedulability test.
1400 static void x86_pmu_cancel_txn(const struct pmu *pmu)
1402 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1404 cpuc->group_flag &= ~PERF_EVENT_TXN_STARTED;
1406 * Truncate the collected events.
1408 cpuc->n_added -= cpuc->n_txn;
1409 cpuc->n_events -= cpuc->n_txn;
1413 * Commit group events scheduling transaction
1414 * Perform the group schedulability test as a whole
1415 * Return 0 if success
1417 static int x86_pmu_commit_txn(const struct pmu *pmu)
1419 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1420 int assign[X86_PMC_IDX_MAX];
1421 int n, ret;
1423 n = cpuc->n_events;
1425 if (!x86_pmu_initialized())
1426 return -EAGAIN;
1428 ret = x86_pmu.schedule_events(cpuc, n, assign);
1429 if (ret)
1430 return ret;
1433 * copy new assignment, now we know it is possible
1434 * will be used by hw_perf_enable()
1436 memcpy(cpuc->assign, assign, n*sizeof(int));
1439 * Clear out the txn count so that ->cancel_txn() which gets
1440 * run after ->commit_txn() doesn't undo things.
1442 cpuc->n_txn = 0;
1444 return 0;
1447 static const struct pmu pmu = {
1448 .enable = x86_pmu_enable,
1449 .disable = x86_pmu_disable,
1450 .start = x86_pmu_start,
1451 .stop = x86_pmu_stop,
1452 .read = x86_pmu_read,
1453 .unthrottle = x86_pmu_unthrottle,
1454 .start_txn = x86_pmu_start_txn,
1455 .cancel_txn = x86_pmu_cancel_txn,
1456 .commit_txn = x86_pmu_commit_txn,
1460 * validate that we can schedule this event
1462 static int validate_event(struct perf_event *event)
1464 struct cpu_hw_events *fake_cpuc;
1465 struct event_constraint *c;
1466 int ret = 0;
1468 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1469 if (!fake_cpuc)
1470 return -ENOMEM;
1472 c = x86_pmu.get_event_constraints(fake_cpuc, event);
1474 if (!c || !c->weight)
1475 ret = -ENOSPC;
1477 if (x86_pmu.put_event_constraints)
1478 x86_pmu.put_event_constraints(fake_cpuc, event);
1480 kfree(fake_cpuc);
1482 return ret;
1486 * validate a single event group
1488 * validation include:
1489 * - check events are compatible which each other
1490 * - events do not compete for the same counter
1491 * - number of events <= number of counters
1493 * validation ensures the group can be loaded onto the
1494 * PMU if it was the only group available.
1496 static int validate_group(struct perf_event *event)
1498 struct perf_event *leader = event->group_leader;
1499 struct cpu_hw_events *fake_cpuc;
1500 int ret, n;
1502 ret = -ENOMEM;
1503 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1504 if (!fake_cpuc)
1505 goto out;
1508 * the event is not yet connected with its
1509 * siblings therefore we must first collect
1510 * existing siblings, then add the new event
1511 * before we can simulate the scheduling
1513 ret = -ENOSPC;
1514 n = collect_events(fake_cpuc, leader, true);
1515 if (n < 0)
1516 goto out_free;
1518 fake_cpuc->n_events = n;
1519 n = collect_events(fake_cpuc, event, false);
1520 if (n < 0)
1521 goto out_free;
1523 fake_cpuc->n_events = n;
1525 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
1527 out_free:
1528 kfree(fake_cpuc);
1529 out:
1530 return ret;
1533 const struct pmu *hw_perf_event_init(struct perf_event *event)
1535 const struct pmu *tmp;
1536 int err;
1538 err = __hw_perf_event_init(event);
1539 if (!err) {
1541 * we temporarily connect event to its pmu
1542 * such that validate_group() can classify
1543 * it as an x86 event using is_x86_event()
1545 tmp = event->pmu;
1546 event->pmu = &pmu;
1548 if (event->group_leader != event)
1549 err = validate_group(event);
1550 else
1551 err = validate_event(event);
1553 event->pmu = tmp;
1555 if (err) {
1556 if (event->destroy)
1557 event->destroy(event);
1558 return ERR_PTR(err);
1561 return &pmu;
1565 * callchain support
1568 static inline
1569 void callchain_store(struct perf_callchain_entry *entry, u64 ip)
1571 if (entry->nr < PERF_MAX_STACK_DEPTH)
1572 entry->ip[entry->nr++] = ip;
1575 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
1576 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
1579 static void
1580 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1582 /* Ignore warnings */
1585 static void backtrace_warning(void *data, char *msg)
1587 /* Ignore warnings */
1590 static int backtrace_stack(void *data, char *name)
1592 return 0;
1595 static void backtrace_address(void *data, unsigned long addr, int reliable)
1597 struct perf_callchain_entry *entry = data;
1599 callchain_store(entry, addr);
1602 static const struct stacktrace_ops backtrace_ops = {
1603 .warning = backtrace_warning,
1604 .warning_symbol = backtrace_warning_symbol,
1605 .stack = backtrace_stack,
1606 .address = backtrace_address,
1607 .walk_stack = print_context_stack_bp,
1610 #include "../dumpstack.h"
1612 static void
1613 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1615 callchain_store(entry, PERF_CONTEXT_KERNEL);
1616 callchain_store(entry, regs->ip);
1618 dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
1621 #ifdef CONFIG_COMPAT
1622 static inline int
1623 perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
1625 /* 32-bit process in 64-bit kernel. */
1626 struct stack_frame_ia32 frame;
1627 const void __user *fp;
1629 if (!test_thread_flag(TIF_IA32))
1630 return 0;
1632 fp = compat_ptr(regs->bp);
1633 while (entry->nr < PERF_MAX_STACK_DEPTH) {
1634 unsigned long bytes;
1635 frame.next_frame = 0;
1636 frame.return_address = 0;
1638 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1639 if (bytes != sizeof(frame))
1640 break;
1642 if (fp < compat_ptr(regs->sp))
1643 break;
1645 callchain_store(entry, frame.return_address);
1646 fp = compat_ptr(frame.next_frame);
1648 return 1;
1650 #else
1651 static inline int
1652 perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
1654 return 0;
1656 #endif
1658 static void
1659 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1661 struct stack_frame frame;
1662 const void __user *fp;
1664 if (!user_mode(regs))
1665 regs = task_pt_regs(current);
1667 fp = (void __user *)regs->bp;
1669 callchain_store(entry, PERF_CONTEXT_USER);
1670 callchain_store(entry, regs->ip);
1672 if (perf_callchain_user32(regs, entry))
1673 return;
1675 while (entry->nr < PERF_MAX_STACK_DEPTH) {
1676 unsigned long bytes;
1677 frame.next_frame = NULL;
1678 frame.return_address = 0;
1680 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1681 if (bytes != sizeof(frame))
1682 break;
1684 if ((unsigned long)fp < regs->sp)
1685 break;
1687 callchain_store(entry, frame.return_address);
1688 fp = frame.next_frame;
1692 static void
1693 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1695 int is_user;
1697 if (!regs)
1698 return;
1700 is_user = user_mode(regs);
1702 if (is_user && current->state != TASK_RUNNING)
1703 return;
1705 if (!is_user)
1706 perf_callchain_kernel(regs, entry);
1708 if (current->mm)
1709 perf_callchain_user(regs, entry);
1712 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1714 struct perf_callchain_entry *entry;
1716 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1717 /* TODO: We don't support guest os callchain now */
1718 return NULL;
1721 if (in_nmi())
1722 entry = &__get_cpu_var(pmc_nmi_entry);
1723 else
1724 entry = &__get_cpu_var(pmc_irq_entry);
1726 entry->nr = 0;
1728 perf_do_callchain(regs, entry);
1730 return entry;
1733 void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
1735 regs->ip = ip;
1737 * perf_arch_fetch_caller_regs adds another call, we need to increment
1738 * the skip level
1740 regs->bp = rewind_frame_pointer(skip + 1);
1741 regs->cs = __KERNEL_CS;
1743 * We abuse bit 3 to pass exact information, see perf_misc_flags
1744 * and the comment with PERF_EFLAGS_EXACT.
1746 regs->flags = 0;
1749 unsigned long perf_instruction_pointer(struct pt_regs *regs)
1751 unsigned long ip;
1753 if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
1754 ip = perf_guest_cbs->get_guest_ip();
1755 else
1756 ip = instruction_pointer(regs);
1758 return ip;
1761 unsigned long perf_misc_flags(struct pt_regs *regs)
1763 int misc = 0;
1765 if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1766 if (perf_guest_cbs->is_user_mode())
1767 misc |= PERF_RECORD_MISC_GUEST_USER;
1768 else
1769 misc |= PERF_RECORD_MISC_GUEST_KERNEL;
1770 } else {
1771 if (user_mode(regs))
1772 misc |= PERF_RECORD_MISC_USER;
1773 else
1774 misc |= PERF_RECORD_MISC_KERNEL;
1777 if (regs->flags & PERF_EFLAGS_EXACT)
1778 misc |= PERF_RECORD_MISC_EXACT_IP;
1780 return misc;