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" |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 24 | #include "cpuid.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 25 | #include "hyperv.h" |
Joao Martins | 79033be | 2018-06-13 09:55:44 -0400 | [diff] [blame] | 26 | #include "xen.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 27 | |
Vitaly Kuznetsov | b2d8b16 | 2019-09-16 18:22:57 +0200 | [diff] [blame] | 28 | #include <linux/cpu.h> |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 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" |
Peter Xu | 59508b3 | 2019-12-04 20:07:17 +0100 | [diff] [blame] | 38 | #include "irq.h" |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 39 | #include "fpu.h" |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 40 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 41 | /* "Hv#1" signature */ |
| 42 | #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 |
| 43 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 44 | #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) |
| 45 | |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 46 | static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, |
| 47 | bool vcpu_kick); |
| 48 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 49 | static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) |
| 50 | { |
| 51 | return atomic64_read(&synic->sint[sint]); |
| 52 | } |
| 53 | |
| 54 | static inline int synic_get_sint_vector(u64 sint_value) |
| 55 | { |
| 56 | if (sint_value & HV_SYNIC_SINT_MASKED) |
| 57 | return -1; |
| 58 | return sint_value & HV_SYNIC_SINT_VECTOR_MASK; |
| 59 | } |
| 60 | |
| 61 | static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, |
| 62 | int vector) |
| 63 | { |
| 64 | int i; |
| 65 | |
| 66 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 67 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, |
| 74 | int vector) |
| 75 | { |
| 76 | int i; |
| 77 | u64 sint_value; |
| 78 | |
| 79 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 80 | sint_value = synic_read_sint(synic, i); |
| 81 | if (synic_get_sint_vector(sint_value) == vector && |
| 82 | sint_value & HV_SYNIC_SINT_AUTO_EOI) |
| 83 | return true; |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 88 | static void synic_update_vector(struct kvm_vcpu_hv_synic *synic, |
| 89 | int vector) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 90 | { |
Vitaly Kuznetsov | 0f250a6 | 2021-08-10 23:52:46 +0300 | [diff] [blame] | 91 | struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); |
| 92 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
| 93 | int auto_eoi_old, auto_eoi_new; |
| 94 | |
Vitaly Kuznetsov | 87a8d79 | 2018-12-05 16:36:21 +0100 | [diff] [blame] | 95 | if (vector < HV_SYNIC_FIRST_VALID_VECTOR) |
| 96 | return; |
| 97 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 98 | if (synic_has_vector_connected(synic, vector)) |
| 99 | __set_bit(vector, synic->vec_bitmap); |
| 100 | else |
| 101 | __clear_bit(vector, synic->vec_bitmap); |
| 102 | |
Vitaly Kuznetsov | 0f250a6 | 2021-08-10 23:52:46 +0300 | [diff] [blame] | 103 | auto_eoi_old = bitmap_weight(synic->auto_eoi_bitmap, 256); |
| 104 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 105 | if (synic_has_vector_auto_eoi(synic, vector)) |
| 106 | __set_bit(vector, synic->auto_eoi_bitmap); |
| 107 | else |
| 108 | __clear_bit(vector, synic->auto_eoi_bitmap); |
Vitaly Kuznetsov | 0f250a6 | 2021-08-10 23:52:46 +0300 | [diff] [blame] | 109 | |
| 110 | auto_eoi_new = bitmap_weight(synic->auto_eoi_bitmap, 256); |
| 111 | |
| 112 | if (!!auto_eoi_old == !!auto_eoi_new) |
| 113 | return; |
| 114 | |
Sean Christopherson | 187c883 | 2021-10-21 17:49:27 -0700 | [diff] [blame] | 115 | down_write(&vcpu->kvm->arch.apicv_update_lock); |
Vitaly Kuznetsov | 0f250a6 | 2021-08-10 23:52:46 +0300 | [diff] [blame] | 116 | |
| 117 | if (auto_eoi_new) |
| 118 | hv->synic_auto_eoi_used++; |
| 119 | else |
| 120 | hv->synic_auto_eoi_used--; |
| 121 | |
| 122 | __kvm_request_apicv_update(vcpu->kvm, |
| 123 | !hv->synic_auto_eoi_used, |
| 124 | APICV_INHIBIT_REASON_HYPERV); |
| 125 | |
Sean Christopherson | 187c883 | 2021-10-21 17:49:27 -0700 | [diff] [blame] | 126 | up_write(&vcpu->kvm->arch.apicv_update_lock); |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, |
| 130 | u64 data, bool host) |
| 131 | { |
| 132 | int vector, old_vector; |
Vitaly Kuznetsov | 915e6f7 | 2018-03-01 15:15:14 +0100 | [diff] [blame] | 133 | bool masked; |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 134 | |
| 135 | vector = data & HV_SYNIC_SINT_VECTOR_MASK; |
Vitaly Kuznetsov | 915e6f7 | 2018-03-01 15:15:14 +0100 | [diff] [blame] | 136 | masked = data & HV_SYNIC_SINT_MASKED; |
| 137 | |
| 138 | /* |
| 139 | * Valid vectors are 16-255, however, nested Hyper-V attempts to write |
| 140 | * default '0x10000' value on boot and this should not #GP. We need to |
| 141 | * allow zero-initing the register from host as well. |
| 142 | */ |
| 143 | if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) |
Vitaly Kuznetsov | 98f65ad | 2018-03-01 15:15:13 +0100 | [diff] [blame] | 144 | return 1; |
| 145 | /* |
| 146 | * Guest may configure multiple SINTs to use the same vector, so |
| 147 | * we maintain a bitmap of vectors handled by synic, and a |
| 148 | * bitmap of vectors with auto-eoi behavior. The bitmaps are |
| 149 | * updated here, and atomically queried on fast paths. |
| 150 | */ |
| 151 | old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK; |
| 152 | |
| 153 | atomic64_set(&synic->sint[sint], data); |
| 154 | |
| 155 | synic_update_vector(synic, old_vector); |
| 156 | |
| 157 | synic_update_vector(synic, vector); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 158 | |
| 159 | /* Load SynIC vectors into EOI exit bitmap */ |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 160 | kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic)); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 164 | static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) |
| 165 | { |
| 166 | struct kvm_vcpu *vcpu = NULL; |
| 167 | int i; |
| 168 | |
Vitaly Kuznetsov | 9170200 | 2018-08-22 12:18:28 +0200 | [diff] [blame] | 169 | if (vpidx >= KVM_MAX_VCPUS) |
| 170 | return NULL; |
| 171 | |
| 172 | vcpu = kvm_get_vcpu(kvm, vpidx); |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 173 | if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx) |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 174 | return vcpu; |
| 175 | kvm_for_each_vcpu(i, vcpu, kvm) |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 176 | if (kvm_hv_get_vpindex(vcpu) == vpidx) |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 177 | return vcpu; |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | 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] | 182 | { |
| 183 | struct kvm_vcpu *vcpu; |
| 184 | struct kvm_vcpu_hv_synic *synic; |
| 185 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 186 | vcpu = get_vcpu_by_vpidx(kvm, vpidx); |
Wanpeng Li | 919f4eb | 2021-02-26 15:59:59 +0800 | [diff] [blame] | 187 | if (!vcpu || !to_hv_vcpu(vcpu)) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 188 | return NULL; |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 189 | synic = to_hv_synic(vcpu); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 190 | return (synic->active) ? synic : NULL; |
| 191 | } |
| 192 | |
| 193 | static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) |
| 194 | { |
| 195 | struct kvm *kvm = vcpu->kvm; |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 196 | struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 197 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 198 | struct kvm_vcpu_hv_stimer *stimer; |
Vitaly Kuznetsov | 08a800a | 2018-11-26 16:47:32 +0100 | [diff] [blame] | 199 | int gsi, idx; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 200 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 201 | trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 202 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 203 | /* Try to deliver pending Hyper-V SynIC timers messages */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 204 | for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { |
| 205 | stimer = &hv_vcpu->stimer[idx]; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 206 | if (stimer->msg_pending && stimer->config.enable && |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 207 | !stimer->config.direct_mode && |
Vitaly Kuznetsov | 08a800a | 2018-11-26 16:47:32 +0100 | [diff] [blame] | 208 | stimer->config.sintx == sint) |
| 209 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 210 | } |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 211 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 212 | idx = srcu_read_lock(&kvm->irq_srcu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 213 | gsi = atomic_read(&synic->sint_to_gsi[sint]); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 214 | if (gsi != -1) |
| 215 | kvm_notify_acked_gsi(kvm, gsi); |
| 216 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 217 | } |
| 218 | |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 219 | static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) |
| 220 | { |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 221 | struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 222 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 223 | |
| 224 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; |
| 225 | hv_vcpu->exit.u.synic.msr = msr; |
| 226 | hv_vcpu->exit.u.synic.control = synic->control; |
| 227 | hv_vcpu->exit.u.synic.evt_page = synic->evt_page; |
| 228 | hv_vcpu->exit.u.synic.msg_page = synic->msg_page; |
| 229 | |
| 230 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 231 | } |
| 232 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 233 | static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, |
| 234 | u32 msr, u64 data, bool host) |
| 235 | { |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 236 | struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 237 | int ret; |
| 238 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 239 | if (!synic->active && !host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 240 | return 1; |
| 241 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 242 | trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); |
| 243 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 244 | ret = 0; |
| 245 | switch (msr) { |
| 246 | case HV_X64_MSR_SCONTROL: |
| 247 | synic->control = 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_SVERSION: |
| 252 | if (!host) { |
| 253 | ret = 1; |
| 254 | break; |
| 255 | } |
| 256 | synic->version = data; |
| 257 | break; |
| 258 | case HV_X64_MSR_SIEFP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 259 | if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && |
| 260 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 261 | if (kvm_clear_guest(vcpu->kvm, |
| 262 | data & PAGE_MASK, PAGE_SIZE)) { |
| 263 | ret = 1; |
| 264 | break; |
| 265 | } |
| 266 | synic->evt_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 267 | if (!host) |
| 268 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 269 | break; |
| 270 | case HV_X64_MSR_SIMP: |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 271 | if ((data & HV_SYNIC_SIMP_ENABLE) && !host && |
| 272 | !synic->dont_zero_synic_pages) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 273 | if (kvm_clear_guest(vcpu->kvm, |
| 274 | data & PAGE_MASK, PAGE_SIZE)) { |
| 275 | ret = 1; |
| 276 | break; |
| 277 | } |
| 278 | synic->msg_page = data; |
Andrey Smetanin | db397571 | 2015-11-10 15:36:35 +0300 | [diff] [blame] | 279 | if (!host) |
| 280 | synic_exit(synic, msr); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 281 | break; |
| 282 | case HV_X64_MSR_EOM: { |
| 283 | int i; |
| 284 | |
| 285 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 286 | kvm_hv_notify_acked_sint(vcpu, i); |
| 287 | break; |
| 288 | } |
| 289 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Andrey Smetanin | 7be58a6 | 2015-12-28 18:27:23 +0300 | [diff] [blame] | 290 | ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | ret = 1; |
| 294 | break; |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 299 | static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu) |
| 300 | { |
Vitaly Kuznetsov | 10d7bf1 | 2021-05-21 11:51:37 +0200 | [diff] [blame] | 301 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 302 | |
Vitaly Kuznetsov | 10d7bf1 | 2021-05-21 11:51:37 +0200 | [diff] [blame] | 303 | return hv_vcpu->cpuid_cache.syndbg_cap_eax & |
| 304 | HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu) |
| 308 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 309 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 310 | |
| 311 | if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL) |
| 312 | hv->hv_syndbg.control.status = |
| 313 | vcpu->run->hyperv.u.syndbg.status; |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr) |
| 318 | { |
Vitaly Kuznetsov | f69b55e | 2021-01-26 14:48:08 +0100 | [diff] [blame] | 319 | struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 320 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 321 | |
| 322 | hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG; |
| 323 | hv_vcpu->exit.u.syndbg.msr = msr; |
| 324 | hv_vcpu->exit.u.syndbg.control = syndbg->control.control; |
| 325 | hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page; |
| 326 | hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page; |
| 327 | hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page; |
| 328 | vcpu->arch.complete_userspace_io = |
| 329 | kvm_hv_syndbg_complete_userspace; |
| 330 | |
| 331 | kvm_make_request(KVM_REQ_HV_EXIT, vcpu); |
| 332 | } |
| 333 | |
| 334 | static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) |
| 335 | { |
Vitaly Kuznetsov | f69b55e | 2021-01-26 14:48:08 +0100 | [diff] [blame] | 336 | struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 337 | |
| 338 | if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) |
| 339 | return 1; |
| 340 | |
| 341 | trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id, |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 342 | to_hv_vcpu(vcpu)->vp_index, msr, data); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 343 | switch (msr) { |
| 344 | case HV_X64_MSR_SYNDBG_CONTROL: |
| 345 | syndbg->control.control = data; |
| 346 | if (!host) |
| 347 | syndbg_exit(vcpu, msr); |
| 348 | break; |
| 349 | case HV_X64_MSR_SYNDBG_STATUS: |
| 350 | syndbg->control.status = data; |
| 351 | break; |
| 352 | case HV_X64_MSR_SYNDBG_SEND_BUFFER: |
| 353 | syndbg->control.send_page = data; |
| 354 | break; |
| 355 | case HV_X64_MSR_SYNDBG_RECV_BUFFER: |
| 356 | syndbg->control.recv_page = data; |
| 357 | break; |
| 358 | case HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 359 | syndbg->control.pending_page = data; |
| 360 | if (!host) |
| 361 | syndbg_exit(vcpu, msr); |
| 362 | break; |
| 363 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 364 | syndbg->options = data; |
| 365 | break; |
| 366 | default: |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
| 374 | { |
Vitaly Kuznetsov | f69b55e | 2021-01-26 14:48:08 +0100 | [diff] [blame] | 375 | struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 376 | |
| 377 | if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) |
| 378 | return 1; |
| 379 | |
| 380 | switch (msr) { |
| 381 | case HV_X64_MSR_SYNDBG_CONTROL: |
| 382 | *pdata = syndbg->control.control; |
| 383 | break; |
| 384 | case HV_X64_MSR_SYNDBG_STATUS: |
| 385 | *pdata = syndbg->control.status; |
| 386 | break; |
| 387 | case HV_X64_MSR_SYNDBG_SEND_BUFFER: |
| 388 | *pdata = syndbg->control.send_page; |
| 389 | break; |
| 390 | case HV_X64_MSR_SYNDBG_RECV_BUFFER: |
| 391 | *pdata = syndbg->control.recv_page; |
| 392 | break; |
| 393 | case HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 394 | *pdata = syndbg->control.pending_page; |
| 395 | break; |
| 396 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 397 | *pdata = syndbg->options; |
| 398 | break; |
| 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 403 | trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 408 | static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata, |
| 409 | bool host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 410 | { |
| 411 | int ret; |
| 412 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 413 | if (!synic->active && !host) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 414 | return 1; |
| 415 | |
| 416 | ret = 0; |
| 417 | switch (msr) { |
| 418 | case HV_X64_MSR_SCONTROL: |
| 419 | *pdata = synic->control; |
| 420 | break; |
| 421 | case HV_X64_MSR_SVERSION: |
| 422 | *pdata = synic->version; |
| 423 | break; |
| 424 | case HV_X64_MSR_SIEFP: |
| 425 | *pdata = synic->evt_page; |
| 426 | break; |
| 427 | case HV_X64_MSR_SIMP: |
| 428 | *pdata = synic->msg_page; |
| 429 | break; |
| 430 | case HV_X64_MSR_EOM: |
| 431 | *pdata = 0; |
| 432 | break; |
| 433 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 434 | *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); |
| 435 | break; |
| 436 | default: |
| 437 | ret = 1; |
| 438 | break; |
| 439 | } |
| 440 | return ret; |
| 441 | } |
| 442 | |
Jiang Biao | ecd8a8c | 2016-11-07 08:56:33 +0800 | [diff] [blame] | 443 | 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] | 444 | { |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 445 | struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 446 | struct kvm_lapic_irq irq; |
| 447 | int ret, vector; |
| 448 | |
| 449 | if (sint >= ARRAY_SIZE(synic->sint)) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | vector = synic_get_sint_vector(synic_read_sint(synic, sint)); |
| 453 | if (vector < 0) |
| 454 | return -ENOENT; |
| 455 | |
| 456 | memset(&irq, 0, sizeof(irq)); |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 457 | irq.shorthand = APIC_DEST_SELF; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 458 | irq.dest_mode = APIC_DEST_PHYSICAL; |
| 459 | irq.delivery_mode = APIC_DM_FIXED; |
| 460 | irq.vector = vector; |
| 461 | irq.level = 1; |
| 462 | |
Radim Krčmář | f98a3ef | 2016-12-15 18:06:45 +0100 | [diff] [blame] | 463 | 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] | 464 | 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] | 465 | return ret; |
| 466 | } |
| 467 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 468 | 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] | 469 | { |
| 470 | struct kvm_vcpu_hv_synic *synic; |
| 471 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 472 | synic = synic_get(kvm, vpidx); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 473 | if (!synic) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | return synic_set_irq(synic, sint); |
| 477 | } |
| 478 | |
| 479 | void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) |
| 480 | { |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 481 | struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 482 | int i; |
| 483 | |
Andrey Smetanin | 18659a9 | 2015-12-23 16:53:59 +0300 | [diff] [blame] | 484 | trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 485 | |
| 486 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) |
| 487 | if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) |
| 488 | kvm_hv_notify_acked_sint(vcpu, i); |
| 489 | } |
| 490 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 491 | 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] | 492 | { |
| 493 | struct kvm_vcpu_hv_synic *synic; |
| 494 | |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 495 | synic = synic_get(kvm, vpidx); |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 496 | if (!synic) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | atomic_set(&synic->sint_to_gsi[sint], gsi); |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | void kvm_hv_irq_routing_update(struct kvm *kvm) |
| 507 | { |
| 508 | struct kvm_irq_routing_table *irq_rt; |
| 509 | struct kvm_kernel_irq_routing_entry *e; |
| 510 | u32 gsi; |
| 511 | |
| 512 | irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, |
| 513 | lockdep_is_held(&kvm->irq_lock)); |
| 514 | |
| 515 | for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { |
| 516 | hlist_for_each_entry(e, &irq_rt->map[gsi], link) { |
| 517 | if (e->type == KVM_IRQ_ROUTING_HV_SINT) |
| 518 | kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, |
| 519 | e->hv_sint.sint, gsi); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static void synic_init(struct kvm_vcpu_hv_synic *synic) |
| 525 | { |
| 526 | int i; |
| 527 | |
| 528 | memset(synic, 0, sizeof(*synic)); |
| 529 | synic->version = HV_SYNIC_VERSION_1; |
| 530 | for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { |
| 531 | atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); |
| 532 | atomic_set(&synic->sint_to_gsi[i], -1); |
| 533 | } |
| 534 | } |
| 535 | |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 536 | static u64 get_time_ref_counter(struct kvm *kvm) |
| 537 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 538 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 539 | struct kvm_vcpu *vcpu; |
| 540 | u64 tsc; |
| 541 | |
| 542 | /* |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 543 | * Fall back to get_kvmclock_ns() when TSC page hasn't been set up, |
| 544 | * is broken, disabled or being updated. |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 545 | */ |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 546 | if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET) |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 547 | return div_u64(get_kvmclock_ns(kvm), 100); |
| 548 | |
| 549 | vcpu = kvm_get_vcpu(kvm, 0); |
| 550 | tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
| 551 | return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) |
| 552 | + hv->tsc_ref.tsc_offset; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 555 | static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 556 | bool vcpu_kick) |
| 557 | { |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 558 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 559 | |
| 560 | set_bit(stimer->index, |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 561 | to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 562 | kvm_make_request(KVM_REQ_HV_STIMER, vcpu); |
| 563 | if (vcpu_kick) |
| 564 | kvm_vcpu_kick(vcpu); |
| 565 | } |
| 566 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 567 | static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) |
| 568 | { |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 569 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 570 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 571 | trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 572 | stimer->index); |
| 573 | |
Andrey Smetanin | 019b978 | 2015-12-28 18:27:19 +0300 | [diff] [blame] | 574 | hrtimer_cancel(&stimer->timer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 575 | clear_bit(stimer->index, |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 576 | to_hv_vcpu(vcpu)->stimer_pending_bitmap); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 577 | stimer->msg_pending = false; |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 578 | stimer->exp_time = 0; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) |
| 582 | { |
| 583 | struct kvm_vcpu_hv_stimer *stimer; |
| 584 | |
| 585 | stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 586 | trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 587 | stimer->index); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 588 | stimer_mark_pending(stimer, true); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 589 | |
| 590 | return HRTIMER_NORESTART; |
| 591 | } |
| 592 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 593 | /* |
| 594 | * stimer_start() assumptions: |
| 595 | * a) stimer->count is not equal to 0 |
| 596 | * b) stimer->config has HV_STIMER_ENABLE flag |
| 597 | */ |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 598 | static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) |
| 599 | { |
| 600 | u64 time_now; |
| 601 | ktime_t ktime_now; |
| 602 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 603 | time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 604 | ktime_now = ktime_get(); |
| 605 | |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 606 | if (stimer->config.periodic) { |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 607 | if (stimer->exp_time) { |
| 608 | if (time_now >= stimer->exp_time) { |
| 609 | u64 remainder; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 610 | |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 611 | div64_u64_rem(time_now - stimer->exp_time, |
| 612 | stimer->count, &remainder); |
| 613 | stimer->exp_time = |
| 614 | time_now + (stimer->count - remainder); |
| 615 | } |
| 616 | } else |
| 617 | stimer->exp_time = time_now + stimer->count; |
| 618 | |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 619 | trace_kvm_hv_stimer_start_periodic( |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 620 | hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 621 | stimer->index, |
| 622 | time_now, stimer->exp_time); |
| 623 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 624 | hrtimer_start(&stimer->timer, |
Andrey Smetanin | f808495 | 2015-12-28 18:27:20 +0300 | [diff] [blame] | 625 | ktime_add_ns(ktime_now, |
| 626 | 100 * (stimer->exp_time - time_now)), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 627 | HRTIMER_MODE_ABS); |
| 628 | return 0; |
| 629 | } |
| 630 | stimer->exp_time = stimer->count; |
| 631 | if (time_now >= stimer->count) { |
| 632 | /* |
| 633 | * Expire timer according to Hypervisor Top-Level Functional |
| 634 | * specification v4(15.3.1): |
| 635 | * "If a one shot is enabled and the specified count is in |
| 636 | * the past, it will expire immediately." |
| 637 | */ |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 638 | stimer_mark_pending(stimer, false); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 642 | trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 643 | stimer->index, |
| 644 | time_now, stimer->count); |
| 645 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 646 | hrtimer_start(&stimer->timer, |
| 647 | ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), |
| 648 | HRTIMER_MODE_ABS); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, |
| 653 | bool host) |
| 654 | { |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 655 | union hv_stimer_config new_config = {.as_uint64 = config}, |
| 656 | old_config = {.as_uint64 = stimer->config.as_uint64}; |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 657 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Vitaly Kuznetsov | 1aa8a41 | 2021-05-21 11:51:53 +0200 | [diff] [blame] | 658 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 659 | struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); |
Vitaly Kuznetsov | dbcf3f9 | 2020-09-24 16:57:52 +0200 | [diff] [blame] | 660 | |
| 661 | if (!synic->active && !host) |
| 662 | return 1; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 663 | |
Vitaly Kuznetsov | 1aa8a41 | 2021-05-21 11:51:53 +0200 | [diff] [blame] | 664 | if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode && |
| 665 | !(hv_vcpu->cpuid_cache.features_edx & |
| 666 | HV_STIMER_DIRECT_MODE_AVAILABLE))) |
| 667 | return 1; |
| 668 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 669 | trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 670 | stimer->index, config, host); |
| 671 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 672 | stimer_cleanup(stimer); |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 673 | if (old_config.enable && |
| 674 | !new_config.direct_mode && new_config.sintx == 0) |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 675 | new_config.enable = 0; |
| 676 | stimer->config.as_uint64 = new_config.as_uint64; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 677 | |
Vitaly Kuznetsov | 013cc6e | 2019-03-13 18:13:42 +0100 | [diff] [blame] | 678 | if (stimer->config.enable) |
| 679 | stimer_mark_pending(stimer, false); |
| 680 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, |
| 685 | bool host) |
| 686 | { |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 687 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 688 | struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); |
Vitaly Kuznetsov | dbcf3f9 | 2020-09-24 16:57:52 +0200 | [diff] [blame] | 689 | |
| 690 | if (!synic->active && !host) |
| 691 | return 1; |
| 692 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 693 | trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 694 | stimer->index, count, host); |
| 695 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 696 | stimer_cleanup(stimer); |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 697 | stimer->count = count; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 698 | if (stimer->count == 0) |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 699 | stimer->config.enable = 0; |
| 700 | else if (stimer->config.auto_enable) |
| 701 | stimer->config.enable = 1; |
Vitaly Kuznetsov | 013cc6e | 2019-03-13 18:13:42 +0100 | [diff] [blame] | 702 | |
| 703 | if (stimer->config.enable) |
| 704 | stimer_mark_pending(stimer, false); |
| 705 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) |
| 710 | { |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 711 | *pconfig = stimer->config.as_uint64; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) |
| 716 | { |
| 717 | *pcount = stimer->count; |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | 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] | 722 | struct hv_message *src_msg, bool no_retry) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 723 | { |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 724 | struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 725 | int msg_off = offsetof(struct hv_message_page, sint_message[sint]); |
| 726 | gfn_t msg_page_gfn; |
| 727 | struct hv_message_header hv_hdr; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 728 | int r; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 729 | |
| 730 | if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) |
| 731 | return -ENOENT; |
| 732 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 733 | msg_page_gfn = synic->msg_page >> PAGE_SHIFT; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 734 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 735 | /* |
| 736 | * Strictly following the spec-mandated ordering would assume setting |
| 737 | * .msg_pending before checking .message_type. However, this function |
| 738 | * is only called in vcpu context so the entire update is atomic from |
| 739 | * guest POV and thus the exact order here doesn't matter. |
| 740 | */ |
| 741 | r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type, |
| 742 | msg_off + offsetof(struct hv_message, |
| 743 | header.message_type), |
| 744 | sizeof(hv_hdr.message_type)); |
| 745 | if (r < 0) |
| 746 | return r; |
| 747 | |
| 748 | if (hv_hdr.message_type != HVMSG_NONE) { |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 749 | if (no_retry) |
| 750 | return 0; |
| 751 | |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 752 | hv_hdr.message_flags.msg_pending = 1; |
| 753 | r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, |
| 754 | &hv_hdr.message_flags, |
| 755 | msg_off + |
| 756 | offsetof(struct hv_message, |
| 757 | header.message_flags), |
| 758 | sizeof(hv_hdr.message_flags)); |
| 759 | if (r < 0) |
| 760 | return r; |
| 761 | return -EAGAIN; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 762 | } |
Roman Kagan | 3a0e773 | 2018-12-10 18:47:26 +0000 | [diff] [blame] | 763 | |
| 764 | r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off, |
| 765 | sizeof(src_msg->header) + |
| 766 | src_msg->header.payload_size); |
| 767 | if (r < 0) |
| 768 | return r; |
| 769 | |
| 770 | r = synic_set_irq(synic, sint); |
| 771 | if (r < 0) |
| 772 | return r; |
| 773 | if (r == 0) |
| 774 | return -EFAULT; |
| 775 | return 0; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 776 | } |
| 777 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 778 | static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 779 | { |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 780 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 781 | struct hv_message *msg = &stimer->msg; |
| 782 | struct hv_timer_message_payload *payload = |
| 783 | (struct hv_timer_message_payload *)&msg->u.payload; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 784 | |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 785 | /* |
| 786 | * To avoid piling up periodic ticks, don't retry message |
| 787 | * delivery for them (within "lazy" lost ticks policy). |
| 788 | */ |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 789 | bool no_retry = stimer->config.periodic; |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 790 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 791 | payload->expiration_time = stimer->exp_time; |
| 792 | payload->delivery_time = get_time_ref_counter(vcpu->kvm); |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 793 | return synic_deliver_msg(to_hv_synic(vcpu), |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 794 | stimer->config.sintx, msg, |
Roman Kagan | 7deec5e | 2018-12-10 18:47:27 +0000 | [diff] [blame] | 795 | no_retry); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 796 | } |
| 797 | |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 798 | static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer) |
| 799 | { |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 800 | struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 801 | struct kvm_lapic_irq irq = { |
| 802 | .delivery_mode = APIC_DM_FIXED, |
| 803 | .vector = stimer->config.apic_vector |
| 804 | }; |
| 805 | |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 806 | if (lapic_in_kernel(vcpu)) |
| 807 | return !kvm_apic_set_irq(vcpu, &irq, NULL); |
| 808 | return 0; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 811 | static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) |
| 812 | { |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 813 | int r, direct = stimer->config.direct_mode; |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 814 | |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 815 | stimer->msg_pending = true; |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 816 | if (!direct) |
| 817 | r = stimer_send_msg(stimer); |
| 818 | else |
| 819 | r = stimer_notify_direct(stimer); |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 820 | trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id, |
Vitaly Kuznetsov | 8644f77 | 2018-11-26 16:47:31 +0100 | [diff] [blame] | 821 | stimer->index, direct, r); |
Andrey Smetanin | ac3e5fc | 2015-12-23 16:54:00 +0300 | [diff] [blame] | 822 | if (!r) { |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 823 | stimer->msg_pending = false; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 824 | if (!(stimer->config.periodic)) |
| 825 | stimer->config.enable = 0; |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 826 | } |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) |
| 830 | { |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 831 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 832 | struct kvm_vcpu_hv_stimer *stimer; |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 833 | u64 time_now, exp_time; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 834 | int i; |
| 835 | |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 836 | if (!hv_vcpu) |
| 837 | return; |
| 838 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 839 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 840 | if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { |
| 841 | stimer = &hv_vcpu->stimer[i]; |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 842 | if (stimer->config.enable) { |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 843 | exp_time = stimer->exp_time; |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 844 | |
Andrey Smetanin | f3b138c | 2015-12-28 18:27:24 +0300 | [diff] [blame] | 845 | if (exp_time) { |
| 846 | time_now = |
| 847 | get_time_ref_counter(vcpu->kvm); |
| 848 | if (time_now >= exp_time) |
| 849 | stimer_expiration(stimer); |
| 850 | } |
| 851 | |
Vitaly Kuznetsov | 6a058a1e | 2018-11-26 16:47:30 +0100 | [diff] [blame] | 852 | if ((stimer->config.enable) && |
Roman Kagan | f1ff89e | 2017-07-20 17:26:40 +0300 | [diff] [blame] | 853 | stimer->count) { |
| 854 | if (!stimer->msg_pending) |
| 855 | stimer_start(stimer); |
| 856 | } else |
Andrey Smetanin | 0cdeabb | 2015-12-28 18:27:21 +0300 | [diff] [blame] | 857 | stimer_cleanup(stimer); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 863 | { |
Vitaly Kuznetsov | ef3f398 | 2021-01-26 14:48:05 +0100 | [diff] [blame] | 864 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 865 | int i; |
| 866 | |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 867 | if (!hv_vcpu) |
| 868 | return; |
| 869 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 870 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 871 | stimer_cleanup(&hv_vcpu->stimer[i]); |
Vitaly Kuznetsov | 4592b7e | 2021-01-26 14:48:13 +0100 | [diff] [blame] | 872 | |
| 873 | kfree(hv_vcpu); |
| 874 | vcpu->arch.hyperv = NULL; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 875 | } |
| 876 | |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 877 | bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu) |
| 878 | { |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 879 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
| 880 | |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 881 | if (!hv_vcpu) |
| 882 | return false; |
| 883 | |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 884 | if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 885 | return false; |
| 886 | return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; |
| 887 | } |
| 888 | EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled); |
| 889 | |
| 890 | bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu, |
| 891 | struct hv_vp_assist_page *assist_page) |
| 892 | { |
| 893 | if (!kvm_hv_assist_page_enabled(vcpu)) |
| 894 | return false; |
| 895 | return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, |
| 896 | assist_page, sizeof(*assist_page)); |
| 897 | } |
| 898 | EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page); |
| 899 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 900 | static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) |
| 901 | { |
| 902 | struct hv_message *msg = &stimer->msg; |
| 903 | struct hv_timer_message_payload *payload = |
| 904 | (struct hv_timer_message_payload *)&msg->u.payload; |
| 905 | |
| 906 | memset(&msg->header, 0, sizeof(msg->header)); |
| 907 | msg->header.message_type = HVMSG_TIMER_EXPIRED; |
| 908 | msg->header.payload_size = sizeof(*payload); |
| 909 | |
| 910 | payload->timer_index = stimer->index; |
| 911 | payload->expiration_time = 0; |
| 912 | payload->delivery_time = 0; |
| 913 | } |
| 914 | |
| 915 | static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) |
| 916 | { |
| 917 | memset(stimer, 0, sizeof(*stimer)); |
| 918 | stimer->index = timer_index; |
| 919 | hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
| 920 | stimer->timer.function = stimer_timer_callback; |
| 921 | stimer_prepare_msg(stimer); |
| 922 | } |
| 923 | |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 924 | static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 925 | { |
Vitaly Kuznetsov | 4592b7e | 2021-01-26 14:48:13 +0100 | [diff] [blame] | 926 | struct kvm_vcpu_hv *hv_vcpu; |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 927 | int i; |
| 928 | |
Vitaly Kuznetsov | 4592b7e | 2021-01-26 14:48:13 +0100 | [diff] [blame] | 929 | hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT); |
| 930 | if (!hv_vcpu) |
| 931 | return -ENOMEM; |
| 932 | |
| 933 | vcpu->arch.hyperv = hv_vcpu; |
| 934 | hv_vcpu->vcpu = vcpu; |
| 935 | |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 936 | synic_init(&hv_vcpu->synic); |
| 937 | |
| 938 | bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); |
| 939 | for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) |
| 940 | stimer_init(&hv_vcpu->stimer[i], i); |
Vitaly Kuznetsov | 4592b7e | 2021-01-26 14:48:13 +0100 | [diff] [blame] | 941 | |
Sean Christopherson | 4eeef24 | 2021-09-10 11:32:19 -0700 | [diff] [blame] | 942 | hv_vcpu->vp_index = vcpu->vcpu_idx; |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 943 | |
| 944 | return 0; |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 945 | } |
| 946 | |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 947 | 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] | 948 | { |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 949 | struct kvm_vcpu_hv_synic *synic; |
| 950 | int r; |
| 951 | |
| 952 | if (!to_hv_vcpu(vcpu)) { |
| 953 | r = kvm_hv_vcpu_init(vcpu); |
| 954 | if (r) |
| 955 | return r; |
| 956 | } |
| 957 | |
| 958 | synic = to_hv_synic(vcpu); |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 959 | |
Roman Kagan | efc479e | 2017-06-22 16:51:01 +0300 | [diff] [blame] | 960 | synic->active = true; |
| 961 | synic->dont_zero_synic_pages = dont_zero_synic_pages; |
Jon Doron | 99b48ec | 2020-07-17 15:52:38 +0300 | [diff] [blame] | 962 | synic->control = HV_SYNIC_CONTROL_ENABLE; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 966 | static bool kvm_hv_msr_partition_wide(u32 msr) |
| 967 | { |
| 968 | bool r = false; |
| 969 | |
| 970 | switch (msr) { |
| 971 | case HV_X64_MSR_GUEST_OS_ID: |
| 972 | case HV_X64_MSR_HYPERCALL: |
| 973 | case HV_X64_MSR_REFERENCE_TSC: |
| 974 | case HV_X64_MSR_TIME_REF_COUNT: |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 975 | case HV_X64_MSR_CRASH_CTL: |
| 976 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 977 | case HV_X64_MSR_RESET: |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 978 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 979 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 980 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 981 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 982 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 983 | r = true; |
| 984 | break; |
| 985 | } |
| 986 | |
| 987 | return r; |
| 988 | } |
| 989 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 990 | static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 991 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 992 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 993 | size_t size = ARRAY_SIZE(hv->hv_crash_param); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 994 | |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 995 | if (WARN_ON_ONCE(index >= size)) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 996 | return -EINVAL; |
| 997 | |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 998 | *pdata = hv->hv_crash_param[array_index_nospec(index, size)]; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 999 | return 0; |
| 1000 | } |
| 1001 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1002 | static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1003 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1004 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1005 | |
| 1006 | *pdata = hv->hv_crash_ctl; |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1010 | static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1011 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1012 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1013 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1014 | hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1019 | static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1020 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1021 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 1022 | size_t size = ARRAY_SIZE(hv->hv_crash_param); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1023 | |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 1024 | if (WARN_ON_ONCE(index >= size)) |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1025 | return -EINVAL; |
| 1026 | |
Marios Pomonis | 8618793 | 2019-12-11 12:47:42 -0800 | [diff] [blame] | 1027 | hv->hv_crash_param[array_index_nospec(index, size)] = data; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
| 1030 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1031 | /* |
| 1032 | * The kvmclock and Hyper-V TSC page use similar formulas, and converting |
| 1033 | * between them is possible: |
| 1034 | * |
| 1035 | * kvmclock formula: |
| 1036 | * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) |
| 1037 | * + system_time |
| 1038 | * |
| 1039 | * Hyper-V formula: |
| 1040 | * nsec/100 = ticks * scale / 2^64 + offset |
| 1041 | * |
| 1042 | * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. |
| 1043 | * By dividing the kvmclock formula by 100 and equating what's left we get: |
| 1044 | * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 1045 | * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 1046 | * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 |
| 1047 | * |
| 1048 | * Now expand the kvmclock formula and divide by 100: |
| 1049 | * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) |
| 1050 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) |
| 1051 | * + system_time |
| 1052 | * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 1053 | * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 |
| 1054 | * + system_time / 100 |
| 1055 | * |
| 1056 | * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: |
| 1057 | * nsec/100 = ticks * scale / 2^64 |
| 1058 | * - tsc_timestamp * scale / 2^64 |
| 1059 | * + system_time / 100 |
| 1060 | * |
| 1061 | * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: |
| 1062 | * offset = system_time / 100 - tsc_timestamp * scale / 2^64 |
| 1063 | * |
| 1064 | * These two equivalencies are implemented in this function. |
| 1065 | */ |
| 1066 | static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, |
Michael Kelley | 7357b1d | 2020-04-22 12:57:34 -0700 | [diff] [blame] | 1067 | struct ms_hyperv_tsc_page *tsc_ref) |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1068 | { |
| 1069 | u64 max_mul; |
| 1070 | |
| 1071 | if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) |
| 1072 | return false; |
| 1073 | |
| 1074 | /* |
| 1075 | * check if scale would overflow, if so we use the time ref counter |
| 1076 | * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 |
| 1077 | * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) |
| 1078 | * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) |
| 1079 | */ |
| 1080 | max_mul = 100ull << (32 - hv_clock->tsc_shift); |
| 1081 | if (hv_clock->tsc_to_system_mul >= max_mul) |
| 1082 | return false; |
| 1083 | |
| 1084 | /* |
| 1085 | * Otherwise compute the scale and offset according to the formulas |
| 1086 | * derived above. |
| 1087 | */ |
| 1088 | tsc_ref->tsc_scale = |
| 1089 | mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), |
| 1090 | hv_clock->tsc_to_system_mul, |
| 1091 | 100); |
| 1092 | |
| 1093 | tsc_ref->tsc_offset = hv_clock->system_time; |
| 1094 | do_div(tsc_ref->tsc_offset, 100); |
| 1095 | tsc_ref->tsc_offset -= |
| 1096 | mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); |
| 1097 | return true; |
| 1098 | } |
| 1099 | |
Vitaly Kuznetsov | 0469f2f | 2021-03-16 15:37:36 +0100 | [diff] [blame] | 1100 | /* |
| 1101 | * Don't touch TSC page values if the guest has opted for TSC emulation after |
| 1102 | * migration. KVM doesn't fully support reenlightenment notifications and TSC |
| 1103 | * access emulation and Hyper-V is known to expect the values in TSC page to |
| 1104 | * stay constant before TSC access emulation is disabled from guest side |
| 1105 | * (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC |
| 1106 | * frequency and guest visible TSC value across migration (and prevent it when |
| 1107 | * TSC scaling is unsupported). |
| 1108 | */ |
| 1109 | static inline bool tsc_page_update_unsafe(struct kvm_hv *hv) |
| 1110 | { |
| 1111 | return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) && |
| 1112 | hv->hv_tsc_emulation_control; |
| 1113 | } |
| 1114 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1115 | void kvm_hv_setup_tsc_page(struct kvm *kvm, |
| 1116 | struct pvclock_vcpu_time_info *hv_clock) |
| 1117 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1118 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1119 | u32 tsc_seq; |
| 1120 | u64 gfn; |
| 1121 | |
| 1122 | BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); |
Michael Kelley | 7357b1d | 2020-04-22 12:57:34 -0700 | [diff] [blame] | 1123 | BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1124 | |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1125 | if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN || |
| 1126 | hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET) |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1127 | return; |
| 1128 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1129 | mutex_lock(&hv->hv_lock); |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1130 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 1131 | goto out_unlock; |
| 1132 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1133 | gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 1134 | /* |
| 1135 | * Because the TSC parameters only vary when there is a |
| 1136 | * change in the master clock, do not bother with caching. |
| 1137 | */ |
| 1138 | if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), |
| 1139 | &tsc_seq, sizeof(tsc_seq)))) |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1140 | goto out_err; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1141 | |
Vitaly Kuznetsov | 0469f2f | 2021-03-16 15:37:36 +0100 | [diff] [blame] | 1142 | if (tsc_seq && tsc_page_update_unsafe(hv)) { |
| 1143 | if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) |
| 1144 | goto out_err; |
| 1145 | |
| 1146 | hv->hv_tsc_page_status = HV_TSC_PAGE_SET; |
| 1147 | goto out_unlock; |
| 1148 | } |
| 1149 | |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1150 | /* |
| 1151 | * While we're computing and writing the parameters, force the |
| 1152 | * guest to use the time reference count MSR. |
| 1153 | */ |
| 1154 | hv->tsc_ref.tsc_sequence = 0; |
| 1155 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 1156 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1157 | goto out_err; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1158 | |
| 1159 | if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1160 | goto out_err; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1161 | |
| 1162 | /* Ensure sequence is zero before writing the rest of the struct. */ |
| 1163 | smp_wmb(); |
| 1164 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1165 | goto out_err; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1166 | |
| 1167 | /* |
| 1168 | * Now switch to the TSC page mechanism by writing the sequence. |
| 1169 | */ |
| 1170 | tsc_seq++; |
| 1171 | if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) |
| 1172 | tsc_seq = 1; |
| 1173 | |
| 1174 | /* Write the struct entirely before the non-zero sequence. */ |
| 1175 | smp_wmb(); |
| 1176 | |
| 1177 | hv->tsc_ref.tsc_sequence = tsc_seq; |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1178 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 1179 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) |
| 1180 | goto out_err; |
| 1181 | |
| 1182 | hv->hv_tsc_page_status = HV_TSC_PAGE_SET; |
| 1183 | goto out_unlock; |
| 1184 | |
| 1185 | out_err: |
| 1186 | hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN; |
Paolo Bonzini | 3f5ad8b | 2016-12-12 10:12:53 +0100 | [diff] [blame] | 1187 | out_unlock: |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1188 | mutex_unlock(&hv->hv_lock); |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Vitaly Kuznetsov | e880c6e | 2021-03-16 15:37:34 +0100 | [diff] [blame] | 1191 | void kvm_hv_invalidate_tsc_page(struct kvm *kvm) |
| 1192 | { |
| 1193 | struct kvm_hv *hv = to_kvm_hv(kvm); |
| 1194 | u64 gfn; |
Wanpeng Li | da6d63a | 2021-05-18 05:00:34 -0700 | [diff] [blame] | 1195 | int idx; |
Vitaly Kuznetsov | e880c6e | 2021-03-16 15:37:34 +0100 | [diff] [blame] | 1196 | |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1197 | if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN || |
Vitaly Kuznetsov | 0469f2f | 2021-03-16 15:37:36 +0100 | [diff] [blame] | 1198 | hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET || |
| 1199 | tsc_page_update_unsafe(hv)) |
Vitaly Kuznetsov | e880c6e | 2021-03-16 15:37:34 +0100 | [diff] [blame] | 1200 | return; |
| 1201 | |
| 1202 | mutex_lock(&hv->hv_lock); |
| 1203 | |
| 1204 | if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) |
| 1205 | goto out_unlock; |
| 1206 | |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1207 | /* Preserve HV_TSC_PAGE_GUEST_CHANGED/HV_TSC_PAGE_HOST_CHANGED states */ |
| 1208 | if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET) |
| 1209 | hv->hv_tsc_page_status = HV_TSC_PAGE_UPDATING; |
| 1210 | |
Vitaly Kuznetsov | e880c6e | 2021-03-16 15:37:34 +0100 | [diff] [blame] | 1211 | gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; |
| 1212 | |
| 1213 | hv->tsc_ref.tsc_sequence = 0; |
Wanpeng Li | da6d63a | 2021-05-18 05:00:34 -0700 | [diff] [blame] | 1214 | |
| 1215 | /* |
| 1216 | * Take the srcu lock as memslots will be accessed to check the gfn |
| 1217 | * cache generation against the memslots generation. |
| 1218 | */ |
| 1219 | idx = srcu_read_lock(&kvm->srcu); |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1220 | if (kvm_write_guest(kvm, gfn_to_gpa(gfn), |
| 1221 | &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) |
| 1222 | hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN; |
Wanpeng Li | da6d63a | 2021-05-18 05:00:34 -0700 | [diff] [blame] | 1223 | srcu_read_unlock(&kvm->srcu, idx); |
Vitaly Kuznetsov | e880c6e | 2021-03-16 15:37:34 +0100 | [diff] [blame] | 1224 | |
| 1225 | out_unlock: |
| 1226 | mutex_unlock(&hv->hv_lock); |
| 1227 | } |
| 1228 | |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1229 | |
| 1230 | static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr) |
| 1231 | { |
Vitaly Kuznetsov | 1561c2c | 2021-05-21 11:51:39 +0200 | [diff] [blame] | 1232 | if (!hv_vcpu->enforce_cpuid) |
| 1233 | return true; |
| 1234 | |
| 1235 | switch (msr) { |
| 1236 | case HV_X64_MSR_GUEST_OS_ID: |
| 1237 | case HV_X64_MSR_HYPERCALL: |
| 1238 | return hv_vcpu->cpuid_cache.features_eax & |
| 1239 | HV_MSR_HYPERCALL_AVAILABLE; |
Vitaly Kuznetsov | b80a92f | 2021-05-21 11:51:40 +0200 | [diff] [blame] | 1240 | case HV_X64_MSR_VP_RUNTIME: |
| 1241 | return hv_vcpu->cpuid_cache.features_eax & |
| 1242 | HV_MSR_VP_RUNTIME_AVAILABLE; |
Vitaly Kuznetsov | c2b3286 | 2021-05-21 11:51:41 +0200 | [diff] [blame] | 1243 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1244 | return hv_vcpu->cpuid_cache.features_eax & |
| 1245 | HV_MSR_TIME_REF_COUNT_AVAILABLE; |
Vitaly Kuznetsov | d2ac25d | 2021-05-21 11:51:42 +0200 | [diff] [blame] | 1246 | case HV_X64_MSR_VP_INDEX: |
| 1247 | return hv_vcpu->cpuid_cache.features_eax & |
| 1248 | HV_MSR_VP_INDEX_AVAILABLE; |
Vitaly Kuznetsov | 679008e | 2021-05-21 11:51:43 +0200 | [diff] [blame] | 1249 | case HV_X64_MSR_RESET: |
| 1250 | return hv_vcpu->cpuid_cache.features_eax & |
| 1251 | HV_MSR_RESET_AVAILABLE; |
Vitaly Kuznetsov | a1ec661 | 2021-05-21 11:51:44 +0200 | [diff] [blame] | 1252 | case HV_X64_MSR_REFERENCE_TSC: |
| 1253 | return hv_vcpu->cpuid_cache.features_eax & |
| 1254 | HV_MSR_REFERENCE_TSC_AVAILABLE; |
Vitaly Kuznetsov | 9e2715c | 2021-05-21 11:51:45 +0200 | [diff] [blame] | 1255 | case HV_X64_MSR_SCONTROL: |
| 1256 | case HV_X64_MSR_SVERSION: |
| 1257 | case HV_X64_MSR_SIEFP: |
| 1258 | case HV_X64_MSR_SIMP: |
| 1259 | case HV_X64_MSR_EOM: |
| 1260 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
| 1261 | return hv_vcpu->cpuid_cache.features_eax & |
| 1262 | HV_MSR_SYNIC_AVAILABLE; |
Vitaly Kuznetsov | eba60dd | 2021-05-21 11:51:46 +0200 | [diff] [blame] | 1263 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1264 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1265 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1266 | case HV_X64_MSR_STIMER3_CONFIG: |
| 1267 | case HV_X64_MSR_STIMER0_COUNT: |
| 1268 | case HV_X64_MSR_STIMER1_COUNT: |
| 1269 | case HV_X64_MSR_STIMER2_COUNT: |
| 1270 | case HV_X64_MSR_STIMER3_COUNT: |
| 1271 | return hv_vcpu->cpuid_cache.features_eax & |
| 1272 | HV_MSR_SYNTIMER_AVAILABLE; |
Vitaly Kuznetsov | 978b574 | 2021-05-21 11:51:47 +0200 | [diff] [blame] | 1273 | case HV_X64_MSR_EOI: |
| 1274 | case HV_X64_MSR_ICR: |
| 1275 | case HV_X64_MSR_TPR: |
| 1276 | case HV_X64_MSR_VP_ASSIST_PAGE: |
| 1277 | return hv_vcpu->cpuid_cache.features_eax & |
| 1278 | HV_MSR_APIC_ACCESS_AVAILABLE; |
| 1279 | break; |
Vitaly Kuznetsov | 9442f3b | 2021-05-21 11:51:48 +0200 | [diff] [blame] | 1280 | case HV_X64_MSR_TSC_FREQUENCY: |
| 1281 | case HV_X64_MSR_APIC_FREQUENCY: |
| 1282 | return hv_vcpu->cpuid_cache.features_eax & |
| 1283 | HV_ACCESS_FREQUENCY_MSRS; |
Vitaly Kuznetsov | 234d01b | 2021-05-21 11:51:49 +0200 | [diff] [blame] | 1284 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 1285 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 1286 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 1287 | return hv_vcpu->cpuid_cache.features_eax & |
| 1288 | HV_ACCESS_REENLIGHTENMENT; |
Vitaly Kuznetsov | 0a19c89 | 2021-05-21 11:51:50 +0200 | [diff] [blame] | 1289 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
| 1290 | case HV_X64_MSR_CRASH_CTL: |
| 1291 | return hv_vcpu->cpuid_cache.features_edx & |
| 1292 | HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; |
Vitaly Kuznetsov | 17b6d51 | 2021-05-21 11:51:51 +0200 | [diff] [blame] | 1293 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 1294 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 1295 | return hv_vcpu->cpuid_cache.features_edx & |
| 1296 | HV_FEATURE_DEBUG_MSRS_AVAILABLE; |
Vitaly Kuznetsov | 1561c2c | 2021-05-21 11:51:39 +0200 | [diff] [blame] | 1297 | default: |
| 1298 | break; |
| 1299 | } |
| 1300 | |
Vitaly Kuznetsov | d66bfa3 | 2021-05-21 11:51:52 +0200 | [diff] [blame] | 1301 | return false; |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1304 | static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, |
| 1305 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1306 | { |
| 1307 | struct kvm *kvm = vcpu->kvm; |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1308 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1309 | |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1310 | if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr))) |
| 1311 | return 1; |
| 1312 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1313 | switch (msr) { |
| 1314 | case HV_X64_MSR_GUEST_OS_ID: |
| 1315 | hv->hv_guest_os_id = data; |
| 1316 | /* setting guest os id to zero disables hypercall page */ |
| 1317 | if (!hv->hv_guest_os_id) |
| 1318 | hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; |
| 1319 | break; |
| 1320 | case HV_X64_MSR_HYPERCALL: { |
Joao Martins | 79033be | 2018-06-13 09:55:44 -0400 | [diff] [blame] | 1321 | u8 instructions[9]; |
| 1322 | int i = 0; |
| 1323 | u64 addr; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1324 | |
| 1325 | /* if guest os id is not set hypercall should remain disabled */ |
| 1326 | if (!hv->hv_guest_os_id) |
| 1327 | break; |
| 1328 | if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { |
| 1329 | hv->hv_hypercall = data; |
| 1330 | break; |
| 1331 | } |
Joao Martins | 79033be | 2018-06-13 09:55:44 -0400 | [diff] [blame] | 1332 | |
| 1333 | /* |
| 1334 | * If Xen and Hyper-V hypercalls are both enabled, disambiguate |
| 1335 | * the same way Xen itself does, by setting the bit 31 of EAX |
| 1336 | * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just |
| 1337 | * going to be clobbered on 64-bit. |
| 1338 | */ |
| 1339 | if (kvm_xen_hypercall_enabled(kvm)) { |
| 1340 | /* orl $0x80000000, %eax */ |
| 1341 | instructions[i++] = 0x0d; |
| 1342 | instructions[i++] = 0x00; |
| 1343 | instructions[i++] = 0x00; |
| 1344 | instructions[i++] = 0x00; |
| 1345 | instructions[i++] = 0x80; |
| 1346 | } |
| 1347 | |
| 1348 | /* vmcall/vmmcall */ |
| 1349 | static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); |
| 1350 | i += 3; |
| 1351 | |
| 1352 | /* ret */ |
| 1353 | ((unsigned char *)instructions)[i++] = 0xc3; |
| 1354 | |
| 1355 | addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; |
| 1356 | if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1357 | return 1; |
| 1358 | hv->hv_hypercall = data; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1359 | break; |
| 1360 | } |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1361 | case HV_X64_MSR_REFERENCE_TSC: |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1362 | hv->hv_tsc_page = data; |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1363 | if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) { |
| 1364 | if (!host) |
| 1365 | hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED; |
| 1366 | else |
| 1367 | hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED; |
Paolo Bonzini | 095cf55 | 2016-02-08 12:54:12 +0100 | [diff] [blame] | 1368 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
Vitaly Kuznetsov | cc9cfdd | 2021-03-16 15:37:35 +0100 | [diff] [blame] | 1369 | } else { |
| 1370 | hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET; |
| 1371 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1372 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1373 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1374 | return kvm_hv_msr_set_crash_data(kvm, |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1375 | msr - HV_X64_MSR_CRASH_P0, |
| 1376 | data); |
| 1377 | case HV_X64_MSR_CRASH_CTL: |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1378 | if (host) |
| 1379 | return kvm_hv_msr_set_crash_ctl(kvm, data); |
| 1380 | |
| 1381 | if (data & HV_CRASH_CTL_CRASH_NOTIFY) { |
| 1382 | vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", |
| 1383 | hv->hv_crash_param[0], |
| 1384 | hv->hv_crash_param[1], |
| 1385 | hv->hv_crash_param[2], |
| 1386 | hv->hv_crash_param[3], |
| 1387 | hv->hv_crash_param[4]); |
| 1388 | |
| 1389 | /* Send notification about crash to user space */ |
| 1390 | kvm_make_request(KVM_REQ_HV_CRASH, vcpu); |
| 1391 | } |
| 1392 | break; |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1393 | case HV_X64_MSR_RESET: |
| 1394 | if (data == 1) { |
| 1395 | vcpu_debug(vcpu, "hyper-v reset requested\n"); |
| 1396 | kvm_make_request(KVM_REQ_HV_RESET, vcpu); |
| 1397 | } |
| 1398 | break; |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1399 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 1400 | hv->hv_reenlightenment_control = data; |
| 1401 | break; |
| 1402 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 1403 | hv->hv_tsc_emulation_control = data; |
| 1404 | break; |
| 1405 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
Vitaly Kuznetsov | d2547cf | 2021-03-16 15:37:33 +0100 | [diff] [blame] | 1406 | if (data && !host) |
| 1407 | return 1; |
| 1408 | |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1409 | hv->hv_tsc_emulation_status = data; |
| 1410 | break; |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1411 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1412 | /* read-only, but still ignore it if host-initiated */ |
| 1413 | if (!host) |
| 1414 | return 1; |
| 1415 | break; |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 1416 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 1417 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 1418 | return syndbg_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1419 | default: |
Miaohe Lin | 2f9f5cd | 2019-12-11 14:26:24 +0800 | [diff] [blame] | 1420 | vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1421 | msr, data); |
| 1422 | return 1; |
| 1423 | } |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1427 | /* Calculate cpu time spent by current task in 100ns units */ |
| 1428 | static u64 current_task_runtime_100ns(void) |
| 1429 | { |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 1430 | u64 utime, stime; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1431 | |
| 1432 | task_cputime_adjusted(current, &utime, &stime); |
Frederic Weisbecker | 5613fda | 2017-01-31 04:09:23 +0100 | [diff] [blame] | 1433 | |
| 1434 | return div_u64(utime + stime, 100); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | 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] | 1438 | { |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 1439 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1440 | |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1441 | if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr))) |
| 1442 | return 1; |
| 1443 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1444 | switch (msr) { |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1445 | case HV_X64_MSR_VP_INDEX: { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1446 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1447 | u32 new_vp_index = (u32)data; |
| 1448 | |
| 1449 | if (!host || new_vp_index >= KVM_MAX_VCPUS) |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1450 | return 1; |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1451 | |
| 1452 | if (new_vp_index == hv_vcpu->vp_index) |
| 1453 | return 0; |
| 1454 | |
| 1455 | /* |
| 1456 | * The VP index is initialized to vcpu_index by |
| 1457 | * kvm_hv_vcpu_postcreate so they initially match. Now the |
| 1458 | * VP index is changing, adjust num_mismatched_vp_indexes if |
| 1459 | * it now matches or no longer matches vcpu_idx. |
| 1460 | */ |
Sean Christopherson | 4eeef24 | 2021-09-10 11:32:19 -0700 | [diff] [blame] | 1461 | if (hv_vcpu->vp_index == vcpu->vcpu_idx) |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1462 | atomic_inc(&hv->num_mismatched_vp_indexes); |
Sean Christopherson | 4eeef24 | 2021-09-10 11:32:19 -0700 | [diff] [blame] | 1463 | else if (new_vp_index == vcpu->vcpu_idx) |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1464 | atomic_dec(&hv->num_mismatched_vp_indexes); |
| 1465 | |
| 1466 | hv_vcpu->vp_index = new_vp_index; |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1467 | break; |
Vitaly Kuznetsov | 87ee613 | 2018-09-26 19:02:56 +0200 | [diff] [blame] | 1468 | } |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1469 | case HV_X64_MSR_VP_ASSIST_PAGE: { |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1470 | u64 gfn; |
| 1471 | unsigned long addr; |
| 1472 | |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1473 | if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) { |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1474 | hv_vcpu->hv_vapic = data; |
Vitaly Kuznetsov | 77c3323 | 2021-11-08 16:28:18 +0100 | [diff] [blame] | 1475 | if (kvm_lapic_set_pv_eoi(vcpu, 0, 0)) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1476 | return 1; |
| 1477 | break; |
| 1478 | } |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1479 | gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1480 | addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); |
| 1481 | if (kvm_is_error_hva(addr)) |
| 1482 | return 1; |
Vitaly Kuznetsov | 12e0c61 | 2018-10-16 18:50:05 +0200 | [diff] [blame] | 1483 | |
| 1484 | /* |
Miaohe Lin | 67b0ae4 | 2019-12-11 14:26:22 +0800 | [diff] [blame] | 1485 | * Clear apic_assist portion of struct hv_vp_assist_page |
Vitaly Kuznetsov | 12e0c61 | 2018-10-16 18:50:05 +0200 | [diff] [blame] | 1486 | * only, there can be valuable data in the rest which needs |
| 1487 | * to be preserved e.g. on migration. |
| 1488 | */ |
Al Viro | 9eb41c5 | 2020-02-18 17:32:46 -0500 | [diff] [blame] | 1489 | if (__put_user(0, (u32 __user *)addr)) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1490 | return 1; |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1491 | hv_vcpu->hv_vapic = data; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1492 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
Vitaly Kuznetsov | 77c3323 | 2021-11-08 16:28:18 +0100 | [diff] [blame] | 1493 | if (kvm_lapic_set_pv_eoi(vcpu, |
Ladi Prosek | 72bbf93 | 2018-10-16 18:49:59 +0200 | [diff] [blame] | 1494 | gfn_to_gpa(gfn) | KVM_MSR_ENABLED, |
| 1495 | sizeof(struct hv_vp_assist_page))) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1496 | return 1; |
| 1497 | break; |
| 1498 | } |
| 1499 | case HV_X64_MSR_EOI: |
| 1500 | return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); |
| 1501 | case HV_X64_MSR_ICR: |
| 1502 | return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); |
| 1503 | case HV_X64_MSR_TPR: |
| 1504 | return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1505 | case HV_X64_MSR_VP_RUNTIME: |
| 1506 | if (!host) |
| 1507 | return 1; |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1508 | hv_vcpu->runtime_offset = data - current_task_runtime_100ns(); |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1509 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1510 | case HV_X64_MSR_SCONTROL: |
| 1511 | case HV_X64_MSR_SVERSION: |
| 1512 | case HV_X64_MSR_SIEFP: |
| 1513 | case HV_X64_MSR_SIMP: |
| 1514 | case HV_X64_MSR_EOM: |
| 1515 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 1516 | return synic_set_msr(to_hv_synic(vcpu), msr, data, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1517 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1518 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1519 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1520 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1521 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1522 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 1523 | return stimer_set_config(to_hv_stimer(vcpu, timer_index), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1524 | data, host); |
| 1525 | } |
| 1526 | case HV_X64_MSR_STIMER0_COUNT: |
| 1527 | case HV_X64_MSR_STIMER1_COUNT: |
| 1528 | case HV_X64_MSR_STIMER2_COUNT: |
| 1529 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1530 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1531 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 1532 | return stimer_set_count(to_hv_stimer(vcpu, timer_index), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1533 | data, host); |
| 1534 | } |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1535 | case HV_X64_MSR_TSC_FREQUENCY: |
| 1536 | case HV_X64_MSR_APIC_FREQUENCY: |
| 1537 | /* read-only, but still ignore it if host-initiated */ |
| 1538 | if (!host) |
| 1539 | return 1; |
| 1540 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1541 | default: |
Miaohe Lin | 2f9f5cd | 2019-12-11 14:26:24 +0800 | [diff] [blame] | 1542 | vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1543 | msr, data); |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 1550 | static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, |
| 1551 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1552 | { |
| 1553 | u64 data = 0; |
| 1554 | struct kvm *kvm = vcpu->kvm; |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1555 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1556 | |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1557 | if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr))) |
| 1558 | return 1; |
| 1559 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1560 | switch (msr) { |
| 1561 | case HV_X64_MSR_GUEST_OS_ID: |
| 1562 | data = hv->hv_guest_os_id; |
| 1563 | break; |
| 1564 | case HV_X64_MSR_HYPERCALL: |
| 1565 | data = hv->hv_hypercall; |
| 1566 | break; |
Andrey Smetanin | 93bf417 | 2015-11-30 19:22:19 +0300 | [diff] [blame] | 1567 | case HV_X64_MSR_TIME_REF_COUNT: |
| 1568 | data = get_time_ref_counter(kvm); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1569 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1570 | case HV_X64_MSR_REFERENCE_TSC: |
| 1571 | data = hv->hv_tsc_page; |
| 1572 | break; |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1573 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1574 | return kvm_hv_msr_get_crash_data(kvm, |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1575 | msr - HV_X64_MSR_CRASH_P0, |
| 1576 | pdata); |
| 1577 | case HV_X64_MSR_CRASH_CTL: |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1578 | return kvm_hv_msr_get_crash_ctl(kvm, pdata); |
Andrey Smetanin | e516ceb | 2015-09-16 12:29:48 +0300 | [diff] [blame] | 1579 | case HV_X64_MSR_RESET: |
| 1580 | data = 0; |
| 1581 | break; |
Vitaly Kuznetsov | a2e164e | 2018-03-01 15:15:12 +0100 | [diff] [blame] | 1582 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
| 1583 | data = hv->hv_reenlightenment_control; |
| 1584 | break; |
| 1585 | case HV_X64_MSR_TSC_EMULATION_CONTROL: |
| 1586 | data = hv->hv_tsc_emulation_control; |
| 1587 | break; |
| 1588 | case HV_X64_MSR_TSC_EMULATION_STATUS: |
| 1589 | data = hv->hv_tsc_emulation_status; |
| 1590 | break; |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 1591 | case HV_X64_MSR_SYNDBG_OPTIONS: |
| 1592 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
| 1593 | return syndbg_get_msr(vcpu, msr, pdata, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1594 | default: |
| 1595 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1596 | return 1; |
| 1597 | } |
| 1598 | |
| 1599 | *pdata = data; |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1603 | static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, |
| 1604 | bool host) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1605 | { |
| 1606 | u64 data = 0; |
Vitaly Kuznetsov | 9ff5e03 | 2021-01-26 14:48:11 +0100 | [diff] [blame] | 1607 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1608 | |
Vitaly Kuznetsov | b412800 | 2021-05-21 11:51:38 +0200 | [diff] [blame] | 1609 | if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr))) |
| 1610 | return 1; |
| 1611 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1612 | switch (msr) { |
Roman Kagan | d3457c8 | 2017-07-14 17:13:20 +0300 | [diff] [blame] | 1613 | case HV_X64_MSR_VP_INDEX: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1614 | data = hv_vcpu->vp_index; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1615 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1616 | case HV_X64_MSR_EOI: |
| 1617 | return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); |
| 1618 | case HV_X64_MSR_ICR: |
| 1619 | return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); |
| 1620 | case HV_X64_MSR_TPR: |
| 1621 | return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); |
Ladi Prosek | d4abc57 | 2018-03-20 15:02:07 +0100 | [diff] [blame] | 1622 | case HV_X64_MSR_VP_ASSIST_PAGE: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1623 | data = hv_vcpu->hv_vapic; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1624 | break; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1625 | case HV_X64_MSR_VP_RUNTIME: |
Vitaly Kuznetsov | 1779a39 | 2018-09-26 19:02:55 +0200 | [diff] [blame] | 1626 | data = current_task_runtime_100ns() + hv_vcpu->runtime_offset; |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1627 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 1628 | case HV_X64_MSR_SCONTROL: |
| 1629 | case HV_X64_MSR_SVERSION: |
| 1630 | case HV_X64_MSR_SIEFP: |
| 1631 | case HV_X64_MSR_SIMP: |
| 1632 | case HV_X64_MSR_EOM: |
| 1633 | case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: |
Vitaly Kuznetsov | e0121fa | 2021-01-26 14:48:06 +0100 | [diff] [blame] | 1634 | return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host); |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1635 | case HV_X64_MSR_STIMER0_CONFIG: |
| 1636 | case HV_X64_MSR_STIMER1_CONFIG: |
| 1637 | case HV_X64_MSR_STIMER2_CONFIG: |
| 1638 | case HV_X64_MSR_STIMER3_CONFIG: { |
| 1639 | int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; |
| 1640 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 1641 | return stimer_get_config(to_hv_stimer(vcpu, timer_index), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1642 | pdata); |
| 1643 | } |
| 1644 | case HV_X64_MSR_STIMER0_COUNT: |
| 1645 | case HV_X64_MSR_STIMER1_COUNT: |
| 1646 | case HV_X64_MSR_STIMER2_COUNT: |
| 1647 | case HV_X64_MSR_STIMER3_COUNT: { |
| 1648 | int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; |
| 1649 | |
Vitaly Kuznetsov | aafa97f | 2021-01-26 14:48:07 +0100 | [diff] [blame] | 1650 | return stimer_get_count(to_hv_stimer(vcpu, timer_index), |
Andrey Smetanin | 1f4b34f | 2015-11-30 19:22:21 +0300 | [diff] [blame] | 1651 | pdata); |
| 1652 | } |
Ladi Prosek | 72c139b | 2017-07-26 13:32:59 +0200 | [diff] [blame] | 1653 | case HV_X64_MSR_TSC_FREQUENCY: |
| 1654 | data = (u64)vcpu->arch.virtual_tsc_khz * 1000; |
| 1655 | break; |
| 1656 | case HV_X64_MSR_APIC_FREQUENCY: |
| 1657 | data = APIC_BUS_FREQUENCY; |
| 1658 | break; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1659 | default: |
| 1660 | vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); |
| 1661 | return 1; |
| 1662 | } |
| 1663 | *pdata = data; |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1667 | 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] | 1668 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1669 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
| 1670 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1671 | if (!host && !vcpu->arch.hyperv_enabled) |
| 1672 | return 1; |
| 1673 | |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 1674 | if (!to_hv_vcpu(vcpu)) { |
| 1675 | if (kvm_hv_vcpu_init(vcpu)) |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1679 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1680 | int r; |
| 1681 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1682 | mutex_lock(&hv->hv_lock); |
Andrey Smetanin | e7d9513 | 2015-07-03 15:01:37 +0300 | [diff] [blame] | 1683 | r = kvm_hv_set_msr_pw(vcpu, msr, data, host); |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1684 | mutex_unlock(&hv->hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1685 | return r; |
| 1686 | } else |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 1687 | return kvm_hv_set_msr(vcpu, msr, data, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1688 | } |
| 1689 | |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1690 | 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] | 1691 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1692 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
| 1693 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1694 | if (!host && !vcpu->arch.hyperv_enabled) |
| 1695 | return 1; |
| 1696 | |
Vitaly Kuznetsov | fc08b62 | 2021-01-26 14:48:15 +0100 | [diff] [blame] | 1697 | if (!to_hv_vcpu(vcpu)) { |
| 1698 | if (kvm_hv_vcpu_init(vcpu)) |
| 1699 | return 1; |
| 1700 | } |
| 1701 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1702 | if (kvm_hv_msr_partition_wide(msr)) { |
| 1703 | int r; |
| 1704 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1705 | mutex_lock(&hv->hv_lock); |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 1706 | r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host); |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1707 | mutex_unlock(&hv->hv_lock); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1708 | return r; |
| 1709 | } else |
Paolo Bonzini | 44883f0 | 2018-07-26 13:01:52 +0200 | [diff] [blame] | 1710 | return kvm_hv_get_msr(vcpu, msr, pdata, host); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1711 | } |
| 1712 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1713 | static __always_inline unsigned long *sparse_set_to_vcpu_mask( |
| 1714 | struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask, |
| 1715 | u64 *vp_bitmap, unsigned long *vcpu_bitmap) |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1716 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 1717 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1718 | struct kvm_vcpu *vcpu; |
| 1719 | int i, bank, sbank = 0; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1720 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1721 | memset(vp_bitmap, 0, |
| 1722 | KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap)); |
| 1723 | for_each_set_bit(bank, (unsigned long *)&valid_bank_mask, |
| 1724 | KVM_HV_MAX_SPARSE_VCPU_SET_BITS) |
| 1725 | vp_bitmap[bank] = sparse_banks[sbank++]; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1726 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1727 | if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) { |
| 1728 | /* for all vcpus vp_index == vcpu_idx */ |
| 1729 | return (unsigned long *)vp_bitmap; |
| 1730 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1731 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1732 | bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1733 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Vitaly Kuznetsov | f2bc14b | 2021-01-26 14:48:12 +0100 | [diff] [blame] | 1734 | if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap)) |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1735 | __set_bit(i, vcpu_bitmap); |
| 1736 | } |
| 1737 | return vcpu_bitmap; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1740 | struct kvm_hv_hcall { |
| 1741 | u64 param; |
| 1742 | u64 ingpa; |
| 1743 | u64 outgpa; |
| 1744 | u16 code; |
| 1745 | u16 rep_cnt; |
| 1746 | u16 rep_idx; |
| 1747 | bool fast; |
| 1748 | bool rep; |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1749 | sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS]; |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1750 | }; |
| 1751 | |
| 1752 | static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex) |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1753 | { |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1754 | int i; |
| 1755 | gpa_t gpa; |
Vitaly Kuznetsov | 72167a9 | 2021-01-26 14:48:10 +0100 | [diff] [blame] | 1756 | struct kvm *kvm = vcpu->kvm; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1757 | struct hv_tlb_flush_ex flush_ex; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1758 | struct hv_tlb_flush flush; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1759 | u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; |
| 1760 | DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1761 | unsigned long *vcpu_mask; |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1762 | u64 valid_bank_mask; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1763 | u64 sparse_banks[64]; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1764 | int sparse_banks_len; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1765 | bool all_cpus; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1766 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1767 | if (!ex) { |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1768 | if (hc->fast) { |
| 1769 | flush.address_space = hc->ingpa; |
| 1770 | flush.flags = hc->outgpa; |
| 1771 | flush.processor_mask = sse128_lo(hc->xmm[0]); |
| 1772 | } else { |
| 1773 | if (unlikely(kvm_read_guest(kvm, hc->ingpa, |
| 1774 | &flush, sizeof(flush)))) |
| 1775 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1776 | } |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1777 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1778 | trace_kvm_hv_flush_tlb(flush.processor_mask, |
| 1779 | flush.address_space, flush.flags); |
| 1780 | |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1781 | valid_bank_mask = BIT_ULL(0); |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1782 | sparse_banks[0] = flush.processor_mask; |
Vitaly Kuznetsov | da66761 | 2019-03-20 18:43:20 +0100 | [diff] [blame] | 1783 | |
| 1784 | /* |
| 1785 | * Work around possible WS2012 bug: it sends hypercalls |
| 1786 | * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear, |
| 1787 | * while also expecting us to flush something and crashing if |
| 1788 | * we don't. Let's treat processor_mask == 0 same as |
| 1789 | * HV_FLUSH_ALL_PROCESSORS. |
| 1790 | */ |
| 1791 | all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) || |
| 1792 | flush.processor_mask == 0; |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1793 | } else { |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1794 | if (hc->fast) { |
| 1795 | flush_ex.address_space = hc->ingpa; |
| 1796 | flush_ex.flags = hc->outgpa; |
| 1797 | memcpy(&flush_ex.hv_vp_set, |
| 1798 | &hc->xmm[0], sizeof(hc->xmm[0])); |
| 1799 | } else { |
| 1800 | if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex, |
| 1801 | sizeof(flush_ex)))) |
| 1802 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1803 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1804 | |
| 1805 | trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask, |
| 1806 | flush_ex.hv_vp_set.format, |
| 1807 | flush_ex.address_space, |
| 1808 | flush_ex.flags); |
| 1809 | |
| 1810 | valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask; |
| 1811 | all_cpus = flush_ex.hv_vp_set.format != |
| 1812 | HV_GENERIC_SET_SPARSE_4K; |
| 1813 | |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1814 | sparse_banks_len = bitmap_weight((unsigned long *)&valid_bank_mask, 64); |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1815 | |
| 1816 | if (!sparse_banks_len && !all_cpus) |
| 1817 | goto ret_success; |
| 1818 | |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 1819 | if (!all_cpus) { |
| 1820 | if (hc->fast) { |
| 1821 | if (sparse_banks_len > HV_HYPERCALL_MAX_XMM_REGISTERS - 1) |
| 1822 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1823 | for (i = 0; i < sparse_banks_len; i += 2) { |
| 1824 | sparse_banks[i] = sse128_lo(hc->xmm[i / 2 + 1]); |
| 1825 | sparse_banks[i + 1] = sse128_hi(hc->xmm[i / 2 + 1]); |
| 1826 | } |
| 1827 | } else { |
| 1828 | gpa = hc->ingpa + offsetof(struct hv_tlb_flush_ex, |
| 1829 | hv_vp_set.bank_contents); |
| 1830 | if (unlikely(kvm_read_guest(kvm, gpa, sparse_banks, |
| 1831 | sparse_banks_len * |
| 1832 | sizeof(sparse_banks[0])))) |
| 1833 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1834 | } |
| 1835 | } |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1836 | } |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1837 | |
Vitaly Kuznetsov | 2cefc5f | 2018-09-26 19:02:58 +0200 | [diff] [blame] | 1838 | /* |
| 1839 | * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't |
| 1840 | * analyze it here, flush TLB regardless of the specified address space. |
| 1841 | */ |
Vitaly Kuznetsov | 6470acc | 2021-09-03 09:51:36 +0200 | [diff] [blame] | 1842 | if (all_cpus) { |
| 1843 | kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH_GUEST); |
| 1844 | } else { |
| 1845 | vcpu_mask = sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, |
| 1846 | vp_bitmap, vcpu_bitmap); |
| 1847 | |
| 1848 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST, |
Vitaly Kuznetsov | 620b243 | 2021-09-03 09:51:41 +0200 | [diff] [blame] | 1849 | vcpu_mask); |
Vitaly Kuznetsov | 6470acc | 2021-09-03 09:51:36 +0200 | [diff] [blame] | 1850 | } |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1851 | |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 1852 | ret_success: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1853 | /* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */ |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1854 | return (u64)HV_STATUS_SUCCESS | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1855 | ((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1858 | static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector, |
| 1859 | unsigned long *vcpu_bitmap) |
| 1860 | { |
| 1861 | struct kvm_lapic_irq irq = { |
| 1862 | .delivery_mode = APIC_DM_FIXED, |
| 1863 | .vector = vector |
| 1864 | }; |
| 1865 | struct kvm_vcpu *vcpu; |
| 1866 | int i; |
| 1867 | |
| 1868 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1869 | if (vcpu_bitmap && !test_bit(i, vcpu_bitmap)) |
| 1870 | continue; |
| 1871 | |
| 1872 | /* We fail only when APIC is disabled */ |
| 1873 | kvm_apic_set_irq(vcpu, &irq, NULL); |
| 1874 | } |
| 1875 | } |
| 1876 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1877 | static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex) |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1878 | { |
Vitaly Kuznetsov | 72167a9 | 2021-01-26 14:48:10 +0100 | [diff] [blame] | 1879 | struct kvm *kvm = vcpu->kvm; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1880 | struct hv_send_ipi_ex send_ipi_ex; |
| 1881 | struct hv_send_ipi send_ipi; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1882 | u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; |
| 1883 | DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); |
| 1884 | unsigned long *vcpu_mask; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1885 | unsigned long valid_bank_mask; |
| 1886 | u64 sparse_banks[64]; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1887 | int sparse_banks_len; |
| 1888 | u32 vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1889 | bool all_cpus; |
| 1890 | |
| 1891 | if (!ex) { |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1892 | if (!hc->fast) { |
| 1893 | if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi, |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1894 | sizeof(send_ipi)))) |
| 1895 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1896 | sparse_banks[0] = send_ipi.cpu_mask; |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1897 | vector = send_ipi.vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1898 | } else { |
| 1899 | /* 'reserved' part of hv_send_ipi should be 0 */ |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1900 | if (unlikely(hc->ingpa >> 32 != 0)) |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1901 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1902 | sparse_banks[0] = hc->outgpa; |
| 1903 | vector = (u32)hc->ingpa; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1904 | } |
| 1905 | all_cpus = false; |
| 1906 | valid_bank_mask = BIT_ULL(0); |
| 1907 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1908 | trace_kvm_hv_send_ipi(vector, sparse_banks[0]); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1909 | } else { |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1910 | if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex, |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1911 | sizeof(send_ipi_ex)))) |
| 1912 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1913 | |
| 1914 | trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector, |
| 1915 | send_ipi_ex.vp_set.format, |
| 1916 | send_ipi_ex.vp_set.valid_bank_mask); |
| 1917 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1918 | vector = send_ipi_ex.vector; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1919 | valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask; |
| 1920 | sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) * |
| 1921 | sizeof(sparse_banks[0]); |
| 1922 | |
| 1923 | all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL; |
| 1924 | |
Sean Christopherson | 3244867 | 2021-12-07 22:09:19 +0000 | [diff] [blame] | 1925 | if (all_cpus) |
| 1926 | goto check_and_send_ipi; |
| 1927 | |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1928 | if (!sparse_banks_len) |
| 1929 | goto ret_success; |
| 1930 | |
Sean Christopherson | 3244867 | 2021-12-07 22:09:19 +0000 | [diff] [blame] | 1931 | if (kvm_read_guest(kvm, |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 1932 | hc->ingpa + offsetof(struct hv_send_ipi_ex, |
| 1933 | vp_set.bank_contents), |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1934 | sparse_banks, |
| 1935 | sparse_banks_len)) |
| 1936 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1937 | } |
| 1938 | |
Sean Christopherson | 3244867 | 2021-12-07 22:09:19 +0000 | [diff] [blame] | 1939 | check_and_send_ipi: |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1940 | if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR)) |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1941 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 1942 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1943 | vcpu_mask = all_cpus ? NULL : |
| 1944 | sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, |
| 1945 | vp_bitmap, vcpu_bitmap); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1946 | |
Vitaly Kuznetsov | f21dd49 | 2018-10-10 17:14:38 +0200 | [diff] [blame] | 1947 | kvm_send_ipi_to_many(kvm, vector, vcpu_mask); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 1948 | |
| 1949 | ret_success: |
| 1950 | return HV_STATUS_SUCCESS; |
| 1951 | } |
| 1952 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1953 | void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu) |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 1954 | { |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1955 | struct kvm_cpuid_entry2 *entry; |
Paolo Bonzini | ffbe17c | 2021-08-09 07:00:58 -0400 | [diff] [blame] | 1956 | struct kvm_vcpu_hv *hv_vcpu; |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1957 | |
| 1958 | entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE, 0); |
Vitaly Kuznetsov | 10d7bf1 | 2021-05-21 11:51:37 +0200 | [diff] [blame] | 1959 | if (entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX) { |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1960 | vcpu->arch.hyperv_enabled = true; |
Vitaly Kuznetsov | 10d7bf1 | 2021-05-21 11:51:37 +0200 | [diff] [blame] | 1961 | } else { |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1962 | vcpu->arch.hyperv_enabled = false; |
Vitaly Kuznetsov | 10d7bf1 | 2021-05-21 11:51:37 +0200 | [diff] [blame] | 1963 | return; |
| 1964 | } |
| 1965 | |
| 1966 | if (!to_hv_vcpu(vcpu) && kvm_hv_vcpu_init(vcpu)) |
| 1967 | return; |
| 1968 | |
| 1969 | hv_vcpu = to_hv_vcpu(vcpu); |
| 1970 | |
| 1971 | entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES, 0); |
| 1972 | if (entry) { |
| 1973 | hv_vcpu->cpuid_cache.features_eax = entry->eax; |
| 1974 | hv_vcpu->cpuid_cache.features_ebx = entry->ebx; |
| 1975 | hv_vcpu->cpuid_cache.features_edx = entry->edx; |
| 1976 | } else { |
| 1977 | hv_vcpu->cpuid_cache.features_eax = 0; |
| 1978 | hv_vcpu->cpuid_cache.features_ebx = 0; |
| 1979 | hv_vcpu->cpuid_cache.features_edx = 0; |
| 1980 | } |
| 1981 | |
| 1982 | entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO, 0); |
| 1983 | if (entry) { |
| 1984 | hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax; |
| 1985 | hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx; |
| 1986 | } else { |
| 1987 | hv_vcpu->cpuid_cache.enlightenments_eax = 0; |
| 1988 | hv_vcpu->cpuid_cache.enlightenments_ebx = 0; |
| 1989 | } |
| 1990 | |
| 1991 | entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 0); |
| 1992 | if (entry) |
| 1993 | hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax; |
| 1994 | else |
| 1995 | hv_vcpu->cpuid_cache.syndbg_cap_eax = 0; |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Vitaly Kuznetsov | 644f706 | 2021-05-21 11:51:36 +0200 | [diff] [blame] | 1998 | int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce) |
| 1999 | { |
| 2000 | struct kvm_vcpu_hv *hv_vcpu; |
| 2001 | int ret = 0; |
| 2002 | |
| 2003 | if (!to_hv_vcpu(vcpu)) { |
| 2004 | if (enforce) { |
| 2005 | ret = kvm_hv_vcpu_init(vcpu); |
| 2006 | if (ret) |
| 2007 | return ret; |
| 2008 | } else { |
| 2009 | return 0; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | hv_vcpu = to_hv_vcpu(vcpu); |
| 2014 | hv_vcpu->enforce_cpuid = enforce; |
| 2015 | |
| 2016 | return ret; |
| 2017 | } |
| 2018 | |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 2019 | bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu) |
| 2020 | { |
| 2021 | return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2022 | } |
| 2023 | |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2024 | static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) |
| 2025 | { |
| 2026 | bool longmode; |
| 2027 | |
Tom Lendacky | b5aead0 | 2021-05-24 12:48:57 -0500 | [diff] [blame] | 2028 | longmode = is_64_bit_hypercall(vcpu); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2029 | if (longmode) |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 2030 | kvm_rax_write(vcpu, result); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2031 | else { |
Sean Christopherson | de3cd11 | 2019-04-30 10:36:17 -0700 | [diff] [blame] | 2032 | kvm_rdx_write(vcpu, result >> 32); |
| 2033 | kvm_rax_write(vcpu, result & 0xffffffff); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 2037 | static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) |
| 2038 | { |
Vitaly Kuznetsov | f5714bb | 2021-07-30 14:26:23 +0200 | [diff] [blame] | 2039 | trace_kvm_hv_hypercall_done(result); |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 2040 | kvm_hv_hypercall_set_result(vcpu, result); |
| 2041 | ++vcpu->stat.hypercalls; |
| 2042 | return kvm_skip_emulated_instruction(vcpu); |
| 2043 | } |
| 2044 | |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2045 | static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) |
| 2046 | { |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 2047 | return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result); |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2048 | } |
| 2049 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2050 | static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2051 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2052 | struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2053 | struct eventfd_ctx *eventfd; |
| 2054 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2055 | if (unlikely(!hc->fast)) { |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2056 | int ret; |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2057 | gpa_t gpa = hc->ingpa; |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2058 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2059 | if ((gpa & (__alignof__(hc->ingpa) - 1)) || |
| 2060 | offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2061 | return HV_STATUS_INVALID_ALIGNMENT; |
| 2062 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2063 | ret = kvm_vcpu_read_guest(vcpu, gpa, |
| 2064 | &hc->ingpa, sizeof(hc->ingpa)); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2065 | if (ret < 0) |
| 2066 | return HV_STATUS_INVALID_ALIGNMENT; |
| 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * Per spec, bits 32-47 contain the extra "flag number". However, we |
| 2071 | * have no use for it, and in all known usecases it is zero, so just |
| 2072 | * report lookup failure if it isn't. |
| 2073 | */ |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2074 | if (hc->ingpa & 0xffff00000000ULL) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2075 | return HV_STATUS_INVALID_PORT_ID; |
| 2076 | /* remaining bits are reserved-zero */ |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2077 | if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2078 | return HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2079 | |
Paolo Bonzini | 452a68d | 2018-05-07 19:24:34 +0200 | [diff] [blame] | 2080 | /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */ |
| 2081 | rcu_read_lock(); |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2082 | eventfd = idr_find(&hv->conn_to_evt, hc->ingpa); |
Paolo Bonzini | 452a68d | 2018-05-07 19:24:34 +0200 | [diff] [blame] | 2083 | rcu_read_unlock(); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2084 | if (!eventfd) |
| 2085 | return HV_STATUS_INVALID_PORT_ID; |
| 2086 | |
| 2087 | eventfd_signal(eventfd, 1); |
| 2088 | return HV_STATUS_SUCCESS; |
| 2089 | } |
| 2090 | |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 2091 | static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) |
| 2092 | { |
| 2093 | switch (hc->code) { |
| 2094 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: |
| 2095 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: |
| 2096 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: |
| 2097 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: |
| 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | return false; |
| 2102 | } |
| 2103 | |
| 2104 | static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc) |
| 2105 | { |
| 2106 | int reg; |
| 2107 | |
| 2108 | kvm_fpu_get(); |
| 2109 | for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++) |
| 2110 | _kvm_read_sse_reg(reg, &hc->xmm[reg]); |
| 2111 | kvm_fpu_put(); |
| 2112 | } |
| 2113 | |
Vitaly Kuznetsov | 4ad81a9 | 2021-05-21 11:51:54 +0200 | [diff] [blame] | 2114 | static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code) |
| 2115 | { |
Vitaly Kuznetsov | 34ef7d7 | 2021-05-21 11:51:55 +0200 | [diff] [blame] | 2116 | if (!hv_vcpu->enforce_cpuid) |
| 2117 | return true; |
| 2118 | |
| 2119 | switch (code) { |
| 2120 | case HVCALL_NOTIFY_LONG_SPIN_WAIT: |
| 2121 | return hv_vcpu->cpuid_cache.enlightenments_ebx && |
| 2122 | hv_vcpu->cpuid_cache.enlightenments_ebx != U32_MAX; |
Vitaly Kuznetsov | 4f532b7 | 2021-05-21 11:51:56 +0200 | [diff] [blame] | 2123 | case HVCALL_POST_MESSAGE: |
| 2124 | return hv_vcpu->cpuid_cache.features_ebx & HV_POST_MESSAGES; |
Vitaly Kuznetsov | a60b3c5 | 2021-05-21 11:51:57 +0200 | [diff] [blame] | 2125 | case HVCALL_SIGNAL_EVENT: |
| 2126 | return hv_vcpu->cpuid_cache.features_ebx & HV_SIGNAL_EVENTS; |
Vitaly Kuznetsov | a921cf8 | 2021-05-21 11:51:58 +0200 | [diff] [blame] | 2127 | case HVCALL_POST_DEBUG_DATA: |
| 2128 | case HVCALL_RETRIEVE_DEBUG_DATA: |
| 2129 | case HVCALL_RESET_DEBUG_SESSION: |
| 2130 | /* |
| 2131 | * Return 'true' when SynDBG is disabled so the resulting code |
| 2132 | * will be HV_STATUS_INVALID_HYPERCALL_CODE. |
| 2133 | */ |
| 2134 | return !kvm_hv_is_syndbg_enabled(hv_vcpu->vcpu) || |
| 2135 | hv_vcpu->cpuid_cache.features_ebx & HV_DEBUGGING; |
Vitaly Kuznetsov | bb53ecb | 2021-05-21 11:51:59 +0200 | [diff] [blame] | 2136 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: |
| 2137 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: |
Vitaly Kuznetsov | 445caed | 2021-05-21 11:52:01 +0200 | [diff] [blame] | 2138 | if (!(hv_vcpu->cpuid_cache.enlightenments_eax & |
| 2139 | HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED)) |
| 2140 | return false; |
| 2141 | fallthrough; |
Vitaly Kuznetsov | bb53ecb | 2021-05-21 11:51:59 +0200 | [diff] [blame] | 2142 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: |
| 2143 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: |
| 2144 | return hv_vcpu->cpuid_cache.enlightenments_eax & |
| 2145 | HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; |
Vitaly Kuznetsov | d264eb3 | 2021-05-21 11:52:00 +0200 | [diff] [blame] | 2146 | case HVCALL_SEND_IPI_EX: |
Vitaly Kuznetsov | 445caed | 2021-05-21 11:52:01 +0200 | [diff] [blame] | 2147 | if (!(hv_vcpu->cpuid_cache.enlightenments_eax & |
| 2148 | HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED)) |
| 2149 | return false; |
| 2150 | fallthrough; |
Vitaly Kuznetsov | d264eb3 | 2021-05-21 11:52:00 +0200 | [diff] [blame] | 2151 | case HVCALL_SEND_IPI: |
| 2152 | return hv_vcpu->cpuid_cache.enlightenments_eax & |
| 2153 | HV_X64_CLUSTER_IPI_RECOMMENDED; |
Vitaly Kuznetsov | 34ef7d7 | 2021-05-21 11:51:55 +0200 | [diff] [blame] | 2154 | default: |
| 2155 | break; |
| 2156 | } |
| 2157 | |
Vitaly Kuznetsov | 4ad81a9 | 2021-05-21 11:51:54 +0200 | [diff] [blame] | 2158 | return true; |
| 2159 | } |
| 2160 | |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2161 | int kvm_hv_hypercall(struct kvm_vcpu *vcpu) |
| 2162 | { |
Vitaly Kuznetsov | 4e62aa9 | 2021-07-30 14:26:24 +0200 | [diff] [blame] | 2163 | struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2164 | struct kvm_hv_hcall hc; |
| 2165 | u64 ret = HV_STATUS_SUCCESS; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2166 | |
| 2167 | /* |
| 2168 | * hypercall generates UD from non zero cpl and real mode |
| 2169 | * per HYPER-V spec |
| 2170 | */ |
Jason Baron | b3646477 | 2021-01-14 22:27:56 -0500 | [diff] [blame] | 2171 | if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) { |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2172 | kvm_queue_exception(vcpu, UD_VECTOR); |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 2173 | return 1; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2174 | } |
| 2175 | |
Arnd Bergmann | f4e4805e | 2019-07-12 16:13:09 +0200 | [diff] [blame] | 2176 | #ifdef CONFIG_X86_64 |
Tom Lendacky | b5aead0 | 2021-05-24 12:48:57 -0500 | [diff] [blame] | 2177 | if (is_64_bit_hypercall(vcpu)) { |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2178 | hc.param = kvm_rcx_read(vcpu); |
| 2179 | hc.ingpa = kvm_rdx_read(vcpu); |
| 2180 | hc.outgpa = kvm_r8_read(vcpu); |
Arnd Bergmann | f4e4805e | 2019-07-12 16:13:09 +0200 | [diff] [blame] | 2181 | } else |
| 2182 | #endif |
| 2183 | { |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2184 | hc.param = ((u64)kvm_rdx_read(vcpu) << 32) | |
| 2185 | (kvm_rax_read(vcpu) & 0xffffffff); |
| 2186 | hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) | |
| 2187 | (kvm_rcx_read(vcpu) & 0xffffffff); |
| 2188 | hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) | |
| 2189 | (kvm_rsi_read(vcpu) & 0xffffffff); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2190 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2191 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2192 | hc.code = hc.param & 0xffff; |
| 2193 | hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT); |
| 2194 | hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff; |
| 2195 | hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff; |
| 2196 | hc.rep = !!(hc.rep_cnt || hc.rep_idx); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2197 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2198 | trace_kvm_hv_hypercall(hc.code, hc.fast, hc.rep_cnt, hc.rep_idx, |
| 2199 | hc.ingpa, hc.outgpa); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2200 | |
Vitaly Kuznetsov | 4e62aa9 | 2021-07-30 14:26:24 +0200 | [diff] [blame] | 2201 | if (unlikely(!hv_check_hypercall_access(hv_vcpu, hc.code))) { |
Vitaly Kuznetsov | 4ad81a9 | 2021-05-21 11:51:54 +0200 | [diff] [blame] | 2202 | ret = HV_STATUS_ACCESS_DENIED; |
| 2203 | goto hypercall_complete; |
| 2204 | } |
| 2205 | |
Vitaly Kuznetsov | 4e62aa9 | 2021-07-30 14:26:24 +0200 | [diff] [blame] | 2206 | if (hc.fast && is_xmm_fast_hypercall(&hc)) { |
| 2207 | if (unlikely(hv_vcpu->enforce_cpuid && |
| 2208 | !(hv_vcpu->cpuid_cache.features_edx & |
| 2209 | HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE))) { |
| 2210 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
Vitaly Kuznetsov | 2e2f1e8d | 2021-07-30 14:26:22 +0200 | [diff] [blame] | 2214 | kvm_hv_hypercall_read_xmm(&hc); |
Vitaly Kuznetsov | 4e62aa9 | 2021-07-30 14:26:24 +0200 | [diff] [blame] | 2215 | } |
Vitaly Kuznetsov | 2e2f1e8d | 2021-07-30 14:26:22 +0200 | [diff] [blame] | 2216 | |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2217 | switch (hc.code) { |
Andrey Smetanin | 8ed6d76 | 2016-02-11 16:44:57 +0300 | [diff] [blame] | 2218 | case HVCALL_NOTIFY_LONG_SPIN_WAIT: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2219 | if (unlikely(hc.rep)) { |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 2220 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2221 | break; |
| 2222 | } |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 2223 | kvm_vcpu_on_spin(vcpu, true); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2224 | break; |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2225 | case HVCALL_SIGNAL_EVENT: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2226 | if (unlikely(hc.rep)) { |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 2227 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2228 | break; |
| 2229 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2230 | ret = kvm_hvcall_signal_event(vcpu, &hc); |
Dan Carpenter | d32ef54 | 2018-03-17 14:48:27 +0300 | [diff] [blame] | 2231 | if (ret != HV_STATUS_INVALID_PORT_ID) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2232 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2233 | fallthrough; /* maybe userspace knows this conn_id */ |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2234 | case HVCALL_POST_MESSAGE: |
Paolo Bonzini | a2b5c3c | 2016-03-29 11:23:25 +0200 | [diff] [blame] | 2235 | /* don't bother userspace if it has no way to handle it */ |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2236 | if (unlikely(hc.rep || !to_hv_synic(vcpu)->active)) { |
Vitaly Kuznetsov | 56b9ae7 | 2018-05-16 17:21:27 +0200 | [diff] [blame] | 2237 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
Paolo Bonzini | a2b5c3c | 2016-03-29 11:23:25 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | } |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2240 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 2241 | vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2242 | vcpu->run->hyperv.u.hcall.input = hc.param; |
| 2243 | vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; |
| 2244 | vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; |
Andrey Smetanin | 83326e4 | 2016-02-11 16:45:01 +0300 | [diff] [blame] | 2245 | vcpu->arch.complete_userspace_io = |
| 2246 | kvm_hv_hypercall_complete_userspace; |
| 2247 | return 0; |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 2248 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 2249 | if (unlikely(!hc.rep_cnt || hc.rep_idx)) { |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 2250 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2251 | break; |
| 2252 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2253 | ret = kvm_hv_flush_tlb(vcpu, &hc, false); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 2254 | break; |
| 2255 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 2256 | if (unlikely(hc.rep)) { |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 2257 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2258 | break; |
| 2259 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2260 | ret = kvm_hv_flush_tlb(vcpu, &hc, false); |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 2261 | break; |
| 2262 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 2263 | if (unlikely(!hc.rep_cnt || hc.rep_idx)) { |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 2264 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2265 | break; |
| 2266 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2267 | ret = kvm_hv_flush_tlb(vcpu, &hc, true); |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 2268 | break; |
| 2269 | case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: |
Siddharth Chandrasekaran | 5974565 | 2021-05-26 10:56:10 +0200 | [diff] [blame] | 2270 | if (unlikely(hc.rep)) { |
Vitaly Kuznetsov | c701267 | 2018-05-16 17:21:30 +0200 | [diff] [blame] | 2271 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2272 | break; |
| 2273 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2274 | ret = kvm_hv_flush_tlb(vcpu, &hc, true); |
Vitaly Kuznetsov | e2f11f4 | 2018-05-16 17:21:29 +0200 | [diff] [blame] | 2275 | break; |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 2276 | case HVCALL_SEND_IPI: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2277 | if (unlikely(hc.rep)) { |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 2278 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2279 | break; |
| 2280 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2281 | ret = kvm_hv_send_ipi(vcpu, &hc, false); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 2282 | break; |
| 2283 | case HVCALL_SEND_IPI_EX: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2284 | if (unlikely(hc.fast || hc.rep)) { |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 2285 | ret = HV_STATUS_INVALID_HYPERCALL_INPUT; |
| 2286 | break; |
| 2287 | } |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2288 | ret = kvm_hv_send_ipi(vcpu, &hc, true); |
Vitaly Kuznetsov | 214ff83 | 2018-09-26 19:02:59 +0200 | [diff] [blame] | 2289 | break; |
Jon Doron | b187038 | 2020-05-29 16:45:42 +0300 | [diff] [blame] | 2290 | case HVCALL_POST_DEBUG_DATA: |
| 2291 | case HVCALL_RETRIEVE_DEBUG_DATA: |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2292 | if (unlikely(hc.fast)) { |
Jon Doron | b187038 | 2020-05-29 16:45:42 +0300 | [diff] [blame] | 2293 | ret = HV_STATUS_INVALID_PARAMETER; |
| 2294 | break; |
| 2295 | } |
| 2296 | fallthrough; |
| 2297 | case HVCALL_RESET_DEBUG_SESSION: { |
Vitaly Kuznetsov | f69b55e | 2021-01-26 14:48:08 +0100 | [diff] [blame] | 2298 | struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); |
Jon Doron | b187038 | 2020-05-29 16:45:42 +0300 | [diff] [blame] | 2299 | |
| 2300 | if (!kvm_hv_is_syndbg_enabled(vcpu)) { |
| 2301 | ret = HV_STATUS_INVALID_HYPERCALL_CODE; |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) { |
| 2306 | ret = HV_STATUS_OPERATION_DENIED; |
| 2307 | break; |
| 2308 | } |
| 2309 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; |
| 2310 | vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; |
Siddharth Chandrasekaran | bd38b32 | 2021-05-26 10:56:09 +0200 | [diff] [blame] | 2311 | vcpu->run->hyperv.u.hcall.input = hc.param; |
| 2312 | vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; |
| 2313 | vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; |
Jon Doron | b187038 | 2020-05-29 16:45:42 +0300 | [diff] [blame] | 2314 | vcpu->arch.complete_userspace_io = |
| 2315 | kvm_hv_hypercall_complete_userspace; |
| 2316 | return 0; |
| 2317 | } |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2318 | default: |
Dan Carpenter | d32ef54 | 2018-03-17 14:48:27 +0300 | [diff] [blame] | 2319 | ret = HV_STATUS_INVALID_HYPERCALL_CODE; |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2320 | break; |
| 2321 | } |
| 2322 | |
Vitaly Kuznetsov | 4ad81a9 | 2021-05-21 11:51:54 +0200 | [diff] [blame] | 2323 | hypercall_complete: |
Radim Krčmář | 696ca77 | 2018-05-24 17:50:56 +0200 | [diff] [blame] | 2324 | return kvm_hv_hypercall_complete(vcpu, ret); |
Andrey Smetanin | e83d588 | 2015-07-03 15:01:34 +0300 | [diff] [blame] | 2325 | } |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 2326 | |
| 2327 | void kvm_hv_init_vm(struct kvm *kvm) |
| 2328 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2329 | struct kvm_hv *hv = to_kvm_hv(kvm); |
| 2330 | |
| 2331 | mutex_init(&hv->hv_lock); |
| 2332 | idr_init(&hv->conn_to_evt); |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | void kvm_hv_destroy_vm(struct kvm *kvm) |
| 2336 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2337 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2338 | struct eventfd_ctx *eventfd; |
| 2339 | int i; |
| 2340 | |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2341 | idr_for_each_entry(&hv->conn_to_evt, eventfd, i) |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2342 | eventfd_ctx_put(eventfd); |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2343 | idr_destroy(&hv->conn_to_evt); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd) |
| 2347 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2348 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2349 | struct eventfd_ctx *eventfd; |
| 2350 | int ret; |
| 2351 | |
| 2352 | eventfd = eventfd_ctx_fdget(fd); |
| 2353 | if (IS_ERR(eventfd)) |
| 2354 | return PTR_ERR(eventfd); |
| 2355 | |
| 2356 | mutex_lock(&hv->hv_lock); |
| 2357 | 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] | 2358 | GFP_KERNEL_ACCOUNT); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2359 | mutex_unlock(&hv->hv_lock); |
| 2360 | |
| 2361 | if (ret >= 0) |
| 2362 | return 0; |
| 2363 | |
| 2364 | if (ret == -ENOSPC) |
| 2365 | ret = -EEXIST; |
| 2366 | eventfd_ctx_put(eventfd); |
| 2367 | return ret; |
| 2368 | } |
| 2369 | |
| 2370 | static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id) |
| 2371 | { |
Vitaly Kuznetsov | 05f04ae | 2021-01-26 14:48:09 +0100 | [diff] [blame] | 2372 | struct kvm_hv *hv = to_kvm_hv(kvm); |
Roman Kagan | faeb783 | 2018-02-01 16:48:32 +0300 | [diff] [blame] | 2373 | struct eventfd_ctx *eventfd; |
| 2374 | |
| 2375 | mutex_lock(&hv->hv_lock); |
| 2376 | eventfd = idr_remove(&hv->conn_to_evt, conn_id); |
| 2377 | mutex_unlock(&hv->hv_lock); |
| 2378 | |
| 2379 | if (!eventfd) |
| 2380 | return -ENOENT; |
| 2381 | |
| 2382 | synchronize_srcu(&kvm->srcu); |
| 2383 | eventfd_ctx_put(eventfd); |
| 2384 | return 0; |
| 2385 | } |
| 2386 | |
| 2387 | int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args) |
| 2388 | { |
| 2389 | if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) || |
| 2390 | (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK)) |
| 2391 | return -EINVAL; |
| 2392 | |
| 2393 | if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN) |
| 2394 | return kvm_hv_eventfd_deassign(kvm, args->conn_id); |
| 2395 | return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd); |
Roman Kagan | cbc0236 | 2018-02-01 16:48:31 +0300 | [diff] [blame] | 2396 | } |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2397 | |
Vitaly Kuznetsov | c21d54f | 2020-09-29 17:09:43 +0200 | [diff] [blame] | 2398 | int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, |
| 2399 | struct kvm_cpuid_entry2 __user *entries) |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2400 | { |
Vitaly Kuznetsov | ea15298 | 2019-08-27 18:04:02 +0200 | [diff] [blame] | 2401 | uint16_t evmcs_ver = 0; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2402 | struct kvm_cpuid_entry2 cpuid_entries[] = { |
| 2403 | { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS }, |
| 2404 | { .function = HYPERV_CPUID_INTERFACE }, |
| 2405 | { .function = HYPERV_CPUID_VERSION }, |
| 2406 | { .function = HYPERV_CPUID_FEATURES }, |
| 2407 | { .function = HYPERV_CPUID_ENLIGHTMENT_INFO }, |
| 2408 | { .function = HYPERV_CPUID_IMPLEMENT_LIMITS }, |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 2409 | { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS }, |
| 2410 | { .function = HYPERV_CPUID_SYNDBG_INTERFACE }, |
| 2411 | { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES }, |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2412 | { .function = HYPERV_CPUID_NESTED_FEATURES }, |
| 2413 | }; |
| 2414 | int i, nent = ARRAY_SIZE(cpuid_entries); |
| 2415 | |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 2416 | if (kvm_x86_ops.nested_ops->get_evmcs_version) |
| 2417 | evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu); |
Vitaly Kuznetsov | ea15298 | 2019-08-27 18:04:02 +0200 | [diff] [blame] | 2418 | |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2419 | /* Skip NESTED_FEATURES if eVMCS is not supported */ |
| 2420 | if (!evmcs_ver) |
| 2421 | --nent; |
| 2422 | |
| 2423 | if (cpuid->nent < nent) |
| 2424 | return -E2BIG; |
| 2425 | |
| 2426 | if (cpuid->nent > nent) |
| 2427 | cpuid->nent = nent; |
| 2428 | |
| 2429 | for (i = 0; i < nent; i++) { |
| 2430 | struct kvm_cpuid_entry2 *ent = &cpuid_entries[i]; |
| 2431 | u32 signature[3]; |
| 2432 | |
| 2433 | switch (ent->function) { |
| 2434 | case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS: |
| 2435 | memcpy(signature, "Linux KVM Hv", 12); |
| 2436 | |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 2437 | ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2438 | ent->ebx = signature[0]; |
| 2439 | ent->ecx = signature[1]; |
| 2440 | ent->edx = signature[2]; |
| 2441 | break; |
| 2442 | |
| 2443 | case HYPERV_CPUID_INTERFACE: |
Vitaly Kuznetsov | 8f01455 | 2021-01-26 14:48:14 +0100 | [diff] [blame] | 2444 | ent->eax = HYPERV_CPUID_SIGNATURE_EAX; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2445 | break; |
| 2446 | |
| 2447 | case HYPERV_CPUID_VERSION: |
| 2448 | /* |
| 2449 | * We implement some Hyper-V 2016 functions so let's use |
| 2450 | * this version. |
| 2451 | */ |
| 2452 | ent->eax = 0x00003839; |
| 2453 | ent->ebx = 0x000A0000; |
| 2454 | break; |
| 2455 | |
| 2456 | case HYPERV_CPUID_FEATURES: |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 2457 | ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2458 | ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE; |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 2459 | ent->eax |= HV_MSR_SYNIC_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2460 | ent->eax |= HV_MSR_SYNTIMER_AVAILABLE; |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 2461 | ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE; |
| 2462 | ent->eax |= HV_MSR_HYPERCALL_AVAILABLE; |
| 2463 | ent->eax |= HV_MSR_VP_INDEX_AVAILABLE; |
| 2464 | ent->eax |= HV_MSR_RESET_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2465 | ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE; |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 2466 | ent->eax |= HV_ACCESS_FREQUENCY_MSRS; |
| 2467 | ent->eax |= HV_ACCESS_REENLIGHTENMENT; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2468 | |
Joseph Salisbury | dfc53ba | 2020-09-26 07:26:26 -0700 | [diff] [blame] | 2469 | ent->ebx |= HV_POST_MESSAGES; |
| 2470 | ent->ebx |= HV_SIGNAL_EVENTS; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2471 | |
Siddharth Chandrasekaran | d8f5537 | 2021-05-26 11:03:56 +0200 | [diff] [blame] | 2472 | ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2473 | ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; |
| 2474 | ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 2475 | |
Linus Torvalds | 039aeb9 | 2020-06-03 15:13:47 -0700 | [diff] [blame] | 2476 | ent->ebx |= HV_DEBUGGING; |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 2477 | ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE; |
| 2478 | ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE; |
| 2479 | |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 2480 | /* |
| 2481 | * Direct Synthetic timers only make sense with in-kernel |
| 2482 | * LAPIC |
| 2483 | */ |
Vitaly Kuznetsov | c21d54f | 2020-09-29 17:09:43 +0200 | [diff] [blame] | 2484 | if (!vcpu || lapic_in_kernel(vcpu)) |
Wanpeng Li | a073d7e | 2019-09-16 15:42:32 +0800 | [diff] [blame] | 2485 | ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2486 | |
| 2487 | break; |
| 2488 | |
| 2489 | case HYPERV_CPUID_ENLIGHTMENT_INFO: |
| 2490 | ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; |
| 2491 | ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2492 | ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; |
| 2493 | ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; |
| 2494 | ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; |
Vitaly Kuznetsov | f1adcea | 2019-01-25 12:19:34 +0100 | [diff] [blame] | 2495 | if (evmcs_ver) |
| 2496 | ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; |
Vitaly Kuznetsov | b2d8b16 | 2019-09-16 18:22:57 +0200 | [diff] [blame] | 2497 | if (!cpu_smt_possible()) |
| 2498 | ent->eax |= HV_X64_NO_NONARCH_CORESHARING; |
Vitaly Kuznetsov | 0f250a6 | 2021-08-10 23:52:46 +0300 | [diff] [blame] | 2499 | |
| 2500 | ent->eax |= HV_DEPRECATING_AEOI_RECOMMENDED; |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2501 | /* |
| 2502 | * Default number of spinlock retry attempts, matches |
| 2503 | * HyperV 2016. |
| 2504 | */ |
| 2505 | ent->ebx = 0x00000FFF; |
| 2506 | |
| 2507 | break; |
| 2508 | |
| 2509 | case HYPERV_CPUID_IMPLEMENT_LIMITS: |
| 2510 | /* Maximum number of virtual processors */ |
| 2511 | ent->eax = KVM_MAX_VCPUS; |
| 2512 | /* |
| 2513 | * Maximum number of logical processors, matches |
| 2514 | * HyperV 2016. |
| 2515 | */ |
| 2516 | ent->ebx = 64; |
| 2517 | |
| 2518 | break; |
| 2519 | |
| 2520 | case HYPERV_CPUID_NESTED_FEATURES: |
| 2521 | ent->eax = evmcs_ver; |
| 2522 | |
| 2523 | break; |
| 2524 | |
Jon Doron | f97f5a5 | 2020-05-29 16:45:40 +0300 | [diff] [blame] | 2525 | case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS: |
| 2526 | memcpy(signature, "Linux KVM Hv", 12); |
| 2527 | |
| 2528 | ent->eax = 0; |
| 2529 | ent->ebx = signature[0]; |
| 2530 | ent->ecx = signature[1]; |
| 2531 | ent->edx = signature[2]; |
| 2532 | break; |
| 2533 | |
| 2534 | case HYPERV_CPUID_SYNDBG_INTERFACE: |
| 2535 | memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12); |
| 2536 | ent->eax = signature[0]; |
| 2537 | break; |
| 2538 | |
| 2539 | case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES: |
| 2540 | ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; |
| 2541 | break; |
| 2542 | |
Vitaly Kuznetsov | 2bc3997 | 2018-12-10 18:21:56 +0100 | [diff] [blame] | 2543 | default: |
| 2544 | break; |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | if (copy_to_user(entries, cpuid_entries, |
| 2549 | nent * sizeof(struct kvm_cpuid_entry2))) |
| 2550 | return -EFAULT; |
| 2551 | |
| 2552 | return 0; |
| 2553 | } |