diff options
| author | Linus Torvalds <torvalds@linux-foundation.org> | 2010-05-18 11:19:03 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2010-05-18 11:19:03 -0400 |
| commit | 4d7b4ac22fbec1a03206c6cde353f2fd6942f828 (patch) | |
| tree | 2d96a9e9c28cf6fa628a278decc00ad55a8b043b /arch/x86/kernel | |
| parent | 3aaf51ace5975050ab43c7d4d7e439e0ae7d13d7 (diff) | |
| parent | 94f3ca95787ada3d64339a4ecb2754236ab563f6 (diff) | |
Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: (311 commits)
perf tools: Add mode to build without newt support
perf symbols: symbol inconsistency message should be done only at verbose=1
perf tui: Add explicit -lslang option
perf options: Type check all the remaining OPT_ variants
perf options: Type check OPT_BOOLEAN and fix the offenders
perf options: Check v type in OPT_U?INTEGER
perf options: Introduce OPT_UINTEGER
perf tui: Add workaround for slang < 2.1.4
perf record: Fix bug mismatch with -c option definition
perf options: Introduce OPT_U64
perf tui: Add help window to show key associations
perf tui: Make <- exit menus too
perf newt: Add single key shortcuts for zoom into DSO and threads
perf newt: Exit browser unconditionally when CTRL+C, q or Q is pressed
perf newt: Fix the 'A'/'a' shortcut for annotate
perf newt: Make <- exit the ui_browser
x86, perf: P4 PMU - fix counters management logic
perf newt: Make <- zoom out filters
perf report: Report number of events, not samples
perf hist: Clarify events_stats fields usage
...
Fix up trivial conflicts in kernel/fork.c and tools/perf/builtin-record.c
Diffstat (limited to 'arch/x86/kernel')
| -rw-r--r-- | arch/x86/kernel/Makefile | 2 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/intel.c | 2 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event.c | 815 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_amd.c | 46 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_intel.c | 357 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_intel_ds.c | 641 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_intel_lbr.c | 218 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_p4.c | 857 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/perf_event_p6.c | 31 | ||||
| -rw-r--r-- | arch/x86/kernel/ds.c | 1437 | ||||
| -rw-r--r-- | arch/x86/kernel/ds_selftest.c | 408 | ||||
| -rw-r--r-- | arch/x86/kernel/ds_selftest.h | 15 | ||||
| -rw-r--r-- | arch/x86/kernel/dumpstack.c | 5 | ||||
| -rw-r--r-- | arch/x86/kernel/hw_breakpoint.c | 41 | ||||
| -rw-r--r-- | arch/x86/kernel/kprobes.c | 16 | ||||
| -rw-r--r-- | arch/x86/kernel/process.c | 18 | ||||
| -rw-r--r-- | arch/x86/kernel/process_32.c | 8 | ||||
| -rw-r--r-- | arch/x86/kernel/process_64.c | 8 | ||||
| -rw-r--r-- | arch/x86/kernel/ptrace.c | 384 | ||||
| -rw-r--r-- | arch/x86/kernel/step.c | 46 | ||||
| -rw-r--r-- | arch/x86/kernel/traps.c | 4 |
21 files changed, 2394 insertions, 2965 deletions
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 4c58352209e0..e77b22083721 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile | |||
| @@ -47,8 +47,6 @@ obj-$(CONFIG_X86_TRAMPOLINE) += trampoline.o | |||
| 47 | obj-y += process.o | 47 | obj-y += process.o |
| 48 | obj-y += i387.o xsave.o | 48 | obj-y += i387.o xsave.o |
| 49 | obj-y += ptrace.o | 49 | obj-y += ptrace.o |
| 50 | obj-$(CONFIG_X86_DS) += ds.o | ||
| 51 | obj-$(CONFIG_X86_DS_SELFTEST) += ds_selftest.o | ||
| 52 | obj-$(CONFIG_X86_32) += tls.o | 50 | obj-$(CONFIG_X86_32) += tls.o |
| 53 | obj-$(CONFIG_IA32_EMULATION) += tls.o | 51 | obj-$(CONFIG_IA32_EMULATION) += tls.o |
| 54 | obj-y += step.o | 52 | obj-y += step.o |
diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 1366c7cfd483..f5e5390d3459 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c | |||
| @@ -12,7 +12,6 @@ | |||
| 12 | #include <asm/processor.h> | 12 | #include <asm/processor.h> |
| 13 | #include <asm/pgtable.h> | 13 | #include <asm/pgtable.h> |
| 14 | #include <asm/msr.h> | 14 | #include <asm/msr.h> |
| 15 | #include <asm/ds.h> | ||
| 16 | #include <asm/bugs.h> | 15 | #include <asm/bugs.h> |
| 17 | #include <asm/cpu.h> | 16 | #include <asm/cpu.h> |
| 18 | 17 | ||
| @@ -388,7 +387,6 @@ static void __cpuinit init_intel(struct cpuinfo_x86 *c) | |||
| 388 | set_cpu_cap(c, X86_FEATURE_BTS); | 387 | set_cpu_cap(c, X86_FEATURE_BTS); |
| 389 | if (!(l1 & (1<<12))) | 388 | if (!(l1 & (1<<12))) |
| 390 | set_cpu_cap(c, X86_FEATURE_PEBS); | 389 | set_cpu_cap(c, X86_FEATURE_PEBS); |
| 391 | ds_init_intel(c); | ||
| 392 | } | 390 | } |
| 393 | 391 | ||
| 394 | if (c->x86 == 6 && c->x86_model == 29 && cpu_has_clflush) | 392 | if (c->x86 == 6 && c->x86_model == 29 && cpu_has_clflush) |
diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c index db5bdc8addf8..fd4db0db3708 100644 --- a/arch/x86/kernel/cpu/perf_event.c +++ b/arch/x86/kernel/cpu/perf_event.c | |||
| @@ -31,46 +31,51 @@ | |||
| 31 | #include <asm/nmi.h> | 31 | #include <asm/nmi.h> |
| 32 | #include <asm/compat.h> | 32 | #include <asm/compat.h> |
| 33 | 33 | ||
| 34 | static u64 perf_event_mask __read_mostly; | 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 | ||
| 35 | 44 | ||
| 36 | /* The maximal number of PEBS events: */ | 45 | /* |
| 37 | #define MAX_PEBS_EVENTS 4 | 46 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context |
| 47 | */ | ||
| 48 | static unsigned long | ||
| 49 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) | ||
| 50 | { | ||
| 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; | ||
| 38 | 57 | ||
| 39 | /* The size of a BTS record in bytes: */ | 58 | do { |
| 40 | #define BTS_RECORD_SIZE 24 | 59 | ret = __get_user_pages_fast(addr, 1, 0, &page); |
| 60 | if (!ret) | ||
| 61 | break; | ||
| 41 | 62 | ||
| 42 | /* The size of a per-cpu BTS buffer in bytes: */ | 63 | offset = addr & (PAGE_SIZE - 1); |
| 43 | #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 2048) | 64 | size = min(PAGE_SIZE - offset, n - len); |
| 44 | 65 | ||
| 45 | /* The BTS overflow threshold in bytes from the end of the buffer: */ | 66 | map = kmap_atomic(page, type); |
| 46 | #define BTS_OVFL_TH (BTS_RECORD_SIZE * 128) | 67 | memcpy(to, map+offset, size); |
| 68 | kunmap_atomic(map, type); | ||
| 69 | put_page(page); | ||
| 47 | 70 | ||
| 71 | len += size; | ||
| 72 | to += size; | ||
| 73 | addr += size; | ||
| 48 | 74 | ||
| 49 | /* | 75 | } while (len < n); |
| 50 | * Bits in the debugctlmsr controlling branch tracing. | ||
| 51 | */ | ||
| 52 | #define X86_DEBUGCTL_TR (1 << 6) | ||
| 53 | #define X86_DEBUGCTL_BTS (1 << 7) | ||
| 54 | #define X86_DEBUGCTL_BTINT (1 << 8) | ||
| 55 | #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9) | ||
| 56 | #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10) | ||
| 57 | 76 | ||
| 58 | /* | 77 | return len; |
| 59 | * A debug store configuration. | 78 | } |
| 60 | * | ||
| 61 | * We only support architectures that use 64bit fields. | ||
| 62 | */ | ||
| 63 | struct debug_store { | ||
| 64 | u64 bts_buffer_base; | ||
| 65 | u64 bts_index; | ||
| 66 | u64 bts_absolute_maximum; | ||
| 67 | u64 bts_interrupt_threshold; | ||
| 68 | u64 pebs_buffer_base; | ||
| 69 | u64 pebs_index; | ||
| 70 | u64 pebs_absolute_maximum; | ||
| 71 | u64 pebs_interrupt_threshold; | ||
| 72 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; | ||
| 73 | }; | ||
| 74 | 79 | ||
| 75 | struct event_constraint { | 80 | struct event_constraint { |
| 76 | union { | 81 | union { |
| @@ -89,18 +94,41 @@ struct amd_nb { | |||
| 89 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; | 94 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; |
| 90 | }; | 95 | }; |
| 91 | 96 | ||
| 97 | #define MAX_LBR_ENTRIES 16 | ||
| 98 | |||
| 92 | struct cpu_hw_events { | 99 | struct cpu_hw_events { |
| 100 | /* | ||
| 101 | * Generic x86 PMC bits | ||
| 102 | */ | ||
| 93 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ | 103 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ |
| 94 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | 104 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
| 95 | unsigned long interrupts; | ||
| 96 | int enabled; | 105 | int enabled; |
| 97 | struct debug_store *ds; | ||
| 98 | 106 | ||
| 99 | int n_events; | 107 | int n_events; |
| 100 | int n_added; | 108 | int n_added; |
| 101 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ | 109 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
| 102 | u64 tags[X86_PMC_IDX_MAX]; | 110 | u64 tags[X86_PMC_IDX_MAX]; |
| 103 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ | 111 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
| 112 | |||
| 113 | unsigned int group_flag; | ||
| 114 | |||
| 115 | /* | ||
| 116 | * Intel DebugStore bits | ||
| 117 | */ | ||
| 118 | struct debug_store *ds; | ||
| 119 | u64 pebs_enabled; | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Intel LBR bits | ||
| 123 | */ | ||
| 124 | int lbr_users; | ||
| 125 | void *lbr_context; | ||
| 126 | struct perf_branch_stack lbr_stack; | ||
| 127 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; | ||
| 128 | |||
| 129 | /* | ||
| 130 | * AMD specific bits | ||
| 131 | */ | ||
| 104 | struct amd_nb *amd_nb; | 132 | struct amd_nb *amd_nb; |
| 105 | }; | 133 | }; |
| 106 | 134 | ||
| @@ -114,44 +142,75 @@ struct cpu_hw_events { | |||
| 114 | #define EVENT_CONSTRAINT(c, n, m) \ | 142 | #define EVENT_CONSTRAINT(c, n, m) \ |
| 115 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) | 143 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) |
| 116 | 144 | ||
| 145 | /* | ||
| 146 | * Constraint on the Event code. | ||
| 147 | */ | ||
| 117 | #define INTEL_EVENT_CONSTRAINT(c, n) \ | 148 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
| 118 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK) | 149 | EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) |
| 119 | 150 | ||
| 151 | /* | ||
| 152 | * Constraint on the Event code + UMask + fixed-mask | ||
| 153 | * | ||
| 154 | * filter mask to validate fixed counter events. | ||
| 155 | * the following filters disqualify for fixed counters: | ||
| 156 | * - inv | ||
| 157 | * - edge | ||
| 158 | * - cnt-mask | ||
| 159 | * The other filters are supported by fixed counters. | ||
| 160 | * The any-thread option is supported starting with v3. | ||
| 161 | */ | ||
| 120 | #define FIXED_EVENT_CONSTRAINT(c, n) \ | 162 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
| 121 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), INTEL_ARCH_FIXED_MASK) | 163 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK) |
| 164 | |||
| 165 | /* | ||
| 166 | * Constraint on the Event code + UMask | ||
| 167 | */ | ||
| 168 | #define PEBS_EVENT_CONSTRAINT(c, n) \ | ||
| 169 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) | ||
| 122 | 170 | ||
| 123 | #define EVENT_CONSTRAINT_END \ | 171 | #define EVENT_CONSTRAINT_END \ |
| 124 | EVENT_CONSTRAINT(0, 0, 0) | 172 | EVENT_CONSTRAINT(0, 0, 0) |
| 125 | 173 | ||
| 126 | #define for_each_event_constraint(e, c) \ | 174 | #define for_each_event_constraint(e, c) \ |
| 127 | for ((e) = (c); (e)->cmask; (e)++) | 175 | for ((e) = (c); (e)->weight; (e)++) |
| 176 | |||
| 177 | union perf_capabilities { | ||
| 178 | struct { | ||
| 179 | u64 lbr_format : 6; | ||
| 180 | u64 pebs_trap : 1; | ||
| 181 | u64 pebs_arch_reg : 1; | ||
| 182 | u64 pebs_format : 4; | ||
| 183 | u64 smm_freeze : 1; | ||
| 184 | }; | ||
| 185 | u64 capabilities; | ||
| 186 | }; | ||
| 128 | 187 | ||
| 129 | /* | 188 | /* |
| 130 | * struct x86_pmu - generic x86 pmu | 189 | * struct x86_pmu - generic x86 pmu |
| 131 | */ | 190 | */ |
| 132 | struct x86_pmu { | 191 | struct x86_pmu { |
| 192 | /* | ||
| 193 | * Generic x86 PMC bits | ||
| 194 | */ | ||
| 133 | const char *name; | 195 | const char *name; |
| 134 | int version; | 196 | int version; |
| 135 | int (*handle_irq)(struct pt_regs *); | 197 | int (*handle_irq)(struct pt_regs *); |
| 136 | void (*disable_all)(void); | 198 | void (*disable_all)(void); |
| 137 | void (*enable_all)(void); | 199 | void (*enable_all)(int added); |
| 138 | void (*enable)(struct perf_event *); | 200 | void (*enable)(struct perf_event *); |
| 139 | void (*disable)(struct perf_event *); | 201 | void (*disable)(struct perf_event *); |
| 202 | int (*hw_config)(struct perf_event *event); | ||
| 203 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); | ||
| 140 | unsigned eventsel; | 204 | unsigned eventsel; |
| 141 | unsigned perfctr; | 205 | unsigned perfctr; |
| 142 | u64 (*event_map)(int); | 206 | u64 (*event_map)(int); |
| 143 | u64 (*raw_event)(u64); | ||
| 144 | int max_events; | 207 | int max_events; |
| 145 | int num_events; | 208 | int num_counters; |
| 146 | int num_events_fixed; | 209 | int num_counters_fixed; |
| 147 | int event_bits; | 210 | int cntval_bits; |
| 148 | u64 event_mask; | 211 | u64 cntval_mask; |
| 149 | int apic; | 212 | int apic; |
| 150 | u64 max_period; | 213 | u64 max_period; |
| 151 | u64 intel_ctrl; | ||
| 152 | void (*enable_bts)(u64 config); | ||
| 153 | void (*disable_bts)(void); | ||
| 154 | |||
| 155 | struct event_constraint * | 214 | struct event_constraint * |
| 156 | (*get_event_constraints)(struct cpu_hw_events *cpuc, | 215 | (*get_event_constraints)(struct cpu_hw_events *cpuc, |
| 157 | struct perf_event *event); | 216 | struct perf_event *event); |
| @@ -159,11 +218,32 @@ struct x86_pmu { | |||
| 159 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, | 218 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
| 160 | struct perf_event *event); | 219 | struct perf_event *event); |
| 161 | struct event_constraint *event_constraints; | 220 | struct event_constraint *event_constraints; |
| 221 | void (*quirks)(void); | ||
| 162 | 222 | ||
| 163 | int (*cpu_prepare)(int cpu); | 223 | int (*cpu_prepare)(int cpu); |
| 164 | void (*cpu_starting)(int cpu); | 224 | void (*cpu_starting)(int cpu); |
| 165 | void (*cpu_dying)(int cpu); | 225 | void (*cpu_dying)(int cpu); |
| 166 | void (*cpu_dead)(int cpu); | 226 | void (*cpu_dead)(int cpu); |
| 227 | |||
| 228 | /* | ||
| 229 | * Intel Arch Perfmon v2+ | ||
| 230 | */ | ||
| 231 | u64 intel_ctrl; | ||
| 232 | union perf_capabilities intel_cap; | ||
| 233 | |||
| 234 | /* | ||
| 235 | * Intel DebugStore bits | ||
| 236 | */ | ||
| 237 | int bts, pebs; | ||
| 238 | int pebs_record_size; | ||
| 239 | void (*drain_pebs)(struct pt_regs *regs); | ||
| 240 | struct event_constraint *pebs_constraints; | ||
| 241 | |||
| 242 | /* | ||
| 243 | * Intel LBR | ||
| 244 | */ | ||
| 245 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ | ||
| 246 | int lbr_nr; /* hardware stack size */ | ||
| 167 | }; | 247 | }; |
| 168 | 248 | ||
| 169 | static struct x86_pmu x86_pmu __read_mostly; | 249 | static struct x86_pmu x86_pmu __read_mostly; |
| @@ -198,7 +278,7 @@ static u64 | |||
| 198 | x86_perf_event_update(struct perf_event *event) | 278 | x86_perf_event_update(struct perf_event *event) |
| 199 | { | 279 | { |
| 200 | struct hw_perf_event *hwc = &event->hw; | 280 | struct hw_perf_event *hwc = &event->hw; |
| 201 | int shift = 64 - x86_pmu.event_bits; | 281 | int shift = 64 - x86_pmu.cntval_bits; |
| 202 | u64 prev_raw_count, new_raw_count; | 282 | u64 prev_raw_count, new_raw_count; |
| 203 | int idx = hwc->idx; | 283 | int idx = hwc->idx; |
| 204 | s64 delta; | 284 | s64 delta; |
| @@ -241,33 +321,32 @@ again: | |||
| 241 | static atomic_t active_events; | 321 | static atomic_t active_events; |
| 242 | static DEFINE_MUTEX(pmc_reserve_mutex); | 322 | static DEFINE_MUTEX(pmc_reserve_mutex); |
| 243 | 323 | ||
| 324 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 325 | |||
| 244 | static bool reserve_pmc_hardware(void) | 326 | static bool reserve_pmc_hardware(void) |
| 245 | { | 327 | { |
| 246 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 247 | int i; | 328 | int i; |
| 248 | 329 | ||
| 249 | if (nmi_watchdog == NMI_LOCAL_APIC) | 330 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 250 | disable_lapic_nmi_watchdog(); | 331 | disable_lapic_nmi_watchdog(); |
| 251 | 332 | ||
| 252 | for (i = 0; i < x86_pmu.num_events; i++) { | 333 | for (i = 0; i < x86_pmu.num_counters; i++) { |
| 253 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) | 334 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
| 254 | goto perfctr_fail; | 335 | goto perfctr_fail; |
| 255 | } | 336 | } |
| 256 | 337 | ||
| 257 | for (i = 0; i < x86_pmu.num_events; i++) { | 338 | for (i = 0; i < x86_pmu.num_counters; i++) { |
| 258 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) | 339 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
| 259 | goto eventsel_fail; | 340 | goto eventsel_fail; |
| 260 | } | 341 | } |
| 261 | #endif | ||
| 262 | 342 | ||
| 263 | return true; | 343 | return true; |
| 264 | 344 | ||
| 265 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 266 | eventsel_fail: | 345 | eventsel_fail: |
| 267 | for (i--; i >= 0; i--) | 346 | for (i--; i >= 0; i--) |
| 268 | release_evntsel_nmi(x86_pmu.eventsel + i); | 347 | release_evntsel_nmi(x86_pmu.eventsel + i); |
| 269 | 348 | ||
| 270 | i = x86_pmu.num_events; | 349 | i = x86_pmu.num_counters; |
| 271 | 350 | ||
| 272 | perfctr_fail: | 351 | perfctr_fail: |
| 273 | for (i--; i >= 0; i--) | 352 | for (i--; i >= 0; i--) |
| @@ -277,128 +356,36 @@ perfctr_fail: | |||
| 277 | enable_lapic_nmi_watchdog(); | 356 | enable_lapic_nmi_watchdog(); |
| 278 | 357 | ||
| 279 | return false; | 358 | return false; |
| 280 | #endif | ||
| 281 | } | 359 | } |
| 282 | 360 | ||
| 283 | static void release_pmc_hardware(void) | 361 | static void release_pmc_hardware(void) |
| 284 | { | 362 | { |
| 285 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 286 | int i; | 363 | int i; |
| 287 | 364 | ||
| 288 | for (i = 0; i < x86_pmu.num_events; i++) { | 365 | for (i = 0; i < x86_pmu.num_counters; i++) { |
| 289 | release_perfctr_nmi(x86_pmu.perfctr + i); | 366 | release_perfctr_nmi(x86_pmu.perfctr + i); |
| 290 | release_evntsel_nmi(x86_pmu.eventsel + i); | 367 | release_evntsel_nmi(x86_pmu.eventsel + i); |
| 291 | } | 368 | } |
| 292 | 369 | ||
| 293 | if (nmi_watchdog == NMI_LOCAL_APIC) | 370 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 294 | enable_lapic_nmi_watchdog(); | 371 | enable_lapic_nmi_watchdog(); |
| 295 | #endif | ||
| 296 | } | ||
| 297 | |||
| 298 | static inline bool bts_available(void) | ||
| 299 | { | ||
| 300 | return x86_pmu.enable_bts != NULL; | ||
| 301 | } | 372 | } |
| 302 | 373 | ||
| 303 | static void init_debug_store_on_cpu(int cpu) | 374 | #else |
| 304 | { | ||
| 305 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; | ||
| 306 | |||
| 307 | if (!ds) | ||
| 308 | return; | ||
| 309 | |||
| 310 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, | ||
| 311 | (u32)((u64)(unsigned long)ds), | ||
| 312 | (u32)((u64)(unsigned long)ds >> 32)); | ||
| 313 | } | ||
| 314 | |||
| 315 | static void fini_debug_store_on_cpu(int cpu) | ||
| 316 | { | ||
| 317 | if (!per_cpu(cpu_hw_events, cpu).ds) | ||
| 318 | return; | ||
| 319 | |||
| 320 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); | ||
| 321 | } | ||
| 322 | |||
| 323 | static void release_bts_hardware(void) | ||
| 324 | { | ||
| 325 | int cpu; | ||
| 326 | |||
| 327 | if (!bts_available()) | ||
| 328 | return; | ||
| 329 | |||
| 330 | get_online_cpus(); | ||
| 331 | |||
| 332 | for_each_online_cpu(cpu) | ||
| 333 | fini_debug_store_on_cpu(cpu); | ||
| 334 | |||
| 335 | for_each_possible_cpu(cpu) { | ||
| 336 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; | ||
| 337 | |||
| 338 | if (!ds) | ||
| 339 | continue; | ||
| 340 | |||
| 341 | per_cpu(cpu_hw_events, cpu).ds = NULL; | ||
| 342 | |||
| 343 | kfree((void *)(unsigned long)ds->bts_buffer_base); | ||
| 344 | kfree(ds); | ||
| 345 | } | ||
| 346 | |||
| 347 | put_online_cpus(); | ||
| 348 | } | ||
| 349 | |||
| 350 | static int reserve_bts_hardware(void) | ||
| 351 | { | ||
| 352 | int cpu, err = 0; | ||
| 353 | |||
| 354 | if (!bts_available()) | ||
| 355 | return 0; | ||
| 356 | |||
| 357 | get_online_cpus(); | ||
| 358 | |||
| 359 | for_each_possible_cpu(cpu) { | ||
| 360 | struct debug_store *ds; | ||
| 361 | void *buffer; | ||
| 362 | |||
| 363 | err = -ENOMEM; | ||
| 364 | buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL); | ||
| 365 | if (unlikely(!buffer)) | ||
| 366 | break; | ||
| 367 | |||
| 368 | ds = kzalloc(sizeof(*ds), GFP_KERNEL); | ||
| 369 | if (unlikely(!ds)) { | ||
| 370 | kfree(buffer); | ||
| 371 | break; | ||
| 372 | } | ||
| 373 | |||
| 374 | ds->bts_buffer_base = (u64)(unsigned long)buffer; | ||
| 375 | ds->bts_index = ds->bts_buffer_base; | ||
| 376 | ds->bts_absolute_maximum = | ||
| 377 | ds->bts_buffer_base + BTS_BUFFER_SIZE; | ||
| 378 | ds->bts_interrupt_threshold = | ||
| 379 | ds->bts_absolute_maximum - BTS_OVFL_TH; | ||
| 380 | |||
| 381 | per_cpu(cpu_hw_events, cpu).ds = ds; | ||
| 382 | err = 0; | ||
| 383 | } | ||
| 384 | 375 | ||
| 385 | if (err) | 376 | static bool reserve_pmc_hardware(void) { return true; } |
| 386 | release_bts_hardware(); | 377 | static void release_pmc_hardware(void) {} |
| 387 | else { | ||
| 388 | for_each_online_cpu(cpu) | ||
| 389 | init_debug_store_on_cpu(cpu); | ||
| 390 | } | ||
| 391 | 378 | ||
| 392 | put_online_cpus(); | 379 | #endif |
| 393 | 380 | ||
| 394 | return err; | 381 | static int reserve_ds_buffers(void); |
| 395 | } | 382 | static void release_ds_buffers(void); |
| 396 | 383 | ||
| 397 | static void hw_perf_event_destroy(struct perf_event *event) | 384 | static void hw_perf_event_destroy(struct perf_event *event) |
| 398 | { | 385 | { |
| 399 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { | 386 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
| 400 | release_pmc_hardware(); | 387 | release_pmc_hardware(); |
| 401 | release_bts_hardware(); | 388 | release_ds_buffers(); |
| 402 | mutex_unlock(&pmc_reserve_mutex); | 389 | mutex_unlock(&pmc_reserve_mutex); |
| 403 | } | 390 | } |
| 404 | } | 391 | } |
| @@ -441,54 +428,11 @@ set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr) | |||
| 441 | return 0; | 428 | return 0; |
| 442 | } | 429 | } |
| 443 | 430 | ||
| 444 | /* | 431 | static int x86_setup_perfctr(struct perf_event *event) |
| 445 | * Setup the hardware configuration for a given attr_type | ||
| 446 | */ | ||
| 447 | static int __hw_perf_event_init(struct perf_event *event) | ||
| 448 | { | 432 | { |
| 449 | struct perf_event_attr *attr = &event->attr; | 433 | struct perf_event_attr *attr = &event->attr; |
| 450 | struct hw_perf_event *hwc = &event->hw; | 434 | struct hw_perf_event *hwc = &event->hw; |
| 451 | u64 config; | 435 | u64 config; |
| 452 | int err; | ||
| 453 | |||
| 454 | if (!x86_pmu_initialized()) | ||
| 455 | return -ENODEV; | ||
| 456 | |||
| 457 | err = 0; | ||
| 458 | if (!atomic_inc_not_zero(&active_events)) { | ||
| 459 | mutex_lock(&pmc_reserve_mutex); | ||
| 460 | if (atomic_read(&active_events) == 0) { | ||
| 461 | if (!reserve_pmc_hardware()) | ||
| 462 | err = -EBUSY; | ||
| 463 | else | ||
| 464 | err = reserve_bts_hardware(); | ||
| 465 | } | ||
| 466 | if (!err) | ||
| 467 | atomic_inc(&active_events); | ||
| 468 | mutex_unlock(&pmc_reserve_mutex); | ||
| 469 | } | ||
| 470 | if (err) | ||
| 471 | return err; | ||
| 472 | |||
| 473 | event->destroy = hw_perf_event_destroy; | ||
| 474 | |||
| 475 | /* | ||
| 476 | * Generate PMC IRQs: | ||
| 477 | * (keep 'enabled' bit clear for now) | ||
| 478 | */ | ||
| 479 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; | ||
| 480 | |||
| 481 | hwc->idx = -1; | ||
| 482 | hwc->last_cpu = -1; | ||
| 483 | hwc->last_tag = ~0ULL; | ||
| 484 | |||
| 485 | /* | ||
| 486 | * Count user and OS events unless requested not to. | ||
| 487 | */ | ||
| 488 | if (!attr->exclude_user) | ||
| 489 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; | ||
| 490 | if (!attr->exclude_kernel) | ||
| 491 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; | ||
| 492 | 436 | ||
| 493 | if (!hwc->sample_period) { | 437 | if (!hwc->sample_period) { |
| 494 | hwc->sample_period = x86_pmu.max_period; | 438 | hwc->sample_period = x86_pmu.max_period; |
| @@ -505,16 +449,8 @@ static int __hw_perf_event_init(struct perf_event *event) | |||
| 505 | return -EOPNOTSUPP; | 449 | return -EOPNOTSUPP; |
| 506 | } | 450 | } |
| 507 | 451 | ||
| 508 | /* | 452 | if (attr->type == PERF_TYPE_RAW) |
| 509 | * Raw hw_event type provide the config in the hw_event structure | ||
| 510 | */ | ||
| 511 | if (attr->type == PERF_TYPE_RAW) { | ||
| 512 | hwc->config |= x86_pmu.raw_event(attr->config); | ||
| 513 | if ((hwc->config & ARCH_PERFMON_EVENTSEL_ANY) && | ||
| 514 | perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) | ||
| 515 | return -EACCES; | ||
| 516 | return 0; | 453 | return 0; |
| 517 | } | ||
| 518 | 454 | ||
| 519 | if (attr->type == PERF_TYPE_HW_CACHE) | 455 | if (attr->type == PERF_TYPE_HW_CACHE) |
| 520 | return set_ext_hw_attr(hwc, attr); | 456 | return set_ext_hw_attr(hwc, attr); |
| @@ -539,11 +475,11 @@ static int __hw_perf_event_init(struct perf_event *event) | |||
| 539 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && | 475 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
| 540 | (hwc->sample_period == 1)) { | 476 | (hwc->sample_period == 1)) { |
| 541 | /* BTS is not supported by this architecture. */ | 477 | /* BTS is not supported by this architecture. */ |
| 542 | if (!bts_available()) | 478 | if (!x86_pmu.bts) |
| 543 | return -EOPNOTSUPP; | 479 | return -EOPNOTSUPP; |
| 544 | 480 | ||
| 545 | /* BTS is currently only allowed for user-mode. */ | 481 | /* BTS is currently only allowed for user-mode. */ |
| 546 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) | 482 | if (!attr->exclude_kernel) |
| 547 | return -EOPNOTSUPP; | 483 | return -EOPNOTSUPP; |
| 548 | } | 484 | } |
| 549 | 485 | ||
| @@ -552,12 +488,87 @@ static int __hw_perf_event_init(struct perf_event *event) | |||
| 552 | return 0; | 488 | return 0; |
| 553 | } | 489 | } |
| 554 | 490 | ||
| 491 | static int x86_pmu_hw_config(struct perf_event *event) | ||
| 492 | { | ||
| 493 | if (event->attr.precise_ip) { | ||
| 494 | int precise = 0; | ||
| 495 | |||
| 496 | /* Support for constant skid */ | ||
| 497 | if (x86_pmu.pebs) | ||
| 498 | precise++; | ||
| 499 | |||
| 500 | /* Support for IP fixup */ | ||
| 501 | if (x86_pmu.lbr_nr) | ||
| 502 | precise++; | ||
| 503 | |||
| 504 | if (event->attr.precise_ip > precise) | ||
| 505 | return -EOPNOTSUPP; | ||
| 506 | } | ||
| 507 | |||
| 508 | /* | ||
| 509 | * Generate PMC IRQs: | ||
| 510 | * (keep 'enabled' bit clear for now) | ||
| 511 | */ | ||
| 512 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; | ||
| 513 | |||
| 514 | /* | ||
| 515 | * Count user and OS events unless requested not to | ||
| 516 | */ | ||
| 517 | if (!event->attr.exclude_user) | ||
| 518 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; | ||
| 519 | if (!event->attr.exclude_kernel) | ||
| 520 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; | ||
| 521 | |||
| 522 | if (event->attr.type == PERF_TYPE_RAW) | ||
| 523 | event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; | ||
| 524 | |||
| 525 | return x86_setup_perfctr(event); | ||
| 526 | } | ||
| 527 | |||
| 528 | /* | ||
| 529 | * Setup the hardware configuration for a given attr_type | ||
| 530 | */ | ||
| 531 | static int __hw_perf_event_init(struct perf_event *event) | ||
| 532 | { | ||
| 533 | int err; | ||
| 534 | |||
| 535 | if (!x86_pmu_initialized()) | ||
| 536 | return -ENODEV; | ||
| 537 | |||
| 538 | err = 0; | ||
| 539 | if (!atomic_inc_not_zero(&active_events)) { | ||
| 540 | mutex_lock(&pmc_reserve_mutex); | ||
| 541 | if (atomic_read(&active_events) == 0) { | ||
| 542 | if (!reserve_pmc_hardware()) | ||
| 543 | err = -EBUSY; | ||
| 544 | else { | ||
| 545 | err = reserve_ds_buffers(); | ||
| 546 | if (err) | ||
| 547 | release_pmc_hardware(); | ||
| 548 | } | ||
| 549 | } | ||
| 550 | if (!err) | ||
| 551 | atomic_inc(&active_events); | ||
| 552 | mutex_unlock(&pmc_reserve_mutex); | ||
| 553 | } | ||
| 554 | if (err) | ||
| 555 | return err; | ||
| 556 | |||
| 557 | event->destroy = hw_perf_event_destroy; | ||
| 558 | |||
| 559 | event->hw.idx = -1; | ||
| 560 | event->hw.last_cpu = -1; | ||
| 561 | event->hw.last_tag = ~0ULL; | ||
| 562 | |||
| 563 | return x86_pmu.hw_config(event); | ||
| 564 | } | ||
| 565 | |||
| 555 | static void x86_pmu_disable_all(void) | 566 | static void x86_pmu_disable_all(void) |
| 556 | { | 567 | { |
| 557 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 568 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 558 | int idx; | 569 | int idx; |
| 559 | 570 | ||
| 560 | for (idx = 0; idx < x86_pmu.num_events; idx++) { | 571 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
| 561 | u64 val; | 572 | u64 val; |
| 562 | 573 | ||
| 563 | if (!test_bit(idx, cpuc->active_mask)) | 574 | if (!test_bit(idx, cpuc->active_mask)) |
| @@ -587,12 +598,12 @@ void hw_perf_disable(void) | |||
| 587 | x86_pmu.disable_all(); | 598 | x86_pmu.disable_all(); |
| 588 | } | 599 | } |
| 589 | 600 | ||
| 590 | static void x86_pmu_enable_all(void) | 601 | static void x86_pmu_enable_all(int added) |
| 591 | { | 602 | { |
| 592 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 603 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 593 | int idx; | 604 | int idx; |
| 594 | 605 | ||
| 595 | for (idx = 0; idx < x86_pmu.num_events; idx++) { | 606 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
| 596 | struct perf_event *event = cpuc->events[idx]; | 607 | struct perf_event *event = cpuc->events[idx]; |
| 597 | u64 val; | 608 | u64 val; |
| 598 | 609 | ||
| @@ -667,14 +678,14 @@ static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) | |||
| 667 | * assign events to counters starting with most | 678 | * assign events to counters starting with most |
| 668 | * constrained events. | 679 | * constrained events. |
| 669 | */ | 680 | */ |
| 670 | wmax = x86_pmu.num_events; | 681 | wmax = x86_pmu.num_counters; |
| 671 | 682 | ||
| 672 | /* | 683 | /* |
| 673 | * when fixed event counters are present, | 684 | * when fixed event counters are present, |
| 674 | * wmax is incremented by 1 to account | 685 | * wmax is incremented by 1 to account |
| 675 | * for one more choice | 686 | * for one more choice |
| 676 | */ | 687 | */ |
| 677 | if (x86_pmu.num_events_fixed) | 688 | if (x86_pmu.num_counters_fixed) |
| 678 | wmax++; | 689 | wmax++; |
| 679 | 690 | ||
| 680 | for (w = 1, num = n; num && w <= wmax; w++) { | 691 | for (w = 1, num = n; num && w <= wmax; w++) { |
| @@ -724,7 +735,7 @@ static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, | |||
| 724 | struct perf_event *event; | 735 | struct perf_event *event; |
| 725 | int n, max_count; | 736 | int n, max_count; |
| 726 | 737 | ||
| 727 | max_count = x86_pmu.num_events + x86_pmu.num_events_fixed; | 738 | max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed; |
| 728 | 739 | ||
| 729 | /* current number of events already accepted */ | 740 | /* current number of events already accepted */ |
| 730 | n = cpuc->n_events; | 741 | n = cpuc->n_events; |
| @@ -795,7 +806,7 @@ void hw_perf_enable(void) | |||
| 795 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 806 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 796 | struct perf_event *event; | 807 | struct perf_event *event; |
| 797 | struct hw_perf_event *hwc; | 808 | struct hw_perf_event *hwc; |
| 798 | int i; | 809 | int i, added = cpuc->n_added; |
| 799 | 810 | ||
| 800 | if (!x86_pmu_initialized()) | 811 | if (!x86_pmu_initialized()) |
| 801 | return; | 812 | return; |
| @@ -847,19 +858,20 @@ void hw_perf_enable(void) | |||
| 847 | cpuc->enabled = 1; | 858 | cpuc->enabled = 1; |
| 848 | barrier(); | 859 | barrier(); |
| 849 | 860 | ||
| 850 | x86_pmu.enable_all(); | 861 | x86_pmu.enable_all(added); |
| 851 | } | 862 | } |
| 852 | 863 | ||
| 853 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc) | 864 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, |
| 865 | u64 enable_mask) | ||
| 854 | { | 866 | { |
| 855 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, | 867 | wrmsrl(hwc->config_base + hwc->idx, hwc->config | enable_mask); |
| 856 | hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE); | ||
| 857 | } | 868 | } |
| 858 | 869 | ||
| 859 | static inline void x86_pmu_disable_event(struct perf_event *event) | 870 | static inline void x86_pmu_disable_event(struct perf_event *event) |
| 860 | { | 871 | { |
| 861 | struct hw_perf_event *hwc = &event->hw; | 872 | struct hw_perf_event *hwc = &event->hw; |
| 862 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, hwc->config); | 873 | |
| 874 | wrmsrl(hwc->config_base + hwc->idx, hwc->config); | ||
| 863 | } | 875 | } |
| 864 | 876 | ||
| 865 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); | 877 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
| @@ -874,7 +886,7 @@ x86_perf_event_set_period(struct perf_event *event) | |||
| 874 | struct hw_perf_event *hwc = &event->hw; | 886 | struct hw_perf_event *hwc = &event->hw; |
| 875 | s64 left = atomic64_read(&hwc->period_left); | 887 | s64 left = atomic64_read(&hwc->period_left); |
| 876 | s64 period = hwc->sample_period; | 888 | s64 period = hwc->sample_period; |
| 877 | int err, ret = 0, idx = hwc->idx; | 889 | int ret = 0, idx = hwc->idx; |
| 878 | 890 | ||
| 879 | if (idx == X86_PMC_IDX_FIXED_BTS) | 891 | if (idx == X86_PMC_IDX_FIXED_BTS) |
| 880 | return 0; | 892 | return 0; |
| @@ -912,8 +924,8 @@ x86_perf_event_set_period(struct perf_event *event) | |||
| 912 | */ | 924 | */ |
| 913 | atomic64_set(&hwc->prev_count, (u64)-left); | 925 | atomic64_set(&hwc->prev_count, (u64)-left); |
| 914 | 926 | ||
| 915 | err = checking_wrmsrl(hwc->event_base + idx, | 927 | wrmsrl(hwc->event_base + idx, |
| 916 | (u64)(-left) & x86_pmu.event_mask); | 928 | (u64)(-left) & x86_pmu.cntval_mask); |
| 917 | 929 | ||
| 918 | perf_event_update_userpage(event); | 930 | perf_event_update_userpage(event); |
| 919 | 931 | ||
| @@ -924,7 +936,8 @@ static void x86_pmu_enable_event(struct perf_event *event) | |||
| 924 | { | 936 | { |
| 925 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 937 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 926 | if (cpuc->enabled) | 938 | if (cpuc->enabled) |
| 927 | __x86_pmu_enable_event(&event->hw); | 939 | __x86_pmu_enable_event(&event->hw, |
| 940 | ARCH_PERFMON_EVENTSEL_ENABLE); | ||
| 928 | } | 941 | } |
| 929 | 942 | ||
| 930 | /* | 943 | /* |
| @@ -950,7 +963,15 @@ static int x86_pmu_enable(struct perf_event *event) | |||
| 950 | if (n < 0) | 963 | if (n < 0) |
| 951 | return n; | 964 | return n; |
| 952 | 965 | ||
| 953 | ret = x86_schedule_events(cpuc, n, assign); | 966 | /* |
| 967 | * If group events scheduling transaction was started, | ||
| 968 | * skip the schedulability test here, it will be peformed | ||
| 969 | * at commit time(->commit_txn) as a whole | ||
| 970 | */ | ||
| 971 | if (cpuc->group_flag & PERF_EVENT_TXN_STARTED) | ||
| 972 | goto out; | ||
| 973 | |||
| 974 | ret = x86_pmu.schedule_events(cpuc, n, assign); | ||
| 954 | if (ret) | 975 | if (ret) |
| 955 | return ret; | 976 | return ret; |
| 956 | /* | 977 | /* |
| @@ -959,6 +980,7 @@ static int x86_pmu_enable(struct perf_event *event) | |||
| 959 | */ | 980 | */ |
| 960 | memcpy(cpuc->assign, assign, n*sizeof(int)); | 981 | memcpy(cpuc->assign, assign, n*sizeof(int)); |
| 961 | 982 | ||
| 983 | out: | ||
| 962 | cpuc->n_events = n; | 984 | cpuc->n_events = n; |
| 963 | cpuc->n_added += n - n0; | 985 | cpuc->n_added += n - n0; |
| 964 | 986 | ||
| @@ -991,11 +1013,12 @@ static void x86_pmu_unthrottle(struct perf_event *event) | |||
| 991 | void perf_event_print_debug(void) | 1013 | void perf_event_print_debug(void) |
| 992 | { | 1014 | { |
| 993 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; | 1015 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
| 1016 | u64 pebs; | ||
| 994 | struct cpu_hw_events *cpuc; | 1017 | struct cpu_hw_events *cpuc; |
| 995 | unsigned long flags; | 1018 | unsigned long flags; |
| 996 | int cpu, idx; | 1019 | int cpu, idx; |
| 997 | 1020 | ||
| 998 | if (!x86_pmu.num_events) | 1021 | if (!x86_pmu.num_counters) |
| 999 | return; | 1022 | return; |
| 1000 | 1023 | ||
| 1001 | local_irq_save(flags); | 1024 | local_irq_save(flags); |
| @@ -1008,16 +1031,18 @@ void perf_event_print_debug(void) | |||
| 1008 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); | 1031 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
| 1009 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | 1032 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); |
| 1010 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | 1033 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); |
| 1034 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); | ||
| 1011 | 1035 | ||
| 1012 | pr_info("\n"); | 1036 | pr_info("\n"); |
| 1013 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | 1037 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); |
| 1014 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | 1038 | pr_info("CPU#%d: status: %016llx\n", cpu, status); |
| 1015 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | 1039 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); |
| 1016 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | 1040 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); |
| 1041 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); | ||
| 1017 | } | 1042 | } |
| 1018 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); | 1043 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
| 1019 | 1044 | ||
| 1020 | for (idx = 0; idx < x86_pmu.num_events; idx++) { | 1045 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
| 1021 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); | 1046 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
| 1022 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); | 1047 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); |
| 1023 | 1048 | ||
| @@ -1030,7 +1055,7 @@ void perf_event_print_debug(void) | |||
| 1030 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", | 1055 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
| 1031 | cpu, idx, prev_left); | 1056 | cpu, idx, prev_left); |
| 1032 | } | 1057 | } |
| 1033 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { | 1058 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
| 1034 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); | 1059 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
| 1035 | 1060 | ||
| 1036 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", | 1061 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
| @@ -1095,7 +1120,7 @@ static int x86_pmu_handle_irq(struct pt_regs *regs) | |||
| 1095 | 1120 | ||
| 1096 | cpuc = &__get_cpu_var(cpu_hw_events); | 1121 | cpuc = &__get_cpu_var(cpu_hw_events); |
| 1097 | 1122 | ||
| 1098 | for (idx = 0; idx < x86_pmu.num_events; idx++) { | 1123 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
| 1099 | if (!test_bit(idx, cpuc->active_mask)) | 1124 | if (!test_bit(idx, cpuc->active_mask)) |
| 1100 | continue; | 1125 | continue; |
| 1101 | 1126 | ||
| @@ -1103,7 +1128,7 @@ static int x86_pmu_handle_irq(struct pt_regs *regs) | |||
| 1103 | hwc = &event->hw; | 1128 | hwc = &event->hw; |
| 1104 | 1129 | ||
| 1105 | val = x86_perf_event_update(event); | 1130 | val = x86_perf_event_update(event); |
| 1106 | if (val & (1ULL << (x86_pmu.event_bits - 1))) | 1131 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
| 1107 | continue; | 1132 | continue; |
| 1108 | 1133 | ||
| 1109 | /* | 1134 | /* |
| @@ -1146,7 +1171,6 @@ void set_perf_event_pending(void) | |||
| 1146 | 1171 | ||
| 1147 | void perf_events_lapic_init(void) | 1172 | void perf_events_lapic_init(void) |
| 1148 | { | 1173 | { |
| 1149 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 1150 | if (!x86_pmu.apic || !x86_pmu_initialized()) | 1174 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
| 1151 | return; | 1175 | return; |
| 1152 | 1176 | ||
| @@ -1154,7 +1178,6 @@ void perf_events_lapic_init(void) | |||
| 1154 | * Always use NMI for PMU | 1178 | * Always use NMI for PMU |
| 1155 | */ | 1179 | */ |
| 1156 | apic_write(APIC_LVTPC, APIC_DM_NMI); | 1180 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 1157 | #endif | ||
| 1158 | } | 1181 | } |
| 1159 | 1182 | ||
| 1160 | static int __kprobes | 1183 | static int __kprobes |
| @@ -1178,9 +1201,7 @@ perf_event_nmi_handler(struct notifier_block *self, | |||
| 1178 | 1201 | ||
| 1179 | regs = args->regs; | 1202 | regs = args->regs; |
| 1180 | 1203 | ||
| 1181 | #ifdef CONFIG_X86_LOCAL_APIC | ||
| 1182 | apic_write(APIC_LVTPC, APIC_DM_NMI); | 1204 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
| 1183 | #endif | ||
| 1184 | /* | 1205 | /* |
| 1185 | * Can't rely on the handled return value to say it was our NMI, two | 1206 | * Can't rely on the handled return value to say it was our NMI, two |
| 1186 | * events could trigger 'simultaneously' raising two back-to-back NMIs. | 1207 | * events could trigger 'simultaneously' raising two back-to-back NMIs. |
| @@ -1217,118 +1238,11 @@ x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) | |||
| 1217 | return &unconstrained; | 1238 | return &unconstrained; |
| 1218 | } | 1239 | } |
| 1219 | 1240 | ||
| 1220 | static int x86_event_sched_in(struct perf_event *event, | ||
| 1221 | struct perf_cpu_context *cpuctx) | ||
| 1222 | { | ||
| 1223 | int ret = 0; | ||
| 1224 | |||
| 1225 | event->state = PERF_EVENT_STATE_ACTIVE; | ||
| 1226 | event->oncpu = smp_processor_id(); | ||
| 1227 | event->tstamp_running += event->ctx->time - event->tstamp_stopped; | ||
| 1228 | |||
| 1229 | if (!is_x86_event(event)) | ||
| 1230 | ret = event->pmu->enable(event); | ||
| 1231 | |||
| 1232 | if (!ret && !is_software_event(event)) | ||
| 1233 | cpuctx->active_oncpu++; | ||
| 1234 | |||
| 1235 | if (!ret && event->attr.exclusive) | ||
| 1236 | cpuctx->exclusive = 1; | ||
| 1237 | |||
| 1238 | return ret; | ||
| 1239 | } | ||
| 1240 | |||
| 1241 | static void x86_event_sched_out(struct perf_event *event, | ||
| 1242 | struct perf_cpu_context *cpuctx) | ||
| 1243 | { | ||
| 1244 | event->state = PERF_EVENT_STATE_INACTIVE; | ||
| 1245 | event->oncpu = -1; | ||
| 1246 | |||
| 1247 | if (!is_x86_event(event)) | ||
| 1248 | event->pmu->disable(event); | ||
| 1249 | |||
| 1250 | event->tstamp_running -= event->ctx->time - event->tstamp_stopped; | ||
| 1251 | |||
| 1252 | if (!is_software_event(event)) | ||
| 1253 | cpuctx->active_oncpu--; | ||
| 1254 | |||
| 1255 | if (event->attr.exclusive || !cpuctx->active_oncpu) | ||
| 1256 | cpuctx->exclusive = 0; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | /* | ||
| 1260 | * Called to enable a whole group of events. | ||
| 1261 | * Returns 1 if the group was enabled, or -EAGAIN if it could not be. | ||
| 1262 | * Assumes the caller has disabled interrupts and has | ||
| 1263 | * frozen the PMU with hw_perf_save_disable. | ||
| 1264 | * | ||
| 1265 | * called with PMU disabled. If successful and return value 1, | ||
| 1266 | * then guaranteed to call perf_enable() and hw_perf_enable() | ||
| 1267 | */ | ||
| 1268 | int hw_perf_group_sched_in(struct perf_event *leader, | ||
| 1269 | struct perf_cpu_context *cpuctx, | ||
| 1270 | struct perf_event_context *ctx) | ||
| 1271 | { | ||
| 1272 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 1273 | struct perf_event *sub; | ||
| 1274 | int assign[X86_PMC_IDX_MAX]; | ||
| 1275 | int n0, n1, ret; | ||
| 1276 | |||
| 1277 | /* n0 = total number of events */ | ||
| 1278 | n0 = collect_events(cpuc, leader, true); | ||
| 1279 | if (n0 < 0) | ||
| 1280 | return n0; | ||
| 1281 | |||
| 1282 | ret = x86_schedule_events(cpuc, n0, assign); | ||
| 1283 | if (ret) | ||
| 1284 | return ret; | ||
| 1285 | |||
| 1286 | ret = x86_event_sched_in(leader, cpuctx); | ||
| 1287 | if (ret) | ||
| 1288 | return ret; | ||
| 1289 | |||
| 1290 | n1 = 1; | ||
| 1291 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { | ||
| 1292 | if (sub->state > PERF_EVENT_STATE_OFF) { | ||
| 1293 | ret = x86_event_sched_in(sub, cpuctx); | ||
| 1294 | if (ret) | ||
| 1295 | goto undo; | ||
| 1296 | ++n1; | ||
| 1297 | } | ||
| 1298 | } | ||
| 1299 | /* | ||
| 1300 | * copy new assignment, now we know it is possible | ||
| 1301 | * will be used by hw_perf_enable() | ||
| 1302 | */ | ||
| 1303 | memcpy(cpuc->assign, assign, n0*sizeof(int)); | ||
| 1304 | |||
| 1305 | cpuc->n_events = n0; | ||
| 1306 | cpuc->n_added += n1; | ||
| 1307 | ctx->nr_active += n1; | ||
| 1308 | |||
| 1309 | /* | ||
| 1310 | * 1 means successful and events are active | ||
| 1311 | * This is not quite true because we defer | ||
| 1312 | * actual activation until hw_perf_enable() but | ||
| 1313 | * this way we* ensure caller won't try to enable | ||
| 1314 | * individual events | ||
| 1315 | */ | ||
| 1316 | return 1; | ||
| 1317 | undo: | ||
| 1318 | x86_event_sched_out(leader, cpuctx); | ||
| 1319 | n0 = 1; | ||
| 1320 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { | ||
| 1321 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { | ||
| 1322 | x86_event_sched_out(sub, cpuctx); | ||
| 1323 | if (++n0 == n1) | ||
| 1324 | break; | ||
| 1325 | } | ||
| 1326 | } | ||
| 1327 | return ret; | ||
| 1328 | } | ||
| 1329 | |||
| 1330 | #include "perf_event_amd.c" | 1241 | #include "perf_event_amd.c" |
| 1331 | #include "perf_event_p6.c" | 1242 | #include "perf_event_p6.c" |
| 1243 | #include "perf_event_p4.c" | ||
| 1244 | #include "perf_event_intel_lbr.c" | ||
| 1245 | #include "perf_event_intel_ds.c" | ||
| 1332 | #include "perf_event_intel.c" | 1246 | #include "perf_event_intel.c" |
| 1333 | 1247 | ||
| 1334 | static int __cpuinit | 1248 | static int __cpuinit |
| @@ -1402,48 +1316,50 @@ void __init init_hw_perf_events(void) | |||
| 1402 | 1316 | ||
| 1403 | pr_cont("%s PMU driver.\n", x86_pmu.name); | 1317 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
| 1404 | 1318 | ||
| 1405 | if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) { | 1319 | if (x86_pmu.quirks) |
| 1320 | x86_pmu.quirks(); | ||
| 1321 | |||
| 1322 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { | ||
| 1406 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", | 1323 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
| 1407 | x86_pmu.num_events, X86_PMC_MAX_GENERIC); | 1324 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
| 1408 | x86_pmu.num_events = X86_PMC_MAX_GENERIC; | 1325 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; |
| 1409 | } | 1326 | } |
| 1410 | perf_event_mask = (1 << x86_pmu.num_events) - 1; | 1327 | x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1; |
| 1411 | perf_max_events = x86_pmu.num_events; | 1328 | perf_max_events = x86_pmu.num_counters; |
| 1412 | 1329 | ||
| 1413 | if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) { | 1330 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
| 1414 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", | 1331 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
| 1415 | x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED); | 1332 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
| 1416 | x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED; | 1333 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; |
| 1417 | } | 1334 | } |
| 1418 | 1335 | ||
| 1419 | perf_event_mask |= | 1336 | x86_pmu.intel_ctrl |= |
| 1420 | ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED; | 1337 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
| 1421 | x86_pmu.intel_ctrl = perf_event_mask; | ||
| 1422 | 1338 | ||
| 1423 | perf_events_lapic_init(); | 1339 | perf_events_lapic_init(); |
| 1424 | register_die_notifier(&perf_event_nmi_notifier); | 1340 | register_die_notifier(&perf_event_nmi_notifier); |
| 1425 | 1341 | ||
| 1426 | unconstrained = (struct event_constraint) | 1342 | unconstrained = (struct event_constraint) |
| 1427 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1, | 1343 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1, |
| 1428 | 0, x86_pmu.num_events); | 1344 | 0, x86_pmu.num_counters); |
| 1429 | 1345 | ||
| 1430 | if (x86_pmu.event_constraints) { | 1346 | if (x86_pmu.event_constraints) { |
| 1431 | for_each_event_constraint(c, x86_pmu.event_constraints) { | 1347 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
| 1432 | if (c->cmask != INTEL_ARCH_FIXED_MASK) | 1348 | if (c->cmask != X86_RAW_EVENT_MASK) |
| 1433 | continue; | 1349 | continue; |
| 1434 | 1350 | ||
| 1435 | c->idxmsk64 |= (1ULL << x86_pmu.num_events) - 1; | 1351 | c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1; |
| 1436 | c->weight += x86_pmu.num_events; | 1352 | c->weight += x86_pmu.num_counters; |
| 1437 | } | 1353 | } |
| 1438 | } | 1354 | } |
| 1439 | 1355 | ||
| 1440 | pr_info("... version: %d\n", x86_pmu.version); | 1356 | pr_info("... version: %d\n", x86_pmu.version); |
| 1441 | pr_info("... bit width: %d\n", x86_pmu.event_bits); | 1357 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); |
| 1442 | pr_info("... generic registers: %d\n", x86_pmu.num_events); | 1358 | pr_info("... generic registers: %d\n", x86_pmu.num_counters); |
| 1443 | pr_info("... value mask: %016Lx\n", x86_pmu.event_mask); | 1359 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); |
| 1444 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); | 1360 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
| 1445 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed); | 1361 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed); |
| 1446 | pr_info("... event mask: %016Lx\n", perf_event_mask); | 1362 | pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl); |
| 1447 | 1363 | ||
| 1448 | perf_cpu_notifier(x86_pmu_notifier); | 1364 | perf_cpu_notifier(x86_pmu_notifier); |
| 1449 | } | 1365 | } |
| @@ -1453,6 +1369,59 @@ static inline void x86_pmu_read(struct perf_event *event) | |||
| 1453 | x86_perf_event_update(event); | 1369 | x86_perf_event_update(event); |
| 1454 | } | 1370 | } |
| 1455 | 1371 | ||
| 1372 | /* | ||
| 1373 | * Start group events scheduling transaction | ||
| 1374 | * Set the flag to make pmu::enable() not perform the | ||
| 1375 | * schedulability test, it will be performed at commit time | ||
| 1376 | */ | ||
| 1377 | static void x86_pmu_start_txn(const struct pmu *pmu) | ||
| 1378 | { | ||
| 1379 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 1380 | |||
| 1381 | cpuc->group_flag |= PERF_EVENT_TXN_STARTED; | ||
| 1382 | } | ||
| 1383 | |||
| 1384 | /* | ||
| 1385 | * Stop group events scheduling transaction | ||
| 1386 | * Clear the flag and pmu::enable() will perform the | ||
| 1387 | * schedulability test. | ||
| 1388 | */ | ||
| 1389 | static void x86_pmu_cancel_txn(const struct pmu *pmu) | ||
| 1390 | { | ||
| 1391 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 1392 | |||
| 1393 | cpuc->group_flag &= ~PERF_EVENT_TXN_STARTED; | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | /* | ||
| 1397 | * Commit group events scheduling transaction | ||
| 1398 | * Perform the group schedulability test as a whole | ||
| 1399 | * Return 0 if success | ||
| 1400 | */ | ||
| 1401 | static int x86_pmu_commit_txn(const struct pmu *pmu) | ||
| 1402 | { | ||
| 1403 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 1404 | int assign[X86_PMC_IDX_MAX]; | ||
| 1405 | int n, ret; | ||
| 1406 | |||
| 1407 | n = cpuc->n_events; | ||
| 1408 | |||
| 1409 | if (!x86_pmu_initialized()) | ||
| 1410 | return -EAGAIN; | ||
| 1411 | |||
| 1412 | ret = x86_pmu.schedule_events(cpuc, n, assign); | ||
| 1413 | if (ret) | ||
| 1414 | return ret; | ||
| 1415 | |||
| 1416 | /* | ||
| 1417 | * copy new assignment, now we know it is possible | ||
| 1418 | * will be used by hw_perf_enable() | ||
| 1419 | */ | ||
| 1420 | memcpy(cpuc->assign, assign, n*sizeof(int)); | ||
| 1421 | |||
| 1422 | return 0; | ||
| 1423 | } | ||
| 1424 | |||
| 1456 | static const struct pmu pmu = { | 1425 | static const struct pmu pmu = { |
| 1457 | .enable = x86_pmu_enable, | 1426 | .enable = x86_pmu_enable, |
| 1458 | .disable = x86_pmu_disable, | 1427 | .disable = x86_pmu_disable, |
| @@ -1460,9 +1429,38 @@ static const struct pmu pmu = { | |||
| 1460 | .stop = x86_pmu_stop, | 1429 | .stop = x86_pmu_stop, |
| 1461 | .read = x86_pmu_read, | 1430 | .read = x86_pmu_read, |
| 1462 | .unthrottle = x86_pmu_unthrottle, | 1431 | .unthrottle = x86_pmu_unthrottle, |
| 1432 | .start_txn = x86_pmu_start_txn, | ||
| 1433 | .cancel_txn = x86_pmu_cancel_txn, | ||
| 1434 | .commit_txn = x86_pmu_commit_txn, | ||
| 1463 | }; | 1435 | }; |
| 1464 | 1436 | ||
| 1465 | /* | 1437 | /* |
| 1438 | * validate that we can schedule this event | ||
| 1439 | */ | ||
| 1440 | static int validate_event(struct perf_event *event) | ||
| 1441 | { | ||
| 1442 | struct cpu_hw_events *fake_cpuc; | ||
| 1443 | struct event_constraint *c; | ||
| 1444 | int ret = 0; | ||
| 1445 | |||
| 1446 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); | ||
| 1447 | if (!fake_cpuc) | ||
| 1448 | return -ENOMEM; | ||
| 1449 | |||
| 1450 | c = x86_pmu.get_event_constraints(fake_cpuc, event); | ||
| 1451 | |||
| 1452 | if (!c || !c->weight) | ||
| 1453 | ret = -ENOSPC; | ||
| 1454 | |||
| 1455 | if (x86_pmu.put_event_constraints) | ||
| 1456 | x86_pmu.put_event_constraints(fake_cpuc, event); | ||
| 1457 | |||
| 1458 | kfree(fake_cpuc); | ||
| 1459 | |||
| 1460 | return ret; | ||
| 1461 | } | ||
| 1462 | |||
| 1463 | /* | ||
| 1466 | * validate a single event group | 1464 | * validate a single event group |
| 1467 | * | 1465 | * |
| 1468 | * validation include: | 1466 | * validation include: |
| @@ -1502,7 +1500,7 @@ static int validate_group(struct perf_event *event) | |||
| 1502 | 1500 | ||
| 1503 | fake_cpuc->n_events = n; | 1501 | fake_cpuc->n_events = n; |
| 1504 | 1502 | ||
| 1505 | ret = x86_schedule_events(fake_cpuc, n, NULL); | 1503 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
| 1506 | 1504 | ||
| 1507 | out_free: | 1505 | out_free: |
| 1508 | kfree(fake_cpuc); | 1506 | kfree(fake_cpuc); |
| @@ -1527,6 +1525,8 @@ const struct pmu *hw_perf_event_init(struct perf_event *event) | |||
| 1527 | 1525 | ||
| 1528 | if (event->group_leader != event) | 1526 | if (event->group_leader != event) |
| 1529 | err = validate_group(event); | 1527 | err = validate_group(event); |
| 1528 | else | ||
| 1529 | err = validate_event(event); | ||
| 1530 | 1530 | ||
| 1531 | event->pmu = tmp; | 1531 | event->pmu = tmp; |
| 1532 | } | 1532 | } |
| @@ -1574,8 +1574,7 @@ static void backtrace_address(void *data, unsigned long addr, int reliable) | |||
| 1574 | { | 1574 | { |
| 1575 | struct perf_callchain_entry *entry = data; | 1575 | struct perf_callchain_entry *entry = data; |
| 1576 | 1576 | ||
| 1577 | if (reliable) | 1577 | callchain_store(entry, addr); |
| 1578 | callchain_store(entry, addr); | ||
| 1579 | } | 1578 | } |
| 1580 | 1579 | ||
| 1581 | static const struct stacktrace_ops backtrace_ops = { | 1580 | static const struct stacktrace_ops backtrace_ops = { |
| @@ -1597,41 +1596,6 @@ perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) | |||
| 1597 | dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry); | 1596 | dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry); |
| 1598 | } | 1597 | } |
| 1599 | 1598 | ||
| 1600 | /* | ||
| 1601 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context | ||
| 1602 | */ | ||
| 1603 | static unsigned long | ||
| 1604 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) | ||
| 1605 | { | ||
| 1606 | unsigned long offset, addr = (unsigned long)from; | ||
| 1607 | int type = in_nmi() ? KM_NMI : KM_IRQ0; | ||
| 1608 | unsigned long size, len = 0; | ||
| 1609 | struct page *page; | ||
| 1610 | void *map; | ||
| 1611 | int ret; | ||
| 1612 | |||
| 1613 | do { | ||
| 1614 | ret = __get_user_pages_fast(addr, 1, 0, &page); | ||
| 1615 | if (!ret) | ||
| 1616 | break; | ||
| 1617 | |||
| 1618 | offset = addr & (PAGE_SIZE - 1); | ||
| 1619 | size = min(PAGE_SIZE - offset, n - len); | ||
| 1620 | |||
| 1621 | map = kmap_atomic(page, type); | ||
| 1622 | memcpy(to, map+offset, size); | ||
| 1623 | kunmap_atomic(map, type); | ||
| 1624 | put_page(page); | ||
| 1625 | |||
| 1626 | len += size; | ||
| 1627 | to += size; | ||
| 1628 | addr += size; | ||
| 1629 | |||
| 1630 | } while (len < n); | ||
| 1631 | |||
| 1632 | return len; | ||
| 1633 | } | ||
| 1634 | |||
| 1635 | #ifdef CONFIG_COMPAT | 1599 | #ifdef CONFIG_COMPAT |
| 1636 | static inline int | 1600 | static inline int |
| 1637 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) | 1601 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) |
| @@ -1727,6 +1691,11 @@ struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) | |||
| 1727 | { | 1691 | { |
| 1728 | struct perf_callchain_entry *entry; | 1692 | struct perf_callchain_entry *entry; |
| 1729 | 1693 | ||
| 1694 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { | ||
| 1695 | /* TODO: We don't support guest os callchain now */ | ||
| 1696 | return NULL; | ||
| 1697 | } | ||
| 1698 | |||
| 1730 | if (in_nmi()) | 1699 | if (in_nmi()) |
| 1731 | entry = &__get_cpu_var(pmc_nmi_entry); | 1700 | entry = &__get_cpu_var(pmc_nmi_entry); |
| 1732 | else | 1701 | else |
| @@ -1750,3 +1719,37 @@ void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int ski | |||
| 1750 | regs->cs = __KERNEL_CS; | 1719 | regs->cs = __KERNEL_CS; |
| 1751 | local_save_flags(regs->flags); | 1720 | local_save_flags(regs->flags); |
| 1752 | } | 1721 | } |
| 1722 | |||
| 1723 | unsigned long perf_instruction_pointer(struct pt_regs *regs) | ||
| 1724 | { | ||
| 1725 | unsigned long ip; | ||
| 1726 | |||
| 1727 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) | ||
| 1728 | ip = perf_guest_cbs->get_guest_ip(); | ||
| 1729 | else | ||
| 1730 | ip = instruction_pointer(regs); | ||
| 1731 | |||
| 1732 | return ip; | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | unsigned long perf_misc_flags(struct pt_regs *regs) | ||
| 1736 | { | ||
| 1737 | int misc = 0; | ||
| 1738 | |||
| 1739 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { | ||
| 1740 | if (perf_guest_cbs->is_user_mode()) | ||
| 1741 | misc |= PERF_RECORD_MISC_GUEST_USER; | ||
| 1742 | else | ||
| 1743 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; | ||
| 1744 | } else { | ||
| 1745 | if (user_mode(regs)) | ||
| 1746 | misc |= PERF_RECORD_MISC_USER; | ||
| 1747 | else | ||
| 1748 | misc |= PERF_RECORD_MISC_KERNEL; | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | if (regs->flags & PERF_EFLAGS_EXACT) | ||
| 1752 | misc |= PERF_RECORD_MISC_EXACT_IP; | ||
| 1753 | |||
| 1754 | return misc; | ||
| 1755 | } | ||
diff --git a/arch/x86/kernel/cpu/perf_event_amd.c b/arch/x86/kernel/cpu/perf_event_amd.c index db6f7d4056e1..611df11ba15e 100644 --- a/arch/x86/kernel/cpu/perf_event_amd.c +++ b/arch/x86/kernel/cpu/perf_event_amd.c | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | static DEFINE_RAW_SPINLOCK(amd_nb_lock); | 3 | static DEFINE_RAW_SPINLOCK(amd_nb_lock); |
| 4 | 4 | ||
| 5 | static __initconst u64 amd_hw_cache_event_ids | 5 | static __initconst const u64 amd_hw_cache_event_ids |
| 6 | [PERF_COUNT_HW_CACHE_MAX] | 6 | [PERF_COUNT_HW_CACHE_MAX] |
| 7 | [PERF_COUNT_HW_CACHE_OP_MAX] | 7 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 8 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | 8 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| @@ -111,22 +111,19 @@ static u64 amd_pmu_event_map(int hw_event) | |||
| 111 | return amd_perfmon_event_map[hw_event]; | 111 | return amd_perfmon_event_map[hw_event]; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | static u64 amd_pmu_raw_event(u64 hw_event) | 114 | static int amd_pmu_hw_config(struct perf_event *event) |
| 115 | { | 115 | { |
| 116 | #define K7_EVNTSEL_EVENT_MASK 0xF000000FFULL | 116 | int ret = x86_pmu_hw_config(event); |
| 117 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL | 117 | |
| 118 | #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL | 118 | if (ret) |
| 119 | #define K7_EVNTSEL_INV_MASK 0x000800000ULL | 119 | return ret; |
| 120 | #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL | 120 | |
| 121 | 121 | if (event->attr.type != PERF_TYPE_RAW) | |
| 122 | #define K7_EVNTSEL_MASK \ | 122 | return 0; |
| 123 | (K7_EVNTSEL_EVENT_MASK | \ | 123 | |
| 124 | K7_EVNTSEL_UNIT_MASK | \ | 124 | event->hw.config |= event->attr.config & AMD64_RAW_EVENT_MASK; |
| 125 | K7_EVNTSEL_EDGE_MASK | \ | 125 | |
| 126 | K7_EVNTSEL_INV_MASK | \ | 126 | return 0; |
| 127 | K7_EVNTSEL_REG_MASK) | ||
| 128 | |||
| 129 | return hw_event & K7_EVNTSEL_MASK; | ||
| 130 | } | 127 | } |
| 131 | 128 | ||
| 132 | /* | 129 | /* |
| @@ -165,7 +162,7 @@ static void amd_put_event_constraints(struct cpu_hw_events *cpuc, | |||
| 165 | * be removed on one CPU at a time AND PMU is disabled | 162 | * be removed on one CPU at a time AND PMU is disabled |
| 166 | * when we come here | 163 | * when we come here |
| 167 | */ | 164 | */ |
| 168 | for (i = 0; i < x86_pmu.num_events; i++) { | 165 | for (i = 0; i < x86_pmu.num_counters; i++) { |
| 169 | if (nb->owners[i] == event) { | 166 | if (nb->owners[i] == event) { |
| 170 | cmpxchg(nb->owners+i, event, NULL); | 167 | cmpxchg(nb->owners+i, event, NULL); |
| 171 | break; | 168 | break; |
| @@ -215,7 +212,7 @@ amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) | |||
| 215 | struct hw_perf_event *hwc = &event->hw; | 212 | struct hw_perf_event *hwc = &event->hw; |
| 216 | struct amd_nb *nb = cpuc->amd_nb; | 213 | struct amd_nb *nb = cpuc->amd_nb; |
| 217 | struct perf_event *old = NULL; | 214 | struct perf_event *old = NULL; |
| 218 | int max = x86_pmu.num_events; | 215 | int max = x86_pmu.num_counters; |
| 219 | int i, j, k = -1; | 216 | int i, j, k = -1; |
| 220 | 217 | ||
| 221 | /* | 218 | /* |
| @@ -293,7 +290,7 @@ static struct amd_nb *amd_alloc_nb(int cpu, int nb_id) | |||
| 293 | /* | 290 | /* |
| 294 | * initialize all possible NB constraints | 291 | * initialize all possible NB constraints |
| 295 | */ | 292 | */ |
| 296 | for (i = 0; i < x86_pmu.num_events; i++) { | 293 | for (i = 0; i < x86_pmu.num_counters; i++) { |
| 297 | __set_bit(i, nb->event_constraints[i].idxmsk); | 294 | __set_bit(i, nb->event_constraints[i].idxmsk); |
| 298 | nb->event_constraints[i].weight = 1; | 295 | nb->event_constraints[i].weight = 1; |
| 299 | } | 296 | } |
| @@ -371,21 +368,22 @@ static void amd_pmu_cpu_dead(int cpu) | |||
| 371 | raw_spin_unlock(&amd_nb_lock); | 368 | raw_spin_unlock(&amd_nb_lock); |
| 372 | } | 369 | } |
| 373 | 370 | ||
| 374 | static __initconst struct x86_pmu amd_pmu = { | 371 | static __initconst const struct x86_pmu amd_pmu = { |
| 375 | .name = "AMD", | 372 | .name = "AMD", |
| 376 | .handle_irq = x86_pmu_handle_irq, | 373 | .handle_irq = x86_pmu_handle_irq, |
| 377 | .disable_all = x86_pmu_disable_all, | 374 | .disable_all = x86_pmu_disable_all, |
| 378 | .enable_all = x86_pmu_enable_all, | 375 | .enable_all = x86_pmu_enable_all, |
| 379 | .enable = x86_pmu_enable_event, | 376 | .enable = x86_pmu_enable_event, |
| 380 | .disable = x86_pmu_disable_event, | 377 | .disable = x86_pmu_disable_event, |
| 378 | .hw_config = amd_pmu_hw_config, | ||
| 379 | .schedule_events = x86_schedule_events, | ||
| 381 | .eventsel = MSR_K7_EVNTSEL0, | 380 | .eventsel = MSR_K7_EVNTSEL0, |
| 382 | .perfctr = MSR_K7_PERFCTR0, | 381 | .perfctr = MSR_K7_PERFCTR0, |
| 383 | .event_map = amd_pmu_event_map, | 382 | .event_map = amd_pmu_event_map, |
| 384 | .raw_event = amd_pmu_raw_event, | ||
| 385 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), | 383 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
| 386 | .num_events = 4, | 384 | .num_counters = 4, |
| 387 | .event_bits = 48, | 385 | .cntval_bits = 48, |
| 388 | .event_mask = (1ULL << 48) - 1, | 386 | .cntval_mask = (1ULL << 48) - 1, |
| 389 | .apic = 1, | 387 | .apic = 1, |
| 390 | /* use highest bit to detect overflow */ | 388 | /* use highest bit to detect overflow */ |
| 391 | .max_period = (1ULL << 47) - 1, | 389 | .max_period = (1ULL << 47) - 1, |
diff --git a/arch/x86/kernel/cpu/perf_event_intel.c b/arch/x86/kernel/cpu/perf_event_intel.c index 9c794ac87837..fdbc652d3feb 100644 --- a/arch/x86/kernel/cpu/perf_event_intel.c +++ b/arch/x86/kernel/cpu/perf_event_intel.c | |||
| @@ -88,7 +88,7 @@ static u64 intel_pmu_event_map(int hw_event) | |||
| 88 | return intel_perfmon_event_map[hw_event]; | 88 | return intel_perfmon_event_map[hw_event]; |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | static __initconst u64 westmere_hw_cache_event_ids | 91 | static __initconst const u64 westmere_hw_cache_event_ids |
| 92 | [PERF_COUNT_HW_CACHE_MAX] | 92 | [PERF_COUNT_HW_CACHE_MAX] |
| 93 | [PERF_COUNT_HW_CACHE_OP_MAX] | 93 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 94 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | 94 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| @@ -179,7 +179,7 @@ static __initconst u64 westmere_hw_cache_event_ids | |||
| 179 | }, | 179 | }, |
| 180 | }; | 180 | }; |
| 181 | 181 | ||
| 182 | static __initconst u64 nehalem_hw_cache_event_ids | 182 | static __initconst const u64 nehalem_hw_cache_event_ids |
| 183 | [PERF_COUNT_HW_CACHE_MAX] | 183 | [PERF_COUNT_HW_CACHE_MAX] |
| 184 | [PERF_COUNT_HW_CACHE_OP_MAX] | 184 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 185 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | 185 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| @@ -270,7 +270,7 @@ static __initconst u64 nehalem_hw_cache_event_ids | |||
| 270 | }, | 270 | }, |
| 271 | }; | 271 | }; |
| 272 | 272 | ||
| 273 | static __initconst u64 core2_hw_cache_event_ids | 273 | static __initconst const u64 core2_hw_cache_event_ids |
| 274 | [PERF_COUNT_HW_CACHE_MAX] | 274 | [PERF_COUNT_HW_CACHE_MAX] |
| 275 | [PERF_COUNT_HW_CACHE_OP_MAX] | 275 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 276 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | 276 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| @@ -361,7 +361,7 @@ static __initconst u64 core2_hw_cache_event_ids | |||
| 361 | }, | 361 | }, |
| 362 | }; | 362 | }; |
| 363 | 363 | ||
| 364 | static __initconst u64 atom_hw_cache_event_ids | 364 | static __initconst const u64 atom_hw_cache_event_ids |
| 365 | [PERF_COUNT_HW_CACHE_MAX] | 365 | [PERF_COUNT_HW_CACHE_MAX] |
| 366 | [PERF_COUNT_HW_CACHE_OP_MAX] | 366 | [PERF_COUNT_HW_CACHE_OP_MAX] |
| 367 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | 367 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = |
| @@ -452,60 +452,6 @@ static __initconst u64 atom_hw_cache_event_ids | |||
| 452 | }, | 452 | }, |
| 453 | }; | 453 | }; |
| 454 | 454 | ||
| 455 | static u64 intel_pmu_raw_event(u64 hw_event) | ||
| 456 | { | ||
| 457 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL | ||
| 458 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL | ||
| 459 | #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL | ||
| 460 | #define CORE_EVNTSEL_INV_MASK 0x00800000ULL | ||
| 461 | #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL | ||
| 462 | |||
| 463 | #define CORE_EVNTSEL_MASK \ | ||
| 464 | (INTEL_ARCH_EVTSEL_MASK | \ | ||
| 465 | INTEL_ARCH_UNIT_MASK | \ | ||
| 466 | INTEL_ARCH_EDGE_MASK | \ | ||
| 467 | INTEL_ARCH_INV_MASK | \ | ||
| 468 | INTEL_ARCH_CNT_MASK) | ||
| 469 | |||
| 470 | return hw_event & CORE_EVNTSEL_MASK; | ||
| 471 | } | ||
| 472 | |||
| 473 | static void intel_pmu_enable_bts(u64 config) | ||
| 474 | { | ||
| 475 | unsigned long debugctlmsr; | ||
| 476 | |||
| 477 | debugctlmsr = get_debugctlmsr(); | ||
| 478 | |||
| 479 | debugctlmsr |= X86_DEBUGCTL_TR; | ||
| 480 | debugctlmsr |= X86_DEBUGCTL_BTS; | ||
| 481 | debugctlmsr |= X86_DEBUGCTL_BTINT; | ||
| 482 | |||
| 483 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) | ||
| 484 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS; | ||
| 485 | |||
| 486 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) | ||
| 487 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR; | ||
| 488 | |||
| 489 | update_debugctlmsr(debugctlmsr); | ||
| 490 | } | ||
| 491 | |||
| 492 | static void intel_pmu_disable_bts(void) | ||
| 493 | { | ||
| 494 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 495 | unsigned long debugctlmsr; | ||
| 496 | |||
| 497 | if (!cpuc->ds) | ||
| 498 | return; | ||
| 499 | |||
| 500 | debugctlmsr = get_debugctlmsr(); | ||
| 501 | |||
| 502 | debugctlmsr &= | ||
| 503 | ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT | | ||
| 504 | X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR); | ||
| 505 | |||
| 506 | update_debugctlmsr(debugctlmsr); | ||
| 507 | } | ||
| 508 | |||
| 509 | static void intel_pmu_disable_all(void) | 455 | static void intel_pmu_disable_all(void) |
| 510 | { | 456 | { |
| 511 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 457 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| @@ -514,12 +460,17 @@ static void intel_pmu_disable_all(void) | |||
| 514 | 460 | ||
| 515 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) | 461 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) |
| 516 | intel_pmu_disable_bts(); | 462 | intel_pmu_disable_bts(); |
| 463 | |||
| 464 | intel_pmu_pebs_disable_all(); | ||
| 465 | intel_pmu_lbr_disable_all(); | ||
| 517 | } | 466 | } |
| 518 | 467 | ||
| 519 | static void intel_pmu_enable_all(void) | 468 | static void intel_pmu_enable_all(int added) |
| 520 | { | 469 | { |
| 521 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | 470 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
| 522 | 471 | ||
| 472 | intel_pmu_pebs_enable_all(); | ||
| 473 | intel_pmu_lbr_enable_all(); | ||
| 523 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); | 474 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
| 524 | 475 | ||
| 525 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { | 476 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { |
| @@ -533,6 +484,42 @@ static void intel_pmu_enable_all(void) | |||
| 533 | } | 484 | } |
| 534 | } | 485 | } |
| 535 | 486 | ||
| 487 | /* | ||
| 488 | * Workaround for: | ||
| 489 | * Intel Errata AAK100 (model 26) | ||
| 490 | * Intel Errata AAP53 (model 30) | ||
| 491 | * Intel Errata BD53 (model 44) | ||
| 492 | * | ||
| 493 | * These chips need to be 'reset' when adding counters by programming | ||
| 494 | * the magic three (non counting) events 0x4300D2, 0x4300B1 and 0x4300B5 | ||
| 495 | * either in sequence on the same PMC or on different PMCs. | ||
| 496 | */ | ||
| 497 | static void intel_pmu_nhm_enable_all(int added) | ||
| 498 | { | ||
| 499 | if (added) { | ||
| 500 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 501 | int i; | ||
| 502 | |||
| 503 | wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 0, 0x4300D2); | ||
| 504 | wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 1, 0x4300B1); | ||
| 505 | wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 2, 0x4300B5); | ||
| 506 | |||
| 507 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x3); | ||
| 508 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0); | ||
| 509 | |||
| 510 | for (i = 0; i < 3; i++) { | ||
| 511 | struct perf_event *event = cpuc->events[i]; | ||
| 512 | |||
| 513 | if (!event) | ||
| 514 | continue; | ||
| 515 | |||
| 516 | __x86_pmu_enable_event(&event->hw, | ||
| 517 | ARCH_PERFMON_EVENTSEL_ENABLE); | ||
| 518 | } | ||
| 519 | } | ||
| 520 | intel_pmu_enable_all(added); | ||
| 521 | } | ||
| 522 | |||
| 536 | static inline u64 intel_pmu_get_status(void) | 523 | static inline u64 intel_pmu_get_status(void) |
| 537 | { | 524 | { |
| 538 | u64 status; | 525 | u64 status; |
| @@ -547,8 +534,7 @@ static inline void intel_pmu_ack_status(u64 ack) | |||
| 547 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); | 534 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); |
| 548 | } | 535 | } |
| 549 | 536 | ||
| 550 | static inline void | 537 | static void intel_pmu_disable_fixed(struct hw_perf_event *hwc) |
| 551 | intel_pmu_disable_fixed(struct hw_perf_event *hwc) | ||
| 552 | { | 538 | { |
| 553 | int idx = hwc->idx - X86_PMC_IDX_FIXED; | 539 | int idx = hwc->idx - X86_PMC_IDX_FIXED; |
| 554 | u64 ctrl_val, mask; | 540 | u64 ctrl_val, mask; |
| @@ -557,71 +543,10 @@ intel_pmu_disable_fixed(struct hw_perf_event *hwc) | |||
| 557 | 543 | ||
| 558 | rdmsrl(hwc->config_base, ctrl_val); | 544 | rdmsrl(hwc->config_base, ctrl_val); |
| 559 | ctrl_val &= ~mask; | 545 | ctrl_val &= ~mask; |
| 560 | (void)checking_wrmsrl(hwc->config_base, ctrl_val); | 546 | wrmsrl(hwc->config_base, ctrl_val); |
| 561 | } | ||
| 562 | |||
| 563 | static void intel_pmu_drain_bts_buffer(void) | ||
| 564 | { | ||
| 565 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 566 | struct debug_store *ds = cpuc->ds; | ||
| 567 | struct bts_record { | ||
| 568 | u64 from; | ||
| 569 | u64 to; | ||
| 570 | u64 flags; | ||
| 571 | }; | ||
| 572 | struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS]; | ||
| 573 | struct bts_record *at, *top; | ||
| 574 | struct perf_output_handle handle; | ||
| 575 | struct perf_event_header header; | ||
| 576 | struct perf_sample_data data; | ||
| 577 | struct pt_regs regs; | ||
| 578 | |||
| 579 | if (!event) | ||
| 580 | return; | ||
| 581 | |||
| 582 | if (!ds) | ||
| 583 | return; | ||
| 584 | |||
| 585 | at = (struct bts_record *)(unsigned long)ds->bts_buffer_base; | ||
| 586 | top = (struct bts_record *)(unsigned long)ds->bts_index; | ||
| 587 | |||
| 588 | if (top <= at) | ||
| 589 | return; | ||
| 590 | |||
| 591 | ds->bts_index = ds->bts_buffer_base; | ||
| 592 | |||
| 593 | perf_sample_data_init(&data, 0); | ||
| 594 | |||
| 595 | data.period = event->hw.last_period; | ||
| 596 | regs.ip = 0; | ||
| 597 | |||
| 598 | /* | ||
| 599 | * Prepare a generic sample, i.e. fill in the invariant fields. | ||
| 600 | * We will overwrite the from and to address before we output | ||
| 601 | * the sample. | ||
| 602 | */ | ||
| 603 | perf_prepare_sample(&header, &data, event, ®s); | ||
| 604 | |||
| 605 | if (perf_output_begin(&handle, event, | ||
| 606 | header.size * (top - at), 1, 1)) | ||
| 607 | return; | ||
| 608 | |||
| 609 | for (; at < top; at++) { | ||
| 610 | data.ip = at->from; | ||
| 611 | data.addr = at->to; | ||
| 612 | |||
| 613 | perf_output_sample(&handle, &header, &data, event); | ||
| 614 | } | ||
| 615 | |||
| 616 | perf_output_end(&handle); | ||
| 617 | |||
| 618 | /* There's new data available. */ | ||
| 619 | event->hw.interrupts++; | ||
| 620 | event->pending_kill = POLL_IN; | ||
| 621 | } | 547 | } |
| 622 | 548 | ||
| 623 | static inline void | 549 | static void intel_pmu_disable_event(struct perf_event *event) |
| 624 | intel_pmu_disable_event(struct perf_event *event) | ||
| 625 | { | 550 | { |
| 626 | struct hw_perf_event *hwc = &event->hw; | 551 | struct hw_perf_event *hwc = &event->hw; |
| 627 | 552 | ||
| @@ -637,14 +562,15 @@ intel_pmu_disable_event(struct perf_event *event) | |||
| 637 | } | 562 | } |
| 638 | 563 | ||
| 639 | x86_pmu_disable_event(event); | 564 | x86_pmu_disable_event(event); |
| 565 | |||
| 566 | if (unlikely(event->attr.precise_ip)) | ||
| 567 | intel_pmu_pebs_disable(event); | ||
| 640 | } | 568 | } |
| 641 | 569 | ||
| 642 | static inline void | 570 | static void intel_pmu_enable_fixed(struct hw_perf_event *hwc) |
| 643 | intel_pmu_enable_fixed(struct hw_perf_event *hwc) | ||
| 644 | { | 571 | { |
| 645 | int idx = hwc->idx - X86_PMC_IDX_FIXED; | 572 | int idx = hwc->idx - X86_PMC_IDX_FIXED; |
| 646 | u64 ctrl_val, bits, mask; | 573 | u64 ctrl_val, bits, mask; |
| 647 | int err; | ||
| 648 | 574 | ||
| 649 | /* | 575 | /* |
| 650 | * Enable IRQ generation (0x8), | 576 | * Enable IRQ generation (0x8), |
| @@ -669,7 +595,7 @@ intel_pmu_enable_fixed(struct hw_perf_event *hwc) | |||
| 669 | rdmsrl(hwc->config_base, ctrl_val); | 595 | rdmsrl(hwc->config_base, ctrl_val); |
| 670 | ctrl_val &= ~mask; | 596 | ctrl_val &= ~mask; |
| 671 | ctrl_val |= bits; | 597 | ctrl_val |= bits; |
| 672 | err = checking_wrmsrl(hwc->config_base, ctrl_val); | 598 | wrmsrl(hwc->config_base, ctrl_val); |
| 673 | } | 599 | } |
| 674 | 600 | ||
| 675 | static void intel_pmu_enable_event(struct perf_event *event) | 601 | static void intel_pmu_enable_event(struct perf_event *event) |
| @@ -689,7 +615,10 @@ static void intel_pmu_enable_event(struct perf_event *event) | |||
| 689 | return; | 615 | return; |
| 690 | } | 616 | } |
| 691 | 617 | ||
| 692 | __x86_pmu_enable_event(hwc); | 618 | if (unlikely(event->attr.precise_ip)) |
| 619 | intel_pmu_pebs_enable(event); | ||
| 620 | |||
| 621 | __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); | ||
| 693 | } | 622 | } |
| 694 | 623 | ||
| 695 | /* | 624 | /* |
| @@ -708,20 +637,20 @@ static void intel_pmu_reset(void) | |||
| 708 | unsigned long flags; | 637 | unsigned long flags; |
| 709 | int idx; | 638 | int idx; |
| 710 | 639 | ||
| 711 | if (!x86_pmu.num_events) | 640 | if (!x86_pmu.num_counters) |
| 712 | return; | 641 | return; |
| 713 | 642 | ||
| 714 | local_irq_save(flags); | 643 | local_irq_save(flags); |
| 715 | 644 | ||
| 716 | printk("clearing PMU state on CPU#%d\n", smp_processor_id()); | 645 | printk("clearing PMU state on CPU#%d\n", smp_processor_id()); |
| 717 | 646 | ||
| 718 | for (idx = 0; idx < x86_pmu.num_events; idx++) { | 647 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
| 719 | checking_wrmsrl(x86_pmu.eventsel + idx, 0ull); | 648 | checking_wrmsrl(x86_pmu.eventsel + idx, 0ull); |
| 720 | checking_wrmsrl(x86_pmu.perfctr + idx, 0ull); | 649 | checking_wrmsrl(x86_pmu.perfctr + idx, 0ull); |
| 721 | } | 650 | } |
| 722 | for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) { | 651 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) |
| 723 | checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull); | 652 | checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull); |
| 724 | } | 653 | |
| 725 | if (ds) | 654 | if (ds) |
| 726 | ds->bts_index = ds->bts_buffer_base; | 655 | ds->bts_index = ds->bts_buffer_base; |
| 727 | 656 | ||
| @@ -747,7 +676,7 @@ static int intel_pmu_handle_irq(struct pt_regs *regs) | |||
| 747 | intel_pmu_drain_bts_buffer(); | 676 | intel_pmu_drain_bts_buffer(); |
| 748 | status = intel_pmu_get_status(); | 677 | status = intel_pmu_get_status(); |
| 749 | if (!status) { | 678 | if (!status) { |
| 750 | intel_pmu_enable_all(); | 679 | intel_pmu_enable_all(0); |
| 751 | return 0; | 680 | return 0; |
| 752 | } | 681 | } |
| 753 | 682 | ||
| @@ -762,6 +691,15 @@ again: | |||
| 762 | 691 | ||
| 763 | inc_irq_stat(apic_perf_irqs); | 692 | inc_irq_stat(apic_perf_irqs); |
| 764 | ack = status; | 693 | ack = status; |
| 694 | |||
| 695 | intel_pmu_lbr_read(); | ||
| 696 | |||
| 697 | /* | ||
| 698 | * PEBS overflow sets bit 62 in the global status register | ||
| 699 | */ | ||
| 700 | if (__test_and_clear_bit(62, (unsigned long *)&status)) | ||
| 701 | x86_pmu.drain_pebs(regs); | ||
| 702 | |||
| 765 | for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { | 703 | for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
| 766 | struct perf_event *event = cpuc->events[bit]; | 704 | struct perf_event *event = cpuc->events[bit]; |
| 767 | 705 | ||
| @@ -787,26 +725,22 @@ again: | |||
| 787 | goto again; | 725 | goto again; |
| 788 | 726 | ||
| 789 | done: | 727 | done: |
| 790 | intel_pmu_enable_all(); | 728 | intel_pmu_enable_all(0); |
| 791 | return 1; | 729 | return 1; |
| 792 | } | 730 | } |
| 793 | 731 | ||
| 794 | static struct event_constraint bts_constraint = | ||
| 795 | EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0); | ||
| 796 | |||
| 797 | static struct event_constraint * | 732 | static struct event_constraint * |
| 798 | intel_special_constraints(struct perf_event *event) | 733 | intel_bts_constraints(struct perf_event *event) |
| 799 | { | 734 | { |
| 800 | unsigned int hw_event; | 735 | struct hw_perf_event *hwc = &event->hw; |
| 801 | 736 | unsigned int hw_event, bts_event; | |
| 802 | hw_event = event->hw.config & INTEL_ARCH_EVENT_MASK; | ||
| 803 | 737 | ||
| 804 | if (unlikely((hw_event == | 738 | hw_event = hwc->config & INTEL_ARCH_EVENT_MASK; |
| 805 | x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) && | 739 | bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS); |
| 806 | (event->hw.sample_period == 1))) { | ||
| 807 | 740 | ||
| 741 | if (unlikely(hw_event == bts_event && hwc->sample_period == 1)) | ||
| 808 | return &bts_constraint; | 742 | return &bts_constraint; |
| 809 | } | 743 | |
| 810 | return NULL; | 744 | return NULL; |
| 811 | } | 745 | } |
| 812 | 746 | ||
| @@ -815,24 +749,53 @@ intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event | |||
| 815 | { | 749 | { |
| 816 | struct event_constraint *c; | 750 | struct event_constraint *c; |
| 817 | 751 | ||
| 818 | c = intel_special_constraints(event); | 752 | c = intel_bts_constraints(event); |
| 753 | if (c) | ||
| 754 | return c; | ||
| 755 | |||
| 756 | c = intel_pebs_constraints(event); | ||
| 819 | if (c) | 757 | if (c) |
| 820 | return c; | 758 | return c; |
| 821 | 759 | ||
| 822 | return x86_get_event_constraints(cpuc, event); | 760 | return x86_get_event_constraints(cpuc, event); |
| 823 | } | 761 | } |
| 824 | 762 | ||
| 825 | static __initconst struct x86_pmu core_pmu = { | 763 | static int intel_pmu_hw_config(struct perf_event *event) |
| 764 | { | ||
| 765 | int ret = x86_pmu_hw_config(event); | ||
| 766 | |||
| 767 | if (ret) | ||
| 768 | return ret; | ||
| 769 | |||
| 770 | if (event->attr.type != PERF_TYPE_RAW) | ||
| 771 | return 0; | ||
| 772 | |||
| 773 | if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY)) | ||
| 774 | return 0; | ||
| 775 | |||
| 776 | if (x86_pmu.version < 3) | ||
| 777 | return -EINVAL; | ||
| 778 | |||
| 779 | if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) | ||
| 780 | return -EACCES; | ||
| 781 | |||
| 782 | event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY; | ||
| 783 | |||
| 784 | return 0; | ||
| 785 | } | ||
| 786 | |||
| 787 | static __initconst const struct x86_pmu core_pmu = { | ||
| 826 | .name = "core", | 788 | .name = "core", |
| 827 | .handle_irq = x86_pmu_handle_irq, | 789 | .handle_irq = x86_pmu_handle_irq, |
| 828 | .disable_all = x86_pmu_disable_all, | 790 | .disable_all = x86_pmu_disable_all, |
| 829 | .enable_all = x86_pmu_enable_all, | 791 | .enable_all = x86_pmu_enable_all, |
| 830 | .enable = x86_pmu_enable_event, | 792 | .enable = x86_pmu_enable_event, |
| 831 | .disable = x86_pmu_disable_event, | 793 | .disable = x86_pmu_disable_event, |
| 794 | .hw_config = x86_pmu_hw_config, | ||
| 795 | .schedule_events = x86_schedule_events, | ||
| 832 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, | 796 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 833 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, | 797 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
| 834 | .event_map = intel_pmu_event_map, | 798 | .event_map = intel_pmu_event_map, |
| 835 | .raw_event = intel_pmu_raw_event, | ||
| 836 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), | 799 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
| 837 | .apic = 1, | 800 | .apic = 1, |
| 838 | /* | 801 | /* |
| @@ -845,17 +808,32 @@ static __initconst struct x86_pmu core_pmu = { | |||
| 845 | .event_constraints = intel_core_event_constraints, | 808 | .event_constraints = intel_core_event_constraints, |
| 846 | }; | 809 | }; |
| 847 | 810 | ||
| 848 | static __initconst struct x86_pmu intel_pmu = { | 811 | static void intel_pmu_cpu_starting(int cpu) |
| 812 | { | ||
| 813 | init_debug_store_on_cpu(cpu); | ||
| 814 | /* | ||
| 815 | * Deal with CPUs that don't clear their LBRs on power-up. | ||
| 816 | */ | ||
| 817 | intel_pmu_lbr_reset(); | ||
| 818 | } | ||
| 819 | |||
| 820 | static void intel_pmu_cpu_dying(int cpu) | ||
| 821 | { | ||
| 822 | fini_debug_store_on_cpu(cpu); | ||
| 823 | } | ||
| 824 | |||
| 825 | static __initconst const struct x86_pmu intel_pmu = { | ||
| 849 | .name = "Intel", | 826 | .name = "Intel", |
| 850 | .handle_irq = intel_pmu_handle_irq, | 827 | .handle_irq = intel_pmu_handle_irq, |
| 851 | .disable_all = intel_pmu_disable_all, | 828 | .disable_all = intel_pmu_disable_all, |
| 852 | .enable_all = intel_pmu_enable_all, | 829 | .enable_all = intel_pmu_enable_all, |
| 853 | .enable = intel_pmu_enable_event, | 830 | .enable = intel_pmu_enable_event, |
| 854 | .disable = intel_pmu_disable_event, | 831 | .disable = intel_pmu_disable_event, |
| 832 | .hw_config = intel_pmu_hw_config, | ||
| 833 | .schedule_events = x86_schedule_events, | ||
| 855 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, | 834 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
| 856 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, | 835 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, |
| 857 | .event_map = intel_pmu_event_map, | 836 | .event_map = intel_pmu_event_map, |
| 858 | .raw_event = intel_pmu_raw_event, | ||
| 859 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), | 837 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
| 860 | .apic = 1, | 838 | .apic = 1, |
| 861 | /* | 839 | /* |
| @@ -864,14 +842,38 @@ static __initconst struct x86_pmu intel_pmu = { | |||
| 864 | * the generic event period: | 842 | * the generic event period: |
| 865 | */ | 843 | */ |
| 866 | .max_period = (1ULL << 31) - 1, | 844 | .max_period = (1ULL << 31) - 1, |
| 867 | .enable_bts = intel_pmu_enable_bts, | ||
| 868 | .disable_bts = intel_pmu_disable_bts, | ||
| 869 | .get_event_constraints = intel_get_event_constraints, | 845 | .get_event_constraints = intel_get_event_constraints, |
| 870 | 846 | ||
| 871 | .cpu_starting = init_debug_store_on_cpu, | 847 | .cpu_starting = intel_pmu_cpu_starting, |
| 872 | .cpu_dying = fini_debug_store_on_cpu, | 848 | .cpu_dying = intel_pmu_cpu_dying, |
| 873 | }; | 849 | }; |
| 874 | 850 | ||
| 851 | static void intel_clovertown_quirks(void) | ||
| 852 | { | ||
| 853 | /* | ||
| 854 | * PEBS is unreliable due to: | ||
| 855 | * | ||
| 856 | * AJ67 - PEBS may experience CPL leaks | ||
| 857 | * AJ68 - PEBS PMI may be delayed by one event | ||
| 858 | * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12] | ||
| 859 | * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS | ||
| 860 | * | ||
| 861 | * AJ67 could be worked around by restricting the OS/USR flags. | ||
| 862 | * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI. | ||
| 863 | * | ||
| 864 | * AJ106 could possibly be worked around by not allowing LBR | ||
| 865 | * usage from PEBS, including the fixup. | ||
| 866 | * AJ68 could possibly be worked around by always programming | ||
| 867 | * a pebs_event_reset[0] value and coping with the lost events. | ||
| 868 | * | ||
| 869 | * But taken together it might just make sense to not enable PEBS on | ||
| 870 | * these chips. | ||
| 871 | */ | ||
| 872 | printk(KERN_WARNING "PEBS disabled due to CPU errata.\n"); | ||
| 873 | x86_pmu.pebs = 0; | ||
| 874 | x86_pmu.pebs_constraints = NULL; | ||
| 875 | } | ||
| 876 | |||
| 875 | static __init int intel_pmu_init(void) | 877 | static __init int intel_pmu_init(void) |
| 876 | { | 878 | { |
| 877 | union cpuid10_edx edx; | 879 | union cpuid10_edx edx; |
| @@ -881,12 +883,13 @@ static __init int intel_pmu_init(void) | |||
| 881 | int version; | 883 | int version; |
| 882 | 884 | ||
| 883 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { | 885 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
| 884 | /* check for P6 processor family */ | 886 | switch (boot_cpu_data.x86) { |
| 885 | if (boot_cpu_data.x86 == 6) { | 887 | case 0x6: |
| 886 | return p6_pmu_init(); | 888 | return p6_pmu_init(); |
| 887 | } else { | 889 | case 0xf: |
| 890 | return p4_pmu_init(); | ||
| 891 | } | ||
| 888 | return -ENODEV; | 892 | return -ENODEV; |
| 889 | } | ||
| 890 | } | 893 | } |
| 891 | 894 | ||
| 892 | /* | 895 | /* |
| @@ -904,16 +907,28 @@ static __init int intel_pmu_init(void) | |||
| 904 | x86_pmu = intel_pmu; | 907 | x86_pmu = intel_pmu; |
| 905 | 908 | ||
| 906 | x86_pmu.version = version; | 909 | x86_pmu.version = version; |
| 907 | x86_pmu.num_events = eax.split.num_events; | 910 | x86_pmu.num_counters = eax.split.num_counters; |
| 908 | x86_pmu.event_bits = eax.split.bit_width; | 911 | x86_pmu.cntval_bits = eax.split.bit_width; |
| 909 | x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1; | 912 | x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1; |
| 910 | 913 | ||
| 911 | /* | 914 | /* |
| 912 | * Quirk: v2 perfmon does not report fixed-purpose events, so | 915 | * Quirk: v2 perfmon does not report fixed-purpose events, so |
| 913 | * assume at least 3 events: | 916 | * assume at least 3 events: |
| 914 | */ | 917 | */ |
| 915 | if (version > 1) | 918 | if (version > 1) |
| 916 | x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3); | 919 | x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3); |
| 920 | |||
| 921 | /* | ||
| 922 | * v2 and above have a perf capabilities MSR | ||
| 923 | */ | ||
| 924 | if (version > 1) { | ||
| 925 | u64 capabilities; | ||
| 926 | |||
| 927 | rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities); | ||
| 928 | x86_pmu.intel_cap.capabilities = capabilities; | ||
| 929 | } | ||
| 930 | |||
| 931 | intel_ds_init(); | ||
| 917 | 932 | ||
| 918 | /* | 933 | /* |
| 919 | * Install the hw-cache-events table: | 934 | * Install the hw-cache-events table: |
| @@ -924,12 +939,15 @@ static __init int intel_pmu_init(void) | |||
| 924 | break; | 939 | break; |
| 925 | 940 | ||
| 926 | case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ | 941 | case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ |
| 942 | x86_pmu.quirks = intel_clovertown_quirks; | ||
| 927 | case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ | 943 | case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ |
| 928 | case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ | 944 | case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ |
| 929 | case 29: /* six-core 45 nm xeon "Dunnington" */ | 945 | case 29: /* six-core 45 nm xeon "Dunnington" */ |
| 930 | memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, | 946 | memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, |
| 931 | sizeof(hw_cache_event_ids)); | 947 | sizeof(hw_cache_event_ids)); |
| 932 | 948 | ||
| 949 | intel_pmu_lbr_init_core(); | ||
| 950 | |||
| 933 | x86_pmu.event_constraints = intel_core2_event_constraints; | 951 | x86_pmu.event_constraints = intel_core2_event_constraints; |
| 934 | pr_cont("Core2 events, "); | 952 | pr_cont("Core2 events, "); |
| 935 | break; | 953 | break; |
| @@ -940,13 +958,19 @@ static __init int intel_pmu_init(void) | |||
| 940 | memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, | 958 | memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, |
| 941 | sizeof(hw_cache_event_ids)); | 959 | sizeof(hw_cache_event_ids)); |
| 942 | 960 | ||
| 961 | intel_pmu_lbr_init_nhm(); | ||
| 962 | |||
| 943 | x86_pmu.event_constraints = intel_nehalem_event_constraints; | 963 | x86_pmu.event_constraints = intel_nehalem_event_constraints; |
| 944 | pr_cont("Nehalem/Corei7 events, "); | 964 | x86_pmu.enable_all = intel_pmu_nhm_enable_all; |
| 965 | pr_cont("Nehalem events, "); | ||
| 945 | break; | 966 | break; |
| 967 | |||
| 946 | case 28: /* Atom */ | 968 | case 28: /* Atom */ |
| 947 | memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, | 969 | memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, |
| 948 | sizeof(hw_cache_event_ids)); | 970 | sizeof(hw_cache_event_ids)); |
| 949 | 971 | ||
| 972 | intel_pmu_lbr_init_atom(); | ||
| 973 | |||
| 950 | x86_pmu.event_constraints = intel_gen_event_constraints; | 974 | x86_pmu.event_constraints = intel_gen_event_constraints; |
| 951 | pr_cont("Atom events, "); | 975 | pr_cont("Atom events, "); |
| 952 | break; | 976 | break; |
| @@ -956,7 +980,10 @@ static __init int intel_pmu_init(void) | |||
| 956 | memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids, | 980 | memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids, |
| 957 | sizeof(hw_cache_event_ids)); | 981 | sizeof(hw_cache_event_ids)); |
| 958 | 982 | ||
| 983 | intel_pmu_lbr_init_nhm(); | ||
| 984 | |||
| 959 | x86_pmu.event_constraints = intel_westmere_event_constraints; | 985 | x86_pmu.event_constraints = intel_westmere_event_constraints; |
| 986 | x86_pmu.enable_all = intel_pmu_nhm_enable_all; | ||
| 960 | pr_cont("Westmere events, "); | 987 | pr_cont("Westmere events, "); |
| 961 | break; | 988 | break; |
| 962 | 989 | ||
diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c new file mode 100644 index 000000000000..18018d1311cd --- /dev/null +++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c | |||
| @@ -0,0 +1,641 @@ | |||
| 1 | #ifdef CONFIG_CPU_SUP_INTEL | ||
| 2 | |||
| 3 | /* The maximal number of PEBS events: */ | ||
| 4 | #define MAX_PEBS_EVENTS 4 | ||
| 5 | |||
| 6 | /* The size of a BTS record in bytes: */ | ||
| 7 | #define BTS_RECORD_SIZE 24 | ||
| 8 | |||
| 9 | #define BTS_BUFFER_SIZE (PAGE_SIZE << 4) | ||
| 10 | #define PEBS_BUFFER_SIZE PAGE_SIZE | ||
| 11 | |||
| 12 | /* | ||
| 13 | * pebs_record_32 for p4 and core not supported | ||
| 14 | |||
| 15 | struct pebs_record_32 { | ||
| 16 | u32 flags, ip; | ||
| 17 | u32 ax, bc, cx, dx; | ||
| 18 | u32 si, di, bp, sp; | ||
| 19 | }; | ||
| 20 | |||
| 21 | */ | ||
| 22 | |||
| 23 | struct pebs_record_core { | ||
| 24 | u64 flags, ip; | ||
| 25 | u64 ax, bx, cx, dx; | ||
| 26 | u64 si, di, bp, sp; | ||
| 27 | u64 r8, r9, r10, r11; | ||
| 28 | u64 r12, r13, r14, r15; | ||
| 29 | }; | ||
| 30 | |||
| 31 | struct pebs_record_nhm { | ||
| 32 | u64 flags, ip; | ||
| 33 | u64 ax, bx, cx, dx; | ||
| 34 | u64 si, di, bp, sp; | ||
| 35 | u64 r8, r9, r10, r11; | ||
| 36 | u64 r12, r13, r14, r15; | ||
| 37 | u64 status, dla, dse, lat; | ||
| 38 | }; | ||
| 39 | |||
| 40 | /* | ||
| 41 | * A debug store configuration. | ||
| 42 | * | ||
| 43 | * We only support architectures that use 64bit fields. | ||
| 44 | */ | ||
| 45 | struct debug_store { | ||
| 46 | u64 bts_buffer_base; | ||
| 47 | u64 bts_index; | ||
| 48 | u64 bts_absolute_maximum; | ||
| 49 | u64 bts_interrupt_threshold; | ||
| 50 | u64 pebs_buffer_base; | ||
| 51 | u64 pebs_index; | ||
| 52 | u64 pebs_absolute_maximum; | ||
| 53 | u64 pebs_interrupt_threshold; | ||
| 54 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; | ||
| 55 | }; | ||
| 56 | |||
| 57 | static void init_debug_store_on_cpu(int cpu) | ||
| 58 | { | ||
| 59 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; | ||
| 60 | |||
| 61 | if (!ds) | ||
| 62 | return; | ||
| 63 | |||
| 64 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, | ||
| 65 | (u32)((u64)(unsigned long)ds), | ||
| 66 | (u32)((u64)(unsigned long)ds >> 32)); | ||
| 67 | } | ||
| 68 | |||
| 69 | static void fini_debug_store_on_cpu(int cpu) | ||
| 70 | { | ||
| 71 | if (!per_cpu(cpu_hw_events, cpu).ds) | ||
| 72 | return; | ||
| 73 | |||
| 74 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); | ||
| 75 | } | ||
| 76 | |||
| 77 | static void release_ds_buffers(void) | ||
| 78 | { | ||
| 79 | int cpu; | ||
| 80 | |||
| 81 | if (!x86_pmu.bts && !x86_pmu.pebs) | ||
| 82 | return; | ||
| 83 | |||
| 84 | get_online_cpus(); | ||
| 85 | |||
| 86 | for_each_online_cpu(cpu) | ||
| 87 | fini_debug_store_on_cpu(cpu); | ||
| 88 | |||
| 89 | for_each_possible_cpu(cpu) { | ||
| 90 | struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds; | ||
| 91 | |||
| 92 | if (!ds) | ||
| 93 | continue; | ||
| 94 | |||
| 95 | per_cpu(cpu_hw_events, cpu).ds = NULL; | ||
| 96 | |||
| 97 | kfree((void *)(unsigned long)ds->pebs_buffer_base); | ||
| 98 | kfree((void *)(unsigned long)ds->bts_buffer_base); | ||
| 99 | kfree(ds); | ||
| 100 | } | ||
| 101 | |||
| 102 | put_online_cpus(); | ||
| 103 | } | ||
| 104 | |||
| 105 | static int reserve_ds_buffers(void) | ||
| 106 | { | ||
| 107 | int cpu, err = 0; | ||
| 108 | |||
| 109 | if (!x86_pmu.bts && !x86_pmu.pebs) | ||
| 110 | return 0; | ||
| 111 | |||
| 112 | get_online_cpus(); | ||
| 113 | |||
| 114 | for_each_possible_cpu(cpu) { | ||
| 115 | struct debug_store *ds; | ||
| 116 | void *buffer; | ||
| 117 | int max, thresh; | ||
| 118 | |||
| 119 | err = -ENOMEM; | ||
| 120 | ds = kzalloc(sizeof(*ds), GFP_KERNEL); | ||
| 121 | if (unlikely(!ds)) | ||
| 122 | break; | ||
| 123 | per_cpu(cpu_hw_events, cpu).ds = ds; | ||
| 124 | |||
| 125 | if (x86_pmu.bts) { | ||
| 126 | buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL); | ||
| 127 | if (unlikely(!buffer)) | ||
| 128 | break; | ||
| 129 | |||
| 130 | max = BTS_BUFFER_SIZE / BTS_RECORD_SIZE; | ||
| 131 | thresh = max / 16; | ||
| 132 | |||
| 133 | ds->bts_buffer_base = (u64)(unsigned long)buffer; | ||
| 134 | ds->bts_index = ds->bts_buffer_base; | ||
| 135 | ds->bts_absolute_maximum = ds->bts_buffer_base + | ||
| 136 | max * BTS_RECORD_SIZE; | ||
| 137 | ds->bts_interrupt_threshold = ds->bts_absolute_maximum - | ||
| 138 | thresh * BTS_RECORD_SIZE; | ||
| 139 | } | ||
| 140 | |||
| 141 | if (x86_pmu.pebs) { | ||
| 142 | buffer = kzalloc(PEBS_BUFFER_SIZE, GFP_KERNEL); | ||
| 143 | if (unlikely(!buffer)) | ||
| 144 | break; | ||
| 145 | |||
| 146 | max = PEBS_BUFFER_SIZE / x86_pmu.pebs_record_size; | ||
| 147 | |||
| 148 | ds->pebs_buffer_base = (u64)(unsigned long)buffer; | ||
| 149 | ds->pebs_index = ds->pebs_buffer_base; | ||
| 150 | ds->pebs_absolute_maximum = ds->pebs_buffer_base + | ||
| 151 | max * x86_pmu.pebs_record_size; | ||
| 152 | /* | ||
| 153 | * Always use single record PEBS | ||
| 154 | */ | ||
| 155 | ds->pebs_interrupt_threshold = ds->pebs_buffer_base + | ||
| 156 | x86_pmu.pebs_record_size; | ||
| 157 | } | ||
| 158 | |||
| 159 | err = 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (err) | ||
| 163 | release_ds_buffers(); | ||
| 164 | else { | ||
| 165 | for_each_online_cpu(cpu) | ||
| 166 | init_debug_store_on_cpu(cpu); | ||
| 167 | } | ||
| 168 | |||
| 169 | put_online_cpus(); | ||
| 170 | |||
| 171 | return err; | ||
| 172 | } | ||
| 173 | |||
| 174 | /* | ||
| 175 | * BTS | ||
| 176 | */ | ||
| 177 | |||
| 178 | static struct event_constraint bts_constraint = | ||
| 179 | EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0); | ||
| 180 | |||
| 181 | static void intel_pmu_enable_bts(u64 config) | ||
| 182 | { | ||
| 183 | unsigned long debugctlmsr; | ||
| 184 | |||
| 185 | debugctlmsr = get_debugctlmsr(); | ||
| 186 | |||
| 187 | debugctlmsr |= DEBUGCTLMSR_TR; | ||
| 188 | debugctlmsr |= DEBUGCTLMSR_BTS; | ||
| 189 | debugctlmsr |= DEBUGCTLMSR_BTINT; | ||
| 190 | |||
| 191 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) | ||
| 192 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_OS; | ||
| 193 | |||
| 194 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) | ||
| 195 | debugctlmsr |= DEBUGCTLMSR_BTS_OFF_USR; | ||
| 196 | |||
| 197 | update_debugctlmsr(debugctlmsr); | ||
| 198 | } | ||
| 199 | |||
| 200 | static void intel_pmu_disable_bts(void) | ||
| 201 | { | ||
| 202 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 203 | unsigned long debugctlmsr; | ||
| 204 | |||
| 205 | if (!cpuc->ds) | ||
| 206 | return; | ||
| 207 | |||
| 208 | debugctlmsr = get_debugctlmsr(); | ||
| 209 | |||
| 210 | debugctlmsr &= | ||
| 211 | ~(DEBUGCTLMSR_TR | DEBUGCTLMSR_BTS | DEBUGCTLMSR_BTINT | | ||
| 212 | DEBUGCTLMSR_BTS_OFF_OS | DEBUGCTLMSR_BTS_OFF_USR); | ||
| 213 | |||
| 214 | update_debugctlmsr(debugctlmsr); | ||
| 215 | } | ||
| 216 | |||
| 217 | static void intel_pmu_drain_bts_buffer(void) | ||
| 218 | { | ||
| 219 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 220 | struct debug_store *ds = cpuc->ds; | ||
| 221 | struct bts_record { | ||
| 222 | u64 from; | ||
| 223 | u64 to; | ||
| 224 | u64 flags; | ||
| 225 | }; | ||
| 226 | struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS]; | ||
| 227 | struct bts_record *at, *top; | ||
| 228 | struct perf_output_handle handle; | ||
| 229 | struct perf_event_header header; | ||
| 230 | struct perf_sample_data data; | ||
| 231 | struct pt_regs regs; | ||
| 232 | |||
| 233 | if (!event) | ||
| 234 | return; | ||
| 235 | |||
| 236 | if (!ds) | ||
| 237 | return; | ||
| 238 | |||
| 239 | at = (struct bts_record *)(unsigned long)ds->bts_buffer_base; | ||
| 240 | top = (struct bts_record *)(unsigned long)ds->bts_index; | ||
| 241 | |||
| 242 | if (top <= at) | ||
| 243 | return; | ||
| 244 | |||
| 245 | ds->bts_index = ds->bts_buffer_base; | ||
| 246 | |||
| 247 | perf_sample_data_init(&data, 0); | ||
| 248 | data.period = event->hw.last_period; | ||
| 249 | regs.ip = 0; | ||
| 250 | |||
| 251 | /* | ||
| 252 | * Prepare a generic sample, i.e. fill in the invariant fields. | ||
| 253 | * We will overwrite the from and to address before we output | ||
| 254 | * the sample. | ||
| 255 | */ | ||
| 256 | perf_prepare_sample(&header, &data, event, ®s); | ||
| 257 | |||
| 258 | if (perf_output_begin(&handle, event, header.size * (top - at), 1, 1)) | ||
| 259 | return; | ||
| 260 | |||
| 261 | for (; at < top; at++) { | ||
| 262 | data.ip = at->from; | ||
| 263 | data.addr = at->to; | ||
| 264 | |||
| 265 | perf_output_sample(&handle, &header, &data, event); | ||
| 266 | } | ||
| 267 | |||
| 268 | perf_output_end(&handle); | ||
| 269 | |||
| 270 | /* There's new data available. */ | ||
| 271 | event->hw.interrupts++; | ||
| 272 | event->pending_kill = POLL_IN; | ||
| 273 | } | ||
| 274 | |||
| 275 | /* | ||
| 276 | * PEBS | ||
| 277 | */ | ||
| 278 | |||
| 279 | static struct event_constraint intel_core_pebs_events[] = { | ||
| 280 | PEBS_EVENT_CONSTRAINT(0x00c0, 0x1), /* INSTR_RETIRED.ANY */ | ||
| 281 | PEBS_EVENT_CONSTRAINT(0xfec1, 0x1), /* X87_OPS_RETIRED.ANY */ | ||
| 282 | PEBS_EVENT_CONSTRAINT(0x00c5, 0x1), /* BR_INST_RETIRED.MISPRED */ | ||
| 283 | PEBS_EVENT_CONSTRAINT(0x1fc7, 0x1), /* SIMD_INST_RETURED.ANY */ | ||
| 284 | PEBS_EVENT_CONSTRAINT(0x01cb, 0x1), /* MEM_LOAD_RETIRED.L1D_MISS */ | ||
| 285 | PEBS_EVENT_CONSTRAINT(0x02cb, 0x1), /* MEM_LOAD_RETIRED.L1D_LINE_MISS */ | ||
| 286 | PEBS_EVENT_CONSTRAINT(0x04cb, 0x1), /* MEM_LOAD_RETIRED.L2_MISS */ | ||
| 287 | PEBS_EVENT_CONSTRAINT(0x08cb, 0x1), /* MEM_LOAD_RETIRED.L2_LINE_MISS */ | ||
| 288 | PEBS_EVENT_CONSTRAINT(0x10cb, 0x1), /* MEM_LOAD_RETIRED.DTLB_MISS */ | ||
| 289 | EVENT_CONSTRAINT_END | ||
| 290 | }; | ||
| 291 | |||
| 292 | static struct event_constraint intel_nehalem_pebs_events[] = { | ||
| 293 | PEBS_EVENT_CONSTRAINT(0x00c0, 0xf), /* INSTR_RETIRED.ANY */ | ||
| 294 | PEBS_EVENT_CONSTRAINT(0xfec1, 0xf), /* X87_OPS_RETIRED.ANY */ | ||
| 295 | PEBS_EVENT_CONSTRAINT(0x00c5, 0xf), /* BR_INST_RETIRED.MISPRED */ | ||
| 296 | PEBS_EVENT_CONSTRAINT(0x1fc7, 0xf), /* SIMD_INST_RETURED.ANY */ | ||
| 297 | PEBS_EVENT_CONSTRAINT(0x01cb, 0xf), /* MEM_LOAD_RETIRED.L1D_MISS */ | ||
| 298 | PEBS_EVENT_CONSTRAINT(0x02cb, 0xf), /* MEM_LOAD_RETIRED.L1D_LINE_MISS */ | ||
| 299 | PEBS_EVENT_CONSTRAINT(0x04cb, 0xf), /* MEM_LOAD_RETIRED.L2_MISS */ | ||
| 300 | PEBS_EVENT_CONSTRAINT(0x08cb, 0xf), /* MEM_LOAD_RETIRED.L2_LINE_MISS */ | ||
| 301 | PEBS_EVENT_CONSTRAINT(0x10cb, 0xf), /* MEM_LOAD_RETIRED.DTLB_MISS */ | ||
| 302 | EVENT_CONSTRAINT_END | ||
| 303 | }; | ||
| 304 | |||
| 305 | static struct event_constraint * | ||
| 306 | intel_pebs_constraints(struct perf_event *event) | ||
| 307 | { | ||
| 308 | struct event_constraint *c; | ||
| 309 | |||
| 310 | if (!event->attr.precise_ip) | ||
| 311 | return NULL; | ||
| 312 | |||
| 313 | if (x86_pmu.pebs_constraints) { | ||
| 314 | for_each_event_constraint(c, x86_pmu.pebs_constraints) { | ||
| 315 | if ((event->hw.config & c->cmask) == c->code) | ||
| 316 | return c; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | return &emptyconstraint; | ||
| 321 | } | ||
| 322 | |||
| 323 | static void intel_pmu_pebs_enable(struct perf_event *event) | ||
| 324 | { | ||
| 325 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 326 | struct hw_perf_event *hwc = &event->hw; | ||
| 327 | |||
| 328 | hwc->config &= ~ARCH_PERFMON_EVENTSEL_INT; | ||
| 329 | |||
| 330 | cpuc->pebs_enabled |= 1ULL << hwc->idx; | ||
| 331 | WARN_ON_ONCE(cpuc->enabled); | ||
| 332 | |||
| 333 | if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1) | ||
| 334 | intel_pmu_lbr_enable(event); | ||
| 335 | } | ||
| 336 | |||
| 337 | static void intel_pmu_pebs_disable(struct perf_event *event) | ||
| 338 | { | ||
| 339 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 340 | struct hw_perf_event *hwc = &event->hw; | ||
| 341 | |||
| 342 | cpuc->pebs_enabled &= ~(1ULL << hwc->idx); | ||
| 343 | if (cpuc->enabled) | ||
| 344 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); | ||
| 345 | |||
| 346 | hwc->config |= ARCH_PERFMON_EVENTSEL_INT; | ||
| 347 | |||
| 348 | if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1) | ||
| 349 | intel_pmu_lbr_disable(event); | ||
| 350 | } | ||
| 351 | |||
| 352 | static void intel_pmu_pebs_enable_all(void) | ||
| 353 | { | ||
| 354 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 355 | |||
| 356 | if (cpuc->pebs_enabled) | ||
| 357 | wrmsrl(MSR_IA32_PEBS_ENABLE, cpuc->pebs_enabled); | ||
| 358 | } | ||
| 359 | |||
| 360 | static void intel_pmu_pebs_disable_all(void) | ||
| 361 | { | ||
| 362 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 363 | |||
| 364 | if (cpuc->pebs_enabled) | ||
| 365 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); | ||
| 366 | } | ||
| 367 | |||
| 368 | #include <asm/insn.h> | ||
| 369 | |||
| 370 | static inline bool kernel_ip(unsigned long ip) | ||
| 371 | { | ||
| 372 | #ifdef CONFIG_X86_32 | ||
| 373 | return ip > PAGE_OFFSET; | ||
| 374 | #else | ||
| 375 | return (long)ip < 0; | ||
| 376 | #endif | ||
| 377 | } | ||
| 378 | |||
| 379 | static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs) | ||
| 380 | { | ||
| 381 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 382 | unsigned long from = cpuc->lbr_entries[0].from; | ||
| 383 | unsigned long old_to, to = cpuc->lbr_entries[0].to; | ||
| 384 | unsigned long ip = regs->ip; | ||
| 385 | |||
| 386 | /* | ||
| 387 | * We don't need to fixup if the PEBS assist is fault like | ||
| 388 | */ | ||
| 389 | if (!x86_pmu.intel_cap.pebs_trap) | ||
| 390 | return 1; | ||
| 391 | |||
| 392 | /* | ||
| 393 | * No LBR entry, no basic block, no rewinding | ||
| 394 | */ | ||
| 395 | if (!cpuc->lbr_stack.nr || !from || !to) | ||
| 396 | return 0; | ||
| 397 | |||
| 398 | /* | ||
| 399 | * Basic blocks should never cross user/kernel boundaries | ||
| 400 | */ | ||
| 401 | if (kernel_ip(ip) != kernel_ip(to)) | ||
| 402 | return 0; | ||
| 403 | |||
| 404 | /* | ||
| 405 | * unsigned math, either ip is before the start (impossible) or | ||
| 406 | * the basic block is larger than 1 page (sanity) | ||
| 407 | */ | ||
| 408 | if ((ip - to) > PAGE_SIZE) | ||
| 409 | return 0; | ||
| 410 | |||
| 411 | /* | ||
| 412 | * We sampled a branch insn, rewind using the LBR stack | ||
| 413 | */ | ||
| 414 | if (ip == to) { | ||
| 415 | regs->ip = from; | ||
| 416 | return 1; | ||
| 417 | } | ||
| 418 | |||
| 419 | do { | ||
| 420 | struct insn insn; | ||
| 421 | u8 buf[MAX_INSN_SIZE]; | ||
| 422 | void *kaddr; | ||
| 423 | |||
| 424 | old_to = to; | ||
| 425 | if (!kernel_ip(ip)) { | ||
| 426 | int bytes, size = MAX_INSN_SIZE; | ||
| 427 | |||
| 428 | bytes = copy_from_user_nmi(buf, (void __user *)to, size); | ||
| 429 | if (bytes != size) | ||
| 430 | return 0; | ||
| 431 | |||
| 432 | kaddr = buf; | ||
| 433 | } else | ||
| 434 | kaddr = (void *)to; | ||
| 435 | |||
| 436 | kernel_insn_init(&insn, kaddr); | ||
| 437 | insn_get_length(&insn); | ||
| 438 | to += insn.length; | ||
| 439 | } while (to < ip); | ||
| 440 | |||
| 441 | if (to == ip) { | ||
| 442 | regs->ip = old_to; | ||
| 443 | return 1; | ||
| 444 | } | ||
| 445 | |||
| 446 | /* | ||
| 447 | * Even though we decoded the basic block, the instruction stream | ||
| 448 | * never matched the given IP, either the TO or the IP got corrupted. | ||
| 449 | */ | ||
| 450 | return 0; | ||
| 451 | } | ||
| 452 | |||
| 453 | static int intel_pmu_save_and_restart(struct perf_event *event); | ||
| 454 | |||
| 455 | static void __intel_pmu_pebs_event(struct perf_event *event, | ||
| 456 | struct pt_regs *iregs, void *__pebs) | ||
| 457 | { | ||
| 458 | /* | ||
| 459 | * We cast to pebs_record_core since that is a subset of | ||
| 460 | * both formats and we don't use the other fields in this | ||
| 461 | * routine. | ||
| 462 | */ | ||
| 463 | struct pebs_record_core *pebs = __pebs; | ||
| 464 | struct perf_sample_data data; | ||
| 465 | struct pt_regs regs; | ||
| 466 | |||
| 467 | if (!intel_pmu_save_and_restart(event)) | ||
| 468 | return; | ||
| 469 | |||
| 470 | perf_sample_data_init(&data, 0); | ||
| 471 | data.period = event->hw.last_period; | ||
| 472 | |||
| 473 | /* | ||
| 474 | * We use the interrupt regs as a base because the PEBS record | ||
| 475 | * does not contain a full regs set, specifically it seems to | ||
| 476 | * lack segment descriptors, which get used by things like | ||
| 477 | * user_mode(). | ||
| 478 | * | ||
| 479 | * In the simple case fix up only the IP and BP,SP regs, for | ||
| 480 | * PERF_SAMPLE_IP and PERF_SAMPLE_CALLCHAIN to function properly. | ||
| 481 | * A possible PERF_SAMPLE_REGS will have to transfer all regs. | ||
| 482 | */ | ||
| 483 | regs = *iregs; | ||
| 484 | regs.ip = pebs->ip; | ||
| 485 | regs.bp = pebs->bp; | ||
| 486 | regs.sp = pebs->sp; | ||
| 487 | |||
| 488 | if (event->attr.precise_ip > 1 && intel_pmu_pebs_fixup_ip(®s)) | ||
| 489 | regs.flags |= PERF_EFLAGS_EXACT; | ||
| 490 | else | ||
| 491 | regs.flags &= ~PERF_EFLAGS_EXACT; | ||
| 492 | |||
| 493 | if (perf_event_overflow(event, 1, &data, ®s)) | ||
| 494 | x86_pmu_stop(event); | ||
| 495 | } | ||
| 496 | |||
| 497 | static void intel_pmu_drain_pebs_core(struct pt_regs *iregs) | ||
| 498 | { | ||
| 499 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 500 | struct debug_store *ds = cpuc->ds; | ||
| 501 | struct perf_event *event = cpuc->events[0]; /* PMC0 only */ | ||
| 502 | struct pebs_record_core *at, *top; | ||
| 503 | int n; | ||
| 504 | |||
| 505 | if (!ds || !x86_pmu.pebs) | ||
| 506 | return; | ||
| 507 | |||
| 508 | at = (struct pebs_record_core *)(unsigned long)ds->pebs_buffer_base; | ||
| 509 | top = (struct pebs_record_core *)(unsigned long)ds->pebs_index; | ||
| 510 | |||
| 511 | /* | ||
| 512 | * Whatever else happens, drain the thing | ||
| 513 | */ | ||
| 514 | ds->pebs_index = ds->pebs_buffer_base; | ||
| 515 | |||
| 516 | if (!test_bit(0, cpuc->active_mask)) | ||
| 517 | return; | ||
| 518 | |||
| 519 | WARN_ON_ONCE(!event); | ||
| 520 | |||
| 521 | if (!event->attr.precise_ip) | ||
| 522 | return; | ||
| 523 | |||
| 524 | n = top - at; | ||
| 525 | if (n <= 0) | ||
| 526 | return; | ||
| 527 | |||
| 528 | /* | ||
| 529 | * Should not happen, we program the threshold at 1 and do not | ||
| 530 | * set a reset value. | ||
| 531 | */ | ||
| 532 | WARN_ON_ONCE(n > 1); | ||
| 533 | at += n - 1; | ||
| 534 | |||
| 535 | __intel_pmu_pebs_event(event, iregs, at); | ||
| 536 | } | ||
| 537 | |||
| 538 | static void intel_pmu_drain_pebs_nhm(struct pt_regs *iregs) | ||
| 539 | { | ||
| 540 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 541 | struct debug_store *ds = cpuc->ds; | ||
| 542 | struct pebs_record_nhm *at, *top; | ||
| 543 | struct perf_event *event = NULL; | ||
| 544 | u64 status = 0; | ||
| 545 | int bit, n; | ||
| 546 | |||
| 547 | if (!ds || !x86_pmu.pebs) | ||
| 548 | return; | ||
| 549 | |||
| 550 | at = (struct pebs_record_nhm *)(unsigned long)ds->pebs_buffer_base; | ||
| 551 | top = (struct pebs_record_nhm *)(unsigned long)ds->pebs_index; | ||
| 552 | |||
| 553 | ds->pebs_index = ds->pebs_buffer_base; | ||
| 554 | |||
| 555 | n = top - at; | ||
| 556 | if (n <= 0) | ||
| 557 | return; | ||
| 558 | |||
| 559 | /* | ||
| 560 | * Should not happen, we program the threshold at 1 and do not | ||
| 561 | * set a reset value. | ||
| 562 | */ | ||
| 563 | WARN_ON_ONCE(n > MAX_PEBS_EVENTS); | ||
| 564 | |||
| 565 | for ( ; at < top; at++) { | ||
| 566 | for_each_set_bit(bit, (unsigned long *)&at->status, MAX_PEBS_EVENTS) { | ||
| 567 | event = cpuc->events[bit]; | ||
| 568 | if (!test_bit(bit, cpuc->active_mask)) | ||
| 569 | continue; | ||
| 570 | |||
| 571 | WARN_ON_ONCE(!event); | ||
| 572 | |||
| 573 | if (!event->attr.precise_ip) | ||
| 574 | continue; | ||
| 575 | |||
| 576 | if (__test_and_set_bit(bit, (unsigned long *)&status)) | ||
| 577 | continue; | ||
| 578 | |||
| 579 | break; | ||
| 580 | } | ||
| 581 | |||
| 582 | if (!event || bit >= MAX_PEBS_EVENTS) | ||
| 583 | continue; | ||
| 584 | |||
| 585 | __intel_pmu_pebs_event(event, iregs, at); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | /* | ||
| 590 | * BTS, PEBS probe and setup | ||
| 591 | */ | ||
| 592 | |||
| 593 | static void intel_ds_init(void) | ||
| 594 | { | ||
| 595 | /* | ||
| 596 | * No support for 32bit formats | ||
| 597 | */ | ||
| 598 | if (!boot_cpu_has(X86_FEATURE_DTES64)) | ||
| 599 | return; | ||
| 600 | |||
| 601 | x86_pmu.bts = boot_cpu_has(X86_FEATURE_BTS); | ||
| 602 | x86_pmu.pebs = boot_cpu_has(X86_FEATURE_PEBS); | ||
| 603 | if (x86_pmu.pebs) { | ||
| 604 | char pebs_type = x86_pmu.intel_cap.pebs_trap ? '+' : '-'; | ||
| 605 | int format = x86_pmu.intel_cap.pebs_format; | ||
| 606 | |||
| 607 | switch (format) { | ||
| 608 | case 0: | ||
| 609 | printk(KERN_CONT "PEBS fmt0%c, ", pebs_type); | ||
| 610 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_core); | ||
| 611 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_core; | ||
| 612 | x86_pmu.pebs_constraints = intel_core_pebs_events; | ||
| 613 | break; | ||
| 614 | |||
| 615 | case 1: | ||
| 616 | printk(KERN_CONT "PEBS fmt1%c, ", pebs_type); | ||
| 617 | x86_pmu.pebs_record_size = sizeof(struct pebs_record_nhm); | ||
| 618 | x86_pmu.drain_pebs = intel_pmu_drain_pebs_nhm; | ||
| 619 | x86_pmu.pebs_constraints = intel_nehalem_pebs_events; | ||
| 620 | break; | ||
| 621 | |||
| 622 | default: | ||
| 623 | printk(KERN_CONT "no PEBS fmt%d%c, ", format, pebs_type); | ||
| 624 | x86_pmu.pebs = 0; | ||
| 625 | break; | ||
| 626 | } | ||
| 627 | } | ||
| 628 | } | ||
| 629 | |||
| 630 | #else /* CONFIG_CPU_SUP_INTEL */ | ||
| 631 | |||
| 632 | static int reserve_ds_buffers(void) | ||
| 633 | { | ||
| 634 | return 0; | ||
| 635 | } | ||
| 636 | |||
| 637 | static void release_ds_buffers(void) | ||
| 638 | { | ||
| 639 | } | ||
| 640 | |||
| 641 | #endif /* CONFIG_CPU_SUP_INTEL */ | ||
diff --git a/arch/x86/kernel/cpu/perf_event_intel_lbr.c b/arch/x86/kernel/cpu/perf_event_intel_lbr.c new file mode 100644 index 000000000000..d202c1bece1a --- /dev/null +++ b/arch/x86/kernel/cpu/perf_event_intel_lbr.c | |||
| @@ -0,0 +1,218 @@ | |||
| 1 | #ifdef CONFIG_CPU_SUP_INTEL | ||
| 2 | |||
| 3 | enum { | ||
| 4 | LBR_FORMAT_32 = 0x00, | ||
| 5 | LBR_FORMAT_LIP = 0x01, | ||
| 6 | LBR_FORMAT_EIP = 0x02, | ||
| 7 | LBR_FORMAT_EIP_FLAGS = 0x03, | ||
| 8 | }; | ||
| 9 | |||
| 10 | /* | ||
| 11 | * We only support LBR implementations that have FREEZE_LBRS_ON_PMI | ||
| 12 | * otherwise it becomes near impossible to get a reliable stack. | ||
| 13 | */ | ||
| 14 | |||
| 15 | static void __intel_pmu_lbr_enable(void) | ||
| 16 | { | ||
| 17 | u64 debugctl; | ||
| 18 | |||
| 19 | rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); | ||
| 20 | debugctl |= (DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); | ||
| 21 | wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); | ||
| 22 | } | ||
| 23 | |||
| 24 | static void __intel_pmu_lbr_disable(void) | ||
| 25 | { | ||
| 26 | u64 debugctl; | ||
| 27 | |||
| 28 | rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); | ||
| 29 | debugctl &= ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI); | ||
| 30 | wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); | ||
| 31 | } | ||
| 32 | |||
| 33 | static void intel_pmu_lbr_reset_32(void) | ||
| 34 | { | ||
| 35 | int i; | ||
| 36 | |||
| 37 | for (i = 0; i < x86_pmu.lbr_nr; i++) | ||
| 38 | wrmsrl(x86_pmu.lbr_from + i, 0); | ||
| 39 | } | ||
| 40 | |||
| 41 | static void intel_pmu_lbr_reset_64(void) | ||
| 42 | { | ||
| 43 | int i; | ||
| 44 | |||
| 45 | for (i = 0; i < x86_pmu.lbr_nr; i++) { | ||
| 46 | wrmsrl(x86_pmu.lbr_from + i, 0); | ||
| 47 | wrmsrl(x86_pmu.lbr_to + i, 0); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | static void intel_pmu_lbr_reset(void) | ||
| 52 | { | ||
| 53 | if (!x86_pmu.lbr_nr) | ||
| 54 | return; | ||
| 55 | |||
| 56 | if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32) | ||
| 57 | intel_pmu_lbr_reset_32(); | ||
| 58 | else | ||
| 59 | intel_pmu_lbr_reset_64(); | ||
| 60 | } | ||
| 61 | |||
| 62 | static void intel_pmu_lbr_enable(struct perf_event *event) | ||
| 63 | { | ||
| 64 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 65 | |||
| 66 | if (!x86_pmu.lbr_nr) | ||
| 67 | return; | ||
| 68 | |||
| 69 | WARN_ON_ONCE(cpuc->enabled); | ||
| 70 | |||
| 71 | /* | ||
| 72 | * Reset the LBR stack if we changed task context to | ||
| 73 | * avoid data leaks. | ||
| 74 | */ | ||
| 75 | |||
| 76 | if (event->ctx->task && cpuc->lbr_context != event->ctx) { | ||
| 77 | intel_pmu_lbr_reset(); | ||
| 78 | cpuc->lbr_context = event->ctx; | ||
| 79 | } | ||
| 80 | |||
| 81 | cpuc->lbr_users++; | ||
| 82 | } | ||
| 83 | |||
| 84 | static void intel_pmu_lbr_disable(struct perf_event *event) | ||
| 85 | { | ||
| 86 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 87 | |||
| 88 | if (!x86_pmu.lbr_nr) | ||
| 89 | return; | ||
| 90 | |||
| 91 | cpuc->lbr_users--; | ||
| 92 | WARN_ON_ONCE(cpuc->lbr_users < 0); | ||
| 93 | |||
| 94 | if (cpuc->enabled && !cpuc->lbr_users) | ||
| 95 | __intel_pmu_lbr_disable(); | ||
| 96 | } | ||
| 97 | |||
| 98 | static void intel_pmu_lbr_enable_all(void) | ||
| 99 | { | ||
| 100 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 101 | |||
| 102 | if (cpuc->lbr_users) | ||
| 103 | __intel_pmu_lbr_enable(); | ||
| 104 | } | ||
| 105 | |||
| 106 | static void intel_pmu_lbr_disable_all(void) | ||
| 107 | { | ||
| 108 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 109 | |||
| 110 | if (cpuc->lbr_users) | ||
| 111 | __intel_pmu_lbr_disable(); | ||
| 112 | } | ||
| 113 | |||
| 114 | static inline u64 intel_pmu_lbr_tos(void) | ||
| 115 | { | ||
| 116 | u64 tos; | ||
| 117 | |||
| 118 | rdmsrl(x86_pmu.lbr_tos, tos); | ||
| 119 | |||
| 120 | return tos; | ||
| 121 | } | ||
| 122 | |||
| 123 | static void intel_pmu_lbr_read_32(struct cpu_hw_events *cpuc) | ||
| 124 | { | ||
| 125 | unsigned long mask = x86_pmu.lbr_nr - 1; | ||
| 126 | u64 tos = intel_pmu_lbr_tos(); | ||
| 127 | int i; | ||
| 128 | |||
| 129 | for (i = 0; i < x86_pmu.lbr_nr; i++) { | ||
| 130 | unsigned long lbr_idx = (tos - i) & mask; | ||
| 131 | union { | ||
| 132 | struct { | ||
| 133 | u32 from; | ||
| 134 | u32 to; | ||
| 135 | }; | ||
| 136 | u64 lbr; | ||
| 137 | } msr_lastbranch; | ||
| 138 | |||
| 139 | rdmsrl(x86_pmu.lbr_from + lbr_idx, msr_lastbranch.lbr); | ||
| 140 | |||
| 141 | cpuc->lbr_entries[i].from = msr_lastbranch.from; | ||
| 142 | cpuc->lbr_entries[i].to = msr_lastbranch.to; | ||
| 143 | cpuc->lbr_entries[i].flags = 0; | ||
| 144 | } | ||
| 145 | cpuc->lbr_stack.nr = i; | ||
| 146 | } | ||
| 147 | |||
| 148 | #define LBR_FROM_FLAG_MISPRED (1ULL << 63) | ||
| 149 | |||
| 150 | /* | ||
| 151 | * Due to lack of segmentation in Linux the effective address (offset) | ||
| 152 | * is the same as the linear address, allowing us to merge the LIP and EIP | ||
| 153 | * LBR formats. | ||
| 154 | */ | ||
| 155 | static void intel_pmu_lbr_read_64(struct cpu_hw_events *cpuc) | ||
| 156 | { | ||
| 157 | unsigned long mask = x86_pmu.lbr_nr - 1; | ||
| 158 | int lbr_format = x86_pmu.intel_cap.lbr_format; | ||
| 159 | u64 tos = intel_pmu_lbr_tos(); | ||
| 160 | int i; | ||
| 161 | |||
| 162 | for (i = 0; i < x86_pmu.lbr_nr; i++) { | ||
| 163 | unsigned long lbr_idx = (tos - i) & mask; | ||
| 164 | u64 from, to, flags = 0; | ||
| 165 | |||
| 166 | rdmsrl(x86_pmu.lbr_from + lbr_idx, from); | ||
| 167 | rdmsrl(x86_pmu.lbr_to + lbr_idx, to); | ||
| 168 | |||
| 169 | if (lbr_format == LBR_FORMAT_EIP_FLAGS) { | ||
| 170 | flags = !!(from & LBR_FROM_FLAG_MISPRED); | ||
| 171 | from = (u64)((((s64)from) << 1) >> 1); | ||
| 172 | } | ||
| 173 | |||
| 174 | cpuc->lbr_entries[i].from = from; | ||
| 175 | cpuc->lbr_entries[i].to = to; | ||
| 176 | cpuc->lbr_entries[i].flags = flags; | ||
| 177 | } | ||
| 178 | cpuc->lbr_stack.nr = i; | ||
| 179 | } | ||
| 180 | |||
| 181 | static void intel_pmu_lbr_read(void) | ||
| 182 | { | ||
| 183 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 184 | |||
| 185 | if (!cpuc->lbr_users) | ||
| 186 | return; | ||
| 187 | |||
| 188 | if (x86_pmu.intel_cap.lbr_format == LBR_FORMAT_32) | ||
| 189 | intel_pmu_lbr_read_32(cpuc); | ||
| 190 | else | ||
| 191 | intel_pmu_lbr_read_64(cpuc); | ||
| 192 | } | ||
| 193 | |||
| 194 | static void intel_pmu_lbr_init_core(void) | ||
| 195 | { | ||
| 196 | x86_pmu.lbr_nr = 4; | ||
| 197 | x86_pmu.lbr_tos = 0x01c9; | ||
| 198 | x86_pmu.lbr_from = 0x40; | ||
| 199 | x86_pmu.lbr_to = 0x60; | ||
| 200 | } | ||
| 201 | |||
| 202 | static void intel_pmu_lbr_init_nhm(void) | ||
| 203 | { | ||
| 204 | x86_pmu.lbr_nr = 16; | ||
| 205 | x86_pmu.lbr_tos = 0x01c9; | ||
| 206 | x86_pmu.lbr_from = 0x680; | ||
| 207 | x86_pmu.lbr_to = 0x6c0; | ||
| 208 | } | ||
| 209 | |||
| 210 | static void intel_pmu_lbr_init_atom(void) | ||
| 211 | { | ||
| 212 | x86_pmu.lbr_nr = 8; | ||
| 213 | x86_pmu.lbr_tos = 0x01c9; | ||
| 214 | x86_pmu.lbr_from = 0x40; | ||
| 215 | x86_pmu.lbr_to = 0x60; | ||
| 216 | } | ||
| 217 | |||
| 218 | #endif /* CONFIG_CPU_SUP_INTEL */ | ||
diff --git a/arch/x86/kernel/cpu/perf_event_p4.c b/arch/x86/kernel/cpu/perf_event_p4.c new file mode 100644 index 000000000000..424fc8de68e4 --- /dev/null +++ b/arch/x86/kernel/cpu/perf_event_p4.c | |||
| @@ -0,0 +1,857 @@ | |||
| 1 | /* | ||
| 2 | * Netburst Perfomance Events (P4, old Xeon) | ||
| 3 | * | ||
| 4 | * Copyright (C) 2010 Parallels, Inc., Cyrill Gorcunov <gorcunov@openvz.org> | ||
| 5 | * Copyright (C) 2010 Intel Corporation, Lin Ming <ming.m.lin@intel.com> | ||
| 6 | * | ||
| 7 | * For licencing details see kernel-base/COPYING | ||
| 8 | */ | ||
| 9 | |||
| 10 | #ifdef CONFIG_CPU_SUP_INTEL | ||
| 11 | |||
| 12 | #include <asm/perf_event_p4.h> | ||
| 13 | |||
| 14 | #define P4_CNTR_LIMIT 3 | ||
| 15 | /* | ||
| 16 | * array indices: 0,1 - HT threads, used with HT enabled cpu | ||
| 17 | */ | ||
| 18 | struct p4_event_bind { | ||
| 19 | unsigned int opcode; /* Event code and ESCR selector */ | ||
| 20 | unsigned int escr_msr[2]; /* ESCR MSR for this event */ | ||
| 21 | char cntr[2][P4_CNTR_LIMIT]; /* counter index (offset), -1 on abscence */ | ||
| 22 | }; | ||
| 23 | |||
| 24 | struct p4_cache_event_bind { | ||
| 25 | unsigned int metric_pebs; | ||
| 26 | unsigned int metric_vert; | ||
| 27 | }; | ||
| 28 | |||
| 29 | #define P4_GEN_CACHE_EVENT_BIND(name) \ | ||
| 30 | [P4_CACHE__##name] = { \ | ||
| 31 | .metric_pebs = P4_PEBS__##name, \ | ||
| 32 | .metric_vert = P4_VERT__##name, \ | ||
| 33 | } | ||
| 34 | |||
| 35 | static struct p4_cache_event_bind p4_cache_event_bind_map[] = { | ||
| 36 | P4_GEN_CACHE_EVENT_BIND(1stl_cache_load_miss_retired), | ||
| 37 | P4_GEN_CACHE_EVENT_BIND(2ndl_cache_load_miss_retired), | ||
| 38 | P4_GEN_CACHE_EVENT_BIND(dtlb_load_miss_retired), | ||
| 39 | P4_GEN_CACHE_EVENT_BIND(dtlb_store_miss_retired), | ||
| 40 | }; | ||
| 41 | |||
| 42 | /* | ||
| 43 | * Note that we don't use CCCR1 here, there is an | ||
| 44 | * exception for P4_BSQ_ALLOCATION but we just have | ||
| 45 | * no workaround | ||
| 46 | * | ||
| 47 | * consider this binding as resources which particular | ||
| 48 | * event may borrow, it doesn't contain EventMask, | ||
| 49 | * Tags and friends -- they are left to a caller | ||
| 50 | */ | ||
| 51 | static struct p4_event_bind p4_event_bind_map[] = { | ||
| 52 | [P4_EVENT_TC_DELIVER_MODE] = { | ||
| 53 | .opcode = P4_OPCODE(P4_EVENT_TC_DELIVER_MODE), | ||
| 54 | .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 }, | ||
| 55 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 56 | }, | ||
| 57 | [P4_EVENT_BPU_FETCH_REQUEST] = { | ||
| 58 | .opcode = P4_OPCODE(P4_EVENT_BPU_FETCH_REQUEST), | ||
| 59 | .escr_msr = { MSR_P4_BPU_ESCR0, MSR_P4_BPU_ESCR1 }, | ||
| 60 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 61 | }, | ||
| 62 | [P4_EVENT_ITLB_REFERENCE] = { | ||
| 63 | .opcode = P4_OPCODE(P4_EVENT_ITLB_REFERENCE), | ||
| 64 | .escr_msr = { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 }, | ||
| 65 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 66 | }, | ||
| 67 | [P4_EVENT_MEMORY_CANCEL] = { | ||
| 68 | .opcode = P4_OPCODE(P4_EVENT_MEMORY_CANCEL), | ||
| 69 | .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 }, | ||
| 70 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 71 | }, | ||
| 72 | [P4_EVENT_MEMORY_COMPLETE] = { | ||
| 73 | .opcode = P4_OPCODE(P4_EVENT_MEMORY_COMPLETE), | ||
| 74 | .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 }, | ||
| 75 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 76 | }, | ||
| 77 | [P4_EVENT_LOAD_PORT_REPLAY] = { | ||
| 78 | .opcode = P4_OPCODE(P4_EVENT_LOAD_PORT_REPLAY), | ||
| 79 | .escr_msr = { MSR_P4_SAAT_ESCR0, MSR_P4_SAAT_ESCR1 }, | ||
| 80 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 81 | }, | ||
| 82 | [P4_EVENT_STORE_PORT_REPLAY] = { | ||
| 83 | .opcode = P4_OPCODE(P4_EVENT_STORE_PORT_REPLAY), | ||
| 84 | .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 }, | ||
| 85 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 86 | }, | ||
| 87 | [P4_EVENT_MOB_LOAD_REPLAY] = { | ||
| 88 | .opcode = P4_OPCODE(P4_EVENT_MOB_LOAD_REPLAY), | ||
| 89 | .escr_msr = { MSR_P4_MOB_ESCR0, MSR_P4_MOB_ESCR1 }, | ||
| 90 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 91 | }, | ||
| 92 | [P4_EVENT_PAGE_WALK_TYPE] = { | ||
| 93 | .opcode = P4_OPCODE(P4_EVENT_PAGE_WALK_TYPE), | ||
| 94 | .escr_msr = { MSR_P4_PMH_ESCR0, MSR_P4_PMH_ESCR1 }, | ||
| 95 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 96 | }, | ||
| 97 | [P4_EVENT_BSQ_CACHE_REFERENCE] = { | ||
| 98 | .opcode = P4_OPCODE(P4_EVENT_BSQ_CACHE_REFERENCE), | ||
| 99 | .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 }, | ||
| 100 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 101 | }, | ||
| 102 | [P4_EVENT_IOQ_ALLOCATION] = { | ||
| 103 | .opcode = P4_OPCODE(P4_EVENT_IOQ_ALLOCATION), | ||
| 104 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 105 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 106 | }, | ||
| 107 | [P4_EVENT_IOQ_ACTIVE_ENTRIES] = { /* shared ESCR */ | ||
| 108 | .opcode = P4_OPCODE(P4_EVENT_IOQ_ACTIVE_ENTRIES), | ||
| 109 | .escr_msr = { MSR_P4_FSB_ESCR1, MSR_P4_FSB_ESCR1 }, | ||
| 110 | .cntr = { {2, -1, -1}, {3, -1, -1} }, | ||
| 111 | }, | ||
| 112 | [P4_EVENT_FSB_DATA_ACTIVITY] = { | ||
| 113 | .opcode = P4_OPCODE(P4_EVENT_FSB_DATA_ACTIVITY), | ||
| 114 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 115 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 116 | }, | ||
| 117 | [P4_EVENT_BSQ_ALLOCATION] = { /* shared ESCR, broken CCCR1 */ | ||
| 118 | .opcode = P4_OPCODE(P4_EVENT_BSQ_ALLOCATION), | ||
| 119 | .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR0 }, | ||
| 120 | .cntr = { {0, -1, -1}, {1, -1, -1} }, | ||
| 121 | }, | ||
| 122 | [P4_EVENT_BSQ_ACTIVE_ENTRIES] = { /* shared ESCR */ | ||
| 123 | .opcode = P4_OPCODE(P4_EVENT_BSQ_ACTIVE_ENTRIES), | ||
| 124 | .escr_msr = { MSR_P4_BSU_ESCR1 , MSR_P4_BSU_ESCR1 }, | ||
| 125 | .cntr = { {2, -1, -1}, {3, -1, -1} }, | ||
| 126 | }, | ||
| 127 | [P4_EVENT_SSE_INPUT_ASSIST] = { | ||
| 128 | .opcode = P4_OPCODE(P4_EVENT_SSE_INPUT_ASSIST), | ||
| 129 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 130 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 131 | }, | ||
| 132 | [P4_EVENT_PACKED_SP_UOP] = { | ||
| 133 | .opcode = P4_OPCODE(P4_EVENT_PACKED_SP_UOP), | ||
| 134 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 135 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 136 | }, | ||
| 137 | [P4_EVENT_PACKED_DP_UOP] = { | ||
| 138 | .opcode = P4_OPCODE(P4_EVENT_PACKED_DP_UOP), | ||
| 139 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 140 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 141 | }, | ||
| 142 | [P4_EVENT_SCALAR_SP_UOP] = { | ||
| 143 | .opcode = P4_OPCODE(P4_EVENT_SCALAR_SP_UOP), | ||
| 144 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 145 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 146 | }, | ||
| 147 | [P4_EVENT_SCALAR_DP_UOP] = { | ||
| 148 | .opcode = P4_OPCODE(P4_EVENT_SCALAR_DP_UOP), | ||
| 149 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 150 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 151 | }, | ||
| 152 | [P4_EVENT_64BIT_MMX_UOP] = { | ||
| 153 | .opcode = P4_OPCODE(P4_EVENT_64BIT_MMX_UOP), | ||
| 154 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 155 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 156 | }, | ||
| 157 | [P4_EVENT_128BIT_MMX_UOP] = { | ||
| 158 | .opcode = P4_OPCODE(P4_EVENT_128BIT_MMX_UOP), | ||
| 159 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 160 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 161 | }, | ||
| 162 | [P4_EVENT_X87_FP_UOP] = { | ||
| 163 | .opcode = P4_OPCODE(P4_EVENT_X87_FP_UOP), | ||
| 164 | .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 }, | ||
| 165 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 166 | }, | ||
| 167 | [P4_EVENT_TC_MISC] = { | ||
| 168 | .opcode = P4_OPCODE(P4_EVENT_TC_MISC), | ||
| 169 | .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 }, | ||
| 170 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 171 | }, | ||
| 172 | [P4_EVENT_GLOBAL_POWER_EVENTS] = { | ||
| 173 | .opcode = P4_OPCODE(P4_EVENT_GLOBAL_POWER_EVENTS), | ||
| 174 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 175 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 176 | }, | ||
| 177 | [P4_EVENT_TC_MS_XFER] = { | ||
| 178 | .opcode = P4_OPCODE(P4_EVENT_TC_MS_XFER), | ||
| 179 | .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 }, | ||
| 180 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 181 | }, | ||
| 182 | [P4_EVENT_UOP_QUEUE_WRITES] = { | ||
| 183 | .opcode = P4_OPCODE(P4_EVENT_UOP_QUEUE_WRITES), | ||
| 184 | .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 }, | ||
| 185 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 186 | }, | ||
| 187 | [P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE] = { | ||
| 188 | .opcode = P4_OPCODE(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE), | ||
| 189 | .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR0 }, | ||
| 190 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 191 | }, | ||
| 192 | [P4_EVENT_RETIRED_BRANCH_TYPE] = { | ||
| 193 | .opcode = P4_OPCODE(P4_EVENT_RETIRED_BRANCH_TYPE), | ||
| 194 | .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR1 }, | ||
| 195 | .cntr = { {4, 5, -1}, {6, 7, -1} }, | ||
| 196 | }, | ||
| 197 | [P4_EVENT_RESOURCE_STALL] = { | ||
| 198 | .opcode = P4_OPCODE(P4_EVENT_RESOURCE_STALL), | ||
| 199 | .escr_msr = { MSR_P4_ALF_ESCR0, MSR_P4_ALF_ESCR1 }, | ||
| 200 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 201 | }, | ||
| 202 | [P4_EVENT_WC_BUFFER] = { | ||
| 203 | .opcode = P4_OPCODE(P4_EVENT_WC_BUFFER), | ||
| 204 | .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 }, | ||
| 205 | .cntr = { {8, 9, -1}, {10, 11, -1} }, | ||
| 206 | }, | ||
| 207 | [P4_EVENT_B2B_CYCLES] = { | ||
| 208 | .opcode = P4_OPCODE(P4_EVENT_B2B_CYCLES), | ||
| 209 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 210 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 211 | }, | ||
| 212 | [P4_EVENT_BNR] = { | ||
| 213 | .opcode = P4_OPCODE(P4_EVENT_BNR), | ||
| 214 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 215 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 216 | }, | ||
| 217 | [P4_EVENT_SNOOP] = { | ||
| 218 | .opcode = P4_OPCODE(P4_EVENT_SNOOP), | ||
| 219 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 220 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 221 | }, | ||
| 222 | [P4_EVENT_RESPONSE] = { | ||
| 223 | .opcode = P4_OPCODE(P4_EVENT_RESPONSE), | ||
| 224 | .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 }, | ||
| 225 | .cntr = { {0, -1, -1}, {2, -1, -1} }, | ||
| 226 | }, | ||
| 227 | [P4_EVENT_FRONT_END_EVENT] = { | ||
| 228 | .opcode = P4_OPCODE(P4_EVENT_FRONT_END_EVENT), | ||
| 229 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 230 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 231 | }, | ||
| 232 | [P4_EVENT_EXECUTION_EVENT] = { | ||
| 233 | .opcode = P4_OPCODE(P4_EVENT_EXECUTION_EVENT), | ||
| 234 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 235 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 236 | }, | ||
| 237 | [P4_EVENT_REPLAY_EVENT] = { | ||
| 238 | .opcode = P4_OPCODE(P4_EVENT_REPLAY_EVENT), | ||
| 239 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 240 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 241 | }, | ||
| 242 | [P4_EVENT_INSTR_RETIRED] = { | ||
| 243 | .opcode = P4_OPCODE(P4_EVENT_INSTR_RETIRED), | ||
| 244 | .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 }, | ||
| 245 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 246 | }, | ||
| 247 | [P4_EVENT_UOPS_RETIRED] = { | ||
| 248 | .opcode = P4_OPCODE(P4_EVENT_UOPS_RETIRED), | ||
| 249 | .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 }, | ||
| 250 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 251 | }, | ||
| 252 | [P4_EVENT_UOP_TYPE] = { | ||
| 253 | .opcode = P4_OPCODE(P4_EVENT_UOP_TYPE), | ||
| 254 | .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 }, | ||
| 255 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 256 | }, | ||
| 257 | [P4_EVENT_BRANCH_RETIRED] = { | ||
| 258 | .opcode = P4_OPCODE(P4_EVENT_BRANCH_RETIRED), | ||
| 259 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 260 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 261 | }, | ||
| 262 | [P4_EVENT_MISPRED_BRANCH_RETIRED] = { | ||
| 263 | .opcode = P4_OPCODE(P4_EVENT_MISPRED_BRANCH_RETIRED), | ||
| 264 | .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 }, | ||
| 265 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 266 | }, | ||
| 267 | [P4_EVENT_X87_ASSIST] = { | ||
| 268 | .opcode = P4_OPCODE(P4_EVENT_X87_ASSIST), | ||
| 269 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 270 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 271 | }, | ||
| 272 | [P4_EVENT_MACHINE_CLEAR] = { | ||
| 273 | .opcode = P4_OPCODE(P4_EVENT_MACHINE_CLEAR), | ||
| 274 | .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 }, | ||
| 275 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 276 | }, | ||
| 277 | [P4_EVENT_INSTR_COMPLETED] = { | ||
| 278 | .opcode = P4_OPCODE(P4_EVENT_INSTR_COMPLETED), | ||
| 279 | .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 }, | ||
| 280 | .cntr = { {12, 13, 16}, {14, 15, 17} }, | ||
| 281 | }, | ||
| 282 | }; | ||
| 283 | |||
| 284 | #define P4_GEN_CACHE_EVENT(event, bit, cache_event) \ | ||
| 285 | p4_config_pack_escr(P4_ESCR_EVENT(event) | \ | ||
| 286 | P4_ESCR_EMASK_BIT(event, bit)) | \ | ||
| 287 | p4_config_pack_cccr(cache_event | \ | ||
| 288 | P4_CCCR_ESEL(P4_OPCODE_ESEL(P4_OPCODE(event)))) | ||
| 289 | |||
| 290 | static __initconst const u64 p4_hw_cache_event_ids | ||
| 291 | [PERF_COUNT_HW_CACHE_MAX] | ||
| 292 | [PERF_COUNT_HW_CACHE_OP_MAX] | ||
| 293 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | ||
| 294 | { | ||
| 295 | [ C(L1D ) ] = { | ||
| 296 | [ C(OP_READ) ] = { | ||
| 297 | [ C(RESULT_ACCESS) ] = 0x0, | ||
| 298 | [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS, | ||
| 299 | P4_CACHE__1stl_cache_load_miss_retired), | ||
| 300 | }, | ||
| 301 | }, | ||
| 302 | [ C(LL ) ] = { | ||
| 303 | [ C(OP_READ) ] = { | ||
| 304 | [ C(RESULT_ACCESS) ] = 0x0, | ||
| 305 | [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS, | ||
| 306 | P4_CACHE__2ndl_cache_load_miss_retired), | ||
| 307 | }, | ||
| 308 | }, | ||
| 309 | [ C(DTLB) ] = { | ||
| 310 | [ C(OP_READ) ] = { | ||
| 311 | [ C(RESULT_ACCESS) ] = 0x0, | ||
| 312 | [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS, | ||
| 313 | P4_CACHE__dtlb_load_miss_retired), | ||
| 314 | }, | ||
| 315 | [ C(OP_WRITE) ] = { | ||
| 316 | [ C(RESULT_ACCESS) ] = 0x0, | ||
| 317 | [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS, | ||
| 318 | P4_CACHE__dtlb_store_miss_retired), | ||
| 319 | }, | ||
| 320 | }, | ||
| 321 | [ C(ITLB) ] = { | ||
| 322 | [ C(OP_READ) ] = { | ||
| 323 | [ C(RESULT_ACCESS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, HIT, | ||
| 324 | P4_CACHE__itlb_reference_hit), | ||
| 325 | [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, MISS, | ||
| 326 | P4_CACHE__itlb_reference_miss), | ||
| 327 | }, | ||
| 328 | [ C(OP_WRITE) ] = { | ||
| 329 | [ C(RESULT_ACCESS) ] = -1, | ||
| 330 | [ C(RESULT_MISS) ] = -1, | ||
| 331 | }, | ||
| 332 | [ C(OP_PREFETCH) ] = { | ||
| 333 | [ C(RESULT_ACCESS) ] = -1, | ||
| 334 | [ C(RESULT_MISS) ] = -1, | ||
| 335 | }, | ||
| 336 | }, | ||
| 337 | }; | ||
| 338 | |||
| 339 | static u64 p4_general_events[PERF_COUNT_HW_MAX] = { | ||
| 340 | /* non-halted CPU clocks */ | ||
| 341 | [PERF_COUNT_HW_CPU_CYCLES] = | ||
| 342 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) | | ||
| 343 | P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)), | ||
| 344 | |||
| 345 | /* | ||
| 346 | * retired instructions | ||
| 347 | * in a sake of simplicity we don't use the FSB tagging | ||
| 348 | */ | ||
| 349 | [PERF_COUNT_HW_INSTRUCTIONS] = | ||
| 350 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_INSTR_RETIRED) | | ||
| 351 | P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) | | ||
| 352 | P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG)), | ||
| 353 | |||
| 354 | /* cache hits */ | ||
| 355 | [PERF_COUNT_HW_CACHE_REFERENCES] = | ||
| 356 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) | | ||
| 357 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) | | ||
| 358 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) | | ||
| 359 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) | | ||
| 360 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) | | ||
| 361 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) | | ||
| 362 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM)), | ||
| 363 | |||
| 364 | /* cache misses */ | ||
| 365 | [PERF_COUNT_HW_CACHE_MISSES] = | ||
| 366 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) | | ||
| 367 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) | | ||
| 368 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) | | ||
| 369 | P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS)), | ||
| 370 | |||
| 371 | /* branch instructions retired */ | ||
| 372 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = | ||
| 373 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_RETIRED_BRANCH_TYPE) | | ||
| 374 | P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) | | ||
| 375 | P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) | | ||
| 376 | P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) | | ||
| 377 | P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT)), | ||
| 378 | |||
| 379 | /* mispredicted branches retired */ | ||
| 380 | [PERF_COUNT_HW_BRANCH_MISSES] = | ||
| 381 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_MISPRED_BRANCH_RETIRED) | | ||
| 382 | P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS)), | ||
| 383 | |||
| 384 | /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */ | ||
| 385 | [PERF_COUNT_HW_BUS_CYCLES] = | ||
| 386 | p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_FSB_DATA_ACTIVITY) | | ||
| 387 | P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) | | ||
| 388 | P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN)) | | ||
| 389 | p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE), | ||
| 390 | }; | ||
| 391 | |||
| 392 | static struct p4_event_bind *p4_config_get_bind(u64 config) | ||
| 393 | { | ||
| 394 | unsigned int evnt = p4_config_unpack_event(config); | ||
| 395 | struct p4_event_bind *bind = NULL; | ||
| 396 | |||
| 397 | if (evnt < ARRAY_SIZE(p4_event_bind_map)) | ||
| 398 | bind = &p4_event_bind_map[evnt]; | ||
| 399 | |||
| 400 | return bind; | ||
| 401 | } | ||
| 402 | |||
| 403 | static u64 p4_pmu_event_map(int hw_event) | ||
| 404 | { | ||
| 405 | struct p4_event_bind *bind; | ||
| 406 | unsigned int esel; | ||
| 407 | u64 config; | ||
| 408 | |||
| 409 | config = p4_general_events[hw_event]; | ||
| 410 | bind = p4_config_get_bind(config); | ||
| 411 | esel = P4_OPCODE_ESEL(bind->opcode); | ||
| 412 | config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel)); | ||
| 413 | |||
| 414 | return config; | ||
| 415 | } | ||
| 416 | |||
| 417 | static int p4_hw_config(struct perf_event *event) | ||
| 418 | { | ||
| 419 | int cpu = get_cpu(); | ||
| 420 | int rc = 0; | ||
| 421 | unsigned int evnt; | ||
| 422 | u32 escr, cccr; | ||
| 423 | |||
| 424 | /* | ||
| 425 | * the reason we use cpu that early is that: if we get scheduled | ||
| 426 | * first time on the same cpu -- we will not need swap thread | ||
| 427 | * specific flags in config (and will save some cpu cycles) | ||
| 428 | */ | ||
| 429 | |||
| 430 | cccr = p4_default_cccr_conf(cpu); | ||
| 431 | escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel, | ||
| 432 | event->attr.exclude_user); | ||
| 433 | event->hw.config = p4_config_pack_escr(escr) | | ||
| 434 | p4_config_pack_cccr(cccr); | ||
| 435 | |||
| 436 | if (p4_ht_active() && p4_ht_thread(cpu)) | ||
| 437 | event->hw.config = p4_set_ht_bit(event->hw.config); | ||
| 438 | |||
| 439 | if (event->attr.type == PERF_TYPE_RAW) { | ||
| 440 | |||
| 441 | /* user data may have out-of-bound event index */ | ||
| 442 | evnt = p4_config_unpack_event(event->attr.config); | ||
| 443 | if (evnt >= ARRAY_SIZE(p4_event_bind_map)) { | ||
| 444 | rc = -EINVAL; | ||
| 445 | goto out; | ||
| 446 | } | ||
| 447 | |||
| 448 | /* | ||
| 449 | * We don't control raw events so it's up to the caller | ||
| 450 | * to pass sane values (and we don't count the thread number | ||
| 451 | * on HT machine but allow HT-compatible specifics to be | ||
| 452 | * passed on) | ||
| 453 | * | ||
| 454 | * XXX: HT wide things should check perf_paranoid_cpu() && | ||
| 455 | * CAP_SYS_ADMIN | ||
| 456 | */ | ||
| 457 | event->hw.config |= event->attr.config & | ||
| 458 | (p4_config_pack_escr(P4_ESCR_MASK_HT) | | ||
| 459 | p4_config_pack_cccr(P4_CCCR_MASK_HT)); | ||
| 460 | } | ||
| 461 | |||
| 462 | rc = x86_setup_perfctr(event); | ||
| 463 | out: | ||
| 464 | put_cpu(); | ||
| 465 | return rc; | ||
| 466 | } | ||
| 467 | |||
| 468 | static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc) | ||
| 469 | { | ||
| 470 | unsigned long dummy; | ||
| 471 | |||
| 472 | rdmsrl(hwc->config_base + hwc->idx, dummy); | ||
| 473 | if (dummy & P4_CCCR_OVF) { | ||
| 474 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, | ||
| 475 | ((u64)dummy) & ~P4_CCCR_OVF); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | static inline void p4_pmu_disable_event(struct perf_event *event) | ||
| 480 | { | ||
| 481 | struct hw_perf_event *hwc = &event->hw; | ||
| 482 | |||
| 483 | /* | ||
| 484 | * If event gets disabled while counter is in overflowed | ||
| 485 | * state we need to clear P4_CCCR_OVF, otherwise interrupt get | ||
| 486 | * asserted again and again | ||
| 487 | */ | ||
| 488 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, | ||
| 489 | (u64)(p4_config_unpack_cccr(hwc->config)) & | ||
| 490 | ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED); | ||
| 491 | } | ||
| 492 | |||
| 493 | static void p4_pmu_disable_all(void) | ||
| 494 | { | ||
| 495 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 496 | int idx; | ||
| 497 | |||
| 498 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { | ||
| 499 | struct perf_event *event = cpuc->events[idx]; | ||
| 500 | if (!test_bit(idx, cpuc->active_mask)) | ||
| 501 | continue; | ||
| 502 | p4_pmu_disable_event(event); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | static void p4_pmu_enable_event(struct perf_event *event) | ||
| 507 | { | ||
| 508 | struct hw_perf_event *hwc = &event->hw; | ||
| 509 | int thread = p4_ht_config_thread(hwc->config); | ||
| 510 | u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config)); | ||
| 511 | unsigned int idx = p4_config_unpack_event(hwc->config); | ||
| 512 | unsigned int idx_cache = p4_config_unpack_cache_event(hwc->config); | ||
| 513 | struct p4_event_bind *bind; | ||
| 514 | struct p4_cache_event_bind *bind_cache; | ||
| 515 | u64 escr_addr, cccr; | ||
| 516 | |||
| 517 | bind = &p4_event_bind_map[idx]; | ||
| 518 | escr_addr = (u64)bind->escr_msr[thread]; | ||
| 519 | |||
| 520 | /* | ||
| 521 | * - we dont support cascaded counters yet | ||
| 522 | * - and counter 1 is broken (erratum) | ||
| 523 | */ | ||
| 524 | WARN_ON_ONCE(p4_is_event_cascaded(hwc->config)); | ||
| 525 | WARN_ON_ONCE(hwc->idx == 1); | ||
| 526 | |||
| 527 | /* we need a real Event value */ | ||
| 528 | escr_conf &= ~P4_ESCR_EVENT_MASK; | ||
| 529 | escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode)); | ||
| 530 | |||
| 531 | cccr = p4_config_unpack_cccr(hwc->config); | ||
| 532 | |||
| 533 | /* | ||
| 534 | * it could be Cache event so that we need to | ||
| 535 | * set metrics into additional MSRs | ||
| 536 | */ | ||
| 537 | BUILD_BUG_ON(P4_CACHE__MAX > P4_CCCR_CACHE_OPS_MASK); | ||
| 538 | if (idx_cache > P4_CACHE__NONE && | ||
| 539 | idx_cache < ARRAY_SIZE(p4_cache_event_bind_map)) { | ||
| 540 | bind_cache = &p4_cache_event_bind_map[idx_cache]; | ||
| 541 | (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind_cache->metric_pebs); | ||
| 542 | (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind_cache->metric_vert); | ||
| 543 | } | ||
| 544 | |||
| 545 | (void)checking_wrmsrl(escr_addr, escr_conf); | ||
| 546 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, | ||
| 547 | (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE); | ||
| 548 | } | ||
| 549 | |||
| 550 | static void p4_pmu_enable_all(int added) | ||
| 551 | { | ||
| 552 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 553 | int idx; | ||
| 554 | |||
| 555 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { | ||
| 556 | struct perf_event *event = cpuc->events[idx]; | ||
| 557 | if (!test_bit(idx, cpuc->active_mask)) | ||
| 558 | continue; | ||
| 559 | p4_pmu_enable_event(event); | ||
| 560 | } | ||
| 561 | } | ||
| 562 | |||
| 563 | static int p4_pmu_handle_irq(struct pt_regs *regs) | ||
| 564 | { | ||
| 565 | struct perf_sample_data data; | ||
| 566 | struct cpu_hw_events *cpuc; | ||
| 567 | struct perf_event *event; | ||
| 568 | struct hw_perf_event *hwc; | ||
| 569 | int idx, handled = 0; | ||
| 570 | u64 val; | ||
| 571 | |||
| 572 | data.addr = 0; | ||
| 573 | data.raw = NULL; | ||
| 574 | |||
| 575 | cpuc = &__get_cpu_var(cpu_hw_events); | ||
| 576 | |||
| 577 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { | ||
| 578 | |||
| 579 | if (!test_bit(idx, cpuc->active_mask)) | ||
| 580 | continue; | ||
| 581 | |||
| 582 | event = cpuc->events[idx]; | ||
| 583 | hwc = &event->hw; | ||
| 584 | |||
| 585 | WARN_ON_ONCE(hwc->idx != idx); | ||
| 586 | |||
| 587 | /* | ||
| 588 | * FIXME: Redundant call, actually not needed | ||
| 589 | * but just to check if we're screwed | ||
| 590 | */ | ||
| 591 | p4_pmu_clear_cccr_ovf(hwc); | ||
| 592 | |||
| 593 | val = x86_perf_event_update(event); | ||
| 594 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) | ||
| 595 | continue; | ||
| 596 | |||
| 597 | /* | ||
| 598 | * event overflow | ||
| 599 | */ | ||
| 600 | handled = 1; | ||
| 601 | data.period = event->hw.last_period; | ||
| 602 | |||
| 603 | if (!x86_perf_event_set_period(event)) | ||
| 604 | continue; | ||
| 605 | if (perf_event_overflow(event, 1, &data, regs)) | ||
| 606 | p4_pmu_disable_event(event); | ||
| 607 | } | ||
| 608 | |||
| 609 | if (handled) { | ||
| 610 | /* p4 quirk: unmask it again */ | ||
| 611 | apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED); | ||
| 612 | inc_irq_stat(apic_perf_irqs); | ||
| 613 | } | ||
| 614 | |||
| 615 | return handled; | ||
| 616 | } | ||
| 617 | |||
| 618 | /* | ||
| 619 | * swap thread specific fields according to a thread | ||
| 620 | * we are going to run on | ||
| 621 | */ | ||
| 622 | static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu) | ||
| 623 | { | ||
| 624 | u32 escr, cccr; | ||
| 625 | |||
| 626 | /* | ||
| 627 | * we either lucky and continue on same cpu or no HT support | ||
| 628 | */ | ||
| 629 | if (!p4_should_swap_ts(hwc->config, cpu)) | ||
| 630 | return; | ||
| 631 | |||
| 632 | /* | ||
| 633 | * the event is migrated from an another logical | ||
| 634 | * cpu, so we need to swap thread specific flags | ||
| 635 | */ | ||
| 636 | |||
| 637 | escr = p4_config_unpack_escr(hwc->config); | ||
| 638 | cccr = p4_config_unpack_cccr(hwc->config); | ||
| 639 | |||
| 640 | if (p4_ht_thread(cpu)) { | ||
| 641 | cccr &= ~P4_CCCR_OVF_PMI_T0; | ||
| 642 | cccr |= P4_CCCR_OVF_PMI_T1; | ||
| 643 | if (escr & P4_ESCR_T0_OS) { | ||
| 644 | escr &= ~P4_ESCR_T0_OS; | ||
| 645 | escr |= P4_ESCR_T1_OS; | ||
| 646 | } | ||
| 647 | if (escr & P4_ESCR_T0_USR) { | ||
| 648 | escr &= ~P4_ESCR_T0_USR; | ||
| 649 | escr |= P4_ESCR_T1_USR; | ||
| 650 | } | ||
| 651 | hwc->config = p4_config_pack_escr(escr); | ||
| 652 | hwc->config |= p4_config_pack_cccr(cccr); | ||
| 653 | hwc->config |= P4_CONFIG_HT; | ||
| 654 | } else { | ||
| 655 | cccr &= ~P4_CCCR_OVF_PMI_T1; | ||
| 656 | cccr |= P4_CCCR_OVF_PMI_T0; | ||
| 657 | if (escr & P4_ESCR_T1_OS) { | ||
| 658 | escr &= ~P4_ESCR_T1_OS; | ||
| 659 | escr |= P4_ESCR_T0_OS; | ||
| 660 | } | ||
| 661 | if (escr & P4_ESCR_T1_USR) { | ||
| 662 | escr &= ~P4_ESCR_T1_USR; | ||
| 663 | escr |= P4_ESCR_T0_USR; | ||
| 664 | } | ||
| 665 | hwc->config = p4_config_pack_escr(escr); | ||
| 666 | hwc->config |= p4_config_pack_cccr(cccr); | ||
| 667 | hwc->config &= ~P4_CONFIG_HT; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | /* | ||
| 672 | * ESCR address hashing is tricky, ESCRs are not sequential | ||
| 673 | * in memory but all starts from MSR_P4_BSU_ESCR0 (0x03e0) and | ||
| 674 | * the metric between any ESCRs is laid in range [0xa0,0xe1] | ||
| 675 | * | ||
| 676 | * so we make ~70% filled hashtable | ||
| 677 | */ | ||
| 678 | |||
| 679 | #define P4_ESCR_MSR_BASE 0x000003a0 | ||
| 680 | #define P4_ESCR_MSR_MAX 0x000003e1 | ||
| 681 | #define P4_ESCR_MSR_TABLE_SIZE (P4_ESCR_MSR_MAX - P4_ESCR_MSR_BASE + 1) | ||
| 682 | #define P4_ESCR_MSR_IDX(msr) (msr - P4_ESCR_MSR_BASE) | ||
| 683 | #define P4_ESCR_MSR_TABLE_ENTRY(msr) [P4_ESCR_MSR_IDX(msr)] = msr | ||
| 684 | |||
| 685 | static const unsigned int p4_escr_table[P4_ESCR_MSR_TABLE_SIZE] = { | ||
| 686 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR0), | ||
| 687 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR1), | ||
| 688 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR0), | ||
| 689 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR1), | ||
| 690 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR0), | ||
| 691 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR1), | ||
| 692 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR0), | ||
| 693 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR1), | ||
| 694 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR2), | ||
| 695 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR3), | ||
| 696 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR4), | ||
| 697 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR5), | ||
| 698 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR0), | ||
| 699 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR1), | ||
| 700 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR0), | ||
| 701 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR1), | ||
| 702 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR0), | ||
| 703 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR1), | ||
| 704 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR0), | ||
| 705 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR1), | ||
| 706 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR0), | ||
| 707 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR1), | ||
| 708 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR0), | ||
| 709 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR1), | ||
| 710 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR0), | ||
| 711 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR1), | ||
| 712 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR0), | ||
| 713 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR1), | ||
| 714 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR0), | ||
| 715 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR1), | ||
| 716 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR0), | ||
| 717 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR1), | ||
| 718 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR0), | ||
| 719 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR1), | ||
| 720 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR0), | ||
| 721 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR1), | ||
| 722 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR0), | ||
| 723 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR1), | ||
| 724 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR0), | ||
| 725 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR1), | ||
| 726 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR0), | ||
| 727 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR1), | ||
| 728 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR0), | ||
| 729 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR1), | ||
| 730 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR0), | ||
| 731 | P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR1), | ||
| 732 | }; | ||
| 733 | |||
| 734 | static int p4_get_escr_idx(unsigned int addr) | ||
| 735 | { | ||
| 736 | unsigned int idx = P4_ESCR_MSR_IDX(addr); | ||
| 737 | |||
| 738 | if (unlikely(idx >= P4_ESCR_MSR_TABLE_SIZE || | ||
| 739 | !p4_escr_table[idx])) { | ||
| 740 | WARN_ONCE(1, "P4 PMU: Wrong address passed: %x\n", addr); | ||
| 741 | return -1; | ||
| 742 | } | ||
| 743 | |||
| 744 | return idx; | ||
| 745 | } | ||
| 746 | |||
| 747 | static int p4_next_cntr(int thread, unsigned long *used_mask, | ||
| 748 | struct p4_event_bind *bind) | ||
| 749 | { | ||
| 750 | int i, j; | ||
| 751 | |||
| 752 | for (i = 0; i < P4_CNTR_LIMIT; i++) { | ||
| 753 | j = bind->cntr[thread][i]; | ||
| 754 | if (j != -1 && !test_bit(j, used_mask)) | ||
| 755 | return j; | ||
| 756 | } | ||
| 757 | |||
| 758 | return -1; | ||
| 759 | } | ||
| 760 | |||
| 761 | static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) | ||
| 762 | { | ||
| 763 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | ||
| 764 | unsigned long escr_mask[BITS_TO_LONGS(P4_ESCR_MSR_TABLE_SIZE)]; | ||
| 765 | int cpu = raw_smp_processor_id(); | ||
| 766 | struct hw_perf_event *hwc; | ||
| 767 | struct p4_event_bind *bind; | ||
| 768 | unsigned int i, thread, num; | ||
| 769 | int cntr_idx, escr_idx; | ||
| 770 | |||
| 771 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); | ||
| 772 | bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE); | ||
| 773 | |||
| 774 | for (i = 0, num = n; i < n; i++, num--) { | ||
| 775 | |||
| 776 | hwc = &cpuc->event_list[i]->hw; | ||
| 777 | thread = p4_ht_thread(cpu); | ||
| 778 | bind = p4_config_get_bind(hwc->config); | ||
| 779 | escr_idx = p4_get_escr_idx(bind->escr_msr[thread]); | ||
| 780 | if (unlikely(escr_idx == -1)) | ||
| 781 | goto done; | ||
| 782 | |||
| 783 | if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) { | ||
| 784 | cntr_idx = hwc->idx; | ||
| 785 | if (assign) | ||
| 786 | assign[i] = hwc->idx; | ||
| 787 | goto reserve; | ||
| 788 | } | ||
| 789 | |||
| 790 | cntr_idx = p4_next_cntr(thread, used_mask, bind); | ||
| 791 | if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) | ||
| 792 | goto done; | ||
| 793 | |||
| 794 | p4_pmu_swap_config_ts(hwc, cpu); | ||
| 795 | if (assign) | ||
| 796 | assign[i] = cntr_idx; | ||
| 797 | reserve: | ||
| 798 | set_bit(cntr_idx, used_mask); | ||
| 799 | set_bit(escr_idx, escr_mask); | ||
| 800 | } | ||
| 801 | |||
| 802 | done: | ||
| 803 | return num ? -ENOSPC : 0; | ||
| 804 | } | ||
| 805 | |||
| 806 | static __initconst const struct x86_pmu p4_pmu = { | ||
| 807 | .name = "Netburst P4/Xeon", | ||
| 808 | .handle_irq = p4_pmu_handle_irq, | ||
| 809 | .disable_all = p4_pmu_disable_all, | ||
| 810 | .enable_all = p4_pmu_enable_all, | ||
| 811 | .enable = p4_pmu_enable_event, | ||
| 812 | .disable = p4_pmu_disable_event, | ||
| 813 | .eventsel = MSR_P4_BPU_CCCR0, | ||
| 814 | .perfctr = MSR_P4_BPU_PERFCTR0, | ||
| 815 | .event_map = p4_pmu_event_map, | ||
| 816 | .max_events = ARRAY_SIZE(p4_general_events), | ||
| 817 | .get_event_constraints = x86_get_event_constraints, | ||
| 818 | /* | ||
| 819 | * IF HT disabled we may need to use all | ||
| 820 | * ARCH_P4_MAX_CCCR counters simulaneously | ||
| 821 | * though leave it restricted at moment assuming | ||
| 822 | * HT is on | ||
| 823 | */ | ||
| 824 | .num_counters = ARCH_P4_MAX_CCCR, | ||
| 825 | .apic = 1, | ||
| 826 | .cntval_bits = 40, | ||
| 827 | .cntval_mask = (1ULL << 40) - 1, | ||
| 828 | .max_period = (1ULL << 39) - 1, | ||
| 829 | .hw_config = p4_hw_config, | ||
| 830 | .schedule_events = p4_pmu_schedule_events, | ||
| 831 | }; | ||
| 832 | |||
| 833 | static __init int p4_pmu_init(void) | ||
| 834 | { | ||
| 835 | unsigned int low, high; | ||
| 836 | |||
| 837 | /* If we get stripped -- indexig fails */ | ||
| 838 | BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC); | ||
| 839 | |||
| 840 | rdmsr(MSR_IA32_MISC_ENABLE, low, high); | ||
| 841 | if (!(low & (1 << 7))) { | ||
| 842 | pr_cont("unsupported Netburst CPU model %d ", | ||
| 843 | boot_cpu_data.x86_model); | ||
| 844 | return -ENODEV; | ||
| 845 | } | ||
| 846 | |||
| 847 | memcpy(hw_cache_event_ids, p4_hw_cache_event_ids, | ||
| 848 | sizeof(hw_cache_event_ids)); | ||
| 849 | |||
| 850 | pr_cont("Netburst events, "); | ||
| 851 | |||
| 852 | x86_pmu = p4_pmu; | ||
| 853 | |||
| 854 | return 0; | ||
| 855 | } | ||
| 856 | |||
| 857 | #endif /* CONFIG_CPU_SUP_INTEL */ | ||
diff --git a/arch/x86/kernel/cpu/perf_event_p6.c b/arch/x86/kernel/cpu/perf_event_p6.c index a330485d14da..34ba07be2cda 100644 --- a/arch/x86/kernel/cpu/perf_event_p6.c +++ b/arch/x86/kernel/cpu/perf_event_p6.c | |||
| @@ -27,24 +27,6 @@ static u64 p6_pmu_event_map(int hw_event) | |||
| 27 | */ | 27 | */ |
| 28 | #define P6_NOP_EVENT 0x0000002EULL | 28 | #define P6_NOP_EVENT 0x0000002EULL |
| 29 | 29 | ||
| 30 | static u64 p6_pmu_raw_event(u64 hw_event) | ||
| 31 | { | ||
| 32 | #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL | ||
| 33 | #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL | ||
| 34 | #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL | ||
| 35 | #define P6_EVNTSEL_INV_MASK 0x00800000ULL | ||
| 36 | #define P6_EVNTSEL_REG_MASK 0xFF000000ULL | ||
| 37 | |||
| 38 | #define P6_EVNTSEL_MASK \ | ||
| 39 | (P6_EVNTSEL_EVENT_MASK | \ | ||
| 40 | P6_EVNTSEL_UNIT_MASK | \ | ||
| 41 | P6_EVNTSEL_EDGE_MASK | \ | ||
| 42 | P6_EVNTSEL_INV_MASK | \ | ||
| 43 | P6_EVNTSEL_REG_MASK) | ||
| 44 | |||
| 45 | return hw_event & P6_EVNTSEL_MASK; | ||
| 46 | } | ||
| 47 | |||
| 48 | static struct event_constraint p6_event_constraints[] = | 30 | static struct event_constraint p6_event_constraints[] = |
| 49 | { | 31 | { |
| 50 | INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */ | 32 | INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */ |
| @@ -66,7 +48,7 @@ static void p6_pmu_disable_all(void) | |||
| 66 | wrmsrl(MSR_P6_EVNTSEL0, val); | 48 | wrmsrl(MSR_P6_EVNTSEL0, val); |
| 67 | } | 49 | } |
| 68 | 50 | ||
| 69 | static void p6_pmu_enable_all(void) | 51 | static void p6_pmu_enable_all(int added) |
| 70 | { | 52 | { |
| 71 | unsigned long val; | 53 | unsigned long val; |
| 72 | 54 | ||
| @@ -102,22 +84,23 @@ static void p6_pmu_enable_event(struct perf_event *event) | |||
| 102 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, val); | 84 | (void)checking_wrmsrl(hwc->config_base + hwc->idx, val); |
| 103 | } | 85 | } |
| 104 | 86 | ||
| 105 | static __initconst struct x86_pmu p6_pmu = { | 87 | static __initconst const struct x86_pmu p6_pmu = { |
| 106 | .name = "p6", | 88 | .name = "p6", |
| 107 | .handle_irq = x86_pmu_handle_irq, | 89 | .handle_irq = x86_pmu_handle_irq, |
| 108 | .disable_all = p6_pmu_disable_all, | 90 | .disable_all = p6_pmu_disable_all, |
| 109 | .enable_all = p6_pmu_enable_all, | 91 | .enable_all = p6_pmu_enable_all, |
| 110 | .enable = p6_pmu_enable_event, | 92 | .enable = p6_pmu_enable_event, |
| 111 | .disable = p6_pmu_disable_event, | 93 | .disable = p6_pmu_disable_event, |
| 94 | .hw_config = x86_pmu_hw_config, | ||
| 95 | .schedule_events = x86_schedule_events, | ||
| 112 | .eventsel = MSR_P6_EVNTSEL0, | 96 | .eventsel = MSR_P6_EVNTSEL0, |
| 113 | .perfctr = MSR_P6_PERFCTR0, | 97 | .perfctr = MSR_P6_PERFCTR0, |
| 114 | .event_map = p6_pmu_event_map, | 98 | .event_map = p6_pmu_event_map, |
| 115 | .raw_event = p6_pmu_raw_event, | ||
| 116 | .max_events = ARRAY_SIZE(p6_perfmon_event_map), | 99 | .max_events = ARRAY_SIZE(p6_perfmon_event_map), |
| 117 | .apic = 1, | 100 | .apic = 1, |
| 118 | .max_period = (1ULL << 31) - 1, | 101 | .max_period = (1ULL << 31) - 1, |
| 119 | .version = 0, | 102 | .version = 0, |
| 120 | .num_events = 2, | 103 | .num_counters = 2, |
| 121 | /* | 104 | /* |
| 122 | * Events have 40 bits implemented. However they are designed such | 105 | * Events have 40 bits implemented. However they are designed such |
| 123 | * that bits [32-39] are sign extensions of bit 31. As such the | 106 | * that bits [32-39] are sign extensions of bit 31. As such the |
| @@ -125,8 +108,8 @@ static __initconst struct x86_pmu p6_pmu = { | |||
| 125 | * | 108 | * |
| 126 | * See IA-32 Intel Architecture Software developer manual Vol 3B | 109 | * See IA-32 Intel Architecture Software developer manual Vol 3B |
| 127 | */ | 110 | */ |
| 128 | .event_bits = 32, | 111 | .cntval_bits = 32, |
| 129 | .event_mask = (1ULL << 32) - 1, | 112 | .cntval_mask = (1ULL << 32) - 1, |
| 130 | .get_event_constraints = x86_get_event_constraints, | 113 | .get_event_constraints = x86_get_event_constraints, |
| 131 | .event_constraints = p6_event_constraints, | 114 | .event_constraints = p6_event_constraints, |
| 132 | }; | 115 | }; |
diff --git a/arch/x86/kernel/ds.c b/arch/x86/kernel/ds.c deleted file mode 100644 index 1c47390dd0e5..000000000000 --- a/arch/x86/kernel/ds.c +++ /dev/null | |||
| @@ -1,1437 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Debug Store support | ||
| 3 | * | ||
| 4 | * This provides a low-level interface to the hardware's Debug Store | ||
| 5 | * feature that is used for branch trace store (BTS) and | ||
| 6 | * precise-event based sampling (PEBS). | ||
| 7 | * | ||
| 8 | * It manages: | ||
| 9 | * - DS and BTS hardware configuration | ||
| 10 | * - buffer overflow handling (to be done) | ||
| 11 | * - buffer access | ||
| 12 | * | ||
| 13 | * It does not do: | ||
| 14 | * - security checking (is the caller allowed to trace the task) | ||
| 15 | * - buffer allocation (memory accounting) | ||
| 16 | * | ||
| 17 | * | ||
| 18 | * Copyright (C) 2007-2009 Intel Corporation. | ||
| 19 | * Markus Metzger <markus.t.metzger@intel.com>, 2007-2009 | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <linux/kernel.h> | ||
| 23 | #include <linux/string.h> | ||
| 24 | #include <linux/errno.h> | ||
| 25 | #include <linux/sched.h> | ||
| 26 | #include <linux/slab.h> | ||
| 27 | #include <linux/mm.h> | ||
| 28 | #include <linux/trace_clock.h> | ||
| 29 | |||
| 30 | #include <asm/ds.h> | ||
| 31 | |||
| 32 | #include "ds_selftest.h" | ||
| 33 | |||
| 34 | /* | ||
| 35 | * The configuration for a particular DS hardware implementation: | ||
| 36 | */ | ||
| 37 | struct ds_configuration { | ||
| 38 | /* The name of the configuration: */ | ||
| 39 | const char *name; | ||
| 40 | |||
| 41 | /* The size of pointer-typed fields in DS, BTS, and PEBS: */ | ||
| 42 | unsigned char sizeof_ptr_field; | ||
| 43 | |||
| 44 | /* The size of a BTS/PEBS record in bytes: */ | ||
| 45 | unsigned char sizeof_rec[2]; | ||
| 46 | |||
| 47 | /* The number of pebs counter reset values in the DS structure. */ | ||
| 48 | unsigned char nr_counter_reset; | ||
| 49 | |||
| 50 | /* Control bit-masks indexed by enum ds_feature: */ | ||
| 51 | unsigned long ctl[dsf_ctl_max]; | ||
| 52 | }; | ||
| 53 | static struct ds_configuration ds_cfg __read_mostly; | ||
| 54 | |||
| 55 | |||
| 56 | /* Maximal size of a DS configuration: */ | ||
| 57 | #define MAX_SIZEOF_DS 0x80 | ||
| 58 | |||
| 59 | /* Maximal size of a BTS record: */ | ||
| 60 | #define MAX_SIZEOF_BTS (3 * 8) | ||
| 61 | |||
| 62 | /* BTS and PEBS buffer alignment: */ | ||
| 63 | #define DS_ALIGNMENT (1 << 3) | ||
| 64 | |||
| 65 | /* Number of buffer pointers in DS: */ | ||
| 66 | #define NUM_DS_PTR_FIELDS 8 | ||
| 67 | |||
| 68 | /* Size of a pebs reset value in DS: */ | ||
| 69 | #define PEBS_RESET_FIELD_SIZE 8 | ||
| 70 | |||
| 71 | /* Mask of control bits in the DS MSR register: */ | ||
| 72 | #define BTS_CONTROL \ | ||
| 73 | ( ds_cfg.ctl[dsf_bts] | \ | ||
| 74 | ds_cfg.ctl[dsf_bts_kernel] | \ | ||
| 75 | ds_cfg.ctl[dsf_bts_user] | \ | ||
| 76 | ds_cfg.ctl[dsf_bts_overflow] ) | ||
| 77 | |||
| 78 | /* | ||
| 79 | * A BTS or PEBS tracer. | ||
| 80 | * | ||
| 81 | * This holds the configuration of the tracer and serves as a handle | ||
| 82 | * to identify tracers. | ||
| 83 | */ | ||
| 84 | struct ds_tracer { | ||
| 85 | /* The DS context (partially) owned by this tracer. */ | ||
| 86 | struct ds_context *context; | ||
| 87 | /* The buffer provided on ds_request() and its size in bytes. */ | ||
| 88 | void *buffer; | ||
| 89 | size_t size; | ||
| 90 | }; | ||
| 91 | |||
| 92 | struct bts_tracer { | ||
| 93 | /* The common DS part: */ | ||
| 94 | struct ds_tracer ds; | ||
| 95 | |||
| 96 | /* The trace including the DS configuration: */ | ||
| 97 | struct bts_trace trace; | ||
| 98 | |||
| 99 | /* Buffer overflow notification function: */ | ||
| 100 | bts_ovfl_callback_t ovfl; | ||
| 101 | |||
| 102 | /* Active flags affecting trace collection. */ | ||
| 103 | unsigned int flags; | ||
| 104 | }; | ||
| 105 | |||
| 106 | struct pebs_tracer { | ||
| 107 | /* The common DS part: */ | ||
| 108 | struct ds_tracer ds; | ||
| 109 | |||
| 110 | /* The trace including the DS configuration: */ | ||
| 111 | struct pebs_trace trace; | ||
| 112 | |||
| 113 | /* Buffer overflow notification function: */ | ||
| 114 | pebs_ovfl_callback_t ovfl; | ||
| 115 | }; | ||
| 116 | |||
| 117 | /* | ||
| 118 | * Debug Store (DS) save area configuration (see Intel64 and IA32 | ||
| 119 | * Architectures Software Developer's Manual, section 18.5) | ||
| 120 | * | ||
| 121 | * The DS configuration consists of the following fields; different | ||
| 122 | * architetures vary in the size of those fields. | ||
| 123 | * | ||
| 124 | * - double-word aligned base linear address of the BTS buffer | ||
| 125 | * - write pointer into the BTS buffer | ||
| 126 | * - end linear address of the BTS buffer (one byte beyond the end of | ||
| 127 | * the buffer) | ||
| 128 | * - interrupt pointer into BTS buffer | ||
| 129 | * (interrupt occurs when write pointer passes interrupt pointer) | ||
| 130 | * - double-word aligned base linear address of the PEBS buffer | ||
| 131 | * - write pointer into the PEBS buffer | ||
| 132 | * - end linear address of the PEBS buffer (one byte beyond the end of | ||
| 133 | * the buffer) | ||
| 134 | * - interrupt pointer into PEBS buffer | ||
| 135 | * (interrupt occurs when write pointer passes interrupt pointer) | ||
| 136 | * - value to which counter is reset following counter overflow | ||
| 137 | * | ||
| 138 | * Later architectures use 64bit pointers throughout, whereas earlier | ||
| 139 | * architectures use 32bit pointers in 32bit mode. | ||
| 140 | * | ||
| 141 | * | ||
| 142 | * We compute the base address for the first 8 fields based on: | ||
| 143 | * - the field size stored in the DS configuration | ||
| 144 | * - the relative field position | ||
| 145 | * - an offset giving the start of the respective region | ||
| 146 | * | ||
| 147 | * This offset is further used to index various arrays holding | ||
| 148 | * information for BTS and PEBS at the respective index. | ||
| 149 | * | ||
| 150 | * On later 32bit processors, we only access the lower 32bit of the | ||
| 151 | * 64bit pointer fields. The upper halves will be zeroed out. | ||
| 152 | */ | ||
| 153 | |||
| 154 | enum ds_field { | ||
| 155 | ds_buffer_base = 0, | ||
| 156 | ds_index, | ||
| 157 | ds_absolute_maximum, | ||
| 158 | ds_interrupt_threshold, | ||
| 159 | }; | ||
| 160 | |||
| 161 | enum ds_qualifier { | ||
| 162 | ds_bts = 0, | ||
| 163 | ds_pebs | ||
| 164 | }; | ||
| 165 | |||
| 166 | static inline unsigned long | ||
| 167 | ds_get(const unsigned char *base, enum ds_qualifier qual, enum ds_field field) | ||
| 168 | { | ||
| 169 | base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual))); | ||
| 170 | return *(unsigned long *)base; | ||
| 171 | } | ||
| 172 | |||
| 173 | static inline void | ||
| 174 | ds_set(unsigned char *base, enum ds_qualifier qual, enum ds_field field, | ||
| 175 | unsigned long value) | ||
| 176 | { | ||
| 177 | base += (ds_cfg.sizeof_ptr_field * (field + (4 * qual))); | ||
| 178 | (*(unsigned long *)base) = value; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | /* | ||
| 183 | * Locking is done only for allocating BTS or PEBS resources. | ||
| 184 | */ | ||
| 185 | static DEFINE_SPINLOCK(ds_lock); | ||
| 186 | |||
| 187 | /* | ||
| 188 | * We either support (system-wide) per-cpu or per-thread allocation. | ||
| 189 | * We distinguish the two based on the task_struct pointer, where a | ||
| 190 | * NULL pointer indicates per-cpu allocation for the current cpu. | ||
| 191 | * | ||
| 192 | * Allocations are use-counted. As soon as resources are allocated, | ||
| 193 | * further allocations must be of the same type (per-cpu or | ||
| 194 | * per-thread). We model this by counting allocations (i.e. the number | ||
| 195 | * of tracers of a certain type) for one type negatively: | ||
| 196 | * =0 no tracers | ||
| 197 | * >0 number of per-thread tracers | ||
| 198 | * <0 number of per-cpu tracers | ||
| 199 | * | ||
| 200 | * Tracers essentially gives the number of ds contexts for a certain | ||
| 201 | * type of allocation. | ||
| 202 | */ | ||
| 203 | static atomic_t tracers = ATOMIC_INIT(0); | ||
| 204 | |||
| 205 | static inline int get_tracer(struct task_struct *task) | ||
| 206 | { | ||
| 207 | int error; | ||
| 208 | |||
| 209 | spin_lock_irq(&ds_lock); | ||
| 210 | |||
| 211 | if (task) { | ||
| 212 | error = -EPERM; | ||
| 213 | if (atomic_read(&tracers) < 0) | ||
| 214 | goto out; | ||
| 215 | atomic_inc(&tracers); | ||
| 216 | } else { | ||
| 217 | error = -EPERM; | ||
| 218 | if (atomic_read(&tracers) > 0) | ||
| 219 | goto out; | ||
| 220 | atomic_dec(&tracers); | ||
| 221 | } | ||
| 222 | |||
| 223 | error = 0; | ||
| 224 | out: | ||
| 225 | spin_unlock_irq(&ds_lock); | ||
| 226 | return error; | ||
| 227 | } | ||
| 228 | |||
| 229 | static inline void put_tracer(struct task_struct *task) | ||
| 230 | { | ||
| 231 | if (task) | ||
| 232 | atomic_dec(&tracers); | ||
| 233 | else | ||
| 234 | atomic_inc(&tracers); | ||
| 235 | } | ||
| 236 | |||
| 237 | /* | ||
| 238 | * The DS context is either attached to a thread or to a cpu: | ||
| 239 | * - in the former case, the thread_struct contains a pointer to the | ||
| 240 | * attached context. | ||
| 241 | * - in the latter case, we use a static array of per-cpu context | ||
| 242 | * pointers. | ||
| 243 | * | ||
| 244 | * Contexts are use-counted. They are allocated on first access and | ||
| 245 | * deallocated when the last user puts the context. | ||
| 246 | */ | ||
| 247 | struct ds_context { | ||
| 248 | /* The DS configuration; goes into MSR_IA32_DS_AREA: */ | ||
| 249 | unsigned char ds[MAX_SIZEOF_DS]; | ||
| 250 | |||
| 251 | /* The owner of the BTS and PEBS configuration, respectively: */ | ||
| 252 | struct bts_tracer *bts_master; | ||
| 253 | struct pebs_tracer *pebs_master; | ||
| 254 | |||
| 255 | /* Use count: */ | ||
| 256 | unsigned long count; | ||
| 257 | |||
| 258 | /* Pointer to the context pointer field: */ | ||
| 259 | struct ds_context **this; | ||
| 260 | |||
| 261 | /* The traced task; NULL for cpu tracing: */ | ||
| 262 | struct task_struct *task; | ||
| 263 | |||
| 264 | /* The traced cpu; only valid if task is NULL: */ | ||
| 265 | int cpu; | ||
| 266 | }; | ||
| 267 | |||
| 268 | static DEFINE_PER_CPU(struct ds_context *, cpu_ds_context); | ||
| 269 | |||
| 270 | |||
| 271 | static struct ds_context *ds_get_context(struct task_struct *task, int cpu) | ||
| 272 | { | ||
| 273 | struct ds_context **p_context = | ||
| 274 | (task ? &task->thread.ds_ctx : &per_cpu(cpu_ds_context, cpu)); | ||
| 275 | struct ds_context *context = NULL; | ||
| 276 | struct ds_context *new_context = NULL; | ||
| 277 | |||
| 278 | /* Chances are small that we already have a context. */ | ||
| 279 | new_context = kzalloc(sizeof(*new_context), GFP_KERNEL); | ||
| 280 | if (!new_context) | ||
| 281 | return NULL; | ||
| 282 | |||
| 283 | spin_lock_irq(&ds_lock); | ||
| 284 | |||
| 285 | context = *p_context; | ||
| 286 | if (likely(!context)) { | ||
| 287 | context = new_context; | ||
| 288 | |||
| 289 | context->this = p_context; | ||
| 290 | context->task = task; | ||
| 291 | context->cpu = cpu; | ||
| 292 | context->count = 0; | ||
| 293 | |||
| 294 | *p_context = context; | ||
| 295 | } | ||
| 296 | |||
| 297 | context->count++; | ||
| 298 | |||
| 299 | spin_unlock_irq(&ds_lock); | ||
| 300 | |||
| 301 | if (context != new_context) | ||
| 302 | kfree(new_context); | ||
| 303 | |||
| 304 | return context; | ||
| 305 | } | ||
| 306 | |||
| 307 | static void ds_put_context(struct ds_context *context) | ||
| 308 | { | ||
| 309 | struct task_struct *task; | ||
| 310 | unsigned long irq; | ||
| 311 | |||
| 312 | if (!context) | ||
| 313 | return; | ||
| 314 | |||
| 315 | spin_lock_irqsave(&ds_lock, irq); | ||
| 316 | |||
| 317 | if (--context->count) { | ||
| 318 | spin_unlock_irqrestore(&ds_lock, irq); | ||
| 319 | return; | ||
| 320 | } | ||
| 321 | |||
| 322 | *(context->this) = NULL; | ||
| 323 | |||
| 324 | task = context->task; | ||
| 325 | |||
| 326 | if (task) | ||
| 327 | clear_tsk_thread_flag(task, TIF_DS_AREA_MSR); | ||
| 328 | |||
| 329 | /* | ||
| 330 | * We leave the (now dangling) pointer to the DS configuration in | ||
| 331 | * the DS_AREA msr. This is as good or as bad as replacing it with | ||
| 332 | * NULL - the hardware would crash if we enabled tracing. | ||
| 333 | * | ||
| 334 | * This saves us some problems with having to write an msr on a | ||
| 335 | * different cpu while preventing others from doing the same for the | ||
| 336 | * next context for that same cpu. | ||
| 337 | */ | ||
| 338 | |||
| 339 | spin_unlock_irqrestore(&ds_lock, irq); | ||
| 340 | |||
| 341 | /* The context might still be in use for context switching. */ | ||
| 342 | if (task && (task != current)) | ||
| 343 | wait_task_context_switch(task); | ||
| 344 | |||
| 345 | kfree(context); | ||
| 346 | } | ||
| 347 | |||
| 348 | static void ds_install_ds_area(struct ds_context *context) | ||
| 349 | { | ||
| 350 | unsigned long ds; | ||
| 351 | |||
| 352 | ds = (unsigned long)context->ds; | ||
| 353 | |||
| 354 | /* | ||
| 355 | * There is a race between the bts master and the pebs master. | ||
| 356 | * | ||
| 357 | * The thread/cpu access is synchronized via get/put_cpu() for | ||
| 358 | * task tracing and via wrmsr_on_cpu for cpu tracing. | ||
| 359 | * | ||
| 360 | * If bts and pebs are collected for the same task or same cpu, | ||
| 361 | * the same confiuration is written twice. | ||
| 362 | */ | ||
| 363 | if (context->task) { | ||
| 364 | get_cpu(); | ||
| 365 | if (context->task == current) | ||
| 366 | wrmsrl(MSR_IA32_DS_AREA, ds); | ||
| 367 | set_tsk_thread_flag(context->task, TIF_DS_AREA_MSR); | ||
| 368 | put_cpu(); | ||
| 369 | } else | ||
| 370 | wrmsr_on_cpu(context->cpu, MSR_IA32_DS_AREA, | ||
| 371 | (u32)((u64)ds), (u32)((u64)ds >> 32)); | ||
| 372 | } | ||
| 373 | |||
| 374 | /* | ||
| 375 | * Call the tracer's callback on a buffer overflow. | ||
| 376 | * | ||
| 377 | * context: the ds context | ||
| 378 | * qual: the buffer type | ||
| 379 | */ | ||
| 380 | static void ds_overflow(struct ds_context *context, enum ds_qualifier qual) | ||
| 381 | { | ||
| 382 | switch (qual) { | ||
| 383 | case ds_bts: | ||
| 384 | if (context->bts_master && | ||
| 385 | context->bts_master->ovfl) | ||
| 386 | context->bts_master->ovfl(context->bts_master); | ||
| 387 | break; | ||
| 388 | case ds_pebs: | ||
| 389 | if (context->pebs_master && | ||
| 390 | context->pebs_master->ovfl) | ||
| 391 | context->pebs_master->ovfl(context->pebs_master); | ||
| 392 | break; | ||
| 393 | } | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | /* | ||
| 398 | * Write raw data into the BTS or PEBS buffer. | ||
| 399 | * | ||
| 400 | * The remainder of any partially written record is zeroed out. | ||
| 401 | * | ||
| 402 | * context: the DS context | ||
| 403 | * qual: the buffer type | ||
| 404 | * record: the data to write | ||
| 405 | * size: the size of the data | ||
| 406 | */ | ||
| 407 | static int ds_write(struct ds_context *context, enum ds_qualifier qual, | ||
| 408 | const void *record, size_t size) | ||
| 409 | { | ||
| 410 | int bytes_written = 0; | ||
| 411 | |||
| 412 | if (!record) | ||
| 413 | return -EINVAL; | ||
| 414 | |||
| 415 | while (size) { | ||
| 416 | unsigned long base, index, end, write_end, int_th; | ||
| 417 | unsigned long write_size, adj_write_size; | ||
| 418 | |||
| 419 | /* | ||
| 420 | * Write as much as possible without producing an | ||
| 421 | * overflow interrupt. | ||
| 422 | * | ||
| 423 | * Interrupt_threshold must either be | ||
| 424 | * - bigger than absolute_maximum or | ||
| 425 | * - point to a record between buffer_base and absolute_maximum | ||
| 426 | * | ||
| 427 | * Index points to a valid record. | ||
| 428 | */ | ||
| 429 | base = ds_get(context->ds, qual, ds_buffer_base); | ||
| 430 | index = ds_get(context->ds, qual, ds_index); | ||
| 431 | end = ds_get(context->ds, qual, ds_absolute_maximum); | ||
| 432 | int_th = ds_get(context->ds, qual, ds_interrupt_threshold); | ||
| 433 | |||
| 434 | write_end = min(end, int_th); | ||
| 435 | |||
| 436 | /* | ||
| 437 | * If we are already beyond the interrupt threshold, | ||
| 438 | * we fill the entire buffer. | ||
| 439 | */ | ||
| 440 | if (write_end <= index) | ||
| 441 | write_end = end; | ||
| 442 | |||
| 443 | if (write_end <= index) | ||
| 444 | break; | ||
| 445 | |||
| 446 | write_size = min((unsigned long) size, write_end - index); | ||
| 447 | memcpy((void *)index, record, write_size); | ||
| 448 | |||
| 449 | record = (const char *)record + write_size; | ||
| 450 | size -= write_size; | ||
| 451 | bytes_written += write_size; | ||
| 452 | |||
| 453 | adj_write_size = write_size / ds_cfg.sizeof_rec[qual]; | ||
| 454 | adj_write_size *= ds_cfg.sizeof_rec[qual]; | ||
| 455 | |||
| 456 | /* Zero out trailing bytes. */ | ||
| 457 | memset((char *)index + write_size, 0, | ||
| 458 | adj_write_size - write_size); | ||
| 459 | index += adj_write_size; | ||
| 460 | |||
| 461 | if (index >= end) | ||
| 462 | index = base; | ||
| 463 | ds_set(context->ds, qual, ds_index, index); | ||
| 464 | |||
| 465 | if (index >= int_th) | ||
| 466 | ds_overflow(context, qual); | ||
| 467 | } | ||
| 468 | |||
| 469 | return bytes_written; | ||
| 470 | } | ||
| 471 | |||
| 472 | |||
| 473 | /* | ||
| 474 | * Branch Trace Store (BTS) uses the following format. Different | ||
| 475 | * architectures vary in the size of those fields. | ||
| 476 | * - source linear address | ||
| 477 | * - destination linear address | ||
| 478 | * - flags | ||
| 479 | * | ||
| 480 | * Later architectures use 64bit pointers throughout, whereas earlier | ||
| 481 | * architectures use 32bit pointers in 32bit mode. | ||
| 482 | * | ||
| 483 | * We compute the base address for the fields based on: | ||
| 484 | * - the field size stored in the DS configuration | ||
| 485 | * - the relative field position | ||
| 486 | * | ||
| 487 | * In order to store additional information in the BTS buffer, we use | ||
| 488 | * a special source address to indicate that the record requires | ||
| 489 | * special interpretation. | ||
| 490 | * | ||
| 491 | * Netburst indicated via a bit in the flags field whether the branch | ||
| 492 | * was predicted; this is ignored. | ||
| 493 | * | ||
| 494 | * We use two levels of abstraction: | ||
| 495 | * - the raw data level defined here | ||
| 496 | * - an arch-independent level defined in ds.h | ||
| 497 | */ | ||
| 498 | |||
| 499 | enum bts_field { | ||
| 500 | bts_from, | ||
| 501 | bts_to, | ||
| 502 | bts_flags, | ||
| 503 | |||
| 504 | bts_qual = bts_from, | ||
| 505 | bts_clock = bts_to, | ||
| 506 | bts_pid = bts_flags, | ||
| 507 | |||
| 508 | bts_qual_mask = (bts_qual_max - 1), | ||
| 509 | bts_escape = ((unsigned long)-1 & ~bts_qual_mask) | ||
| 510 | }; | ||
| 511 | |||
| 512 | static inline unsigned long bts_get(const char *base, unsigned long field) | ||
| 513 | { | ||
| 514 | base += (ds_cfg.sizeof_ptr_field * field); | ||
| 515 | return *(unsigned long *)base; | ||
| 516 | } | ||
| 517 | |||
| 518 | static inline void bts_set(char *base, unsigned long field, unsigned long val) | ||
| 519 | { | ||
| 520 | base += (ds_cfg.sizeof_ptr_field * field); | ||
| 521 | (*(unsigned long *)base) = val; | ||
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | /* | ||
| 526 | * The raw BTS data is architecture dependent. | ||
| 527 | * | ||
| 528 | * For higher-level users, we give an arch-independent view. | ||
| 529 | * - ds.h defines struct bts_struct | ||
| 530 | * - bts_read translates one raw bts record into a bts_struct | ||
| 531 | * - bts_write translates one bts_struct into the raw format and | ||
| 532 | * writes it into the top of the parameter tracer's buffer. | ||
| 533 | * | ||
| 534 | * return: bytes read/written on success; -Eerrno, otherwise | ||
| 535 | */ | ||
| 536 | static int | ||
| 537 | bts_read(struct bts_tracer *tracer, const void *at, struct bts_struct *out) | ||
| 538 | { | ||
| 539 | if (!tracer) | ||
| 540 | return -EINVAL; | ||
| 541 | |||
| 542 | if (at < tracer->trace.ds.begin) | ||
| 543 | return -EINVAL; | ||
| 544 | |||
| 545 | if (tracer->trace.ds.end < (at + tracer->trace.ds.size)) | ||
| 546 | return -EINVAL; | ||
| 547 | |||
| 548 | memset(out, 0, sizeof(*out)); | ||
| 549 | if ((bts_get(at, bts_qual) & ~bts_qual_mask) == bts_escape) { | ||
| 550 | out->qualifier = (bts_get(at, bts_qual) & bts_qual_mask); | ||
| 551 | out->variant.event.clock = bts_get(at, bts_clock); | ||
| 552 | out->variant.event.pid = bts_get(at, bts_pid); | ||
| 553 | } else { | ||
| 554 | out->qualifier = bts_branch; | ||
| 555 | out->variant.lbr.from = bts_get(at, bts_from); | ||
| 556 | out->variant.lbr.to = bts_get(at, bts_to); | ||
| 557 | |||
| 558 | if (!out->variant.lbr.from && !out->variant.lbr.to) | ||
| 559 | out->qualifier = bts_invalid; | ||
| 560 | } | ||
| 561 | |||
| 562 | return ds_cfg.sizeof_rec[ds_bts]; | ||
| 563 | } | ||
| 564 | |||
| 565 | static int bts_write(struct bts_tracer *tracer, const struct bts_struct *in) | ||
| 566 | { | ||
| 567 | unsigned char raw[MAX_SIZEOF_BTS]; | ||
| 568 | |||
| 569 | if (!tracer) | ||
| 570 | return -EINVAL; | ||
| 571 | |||
| 572 | if (MAX_SIZEOF_BTS < ds_cfg.sizeof_rec[ds_bts]) | ||
| 573 | return -EOVERFLOW; | ||
| 574 | |||
| 575 | switch (in->qualifier) { | ||
| 576 | case bts_invalid: | ||
| 577 | bts_set(raw, bts_from, 0); | ||
| 578 | bts_set(raw, bts_to, 0); | ||
| 579 | bts_set(raw, bts_flags, 0); | ||
| 580 | break; | ||
| 581 | case bts_branch: | ||
| 582 | bts_set(raw, bts_from, in->variant.lbr.from); | ||
| 583 | bts_set(raw, bts_to, in->variant.lbr.to); | ||
| 584 | bts_set(raw, bts_flags, 0); | ||
| 585 | break; | ||
| 586 | case bts_task_arrives: | ||
| 587 | case bts_task_departs: | ||
| 588 | bts_set(raw, bts_qual, (bts_escape | in->qualifier)); | ||
| 589 | bts_set(raw, bts_clock, in->variant.event.clock); | ||
| 590 | bts_set(raw, bts_pid, in->variant.event.pid); | ||
| 591 | break; | ||
| 592 | default: | ||
| 593 | return -EINVAL; | ||
| 594 | } | ||
| 595 | |||
| 596 | return ds_write(tracer->ds.context, ds_bts, raw, | ||
| 597 | ds_cfg.sizeof_rec[ds_bts]); | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | static void ds_write_config(struct ds_context *context, | ||
| 602 | struct ds_trace *cfg, enum ds_qualifier qual) | ||
| 603 | { | ||
| 604 | unsigned char *ds = context->ds; | ||
| 605 | |||
| 606 | ds_set(ds, qual, ds_buffer_base, (unsigned long)cfg->begin); | ||
| 607 | ds_set(ds, qual, ds_index, (unsigned long)cfg->top); | ||
| 608 | ds_set(ds, qual, ds_absolute_maximum, (unsigned long)cfg->end); | ||
| 609 | ds_set(ds, qual, ds_interrupt_threshold, (unsigned long)cfg->ith); | ||
| 610 | } | ||
| 611 | |||
| 612 | static void ds_read_config(struct ds_context *context, | ||
| 613 | struct ds_trace *cfg, enum ds_qualifier qual) | ||
| 614 | { | ||
| 615 | unsigned char *ds = context->ds; | ||
| 616 | |||
| 617 | cfg->begin = (void *)ds_get(ds, qual, ds_buffer_base); | ||
| 618 | cfg->top = (void *)ds_get(ds, qual, ds_index); | ||
| 619 | cfg->end = (void *)ds_get(ds, qual, ds_absolute_maximum); | ||
| 620 | cfg->ith = (void *)ds_get(ds, qual, ds_interrupt_threshold); | ||
| 621 | } | ||
| 622 | |||
| 623 | static void ds_init_ds_trace(struct ds_trace *trace, enum ds_qualifier qual, | ||
| 624 | void *base, size_t size, size_t ith, | ||
| 625 | unsigned int flags) { | ||
| 626 | unsigned long buffer, adj; | ||
| 627 | |||
| 628 | /* | ||
| 629 | * Adjust the buffer address and size to meet alignment | ||
| 630 | * constraints: | ||
| 631 | * - buffer is double-word aligned | ||
| 632 | * - size is multiple of record size | ||
| 633 | * | ||
| 634 | * We checked the size at the very beginning; we have enough | ||
| 635 | * space to do the adjustment. | ||
| 636 | */ | ||
| 637 | buffer = (unsigned long)base; | ||
| 638 | |||
| 639 | adj = ALIGN(buffer, DS_ALIGNMENT) - buffer; | ||
| 640 | buffer += adj; | ||
| 641 | size -= adj; | ||
| 642 | |||
| 643 | trace->n = size / ds_cfg.sizeof_rec[qual]; | ||
| 644 | trace->size = ds_cfg.sizeof_rec[qual]; | ||
| 645 | |||
| 646 | size = (trace->n * trace->size); | ||
| 647 | |||
| 648 | trace->begin = (void *)buffer; | ||
| 649 | trace->top = trace->begin; | ||
| 650 | trace->end = (void *)(buffer + size); | ||
| 651 | /* | ||
| 652 | * The value for 'no threshold' is -1, which will set the | ||
| 653 | * threshold outside of the buffer, just like we want it. | ||
| 654 | */ | ||
| 655 | ith *= ds_cfg.sizeof_rec[qual]; | ||
| 656 | trace->ith = (void *)(buffer + size - ith); | ||
| 657 | |||
| 658 | trace->flags = flags; | ||
| 659 | } | ||
| 660 | |||
| 661 | |||
| 662 | static int ds_request(struct ds_tracer *tracer, struct ds_trace *trace, | ||
| 663 | enum ds_qualifier qual, struct task_struct *task, | ||
| 664 | int cpu, void *base, size_t size, size_t th) | ||
| 665 | { | ||
| 666 | struct ds_context *context; | ||
| 667 | int error; | ||
| 668 | size_t req_size; | ||
| 669 | |||
| 670 | error = -EOPNOTSUPP; | ||
| 671 | if (!ds_cfg.sizeof_rec[qual]) | ||
| 672 | goto out; | ||
| 673 | |||
| 674 | error = -EINVAL; | ||
| 675 | if (!base) | ||
| 676 | goto out; | ||
| 677 | |||
| 678 | req_size = ds_cfg.sizeof_rec[qual]; | ||
| 679 | /* We might need space for alignment adjustments. */ | ||
| 680 | if (!IS_ALIGNED((unsigned long)base, DS_ALIGNMENT)) | ||
| 681 | req_size += DS_ALIGNMENT; | ||
| 682 | |||
| 683 | error = -EINVAL; | ||
| 684 | if (size < req_size) | ||
| 685 | goto out; | ||
| 686 | |||
| 687 | if (th != (size_t)-1) { | ||
| 688 | th *= ds_cfg.sizeof_rec[qual]; | ||
| 689 | |||
| 690 | error = -EINVAL; | ||
| 691 | if (size <= th) | ||
| 692 | goto out; | ||
| 693 | } | ||
| 694 | |||
| 695 | tracer->buffer = base; | ||
| 696 | tracer->size = size; | ||
| 697 | |||
| 698 | error = -ENOMEM; | ||
| 699 | context = ds_get_context(task, cpu); | ||
| 700 | if (!context) | ||
| 701 | goto out; | ||
| 702 | tracer->context = context; | ||
| 703 | |||
| 704 | /* | ||
| 705 | * Defer any tracer-specific initialization work for the context until | ||
| 706 | * context ownership has been clarified. | ||
| 707 | */ | ||
| 708 | |||
| 709 | error = 0; | ||
| 710 | out: | ||
| 711 | return error; | ||
| 712 | } | ||
| 713 | |||
| 714 | static struct bts_tracer *ds_request_bts(struct task_struct *task, int cpu, | ||
| 715 | void *base, size_t size, | ||
| 716 | bts_ovfl_callback_t ovfl, size_t th, | ||
| 717 | unsigned int flags) | ||
| 718 | { | ||
| 719 | struct bts_tracer *tracer; | ||
| 720 | int error; | ||
| 721 | |||
| 722 | /* Buffer overflow notification is not yet implemented. */ | ||
| 723 | error = -EOPNOTSUPP; | ||
| 724 | if (ovfl) | ||
| 725 | goto out; | ||
| 726 | |||
| 727 | error = get_tracer(task); | ||
| 728 | if (error < 0) | ||
| 729 | goto out; | ||
| 730 | |||
| 731 | error = -ENOMEM; | ||
| 732 | tracer = kzalloc(sizeof(*tracer), GFP_KERNEL); | ||
| 733 | if (!tracer) | ||
| 734 | goto out_put_tracer; | ||
| 735 | tracer->ovfl = ovfl; | ||
| 736 | |||
| 737 | /* Do some more error checking and acquire a tracing context. */ | ||
| 738 | error = ds_request(&tracer->ds, &tracer->trace.ds, | ||
| 739 | ds_bts, task, cpu, base, size, th); | ||
| 740 | if (error < 0) | ||
| 741 | goto out_tracer; | ||
| 742 | |||
| 743 | /* Claim the bts part of the tracing context we acquired above. */ | ||
| 744 | spin_lock_irq(&ds_lock); | ||
| 745 | |||
| 746 | error = -EPERM; | ||
| 747 | if (tracer->ds.context->bts_master) | ||
| 748 | goto out_unlock; | ||
| 749 | tracer->ds.context->bts_master = tracer; | ||
| 750 | |||
| 751 | spin_unlock_irq(&ds_lock); | ||
| 752 | |||
| 753 | /* | ||
| 754 | * Now that we own the bts part of the context, let's complete the | ||
| 755 | * initialization for that part. | ||
| 756 | */ | ||
| 757 | ds_init_ds_trace(&tracer->trace.ds, ds_bts, base, size, th, flags); | ||
| 758 | ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_bts); | ||
| 759 | ds_install_ds_area(tracer->ds.context); | ||
| 760 | |||
| 761 | tracer->trace.read = bts_read; | ||
| 762 | tracer->trace.write = bts_write; | ||
| 763 | |||
| 764 | /* Start tracing. */ | ||
| 765 | ds_resume_bts(tracer); | ||
| 766 | |||
| 767 | return tracer; | ||
| 768 | |||
| 769 | out_unlock: | ||
| 770 | spin_unlock_irq(&ds_lock); | ||
| 771 | ds_put_context(tracer->ds.context); | ||
| 772 | out_tracer: | ||
| 773 | kfree(tracer); | ||
| 774 | out_put_tracer: | ||
| 775 | put_tracer(task); | ||
| 776 | out: | ||
| 777 | return ERR_PTR(error); | ||
| 778 | } | ||
| 779 | |||
| 780 | struct bts_tracer *ds_request_bts_task(struct task_struct *task, | ||
| 781 | void *base, size_t size, | ||
| 782 | bts_ovfl_callback_t ovfl, | ||
| 783 | size_t th, unsigned int flags) | ||
| 784 | { | ||
| 785 | return ds_request_bts(task, 0, base, size, ovfl, th, flags); | ||
| 786 | } | ||
| 787 | |||
| 788 | struct bts_tracer *ds_request_bts_cpu(int cpu, void *base, size_t size, | ||
| 789 | bts_ovfl_callback_t ovfl, | ||
| 790 | size_t th, unsigned int flags) | ||
| 791 | { | ||
| 792 | return ds_request_bts(NULL, cpu, base, size, ovfl, th, flags); | ||
| 793 | } | ||
| 794 | |||
| 795 | static struct pebs_tracer *ds_request_pebs(struct task_struct *task, int cpu, | ||
| 796 | void *base, size_t size, | ||
| 797 | pebs_ovfl_callback_t ovfl, size_t th, | ||
| 798 | unsigned int flags) | ||
| 799 | { | ||
| 800 | struct pebs_tracer *tracer; | ||
| 801 | int error; | ||
| 802 | |||
| 803 | /* Buffer overflow notification is not yet implemented. */ | ||
| 804 | error = -EOPNOTSUPP; | ||
| 805 | if (ovfl) | ||
| 806 | goto out; | ||
| 807 | |||
| 808 | error = get_tracer(task); | ||
| 809 | if (error < 0) | ||
| 810 | goto out; | ||
| 811 | |||
| 812 | error = -ENOMEM; | ||
| 813 | tracer = kzalloc(sizeof(*tracer), GFP_KERNEL); | ||
| 814 | if (!tracer) | ||
| 815 | goto out_put_tracer; | ||
| 816 | tracer->ovfl = ovfl; | ||
| 817 | |||
| 818 | /* Do some more error checking and acquire a tracing context. */ | ||
| 819 | error = ds_request(&tracer->ds, &tracer->trace.ds, | ||
| 820 | ds_pebs, task, cpu, base, size, th); | ||
| 821 | if (error < 0) | ||
| 822 | goto out_tracer; | ||
| 823 | |||
| 824 | /* Claim the pebs part of the tracing context we acquired above. */ | ||
| 825 | spin_lock_irq(&ds_lock); | ||
| 826 | |||
| 827 | error = -EPERM; | ||
| 828 | if (tracer->ds.context->pebs_master) | ||
| 829 | goto out_unlock; | ||
| 830 | tracer->ds.context->pebs_master = tracer; | ||
| 831 | |||
| 832 | spin_unlock_irq(&ds_lock); | ||
| 833 | |||
| 834 | /* | ||
| 835 | * Now that we own the pebs part of the context, let's complete the | ||
| 836 | * initialization for that part. | ||
| 837 | */ | ||
| 838 | ds_init_ds_trace(&tracer->trace.ds, ds_pebs, base, size, th, flags); | ||
| 839 | ds_write_config(tracer->ds.context, &tracer->trace.ds, ds_pebs); | ||
| 840 | ds_install_ds_area(tracer->ds.context); | ||
| 841 | |||
| 842 | /* Start tracing. */ | ||
| 843 | ds_resume_pebs(tracer); | ||
| 844 | |||
| 845 | return tracer; | ||
| 846 | |||
| 847 | out_unlock: | ||
| 848 | spin_unlock_irq(&ds_lock); | ||
| 849 | ds_put_context(tracer->ds.context); | ||
| 850 | out_tracer: | ||
| 851 | kfree(tracer); | ||
| 852 | out_put_tracer: | ||
| 853 | put_tracer(task); | ||
| 854 | out: | ||
| 855 | return ERR_PTR(error); | ||
| 856 | } | ||
| 857 | |||
| 858 | struct pebs_tracer *ds_request_pebs_task(struct task_struct *task, | ||
| 859 | void *base, size_t size, | ||
| 860 | pebs_ovfl_callback_t ovfl, | ||
| 861 | size_t th, unsigned int flags) | ||
| 862 | { | ||
| 863 | return ds_request_pebs(task, 0, base, size, ovfl, th, flags); | ||
| 864 | } | ||
| 865 | |||
| 866 | struct pebs_tracer *ds_request_pebs_cpu(int cpu, void *base, size_t size, | ||
| 867 | pebs_ovfl_callback_t ovfl, | ||
| 868 | size_t th, unsigned int flags) | ||
| 869 | { | ||
| 870 | return ds_request_pebs(NULL, cpu, base, size, ovfl, th, flags); | ||
| 871 | } | ||
| 872 | |||
| 873 | static void ds_free_bts(struct bts_tracer *tracer) | ||
| 874 | { | ||
| 875 | struct task_struct *task; | ||
| 876 | |||
| 877 | task = tracer->ds.context->task; | ||
| 878 | |||
| 879 | WARN_ON_ONCE(tracer->ds.context->bts_master != tracer); | ||
| 880 | tracer->ds.context->bts_master = NULL; | ||
| 881 | |||
| 882 | /* Make sure tracing stopped and the tracer is not in use. */ | ||
| 883 | if (task && (task != current)) | ||
| 884 | wait_task_context_switch(task); | ||
| 885 | |||
| 886 | ds_put_context(tracer->ds.context); | ||
| 887 | put_tracer(task); | ||
| 888 | |||
| 889 | kfree(tracer); | ||
| 890 | } | ||
| 891 | |||
| 892 | void ds_release_bts(struct bts_tracer *tracer) | ||
| 893 | { | ||
| 894 | might_sleep(); | ||
| 895 | |||
| 896 | if (!tracer) | ||
| 897 | return; | ||
| 898 | |||
| 899 | ds_suspend_bts(tracer); | ||
| 900 | ds_free_bts(tracer); | ||
| 901 | } | ||
| 902 | |||
| 903 | int ds_release_bts_noirq(struct bts_tracer *tracer) | ||
| 904 | { | ||
| 905 | struct task_struct *task; | ||
| 906 | unsigned long irq; | ||
| 907 | int error; | ||
| 908 | |||
| 909 | if (!tracer) | ||
| 910 | return 0; | ||
| 911 | |||
| 912 | task = tracer->ds.context->task; | ||
| 913 | |||
| 914 | local_irq_save(irq); | ||
| 915 | |||
| 916 | error = -EPERM; | ||
| 917 | if (!task && | ||
| 918 | (tracer->ds.context->cpu != smp_processor_id())) | ||
| 919 | goto out; | ||
| 920 | |||
| 921 | error = -EPERM; | ||
| 922 | if (task && (task != current)) | ||
| 923 | goto out; | ||
| 924 | |||
| 925 | ds_suspend_bts_noirq(tracer); | ||
| 926 | ds_free_bts(tracer); | ||
| 927 | |||
| 928 | error = 0; | ||
| 929 | out: | ||
| 930 | local_irq_restore(irq); | ||
| 931 | return error; | ||
| 932 | } | ||
| 933 | |||
| 934 | static void update_task_debugctlmsr(struct task_struct *task, | ||
| 935 | unsigned long debugctlmsr) | ||
| 936 | { | ||
| 937 | task->thread.debugctlmsr = debugctlmsr; | ||
| 938 | |||
| 939 | get_cpu(); | ||
| 940 | if (task == current) | ||
| 941 | update_debugctlmsr(debugctlmsr); | ||
| 942 | put_cpu(); | ||
| 943 | } | ||
| 944 | |||
| 945 | void ds_suspend_bts(struct bts_tracer *tracer) | ||
| 946 | { | ||
| 947 | struct task_struct *task; | ||
| 948 | unsigned long debugctlmsr; | ||
| 949 | int cpu; | ||
| 950 | |||
| 951 | if (!tracer) | ||
| 952 | return; | ||
| 953 | |||
| 954 | tracer->flags = 0; | ||
| 955 | |||
| 956 | task = tracer->ds.context->task; | ||
| 957 | cpu = tracer->ds.context->cpu; | ||
| 958 | |||
| 959 | WARN_ON(!task && irqs_disabled()); | ||
| 960 | |||
| 961 | debugctlmsr = (task ? | ||
| 962 | task->thread.debugctlmsr : | ||
| 963 | get_debugctlmsr_on_cpu(cpu)); | ||
| 964 | debugctlmsr &= ~BTS_CONTROL; | ||
| 965 | |||
| 966 | if (task) | ||
| 967 | update_task_debugctlmsr(task, debugctlmsr); | ||
| 968 | else | ||
| 969 | update_debugctlmsr_on_cpu(cpu, debugctlmsr); | ||
| 970 | } | ||
| 971 | |||
| 972 | int ds_suspend_bts_noirq(struct bts_tracer *tracer) | ||
| 973 | { | ||
| 974 | struct task_struct *task; | ||
| 975 | unsigned long debugctlmsr, irq; | ||
| 976 | int cpu, error = 0; | ||
| 977 | |||
| 978 | if (!tracer) | ||
| 979 | return 0; | ||
| 980 | |||
| 981 | tracer->flags = 0; | ||
| 982 | |||
| 983 | task = tracer->ds.context->task; | ||
| 984 | cpu = tracer->ds.context->cpu; | ||
| 985 | |||
| 986 | local_irq_save(irq); | ||
| 987 | |||
| 988 | error = -EPERM; | ||
| 989 | if (!task && (cpu != smp_processor_id())) | ||
| 990 | goto out; | ||
| 991 | |||
| 992 | debugctlmsr = (task ? | ||
| 993 | task->thread.debugctlmsr : | ||
| 994 | get_debugctlmsr()); | ||
| 995 | debugctlmsr &= ~BTS_CONTROL; | ||
| 996 | |||
| 997 | if (task) | ||
| 998 | update_task_debugctlmsr(task, debugctlmsr); | ||
| 999 | else | ||
| 1000 | update_debugctlmsr(debugctlmsr); | ||
| 1001 | |||
| 1002 | error = 0; | ||
| 1003 | out: | ||
| 1004 | local_irq_restore(irq); | ||
| 1005 | return error; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | static unsigned long ds_bts_control(struct bts_tracer *tracer) | ||
| 1009 | { | ||
| 1010 | unsigned long control; | ||
| 1011 | |||
| 1012 | control = ds_cfg.ctl[dsf_bts]; | ||
| 1013 | if (!(tracer->trace.ds.flags & BTS_KERNEL)) | ||
| 1014 | control |= ds_cfg.ctl[dsf_bts_kernel]; | ||
| 1015 | if (!(tracer->trace.ds.flags & BTS_USER)) | ||
| 1016 | control |= ds_cfg.ctl[dsf_bts_user]; | ||
| 1017 | |||
| 1018 | return control; | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | void ds_resume_bts(struct bts_tracer *tracer) | ||
| 1022 | { | ||
| 1023 | struct task_struct *task; | ||
| 1024 | unsigned long debugctlmsr; | ||
| 1025 | int cpu; | ||
| 1026 | |||
| 1027 | if (!tracer) | ||
| 1028 | return; | ||
| 1029 | |||
| 1030 | tracer->flags = tracer->trace.ds.flags; | ||
| 1031 | |||
| 1032 | task = tracer->ds.context->task; | ||
| 1033 | cpu = tracer->ds.context->cpu; | ||
| 1034 | |||
| 1035 | WARN_ON(!task && irqs_disabled()); | ||
| 1036 | |||
| 1037 | debugctlmsr = (task ? | ||
| 1038 | task->thread.debugctlmsr : | ||
| 1039 | get_debugctlmsr_on_cpu(cpu)); | ||
| 1040 | debugctlmsr |= ds_bts_control(tracer); | ||
| 1041 | |||
| 1042 | if (task) | ||
| 1043 | update_task_debugctlmsr(task, debugctlmsr); | ||
| 1044 | else | ||
| 1045 | update_debugctlmsr_on_cpu(cpu, debugctlmsr); | ||
| 1046 | } | ||
| 1047 | |||
| 1048 | int ds_resume_bts_noirq(struct bts_tracer *tracer) | ||
| 1049 | { | ||
| 1050 | struct task_struct *task; | ||
| 1051 | unsigned long debugctlmsr, irq; | ||
| 1052 | int cpu, error = 0; | ||
| 1053 | |||
| 1054 | if (!tracer) | ||
| 1055 | return 0; | ||
| 1056 | |||
| 1057 | tracer->flags = tracer->trace.ds.flags; | ||
| 1058 | |||
| 1059 | task = tracer->ds.context->task; | ||
| 1060 | cpu = tracer->ds.context->cpu; | ||
| 1061 | |||
| 1062 | local_irq_save(irq); | ||
| 1063 | |||
| 1064 | error = -EPERM; | ||
| 1065 | if (!task && (cpu != smp_processor_id())) | ||
| 1066 | goto out; | ||
| 1067 | |||
| 1068 | debugctlmsr = (task ? | ||
| 1069 | task->thread.debugctlmsr : | ||
| 1070 | get_debugctlmsr()); | ||
| 1071 | debugctlmsr |= ds_bts_control(tracer); | ||
| 1072 | |||
| 1073 | if (task) | ||
| 1074 | update_task_debugctlmsr(task, debugctlmsr); | ||
| 1075 | else | ||
| 1076 | update_debugctlmsr(debugctlmsr); | ||
| 1077 | |||
| 1078 | error = 0; | ||
| 1079 | out: | ||
| 1080 | local_irq_restore(irq); | ||
| 1081 | return error; | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | static void ds_free_pebs(struct pebs_tracer *tracer) | ||
| 1085 | { | ||
| 1086 | struct task_struct *task; | ||
| 1087 | |||
| 1088 | task = tracer->ds.context->task; | ||
| 1089 | |||
| 1090 | WARN_ON_ONCE(tracer->ds.context->pebs_master != tracer); | ||
| 1091 | tracer->ds.context->pebs_master = NULL; | ||
| 1092 | |||
| 1093 | ds_put_context(tracer->ds.context); | ||
| 1094 | put_tracer(task); | ||
| 1095 | |||
| 1096 | kfree(tracer); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | void ds_release_pebs(struct pebs_tracer *tracer) | ||
| 1100 | { | ||
| 1101 | might_sleep(); | ||
| 1102 | |||
| 1103 | if (!tracer) | ||
| 1104 | return; | ||
| 1105 | |||
| 1106 | ds_suspend_pebs(tracer); | ||
| 1107 | ds_free_pebs(tracer); | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | int ds_release_pebs_noirq(struct pebs_tracer *tracer) | ||
| 1111 | { | ||
| 1112 | struct task_struct *task; | ||
| 1113 | unsigned long irq; | ||
| 1114 | int error; | ||
| 1115 | |||
| 1116 | if (!tracer) | ||
| 1117 | return 0; | ||
| 1118 | |||
| 1119 | task = tracer->ds.context->task; | ||
| 1120 | |||
| 1121 | local_irq_save(irq); | ||
| 1122 | |||
| 1123 | error = -EPERM; | ||
| 1124 | if (!task && | ||
| 1125 | (tracer->ds.context->cpu != smp_processor_id())) | ||
| 1126 | goto out; | ||
| 1127 | |||
| 1128 | error = -EPERM; | ||
| 1129 | if (task && (task != current)) | ||
| 1130 | goto out; | ||
| 1131 | |||
| 1132 | ds_suspend_pebs_noirq(tracer); | ||
| 1133 | ds_free_pebs(tracer); | ||
| 1134 | |||
| 1135 | error = 0; | ||
| 1136 | out: | ||
| 1137 | local_irq_restore(irq); | ||
| 1138 | return error; | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | void ds_suspend_pebs(struct pebs_tracer *tracer) | ||
| 1142 | { | ||
| 1143 | |||
| 1144 | } | ||
| 1145 | |||
| 1146 | int ds_suspend_pebs_noirq(struct pebs_tracer *tracer) | ||
| 1147 | { | ||
| 1148 | return 0; | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | void ds_resume_pebs(struct pebs_tracer *tracer) | ||
| 1152 | { | ||
| 1153 | |||
| 1154 | } | ||
| 1155 | |||
| 1156 | int ds_resume_pebs_noirq(struct pebs_tracer *tracer) | ||
| 1157 | { | ||
| 1158 | return 0; | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | const struct bts_trace *ds_read_bts(struct bts_tracer *tracer) | ||
| 1162 | { | ||
| 1163 | if (!tracer) | ||
| 1164 | return NULL; | ||
| 1165 | |||
| 1166 | ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_bts); | ||
| 1167 | return &tracer->trace; | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | const struct pebs_trace *ds_read_pebs(struct pebs_tracer *tracer) | ||
| 1171 | { | ||
| 1172 | if (!tracer) | ||
| 1173 | return NULL; | ||
| 1174 | |||
| 1175 | ds_read_config(tracer->ds.context, &tracer->trace.ds, ds_pebs); | ||
| 1176 | |||
| 1177 | tracer->trace.counters = ds_cfg.nr_counter_reset; | ||
| 1178 | memcpy(tracer->trace.counter_reset, | ||
| 1179 | tracer->ds.context->ds + | ||
| 1180 | (NUM_DS_PTR_FIELDS * ds_cfg.sizeof_ptr_field), | ||
| 1181 | ds_cfg.nr_counter_reset * PEBS_RESET_FIELD_SIZE); | ||
| 1182 | |||
| 1183 | return &tracer->trace; | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | int ds_reset_bts(struct bts_tracer *tracer) | ||
| 1187 | { | ||
| 1188 | if (!tracer) | ||
| 1189 | return -EINVAL; | ||
| 1190 | |||
| 1191 | tracer->trace.ds.top = tracer->trace.ds.begin; | ||
| 1192 | |||
| 1193 | ds_set(tracer->ds.context->ds, ds_bts, ds_index, | ||
| 1194 | (unsigned long)tracer->trace.ds.top); | ||
| 1195 | |||
| 1196 | return 0; | ||
| 1197 | } | ||
| 1198 | |||
| 1199 | int ds_reset_pebs(struct pebs_tracer *tracer) | ||
| 1200 | { | ||
| 1201 | if (!tracer) | ||
| 1202 | return -EINVAL; | ||
| 1203 | |||
| 1204 | tracer->trace.ds.top = tracer->trace.ds.begin; | ||
| 1205 | |||
| 1206 | ds_set(tracer->ds.context->ds, ds_pebs, ds_index, | ||
| 1207 | (unsigned long)tracer->trace.ds.top); | ||
| 1208 | |||
| 1209 | return 0; | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | int ds_set_pebs_reset(struct pebs_tracer *tracer, | ||
| 1213 | unsigned int counter, u64 value) | ||
| 1214 | { | ||
| 1215 | if (!tracer) | ||
| 1216 | return -EINVAL; | ||
| 1217 | |||
| 1218 | if (ds_cfg.nr_counter_reset < counter) | ||
| 1219 | return -EINVAL; | ||
| 1220 | |||
| 1221 | *(u64 *)(tracer->ds.context->ds + | ||
| 1222 | (NUM_DS_PTR_FIELDS * ds_cfg.sizeof_ptr_field) + | ||
| 1223 | (counter * PEBS_RESET_FIELD_SIZE)) = value; | ||
| 1224 | |||
| 1225 | return 0; | ||
| 1226 | } | ||
| 1227 | |||
| 1228 | static const struct ds_configuration ds_cfg_netburst = { | ||
| 1229 | .name = "Netburst", | ||
| 1230 | .ctl[dsf_bts] = (1 << 2) | (1 << 3), | ||
| 1231 | .ctl[dsf_bts_kernel] = (1 << 5), | ||
| 1232 | .ctl[dsf_bts_user] = (1 << 6), | ||
| 1233 | .nr_counter_reset = 1, | ||
| 1234 | }; | ||
| 1235 | static const struct ds_configuration ds_cfg_pentium_m = { | ||
| 1236 | .name = "Pentium M", | ||
| 1237 | .ctl[dsf_bts] = (1 << 6) | (1 << 7), | ||
| 1238 | .nr_counter_reset = 1, | ||
| 1239 | }; | ||
| 1240 | static const struct ds_configuration ds_cfg_core2_atom = { | ||
| 1241 | .name = "Core 2/Atom", | ||
| 1242 | .ctl[dsf_bts] = (1 << 6) | (1 << 7), | ||
| 1243 | .ctl[dsf_bts_kernel] = (1 << 9), | ||
| 1244 | .ctl[dsf_bts_user] = (1 << 10), | ||
| 1245 | .nr_counter_reset = 1, | ||
| 1246 | }; | ||
| 1247 | static const struct ds_configuration ds_cfg_core_i7 = { | ||
| 1248 | .name = "Core i7", | ||
| 1249 | .ctl[dsf_bts] = (1 << 6) | (1 << 7), | ||
| 1250 | .ctl[dsf_bts_kernel] = (1 << 9), | ||
| 1251 | .ctl[dsf_bts_user] = (1 << 10), | ||
| 1252 | .nr_counter_reset = 4, | ||
| 1253 | }; | ||
| 1254 | |||
| 1255 | static void | ||
| 1256 | ds_configure(const struct ds_configuration *cfg, | ||
| 1257 | struct cpuinfo_x86 *cpu) | ||
| 1258 | { | ||
| 1259 | unsigned long nr_pebs_fields = 0; | ||
| 1260 | |||
| 1261 | printk(KERN_INFO "[ds] using %s configuration\n", cfg->name); | ||
| 1262 | |||
| 1263 | #ifdef __i386__ | ||
| 1264 | nr_pebs_fields = 10; | ||
| 1265 | #else | ||
| 1266 | nr_pebs_fields = 18; | ||
| 1267 | #endif | ||
| 1268 | |||
| 1269 | /* | ||
| 1270 | * Starting with version 2, architectural performance | ||
| 1271 | * monitoring supports a format specifier. | ||
| 1272 | */ | ||
| 1273 | if ((cpuid_eax(0xa) & 0xff) > 1) { | ||
| 1274 | unsigned long perf_capabilities, format; | ||
| 1275 | |||
| 1276 | rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_capabilities); | ||
| 1277 | |||
| 1278 | format = (perf_capabilities >> 8) & 0xf; | ||
| 1279 | |||
| 1280 | switch (format) { | ||
| 1281 | case 0: | ||
| 1282 | nr_pebs_fields = 18; | ||
| 1283 | break; | ||
| 1284 | case 1: | ||
| 1285 | nr_pebs_fields = 22; | ||
| 1286 | break; | ||
| 1287 | default: | ||
| 1288 | printk(KERN_INFO | ||
| 1289 | "[ds] unknown PEBS format: %lu\n", format); | ||
| 1290 | nr_pebs_fields = 0; | ||
| 1291 | break; | ||
| 1292 | } | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | memset(&ds_cfg, 0, sizeof(ds_cfg)); | ||
| 1296 | ds_cfg = *cfg; | ||
| 1297 | |||
| 1298 | ds_cfg.sizeof_ptr_field = | ||
| 1299 | (cpu_has(cpu, X86_FEATURE_DTES64) ? 8 : 4); | ||
| 1300 | |||
| 1301 | ds_cfg.sizeof_rec[ds_bts] = ds_cfg.sizeof_ptr_field * 3; | ||
| 1302 | ds_cfg.sizeof_rec[ds_pebs] = ds_cfg.sizeof_ptr_field * nr_pebs_fields; | ||
| 1303 | |||
| 1304 | if (!cpu_has(cpu, X86_FEATURE_BTS)) { | ||
| 1305 | ds_cfg.sizeof_rec[ds_bts] = 0; | ||
| 1306 | printk(KERN_INFO "[ds] bts not available\n"); | ||
| 1307 | } | ||
| 1308 | if (!cpu_has(cpu, X86_FEATURE_PEBS)) { | ||
| 1309 | ds_cfg.sizeof_rec[ds_pebs] = 0; | ||
| 1310 | printk(KERN_INFO "[ds] pebs not available\n"); | ||
| 1311 | } | ||
| 1312 | |||
| 1313 | printk(KERN_INFO "[ds] sizes: address: %u bit, ", | ||
| 1314 | 8 * ds_cfg.sizeof_ptr_field); | ||
| 1315 | printk("bts/pebs record: %u/%u bytes\n", | ||
| 1316 | ds_cfg.sizeof_rec[ds_bts], ds_cfg.sizeof_rec[ds_pebs]); | ||
| 1317 | |||
| 1318 | WARN_ON_ONCE(MAX_PEBS_COUNTERS < ds_cfg.nr_counter_reset); | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | void __cpuinit ds_init_intel(struct cpuinfo_x86 *c) | ||
| 1322 | { | ||
| 1323 | /* Only configure the first cpu. Others are identical. */ | ||
| 1324 | if (ds_cfg.name) | ||
| 1325 | return; | ||
| 1326 | |||
| 1327 | switch (c->x86) { | ||
| 1328 | case 0x6: | ||
| 1329 | switch (c->x86_model) { | ||
| 1330 | case 0x9: | ||
| 1331 | case 0xd: /* Pentium M */ | ||
| 1332 | ds_configure(&ds_cfg_pentium_m, c); | ||
| 1333 | break; | ||
| 1334 | case 0xf: | ||
| 1335 | case 0x17: /* Core2 */ | ||
| 1336 | case 0x1c: /* Atom */ | ||
| 1337 | ds_configure(&ds_cfg_core2_atom, c); | ||
| 1338 | break; | ||
| 1339 | case 0x1a: /* Core i7 */ | ||
| 1340 | ds_configure(&ds_cfg_core_i7, c); | ||
| 1341 | break; | ||
| 1342 | default: | ||
| 1343 | /* Sorry, don't know about them. */ | ||
| 1344 | break; | ||
| 1345 | } | ||
| 1346 | break; | ||
| 1347 | case 0xf: | ||
| 1348 | switch (c->x86_model) { | ||
| 1349 | case 0x0: | ||
| 1350 | case 0x1: | ||
| 1351 | case 0x2: /* Netburst */ | ||
| 1352 | ds_configure(&ds_cfg_netburst, c); | ||
| 1353 | break; | ||
| 1354 | default: | ||
| 1355 | /* Sorry, don't know about them. */ | ||
| 1356 | break; | ||
| 1357 | } | ||
| 1358 | break; | ||
| 1359 | default: | ||
| 1360 | /* Sorry, don't know about them. */ | ||
| 1361 | break; | ||
| 1362 | } | ||
| 1363 | } | ||
| 1364 | |||
| 1365 | static inline void ds_take_timestamp(struct ds_context *context, | ||
| 1366 | enum bts_qualifier qualifier, | ||
| 1367 | struct task_struct *task) | ||
| 1368 | { | ||
| 1369 | struct bts_tracer *tracer = context->bts_master; | ||
| 1370 | struct bts_struct ts; | ||
| 1371 | |||
| 1372 | /* Prevent compilers from reading the tracer pointer twice. */ | ||
| 1373 | barrier(); | ||
| 1374 | |||
| 1375 | if (!tracer || !(tracer->flags & BTS_TIMESTAMPS)) | ||
| 1376 | return; | ||
| 1377 | |||
| 1378 | memset(&ts, 0, sizeof(ts)); | ||
| 1379 | ts.qualifier = qualifier; | ||
| 1380 | ts.variant.event.clock = trace_clock_global(); | ||
| 1381 | ts.variant.event.pid = task->pid; | ||
| 1382 | |||
| 1383 | bts_write(tracer, &ts); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | /* | ||
| 1387 | * Change the DS configuration from tracing prev to tracing next. | ||
| 1388 | */ | ||
| 1389 | void ds_switch_to(struct task_struct *prev, struct task_struct *next) | ||
| 1390 | { | ||
| 1391 | struct ds_context *prev_ctx = prev->thread.ds_ctx; | ||
| 1392 | struct ds_context *next_ctx = next->thread.ds_ctx; | ||
| 1393 | unsigned long debugctlmsr = next->thread.debugctlmsr; | ||
| 1394 | |||
| 1395 | /* Make sure all data is read before we start. */ | ||
| 1396 | barrier(); | ||
| 1397 | |||
| 1398 | if (prev_ctx) { | ||
| 1399 | update_debugctlmsr(0); | ||
| 1400 | |||
| 1401 | ds_take_timestamp(prev_ctx, bts_task_departs, prev); | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | if (next_ctx) { | ||
| 1405 | ds_take_timestamp(next_ctx, bts_task_arrives, next); | ||
| 1406 | |||
| 1407 | wrmsrl(MSR_IA32_DS_AREA, (unsigned long)next_ctx->ds); | ||
| 1408 | } | ||
| 1409 | |||
| 1410 | update_debugctlmsr(debugctlmsr); | ||
| 1411 | } | ||
| 1412 | |||
| 1413 | static __init int ds_selftest(void) | ||
| 1414 | { | ||
| 1415 | if (ds_cfg.sizeof_rec[ds_bts]) { | ||
| 1416 | int error; | ||
| 1417 | |||
| 1418 | error = ds_selftest_bts(); | ||
| 1419 | if (error) { | ||
| 1420 | WARN(1, "[ds] selftest failed. disabling bts.\n"); | ||
| 1421 | ds_cfg.sizeof_rec[ds_bts] = 0; | ||
| 1422 | } | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | if (ds_cfg.sizeof_rec[ds_pebs]) { | ||
| 1426 | int error; | ||
| 1427 | |||
| 1428 | error = ds_selftest_pebs(); | ||
| 1429 | if (error) { | ||
| 1430 | WARN(1, "[ds] selftest failed. disabling pebs.\n"); | ||
| 1431 | ds_cfg.sizeof_rec[ds_pebs] = 0; | ||
| 1432 | } | ||
| 1433 | } | ||
| 1434 | |||
| 1435 | return 0; | ||
| 1436 | } | ||
| 1437 | device_initcall(ds_selftest); | ||
diff --git a/arch/x86/kernel/ds_selftest.c b/arch/x86/kernel/ds_selftest.c deleted file mode 100644 index 6bc7c199ab99..000000000000 --- a/arch/x86/kernel/ds_selftest.c +++ /dev/null | |||
| @@ -1,408 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Debug Store support - selftest | ||
| 3 | * | ||
| 4 | * | ||
| 5 | * Copyright (C) 2009 Intel Corporation. | ||
| 6 | * Markus Metzger <markus.t.metzger@intel.com>, 2009 | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "ds_selftest.h" | ||
| 10 | |||
| 11 | #include <linux/kernel.h> | ||
| 12 | #include <linux/string.h> | ||
| 13 | #include <linux/smp.h> | ||
| 14 | #include <linux/cpu.h> | ||
| 15 | |||
| 16 | #include <asm/ds.h> | ||
| 17 | |||
| 18 | |||
| 19 | #define BUFFER_SIZE 521 /* Intentionally chose an odd size. */ | ||
| 20 | #define SMALL_BUFFER_SIZE 24 /* A single bts entry. */ | ||
| 21 | |||
| 22 | struct ds_selftest_bts_conf { | ||
| 23 | struct bts_tracer *tracer; | ||
| 24 | int error; | ||
| 25 | int (*suspend)(struct bts_tracer *); | ||
| 26 | int (*resume)(struct bts_tracer *); | ||
| 27 | }; | ||
| 28 | |||
| 29 | static int ds_selftest_bts_consistency(const struct bts_trace *trace) | ||
| 30 | { | ||
| 31 | int error = 0; | ||
| 32 | |||
| 33 | if (!trace) { | ||
| 34 | printk(KERN_CONT "failed to access trace..."); | ||
| 35 | /* Bail out. Other tests are pointless. */ | ||
| 36 | return -1; | ||
| 37 | } | ||
| 38 | |||
| 39 | if (!trace->read) { | ||
| 40 | printk(KERN_CONT "bts read not available..."); | ||
| 41 | error = -1; | ||
| 42 | } | ||
| 43 | |||
| 44 | /* Do some sanity checks on the trace configuration. */ | ||
| 45 | if (!trace->ds.n) { | ||
| 46 | printk(KERN_CONT "empty bts buffer..."); | ||
| 47 | error = -1; | ||
| 48 | } | ||
| 49 | if (!trace->ds.size) { | ||
| 50 | printk(KERN_CONT "bad bts trace setup..."); | ||
| 51 | error = -1; | ||
| 52 | } | ||
| 53 | if (trace->ds.end != | ||
| 54 | (char *)trace->ds.begin + (trace->ds.n * trace->ds.size)) { | ||
| 55 | printk(KERN_CONT "bad bts buffer setup..."); | ||
| 56 | error = -1; | ||
| 57 | } | ||
| 58 | /* | ||
| 59 | * We allow top in [begin; end], since its not clear when the | ||
| 60 | * overflow adjustment happens: after the increment or before the | ||
| 61 | * write. | ||
| 62 | */ | ||
| 63 | if ((trace->ds.top < trace->ds.begin) || | ||
| 64 | (trace->ds.end < trace->ds.top)) { | ||
| 65 | printk(KERN_CONT "bts top out of bounds..."); | ||
| 66 | error = -1; | ||
| 67 | } | ||
| 68 | |||
| 69 | return error; | ||
| 70 | } | ||
| 71 | |||
| 72 | static int ds_selftest_bts_read(struct bts_tracer *tracer, | ||
| 73 | const struct bts_trace *trace, | ||
| 74 | const void *from, const void *to) | ||
| 75 | { | ||
| 76 | const unsigned char *at; | ||
| 77 | |||
| 78 | /* | ||
| 79 | * Check a few things which do not belong to this test. | ||
| 80 | * They should be covered by other tests. | ||
| 81 | */ | ||
| 82 | if (!trace) | ||
| 83 | return -1; | ||
| 84 | |||
| 85 | if (!trace->read) | ||
| 86 | return -1; | ||
| 87 | |||
| 88 | if (to < from) | ||
| 89 | return -1; | ||
| 90 | |||
| 91 | if (from < trace->ds.begin) | ||
| 92 | return -1; | ||
| 93 | |||
| 94 | if (trace->ds.end < to) | ||
| 95 | return -1; | ||
| 96 | |||
| 97 | if (!trace->ds.size) | ||
| 98 | return -1; | ||
| 99 | |||
| 100 | /* Now to the test itself. */ | ||
| 101 | for (at = from; (void *)at < to; at += trace->ds.size) { | ||
| 102 | struct bts_struct bts; | ||
| 103 | unsigned long index; | ||
| 104 | int error; | ||
| 105 | |||
| 106 | if (((void *)at - trace->ds.begin) % trace->ds.size) { | ||
| 107 | printk(KERN_CONT | ||
| 108 | "read from non-integer index..."); | ||
| 109 | return -1; | ||
| 110 | } | ||
| 111 | index = ((void *)at - trace->ds.begin) / trace->ds.size; | ||
| 112 | |||
| 113 | memset(&bts, 0, sizeof(bts)); | ||
| 114 | error = trace->read(tracer, at, &bts); | ||
| 115 | if (error < 0) { | ||
| 116 | printk(KERN_CONT | ||
| 117 | "error reading bts trace at [%lu] (0x%p)...", | ||
| 118 | index, at); | ||
| 119 | return error; | ||
| 120 | } | ||
| 121 | |||
| 122 | switch (bts.qualifier) { | ||
| 123 | case BTS_BRANCH: | ||
| 124 | break; | ||
| 125 | default: | ||
| 126 | printk(KERN_CONT | ||
| 127 | "unexpected bts entry %llu at [%lu] (0x%p)...", | ||
| 128 | bts.qualifier, index, at); | ||
| 129 | return -1; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | static void ds_selftest_bts_cpu(void *arg) | ||
| 137 | { | ||
| 138 | struct ds_selftest_bts_conf *conf = arg; | ||
| 139 | const struct bts_trace *trace; | ||
| 140 | void *top; | ||
| 141 | |||
| 142 | if (IS_ERR(conf->tracer)) { | ||
| 143 | conf->error = PTR_ERR(conf->tracer); | ||
| 144 | conf->tracer = NULL; | ||
| 145 | |||
| 146 | printk(KERN_CONT | ||
| 147 | "initialization failed (err: %d)...", conf->error); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | /* We should meanwhile have enough trace. */ | ||
| 152 | conf->error = conf->suspend(conf->tracer); | ||
| 153 | if (conf->error < 0) | ||
| 154 | return; | ||
| 155 | |||
| 156 | /* Let's see if we can access the trace. */ | ||
| 157 | trace = ds_read_bts(conf->tracer); | ||
| 158 | |||
| 159 | conf->error = ds_selftest_bts_consistency(trace); | ||
| 160 | if (conf->error < 0) | ||
| 161 | return; | ||
| 162 | |||
| 163 | /* If everything went well, we should have a few trace entries. */ | ||
| 164 | if (trace->ds.top == trace->ds.begin) { | ||
| 165 | /* | ||
| 166 | * It is possible but highly unlikely that we got a | ||
| 167 | * buffer overflow and end up at exactly the same | ||
| 168 | * position we started from. | ||
| 169 | * Let's issue a warning, but continue. | ||
| 170 | */ | ||
| 171 | printk(KERN_CONT "no trace/overflow..."); | ||
| 172 | } | ||
| 173 | |||
| 174 | /* Let's try to read the trace we collected. */ | ||
| 175 | conf->error = | ||
| 176 | ds_selftest_bts_read(conf->tracer, trace, | ||
| 177 | trace->ds.begin, trace->ds.top); | ||
| 178 | if (conf->error < 0) | ||
| 179 | return; | ||
| 180 | |||
| 181 | /* | ||
| 182 | * Let's read the trace again. | ||
| 183 | * Since we suspended tracing, we should get the same result. | ||
| 184 | */ | ||
| 185 | top = trace->ds.top; | ||
| 186 | |||
| 187 | trace = ds_read_bts(conf->tracer); | ||
| 188 | conf->error = ds_selftest_bts_consistency(trace); | ||
| 189 | if (conf->error < 0) | ||
| 190 | return; | ||
| 191 | |||
| 192 | if (top != trace->ds.top) { | ||
| 193 | printk(KERN_CONT "suspend not working..."); | ||
| 194 | conf->error = -1; | ||
| 195 | return; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* Let's collect some more trace - see if resume is working. */ | ||
| 199 | conf->error = conf->resume(conf->tracer); | ||
| 200 | if (conf->error < 0) | ||
| 201 | return; | ||
| 202 | |||
| 203 | conf->error = conf->suspend(conf->tracer); | ||
| 204 | if (conf->error < 0) | ||
| 205 | return; | ||
| 206 | |||
| 207 | trace = ds_read_bts(conf->tracer); | ||
| 208 | |||
| 209 | conf->error = ds_selftest_bts_consistency(trace); | ||
| 210 | if (conf->error < 0) | ||
| 211 | return; | ||
| 212 | |||
| 213 | if (trace->ds.top == top) { | ||
| 214 | /* | ||
| 215 | * It is possible but highly unlikely that we got a | ||
| 216 | * buffer overflow and end up at exactly the same | ||
| 217 | * position we started from. | ||
| 218 | * Let's issue a warning and check the full trace. | ||
| 219 | */ | ||
| 220 | printk(KERN_CONT | ||
| 221 | "no resume progress/overflow..."); | ||
| 222 | |||
| 223 | conf->error = | ||
| 224 | ds_selftest_bts_read(conf->tracer, trace, | ||
| 225 | trace->ds.begin, trace->ds.end); | ||
| 226 | } else if (trace->ds.top < top) { | ||
| 227 | /* | ||
| 228 | * We had a buffer overflow - the entire buffer should | ||
| 229 | * contain trace records. | ||
| 230 | */ | ||
| 231 | conf->error = | ||
| 232 | ds_selftest_bts_read(conf->tracer, trace, | ||
| 233 | trace->ds.begin, trace->ds.end); | ||
| 234 | } else { | ||
| 235 | /* | ||
| 236 | * It is quite likely that the buffer did not overflow. | ||
| 237 | * Let's just check the delta trace. | ||
| 238 | */ | ||
| 239 | conf->error = | ||
| 240 | ds_selftest_bts_read(conf->tracer, trace, top, | ||
| 241 | trace->ds.top); | ||
| 242 | } | ||
| 243 | if (conf->error < 0) | ||
| 244 | return; | ||
| 245 | |||
| 246 | conf->error = 0; | ||
| 247 | } | ||
| 248 | |||
| 249 | static int ds_suspend_bts_wrap(struct bts_tracer *tracer) | ||
| 250 | { | ||
| 251 | ds_suspend_bts(tracer); | ||
| 252 | return 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | static int ds_resume_bts_wrap(struct bts_tracer *tracer) | ||
| 256 | { | ||
| 257 | ds_resume_bts(tracer); | ||
| 258 | return 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | static void ds_release_bts_noirq_wrap(void *tracer) | ||
| 262 | { | ||
| 263 | (void)ds_release_bts_noirq(tracer); | ||
| 264 | } | ||
| 265 | |||
| 266 | static int ds_selftest_bts_bad_release_noirq(int cpu, | ||
| 267 | struct bts_tracer *tracer) | ||
| 268 | { | ||
| 269 | int error = -EPERM; | ||
| 270 | |||
| 271 | /* Try to release the tracer on the wrong cpu. */ | ||
| 272 | get_cpu(); | ||
| 273 | if (cpu != smp_processor_id()) { | ||
| 274 | error = ds_release_bts_noirq(tracer); | ||
| 275 | if (error != -EPERM) | ||
| 276 | printk(KERN_CONT "release on wrong cpu..."); | ||
| 277 | } | ||
| 278 | put_cpu(); | ||
| 279 | |||
| 280 | return error ? 0 : -1; | ||
| 281 | } | ||
| 282 | |||
| 283 | static int ds_selftest_bts_bad_request_cpu(int cpu, void *buffer) | ||
| 284 | { | ||
| 285 | struct bts_tracer *tracer; | ||
| 286 | int error; | ||
| 287 | |||
| 288 | /* Try to request cpu tracing while task tracing is active. */ | ||
| 289 | tracer = ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE, NULL, | ||
| 290 | (size_t)-1, BTS_KERNEL); | ||
| 291 | error = PTR_ERR(tracer); | ||
| 292 | if (!IS_ERR(tracer)) { | ||
| 293 | ds_release_bts(tracer); | ||
| 294 | error = 0; | ||
| 295 | } | ||
| 296 | |||
| 297 | if (error != -EPERM) | ||
| 298 | printk(KERN_CONT "cpu/task tracing overlap..."); | ||
| 299 | |||
| 300 | return error ? 0 : -1; | ||
| 301 | } | ||
| 302 | |||
| 303 | static int ds_selftest_bts_bad_request_task(void *buffer) | ||
| 304 | { | ||
| 305 | struct bts_tracer *tracer; | ||
| 306 | int error; | ||
| 307 | |||
| 308 | /* Try to request cpu tracing while task tracing is active. */ | ||
| 309 | tracer = ds_request_bts_task(current, buffer, BUFFER_SIZE, NULL, | ||
| 310 | (size_t)-1, BTS_KERNEL); | ||
| 311 | error = PTR_ERR(tracer); | ||
| 312 | if (!IS_ERR(tracer)) { | ||
| 313 | error = 0; | ||
| 314 | ds_release_bts(tracer); | ||
| 315 | } | ||
| 316 | |||
| 317 | if (error != -EPERM) | ||
| 318 | printk(KERN_CONT "task/cpu tracing overlap..."); | ||
| 319 | |||
| 320 | return error ? 0 : -1; | ||
| 321 | } | ||
| 322 | |||
| 323 | int ds_selftest_bts(void) | ||
| 324 | { | ||
| 325 | struct ds_selftest_bts_conf conf; | ||
| 326 | unsigned char buffer[BUFFER_SIZE], *small_buffer; | ||
| 327 | unsigned long irq; | ||
| 328 | int cpu; | ||
| 329 | |||
| 330 | printk(KERN_INFO "[ds] bts selftest..."); | ||
| 331 | conf.error = 0; | ||
| 332 | |||
| 333 | small_buffer = (unsigned char *)ALIGN((unsigned long)buffer, 8) + 8; | ||
| 334 | |||
| 335 | get_online_cpus(); | ||
| 336 | for_each_online_cpu(cpu) { | ||
| 337 | conf.suspend = ds_suspend_bts_wrap; | ||
| 338 | conf.resume = ds_resume_bts_wrap; | ||
| 339 | conf.tracer = | ||
| 340 | ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE, | ||
| 341 | NULL, (size_t)-1, BTS_KERNEL); | ||
| 342 | ds_selftest_bts_cpu(&conf); | ||
| 343 | if (conf.error >= 0) | ||
| 344 | conf.error = ds_selftest_bts_bad_request_task(buffer); | ||
| 345 | ds_release_bts(conf.tracer); | ||
| 346 | if (conf.error < 0) | ||
| 347 | goto out; | ||
| 348 | |||
| 349 | conf.suspend = ds_suspend_bts_noirq; | ||
| 350 | conf.resume = ds_resume_bts_noirq; | ||
| 351 | conf.tracer = | ||
| 352 | ds_request_bts_cpu(cpu, buffer, BUFFER_SIZE, | ||
| 353 | NULL, (size_t)-1, BTS_KERNEL); | ||
| 354 | smp_call_function_single(cpu, ds_selftest_bts_cpu, &conf, 1); | ||
| 355 | if (conf.error >= 0) { | ||
| 356 | conf.error = | ||
| 357 | ds_selftest_bts_bad_release_noirq(cpu, | ||
| 358 | conf.tracer); | ||
| 359 | /* We must not release the tracer twice. */ | ||
| 360 | if (conf.error < 0) | ||
| 361 | conf.tracer = NULL; | ||
| 362 | } | ||
| 363 | if (conf.error >= 0) | ||
| 364 | conf.error = ds_selftest_bts_bad_request_task(buffer); | ||
| 365 | smp_call_function_single(cpu, ds_release_bts_noirq_wrap, | ||
| 366 | conf.tracer, 1); | ||
| 367 | if (conf.error < 0) | ||
| 368 | goto out; | ||
| 369 | } | ||
| 370 | |||
| 371 | conf.suspend = ds_suspend_bts_wrap; | ||
| 372 | conf.resume = ds_resume_bts_wrap; | ||
| 373 | conf.tracer = | ||
| 374 | ds_request_bts_task(current, buffer, BUFFER_SIZE, | ||
| 375 | NULL, (size_t)-1, BTS_KERNEL); | ||
| 376 | ds_selftest_bts_cpu(&conf); | ||
| 377 | if (conf.error >= 0) | ||
| 378 | conf.error = ds_selftest_bts_bad_request_cpu(0, buffer); | ||
| 379 | ds_release_bts(conf.tracer); | ||
| 380 | if (conf.error < 0) | ||
| 381 | goto out; | ||
| 382 | |||
| 383 | conf.suspend = ds_suspend_bts_noirq; | ||
| 384 | conf.resume = ds_resume_bts_noirq; | ||
| 385 | conf.tracer = | ||
| 386 | ds_request_bts_task(current, small_buffer, SMALL_BUFFER_SIZE, | ||
| 387 | NULL, (size_t)-1, BTS_KERNEL); | ||
| 388 | local_irq_save(irq); | ||
| 389 | ds_selftest_bts_cpu(&conf); | ||
| 390 | if (conf.error >= 0) | ||
| 391 | conf.error = ds_selftest_bts_bad_request_cpu(0, buffer); | ||
| 392 | ds_release_bts_noirq(conf.tracer); | ||
| 393 | local_irq_restore(irq); | ||
| 394 | if (conf.error < 0) | ||
| 395 | goto out; | ||
| 396 | |||
| 397 | conf.error = 0; | ||
| 398 | out: | ||
| 399 | put_online_cpus(); | ||
| 400 | printk(KERN_CONT "%s.\n", (conf.error ? "failed" : "passed")); | ||
| 401 | |||
| 402 | return conf.error; | ||
| 403 | } | ||
| 404 | |||
| 405 | int ds_selftest_pebs(void) | ||
| 406 | { | ||
| 407 | return 0; | ||
| 408 | } | ||
diff --git a/arch/x86/kernel/ds_selftest.h b/arch/x86/kernel/ds_selftest.h deleted file mode 100644 index 2ba8745c6663..000000000000 --- a/arch/x86/kernel/ds_selftest.h +++ /dev/null | |||
| @@ -1,15 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Debug Store support - selftest | ||
| 3 | * | ||
| 4 | * | ||
| 5 | * Copyright (C) 2009 Intel Corporation. | ||
| 6 | * Markus Metzger <markus.t.metzger@intel.com>, 2009 | ||
| 7 | */ | ||
| 8 | |||
| 9 | #ifdef CONFIG_X86_DS_SELFTEST | ||
| 10 | extern int ds_selftest_bts(void); | ||
| 11 | extern int ds_selftest_pebs(void); | ||
| 12 | #else | ||
| 13 | static inline int ds_selftest_bts(void) { return 0; } | ||
| 14 | static inline int ds_selftest_pebs(void) { return 0; } | ||
| 15 | #endif | ||
diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c index 6d817554780a..c89a386930b7 100644 --- a/arch/x86/kernel/dumpstack.c +++ b/arch/x86/kernel/dumpstack.c | |||
| @@ -224,11 +224,6 @@ unsigned __kprobes long oops_begin(void) | |||
| 224 | int cpu; | 224 | int cpu; |
| 225 | unsigned long flags; | 225 | unsigned long flags; |
| 226 | 226 | ||
| 227 | /* notify the hw-branch tracer so it may disable tracing and | ||
| 228 | add the last trace to the trace buffer - | ||
| 229 | the earlier this happens, the more useful the trace. */ | ||
| 230 | trace_hw_branch_oops(); | ||
| 231 | |||
| 232 | oops_enter(); | 227 | oops_enter(); |
| 233 | 228 | ||
| 234 | /* racy, but better than risking deadlock. */ | 229 | /* racy, but better than risking deadlock. */ |
diff --git a/arch/x86/kernel/hw_breakpoint.c b/arch/x86/kernel/hw_breakpoint.c index d6cc065f519f..a8f1b803d2fd 100644 --- a/arch/x86/kernel/hw_breakpoint.c +++ b/arch/x86/kernel/hw_breakpoint.c | |||
| @@ -189,25 +189,16 @@ static int get_hbp_len(u8 hbp_len) | |||
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | /* | 191 | /* |
| 192 | * Check for virtual address in user space. | ||
| 193 | */ | ||
| 194 | int arch_check_va_in_userspace(unsigned long va, u8 hbp_len) | ||
| 195 | { | ||
| 196 | unsigned int len; | ||
| 197 | |||
| 198 | len = get_hbp_len(hbp_len); | ||
| 199 | |||
| 200 | return (va <= TASK_SIZE - len); | ||
| 201 | } | ||
| 202 | |||
| 203 | /* | ||
| 204 | * Check for virtual address in kernel space. | 192 | * Check for virtual address in kernel space. |
| 205 | */ | 193 | */ |
| 206 | static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len) | 194 | int arch_check_bp_in_kernelspace(struct perf_event *bp) |
| 207 | { | 195 | { |
| 208 | unsigned int len; | 196 | unsigned int len; |
| 197 | unsigned long va; | ||
| 198 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); | ||
| 209 | 199 | ||
| 210 | len = get_hbp_len(hbp_len); | 200 | va = info->address; |
| 201 | len = get_hbp_len(info->len); | ||
| 211 | 202 | ||
| 212 | return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); | 203 | return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE); |
| 213 | } | 204 | } |
| @@ -300,8 +291,7 @@ static int arch_build_bp_info(struct perf_event *bp) | |||
| 300 | /* | 291 | /* |
| 301 | * Validate the arch-specific HW Breakpoint register settings | 292 | * Validate the arch-specific HW Breakpoint register settings |
| 302 | */ | 293 | */ |
| 303 | int arch_validate_hwbkpt_settings(struct perf_event *bp, | 294 | int arch_validate_hwbkpt_settings(struct perf_event *bp) |
| 304 | struct task_struct *tsk) | ||
| 305 | { | 295 | { |
| 306 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); | 296 | struct arch_hw_breakpoint *info = counter_arch_bp(bp); |
| 307 | unsigned int align; | 297 | unsigned int align; |
| @@ -314,16 +304,6 @@ int arch_validate_hwbkpt_settings(struct perf_event *bp, | |||
| 314 | 304 | ||
| 315 | ret = -EINVAL; | 305 | ret = -EINVAL; |
| 316 | 306 | ||
| 317 | if (info->type == X86_BREAKPOINT_EXECUTE) | ||
| 318 | /* | ||
| 319 | * Ptrace-refactoring code | ||
| 320 | * For now, we'll allow instruction breakpoint only for user-space | ||
| 321 | * addresses | ||
| 322 | */ | ||
| 323 | if ((!arch_check_va_in_userspace(info->address, info->len)) && | ||
| 324 | info->len != X86_BREAKPOINT_EXECUTE) | ||
| 325 | return ret; | ||
| 326 | |||
| 327 | switch (info->len) { | 307 | switch (info->len) { |
| 328 | case X86_BREAKPOINT_LEN_1: | 308 | case X86_BREAKPOINT_LEN_1: |
| 329 | align = 0; | 309 | align = 0; |
| @@ -350,15 +330,6 @@ int arch_validate_hwbkpt_settings(struct perf_event *bp, | |||
| 350 | if (info->address & align) | 330 | if (info->address & align) |
| 351 | return -EINVAL; | 331 | return -EINVAL; |
| 352 | 332 | ||
| 353 | /* Check that the virtual address is in the proper range */ | ||
| 354 | if (tsk) { | ||
| 355 | if (!arch_check_va_in_userspace(info->address, info->len)) | ||
| 356 | return -EFAULT; | ||
| 357 | } else { | ||
| 358 | if (!arch_check_va_in_kernelspace(info->address, info->len)) | ||
| 359 | return -EFAULT; | ||
| 360 | } | ||
| 361 | |||
| 362 | return 0; | 333 | return 0; |
| 363 | } | 334 | } |
| 364 | 335 | ||
diff --git a/arch/x86/kernel/kprobes.c b/arch/x86/kernel/kprobes.c index 1658efdfb4e5..345a4b1fe144 100644 --- a/arch/x86/kernel/kprobes.c +++ b/arch/x86/kernel/kprobes.c | |||
| @@ -422,14 +422,22 @@ static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs, | |||
| 422 | 422 | ||
| 423 | static void __kprobes clear_btf(void) | 423 | static void __kprobes clear_btf(void) |
| 424 | { | 424 | { |
| 425 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | 425 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
| 426 | update_debugctlmsr(0); | 426 | unsigned long debugctl = get_debugctlmsr(); |
| 427 | |||
| 428 | debugctl &= ~DEBUGCTLMSR_BTF; | ||
| 429 | update_debugctlmsr(debugctl); | ||
| 430 | } | ||
| 427 | } | 431 | } |
| 428 | 432 | ||
| 429 | static void __kprobes restore_btf(void) | 433 | static void __kprobes restore_btf(void) |
| 430 | { | 434 | { |
| 431 | if (test_thread_flag(TIF_DEBUGCTLMSR)) | 435 | if (test_thread_flag(TIF_BLOCKSTEP)) { |
| 432 | update_debugctlmsr(current->thread.debugctlmsr); | 436 | unsigned long debugctl = get_debugctlmsr(); |
| 437 | |||
| 438 | debugctl |= DEBUGCTLMSR_BTF; | ||
| 439 | update_debugctlmsr(debugctl); | ||
| 440 | } | ||
| 433 | } | 441 | } |
| 434 | 442 | ||
| 435 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, | 443 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 0415c3ef91b5..cc6877535ef4 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c | |||
| @@ -20,7 +20,6 @@ | |||
| 20 | #include <asm/idle.h> | 20 | #include <asm/idle.h> |
| 21 | #include <asm/uaccess.h> | 21 | #include <asm/uaccess.h> |
| 22 | #include <asm/i387.h> | 22 | #include <asm/i387.h> |
| 23 | #include <asm/ds.h> | ||
| 24 | #include <asm/debugreg.h> | 23 | #include <asm/debugreg.h> |
| 25 | 24 | ||
| 26 | unsigned long idle_halt; | 25 | unsigned long idle_halt; |
| @@ -50,8 +49,6 @@ void free_thread_xstate(struct task_struct *tsk) | |||
| 50 | kmem_cache_free(task_xstate_cachep, tsk->thread.xstate); | 49 | kmem_cache_free(task_xstate_cachep, tsk->thread.xstate); |
| 51 | tsk->thread.xstate = NULL; | 50 | tsk->thread.xstate = NULL; |
| 52 | } | 51 | } |
| 53 | |||
| 54 | WARN(tsk->thread.ds_ctx, "leaking DS context\n"); | ||
| 55 | } | 52 | } |
| 56 | 53 | ||
| 57 | void free_thread_info(struct thread_info *ti) | 54 | void free_thread_info(struct thread_info *ti) |
| @@ -198,11 +195,16 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p, | |||
| 198 | prev = &prev_p->thread; | 195 | prev = &prev_p->thread; |
| 199 | next = &next_p->thread; | 196 | next = &next_p->thread; |
| 200 | 197 | ||
| 201 | if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) || | 198 | if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^ |
| 202 | test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR)) | 199 | test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) { |
| 203 | ds_switch_to(prev_p, next_p); | 200 | unsigned long debugctl = get_debugctlmsr(); |
| 204 | else if (next->debugctlmsr != prev->debugctlmsr) | 201 | |
| 205 | update_debugctlmsr(next->debugctlmsr); | 202 | debugctl &= ~DEBUGCTLMSR_BTF; |
| 203 | if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) | ||
| 204 | debugctl |= DEBUGCTLMSR_BTF; | ||
| 205 | |||
| 206 | update_debugctlmsr(debugctl); | ||
| 207 | } | ||
| 206 | 208 | ||
| 207 | if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^ | 209 | if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^ |
| 208 | test_tsk_thread_flag(next_p, TIF_NOTSC)) { | 210 | test_tsk_thread_flag(next_p, TIF_NOTSC)) { |
diff --git a/arch/x86/kernel/process_32.c b/arch/x86/kernel/process_32.c index f6c62667e30c..75090c589b7a 100644 --- a/arch/x86/kernel/process_32.c +++ b/arch/x86/kernel/process_32.c | |||
| @@ -55,7 +55,6 @@ | |||
| 55 | #include <asm/cpu.h> | 55 | #include <asm/cpu.h> |
| 56 | #include <asm/idle.h> | 56 | #include <asm/idle.h> |
| 57 | #include <asm/syscalls.h> | 57 | #include <asm/syscalls.h> |
| 58 | #include <asm/ds.h> | ||
| 59 | #include <asm/debugreg.h> | 58 | #include <asm/debugreg.h> |
| 60 | 59 | ||
| 61 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); | 60 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); |
| @@ -238,13 +237,6 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, | |||
| 238 | kfree(p->thread.io_bitmap_ptr); | 237 | kfree(p->thread.io_bitmap_ptr); |
| 239 | p->thread.io_bitmap_max = 0; | 238 | p->thread.io_bitmap_max = 0; |
| 240 | } | 239 | } |
| 241 | |||
| 242 | clear_tsk_thread_flag(p, TIF_DS_AREA_MSR); | ||
| 243 | p->thread.ds_ctx = NULL; | ||
| 244 | |||
| 245 | clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR); | ||
| 246 | p->thread.debugctlmsr = 0; | ||
| 247 | |||
| 248 | return err; | 240 | return err; |
| 249 | } | 241 | } |
| 250 | 242 | ||
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 17cb3295cbf7..50cc84ac0a0d 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c | |||
| @@ -49,7 +49,6 @@ | |||
| 49 | #include <asm/ia32.h> | 49 | #include <asm/ia32.h> |
| 50 | #include <asm/idle.h> | 50 | #include <asm/idle.h> |
| 51 | #include <asm/syscalls.h> | 51 | #include <asm/syscalls.h> |
| 52 | #include <asm/ds.h> | ||
| 53 | #include <asm/debugreg.h> | 52 | #include <asm/debugreg.h> |
| 54 | 53 | ||
| 55 | asmlinkage extern void ret_from_fork(void); | 54 | asmlinkage extern void ret_from_fork(void); |
| @@ -313,13 +312,6 @@ int copy_thread(unsigned long clone_flags, unsigned long sp, | |||
| 313 | if (err) | 312 | if (err) |
| 314 | goto out; | 313 | goto out; |
| 315 | } | 314 | } |
| 316 | |||
| 317 | clear_tsk_thread_flag(p, TIF_DS_AREA_MSR); | ||
| 318 | p->thread.ds_ctx = NULL; | ||
| 319 | |||
| 320 | clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR); | ||
| 321 | p->thread.debugctlmsr = 0; | ||
| 322 | |||
| 323 | err = 0; | 315 | err = 0; |
| 324 | out: | 316 | out: |
| 325 | if (err && p->thread.io_bitmap_ptr) { | 317 | if (err && p->thread.io_bitmap_ptr) { |
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c index 2e9b55027b7e..70c4872cd8aa 100644 --- a/arch/x86/kernel/ptrace.c +++ b/arch/x86/kernel/ptrace.c | |||
| @@ -2,9 +2,6 @@ | |||
| 2 | /* | 2 | /* |
| 3 | * Pentium III FXSR, SSE support | 3 | * Pentium III FXSR, SSE support |
| 4 | * Gareth Hughes <gareth@valinux.com>, May 2000 | 4 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 5 | * | ||
| 6 | * BTS tracing | ||
| 7 | * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007 | ||
| 8 | */ | 5 | */ |
| 9 | 6 | ||
| 10 | #include <linux/kernel.h> | 7 | #include <linux/kernel.h> |
| @@ -22,7 +19,6 @@ | |||
| 22 | #include <linux/audit.h> | 19 | #include <linux/audit.h> |
| 23 | #include <linux/seccomp.h> | 20 | #include <linux/seccomp.h> |
| 24 | #include <linux/signal.h> | 21 | #include <linux/signal.h> |
| 25 | #include <linux/workqueue.h> | ||
| 26 | #include <linux/perf_event.h> | 22 | #include <linux/perf_event.h> |
| 27 | #include <linux/hw_breakpoint.h> | 23 | #include <linux/hw_breakpoint.h> |
| 28 | 24 | ||
| @@ -36,7 +32,6 @@ | |||
| 36 | #include <asm/desc.h> | 32 | #include <asm/desc.h> |
| 37 | #include <asm/prctl.h> | 33 | #include <asm/prctl.h> |
| 38 | #include <asm/proto.h> | 34 | #include <asm/proto.h> |
| 39 | #include <asm/ds.h> | ||
| 40 | #include <asm/hw_breakpoint.h> | 35 | #include <asm/hw_breakpoint.h> |
| 41 | 36 | ||
| 42 | #include "tls.h" | 37 | #include "tls.h" |
| @@ -693,7 +688,7 @@ static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr, | |||
| 693 | struct perf_event_attr attr; | 688 | struct perf_event_attr attr; |
| 694 | 689 | ||
| 695 | if (!t->ptrace_bps[nr]) { | 690 | if (!t->ptrace_bps[nr]) { |
| 696 | hw_breakpoint_init(&attr); | 691 | ptrace_breakpoint_init(&attr); |
| 697 | /* | 692 | /* |
| 698 | * Put stub len and type to register (reserve) an inactive but | 693 | * Put stub len and type to register (reserve) an inactive but |
| 699 | * correct bp | 694 | * correct bp |
| @@ -789,342 +784,6 @@ static int ioperm_get(struct task_struct *target, | |||
| 789 | 0, IO_BITMAP_BYTES); | 784 | 0, IO_BITMAP_BYTES); |
| 790 | } | 785 | } |
| 791 | 786 | ||
| 792 | #ifdef CONFIG_X86_PTRACE_BTS | ||
| 793 | /* | ||
| 794 | * A branch trace store context. | ||
| 795 | * | ||
| 796 | * Contexts may only be installed by ptrace_bts_config() and only for | ||
| 797 | * ptraced tasks. | ||
| 798 | * | ||
| 799 | * Contexts are destroyed when the tracee is detached from the tracer. | ||
| 800 | * The actual destruction work requires interrupts enabled, so the | ||
| 801 | * work is deferred and will be scheduled during __ptrace_unlink(). | ||
| 802 | * | ||
| 803 | * Contexts hold an additional task_struct reference on the traced | ||
| 804 | * task, as well as a reference on the tracer's mm. | ||
| 805 | * | ||
| 806 | * Ptrace already holds a task_struct for the duration of ptrace operations, | ||
| 807 | * but since destruction is deferred, it may be executed after both | ||
| 808 | * tracer and tracee exited. | ||
| 809 | */ | ||
| 810 | struct bts_context { | ||
| 811 | /* The branch trace handle. */ | ||
| 812 | struct bts_tracer *tracer; | ||
| 813 | |||
| 814 | /* The buffer used to store the branch trace and its size. */ | ||
| 815 | void *buffer; | ||
| 816 | unsigned int size; | ||
| 817 | |||
| 818 | /* The mm that paid for the above buffer. */ | ||
| 819 | struct mm_struct *mm; | ||
| 820 | |||
| 821 | /* The task this context belongs to. */ | ||
| 822 | struct task_struct *task; | ||
| 823 | |||
| 824 | /* The signal to send on a bts buffer overflow. */ | ||
| 825 | unsigned int bts_ovfl_signal; | ||
| 826 | |||
| 827 | /* The work struct to destroy a context. */ | ||
| 828 | struct work_struct work; | ||
| 829 | }; | ||
| 830 | |||
| 831 | static int alloc_bts_buffer(struct bts_context *context, unsigned int size) | ||
| 832 | { | ||
| 833 | void *buffer = NULL; | ||
| 834 | int err = -ENOMEM; | ||
| 835 | |||
| 836 | err = account_locked_memory(current->mm, current->signal->rlim, size); | ||
| 837 | if (err < 0) | ||
| 838 | return err; | ||
| 839 | |||
| 840 | buffer = kzalloc(size, GFP_KERNEL); | ||
| 841 | if (!buffer) | ||
| 842 | goto out_refund; | ||
| 843 | |||
| 844 | context->buffer = buffer; | ||
| 845 | context->size = size; | ||
| 846 | context->mm = get_task_mm(current); | ||
| 847 | |||
| 848 | return 0; | ||
| 849 | |||
| 850 | out_refund: | ||
| 851 | refund_locked_memory(current->mm, size); | ||
| 852 | return err; | ||
| 853 | } | ||
| 854 | |||
| 855 | static inline void free_bts_buffer(struct bts_context *context) | ||
| 856 | { | ||
| 857 | if (!context->buffer) | ||
| 858 | return; | ||
| 859 | |||
| 860 | kfree(context->buffer); | ||
| 861 | context->buffer = NULL; | ||
| 862 | |||
| 863 | refund_locked_memory(context->mm, context->size); | ||
| 864 | context->size = 0; | ||
| 865 | |||
| 866 | mmput(context->mm); | ||
| 867 | context->mm = NULL; | ||
| 868 | } | ||
| 869 | |||
| 870 | static void free_bts_context_work(struct work_struct *w) | ||
| 871 | { | ||
| 872 | struct bts_context *context; | ||
| 873 | |||
| 874 | context = container_of(w, struct bts_context, work); | ||
| 875 | |||
| 876 | ds_release_bts(context->tracer); | ||
| 877 | put_task_struct(context->task); | ||
| 878 | free_bts_buffer(context); | ||
| 879 | kfree(context); | ||
| 880 | } | ||
| 881 | |||
| 882 | static inline void free_bts_context(struct bts_context *context) | ||
| 883 | { | ||
| 884 | INIT_WORK(&context->work, free_bts_context_work); | ||
| 885 | schedule_work(&context->work); | ||
| 886 | } | ||
| 887 | |||
| 888 | static inline struct bts_context *alloc_bts_context(struct task_struct *task) | ||
| 889 | { | ||
| 890 | struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL); | ||
| 891 | if (context) { | ||
| 892 | context->task = task; | ||
| 893 | task->bts = context; | ||
| 894 | |||
| 895 | get_task_struct(task); | ||
| 896 | } | ||
| 897 | |||
| 898 | return context; | ||
| 899 | } | ||
| 900 | |||
| 901 | static int ptrace_bts_read_record(struct task_struct *child, size_t index, | ||
| 902 | struct bts_struct __user *out) | ||
| 903 | { | ||
| 904 | struct bts_context *context; | ||
| 905 | const struct bts_trace *trace; | ||
| 906 | struct bts_struct bts; | ||
| 907 | const unsigned char *at; | ||
| 908 | int error; | ||
| 909 | |||
| 910 | context = child->bts; | ||
| 911 | if (!context) | ||
| 912 | return -ESRCH; | ||
| 913 | |||
| 914 | trace = ds_read_bts(context->tracer); | ||
| 915 | if (!trace) | ||
| 916 | return -ESRCH; | ||
| 917 | |||
| 918 | at = trace->ds.top - ((index + 1) * trace->ds.size); | ||
| 919 | if ((void *)at < trace->ds.begin) | ||
| 920 | at += (trace->ds.n * trace->ds.size); | ||
| 921 | |||
| 922 | if (!trace->read) | ||
| 923 | return -EOPNOTSUPP; | ||
| 924 | |||
| 925 | error = trace->read(context->tracer, at, &bts); | ||
| 926 | if (error < 0) | ||
| 927 | return error; | ||
| 928 | |||
| 929 | if (copy_to_user(out, &bts, sizeof(bts))) | ||
| 930 | return -EFAULT; | ||
| 931 | |||
| 932 | return sizeof(bts); | ||
| 933 | } | ||
| 934 | |||
| 935 | static int ptrace_bts_drain(struct task_struct *child, | ||
| 936 | long size, | ||
| 937 | struct bts_struct __user *out) | ||
| 938 | { | ||
| 939 | struct bts_context *context; | ||
| 940 | const struct bts_trace *trace; | ||
| 941 | const unsigned char *at; | ||
| 942 | int error, drained = 0; | ||
| 943 | |||
| 944 | context = child->bts; | ||
| 945 | if (!context) | ||
| 946 | return -ESRCH; | ||
| 947 | |||
| 948 | trace = ds_read_bts(context->tracer); | ||
| 949 | if (!trace) | ||
| 950 | return -ESRCH; | ||
| 951 | |||
| 952 | if (!trace->read) | ||
| 953 | return -EOPNOTSUPP; | ||
| 954 | |||
| 955 | if (size < (trace->ds.top - trace->ds.begin)) | ||
| 956 | return -EIO; | ||
| 957 | |||
| 958 | for (at = trace->ds.begin; (void *)at < trace->ds.top; | ||
| 959 | out++, drained++, at += trace->ds.size) { | ||
| 960 | struct bts_struct bts; | ||
| 961 | |||
| 962 | error = trace->read(context->tracer, at, &bts); | ||
| 963 | if (error < 0) | ||
| 964 | return error; | ||
| 965 | |||
| 966 | if (copy_to_user(out, &bts, sizeof(bts))) | ||
| 967 | return -EFAULT; | ||
| 968 | } | ||
| 969 | |||
| 970 | memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size); | ||
| 971 | |||
| 972 | error = ds_reset_bts(context->tracer); | ||
| 973 | if (error < 0) | ||
| 974 | return error; | ||
| 975 | |||
| 976 | return drained; | ||
| 977 | } | ||
| 978 | |||
| 979 | static int ptrace_bts_config(struct task_struct *child, | ||
| 980 | long cfg_size, | ||
| 981 | const struct ptrace_bts_config __user *ucfg) | ||
| 982 | { | ||
| 983 | struct bts_context *context; | ||
| 984 | struct ptrace_bts_config cfg; | ||
| 985 | unsigned int flags = 0; | ||
| 986 | |||
| 987 | if (cfg_size < sizeof(cfg)) | ||
| 988 | return -EIO; | ||
| 989 | |||
| 990 | if (copy_from_user(&cfg, ucfg, sizeof(cfg))) | ||
| 991 | return -EFAULT; | ||
| 992 | |||
| 993 | context = child->bts; | ||
| 994 | if (!context) | ||
| 995 | context = alloc_bts_context(child); | ||
| 996 | if (!context) | ||
| 997 | return -ENOMEM; | ||
| 998 | |||
| 999 | if (cfg.flags & PTRACE_BTS_O_SIGNAL) { | ||
| 1000 | if (!cfg.signal) | ||
| 1001 | return -EINVAL; | ||
| 1002 | |||
| 1003 | return -EOPNOTSUPP; | ||
| 1004 | context->bts_ovfl_signal = cfg.signal; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | ds_release_bts(context->tracer); | ||
| 1008 | context->tracer = NULL; | ||
| 1009 | |||
| 1010 | if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) { | ||
| 1011 | int err; | ||
| 1012 | |||
| 1013 | free_bts_buffer(context); | ||
| 1014 | if (!cfg.size) | ||
| 1015 | return 0; | ||
| 1016 | |||
| 1017 | err = alloc_bts_buffer(context, cfg.size); | ||
| 1018 | if (err < 0) | ||
| 1019 | return err; | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | if (cfg.flags & PTRACE_BTS_O_TRACE) | ||
| 1023 | flags |= BTS_USER; | ||
| 1024 | |||
| 1025 | if (cfg.flags & PTRACE_BTS_O_SCHED) | ||
| 1026 | flags |= BTS_TIMESTAMPS; | ||
| 1027 | |||
| 1028 | context->tracer = | ||
| 1029 | ds_request_bts_task(child, context->buffer, context->size, | ||
| 1030 | NULL, (size_t)-1, flags); | ||
| 1031 | if (unlikely(IS_ERR(context->tracer))) { | ||
| 1032 | int error = PTR_ERR(context->tracer); | ||
| 1033 | |||
| 1034 | free_bts_buffer(context); | ||
| 1035 | context->tracer = NULL; | ||
| 1036 | return error; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | return sizeof(cfg); | ||
| 1040 | } | ||
| 1041 | |||
| 1042 | static int ptrace_bts_status(struct task_struct *child, | ||
| 1043 | long cfg_size, | ||
| 1044 | struct ptrace_bts_config __user *ucfg) | ||
| 1045 | { | ||
| 1046 | struct bts_context *context; | ||
| 1047 | const struct bts_trace *trace; | ||
| 1048 | struct ptrace_bts_config cfg; | ||
| 1049 | |||
| 1050 | context = child->bts; | ||
| 1051 | if (!context) | ||
| 1052 | return -ESRCH; | ||
| 1053 | |||
| 1054 | if (cfg_size < sizeof(cfg)) | ||
| 1055 | return -EIO; | ||
| 1056 | |||
| 1057 | trace = ds_read_bts(context->tracer); | ||
| 1058 | if (!trace) | ||
| 1059 | return -ESRCH; | ||
| 1060 | |||
| 1061 | memset(&cfg, 0, sizeof(cfg)); | ||
| 1062 | cfg.size = trace->ds.end - trace->ds.begin; | ||
| 1063 | cfg.signal = context->bts_ovfl_signal; | ||
| 1064 | cfg.bts_size = sizeof(struct bts_struct); | ||
| 1065 | |||
| 1066 | if (cfg.signal) | ||
| 1067 | cfg.flags |= PTRACE_BTS_O_SIGNAL; | ||
| 1068 | |||
| 1069 | if (trace->ds.flags & BTS_USER) | ||
| 1070 | cfg.flags |= PTRACE_BTS_O_TRACE; | ||
| 1071 | |||
| 1072 | if (trace->ds.flags & BTS_TIMESTAMPS) | ||
| 1073 | cfg.flags |= PTRACE_BTS_O_SCHED; | ||
| 1074 | |||
| 1075 | if (copy_to_user(ucfg, &cfg, sizeof(cfg))) | ||
| 1076 | return -EFAULT; | ||
| 1077 | |||
| 1078 | return sizeof(cfg); | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | static int ptrace_bts_clear(struct task_struct *child) | ||
| 1082 | { | ||
| 1083 | struct bts_context *context; | ||
| 1084 | const struct bts_trace *trace; | ||
| 1085 | |||
| 1086 | context = child->bts; | ||
| 1087 | if (!context) | ||
| 1088 | return -ESRCH; | ||
| 1089 | |||
| 1090 | trace = ds_read_bts(context->tracer); | ||
| 1091 | if (!trace) | ||
| 1092 | return -ESRCH; | ||
| 1093 | |||
| 1094 | memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size); | ||
| 1095 | |||
| 1096 | return ds_reset_bts(context->tracer); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | static int ptrace_bts_size(struct task_struct *child) | ||
| 1100 | { | ||
| 1101 | struct bts_context *context; | ||
| 1102 | const struct bts_trace *trace; | ||
| 1103 | |||
| 1104 | context = child->bts; | ||
| 1105 | if (!context) | ||
| 1106 | return -ESRCH; | ||
| 1107 | |||
| 1108 | trace = ds_read_bts(context->tracer); | ||
| 1109 | if (!trace) | ||
| 1110 | return -ESRCH; | ||
| 1111 | |||
| 1112 | return (trace->ds.top - trace->ds.begin) / trace->ds.size; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | /* | ||
| 1116 | * Called from __ptrace_unlink() after the child has been moved back | ||
| 1117 | * to its original parent. | ||
| 1118 | */ | ||
| 1119 | void ptrace_bts_untrace(struct task_struct *child) | ||
| 1120 | { | ||
| 1121 | if (unlikely(child->bts)) { | ||
| 1122 | free_bts_context(child->bts); | ||
| 1123 | child->bts = NULL; | ||
| 1124 | } | ||
| 1125 | } | ||
| 1126 | #endif /* CONFIG_X86_PTRACE_BTS */ | ||
| 1127 | |||
| 1128 | /* | 787 | /* |
| 1129 | * Called by kernel/ptrace.c when detaching.. | 788 | * Called by kernel/ptrace.c when detaching.. |
| 1130 | * | 789 | * |
| @@ -1252,39 +911,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data) | |||
| 1252 | break; | 911 | break; |
| 1253 | #endif | 912 | #endif |
| 1254 | 913 | ||
| 1255 | /* | ||
| 1256 | * These bits need more cooking - not enabled yet: | ||
| 1257 | */ | ||
| 1258 | #ifdef CONFIG_X86_PTRACE_BTS | ||
| 1259 | case PTRACE_BTS_CONFIG: | ||
| 1260 | ret = ptrace_bts_config | ||
| 1261 | (child, data, (struct ptrace_bts_config __user *)addr); | ||
| 1262 | break; | ||
| 1263 | |||
| 1264 | case PTRACE_BTS_STATUS: | ||
| 1265 | ret = ptrace_bts_status | ||
| 1266 | (child, data, (struct ptrace_bts_config __user *)addr); | ||
| 1267 | break; | ||
| 1268 | |||
| 1269 | case PTRACE_BTS_SIZE: | ||
| 1270 | ret = ptrace_bts_size(child); | ||
| 1271 | break; | ||
| 1272 | |||
| 1273 | case PTRACE_BTS_GET: | ||
| 1274 | ret = ptrace_bts_read_record | ||
| 1275 | (child, data, (struct bts_struct __user *) addr); | ||
| 1276 | break; | ||
| 1277 | |||
| 1278 | case PTRACE_BTS_CLEAR: | ||
| 1279 | ret = ptrace_bts_clear(child); | ||
| 1280 | break; | ||
| 1281 | |||
| 1282 | case PTRACE_BTS_DRAIN: | ||
| 1283 | ret = ptrace_bts_drain | ||
| 1284 | (child, data, (struct bts_struct __user *) addr); | ||
| 1285 | break; | ||
| 1286 | #endif /* CONFIG_X86_PTRACE_BTS */ | ||
| 1287 | |||
| 1288 | default: | 914 | default: |
| 1289 | ret = ptrace_request(child, request, addr, data); | 915 | ret = ptrace_request(child, request, addr, data); |
| 1290 | break; | 916 | break; |
| @@ -1544,14 +1170,6 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request, | |||
| 1544 | 1170 | ||
| 1545 | case PTRACE_GET_THREAD_AREA: | 1171 | case PTRACE_GET_THREAD_AREA: |
| 1546 | case PTRACE_SET_THREAD_AREA: | 1172 | case PTRACE_SET_THREAD_AREA: |
| 1547 | #ifdef CONFIG_X86_PTRACE_BTS | ||
| 1548 | case PTRACE_BTS_CONFIG: | ||
| 1549 | case PTRACE_BTS_STATUS: | ||
| 1550 | case PTRACE_BTS_SIZE: | ||
| 1551 | case PTRACE_BTS_GET: | ||
| 1552 | case PTRACE_BTS_CLEAR: | ||
| 1553 | case PTRACE_BTS_DRAIN: | ||
| 1554 | #endif /* CONFIG_X86_PTRACE_BTS */ | ||
| 1555 | return arch_ptrace(child, request, addr, data); | 1173 | return arch_ptrace(child, request, addr, data); |
| 1556 | 1174 | ||
| 1557 | default: | 1175 | default: |
diff --git a/arch/x86/kernel/step.c b/arch/x86/kernel/step.c index 3149032ff107..58de45ee08b6 100644 --- a/arch/x86/kernel/step.c +++ b/arch/x86/kernel/step.c | |||
| @@ -158,22 +158,6 @@ static int enable_single_step(struct task_struct *child) | |||
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | /* | 160 | /* |
| 161 | * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running. | ||
| 162 | */ | ||
| 163 | static void write_debugctlmsr(struct task_struct *child, unsigned long val) | ||
| 164 | { | ||
| 165 | if (child->thread.debugctlmsr == val) | ||
| 166 | return; | ||
| 167 | |||
| 168 | child->thread.debugctlmsr = val; | ||
| 169 | |||
| 170 | if (child != current) | ||
| 171 | return; | ||
| 172 | |||
| 173 | update_debugctlmsr(val); | ||
| 174 | } | ||
| 175 | |||
| 176 | /* | ||
| 177 | * Enable single or block step. | 161 | * Enable single or block step. |
| 178 | */ | 162 | */ |
| 179 | static void enable_step(struct task_struct *child, bool block) | 163 | static void enable_step(struct task_struct *child, bool block) |
| @@ -186,15 +170,17 @@ static void enable_step(struct task_struct *child, bool block) | |||
| 186 | * that uses user-mode single stepping itself. | 170 | * that uses user-mode single stepping itself. |
| 187 | */ | 171 | */ |
| 188 | if (enable_single_step(child) && block) { | 172 | if (enable_single_step(child) && block) { |
| 189 | set_tsk_thread_flag(child, TIF_DEBUGCTLMSR); | 173 | unsigned long debugctl = get_debugctlmsr(); |
| 190 | write_debugctlmsr(child, | 174 | |
| 191 | child->thread.debugctlmsr | DEBUGCTLMSR_BTF); | 175 | debugctl |= DEBUGCTLMSR_BTF; |
| 192 | } else { | 176 | update_debugctlmsr(debugctl); |
| 193 | write_debugctlmsr(child, | 177 | set_tsk_thread_flag(child, TIF_BLOCKSTEP); |
| 194 | child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF); | 178 | } else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) { |
| 195 | 179 | unsigned long debugctl = get_debugctlmsr(); | |
| 196 | if (!child->thread.debugctlmsr) | 180 | |
| 197 | clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); | 181 | debugctl &= ~DEBUGCTLMSR_BTF; |
| 182 | update_debugctlmsr(debugctl); | ||
| 183 | clear_tsk_thread_flag(child, TIF_BLOCKSTEP); | ||
| 198 | } | 184 | } |
| 199 | } | 185 | } |
| 200 | 186 | ||
| @@ -213,11 +199,13 @@ void user_disable_single_step(struct task_struct *child) | |||
| 213 | /* | 199 | /* |
| 214 | * Make sure block stepping (BTF) is disabled. | 200 | * Make sure block stepping (BTF) is disabled. |
| 215 | */ | 201 | */ |
| 216 | write_debugctlmsr(child, | 202 | if (test_tsk_thread_flag(child, TIF_BLOCKSTEP)) { |
| 217 | child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF); | 203 | unsigned long debugctl = get_debugctlmsr(); |
| 218 | 204 | ||
| 219 | if (!child->thread.debugctlmsr) | 205 | debugctl &= ~DEBUGCTLMSR_BTF; |
| 220 | clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR); | 206 | update_debugctlmsr(debugctl); |
| 207 | clear_tsk_thread_flag(child, TIF_BLOCKSTEP); | ||
| 208 | } | ||
| 221 | 209 | ||
| 222 | /* Always clear TIF_SINGLESTEP... */ | 210 | /* Always clear TIF_SINGLESTEP... */ |
| 223 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); | 211 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 1168e4454188..36f1bd9f8e76 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c | |||
| @@ -543,11 +543,11 @@ dotraplinkage void __kprobes do_debug(struct pt_regs *regs, long error_code) | |||
| 543 | 543 | ||
| 544 | /* DR6 may or may not be cleared by the CPU */ | 544 | /* DR6 may or may not be cleared by the CPU */ |
| 545 | set_debugreg(0, 6); | 545 | set_debugreg(0, 6); |
| 546 | |||
| 546 | /* | 547 | /* |
| 547 | * The processor cleared BTF, so don't mark that we need it set. | 548 | * The processor cleared BTF, so don't mark that we need it set. |
| 548 | */ | 549 | */ |
| 549 | clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR); | 550 | clear_tsk_thread_flag(tsk, TIF_BLOCKSTEP); |
| 550 | tsk->thread.debugctlmsr = 0; | ||
| 551 | 551 | ||
| 552 | /* Store the virtualized DR6 value */ | 552 | /* Store the virtualized DR6 value */ |
| 553 | tsk->thread.debugreg6 = dr6; | 553 | tsk->thread.debugreg6 = dr6; |
