Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2 | /* |
| 3 | * KVM Microsoft Hyper-V emulation |
| 4 | * |
| 5 | * derived from arch/x86/kvm/x86.c |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * Copyright (C) 2008 Qumranet, Inc. |
| 9 | * Copyright IBM Corporation, 2008 |
| 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
| 11 | * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> |
| 12 | * |
| 13 | * Authors: |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * Yaniv Kamay <yaniv@qumranet.com> |
| 16 | * Amit Shah <amit.shah@qumranet.com> |
| 17 | * Ben-Ami Yassour <benami@il.ibm.com> |
| 18 | * Andrey Smetanin <asmetanin@virtuozzo.com> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "x86.h" |
| 22 | #include "lapic.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 23 | #include "ioapic.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 24 | #include "hyperv.h" |
| 25 | |
Vitaly Kuznetsov | b2d8b16 | 2019-09-16 18:22:57 +0200 | [diff] [blame^] | 26 | #include <linux/cpu.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 27 | #include <linux/kvm_host.h> |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 28 | #include <linux/highmem.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 29 | #include <linux/sched/cputime.h> |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 30 | #include <linux/eventfd.h> |
Ingo Molnar | 32ef551 | 2017-02-05 11:48:36 +0100 | [diff] [blame] | 31 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 32 | #include <asm/apicdef.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 33 | #include <trace/events/kvm.h> |
| 34 | |
| 35 | #include "trace.h" |
| 36 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 37 | #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) |
| 38 | |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 39 | static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, |
| 40 | bool vcpu_kick); |
| 41 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 42 | static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) |
| 43 | { |
| 44 | return atomic64_read(&synic->sint[sint]); |
| 45 | } |
| 46 | |
| 47 | static inline int synic_get_sint_vector(u64 sint_value) |
| 48 | { |
| 49 | if (sint_value & HV_SYNIC_SINT_MASKED) |
| 50 | return -1; |
| 51 | return sint_value & HV_SYNIC_SINT_VECTOR_MASK; |
| 52 | } |
| 53 | |
| 54 | static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, |
| 55 | int vector) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 60 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, |
| 67 | int vector) |
| 68 | { |
| 69 | int i; |
| 70 | u64 sint_value; |
| 71 | |
| 72 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 73 | sint_value = synic_read_sint(synic, i); |
| 74 | if (synic_get_sint_vector(sint_value) == vector && |
| 75 | sint_value & HV_SYNIC_SINT_AUTO_EOI) |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 81 | static void synic_update_vector(struct kvm_vcpu_hv_synic *synic, |
| 82 | int vector) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 83 | { |
Vitaly Kuznetsov | 87a8d79 | 2018-12-05 16:36:21 +0100 | [diff] [blame] | 84 | if (vector < HV_SYNIC_FIRST_VALID_VECTOR) |
| 85 | return; |
| 86 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 87 | if (synic_has_vector_connected(synic, vector)) |
| 88 | __set_bit(vector, synic->vec_bitmap); |
| 89 | else |
| 90 | __clear_bit(vector, synic->vec_bitmap); |
| 91 | |
| 92 | if (synic_has_vector_auto_eoi(synic, vector)) |
| 93 | __set_bit(vector, synic->auto_eoi_bitmap); |
| 94 | else |
| 95 | __clear_bit(vector, synic->auto_eoi_bitmap); |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, |
| 99 | u64 data, bool host) |
| 100 | { |
| 101 | int vector, old_vector; |
Vitaly Kuznetsov | 915e6f7 | 2018-03-01 15:15:14 +0100 | [diff] [blame] | 102 | bool masked; |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 103 | |
| 104 | vector = data & HV_SYNIC_SINT_VECTOR_MASK; |
Vitaly Kuznetsov | 915e6f7 | 2018-03-01 15:15:14 +0100 | [diff] [blame] | 105 | masked = data & HV_SYNIC_SINT_MASKED; |
| 106 | |
| 107 | /* |
| 108 | * Valid vectors are 16-255, however, nested Hyper-V attempts to write |
| 109 | * default '0x10000' value on boot and this should not #GP. We need to |
| 110 | * allow zero-initing the register from host as well. |
| 111 | */ |
| 112 | if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 113 | return 1; |
| 114 | /* |
| 115 | * Guest may configure multiple SINTs to use the same vector, so |
| 116 | * we maintain a bitmap of vectors handled by synic, and a |
| 117 | * bitmap of vectors with auto-eoi behavior. The bitmaps are |
| 118 | * updated here, and atomically queried on fast paths. |
| 119 | */ |
| 120 | old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK; |
| 121 | |
| 122 | atomic64_set(&synic->sint[sint], data); |
| 123 | |
| 124 | synic_update_vector(synic, old_vector); |
| 125 | |
| 126 | synic_update_vector(synic, vector); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 127 | |
| 128 | /* Load SynIC vectors into EOI exit bitmap */ |
| 129 | kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); |
| 130 | return 0; |
| 131 | } |
| 132 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 133 | static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) |
| 134 | { |
| 135 | struct kvm_vcpu *vcpu = NULL; |
| 136 | int i; |
| 137 | |
Vitaly Kuznetsov | 9170200 | 2018-08-22 12:18:28 +0200 | [diff] [blame] | 138 | if (vpidx >= KVM_MAX_VCPUS) |
| 139 | return NULL; |
| 140 | |
| 141 | vcpu = kvm_get_vcpu(kvm, vpidx); |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 142 | if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx) |
| 143 | return vcpu; |
| 144 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 145 | if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx) |
| 146 | return vcpu; |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 151 | { |
| 152 | struct kvm_vcpu *vcpu; |
| 153 | struct kvm_vcpu_hv_synic *synic; |
| 154 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 155 | vcpu = get_vcpu_by_vpidx(kvm, vpidx); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 156 | if (!vcpu) |
| 157 | return NULL; |
| 158 | synic = vcpu_to_synic(vcpu); |
| 159 | return (synic->active) ? synic : NULL; |
| 160 | } |
| 161 | |
| 162 | static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) |
| 163 | { |
| 164 | struct kvm *kvm = vcpu->kvm; |
Andrey Smetanin | 765eaa0 | 2015-11-30 19:22:20 +0300 | [diff] [blame] | 165 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 166 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 167 | struct kvm_vcpu_hv_stimer *stimer; |
Vitaly Kuznetsov | 08a800a | 2018-11-26 16:47:32 +0100 | [diff] [blame] | 168 | int gsi, idx; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 169 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 170 | trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 171 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 172 | /* Try to deliver pending Hyper-V SynIC timers messages */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 173 | for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { |
| 174 | stimer = &hv_vcpu->stimer[idx]; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 175 | if (stimer->msg_pending && stimer->config.enable && |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 176 | !stimer->config.direct_mode && |
Vitaly Kuznetsov | 08a800a | 2018-11-26 16:47:32 +0100 | [diff] [blame] | 177 | stimer->config.sintx == sint) |
| 178 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 179 | } |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 180 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 181 | idx = srcu_read_lock(&kvm->irq_srcu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 182 | gsi = atomic_read(&synic->sint_to_gsi[sint]); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 183 | if (gsi != -1) |
| 184 | kvm_notify_acked_gsi(kvm, gsi); |
| 185 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 186 | } |
| 187 | |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 188 | static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) |
| 189 | { |
| 190 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 191 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
| 192 | |
| 193 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; |
| 194 | hv_vcpu->exit.u.synic.msr = msr; |
| 195 | hv_vcpu->exit.u.synic.control = synic->control; |
| 196 | hv_vcpu->exit.u.synic.evt_page = synic->evt_page; |
| 197 | hv_vcpu->exit.u.synic.msg_page = synic->msg_page; |
| 198 | |
| 199 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 200 | } |
| 201 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 202 | static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, |
| 203 | u32 msr, u64 data, bool host) |
| 204 | { |
| 205 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 206 | int ret; |
| 207 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 208 | if (!synic->active && !host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 209 | return 1; |
| 210 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 211 | trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); |
| 212 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 213 | ret = 0; |
| 214 | switch (msr) { |
| 215 | case HV_X64_MSR_SCONTROL: |
| 216 | synic->control = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 217 | if (!host) |
| 218 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 219 | break; |
| 220 | case HV_X64_MSR_SVERSION: |
| 221 | if (!host) { |
| 222 | ret = 1; |
| 223 | break; |
| 224 | } |
| 225 | synic->version = data; |
| 226 | break; |
| 227 | case HV_X64_MSR_SIEFP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 228 | if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && |
| 229 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 230 | if (kvm_clear_guest(vcpu->kvm, |
| 231 | data & PAGE_MASK, PAGE_SIZE)) { |
| 232 | ret = 1; |
| 233 | break; |
| 234 | } |
| 235 | synic->evt_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 236 | if (!host) |
| 237 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 238 | break; |
| 239 | case HV_X64_MSR_SIMP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 240 | if ((data & HV_SYNIC_SIMP_ENABLE) && !host && |
| 241 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 242 | if (kvm_clear_guest(vcpu->kvm, |
| 243 | data & PAGE_MASK, PAGE_SIZE)) { |
| 244 | ret = 1; |
| 245 | break; |
| 246 | } |
| 247 | synic->msg_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 248 | if (!host) |
| 249 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 250 | break; |
| 251 | case HV_X64_MSR_EOM: { |
| 252 | int i; |
| 253 | |
| 254 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 255 | kvm_hv_notify_acked_sint(vcpu, i); |
| 256 | break; |
| 257 | } |
| 258 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Andrey Smetanin | 7be58a6 | 2015-12-28 18:27:23 +0300 | [diff] [blame] | 259 | ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 260 | break; |
| 261 | default: |
| 262 | ret = 1; |
| 263 | break; |
| 264 | } |
| 265 | return ret; |
| 266 | } |
| 267 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 268 | static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata, |
| 269 | bool host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 270 | { |
| 271 | int ret; |
| 272 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 273 | if (!synic->active && !host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 274 | return 1; |
| 275 | |
| 276 | ret = 0; |
| 277 | switch (msr) { |
| 278 | case HV_X64_MSR_SCONTROL: |
| 279 | *pdata = synic->control; |
| 280 | break; |
| 281 | case HV_X64_MSR_SVERSION: |
| 282 | *pdata = synic->version; |
| 283 | break; |
| 284 | case HV_X64_MSR_SIEFP: |
| 285 | *pdata = synic->evt_page; |
| 286 | break; |
| 287 | case HV_X64_MSR_SIMP: |
| 288 | *pdata = synic->msg_page; |
| 289 | break; |
| 290 | case HV_X64_MSR_EOM: |
| 291 | *pdata = 0; |
| 292 | break; |
| 293 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 294 | *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); |
| 295 | break; |
| 296 | default: |
| 297 | ret = 1; |
| 298 | break; |
| 299 | } |
| 300 | return ret; |
| 301 | } |
| 302 | |
Jiang Biao | ecd8a8c | 2016-11-07 08:56:33 +0800 | [diff] [blame] | 303 | static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 304 | { |
| 305 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
| 306 | struct kvm_lapic_irq irq; |
| 307 | int ret, vector; |
| 308 | |
| 309 | if (sint >= ARRAY_SIZE(synic->sint)) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | vector = synic_get_sint_vector(synic_read_sint(synic, sint)); |
| 313 | if (vector < 0) |
| 314 | return -ENOENT; |
| 315 | |
| 316 | memset(&irq, 0, sizeof(irq)); |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 317 | irq.shorthand = APIC_DEST_SELF; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 318 | irq.dest_mode = APIC_DEST_PHYSICAL; |
| 319 | irq.delivery_mode = APIC_DM_FIXED; |
| 320 | irq.vector = vector; |
| 321 | irq.level = 1; |
| 322 | |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 323 | ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL); |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 324 | trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 328 | int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 329 | { |
| 330 | struct kvm_vcpu_hv_synic *synic; |
| 331 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 332 | synic = synic_get(kvm, vpidx); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 333 | if (!synic) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | return synic_set_irq(synic, sint); |
| 337 | } |
| 338 | |
| 339 | void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) |
| 340 | { |
| 341 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 342 | int i; |
| 343 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 344 | trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 345 | |
| 346 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 347 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 348 | kvm_hv_notify_acked_sint(vcpu, i); |
| 349 | } |
| 350 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 351 | static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 352 | { |
| 353 | struct kvm_vcpu_hv_synic *synic; |
| 354 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 355 | synic = synic_get(kvm, vpidx); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 356 | if (!synic) |
| 357 | return -EINVAL; |
| 358 | |
| 359 | if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) |
| 360 | return -EINVAL; |
| 361 | |
| 362 | atomic_set(&synic->sint_to_gsi[sint], gsi); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | void kvm_hv_irq_routing_update(struct kvm *kvm) |
| 367 | { |
| 368 | struct kvm_irq_routing_table *irq_rt; |
| 369 | struct kvm_kernel_irq_routing_entry *e; |
| 370 | u32 gsi; |
| 371 | |
| 372 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 373 | lockdep_is_held(&kvm->irq_lock)); |
| 374 | |
| 375 | for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { |
| 376 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 377 | if (e->type == KVM_IRQ_ROUTING_HV_SINT) |
| 378 | kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, |
| 379 | e->hv_sint.sint, gsi); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void synic_init(struct kvm_vcpu_hv_synic *synic) |
| 385 | { |
| 386 | int i; |
| 387 | |
| 388 | memset(synic, 0, sizeof(*synic)); |
| 389 | synic->version = HV_SYNIC_VERSION_1; |
| 390 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 391 | atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); |
| 392 | atomic_set(&synic->sint_to_gsi[i], -1); |
| 393 | } |
| 394 | } |
| 395 | |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 396 | static u64 get_time_ref_counter(struct kvm *kvm) |
| 397 | { |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 398 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 399 | struct kvm_vcpu *vcpu; |
| 400 | u64 tsc; |
| 401 | |
| 402 | /* |
| 403 | * The guest has not set up the TSC page or the clock isn't |
| 404 | * stable, fall back to get_kvmclock_ns. |
| 405 | */ |
| 406 | if (!hv->tsc_ref.tsc_sequence) |
| 407 | return div_u64(get_kvmclock_ns(kvm), 100); |
| 408 | |
| 409 | vcpu = kvm_get_vcpu(kvm, 0); |
| 410 | tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 411 | return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) |
| 412 | + hv->tsc_ref.tsc_offset; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 413 | } |
| 414 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 415 | static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 416 | bool vcpu_kick) |
| 417 | { |
| 418 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 419 | |
| 420 | set_bit(stimer->index, |
| 421 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 422 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 423 | if (vcpu_kick) |
| 424 | kvm_vcpu_kick(vcpu); |
| 425 | } |
| 426 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 427 | static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) |
| 428 | { |
| 429 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 430 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 431 | trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id, |
| 432 | stimer->index); |
| 433 | |
Andrey Smetanin | 019b978 | 2015-12-28 18:27:19 +0300 | [diff] [blame] | 434 | hrtimer_cancel(&stimer->timer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 435 | clear_bit(stimer->index, |
| 436 | vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
| 437 | stimer->msg_pending = false; |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 438 | stimer->exp_time = 0; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) |
| 442 | { |
| 443 | struct kvm_vcpu_hv_stimer *stimer; |
| 444 | |
| 445 | stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 446 | trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id, |
| 447 | stimer->index); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 448 | stimer_mark_pending(stimer, true); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 449 | |
| 450 | return HRTIMER_NORESTART; |
| 451 | } |
| 452 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 453 | /* |
| 454 | * stimer_start() assumptions: |
| 455 | * a) stimer->count is not equal to 0 |
| 456 | * b) stimer->config has HV_STIMER_ENABLE flag |
| 457 | */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 458 | static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) |
| 459 | { |
| 460 | u64 time_now; |
| 461 | ktime_t ktime_now; |
| 462 | |
| 463 | time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); |
| 464 | ktime_now = ktime_get(); |
| 465 | |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 466 | if (stimer->config.periodic) { |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 467 | if (stimer->exp_time) { |
| 468 | if (time_now >= stimer->exp_time) { |
| 469 | u64 remainder; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 470 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 471 | div64_u64_rem(time_now - stimer->exp_time, |
| 472 | stimer->count, &remainder); |
| 473 | stimer->exp_time = |
| 474 | time_now + (stimer->count - remainder); |
| 475 | } |
| 476 | } else |
| 477 | stimer->exp_time = time_now + stimer->count; |
| 478 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 479 | trace_kvm_hv_stimer_start_periodic( |
| 480 | stimer_to_vcpu(stimer)->vcpu_id, |
| 481 | stimer->index, |
| 482 | time_now, stimer->exp_time); |
| 483 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 484 | hrtimer_start(&stimer->timer, |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 485 | ktime_add_ns(ktime_now, |
| 486 | 100 * (stimer->exp_time - time_now)), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 487 | HRTIMER_MODE_ABS); |
| 488 | return 0; |
| 489 | } |
| 490 | stimer->exp_time = stimer->count; |
| 491 | if (time_now >= stimer->count) { |
| 492 | /* |
| 493 | * Expire timer according to Hypervisor Top-Level Functional |
| 494 | * specification v4(15.3.1): |
| 495 | * "If a one shot is enabled and the specified count is in |
| 496 | * the past, it will expire immediately." |
| 497 | */ |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 498 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 502 | trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id, |
| 503 | stimer->index, |
| 504 | time_now, stimer->count); |
| 505 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 506 | hrtimer_start(&stimer->timer, |
| 507 | ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), |
| 508 | HRTIMER_MODE_ABS); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, |
| 513 | bool host) |
| 514 | { |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 515 | union hv_stimer_config new_config = {.as_uint64 = config}, |
| 516 | old_config = {.as_uint64 = stimer->config.as_uint64}; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 517 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 518 | trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id, |
| 519 | stimer->index, config, host); |
| 520 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 521 | stimer_cleanup(stimer); |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 522 | if (old_config.enable && |
| 523 | !new_config.direct_mode && new_config.sintx == 0) |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 524 | new_config.enable = 0; |
| 525 | stimer->config.as_uint64 = new_config.as_uint64; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 526 | |
Vitaly Kuznetsov | 013cc6e | 2019-03-13 18:13:42 +0100 | [diff] [blame] | 527 | if (stimer->config.enable) |
| 528 | stimer_mark_pending(stimer, false); |
| 529 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, |
| 534 | bool host) |
| 535 | { |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 536 | trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id, |
| 537 | stimer->index, count, host); |
| 538 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 539 | stimer_cleanup(stimer); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 540 | stimer->count = count; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 541 | if (stimer->count == 0) |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 542 | stimer->config.enable = 0; |
| 543 | else if (stimer->config.auto_enable) |
| 544 | stimer->config.enable = 1; |
Vitaly Kuznetsov | 013cc6e | 2019-03-13 18:13:42 +0100 | [diff] [blame] | 545 | |
| 546 | if (stimer->config.enable) |
| 547 | stimer_mark_pending(stimer, false); |
| 548 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) |
| 553 | { |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 554 | *pconfig = stimer->config.as_uint64; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) |
| 559 | { |
| 560 | *pcount = stimer->count; |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 565 | struct hv_message *src_msg, bool no_retry) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 566 | { |
| 567 | struct kvm_vcpu *vcpu = synic_to_vcpu(synic); |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 568 | int msg_off = offsetof(struct hv_message_page, sint_message[sint]); |
| 569 | gfn_t msg_page_gfn; |
| 570 | struct hv_message_header hv_hdr; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 571 | int r; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 572 | |
| 573 | if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) |
| 574 | return -ENOENT; |
| 575 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 576 | msg_page_gfn = synic->msg_page >> PAGE_SHIFT; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 577 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 578 | /* |
| 579 | * Strictly following the spec-mandated ordering would assume setting |
| 580 | * .msg_pending before checking .message_type. However, this function |
| 581 | * is only called in vcpu context so the entire update is atomic from |
| 582 | * guest POV and thus the exact order here doesn't matter. |
| 583 | */ |
| 584 | r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type, |
| 585 | msg_off + offsetof(struct hv_message, |
| 586 | header.message_type), |
| 587 | sizeof(hv_hdr.message_type)); |
| 588 | if (r < 0) |
| 589 | return r; |
| 590 | |
| 591 | if (hv_hdr.message_type != HVMSG_NONE) { |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 592 | if (no_retry) |
| 593 | return 0; |
| 594 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 595 | hv_hdr.message_flags.msg_pending = 1; |
| 596 | r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, |
| 597 | &hv_hdr.message_flags, |
| 598 | msg_off + |
| 599 | offsetof(struct hv_message, |
| 600 | header.message_flags), |
| 601 | sizeof(hv_hdr.message_flags)); |
| 602 | if (r < 0) |
| 603 | return r; |
| 604 | return -EAGAIN; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 605 | } |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 606 | |
| 607 | r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off, |
| 608 | sizeof(src_msg->header) + |
| 609 | src_msg->header.payload_size); |
| 610 | if (r < 0) |
| 611 | return r; |
| 612 | |
| 613 | r = synic_set_irq(synic, sint); |
| 614 | if (r < 0) |
| 615 | return r; |
| 616 | if (r == 0) |
| 617 | return -EFAULT; |
| 618 | return 0; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 619 | } |
| 620 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 621 | static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 622 | { |
| 623 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 624 | struct hv_message *msg = &stimer->msg; |
| 625 | struct hv_timer_message_payload *payload = |
| 626 | (struct hv_timer_message_payload *)&msg->u.payload; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 627 | |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 628 | /* |
| 629 | * To avoid piling up periodic ticks, don't retry message |
| 630 | * delivery for them (within "lazy" lost ticks policy). |
| 631 | */ |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 632 | bool no_retry = stimer->config.periodic; |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 633 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 634 | payload->expiration_time = stimer->exp_time; |
| 635 | payload->delivery_time = get_time_ref_counter(vcpu->kvm); |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 636 | return synic_deliver_msg(vcpu_to_synic(vcpu), |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 637 | stimer->config.sintx, msg, |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 638 | no_retry); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 639 | } |
| 640 | |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 641 | static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer) |
| 642 | { |
| 643 | struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); |
| 644 | struct kvm_lapic_irq irq = { |
| 645 | .delivery_mode = APIC_DM_FIXED, |
| 646 | .vector = stimer->config.apic_vector |
| 647 | }; |
| 648 | |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 649 | if (lapic_in_kernel(vcpu)) |
| 650 | return !kvm_apic_set_irq(vcpu, &irq, NULL); |
| 651 | return 0; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 654 | static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) |
| 655 | { |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 656 | int r, direct = stimer->config.direct_mode; |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 657 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 658 | stimer->msg_pending = true; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 659 | if (!direct) |
| 660 | r = stimer_send_msg(stimer); |
| 661 | else |
| 662 | r = stimer_notify_direct(stimer); |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 663 | trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id, |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 664 | stimer->index, direct, r); |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 665 | if (!r) { |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 666 | stimer->msg_pending = false; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 667 | if (!(stimer->config.periodic)) |
| 668 | stimer->config.enable = 0; |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 669 | } |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) |
| 673 | { |
| 674 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 675 | struct kvm_vcpu_hv_stimer *stimer; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 676 | u64 time_now, exp_time; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 677 | int i; |
| 678 | |
| 679 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 680 | if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { |
| 681 | stimer = &hv_vcpu->stimer[i]; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 682 | if (stimer->config.enable) { |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 683 | exp_time = stimer->exp_time; |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 684 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 685 | if (exp_time) { |
| 686 | time_now = |
| 687 | get_time_ref_counter(vcpu->kvm); |
| 688 | if (time_now >= exp_time) |
| 689 | stimer_expiration(stimer); |
| 690 | } |
| 691 | |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 692 | if ((stimer->config.enable) && |
Roman Kagan | f1ff89e | 2017-07-20 17:26:40 +0300 | [diff] [blame] | 693 | stimer->count) { |
| 694 | if (!stimer->msg_pending) |
| 695 | stimer_start(stimer); |
| 696 | } else |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 697 | stimer_cleanup(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 703 | { |
| 704 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 705 | int i; |
| 706 | |
| 707 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 708 | stimer_cleanup(&hv_vcpu->stimer[i]); |
| 709 | } |
| 710 | |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 711 | bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu) |
| 712 | { |
| 713 | if (!(vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) |
| 714 | return false; |
| 715 | return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; |
| 716 | } |
| 717 | EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled); |
| 718 | |
| 719 | bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu, |
| 720 | struct hv_vp_assist_page *assist_page) |
| 721 | { |
| 722 | if (!kvm_hv_assist_page_enabled(vcpu)) |
| 723 | return false; |
| 724 | return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, |
| 725 | assist_page, sizeof(*assist_page)); |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page); |
| 728 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 729 | static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) |
| 730 | { |
| 731 | struct hv_message *msg = &stimer->msg; |
| 732 | struct hv_timer_message_payload *payload = |
| 733 | (struct hv_timer_message_payload *)&msg->u.payload; |
| 734 | |
| 735 | memset(&msg->header, 0, sizeof(msg->header)); |
| 736 | msg->header.message_type = HVMSG_TIMER_EXPIRED; |
| 737 | msg->header.payload_size = sizeof(*payload); |
| 738 | |
| 739 | payload->timer_index = stimer->index; |
| 740 | payload->expiration_time = 0; |
| 741 | payload->delivery_time = 0; |
| 742 | } |
| 743 | |
| 744 | static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) |
| 745 | { |
| 746 | memset(stimer, 0, sizeof(*stimer)); |
| 747 | stimer->index = timer_index; |
| 748 | hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 749 | stimer->timer.function = stimer_timer_callback; |
| 750 | stimer_prepare_msg(stimer); |
| 751 | } |
| 752 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 753 | void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) |
| 754 | { |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 755 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 756 | int i; |
| 757 | |
| 758 | synic_init(&hv_vcpu->synic); |
| 759 | |
| 760 | bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); |
| 761 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 762 | stimer_init(&hv_vcpu->stimer[i], i); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 763 | } |
| 764 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 765 | void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 766 | { |
| 767 | struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); |
| 768 | |
| 769 | hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu); |
| 770 | } |
| 771 | |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 772 | int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 773 | { |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 774 | struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); |
| 775 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 776 | /* |
| 777 | * Hyper-V SynIC auto EOI SINT's are |
| 778 | * not compatible with APICV, so deactivate APICV |
| 779 | */ |
| 780 | kvm_vcpu_deactivate_apicv(vcpu); |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 781 | synic->active = true; |
| 782 | synic->dont_zero_synic_pages = dont_zero_synic_pages; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 786 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 787 | { |
| 788 | bool r = false; |
| 789 | |
| 790 | switch (msr) { |
| 791 | case HV_X64_MSR_GUEST_OS_ID: |
| 792 | case HV_X64_MSR_HYPERCALL: |
| 793 | case HV_X64_MSR_REFERENCE_TSC: |
| 794 | case HV_X64_MSR_TIME_REF_COUNT: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 795 | case HV_X64_MSR_CRASH_CTL: |
| 796 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 797 | case HV_X64_MSR_RESET: |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 798 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 799 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 800 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 801 | r = true; |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | return r; |
| 806 | } |
| 807 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 808 | static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, |
| 809 | u32 index, u64 *pdata) |
| 810 | { |
| 811 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 812 | |
| 813 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | *pdata = hv->hv_crash_param[index]; |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) |
| 821 | { |
| 822 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 823 | |
| 824 | *pdata = hv->hv_crash_ctl; |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) |
| 829 | { |
| 830 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 831 | |
| 832 | if (host) |
Vitaly Kuznetsov | a4987de | 2018-12-10 18:21:53 +0100 | [diff] [blame] | 833 | hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 834 | |
Vitaly Kuznetsov | a4987de | 2018-12-10 18:21:53 +0100 | [diff] [blame] | 835 | if (!host && (data & HV_CRASH_CTL_CRASH_NOTIFY)) { |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 836 | |
| 837 | vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", |
| 838 | hv->hv_crash_param[0], |
| 839 | hv->hv_crash_param[1], |
| 840 | hv->hv_crash_param[2], |
| 841 | hv->hv_crash_param[3], |
| 842 | hv->hv_crash_param[4]); |
| 843 | |
| 844 | /* Send notification about crash to user space */ |
| 845 | kvm_make_request(KVM_REQ_HV_CRASH, vcpu); |
| 846 | } |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, |
| 852 | u32 index, u64 data) |
| 853 | { |
| 854 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 855 | |
| 856 | if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) |
| 857 | return -EINVAL; |
| 858 | |
| 859 | hv->hv_crash_param[index] = data; |
| 860 | return 0; |
| 861 | } |
| 862 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 863 | /* |
| 864 | * The kvmclock and Hyper-V TSC page use similar formulas, and converting |
| 865 | * between them is possible: |
| 866 | * |
| 867 | * kvmclock formula: |
| 868 | * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) |
| 869 | * + system_time |
| 870 | * |
| 871 | * Hyper-V formula: |
| 872 | * nsec/100 = ticks * scale / 2^64 + offset |
| 873 | * |
| 874 | * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. |
| 875 | * By dividing the kvmclock formula by 100 and equating what's left we get: |
| 876 | * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 877 | * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 878 | * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 |
| 879 | * |
| 880 | * Now expand the kvmclock formula and divide by 100: |
| 881 | * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) |
| 882 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) |
| 883 | * + system_time |
| 884 | * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 885 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 886 | * + system_time / 100 |
| 887 | * |
| 888 | * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: |
| 889 | * nsec/100 = ticks * scale / 2^64 |
| 890 | * - tsc_timestamp * scale / 2^64 |
| 891 | * + system_time / 100 |
| 892 | * |
| 893 | * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: |
| 894 | * offset = system_time / 100 - tsc_timestamp * scale / 2^64 |
| 895 | * |
| 896 | * These two equivalencies are implemented in this function. |
| 897 | */ |
| 898 | static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, |
| 899 | HV_REFERENCE_TSC_PAGE *tsc_ref) |
| 900 | { |
| 901 | u64 max_mul; |
| 902 | |
| 903 | if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) |
| 904 | return false; |
| 905 | |
| 906 | /* |
| 907 | * check if scale would overflow, if so we use the time ref counter |
| 908 | * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 |
| 909 | * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) |
| 910 | * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) |
| 911 | */ |
| 912 | max_mul = 100ull << (32 - hv_clock->tsc_shift); |
| 913 | if (hv_clock->tsc_to_system_mul >= max_mul) |
| 914 | return false; |
| 915 | |
| 916 | /* |
| 917 | * Otherwise compute the scale and offset according to the formulas |
| 918 | * derived above. |
| 919 | */ |
| 920 | tsc_ref->tsc_scale = |
| 921 | mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), |
| 922 | hv_clock->tsc_to_system_mul, |
| 923 | 100); |
| 924 | |
| 925 | tsc_ref->tsc_offset = hv_clock->system_time; |
| 926 | do_div(tsc_ref->tsc_offset, 100); |
| 927 | tsc_ref->tsc_offset -= |
| 928 | mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | void kvm_hv_setup_tsc_page(struct kvm *kvm, |
| 933 | struct pvclock_vcpu_time_info *hv_clock) |
| 934 | { |
| 935 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 936 | u32 tsc_seq; |
| 937 | u64 gfn; |
| 938 | |
| 939 | BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); |
| 940 | BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0); |
| 941 | |
| 942 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 943 | return; |
| 944 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 945 | mutex_lock(&kvm->arch.hyperv.hv_lock); |
| 946 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 947 | goto out_unlock; |
| 948 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 949 | gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 950 | /* |
| 951 | * Because the TSC parameters only vary when there is a |
| 952 | * change in the master clock, do not bother with caching. |
| 953 | */ |
| 954 | if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), |
| 955 | &tsc_seq, sizeof(tsc_seq)))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 956 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 957 | |
| 958 | /* |
| 959 | * While we're computing and writing the parameters, force the |
| 960 | * guest to use the time reference count MSR. |
| 961 | */ |
| 962 | hv->tsc_ref.tsc_sequence = 0; |
| 963 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 964 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 965 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 966 | |
| 967 | if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 968 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 969 | |
| 970 | /* Ensure sequence is zero before writing the rest of the struct. */ |
| 971 | smp_wmb(); |
| 972 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 973 | goto out_unlock; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * Now switch to the TSC page mechanism by writing the sequence. |
| 977 | */ |
| 978 | tsc_seq++; |
| 979 | if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) |
| 980 | tsc_seq = 1; |
| 981 | |
| 982 | /* Write the struct entirely before the non-zero sequence. */ |
| 983 | smp_wmb(); |
| 984 | |
| 985 | hv->tsc_ref.tsc_sequence = tsc_seq; |
| 986 | kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 987 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 988 | out_unlock: |
| 989 | mutex_unlock(&kvm->arch.hyperv.hv_lock); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 992 | static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, |
| 993 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 994 | { |
| 995 | struct kvm *kvm = vcpu->kvm; |
| 996 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 997 | |
| 998 | switch (msr) { |
| 999 | case HV_X64_MSR_GUEST_OS_ID: |
| 1000 | hv->hv_guest_os_id = data; |
| 1001 | /* setting guest os id to zero disables hypercall page */ |
| 1002 | if (!hv->hv_guest_os_id) |
| 1003 | hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 1004 | break; |
| 1005 | case HV_X64_MSR_HYPERCALL: { |
| 1006 | u64 gfn; |
| 1007 | unsigned long addr; |
| 1008 | u8 instructions[4]; |
| 1009 | |
| 1010 | /* if guest os id is not set hypercall should remain disabled */ |
| 1011 | if (!hv->hv_guest_os_id) |
| 1012 | break; |
| 1013 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1014 | hv->hv_hypercall = data; |
| 1015 | break; |
| 1016 | } |
| 1017 | gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; |
| 1018 | addr = gfn_to_hva(kvm, gfn); |
| 1019 | if (kvm_is_error_hva(addr)) |
| 1020 | return 1; |
| 1021 | kvm_x86_ops->patch_hypercall(vcpu, instructions); |
| 1022 | ((unsigned char *)instructions)[3] = 0xc3; /* ret */ |
| 1023 | if (__copy_to_user((void __user *)addr, instructions, 4)) |
| 1024 | return 1; |
| 1025 | hv->hv_hypercall = data; |
| 1026 | mark_page_dirty(kvm, gfn); |
| 1027 | break; |
| 1028 | } |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1029 | case HV_X64_MSR_REFERENCE_TSC: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1030 | hv->hv_tsc_page = data; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1031 | if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) |
| 1032 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1033 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1034 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 1035 | return kvm_hv_msr_set_crash_data(vcpu, |
| 1036 | msr - HV_X64_MSR_CRASH_P0, |
| 1037 | data); |
| 1038 | case HV_X64_MSR_CRASH_CTL: |
| 1039 | return kvm_hv_msr_set_crash_ctl(vcpu, data, host); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1040 | case HV_X64_MSR_RESET: |
| 1041 | if (data == 1) { |
| 1042 | vcpu_debug(vcpu, "hyper-v reset requested\n"); |
| 1043 | kvm_make_request(KVM_REQ_HV_RESET, vcpu); |
| 1044 | } |
| 1045 | break; |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1046 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 1047 | hv->hv_reenlightenment_control = data; |
| 1048 | break; |
| 1049 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 1050 | hv->hv_tsc_emulation_control = data; |
| 1051 | break; |
| 1052 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 1053 | hv->hv_tsc_emulation_status = data; |
| 1054 | break; |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1055 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1056 | /* read-only, but still ignore it if host-initiated */ |
| 1057 | if (!host) |
| 1058 | return 1; |
| 1059 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1060 | default: |
| 1061 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 1062 | msr, data); |
| 1063 | return 1; |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1068 | /* Calculate cpu time spent by current task in 100ns units */ |
| 1069 | static u64 current_task_runtime_100ns(void) |
| 1070 | { |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 1071 | u64 utime, stime; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1072 | |
| 1073 | task_cputime_adjusted(current, &utime, &stime); |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 1074 | |
| 1075 | return div_u64(utime + stime, 100); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1079 | { |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1080 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1081 | |
| 1082 | switch (msr) { |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1083 | case HV_X64_MSR_VP_INDEX: { |
| 1084 | struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; |
| 1085 | int vcpu_idx = kvm_vcpu_get_idx(vcpu); |
| 1086 | u32 new_vp_index = (u32)data; |
| 1087 | |
| 1088 | if (!host || new_vp_index >= KVM_MAX_VCPUS) |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1089 | return 1; |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1090 | |
| 1091 | if (new_vp_index == hv_vcpu->vp_index) |
| 1092 | return 0; |
| 1093 | |
| 1094 | /* |
| 1095 | * The VP index is initialized to vcpu_index by |
| 1096 | * kvm_hv_vcpu_postcreate so they initially match. Now the |
| 1097 | * VP index is changing, adjust num_mismatched_vp_indexes if |
| 1098 | * it now matches or no longer matches vcpu_idx. |
| 1099 | */ |
| 1100 | if (hv_vcpu->vp_index == vcpu_idx) |
| 1101 | atomic_inc(&hv->num_mismatched_vp_indexes); |
| 1102 | else if (new_vp_index == vcpu_idx) |
| 1103 | atomic_dec(&hv->num_mismatched_vp_indexes); |
| 1104 | |
| 1105 | hv_vcpu->vp_index = new_vp_index; |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1106 | break; |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1107 | } |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1108 | case HV_X64_MSR_VP_ASSIST_PAGE: { |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1109 | u64 gfn; |
| 1110 | unsigned long addr; |
| 1111 | |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1112 | if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) { |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1113 | hv_vcpu->hv_vapic = data; |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 1114 | if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0)) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1115 | return 1; |
| 1116 | break; |
| 1117 | } |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1118 | gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1119 | addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); |
| 1120 | if (kvm_is_error_hva(addr)) |
| 1121 | return 1; |
Vitaly Kuznetsov | 12e0c61 | 2018-10-16 18:50:05 +0200 | [diff] [blame] | 1122 | |
| 1123 | /* |
| 1124 | * Clear apic_assist portion of f(struct hv_vp_assist_page |
| 1125 | * only, there can be valuable data in the rest which needs |
| 1126 | * to be preserved e.g. on migration. |
| 1127 | */ |
| 1128 | if (__clear_user((void __user *)addr, sizeof(u32))) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1129 | return 1; |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1130 | hv_vcpu->hv_vapic = data; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1131 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 1132 | if (kvm_lapic_enable_pv_eoi(vcpu, |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 1133 | gfn_to_gpa(gfn) | KVM_MSR_ENABLED, |
| 1134 | sizeof(struct hv_vp_assist_page))) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1135 | return 1; |
| 1136 | break; |
| 1137 | } |
| 1138 | case HV_X64_MSR_EOI: |
| 1139 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1140 | case HV_X64_MSR_ICR: |
| 1141 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1142 | case HV_X64_MSR_TPR: |
| 1143 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1144 | case HV_X64_MSR_VP_RUNTIME: |
| 1145 | if (!host) |
| 1146 | return 1; |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1147 | hv_vcpu->runtime_offset = data - current_task_runtime_100ns(); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1148 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1149 | case HV_X64_MSR_SCONTROL: |
| 1150 | case HV_X64_MSR_SVERSION: |
| 1151 | case HV_X64_MSR_SIEFP: |
| 1152 | case HV_X64_MSR_SIMP: |
| 1153 | case HV_X64_MSR_EOM: |
| 1154 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 1155 | return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1156 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1157 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1158 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1159 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1160 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1161 | |
| 1162 | return stimer_set_config(vcpu_to_stimer(vcpu, timer_index), |
| 1163 | data, host); |
| 1164 | } |
| 1165 | case HV_X64_MSR_STIMER0_COUNT: |
| 1166 | case HV_X64_MSR_STIMER1_COUNT: |
| 1167 | case HV_X64_MSR_STIMER2_COUNT: |
| 1168 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1169 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1170 | |
| 1171 | return stimer_set_count(vcpu_to_stimer(vcpu, timer_index), |
| 1172 | data, host); |
| 1173 | } |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1174 | case HV_X64_MSR_TSC_FREQUENCY: |
| 1175 | case HV_X64_MSR_APIC_FREQUENCY: |
| 1176 | /* read-only, but still ignore it if host-initiated */ |
| 1177 | if (!host) |
| 1178 | return 1; |
| 1179 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1180 | default: |
| 1181 | vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", |
| 1182 | msr, data); |
| 1183 | return 1; |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) |
| 1190 | { |
| 1191 | u64 data = 0; |
| 1192 | struct kvm *kvm = vcpu->kvm; |
| 1193 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 1194 | |
| 1195 | switch (msr) { |
| 1196 | case HV_X64_MSR_GUEST_OS_ID: |
| 1197 | data = hv->hv_guest_os_id; |
| 1198 | break; |
| 1199 | case HV_X64_MSR_HYPERCALL: |
| 1200 | data = hv->hv_hypercall; |
| 1201 | break; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 1202 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1203 | data = get_time_ref_counter(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1204 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1205 | case HV_X64_MSR_REFERENCE_TSC: |
| 1206 | data = hv->hv_tsc_page; |
| 1207 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1208 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 1209 | return kvm_hv_msr_get_crash_data(vcpu, |
| 1210 | msr - HV_X64_MSR_CRASH_P0, |
| 1211 | pdata); |
| 1212 | case HV_X64_MSR_CRASH_CTL: |
| 1213 | return kvm_hv_msr_get_crash_ctl(vcpu, pdata); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1214 | case HV_X64_MSR_RESET: |
| 1215 | data = 0; |
| 1216 | break; |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1217 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 1218 | data = hv->hv_reenlightenment_control; |
| 1219 | break; |
| 1220 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 1221 | data = hv->hv_tsc_emulation_control; |
| 1222 | break; |
| 1223 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 1224 | data = hv->hv_tsc_emulation_status; |
| 1225 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1226 | default: |
| 1227 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1228 | return 1; |
| 1229 | } |
| 1230 | |
| 1231 | *pdata = data; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1235 | static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, |
| 1236 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1237 | { |
| 1238 | u64 data = 0; |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1239 | struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1240 | |
| 1241 | switch (msr) { |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1242 | case HV_X64_MSR_VP_INDEX: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1243 | data = hv_vcpu->vp_index; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1244 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1245 | case HV_X64_MSR_EOI: |
| 1246 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1247 | case HV_X64_MSR_ICR: |
| 1248 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1249 | case HV_X64_MSR_TPR: |
| 1250 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1251 | case HV_X64_MSR_VP_ASSIST_PAGE: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1252 | data = hv_vcpu->hv_vapic; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1253 | break; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1254 | case HV_X64_MSR_VP_RUNTIME: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1255 | data = current_task_runtime_100ns() + hv_vcpu->runtime_offset; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1256 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1257 | case HV_X64_MSR_SCONTROL: |
| 1258 | case HV_X64_MSR_SVERSION: |
| 1259 | case HV_X64_MSR_SIEFP: |
| 1260 | case HV_X64_MSR_SIMP: |
| 1261 | case HV_X64_MSR_EOM: |
| 1262 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1263 | return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1264 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1265 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1266 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1267 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1268 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1269 | |
| 1270 | return stimer_get_config(vcpu_to_stimer(vcpu, timer_index), |
| 1271 | pdata); |
| 1272 | } |
| 1273 | case HV_X64_MSR_STIMER0_COUNT: |
| 1274 | case HV_X64_MSR_STIMER1_COUNT: |
| 1275 | case HV_X64_MSR_STIMER2_COUNT: |
| 1276 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1277 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1278 | |
| 1279 | return stimer_get_count(vcpu_to_stimer(vcpu, timer_index), |
| 1280 | pdata); |
| 1281 | } |
Ladi Prosek | 72c139b | 2017-07-26 13:32:59 +0200 | [diff] [blame] | 1282 | case HV_X64_MSR_TSC_FREQUENCY: |
| 1283 | data = (u64)vcpu->arch.virtual_tsc_khz * 1000; |
| 1284 | break; |
| 1285 | case HV_X64_MSR_APIC_FREQUENCY: |
| 1286 | data = APIC_BUS_FREQUENCY; |
| 1287 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1288 | default: |
| 1289 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1290 | return 1; |
| 1291 | } |
| 1292 | *pdata = data; |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1296 | int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1297 | { |
| 1298 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1299 | int r; |
| 1300 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1301 | mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1302 | r = kvm_hv_set_msr_pw(vcpu, msr, data, host); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1303 | mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1304 | return r; |
| 1305 | } else |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1306 | return kvm_hv_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1307 | } |
| 1308 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1309 | int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1310 | { |
| 1311 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1312 | int r; |
| 1313 | |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1314 | mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1315 | r = kvm_hv_get_msr_pw(vcpu, msr, pdata); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1316 | mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1317 | return r; |
| 1318 | } else |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1319 | return kvm_hv_get_msr(vcpu, msr, pdata, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1320 | } |
| 1321 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1322 | static __always_inline unsigned long *sparse_set_to_vcpu_mask( |
| 1323 | struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask, |
| 1324 | u64 *vp_bitmap, unsigned long *vcpu_bitmap) |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1325 | { |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1326 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 1327 | struct kvm_vcpu *vcpu; |
| 1328 | int i, bank, sbank = 0; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1329 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1330 | memset(vp_bitmap, 0, |
| 1331 | KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap)); |
| 1332 | for_each_set_bit(bank, (unsigned long *)&valid_bank_mask, |
| 1333 | KVM_HV_MAX_SPARSE_VCPU_SET_BITS) |
| 1334 | vp_bitmap[bank] = sparse_banks[sbank++]; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1335 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1336 | if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) { |
| 1337 | /* for all vcpus vp_index == vcpu_idx */ |
| 1338 | return (unsigned long *)vp_bitmap; |
| 1339 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1340 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1341 | bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1342 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1343 | if (test_bit(vcpu_to_hv_vcpu(vcpu)->vp_index, |
| 1344 | (unsigned long *)vp_bitmap)) |
| 1345 | __set_bit(i, vcpu_bitmap); |
| 1346 | } |
| 1347 | return vcpu_bitmap; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1348 | } |
| 1349 | |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1350 | static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa, |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1351 | u16 rep_cnt, bool ex) |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1352 | { |
| 1353 | struct kvm *kvm = current_vcpu->kvm; |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1354 | struct kvm_vcpu_hv *hv_vcpu = ¤t_vcpu->arch.hyperv; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1355 | struct hv_tlb_flush_ex flush_ex; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1356 | struct hv_tlb_flush flush; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1357 | u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; |
| 1358 | DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1359 | unsigned long *vcpu_mask; |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1360 | u64 valid_bank_mask; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1361 | u64 sparse_banks[64]; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1362 | int sparse_banks_len; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1363 | bool all_cpus; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1364 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1365 | if (!ex) { |
| 1366 | if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush)))) |
| 1367 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1368 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1369 | trace_kvm_hv_flush_tlb(flush.processor_mask, |
| 1370 | flush.address_space, flush.flags); |
| 1371 | |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1372 | valid_bank_mask = BIT_ULL(0); |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1373 | sparse_banks[0] = flush.processor_mask; |
Vitaly Kuznetsov | da66761 | 2019-03-20 18:43:20 +0100 | [diff] [blame] | 1374 | |
| 1375 | /* |
| 1376 | * Work around possible WS2012 bug: it sends hypercalls |
| 1377 | * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear, |
| 1378 | * while also expecting us to flush something and crashing if |
| 1379 | * we don't. Let's treat processor_mask == 0 same as |
| 1380 | * HV_FLUSH_ALL_PROCESSORS. |
| 1381 | */ |
| 1382 | all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) || |
| 1383 | flush.processor_mask == 0; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1384 | } else { |
| 1385 | if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex, |
| 1386 | sizeof(flush_ex)))) |
| 1387 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1388 | |
| 1389 | trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask, |
| 1390 | flush_ex.hv_vp_set.format, |
| 1391 | flush_ex.address_space, |
| 1392 | flush_ex.flags); |
| 1393 | |
| 1394 | valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask; |
| 1395 | all_cpus = flush_ex.hv_vp_set.format != |
| 1396 | HV_GENERIC_SET_SPARSE_4K; |
| 1397 | |
Vitaly Kuznetsov | 0b0a31b | 2018-09-26 19:02:57 +0200 | [diff] [blame] | 1398 | sparse_banks_len = |
| 1399 | bitmap_weight((unsigned long *)&valid_bank_mask, 64) * |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1400 | sizeof(sparse_banks[0]); |
| 1401 | |
| 1402 | if (!sparse_banks_len && !all_cpus) |
| 1403 | goto ret_success; |
| 1404 | |
| 1405 | if (!all_cpus && |
| 1406 | kvm_read_guest(kvm, |
| 1407 | ingpa + offsetof(struct hv_tlb_flush_ex, |
| 1408 | hv_vp_set.bank_contents), |
| 1409 | sparse_banks, |
| 1410 | sparse_banks_len)) |
| 1411 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1412 | } |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1413 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1414 | cpumask_clear(&hv_vcpu->tlb_flush); |
| 1415 | |
| 1416 | vcpu_mask = all_cpus ? NULL : |
| 1417 | sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, |
| 1418 | vp_bitmap, vcpu_bitmap); |
| 1419 | |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1420 | /* |
| 1421 | * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't |
| 1422 | * analyze it here, flush TLB regardless of the specified address space. |
| 1423 | */ |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1424 | kvm_make_vcpus_request_mask(kvm, |
| 1425 | KVM_REQ_TLB_FLUSH | KVM_REQUEST_NO_WAKEUP, |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1426 | vcpu_mask, &hv_vcpu->tlb_flush); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1427 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1428 | ret_success: |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1429 | /* We always do full TLB flush, set rep_done = rep_cnt. */ |
| 1430 | return (u64)HV_STATUS_SUCCESS | |
| 1431 | ((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); |
| 1432 | } |
| 1433 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1434 | static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector, |
| 1435 | unsigned long *vcpu_bitmap) |
| 1436 | { |
| 1437 | struct kvm_lapic_irq irq = { |
| 1438 | .delivery_mode = APIC_DM_FIXED, |
| 1439 | .vector = vector |
| 1440 | }; |
| 1441 | struct kvm_vcpu *vcpu; |
| 1442 | int i; |
| 1443 | |
| 1444 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1445 | if (vcpu_bitmap && !test_bit(i, vcpu_bitmap)) |
| 1446 | continue; |
| 1447 | |
| 1448 | /* We fail only when APIC is disabled */ |
| 1449 | kvm_apic_set_irq(vcpu, &irq, NULL); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1453 | static u64 kvm_hv_send_ipi(struct kvm_vcpu *current_vcpu, u64 ingpa, u64 outgpa, |
| 1454 | bool ex, bool fast) |
| 1455 | { |
| 1456 | struct kvm *kvm = current_vcpu->kvm; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1457 | struct hv_send_ipi_ex send_ipi_ex; |
| 1458 | struct hv_send_ipi send_ipi; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1459 | u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; |
| 1460 | DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1461 | unsigned long *vcpu_mask; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1462 | unsigned long valid_bank_mask; |
| 1463 | u64 sparse_banks[64]; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1464 | int sparse_banks_len; |
| 1465 | u32 vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1466 | bool all_cpus; |
| 1467 | |
| 1468 | if (!ex) { |
| 1469 | if (!fast) { |
| 1470 | if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi, |
| 1471 | sizeof(send_ipi)))) |
| 1472 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1473 | sparse_banks[0] = send_ipi.cpu_mask; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1474 | vector = send_ipi.vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1475 | } else { |
| 1476 | /* 'reserved' part of hv_send_ipi should be 0 */ |
| 1477 | if (unlikely(ingpa >> 32 != 0)) |
| 1478 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1479 | sparse_banks[0] = outgpa; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1480 | vector = (u32)ingpa; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1481 | } |
| 1482 | all_cpus = false; |
| 1483 | valid_bank_mask = BIT_ULL(0); |
| 1484 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1485 | trace_kvm_hv_send_ipi(vector, sparse_banks[0]); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1486 | } else { |
| 1487 | if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex, |
| 1488 | sizeof(send_ipi_ex)))) |
| 1489 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1490 | |
| 1491 | trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector, |
| 1492 | send_ipi_ex.vp_set.format, |
| 1493 | send_ipi_ex.vp_set.valid_bank_mask); |
| 1494 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1495 | vector = send_ipi_ex.vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1496 | valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask; |
| 1497 | sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) * |
| 1498 | sizeof(sparse_banks[0]); |
| 1499 | |
| 1500 | all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL; |
| 1501 | |
| 1502 | if (!sparse_banks_len) |
| 1503 | goto ret_success; |
| 1504 | |
| 1505 | if (!all_cpus && |
| 1506 | kvm_read_guest(kvm, |
| 1507 | ingpa + offsetof(struct hv_send_ipi_ex, |
| 1508 | vp_set.bank_contents), |
| 1509 | sparse_banks, |
| 1510 | sparse_banks_len)) |
| 1511 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1512 | } |
| 1513 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1514 | if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR)) |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1515 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1516 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1517 | vcpu_mask = all_cpus ? NULL : |
| 1518 | sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, |
| 1519 | vp_bitmap, vcpu_bitmap); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1520 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1521 | kvm_send_ipi_to_many(kvm, vector, vcpu_mask); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1522 | |
| 1523 | ret_success: |
| 1524 | return HV_STATUS_SUCCESS; |
| 1525 | } |
| 1526 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1527 | bool kvm_hv_hypercall_enabled(struct kvm *kvm) |
| 1528 | { |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1529 | return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1530 | } |
| 1531 | |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1532 | static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) |
| 1533 | { |
| 1534 | bool longmode; |
| 1535 | |
| 1536 | longmode = is_64_bit_mode(vcpu); |
| 1537 | if (longmode) |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1538 | kvm_rax_write(vcpu, result); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1539 | else { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1540 | kvm_rdx_write(vcpu, result >> 32); |
| 1541 | kvm_rax_write(vcpu, result & 0xffffffff); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 1545 | static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) |
| 1546 | { |
| 1547 | kvm_hv_hypercall_set_result(vcpu, result); |
| 1548 | ++vcpu->stat.hypercalls; |
| 1549 | return kvm_skip_emulated_instruction(vcpu); |
| 1550 | } |
| 1551 | |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1552 | static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) |
| 1553 | { |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 1554 | return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1555 | } |
| 1556 | |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1557 | static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param) |
| 1558 | { |
| 1559 | struct eventfd_ctx *eventfd; |
| 1560 | |
| 1561 | if (unlikely(!fast)) { |
| 1562 | int ret; |
| 1563 | gpa_t gpa = param; |
| 1564 | |
| 1565 | if ((gpa & (__alignof__(param) - 1)) || |
| 1566 | offset_in_page(gpa) + sizeof(param) > PAGE_SIZE) |
| 1567 | return HV_STATUS_INVALID_ALIGNMENT; |
| 1568 | |
| 1569 | ret = kvm_vcpu_read_guest(vcpu, gpa, ¶m, sizeof(param)); |
| 1570 | if (ret < 0) |
| 1571 | return HV_STATUS_INVALID_ALIGNMENT; |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * Per spec, bits 32-47 contain the extra "flag number". However, we |
| 1576 | * have no use for it, and in all known usecases it is zero, so just |
| 1577 | * report lookup failure if it isn't. |
| 1578 | */ |
| 1579 | if (param & 0xffff00000000ULL) |
| 1580 | return HV_STATUS_INVALID_PORT_ID; |
| 1581 | /* remaining bits are reserved-zero */ |
| 1582 | if (param & ~KVM_HYPERV_CONN_ID_MASK) |
| 1583 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1584 | |
Paolo Bonzini | 452a68d | 2018-05-07 19:24:34 +0200 | [diff] [blame] | 1585 | /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */ |
| 1586 | rcu_read_lock(); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1587 | eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param); |
Paolo Bonzini | 452a68d | 2018-05-07 19:24:34 +0200 | [diff] [blame] | 1588 | rcu_read_unlock(); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1589 | if (!eventfd) |
| 1590 | return HV_STATUS_INVALID_PORT_ID; |
| 1591 | |
| 1592 | eventfd_signal(eventfd, 1); |
| 1593 | return HV_STATUS_SUCCESS; |
| 1594 | } |
| 1595 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1596 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 1597 | { |
Dan Carpenter | d32ef54 | 2018-03-17 14:48:27 +0300 | [diff] [blame] | 1598 | u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS; |
| 1599 | uint16_t code, rep_idx, rep_cnt; |
Arnd Bergmann | f4e4805e | 2019-07-12 16:13:09 +0200 | [diff] [blame] | 1600 | bool fast, rep; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1601 | |
| 1602 | /* |
| 1603 | * hypercall generates UD from non zero cpl and real mode |
| 1604 | * per HYPER-V spec |
| 1605 | */ |
| 1606 | if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { |
| 1607 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 1608 | return 1; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1609 | } |
| 1610 | |
Arnd Bergmann | f4e4805e | 2019-07-12 16:13:09 +0200 | [diff] [blame] | 1611 | #ifdef CONFIG_X86_64 |
| 1612 | if (is_64_bit_mode(vcpu)) { |
| 1613 | param = kvm_rcx_read(vcpu); |
| 1614 | ingpa = kvm_rdx_read(vcpu); |
| 1615 | outgpa = kvm_r8_read(vcpu); |
| 1616 | } else |
| 1617 | #endif |
| 1618 | { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 1619 | param = ((u64)kvm_rdx_read(vcpu) << 32) | |
| 1620 | (kvm_rax_read(vcpu) & 0xffffffff); |
| 1621 | ingpa = ((u64)kvm_rbx_read(vcpu) << 32) | |
| 1622 | (kvm_rcx_read(vcpu) & 0xffffffff); |
| 1623 | outgpa = ((u64)kvm_rdi_read(vcpu) << 32) | |
| 1624 | (kvm_rsi_read(vcpu) & 0xffffffff); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1625 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1626 | |
| 1627 | code = param & 0xffff; |
Vitaly Kuznetsov | 142c95d | 2018-05-16 17:21:26 +0200 | [diff] [blame] | 1628 | fast = !!(param & HV_HYPERCALL_FAST_BIT); |
| 1629 | rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff; |
| 1630 | rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff; |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 1631 | rep = !!(rep_cnt || rep_idx); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1632 | |
| 1633 | trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); |
| 1634 | |
| 1635 | switch (code) { |
Andrey Smetanin | 8ed6d76 | 2016-02-11 16:44:57 +0300 | [diff] [blame] | 1636 | case HVCALL_NOTIFY_LONG_SPIN_WAIT: |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 1637 | if (unlikely(rep)) { |
| 1638 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1639 | break; |
| 1640 | } |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 1641 | kvm_vcpu_on_spin(vcpu, true); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1642 | break; |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1643 | case HVCALL_SIGNAL_EVENT: |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 1644 | if (unlikely(rep)) { |
| 1645 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1646 | break; |
| 1647 | } |
Dan Carpenter | d32ef54 | 2018-03-17 14:48:27 +0300 | [diff] [blame] | 1648 | ret = kvm_hvcall_signal_event(vcpu, fast, ingpa); |
| 1649 | if (ret != HV_STATUS_INVALID_PORT_ID) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1650 | break; |
Gustavo A. R. Silva | b2869f2 | 2019-01-25 12:23:17 -0600 | [diff] [blame] | 1651 | /* fall through - maybe userspace knows this conn_id. */ |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1652 | case HVCALL_POST_MESSAGE: |
Paolo Bonzini | a2b5c3c | 2016-03-29 11:23:25 +0200 | [diff] [blame] | 1653 | /* don't bother userspace if it has no way to handle it */ |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 1654 | if (unlikely(rep || !vcpu_to_synic(vcpu)->active)) { |
| 1655 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
Paolo Bonzini | a2b5c3c | 2016-03-29 11:23:25 +0200 | [diff] [blame] | 1656 | break; |
| 1657 | } |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 1658 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 1659 | vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; |
| 1660 | vcpu->run->hyperv.u.hcall.input = param; |
| 1661 | vcpu->run->hyperv.u.hcall.params[0] = ingpa; |
| 1662 | vcpu->run->hyperv.u.hcall.params[1] = outgpa; |
| 1663 | vcpu->arch.complete_userspace_io = |
| 1664 | kvm_hv_hypercall_complete_userspace; |
| 1665 | return 0; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1666 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: |
| 1667 | if (unlikely(fast || !rep_cnt || rep_idx)) { |
| 1668 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1669 | break; |
| 1670 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1671 | ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1672 | break; |
| 1673 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: |
| 1674 | if (unlikely(fast || rep)) { |
| 1675 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1676 | break; |
| 1677 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1678 | ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false); |
| 1679 | break; |
| 1680 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: |
| 1681 | if (unlikely(fast || !rep_cnt || rep_idx)) { |
| 1682 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1683 | break; |
| 1684 | } |
| 1685 | ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true); |
| 1686 | break; |
| 1687 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: |
| 1688 | if (unlikely(fast || rep)) { |
| 1689 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1690 | break; |
| 1691 | } |
| 1692 | ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1693 | break; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1694 | case HVCALL_SEND_IPI: |
| 1695 | if (unlikely(rep)) { |
| 1696 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1697 | break; |
| 1698 | } |
| 1699 | ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast); |
| 1700 | break; |
| 1701 | case HVCALL_SEND_IPI_EX: |
| 1702 | if (unlikely(fast || rep)) { |
| 1703 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1704 | break; |
| 1705 | } |
| 1706 | ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false); |
| 1707 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1708 | default: |
Dan Carpenter | d32ef54 | 2018-03-17 14:48:27 +0300 | [diff] [blame] | 1709 | ret = HV_STATUS_INVALID_HYPERCALL_CODE; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 1713 | return kvm_hv_hypercall_complete(vcpu, ret); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1714 | } |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 1715 | |
| 1716 | void kvm_hv_init_vm(struct kvm *kvm) |
| 1717 | { |
| 1718 | mutex_init(&kvm->arch.hyperv.hv_lock); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1719 | idr_init(&kvm->arch.hyperv.conn_to_evt); |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | void kvm_hv_destroy_vm(struct kvm *kvm) |
| 1723 | { |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1724 | struct eventfd_ctx *eventfd; |
| 1725 | int i; |
| 1726 | |
| 1727 | idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i) |
| 1728 | eventfd_ctx_put(eventfd); |
| 1729 | idr_destroy(&kvm->arch.hyperv.conn_to_evt); |
| 1730 | } |
| 1731 | |
| 1732 | static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd) |
| 1733 | { |
| 1734 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 1735 | struct eventfd_ctx *eventfd; |
| 1736 | int ret; |
| 1737 | |
| 1738 | eventfd = eventfd_ctx_fdget(fd); |
| 1739 | if (IS_ERR(eventfd)) |
| 1740 | return PTR_ERR(eventfd); |
| 1741 | |
| 1742 | mutex_lock(&hv->hv_lock); |
| 1743 | ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1, |
Ben Gardon | 254272c | 2019-02-11 11:02:50 -0800 | [diff] [blame] | 1744 | GFP_KERNEL_ACCOUNT); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 1745 | mutex_unlock(&hv->hv_lock); |
| 1746 | |
| 1747 | if (ret >= 0) |
| 1748 | return 0; |
| 1749 | |
| 1750 | if (ret == -ENOSPC) |
| 1751 | ret = -EEXIST; |
| 1752 | eventfd_ctx_put(eventfd); |
| 1753 | return ret; |
| 1754 | } |
| 1755 | |
| 1756 | static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id) |
| 1757 | { |
| 1758 | struct kvm_hv *hv = &kvm->arch.hyperv; |
| 1759 | struct eventfd_ctx *eventfd; |
| 1760 | |
| 1761 | mutex_lock(&hv->hv_lock); |
| 1762 | eventfd = idr_remove(&hv->conn_to_evt, conn_id); |
| 1763 | mutex_unlock(&hv->hv_lock); |
| 1764 | |
| 1765 | if (!eventfd) |
| 1766 | return -ENOENT; |
| 1767 | |
| 1768 | synchronize_srcu(&kvm->srcu); |
| 1769 | eventfd_ctx_put(eventfd); |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args) |
| 1774 | { |
| 1775 | if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) || |
| 1776 | (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK)) |
| 1777 | return -EINVAL; |
| 1778 | |
| 1779 | if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN) |
| 1780 | return kvm_hv_eventfd_deassign(kvm, args->conn_id); |
| 1781 | return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd); |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 1782 | } |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1783 | |
| 1784 | int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, |
| 1785 | struct kvm_cpuid_entry2 __user *entries) |
| 1786 | { |
Vitaly Kuznetsov | ea15298 | 2019-08-27 18:04:02 +0200 | [diff] [blame] | 1787 | uint16_t evmcs_ver = 0; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1788 | struct kvm_cpuid_entry2 cpuid_entries[] = { |
| 1789 | { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS }, |
| 1790 | { .function = HYPERV_CPUID_INTERFACE }, |
| 1791 | { .function = HYPERV_CPUID_VERSION }, |
| 1792 | { .function = HYPERV_CPUID_FEATURES }, |
| 1793 | { .function = HYPERV_CPUID_ENLIGHTMENT_INFO }, |
| 1794 | { .function = HYPERV_CPUID_IMPLEMENT_LIMITS }, |
| 1795 | { .function = HYPERV_CPUID_NESTED_FEATURES }, |
| 1796 | }; |
| 1797 | int i, nent = ARRAY_SIZE(cpuid_entries); |
| 1798 | |
Vitaly Kuznetsov | ea15298 | 2019-08-27 18:04:02 +0200 | [diff] [blame] | 1799 | if (kvm_x86_ops->nested_get_evmcs_version) |
| 1800 | evmcs_ver = kvm_x86_ops->nested_get_evmcs_version(vcpu); |
| 1801 | |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1802 | /* Skip NESTED_FEATURES if eVMCS is not supported */ |
| 1803 | if (!evmcs_ver) |
| 1804 | --nent; |
| 1805 | |
| 1806 | if (cpuid->nent < nent) |
| 1807 | return -E2BIG; |
| 1808 | |
| 1809 | if (cpuid->nent > nent) |
| 1810 | cpuid->nent = nent; |
| 1811 | |
| 1812 | for (i = 0; i < nent; i++) { |
| 1813 | struct kvm_cpuid_entry2 *ent = &cpuid_entries[i]; |
| 1814 | u32 signature[3]; |
| 1815 | |
| 1816 | switch (ent->function) { |
| 1817 | case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS: |
| 1818 | memcpy(signature, "Linux KVM Hv", 12); |
| 1819 | |
| 1820 | ent->eax = HYPERV_CPUID_NESTED_FEATURES; |
| 1821 | ent->ebx = signature[0]; |
| 1822 | ent->ecx = signature[1]; |
| 1823 | ent->edx = signature[2]; |
| 1824 | break; |
| 1825 | |
| 1826 | case HYPERV_CPUID_INTERFACE: |
| 1827 | memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12); |
| 1828 | ent->eax = signature[0]; |
| 1829 | break; |
| 1830 | |
| 1831 | case HYPERV_CPUID_VERSION: |
| 1832 | /* |
| 1833 | * We implement some Hyper-V 2016 functions so let's use |
| 1834 | * this version. |
| 1835 | */ |
| 1836 | ent->eax = 0x00003839; |
| 1837 | ent->ebx = 0x000A0000; |
| 1838 | break; |
| 1839 | |
| 1840 | case HYPERV_CPUID_FEATURES: |
| 1841 | ent->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE; |
| 1842 | ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE; |
| 1843 | ent->eax |= HV_X64_MSR_SYNIC_AVAILABLE; |
| 1844 | ent->eax |= HV_MSR_SYNTIMER_AVAILABLE; |
| 1845 | ent->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE; |
| 1846 | ent->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE; |
| 1847 | ent->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE; |
| 1848 | ent->eax |= HV_X64_MSR_RESET_AVAILABLE; |
| 1849 | ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1850 | ent->eax |= HV_X64_ACCESS_FREQUENCY_MSRS; |
| 1851 | ent->eax |= HV_X64_ACCESS_REENLIGHTENMENT; |
| 1852 | |
| 1853 | ent->ebx |= HV_X64_POST_MESSAGES; |
| 1854 | ent->ebx |= HV_X64_SIGNAL_EVENTS; |
| 1855 | |
| 1856 | ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; |
| 1857 | ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 1858 | |
| 1859 | /* |
| 1860 | * Direct Synthetic timers only make sense with in-kernel |
| 1861 | * LAPIC |
| 1862 | */ |
| 1863 | if (lapic_in_kernel(vcpu)) |
| 1864 | ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1865 | |
| 1866 | break; |
| 1867 | |
| 1868 | case HYPERV_CPUID_ENLIGHTMENT_INFO: |
| 1869 | ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; |
| 1870 | ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1871 | ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; |
| 1872 | ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; |
| 1873 | ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; |
Vitaly Kuznetsov | f1adcea | 2019-01-25 12:19:34 +0100 | [diff] [blame] | 1874 | if (evmcs_ver) |
| 1875 | ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; |
Vitaly Kuznetsov | b2d8b16 | 2019-09-16 18:22:57 +0200 | [diff] [blame^] | 1876 | if (!cpu_smt_possible()) |
| 1877 | ent->eax |= HV_X64_NO_NONARCH_CORESHARING; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 1878 | /* |
| 1879 | * Default number of spinlock retry attempts, matches |
| 1880 | * HyperV 2016. |
| 1881 | */ |
| 1882 | ent->ebx = 0x00000FFF; |
| 1883 | |
| 1884 | break; |
| 1885 | |
| 1886 | case HYPERV_CPUID_IMPLEMENT_LIMITS: |
| 1887 | /* Maximum number of virtual processors */ |
| 1888 | ent->eax = KVM_MAX_VCPUS; |
| 1889 | /* |
| 1890 | * Maximum number of logical processors, matches |
| 1891 | * HyperV 2016. |
| 1892 | */ |
| 1893 | ent->ebx = 64; |
| 1894 | |
| 1895 | break; |
| 1896 | |
| 1897 | case HYPERV_CPUID_NESTED_FEATURES: |
| 1898 | ent->eax = evmcs_ver; |
| 1899 | |
| 1900 | break; |
| 1901 | |
| 1902 | default: |
| 1903 | break; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | if (copy_to_user(entries, cpuid_entries, |
| 1908 | nent * sizeof(struct kvm_cpuid_entry2))) |
| 1909 | return -EFAULT; |
| 1910 | |
| 1911 | return 0; |
| 1912 | } |