1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel-based Virtual Machine driver for Linux
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Yaniv Kamay <yaniv@qumranet.com>
12 * Avi Kivity <avi@qumranet.com>
18 #include <linux/kvm_types.h>
19 #include <linux/kvm_host.h>
23 static const u32 host_save_user_msrs[] = {
25 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
28 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
32 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
34 #define MSRPM_OFFSETS 16
35 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
36 extern bool npt_enabled;
39 VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
41 VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
43 VMCB_INTR, /* int_ctl, int_vector */
44 VMCB_NPT, /* npt_en, nCR3, gPAT */
45 VMCB_CR, /* CR0, CR3, CR4, EFER */
46 VMCB_DR, /* DR6, DR7 */
47 VMCB_DT, /* GDT, IDT */
48 VMCB_SEG, /* CS, DS, SS, ES, CPL */
49 VMCB_CR2, /* CR2 only */
50 VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
51 VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
52 * AVIC PHYSICAL_TABLE pointer,
53 * AVIC LOGICAL_TABLE pointer
58 /* TPR and CR2 are always written before VMRUN */
59 #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
62 bool active; /* SEV enabled guest */
63 unsigned int asid; /* ASID used for this guest */
64 unsigned int handle; /* SEV firmware handle */
65 int fd; /* SEV device fd */
66 unsigned long pages_locked; /* Number of pages locked */
67 struct list_head regions_list; /* List of registered regions */
73 /* Struct members for AVIC */
75 struct page *avic_logical_id_table_page;
76 struct page *avic_physical_id_table_page;
77 struct hlist_node hnode;
79 struct kvm_sev_info sev_info;
90 /* These are the merged vectors */
93 /* gpa pointers to the real vectors */
97 /* A VMEXIT is required but not yet emulated */
100 /* cache for intercepts of the guest */
103 u32 intercept_exceptions;
106 /* Nested Paging related state */
111 struct kvm_vcpu vcpu;
113 unsigned long vmcb_pa;
114 struct svm_cpu_data *svm_data;
115 uint64_t asid_generation;
116 uint64_t sysenter_esp;
117 uint64_t sysenter_eip;
124 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
134 * Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be
135 * translated into the appropriate L2_CFG bits on the host to
136 * perform speculative control.
144 struct nested_state nested;
147 u64 nmi_singlestep_guest_rflags;
149 unsigned int3_injected;
150 unsigned long int3_rip;
152 /* cached guest cpuid flags for faster access */
153 bool nrips_enabled : 1;
157 struct page *avic_backing_page;
158 u64 *avic_physical_id_cache;
159 bool avic_is_running;
162 * Per-vcpu list of struct amd_svm_iommu_ir:
163 * This is used mainly to store interrupt remapping information used
164 * when update the vcpu affinity. This avoids the need to scan for
165 * IRTE and try to match ga_tag in the IOMMU driver.
167 struct list_head ir_list;
168 spinlock_t ir_list_lock;
170 /* which host CPU was used for running this vcpu */
171 unsigned int last_cpu;
174 void recalc_intercepts(struct vcpu_svm *svm);
176 static inline void mark_all_dirty(struct vmcb *vmcb)
178 vmcb->control.clean = 0;
181 static inline void mark_all_clean(struct vmcb *vmcb)
183 vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
184 & ~VMCB_ALWAYS_DIRTY_MASK;
187 static inline void mark_dirty(struct vmcb *vmcb, int bit)
189 vmcb->control.clean &= ~(1 << bit);
192 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
194 return container_of(vcpu, struct vcpu_svm, vcpu);
197 static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
199 if (is_guest_mode(&svm->vcpu))
200 return svm->nested.hsave;
205 static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
207 struct vmcb *vmcb = get_host_vmcb(svm);
209 vmcb->control.intercept_cr |= (1U << bit);
211 recalc_intercepts(svm);
214 static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
216 struct vmcb *vmcb = get_host_vmcb(svm);
218 vmcb->control.intercept_cr &= ~(1U << bit);
220 recalc_intercepts(svm);
223 static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
225 struct vmcb *vmcb = get_host_vmcb(svm);
227 return vmcb->control.intercept_cr & (1U << bit);
230 static inline void set_dr_intercepts(struct vcpu_svm *svm)
232 struct vmcb *vmcb = get_host_vmcb(svm);
234 vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
235 | (1 << INTERCEPT_DR1_READ)
236 | (1 << INTERCEPT_DR2_READ)
237 | (1 << INTERCEPT_DR3_READ)
238 | (1 << INTERCEPT_DR4_READ)
239 | (1 << INTERCEPT_DR5_READ)
240 | (1 << INTERCEPT_DR6_READ)
241 | (1 << INTERCEPT_DR7_READ)
242 | (1 << INTERCEPT_DR0_WRITE)
243 | (1 << INTERCEPT_DR1_WRITE)
244 | (1 << INTERCEPT_DR2_WRITE)
245 | (1 << INTERCEPT_DR3_WRITE)
246 | (1 << INTERCEPT_DR4_WRITE)
247 | (1 << INTERCEPT_DR5_WRITE)
248 | (1 << INTERCEPT_DR6_WRITE)
249 | (1 << INTERCEPT_DR7_WRITE);
251 recalc_intercepts(svm);
254 static inline void clr_dr_intercepts(struct vcpu_svm *svm)
256 struct vmcb *vmcb = get_host_vmcb(svm);
258 vmcb->control.intercept_dr = 0;
260 recalc_intercepts(svm);
263 static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
265 struct vmcb *vmcb = get_host_vmcb(svm);
267 vmcb->control.intercept_exceptions |= (1U << bit);
269 recalc_intercepts(svm);
272 static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
274 struct vmcb *vmcb = get_host_vmcb(svm);
276 vmcb->control.intercept_exceptions &= ~(1U << bit);
278 recalc_intercepts(svm);
281 static inline void set_intercept(struct vcpu_svm *svm, int bit)
283 struct vmcb *vmcb = get_host_vmcb(svm);
285 vmcb->control.intercept |= (1ULL << bit);
287 recalc_intercepts(svm);
290 static inline void clr_intercept(struct vcpu_svm *svm, int bit)
292 struct vmcb *vmcb = get_host_vmcb(svm);
294 vmcb->control.intercept &= ~(1ULL << bit);
296 recalc_intercepts(svm);
299 static inline bool is_intercept(struct vcpu_svm *svm, int bit)
301 return (svm->vmcb->control.intercept & (1ULL << bit)) != 0;
304 static inline bool vgif_enabled(struct vcpu_svm *svm)
306 return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
309 static inline void enable_gif(struct vcpu_svm *svm)
311 if (vgif_enabled(svm))
312 svm->vmcb->control.int_ctl |= V_GIF_MASK;
314 svm->vcpu.arch.hflags |= HF_GIF_MASK;
317 static inline void disable_gif(struct vcpu_svm *svm)
319 if (vgif_enabled(svm))
320 svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
322 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
325 static inline bool gif_set(struct vcpu_svm *svm)
327 if (vgif_enabled(svm))
328 return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
330 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
334 #define MSR_INVALID 0xffffffffU
336 u32 svm_msrpm_offset(u32 msr);
337 void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer);
338 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
339 int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
340 void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa);
341 void disable_nmi_singlestep(struct vcpu_svm *svm);
345 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
346 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
347 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
349 /* This function returns true if it is save to enable the nmi window */
350 static inline bool nested_svm_nmi(struct vcpu_svm *svm)
352 if (!is_guest_mode(&svm->vcpu))
355 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
358 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
359 svm->nested.exit_required = true;
364 static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
366 return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
369 void enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
370 struct vmcb *nested_vmcb, struct kvm_host_map *map);
371 int nested_svm_vmrun(struct vcpu_svm *svm);
372 void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb);
373 int nested_svm_vmexit(struct vcpu_svm *svm);
374 int nested_svm_exit_handled(struct vcpu_svm *svm);
375 int nested_svm_check_permissions(struct vcpu_svm *svm);
376 int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
377 bool has_error_code, u32 error_code);
378 int svm_check_nested_events(struct kvm_vcpu *vcpu);
379 int nested_svm_exit_special(struct vcpu_svm *svm);