xref: /linux/arch/x86/kvm/msrs.h (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef ARCH_X86_KVM_MSRS_H
3 #define ARCH_X86_KVM_MSRS_H
4 
5 #include <linux/kvm_host.h>
6 #include <linux/user-return-notifier.h>
7 
8 #include "cpuid.h"
9 #include "regs.h"
10 
11 extern bool report_ignored_msrs;
12 extern bool ignore_msrs;
13 
14 extern u32 __read_mostly kvm_nr_uret_msrs;
15 
16 static inline void kvm_pr_unimpl_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
17 {
18 	if (report_ignored_msrs)
19 		vcpu_unimpl(vcpu, "Unhandled WRMSR(0x%x) = 0x%llx\n", msr, data);
20 }
21 
22 static inline void kvm_pr_unimpl_rdmsr(struct kvm_vcpu *vcpu, u32 msr)
23 {
24 	if (report_ignored_msrs)
25 		vcpu_unimpl(vcpu, "Unhandled RDMSR(0x%x)\n", msr);
26 }
27 
28 /*
29  * The first...last VMX feature MSRs that are emulated by KVM.  This may or may
30  * not cover all known VMX MSRs, as KVM doesn't emulate an MSR until there's an
31  * associated feature that KVM supports for nested virtualization.
32  */
33 #define KVM_FIRST_EMULATED_VMX_MSR	MSR_IA32_VMX_BASIC
34 #define KVM_LAST_EMULATED_VMX_MSR	MSR_IA32_VMX_VMFUNC
35 
36 /*
37  * KVM's internal, non-ABI indices for synthetic MSRs. The values themselves
38  * are arbitrary and have no meaning, the only requirement is that they don't
39  * conflict with "real" MSRs that KVM supports. Use values at the upper end
40  * of KVM's reserved paravirtual MSR range to minimize churn, i.e. these values
41  * will be usable until KVM exhausts its supply of paravirtual MSR indices.
42  */
43 #define MSR_KVM_INTERNAL_GUEST_SSP	0x4b564dff
44 
45 #define MSR_IA32_CR_PAT_DEFAULT	\
46 	PAT_VALUE(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC)
47 
48 void kvm_init_msr_lists(void);
49 int kvm_get_msr_index_list(struct kvm_msr_list __user *user_msr_list);
50 int kvm_get_feature_msr_index_list(struct kvm_msr_list __user *user_msr_list);
51 int kvm_get_feature_msrs(struct kvm_msrs __user *user_msrs);
52 
53 int kvm_get_msrs(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs);
54 int kvm_set_msrs(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs);
55 
56 int kvm_get_set_one_reg(struct kvm_vcpu *vcpu, unsigned int ioctl,
57 			void __user *argp);
58 int kvm_get_reg_list(struct kvm_vcpu *vcpu,
59 		     struct kvm_reg_list __user *user_list);
60 
61 bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
62 int kvm_emulate_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
63 int kvm_emulate_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
64 int __kvm_emulate_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
65 int __kvm_emulate_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
66 int kvm_msr_read(struct kvm_vcpu *vcpu, u32 index, u64 *data);
67 int kvm_msr_write(struct kvm_vcpu *vcpu, u32 index, u64 data);
68 int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu);
69 int kvm_emulate_rdmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg);
70 int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu);
71 int kvm_emulate_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg);
72 
73 fastpath_t handle_fastpath_wrmsr(struct kvm_vcpu *vcpu);
74 fastpath_t handle_fastpath_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg);
75 
76 int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
77 int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
78 
79 int kvm_add_user_return_msr(u32 msr);
80 int kvm_find_user_return_msr(u32 msr);
81 int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask);
82 u64 kvm_get_user_return_msr(unsigned int slot);
83 
84 static inline bool kvm_is_supported_user_return_msr(u32 msr)
85 {
86 	return kvm_find_user_return_msr(msr) >= 0;
87 }
88 
89 void kvm_user_return_msr_cpu_online(void);
90 void drop_user_return_notifiers(void);
91 void kvm_destroy_user_return_msrs(void);
92 
93 int kvm_emulator_get_msr_with_filter(struct kvm_vcpu *vcpu, u32 msr_index,
94 				     u64 *pdata);
95 int kvm_emulator_set_msr_with_filter(struct kvm_vcpu *vcpu, u32 msr_index,
96 				     u64 data);
97 int kvm_emulator_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
98 
99 bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
100 
101 enum kvm_msr_access {
102 	MSR_TYPE_R	= BIT(0),
103 	MSR_TYPE_W	= BIT(1),
104 	MSR_TYPE_RW	= MSR_TYPE_R | MSR_TYPE_W,
105 };
106 
107 /*
108  * Internal error codes that are used to indicate that MSR emulation encountered
109  * an error that should result in #GP in the guest, unless userspace handles it.
110  * Note, '1', '0', and negative numbers are off limits, as they are used by KVM
111  * as part of KVM's lightly documented internal KVM_RUN return codes.
112  *
113  * UNSUPPORTED	- The MSR isn't supported, either because it is completely
114  *		  unknown to KVM, or because the MSR should not exist according
115  *		  to the vCPU model.
116  *
117  * FILTERED	- Access to the MSR is denied by a userspace MSR filter.
118  */
119 #define  KVM_MSR_RET_UNSUPPORTED	2
120 #define  KVM_MSR_RET_FILTERED		3
121 
122 int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, struct kvm_msr_filter *filter);
123 void kvm_free_msr_filter(struct kvm_x86_msr_filter *msr_filter);
124 
125 int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
126 int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
127 
128 u64 kvm_get_arch_capabilities(void);
129 int kvm_spec_ctrl_test_value(u64 value);
130 
131 #define CET_US_RESERVED_BITS		GENMASK(9, 6)
132 #define CET_US_SHSTK_MASK_BITS		GENMASK(1, 0)
133 #define CET_US_IBT_MASK_BITS		(GENMASK_ULL(5, 2) | GENMASK_ULL(63, 10))
134 #define CET_US_LEGACY_BITMAP_BASE(data)	((data) >> 12)
135 
136 static inline bool kvm_is_valid_u_s_cet(struct kvm_vcpu *vcpu, u64 data)
137 {
138 	if (data & CET_US_RESERVED_BITS)
139 		return false;
140 	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_SHSTK) &&
141 	    (data & CET_US_SHSTK_MASK_BITS))
142 		return false;
143 	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_IBT) &&
144 	    (data & CET_US_IBT_MASK_BITS))
145 		return false;
146 	if (!IS_ALIGNED(CET_US_LEGACY_BITMAP_BASE(data), 4))
147 		return false;
148 	/* IBT can be suppressed iff the TRACKER isn't WAIT_ENDBR. */
149 	if ((data & CET_SUPPRESS) && (data & CET_WAIT_ENDBR))
150 		return false;
151 
152 	return true;
153 }
154 
155 #endif
156