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