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