1e83d5887SAndrey Smetanin /* 2e83d5887SAndrey Smetanin * KVM Microsoft Hyper-V emulation 3e83d5887SAndrey Smetanin * 4e83d5887SAndrey Smetanin * derived from arch/x86/kvm/x86.c 5e83d5887SAndrey Smetanin * 6e83d5887SAndrey Smetanin * Copyright (C) 2006 Qumranet, Inc. 7e83d5887SAndrey Smetanin * Copyright (C) 2008 Qumranet, Inc. 8e83d5887SAndrey Smetanin * Copyright IBM Corporation, 2008 9e83d5887SAndrey Smetanin * Copyright 2010 Red Hat, Inc. and/or its affiliates. 10e83d5887SAndrey Smetanin * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> 11e83d5887SAndrey Smetanin * 12e83d5887SAndrey Smetanin * Authors: 13e83d5887SAndrey Smetanin * Avi Kivity <avi@qumranet.com> 14e83d5887SAndrey Smetanin * Yaniv Kamay <yaniv@qumranet.com> 15e83d5887SAndrey Smetanin * Amit Shah <amit.shah@qumranet.com> 16e83d5887SAndrey Smetanin * Ben-Ami Yassour <benami@il.ibm.com> 17e83d5887SAndrey Smetanin * Andrey Smetanin <asmetanin@virtuozzo.com> 18e83d5887SAndrey Smetanin * 19e83d5887SAndrey Smetanin * This work is licensed under the terms of the GNU GPL, version 2. See 20e83d5887SAndrey Smetanin * the COPYING file in the top-level directory. 21e83d5887SAndrey Smetanin * 22e83d5887SAndrey Smetanin */ 23e83d5887SAndrey Smetanin 24e83d5887SAndrey Smetanin #include "x86.h" 25e83d5887SAndrey Smetanin #include "lapic.h" 265c919412SAndrey Smetanin #include "ioapic.h" 27e83d5887SAndrey Smetanin #include "hyperv.h" 28e83d5887SAndrey Smetanin 29e83d5887SAndrey Smetanin #include <linux/kvm_host.h> 30765eaa0fSAndrey Smetanin #include <linux/highmem.h> 3132ef5517SIngo Molnar #include <linux/sched/cputime.h> 32*faeb7833SRoman Kagan #include <linux/eventfd.h> 3332ef5517SIngo Molnar 345c919412SAndrey Smetanin #include <asm/apicdef.h> 35e83d5887SAndrey Smetanin #include <trace/events/kvm.h> 36e83d5887SAndrey Smetanin 37e83d5887SAndrey Smetanin #include "trace.h" 38e83d5887SAndrey Smetanin 395c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) 405c919412SAndrey Smetanin { 415c919412SAndrey Smetanin return atomic64_read(&synic->sint[sint]); 425c919412SAndrey Smetanin } 435c919412SAndrey Smetanin 445c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value) 455c919412SAndrey Smetanin { 465c919412SAndrey Smetanin if (sint_value & HV_SYNIC_SINT_MASKED) 475c919412SAndrey Smetanin return -1; 485c919412SAndrey Smetanin return sint_value & HV_SYNIC_SINT_VECTOR_MASK; 495c919412SAndrey Smetanin } 505c919412SAndrey Smetanin 515c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, 525c919412SAndrey Smetanin int vector) 535c919412SAndrey Smetanin { 545c919412SAndrey Smetanin int i; 555c919412SAndrey Smetanin 565c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 575c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 585c919412SAndrey Smetanin return true; 595c919412SAndrey Smetanin } 605c919412SAndrey Smetanin return false; 615c919412SAndrey Smetanin } 625c919412SAndrey Smetanin 635c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, 645c919412SAndrey Smetanin int vector) 655c919412SAndrey Smetanin { 665c919412SAndrey Smetanin int i; 675c919412SAndrey Smetanin u64 sint_value; 685c919412SAndrey Smetanin 695c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 705c919412SAndrey Smetanin sint_value = synic_read_sint(synic, i); 715c919412SAndrey Smetanin if (synic_get_sint_vector(sint_value) == vector && 725c919412SAndrey Smetanin sint_value & HV_SYNIC_SINT_AUTO_EOI) 735c919412SAndrey Smetanin return true; 745c919412SAndrey Smetanin } 755c919412SAndrey Smetanin return false; 765c919412SAndrey Smetanin } 775c919412SAndrey Smetanin 787be58a64SAndrey Smetanin static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, 797be58a64SAndrey Smetanin u64 data, bool host) 805c919412SAndrey Smetanin { 815c919412SAndrey Smetanin int vector; 825c919412SAndrey Smetanin 835c919412SAndrey Smetanin vector = data & HV_SYNIC_SINT_VECTOR_MASK; 847be58a64SAndrey Smetanin if (vector < 16 && !host) 855c919412SAndrey Smetanin return 1; 865c919412SAndrey Smetanin /* 875c919412SAndrey Smetanin * Guest may configure multiple SINTs to use the same vector, so 885c919412SAndrey Smetanin * we maintain a bitmap of vectors handled by synic, and a 895c919412SAndrey Smetanin * bitmap of vectors with auto-eoi behavior. The bitmaps are 905c919412SAndrey Smetanin * updated here, and atomically queried on fast paths. 915c919412SAndrey Smetanin */ 925c919412SAndrey Smetanin 935c919412SAndrey Smetanin atomic64_set(&synic->sint[sint], data); 945c919412SAndrey Smetanin 955c919412SAndrey Smetanin if (synic_has_vector_connected(synic, vector)) 965c919412SAndrey Smetanin __set_bit(vector, synic->vec_bitmap); 975c919412SAndrey Smetanin else 985c919412SAndrey Smetanin __clear_bit(vector, synic->vec_bitmap); 995c919412SAndrey Smetanin 1005c919412SAndrey Smetanin if (synic_has_vector_auto_eoi(synic, vector)) 1015c919412SAndrey Smetanin __set_bit(vector, synic->auto_eoi_bitmap); 1025c919412SAndrey Smetanin else 1035c919412SAndrey Smetanin __clear_bit(vector, synic->auto_eoi_bitmap); 1045c919412SAndrey Smetanin 1055c919412SAndrey Smetanin /* Load SynIC vectors into EOI exit bitmap */ 1065c919412SAndrey Smetanin kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); 1075c919412SAndrey Smetanin return 0; 1085c919412SAndrey Smetanin } 1095c919412SAndrey Smetanin 110d3457c87SRoman Kagan static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) 111d3457c87SRoman Kagan { 112d3457c87SRoman Kagan struct kvm_vcpu *vcpu = NULL; 113d3457c87SRoman Kagan int i; 114d3457c87SRoman Kagan 115d3457c87SRoman Kagan if (vpidx < KVM_MAX_VCPUS) 116d3457c87SRoman Kagan vcpu = kvm_get_vcpu(kvm, vpidx); 117d3457c87SRoman Kagan if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx) 118d3457c87SRoman Kagan return vcpu; 119d3457c87SRoman Kagan kvm_for_each_vcpu(i, vcpu, kvm) 120d3457c87SRoman Kagan if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx) 121d3457c87SRoman Kagan return vcpu; 122d3457c87SRoman Kagan return NULL; 123d3457c87SRoman Kagan } 124d3457c87SRoman Kagan 125d3457c87SRoman Kagan static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx) 1265c919412SAndrey Smetanin { 1275c919412SAndrey Smetanin struct kvm_vcpu *vcpu; 1285c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 1295c919412SAndrey Smetanin 130d3457c87SRoman Kagan vcpu = get_vcpu_by_vpidx(kvm, vpidx); 1315c919412SAndrey Smetanin if (!vcpu) 1325c919412SAndrey Smetanin return NULL; 1335c919412SAndrey Smetanin synic = vcpu_to_synic(vcpu); 1345c919412SAndrey Smetanin return (synic->active) ? synic : NULL; 1355c919412SAndrey Smetanin } 1365c919412SAndrey Smetanin 137765eaa0fSAndrey Smetanin static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic, 138765eaa0fSAndrey Smetanin u32 sint) 139765eaa0fSAndrey Smetanin { 140765eaa0fSAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 141765eaa0fSAndrey Smetanin struct page *page; 142765eaa0fSAndrey Smetanin gpa_t gpa; 143765eaa0fSAndrey Smetanin struct hv_message *msg; 144765eaa0fSAndrey Smetanin struct hv_message_page *msg_page; 145765eaa0fSAndrey Smetanin 146765eaa0fSAndrey Smetanin gpa = synic->msg_page & PAGE_MASK; 147765eaa0fSAndrey Smetanin page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); 148765eaa0fSAndrey Smetanin if (is_error_page(page)) { 149765eaa0fSAndrey Smetanin vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n", 150765eaa0fSAndrey Smetanin gpa); 151765eaa0fSAndrey Smetanin return; 152765eaa0fSAndrey Smetanin } 153765eaa0fSAndrey Smetanin msg_page = kmap_atomic(page); 154765eaa0fSAndrey Smetanin 155765eaa0fSAndrey Smetanin msg = &msg_page->sint_message[sint]; 156765eaa0fSAndrey Smetanin msg->header.message_flags.msg_pending = 0; 157765eaa0fSAndrey Smetanin 158765eaa0fSAndrey Smetanin kunmap_atomic(msg_page); 159765eaa0fSAndrey Smetanin kvm_release_page_dirty(page); 160765eaa0fSAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 161765eaa0fSAndrey Smetanin } 162765eaa0fSAndrey Smetanin 1635c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) 1645c919412SAndrey Smetanin { 1655c919412SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 166765eaa0fSAndrey Smetanin struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); 1671f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 1681f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 1691f4b34f8SAndrey Smetanin int gsi, idx, stimers_pending; 1705c919412SAndrey Smetanin 17118659a9cSAndrey Smetanin trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); 1725c919412SAndrey Smetanin 173765eaa0fSAndrey Smetanin if (synic->msg_page & HV_SYNIC_SIMP_ENABLE) 174765eaa0fSAndrey Smetanin synic_clear_sint_msg_pending(synic, sint); 175765eaa0fSAndrey Smetanin 1761f4b34f8SAndrey Smetanin /* Try to deliver pending Hyper-V SynIC timers messages */ 1771f4b34f8SAndrey Smetanin stimers_pending = 0; 1781f4b34f8SAndrey Smetanin for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { 1791f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[idx]; 1801f4b34f8SAndrey Smetanin if (stimer->msg_pending && 1811f4b34f8SAndrey Smetanin (stimer->config & HV_STIMER_ENABLE) && 1821f4b34f8SAndrey Smetanin HV_STIMER_SINT(stimer->config) == sint) { 1831f4b34f8SAndrey Smetanin set_bit(stimer->index, 1841f4b34f8SAndrey Smetanin hv_vcpu->stimer_pending_bitmap); 1851f4b34f8SAndrey Smetanin stimers_pending++; 1861f4b34f8SAndrey Smetanin } 1871f4b34f8SAndrey Smetanin } 1881f4b34f8SAndrey Smetanin if (stimers_pending) 1891f4b34f8SAndrey Smetanin kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 1901f4b34f8SAndrey Smetanin 1915c919412SAndrey Smetanin idx = srcu_read_lock(&kvm->irq_srcu); 1921f4b34f8SAndrey Smetanin gsi = atomic_read(&synic->sint_to_gsi[sint]); 1935c919412SAndrey Smetanin if (gsi != -1) 1945c919412SAndrey Smetanin kvm_notify_acked_gsi(kvm, gsi); 1955c919412SAndrey Smetanin srcu_read_unlock(&kvm->irq_srcu, idx); 1965c919412SAndrey Smetanin } 1975c919412SAndrey Smetanin 198db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) 199db397571SAndrey Smetanin { 200db397571SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 201db397571SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; 202db397571SAndrey Smetanin 203db397571SAndrey Smetanin hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; 204db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msr = msr; 205db397571SAndrey Smetanin hv_vcpu->exit.u.synic.control = synic->control; 206db397571SAndrey Smetanin hv_vcpu->exit.u.synic.evt_page = synic->evt_page; 207db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msg_page = synic->msg_page; 208db397571SAndrey Smetanin 209db397571SAndrey Smetanin kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 210db397571SAndrey Smetanin } 211db397571SAndrey Smetanin 2125c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, 2135c919412SAndrey Smetanin u32 msr, u64 data, bool host) 2145c919412SAndrey Smetanin { 2155c919412SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 2165c919412SAndrey Smetanin int ret; 2175c919412SAndrey Smetanin 2185c919412SAndrey Smetanin if (!synic->active) 2195c919412SAndrey Smetanin return 1; 2205c919412SAndrey Smetanin 22118659a9cSAndrey Smetanin trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); 22218659a9cSAndrey Smetanin 2235c919412SAndrey Smetanin ret = 0; 2245c919412SAndrey Smetanin switch (msr) { 2255c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 2265c919412SAndrey Smetanin synic->control = data; 227db397571SAndrey Smetanin if (!host) 228db397571SAndrey Smetanin synic_exit(synic, msr); 2295c919412SAndrey Smetanin break; 2305c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 2315c919412SAndrey Smetanin if (!host) { 2325c919412SAndrey Smetanin ret = 1; 2335c919412SAndrey Smetanin break; 2345c919412SAndrey Smetanin } 2355c919412SAndrey Smetanin synic->version = data; 2365c919412SAndrey Smetanin break; 2375c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 238efc479e6SRoman Kagan if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && 239efc479e6SRoman Kagan !synic->dont_zero_synic_pages) 2405c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2415c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2425c919412SAndrey Smetanin ret = 1; 2435c919412SAndrey Smetanin break; 2445c919412SAndrey Smetanin } 2455c919412SAndrey Smetanin synic->evt_page = data; 246db397571SAndrey Smetanin if (!host) 247db397571SAndrey Smetanin synic_exit(synic, msr); 2485c919412SAndrey Smetanin break; 2495c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 250efc479e6SRoman Kagan if ((data & HV_SYNIC_SIMP_ENABLE) && !host && 251efc479e6SRoman Kagan !synic->dont_zero_synic_pages) 2525c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2535c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2545c919412SAndrey Smetanin ret = 1; 2555c919412SAndrey Smetanin break; 2565c919412SAndrey Smetanin } 2575c919412SAndrey Smetanin synic->msg_page = data; 258db397571SAndrey Smetanin if (!host) 259db397571SAndrey Smetanin synic_exit(synic, msr); 2605c919412SAndrey Smetanin break; 2615c919412SAndrey Smetanin case HV_X64_MSR_EOM: { 2625c919412SAndrey Smetanin int i; 2635c919412SAndrey Smetanin 2645c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 2655c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 2665c919412SAndrey Smetanin break; 2675c919412SAndrey Smetanin } 2685c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 2697be58a64SAndrey Smetanin ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); 2705c919412SAndrey Smetanin break; 2715c919412SAndrey Smetanin default: 2725c919412SAndrey Smetanin ret = 1; 2735c919412SAndrey Smetanin break; 2745c919412SAndrey Smetanin } 2755c919412SAndrey Smetanin return ret; 2765c919412SAndrey Smetanin } 2775c919412SAndrey Smetanin 2785c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata) 2795c919412SAndrey Smetanin { 2805c919412SAndrey Smetanin int ret; 2815c919412SAndrey Smetanin 2825c919412SAndrey Smetanin if (!synic->active) 2835c919412SAndrey Smetanin return 1; 2845c919412SAndrey Smetanin 2855c919412SAndrey Smetanin ret = 0; 2865c919412SAndrey Smetanin switch (msr) { 2875c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 2885c919412SAndrey Smetanin *pdata = synic->control; 2895c919412SAndrey Smetanin break; 2905c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 2915c919412SAndrey Smetanin *pdata = synic->version; 2925c919412SAndrey Smetanin break; 2935c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 2945c919412SAndrey Smetanin *pdata = synic->evt_page; 2955c919412SAndrey Smetanin break; 2965c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 2975c919412SAndrey Smetanin *pdata = synic->msg_page; 2985c919412SAndrey Smetanin break; 2995c919412SAndrey Smetanin case HV_X64_MSR_EOM: 3005c919412SAndrey Smetanin *pdata = 0; 3015c919412SAndrey Smetanin break; 3025c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 3035c919412SAndrey Smetanin *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); 3045c919412SAndrey Smetanin break; 3055c919412SAndrey Smetanin default: 3065c919412SAndrey Smetanin ret = 1; 3075c919412SAndrey Smetanin break; 3085c919412SAndrey Smetanin } 3095c919412SAndrey Smetanin return ret; 3105c919412SAndrey Smetanin } 3115c919412SAndrey Smetanin 312ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) 3135c919412SAndrey Smetanin { 3145c919412SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 3155c919412SAndrey Smetanin struct kvm_lapic_irq irq; 3165c919412SAndrey Smetanin int ret, vector; 3175c919412SAndrey Smetanin 3185c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint)) 3195c919412SAndrey Smetanin return -EINVAL; 3205c919412SAndrey Smetanin 3215c919412SAndrey Smetanin vector = synic_get_sint_vector(synic_read_sint(synic, sint)); 3225c919412SAndrey Smetanin if (vector < 0) 3235c919412SAndrey Smetanin return -ENOENT; 3245c919412SAndrey Smetanin 3255c919412SAndrey Smetanin memset(&irq, 0, sizeof(irq)); 326f98a3efbSRadim Krčmář irq.shorthand = APIC_DEST_SELF; 3275c919412SAndrey Smetanin irq.dest_mode = APIC_DEST_PHYSICAL; 3285c919412SAndrey Smetanin irq.delivery_mode = APIC_DM_FIXED; 3295c919412SAndrey Smetanin irq.vector = vector; 3305c919412SAndrey Smetanin irq.level = 1; 3315c919412SAndrey Smetanin 332f98a3efbSRadim Krčmář ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL); 33318659a9cSAndrey Smetanin trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret); 3345c919412SAndrey Smetanin return ret; 3355c919412SAndrey Smetanin } 3365c919412SAndrey Smetanin 337d3457c87SRoman Kagan int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint) 3385c919412SAndrey Smetanin { 3395c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 3405c919412SAndrey Smetanin 341d3457c87SRoman Kagan synic = synic_get(kvm, vpidx); 3425c919412SAndrey Smetanin if (!synic) 3435c919412SAndrey Smetanin return -EINVAL; 3445c919412SAndrey Smetanin 3455c919412SAndrey Smetanin return synic_set_irq(synic, sint); 3465c919412SAndrey Smetanin } 3475c919412SAndrey Smetanin 3485c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) 3495c919412SAndrey Smetanin { 3505c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); 3515c919412SAndrey Smetanin int i; 3525c919412SAndrey Smetanin 35318659a9cSAndrey Smetanin trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); 3545c919412SAndrey Smetanin 3555c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 3565c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 3575c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 3585c919412SAndrey Smetanin } 3595c919412SAndrey Smetanin 360d3457c87SRoman Kagan static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi) 3615c919412SAndrey Smetanin { 3625c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 3635c919412SAndrey Smetanin 364d3457c87SRoman Kagan synic = synic_get(kvm, vpidx); 3655c919412SAndrey Smetanin if (!synic) 3665c919412SAndrey Smetanin return -EINVAL; 3675c919412SAndrey Smetanin 3685c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) 3695c919412SAndrey Smetanin return -EINVAL; 3705c919412SAndrey Smetanin 3715c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[sint], gsi); 3725c919412SAndrey Smetanin return 0; 3735c919412SAndrey Smetanin } 3745c919412SAndrey Smetanin 3755c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm) 3765c919412SAndrey Smetanin { 3775c919412SAndrey Smetanin struct kvm_irq_routing_table *irq_rt; 3785c919412SAndrey Smetanin struct kvm_kernel_irq_routing_entry *e; 3795c919412SAndrey Smetanin u32 gsi; 3805c919412SAndrey Smetanin 3815c919412SAndrey Smetanin irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, 3825c919412SAndrey Smetanin lockdep_is_held(&kvm->irq_lock)); 3835c919412SAndrey Smetanin 3845c919412SAndrey Smetanin for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { 3855c919412SAndrey Smetanin hlist_for_each_entry(e, &irq_rt->map[gsi], link) { 3865c919412SAndrey Smetanin if (e->type == KVM_IRQ_ROUTING_HV_SINT) 3875c919412SAndrey Smetanin kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, 3885c919412SAndrey Smetanin e->hv_sint.sint, gsi); 3895c919412SAndrey Smetanin } 3905c919412SAndrey Smetanin } 3915c919412SAndrey Smetanin } 3925c919412SAndrey Smetanin 3935c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic) 3945c919412SAndrey Smetanin { 3955c919412SAndrey Smetanin int i; 3965c919412SAndrey Smetanin 3975c919412SAndrey Smetanin memset(synic, 0, sizeof(*synic)); 3985c919412SAndrey Smetanin synic->version = HV_SYNIC_VERSION_1; 3995c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 4005c919412SAndrey Smetanin atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); 4015c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[i], -1); 4025c919412SAndrey Smetanin } 4035c919412SAndrey Smetanin } 4045c919412SAndrey Smetanin 40593bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm) 40693bf4172SAndrey Smetanin { 407095cf55dSPaolo Bonzini struct kvm_hv *hv = &kvm->arch.hyperv; 408095cf55dSPaolo Bonzini struct kvm_vcpu *vcpu; 409095cf55dSPaolo Bonzini u64 tsc; 410095cf55dSPaolo Bonzini 411095cf55dSPaolo Bonzini /* 412095cf55dSPaolo Bonzini * The guest has not set up the TSC page or the clock isn't 413095cf55dSPaolo Bonzini * stable, fall back to get_kvmclock_ns. 414095cf55dSPaolo Bonzini */ 415095cf55dSPaolo Bonzini if (!hv->tsc_ref.tsc_sequence) 416108b249cSPaolo Bonzini return div_u64(get_kvmclock_ns(kvm), 100); 417095cf55dSPaolo Bonzini 418095cf55dSPaolo Bonzini vcpu = kvm_get_vcpu(kvm, 0); 419095cf55dSPaolo Bonzini tsc = kvm_read_l1_tsc(vcpu, rdtsc()); 420095cf55dSPaolo Bonzini return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) 421095cf55dSPaolo Bonzini + hv->tsc_ref.tsc_offset; 42293bf4172SAndrey Smetanin } 42393bf4172SAndrey Smetanin 424f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, 4251f4b34f8SAndrey Smetanin bool vcpu_kick) 4261f4b34f8SAndrey Smetanin { 4271f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 4281f4b34f8SAndrey Smetanin 4291f4b34f8SAndrey Smetanin set_bit(stimer->index, 4301f4b34f8SAndrey Smetanin vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); 4311f4b34f8SAndrey Smetanin kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 4321f4b34f8SAndrey Smetanin if (vcpu_kick) 4331f4b34f8SAndrey Smetanin kvm_vcpu_kick(vcpu); 4341f4b34f8SAndrey Smetanin } 4351f4b34f8SAndrey Smetanin 4361f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) 4371f4b34f8SAndrey Smetanin { 4381f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 4391f4b34f8SAndrey Smetanin 440ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id, 441ac3e5fcaSAndrey Smetanin stimer->index); 442ac3e5fcaSAndrey Smetanin 443019b9781SAndrey Smetanin hrtimer_cancel(&stimer->timer); 4441f4b34f8SAndrey Smetanin clear_bit(stimer->index, 4451f4b34f8SAndrey Smetanin vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); 4461f4b34f8SAndrey Smetanin stimer->msg_pending = false; 447f808495dSAndrey Smetanin stimer->exp_time = 0; 4481f4b34f8SAndrey Smetanin } 4491f4b34f8SAndrey Smetanin 4501f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) 4511f4b34f8SAndrey Smetanin { 4521f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 4531f4b34f8SAndrey Smetanin 4541f4b34f8SAndrey Smetanin stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); 455ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id, 456ac3e5fcaSAndrey Smetanin stimer->index); 457f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, true); 4581f4b34f8SAndrey Smetanin 4591f4b34f8SAndrey Smetanin return HRTIMER_NORESTART; 4601f4b34f8SAndrey Smetanin } 4611f4b34f8SAndrey Smetanin 462f808495dSAndrey Smetanin /* 463f808495dSAndrey Smetanin * stimer_start() assumptions: 464f808495dSAndrey Smetanin * a) stimer->count is not equal to 0 465f808495dSAndrey Smetanin * b) stimer->config has HV_STIMER_ENABLE flag 466f808495dSAndrey Smetanin */ 4671f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) 4681f4b34f8SAndrey Smetanin { 4691f4b34f8SAndrey Smetanin u64 time_now; 4701f4b34f8SAndrey Smetanin ktime_t ktime_now; 4711f4b34f8SAndrey Smetanin 4721f4b34f8SAndrey Smetanin time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); 4731f4b34f8SAndrey Smetanin ktime_now = ktime_get(); 4741f4b34f8SAndrey Smetanin 4751f4b34f8SAndrey Smetanin if (stimer->config & HV_STIMER_PERIODIC) { 476f808495dSAndrey Smetanin if (stimer->exp_time) { 477f808495dSAndrey Smetanin if (time_now >= stimer->exp_time) { 478f808495dSAndrey Smetanin u64 remainder; 4791f4b34f8SAndrey Smetanin 480f808495dSAndrey Smetanin div64_u64_rem(time_now - stimer->exp_time, 481f808495dSAndrey Smetanin stimer->count, &remainder); 482f808495dSAndrey Smetanin stimer->exp_time = 483f808495dSAndrey Smetanin time_now + (stimer->count - remainder); 484f808495dSAndrey Smetanin } 485f808495dSAndrey Smetanin } else 4861f4b34f8SAndrey Smetanin stimer->exp_time = time_now + stimer->count; 487f808495dSAndrey Smetanin 488ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_start_periodic( 489ac3e5fcaSAndrey Smetanin stimer_to_vcpu(stimer)->vcpu_id, 490ac3e5fcaSAndrey Smetanin stimer->index, 491ac3e5fcaSAndrey Smetanin time_now, stimer->exp_time); 492ac3e5fcaSAndrey Smetanin 4931f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 494f808495dSAndrey Smetanin ktime_add_ns(ktime_now, 495f808495dSAndrey Smetanin 100 * (stimer->exp_time - time_now)), 4961f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 4971f4b34f8SAndrey Smetanin return 0; 4981f4b34f8SAndrey Smetanin } 4991f4b34f8SAndrey Smetanin stimer->exp_time = stimer->count; 5001f4b34f8SAndrey Smetanin if (time_now >= stimer->count) { 5011f4b34f8SAndrey Smetanin /* 5021f4b34f8SAndrey Smetanin * Expire timer according to Hypervisor Top-Level Functional 5031f4b34f8SAndrey Smetanin * specification v4(15.3.1): 5041f4b34f8SAndrey Smetanin * "If a one shot is enabled and the specified count is in 5051f4b34f8SAndrey Smetanin * the past, it will expire immediately." 5061f4b34f8SAndrey Smetanin */ 507f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 5081f4b34f8SAndrey Smetanin return 0; 5091f4b34f8SAndrey Smetanin } 5101f4b34f8SAndrey Smetanin 511ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id, 512ac3e5fcaSAndrey Smetanin stimer->index, 513ac3e5fcaSAndrey Smetanin time_now, stimer->count); 514ac3e5fcaSAndrey Smetanin 5151f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 5161f4b34f8SAndrey Smetanin ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), 5171f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 5181f4b34f8SAndrey Smetanin return 0; 5191f4b34f8SAndrey Smetanin } 5201f4b34f8SAndrey Smetanin 5211f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, 5221f4b34f8SAndrey Smetanin bool host) 5231f4b34f8SAndrey Smetanin { 524ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id, 525ac3e5fcaSAndrey Smetanin stimer->index, config, host); 526ac3e5fcaSAndrey Smetanin 527f3b138c5SAndrey Smetanin stimer_cleanup(stimer); 52823a3b201SAndrey Smetanin if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0) 5291f4b34f8SAndrey Smetanin config &= ~HV_STIMER_ENABLE; 5301f4b34f8SAndrey Smetanin stimer->config = config; 531f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 5321f4b34f8SAndrey Smetanin return 0; 5331f4b34f8SAndrey Smetanin } 5341f4b34f8SAndrey Smetanin 5351f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, 5361f4b34f8SAndrey Smetanin bool host) 5371f4b34f8SAndrey Smetanin { 538ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id, 539ac3e5fcaSAndrey Smetanin stimer->index, count, host); 540ac3e5fcaSAndrey Smetanin 5411f4b34f8SAndrey Smetanin stimer_cleanup(stimer); 542f3b138c5SAndrey Smetanin stimer->count = count; 5431f4b34f8SAndrey Smetanin if (stimer->count == 0) 5441f4b34f8SAndrey Smetanin stimer->config &= ~HV_STIMER_ENABLE; 545f3b138c5SAndrey Smetanin else if (stimer->config & HV_STIMER_AUTOENABLE) 5461f4b34f8SAndrey Smetanin stimer->config |= HV_STIMER_ENABLE; 547f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 5481f4b34f8SAndrey Smetanin return 0; 5491f4b34f8SAndrey Smetanin } 5501f4b34f8SAndrey Smetanin 5511f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) 5521f4b34f8SAndrey Smetanin { 5531f4b34f8SAndrey Smetanin *pconfig = stimer->config; 5541f4b34f8SAndrey Smetanin return 0; 5551f4b34f8SAndrey Smetanin } 5561f4b34f8SAndrey Smetanin 5571f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) 5581f4b34f8SAndrey Smetanin { 5591f4b34f8SAndrey Smetanin *pcount = stimer->count; 5601f4b34f8SAndrey Smetanin return 0; 5611f4b34f8SAndrey Smetanin } 5621f4b34f8SAndrey Smetanin 5631f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, 5641f4b34f8SAndrey Smetanin struct hv_message *src_msg) 5651f4b34f8SAndrey Smetanin { 5661f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 5671f4b34f8SAndrey Smetanin struct page *page; 5681f4b34f8SAndrey Smetanin gpa_t gpa; 5691f4b34f8SAndrey Smetanin struct hv_message *dst_msg; 5701f4b34f8SAndrey Smetanin int r; 5711f4b34f8SAndrey Smetanin struct hv_message_page *msg_page; 5721f4b34f8SAndrey Smetanin 5731f4b34f8SAndrey Smetanin if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) 5741f4b34f8SAndrey Smetanin return -ENOENT; 5751f4b34f8SAndrey Smetanin 5761f4b34f8SAndrey Smetanin gpa = synic->msg_page & PAGE_MASK; 5771f4b34f8SAndrey Smetanin page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); 5781f4b34f8SAndrey Smetanin if (is_error_page(page)) 5791f4b34f8SAndrey Smetanin return -EFAULT; 5801f4b34f8SAndrey Smetanin 5811f4b34f8SAndrey Smetanin msg_page = kmap_atomic(page); 5821f4b34f8SAndrey Smetanin dst_msg = &msg_page->sint_message[sint]; 5831f4b34f8SAndrey Smetanin if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE, 5841f4b34f8SAndrey Smetanin src_msg->header.message_type) != HVMSG_NONE) { 5851f4b34f8SAndrey Smetanin dst_msg->header.message_flags.msg_pending = 1; 5861f4b34f8SAndrey Smetanin r = -EAGAIN; 5871f4b34f8SAndrey Smetanin } else { 5881f4b34f8SAndrey Smetanin memcpy(&dst_msg->u.payload, &src_msg->u.payload, 5891f4b34f8SAndrey Smetanin src_msg->header.payload_size); 5901f4b34f8SAndrey Smetanin dst_msg->header.message_type = src_msg->header.message_type; 5911f4b34f8SAndrey Smetanin dst_msg->header.payload_size = src_msg->header.payload_size; 5921f4b34f8SAndrey Smetanin r = synic_set_irq(synic, sint); 5931f4b34f8SAndrey Smetanin if (r >= 1) 5941f4b34f8SAndrey Smetanin r = 0; 5951f4b34f8SAndrey Smetanin else if (r == 0) 5961f4b34f8SAndrey Smetanin r = -EFAULT; 5971f4b34f8SAndrey Smetanin } 5981f4b34f8SAndrey Smetanin kunmap_atomic(msg_page); 5991f4b34f8SAndrey Smetanin kvm_release_page_dirty(page); 6001f4b34f8SAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 6011f4b34f8SAndrey Smetanin return r; 6021f4b34f8SAndrey Smetanin } 6031f4b34f8SAndrey Smetanin 6040cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) 6051f4b34f8SAndrey Smetanin { 6061f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 6071f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 6081f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 6091f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 6101f4b34f8SAndrey Smetanin 6111f4b34f8SAndrey Smetanin payload->expiration_time = stimer->exp_time; 6121f4b34f8SAndrey Smetanin payload->delivery_time = get_time_ref_counter(vcpu->kvm); 6130cdeabb1SAndrey Smetanin return synic_deliver_msg(vcpu_to_synic(vcpu), 6141f4b34f8SAndrey Smetanin HV_STIMER_SINT(stimer->config), msg); 6151f4b34f8SAndrey Smetanin } 6161f4b34f8SAndrey Smetanin 6171f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) 6181f4b34f8SAndrey Smetanin { 619ac3e5fcaSAndrey Smetanin int r; 620ac3e5fcaSAndrey Smetanin 6210cdeabb1SAndrey Smetanin stimer->msg_pending = true; 622ac3e5fcaSAndrey Smetanin r = stimer_send_msg(stimer); 623ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id, 624ac3e5fcaSAndrey Smetanin stimer->index, r); 625ac3e5fcaSAndrey Smetanin if (!r) { 6260cdeabb1SAndrey Smetanin stimer->msg_pending = false; 6271f4b34f8SAndrey Smetanin if (!(stimer->config & HV_STIMER_PERIODIC)) 6281ac1b65aSAndrey Smetanin stimer->config &= ~HV_STIMER_ENABLE; 6290cdeabb1SAndrey Smetanin } 6301f4b34f8SAndrey Smetanin } 6311f4b34f8SAndrey Smetanin 6321f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) 6331f4b34f8SAndrey Smetanin { 6341f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 6351f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 636f3b138c5SAndrey Smetanin u64 time_now, exp_time; 6371f4b34f8SAndrey Smetanin int i; 6381f4b34f8SAndrey Smetanin 6391f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 6401f4b34f8SAndrey Smetanin if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { 6411f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[i]; 6421f4b34f8SAndrey Smetanin if (stimer->config & HV_STIMER_ENABLE) { 643f3b138c5SAndrey Smetanin exp_time = stimer->exp_time; 6440cdeabb1SAndrey Smetanin 645f3b138c5SAndrey Smetanin if (exp_time) { 646f3b138c5SAndrey Smetanin time_now = 647f3b138c5SAndrey Smetanin get_time_ref_counter(vcpu->kvm); 648f3b138c5SAndrey Smetanin if (time_now >= exp_time) 649f3b138c5SAndrey Smetanin stimer_expiration(stimer); 650f3b138c5SAndrey Smetanin } 651f3b138c5SAndrey Smetanin 652f3b138c5SAndrey Smetanin if ((stimer->config & HV_STIMER_ENABLE) && 653f1ff89ecSRoman Kagan stimer->count) { 654f1ff89ecSRoman Kagan if (!stimer->msg_pending) 6550cdeabb1SAndrey Smetanin stimer_start(stimer); 656f1ff89ecSRoman Kagan } else 6570cdeabb1SAndrey Smetanin stimer_cleanup(stimer); 6581f4b34f8SAndrey Smetanin } 6591f4b34f8SAndrey Smetanin } 6601f4b34f8SAndrey Smetanin } 6611f4b34f8SAndrey Smetanin 6621f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) 6631f4b34f8SAndrey Smetanin { 6641f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 6651f4b34f8SAndrey Smetanin int i; 6661f4b34f8SAndrey Smetanin 6671f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 6681f4b34f8SAndrey Smetanin stimer_cleanup(&hv_vcpu->stimer[i]); 6691f4b34f8SAndrey Smetanin } 6701f4b34f8SAndrey Smetanin 6711f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) 6721f4b34f8SAndrey Smetanin { 6731f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 6741f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 6751f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 6761f4b34f8SAndrey Smetanin 6771f4b34f8SAndrey Smetanin memset(&msg->header, 0, sizeof(msg->header)); 6781f4b34f8SAndrey Smetanin msg->header.message_type = HVMSG_TIMER_EXPIRED; 6791f4b34f8SAndrey Smetanin msg->header.payload_size = sizeof(*payload); 6801f4b34f8SAndrey Smetanin 6811f4b34f8SAndrey Smetanin payload->timer_index = stimer->index; 6821f4b34f8SAndrey Smetanin payload->expiration_time = 0; 6831f4b34f8SAndrey Smetanin payload->delivery_time = 0; 6841f4b34f8SAndrey Smetanin } 6851f4b34f8SAndrey Smetanin 6861f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) 6871f4b34f8SAndrey Smetanin { 6881f4b34f8SAndrey Smetanin memset(stimer, 0, sizeof(*stimer)); 6891f4b34f8SAndrey Smetanin stimer->index = timer_index; 6901f4b34f8SAndrey Smetanin hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 6911f4b34f8SAndrey Smetanin stimer->timer.function = stimer_timer_callback; 6921f4b34f8SAndrey Smetanin stimer_prepare_msg(stimer); 6931f4b34f8SAndrey Smetanin } 6941f4b34f8SAndrey Smetanin 6955c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) 6965c919412SAndrey Smetanin { 6971f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 6981f4b34f8SAndrey Smetanin int i; 6991f4b34f8SAndrey Smetanin 7001f4b34f8SAndrey Smetanin synic_init(&hv_vcpu->synic); 7011f4b34f8SAndrey Smetanin 7021f4b34f8SAndrey Smetanin bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); 7031f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 7041f4b34f8SAndrey Smetanin stimer_init(&hv_vcpu->stimer[i], i); 7055c919412SAndrey Smetanin } 7065c919412SAndrey Smetanin 707d3457c87SRoman Kagan void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu) 708d3457c87SRoman Kagan { 709d3457c87SRoman Kagan struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 710d3457c87SRoman Kagan 711d3457c87SRoman Kagan hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu); 712d3457c87SRoman Kagan } 713d3457c87SRoman Kagan 714efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) 7155c919412SAndrey Smetanin { 716efc479e6SRoman Kagan struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); 717efc479e6SRoman Kagan 7185c919412SAndrey Smetanin /* 7195c919412SAndrey Smetanin * Hyper-V SynIC auto EOI SINT's are 7205c919412SAndrey Smetanin * not compatible with APICV, so deactivate APICV 7215c919412SAndrey Smetanin */ 7225c919412SAndrey Smetanin kvm_vcpu_deactivate_apicv(vcpu); 723efc479e6SRoman Kagan synic->active = true; 724efc479e6SRoman Kagan synic->dont_zero_synic_pages = dont_zero_synic_pages; 7255c919412SAndrey Smetanin return 0; 7265c919412SAndrey Smetanin } 7275c919412SAndrey Smetanin 728e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr) 729e83d5887SAndrey Smetanin { 730e83d5887SAndrey Smetanin bool r = false; 731e83d5887SAndrey Smetanin 732e83d5887SAndrey Smetanin switch (msr) { 733e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 734e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 735e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 736e83d5887SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 737e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 738e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 739e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 740e83d5887SAndrey Smetanin r = true; 741e83d5887SAndrey Smetanin break; 742e83d5887SAndrey Smetanin } 743e83d5887SAndrey Smetanin 744e83d5887SAndrey Smetanin return r; 745e83d5887SAndrey Smetanin } 746e83d5887SAndrey Smetanin 747e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, 748e7d9513bSAndrey Smetanin u32 index, u64 *pdata) 749e7d9513bSAndrey Smetanin { 750e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 751e7d9513bSAndrey Smetanin 752e7d9513bSAndrey Smetanin if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) 753e7d9513bSAndrey Smetanin return -EINVAL; 754e7d9513bSAndrey Smetanin 755e7d9513bSAndrey Smetanin *pdata = hv->hv_crash_param[index]; 756e7d9513bSAndrey Smetanin return 0; 757e7d9513bSAndrey Smetanin } 758e7d9513bSAndrey Smetanin 759e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) 760e7d9513bSAndrey Smetanin { 761e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 762e7d9513bSAndrey Smetanin 763e7d9513bSAndrey Smetanin *pdata = hv->hv_crash_ctl; 764e7d9513bSAndrey Smetanin return 0; 765e7d9513bSAndrey Smetanin } 766e7d9513bSAndrey Smetanin 767e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) 768e7d9513bSAndrey Smetanin { 769e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 770e7d9513bSAndrey Smetanin 771e7d9513bSAndrey Smetanin if (host) 772e7d9513bSAndrey Smetanin hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY; 773e7d9513bSAndrey Smetanin 774e7d9513bSAndrey Smetanin if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) { 775e7d9513bSAndrey Smetanin 776e7d9513bSAndrey Smetanin vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", 777e7d9513bSAndrey Smetanin hv->hv_crash_param[0], 778e7d9513bSAndrey Smetanin hv->hv_crash_param[1], 779e7d9513bSAndrey Smetanin hv->hv_crash_param[2], 780e7d9513bSAndrey Smetanin hv->hv_crash_param[3], 781e7d9513bSAndrey Smetanin hv->hv_crash_param[4]); 782e7d9513bSAndrey Smetanin 783e7d9513bSAndrey Smetanin /* Send notification about crash to user space */ 784e7d9513bSAndrey Smetanin kvm_make_request(KVM_REQ_HV_CRASH, vcpu); 785e7d9513bSAndrey Smetanin } 786e7d9513bSAndrey Smetanin 787e7d9513bSAndrey Smetanin return 0; 788e7d9513bSAndrey Smetanin } 789e7d9513bSAndrey Smetanin 790e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, 791e7d9513bSAndrey Smetanin u32 index, u64 data) 792e7d9513bSAndrey Smetanin { 793e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 794e7d9513bSAndrey Smetanin 795e7d9513bSAndrey Smetanin if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) 796e7d9513bSAndrey Smetanin return -EINVAL; 797e7d9513bSAndrey Smetanin 798e7d9513bSAndrey Smetanin hv->hv_crash_param[index] = data; 799e7d9513bSAndrey Smetanin return 0; 800e7d9513bSAndrey Smetanin } 801e7d9513bSAndrey Smetanin 802095cf55dSPaolo Bonzini /* 803095cf55dSPaolo Bonzini * The kvmclock and Hyper-V TSC page use similar formulas, and converting 804095cf55dSPaolo Bonzini * between them is possible: 805095cf55dSPaolo Bonzini * 806095cf55dSPaolo Bonzini * kvmclock formula: 807095cf55dSPaolo Bonzini * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) 808095cf55dSPaolo Bonzini * + system_time 809095cf55dSPaolo Bonzini * 810095cf55dSPaolo Bonzini * Hyper-V formula: 811095cf55dSPaolo Bonzini * nsec/100 = ticks * scale / 2^64 + offset 812095cf55dSPaolo Bonzini * 813095cf55dSPaolo Bonzini * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. 814095cf55dSPaolo Bonzini * By dividing the kvmclock formula by 100 and equating what's left we get: 815095cf55dSPaolo Bonzini * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 816095cf55dSPaolo Bonzini * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 817095cf55dSPaolo Bonzini * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 818095cf55dSPaolo Bonzini * 819095cf55dSPaolo Bonzini * Now expand the kvmclock formula and divide by 100: 820095cf55dSPaolo Bonzini * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) 821095cf55dSPaolo Bonzini * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) 822095cf55dSPaolo Bonzini * + system_time 823095cf55dSPaolo Bonzini * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 824095cf55dSPaolo Bonzini * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 825095cf55dSPaolo Bonzini * + system_time / 100 826095cf55dSPaolo Bonzini * 827095cf55dSPaolo Bonzini * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: 828095cf55dSPaolo Bonzini * nsec/100 = ticks * scale / 2^64 829095cf55dSPaolo Bonzini * - tsc_timestamp * scale / 2^64 830095cf55dSPaolo Bonzini * + system_time / 100 831095cf55dSPaolo Bonzini * 832095cf55dSPaolo Bonzini * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: 833095cf55dSPaolo Bonzini * offset = system_time / 100 - tsc_timestamp * scale / 2^64 834095cf55dSPaolo Bonzini * 835095cf55dSPaolo Bonzini * These two equivalencies are implemented in this function. 836095cf55dSPaolo Bonzini */ 837095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, 838095cf55dSPaolo Bonzini HV_REFERENCE_TSC_PAGE *tsc_ref) 839095cf55dSPaolo Bonzini { 840095cf55dSPaolo Bonzini u64 max_mul; 841095cf55dSPaolo Bonzini 842095cf55dSPaolo Bonzini if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) 843095cf55dSPaolo Bonzini return false; 844095cf55dSPaolo Bonzini 845095cf55dSPaolo Bonzini /* 846095cf55dSPaolo Bonzini * check if scale would overflow, if so we use the time ref counter 847095cf55dSPaolo Bonzini * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 848095cf55dSPaolo Bonzini * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) 849095cf55dSPaolo Bonzini * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) 850095cf55dSPaolo Bonzini */ 851095cf55dSPaolo Bonzini max_mul = 100ull << (32 - hv_clock->tsc_shift); 852095cf55dSPaolo Bonzini if (hv_clock->tsc_to_system_mul >= max_mul) 853095cf55dSPaolo Bonzini return false; 854095cf55dSPaolo Bonzini 855095cf55dSPaolo Bonzini /* 856095cf55dSPaolo Bonzini * Otherwise compute the scale and offset according to the formulas 857095cf55dSPaolo Bonzini * derived above. 858095cf55dSPaolo Bonzini */ 859095cf55dSPaolo Bonzini tsc_ref->tsc_scale = 860095cf55dSPaolo Bonzini mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), 861095cf55dSPaolo Bonzini hv_clock->tsc_to_system_mul, 862095cf55dSPaolo Bonzini 100); 863095cf55dSPaolo Bonzini 864095cf55dSPaolo Bonzini tsc_ref->tsc_offset = hv_clock->system_time; 865095cf55dSPaolo Bonzini do_div(tsc_ref->tsc_offset, 100); 866095cf55dSPaolo Bonzini tsc_ref->tsc_offset -= 867095cf55dSPaolo Bonzini mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); 868095cf55dSPaolo Bonzini return true; 869095cf55dSPaolo Bonzini } 870095cf55dSPaolo Bonzini 871095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm, 872095cf55dSPaolo Bonzini struct pvclock_vcpu_time_info *hv_clock) 873095cf55dSPaolo Bonzini { 874095cf55dSPaolo Bonzini struct kvm_hv *hv = &kvm->arch.hyperv; 875095cf55dSPaolo Bonzini u32 tsc_seq; 876095cf55dSPaolo Bonzini u64 gfn; 877095cf55dSPaolo Bonzini 878095cf55dSPaolo Bonzini BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); 879095cf55dSPaolo Bonzini BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0); 880095cf55dSPaolo Bonzini 881095cf55dSPaolo Bonzini if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 882095cf55dSPaolo Bonzini return; 883095cf55dSPaolo Bonzini 8843f5ad8beSPaolo Bonzini mutex_lock(&kvm->arch.hyperv.hv_lock); 8853f5ad8beSPaolo Bonzini if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 8863f5ad8beSPaolo Bonzini goto out_unlock; 8873f5ad8beSPaolo Bonzini 888095cf55dSPaolo Bonzini gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; 889095cf55dSPaolo Bonzini /* 890095cf55dSPaolo Bonzini * Because the TSC parameters only vary when there is a 891095cf55dSPaolo Bonzini * change in the master clock, do not bother with caching. 892095cf55dSPaolo Bonzini */ 893095cf55dSPaolo Bonzini if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), 894095cf55dSPaolo Bonzini &tsc_seq, sizeof(tsc_seq)))) 8953f5ad8beSPaolo Bonzini goto out_unlock; 896095cf55dSPaolo Bonzini 897095cf55dSPaolo Bonzini /* 898095cf55dSPaolo Bonzini * While we're computing and writing the parameters, force the 899095cf55dSPaolo Bonzini * guest to use the time reference count MSR. 900095cf55dSPaolo Bonzini */ 901095cf55dSPaolo Bonzini hv->tsc_ref.tsc_sequence = 0; 902095cf55dSPaolo Bonzini if (kvm_write_guest(kvm, gfn_to_gpa(gfn), 903095cf55dSPaolo Bonzini &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) 9043f5ad8beSPaolo Bonzini goto out_unlock; 905095cf55dSPaolo Bonzini 906095cf55dSPaolo Bonzini if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) 9073f5ad8beSPaolo Bonzini goto out_unlock; 908095cf55dSPaolo Bonzini 909095cf55dSPaolo Bonzini /* Ensure sequence is zero before writing the rest of the struct. */ 910095cf55dSPaolo Bonzini smp_wmb(); 911095cf55dSPaolo Bonzini if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) 9123f5ad8beSPaolo Bonzini goto out_unlock; 913095cf55dSPaolo Bonzini 914095cf55dSPaolo Bonzini /* 915095cf55dSPaolo Bonzini * Now switch to the TSC page mechanism by writing the sequence. 916095cf55dSPaolo Bonzini */ 917095cf55dSPaolo Bonzini tsc_seq++; 918095cf55dSPaolo Bonzini if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) 919095cf55dSPaolo Bonzini tsc_seq = 1; 920095cf55dSPaolo Bonzini 921095cf55dSPaolo Bonzini /* Write the struct entirely before the non-zero sequence. */ 922095cf55dSPaolo Bonzini smp_wmb(); 923095cf55dSPaolo Bonzini 924095cf55dSPaolo Bonzini hv->tsc_ref.tsc_sequence = tsc_seq; 925095cf55dSPaolo Bonzini kvm_write_guest(kvm, gfn_to_gpa(gfn), 926095cf55dSPaolo Bonzini &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)); 9273f5ad8beSPaolo Bonzini out_unlock: 9283f5ad8beSPaolo Bonzini mutex_unlock(&kvm->arch.hyperv.hv_lock); 929095cf55dSPaolo Bonzini } 930095cf55dSPaolo Bonzini 931e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, 932e7d9513bSAndrey Smetanin bool host) 933e83d5887SAndrey Smetanin { 934e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 935e83d5887SAndrey Smetanin struct kvm_hv *hv = &kvm->arch.hyperv; 936e83d5887SAndrey Smetanin 937e83d5887SAndrey Smetanin switch (msr) { 938e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 939e83d5887SAndrey Smetanin hv->hv_guest_os_id = data; 940e83d5887SAndrey Smetanin /* setting guest os id to zero disables hypercall page */ 941e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 942e83d5887SAndrey Smetanin hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; 943e83d5887SAndrey Smetanin break; 944e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: { 945e83d5887SAndrey Smetanin u64 gfn; 946e83d5887SAndrey Smetanin unsigned long addr; 947e83d5887SAndrey Smetanin u8 instructions[4]; 948e83d5887SAndrey Smetanin 949e83d5887SAndrey Smetanin /* if guest os id is not set hypercall should remain disabled */ 950e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 951e83d5887SAndrey Smetanin break; 952e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { 953e83d5887SAndrey Smetanin hv->hv_hypercall = data; 954e83d5887SAndrey Smetanin break; 955e83d5887SAndrey Smetanin } 956e83d5887SAndrey Smetanin gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; 957e83d5887SAndrey Smetanin addr = gfn_to_hva(kvm, gfn); 958e83d5887SAndrey Smetanin if (kvm_is_error_hva(addr)) 959e83d5887SAndrey Smetanin return 1; 960e83d5887SAndrey Smetanin kvm_x86_ops->patch_hypercall(vcpu, instructions); 961e83d5887SAndrey Smetanin ((unsigned char *)instructions)[3] = 0xc3; /* ret */ 962e83d5887SAndrey Smetanin if (__copy_to_user((void __user *)addr, instructions, 4)) 963e83d5887SAndrey Smetanin return 1; 964e83d5887SAndrey Smetanin hv->hv_hypercall = data; 965e83d5887SAndrey Smetanin mark_page_dirty(kvm, gfn); 966e83d5887SAndrey Smetanin break; 967e83d5887SAndrey Smetanin } 968095cf55dSPaolo Bonzini case HV_X64_MSR_REFERENCE_TSC: 969e83d5887SAndrey Smetanin hv->hv_tsc_page = data; 970095cf55dSPaolo Bonzini if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) 971095cf55dSPaolo Bonzini kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); 972e83d5887SAndrey Smetanin break; 973e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 974e7d9513bSAndrey Smetanin return kvm_hv_msr_set_crash_data(vcpu, 975e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 976e7d9513bSAndrey Smetanin data); 977e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 978e7d9513bSAndrey Smetanin return kvm_hv_msr_set_crash_ctl(vcpu, data, host); 979e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 980e516cebbSAndrey Smetanin if (data == 1) { 981e516cebbSAndrey Smetanin vcpu_debug(vcpu, "hyper-v reset requested\n"); 982e516cebbSAndrey Smetanin kvm_make_request(KVM_REQ_HV_RESET, vcpu); 983e516cebbSAndrey Smetanin } 984e516cebbSAndrey Smetanin break; 985e83d5887SAndrey Smetanin default: 986e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", 987e83d5887SAndrey Smetanin msr, data); 988e83d5887SAndrey Smetanin return 1; 989e83d5887SAndrey Smetanin } 990e83d5887SAndrey Smetanin return 0; 991e83d5887SAndrey Smetanin } 992e83d5887SAndrey Smetanin 9939eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */ 9949eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void) 9959eec50b8SAndrey Smetanin { 9965613fda9SFrederic Weisbecker u64 utime, stime; 9979eec50b8SAndrey Smetanin 9989eec50b8SAndrey Smetanin task_cputime_adjusted(current, &utime, &stime); 9995613fda9SFrederic Weisbecker 10005613fda9SFrederic Weisbecker return div_u64(utime + stime, 100); 10019eec50b8SAndrey Smetanin } 10029eec50b8SAndrey Smetanin 10039eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1004e83d5887SAndrey Smetanin { 1005e83d5887SAndrey Smetanin struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; 1006e83d5887SAndrey Smetanin 1007e83d5887SAndrey Smetanin switch (msr) { 1008d3457c87SRoman Kagan case HV_X64_MSR_VP_INDEX: 1009d3457c87SRoman Kagan if (!host) 1010d3457c87SRoman Kagan return 1; 1011d3457c87SRoman Kagan hv->vp_index = (u32)data; 1012d3457c87SRoman Kagan break; 1013e83d5887SAndrey Smetanin case HV_X64_MSR_APIC_ASSIST_PAGE: { 1014e83d5887SAndrey Smetanin u64 gfn; 1015e83d5887SAndrey Smetanin unsigned long addr; 1016e83d5887SAndrey Smetanin 1017e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { 1018e83d5887SAndrey Smetanin hv->hv_vapic = data; 1019e83d5887SAndrey Smetanin if (kvm_lapic_enable_pv_eoi(vcpu, 0)) 1020e83d5887SAndrey Smetanin return 1; 1021e83d5887SAndrey Smetanin break; 1022e83d5887SAndrey Smetanin } 1023e83d5887SAndrey Smetanin gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT; 1024e83d5887SAndrey Smetanin addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); 1025e83d5887SAndrey Smetanin if (kvm_is_error_hva(addr)) 1026e83d5887SAndrey Smetanin return 1; 1027e83d5887SAndrey Smetanin if (__clear_user((void __user *)addr, PAGE_SIZE)) 1028e83d5887SAndrey Smetanin return 1; 1029e83d5887SAndrey Smetanin hv->hv_vapic = data; 1030e83d5887SAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gfn); 1031e83d5887SAndrey Smetanin if (kvm_lapic_enable_pv_eoi(vcpu, 1032e83d5887SAndrey Smetanin gfn_to_gpa(gfn) | KVM_MSR_ENABLED)) 1033e83d5887SAndrey Smetanin return 1; 1034e83d5887SAndrey Smetanin break; 1035e83d5887SAndrey Smetanin } 1036e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 1037e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); 1038e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 1039e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); 1040e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 1041e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); 10429eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 10439eec50b8SAndrey Smetanin if (!host) 10449eec50b8SAndrey Smetanin return 1; 10459eec50b8SAndrey Smetanin hv->runtime_offset = data - current_task_runtime_100ns(); 10469eec50b8SAndrey Smetanin break; 10475c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 10485c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 10495c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 10505c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 10515c919412SAndrey Smetanin case HV_X64_MSR_EOM: 10525c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 10535c919412SAndrey Smetanin return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); 10541f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 10551f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 10561f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 10571f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 10581f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 10591f4b34f8SAndrey Smetanin 10601f4b34f8SAndrey Smetanin return stimer_set_config(vcpu_to_stimer(vcpu, timer_index), 10611f4b34f8SAndrey Smetanin data, host); 10621f4b34f8SAndrey Smetanin } 10631f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 10641f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 10651f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 10661f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 10671f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 10681f4b34f8SAndrey Smetanin 10691f4b34f8SAndrey Smetanin return stimer_set_count(vcpu_to_stimer(vcpu, timer_index), 10701f4b34f8SAndrey Smetanin data, host); 10711f4b34f8SAndrey Smetanin } 1072e83d5887SAndrey Smetanin default: 1073e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", 1074e83d5887SAndrey Smetanin msr, data); 1075e83d5887SAndrey Smetanin return 1; 1076e83d5887SAndrey Smetanin } 1077e83d5887SAndrey Smetanin 1078e83d5887SAndrey Smetanin return 0; 1079e83d5887SAndrey Smetanin } 1080e83d5887SAndrey Smetanin 1081e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 1082e83d5887SAndrey Smetanin { 1083e83d5887SAndrey Smetanin u64 data = 0; 1084e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 1085e83d5887SAndrey Smetanin struct kvm_hv *hv = &kvm->arch.hyperv; 1086e83d5887SAndrey Smetanin 1087e83d5887SAndrey Smetanin switch (msr) { 1088e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 1089e83d5887SAndrey Smetanin data = hv->hv_guest_os_id; 1090e83d5887SAndrey Smetanin break; 1091e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 1092e83d5887SAndrey Smetanin data = hv->hv_hypercall; 1093e83d5887SAndrey Smetanin break; 109493bf4172SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 109593bf4172SAndrey Smetanin data = get_time_ref_counter(kvm); 1096e83d5887SAndrey Smetanin break; 1097e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 1098e83d5887SAndrey Smetanin data = hv->hv_tsc_page; 1099e83d5887SAndrey Smetanin break; 1100e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 1101e7d9513bSAndrey Smetanin return kvm_hv_msr_get_crash_data(vcpu, 1102e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 1103e7d9513bSAndrey Smetanin pdata); 1104e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 1105e7d9513bSAndrey Smetanin return kvm_hv_msr_get_crash_ctl(vcpu, pdata); 1106e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 1107e516cebbSAndrey Smetanin data = 0; 1108e516cebbSAndrey Smetanin break; 1109e83d5887SAndrey Smetanin default: 1110e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1111e83d5887SAndrey Smetanin return 1; 1112e83d5887SAndrey Smetanin } 1113e83d5887SAndrey Smetanin 1114e83d5887SAndrey Smetanin *pdata = data; 1115e83d5887SAndrey Smetanin return 0; 1116e83d5887SAndrey Smetanin } 1117e83d5887SAndrey Smetanin 1118e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 1119e83d5887SAndrey Smetanin { 1120e83d5887SAndrey Smetanin u64 data = 0; 1121e83d5887SAndrey Smetanin struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; 1122e83d5887SAndrey Smetanin 1123e83d5887SAndrey Smetanin switch (msr) { 1124d3457c87SRoman Kagan case HV_X64_MSR_VP_INDEX: 1125d3457c87SRoman Kagan data = hv->vp_index; 1126e83d5887SAndrey Smetanin break; 1127e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 1128e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); 1129e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 1130e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); 1131e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 1132e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); 1133e83d5887SAndrey Smetanin case HV_X64_MSR_APIC_ASSIST_PAGE: 1134e83d5887SAndrey Smetanin data = hv->hv_vapic; 1135e83d5887SAndrey Smetanin break; 11369eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 11379eec50b8SAndrey Smetanin data = current_task_runtime_100ns() + hv->runtime_offset; 11389eec50b8SAndrey Smetanin break; 11395c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 11405c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 11415c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 11425c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 11435c919412SAndrey Smetanin case HV_X64_MSR_EOM: 11445c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 11455c919412SAndrey Smetanin return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata); 11461f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 11471f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 11481f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 11491f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 11501f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 11511f4b34f8SAndrey Smetanin 11521f4b34f8SAndrey Smetanin return stimer_get_config(vcpu_to_stimer(vcpu, timer_index), 11531f4b34f8SAndrey Smetanin pdata); 11541f4b34f8SAndrey Smetanin } 11551f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 11561f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 11571f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 11581f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 11591f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 11601f4b34f8SAndrey Smetanin 11611f4b34f8SAndrey Smetanin return stimer_get_count(vcpu_to_stimer(vcpu, timer_index), 11621f4b34f8SAndrey Smetanin pdata); 11631f4b34f8SAndrey Smetanin } 116472c139baSLadi Prosek case HV_X64_MSR_TSC_FREQUENCY: 116572c139baSLadi Prosek data = (u64)vcpu->arch.virtual_tsc_khz * 1000; 116672c139baSLadi Prosek break; 116772c139baSLadi Prosek case HV_X64_MSR_APIC_FREQUENCY: 116872c139baSLadi Prosek data = APIC_BUS_FREQUENCY; 116972c139baSLadi Prosek break; 1170e83d5887SAndrey Smetanin default: 1171e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1172e83d5887SAndrey Smetanin return 1; 1173e83d5887SAndrey Smetanin } 1174e83d5887SAndrey Smetanin *pdata = data; 1175e83d5887SAndrey Smetanin return 0; 1176e83d5887SAndrey Smetanin } 1177e83d5887SAndrey Smetanin 1178e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1179e83d5887SAndrey Smetanin { 1180e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 1181e83d5887SAndrey Smetanin int r; 1182e83d5887SAndrey Smetanin 11833f5ad8beSPaolo Bonzini mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); 1184e7d9513bSAndrey Smetanin r = kvm_hv_set_msr_pw(vcpu, msr, data, host); 11853f5ad8beSPaolo Bonzini mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); 1186e83d5887SAndrey Smetanin return r; 1187e83d5887SAndrey Smetanin } else 11889eec50b8SAndrey Smetanin return kvm_hv_set_msr(vcpu, msr, data, host); 1189e83d5887SAndrey Smetanin } 1190e83d5887SAndrey Smetanin 1191e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 1192e83d5887SAndrey Smetanin { 1193e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 1194e83d5887SAndrey Smetanin int r; 1195e83d5887SAndrey Smetanin 11963f5ad8beSPaolo Bonzini mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock); 1197e83d5887SAndrey Smetanin r = kvm_hv_get_msr_pw(vcpu, msr, pdata); 11983f5ad8beSPaolo Bonzini mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock); 1199e83d5887SAndrey Smetanin return r; 1200e83d5887SAndrey Smetanin } else 1201e83d5887SAndrey Smetanin return kvm_hv_get_msr(vcpu, msr, pdata); 1202e83d5887SAndrey Smetanin } 1203e83d5887SAndrey Smetanin 1204e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm) 1205e83d5887SAndrey Smetanin { 12063f5ad8beSPaolo Bonzini return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE; 1207e83d5887SAndrey Smetanin } 1208e83d5887SAndrey Smetanin 120983326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) 121083326e43SAndrey Smetanin { 121183326e43SAndrey Smetanin bool longmode; 121283326e43SAndrey Smetanin 121383326e43SAndrey Smetanin longmode = is_64_bit_mode(vcpu); 121483326e43SAndrey Smetanin if (longmode) 121583326e43SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RAX, result); 121683326e43SAndrey Smetanin else { 121783326e43SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32); 121883326e43SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff); 121983326e43SAndrey Smetanin } 122083326e43SAndrey Smetanin } 122183326e43SAndrey Smetanin 122283326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) 122383326e43SAndrey Smetanin { 122483326e43SAndrey Smetanin struct kvm_run *run = vcpu->run; 122583326e43SAndrey Smetanin 122683326e43SAndrey Smetanin kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result); 122783326e43SAndrey Smetanin return 1; 122883326e43SAndrey Smetanin } 122983326e43SAndrey Smetanin 1230*faeb7833SRoman Kagan static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param) 1231*faeb7833SRoman Kagan { 1232*faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 1233*faeb7833SRoman Kagan 1234*faeb7833SRoman Kagan if (unlikely(!fast)) { 1235*faeb7833SRoman Kagan int ret; 1236*faeb7833SRoman Kagan gpa_t gpa = param; 1237*faeb7833SRoman Kagan 1238*faeb7833SRoman Kagan if ((gpa & (__alignof__(param) - 1)) || 1239*faeb7833SRoman Kagan offset_in_page(gpa) + sizeof(param) > PAGE_SIZE) 1240*faeb7833SRoman Kagan return HV_STATUS_INVALID_ALIGNMENT; 1241*faeb7833SRoman Kagan 1242*faeb7833SRoman Kagan ret = kvm_vcpu_read_guest(vcpu, gpa, ¶m, sizeof(param)); 1243*faeb7833SRoman Kagan if (ret < 0) 1244*faeb7833SRoman Kagan return HV_STATUS_INVALID_ALIGNMENT; 1245*faeb7833SRoman Kagan } 1246*faeb7833SRoman Kagan 1247*faeb7833SRoman Kagan /* 1248*faeb7833SRoman Kagan * Per spec, bits 32-47 contain the extra "flag number". However, we 1249*faeb7833SRoman Kagan * have no use for it, and in all known usecases it is zero, so just 1250*faeb7833SRoman Kagan * report lookup failure if it isn't. 1251*faeb7833SRoman Kagan */ 1252*faeb7833SRoman Kagan if (param & 0xffff00000000ULL) 1253*faeb7833SRoman Kagan return HV_STATUS_INVALID_PORT_ID; 1254*faeb7833SRoman Kagan /* remaining bits are reserved-zero */ 1255*faeb7833SRoman Kagan if (param & ~KVM_HYPERV_CONN_ID_MASK) 1256*faeb7833SRoman Kagan return HV_STATUS_INVALID_HYPERCALL_INPUT; 1257*faeb7833SRoman Kagan 1258*faeb7833SRoman Kagan /* conn_to_evt is protected by vcpu->kvm->srcu */ 1259*faeb7833SRoman Kagan eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param); 1260*faeb7833SRoman Kagan if (!eventfd) 1261*faeb7833SRoman Kagan return HV_STATUS_INVALID_PORT_ID; 1262*faeb7833SRoman Kagan 1263*faeb7833SRoman Kagan eventfd_signal(eventfd, 1); 1264*faeb7833SRoman Kagan return HV_STATUS_SUCCESS; 1265*faeb7833SRoman Kagan } 1266*faeb7833SRoman Kagan 1267e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu) 1268e83d5887SAndrey Smetanin { 1269e83d5887SAndrey Smetanin u64 param, ingpa, outgpa, ret; 1270e83d5887SAndrey Smetanin uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; 1271e83d5887SAndrey Smetanin bool fast, longmode; 1272e83d5887SAndrey Smetanin 1273e83d5887SAndrey Smetanin /* 1274e83d5887SAndrey Smetanin * hypercall generates UD from non zero cpl and real mode 1275e83d5887SAndrey Smetanin * per HYPER-V spec 1276e83d5887SAndrey Smetanin */ 1277e83d5887SAndrey Smetanin if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { 1278e83d5887SAndrey Smetanin kvm_queue_exception(vcpu, UD_VECTOR); 12790d9c055eSAndrey Smetanin return 1; 1280e83d5887SAndrey Smetanin } 1281e83d5887SAndrey Smetanin 1282e83d5887SAndrey Smetanin longmode = is_64_bit_mode(vcpu); 1283e83d5887SAndrey Smetanin 1284e83d5887SAndrey Smetanin if (!longmode) { 1285e83d5887SAndrey Smetanin param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | 1286e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); 1287e83d5887SAndrey Smetanin ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | 1288e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); 1289e83d5887SAndrey Smetanin outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | 1290e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); 1291e83d5887SAndrey Smetanin } 1292e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64 1293e83d5887SAndrey Smetanin else { 1294e83d5887SAndrey Smetanin param = kvm_register_read(vcpu, VCPU_REGS_RCX); 1295e83d5887SAndrey Smetanin ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); 1296e83d5887SAndrey Smetanin outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); 1297e83d5887SAndrey Smetanin } 1298e83d5887SAndrey Smetanin #endif 1299e83d5887SAndrey Smetanin 1300e83d5887SAndrey Smetanin code = param & 0xffff; 1301e83d5887SAndrey Smetanin fast = (param >> 16) & 0x1; 1302e83d5887SAndrey Smetanin rep_cnt = (param >> 32) & 0xfff; 1303e83d5887SAndrey Smetanin rep_idx = (param >> 48) & 0xfff; 1304e83d5887SAndrey Smetanin 1305e83d5887SAndrey Smetanin trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); 1306e83d5887SAndrey Smetanin 1307b2fdc257SAndrey Smetanin /* Hypercall continuation is not supported yet */ 1308b2fdc257SAndrey Smetanin if (rep_cnt || rep_idx) { 1309b2fdc257SAndrey Smetanin res = HV_STATUS_INVALID_HYPERCALL_CODE; 1310b2fdc257SAndrey Smetanin goto set_result; 1311b2fdc257SAndrey Smetanin } 1312b2fdc257SAndrey Smetanin 1313e83d5887SAndrey Smetanin switch (code) { 13148ed6d767SAndrey Smetanin case HVCALL_NOTIFY_LONG_SPIN_WAIT: 1315de63ad4cSLongpeng(Mike) kvm_vcpu_on_spin(vcpu, true); 1316e83d5887SAndrey Smetanin break; 131783326e43SAndrey Smetanin case HVCALL_SIGNAL_EVENT: 1318*faeb7833SRoman Kagan res = kvm_hvcall_signal_event(vcpu, fast, ingpa); 1319*faeb7833SRoman Kagan if (res != HV_STATUS_INVALID_PORT_ID) 1320*faeb7833SRoman Kagan break; 1321*faeb7833SRoman Kagan /* maybe userspace knows this conn_id: fall through */ 1322*faeb7833SRoman Kagan case HVCALL_POST_MESSAGE: 1323a2b5c3c0SPaolo Bonzini /* don't bother userspace if it has no way to handle it */ 1324a2b5c3c0SPaolo Bonzini if (!vcpu_to_synic(vcpu)->active) { 1325a2b5c3c0SPaolo Bonzini res = HV_STATUS_INVALID_HYPERCALL_CODE; 1326a2b5c3c0SPaolo Bonzini break; 1327a2b5c3c0SPaolo Bonzini } 132883326e43SAndrey Smetanin vcpu->run->exit_reason = KVM_EXIT_HYPERV; 132983326e43SAndrey Smetanin vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; 133083326e43SAndrey Smetanin vcpu->run->hyperv.u.hcall.input = param; 133183326e43SAndrey Smetanin vcpu->run->hyperv.u.hcall.params[0] = ingpa; 133283326e43SAndrey Smetanin vcpu->run->hyperv.u.hcall.params[1] = outgpa; 133383326e43SAndrey Smetanin vcpu->arch.complete_userspace_io = 133483326e43SAndrey Smetanin kvm_hv_hypercall_complete_userspace; 133583326e43SAndrey Smetanin return 0; 1336e83d5887SAndrey Smetanin default: 1337e83d5887SAndrey Smetanin res = HV_STATUS_INVALID_HYPERCALL_CODE; 1338e83d5887SAndrey Smetanin break; 1339e83d5887SAndrey Smetanin } 1340e83d5887SAndrey Smetanin 1341b2fdc257SAndrey Smetanin set_result: 1342e83d5887SAndrey Smetanin ret = res | (((u64)rep_done & 0xfff) << 32); 134383326e43SAndrey Smetanin kvm_hv_hypercall_set_result(vcpu, ret); 1344e83d5887SAndrey Smetanin return 1; 1345e83d5887SAndrey Smetanin } 1346cbc0236aSRoman Kagan 1347cbc0236aSRoman Kagan void kvm_hv_init_vm(struct kvm *kvm) 1348cbc0236aSRoman Kagan { 1349cbc0236aSRoman Kagan mutex_init(&kvm->arch.hyperv.hv_lock); 1350*faeb7833SRoman Kagan idr_init(&kvm->arch.hyperv.conn_to_evt); 1351cbc0236aSRoman Kagan } 1352cbc0236aSRoman Kagan 1353cbc0236aSRoman Kagan void kvm_hv_destroy_vm(struct kvm *kvm) 1354cbc0236aSRoman Kagan { 1355*faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 1356*faeb7833SRoman Kagan int i; 1357*faeb7833SRoman Kagan 1358*faeb7833SRoman Kagan idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i) 1359*faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 1360*faeb7833SRoman Kagan idr_destroy(&kvm->arch.hyperv.conn_to_evt); 1361*faeb7833SRoman Kagan } 1362*faeb7833SRoman Kagan 1363*faeb7833SRoman Kagan static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd) 1364*faeb7833SRoman Kagan { 1365*faeb7833SRoman Kagan struct kvm_hv *hv = &kvm->arch.hyperv; 1366*faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 1367*faeb7833SRoman Kagan int ret; 1368*faeb7833SRoman Kagan 1369*faeb7833SRoman Kagan eventfd = eventfd_ctx_fdget(fd); 1370*faeb7833SRoman Kagan if (IS_ERR(eventfd)) 1371*faeb7833SRoman Kagan return PTR_ERR(eventfd); 1372*faeb7833SRoman Kagan 1373*faeb7833SRoman Kagan mutex_lock(&hv->hv_lock); 1374*faeb7833SRoman Kagan ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1, 1375*faeb7833SRoman Kagan GFP_KERNEL); 1376*faeb7833SRoman Kagan mutex_unlock(&hv->hv_lock); 1377*faeb7833SRoman Kagan 1378*faeb7833SRoman Kagan if (ret >= 0) 1379*faeb7833SRoman Kagan return 0; 1380*faeb7833SRoman Kagan 1381*faeb7833SRoman Kagan if (ret == -ENOSPC) 1382*faeb7833SRoman Kagan ret = -EEXIST; 1383*faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 1384*faeb7833SRoman Kagan return ret; 1385*faeb7833SRoman Kagan } 1386*faeb7833SRoman Kagan 1387*faeb7833SRoman Kagan static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id) 1388*faeb7833SRoman Kagan { 1389*faeb7833SRoman Kagan struct kvm_hv *hv = &kvm->arch.hyperv; 1390*faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 1391*faeb7833SRoman Kagan 1392*faeb7833SRoman Kagan mutex_lock(&hv->hv_lock); 1393*faeb7833SRoman Kagan eventfd = idr_remove(&hv->conn_to_evt, conn_id); 1394*faeb7833SRoman Kagan mutex_unlock(&hv->hv_lock); 1395*faeb7833SRoman Kagan 1396*faeb7833SRoman Kagan if (!eventfd) 1397*faeb7833SRoman Kagan return -ENOENT; 1398*faeb7833SRoman Kagan 1399*faeb7833SRoman Kagan synchronize_srcu(&kvm->srcu); 1400*faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 1401*faeb7833SRoman Kagan return 0; 1402*faeb7833SRoman Kagan } 1403*faeb7833SRoman Kagan 1404*faeb7833SRoman Kagan int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args) 1405*faeb7833SRoman Kagan { 1406*faeb7833SRoman Kagan if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) || 1407*faeb7833SRoman Kagan (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK)) 1408*faeb7833SRoman Kagan return -EINVAL; 1409*faeb7833SRoman Kagan 1410*faeb7833SRoman Kagan if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN) 1411*faeb7833SRoman Kagan return kvm_hv_eventfd_deassign(kvm, args->conn_id); 1412*faeb7833SRoman Kagan return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd); 1413cbc0236aSRoman Kagan } 1414