Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * KVM Microsoft Hyper-V emulation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * derived from arch/x86/kvm/x86.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (C) 2006 Qumranet, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Copyright (C) 2008 Qumranet, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * Copyright IBM Corporation, 2008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *   Avi Kivity   <avi@qumranet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *   Yaniv Kamay  <yaniv@qumranet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *   Amit Shah    <amit.shah@qumranet.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *   Ben-Ami Yassour <benami@il.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *   Andrey Smetanin <asmetanin@virtuozzo.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "x86.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "lapic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "ioapic.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "cpuid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "hyperv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/sched/cputime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/eventfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <asm/apicdef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <trace/events/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "irq.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 				bool vcpu_kick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	return atomic64_read(&synic->sint[sint]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static inline int synic_get_sint_vector(u64 sint_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	if (sint_value & HV_SYNIC_SINT_MASKED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 				      int vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 				     int vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	u64 sint_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		sint_value = synic_read_sint(synic, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		if (synic_get_sint_vector(sint_value) == vector &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 				int vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	if (synic_has_vector_connected(synic, vector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		__set_bit(vector, synic->vec_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		__clear_bit(vector, synic->vec_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (synic_has_vector_auto_eoi(synic, vector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		__set_bit(vector, synic->auto_eoi_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		__clear_bit(vector, synic->auto_eoi_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			  u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	int vector, old_vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	bool masked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	masked = data & HV_SYNIC_SINT_MASKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	 * default '0x10000' value on boot and this should not #GP. We need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 * allow zero-initing the register from host as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	 * Guest may configure multiple SINTs to use the same vector, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	 * we maintain a bitmap of vectors handled by synic, and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	 * updated here, and atomically queried on fast paths.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	atomic64_set(&synic->sint[sint], data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	synic_update_vector(synic, old_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	synic_update_vector(synic, vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	/* Load SynIC vectors into EOI exit bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct kvm_vcpu *vcpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (vpidx >= KVM_MAX_VCPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	vcpu = kvm_get_vcpu(kvm, vpidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		return vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	kvm_for_each_vcpu(i, vcpu, kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			return vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	struct kvm_vcpu_hv_synic *synic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	vcpu = get_vcpu_by_vpidx(kvm, vpidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	if (!vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return (synic->active) ? synic : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	struct kvm_vcpu_hv_stimer *stimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	int gsi, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	/* Try to deliver pending Hyper-V SynIC timers messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		stimer = &hv_vcpu->stimer[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		if (stimer->msg_pending && stimer->config.enable &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		    !stimer->config.direct_mode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		    stimer->config.sintx == sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			stimer_mark_pending(stimer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	idx = srcu_read_lock(&kvm->irq_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	gsi = atomic_read(&synic->sint_to_gsi[sint]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	if (gsi != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		kvm_notify_acked_gsi(kvm, gsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	srcu_read_unlock(&kvm->irq_srcu, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	hv_vcpu->exit.u.synic.msr = msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	hv_vcpu->exit.u.synic.control = synic->control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			 u32 msr, u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (!synic->active && (!host || data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	case HV_X64_MSR_SCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		synic->control = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			synic_exit(synic, msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	case HV_X64_MSR_SVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		if (!host) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		synic->version = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	case HV_X64_MSR_SIEFP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		    !synic->dont_zero_synic_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			if (kvm_clear_guest(vcpu->kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 					    data & PAGE_MASK, PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 				ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		synic->evt_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 			synic_exit(synic, msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	case HV_X64_MSR_SIMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		    !synic->dont_zero_synic_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 			if (kvm_clear_guest(vcpu->kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 					    data & PAGE_MASK, PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 				ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		synic->msg_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			synic_exit(synic, msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	case HV_X64_MSR_EOM: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		if (!synic->active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			kvm_hv_notify_acked_sint(vcpu, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	struct kvm_cpuid_entry2 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	entry = kvm_find_cpuid_entry(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 				     HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 				     0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	return entry->eax & HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		hv->hv_syndbg.control.status =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			vcpu->run->hyperv.u.syndbg.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	hv_vcpu->exit.u.syndbg.msr = msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	vcpu->arch.complete_userspace_io =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 			kvm_hv_syndbg_complete_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 				    vcpu_to_hv_vcpu(vcpu)->vp_index, msr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	case HV_X64_MSR_SYNDBG_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		syndbg->control.control = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 			syndbg_exit(vcpu, msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	case HV_X64_MSR_SYNDBG_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		syndbg->control.status = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	case HV_X64_MSR_SYNDBG_SEND_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		syndbg->control.send_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	case HV_X64_MSR_SYNDBG_RECV_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		syndbg->control.recv_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		syndbg->control.pending_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			syndbg_exit(vcpu, msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	case HV_X64_MSR_SYNDBG_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		syndbg->options = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	case HV_X64_MSR_SYNDBG_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		*pdata = syndbg->control.control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	case HV_X64_MSR_SYNDBG_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		*pdata = syndbg->control.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	case HV_X64_MSR_SYNDBG_SEND_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		*pdata = syndbg->control.send_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	case HV_X64_MSR_SYNDBG_RECV_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		*pdata = syndbg->control.recv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		*pdata = syndbg->control.pending_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	case HV_X64_MSR_SYNDBG_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		*pdata = syndbg->options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 				    vcpu_to_hv_vcpu(vcpu)->vp_index, msr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 				    *pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 			 bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (!synic->active && !host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	case HV_X64_MSR_SCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		*pdata = synic->control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case HV_X64_MSR_SVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		*pdata = synic->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	case HV_X64_MSR_SIEFP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		*pdata = synic->evt_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	case HV_X64_MSR_SIMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		*pdata = synic->msg_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	case HV_X64_MSR_EOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		*pdata = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	struct kvm_lapic_irq irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	int ret, vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (sint >= ARRAY_SIZE(synic->sint))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (vector < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	memset(&irq, 0, sizeof(irq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	irq.shorthand = APIC_DEST_SELF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	irq.dest_mode = APIC_DEST_PHYSICAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	irq.delivery_mode = APIC_DM_FIXED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	irq.vector = vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	irq.level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	struct kvm_vcpu_hv_synic *synic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	synic = synic_get(kvm, vpidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (!synic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	return synic_set_irq(synic, sint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			kvm_hv_notify_acked_sint(vcpu, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	struct kvm_vcpu_hv_synic *synic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	synic = synic_get(kvm, vpidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	if (!synic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	atomic_set(&synic->sint_to_gsi[sint], gsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) void kvm_hv_irq_routing_update(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	struct kvm_irq_routing_table *irq_rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct kvm_kernel_irq_routing_entry *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	u32 gsi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 					lockdep_is_held(&kvm->irq_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 						    e->hv_sint.sint, gsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) static void synic_init(struct kvm_vcpu_hv_synic *synic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	memset(synic, 0, sizeof(*synic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	synic->version = HV_SYNIC_VERSION_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		atomic_set(&synic->sint_to_gsi[i], -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static u64 get_time_ref_counter(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	u64 tsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	 * The guest has not set up the TSC page or the clock isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	 * stable, fall back to get_kvmclock_ns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	if (!hv->tsc_ref.tsc_sequence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		return div_u64(get_kvmclock_ns(kvm), 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	vcpu = kvm_get_vcpu(kvm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	tsc = kvm_read_l1_tsc(vcpu, rdtsc());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		+ hv->tsc_ref.tsc_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 				bool vcpu_kick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	set_bit(stimer->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (vcpu_kick)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		kvm_vcpu_kick(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 				    stimer->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	hrtimer_cancel(&stimer->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	clear_bit(stimer->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		  vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	stimer->msg_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	stimer->exp_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	struct kvm_vcpu_hv_stimer *stimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 				     stimer->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	stimer_mark_pending(stimer, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)  * stimer_start() assumptions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * a) stimer->count is not equal to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * b) stimer->config has HV_STIMER_ENABLE flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	u64 time_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	ktime_t ktime_now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	ktime_now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	if (stimer->config.periodic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (stimer->exp_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			if (time_now >= stimer->exp_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 				u64 remainder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				div64_u64_rem(time_now - stimer->exp_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 					      stimer->count, &remainder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 				stimer->exp_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 					time_now + (stimer->count - remainder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 			stimer->exp_time = time_now + stimer->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		trace_kvm_hv_stimer_start_periodic(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 					stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 					stimer->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 					time_now, stimer->exp_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		hrtimer_start(&stimer->timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			      ktime_add_ns(ktime_now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 					   100 * (stimer->exp_time - time_now)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			      HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	stimer->exp_time = stimer->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (time_now >= stimer->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		 * Expire timer according to Hypervisor Top-Level Functional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		 * specification v4(15.3.1):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		 * "If a one shot is enabled and the specified count is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		 * the past, it will expire immediately."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		stimer_mark_pending(stimer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 					   stimer->index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 					   time_now, stimer->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	hrtimer_start(&stimer->timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		      ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		      HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			     bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	union hv_stimer_config new_config = {.as_uint64 = config},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		old_config = {.as_uint64 = stimer->config.as_uint64};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (!synic->active && (!host || config))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 				       stimer->index, config, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	stimer_cleanup(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (old_config.enable &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	    !new_config.direct_mode && new_config.sintx == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		new_config.enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	stimer->config.as_uint64 = new_config.as_uint64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	if (stimer->config.enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		stimer_mark_pending(stimer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			    bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	if (!synic->active && (!host || count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 				      stimer->index, count, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	stimer_cleanup(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	stimer->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (stimer->count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		stimer->config.enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	else if (stimer->config.auto_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		stimer->config.enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (stimer->config.enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		stimer_mark_pending(stimer, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	*pconfig = stimer->config.as_uint64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	*pcount = stimer->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			     struct hv_message *src_msg, bool no_retry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	gfn_t msg_page_gfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	struct hv_message_header hv_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	 * Strictly following the spec-mandated ordering would assume setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	 * .msg_pending before checking .message_type.  However, this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	 * is only called in vcpu context so the entire update is atomic from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	 * guest POV and thus the exact order here doesn't matter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 				     msg_off + offsetof(struct hv_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 							header.message_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 				     sizeof(hv_hdr.message_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (hv_hdr.message_type != HVMSG_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		if (no_retry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		hv_hdr.message_flags.msg_pending = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 					      &hv_hdr.message_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 					      msg_off +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 					      offsetof(struct hv_message,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 						       header.message_flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 					      sizeof(hv_hdr.message_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				      sizeof(src_msg->header) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 				      src_msg->header.payload_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	r = synic_set_irq(synic, sint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (r == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	struct hv_message *msg = &stimer->msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	struct hv_timer_message_payload *payload =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			(struct hv_timer_message_payload *)&msg->u.payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	 * To avoid piling up periodic ticks, don't retry message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	 * delivery for them (within "lazy" lost ticks policy).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	bool no_retry = stimer->config.periodic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	payload->expiration_time = stimer->exp_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	payload->delivery_time = get_time_ref_counter(vcpu->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	return synic_deliver_msg(vcpu_to_synic(vcpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				 stimer->config.sintx, msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				 no_retry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	struct kvm_lapic_irq irq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		.delivery_mode = APIC_DM_FIXED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		.vector = stimer->config.apic_vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (lapic_in_kernel(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		return !kvm_apic_set_irq(vcpu, &irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	int r, direct = stimer->config.direct_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	stimer->msg_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (!direct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		r = stimer_send_msg(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		r = stimer_notify_direct(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 				       stimer->index, direct, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	if (!r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		stimer->msg_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		if (!(stimer->config.periodic))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			stimer->config.enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	struct kvm_vcpu_hv_stimer *stimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	u64 time_now, exp_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 			stimer = &hv_vcpu->stimer[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			if (stimer->config.enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 				exp_time = stimer->exp_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 				if (exp_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 					time_now =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 						get_time_ref_counter(vcpu->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 					if (time_now >= exp_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 						stimer_expiration(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 				if ((stimer->config.enable) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 				    stimer->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 					if (!stimer->msg_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 						stimer_start(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 					stimer_cleanup(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		stimer_cleanup(&hv_vcpu->stimer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (!(vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			    struct hv_vp_assist_page *assist_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (!kvm_hv_assist_page_enabled(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 				      assist_page, sizeof(*assist_page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	struct hv_message *msg = &stimer->msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	struct hv_timer_message_payload *payload =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			(struct hv_timer_message_payload *)&msg->u.payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	memset(&msg->header, 0, sizeof(msg->header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	msg->header.message_type = HVMSG_TIMER_EXPIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	msg->header.payload_size = sizeof(*payload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	payload->timer_index = stimer->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	payload->expiration_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	payload->delivery_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	memset(stimer, 0, sizeof(*stimer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	stimer->index = timer_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	stimer->timer.function = stimer_timer_callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	stimer_prepare_msg(stimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	synic_init(&hv_vcpu->synic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		stimer_init(&hv_vcpu->stimer[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	 * Hyper-V SynIC auto EOI SINT's are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	 * not compatible with APICV, so request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	 * to deactivate APICV permanently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	synic->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	synic->dont_zero_synic_pages = dont_zero_synic_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	synic->control = HV_SYNIC_CONTROL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) static bool kvm_hv_msr_partition_wide(u32 msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	bool r = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	case HV_X64_MSR_GUEST_OS_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	case HV_X64_MSR_HYPERCALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	case HV_X64_MSR_REFERENCE_TSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	case HV_X64_MSR_TIME_REF_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	case HV_X64_MSR_CRASH_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	case HV_X64_MSR_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	case HV_X64_MSR_TSC_EMULATION_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	case HV_X64_MSR_SYNDBG_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		r = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 				     u32 index, u64 *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	size_t size = ARRAY_SIZE(hv->hv_crash_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	if (WARN_ON_ONCE(index >= size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	*pdata = hv->hv_crash_param[array_index_nospec(index, size)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	*pdata = hv->hv_crash_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	if (host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	if (!host && (data & HV_CRASH_CTL_CRASH_NOTIFY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			  hv->hv_crash_param[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			  hv->hv_crash_param[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			  hv->hv_crash_param[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			  hv->hv_crash_param[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			  hv->hv_crash_param[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		/* Send notification about crash to user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 				     u32 index, u64 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	size_t size = ARRAY_SIZE(hv->hv_crash_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	if (WARN_ON_ONCE(index >= size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	hv->hv_crash_param[array_index_nospec(index, size)] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * between them is possible:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * kvmclock formula:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  *           + system_time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * Hyper-V formula:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  *    nsec/100 = ticks * scale / 2^64 + offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)  * By dividing the kvmclock formula by 100 and equating what's left we get:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)  * Now expand the kvmclock formula and divide by 100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)  *           + system_time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  *               + system_time / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  *    nsec/100 = ticks * scale / 2^64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  *               - tsc_timestamp * scale / 2^64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  *               + system_time / 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  * These two equivalencies are implemented in this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 					struct ms_hyperv_tsc_page *tsc_ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	u64 max_mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	 * check if scale would overflow, if so we use the time ref counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	 *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	max_mul = 100ull << (32 - hv_clock->tsc_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (hv_clock->tsc_to_system_mul >= max_mul)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 * Otherwise compute the scale and offset according to the formulas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	 * derived above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	tsc_ref->tsc_scale =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 				hv_clock->tsc_to_system_mul,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 				100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	tsc_ref->tsc_offset = hv_clock->system_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	do_div(tsc_ref->tsc_offset, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	tsc_ref->tsc_offset -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) void kvm_hv_setup_tsc_page(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			   struct pvclock_vcpu_time_info *hv_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	u32 tsc_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	u64 gfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	mutex_lock(&kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	 * Because the TSC parameters only vary when there is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	 * change in the master clock, do not bother with caching.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 				    &tsc_seq, sizeof(tsc_seq))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	 * While we're computing and writing the parameters, force the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	 * guest to use the time reference count MSR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	hv->tsc_ref.tsc_sequence = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			    &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	/* Ensure sequence is zero before writing the rest of the struct.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	 * Now switch to the TSC page mechanism by writing the sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	tsc_seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		tsc_seq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	/* Write the struct entirely before the non-zero sequence.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	hv->tsc_ref.tsc_sequence = tsc_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	kvm_write_guest(kvm, gfn_to_gpa(gfn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	mutex_unlock(&kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 			     bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	case HV_X64_MSR_GUEST_OS_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		hv->hv_guest_os_id = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		/* setting guest os id to zero disables hypercall page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		if (!hv->hv_guest_os_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	case HV_X64_MSR_HYPERCALL: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		u64 gfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		u8 instructions[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		/* if guest os id is not set hypercall should remain disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		if (!hv->hv_guest_os_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			hv->hv_hypercall = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		addr = gfn_to_hva(kvm, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		if (kvm_is_error_hva(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		kvm_x86_ops.patch_hypercall(vcpu, instructions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		if (__copy_to_user((void __user *)addr, instructions, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		hv->hv_hypercall = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		mark_page_dirty(kvm, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	case HV_X64_MSR_REFERENCE_TSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		hv->hv_tsc_page = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		return kvm_hv_msr_set_crash_data(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 						 msr - HV_X64_MSR_CRASH_P0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 						 data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	case HV_X64_MSR_CRASH_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	case HV_X64_MSR_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		if (data == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			vcpu_debug(vcpu, "hyper-v reset requested\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		hv->hv_reenlightenment_control = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		hv->hv_tsc_emulation_control = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	case HV_X64_MSR_TSC_EMULATION_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		hv->hv_tsc_emulation_status = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	case HV_X64_MSR_TIME_REF_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		/* read-only, but still ignore it if host-initiated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	case HV_X64_MSR_SYNDBG_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		return syndbg_set_msr(vcpu, msr, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 			    msr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) /* Calculate cpu time spent by current task in 100ns units */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static u64 current_task_runtime_100ns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	u64 utime, stime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	task_cputime_adjusted(current, &utime, &stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	return div_u64(utime + stime, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	case HV_X64_MSR_VP_INDEX: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		int vcpu_idx = kvm_vcpu_get_idx(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		u32 new_vp_index = (u32)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		if (!host || new_vp_index >= KVM_MAX_VCPUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		if (new_vp_index == hv_vcpu->vp_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		 * The VP index is initialized to vcpu_index by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		 * kvm_hv_vcpu_postcreate so they initially match.  Now the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		 * VP index is changing, adjust num_mismatched_vp_indexes if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		 * it now matches or no longer matches vcpu_idx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (hv_vcpu->vp_index == vcpu_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			atomic_inc(&hv->num_mismatched_vp_indexes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		else if (new_vp_index == vcpu_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			atomic_dec(&hv->num_mismatched_vp_indexes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		hv_vcpu->vp_index = new_vp_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	case HV_X64_MSR_VP_ASSIST_PAGE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		u64 gfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 			hv_vcpu->hv_vapic = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		if (kvm_is_error_hva(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		 * Clear apic_assist portion of struct hv_vp_assist_page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		 * only, there can be valuable data in the rest which needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		 * to be preserved e.g. on migration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		if (__put_user(0, (u32 __user *)addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		hv_vcpu->hv_vapic = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		if (kvm_lapic_enable_pv_eoi(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 					    sizeof(struct hv_vp_assist_page)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	case HV_X64_MSR_EOI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	case HV_X64_MSR_ICR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	case HV_X64_MSR_TPR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	case HV_X64_MSR_VP_RUNTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 		hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	case HV_X64_MSR_SCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	case HV_X64_MSR_SVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	case HV_X64_MSR_SIEFP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	case HV_X64_MSR_SIMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	case HV_X64_MSR_EOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	case HV_X64_MSR_STIMER0_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	case HV_X64_MSR_STIMER1_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	case HV_X64_MSR_STIMER2_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	case HV_X64_MSR_STIMER3_CONFIG: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 					 data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	case HV_X64_MSR_STIMER0_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	case HV_X64_MSR_STIMER1_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	case HV_X64_MSR_STIMER2_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	case HV_X64_MSR_STIMER3_COUNT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 					data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	case HV_X64_MSR_TSC_FREQUENCY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	case HV_X64_MSR_APIC_FREQUENCY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		/* read-only, but still ignore it if host-initiated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (!host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			    msr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 			     bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	u64 data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	case HV_X64_MSR_GUEST_OS_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		data = hv->hv_guest_os_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	case HV_X64_MSR_HYPERCALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		data = hv->hv_hypercall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	case HV_X64_MSR_TIME_REF_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		data = get_time_ref_counter(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	case HV_X64_MSR_REFERENCE_TSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		data = hv->hv_tsc_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		return kvm_hv_msr_get_crash_data(vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 						 msr - HV_X64_MSR_CRASH_P0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 						 pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	case HV_X64_MSR_CRASH_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	case HV_X64_MSR_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		data = hv->hv_reenlightenment_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		data = hv->hv_tsc_emulation_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	case HV_X64_MSR_TSC_EMULATION_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		data = hv->hv_tsc_emulation_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	case HV_X64_MSR_SYNDBG_OPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		return syndbg_get_msr(vcpu, msr, pdata, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	*pdata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			  bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	u64 data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	switch (msr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	case HV_X64_MSR_VP_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		data = hv_vcpu->vp_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	case HV_X64_MSR_EOI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	case HV_X64_MSR_ICR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	case HV_X64_MSR_TPR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	case HV_X64_MSR_VP_ASSIST_PAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		data = hv_vcpu->hv_vapic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	case HV_X64_MSR_VP_RUNTIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	case HV_X64_MSR_SCONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	case HV_X64_MSR_SVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	case HV_X64_MSR_SIEFP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	case HV_X64_MSR_SIMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	case HV_X64_MSR_EOM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	case HV_X64_MSR_STIMER0_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	case HV_X64_MSR_STIMER1_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	case HV_X64_MSR_STIMER2_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	case HV_X64_MSR_STIMER3_CONFIG: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 					 pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	case HV_X64_MSR_STIMER0_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	case HV_X64_MSR_STIMER1_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	case HV_X64_MSR_STIMER2_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	case HV_X64_MSR_STIMER3_COUNT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 					pdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	case HV_X64_MSR_TSC_FREQUENCY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	case HV_X64_MSR_APIC_FREQUENCY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		data = APIC_BUS_FREQUENCY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	*pdata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	if (kvm_hv_msr_partition_wide(msr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		return kvm_hv_set_msr(vcpu, msr, data, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	if (kvm_hv_msr_partition_wide(msr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		return kvm_hv_get_msr(vcpu, msr, pdata, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) static __always_inline unsigned long *sparse_set_to_vcpu_mask(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	u64 *vp_bitmap, unsigned long *vcpu_bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	int i, bank, sbank = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	memset(vp_bitmap, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	       KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		vp_bitmap[bank] = sparse_banks[sbank++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		/* for all vcpus vp_index == vcpu_idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		return (unsigned long *)vp_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		if (test_bit(vcpu_to_hv_vcpu(vcpu)->vp_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 			     (unsigned long *)vp_bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 			__set_bit(i, vcpu_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	return vcpu_bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 			    u16 rep_cnt, bool ex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	struct kvm *kvm = current_vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	struct kvm_vcpu_hv *hv_vcpu = &current_vcpu->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	struct hv_tlb_flush_ex flush_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	struct hv_tlb_flush flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	unsigned long *vcpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	u64 valid_bank_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	u64 sparse_banks[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	int sparse_banks_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	bool all_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	if (!ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		trace_kvm_hv_flush_tlb(flush.processor_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 				       flush.address_space, flush.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		valid_bank_mask = BIT_ULL(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		sparse_banks[0] = flush.processor_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		 * Work around possible WS2012 bug: it sends hypercalls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		 * while also expecting us to flush something and crashing if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		 * we don't. Let's treat processor_mask == 0 same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		 * HV_FLUSH_ALL_PROCESSORS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			flush.processor_mask == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 					    sizeof(flush_ex))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 					  flush_ex.hv_vp_set.format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 					  flush_ex.address_space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 					  flush_ex.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		all_cpus = flush_ex.hv_vp_set.format !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			HV_GENERIC_SET_SPARSE_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		sparse_banks_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			bitmap_weight((unsigned long *)&valid_bank_mask, 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 			sizeof(sparse_banks[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		if (!sparse_banks_len && !all_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 			goto ret_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		if (!all_cpus &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		    kvm_read_guest(kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 				   ingpa + offsetof(struct hv_tlb_flush_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 						    hv_vp_set.bank_contents),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 				   sparse_banks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 				   sparse_banks_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	cpumask_clear(&hv_vcpu->tlb_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	vcpu_mask = all_cpus ? NULL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 					vp_bitmap, vcpu_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	 * analyze it here, flush TLB regardless of the specified address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 				    NULL, vcpu_mask, &hv_vcpu->tlb_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) ret_success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	/* We always do full TLB flush, set rep_done = rep_cnt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	return (u64)HV_STATUS_SUCCESS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 				 unsigned long *vcpu_bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	struct kvm_lapic_irq irq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		.delivery_mode = APIC_DM_FIXED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		.vector = vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		/* We fail only when APIC is disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		kvm_apic_set_irq(vcpu, &irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) static u64 kvm_hv_send_ipi(struct kvm_vcpu *current_vcpu, u64 ingpa, u64 outgpa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 			   bool ex, bool fast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	struct kvm *kvm = current_vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	struct hv_send_ipi_ex send_ipi_ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	struct hv_send_ipi send_ipi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	unsigned long *vcpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	unsigned long valid_bank_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	u64 sparse_banks[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	int sparse_banks_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	u32 vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	bool all_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	if (!ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		if (!fast) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 			if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 						    sizeof(send_ipi))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 			sparse_banks[0] = send_ipi.cpu_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			vector = send_ipi.vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 			/* 'reserved' part of hv_send_ipi should be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 			if (unlikely(ingpa >> 32 != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			sparse_banks[0] = outgpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 			vector = (u32)ingpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		all_cpus = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 		valid_bank_mask = BIT_ULL(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 					    sizeof(send_ipi_ex))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 					 send_ipi_ex.vp_set.format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 					 send_ipi_ex.vp_set.valid_bank_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		vector = send_ipi_ex.vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			sizeof(sparse_banks[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		if (all_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			goto check_and_send_ipi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		if (!sparse_banks_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 			goto ret_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		if (kvm_read_guest(kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 				   ingpa + offsetof(struct hv_send_ipi_ex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 						    vp_set.bank_contents),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 				   sparse_banks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 				   sparse_banks_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) check_and_send_ipi:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	vcpu_mask = all_cpus ? NULL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 					vp_bitmap, vcpu_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) ret_success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	return HV_STATUS_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) bool kvm_hv_hypercall_enabled(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	return READ_ONCE(kvm->arch.hyperv.hv_guest_os_id) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	bool longmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	longmode = is_64_bit_mode(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	if (longmode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		kvm_rax_write(vcpu, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		kvm_rdx_write(vcpu, result >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		kvm_rax_write(vcpu, result & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	kvm_hv_hypercall_set_result(vcpu, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	++vcpu->stat.hypercalls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	return kvm_skip_emulated_instruction(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	struct eventfd_ctx *eventfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	if (unlikely(!fast)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		gpa_t gpa = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		if ((gpa & (__alignof__(param) - 1)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		    offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 			return HV_STATUS_INVALID_ALIGNMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		ret = kvm_vcpu_read_guest(vcpu, gpa, &param, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			return HV_STATUS_INVALID_ALIGNMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	 * Per spec, bits 32-47 contain the extra "flag number".  However, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	 * have no use for it, and in all known usecases it is zero, so just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	 * report lookup failure if it isn't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	if (param & 0xffff00000000ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		return HV_STATUS_INVALID_PORT_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	/* remaining bits are reserved-zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	if (param & ~KVM_HYPERV_CONN_ID_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	/* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	if (!eventfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		return HV_STATUS_INVALID_PORT_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	eventfd_signal(eventfd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	return HV_STATUS_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	uint16_t code, rep_idx, rep_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	bool fast, rep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	 * hypercall generates UD from non zero cpl and real mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	 * per HYPER-V spec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	if (kvm_x86_ops.get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		kvm_queue_exception(vcpu, UD_VECTOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (is_64_bit_mode(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		param = kvm_rcx_read(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		ingpa = kvm_rdx_read(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		outgpa = kvm_r8_read(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		param = ((u64)kvm_rdx_read(vcpu) << 32) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 			(kvm_rax_read(vcpu) & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			(kvm_rcx_read(vcpu) & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 			(kvm_rsi_read(vcpu) & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	code = param & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	fast = !!(param & HV_HYPERCALL_FAST_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	rep = !!(rep_cnt || rep_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		if (unlikely(rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		kvm_vcpu_on_spin(vcpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	case HVCALL_SIGNAL_EVENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		if (unlikely(rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		ret = kvm_hvcall_signal_event(vcpu, fast, ingpa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		if (ret != HV_STATUS_INVALID_PORT_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		fallthrough;	/* maybe userspace knows this conn_id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	case HVCALL_POST_MESSAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		/* don't bother userspace if it has no way to handle it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		if (unlikely(rep || !vcpu_to_synic(vcpu)->active)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		vcpu->run->hyperv.u.hcall.input = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		vcpu->arch.complete_userspace_io =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 				kvm_hv_hypercall_complete_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		if (unlikely(fast || !rep_cnt || rep_idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		if (unlikely(fast || rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		if (unlikely(fast || !rep_cnt || rep_idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 		if (unlikely(fast || rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	case HVCALL_SEND_IPI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		if (unlikely(rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	case HVCALL_SEND_IPI_EX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		if (unlikely(fast || rep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	case HVCALL_POST_DEBUG_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	case HVCALL_RETRIEVE_DEBUG_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		if (unlikely(fast)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 			ret = HV_STATUS_INVALID_PARAMETER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	case HVCALL_RESET_DEBUG_SESSION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		if (!kvm_hv_is_syndbg_enabled(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			ret = HV_STATUS_INVALID_HYPERCALL_CODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 			ret = HV_STATUS_OPERATION_DENIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		vcpu->run->hyperv.u.hcall.input = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		vcpu->arch.complete_userspace_io =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 				kvm_hv_hypercall_complete_userspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		ret = HV_STATUS_INVALID_HYPERCALL_CODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	return kvm_hv_hypercall_complete(vcpu, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) void kvm_hv_init_vm(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	mutex_init(&kvm->arch.hyperv.hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	idr_init(&kvm->arch.hyperv.conn_to_evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) void kvm_hv_destroy_vm(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	struct eventfd_ctx *eventfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		eventfd_ctx_put(eventfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	struct eventfd_ctx *eventfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	eventfd = eventfd_ctx_fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	if (IS_ERR(eventfd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		return PTR_ERR(eventfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	mutex_lock(&hv->hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	mutex_unlock(&hv->hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	if (ret == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	eventfd_ctx_put(eventfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	struct kvm_hv *hv = &kvm->arch.hyperv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	struct eventfd_ctx *eventfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	mutex_lock(&hv->hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	eventfd = idr_remove(&hv->conn_to_evt, conn_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	mutex_unlock(&hv->hv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	if (!eventfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	synchronize_srcu(&kvm->srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	eventfd_ctx_put(eventfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	    (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		return kvm_hv_eventfd_deassign(kvm, args->conn_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 				struct kvm_cpuid_entry2 __user *entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	uint16_t evmcs_ver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	struct kvm_cpuid_entry2 cpuid_entries[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 		{ .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		{ .function = HYPERV_CPUID_INTERFACE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		{ .function = HYPERV_CPUID_VERSION },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		{ .function = HYPERV_CPUID_FEATURES },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		{ .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		{ .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 		{ .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		{ .function = HYPERV_CPUID_SYNDBG_INTERFACE },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		{ .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 		{ .function = HYPERV_CPUID_NESTED_FEATURES },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	int i, nent = ARRAY_SIZE(cpuid_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	if (kvm_x86_ops.nested_ops->get_evmcs_version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	/* Skip NESTED_FEATURES if eVMCS is not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	if (!evmcs_ver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		--nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	if (cpuid->nent < nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	if (cpuid->nent > nent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		cpuid->nent = nent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	for (i = 0; i < nent; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		u32 signature[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		switch (ent->function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 			memcpy(signature, "Linux KVM Hv", 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 			ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 			ent->ebx = signature[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 			ent->ecx = signature[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 			ent->edx = signature[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		case HYPERV_CPUID_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 			ent->eax = signature[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		case HYPERV_CPUID_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			 * We implement some Hyper-V 2016 functions so let's use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			 * this version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			ent->eax = 0x00003839;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 			ent->ebx = 0x000A0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		case HYPERV_CPUID_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 			ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 			ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 			ent->eax |= HV_MSR_SYNIC_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 			ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 			ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 			ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			ent->eax |= HV_MSR_RESET_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 			ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 			ent->eax |= HV_ACCESS_REENLIGHTENMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 			ent->ebx |= HV_POST_MESSAGES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 			ent->ebx |= HV_SIGNAL_EVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 			ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 			ent->ebx |= HV_DEBUGGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 			 * Direct Synthetic timers only make sense with in-kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 			 * LAPIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			if (lapic_in_kernel(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 				ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		case HYPERV_CPUID_ENLIGHTMENT_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 			ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 			ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 			ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 			ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 			if (evmcs_ver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 				ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 			if (!cpu_smt_possible())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 				ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 			 * Default number of spinlock retry attempts, matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 			 * HyperV 2016.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 			ent->ebx = 0x00000FFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		case HYPERV_CPUID_IMPLEMENT_LIMITS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			/* Maximum number of virtual processors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 			ent->eax = KVM_MAX_VCPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			 * Maximum number of logical processors, matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 			 * HyperV 2016.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			ent->ebx = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		case HYPERV_CPUID_NESTED_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			ent->eax = evmcs_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 			memcpy(signature, "Linux KVM Hv", 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 			ent->eax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 			ent->ebx = signature[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			ent->ecx = signature[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			ent->edx = signature[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		case HYPERV_CPUID_SYNDBG_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 			memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 			ent->eax = signature[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 			ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	if (copy_to_user(entries, cpuid_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			 nent * sizeof(struct kvm_cpuid_entry2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) }