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 | static void kvm_pmi_trigger_fn(struct irq_work *irq_work) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 87 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 88 | struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); |
| 89 | struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 90 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 91 | kvm_pmu_deliver_pmi(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void kvm_perf_overflow(struct perf_event *perf_event, |
| 95 | struct perf_sample_data *data, |
| 96 | struct pt_regs *regs) |
| 97 | { |
| 98 | struct kvm_pmc *pmc = perf_event->overflow_handler_context; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 99 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 100 | |
| 101 | if (!test_and_set_bit(pmc->idx, |
| 102 | (unsigned long *)&pmu->reprogram_pmi)) { |
Nadav Amit | 671bd99 | 2014-04-18 03:35:08 +0300 | [diff] [blame] | 103 | __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); |
| 104 | kvm_make_request(KVM_REQ_PMU, pmc->vcpu); |
| 105 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void kvm_perf_overflow_intr(struct perf_event *perf_event, |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 109 | struct perf_sample_data *data, |
| 110 | struct pt_regs *regs) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 111 | { |
| 112 | struct kvm_pmc *pmc = perf_event->overflow_handler_context; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 113 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 114 | |
| 115 | if (!test_and_set_bit(pmc->idx, |
| 116 | (unsigned long *)&pmu->reprogram_pmi)) { |
Nadav Amit | 671bd99 | 2014-04-18 03:35:08 +0300 | [diff] [blame] | 117 | __set_bit(pmc->idx, (unsigned long *)&pmu->global_status); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 118 | kvm_make_request(KVM_REQ_PMU, pmc->vcpu); |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 119 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 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 |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 124 | * time of vmexit and is not going to re-enter guest mode until |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 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, |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 160 | unsigned config, bool exclude_user, |
| 161 | bool exclude_kernel, bool intr, |
| 162 | bool in_tx, bool in_tx_cp) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 163 | { |
| 164 | struct perf_event *event; |
| 165 | struct perf_event_attr attr = { |
| 166 | .type = type, |
| 167 | .size = sizeof(attr), |
| 168 | .pinned = true, |
| 169 | .exclude_idle = true, |
| 170 | .exclude_host = 1, |
| 171 | .exclude_user = exclude_user, |
| 172 | .exclude_kernel = exclude_kernel, |
| 173 | .config = config, |
| 174 | }; |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 175 | |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 176 | if (in_tx) |
| 177 | attr.config |= HSW_IN_TX; |
| 178 | if (in_tx_cp) |
| 179 | attr.config |= HSW_IN_TX_CHECKPOINTED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 180 | |
| 181 | attr.sample_period = (-pmc->counter) & pmc_bitmask(pmc); |
| 182 | |
| 183 | event = perf_event_create_kernel_counter(&attr, -1, current, |
| 184 | intr ? kvm_perf_overflow_intr : |
| 185 | kvm_perf_overflow, pmc); |
| 186 | if (IS_ERR(event)) { |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 187 | printk_once("kvm_pmu: event creation failed %ld\n", |
| 188 | PTR_ERR(event)); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
| 192 | pmc->perf_event = event; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 193 | clear_bit(pmc->idx, (unsigned long*)&pmc_to_pmu(pmc)->reprogram_pmi); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, |
| 197 | u8 unit_mask) |
| 198 | { |
| 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < ARRAY_SIZE(arch_events); i++) |
| 202 | if (arch_events[i].eventsel == event_select |
| 203 | && arch_events[i].unit_mask == unit_mask |
| 204 | && (pmu->available_event_types & (1 << i))) |
| 205 | break; |
| 206 | |
| 207 | if (i == ARRAY_SIZE(arch_events)) |
| 208 | return PERF_COUNT_HW_MAX; |
| 209 | |
| 210 | return arch_events[i].event_type; |
| 211 | } |
| 212 | |
| 213 | static void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) |
| 214 | { |
| 215 | unsigned config, type = PERF_TYPE_RAW; |
| 216 | u8 event_select, unit_mask; |
| 217 | |
Gleb Natapov | a7b9d2c | 2012-02-26 16:55:40 +0200 | [diff] [blame] | 218 | if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) |
| 219 | printk_once("kvm pmu: pin control bit is ignored\n"); |
| 220 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 221 | pmc->eventsel = eventsel; |
| 222 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 223 | pmc_stop_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 224 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 225 | if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 226 | return; |
| 227 | |
| 228 | event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT; |
| 229 | unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8; |
| 230 | |
Gleb Natapov | fac3368 | 2012-02-26 16:55:41 +0200 | [diff] [blame] | 231 | if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 232 | ARCH_PERFMON_EVENTSEL_INV | |
| 233 | ARCH_PERFMON_EVENTSEL_CMASK | |
| 234 | HSW_IN_TX | |
| 235 | HSW_IN_TX_CHECKPOINTED))) { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 236 | config = find_arch_event(pmc_to_pmu(pmc), event_select, |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 237 | unit_mask); |
| 238 | if (config != PERF_COUNT_HW_MAX) |
| 239 | type = PERF_TYPE_HARDWARE; |
| 240 | } |
| 241 | |
| 242 | if (type == PERF_TYPE_RAW) |
| 243 | config = eventsel & X86_RAW_EVENT_MASK; |
| 244 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 245 | pmc_reprogram_counter(pmc, type, config, |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 246 | !(eventsel & ARCH_PERFMON_EVENTSEL_USR), |
| 247 | !(eventsel & ARCH_PERFMON_EVENTSEL_OS), |
| 248 | eventsel & ARCH_PERFMON_EVENTSEL_INT, |
| 249 | (eventsel & HSW_IN_TX), |
| 250 | (eventsel & HSW_IN_TX_CHECKPOINTED)); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 253 | static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 254 | { |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 255 | unsigned en_field = ctrl & 0x3; |
| 256 | bool pmi = ctrl & 0x8; |
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 | pmc_stop_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 259 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 260 | if (!en_field || !pmc_is_enabled(pmc)) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 261 | return; |
| 262 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 263 | pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE, |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 264 | arch_events[fixed_pmc_events[idx]].event_type, |
| 265 | !(en_field & 0x2), /* exclude user */ |
| 266 | !(en_field & 0x1), /* exclude kernel */ |
| 267 | pmi, false, false); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 270 | static inline u8 fixed_ctrl_field(u64 ctrl, int idx) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 271 | { |
| 272 | return (ctrl >> (idx * 4)) & 0xf; |
| 273 | } |
| 274 | |
| 275 | static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) |
| 276 | { |
| 277 | int i; |
| 278 | |
| 279 | for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 280 | u8 old_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i); |
| 281 | u8 new_ctrl = fixed_ctrl_field(data, i); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 282 | struct kvm_pmc *pmc = get_fixed_pmc_idx(pmu, i); |
| 283 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 284 | if (old_ctrl == new_ctrl) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 285 | continue; |
| 286 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 287 | reprogram_fixed_counter(pmc, new_ctrl, i); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | pmu->fixed_ctr_ctrl = data; |
| 291 | } |
| 292 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 293 | static void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 294 | { |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 295 | struct kvm_pmc *pmc = global_idx_to_pmc(pmu, pmc_idx); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 296 | |
| 297 | if (!pmc) |
| 298 | return; |
| 299 | |
| 300 | if (pmc_is_gp(pmc)) |
| 301 | reprogram_gp_counter(pmc, pmc->eventsel); |
| 302 | else { |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 303 | int idx = pmc_idx - INTEL_PMC_IDX_FIXED; |
| 304 | u8 ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, idx); |
| 305 | |
| 306 | reprogram_fixed_counter(pmc, ctrl, idx); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) |
| 311 | { |
| 312 | int bit; |
| 313 | u64 diff = pmu->global_ctrl ^ data; |
| 314 | |
| 315 | pmu->global_ctrl = data; |
| 316 | |
| 317 | for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 318 | reprogram_counter(pmu, bit); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Wei Huang | e5af058 | 2015-06-19 15:51:47 +0200 | [diff] [blame^] | 321 | void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) |
| 322 | { |
| 323 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 324 | u64 bitmask; |
| 325 | int bit; |
| 326 | |
| 327 | bitmask = pmu->reprogram_pmi; |
| 328 | |
| 329 | for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) { |
| 330 | struct kvm_pmc *pmc = global_idx_to_pmc(pmu, bit); |
| 331 | |
| 332 | if (unlikely(!pmc || !pmc->perf_event)) { |
| 333 | clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi); |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | reprogram_counter(pmu, bit); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* check if idx is a valid index to access PMU */ |
| 342 | int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx) |
| 343 | { |
| 344 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 345 | bool fixed = idx & (1u << 30); |
| 346 | idx &= ~(3u << 30); |
| 347 | return (!fixed && idx >= pmu->nr_arch_gp_counters) || |
| 348 | (fixed && idx >= pmu->nr_arch_fixed_counters); |
| 349 | } |
| 350 | |
| 351 | int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) |
| 352 | { |
| 353 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 354 | bool fast_mode = idx & (1u << 31); |
| 355 | bool fixed = idx & (1u << 30); |
| 356 | struct kvm_pmc *counters; |
| 357 | u64 ctr_val; |
| 358 | |
| 359 | idx &= ~(3u << 30); |
| 360 | if (!fixed && idx >= pmu->nr_arch_gp_counters) |
| 361 | return 1; |
| 362 | if (fixed && idx >= pmu->nr_arch_fixed_counters) |
| 363 | return 1; |
| 364 | counters = fixed ? pmu->fixed_counters : pmu->gp_counters; |
| 365 | |
| 366 | ctr_val = pmc_read_counter(&counters[idx]); |
| 367 | if (fast_mode) |
| 368 | ctr_val = (u32)ctr_val; |
| 369 | |
| 370 | *data = ctr_val; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu) |
| 375 | { |
| 376 | if (vcpu->arch.apic) |
| 377 | kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC); |
| 378 | } |
| 379 | |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 380 | bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 381 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 382 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 383 | int ret; |
| 384 | |
| 385 | switch (msr) { |
| 386 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 387 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 388 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 389 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 390 | ret = pmu->version > 1; |
| 391 | break; |
| 392 | default: |
| 393 | ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) |
| 394 | || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) |
| 395 | || get_fixed_pmc(pmu, msr); |
| 396 | break; |
| 397 | } |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
| 402 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 403 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 404 | struct kvm_pmc *pmc; |
| 405 | |
| 406 | switch (index) { |
| 407 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 408 | *data = pmu->fixed_ctr_ctrl; |
| 409 | return 0; |
| 410 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 411 | *data = pmu->global_status; |
| 412 | return 0; |
| 413 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 414 | *data = pmu->global_ctrl; |
| 415 | return 0; |
| 416 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 417 | *data = pmu->global_ovf_ctrl; |
| 418 | return 0; |
| 419 | default: |
| 420 | if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || |
| 421 | (pmc = get_fixed_pmc(pmu, index))) { |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 422 | *data = pmc_read_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 423 | return 0; |
| 424 | } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { |
| 425 | *data = pmc->eventsel; |
| 426 | return 0; |
| 427 | } |
| 428 | } |
| 429 | return 1; |
| 430 | } |
| 431 | |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 432 | 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] | 433 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 434 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 435 | struct kvm_pmc *pmc; |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 436 | u32 index = msr_info->index; |
| 437 | u64 data = msr_info->data; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 438 | |
| 439 | switch (index) { |
| 440 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 441 | if (pmu->fixed_ctr_ctrl == data) |
| 442 | return 0; |
Sasikantha babu | fea5295 | 2012-03-21 18:49:00 +0530 | [diff] [blame] | 443 | if (!(data & 0xfffffffffffff444ull)) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 444 | reprogram_fixed_counters(pmu, data); |
| 445 | return 0; |
| 446 | } |
| 447 | break; |
| 448 | case MSR_CORE_PERF_GLOBAL_STATUS: |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 449 | if (msr_info->host_initiated) { |
| 450 | pmu->global_status = data; |
| 451 | return 0; |
| 452 | } |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 453 | break; /* RO MSR */ |
| 454 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 455 | if (pmu->global_ctrl == data) |
| 456 | return 0; |
| 457 | if (!(data & pmu->global_ctrl_mask)) { |
| 458 | global_ctrl_changed(pmu, data); |
| 459 | return 0; |
| 460 | } |
| 461 | break; |
| 462 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 463 | if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) { |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 464 | if (!msr_info->host_initiated) |
| 465 | pmu->global_status &= ~data; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 466 | pmu->global_ovf_ctrl = data; |
| 467 | return 0; |
| 468 | } |
| 469 | break; |
| 470 | default: |
| 471 | if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) || |
| 472 | (pmc = get_fixed_pmc(pmu, index))) { |
Paolo Bonzini | afd80d8 | 2013-03-28 17:18:35 +0100 | [diff] [blame] | 473 | if (!msr_info->host_initiated) |
| 474 | data = (s64)(s32)data; |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 475 | pmc->counter += data - pmc_read_counter(pmc); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 476 | return 0; |
| 477 | } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) { |
| 478 | if (data == pmc->eventsel) |
| 479 | return 0; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 480 | if (!(data & pmu->reserved_bits)) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 481 | reprogram_gp_counter(pmc, data); |
| 482 | return 0; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | return 1; |
| 487 | } |
| 488 | |
Wei Huang | e84cfe4 | 2015-06-19 14:15:28 +0200 | [diff] [blame] | 489 | /* refresh PMU settings. This function generally is called when underlying |
| 490 | * settings are changed (such as changes of PMU CPUID by guest VMs), which |
| 491 | * should rarely happen. |
| 492 | */ |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 493 | void kvm_pmu_refresh(struct kvm_vcpu *vcpu) |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 494 | { |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 495 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 496 | struct kvm_cpuid_entry2 *entry; |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 497 | union cpuid10_eax eax; |
| 498 | union cpuid10_edx edx; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 499 | |
| 500 | pmu->nr_arch_gp_counters = 0; |
| 501 | pmu->nr_arch_fixed_counters = 0; |
| 502 | pmu->counter_bitmask[KVM_PMC_GP] = 0; |
| 503 | pmu->counter_bitmask[KVM_PMC_FIXED] = 0; |
| 504 | pmu->version = 0; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 505 | pmu->reserved_bits = 0xffffffff00200000ull; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 506 | |
| 507 | entry = kvm_find_cpuid_entry(vcpu, 0xa, 0); |
| 508 | if (!entry) |
| 509 | return; |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 510 | eax.full = entry->eax; |
| 511 | edx.full = entry->edx; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 512 | |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 513 | pmu->version = eax.split.version_id; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 514 | if (!pmu->version) |
| 515 | return; |
| 516 | |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 517 | pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters, |
| 518 | INTEL_PMC_MAX_GENERIC); |
| 519 | pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1; |
| 520 | pmu->available_event_types = ~entry->ebx & |
| 521 | ((1ull << eax.split.mask_length) - 1); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 522 | |
| 523 | if (pmu->version == 1) { |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 524 | pmu->nr_arch_fixed_counters = 0; |
| 525 | } else { |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 526 | pmu->nr_arch_fixed_counters = |
| 527 | min_t(int, edx.split.num_counters_fixed, |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 528 | INTEL_PMC_MAX_FIXED); |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 529 | pmu->counter_bitmask[KVM_PMC_FIXED] = |
Nadav Amit | d27aa7f | 2014-08-20 13:25:52 +0300 | [diff] [blame] | 530 | ((u64)1 << edx.split.bit_width_fixed) - 1; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 533 | pmu->global_ctrl = ((1 << pmu->nr_arch_gp_counters) - 1) | |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 534 | (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED); |
Gleb Natapov | f19a0c2 | 2012-04-09 17:38:35 +0300 | [diff] [blame] | 535 | pmu->global_ctrl_mask = ~pmu->global_ctrl; |
Andi Kleen | 103af0a | 2013-07-18 15:57:02 -0700 | [diff] [blame] | 536 | |
| 537 | entry = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 538 | if (entry && |
| 539 | (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) && |
| 540 | (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) |
| 541 | pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Wei Huang | e5af058 | 2015-06-19 15:51:47 +0200 | [diff] [blame^] | 544 | void kvm_pmu_reset(struct kvm_vcpu *vcpu) |
| 545 | { |
| 546 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 547 | int i; |
| 548 | |
| 549 | irq_work_sync(&pmu->irq_work); |
| 550 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
| 551 | struct kvm_pmc *pmc = &pmu->gp_counters[i]; |
| 552 | pmc_stop_counter(pmc); |
| 553 | pmc->counter = pmc->eventsel = 0; |
| 554 | } |
| 555 | |
| 556 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) |
| 557 | pmc_stop_counter(&pmu->fixed_counters[i]); |
| 558 | |
| 559 | pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = |
| 560 | pmu->global_ovf_ctrl = 0; |
| 561 | } |
| 562 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 563 | void kvm_pmu_init(struct kvm_vcpu *vcpu) |
| 564 | { |
| 565 | int i; |
Wei Huang | 212dba1 | 2015-06-19 14:00:33 +0200 | [diff] [blame] | 566 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 567 | |
| 568 | memset(pmu, 0, sizeof(*pmu)); |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 569 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 570 | pmu->gp_counters[i].type = KVM_PMC_GP; |
| 571 | pmu->gp_counters[i].vcpu = vcpu; |
| 572 | pmu->gp_counters[i].idx = i; |
| 573 | } |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 574 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 575 | pmu->fixed_counters[i].type = KVM_PMC_FIXED; |
| 576 | pmu->fixed_counters[i].vcpu = vcpu; |
Robert Richter | 15c7ad5 | 2012-06-20 20:46:33 +0200 | [diff] [blame] | 577 | pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 578 | } |
Wei Huang | c6702c9 | 2015-06-19 13:44:45 +0200 | [diff] [blame] | 579 | init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); |
| 580 | kvm_pmu_refresh(vcpu); |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Gleb Natapov | f5132b0 | 2011-11-10 14:57:22 +0200 | [diff] [blame] | 583 | void kvm_pmu_destroy(struct kvm_vcpu *vcpu) |
| 584 | { |
| 585 | kvm_pmu_reset(vcpu); |
| 586 | } |