Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 1 | /* |
Guo Chao | c7a7062 | 2012-06-28 15:23:08 +0800 | [diff] [blame] | 2 | * Kernel-based Virtual Machine -- Performance Monitoring Unit support |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Avi Kivity <avi@redhat.com> |
| 8 | * Gleb Natapov <gleb@redhat.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 11 | * the COPYING file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kvm_host.h> |
| 17 | #include <linux/perf_event.h> |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 18 | #include <asm/perf_event.h> |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 19 | #include "x86.h" |
| 20 | #include "cpuid.h" |
| 21 | #include "lapic.h" |
Wei Huang | 474a5bb | 2015-06-19 13:54:23 +0200 | [diff] [blame] | 22 | #include "pmu.h" |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 23 | |
Wei Huang | 474a5bb | 2015-06-19 13:54:23 +0200 | [diff] [blame] | 24 | static struct kvm_event_hw_type_mapping arch_events[] = { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 25 | /* Index must match CPUID 0x0A.EBX bit vector */ |
| 26 | [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES }, |
| 27 | [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS }, |
| 28 | [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES }, |
| 29 | [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES }, |
| 30 | [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES }, |
| 31 | [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 32 | [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES }, |
Gleb Natapov | 62079d8 | 2012-02-26 16:55:42 +0200 | [diff] [blame] | 33 | [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* mapping between fixed pmc index and arch_events array */ |
Xiubo Li | 52eb5a6 | 2015-03-13 17:39:45 +0800 | [diff] [blame] | 37 | static int fixed_pmc_events[] = {1, 0, 7}; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 38 | |
| 39 | static bool pmc_is_gp(struct kvm_pmc *pmc) |
| 40 | { |
| 41 | return pmc->type == KVM_PMC_GP; |
| 42 | } |
| 43 | |
| 44 | static inline u64 pmc_bitmask(struct kvm_pmc *pmc) |
| 45 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 46 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 47 | |
| 48 | return pmu->counter_bitmask[pmc->type]; |
| 49 | } |
| 50 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 51 | static inline bool pmc_is_enabled(struct kvm_pmc *pmc) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 52 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 53 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 54 | return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); |
| 55 | } |
| 56 | |
| 57 | static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, |
| 58 | u32 base) |
| 59 | { |
| 60 | if (msr >= base && msr < base + pmu->nr_arch_gp_counters) |
| 61 | return &pmu->gp_counters[msr - base]; |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr) |
| 66 | { |
| 67 | int base = MSR_CORE_PERF_FIXED_CTR0; |
| 68 | if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) |
| 69 | return &pmu->fixed_counters[msr - base]; |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | static inline struct kvm_pmc *get_fixed_pmc_idx(struct kvm_pmu *pmu, int idx) |
| 74 | { |
| 75 | return get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + idx); |
| 76 | } |
| 77 | |
| 78 | static struct kvm_pmc *global_idx_to_pmc(struct kvm_pmu *pmu, int idx) |
| 79 | { |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 80 | if (idx < INTEL_PMC_IDX_FIXED) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 81 | return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + idx, MSR_P6_EVNTSEL0); |
| 82 | else |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 83 | return get_fixed_pmc_idx(pmu, idx - INTEL_PMC_IDX_FIXED); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 86 | void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 87 | { |
| 88 | if (vcpu->arch.apic) |
| 89 | kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC); |
| 90 | } |
| 91 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 92 | static void kvm_pmi_trigger_fn(struct irq_work *irq_work) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 93 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 94 | struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); |
| 95 | struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 96 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 97 | kvm_pmu_deliver_pmi(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void kvm_perf_overflow(struct perf_event *perf_event, |
| 101 | struct perf_sample_data *data, |
| 102 | struct pt_regs *regs) |
| 103 | { |
| 104 | struct kvm_pmc *pmc = perf_event->overflow_handler_context; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 105 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Nadav Amit | 671bd99 | 2014-04-18 03:35:08 +0300 | [diff] [blame] | 106 | if (!test_and_set_bit(pmc->idx, (unsigned long *)&pmu->reprogram_pmi)) { |
| 107 | __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); |
| 108 | kvm_make_request(KVM_REQ_PMU, pmc->vcpu); |
| 109 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void kvm_perf_overflow_intr(struct perf_event *perf_event, |
| 113 | struct perf_sample_data *data, struct pt_regs *regs) |
| 114 | { |
| 115 | struct kvm_pmc *pmc = perf_event->overflow_handler_context; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 116 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 117 | if (!test_and_set_bit(pmc->idx, (unsigned long *)&pmu->reprogram_pmi)) { |
Nadav Amit | 671bd99 | 2014-04-18 03:35:08 +0300 | [diff] [blame] | 118 | __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 119 | kvm_make_request(KVM_REQ_PMU, pmc->vcpu); |
| 120 | /* |
| 121 | * Inject PMI. If vcpu was in a guest mode during NMI PMI |
| 122 | * can be ejected on a guest mode re-entry. Otherwise we can't |
| 123 | * be sure that vcpu wasn't executing hlt instruction at the |
| 124 | * time of vmexit and is not going to re-enter guest mode until, |
| 125 | * woken up. So we should wake it, but this is impossible from |
| 126 | * NMI context. Do it from irq work instead. |
| 127 | */ |
| 128 | if (!kvm_is_in_guest()) |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 129 | irq_work_queue(&pmc_to_pmu(pmc)->irq_work); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 130 | else |
| 131 | kvm_make_request(KVM_REQ_PMI, pmc->vcpu); |
| 132 | } |
| 133 | } |
| 134 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 135 | static u64 pmc_read_counter(struct kvm_pmc *pmc) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 136 | { |
| 137 | u64 counter, enabled, running; |
| 138 | |
| 139 | counter = pmc->counter; |
| 140 | |
| 141 | if (pmc->perf_event) |
| 142 | counter += perf_event_read_value(pmc->perf_event, |
| 143 | &enabled, &running); |
| 144 | |
| 145 | /* FIXME: Scaling needed? */ |
| 146 | |
| 147 | return counter & pmc_bitmask(pmc); |
| 148 | } |
| 149 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 150 | static void pmc_stop_counter(struct kvm_pmc *pmc) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 151 | { |
| 152 | if (pmc->perf_event) { |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 153 | pmc->counter = pmc_read_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 154 | perf_event_release_kernel(pmc->perf_event); |
| 155 | pmc->perf_event = NULL; |
| 156 | } |
| 157 | } |
| 158 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 159 | static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 160 | unsigned config, bool exclude_user, bool exclude_kernel, |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 161 | bool intr, bool in_tx, bool in_tx_cp) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 162 | { |
| 163 | struct perf_event *event; |
| 164 | struct perf_event_attr attr = { |
| 165 | .type = type, |
| 166 | .size = sizeof(attr), |
| 167 | .pinned = true, |
| 168 | .exclude_idle = true, |
| 169 | .exclude_host = 1, |
| 170 | .exclude_user = exclude_user, |
| 171 | .exclude_kernel = exclude_kernel, |
| 172 | .config = config, |
| 173 | }; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 174 | if (in_tx) |
| 175 | attr.config |= HSW_IN_TX; |
| 176 | if (in_tx_cp) |
| 177 | attr.config |= HSW_IN_TX_CHECKPOINTED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 178 | |
| 179 | attr.sample_period = (-pmc->counter) & pmc_bitmask(pmc); |
| 180 | |
| 181 | event = perf_event_create_kernel_counter(&attr, -1, current, |
| 182 | intr ? kvm_perf_overflow_intr : |
| 183 | kvm_perf_overflow, pmc); |
| 184 | if (IS_ERR(event)) { |
| 185 | printk_once("kvm: pmu event creation failed %ld\n", |
| 186 | PTR_ERR(event)); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | pmc->perf_event = event; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 191 | clear_bit(pmc->idx, (unsigned long*)&pmc_to_pmu(pmc)->reprogram_pmi); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, |
| 195 | u8 unit_mask) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(arch_events); i++) |
| 200 | if (arch_events[i].eventsel == event_select |
| 201 | && arch_events[i].unit_mask == unit_mask |
| 202 | && (pmu->available_event_types & (1 << i))) |
| 203 | break; |
| 204 | |
| 205 | if (i == ARRAY_SIZE(arch_events)) |
| 206 | return PERF_COUNT_HW_MAX; |
| 207 | |
| 208 | return arch_events[i].event_type; |
| 209 | } |
| 210 | |
| 211 | static void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) |
| 212 | { |
| 213 | unsigned config, type = PERF_TYPE_RAW; |
| 214 | u8 event_select, unit_mask; |
| 215 | |
Gleb Natapov | a7b9d2c | 2012-02-26 16:55:40 +0200 | [diff] [blame] | 216 | if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) |
| 217 | printk_once("kvm pmu: pin control bit is ignored\n"); |
| 218 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 219 | pmc->eventsel = eventsel; |
| 220 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 221 | pmc_stop_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 222 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 223 | if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 224 | return; |
| 225 | |
| 226 | event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT; |
| 227 | unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; |
| 228 | |
Gleb Natapov | fac3368 | 2012-02-26 16:55:41 +0200 | [diff] [blame] | 229 | if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 230 | ARCH_PERFMON_EVENTSEL_INV | |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 231 | ARCH_PERFMON_EVENTSEL_CMASK | |
| 232 | HSW_IN_TX | |
| 233 | HSW_IN_TX_CHECKPOINTED))) { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 234 | config = find_arch_event(pmc_to_pmu(pmc), event_select, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 235 | unit_mask); |
| 236 | if (config != PERF_COUNT_HW_MAX) |
| 237 | type = PERF_TYPE_HARDWARE; |
| 238 | } |
| 239 | |
| 240 | if (type == PERF_TYPE_RAW) |
| 241 | config = eventsel & X86_RAW_EVENT_MASK; |
| 242 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 243 | pmc_reprogram_counter(pmc, type, config, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 244 | !(eventsel & ARCH_PERFMON_EVENTSEL_USR), |
| 245 | !(eventsel & ARCH_PERFMON_EVENTSEL_OS), |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 246 | eventsel & ARCH_PERFMON_EVENTSEL_INT, |
| 247 | (eventsel & HSW_IN_TX), |
| 248 | (eventsel & HSW_IN_TX_CHECKPOINTED)); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx) |
| 252 | { |
| 253 | unsigned en = en_pmi & 0x3; |
| 254 | bool pmi = en_pmi & 0x8; |
| 255 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 256 | pmc_stop_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 257 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 258 | if (!en || !pmc_is_enabled(pmc)) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 259 | return; |
| 260 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 261 | pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 262 | arch_events[fixed_pmc_events[idx]].event_type, |
| 263 | !(en & 0x2), /* exclude user */ |
| 264 | !(en & 0x1), /* exclude kernel */ |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 265 | pmi, false, false); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 268 | static inline u8 fixed_ctrl_field(u64 ctrl, int idx) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 269 | { |
| 270 | return (ctrl >> (idx * 4)) & 0xf; |
| 271 | } |
| 272 | |
| 273 | static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) |
| 274 | { |
| 275 | int i; |
| 276 | |
| 277 | for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 278 | u8 en_pmi = fixed_ctrl_field(data, i); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 279 | struct kvm_pmc *pmc = get_fixed_pmc_idx(pmu, i); |
| 280 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 281 | if (fixed_ctrl_field(pmu->fixed_ctr_ctrl, i) == en_pmi) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 282 | continue; |
| 283 | |
| 284 | reprogram_fixed_counter(pmc, en_pmi, i); |
| 285 | } |
| 286 | |
| 287 | pmu->fixed_ctr_ctrl = data; |
| 288 | } |
| 289 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 290 | static void reprogram_counter(struct kvm_pmu *pmu, int idx) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 291 | { |
| 292 | struct kvm_pmc *pmc = global_idx_to_pmc(pmu, idx); |
| 293 | |
| 294 | if (!pmc) |
| 295 | return; |
| 296 | |
| 297 | if (pmc_is_gp(pmc)) |
| 298 | reprogram_gp_counter(pmc, pmc->eventsel); |
| 299 | else { |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 300 | int fidx = idx - INTEL_PMC_IDX_FIXED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 301 | reprogram_fixed_counter(pmc, |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 302 | fixed_ctrl_field(pmu->fixed_ctr_ctrl, fidx), fidx); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) |
| 307 | { |
| 308 | int bit; |
| 309 | u64 diff = pmu->global_ctrl ^ data; |
| 310 | |
| 311 | pmu->global_ctrl = data; |
| 312 | |
| 313 | for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 314 | reprogram_counter(pmu, bit); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 317 | bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 318 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 319 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 320 | int ret; |
| 321 | |
| 322 | switch (msr) { |
| 323 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 324 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 325 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 326 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 327 | ret = pmu->version > 1; |
| 328 | break; |
| 329 | default: |
| 330 | ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) |
| 331 | || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) |
| 332 | || get_fixed_pmc(pmu, msr); |
| 333 | break; |
| 334 | } |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
| 339 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 340 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 341 | struct kvm_pmc *pmc; |
| 342 | |
| 343 | switch (index) { |
| 344 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 345 | *data = pmu->fixed_ctr_ctrl; |
| 346 | return 0; |
| 347 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 348 | *data = pmu->global_status; |
| 349 | return 0; |
| 350 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 351 | *data = pmu->global_ctrl; |
| 352 | return 0; |
| 353 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 354 | *data = pmu->global_ovf_ctrl; |
| 355 | return 0; |
| 356 | default: |
| 357 | if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || |
| 358 | (pmc = get_fixed_pmc(pmu, index))) { |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 359 | *data = pmc_read_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { |
| 362 | *data = pmc->eventsel; |
| 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | return 1; |
| 367 | } |
| 368 | |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 369 | int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 370 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 371 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 372 | struct kvm_pmc *pmc; |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 373 | u32 index = msr_info->index; |
| 374 | u64 data = msr_info->data; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 375 | |
| 376 | switch (index) { |
| 377 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 378 | if (pmu->fixed_ctr_ctrl == data) |
| 379 | return 0; |
Sasikantha babu | fea5295 | 2012-03-21 18:49:00 +0530 | [diff] [blame] | 380 | if (!(data & 0xfffffffffffff444ull)) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 381 | reprogram_fixed_counters(pmu, data); |
| 382 | return 0; |
| 383 | } |
| 384 | break; |
| 385 | case MSR_CORE_PERF_GLOBAL_STATUS: |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 386 | if (msr_info->host_initiated) { |
| 387 | pmu->global_status = data; |
| 388 | return 0; |
| 389 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 390 | break; /* RO MSR */ |
| 391 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 392 | if (pmu->global_ctrl == data) |
| 393 | return 0; |
| 394 | if (!(data & pmu->global_ctrl_mask)) { |
| 395 | global_ctrl_changed(pmu, data); |
| 396 | return 0; |
| 397 | } |
| 398 | break; |
| 399 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 400 | if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) { |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 401 | if (!msr_info->host_initiated) |
| 402 | pmu->global_status &= ~data; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 403 | pmu->global_ovf_ctrl = data; |
| 404 | return 0; |
| 405 | } |
| 406 | break; |
| 407 | default: |
| 408 | if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || |
| 409 | (pmc = get_fixed_pmc(pmu, index))) { |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 410 | if (!msr_info->host_initiated) |
| 411 | data = (s64)(s32)data; |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 412 | pmc->counter += data - pmc_read_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 413 | return 0; |
| 414 | } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { |
| 415 | if (data == pmc->eventsel) |
| 416 | return 0; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 417 | if (!(data & pmu->reserved_bits)) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 418 | reprogram_gp_counter(pmc, data); |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 426 | int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned pmc) |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 427 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 428 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Nadav Amit | 67f4d42 | 2014-06-02 18:34:09 +0300 | [diff] [blame] | 429 | bool fixed = pmc & (1u << 30); |
| 430 | pmc &= ~(3u << 30); |
| 431 | return (!fixed && pmc >= pmu->nr_arch_gp_counters) || |
| 432 | (fixed && pmc >= pmu->nr_arch_fixed_counters); |
| 433 | } |
| 434 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 435 | int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 436 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 437 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 438 | bool fast_mode = pmc & (1u << 31); |
| 439 | bool fixed = pmc & (1u << 30); |
| 440 | struct kvm_pmc *counters; |
| 441 | u64 ctr; |
| 442 | |
Gleb Natapov | 270c6c7 | 2012-02-16 14:44:11 +0200 | [diff] [blame] | 443 | pmc &= ~(3u << 30); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 444 | if (!fixed && pmc >= pmu->nr_arch_gp_counters) |
| 445 | return 1; |
| 446 | if (fixed && pmc >= pmu->nr_arch_fixed_counters) |
| 447 | return 1; |
| 448 | counters = fixed ? pmu->fixed_counters : pmu->gp_counters; |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 449 | ctr = pmc_read_counter(&counters[pmc]); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 450 | if (fast_mode) |
| 451 | ctr = (u32)ctr; |
| 452 | *data = ctr; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 457 | void kvm_pmu_refresh(struct kvm_vcpu *vcpu) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 458 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 459 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 460 | struct kvm_cpuid_entry2 *entry; |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 461 | union cpuid10_eax eax; |
| 462 | union cpuid10_edx edx; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 463 | |
| 464 | pmu->nr_arch_gp_counters = 0; |
| 465 | pmu->nr_arch_fixed_counters = 0; |
| 466 | pmu->counter_bitmask[KVM_PMC_GP] = 0; |
| 467 | pmu->counter_bitmask[KVM_PMC_FIXED] = 0; |
| 468 | pmu->version = 0; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 469 | pmu->reserved_bits = 0xffffffff00200000ull; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 470 | |
| 471 | entry = kvm_find_cpuid_entry(vcpu, 0xa, 0); |
| 472 | if (!entry) |
| 473 | return; |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 474 | eax.full = entry->eax; |
| 475 | edx.full = entry->edx; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 476 | |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 477 | pmu->version = eax.split.version_id; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 478 | if (!pmu->version) |
| 479 | return; |
| 480 | |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 481 | pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters, |
| 482 | INTEL_PMC_MAX_GENERIC); |
| 483 | pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1; |
| 484 | pmu->available_event_types = ~entry->ebx & |
| 485 | ((1ull << eax.split.mask_length) - 1); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 486 | |
| 487 | if (pmu->version == 1) { |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 488 | pmu->nr_arch_fixed_counters = 0; |
| 489 | } else { |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 490 | pmu->nr_arch_fixed_counters = |
| 491 | min_t(int, edx.split.num_counters_fixed, |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 492 | INTEL_PMC_MAX_FIXED); |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 493 | pmu->counter_bitmask[KVM_PMC_FIXED] = |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 494 | ((u64)1 << edx.split.bit_width_fixed) - 1; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 497 | pmu->global_ctrl = ((1 << pmu->nr_arch_gp_counters) - 1) | |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 498 | (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED); |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 499 | pmu->global_ctrl_mask = ~pmu->global_ctrl; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 500 | |
| 501 | entry = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 502 | if (entry && |
| 503 | (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) && |
| 504 | (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) |
| 505 | pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void kvm_pmu_init(struct kvm_vcpu *vcpu) |
| 509 | { |
| 510 | int i; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 511 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 512 | |
| 513 | memset(pmu, 0, sizeof(*pmu)); |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 514 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 515 | pmu->gp_counters[i].type = KVM_PMC_GP; |
| 516 | pmu->gp_counters[i].vcpu = vcpu; |
| 517 | pmu->gp_counters[i].idx = i; |
| 518 | } |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 519 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 520 | pmu->fixed_counters[i].type = KVM_PMC_FIXED; |
| 521 | pmu->fixed_counters[i].vcpu = vcpu; |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 522 | pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 523 | } |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 524 | init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); |
| 525 | kvm_pmu_refresh(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void kvm_pmu_reset(struct kvm_vcpu *vcpu) |
| 529 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 530 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 531 | int i; |
| 532 | |
| 533 | irq_work_sync(&pmu->irq_work); |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 534 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 535 | struct kvm_pmc *pmc = &pmu->gp_counters[i]; |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 536 | pmc_stop_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 537 | pmc->counter = pmc->eventsel = 0; |
| 538 | } |
| 539 | |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 540 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 541 | pmc_stop_counter(&pmu->fixed_counters[i]); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 542 | |
| 543 | pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = |
| 544 | pmu->global_ovf_ctrl = 0; |
| 545 | } |
| 546 | |
| 547 | void kvm_pmu_destroy(struct kvm_vcpu *vcpu) |
| 548 | { |
| 549 | kvm_pmu_reset(vcpu); |
| 550 | } |
| 551 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 552 | void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 553 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame^] | 554 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 555 | u64 bitmask; |
| 556 | int bit; |
| 557 | |
| 558 | bitmask = pmu->reprogram_pmi; |
| 559 | |
| 560 | for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) { |
| 561 | struct kvm_pmc *pmc = global_idx_to_pmc(pmu, bit); |
| 562 | |
| 563 | if (unlikely(!pmc || !pmc->perf_event)) { |
| 564 | clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi); |
| 565 | continue; |
| 566 | } |
| 567 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 568 | reprogram_counter(pmu, bit); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 569 | } |
| 570 | } |