120c8ccb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e83d5887SAndrey Smetanin /* 3e83d5887SAndrey Smetanin * KVM Microsoft Hyper-V emulation 4e83d5887SAndrey Smetanin * 5e83d5887SAndrey Smetanin * derived from arch/x86/kvm/x86.c 6e83d5887SAndrey Smetanin * 7e83d5887SAndrey Smetanin * Copyright (C) 2006 Qumranet, Inc. 8e83d5887SAndrey Smetanin * Copyright (C) 2008 Qumranet, Inc. 9e83d5887SAndrey Smetanin * Copyright IBM Corporation, 2008 10e83d5887SAndrey Smetanin * Copyright 2010 Red Hat, Inc. and/or its affiliates. 11e83d5887SAndrey Smetanin * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com> 12e83d5887SAndrey Smetanin * 13e83d5887SAndrey Smetanin * Authors: 14e83d5887SAndrey Smetanin * Avi Kivity <avi@qumranet.com> 15e83d5887SAndrey Smetanin * Yaniv Kamay <yaniv@qumranet.com> 16e83d5887SAndrey Smetanin * Amit Shah <amit.shah@qumranet.com> 17e83d5887SAndrey Smetanin * Ben-Ami Yassour <benami@il.ibm.com> 18e83d5887SAndrey Smetanin * Andrey Smetanin <asmetanin@virtuozzo.com> 19e83d5887SAndrey Smetanin */ 20e83d5887SAndrey Smetanin 21e83d5887SAndrey Smetanin #include "x86.h" 22e83d5887SAndrey Smetanin #include "lapic.h" 235c919412SAndrey Smetanin #include "ioapic.h" 24f97f5a56SJon Doron #include "cpuid.h" 25e83d5887SAndrey Smetanin #include "hyperv.h" 2679033bebSJoao Martins #include "xen.h" 27e83d5887SAndrey Smetanin 28b2d8b167SVitaly Kuznetsov #include <linux/cpu.h> 29e83d5887SAndrey Smetanin #include <linux/kvm_host.h> 30765eaa0fSAndrey Smetanin #include <linux/highmem.h> 3132ef5517SIngo Molnar #include <linux/sched/cputime.h> 32faeb7833SRoman 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" 3859508b30SPeter Xu #include "irq.h" 395974565bSSiddharth Chandrasekaran #include "fpu.h" 40e83d5887SAndrey Smetanin 418f014550SVitaly Kuznetsov /* "Hv#1" signature */ 428f014550SVitaly Kuznetsov #define HYPERV_CPUID_SIGNATURE_EAX 0x31237648 438f014550SVitaly Kuznetsov 44f21dd494SVitaly Kuznetsov #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) 45f21dd494SVitaly Kuznetsov 468644f771SVitaly Kuznetsov static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, 478644f771SVitaly Kuznetsov bool vcpu_kick); 488644f771SVitaly Kuznetsov 495c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint) 505c919412SAndrey Smetanin { 515c919412SAndrey Smetanin return atomic64_read(&synic->sint[sint]); 525c919412SAndrey Smetanin } 535c919412SAndrey Smetanin 545c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value) 555c919412SAndrey Smetanin { 565c919412SAndrey Smetanin if (sint_value & HV_SYNIC_SINT_MASKED) 575c919412SAndrey Smetanin return -1; 585c919412SAndrey Smetanin return sint_value & HV_SYNIC_SINT_VECTOR_MASK; 595c919412SAndrey Smetanin } 605c919412SAndrey Smetanin 615c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic, 625c919412SAndrey Smetanin int vector) 635c919412SAndrey Smetanin { 645c919412SAndrey Smetanin int i; 655c919412SAndrey Smetanin 665c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 675c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 685c919412SAndrey Smetanin return true; 695c919412SAndrey Smetanin } 705c919412SAndrey Smetanin return false; 715c919412SAndrey Smetanin } 725c919412SAndrey Smetanin 735c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic, 745c919412SAndrey Smetanin int vector) 755c919412SAndrey Smetanin { 765c919412SAndrey Smetanin int i; 775c919412SAndrey Smetanin u64 sint_value; 785c919412SAndrey Smetanin 795c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 805c919412SAndrey Smetanin sint_value = synic_read_sint(synic, i); 815c919412SAndrey Smetanin if (synic_get_sint_vector(sint_value) == vector && 825c919412SAndrey Smetanin sint_value & HV_SYNIC_SINT_AUTO_EOI) 835c919412SAndrey Smetanin return true; 845c919412SAndrey Smetanin } 855c919412SAndrey Smetanin return false; 865c919412SAndrey Smetanin } 875c919412SAndrey Smetanin 8898f65ad4SVitaly Kuznetsov static void synic_update_vector(struct kvm_vcpu_hv_synic *synic, 8998f65ad4SVitaly Kuznetsov int vector) 905c919412SAndrey Smetanin { 9187a8d795SVitaly Kuznetsov if (vector < HV_SYNIC_FIRST_VALID_VECTOR) 9287a8d795SVitaly Kuznetsov return; 9387a8d795SVitaly Kuznetsov 945c919412SAndrey Smetanin if (synic_has_vector_connected(synic, vector)) 955c919412SAndrey Smetanin __set_bit(vector, synic->vec_bitmap); 965c919412SAndrey Smetanin else 975c919412SAndrey Smetanin __clear_bit(vector, synic->vec_bitmap); 985c919412SAndrey Smetanin 995c919412SAndrey Smetanin if (synic_has_vector_auto_eoi(synic, vector)) 1005c919412SAndrey Smetanin __set_bit(vector, synic->auto_eoi_bitmap); 1015c919412SAndrey Smetanin else 1025c919412SAndrey Smetanin __clear_bit(vector, synic->auto_eoi_bitmap); 10398f65ad4SVitaly Kuznetsov } 10498f65ad4SVitaly Kuznetsov 10598f65ad4SVitaly Kuznetsov static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, 10698f65ad4SVitaly Kuznetsov u64 data, bool host) 10798f65ad4SVitaly Kuznetsov { 10898f65ad4SVitaly Kuznetsov int vector, old_vector; 109915e6f78SVitaly Kuznetsov bool masked; 11098f65ad4SVitaly Kuznetsov 11198f65ad4SVitaly Kuznetsov vector = data & HV_SYNIC_SINT_VECTOR_MASK; 112915e6f78SVitaly Kuznetsov masked = data & HV_SYNIC_SINT_MASKED; 113915e6f78SVitaly Kuznetsov 114915e6f78SVitaly Kuznetsov /* 115915e6f78SVitaly Kuznetsov * Valid vectors are 16-255, however, nested Hyper-V attempts to write 116915e6f78SVitaly Kuznetsov * default '0x10000' value on boot and this should not #GP. We need to 117915e6f78SVitaly Kuznetsov * allow zero-initing the register from host as well. 118915e6f78SVitaly Kuznetsov */ 119915e6f78SVitaly Kuznetsov if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked) 12098f65ad4SVitaly Kuznetsov return 1; 12198f65ad4SVitaly Kuznetsov /* 12298f65ad4SVitaly Kuznetsov * Guest may configure multiple SINTs to use the same vector, so 12398f65ad4SVitaly Kuznetsov * we maintain a bitmap of vectors handled by synic, and a 12498f65ad4SVitaly Kuznetsov * bitmap of vectors with auto-eoi behavior. The bitmaps are 12598f65ad4SVitaly Kuznetsov * updated here, and atomically queried on fast paths. 12698f65ad4SVitaly Kuznetsov */ 12798f65ad4SVitaly Kuznetsov old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK; 12898f65ad4SVitaly Kuznetsov 12998f65ad4SVitaly Kuznetsov atomic64_set(&synic->sint[sint], data); 13098f65ad4SVitaly Kuznetsov 13198f65ad4SVitaly Kuznetsov synic_update_vector(synic, old_vector); 13298f65ad4SVitaly Kuznetsov 13398f65ad4SVitaly Kuznetsov synic_update_vector(synic, vector); 1345c919412SAndrey Smetanin 1355c919412SAndrey Smetanin /* Load SynIC vectors into EOI exit bitmap */ 136e0121fa2SVitaly Kuznetsov kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic)); 1375c919412SAndrey Smetanin return 0; 1385c919412SAndrey Smetanin } 1395c919412SAndrey Smetanin 140d3457c87SRoman Kagan static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx) 141d3457c87SRoman Kagan { 142d3457c87SRoman Kagan struct kvm_vcpu *vcpu = NULL; 143d3457c87SRoman Kagan int i; 144d3457c87SRoman Kagan 1459170200eSVitaly Kuznetsov if (vpidx >= KVM_MAX_VCPUS) 1469170200eSVitaly Kuznetsov return NULL; 1479170200eSVitaly Kuznetsov 148d3457c87SRoman Kagan vcpu = kvm_get_vcpu(kvm, vpidx); 149f2bc14b6SVitaly Kuznetsov if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx) 150d3457c87SRoman Kagan return vcpu; 151d3457c87SRoman Kagan kvm_for_each_vcpu(i, vcpu, kvm) 152f2bc14b6SVitaly Kuznetsov if (kvm_hv_get_vpindex(vcpu) == vpidx) 153d3457c87SRoman Kagan return vcpu; 154d3457c87SRoman Kagan return NULL; 155d3457c87SRoman Kagan } 156d3457c87SRoman Kagan 157d3457c87SRoman Kagan static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx) 1585c919412SAndrey Smetanin { 1595c919412SAndrey Smetanin struct kvm_vcpu *vcpu; 1605c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 1615c919412SAndrey Smetanin 162d3457c87SRoman Kagan vcpu = get_vcpu_by_vpidx(kvm, vpidx); 163919f4ebcSWanpeng Li if (!vcpu || !to_hv_vcpu(vcpu)) 1645c919412SAndrey Smetanin return NULL; 165e0121fa2SVitaly Kuznetsov synic = to_hv_synic(vcpu); 1665c919412SAndrey Smetanin return (synic->active) ? synic : NULL; 1675c919412SAndrey Smetanin } 1685c919412SAndrey Smetanin 1695c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint) 1705c919412SAndrey Smetanin { 1715c919412SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 172e0121fa2SVitaly Kuznetsov struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 173ef3f3980SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1741f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 17508a800acSVitaly Kuznetsov int gsi, idx; 1765c919412SAndrey Smetanin 17718659a9cSAndrey Smetanin trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint); 1785c919412SAndrey Smetanin 1791f4b34f8SAndrey Smetanin /* Try to deliver pending Hyper-V SynIC timers messages */ 1801f4b34f8SAndrey Smetanin for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) { 1811f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[idx]; 1826a058a1eSVitaly Kuznetsov if (stimer->msg_pending && stimer->config.enable && 1838644f771SVitaly Kuznetsov !stimer->config.direct_mode && 18408a800acSVitaly Kuznetsov stimer->config.sintx == sint) 18508a800acSVitaly Kuznetsov stimer_mark_pending(stimer, false); 1861f4b34f8SAndrey Smetanin } 1871f4b34f8SAndrey Smetanin 1885c919412SAndrey Smetanin idx = srcu_read_lock(&kvm->irq_srcu); 1891f4b34f8SAndrey Smetanin gsi = atomic_read(&synic->sint_to_gsi[sint]); 1905c919412SAndrey Smetanin if (gsi != -1) 1915c919412SAndrey Smetanin kvm_notify_acked_gsi(kvm, gsi); 1925c919412SAndrey Smetanin srcu_read_unlock(&kvm->irq_srcu, idx); 1935c919412SAndrey Smetanin } 1945c919412SAndrey Smetanin 195db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr) 196db397571SAndrey Smetanin { 197e0121fa2SVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 1989ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 199db397571SAndrey Smetanin 200db397571SAndrey Smetanin hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC; 201db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msr = msr; 202db397571SAndrey Smetanin hv_vcpu->exit.u.synic.control = synic->control; 203db397571SAndrey Smetanin hv_vcpu->exit.u.synic.evt_page = synic->evt_page; 204db397571SAndrey Smetanin hv_vcpu->exit.u.synic.msg_page = synic->msg_page; 205db397571SAndrey Smetanin 206db397571SAndrey Smetanin kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 207db397571SAndrey Smetanin } 208db397571SAndrey Smetanin 2095c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic, 2105c919412SAndrey Smetanin u32 msr, u64 data, bool host) 2115c919412SAndrey Smetanin { 212e0121fa2SVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 2135c919412SAndrey Smetanin int ret; 2145c919412SAndrey Smetanin 21544883f01SPaolo Bonzini if (!synic->active && !host) 2165c919412SAndrey Smetanin return 1; 2175c919412SAndrey Smetanin 21818659a9cSAndrey Smetanin trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); 21918659a9cSAndrey Smetanin 2205c919412SAndrey Smetanin ret = 0; 2215c919412SAndrey Smetanin switch (msr) { 2225c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 2235c919412SAndrey Smetanin synic->control = data; 224db397571SAndrey Smetanin if (!host) 225db397571SAndrey Smetanin synic_exit(synic, msr); 2265c919412SAndrey Smetanin break; 2275c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 2285c919412SAndrey Smetanin if (!host) { 2295c919412SAndrey Smetanin ret = 1; 2305c919412SAndrey Smetanin break; 2315c919412SAndrey Smetanin } 2325c919412SAndrey Smetanin synic->version = data; 2335c919412SAndrey Smetanin break; 2345c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 235efc479e6SRoman Kagan if ((data & HV_SYNIC_SIEFP_ENABLE) && !host && 236efc479e6SRoman Kagan !synic->dont_zero_synic_pages) 2375c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2385c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2395c919412SAndrey Smetanin ret = 1; 2405c919412SAndrey Smetanin break; 2415c919412SAndrey Smetanin } 2425c919412SAndrey Smetanin synic->evt_page = data; 243db397571SAndrey Smetanin if (!host) 244db397571SAndrey Smetanin synic_exit(synic, msr); 2455c919412SAndrey Smetanin break; 2465c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 247efc479e6SRoman Kagan if ((data & HV_SYNIC_SIMP_ENABLE) && !host && 248efc479e6SRoman Kagan !synic->dont_zero_synic_pages) 2495c919412SAndrey Smetanin if (kvm_clear_guest(vcpu->kvm, 2505c919412SAndrey Smetanin data & PAGE_MASK, PAGE_SIZE)) { 2515c919412SAndrey Smetanin ret = 1; 2525c919412SAndrey Smetanin break; 2535c919412SAndrey Smetanin } 2545c919412SAndrey Smetanin synic->msg_page = data; 255db397571SAndrey Smetanin if (!host) 256db397571SAndrey Smetanin synic_exit(synic, msr); 2575c919412SAndrey Smetanin break; 2585c919412SAndrey Smetanin case HV_X64_MSR_EOM: { 2595c919412SAndrey Smetanin int i; 2605c919412SAndrey Smetanin 2615c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 2625c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 2635c919412SAndrey Smetanin break; 2645c919412SAndrey Smetanin } 2655c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 2667be58a64SAndrey Smetanin ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host); 2675c919412SAndrey Smetanin break; 2685c919412SAndrey Smetanin default: 2695c919412SAndrey Smetanin ret = 1; 2705c919412SAndrey Smetanin break; 2715c919412SAndrey Smetanin } 2725c919412SAndrey Smetanin return ret; 2735c919412SAndrey Smetanin } 2745c919412SAndrey Smetanin 275f97f5a56SJon Doron static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu) 276f97f5a56SJon Doron { 27710d7bf1eSVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 278f97f5a56SJon Doron 27910d7bf1eSVitaly Kuznetsov return hv_vcpu->cpuid_cache.syndbg_cap_eax & 28010d7bf1eSVitaly Kuznetsov HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; 281f97f5a56SJon Doron } 282f97f5a56SJon Doron 283f97f5a56SJon Doron static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu) 284f97f5a56SJon Doron { 28505f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 286f97f5a56SJon Doron 287f97f5a56SJon Doron if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL) 288f97f5a56SJon Doron hv->hv_syndbg.control.status = 289f97f5a56SJon Doron vcpu->run->hyperv.u.syndbg.status; 290f97f5a56SJon Doron return 1; 291f97f5a56SJon Doron } 292f97f5a56SJon Doron 293f97f5a56SJon Doron static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr) 294f97f5a56SJon Doron { 295f69b55efSVitaly Kuznetsov struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 2969ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 297f97f5a56SJon Doron 298f97f5a56SJon Doron hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG; 299f97f5a56SJon Doron hv_vcpu->exit.u.syndbg.msr = msr; 300f97f5a56SJon Doron hv_vcpu->exit.u.syndbg.control = syndbg->control.control; 301f97f5a56SJon Doron hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page; 302f97f5a56SJon Doron hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page; 303f97f5a56SJon Doron hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page; 304f97f5a56SJon Doron vcpu->arch.complete_userspace_io = 305f97f5a56SJon Doron kvm_hv_syndbg_complete_userspace; 306f97f5a56SJon Doron 307f97f5a56SJon Doron kvm_make_request(KVM_REQ_HV_EXIT, vcpu); 308f97f5a56SJon Doron } 309f97f5a56SJon Doron 310f97f5a56SJon Doron static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 311f97f5a56SJon Doron { 312f69b55efSVitaly Kuznetsov struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 313f97f5a56SJon Doron 314f97f5a56SJon Doron if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) 315f97f5a56SJon Doron return 1; 316f97f5a56SJon Doron 317f97f5a56SJon Doron trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id, 318ef3f3980SVitaly Kuznetsov to_hv_vcpu(vcpu)->vp_index, msr, data); 319f97f5a56SJon Doron switch (msr) { 320f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_CONTROL: 321f97f5a56SJon Doron syndbg->control.control = data; 322f97f5a56SJon Doron if (!host) 323f97f5a56SJon Doron syndbg_exit(vcpu, msr); 324f97f5a56SJon Doron break; 325f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_STATUS: 326f97f5a56SJon Doron syndbg->control.status = data; 327f97f5a56SJon Doron break; 328f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_SEND_BUFFER: 329f97f5a56SJon Doron syndbg->control.send_page = data; 330f97f5a56SJon Doron break; 331f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_RECV_BUFFER: 332f97f5a56SJon Doron syndbg->control.recv_page = data; 333f97f5a56SJon Doron break; 334f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_PENDING_BUFFER: 335f97f5a56SJon Doron syndbg->control.pending_page = data; 336f97f5a56SJon Doron if (!host) 337f97f5a56SJon Doron syndbg_exit(vcpu, msr); 338f97f5a56SJon Doron break; 339f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_OPTIONS: 340f97f5a56SJon Doron syndbg->options = data; 341f97f5a56SJon Doron break; 342f97f5a56SJon Doron default: 343f97f5a56SJon Doron break; 344f97f5a56SJon Doron } 345f97f5a56SJon Doron 346f97f5a56SJon Doron return 0; 347f97f5a56SJon Doron } 348f97f5a56SJon Doron 349f97f5a56SJon Doron static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) 350f97f5a56SJon Doron { 351f69b55efSVitaly Kuznetsov struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 352f97f5a56SJon Doron 353f97f5a56SJon Doron if (!kvm_hv_is_syndbg_enabled(vcpu) && !host) 354f97f5a56SJon Doron return 1; 355f97f5a56SJon Doron 356f97f5a56SJon Doron switch (msr) { 357f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_CONTROL: 358f97f5a56SJon Doron *pdata = syndbg->control.control; 359f97f5a56SJon Doron break; 360f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_STATUS: 361f97f5a56SJon Doron *pdata = syndbg->control.status; 362f97f5a56SJon Doron break; 363f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_SEND_BUFFER: 364f97f5a56SJon Doron *pdata = syndbg->control.send_page; 365f97f5a56SJon Doron break; 366f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_RECV_BUFFER: 367f97f5a56SJon Doron *pdata = syndbg->control.recv_page; 368f97f5a56SJon Doron break; 369f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_PENDING_BUFFER: 370f97f5a56SJon Doron *pdata = syndbg->control.pending_page; 371f97f5a56SJon Doron break; 372f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_OPTIONS: 373f97f5a56SJon Doron *pdata = syndbg->options; 374f97f5a56SJon Doron break; 375f97f5a56SJon Doron default: 376f97f5a56SJon Doron break; 377f97f5a56SJon Doron } 378f97f5a56SJon Doron 379f2bc14b6SVitaly Kuznetsov trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata); 380f97f5a56SJon Doron 381f97f5a56SJon Doron return 0; 382f97f5a56SJon Doron } 383f97f5a56SJon Doron 38444883f01SPaolo Bonzini static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata, 38544883f01SPaolo Bonzini bool host) 3865c919412SAndrey Smetanin { 3875c919412SAndrey Smetanin int ret; 3885c919412SAndrey Smetanin 38944883f01SPaolo Bonzini if (!synic->active && !host) 3905c919412SAndrey Smetanin return 1; 3915c919412SAndrey Smetanin 3925c919412SAndrey Smetanin ret = 0; 3935c919412SAndrey Smetanin switch (msr) { 3945c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 3955c919412SAndrey Smetanin *pdata = synic->control; 3965c919412SAndrey Smetanin break; 3975c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 3985c919412SAndrey Smetanin *pdata = synic->version; 3995c919412SAndrey Smetanin break; 4005c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 4015c919412SAndrey Smetanin *pdata = synic->evt_page; 4025c919412SAndrey Smetanin break; 4035c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 4045c919412SAndrey Smetanin *pdata = synic->msg_page; 4055c919412SAndrey Smetanin break; 4065c919412SAndrey Smetanin case HV_X64_MSR_EOM: 4075c919412SAndrey Smetanin *pdata = 0; 4085c919412SAndrey Smetanin break; 4095c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 4105c919412SAndrey Smetanin *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]); 4115c919412SAndrey Smetanin break; 4125c919412SAndrey Smetanin default: 4135c919412SAndrey Smetanin ret = 1; 4145c919412SAndrey Smetanin break; 4155c919412SAndrey Smetanin } 4165c919412SAndrey Smetanin return ret; 4175c919412SAndrey Smetanin } 4185c919412SAndrey Smetanin 419ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint) 4205c919412SAndrey Smetanin { 421e0121fa2SVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 4225c919412SAndrey Smetanin struct kvm_lapic_irq irq; 4235c919412SAndrey Smetanin int ret, vector; 4245c919412SAndrey Smetanin 4255c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint)) 4265c919412SAndrey Smetanin return -EINVAL; 4275c919412SAndrey Smetanin 4285c919412SAndrey Smetanin vector = synic_get_sint_vector(synic_read_sint(synic, sint)); 4295c919412SAndrey Smetanin if (vector < 0) 4305c919412SAndrey Smetanin return -ENOENT; 4315c919412SAndrey Smetanin 4325c919412SAndrey Smetanin memset(&irq, 0, sizeof(irq)); 433f98a3efbSRadim Krčmář irq.shorthand = APIC_DEST_SELF; 4345c919412SAndrey Smetanin irq.dest_mode = APIC_DEST_PHYSICAL; 4355c919412SAndrey Smetanin irq.delivery_mode = APIC_DM_FIXED; 4365c919412SAndrey Smetanin irq.vector = vector; 4375c919412SAndrey Smetanin irq.level = 1; 4385c919412SAndrey Smetanin 439f98a3efbSRadim Krčmář ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL); 44018659a9cSAndrey Smetanin trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret); 4415c919412SAndrey Smetanin return ret; 4425c919412SAndrey Smetanin } 4435c919412SAndrey Smetanin 444d3457c87SRoman Kagan int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint) 4455c919412SAndrey Smetanin { 4465c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 4475c919412SAndrey Smetanin 448d3457c87SRoman Kagan synic = synic_get(kvm, vpidx); 4495c919412SAndrey Smetanin if (!synic) 4505c919412SAndrey Smetanin return -EINVAL; 4515c919412SAndrey Smetanin 4525c919412SAndrey Smetanin return synic_set_irq(synic, sint); 4535c919412SAndrey Smetanin } 4545c919412SAndrey Smetanin 4555c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) 4565c919412SAndrey Smetanin { 457e0121fa2SVitaly Kuznetsov struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 4585c919412SAndrey Smetanin int i; 4595c919412SAndrey Smetanin 46018659a9cSAndrey Smetanin trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector); 4615c919412SAndrey Smetanin 4625c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) 4635c919412SAndrey Smetanin if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector) 4645c919412SAndrey Smetanin kvm_hv_notify_acked_sint(vcpu, i); 4655c919412SAndrey Smetanin } 4665c919412SAndrey Smetanin 467d3457c87SRoman Kagan static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi) 4685c919412SAndrey Smetanin { 4695c919412SAndrey Smetanin struct kvm_vcpu_hv_synic *synic; 4705c919412SAndrey Smetanin 471d3457c87SRoman Kagan synic = synic_get(kvm, vpidx); 4725c919412SAndrey Smetanin if (!synic) 4735c919412SAndrey Smetanin return -EINVAL; 4745c919412SAndrey Smetanin 4755c919412SAndrey Smetanin if (sint >= ARRAY_SIZE(synic->sint_to_gsi)) 4765c919412SAndrey Smetanin return -EINVAL; 4775c919412SAndrey Smetanin 4785c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[sint], gsi); 4795c919412SAndrey Smetanin return 0; 4805c919412SAndrey Smetanin } 4815c919412SAndrey Smetanin 4825c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm) 4835c919412SAndrey Smetanin { 4845c919412SAndrey Smetanin struct kvm_irq_routing_table *irq_rt; 4855c919412SAndrey Smetanin struct kvm_kernel_irq_routing_entry *e; 4865c919412SAndrey Smetanin u32 gsi; 4875c919412SAndrey Smetanin 4885c919412SAndrey Smetanin irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu, 4895c919412SAndrey Smetanin lockdep_is_held(&kvm->irq_lock)); 4905c919412SAndrey Smetanin 4915c919412SAndrey Smetanin for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) { 4925c919412SAndrey Smetanin hlist_for_each_entry(e, &irq_rt->map[gsi], link) { 4935c919412SAndrey Smetanin if (e->type == KVM_IRQ_ROUTING_HV_SINT) 4945c919412SAndrey Smetanin kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu, 4955c919412SAndrey Smetanin e->hv_sint.sint, gsi); 4965c919412SAndrey Smetanin } 4975c919412SAndrey Smetanin } 4985c919412SAndrey Smetanin } 4995c919412SAndrey Smetanin 5005c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic) 5015c919412SAndrey Smetanin { 5025c919412SAndrey Smetanin int i; 5035c919412SAndrey Smetanin 5045c919412SAndrey Smetanin memset(synic, 0, sizeof(*synic)); 5055c919412SAndrey Smetanin synic->version = HV_SYNIC_VERSION_1; 5065c919412SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(synic->sint); i++) { 5075c919412SAndrey Smetanin atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED); 5085c919412SAndrey Smetanin atomic_set(&synic->sint_to_gsi[i], -1); 5095c919412SAndrey Smetanin } 5105c919412SAndrey Smetanin } 5115c919412SAndrey Smetanin 51293bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm) 51393bf4172SAndrey Smetanin { 51405f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 515095cf55dSPaolo Bonzini struct kvm_vcpu *vcpu; 516095cf55dSPaolo Bonzini u64 tsc; 517095cf55dSPaolo Bonzini 518095cf55dSPaolo Bonzini /* 519cc9cfddbSVitaly Kuznetsov * Fall back to get_kvmclock_ns() when TSC page hasn't been set up, 520cc9cfddbSVitaly Kuznetsov * is broken, disabled or being updated. 521095cf55dSPaolo Bonzini */ 522cc9cfddbSVitaly Kuznetsov if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET) 523108b249cSPaolo Bonzini return div_u64(get_kvmclock_ns(kvm), 100); 524095cf55dSPaolo Bonzini 525095cf55dSPaolo Bonzini vcpu = kvm_get_vcpu(kvm, 0); 526095cf55dSPaolo Bonzini tsc = kvm_read_l1_tsc(vcpu, rdtsc()); 527095cf55dSPaolo Bonzini return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64) 528095cf55dSPaolo Bonzini + hv->tsc_ref.tsc_offset; 52993bf4172SAndrey Smetanin } 53093bf4172SAndrey Smetanin 531f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, 5321f4b34f8SAndrey Smetanin bool vcpu_kick) 5331f4b34f8SAndrey Smetanin { 534aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 5351f4b34f8SAndrey Smetanin 5361f4b34f8SAndrey Smetanin set_bit(stimer->index, 537ef3f3980SVitaly Kuznetsov to_hv_vcpu(vcpu)->stimer_pending_bitmap); 5381f4b34f8SAndrey Smetanin kvm_make_request(KVM_REQ_HV_STIMER, vcpu); 5391f4b34f8SAndrey Smetanin if (vcpu_kick) 5401f4b34f8SAndrey Smetanin kvm_vcpu_kick(vcpu); 5411f4b34f8SAndrey Smetanin } 5421f4b34f8SAndrey Smetanin 5431f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer) 5441f4b34f8SAndrey Smetanin { 545aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 5461f4b34f8SAndrey Smetanin 547aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id, 548ac3e5fcaSAndrey Smetanin stimer->index); 549ac3e5fcaSAndrey Smetanin 550019b9781SAndrey Smetanin hrtimer_cancel(&stimer->timer); 5511f4b34f8SAndrey Smetanin clear_bit(stimer->index, 552ef3f3980SVitaly Kuznetsov to_hv_vcpu(vcpu)->stimer_pending_bitmap); 5531f4b34f8SAndrey Smetanin stimer->msg_pending = false; 554f808495dSAndrey Smetanin stimer->exp_time = 0; 5551f4b34f8SAndrey Smetanin } 5561f4b34f8SAndrey Smetanin 5571f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer) 5581f4b34f8SAndrey Smetanin { 5591f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 5601f4b34f8SAndrey Smetanin 5611f4b34f8SAndrey Smetanin stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer); 562aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id, 563ac3e5fcaSAndrey Smetanin stimer->index); 564f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, true); 5651f4b34f8SAndrey Smetanin 5661f4b34f8SAndrey Smetanin return HRTIMER_NORESTART; 5671f4b34f8SAndrey Smetanin } 5681f4b34f8SAndrey Smetanin 569f808495dSAndrey Smetanin /* 570f808495dSAndrey Smetanin * stimer_start() assumptions: 571f808495dSAndrey Smetanin * a) stimer->count is not equal to 0 572f808495dSAndrey Smetanin * b) stimer->config has HV_STIMER_ENABLE flag 573f808495dSAndrey Smetanin */ 5741f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer) 5751f4b34f8SAndrey Smetanin { 5761f4b34f8SAndrey Smetanin u64 time_now; 5771f4b34f8SAndrey Smetanin ktime_t ktime_now; 5781f4b34f8SAndrey Smetanin 579aafa97fdSVitaly Kuznetsov time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm); 5801f4b34f8SAndrey Smetanin ktime_now = ktime_get(); 5811f4b34f8SAndrey Smetanin 5826a058a1eSVitaly Kuznetsov if (stimer->config.periodic) { 583f808495dSAndrey Smetanin if (stimer->exp_time) { 584f808495dSAndrey Smetanin if (time_now >= stimer->exp_time) { 585f808495dSAndrey Smetanin u64 remainder; 5861f4b34f8SAndrey Smetanin 587f808495dSAndrey Smetanin div64_u64_rem(time_now - stimer->exp_time, 588f808495dSAndrey Smetanin stimer->count, &remainder); 589f808495dSAndrey Smetanin stimer->exp_time = 590f808495dSAndrey Smetanin time_now + (stimer->count - remainder); 591f808495dSAndrey Smetanin } 592f808495dSAndrey Smetanin } else 5931f4b34f8SAndrey Smetanin stimer->exp_time = time_now + stimer->count; 594f808495dSAndrey Smetanin 595ac3e5fcaSAndrey Smetanin trace_kvm_hv_stimer_start_periodic( 596aafa97fdSVitaly Kuznetsov hv_stimer_to_vcpu(stimer)->vcpu_id, 597ac3e5fcaSAndrey Smetanin stimer->index, 598ac3e5fcaSAndrey Smetanin time_now, stimer->exp_time); 599ac3e5fcaSAndrey Smetanin 6001f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 601f808495dSAndrey Smetanin ktime_add_ns(ktime_now, 602f808495dSAndrey Smetanin 100 * (stimer->exp_time - time_now)), 6031f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 6041f4b34f8SAndrey Smetanin return 0; 6051f4b34f8SAndrey Smetanin } 6061f4b34f8SAndrey Smetanin stimer->exp_time = stimer->count; 6071f4b34f8SAndrey Smetanin if (time_now >= stimer->count) { 6081f4b34f8SAndrey Smetanin /* 6091f4b34f8SAndrey Smetanin * Expire timer according to Hypervisor Top-Level Functional 6101f4b34f8SAndrey Smetanin * specification v4(15.3.1): 6111f4b34f8SAndrey Smetanin * "If a one shot is enabled and the specified count is in 6121f4b34f8SAndrey Smetanin * the past, it will expire immediately." 6131f4b34f8SAndrey Smetanin */ 614f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 6151f4b34f8SAndrey Smetanin return 0; 6161f4b34f8SAndrey Smetanin } 6171f4b34f8SAndrey Smetanin 618aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id, 619ac3e5fcaSAndrey Smetanin stimer->index, 620ac3e5fcaSAndrey Smetanin time_now, stimer->count); 621ac3e5fcaSAndrey Smetanin 6221f4b34f8SAndrey Smetanin hrtimer_start(&stimer->timer, 6231f4b34f8SAndrey Smetanin ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)), 6241f4b34f8SAndrey Smetanin HRTIMER_MODE_ABS); 6251f4b34f8SAndrey Smetanin return 0; 6261f4b34f8SAndrey Smetanin } 6271f4b34f8SAndrey Smetanin 6281f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config, 6291f4b34f8SAndrey Smetanin bool host) 6301f4b34f8SAndrey Smetanin { 6318644f771SVitaly Kuznetsov union hv_stimer_config new_config = {.as_uint64 = config}, 6328644f771SVitaly Kuznetsov old_config = {.as_uint64 = stimer->config.as_uint64}; 633aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 6341aa8a418SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 635e0121fa2SVitaly Kuznetsov struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 636dbcf3f96SVitaly Kuznetsov 637dbcf3f96SVitaly Kuznetsov if (!synic->active && !host) 638dbcf3f96SVitaly Kuznetsov return 1; 6396a058a1eSVitaly Kuznetsov 6401aa8a418SVitaly Kuznetsov if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode && 6411aa8a418SVitaly Kuznetsov !(hv_vcpu->cpuid_cache.features_edx & 6421aa8a418SVitaly Kuznetsov HV_STIMER_DIRECT_MODE_AVAILABLE))) 6431aa8a418SVitaly Kuznetsov return 1; 6441aa8a418SVitaly Kuznetsov 645aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id, 646ac3e5fcaSAndrey Smetanin stimer->index, config, host); 647ac3e5fcaSAndrey Smetanin 648f3b138c5SAndrey Smetanin stimer_cleanup(stimer); 6498644f771SVitaly Kuznetsov if (old_config.enable && 6508644f771SVitaly Kuznetsov !new_config.direct_mode && new_config.sintx == 0) 6516a058a1eSVitaly Kuznetsov new_config.enable = 0; 6526a058a1eSVitaly Kuznetsov stimer->config.as_uint64 = new_config.as_uint64; 6538644f771SVitaly Kuznetsov 654013cc6ebSVitaly Kuznetsov if (stimer->config.enable) 655f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 656013cc6ebSVitaly Kuznetsov 6571f4b34f8SAndrey Smetanin return 0; 6581f4b34f8SAndrey Smetanin } 6591f4b34f8SAndrey Smetanin 6601f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count, 6611f4b34f8SAndrey Smetanin bool host) 6621f4b34f8SAndrey Smetanin { 663aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 664e0121fa2SVitaly Kuznetsov struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu); 665dbcf3f96SVitaly Kuznetsov 666dbcf3f96SVitaly Kuznetsov if (!synic->active && !host) 667dbcf3f96SVitaly Kuznetsov return 1; 668dbcf3f96SVitaly Kuznetsov 669aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id, 670ac3e5fcaSAndrey Smetanin stimer->index, count, host); 671ac3e5fcaSAndrey Smetanin 6721f4b34f8SAndrey Smetanin stimer_cleanup(stimer); 673f3b138c5SAndrey Smetanin stimer->count = count; 6741f4b34f8SAndrey Smetanin if (stimer->count == 0) 6756a058a1eSVitaly Kuznetsov stimer->config.enable = 0; 6766a058a1eSVitaly Kuznetsov else if (stimer->config.auto_enable) 6776a058a1eSVitaly Kuznetsov stimer->config.enable = 1; 678013cc6ebSVitaly Kuznetsov 679013cc6ebSVitaly Kuznetsov if (stimer->config.enable) 680f3b138c5SAndrey Smetanin stimer_mark_pending(stimer, false); 681013cc6ebSVitaly Kuznetsov 6821f4b34f8SAndrey Smetanin return 0; 6831f4b34f8SAndrey Smetanin } 6841f4b34f8SAndrey Smetanin 6851f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig) 6861f4b34f8SAndrey Smetanin { 6876a058a1eSVitaly Kuznetsov *pconfig = stimer->config.as_uint64; 6881f4b34f8SAndrey Smetanin return 0; 6891f4b34f8SAndrey Smetanin } 6901f4b34f8SAndrey Smetanin 6911f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount) 6921f4b34f8SAndrey Smetanin { 6931f4b34f8SAndrey Smetanin *pcount = stimer->count; 6941f4b34f8SAndrey Smetanin return 0; 6951f4b34f8SAndrey Smetanin } 6961f4b34f8SAndrey Smetanin 6971f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint, 6987deec5e0SRoman Kagan struct hv_message *src_msg, bool no_retry) 6991f4b34f8SAndrey Smetanin { 700e0121fa2SVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic); 7013a0e7731SRoman Kagan int msg_off = offsetof(struct hv_message_page, sint_message[sint]); 7023a0e7731SRoman Kagan gfn_t msg_page_gfn; 7033a0e7731SRoman Kagan struct hv_message_header hv_hdr; 7041f4b34f8SAndrey Smetanin int r; 7051f4b34f8SAndrey Smetanin 7061f4b34f8SAndrey Smetanin if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE)) 7071f4b34f8SAndrey Smetanin return -ENOENT; 7081f4b34f8SAndrey Smetanin 7093a0e7731SRoman Kagan msg_page_gfn = synic->msg_page >> PAGE_SHIFT; 7101f4b34f8SAndrey Smetanin 7113a0e7731SRoman Kagan /* 7123a0e7731SRoman Kagan * Strictly following the spec-mandated ordering would assume setting 7133a0e7731SRoman Kagan * .msg_pending before checking .message_type. However, this function 7143a0e7731SRoman Kagan * is only called in vcpu context so the entire update is atomic from 7153a0e7731SRoman Kagan * guest POV and thus the exact order here doesn't matter. 7163a0e7731SRoman Kagan */ 7173a0e7731SRoman Kagan r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type, 7183a0e7731SRoman Kagan msg_off + offsetof(struct hv_message, 7193a0e7731SRoman Kagan header.message_type), 7203a0e7731SRoman Kagan sizeof(hv_hdr.message_type)); 7213a0e7731SRoman Kagan if (r < 0) 7221f4b34f8SAndrey Smetanin return r; 7233a0e7731SRoman Kagan 7243a0e7731SRoman Kagan if (hv_hdr.message_type != HVMSG_NONE) { 7257deec5e0SRoman Kagan if (no_retry) 7267deec5e0SRoman Kagan return 0; 7277deec5e0SRoman Kagan 7283a0e7731SRoman Kagan hv_hdr.message_flags.msg_pending = 1; 7293a0e7731SRoman Kagan r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, 7303a0e7731SRoman Kagan &hv_hdr.message_flags, 7313a0e7731SRoman Kagan msg_off + 7323a0e7731SRoman Kagan offsetof(struct hv_message, 7333a0e7731SRoman Kagan header.message_flags), 7343a0e7731SRoman Kagan sizeof(hv_hdr.message_flags)); 7353a0e7731SRoman Kagan if (r < 0) 7363a0e7731SRoman Kagan return r; 7373a0e7731SRoman Kagan return -EAGAIN; 7383a0e7731SRoman Kagan } 7393a0e7731SRoman Kagan 7403a0e7731SRoman Kagan r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off, 7413a0e7731SRoman Kagan sizeof(src_msg->header) + 7423a0e7731SRoman Kagan src_msg->header.payload_size); 7433a0e7731SRoman Kagan if (r < 0) 7443a0e7731SRoman Kagan return r; 7453a0e7731SRoman Kagan 7463a0e7731SRoman Kagan r = synic_set_irq(synic, sint); 7473a0e7731SRoman Kagan if (r < 0) 7483a0e7731SRoman Kagan return r; 7493a0e7731SRoman Kagan if (r == 0) 7503a0e7731SRoman Kagan return -EFAULT; 7513a0e7731SRoman Kagan return 0; 7521f4b34f8SAndrey Smetanin } 7531f4b34f8SAndrey Smetanin 7540cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer) 7551f4b34f8SAndrey Smetanin { 756aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 7571f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 7581f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 7591f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 7601f4b34f8SAndrey Smetanin 7617deec5e0SRoman Kagan /* 7627deec5e0SRoman Kagan * To avoid piling up periodic ticks, don't retry message 7637deec5e0SRoman Kagan * delivery for them (within "lazy" lost ticks policy). 7647deec5e0SRoman Kagan */ 7656a058a1eSVitaly Kuznetsov bool no_retry = stimer->config.periodic; 7667deec5e0SRoman Kagan 7671f4b34f8SAndrey Smetanin payload->expiration_time = stimer->exp_time; 7681f4b34f8SAndrey Smetanin payload->delivery_time = get_time_ref_counter(vcpu->kvm); 769e0121fa2SVitaly Kuznetsov return synic_deliver_msg(to_hv_synic(vcpu), 7706a058a1eSVitaly Kuznetsov stimer->config.sintx, msg, 7717deec5e0SRoman Kagan no_retry); 7721f4b34f8SAndrey Smetanin } 7731f4b34f8SAndrey Smetanin 7748644f771SVitaly Kuznetsov static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer) 7758644f771SVitaly Kuznetsov { 776aafa97fdSVitaly Kuznetsov struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer); 7778644f771SVitaly Kuznetsov struct kvm_lapic_irq irq = { 7788644f771SVitaly Kuznetsov .delivery_mode = APIC_DM_FIXED, 7798644f771SVitaly Kuznetsov .vector = stimer->config.apic_vector 7808644f771SVitaly Kuznetsov }; 7818644f771SVitaly Kuznetsov 782a073d7e3SWanpeng Li if (lapic_in_kernel(vcpu)) 7838644f771SVitaly Kuznetsov return !kvm_apic_set_irq(vcpu, &irq, NULL); 784a073d7e3SWanpeng Li return 0; 7858644f771SVitaly Kuznetsov } 7868644f771SVitaly Kuznetsov 7871f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) 7881f4b34f8SAndrey Smetanin { 7898644f771SVitaly Kuznetsov int r, direct = stimer->config.direct_mode; 790ac3e5fcaSAndrey Smetanin 7910cdeabb1SAndrey Smetanin stimer->msg_pending = true; 7928644f771SVitaly Kuznetsov if (!direct) 793ac3e5fcaSAndrey Smetanin r = stimer_send_msg(stimer); 7948644f771SVitaly Kuznetsov else 7958644f771SVitaly Kuznetsov r = stimer_notify_direct(stimer); 796aafa97fdSVitaly Kuznetsov trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id, 7978644f771SVitaly Kuznetsov stimer->index, direct, r); 798ac3e5fcaSAndrey Smetanin if (!r) { 7990cdeabb1SAndrey Smetanin stimer->msg_pending = false; 8006a058a1eSVitaly Kuznetsov if (!(stimer->config.periodic)) 8016a058a1eSVitaly Kuznetsov stimer->config.enable = 0; 8020cdeabb1SAndrey Smetanin } 8031f4b34f8SAndrey Smetanin } 8041f4b34f8SAndrey Smetanin 8051f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu) 8061f4b34f8SAndrey Smetanin { 807ef3f3980SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 8081f4b34f8SAndrey Smetanin struct kvm_vcpu_hv_stimer *stimer; 809f3b138c5SAndrey Smetanin u64 time_now, exp_time; 8101f4b34f8SAndrey Smetanin int i; 8111f4b34f8SAndrey Smetanin 812f2bc14b6SVitaly Kuznetsov if (!hv_vcpu) 813f2bc14b6SVitaly Kuznetsov return; 814f2bc14b6SVitaly Kuznetsov 8151f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 8161f4b34f8SAndrey Smetanin if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) { 8171f4b34f8SAndrey Smetanin stimer = &hv_vcpu->stimer[i]; 8186a058a1eSVitaly Kuznetsov if (stimer->config.enable) { 819f3b138c5SAndrey Smetanin exp_time = stimer->exp_time; 8200cdeabb1SAndrey Smetanin 821f3b138c5SAndrey Smetanin if (exp_time) { 822f3b138c5SAndrey Smetanin time_now = 823f3b138c5SAndrey Smetanin get_time_ref_counter(vcpu->kvm); 824f3b138c5SAndrey Smetanin if (time_now >= exp_time) 825f3b138c5SAndrey Smetanin stimer_expiration(stimer); 826f3b138c5SAndrey Smetanin } 827f3b138c5SAndrey Smetanin 8286a058a1eSVitaly Kuznetsov if ((stimer->config.enable) && 829f1ff89ecSRoman Kagan stimer->count) { 830f1ff89ecSRoman Kagan if (!stimer->msg_pending) 8310cdeabb1SAndrey Smetanin stimer_start(stimer); 832f1ff89ecSRoman Kagan } else 8330cdeabb1SAndrey Smetanin stimer_cleanup(stimer); 8341f4b34f8SAndrey Smetanin } 8351f4b34f8SAndrey Smetanin } 8361f4b34f8SAndrey Smetanin } 8371f4b34f8SAndrey Smetanin 8381f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu) 8391f4b34f8SAndrey Smetanin { 840ef3f3980SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 8411f4b34f8SAndrey Smetanin int i; 8421f4b34f8SAndrey Smetanin 843fc08b628SVitaly Kuznetsov if (!hv_vcpu) 844fc08b628SVitaly Kuznetsov return; 845fc08b628SVitaly Kuznetsov 8461f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 8471f4b34f8SAndrey Smetanin stimer_cleanup(&hv_vcpu->stimer[i]); 8484592b7eaSVitaly Kuznetsov 8494592b7eaSVitaly Kuznetsov kfree(hv_vcpu); 8504592b7eaSVitaly Kuznetsov vcpu->arch.hyperv = NULL; 8511f4b34f8SAndrey Smetanin } 8521f4b34f8SAndrey Smetanin 85372bbf935SLadi Prosek bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu) 85472bbf935SLadi Prosek { 8559ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 8569ff5e030SVitaly Kuznetsov 857f2bc14b6SVitaly Kuznetsov if (!hv_vcpu) 858f2bc14b6SVitaly Kuznetsov return false; 859f2bc14b6SVitaly Kuznetsov 8609ff5e030SVitaly Kuznetsov if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) 86172bbf935SLadi Prosek return false; 86272bbf935SLadi Prosek return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED; 86372bbf935SLadi Prosek } 86472bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled); 86572bbf935SLadi Prosek 86672bbf935SLadi Prosek bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu, 86772bbf935SLadi Prosek struct hv_vp_assist_page *assist_page) 86872bbf935SLadi Prosek { 86972bbf935SLadi Prosek if (!kvm_hv_assist_page_enabled(vcpu)) 87072bbf935SLadi Prosek return false; 87172bbf935SLadi Prosek return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, 87272bbf935SLadi Prosek assist_page, sizeof(*assist_page)); 87372bbf935SLadi Prosek } 87472bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page); 87572bbf935SLadi Prosek 8761f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer) 8771f4b34f8SAndrey Smetanin { 8781f4b34f8SAndrey Smetanin struct hv_message *msg = &stimer->msg; 8791f4b34f8SAndrey Smetanin struct hv_timer_message_payload *payload = 8801f4b34f8SAndrey Smetanin (struct hv_timer_message_payload *)&msg->u.payload; 8811f4b34f8SAndrey Smetanin 8821f4b34f8SAndrey Smetanin memset(&msg->header, 0, sizeof(msg->header)); 8831f4b34f8SAndrey Smetanin msg->header.message_type = HVMSG_TIMER_EXPIRED; 8841f4b34f8SAndrey Smetanin msg->header.payload_size = sizeof(*payload); 8851f4b34f8SAndrey Smetanin 8861f4b34f8SAndrey Smetanin payload->timer_index = stimer->index; 8871f4b34f8SAndrey Smetanin payload->expiration_time = 0; 8881f4b34f8SAndrey Smetanin payload->delivery_time = 0; 8891f4b34f8SAndrey Smetanin } 8901f4b34f8SAndrey Smetanin 8911f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index) 8921f4b34f8SAndrey Smetanin { 8931f4b34f8SAndrey Smetanin memset(stimer, 0, sizeof(*stimer)); 8941f4b34f8SAndrey Smetanin stimer->index = timer_index; 8951f4b34f8SAndrey Smetanin hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 8961f4b34f8SAndrey Smetanin stimer->timer.function = stimer_timer_callback; 8971f4b34f8SAndrey Smetanin stimer_prepare_msg(stimer); 8981f4b34f8SAndrey Smetanin } 8991f4b34f8SAndrey Smetanin 900fc08b628SVitaly Kuznetsov static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu) 9015c919412SAndrey Smetanin { 9024592b7eaSVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu; 9031f4b34f8SAndrey Smetanin int i; 9041f4b34f8SAndrey Smetanin 9054592b7eaSVitaly Kuznetsov hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT); 9064592b7eaSVitaly Kuznetsov if (!hv_vcpu) 9074592b7eaSVitaly Kuznetsov return -ENOMEM; 9084592b7eaSVitaly Kuznetsov 9094592b7eaSVitaly Kuznetsov vcpu->arch.hyperv = hv_vcpu; 9104592b7eaSVitaly Kuznetsov hv_vcpu->vcpu = vcpu; 9114592b7eaSVitaly Kuznetsov 9121f4b34f8SAndrey Smetanin synic_init(&hv_vcpu->synic); 9131f4b34f8SAndrey Smetanin 9141f4b34f8SAndrey Smetanin bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); 9151f4b34f8SAndrey Smetanin for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++) 9161f4b34f8SAndrey Smetanin stimer_init(&hv_vcpu->stimer[i], i); 9174592b7eaSVitaly Kuznetsov 918d3457c87SRoman Kagan hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu); 919fc08b628SVitaly Kuznetsov 920fc08b628SVitaly Kuznetsov return 0; 921d3457c87SRoman Kagan } 922d3457c87SRoman Kagan 923efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages) 9245c919412SAndrey Smetanin { 925fc08b628SVitaly Kuznetsov struct kvm_vcpu_hv_synic *synic; 926fc08b628SVitaly Kuznetsov int r; 927fc08b628SVitaly Kuznetsov 928fc08b628SVitaly Kuznetsov if (!to_hv_vcpu(vcpu)) { 929fc08b628SVitaly Kuznetsov r = kvm_hv_vcpu_init(vcpu); 930fc08b628SVitaly Kuznetsov if (r) 931fc08b628SVitaly Kuznetsov return r; 932fc08b628SVitaly Kuznetsov } 933fc08b628SVitaly Kuznetsov 934fc08b628SVitaly Kuznetsov synic = to_hv_synic(vcpu); 935efc479e6SRoman Kagan 9365c919412SAndrey Smetanin /* 9375c919412SAndrey Smetanin * Hyper-V SynIC auto EOI SINT's are 938f4fdc0a2SSuravee Suthikulpanit * not compatible with APICV, so request 939f4fdc0a2SSuravee Suthikulpanit * to deactivate APICV permanently. 9405c919412SAndrey Smetanin */ 941f4fdc0a2SSuravee Suthikulpanit kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV); 942efc479e6SRoman Kagan synic->active = true; 943efc479e6SRoman Kagan synic->dont_zero_synic_pages = dont_zero_synic_pages; 94499b48eccSJon Doron synic->control = HV_SYNIC_CONTROL_ENABLE; 9455c919412SAndrey Smetanin return 0; 9465c919412SAndrey Smetanin } 9475c919412SAndrey Smetanin 948e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr) 949e83d5887SAndrey Smetanin { 950e83d5887SAndrey Smetanin bool r = false; 951e83d5887SAndrey Smetanin 952e83d5887SAndrey Smetanin switch (msr) { 953e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 954e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 955e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 956e83d5887SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 957e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 958e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 959e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 960a2e164e7SVitaly Kuznetsov case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 961a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_CONTROL: 962a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_STATUS: 963f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_OPTIONS: 964f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 965e83d5887SAndrey Smetanin r = true; 966e83d5887SAndrey Smetanin break; 967e83d5887SAndrey Smetanin } 968e83d5887SAndrey Smetanin 969e83d5887SAndrey Smetanin return r; 970e83d5887SAndrey Smetanin } 971e83d5887SAndrey Smetanin 97205f04ae4SVitaly Kuznetsov static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata) 973e7d9513bSAndrey Smetanin { 97405f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 97586187937SMarios Pomonis size_t size = ARRAY_SIZE(hv->hv_crash_param); 976e7d9513bSAndrey Smetanin 97786187937SMarios Pomonis if (WARN_ON_ONCE(index >= size)) 978e7d9513bSAndrey Smetanin return -EINVAL; 979e7d9513bSAndrey Smetanin 98086187937SMarios Pomonis *pdata = hv->hv_crash_param[array_index_nospec(index, size)]; 981e7d9513bSAndrey Smetanin return 0; 982e7d9513bSAndrey Smetanin } 983e7d9513bSAndrey Smetanin 98405f04ae4SVitaly Kuznetsov static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata) 985e7d9513bSAndrey Smetanin { 98605f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 987e7d9513bSAndrey Smetanin 988e7d9513bSAndrey Smetanin *pdata = hv->hv_crash_ctl; 989e7d9513bSAndrey Smetanin return 0; 990e7d9513bSAndrey Smetanin } 991e7d9513bSAndrey Smetanin 99205f04ae4SVitaly Kuznetsov static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data) 993e7d9513bSAndrey Smetanin { 99405f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 995e7d9513bSAndrey Smetanin 996a4987defSVitaly Kuznetsov hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY; 997e7d9513bSAndrey Smetanin 998e7d9513bSAndrey Smetanin return 0; 999e7d9513bSAndrey Smetanin } 1000e7d9513bSAndrey Smetanin 100105f04ae4SVitaly Kuznetsov static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data) 1002e7d9513bSAndrey Smetanin { 100305f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 100486187937SMarios Pomonis size_t size = ARRAY_SIZE(hv->hv_crash_param); 1005e7d9513bSAndrey Smetanin 100686187937SMarios Pomonis if (WARN_ON_ONCE(index >= size)) 1007e7d9513bSAndrey Smetanin return -EINVAL; 1008e7d9513bSAndrey Smetanin 100986187937SMarios Pomonis hv->hv_crash_param[array_index_nospec(index, size)] = data; 1010e7d9513bSAndrey Smetanin return 0; 1011e7d9513bSAndrey Smetanin } 1012e7d9513bSAndrey Smetanin 1013095cf55dSPaolo Bonzini /* 1014095cf55dSPaolo Bonzini * The kvmclock and Hyper-V TSC page use similar formulas, and converting 1015095cf55dSPaolo Bonzini * between them is possible: 1016095cf55dSPaolo Bonzini * 1017095cf55dSPaolo Bonzini * kvmclock formula: 1018095cf55dSPaolo Bonzini * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32) 1019095cf55dSPaolo Bonzini * + system_time 1020095cf55dSPaolo Bonzini * 1021095cf55dSPaolo Bonzini * Hyper-V formula: 1022095cf55dSPaolo Bonzini * nsec/100 = ticks * scale / 2^64 + offset 1023095cf55dSPaolo Bonzini * 1024095cf55dSPaolo Bonzini * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula. 1025095cf55dSPaolo Bonzini * By dividing the kvmclock formula by 100 and equating what's left we get: 1026095cf55dSPaolo Bonzini * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1027095cf55dSPaolo Bonzini * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100 1028095cf55dSPaolo Bonzini * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100 1029095cf55dSPaolo Bonzini * 1030095cf55dSPaolo Bonzini * Now expand the kvmclock formula and divide by 100: 1031095cf55dSPaolo Bonzini * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32) 1032095cf55dSPaolo Bonzini * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) 1033095cf55dSPaolo Bonzini * + system_time 1034095cf55dSPaolo Bonzini * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1035095cf55dSPaolo Bonzini * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100 1036095cf55dSPaolo Bonzini * + system_time / 100 1037095cf55dSPaolo Bonzini * 1038095cf55dSPaolo Bonzini * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64: 1039095cf55dSPaolo Bonzini * nsec/100 = ticks * scale / 2^64 1040095cf55dSPaolo Bonzini * - tsc_timestamp * scale / 2^64 1041095cf55dSPaolo Bonzini * + system_time / 100 1042095cf55dSPaolo Bonzini * 1043095cf55dSPaolo Bonzini * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out: 1044095cf55dSPaolo Bonzini * offset = system_time / 100 - tsc_timestamp * scale / 2^64 1045095cf55dSPaolo Bonzini * 1046095cf55dSPaolo Bonzini * These two equivalencies are implemented in this function. 1047095cf55dSPaolo Bonzini */ 1048095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock, 10497357b1dfSMichael Kelley struct ms_hyperv_tsc_page *tsc_ref) 1050095cf55dSPaolo Bonzini { 1051095cf55dSPaolo Bonzini u64 max_mul; 1052095cf55dSPaolo Bonzini 1053095cf55dSPaolo Bonzini if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT)) 1054095cf55dSPaolo Bonzini return false; 1055095cf55dSPaolo Bonzini 1056095cf55dSPaolo Bonzini /* 1057095cf55dSPaolo Bonzini * check if scale would overflow, if so we use the time ref counter 1058095cf55dSPaolo Bonzini * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64 1059095cf55dSPaolo Bonzini * tsc_to_system_mul / 100 >= 2^(32-tsc_shift) 1060095cf55dSPaolo Bonzini * tsc_to_system_mul >= 100 * 2^(32-tsc_shift) 1061095cf55dSPaolo Bonzini */ 1062095cf55dSPaolo Bonzini max_mul = 100ull << (32 - hv_clock->tsc_shift); 1063095cf55dSPaolo Bonzini if (hv_clock->tsc_to_system_mul >= max_mul) 1064095cf55dSPaolo Bonzini return false; 1065095cf55dSPaolo Bonzini 1066095cf55dSPaolo Bonzini /* 1067095cf55dSPaolo Bonzini * Otherwise compute the scale and offset according to the formulas 1068095cf55dSPaolo Bonzini * derived above. 1069095cf55dSPaolo Bonzini */ 1070095cf55dSPaolo Bonzini tsc_ref->tsc_scale = 1071095cf55dSPaolo Bonzini mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift), 1072095cf55dSPaolo Bonzini hv_clock->tsc_to_system_mul, 1073095cf55dSPaolo Bonzini 100); 1074095cf55dSPaolo Bonzini 1075095cf55dSPaolo Bonzini tsc_ref->tsc_offset = hv_clock->system_time; 1076095cf55dSPaolo Bonzini do_div(tsc_ref->tsc_offset, 100); 1077095cf55dSPaolo Bonzini tsc_ref->tsc_offset -= 1078095cf55dSPaolo Bonzini mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64); 1079095cf55dSPaolo Bonzini return true; 1080095cf55dSPaolo Bonzini } 1081095cf55dSPaolo Bonzini 10820469f2f7SVitaly Kuznetsov /* 10830469f2f7SVitaly Kuznetsov * Don't touch TSC page values if the guest has opted for TSC emulation after 10840469f2f7SVitaly Kuznetsov * migration. KVM doesn't fully support reenlightenment notifications and TSC 10850469f2f7SVitaly Kuznetsov * access emulation and Hyper-V is known to expect the values in TSC page to 10860469f2f7SVitaly Kuznetsov * stay constant before TSC access emulation is disabled from guest side 10870469f2f7SVitaly Kuznetsov * (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC 10880469f2f7SVitaly Kuznetsov * frequency and guest visible TSC value across migration (and prevent it when 10890469f2f7SVitaly Kuznetsov * TSC scaling is unsupported). 10900469f2f7SVitaly Kuznetsov */ 10910469f2f7SVitaly Kuznetsov static inline bool tsc_page_update_unsafe(struct kvm_hv *hv) 10920469f2f7SVitaly Kuznetsov { 10930469f2f7SVitaly Kuznetsov return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) && 10940469f2f7SVitaly Kuznetsov hv->hv_tsc_emulation_control; 10950469f2f7SVitaly Kuznetsov } 10960469f2f7SVitaly Kuznetsov 1097095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm, 1098095cf55dSPaolo Bonzini struct pvclock_vcpu_time_info *hv_clock) 1099095cf55dSPaolo Bonzini { 110005f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 1101095cf55dSPaolo Bonzini u32 tsc_seq; 1102095cf55dSPaolo Bonzini u64 gfn; 1103095cf55dSPaolo Bonzini 1104095cf55dSPaolo Bonzini BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence)); 11057357b1dfSMichael Kelley BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0); 1106095cf55dSPaolo Bonzini 1107cc9cfddbSVitaly Kuznetsov if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN || 1108cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET) 1109095cf55dSPaolo Bonzini return; 1110095cf55dSPaolo Bonzini 111105f04ae4SVitaly Kuznetsov mutex_lock(&hv->hv_lock); 11123f5ad8beSPaolo Bonzini if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 11133f5ad8beSPaolo Bonzini goto out_unlock; 11143f5ad8beSPaolo Bonzini 1115095cf55dSPaolo Bonzini gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; 1116095cf55dSPaolo Bonzini /* 1117095cf55dSPaolo Bonzini * Because the TSC parameters only vary when there is a 1118095cf55dSPaolo Bonzini * change in the master clock, do not bother with caching. 1119095cf55dSPaolo Bonzini */ 1120095cf55dSPaolo Bonzini if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn), 1121095cf55dSPaolo Bonzini &tsc_seq, sizeof(tsc_seq)))) 1122cc9cfddbSVitaly Kuznetsov goto out_err; 1123095cf55dSPaolo Bonzini 11240469f2f7SVitaly Kuznetsov if (tsc_seq && tsc_page_update_unsafe(hv)) { 11250469f2f7SVitaly Kuznetsov if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) 11260469f2f7SVitaly Kuznetsov goto out_err; 11270469f2f7SVitaly Kuznetsov 11280469f2f7SVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_SET; 11290469f2f7SVitaly Kuznetsov goto out_unlock; 11300469f2f7SVitaly Kuznetsov } 11310469f2f7SVitaly Kuznetsov 1132095cf55dSPaolo Bonzini /* 1133095cf55dSPaolo Bonzini * While we're computing and writing the parameters, force the 1134095cf55dSPaolo Bonzini * guest to use the time reference count MSR. 1135095cf55dSPaolo Bonzini */ 1136095cf55dSPaolo Bonzini hv->tsc_ref.tsc_sequence = 0; 1137095cf55dSPaolo Bonzini if (kvm_write_guest(kvm, gfn_to_gpa(gfn), 1138095cf55dSPaolo Bonzini &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) 1139cc9cfddbSVitaly Kuznetsov goto out_err; 1140095cf55dSPaolo Bonzini 1141095cf55dSPaolo Bonzini if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref)) 1142cc9cfddbSVitaly Kuznetsov goto out_err; 1143095cf55dSPaolo Bonzini 1144095cf55dSPaolo Bonzini /* Ensure sequence is zero before writing the rest of the struct. */ 1145095cf55dSPaolo Bonzini smp_wmb(); 1146095cf55dSPaolo Bonzini if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref))) 1147cc9cfddbSVitaly Kuznetsov goto out_err; 1148095cf55dSPaolo Bonzini 1149095cf55dSPaolo Bonzini /* 1150095cf55dSPaolo Bonzini * Now switch to the TSC page mechanism by writing the sequence. 1151095cf55dSPaolo Bonzini */ 1152095cf55dSPaolo Bonzini tsc_seq++; 1153095cf55dSPaolo Bonzini if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0) 1154095cf55dSPaolo Bonzini tsc_seq = 1; 1155095cf55dSPaolo Bonzini 1156095cf55dSPaolo Bonzini /* Write the struct entirely before the non-zero sequence. */ 1157095cf55dSPaolo Bonzini smp_wmb(); 1158095cf55dSPaolo Bonzini 1159095cf55dSPaolo Bonzini hv->tsc_ref.tsc_sequence = tsc_seq; 1160cc9cfddbSVitaly Kuznetsov if (kvm_write_guest(kvm, gfn_to_gpa(gfn), 1161cc9cfddbSVitaly Kuznetsov &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) 1162cc9cfddbSVitaly Kuznetsov goto out_err; 1163cc9cfddbSVitaly Kuznetsov 1164cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_SET; 1165cc9cfddbSVitaly Kuznetsov goto out_unlock; 1166cc9cfddbSVitaly Kuznetsov 1167cc9cfddbSVitaly Kuznetsov out_err: 1168cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN; 11693f5ad8beSPaolo Bonzini out_unlock: 117005f04ae4SVitaly Kuznetsov mutex_unlock(&hv->hv_lock); 1171095cf55dSPaolo Bonzini } 1172095cf55dSPaolo Bonzini 1173e880c6eaSVitaly Kuznetsov void kvm_hv_invalidate_tsc_page(struct kvm *kvm) 1174e880c6eaSVitaly Kuznetsov { 1175e880c6eaSVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 1176e880c6eaSVitaly Kuznetsov u64 gfn; 1177da6d63a0SWanpeng Li int idx; 1178e880c6eaSVitaly Kuznetsov 1179cc9cfddbSVitaly Kuznetsov if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN || 11800469f2f7SVitaly Kuznetsov hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET || 11810469f2f7SVitaly Kuznetsov tsc_page_update_unsafe(hv)) 1182e880c6eaSVitaly Kuznetsov return; 1183e880c6eaSVitaly Kuznetsov 1184e880c6eaSVitaly Kuznetsov mutex_lock(&hv->hv_lock); 1185e880c6eaSVitaly Kuznetsov 1186e880c6eaSVitaly Kuznetsov if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) 1187e880c6eaSVitaly Kuznetsov goto out_unlock; 1188e880c6eaSVitaly Kuznetsov 1189cc9cfddbSVitaly Kuznetsov /* Preserve HV_TSC_PAGE_GUEST_CHANGED/HV_TSC_PAGE_HOST_CHANGED states */ 1190cc9cfddbSVitaly Kuznetsov if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET) 1191cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_UPDATING; 1192cc9cfddbSVitaly Kuznetsov 1193e880c6eaSVitaly Kuznetsov gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; 1194e880c6eaSVitaly Kuznetsov 1195e880c6eaSVitaly Kuznetsov hv->tsc_ref.tsc_sequence = 0; 1196da6d63a0SWanpeng Li 1197da6d63a0SWanpeng Li /* 1198da6d63a0SWanpeng Li * Take the srcu lock as memslots will be accessed to check the gfn 1199da6d63a0SWanpeng Li * cache generation against the memslots generation. 1200da6d63a0SWanpeng Li */ 1201da6d63a0SWanpeng Li idx = srcu_read_lock(&kvm->srcu); 1202cc9cfddbSVitaly Kuznetsov if (kvm_write_guest(kvm, gfn_to_gpa(gfn), 1203cc9cfddbSVitaly Kuznetsov &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) 1204cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN; 1205da6d63a0SWanpeng Li srcu_read_unlock(&kvm->srcu, idx); 1206e880c6eaSVitaly Kuznetsov 1207e880c6eaSVitaly Kuznetsov out_unlock: 1208e880c6eaSVitaly Kuznetsov mutex_unlock(&hv->hv_lock); 1209e880c6eaSVitaly Kuznetsov } 1210e880c6eaSVitaly Kuznetsov 1211b4128000SVitaly Kuznetsov 1212b4128000SVitaly Kuznetsov static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr) 1213b4128000SVitaly Kuznetsov { 12141561c2cbSVitaly Kuznetsov if (!hv_vcpu->enforce_cpuid) 12151561c2cbSVitaly Kuznetsov return true; 12161561c2cbSVitaly Kuznetsov 12171561c2cbSVitaly Kuznetsov switch (msr) { 12181561c2cbSVitaly Kuznetsov case HV_X64_MSR_GUEST_OS_ID: 12191561c2cbSVitaly Kuznetsov case HV_X64_MSR_HYPERCALL: 12201561c2cbSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 12211561c2cbSVitaly Kuznetsov HV_MSR_HYPERCALL_AVAILABLE; 1222b80a92ffSVitaly Kuznetsov case HV_X64_MSR_VP_RUNTIME: 1223b80a92ffSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1224b80a92ffSVitaly Kuznetsov HV_MSR_VP_RUNTIME_AVAILABLE; 1225c2b32867SVitaly Kuznetsov case HV_X64_MSR_TIME_REF_COUNT: 1226c2b32867SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1227c2b32867SVitaly Kuznetsov HV_MSR_TIME_REF_COUNT_AVAILABLE; 1228d2ac25d4SVitaly Kuznetsov case HV_X64_MSR_VP_INDEX: 1229d2ac25d4SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1230d2ac25d4SVitaly Kuznetsov HV_MSR_VP_INDEX_AVAILABLE; 1231679008e4SVitaly Kuznetsov case HV_X64_MSR_RESET: 1232679008e4SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1233679008e4SVitaly Kuznetsov HV_MSR_RESET_AVAILABLE; 1234a1ec661cSVitaly Kuznetsov case HV_X64_MSR_REFERENCE_TSC: 1235a1ec661cSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1236a1ec661cSVitaly Kuznetsov HV_MSR_REFERENCE_TSC_AVAILABLE; 12379e2715caSVitaly Kuznetsov case HV_X64_MSR_SCONTROL: 12389e2715caSVitaly Kuznetsov case HV_X64_MSR_SVERSION: 12399e2715caSVitaly Kuznetsov case HV_X64_MSR_SIEFP: 12409e2715caSVitaly Kuznetsov case HV_X64_MSR_SIMP: 12419e2715caSVitaly Kuznetsov case HV_X64_MSR_EOM: 12429e2715caSVitaly Kuznetsov case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 12439e2715caSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 12449e2715caSVitaly Kuznetsov HV_MSR_SYNIC_AVAILABLE; 1245eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER0_CONFIG: 1246eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER1_CONFIG: 1247eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER2_CONFIG: 1248eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER3_CONFIG: 1249eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER0_COUNT: 1250eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER1_COUNT: 1251eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER2_COUNT: 1252eba60ddaSVitaly Kuznetsov case HV_X64_MSR_STIMER3_COUNT: 1253eba60ddaSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1254eba60ddaSVitaly Kuznetsov HV_MSR_SYNTIMER_AVAILABLE; 1255978b5747SVitaly Kuznetsov case HV_X64_MSR_EOI: 1256978b5747SVitaly Kuznetsov case HV_X64_MSR_ICR: 1257978b5747SVitaly Kuznetsov case HV_X64_MSR_TPR: 1258978b5747SVitaly Kuznetsov case HV_X64_MSR_VP_ASSIST_PAGE: 1259978b5747SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1260978b5747SVitaly Kuznetsov HV_MSR_APIC_ACCESS_AVAILABLE; 1261978b5747SVitaly Kuznetsov break; 12629442f3bdSVitaly Kuznetsov case HV_X64_MSR_TSC_FREQUENCY: 12639442f3bdSVitaly Kuznetsov case HV_X64_MSR_APIC_FREQUENCY: 12649442f3bdSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 12659442f3bdSVitaly Kuznetsov HV_ACCESS_FREQUENCY_MSRS; 1266234d01baSVitaly Kuznetsov case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 1267234d01baSVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_CONTROL: 1268234d01baSVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_STATUS: 1269234d01baSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_eax & 1270234d01baSVitaly Kuznetsov HV_ACCESS_REENLIGHTENMENT; 12710a19c899SVitaly Kuznetsov case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 12720a19c899SVitaly Kuznetsov case HV_X64_MSR_CRASH_CTL: 12730a19c899SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_edx & 12740a19c899SVitaly Kuznetsov HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; 127517b6d517SVitaly Kuznetsov case HV_X64_MSR_SYNDBG_OPTIONS: 127617b6d517SVitaly Kuznetsov case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 127717b6d517SVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_edx & 127817b6d517SVitaly Kuznetsov HV_FEATURE_DEBUG_MSRS_AVAILABLE; 12791561c2cbSVitaly Kuznetsov default: 12801561c2cbSVitaly Kuznetsov break; 12811561c2cbSVitaly Kuznetsov } 12821561c2cbSVitaly Kuznetsov 1283d66bfa36SVitaly Kuznetsov return false; 1284b4128000SVitaly Kuznetsov } 1285b4128000SVitaly Kuznetsov 1286e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, 1287e7d9513bSAndrey Smetanin bool host) 1288e83d5887SAndrey Smetanin { 1289e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 129005f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 1291e83d5887SAndrey Smetanin 1292b4128000SVitaly Kuznetsov if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr))) 1293b4128000SVitaly Kuznetsov return 1; 1294b4128000SVitaly Kuznetsov 1295e83d5887SAndrey Smetanin switch (msr) { 1296e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 1297e83d5887SAndrey Smetanin hv->hv_guest_os_id = data; 1298e83d5887SAndrey Smetanin /* setting guest os id to zero disables hypercall page */ 1299e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 1300e83d5887SAndrey Smetanin hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE; 1301e83d5887SAndrey Smetanin break; 1302e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: { 130379033bebSJoao Martins u8 instructions[9]; 130479033bebSJoao Martins int i = 0; 130579033bebSJoao Martins u64 addr; 1306e83d5887SAndrey Smetanin 1307e83d5887SAndrey Smetanin /* if guest os id is not set hypercall should remain disabled */ 1308e83d5887SAndrey Smetanin if (!hv->hv_guest_os_id) 1309e83d5887SAndrey Smetanin break; 1310e83d5887SAndrey Smetanin if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) { 1311e83d5887SAndrey Smetanin hv->hv_hypercall = data; 1312e83d5887SAndrey Smetanin break; 1313e83d5887SAndrey Smetanin } 131479033bebSJoao Martins 131579033bebSJoao Martins /* 131679033bebSJoao Martins * If Xen and Hyper-V hypercalls are both enabled, disambiguate 131779033bebSJoao Martins * the same way Xen itself does, by setting the bit 31 of EAX 131879033bebSJoao Martins * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just 131979033bebSJoao Martins * going to be clobbered on 64-bit. 132079033bebSJoao Martins */ 132179033bebSJoao Martins if (kvm_xen_hypercall_enabled(kvm)) { 132279033bebSJoao Martins /* orl $0x80000000, %eax */ 132379033bebSJoao Martins instructions[i++] = 0x0d; 132479033bebSJoao Martins instructions[i++] = 0x00; 132579033bebSJoao Martins instructions[i++] = 0x00; 132679033bebSJoao Martins instructions[i++] = 0x00; 132779033bebSJoao Martins instructions[i++] = 0x80; 132879033bebSJoao Martins } 132979033bebSJoao Martins 133079033bebSJoao Martins /* vmcall/vmmcall */ 133179033bebSJoao Martins static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i); 133279033bebSJoao Martins i += 3; 133379033bebSJoao Martins 133479033bebSJoao Martins /* ret */ 133579033bebSJoao Martins ((unsigned char *)instructions)[i++] = 0xc3; 133679033bebSJoao Martins 133779033bebSJoao Martins addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK; 133879033bebSJoao Martins if (kvm_vcpu_write_guest(vcpu, addr, instructions, i)) 1339e83d5887SAndrey Smetanin return 1; 1340e83d5887SAndrey Smetanin hv->hv_hypercall = data; 1341e83d5887SAndrey Smetanin break; 1342e83d5887SAndrey Smetanin } 1343095cf55dSPaolo Bonzini case HV_X64_MSR_REFERENCE_TSC: 1344e83d5887SAndrey Smetanin hv->hv_tsc_page = data; 1345cc9cfddbSVitaly Kuznetsov if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) { 1346cc9cfddbSVitaly Kuznetsov if (!host) 1347cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED; 1348cc9cfddbSVitaly Kuznetsov else 1349cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED; 1350095cf55dSPaolo Bonzini kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); 1351cc9cfddbSVitaly Kuznetsov } else { 1352cc9cfddbSVitaly Kuznetsov hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET; 1353cc9cfddbSVitaly Kuznetsov } 1354e83d5887SAndrey Smetanin break; 1355e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 135605f04ae4SVitaly Kuznetsov return kvm_hv_msr_set_crash_data(kvm, 1357e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 1358e7d9513bSAndrey Smetanin data); 1359e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 136005f04ae4SVitaly Kuznetsov if (host) 136105f04ae4SVitaly Kuznetsov return kvm_hv_msr_set_crash_ctl(kvm, data); 136205f04ae4SVitaly Kuznetsov 136305f04ae4SVitaly Kuznetsov if (data & HV_CRASH_CTL_CRASH_NOTIFY) { 136405f04ae4SVitaly Kuznetsov vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n", 136505f04ae4SVitaly Kuznetsov hv->hv_crash_param[0], 136605f04ae4SVitaly Kuznetsov hv->hv_crash_param[1], 136705f04ae4SVitaly Kuznetsov hv->hv_crash_param[2], 136805f04ae4SVitaly Kuznetsov hv->hv_crash_param[3], 136905f04ae4SVitaly Kuznetsov hv->hv_crash_param[4]); 137005f04ae4SVitaly Kuznetsov 137105f04ae4SVitaly Kuznetsov /* Send notification about crash to user space */ 137205f04ae4SVitaly Kuznetsov kvm_make_request(KVM_REQ_HV_CRASH, vcpu); 137305f04ae4SVitaly Kuznetsov } 137405f04ae4SVitaly Kuznetsov break; 1375e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 1376e516cebbSAndrey Smetanin if (data == 1) { 1377e516cebbSAndrey Smetanin vcpu_debug(vcpu, "hyper-v reset requested\n"); 1378e516cebbSAndrey Smetanin kvm_make_request(KVM_REQ_HV_RESET, vcpu); 1379e516cebbSAndrey Smetanin } 1380e516cebbSAndrey Smetanin break; 1381a2e164e7SVitaly Kuznetsov case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 1382a2e164e7SVitaly Kuznetsov hv->hv_reenlightenment_control = data; 1383a2e164e7SVitaly Kuznetsov break; 1384a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_CONTROL: 1385a2e164e7SVitaly Kuznetsov hv->hv_tsc_emulation_control = data; 1386a2e164e7SVitaly Kuznetsov break; 1387a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_STATUS: 1388d2547cf5SVitaly Kuznetsov if (data && !host) 1389d2547cf5SVitaly Kuznetsov return 1; 1390d2547cf5SVitaly Kuznetsov 1391a2e164e7SVitaly Kuznetsov hv->hv_tsc_emulation_status = data; 1392a2e164e7SVitaly Kuznetsov break; 139344883f01SPaolo Bonzini case HV_X64_MSR_TIME_REF_COUNT: 139444883f01SPaolo Bonzini /* read-only, but still ignore it if host-initiated */ 139544883f01SPaolo Bonzini if (!host) 139644883f01SPaolo Bonzini return 1; 139744883f01SPaolo Bonzini break; 1398f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_OPTIONS: 1399f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 1400f97f5a56SJon Doron return syndbg_set_msr(vcpu, msr, data, host); 1401e83d5887SAndrey Smetanin default: 14022f9f5cddSMiaohe Lin vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", 1403e83d5887SAndrey Smetanin msr, data); 1404e83d5887SAndrey Smetanin return 1; 1405e83d5887SAndrey Smetanin } 1406e83d5887SAndrey Smetanin return 0; 1407e83d5887SAndrey Smetanin } 1408e83d5887SAndrey Smetanin 14099eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */ 14109eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void) 14119eec50b8SAndrey Smetanin { 14125613fda9SFrederic Weisbecker u64 utime, stime; 14139eec50b8SAndrey Smetanin 14149eec50b8SAndrey Smetanin task_cputime_adjusted(current, &utime, &stime); 14155613fda9SFrederic Weisbecker 14165613fda9SFrederic Weisbecker return div_u64(utime + stime, 100); 14179eec50b8SAndrey Smetanin } 14189eec50b8SAndrey Smetanin 14199eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1420e83d5887SAndrey Smetanin { 14219ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1422e83d5887SAndrey Smetanin 1423b4128000SVitaly Kuznetsov if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr))) 1424b4128000SVitaly Kuznetsov return 1; 1425b4128000SVitaly Kuznetsov 1426e83d5887SAndrey Smetanin switch (msr) { 142787ee613dSVitaly Kuznetsov case HV_X64_MSR_VP_INDEX: { 142805f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 142987ee613dSVitaly Kuznetsov int vcpu_idx = kvm_vcpu_get_idx(vcpu); 143087ee613dSVitaly Kuznetsov u32 new_vp_index = (u32)data; 143187ee613dSVitaly Kuznetsov 143287ee613dSVitaly Kuznetsov if (!host || new_vp_index >= KVM_MAX_VCPUS) 1433d3457c87SRoman Kagan return 1; 143487ee613dSVitaly Kuznetsov 143587ee613dSVitaly Kuznetsov if (new_vp_index == hv_vcpu->vp_index) 143687ee613dSVitaly Kuznetsov return 0; 143787ee613dSVitaly Kuznetsov 143887ee613dSVitaly Kuznetsov /* 143987ee613dSVitaly Kuznetsov * The VP index is initialized to vcpu_index by 144087ee613dSVitaly Kuznetsov * kvm_hv_vcpu_postcreate so they initially match. Now the 144187ee613dSVitaly Kuznetsov * VP index is changing, adjust num_mismatched_vp_indexes if 144287ee613dSVitaly Kuznetsov * it now matches or no longer matches vcpu_idx. 144387ee613dSVitaly Kuznetsov */ 144487ee613dSVitaly Kuznetsov if (hv_vcpu->vp_index == vcpu_idx) 144587ee613dSVitaly Kuznetsov atomic_inc(&hv->num_mismatched_vp_indexes); 144687ee613dSVitaly Kuznetsov else if (new_vp_index == vcpu_idx) 144787ee613dSVitaly Kuznetsov atomic_dec(&hv->num_mismatched_vp_indexes); 144887ee613dSVitaly Kuznetsov 144987ee613dSVitaly Kuznetsov hv_vcpu->vp_index = new_vp_index; 1450d3457c87SRoman Kagan break; 145187ee613dSVitaly Kuznetsov } 1452d4abc577SLadi Prosek case HV_X64_MSR_VP_ASSIST_PAGE: { 1453e83d5887SAndrey Smetanin u64 gfn; 1454e83d5887SAndrey Smetanin unsigned long addr; 1455e83d5887SAndrey Smetanin 1456d4abc577SLadi Prosek if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) { 14571779a39fSVitaly Kuznetsov hv_vcpu->hv_vapic = data; 145872bbf935SLadi Prosek if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0)) 1459e83d5887SAndrey Smetanin return 1; 1460e83d5887SAndrey Smetanin break; 1461e83d5887SAndrey Smetanin } 1462d4abc577SLadi Prosek gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT; 1463e83d5887SAndrey Smetanin addr = kvm_vcpu_gfn_to_hva(vcpu, gfn); 1464e83d5887SAndrey Smetanin if (kvm_is_error_hva(addr)) 1465e83d5887SAndrey Smetanin return 1; 146612e0c618SVitaly Kuznetsov 146712e0c618SVitaly Kuznetsov /* 146867b0ae43SMiaohe Lin * Clear apic_assist portion of struct hv_vp_assist_page 146912e0c618SVitaly Kuznetsov * only, there can be valuable data in the rest which needs 147012e0c618SVitaly Kuznetsov * to be preserved e.g. on migration. 147112e0c618SVitaly Kuznetsov */ 14729eb41c52SAl Viro if (__put_user(0, (u32 __user *)addr)) 1473e83d5887SAndrey Smetanin return 1; 14741779a39fSVitaly Kuznetsov hv_vcpu->hv_vapic = data; 1475e83d5887SAndrey Smetanin kvm_vcpu_mark_page_dirty(vcpu, gfn); 1476e83d5887SAndrey Smetanin if (kvm_lapic_enable_pv_eoi(vcpu, 147772bbf935SLadi Prosek gfn_to_gpa(gfn) | KVM_MSR_ENABLED, 147872bbf935SLadi Prosek sizeof(struct hv_vp_assist_page))) 1479e83d5887SAndrey Smetanin return 1; 1480e83d5887SAndrey Smetanin break; 1481e83d5887SAndrey Smetanin } 1482e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 1483e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data); 1484e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 1485e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data); 1486e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 1487e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data); 14889eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 14899eec50b8SAndrey Smetanin if (!host) 14909eec50b8SAndrey Smetanin return 1; 14911779a39fSVitaly Kuznetsov hv_vcpu->runtime_offset = data - current_task_runtime_100ns(); 14929eec50b8SAndrey Smetanin break; 14935c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 14945c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 14955c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 14965c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 14975c919412SAndrey Smetanin case HV_X64_MSR_EOM: 14985c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 1499e0121fa2SVitaly Kuznetsov return synic_set_msr(to_hv_synic(vcpu), msr, data, host); 15001f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 15011f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 15021f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 15031f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 15041f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 15051f4b34f8SAndrey Smetanin 1506aafa97fdSVitaly Kuznetsov return stimer_set_config(to_hv_stimer(vcpu, timer_index), 15071f4b34f8SAndrey Smetanin data, host); 15081f4b34f8SAndrey Smetanin } 15091f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 15101f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 15111f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 15121f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 15131f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 15141f4b34f8SAndrey Smetanin 1515aafa97fdSVitaly Kuznetsov return stimer_set_count(to_hv_stimer(vcpu, timer_index), 15161f4b34f8SAndrey Smetanin data, host); 15171f4b34f8SAndrey Smetanin } 151844883f01SPaolo Bonzini case HV_X64_MSR_TSC_FREQUENCY: 151944883f01SPaolo Bonzini case HV_X64_MSR_APIC_FREQUENCY: 152044883f01SPaolo Bonzini /* read-only, but still ignore it if host-initiated */ 152144883f01SPaolo Bonzini if (!host) 152244883f01SPaolo Bonzini return 1; 152344883f01SPaolo Bonzini break; 1524e83d5887SAndrey Smetanin default: 15252f9f5cddSMiaohe Lin vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n", 1526e83d5887SAndrey Smetanin msr, data); 1527e83d5887SAndrey Smetanin return 1; 1528e83d5887SAndrey Smetanin } 1529e83d5887SAndrey Smetanin 1530e83d5887SAndrey Smetanin return 0; 1531e83d5887SAndrey Smetanin } 1532e83d5887SAndrey Smetanin 1533f97f5a56SJon Doron static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, 1534f97f5a56SJon Doron bool host) 1535e83d5887SAndrey Smetanin { 1536e83d5887SAndrey Smetanin u64 data = 0; 1537e83d5887SAndrey Smetanin struct kvm *kvm = vcpu->kvm; 153805f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 1539e83d5887SAndrey Smetanin 1540b4128000SVitaly Kuznetsov if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr))) 1541b4128000SVitaly Kuznetsov return 1; 1542b4128000SVitaly Kuznetsov 1543e83d5887SAndrey Smetanin switch (msr) { 1544e83d5887SAndrey Smetanin case HV_X64_MSR_GUEST_OS_ID: 1545e83d5887SAndrey Smetanin data = hv->hv_guest_os_id; 1546e83d5887SAndrey Smetanin break; 1547e83d5887SAndrey Smetanin case HV_X64_MSR_HYPERCALL: 1548e83d5887SAndrey Smetanin data = hv->hv_hypercall; 1549e83d5887SAndrey Smetanin break; 155093bf4172SAndrey Smetanin case HV_X64_MSR_TIME_REF_COUNT: 155193bf4172SAndrey Smetanin data = get_time_ref_counter(kvm); 1552e83d5887SAndrey Smetanin break; 1553e83d5887SAndrey Smetanin case HV_X64_MSR_REFERENCE_TSC: 1554e83d5887SAndrey Smetanin data = hv->hv_tsc_page; 1555e83d5887SAndrey Smetanin break; 1556e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: 155705f04ae4SVitaly Kuznetsov return kvm_hv_msr_get_crash_data(kvm, 1558e7d9513bSAndrey Smetanin msr - HV_X64_MSR_CRASH_P0, 1559e7d9513bSAndrey Smetanin pdata); 1560e7d9513bSAndrey Smetanin case HV_X64_MSR_CRASH_CTL: 156105f04ae4SVitaly Kuznetsov return kvm_hv_msr_get_crash_ctl(kvm, pdata); 1562e516cebbSAndrey Smetanin case HV_X64_MSR_RESET: 1563e516cebbSAndrey Smetanin data = 0; 1564e516cebbSAndrey Smetanin break; 1565a2e164e7SVitaly Kuznetsov case HV_X64_MSR_REENLIGHTENMENT_CONTROL: 1566a2e164e7SVitaly Kuznetsov data = hv->hv_reenlightenment_control; 1567a2e164e7SVitaly Kuznetsov break; 1568a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_CONTROL: 1569a2e164e7SVitaly Kuznetsov data = hv->hv_tsc_emulation_control; 1570a2e164e7SVitaly Kuznetsov break; 1571a2e164e7SVitaly Kuznetsov case HV_X64_MSR_TSC_EMULATION_STATUS: 1572a2e164e7SVitaly Kuznetsov data = hv->hv_tsc_emulation_status; 1573a2e164e7SVitaly Kuznetsov break; 1574f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_OPTIONS: 1575f97f5a56SJon Doron case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: 1576f97f5a56SJon Doron return syndbg_get_msr(vcpu, msr, pdata, host); 1577e83d5887SAndrey Smetanin default: 1578e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1579e83d5887SAndrey Smetanin return 1; 1580e83d5887SAndrey Smetanin } 1581e83d5887SAndrey Smetanin 1582e83d5887SAndrey Smetanin *pdata = data; 1583e83d5887SAndrey Smetanin return 0; 1584e83d5887SAndrey Smetanin } 1585e83d5887SAndrey Smetanin 158644883f01SPaolo Bonzini static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, 158744883f01SPaolo Bonzini bool host) 1588e83d5887SAndrey Smetanin { 1589e83d5887SAndrey Smetanin u64 data = 0; 15909ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1591e83d5887SAndrey Smetanin 1592b4128000SVitaly Kuznetsov if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr))) 1593b4128000SVitaly Kuznetsov return 1; 1594b4128000SVitaly Kuznetsov 1595e83d5887SAndrey Smetanin switch (msr) { 1596d3457c87SRoman Kagan case HV_X64_MSR_VP_INDEX: 15971779a39fSVitaly Kuznetsov data = hv_vcpu->vp_index; 1598e83d5887SAndrey Smetanin break; 1599e83d5887SAndrey Smetanin case HV_X64_MSR_EOI: 1600e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata); 1601e83d5887SAndrey Smetanin case HV_X64_MSR_ICR: 1602e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata); 1603e83d5887SAndrey Smetanin case HV_X64_MSR_TPR: 1604e83d5887SAndrey Smetanin return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata); 1605d4abc577SLadi Prosek case HV_X64_MSR_VP_ASSIST_PAGE: 16061779a39fSVitaly Kuznetsov data = hv_vcpu->hv_vapic; 1607e83d5887SAndrey Smetanin break; 16089eec50b8SAndrey Smetanin case HV_X64_MSR_VP_RUNTIME: 16091779a39fSVitaly Kuznetsov data = current_task_runtime_100ns() + hv_vcpu->runtime_offset; 16109eec50b8SAndrey Smetanin break; 16115c919412SAndrey Smetanin case HV_X64_MSR_SCONTROL: 16125c919412SAndrey Smetanin case HV_X64_MSR_SVERSION: 16135c919412SAndrey Smetanin case HV_X64_MSR_SIEFP: 16145c919412SAndrey Smetanin case HV_X64_MSR_SIMP: 16155c919412SAndrey Smetanin case HV_X64_MSR_EOM: 16165c919412SAndrey Smetanin case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15: 1617e0121fa2SVitaly Kuznetsov return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host); 16181f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_CONFIG: 16191f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_CONFIG: 16201f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_CONFIG: 16211f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_CONFIG: { 16221f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2; 16231f4b34f8SAndrey Smetanin 1624aafa97fdSVitaly Kuznetsov return stimer_get_config(to_hv_stimer(vcpu, timer_index), 16251f4b34f8SAndrey Smetanin pdata); 16261f4b34f8SAndrey Smetanin } 16271f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER0_COUNT: 16281f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER1_COUNT: 16291f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER2_COUNT: 16301f4b34f8SAndrey Smetanin case HV_X64_MSR_STIMER3_COUNT: { 16311f4b34f8SAndrey Smetanin int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2; 16321f4b34f8SAndrey Smetanin 1633aafa97fdSVitaly Kuznetsov return stimer_get_count(to_hv_stimer(vcpu, timer_index), 16341f4b34f8SAndrey Smetanin pdata); 16351f4b34f8SAndrey Smetanin } 163672c139baSLadi Prosek case HV_X64_MSR_TSC_FREQUENCY: 163772c139baSLadi Prosek data = (u64)vcpu->arch.virtual_tsc_khz * 1000; 163872c139baSLadi Prosek break; 163972c139baSLadi Prosek case HV_X64_MSR_APIC_FREQUENCY: 164072c139baSLadi Prosek data = APIC_BUS_FREQUENCY; 164172c139baSLadi Prosek break; 1642e83d5887SAndrey Smetanin default: 1643e83d5887SAndrey Smetanin vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr); 1644e83d5887SAndrey Smetanin return 1; 1645e83d5887SAndrey Smetanin } 1646e83d5887SAndrey Smetanin *pdata = data; 1647e83d5887SAndrey Smetanin return 0; 1648e83d5887SAndrey Smetanin } 1649e83d5887SAndrey Smetanin 1650e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) 1651e83d5887SAndrey Smetanin { 165205f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 165305f04ae4SVitaly Kuznetsov 16548f014550SVitaly Kuznetsov if (!host && !vcpu->arch.hyperv_enabled) 16558f014550SVitaly Kuznetsov return 1; 16568f014550SVitaly Kuznetsov 1657fc08b628SVitaly Kuznetsov if (!to_hv_vcpu(vcpu)) { 1658fc08b628SVitaly Kuznetsov if (kvm_hv_vcpu_init(vcpu)) 1659fc08b628SVitaly Kuznetsov return 1; 1660fc08b628SVitaly Kuznetsov } 1661fc08b628SVitaly Kuznetsov 1662e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 1663e83d5887SAndrey Smetanin int r; 1664e83d5887SAndrey Smetanin 166505f04ae4SVitaly Kuznetsov mutex_lock(&hv->hv_lock); 1666e7d9513bSAndrey Smetanin r = kvm_hv_set_msr_pw(vcpu, msr, data, host); 166705f04ae4SVitaly Kuznetsov mutex_unlock(&hv->hv_lock); 1668e83d5887SAndrey Smetanin return r; 1669e83d5887SAndrey Smetanin } else 16709eec50b8SAndrey Smetanin return kvm_hv_set_msr(vcpu, msr, data, host); 1671e83d5887SAndrey Smetanin } 1672e83d5887SAndrey Smetanin 167344883f01SPaolo Bonzini int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) 1674e83d5887SAndrey Smetanin { 167505f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 167605f04ae4SVitaly Kuznetsov 16778f014550SVitaly Kuznetsov if (!host && !vcpu->arch.hyperv_enabled) 16788f014550SVitaly Kuznetsov return 1; 16798f014550SVitaly Kuznetsov 1680fc08b628SVitaly Kuznetsov if (!to_hv_vcpu(vcpu)) { 1681fc08b628SVitaly Kuznetsov if (kvm_hv_vcpu_init(vcpu)) 1682fc08b628SVitaly Kuznetsov return 1; 1683fc08b628SVitaly Kuznetsov } 1684fc08b628SVitaly Kuznetsov 1685e83d5887SAndrey Smetanin if (kvm_hv_msr_partition_wide(msr)) { 1686e83d5887SAndrey Smetanin int r; 1687e83d5887SAndrey Smetanin 168805f04ae4SVitaly Kuznetsov mutex_lock(&hv->hv_lock); 1689f97f5a56SJon Doron r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host); 169005f04ae4SVitaly Kuznetsov mutex_unlock(&hv->hv_lock); 1691e83d5887SAndrey Smetanin return r; 1692e83d5887SAndrey Smetanin } else 169344883f01SPaolo Bonzini return kvm_hv_get_msr(vcpu, msr, pdata, host); 1694e83d5887SAndrey Smetanin } 1695e83d5887SAndrey Smetanin 1696f21dd494SVitaly Kuznetsov static __always_inline unsigned long *sparse_set_to_vcpu_mask( 1697f21dd494SVitaly Kuznetsov struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask, 1698f21dd494SVitaly Kuznetsov u64 *vp_bitmap, unsigned long *vcpu_bitmap) 1699c7012676SVitaly Kuznetsov { 170005f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 1701f21dd494SVitaly Kuznetsov struct kvm_vcpu *vcpu; 1702f21dd494SVitaly Kuznetsov int i, bank, sbank = 0; 1703c7012676SVitaly Kuznetsov 1704f21dd494SVitaly Kuznetsov memset(vp_bitmap, 0, 1705f21dd494SVitaly Kuznetsov KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap)); 1706f21dd494SVitaly Kuznetsov for_each_set_bit(bank, (unsigned long *)&valid_bank_mask, 1707f21dd494SVitaly Kuznetsov KVM_HV_MAX_SPARSE_VCPU_SET_BITS) 1708f21dd494SVitaly Kuznetsov vp_bitmap[bank] = sparse_banks[sbank++]; 1709c7012676SVitaly Kuznetsov 1710f21dd494SVitaly Kuznetsov if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) { 1711f21dd494SVitaly Kuznetsov /* for all vcpus vp_index == vcpu_idx */ 1712f21dd494SVitaly Kuznetsov return (unsigned long *)vp_bitmap; 1713f21dd494SVitaly Kuznetsov } 1714c7012676SVitaly Kuznetsov 1715f21dd494SVitaly Kuznetsov bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS); 1716f21dd494SVitaly Kuznetsov kvm_for_each_vcpu(i, vcpu, kvm) { 1717f2bc14b6SVitaly Kuznetsov if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap)) 1718f21dd494SVitaly Kuznetsov __set_bit(i, vcpu_bitmap); 1719f21dd494SVitaly Kuznetsov } 1720f21dd494SVitaly Kuznetsov return vcpu_bitmap; 1721c7012676SVitaly Kuznetsov } 1722c7012676SVitaly Kuznetsov 1723bd38b320SSiddharth Chandrasekaran struct kvm_hv_hcall { 1724bd38b320SSiddharth Chandrasekaran u64 param; 1725bd38b320SSiddharth Chandrasekaran u64 ingpa; 1726bd38b320SSiddharth Chandrasekaran u64 outgpa; 1727bd38b320SSiddharth Chandrasekaran u16 code; 1728bd38b320SSiddharth Chandrasekaran u16 rep_cnt; 1729bd38b320SSiddharth Chandrasekaran u16 rep_idx; 1730bd38b320SSiddharth Chandrasekaran bool fast; 1731bd38b320SSiddharth Chandrasekaran bool rep; 17325974565bSSiddharth Chandrasekaran sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS]; 1733bd38b320SSiddharth Chandrasekaran }; 1734bd38b320SSiddharth Chandrasekaran 1735bd38b320SSiddharth Chandrasekaran static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex) 1736e2f11f42SVitaly Kuznetsov { 17375974565bSSiddharth Chandrasekaran int i; 17385974565bSSiddharth Chandrasekaran gpa_t gpa; 173972167a9dSVitaly Kuznetsov struct kvm *kvm = vcpu->kvm; 17409ff5e030SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 1741c7012676SVitaly Kuznetsov struct hv_tlb_flush_ex flush_ex; 1742e2f11f42SVitaly Kuznetsov struct hv_tlb_flush flush; 1743f21dd494SVitaly Kuznetsov u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; 1744f21dd494SVitaly Kuznetsov DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); 1745f21dd494SVitaly Kuznetsov unsigned long *vcpu_mask; 17462cefc5feSVitaly Kuznetsov u64 valid_bank_mask; 1747c7012676SVitaly Kuznetsov u64 sparse_banks[64]; 1748f21dd494SVitaly Kuznetsov int sparse_banks_len; 1749c7012676SVitaly Kuznetsov bool all_cpus; 1750e2f11f42SVitaly Kuznetsov 1751c7012676SVitaly Kuznetsov if (!ex) { 17525974565bSSiddharth Chandrasekaran if (hc->fast) { 17535974565bSSiddharth Chandrasekaran flush.address_space = hc->ingpa; 17545974565bSSiddharth Chandrasekaran flush.flags = hc->outgpa; 17555974565bSSiddharth Chandrasekaran flush.processor_mask = sse128_lo(hc->xmm[0]); 17565974565bSSiddharth Chandrasekaran } else { 17575974565bSSiddharth Chandrasekaran if (unlikely(kvm_read_guest(kvm, hc->ingpa, 17585974565bSSiddharth Chandrasekaran &flush, sizeof(flush)))) 1759e2f11f42SVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 17605974565bSSiddharth Chandrasekaran } 1761e2f11f42SVitaly Kuznetsov 1762c7012676SVitaly Kuznetsov trace_kvm_hv_flush_tlb(flush.processor_mask, 1763c7012676SVitaly Kuznetsov flush.address_space, flush.flags); 1764c7012676SVitaly Kuznetsov 17652cefc5feSVitaly Kuznetsov valid_bank_mask = BIT_ULL(0); 1766c7012676SVitaly Kuznetsov sparse_banks[0] = flush.processor_mask; 1767da66761cSVitaly Kuznetsov 1768da66761cSVitaly Kuznetsov /* 1769da66761cSVitaly Kuznetsov * Work around possible WS2012 bug: it sends hypercalls 1770da66761cSVitaly Kuznetsov * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear, 1771da66761cSVitaly Kuznetsov * while also expecting us to flush something and crashing if 1772da66761cSVitaly Kuznetsov * we don't. Let's treat processor_mask == 0 same as 1773da66761cSVitaly Kuznetsov * HV_FLUSH_ALL_PROCESSORS. 1774da66761cSVitaly Kuznetsov */ 1775da66761cSVitaly Kuznetsov all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) || 1776da66761cSVitaly Kuznetsov flush.processor_mask == 0; 1777c7012676SVitaly Kuznetsov } else { 17785974565bSSiddharth Chandrasekaran if (hc->fast) { 17795974565bSSiddharth Chandrasekaran flush_ex.address_space = hc->ingpa; 17805974565bSSiddharth Chandrasekaran flush_ex.flags = hc->outgpa; 17815974565bSSiddharth Chandrasekaran memcpy(&flush_ex.hv_vp_set, 17825974565bSSiddharth Chandrasekaran &hc->xmm[0], sizeof(hc->xmm[0])); 17835974565bSSiddharth Chandrasekaran } else { 1784bd38b320SSiddharth Chandrasekaran if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex, 1785c7012676SVitaly Kuznetsov sizeof(flush_ex)))) 1786c7012676SVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 17875974565bSSiddharth Chandrasekaran } 1788c7012676SVitaly Kuznetsov 1789c7012676SVitaly Kuznetsov trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask, 1790c7012676SVitaly Kuznetsov flush_ex.hv_vp_set.format, 1791c7012676SVitaly Kuznetsov flush_ex.address_space, 1792c7012676SVitaly Kuznetsov flush_ex.flags); 1793c7012676SVitaly Kuznetsov 1794c7012676SVitaly Kuznetsov valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask; 1795c7012676SVitaly Kuznetsov all_cpus = flush_ex.hv_vp_set.format != 1796c7012676SVitaly Kuznetsov HV_GENERIC_SET_SPARSE_4K; 1797c7012676SVitaly Kuznetsov 17985974565bSSiddharth Chandrasekaran sparse_banks_len = bitmap_weight((unsigned long *)&valid_bank_mask, 64); 1799c7012676SVitaly Kuznetsov 1800c7012676SVitaly Kuznetsov if (!sparse_banks_len && !all_cpus) 1801c7012676SVitaly Kuznetsov goto ret_success; 1802c7012676SVitaly Kuznetsov 18035974565bSSiddharth Chandrasekaran if (!all_cpus) { 18045974565bSSiddharth Chandrasekaran if (hc->fast) { 18055974565bSSiddharth Chandrasekaran if (sparse_banks_len > HV_HYPERCALL_MAX_XMM_REGISTERS - 1) 1806c7012676SVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 18075974565bSSiddharth Chandrasekaran for (i = 0; i < sparse_banks_len; i += 2) { 18085974565bSSiddharth Chandrasekaran sparse_banks[i] = sse128_lo(hc->xmm[i / 2 + 1]); 18095974565bSSiddharth Chandrasekaran sparse_banks[i + 1] = sse128_hi(hc->xmm[i / 2 + 1]); 18105974565bSSiddharth Chandrasekaran } 18115974565bSSiddharth Chandrasekaran } else { 18125974565bSSiddharth Chandrasekaran gpa = hc->ingpa + offsetof(struct hv_tlb_flush_ex, 18135974565bSSiddharth Chandrasekaran hv_vp_set.bank_contents); 18145974565bSSiddharth Chandrasekaran if (unlikely(kvm_read_guest(kvm, gpa, sparse_banks, 18155974565bSSiddharth Chandrasekaran sparse_banks_len * 18165974565bSSiddharth Chandrasekaran sizeof(sparse_banks[0])))) 18175974565bSSiddharth Chandrasekaran return HV_STATUS_INVALID_HYPERCALL_INPUT; 18185974565bSSiddharth Chandrasekaran } 18195974565bSSiddharth Chandrasekaran } 1820c7012676SVitaly Kuznetsov } 1821e2f11f42SVitaly Kuznetsov 1822f21dd494SVitaly Kuznetsov cpumask_clear(&hv_vcpu->tlb_flush); 1823f21dd494SVitaly Kuznetsov 1824f21dd494SVitaly Kuznetsov vcpu_mask = all_cpus ? NULL : 1825f21dd494SVitaly Kuznetsov sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, 1826f21dd494SVitaly Kuznetsov vp_bitmap, vcpu_bitmap); 1827f21dd494SVitaly Kuznetsov 18282cefc5feSVitaly Kuznetsov /* 18292cefc5feSVitaly Kuznetsov * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't 18302cefc5feSVitaly Kuznetsov * analyze it here, flush TLB regardless of the specified address space. 18312cefc5feSVitaly Kuznetsov */ 18320baedd79SVitaly Kuznetsov kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, 183354163a34SSuravee Suthikulpanit NULL, vcpu_mask, &hv_vcpu->tlb_flush); 1834e2f11f42SVitaly Kuznetsov 1835c7012676SVitaly Kuznetsov ret_success: 1836bd38b320SSiddharth Chandrasekaran /* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */ 1837e2f11f42SVitaly Kuznetsov return (u64)HV_STATUS_SUCCESS | 1838bd38b320SSiddharth Chandrasekaran ((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET); 1839e2f11f42SVitaly Kuznetsov } 1840e2f11f42SVitaly Kuznetsov 1841f21dd494SVitaly Kuznetsov static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector, 1842f21dd494SVitaly Kuznetsov unsigned long *vcpu_bitmap) 1843f21dd494SVitaly Kuznetsov { 1844f21dd494SVitaly Kuznetsov struct kvm_lapic_irq irq = { 1845f21dd494SVitaly Kuznetsov .delivery_mode = APIC_DM_FIXED, 1846f21dd494SVitaly Kuznetsov .vector = vector 1847f21dd494SVitaly Kuznetsov }; 1848f21dd494SVitaly Kuznetsov struct kvm_vcpu *vcpu; 1849f21dd494SVitaly Kuznetsov int i; 1850f21dd494SVitaly Kuznetsov 1851f21dd494SVitaly Kuznetsov kvm_for_each_vcpu(i, vcpu, kvm) { 1852f21dd494SVitaly Kuznetsov if (vcpu_bitmap && !test_bit(i, vcpu_bitmap)) 1853f21dd494SVitaly Kuznetsov continue; 1854f21dd494SVitaly Kuznetsov 1855f21dd494SVitaly Kuznetsov /* We fail only when APIC is disabled */ 1856f21dd494SVitaly Kuznetsov kvm_apic_set_irq(vcpu, &irq, NULL); 1857f21dd494SVitaly Kuznetsov } 1858f21dd494SVitaly Kuznetsov } 1859f21dd494SVitaly Kuznetsov 1860bd38b320SSiddharth Chandrasekaran static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex) 1861214ff83dSVitaly Kuznetsov { 186272167a9dSVitaly Kuznetsov struct kvm *kvm = vcpu->kvm; 1863214ff83dSVitaly Kuznetsov struct hv_send_ipi_ex send_ipi_ex; 1864214ff83dSVitaly Kuznetsov struct hv_send_ipi send_ipi; 1865f21dd494SVitaly Kuznetsov u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS]; 1866f21dd494SVitaly Kuznetsov DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS); 1867f21dd494SVitaly Kuznetsov unsigned long *vcpu_mask; 1868214ff83dSVitaly Kuznetsov unsigned long valid_bank_mask; 1869214ff83dSVitaly Kuznetsov u64 sparse_banks[64]; 1870f21dd494SVitaly Kuznetsov int sparse_banks_len; 1871f21dd494SVitaly Kuznetsov u32 vector; 1872214ff83dSVitaly Kuznetsov bool all_cpus; 1873214ff83dSVitaly Kuznetsov 1874214ff83dSVitaly Kuznetsov if (!ex) { 1875bd38b320SSiddharth Chandrasekaran if (!hc->fast) { 1876bd38b320SSiddharth Chandrasekaran if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi, 1877214ff83dSVitaly Kuznetsov sizeof(send_ipi)))) 1878214ff83dSVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 1879214ff83dSVitaly Kuznetsov sparse_banks[0] = send_ipi.cpu_mask; 1880f21dd494SVitaly Kuznetsov vector = send_ipi.vector; 1881214ff83dSVitaly Kuznetsov } else { 1882214ff83dSVitaly Kuznetsov /* 'reserved' part of hv_send_ipi should be 0 */ 1883bd38b320SSiddharth Chandrasekaran if (unlikely(hc->ingpa >> 32 != 0)) 1884214ff83dSVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 1885bd38b320SSiddharth Chandrasekaran sparse_banks[0] = hc->outgpa; 1886bd38b320SSiddharth Chandrasekaran vector = (u32)hc->ingpa; 1887214ff83dSVitaly Kuznetsov } 1888214ff83dSVitaly Kuznetsov all_cpus = false; 1889214ff83dSVitaly Kuznetsov valid_bank_mask = BIT_ULL(0); 1890214ff83dSVitaly Kuznetsov 1891f21dd494SVitaly Kuznetsov trace_kvm_hv_send_ipi(vector, sparse_banks[0]); 1892214ff83dSVitaly Kuznetsov } else { 1893bd38b320SSiddharth Chandrasekaran if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex, 1894214ff83dSVitaly Kuznetsov sizeof(send_ipi_ex)))) 1895214ff83dSVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 1896214ff83dSVitaly Kuznetsov 1897214ff83dSVitaly Kuznetsov trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector, 1898214ff83dSVitaly Kuznetsov send_ipi_ex.vp_set.format, 1899214ff83dSVitaly Kuznetsov send_ipi_ex.vp_set.valid_bank_mask); 1900214ff83dSVitaly Kuznetsov 1901f21dd494SVitaly Kuznetsov vector = send_ipi_ex.vector; 1902214ff83dSVitaly Kuznetsov valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask; 1903214ff83dSVitaly Kuznetsov sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) * 1904214ff83dSVitaly Kuznetsov sizeof(sparse_banks[0]); 1905214ff83dSVitaly Kuznetsov 1906214ff83dSVitaly Kuznetsov all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL; 1907214ff83dSVitaly Kuznetsov 1908214ff83dSVitaly Kuznetsov if (!sparse_banks_len) 1909214ff83dSVitaly Kuznetsov goto ret_success; 1910214ff83dSVitaly Kuznetsov 1911214ff83dSVitaly Kuznetsov if (!all_cpus && 1912214ff83dSVitaly Kuznetsov kvm_read_guest(kvm, 1913bd38b320SSiddharth Chandrasekaran hc->ingpa + offsetof(struct hv_send_ipi_ex, 1914214ff83dSVitaly Kuznetsov vp_set.bank_contents), 1915214ff83dSVitaly Kuznetsov sparse_banks, 1916214ff83dSVitaly Kuznetsov sparse_banks_len)) 1917214ff83dSVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 1918214ff83dSVitaly Kuznetsov } 1919214ff83dSVitaly Kuznetsov 1920f21dd494SVitaly Kuznetsov if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR)) 1921214ff83dSVitaly Kuznetsov return HV_STATUS_INVALID_HYPERCALL_INPUT; 1922214ff83dSVitaly Kuznetsov 1923f21dd494SVitaly Kuznetsov vcpu_mask = all_cpus ? NULL : 1924f21dd494SVitaly Kuznetsov sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, 1925f21dd494SVitaly Kuznetsov vp_bitmap, vcpu_bitmap); 1926214ff83dSVitaly Kuznetsov 1927f21dd494SVitaly Kuznetsov kvm_send_ipi_to_many(kvm, vector, vcpu_mask); 1928214ff83dSVitaly Kuznetsov 1929214ff83dSVitaly Kuznetsov ret_success: 1930214ff83dSVitaly Kuznetsov return HV_STATUS_SUCCESS; 1931214ff83dSVitaly Kuznetsov } 1932214ff83dSVitaly Kuznetsov 19338f014550SVitaly Kuznetsov void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu) 1934e83d5887SAndrey Smetanin { 19358f014550SVitaly Kuznetsov struct kvm_cpuid_entry2 *entry; 193610d7bf1eSVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu); 19378f014550SVitaly Kuznetsov 19388f014550SVitaly Kuznetsov entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE, 0); 193910d7bf1eSVitaly Kuznetsov if (entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX) { 19408f014550SVitaly Kuznetsov vcpu->arch.hyperv_enabled = true; 194110d7bf1eSVitaly Kuznetsov } else { 19428f014550SVitaly Kuznetsov vcpu->arch.hyperv_enabled = false; 194310d7bf1eSVitaly Kuznetsov return; 194410d7bf1eSVitaly Kuznetsov } 194510d7bf1eSVitaly Kuznetsov 194610d7bf1eSVitaly Kuznetsov if (!to_hv_vcpu(vcpu) && kvm_hv_vcpu_init(vcpu)) 194710d7bf1eSVitaly Kuznetsov return; 194810d7bf1eSVitaly Kuznetsov 194910d7bf1eSVitaly Kuznetsov hv_vcpu = to_hv_vcpu(vcpu); 195010d7bf1eSVitaly Kuznetsov 195110d7bf1eSVitaly Kuznetsov entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES, 0); 195210d7bf1eSVitaly Kuznetsov if (entry) { 195310d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_eax = entry->eax; 195410d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_ebx = entry->ebx; 195510d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_edx = entry->edx; 195610d7bf1eSVitaly Kuznetsov } else { 195710d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_eax = 0; 195810d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_ebx = 0; 195910d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.features_edx = 0; 196010d7bf1eSVitaly Kuznetsov } 196110d7bf1eSVitaly Kuznetsov 196210d7bf1eSVitaly Kuznetsov entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO, 0); 196310d7bf1eSVitaly Kuznetsov if (entry) { 196410d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax; 196510d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx; 196610d7bf1eSVitaly Kuznetsov } else { 196710d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.enlightenments_eax = 0; 196810d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.enlightenments_ebx = 0; 196910d7bf1eSVitaly Kuznetsov } 197010d7bf1eSVitaly Kuznetsov 197110d7bf1eSVitaly Kuznetsov entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 0); 197210d7bf1eSVitaly Kuznetsov if (entry) 197310d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax; 197410d7bf1eSVitaly Kuznetsov else 197510d7bf1eSVitaly Kuznetsov hv_vcpu->cpuid_cache.syndbg_cap_eax = 0; 19768f014550SVitaly Kuznetsov } 19778f014550SVitaly Kuznetsov 1978644f7067SVitaly Kuznetsov int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce) 1979644f7067SVitaly Kuznetsov { 1980644f7067SVitaly Kuznetsov struct kvm_vcpu_hv *hv_vcpu; 1981644f7067SVitaly Kuznetsov int ret = 0; 1982644f7067SVitaly Kuznetsov 1983644f7067SVitaly Kuznetsov if (!to_hv_vcpu(vcpu)) { 1984644f7067SVitaly Kuznetsov if (enforce) { 1985644f7067SVitaly Kuznetsov ret = kvm_hv_vcpu_init(vcpu); 1986644f7067SVitaly Kuznetsov if (ret) 1987644f7067SVitaly Kuznetsov return ret; 1988644f7067SVitaly Kuznetsov } else { 1989644f7067SVitaly Kuznetsov return 0; 1990644f7067SVitaly Kuznetsov } 1991644f7067SVitaly Kuznetsov } 1992644f7067SVitaly Kuznetsov 1993644f7067SVitaly Kuznetsov hv_vcpu = to_hv_vcpu(vcpu); 1994644f7067SVitaly Kuznetsov hv_vcpu->enforce_cpuid = enforce; 1995644f7067SVitaly Kuznetsov 1996644f7067SVitaly Kuznetsov return ret; 1997644f7067SVitaly Kuznetsov } 1998644f7067SVitaly Kuznetsov 19998f014550SVitaly Kuznetsov bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu) 20008f014550SVitaly Kuznetsov { 20018f014550SVitaly Kuznetsov return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id; 2002e83d5887SAndrey Smetanin } 2003e83d5887SAndrey Smetanin 200483326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result) 200583326e43SAndrey Smetanin { 200683326e43SAndrey Smetanin bool longmode; 200783326e43SAndrey Smetanin 200883326e43SAndrey Smetanin longmode = is_64_bit_mode(vcpu); 200983326e43SAndrey Smetanin if (longmode) 2010de3cd117SSean Christopherson kvm_rax_write(vcpu, result); 201183326e43SAndrey Smetanin else { 2012de3cd117SSean Christopherson kvm_rdx_write(vcpu, result >> 32); 2013de3cd117SSean Christopherson kvm_rax_write(vcpu, result & 0xffffffff); 201483326e43SAndrey Smetanin } 201583326e43SAndrey Smetanin } 201683326e43SAndrey Smetanin 2017696ca779SRadim Krčmář static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result) 2018696ca779SRadim Krčmář { 2019696ca779SRadim Krčmář kvm_hv_hypercall_set_result(vcpu, result); 2020696ca779SRadim Krčmář ++vcpu->stat.hypercalls; 2021696ca779SRadim Krčmář return kvm_skip_emulated_instruction(vcpu); 2022696ca779SRadim Krčmář } 2023696ca779SRadim Krčmář 202483326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) 202583326e43SAndrey Smetanin { 2026696ca779SRadim Krčmář return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result); 202783326e43SAndrey Smetanin } 202883326e43SAndrey Smetanin 2029bd38b320SSiddharth Chandrasekaran static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) 2030faeb7833SRoman Kagan { 203105f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(vcpu->kvm); 2032faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 2033faeb7833SRoman Kagan 2034bd38b320SSiddharth Chandrasekaran if (unlikely(!hc->fast)) { 2035faeb7833SRoman Kagan int ret; 2036bd38b320SSiddharth Chandrasekaran gpa_t gpa = hc->ingpa; 2037faeb7833SRoman Kagan 2038bd38b320SSiddharth Chandrasekaran if ((gpa & (__alignof__(hc->ingpa) - 1)) || 2039bd38b320SSiddharth Chandrasekaran offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE) 2040faeb7833SRoman Kagan return HV_STATUS_INVALID_ALIGNMENT; 2041faeb7833SRoman Kagan 2042bd38b320SSiddharth Chandrasekaran ret = kvm_vcpu_read_guest(vcpu, gpa, 2043bd38b320SSiddharth Chandrasekaran &hc->ingpa, sizeof(hc->ingpa)); 2044faeb7833SRoman Kagan if (ret < 0) 2045faeb7833SRoman Kagan return HV_STATUS_INVALID_ALIGNMENT; 2046faeb7833SRoman Kagan } 2047faeb7833SRoman Kagan 2048faeb7833SRoman Kagan /* 2049faeb7833SRoman Kagan * Per spec, bits 32-47 contain the extra "flag number". However, we 2050faeb7833SRoman Kagan * have no use for it, and in all known usecases it is zero, so just 2051faeb7833SRoman Kagan * report lookup failure if it isn't. 2052faeb7833SRoman Kagan */ 2053bd38b320SSiddharth Chandrasekaran if (hc->ingpa & 0xffff00000000ULL) 2054faeb7833SRoman Kagan return HV_STATUS_INVALID_PORT_ID; 2055faeb7833SRoman Kagan /* remaining bits are reserved-zero */ 2056bd38b320SSiddharth Chandrasekaran if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK) 2057faeb7833SRoman Kagan return HV_STATUS_INVALID_HYPERCALL_INPUT; 2058faeb7833SRoman Kagan 2059452a68d0SPaolo Bonzini /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */ 2060452a68d0SPaolo Bonzini rcu_read_lock(); 2061bd38b320SSiddharth Chandrasekaran eventfd = idr_find(&hv->conn_to_evt, hc->ingpa); 2062452a68d0SPaolo Bonzini rcu_read_unlock(); 2063faeb7833SRoman Kagan if (!eventfd) 2064faeb7833SRoman Kagan return HV_STATUS_INVALID_PORT_ID; 2065faeb7833SRoman Kagan 2066faeb7833SRoman Kagan eventfd_signal(eventfd, 1); 2067faeb7833SRoman Kagan return HV_STATUS_SUCCESS; 2068faeb7833SRoman Kagan } 2069faeb7833SRoman Kagan 20705974565bSSiddharth Chandrasekaran static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc) 20715974565bSSiddharth Chandrasekaran { 20725974565bSSiddharth Chandrasekaran switch (hc->code) { 20735974565bSSiddharth Chandrasekaran case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: 20745974565bSSiddharth Chandrasekaran case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: 20755974565bSSiddharth Chandrasekaran case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: 20765974565bSSiddharth Chandrasekaran case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: 20775974565bSSiddharth Chandrasekaran return true; 20785974565bSSiddharth Chandrasekaran } 20795974565bSSiddharth Chandrasekaran 20805974565bSSiddharth Chandrasekaran return false; 20815974565bSSiddharth Chandrasekaran } 20825974565bSSiddharth Chandrasekaran 20835974565bSSiddharth Chandrasekaran static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc) 20845974565bSSiddharth Chandrasekaran { 20855974565bSSiddharth Chandrasekaran int reg; 20865974565bSSiddharth Chandrasekaran 20875974565bSSiddharth Chandrasekaran kvm_fpu_get(); 20885974565bSSiddharth Chandrasekaran for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++) 20895974565bSSiddharth Chandrasekaran _kvm_read_sse_reg(reg, &hc->xmm[reg]); 20905974565bSSiddharth Chandrasekaran kvm_fpu_put(); 20915974565bSSiddharth Chandrasekaran } 20925974565bSSiddharth Chandrasekaran 20934ad81a91SVitaly Kuznetsov static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code) 20944ad81a91SVitaly Kuznetsov { 209534ef7d7bSVitaly Kuznetsov if (!hv_vcpu->enforce_cpuid) 209634ef7d7bSVitaly Kuznetsov return true; 209734ef7d7bSVitaly Kuznetsov 209834ef7d7bSVitaly Kuznetsov switch (code) { 209934ef7d7bSVitaly Kuznetsov case HVCALL_NOTIFY_LONG_SPIN_WAIT: 210034ef7d7bSVitaly Kuznetsov return hv_vcpu->cpuid_cache.enlightenments_ebx && 210134ef7d7bSVitaly Kuznetsov hv_vcpu->cpuid_cache.enlightenments_ebx != U32_MAX; 2102*4f532b7fSVitaly Kuznetsov case HVCALL_POST_MESSAGE: 2103*4f532b7fSVitaly Kuznetsov return hv_vcpu->cpuid_cache.features_ebx & HV_POST_MESSAGES; 210434ef7d7bSVitaly Kuznetsov default: 210534ef7d7bSVitaly Kuznetsov break; 210634ef7d7bSVitaly Kuznetsov } 210734ef7d7bSVitaly Kuznetsov 21084ad81a91SVitaly Kuznetsov return true; 21094ad81a91SVitaly Kuznetsov } 21104ad81a91SVitaly Kuznetsov 2111e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu) 2112e83d5887SAndrey Smetanin { 2113bd38b320SSiddharth Chandrasekaran struct kvm_hv_hcall hc; 2114bd38b320SSiddharth Chandrasekaran u64 ret = HV_STATUS_SUCCESS; 2115e83d5887SAndrey Smetanin 2116e83d5887SAndrey Smetanin /* 2117e83d5887SAndrey Smetanin * hypercall generates UD from non zero cpl and real mode 2118e83d5887SAndrey Smetanin * per HYPER-V spec 2119e83d5887SAndrey Smetanin */ 2120b3646477SJason Baron if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) { 2121e83d5887SAndrey Smetanin kvm_queue_exception(vcpu, UD_VECTOR); 21220d9c055eSAndrey Smetanin return 1; 2123e83d5887SAndrey Smetanin } 2124e83d5887SAndrey Smetanin 2125f4e4805eSArnd Bergmann #ifdef CONFIG_X86_64 2126f4e4805eSArnd Bergmann if (is_64_bit_mode(vcpu)) { 2127bd38b320SSiddharth Chandrasekaran hc.param = kvm_rcx_read(vcpu); 2128bd38b320SSiddharth Chandrasekaran hc.ingpa = kvm_rdx_read(vcpu); 2129bd38b320SSiddharth Chandrasekaran hc.outgpa = kvm_r8_read(vcpu); 2130f4e4805eSArnd Bergmann } else 2131f4e4805eSArnd Bergmann #endif 2132f4e4805eSArnd Bergmann { 2133bd38b320SSiddharth Chandrasekaran hc.param = ((u64)kvm_rdx_read(vcpu) << 32) | 2134de3cd117SSean Christopherson (kvm_rax_read(vcpu) & 0xffffffff); 2135bd38b320SSiddharth Chandrasekaran hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) | 2136de3cd117SSean Christopherson (kvm_rcx_read(vcpu) & 0xffffffff); 2137bd38b320SSiddharth Chandrasekaran hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) | 2138de3cd117SSean Christopherson (kvm_rsi_read(vcpu) & 0xffffffff); 2139e83d5887SAndrey Smetanin } 2140e83d5887SAndrey Smetanin 2141bd38b320SSiddharth Chandrasekaran hc.code = hc.param & 0xffff; 2142bd38b320SSiddharth Chandrasekaran hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT); 2143bd38b320SSiddharth Chandrasekaran hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff; 2144bd38b320SSiddharth Chandrasekaran hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff; 2145bd38b320SSiddharth Chandrasekaran hc.rep = !!(hc.rep_cnt || hc.rep_idx); 2146e83d5887SAndrey Smetanin 21475974565bSSiddharth Chandrasekaran if (hc.fast && is_xmm_fast_hypercall(&hc)) 21485974565bSSiddharth Chandrasekaran kvm_hv_hypercall_read_xmm(&hc); 21495974565bSSiddharth Chandrasekaran 2150bd38b320SSiddharth Chandrasekaran trace_kvm_hv_hypercall(hc.code, hc.fast, hc.rep_cnt, hc.rep_idx, 2151bd38b320SSiddharth Chandrasekaran hc.ingpa, hc.outgpa); 2152e83d5887SAndrey Smetanin 21534ad81a91SVitaly Kuznetsov if (unlikely(!hv_check_hypercall_access(to_hv_vcpu(vcpu), hc.code))) { 21544ad81a91SVitaly Kuznetsov ret = HV_STATUS_ACCESS_DENIED; 21554ad81a91SVitaly Kuznetsov goto hypercall_complete; 21564ad81a91SVitaly Kuznetsov } 21574ad81a91SVitaly Kuznetsov 2158bd38b320SSiddharth Chandrasekaran switch (hc.code) { 21598ed6d767SAndrey Smetanin case HVCALL_NOTIFY_LONG_SPIN_WAIT: 2160bd38b320SSiddharth Chandrasekaran if (unlikely(hc.rep)) { 216156b9ae78SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 216256b9ae78SVitaly Kuznetsov break; 216356b9ae78SVitaly Kuznetsov } 2164de63ad4cSLongpeng(Mike) kvm_vcpu_on_spin(vcpu, true); 2165e83d5887SAndrey Smetanin break; 216683326e43SAndrey Smetanin case HVCALL_SIGNAL_EVENT: 2167bd38b320SSiddharth Chandrasekaran if (unlikely(hc.rep)) { 216856b9ae78SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 216956b9ae78SVitaly Kuznetsov break; 217056b9ae78SVitaly Kuznetsov } 2171bd38b320SSiddharth Chandrasekaran ret = kvm_hvcall_signal_event(vcpu, &hc); 2172d32ef547SDan Carpenter if (ret != HV_STATUS_INVALID_PORT_ID) 2173faeb7833SRoman Kagan break; 2174df561f66SGustavo A. R. Silva fallthrough; /* maybe userspace knows this conn_id */ 2175faeb7833SRoman Kagan case HVCALL_POST_MESSAGE: 2176a2b5c3c0SPaolo Bonzini /* don't bother userspace if it has no way to handle it */ 2177bd38b320SSiddharth Chandrasekaran if (unlikely(hc.rep || !to_hv_synic(vcpu)->active)) { 217856b9ae78SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2179a2b5c3c0SPaolo Bonzini break; 2180a2b5c3c0SPaolo Bonzini } 218183326e43SAndrey Smetanin vcpu->run->exit_reason = KVM_EXIT_HYPERV; 218283326e43SAndrey Smetanin vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; 2183bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.input = hc.param; 2184bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; 2185bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; 218683326e43SAndrey Smetanin vcpu->arch.complete_userspace_io = 218783326e43SAndrey Smetanin kvm_hv_hypercall_complete_userspace; 218883326e43SAndrey Smetanin return 0; 2189e2f11f42SVitaly Kuznetsov case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: 21905974565bSSiddharth Chandrasekaran if (unlikely(!hc.rep_cnt || hc.rep_idx)) { 2191e2f11f42SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2192e2f11f42SVitaly Kuznetsov break; 2193e2f11f42SVitaly Kuznetsov } 2194bd38b320SSiddharth Chandrasekaran ret = kvm_hv_flush_tlb(vcpu, &hc, false); 2195e2f11f42SVitaly Kuznetsov break; 2196e2f11f42SVitaly Kuznetsov case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE: 21975974565bSSiddharth Chandrasekaran if (unlikely(hc.rep)) { 2198e2f11f42SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2199e2f11f42SVitaly Kuznetsov break; 2200e2f11f42SVitaly Kuznetsov } 2201bd38b320SSiddharth Chandrasekaran ret = kvm_hv_flush_tlb(vcpu, &hc, false); 2202c7012676SVitaly Kuznetsov break; 2203c7012676SVitaly Kuznetsov case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX: 22045974565bSSiddharth Chandrasekaran if (unlikely(!hc.rep_cnt || hc.rep_idx)) { 2205c7012676SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2206c7012676SVitaly Kuznetsov break; 2207c7012676SVitaly Kuznetsov } 2208bd38b320SSiddharth Chandrasekaran ret = kvm_hv_flush_tlb(vcpu, &hc, true); 2209c7012676SVitaly Kuznetsov break; 2210c7012676SVitaly Kuznetsov case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX: 22115974565bSSiddharth Chandrasekaran if (unlikely(hc.rep)) { 2212c7012676SVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2213c7012676SVitaly Kuznetsov break; 2214c7012676SVitaly Kuznetsov } 2215bd38b320SSiddharth Chandrasekaran ret = kvm_hv_flush_tlb(vcpu, &hc, true); 2216e2f11f42SVitaly Kuznetsov break; 2217214ff83dSVitaly Kuznetsov case HVCALL_SEND_IPI: 2218bd38b320SSiddharth Chandrasekaran if (unlikely(hc.rep)) { 2219214ff83dSVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2220214ff83dSVitaly Kuznetsov break; 2221214ff83dSVitaly Kuznetsov } 2222bd38b320SSiddharth Chandrasekaran ret = kvm_hv_send_ipi(vcpu, &hc, false); 2223214ff83dSVitaly Kuznetsov break; 2224214ff83dSVitaly Kuznetsov case HVCALL_SEND_IPI_EX: 2225bd38b320SSiddharth Chandrasekaran if (unlikely(hc.fast || hc.rep)) { 2226214ff83dSVitaly Kuznetsov ret = HV_STATUS_INVALID_HYPERCALL_INPUT; 2227214ff83dSVitaly Kuznetsov break; 2228214ff83dSVitaly Kuznetsov } 2229bd38b320SSiddharth Chandrasekaran ret = kvm_hv_send_ipi(vcpu, &hc, true); 2230214ff83dSVitaly Kuznetsov break; 2231b187038bSJon Doron case HVCALL_POST_DEBUG_DATA: 2232b187038bSJon Doron case HVCALL_RETRIEVE_DEBUG_DATA: 2233bd38b320SSiddharth Chandrasekaran if (unlikely(hc.fast)) { 2234b187038bSJon Doron ret = HV_STATUS_INVALID_PARAMETER; 2235b187038bSJon Doron break; 2236b187038bSJon Doron } 2237b187038bSJon Doron fallthrough; 2238b187038bSJon Doron case HVCALL_RESET_DEBUG_SESSION: { 2239f69b55efSVitaly Kuznetsov struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu); 2240b187038bSJon Doron 2241b187038bSJon Doron if (!kvm_hv_is_syndbg_enabled(vcpu)) { 2242b187038bSJon Doron ret = HV_STATUS_INVALID_HYPERCALL_CODE; 2243b187038bSJon Doron break; 2244b187038bSJon Doron } 2245b187038bSJon Doron 2246b187038bSJon Doron if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) { 2247b187038bSJon Doron ret = HV_STATUS_OPERATION_DENIED; 2248b187038bSJon Doron break; 2249b187038bSJon Doron } 2250b187038bSJon Doron vcpu->run->exit_reason = KVM_EXIT_HYPERV; 2251b187038bSJon Doron vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; 2252bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.input = hc.param; 2253bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; 2254bd38b320SSiddharth Chandrasekaran vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; 2255b187038bSJon Doron vcpu->arch.complete_userspace_io = 2256b187038bSJon Doron kvm_hv_hypercall_complete_userspace; 2257b187038bSJon Doron return 0; 2258b187038bSJon Doron } 2259e83d5887SAndrey Smetanin default: 2260d32ef547SDan Carpenter ret = HV_STATUS_INVALID_HYPERCALL_CODE; 2261e83d5887SAndrey Smetanin break; 2262e83d5887SAndrey Smetanin } 2263e83d5887SAndrey Smetanin 22644ad81a91SVitaly Kuznetsov hypercall_complete: 2265696ca779SRadim Krčmář return kvm_hv_hypercall_complete(vcpu, ret); 2266e83d5887SAndrey Smetanin } 2267cbc0236aSRoman Kagan 2268cbc0236aSRoman Kagan void kvm_hv_init_vm(struct kvm *kvm) 2269cbc0236aSRoman Kagan { 227005f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 227105f04ae4SVitaly Kuznetsov 227205f04ae4SVitaly Kuznetsov mutex_init(&hv->hv_lock); 227305f04ae4SVitaly Kuznetsov idr_init(&hv->conn_to_evt); 2274cbc0236aSRoman Kagan } 2275cbc0236aSRoman Kagan 2276cbc0236aSRoman Kagan void kvm_hv_destroy_vm(struct kvm *kvm) 2277cbc0236aSRoman Kagan { 227805f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 2279faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 2280faeb7833SRoman Kagan int i; 2281faeb7833SRoman Kagan 228205f04ae4SVitaly Kuznetsov idr_for_each_entry(&hv->conn_to_evt, eventfd, i) 2283faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 228405f04ae4SVitaly Kuznetsov idr_destroy(&hv->conn_to_evt); 2285faeb7833SRoman Kagan } 2286faeb7833SRoman Kagan 2287faeb7833SRoman Kagan static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd) 2288faeb7833SRoman Kagan { 228905f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 2290faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 2291faeb7833SRoman Kagan int ret; 2292faeb7833SRoman Kagan 2293faeb7833SRoman Kagan eventfd = eventfd_ctx_fdget(fd); 2294faeb7833SRoman Kagan if (IS_ERR(eventfd)) 2295faeb7833SRoman Kagan return PTR_ERR(eventfd); 2296faeb7833SRoman Kagan 2297faeb7833SRoman Kagan mutex_lock(&hv->hv_lock); 2298faeb7833SRoman Kagan ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1, 2299254272ceSBen Gardon GFP_KERNEL_ACCOUNT); 2300faeb7833SRoman Kagan mutex_unlock(&hv->hv_lock); 2301faeb7833SRoman Kagan 2302faeb7833SRoman Kagan if (ret >= 0) 2303faeb7833SRoman Kagan return 0; 2304faeb7833SRoman Kagan 2305faeb7833SRoman Kagan if (ret == -ENOSPC) 2306faeb7833SRoman Kagan ret = -EEXIST; 2307faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 2308faeb7833SRoman Kagan return ret; 2309faeb7833SRoman Kagan } 2310faeb7833SRoman Kagan 2311faeb7833SRoman Kagan static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id) 2312faeb7833SRoman Kagan { 231305f04ae4SVitaly Kuznetsov struct kvm_hv *hv = to_kvm_hv(kvm); 2314faeb7833SRoman Kagan struct eventfd_ctx *eventfd; 2315faeb7833SRoman Kagan 2316faeb7833SRoman Kagan mutex_lock(&hv->hv_lock); 2317faeb7833SRoman Kagan eventfd = idr_remove(&hv->conn_to_evt, conn_id); 2318faeb7833SRoman Kagan mutex_unlock(&hv->hv_lock); 2319faeb7833SRoman Kagan 2320faeb7833SRoman Kagan if (!eventfd) 2321faeb7833SRoman Kagan return -ENOENT; 2322faeb7833SRoman Kagan 2323faeb7833SRoman Kagan synchronize_srcu(&kvm->srcu); 2324faeb7833SRoman Kagan eventfd_ctx_put(eventfd); 2325faeb7833SRoman Kagan return 0; 2326faeb7833SRoman Kagan } 2327faeb7833SRoman Kagan 2328faeb7833SRoman Kagan int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args) 2329faeb7833SRoman Kagan { 2330faeb7833SRoman Kagan if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) || 2331faeb7833SRoman Kagan (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK)) 2332faeb7833SRoman Kagan return -EINVAL; 2333faeb7833SRoman Kagan 2334faeb7833SRoman Kagan if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN) 2335faeb7833SRoman Kagan return kvm_hv_eventfd_deassign(kvm, args->conn_id); 2336faeb7833SRoman Kagan return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd); 2337cbc0236aSRoman Kagan } 23382bc39970SVitaly Kuznetsov 2339c21d54f0SVitaly Kuznetsov int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, 23402bc39970SVitaly Kuznetsov struct kvm_cpuid_entry2 __user *entries) 23412bc39970SVitaly Kuznetsov { 2342ea152987SVitaly Kuznetsov uint16_t evmcs_ver = 0; 23432bc39970SVitaly Kuznetsov struct kvm_cpuid_entry2 cpuid_entries[] = { 23442bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS }, 23452bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_INTERFACE }, 23462bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_VERSION }, 23472bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_FEATURES }, 23482bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_ENLIGHTMENT_INFO }, 23492bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_IMPLEMENT_LIMITS }, 2350f97f5a56SJon Doron { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS }, 2351f97f5a56SJon Doron { .function = HYPERV_CPUID_SYNDBG_INTERFACE }, 2352f97f5a56SJon Doron { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES }, 23532bc39970SVitaly Kuznetsov { .function = HYPERV_CPUID_NESTED_FEATURES }, 23542bc39970SVitaly Kuznetsov }; 23552bc39970SVitaly Kuznetsov int i, nent = ARRAY_SIZE(cpuid_entries); 23562bc39970SVitaly Kuznetsov 235733b22172SPaolo Bonzini if (kvm_x86_ops.nested_ops->get_evmcs_version) 235833b22172SPaolo Bonzini evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu); 2359ea152987SVitaly Kuznetsov 23602bc39970SVitaly Kuznetsov /* Skip NESTED_FEATURES if eVMCS is not supported */ 23612bc39970SVitaly Kuznetsov if (!evmcs_ver) 23622bc39970SVitaly Kuznetsov --nent; 23632bc39970SVitaly Kuznetsov 23642bc39970SVitaly Kuznetsov if (cpuid->nent < nent) 23652bc39970SVitaly Kuznetsov return -E2BIG; 23662bc39970SVitaly Kuznetsov 23672bc39970SVitaly Kuznetsov if (cpuid->nent > nent) 23682bc39970SVitaly Kuznetsov cpuid->nent = nent; 23692bc39970SVitaly Kuznetsov 23702bc39970SVitaly Kuznetsov for (i = 0; i < nent; i++) { 23712bc39970SVitaly Kuznetsov struct kvm_cpuid_entry2 *ent = &cpuid_entries[i]; 23722bc39970SVitaly Kuznetsov u32 signature[3]; 23732bc39970SVitaly Kuznetsov 23742bc39970SVitaly Kuznetsov switch (ent->function) { 23752bc39970SVitaly Kuznetsov case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS: 23762bc39970SVitaly Kuznetsov memcpy(signature, "Linux KVM Hv", 12); 23772bc39970SVitaly Kuznetsov 2378f97f5a56SJon Doron ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES; 23792bc39970SVitaly Kuznetsov ent->ebx = signature[0]; 23802bc39970SVitaly Kuznetsov ent->ecx = signature[1]; 23812bc39970SVitaly Kuznetsov ent->edx = signature[2]; 23822bc39970SVitaly Kuznetsov break; 23832bc39970SVitaly Kuznetsov 23842bc39970SVitaly Kuznetsov case HYPERV_CPUID_INTERFACE: 23858f014550SVitaly Kuznetsov ent->eax = HYPERV_CPUID_SIGNATURE_EAX; 23862bc39970SVitaly Kuznetsov break; 23872bc39970SVitaly Kuznetsov 23882bc39970SVitaly Kuznetsov case HYPERV_CPUID_VERSION: 23892bc39970SVitaly Kuznetsov /* 23902bc39970SVitaly Kuznetsov * We implement some Hyper-V 2016 functions so let's use 23912bc39970SVitaly Kuznetsov * this version. 23922bc39970SVitaly Kuznetsov */ 23932bc39970SVitaly Kuznetsov ent->eax = 0x00003839; 23942bc39970SVitaly Kuznetsov ent->ebx = 0x000A0000; 23952bc39970SVitaly Kuznetsov break; 23962bc39970SVitaly Kuznetsov 23972bc39970SVitaly Kuznetsov case HYPERV_CPUID_FEATURES: 2398dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE; 23992bc39970SVitaly Kuznetsov ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE; 2400dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_SYNIC_AVAILABLE; 24012bc39970SVitaly Kuznetsov ent->eax |= HV_MSR_SYNTIMER_AVAILABLE; 2402dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE; 2403dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_HYPERCALL_AVAILABLE; 2404dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_VP_INDEX_AVAILABLE; 2405dfc53baaSJoseph Salisbury ent->eax |= HV_MSR_RESET_AVAILABLE; 24062bc39970SVitaly Kuznetsov ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE; 2407dfc53baaSJoseph Salisbury ent->eax |= HV_ACCESS_FREQUENCY_MSRS; 2408dfc53baaSJoseph Salisbury ent->eax |= HV_ACCESS_REENLIGHTENMENT; 24092bc39970SVitaly Kuznetsov 2410dfc53baaSJoseph Salisbury ent->ebx |= HV_POST_MESSAGES; 2411dfc53baaSJoseph Salisbury ent->ebx |= HV_SIGNAL_EVENTS; 24122bc39970SVitaly Kuznetsov 2413d8f5537aSSiddharth Chandrasekaran ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; 24142bc39970SVitaly Kuznetsov ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; 24152bc39970SVitaly Kuznetsov ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; 2416a073d7e3SWanpeng Li 2417039aeb9dSLinus Torvalds ent->ebx |= HV_DEBUGGING; 2418f97f5a56SJon Doron ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE; 2419f97f5a56SJon Doron ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE; 2420f97f5a56SJon Doron 2421a073d7e3SWanpeng Li /* 2422a073d7e3SWanpeng Li * Direct Synthetic timers only make sense with in-kernel 2423a073d7e3SWanpeng Li * LAPIC 2424a073d7e3SWanpeng Li */ 2425c21d54f0SVitaly Kuznetsov if (!vcpu || lapic_in_kernel(vcpu)) 24262bc39970SVitaly Kuznetsov ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; 24272bc39970SVitaly Kuznetsov 24282bc39970SVitaly Kuznetsov break; 24292bc39970SVitaly Kuznetsov 24302bc39970SVitaly Kuznetsov case HYPERV_CPUID_ENLIGHTMENT_INFO: 24312bc39970SVitaly Kuznetsov ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; 24322bc39970SVitaly Kuznetsov ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; 24332bc39970SVitaly Kuznetsov ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; 24342bc39970SVitaly Kuznetsov ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; 24352bc39970SVitaly Kuznetsov ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; 2436f1adceafSVitaly Kuznetsov if (evmcs_ver) 24372bc39970SVitaly Kuznetsov ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 2438b2d8b167SVitaly Kuznetsov if (!cpu_smt_possible()) 2439b2d8b167SVitaly Kuznetsov ent->eax |= HV_X64_NO_NONARCH_CORESHARING; 24402bc39970SVitaly Kuznetsov /* 24412bc39970SVitaly Kuznetsov * Default number of spinlock retry attempts, matches 24422bc39970SVitaly Kuznetsov * HyperV 2016. 24432bc39970SVitaly Kuznetsov */ 24442bc39970SVitaly Kuznetsov ent->ebx = 0x00000FFF; 24452bc39970SVitaly Kuznetsov 24462bc39970SVitaly Kuznetsov break; 24472bc39970SVitaly Kuznetsov 24482bc39970SVitaly Kuznetsov case HYPERV_CPUID_IMPLEMENT_LIMITS: 24492bc39970SVitaly Kuznetsov /* Maximum number of virtual processors */ 24502bc39970SVitaly Kuznetsov ent->eax = KVM_MAX_VCPUS; 24512bc39970SVitaly Kuznetsov /* 24522bc39970SVitaly Kuznetsov * Maximum number of logical processors, matches 24532bc39970SVitaly Kuznetsov * HyperV 2016. 24542bc39970SVitaly Kuznetsov */ 24552bc39970SVitaly Kuznetsov ent->ebx = 64; 24562bc39970SVitaly Kuznetsov 24572bc39970SVitaly Kuznetsov break; 24582bc39970SVitaly Kuznetsov 24592bc39970SVitaly Kuznetsov case HYPERV_CPUID_NESTED_FEATURES: 24602bc39970SVitaly Kuznetsov ent->eax = evmcs_ver; 24612bc39970SVitaly Kuznetsov 24622bc39970SVitaly Kuznetsov break; 24632bc39970SVitaly Kuznetsov 2464f97f5a56SJon Doron case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS: 2465f97f5a56SJon Doron memcpy(signature, "Linux KVM Hv", 12); 2466f97f5a56SJon Doron 2467f97f5a56SJon Doron ent->eax = 0; 2468f97f5a56SJon Doron ent->ebx = signature[0]; 2469f97f5a56SJon Doron ent->ecx = signature[1]; 2470f97f5a56SJon Doron ent->edx = signature[2]; 2471f97f5a56SJon Doron break; 2472f97f5a56SJon Doron 2473f97f5a56SJon Doron case HYPERV_CPUID_SYNDBG_INTERFACE: 2474f97f5a56SJon Doron memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12); 2475f97f5a56SJon Doron ent->eax = signature[0]; 2476f97f5a56SJon Doron break; 2477f97f5a56SJon Doron 2478f97f5a56SJon Doron case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES: 2479f97f5a56SJon Doron ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING; 2480f97f5a56SJon Doron break; 2481f97f5a56SJon Doron 24822bc39970SVitaly Kuznetsov default: 24832bc39970SVitaly Kuznetsov break; 24842bc39970SVitaly Kuznetsov } 24852bc39970SVitaly Kuznetsov } 24862bc39970SVitaly Kuznetsov 24872bc39970SVitaly Kuznetsov if (copy_to_user(entries, cpuid_entries, 24882bc39970SVitaly Kuznetsov nent * sizeof(struct kvm_cpuid_entry2))) 24892bc39970SVitaly Kuznetsov return -EFAULT; 24902bc39970SVitaly Kuznetsov 24912bc39970SVitaly Kuznetsov return 0; 24922bc39970SVitaly Kuznetsov } 2493