blob: 767ec7f9951608f984b4ac69a1c3205ce0a93ebc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Avi Kivity26eef702008-07-03 14:59:22 +03002#ifndef ARCH_X86_KVM_X86_H
3#define ARCH_X86_KVM_X86_H
4
5#include <linux/kvm_host.h>
Uros Bizjak3f1a18b2020-10-29 14:56:00 +01006#include <asm/mce.h>
Marcelo Tosatti8d93c872016-06-20 22:28:02 -03007#include <asm/pvclock.h>
Avi Kivity3eeb3282010-01-21 15:31:48 +02008#include "kvm_cache_regs.h"
Sean Christopherson2f728d62020-02-18 15:29:49 -08009#include "kvm_emulate.h"
Avi Kivity26eef702008-07-03 14:59:22 +030010
Uros Bizjak65297342021-08-09 10:39:55 -070011void kvm_spurious_fault(void);
12
Sean Christopherson648fc8a2021-02-03 16:01:16 -080013#define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \
14({ \
15 bool failed = (consistency_check); \
16 if (failed) \
17 trace_kvm_nested_vmenter_failed(#consistency_check, 0); \
18 failed; \
19})
20
Babu Mogerc8e88712018-03-16 16:37:24 -040021#define KVM_DEFAULT_PLE_GAP 128
22#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
23#define KVM_DEFAULT_PLE_WINDOW_GROW 2
24#define KVM_DEFAULT_PLE_WINDOW_SHRINK 0
25#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX UINT_MAX
Babu Moger8566ac82018-03-16 16:37:26 -040026#define KVM_SVM_DEFAULT_PLE_WINDOW_MAX USHRT_MAX
27#define KVM_SVM_DEFAULT_PLE_WINDOW 3000
Babu Mogerc8e88712018-03-16 16:37:24 -040028
29static inline unsigned int __grow_ple_window(unsigned int val,
30 unsigned int base, unsigned int modifier, unsigned int max)
31{
32 u64 ret = val;
33
34 if (modifier < 1)
35 return base;
36
37 if (modifier < base)
38 ret *= modifier;
39 else
40 ret += modifier;
41
42 return min(ret, (u64)max);
43}
44
45static inline unsigned int __shrink_ple_window(unsigned int val,
46 unsigned int base, unsigned int modifier, unsigned int min)
47{
48 if (modifier < 1)
49 return base;
50
51 if (modifier < base)
52 val /= modifier;
53 else
54 val -= modifier;
55
56 return max(val, min);
57}
58
Radim Krčmář74545702015-04-27 15:11:25 +020059#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL
60
Sean Christopherson40e5f9082021-11-25 01:49:43 +000061void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu);
Sean Christophersoncb6a32c2021-03-02 09:45:14 -080062int kvm_check_nested_events(struct kvm_vcpu *vcpu);
63
Avi Kivity26eef702008-07-03 14:59:22 +030064static inline void kvm_clear_exception_queue(struct kvm_vcpu *vcpu)
65{
Liran Alon5c7d4f92017-11-19 18:25:43 +020066 vcpu->arch.exception.pending = false;
Wanpeng Li664f8e22017-08-24 03:35:09 -070067 vcpu->arch.exception.injected = false;
Avi Kivity26eef702008-07-03 14:59:22 +030068}
69
Gleb Natapov66fd3f72009-05-11 13:35:50 +030070static inline void kvm_queue_interrupt(struct kvm_vcpu *vcpu, u8 vector,
71 bool soft)
Avi Kivity937a7ea2008-07-03 15:17:01 +030072{
Liran Alon04140b42018-03-23 03:01:31 +030073 vcpu->arch.interrupt.injected = true;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030074 vcpu->arch.interrupt.soft = soft;
Avi Kivity937a7ea2008-07-03 15:17:01 +030075 vcpu->arch.interrupt.nr = vector;
76}
77
78static inline void kvm_clear_interrupt_queue(struct kvm_vcpu *vcpu)
79{
Liran Alon04140b42018-03-23 03:01:31 +030080 vcpu->arch.interrupt.injected = false;
Avi Kivity937a7ea2008-07-03 15:17:01 +030081}
82
Gleb Natapov3298b752009-05-11 13:35:46 +030083static inline bool kvm_event_needs_reinjection(struct kvm_vcpu *vcpu)
84{
Liran Alon04140b42018-03-23 03:01:31 +030085 return vcpu->arch.exception.injected || vcpu->arch.interrupt.injected ||
Gleb Natapov3298b752009-05-11 13:35:46 +030086 vcpu->arch.nmi_injected;
87}
Gleb Natapov66fd3f72009-05-11 13:35:50 +030088
89static inline bool kvm_exception_is_soft(unsigned int nr)
90{
91 return (nr == BP_VECTOR) || (nr == OF_VECTOR);
92}
Gleb Natapovfc61b802009-07-05 17:39:35 +030093
Avi Kivity3eeb3282010-01-21 15:31:48 +020094static inline bool is_protmode(struct kvm_vcpu *vcpu)
95{
96 return kvm_read_cr0_bits(vcpu, X86_CR0_PE);
97}
98
Avi Kivity836a1b32010-01-21 15:31:49 +020099static inline int is_long_mode(struct kvm_vcpu *vcpu)
100{
101#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +0200102 return vcpu->arch.efer & EFER_LMA;
Avi Kivity836a1b32010-01-21 15:31:49 +0200103#else
104 return 0;
105#endif
106}
107
Nadav Amit57773922014-06-18 17:19:23 +0300108static inline bool is_64_bit_mode(struct kvm_vcpu *vcpu)
109{
110 int cs_db, cs_l;
111
Tom Lendackyb5aead02021-05-24 12:48:57 -0500112 WARN_ON_ONCE(vcpu->arch.guest_state_protected);
113
Nadav Amit57773922014-06-18 17:19:23 +0300114 if (!is_long_mode(vcpu))
115 return false;
Jason Baronb36464772021-01-14 22:27:56 -0500116 static_call(kvm_x86_get_cs_db_l_bits)(vcpu, &cs_db, &cs_l);
Nadav Amit57773922014-06-18 17:19:23 +0300117 return cs_l;
118}
119
Tom Lendackyb5aead02021-05-24 12:48:57 -0500120static inline bool is_64_bit_hypercall(struct kvm_vcpu *vcpu)
121{
122 /*
123 * If running with protected guest state, the CS register is not
124 * accessible. The hypercall register values will have had to been
125 * provided in 64-bit mode, so assume the guest is in 64-bit.
126 */
127 return vcpu->arch.guest_state_protected || is_64_bit_mode(vcpu);
128}
129
Marc Orr04473782018-06-20 17:21:29 -0700130static inline bool x86_exception_has_error_code(unsigned int vector)
131{
132 static u32 exception_has_error_code = BIT(DF_VECTOR) | BIT(TS_VECTOR) |
133 BIT(NP_VECTOR) | BIT(SS_VECTOR) | BIT(GP_VECTOR) |
134 BIT(PF_VECTOR) | BIT(AC_VECTOR);
135
136 return (1U << vector) & exception_has_error_code;
137}
138
Joerg Roedel6539e732010-09-10 17:30:50 +0200139static inline bool mmu_is_nested(struct kvm_vcpu *vcpu)
140{
141 return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu;
142}
143
Avi Kivity836a1b32010-01-21 15:31:49 +0200144static inline int is_pae(struct kvm_vcpu *vcpu)
145{
146 return kvm_read_cr4_bits(vcpu, X86_CR4_PAE);
147}
148
149static inline int is_pse(struct kvm_vcpu *vcpu)
150{
151 return kvm_read_cr4_bits(vcpu, X86_CR4_PSE);
152}
153
154static inline int is_paging(struct kvm_vcpu *vcpu)
155{
Davidlohr Buesoc36fc042012-03-08 12:45:54 +0100156 return likely(kvm_read_cr0_bits(vcpu, X86_CR0_PG));
Avi Kivity836a1b32010-01-21 15:31:49 +0200157}
158
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +0200159static inline bool is_pae_paging(struct kvm_vcpu *vcpu)
160{
161 return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu);
162}
163
Yu Zhangfd8cb432017-08-24 20:27:56 +0800164static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu)
165{
166 return kvm_read_cr4_bits(vcpu, X86_CR4_LA57) ? 57 : 48;
167}
168
Yu Zhangfd8cb432017-08-24 20:27:56 +0800169static inline u64 get_canonical(u64 la, u8 vaddr_bits)
170{
171 return ((int64_t)la << (64 - vaddr_bits)) >> (64 - vaddr_bits);
172}
173
174static inline bool is_noncanonical_address(u64 la, struct kvm_vcpu *vcpu)
175{
Yu Zhangfd8cb432017-08-24 20:27:56 +0800176 return get_canonical(la, vcpu_virt_addr_bits(vcpu)) != la;
Yu Zhangfd8cb432017-08-24 20:27:56 +0800177}
178
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800179static inline void vcpu_cache_mmio_info(struct kvm_vcpu *vcpu,
180 gva_t gva, gfn_t gfn, unsigned access)
181{
Sean Christophersonddfd1732019-02-05 13:01:13 -0800182 u64 gen = kvm_memslots(vcpu->kvm)->generation;
183
Sean Christopherson361209e2019-02-05 13:01:14 -0800184 if (unlikely(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS))
Sean Christophersonddfd1732019-02-05 13:01:13 -0800185 return;
186
Paolo Bonzini9034e6e2017-08-17 18:36:58 +0200187 /*
188 * If this is a shadow nested page table, the "GVA" is
189 * actually a nGPA.
190 */
191 vcpu->arch.mmio_gva = mmu_is_nested(vcpu) ? 0 : gva & PAGE_MASK;
Sean Christopherson871bd032019-08-01 13:35:21 -0700192 vcpu->arch.mmio_access = access;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800193 vcpu->arch.mmio_gfn = gfn;
Sean Christophersonddfd1732019-02-05 13:01:13 -0800194 vcpu->arch.mmio_gen = gen;
David Matlack56f17dd2014-08-18 15:46:07 -0700195}
196
197static inline bool vcpu_match_mmio_gen(struct kvm_vcpu *vcpu)
198{
199 return vcpu->arch.mmio_gen == kvm_memslots(vcpu->kvm)->generation;
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800200}
201
202/*
David Matlack56f17dd2014-08-18 15:46:07 -0700203 * Clear the mmio cache info for the given gva. If gva is MMIO_GVA_ANY, we
204 * clear all mmio cache info.
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800205 */
David Matlack56f17dd2014-08-18 15:46:07 -0700206#define MMIO_GVA_ANY (~(gva_t)0)
207
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800208static inline void vcpu_clear_mmio_info(struct kvm_vcpu *vcpu, gva_t gva)
209{
David Matlack56f17dd2014-08-18 15:46:07 -0700210 if (gva != MMIO_GVA_ANY && vcpu->arch.mmio_gva != (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800211 return;
212
213 vcpu->arch.mmio_gva = 0;
214}
215
216static inline bool vcpu_match_mmio_gva(struct kvm_vcpu *vcpu, unsigned long gva)
217{
David Matlack56f17dd2014-08-18 15:46:07 -0700218 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gva &&
219 vcpu->arch.mmio_gva == (gva & PAGE_MASK))
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800220 return true;
221
222 return false;
223}
224
225static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
226{
David Matlack56f17dd2014-08-18 15:46:07 -0700227 if (vcpu_match_mmio_gen(vcpu) && vcpu->arch.mmio_gfn &&
228 vcpu->arch.mmio_gfn == gpa >> PAGE_SHIFT)
Xiao Guangrongbebb1062011-07-12 03:23:20 +0800229 return true;
230
231 return false;
232}
233
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700234static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int reg)
Nadav Amit57773922014-06-18 17:19:23 +0300235{
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700236 unsigned long val = kvm_register_read_raw(vcpu, reg);
Nadav Amit57773922014-06-18 17:19:23 +0300237
238 return is_64_bit_mode(vcpu) ? val : (u32)val;
239}
240
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700241static inline void kvm_register_write(struct kvm_vcpu *vcpu,
Sean Christopherson489cbcf2019-09-27 14:45:20 -0700242 int reg, unsigned long val)
Nadav Amit27e6fb52014-06-18 17:19:26 +0300243{
244 if (!is_64_bit_mode(vcpu))
245 val = (u32)val;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -0700246 return kvm_register_write_raw(vcpu, reg, val);
Nadav Amit27e6fb52014-06-18 17:19:26 +0300247}
248
Paolo Bonzini41dbc6b2015-07-23 08:22:45 +0200249static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
250{
251 return !(kvm->arch.disabled_quirks & quirk);
252}
253
Liran Alon27cbe7d2019-11-11 11:16:40 +0200254static inline bool kvm_vcpu_latch_init(struct kvm_vcpu *vcpu)
255{
Jason Baronb36464772021-01-14 22:27:56 -0500256 return is_smm(vcpu) || static_call(kvm_x86_apic_init_signal_blocked)(vcpu);
Liran Alon27cbe7d2019-11-11 11:16:40 +0200257}
258
Sean Christopherson9497e1f2019-08-27 14:40:36 -0700259void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +0800260
Paolo Bonzini108b2492016-09-01 14:21:03 +0200261u64 get_kvmclock_ns(struct kvm *kvm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000262
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200263int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
Nadav Har'El064aea72011-05-25 23:04:56 +0300264 gva_t addr, void *val, unsigned int bytes,
265 struct x86_exception *exception);
266
Paolo Bonzinice14e868a2018-06-06 17:37:49 +0200267int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu,
Nadav Har'El6a4d7552011-05-25 23:08:00 +0300268 gva_t addr, void *val, unsigned int bytes,
269 struct x86_exception *exception);
270
Wanpeng Li082d06e2018-04-03 16:28:48 -0700271int handle_ud(struct kvm_vcpu *vcpu);
272
Jim Mattsonda998b42018-10-16 14:29:22 -0700273void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu);
274
Xiao Guangrong19efffa2015-06-15 16:55:31 +0800275void kvm_vcpu_mtrr_init(struct kvm_vcpu *vcpu);
Xiao Guangrongff536042015-06-15 16:55:22 +0800276u8 kvm_mtrr_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
Nadav Amit45666542014-09-18 22:39:44 +0300277bool kvm_mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data);
Xiao Guangrongff536042015-06-15 16:55:22 +0800278int kvm_mtrr_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
279int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
Xiao Guangrong6a39bbc2015-06-15 16:55:35 +0800280bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
281 int page_num);
Feng Wu520040142016-01-25 16:53:33 +0800282bool kvm_vector_hashing_enabled(void);
Mohammed Gamal89786142020-07-10 17:48:03 +0200283void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
Wei Huang4aa26912021-01-26 03:18:28 -0500284int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type,
285 void *insn, int insn_len);
Sean Christopherson736c2912019-12-06 15:57:14 -0800286int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
Sean Christophersonc60658d2018-08-23 13:56:53 -0700287 int emulation_type, void *insn, int insn_len);
Wanpeng Li404d5d72020-04-28 14:23:25 +0800288fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);
Nadav Amit45666542014-09-18 22:39:44 +0300289
Avi Kivity00b27a32011-11-23 16:30:32 +0200290extern u64 host_xcr0;
Sean Christophersoncfc48182020-03-02 15:56:23 -0800291extern u64 supported_xcr0;
Tom Lendacky86137772020-12-10 11:10:07 -0600292extern u64 host_xss;
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100293extern u64 supported_xss;
Like Xu4732f242022-01-11 15:38:23 +0800294extern bool enable_pmu;
Paolo Bonzini4ff41732014-02-24 12:15:16 +0100295
Sean Christopherson615a4ae2020-03-02 15:56:25 -0800296static inline bool kvm_mpx_supported(void)
297{
298 return (supported_xcr0 & (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR))
299 == (XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
300}
301
Marcelo Tosatti9ed96e82014-01-06 12:00:02 -0200302extern unsigned int min_timer_period_us;
303
Liran Alonc4ae60e2018-03-12 13:12:47 +0200304extern bool enable_vmware_backdoor;
305
Wanpeng Li0c5f81d2019-07-06 09:26:51 +0800306extern int pi_inject_timer;
307
Like Xud8550662021-01-08 09:36:55 +0800308extern bool report_ignored_msrs;
309
Marcelo Tosatti8d93c872016-06-20 22:28:02 -0300310static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
311{
312 return pvclock_scale_delta(nsec, vcpu->arch.virtual_tsc_mult,
313 vcpu->arch.virtual_tsc_shift);
314}
315
Paolo Bonzinib51012d2016-01-22 11:39:22 +0100316/* Same "calling convention" as do_div:
317 * - divide (n << 32) by base
318 * - put result in n
319 * - return remainder
320 */
321#define do_shl32_div32(n, base) \
322 ({ \
323 u32 __quot, __rem; \
324 asm("divl %2" : "=a" (__quot), "=d" (__rem) \
325 : "rm" (base), "0" (0), "1" ((u32) n)); \
326 n = __quot; \
327 __rem; \
328 })
329
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700330static inline bool kvm_mwait_in_guest(struct kvm *kvm)
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200331{
Wanpeng Li4d5422c2018-03-12 04:53:02 -0700332 return kvm->arch.mwait_in_guest;
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +0200333}
334
Wanpeng Licaa057a2018-03-12 04:53:03 -0700335static inline bool kvm_hlt_in_guest(struct kvm *kvm)
336{
337 return kvm->arch.hlt_in_guest;
338}
339
Wanpeng Lib31c1142018-03-12 04:53:04 -0700340static inline bool kvm_pause_in_guest(struct kvm *kvm)
341{
342 return kvm->arch.pause_in_guest;
343}
344
Wanpeng Lib5170062019-05-21 14:06:53 +0800345static inline bool kvm_cstate_in_guest(struct kvm *kvm)
346{
347 return kvm->arch.cstate_in_guest;
348}
349
Sean Christophersondb215752021-11-11 02:07:32 +0000350enum kvm_intr_type {
351 /* Values are arbitrary, but must be non-zero. */
352 KVM_HANDLING_IRQ = 1,
353 KVM_HANDLING_NMI,
354};
Andi Kleendd60d212017-07-25 17:20:32 -0700355
Sean Christophersondb215752021-11-11 02:07:32 +0000356static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu,
357 enum kvm_intr_type intr)
Andi Kleendd60d212017-07-25 17:20:32 -0700358{
Sean Christophersondb215752021-11-11 02:07:32 +0000359 WRITE_ONCE(vcpu->arch.handling_intr_from_guest, (u8)intr);
Andi Kleendd60d212017-07-25 17:20:32 -0700360}
361
362static inline void kvm_after_interrupt(struct kvm_vcpu *vcpu)
363{
Sean Christopherson73cd1072021-11-11 02:07:31 +0000364 WRITE_ONCE(vcpu->arch.handling_intr_from_guest, 0);
Andi Kleendd60d212017-07-25 17:20:32 -0700365}
366
Sean Christopherson73cd1072021-11-11 02:07:31 +0000367static inline bool kvm_handling_nmi_from_guest(struct kvm_vcpu *vcpu)
368{
Sean Christophersondb215752021-11-11 02:07:32 +0000369 return vcpu->arch.handling_intr_from_guest == KVM_HANDLING_NMI;
Sean Christopherson73cd1072021-11-11 02:07:31 +0000370}
Paolo Bonzini674ea352019-04-10 11:41:40 +0200371
372static inline bool kvm_pat_valid(u64 data)
373{
374 if (data & 0xF8F8F8F8F8F8F8F8ull)
375 return false;
376 /* 0, 1, 4, 5, 6, 7 are valid values. */
377 return (data | ((data & 0x0202020202020202ull) << 1)) == data;
378}
379
Sean Christopherson9b5e8532020-01-24 15:07:22 -0800380static inline bool kvm_dr7_valid(u64 data)
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500381{
382 /* Bits [63:32] are reserved */
383 return !(data >> 32);
384}
Krish Sadhukhanf5f61452020-05-22 18:19:51 -0400385static inline bool kvm_dr6_valid(u64 data)
386{
387 /* Bits [63:32] are reserved */
388 return !(data >> 32);
389}
Krish Sadhukhanb91991b2020-01-15 19:54:32 -0500390
Uros Bizjak3f1a18b2020-10-29 14:56:00 +0100391/*
392 * Trigger machine check on the host. We assume all the MSRs are already set up
393 * by the CPU and that we still run on the same CPU as the MCE occurred on.
394 * We pass a fake environment to the machine check handler because we want
395 * the guest to be always treated like user space, no matter what context
396 * it used internally.
397 */
398static inline void kvm_machine_check(void)
399{
400#if defined(CONFIG_X86_MCE)
401 struct pt_regs regs = {
402 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
403 .flags = X86_EFLAGS_IF,
404 };
405
406 do_machine_check(&regs);
407#endif
408}
409
Aaron Lewis139a12c2019-10-21 16:30:25 -0700410void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
411void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
Maxim Levitsky841c2be2020-07-08 14:57:31 +0300412int kvm_spec_ctrl_test_value(u64 value);
Sean Christophersonee69c922020-10-06 18:44:16 -0700413bool kvm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Babu Moger3f3393b2020-09-11 14:29:05 -0500414int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
415 struct x86_exception *e);
Babu Moger97150922020-09-11 14:29:12 -0500416int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva);
Alexander Graf51de8152020-09-25 16:34:17 +0200417bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type);
Paolo Bonzini674ea352019-04-10 11:41:40 +0200418
Maxim Levitskycc4cb012020-11-01 13:55:23 +0200419/*
420 * Internal error codes that are used to indicate that MSR emulation encountered
421 * an error that should result in #GP in the guest, unless userspace
422 * handles it.
423 */
424#define KVM_MSR_RET_INVALID 2 /* in-kernel MSR emulation #GP condition */
425#define KVM_MSR_RET_FILTERED 3 /* #GP due to userspace MSR filter */
Peter Xu6abe9c12020-06-22 18:04:41 -0400426
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000427#define __cr4_reserved_bits(__cpu_has, __c) \
428({ \
429 u64 __reserved_bits = CR4_RESERVED_BITS; \
430 \
431 if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \
432 __reserved_bits |= X86_CR4_OSXSAVE; \
433 if (!__cpu_has(__c, X86_FEATURE_SMEP)) \
434 __reserved_bits |= X86_CR4_SMEP; \
435 if (!__cpu_has(__c, X86_FEATURE_SMAP)) \
436 __reserved_bits |= X86_CR4_SMAP; \
437 if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \
438 __reserved_bits |= X86_CR4_FSGSBASE; \
439 if (!__cpu_has(__c, X86_FEATURE_PKU)) \
440 __reserved_bits |= X86_CR4_PKE; \
441 if (!__cpu_has(__c, X86_FEATURE_LA57)) \
442 __reserved_bits |= X86_CR4_LA57; \
443 if (!__cpu_has(__c, X86_FEATURE_UMIP)) \
444 __reserved_bits |= X86_CR4_UMIP; \
Paolo Bonzini53efe522020-07-08 07:02:50 -0400445 if (!__cpu_has(__c, X86_FEATURE_VMX)) \
446 __reserved_bits |= X86_CR4_VMXE; \
Vitaly Kuznetsov4683d752021-02-01 15:28:43 +0100447 if (!__cpu_has(__c, X86_FEATURE_PCID)) \
448 __reserved_bits |= X86_CR4_PCIDE; \
Krish Sadhukhanb899c132020-07-08 00:39:55 +0000449 __reserved_bits; \
450})
451
Tom Lendacky8f423a82020-12-10 11:09:53 -0600452int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
453 void *dst);
454int kvm_sev_es_mmio_read(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes,
455 void *dst);
Tom Lendacky7ed9abf2020-12-10 11:09:54 -0600456int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size,
457 unsigned int port, void *data, unsigned int count,
458 int in);
Tom Lendacky8f423a82020-12-10 11:09:53 -0600459
Avi Kivity26eef702008-07-03 14:59:22 +0300460#endif