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> 315c919412SAndrey Smetanin #include <asm/apicdef.h> 32e83d5887SAndrey Smetanin #include <trace/events/kvm.h> 33e83d5887SAndrey Smetanin 34e83d5887SAndrey Smetanin #include "trace.h" 35e83d5887SAndrey Smetanin 365c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) 375c919412SAndrey Smetanin { 385c919412SAndrey Smetanin return atomic64_read(&synic->sint[sint]); 395c919412SAndrey Smetanin } 405c919412SAndrey Smetanin 415c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value) 425c919412SAndrey Smetanin { 435c919412SAndrey Smetanin if (sint_value & HV_SYNIC_SINT_MASKED) 445c919412SAndrey Smetanin return -1; 455c919412SAndrey Smetanin return sint_value & HV_SYNIC_SINT_VECTOR_MASK; 465c919412SAndrey Smetanin } 475c919412SAndrey Smetanin 485c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, 495c919412SAndrey Smetanin int vector) 505c919412SAndrey Smetanin { 515c919412SAndrey Smetanin int i; 525c919412SAndrey Smetanin 535c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 545c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 555c919412SAndrey Smetanin return true; 565c919412SAndrey Smetanin } 575c919412SAndrey Smetanin return false; 585c919412SAndrey Smetanin } 595c919412SAndrey Smetanin 605c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, 615c919412SAndrey Smetanin int vector) 625c919412SAndrey Smetanin { 635c919412SAndrey Smetanin int i; 645c919412SAndrey Smetanin u64 sint_value; 655c919412SAndrey Smetanin 665c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 675c919412SAndrey Smetanin sint_value = synic_read_sint(synic, i); 685c919412SAndrey Smetanin if (synic_get_sint_vector(sint_value) == vector && 695c919412SAndrey Smetanin sint_value & HV_SYNIC_SINT_AUTO_EOI) 705c919412SAndrey Smetanin return true; 715c919412SAndrey Smetanin } 725c919412SAndrey Smetanin return false; 735c919412SAndrey Smetanin } 745c919412SAndrey Smetanin 755c919412SAndrey Smetanin static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data) 765c919412SAndrey Smetanin { 775c919412SAndrey Smetanin int vector; 785c919412SAndrey Smetanin 795c919412SAndrey Smetanin vector = data & HV_SYNIC_SINT_VECTOR_MASK; 805c919412SAndrey Smetanin if (vector < 16) 815c919412SAndrey Smetanin return 1; 825c919412SAndrey Smetanin /* 835c919412SAndrey Smetanin * Guest may configure multiple SINTs to use the same vector, so 845c919412SAndrey Smetanin * we maintain a bitmap of vectors handled by synic, and a 855c919412SAndrey Smetanin * bitmap of vectors with auto-eoi behavior. The bitmaps are 865c919412SAndrey Smetanin * updated here, and atomically queried on fast paths. 875c919412SAndrey Smetanin */ 885c919412SAndrey Smetanin 895c919412SAndrey Smetanin atomic64_set(&synic->sint[sint], data); 905c919412SAndrey Smetanin 915c919412SAndrey Smetanin if (synic_has_vector_connected(synic, vector)) 925c919412SAndrey Smetanin __set_bit(vector, synic->vec_bitmap); 935c919412SAndrey Smetanin else 945c919412SAndrey Smetanin __clear_bit(vector, synic->vec_bitmap); 955c919412SAndrey Smetanin 965c919412SAndrey Smetanin if (synic_has_vector_auto_eoi(synic, vector)) 975c919412SAndrey Smetanin __set_bit(vector, synic->auto_eoi_bitmap); 985c919412SAndrey Smetanin else 995c919412SAndrey Smetanin __clear_bit(vector, synic->auto_eoi_bitmap); 1005c919412SAndrey Smetanin 1015c919412SAndrey Smetanin /* Load SynIC vectors into EOI exit bitmap */ 1025c919412SAndrey Smetanin kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic)); 1035c919412SAndrey Smetanin return 0; 1045c919412SAndrey Smetanin } 1055c919412SAndrey Smetanin 1065c919412SAndrey Smetanin static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id) 1075c919412SAndrey Smetanin { 1085c919412SAndrey Smetanin struct kvm_vcpu *vcpu; 1095c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 1105c919412SAndrey Smetanin 1115c919412SAndrey Smetanin if (vcpu_id >= atomic_read(&kvm->online_vcpus)) 1125c919412SAndrey Smetanin return NULL; 1135c919412SAndrey Smetanin vcpu = kvm_get_vcpu(kvm, vcpu_id); 1145c919412SAndrey Smetanin if (!vcpu) 1155c919412SAndrey Smetanin return NULL; 1165c919412SAndrey Smetanin synic = vcpu_to_synic(vcpu); 1175c919412SAndrey Smetanin return (synic->active) ? synic : NULL; 1185c919412SAndrey Smetanin } 1195c919412SAndrey Smetanin 120765eaa0fSAndrey Smetanin static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic, 121765eaa0fSAndrey Smetanin u32 sint) 122765eaa0fSAndrey Smetanin { 123765eaa0fSAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 124765eaa0fSAndrey Smetanin struct page *page; 125765eaa0fSAndrey Smetanin gpa_t gpa; 126765eaa0fSAndrey Smetanin struct hv_message *msg; 127765eaa0fSAndrey Smetanin struct hv_message_page *msg_page; 128765eaa0fSAndrey Smetanin 129765eaa0fSAndrey Smetanin gpa = synic->msg_page & PAGE_MASK; 130765eaa0fSAndrey Smetanin page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); 131765eaa0fSAndrey Smetanin if (is_error_page(page)) { 132765eaa0fSAndrey Smetanin vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n", 133765eaa0fSAndrey Smetanin gpa); 134765eaa0fSAndrey Smetanin return; 135765eaa0fSAndrey Smetanin } 136765eaa0fSAndrey Smetanin msg_page = kmap_atomic(page); 137765eaa0fSAndrey Smetanin 138765eaa0fSAndrey Smetanin msg = &msg_page->sint_message[sint]; 139765eaa0fSAndrey Smetanin msg->header.message_flags.msg_pending = 0; 140765eaa0fSAndrey Smetanin 141765eaa0fSAndrey Smetanin kunmap_atomic(msg_page); 142765eaa0fSAndrey Smetanin kvm_release_page_dirty(page); 143765eaa0fSAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 144765eaa0fSAndrey Smetanin } 145765eaa0fSAndrey Smetanin 1465c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) 1475c919412SAndrey Smetanin { 1485c919412SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 149765eaa0fSAndrey Smetanin struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); 1501f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 1511f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 1521f4b34f8SAndrey Smetanin int gsi, idx, stimers_pending; 1535c919412SAndrey Smetanin 1545c919412SAndrey Smetanin vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint); 1555c919412SAndrey Smetanin 156765eaa0fSAndrey Smetanin if (synic->msg_page & HV_SYNIC_SIMP_ENABLE) 157765eaa0fSAndrey Smetanin synic_clear_sint_msg_pending(synic, sint); 158765eaa0fSAndrey Smetanin 1591f4b34f8SAndrey Smetanin /* Try to deliver pending Hyper-V SynIC timers messages */ 1601f4b34f8SAndrey Smetanin stimers_pending = 0; 1611f4b34f8SAndrey Smetanin for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { 1621f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[idx]; 1631f4b34f8SAndrey Smetanin if (stimer->msg_pending && 1641f4b34f8SAndrey Smetanin (stimer->config & HV_STIMER_ENABLE) && 1651f4b34f8SAndrey Smetanin HV_STIMER_SINT(stimer->config) == sint) { 1661f4b34f8SAndrey Smetanin set_bit(stimer->index, 1671f4b34f8SAndrey Smetanin hv_vcpu->stimer_pending_bitmap); 1681f4b34f8SAndrey Smetanin stimers_pending++; 1691f4b34f8SAndrey Smetanin } 1701f4b34f8SAndrey Smetanin } 1711f4b34f8SAndrey Smetanin if (stimers_pending) 1721f4b34f8SAndrey Smetanin kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 1731f4b34f8SAndrey Smetanin 1745c919412SAndrey Smetanin idx = srcu_read_lock(&kvm->irq_srcu); 1751f4b34f8SAndrey Smetanin gsi = atomic_read(&synic->sint_to_gsi[sint]); 1765c919412SAndrey Smetanin if (gsi != -1) 1775c919412SAndrey Smetanin kvm_notify_acked_gsi(kvm, gsi); 1785c919412SAndrey Smetanin srcu_read_unlock(&kvm->irq_srcu, idx); 1795c919412SAndrey Smetanin } 1805c919412SAndrey Smetanin 181db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) 182db397571SAndrey Smetanin { 183db397571SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 184db397571SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; 185db397571SAndrey Smetanin 186db397571SAndrey Smetanin hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; 187db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msr = msr; 188db397571SAndrey Smetanin hv_vcpu->exit.u.synic.control = synic->control; 189db397571SAndrey Smetanin hv_vcpu->exit.u.synic.evt_page = synic->evt_page; 190db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msg_page = synic->msg_page; 191db397571SAndrey Smetanin 192db397571SAndrey Smetanin kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 193db397571SAndrey Smetanin } 194db397571SAndrey Smetanin 1955c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, 1965c919412SAndrey Smetanin u32 msr, u64 data, bool host) 1975c919412SAndrey Smetanin { 1985c919412SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 1995c919412SAndrey Smetanin int ret; 2005c919412SAndrey Smetanin 2015c919412SAndrey Smetanin if (!synic->active) 2025c919412SAndrey Smetanin return 1; 2035c919412SAndrey Smetanin 2045c919412SAndrey Smetanin vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n", 2055c919412SAndrey Smetanin msr, data, host); 2065c919412SAndrey Smetanin ret = 0; 2075c919412SAndrey Smetanin switch (msr) { 2085c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 2095c919412SAndrey Smetanin synic->control = data; 210db397571SAndrey Smetanin if (!host) 211db397571SAndrey Smetanin synic_exit(synic, msr); 2125c919412SAndrey Smetanin break; 2135c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 2145c919412SAndrey Smetanin if (!host) { 2155c919412SAndrey Smetanin ret = 1; 2165c919412SAndrey Smetanin break; 2175c919412SAndrey Smetanin } 2185c919412SAndrey Smetanin synic->version = data; 2195c919412SAndrey Smetanin break; 2205c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 2215c919412SAndrey Smetanin if (data & HV_SYNIC_SIEFP_ENABLE) 2225c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2235c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2245c919412SAndrey Smetanin ret = 1; 2255c919412SAndrey Smetanin break; 2265c919412SAndrey Smetanin } 2275c919412SAndrey Smetanin synic->evt_page = data; 228db397571SAndrey Smetanin if (!host) 229db397571SAndrey Smetanin synic_exit(synic, msr); 2305c919412SAndrey Smetanin break; 2315c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 2325c919412SAndrey Smetanin if (data & HV_SYNIC_SIMP_ENABLE) 2335c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2345c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2355c919412SAndrey Smetanin ret = 1; 2365c919412SAndrey Smetanin break; 2375c919412SAndrey Smetanin } 2385c919412SAndrey Smetanin synic->msg_page = data; 239db397571SAndrey Smetanin if (!host) 240db397571SAndrey Smetanin synic_exit(synic, msr); 2415c919412SAndrey Smetanin break; 2425c919412SAndrey Smetanin case HV_X64_MSR_EOM: { 2435c919412SAndrey Smetanin int i; 2445c919412SAndrey Smetanin 2455c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 2465c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 2475c919412SAndrey Smetanin break; 2485c919412SAndrey Smetanin } 2495c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 2505c919412SAndrey Smetanin ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data); 2515c919412SAndrey Smetanin break; 2525c919412SAndrey Smetanin default: 2535c919412SAndrey Smetanin ret = 1; 2545c919412SAndrey Smetanin break; 2555c919412SAndrey Smetanin } 2565c919412SAndrey Smetanin return ret; 2575c919412SAndrey Smetanin } 2585c919412SAndrey Smetanin 2595c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata) 2605c919412SAndrey Smetanin { 2615c919412SAndrey Smetanin int ret; 2625c919412SAndrey Smetanin 2635c919412SAndrey Smetanin if (!synic->active) 2645c919412SAndrey Smetanin return 1; 2655c919412SAndrey Smetanin 2665c919412SAndrey Smetanin ret = 0; 2675c919412SAndrey Smetanin switch (msr) { 2685c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 2695c919412SAndrey Smetanin *pdata = synic->control; 2705c919412SAndrey Smetanin break; 2715c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 2725c919412SAndrey Smetanin *pdata = synic->version; 2735c919412SAndrey Smetanin break; 2745c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 2755c919412SAndrey Smetanin *pdata = synic->evt_page; 2765c919412SAndrey Smetanin break; 2775c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 2785c919412SAndrey Smetanin *pdata = synic->msg_page; 2795c919412SAndrey Smetanin break; 2805c919412SAndrey Smetanin case HV_X64_MSR_EOM: 2815c919412SAndrey Smetanin *pdata = 0; 2825c919412SAndrey Smetanin break; 2835c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 2845c919412SAndrey Smetanin *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); 2855c919412SAndrey Smetanin break; 2865c919412SAndrey Smetanin default: 2875c919412SAndrey Smetanin ret = 1; 2885c919412SAndrey Smetanin break; 2895c919412SAndrey Smetanin } 2905c919412SAndrey Smetanin return ret; 2915c919412SAndrey Smetanin } 2925c919412SAndrey Smetanin 2935c919412SAndrey Smetanin int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) 2945c919412SAndrey Smetanin { 2955c919412SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 2965c919412SAndrey Smetanin struct kvm_lapic_irq irq; 2975c919412SAndrey Smetanin int ret, vector; 2985c919412SAndrey Smetanin 2995c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint)) 3005c919412SAndrey Smetanin return -EINVAL; 3015c919412SAndrey Smetanin 3025c919412SAndrey Smetanin vector = synic_get_sint_vector(synic_read_sint(synic, sint)); 3035c919412SAndrey Smetanin if (vector < 0) 3045c919412SAndrey Smetanin return -ENOENT; 3055c919412SAndrey Smetanin 3065c919412SAndrey Smetanin memset(&irq, 0, sizeof(irq)); 3075c919412SAndrey Smetanin irq.dest_id = kvm_apic_id(vcpu->arch.apic); 3085c919412SAndrey Smetanin irq.dest_mode = APIC_DEST_PHYSICAL; 3095c919412SAndrey Smetanin irq.delivery_mode = APIC_DM_FIXED; 3105c919412SAndrey Smetanin irq.vector = vector; 3115c919412SAndrey Smetanin irq.level = 1; 3125c919412SAndrey Smetanin 3135c919412SAndrey Smetanin ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL); 3145c919412SAndrey Smetanin vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret); 3155c919412SAndrey Smetanin return ret; 3165c919412SAndrey Smetanin } 3175c919412SAndrey Smetanin 3185c919412SAndrey Smetanin int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint) 3195c919412SAndrey Smetanin { 3205c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 3215c919412SAndrey Smetanin 3225c919412SAndrey Smetanin synic = synic_get(kvm, vcpu_id); 3235c919412SAndrey Smetanin if (!synic) 3245c919412SAndrey Smetanin return -EINVAL; 3255c919412SAndrey Smetanin 3265c919412SAndrey Smetanin return synic_set_irq(synic, sint); 3275c919412SAndrey Smetanin } 3285c919412SAndrey Smetanin 3295c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) 3305c919412SAndrey Smetanin { 3315c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu); 3325c919412SAndrey Smetanin int i; 3335c919412SAndrey Smetanin 3345c919412SAndrey Smetanin vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector); 3355c919412SAndrey Smetanin 3365c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 3375c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 3385c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 3395c919412SAndrey Smetanin } 3405c919412SAndrey Smetanin 3415c919412SAndrey Smetanin static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi) 3425c919412SAndrey Smetanin { 3435c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 3445c919412SAndrey Smetanin 3455c919412SAndrey Smetanin synic = synic_get(kvm, vcpu_id); 3465c919412SAndrey Smetanin if (!synic) 3475c919412SAndrey Smetanin return -EINVAL; 3485c919412SAndrey Smetanin 3495c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) 3505c919412SAndrey Smetanin return -EINVAL; 3515c919412SAndrey Smetanin 3525c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[sint], gsi); 3535c919412SAndrey Smetanin return 0; 3545c919412SAndrey Smetanin } 3555c919412SAndrey Smetanin 3565c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm) 3575c919412SAndrey Smetanin { 3585c919412SAndrey Smetanin struct kvm_irq_routing_table *irq_rt; 3595c919412SAndrey Smetanin struct kvm_kernel_irq_routing_entry *e; 3605c919412SAndrey Smetanin u32 gsi; 3615c919412SAndrey Smetanin 3625c919412SAndrey Smetanin irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, 3635c919412SAndrey Smetanin lockdep_is_held(&kvm->irq_lock)); 3645c919412SAndrey Smetanin 3655c919412SAndrey Smetanin for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { 3665c919412SAndrey Smetanin hlist_for_each_entry(e, &irq_rt->map[gsi], link) { 3675c919412SAndrey Smetanin if (e->type == KVM_IRQ_ROUTING_HV_SINT) 3685c919412SAndrey Smetanin kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, 3695c919412SAndrey Smetanin e->hv_sint.sint, gsi); 3705c919412SAndrey Smetanin } 3715c919412SAndrey Smetanin } 3725c919412SAndrey Smetanin } 3735c919412SAndrey Smetanin 3745c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic) 3755c919412SAndrey Smetanin { 3765c919412SAndrey Smetanin int i; 3775c919412SAndrey Smetanin 3785c919412SAndrey Smetanin memset(synic, 0, sizeof(*synic)); 3795c919412SAndrey Smetanin synic->version = HV_SYNIC_VERSION_1; 3805c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 3815c919412SAndrey Smetanin atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); 3825c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[i], -1); 3835c919412SAndrey Smetanin } 3845c919412SAndrey Smetanin } 3855c919412SAndrey Smetanin 38693bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm) 38793bf4172SAndrey Smetanin { 38893bf4172SAndrey Smetanin return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100); 38993bf4172SAndrey Smetanin } 39093bf4172SAndrey Smetanin 3911f4b34f8SAndrey Smetanin static void stimer_mark_expired(struct kvm_vcpu_hv_stimer *stimer, 3921f4b34f8SAndrey Smetanin bool vcpu_kick) 3931f4b34f8SAndrey Smetanin { 3941f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 3951f4b34f8SAndrey Smetanin 3961f4b34f8SAndrey Smetanin set_bit(stimer->index, 3971f4b34f8SAndrey Smetanin vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); 3981f4b34f8SAndrey Smetanin kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 3991f4b34f8SAndrey Smetanin if (vcpu_kick) 4001f4b34f8SAndrey Smetanin kvm_vcpu_kick(vcpu); 4011f4b34f8SAndrey Smetanin } 4021f4b34f8SAndrey Smetanin 4031f4b34f8SAndrey Smetanin static void stimer_stop(struct kvm_vcpu_hv_stimer *stimer) 4041f4b34f8SAndrey Smetanin { 4051f4b34f8SAndrey Smetanin hrtimer_cancel(&stimer->timer); 4061f4b34f8SAndrey Smetanin } 4071f4b34f8SAndrey Smetanin 4081f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) 4091f4b34f8SAndrey Smetanin { 4101f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 4111f4b34f8SAndrey Smetanin 4121f4b34f8SAndrey Smetanin stimer_stop(stimer); 4131f4b34f8SAndrey Smetanin clear_bit(stimer->index, 4141f4b34f8SAndrey Smetanin vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap); 4151f4b34f8SAndrey Smetanin stimer->msg_pending = false; 4161f4b34f8SAndrey Smetanin } 4171f4b34f8SAndrey Smetanin 4181f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) 4191f4b34f8SAndrey Smetanin { 4201f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 4211f4b34f8SAndrey Smetanin 4221f4b34f8SAndrey Smetanin stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); 4231f4b34f8SAndrey Smetanin stimer_mark_expired(stimer, true); 4241f4b34f8SAndrey Smetanin 4251f4b34f8SAndrey Smetanin return HRTIMER_NORESTART; 4261f4b34f8SAndrey Smetanin } 4271f4b34f8SAndrey Smetanin 4281f4b34f8SAndrey Smetanin static void stimer_restart(struct kvm_vcpu_hv_stimer *stimer) 4291f4b34f8SAndrey Smetanin { 4301f4b34f8SAndrey Smetanin u64 time_now; 4311f4b34f8SAndrey Smetanin ktime_t ktime_now; 4321f4b34f8SAndrey Smetanin u64 remainder; 4331f4b34f8SAndrey Smetanin 4341f4b34f8SAndrey Smetanin time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); 4351f4b34f8SAndrey Smetanin ktime_now = ktime_get(); 4361f4b34f8SAndrey Smetanin 4371f4b34f8SAndrey Smetanin div64_u64_rem(time_now - stimer->exp_time, stimer->count, &remainder); 4381f4b34f8SAndrey Smetanin stimer->exp_time = time_now + (stimer->count - remainder); 4391f4b34f8SAndrey Smetanin 4401f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 4411f4b34f8SAndrey Smetanin ktime_add_ns(ktime_now, 4421f4b34f8SAndrey Smetanin 100 * (stimer->exp_time - time_now)), 4431f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 4441f4b34f8SAndrey Smetanin } 4451f4b34f8SAndrey Smetanin 4461f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) 4471f4b34f8SAndrey Smetanin { 4481f4b34f8SAndrey Smetanin u64 time_now; 4491f4b34f8SAndrey Smetanin ktime_t ktime_now; 4501f4b34f8SAndrey Smetanin 4511f4b34f8SAndrey Smetanin time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm); 4521f4b34f8SAndrey Smetanin ktime_now = ktime_get(); 4531f4b34f8SAndrey Smetanin 4541f4b34f8SAndrey Smetanin if (stimer->config & HV_STIMER_PERIODIC) { 4551f4b34f8SAndrey Smetanin if (stimer->count == 0) 4561f4b34f8SAndrey Smetanin return -EINVAL; 4571f4b34f8SAndrey Smetanin 4581f4b34f8SAndrey Smetanin stimer->exp_time = time_now + stimer->count; 4591f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 4601f4b34f8SAndrey Smetanin ktime_add_ns(ktime_now, 100 * stimer->count), 4611f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 4621f4b34f8SAndrey Smetanin return 0; 4631f4b34f8SAndrey Smetanin } 4641f4b34f8SAndrey Smetanin stimer->exp_time = stimer->count; 4651f4b34f8SAndrey Smetanin if (time_now >= stimer->count) { 4661f4b34f8SAndrey Smetanin /* 4671f4b34f8SAndrey Smetanin * Expire timer according to Hypervisor Top-Level Functional 4681f4b34f8SAndrey Smetanin * specification v4(15.3.1): 4691f4b34f8SAndrey Smetanin * "If a one shot is enabled and the specified count is in 4701f4b34f8SAndrey Smetanin * the past, it will expire immediately." 4711f4b34f8SAndrey Smetanin */ 4721f4b34f8SAndrey Smetanin stimer_mark_expired(stimer, false); 4731f4b34f8SAndrey Smetanin return 0; 4741f4b34f8SAndrey Smetanin } 4751f4b34f8SAndrey Smetanin 4761f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 4771f4b34f8SAndrey Smetanin ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), 4781f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 4791f4b34f8SAndrey Smetanin return 0; 4801f4b34f8SAndrey Smetanin } 4811f4b34f8SAndrey Smetanin 4821f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, 4831f4b34f8SAndrey Smetanin bool host) 4841f4b34f8SAndrey Smetanin { 4851f4b34f8SAndrey Smetanin if (stimer->count == 0 || HV_STIMER_SINT(config) == 0) 4861f4b34f8SAndrey Smetanin config &= ~HV_STIMER_ENABLE; 4871f4b34f8SAndrey Smetanin stimer->config = config; 4881f4b34f8SAndrey Smetanin stimer_cleanup(stimer); 4891f4b34f8SAndrey Smetanin if (stimer->config & HV_STIMER_ENABLE) 4901f4b34f8SAndrey Smetanin if (stimer_start(stimer)) 4911f4b34f8SAndrey Smetanin return 1; 4921f4b34f8SAndrey Smetanin return 0; 4931f4b34f8SAndrey Smetanin } 4941f4b34f8SAndrey Smetanin 4951f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, 4961f4b34f8SAndrey Smetanin bool host) 4971f4b34f8SAndrey Smetanin { 4981f4b34f8SAndrey Smetanin stimer->count = count; 4991f4b34f8SAndrey Smetanin 5001f4b34f8SAndrey Smetanin stimer_cleanup(stimer); 5011f4b34f8SAndrey Smetanin if (stimer->count == 0) 5021f4b34f8SAndrey Smetanin stimer->config &= ~HV_STIMER_ENABLE; 5031f4b34f8SAndrey Smetanin else if (stimer->config & HV_STIMER_AUTOENABLE) { 5041f4b34f8SAndrey Smetanin stimer->config |= HV_STIMER_ENABLE; 5051f4b34f8SAndrey Smetanin if (stimer_start(stimer)) 5061f4b34f8SAndrey Smetanin return 1; 5071f4b34f8SAndrey Smetanin } 5081f4b34f8SAndrey Smetanin 5091f4b34f8SAndrey Smetanin return 0; 5101f4b34f8SAndrey Smetanin } 5111f4b34f8SAndrey Smetanin 5121f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) 5131f4b34f8SAndrey Smetanin { 5141f4b34f8SAndrey Smetanin *pconfig = stimer->config; 5151f4b34f8SAndrey Smetanin return 0; 5161f4b34f8SAndrey Smetanin } 5171f4b34f8SAndrey Smetanin 5181f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) 5191f4b34f8SAndrey Smetanin { 5201f4b34f8SAndrey Smetanin *pcount = stimer->count; 5211f4b34f8SAndrey Smetanin return 0; 5221f4b34f8SAndrey Smetanin } 5231f4b34f8SAndrey Smetanin 5241f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, 5251f4b34f8SAndrey Smetanin struct hv_message *src_msg) 5261f4b34f8SAndrey Smetanin { 5271f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = synic_to_vcpu(synic); 5281f4b34f8SAndrey Smetanin struct page *page; 5291f4b34f8SAndrey Smetanin gpa_t gpa; 5301f4b34f8SAndrey Smetanin struct hv_message *dst_msg; 5311f4b34f8SAndrey Smetanin int r; 5321f4b34f8SAndrey Smetanin struct hv_message_page *msg_page; 5331f4b34f8SAndrey Smetanin 5341f4b34f8SAndrey Smetanin if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) 5351f4b34f8SAndrey Smetanin return -ENOENT; 5361f4b34f8SAndrey Smetanin 5371f4b34f8SAndrey Smetanin gpa = synic->msg_page & PAGE_MASK; 5381f4b34f8SAndrey Smetanin page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT); 5391f4b34f8SAndrey Smetanin if (is_error_page(page)) 5401f4b34f8SAndrey Smetanin return -EFAULT; 5411f4b34f8SAndrey Smetanin 5421f4b34f8SAndrey Smetanin msg_page = kmap_atomic(page); 5431f4b34f8SAndrey Smetanin dst_msg = &msg_page->sint_message[sint]; 5441f4b34f8SAndrey Smetanin if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE, 5451f4b34f8SAndrey Smetanin src_msg->header.message_type) != HVMSG_NONE) { 5461f4b34f8SAndrey Smetanin dst_msg->header.message_flags.msg_pending = 1; 5471f4b34f8SAndrey Smetanin r = -EAGAIN; 5481f4b34f8SAndrey Smetanin } else { 5491f4b34f8SAndrey Smetanin memcpy(&dst_msg->u.payload, &src_msg->u.payload, 5501f4b34f8SAndrey Smetanin src_msg->header.payload_size); 5511f4b34f8SAndrey Smetanin dst_msg->header.message_type = src_msg->header.message_type; 5521f4b34f8SAndrey Smetanin dst_msg->header.payload_size = src_msg->header.payload_size; 5531f4b34f8SAndrey Smetanin r = synic_set_irq(synic, sint); 5541f4b34f8SAndrey Smetanin if (r >= 1) 5551f4b34f8SAndrey Smetanin r = 0; 5561f4b34f8SAndrey Smetanin else if (r == 0) 5571f4b34f8SAndrey Smetanin r = -EFAULT; 5581f4b34f8SAndrey Smetanin } 5591f4b34f8SAndrey Smetanin kunmap_atomic(msg_page); 5601f4b34f8SAndrey Smetanin kvm_release_page_dirty(page); 5611f4b34f8SAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); 5621f4b34f8SAndrey Smetanin return r; 5631f4b34f8SAndrey Smetanin } 5641f4b34f8SAndrey Smetanin 5651f4b34f8SAndrey Smetanin static void stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) 5661f4b34f8SAndrey Smetanin { 5671f4b34f8SAndrey Smetanin struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer); 5681f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 5691f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 5701f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 5711f4b34f8SAndrey Smetanin int r; 5721f4b34f8SAndrey Smetanin 5731f4b34f8SAndrey Smetanin stimer->msg_pending = true; 5741f4b34f8SAndrey Smetanin payload->expiration_time = stimer->exp_time; 5751f4b34f8SAndrey Smetanin payload->delivery_time = get_time_ref_counter(vcpu->kvm); 5761f4b34f8SAndrey Smetanin r = synic_deliver_msg(vcpu_to_synic(vcpu), 5771f4b34f8SAndrey Smetanin HV_STIMER_SINT(stimer->config), msg); 5781f4b34f8SAndrey Smetanin if (!r) 5791f4b34f8SAndrey Smetanin stimer->msg_pending = false; 5801f4b34f8SAndrey Smetanin } 5811f4b34f8SAndrey Smetanin 5821f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) 5831f4b34f8SAndrey Smetanin { 5841f4b34f8SAndrey Smetanin stimer_send_msg(stimer); 5851f4b34f8SAndrey Smetanin if (!(stimer->config & HV_STIMER_PERIODIC)) 586*1ac1b65aSAndrey Smetanin stimer->config &= ~HV_STIMER_ENABLE; 5871f4b34f8SAndrey Smetanin else 5881f4b34f8SAndrey Smetanin stimer_restart(stimer); 5891f4b34f8SAndrey Smetanin } 5901f4b34f8SAndrey Smetanin 5911f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) 5921f4b34f8SAndrey Smetanin { 5931f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 5941f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 5951f4b34f8SAndrey Smetanin u64 time_now; 5961f4b34f8SAndrey Smetanin int i; 5971f4b34f8SAndrey Smetanin 5981f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 5991f4b34f8SAndrey Smetanin if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { 6001f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[i]; 6011f4b34f8SAndrey Smetanin if (stimer->config & HV_STIMER_ENABLE) { 6021f4b34f8SAndrey Smetanin time_now = get_time_ref_counter(vcpu->kvm); 6031f4b34f8SAndrey Smetanin if (time_now >= stimer->exp_time) 6041f4b34f8SAndrey Smetanin stimer_expiration(stimer); 6051f4b34f8SAndrey Smetanin } 6061f4b34f8SAndrey Smetanin } 6071f4b34f8SAndrey Smetanin } 6081f4b34f8SAndrey Smetanin 6091f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) 6101f4b34f8SAndrey Smetanin { 6111f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 6121f4b34f8SAndrey Smetanin int i; 6131f4b34f8SAndrey Smetanin 6141f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 6151f4b34f8SAndrey Smetanin stimer_cleanup(&hv_vcpu->stimer[i]); 6161f4b34f8SAndrey Smetanin } 6171f4b34f8SAndrey Smetanin 6181f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) 6191f4b34f8SAndrey Smetanin { 6201f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 6211f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 6221f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 6231f4b34f8SAndrey Smetanin 6241f4b34f8SAndrey Smetanin memset(&msg->header, 0, sizeof(msg->header)); 6251f4b34f8SAndrey Smetanin msg->header.message_type = HVMSG_TIMER_EXPIRED; 6261f4b34f8SAndrey Smetanin msg->header.payload_size = sizeof(*payload); 6271f4b34f8SAndrey Smetanin 6281f4b34f8SAndrey Smetanin payload->timer_index = stimer->index; 6291f4b34f8SAndrey Smetanin payload->expiration_time = 0; 6301f4b34f8SAndrey Smetanin payload->delivery_time = 0; 6311f4b34f8SAndrey Smetanin } 6321f4b34f8SAndrey Smetanin 6331f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) 6341f4b34f8SAndrey Smetanin { 6351f4b34f8SAndrey Smetanin memset(stimer, 0, sizeof(*stimer)); 6361f4b34f8SAndrey Smetanin stimer->index = timer_index; 6371f4b34f8SAndrey Smetanin hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 6381f4b34f8SAndrey Smetanin stimer->timer.function = stimer_timer_callback; 6391f4b34f8SAndrey Smetanin stimer_prepare_msg(stimer); 6401f4b34f8SAndrey Smetanin } 6411f4b34f8SAndrey Smetanin 6425c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) 6435c919412SAndrey Smetanin { 6441f4b34f8SAndrey Smetanin struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu); 6451f4b34f8SAndrey Smetanin int i; 6461f4b34f8SAndrey Smetanin 6471f4b34f8SAndrey Smetanin synic_init(&hv_vcpu->synic); 6481f4b34f8SAndrey Smetanin 6491f4b34f8SAndrey Smetanin bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); 6501f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 6511f4b34f8SAndrey Smetanin stimer_init(&hv_vcpu->stimer[i], i); 6525c919412SAndrey Smetanin } 6535c919412SAndrey Smetanin 6545c919412SAndrey Smetanin int kvm_hv_activate_synic(struct kvm_vcpu *vcpu) 6555c919412SAndrey Smetanin { 6565c919412SAndrey Smetanin /* 6575c919412SAndrey Smetanin * Hyper-V SynIC auto EOI SINT's are 6585c919412SAndrey Smetanin * not compatible with APICV, so deactivate APICV 6595c919412SAndrey Smetanin */ 6605c919412SAndrey Smetanin kvm_vcpu_deactivate_apicv(vcpu); 6615c919412SAndrey Smetanin vcpu_to_synic(vcpu)->active = true; 6625c919412SAndrey Smetanin return 0; 6635c919412SAndrey Smetanin } 6645c919412SAndrey Smetanin 665e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr) 666e83d5887SAndrey Smetanin { 667e83d5887SAndrey Smetanin bool r = false; 668e83d5887SAndrey Smetanin 669e83d5887SAndrey Smetanin switch (msr) { 670e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 671e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 672e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 673e83d5887SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 674e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 675e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 676e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 677e83d5887SAndrey Smetanin r = true; 678e83d5887SAndrey Smetanin break; 679e83d5887SAndrey Smetanin } 680e83d5887SAndrey Smetanin 681e83d5887SAndrey Smetanin return r; 682e83d5887SAndrey Smetanin } 683e83d5887SAndrey Smetanin 684e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu, 685e7d9513bSAndrey Smetanin u32 index, u64 *pdata) 686e7d9513bSAndrey Smetanin { 687e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 688e7d9513bSAndrey Smetanin 689e7d9513bSAndrey Smetanin if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) 690e7d9513bSAndrey Smetanin return -EINVAL; 691e7d9513bSAndrey Smetanin 692e7d9513bSAndrey Smetanin *pdata = hv->hv_crash_param[index]; 693e7d9513bSAndrey Smetanin return 0; 694e7d9513bSAndrey Smetanin } 695e7d9513bSAndrey Smetanin 696e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata) 697e7d9513bSAndrey Smetanin { 698e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 699e7d9513bSAndrey Smetanin 700e7d9513bSAndrey Smetanin *pdata = hv->hv_crash_ctl; 701e7d9513bSAndrey Smetanin return 0; 702e7d9513bSAndrey Smetanin } 703e7d9513bSAndrey Smetanin 704e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host) 705e7d9513bSAndrey Smetanin { 706e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 707e7d9513bSAndrey Smetanin 708e7d9513bSAndrey Smetanin if (host) 709e7d9513bSAndrey Smetanin hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY; 710e7d9513bSAndrey Smetanin 711e7d9513bSAndrey Smetanin if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) { 712e7d9513bSAndrey Smetanin 713e7d9513bSAndrey Smetanin vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", 714e7d9513bSAndrey Smetanin hv->hv_crash_param[0], 715e7d9513bSAndrey Smetanin hv->hv_crash_param[1], 716e7d9513bSAndrey Smetanin hv->hv_crash_param[2], 717e7d9513bSAndrey Smetanin hv->hv_crash_param[3], 718e7d9513bSAndrey Smetanin hv->hv_crash_param[4]); 719e7d9513bSAndrey Smetanin 720e7d9513bSAndrey Smetanin /* Send notification about crash to user space */ 721e7d9513bSAndrey Smetanin kvm_make_request(KVM_REQ_HV_CRASH, vcpu); 722e7d9513bSAndrey Smetanin } 723e7d9513bSAndrey Smetanin 724e7d9513bSAndrey Smetanin return 0; 725e7d9513bSAndrey Smetanin } 726e7d9513bSAndrey Smetanin 727e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu, 728e7d9513bSAndrey Smetanin u32 index, u64 data) 729e7d9513bSAndrey Smetanin { 730e7d9513bSAndrey Smetanin struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; 731e7d9513bSAndrey Smetanin 732e7d9513bSAndrey Smetanin if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param))) 733e7d9513bSAndrey Smetanin return -EINVAL; 734e7d9513bSAndrey Smetanin 735e7d9513bSAndrey Smetanin hv->hv_crash_param[index] = data; 736e7d9513bSAndrey Smetanin return 0; 737e7d9513bSAndrey Smetanin } 738e7d9513bSAndrey Smetanin 739e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, 740e7d9513bSAndrey Smetanin bool host) 741e83d5887SAndrey Smetanin { 742e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 743e83d5887SAndrey Smetanin struct kvm_hv *hv = &kvm->arch.hyperv; 744e83d5887SAndrey Smetanin 745e83d5887SAndrey Smetanin switch (msr) { 746e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 747e83d5887SAndrey Smetanin hv->hv_guest_os_id = data; 748e83d5887SAndrey Smetanin /* setting guest os id to zero disables hypercall page */ 749e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 750e83d5887SAndrey Smetanin hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; 751e83d5887SAndrey Smetanin break; 752e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: { 753e83d5887SAndrey Smetanin u64 gfn; 754e83d5887SAndrey Smetanin unsigned long addr; 755e83d5887SAndrey Smetanin u8 instructions[4]; 756e83d5887SAndrey Smetanin 757e83d5887SAndrey Smetanin /* if guest os id is not set hypercall should remain disabled */ 758e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 759e83d5887SAndrey Smetanin break; 760e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { 761e83d5887SAndrey Smetanin hv->hv_hypercall = data; 762e83d5887SAndrey Smetanin break; 763e83d5887SAndrey Smetanin } 764e83d5887SAndrey Smetanin gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT; 765e83d5887SAndrey Smetanin addr = gfn_to_hva(kvm, gfn); 766e83d5887SAndrey Smetanin if (kvm_is_error_hva(addr)) 767e83d5887SAndrey Smetanin return 1; 768e83d5887SAndrey Smetanin kvm_x86_ops->patch_hypercall(vcpu, instructions); 769e83d5887SAndrey Smetanin ((unsigned char *)instructions)[3] = 0xc3; /* ret */ 770e83d5887SAndrey Smetanin if (__copy_to_user((void __user *)addr, instructions, 4)) 771e83d5887SAndrey Smetanin return 1; 772e83d5887SAndrey Smetanin hv->hv_hypercall = data; 773e83d5887SAndrey Smetanin mark_page_dirty(kvm, gfn); 774e83d5887SAndrey Smetanin break; 775e83d5887SAndrey Smetanin } 776e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: { 777e83d5887SAndrey Smetanin u64 gfn; 778e83d5887SAndrey Smetanin HV_REFERENCE_TSC_PAGE tsc_ref; 779e83d5887SAndrey Smetanin 780e83d5887SAndrey Smetanin memset(&tsc_ref, 0, sizeof(tsc_ref)); 781e83d5887SAndrey Smetanin hv->hv_tsc_page = data; 782e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 783e83d5887SAndrey Smetanin break; 784e83d5887SAndrey Smetanin gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; 785e83d5887SAndrey Smetanin if (kvm_write_guest( 786e83d5887SAndrey Smetanin kvm, 787e83d5887SAndrey Smetanin gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT, 788e83d5887SAndrey Smetanin &tsc_ref, sizeof(tsc_ref))) 789e83d5887SAndrey Smetanin return 1; 790e83d5887SAndrey Smetanin mark_page_dirty(kvm, gfn); 791e83d5887SAndrey Smetanin break; 792e83d5887SAndrey Smetanin } 793e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 794e7d9513bSAndrey Smetanin return kvm_hv_msr_set_crash_data(vcpu, 795e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 796e7d9513bSAndrey Smetanin data); 797e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 798e7d9513bSAndrey Smetanin return kvm_hv_msr_set_crash_ctl(vcpu, data, host); 799e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 800e516cebbSAndrey Smetanin if (data == 1) { 801e516cebbSAndrey Smetanin vcpu_debug(vcpu, "hyper-v reset requested\n"); 802e516cebbSAndrey Smetanin kvm_make_request(KVM_REQ_HV_RESET, vcpu); 803e516cebbSAndrey Smetanin } 804e516cebbSAndrey Smetanin break; 805e83d5887SAndrey Smetanin default: 806e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", 807e83d5887SAndrey Smetanin msr, data); 808e83d5887SAndrey Smetanin return 1; 809e83d5887SAndrey Smetanin } 810e83d5887SAndrey Smetanin return 0; 811e83d5887SAndrey Smetanin } 812e83d5887SAndrey Smetanin 8139eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */ 8149eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void) 8159eec50b8SAndrey Smetanin { 8169eec50b8SAndrey Smetanin cputime_t utime, stime; 8179eec50b8SAndrey Smetanin 8189eec50b8SAndrey Smetanin task_cputime_adjusted(current, &utime, &stime); 8199eec50b8SAndrey Smetanin return div_u64(cputime_to_nsecs(utime + stime), 100); 8209eec50b8SAndrey Smetanin } 8219eec50b8SAndrey Smetanin 8229eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 823e83d5887SAndrey Smetanin { 824e83d5887SAndrey Smetanin struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; 825e83d5887SAndrey Smetanin 826e83d5887SAndrey Smetanin switch (msr) { 827e83d5887SAndrey Smetanin case HV_X64_MSR_APIC_ASSIST_PAGE: { 828e83d5887SAndrey Smetanin u64 gfn; 829e83d5887SAndrey Smetanin unsigned long addr; 830e83d5887SAndrey Smetanin 831e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) { 832e83d5887SAndrey Smetanin hv->hv_vapic = data; 833e83d5887SAndrey Smetanin if (kvm_lapic_enable_pv_eoi(vcpu, 0)) 834e83d5887SAndrey Smetanin return 1; 835e83d5887SAndrey Smetanin break; 836e83d5887SAndrey Smetanin } 837e83d5887SAndrey Smetanin gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT; 838e83d5887SAndrey Smetanin addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); 839e83d5887SAndrey Smetanin if (kvm_is_error_hva(addr)) 840e83d5887SAndrey Smetanin return 1; 841e83d5887SAndrey Smetanin if (__clear_user((void __user *)addr, PAGE_SIZE)) 842e83d5887SAndrey Smetanin return 1; 843e83d5887SAndrey Smetanin hv->hv_vapic = data; 844e83d5887SAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gfn); 845e83d5887SAndrey Smetanin if (kvm_lapic_enable_pv_eoi(vcpu, 846e83d5887SAndrey Smetanin gfn_to_gpa(gfn) | KVM_MSR_ENABLED)) 847e83d5887SAndrey Smetanin return 1; 848e83d5887SAndrey Smetanin break; 849e83d5887SAndrey Smetanin } 850e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 851e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); 852e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 853e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); 854e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 855e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); 8569eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 8579eec50b8SAndrey Smetanin if (!host) 8589eec50b8SAndrey Smetanin return 1; 8599eec50b8SAndrey Smetanin hv->runtime_offset = data - current_task_runtime_100ns(); 8609eec50b8SAndrey Smetanin break; 8615c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 8625c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 8635c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 8645c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 8655c919412SAndrey Smetanin case HV_X64_MSR_EOM: 8665c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 8675c919412SAndrey Smetanin return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host); 8681f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 8691f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 8701f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 8711f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 8721f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 8731f4b34f8SAndrey Smetanin 8741f4b34f8SAndrey Smetanin return stimer_set_config(vcpu_to_stimer(vcpu, timer_index), 8751f4b34f8SAndrey Smetanin data, host); 8761f4b34f8SAndrey Smetanin } 8771f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 8781f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 8791f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 8801f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 8811f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 8821f4b34f8SAndrey Smetanin 8831f4b34f8SAndrey Smetanin return stimer_set_count(vcpu_to_stimer(vcpu, timer_index), 8841f4b34f8SAndrey Smetanin data, host); 8851f4b34f8SAndrey Smetanin } 886e83d5887SAndrey Smetanin default: 887e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n", 888e83d5887SAndrey Smetanin msr, data); 889e83d5887SAndrey Smetanin return 1; 890e83d5887SAndrey Smetanin } 891e83d5887SAndrey Smetanin 892e83d5887SAndrey Smetanin return 0; 893e83d5887SAndrey Smetanin } 894e83d5887SAndrey Smetanin 895e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 896e83d5887SAndrey Smetanin { 897e83d5887SAndrey Smetanin u64 data = 0; 898e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 899e83d5887SAndrey Smetanin struct kvm_hv *hv = &kvm->arch.hyperv; 900e83d5887SAndrey Smetanin 901e83d5887SAndrey Smetanin switch (msr) { 902e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 903e83d5887SAndrey Smetanin data = hv->hv_guest_os_id; 904e83d5887SAndrey Smetanin break; 905e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 906e83d5887SAndrey Smetanin data = hv->hv_hypercall; 907e83d5887SAndrey Smetanin break; 90893bf4172SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 90993bf4172SAndrey Smetanin data = get_time_ref_counter(kvm); 910e83d5887SAndrey Smetanin break; 911e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 912e83d5887SAndrey Smetanin data = hv->hv_tsc_page; 913e83d5887SAndrey Smetanin break; 914e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 915e7d9513bSAndrey Smetanin return kvm_hv_msr_get_crash_data(vcpu, 916e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 917e7d9513bSAndrey Smetanin pdata); 918e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 919e7d9513bSAndrey Smetanin return kvm_hv_msr_get_crash_ctl(vcpu, pdata); 920e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 921e516cebbSAndrey Smetanin data = 0; 922e516cebbSAndrey Smetanin break; 923e83d5887SAndrey Smetanin default: 924e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 925e83d5887SAndrey Smetanin return 1; 926e83d5887SAndrey Smetanin } 927e83d5887SAndrey Smetanin 928e83d5887SAndrey Smetanin *pdata = data; 929e83d5887SAndrey Smetanin return 0; 930e83d5887SAndrey Smetanin } 931e83d5887SAndrey Smetanin 932e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 933e83d5887SAndrey Smetanin { 934e83d5887SAndrey Smetanin u64 data = 0; 935e83d5887SAndrey Smetanin struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv; 936e83d5887SAndrey Smetanin 937e83d5887SAndrey Smetanin switch (msr) { 938e83d5887SAndrey Smetanin case HV_X64_MSR_VP_INDEX: { 939e83d5887SAndrey Smetanin int r; 940e83d5887SAndrey Smetanin struct kvm_vcpu *v; 941e83d5887SAndrey Smetanin 942e83d5887SAndrey Smetanin kvm_for_each_vcpu(r, v, vcpu->kvm) { 943e83d5887SAndrey Smetanin if (v == vcpu) { 944e83d5887SAndrey Smetanin data = r; 945e83d5887SAndrey Smetanin break; 946e83d5887SAndrey Smetanin } 947e83d5887SAndrey Smetanin } 948e83d5887SAndrey Smetanin break; 949e83d5887SAndrey Smetanin } 950e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 951e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); 952e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 953e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); 954e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 955e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); 956e83d5887SAndrey Smetanin case HV_X64_MSR_APIC_ASSIST_PAGE: 957e83d5887SAndrey Smetanin data = hv->hv_vapic; 958e83d5887SAndrey Smetanin break; 9599eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 9609eec50b8SAndrey Smetanin data = current_task_runtime_100ns() + hv->runtime_offset; 9619eec50b8SAndrey Smetanin break; 9625c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 9635c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 9645c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 9655c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 9665c919412SAndrey Smetanin case HV_X64_MSR_EOM: 9675c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 9685c919412SAndrey Smetanin return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata); 9691f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 9701f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 9711f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 9721f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 9731f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 9741f4b34f8SAndrey Smetanin 9751f4b34f8SAndrey Smetanin return stimer_get_config(vcpu_to_stimer(vcpu, timer_index), 9761f4b34f8SAndrey Smetanin pdata); 9771f4b34f8SAndrey Smetanin } 9781f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 9791f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 9801f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 9811f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 9821f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 9831f4b34f8SAndrey Smetanin 9841f4b34f8SAndrey Smetanin return stimer_get_count(vcpu_to_stimer(vcpu, timer_index), 9851f4b34f8SAndrey Smetanin pdata); 9861f4b34f8SAndrey Smetanin } 987e83d5887SAndrey Smetanin default: 988e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 989e83d5887SAndrey Smetanin return 1; 990e83d5887SAndrey Smetanin } 991e83d5887SAndrey Smetanin *pdata = data; 992e83d5887SAndrey Smetanin return 0; 993e83d5887SAndrey Smetanin } 994e83d5887SAndrey Smetanin 995e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 996e83d5887SAndrey Smetanin { 997e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 998e83d5887SAndrey Smetanin int r; 999e83d5887SAndrey Smetanin 1000e83d5887SAndrey Smetanin mutex_lock(&vcpu->kvm->lock); 1001e7d9513bSAndrey Smetanin r = kvm_hv_set_msr_pw(vcpu, msr, data, host); 1002e83d5887SAndrey Smetanin mutex_unlock(&vcpu->kvm->lock); 1003e83d5887SAndrey Smetanin return r; 1004e83d5887SAndrey Smetanin } else 10059eec50b8SAndrey Smetanin return kvm_hv_set_msr(vcpu, msr, data, host); 1006e83d5887SAndrey Smetanin } 1007e83d5887SAndrey Smetanin 1008e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) 1009e83d5887SAndrey Smetanin { 1010e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 1011e83d5887SAndrey Smetanin int r; 1012e83d5887SAndrey Smetanin 1013e83d5887SAndrey Smetanin mutex_lock(&vcpu->kvm->lock); 1014e83d5887SAndrey Smetanin r = kvm_hv_get_msr_pw(vcpu, msr, pdata); 1015e83d5887SAndrey Smetanin mutex_unlock(&vcpu->kvm->lock); 1016e83d5887SAndrey Smetanin return r; 1017e83d5887SAndrey Smetanin } else 1018e83d5887SAndrey Smetanin return kvm_hv_get_msr(vcpu, msr, pdata); 1019e83d5887SAndrey Smetanin } 1020e83d5887SAndrey Smetanin 1021e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm) 1022e83d5887SAndrey Smetanin { 1023e83d5887SAndrey Smetanin return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE; 1024e83d5887SAndrey Smetanin } 1025e83d5887SAndrey Smetanin 1026e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu) 1027e83d5887SAndrey Smetanin { 1028e83d5887SAndrey Smetanin u64 param, ingpa, outgpa, ret; 1029e83d5887SAndrey Smetanin uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0; 1030e83d5887SAndrey Smetanin bool fast, longmode; 1031e83d5887SAndrey Smetanin 1032e83d5887SAndrey Smetanin /* 1033e83d5887SAndrey Smetanin * hypercall generates UD from non zero cpl and real mode 1034e83d5887SAndrey Smetanin * per HYPER-V spec 1035e83d5887SAndrey Smetanin */ 1036e83d5887SAndrey Smetanin if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) { 1037e83d5887SAndrey Smetanin kvm_queue_exception(vcpu, UD_VECTOR); 1038e83d5887SAndrey Smetanin return 0; 1039e83d5887SAndrey Smetanin } 1040e83d5887SAndrey Smetanin 1041e83d5887SAndrey Smetanin longmode = is_64_bit_mode(vcpu); 1042e83d5887SAndrey Smetanin 1043e83d5887SAndrey Smetanin if (!longmode) { 1044e83d5887SAndrey Smetanin param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) | 1045e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff); 1046e83d5887SAndrey Smetanin ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) | 1047e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff); 1048e83d5887SAndrey Smetanin outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) | 1049e83d5887SAndrey Smetanin (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff); 1050e83d5887SAndrey Smetanin } 1051e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64 1052e83d5887SAndrey Smetanin else { 1053e83d5887SAndrey Smetanin param = kvm_register_read(vcpu, VCPU_REGS_RCX); 1054e83d5887SAndrey Smetanin ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX); 1055e83d5887SAndrey Smetanin outgpa = kvm_register_read(vcpu, VCPU_REGS_R8); 1056e83d5887SAndrey Smetanin } 1057e83d5887SAndrey Smetanin #endif 1058e83d5887SAndrey Smetanin 1059e83d5887SAndrey Smetanin code = param & 0xffff; 1060e83d5887SAndrey Smetanin fast = (param >> 16) & 0x1; 1061e83d5887SAndrey Smetanin rep_cnt = (param >> 32) & 0xfff; 1062e83d5887SAndrey Smetanin rep_idx = (param >> 48) & 0xfff; 1063e83d5887SAndrey Smetanin 1064e83d5887SAndrey Smetanin trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa); 1065e83d5887SAndrey Smetanin 1066e83d5887SAndrey Smetanin switch (code) { 1067e83d5887SAndrey Smetanin case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT: 1068e83d5887SAndrey Smetanin kvm_vcpu_on_spin(vcpu); 1069e83d5887SAndrey Smetanin break; 1070e83d5887SAndrey Smetanin default: 1071e83d5887SAndrey Smetanin res = HV_STATUS_INVALID_HYPERCALL_CODE; 1072e83d5887SAndrey Smetanin break; 1073e83d5887SAndrey Smetanin } 1074e83d5887SAndrey Smetanin 1075e83d5887SAndrey Smetanin ret = res | (((u64)rep_done & 0xfff) << 32); 1076e83d5887SAndrey Smetanin if (longmode) { 1077e83d5887SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RAX, ret); 1078e83d5887SAndrey Smetanin } else { 1079e83d5887SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32); 1080e83d5887SAndrey Smetanin kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff); 1081e83d5887SAndrey Smetanin } 1082e83d5887SAndrey Smetanin 1083e83d5887SAndrey Smetanin return 1; 1084e83d5887SAndrey Smetanin } 1085