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