2020-03-24 10:41:52 +01:00
// SPDX-License-Identifier: GPL-2.0-only
/*
* Kernel - based Virtual Machine driver for Linux
*
* AMD SVM support
*
* Copyright ( C ) 2006 Qumranet , Inc .
* Copyright 2010 Red Hat , Inc . and / or its affiliates .
*
* Authors :
* Yaniv Kamay < yaniv @ qumranet . com >
* Avi Kivity < avi @ qumranet . com >
*/
# ifndef __SVM_SVM_H
# define __SVM_SVM_H
# include <linux/kvm_types.h>
# include <linux/kvm_host.h>
2020-12-10 11:09:47 -06:00
# include <linux/bits.h>
2020-03-24 10:41:52 +01:00
# include <asm/svm.h>
2021-04-27 06:16:35 -05:00
# include <asm/sev-common.h>
2020-03-24 10:41:52 +01:00
2020-12-10 11:10:04 -06:00
# define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
2021-04-12 17:56:05 -04:00
# define IOPM_SIZE PAGE_SIZE * 3
# define MSRPM_SIZE PAGE_SIZE * 2
2021-04-01 14:19:28 +03:00
# define MAX_DIRECT_ACCESS_MSRS 20
2020-03-24 10:41:52 +01:00
# define MSRPM_OFFSETS 16
extern u32 msrpm_offsets [ MSRPM_OFFSETS ] __read_mostly ;
extern bool npt_enabled ;
2021-07-07 15:51:00 +03:00
extern bool intercept_smi ;
2020-03-24 10:41:52 +01:00
2021-06-03 15:14:37 +00:00
/*
* Clean bits in VMCB .
* VMCB_ALL_CLEAN_MASK might also need to
* be updated if this enum is modified .
*/
2020-03-24 10:41:52 +01:00
enum {
VMCB_INTERCEPTS , /* Intercept vectors, TSC offset,
pause filter count */
VMCB_PERM_MAP , /* IOPM Base and MSRPM Base */
VMCB_ASID , /* ASID */
VMCB_INTR , /* int_ctl, int_vector */
VMCB_NPT , /* npt_en, nCR3, gPAT */
VMCB_CR , /* CR0, CR3, CR4, EFER */
VMCB_DR , /* DR6, DR7 */
VMCB_DT , /* GDT, IDT */
VMCB_SEG , /* CS, DS, SS, ES, CPL */
VMCB_CR2 , /* CR2 only */
VMCB_LBR , /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
VMCB_AVIC , /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
* AVIC PHYSICAL_TABLE pointer ,
* AVIC LOGICAL_TABLE pointer
*/
2021-06-03 15:14:37 +00:00
VMCB_SW = 31 , /* Reserved for hypervisor/software use */
2020-03-24 10:41:52 +01:00
} ;
2021-06-03 15:14:37 +00:00
# define VMCB_ALL_CLEAN_MASK ( \
( 1U < < VMCB_INTERCEPTS ) | ( 1U < < VMCB_PERM_MAP ) | \
( 1U < < VMCB_ASID ) | ( 1U < < VMCB_INTR ) | \
( 1U < < VMCB_NPT ) | ( 1U < < VMCB_CR ) | ( 1U < < VMCB_DR ) | \
( 1U < < VMCB_DT ) | ( 1U < < VMCB_SEG ) | ( 1U < < VMCB_CR2 ) | \
( 1U < < VMCB_LBR ) | ( 1U < < VMCB_AVIC ) | \
( 1U < < VMCB_SW ) )
2020-03-24 10:41:52 +01:00
/* TPR and CR2 are always written before VMRUN */
# define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
struct kvm_sev_info {
bool active ; /* SEV enabled guest */
2020-12-10 11:09:38 -06:00
bool es_active ; /* SEV-ES enabled guest */
2020-03-24 10:41:52 +01:00
unsigned int asid ; /* ASID used for this guest */
unsigned int handle ; /* SEV firmware handle */
int fd ; /* SEV device fd */
unsigned long pages_locked ; /* Number of pages locked */
struct list_head regions_list ; /* List of registered regions */
2020-12-15 12:44:07 -05:00
u64 ap_jump_table ; /* SEV-ES AP Jump Table address */
2021-04-08 22:32:14 +00:00
struct kvm * enc_context_owner ; /* Owner of copied encryption context */
2021-11-22 19:50:34 -05:00
unsigned long num_mirrored_vms ; /* Number of VMs sharing this ASID */
2021-03-29 21:42:06 -07:00
struct misc_cg * misc_cg ; /* For misc cgroup accounting */
2021-10-21 10:43:00 -07:00
atomic_t migration_in_progress ;
2020-03-24 10:41:52 +01:00
} ;
struct kvm_svm {
struct kvm kvm ;
/* Struct members for AVIC */
u32 avic_vm_id ;
struct page * avic_logical_id_table_page ;
struct page * avic_physical_id_table_page ;
struct hlist_node hnode ;
struct kvm_sev_info sev_info ;
} ;
struct kvm_vcpu ;
2021-01-13 07:07:52 -05:00
struct kvm_vmcb_info {
struct vmcb * ptr ;
unsigned long pa ;
2021-01-12 11:43:12 -05:00
int cpu ;
2021-01-12 11:43:13 -05:00
uint64_t asid_generation ;
2021-01-13 07:07:52 -05:00
} ;
2021-11-03 10:05:22 -04:00
struct vmcb_save_area_cached {
u64 efer ;
u64 cr4 ;
u64 cr3 ;
u64 cr0 ;
u64 dr7 ;
u64 dr6 ;
} ;
2021-11-03 10:05:26 -04:00
struct vmcb_ctrl_area_cached {
u32 intercepts [ MAX_INTERCEPT ] ;
u16 pause_filter_thresh ;
u16 pause_filter_count ;
u64 iopm_base_pa ;
u64 msrpm_base_pa ;
u64 tsc_offset ;
u32 asid ;
u8 tlb_ctl ;
u32 int_ctl ;
u32 int_vector ;
u32 int_state ;
u32 exit_code ;
u32 exit_code_hi ;
u64 exit_info_1 ;
u64 exit_info_2 ;
u32 exit_int_info ;
u32 exit_int_info_err ;
u64 nested_ctl ;
u32 event_inj ;
u32 event_inj_err ;
u64 nested_cr3 ;
u64 virt_ext ;
} ;
2020-06-25 10:03:22 +02:00
struct svm_nested_state {
2021-01-13 07:07:52 -05:00
struct kvm_vmcb_info vmcb02 ;
2020-03-24 10:41:52 +01:00
u64 hsave_msr ;
u64 vm_cr_msr ;
2020-08-27 20:11:39 +03:00
u64 vmcb12_gpa ;
2021-03-01 15:08:44 -05:00
u64 last_vmcb12_gpa ;
2020-03-24 10:41:52 +01:00
/* These are the merged vectors */
u32 * msrpm ;
2020-04-23 13:22:27 -04:00
/* A VMRUN has started but has not yet been performed, so
* we cannot inject a nested vmexit yet . */
bool nested_run_pending ;
2020-05-13 13:16:12 -04:00
/* cache for control fields of the guest */
2021-11-03 10:05:26 -04:00
struct vmcb_ctrl_area_cached ctl ;
2020-10-01 14:29:54 +03:00
2021-11-03 10:05:22 -04:00
/*
* Note : this struct is not kept up - to - date while L2 runs ; it is only
* valid within nested_svm_vmrun .
*/
struct vmcb_save_area_cached save ;
2020-10-01 14:29:54 +03:00
bool initialized ;
2020-03-24 10:41:52 +01:00
} ;
2021-10-21 10:42:59 -07:00
struct vcpu_sev_es_state {
/* SEV-ES support */
struct vmcb_save_area * vmsa ;
struct ghcb * ghcb ;
struct kvm_host_map ghcb_map ;
bool received_first_sipi ;
/* SEV-ES scratch area support */
void * ghcb_sa ;
2021-11-11 10:52:26 -05:00
u32 ghcb_sa_len ;
2021-10-21 10:42:59 -07:00
bool ghcb_sa_sync ;
bool ghcb_sa_free ;
} ;
2020-03-24 10:41:52 +01:00
struct vcpu_svm {
struct kvm_vcpu vcpu ;
2021-04-06 10:18:10 -07:00
/* vmcb always points at current_vmcb->ptr, it's purely a shorthand. */
2020-03-24 10:41:52 +01:00
struct vmcb * vmcb ;
2021-01-13 07:07:52 -05:00
struct kvm_vmcb_info vmcb01 ;
struct kvm_vmcb_info * current_vmcb ;
2020-03-24 10:41:52 +01:00
struct svm_cpu_data * svm_data ;
2020-10-11 14:48:17 -04:00
u32 asid ;
2021-04-01 14:19:28 +03:00
u32 sysenter_esp_hi ;
u32 sysenter_eip_hi ;
2020-03-24 10:41:52 +01:00
uint64_t tsc_aux ;
u64 msr_decfg ;
u64 next_rip ;
u64 spec_ctrl ;
2021-09-14 18:48:24 +03:00
u64 tsc_ratio_msr ;
2020-03-24 10:41:52 +01:00
/*
* Contains guest - controlled bits of VIRT_SPEC_CTRL , which will be
* translated into the appropriate L2_CFG bits on the host to
* perform speculative control .
*/
u64 virt_spec_ctrl ;
u32 * msrpm ;
ulong nmi_iret_rip ;
2020-06-25 10:03:22 +02:00
struct svm_nested_state nested ;
2020-03-24 10:41:52 +01:00
bool nmi_singlestep ;
u64 nmi_singlestep_guest_rflags ;
unsigned int3_injected ;
unsigned long int3_rip ;
/* cached guest cpuid flags for faster access */
2021-09-14 18:48:24 +03:00
bool nrips_enabled : 1 ;
bool tsc_scaling_enabled : 1 ;
2020-03-24 10:41:52 +01:00
u32 ldr_reg ;
u32 dfr_reg ;
struct page * avic_backing_page ;
u64 * avic_physical_id_cache ;
/*
* Per - vcpu list of struct amd_svm_iommu_ir :
* This is used mainly to store interrupt remapping information used
* when update the vcpu affinity . This avoids the need to scan for
* IRTE and try to match ga_tag in the IOMMU driver .
*/
struct list_head ir_list ;
spinlock_t ir_list_lock ;
2020-09-25 16:34:19 +02:00
/* Save desired MSR intercept (read: pass-through) state */
struct {
DECLARE_BITMAP ( read , MAX_DIRECT_ACCESS_MSRS ) ;
DECLARE_BITMAP ( write , MAX_DIRECT_ACCESS_MSRS ) ;
} shadow_msr_intercept ;
2020-12-10 11:09:40 -06:00
2021-10-21 10:42:59 -07:00
struct vcpu_sev_es_state sev_es ;
2021-02-02 13:01:26 -06:00
bool guest_state_loaded ;
2020-03-24 10:41:52 +01:00
} ;
2020-03-24 10:41:54 +01:00
struct svm_cpu_data {
int cpu ;
u64 asid_generation ;
u32 max_asid ;
u32 next_asid ;
u32 min_asid ;
struct kvm_ldttss_desc * tss_desc ;
struct page * save_area ;
struct vmcb * current_vmcb ;
/* index = sev_asid, value = vmcb pointer */
struct vmcb * * sev_vmcbs ;
} ;
DECLARE_PER_CPU ( struct svm_cpu_data * , svm_data ) ;
2020-03-24 10:41:52 +01:00
void recalc_intercepts ( struct vcpu_svm * svm ) ;
2021-06-24 11:41:03 +02:00
static __always_inline struct kvm_svm * to_kvm_svm ( struct kvm * kvm )
2020-03-31 12:17:38 -04:00
{
return container_of ( kvm , struct kvm_svm , kvm ) ;
}
2021-06-24 11:41:03 +02:00
static __always_inline bool sev_guest ( struct kvm * kvm )
2020-12-10 11:09:38 -06:00
{
# ifdef CONFIG_KVM_AMD_SEV
struct kvm_sev_info * sev = & to_kvm_svm ( kvm ) - > sev_info ;
return sev - > active ;
# else
return false ;
# endif
}
2021-06-24 11:41:03 +02:00
static __always_inline bool sev_es_guest ( struct kvm * kvm )
2020-12-10 11:09:38 -06:00
{
# ifdef CONFIG_KVM_AMD_SEV
struct kvm_sev_info * sev = & to_kvm_svm ( kvm ) - > sev_info ;
2021-11-09 21:50:59 +00:00
return sev - > es_active & & ! WARN_ON_ONCE ( ! sev - > active ) ;
2020-12-10 11:09:38 -06:00
# else
return false ;
# endif
}
2020-06-25 10:03:23 +02:00
static inline void vmcb_mark_all_dirty ( struct vmcb * vmcb )
2020-03-24 10:41:52 +01:00
{
vmcb - > control . clean = 0 ;
}
2020-06-25 10:03:23 +02:00
static inline void vmcb_mark_all_clean ( struct vmcb * vmcb )
2020-03-24 10:41:52 +01:00
{
2021-06-03 15:14:37 +00:00
vmcb - > control . clean = VMCB_ALL_CLEAN_MASK
2020-03-24 10:41:52 +01:00
& ~ VMCB_ALWAYS_DIRTY_MASK ;
}
2020-06-25 10:03:23 +02:00
static inline void vmcb_mark_dirty ( struct vmcb * vmcb , int bit )
2020-03-24 10:41:52 +01:00
{
vmcb - > control . clean & = ~ ( 1 < < bit ) ;
}
2021-03-01 15:08:44 -05:00
static inline bool vmcb_is_dirty ( struct vmcb * vmcb , int bit )
{
return ! test_bit ( bit , ( unsigned long * ) & vmcb - > control . clean ) ;
}
2021-06-24 11:41:06 +02:00
static __always_inline struct vcpu_svm * to_svm ( struct kvm_vcpu * vcpu )
2020-03-24 10:41:52 +01:00
{
return container_of ( vcpu , struct vcpu_svm , vcpu ) ;
}
2021-11-26 07:00:15 -05:00
/*
* Only the PDPTRs are loaded on demand into the shadow MMU . All other
* fields are synchronized in handle_exit , because accessing the VMCB is cheap .
*
* CR3 might be out of date in the VMCB but it is not marked dirty ; instead ,
* KVM_REQ_LOAD_MMU_PGD is always requested when the cached vcpu - > arch . cr3
* is changed . svm_load_mmu_pgd ( ) then syncs the new CR3 value into the VMCB .
*/
# define SVM_REGS_LAZY_LOAD_SET (1 << VCPU_EXREG_PDPTR)
2020-09-11 14:27:58 -05:00
static inline void vmcb_set_intercept ( struct vmcb_control_area * control , u32 bit )
{
WARN_ON_ONCE ( bit > = 32 * MAX_INTERCEPT ) ;
__set_bit ( bit , ( unsigned long * ) & control - > intercepts ) ;
}
static inline void vmcb_clr_intercept ( struct vmcb_control_area * control , u32 bit )
{
WARN_ON_ONCE ( bit > = 32 * MAX_INTERCEPT ) ;
__clear_bit ( bit , ( unsigned long * ) & control - > intercepts ) ;
}
static inline bool vmcb_is_intercept ( struct vmcb_control_area * control , u32 bit )
{
WARN_ON_ONCE ( bit > = 32 * MAX_INTERCEPT ) ;
return test_bit ( bit , ( unsigned long * ) & control - > intercepts ) ;
}
2021-11-03 10:05:26 -04:00
static inline bool vmcb12_is_intercept ( struct vmcb_ctrl_area_cached * control , u32 bit )
{
WARN_ON_ONCE ( bit > = 32 * MAX_INTERCEPT ) ;
return test_bit ( bit , ( unsigned long * ) & control - > intercepts ) ;
}
2020-03-24 10:41:52 +01:00
static inline void set_dr_intercepts ( struct vcpu_svm * svm )
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-12-10 11:09:43 -06:00
if ( ! sev_es_guest ( svm - > vcpu . kvm ) ) {
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR0_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR1_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR2_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR3_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR4_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR5_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR6_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR0_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR1_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR2_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR3_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR4_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR5_WRITE ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR6_WRITE ) ;
}
2020-09-11 14:28:12 -05:00
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR7_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR7_WRITE ) ;
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
static inline void clr_dr_intercepts ( struct vcpu_svm * svm )
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-09-11 14:28:12 -05:00
vmcb - > control . intercepts [ INTERCEPT_DR ] = 0 ;
2020-03-24 10:41:52 +01:00
2020-12-10 11:09:43 -06:00
/* DR7 access must remain intercepted for an SEV-ES guest */
if ( sev_es_guest ( svm - > vcpu . kvm ) ) {
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR7_READ ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_DR7_WRITE ) ;
}
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
2020-09-11 14:28:20 -05:00
static inline void set_exception_intercept ( struct vcpu_svm * svm , u32 bit )
2020-03-24 10:41:52 +01:00
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-09-11 14:28:20 -05:00
WARN_ON_ONCE ( bit > = 32 ) ;
vmcb_set_intercept ( & vmcb - > control , INTERCEPT_EXCEPTION_OFFSET + bit ) ;
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
2020-09-11 14:28:20 -05:00
static inline void clr_exception_intercept ( struct vcpu_svm * svm , u32 bit )
2020-03-24 10:41:52 +01:00
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-09-11 14:28:20 -05:00
WARN_ON_ONCE ( bit > = 32 ) ;
vmcb_clr_intercept ( & vmcb - > control , INTERCEPT_EXCEPTION_OFFSET + bit ) ;
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
2020-06-25 10:03:24 +02:00
static inline void svm_set_intercept ( struct vcpu_svm * svm , int bit )
2020-03-24 10:41:52 +01:00
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-09-11 14:28:28 -05:00
vmcb_set_intercept ( & vmcb - > control , bit ) ;
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
2020-06-25 10:03:24 +02:00
static inline void svm_clr_intercept ( struct vcpu_svm * svm , int bit )
2020-03-24 10:41:52 +01:00
{
2021-01-13 07:07:52 -05:00
struct vmcb * vmcb = svm - > vmcb01 . ptr ;
2020-03-24 10:41:52 +01:00
2020-09-11 14:28:28 -05:00
vmcb_clr_intercept ( & vmcb - > control , bit ) ;
2020-03-24 10:41:52 +01:00
recalc_intercepts ( svm ) ;
}
2020-06-25 10:03:24 +02:00
static inline bool svm_is_intercept ( struct vcpu_svm * svm , int bit )
2020-03-24 10:41:52 +01:00
{
2020-09-11 14:28:28 -05:00
return vmcb_is_intercept ( & svm - > vmcb - > control , bit ) ;
2020-03-24 10:41:52 +01:00
}
static inline bool vgif_enabled ( struct vcpu_svm * svm )
{
return ! ! ( svm - > vmcb - > control . int_ctl & V_GIF_ENABLE_MASK ) ;
}
static inline void enable_gif ( struct vcpu_svm * svm )
{
if ( vgif_enabled ( svm ) )
svm - > vmcb - > control . int_ctl | = V_GIF_MASK ;
else
svm - > vcpu . arch . hflags | = HF_GIF_MASK ;
}
static inline void disable_gif ( struct vcpu_svm * svm )
{
if ( vgif_enabled ( svm ) )
svm - > vmcb - > control . int_ctl & = ~ V_GIF_MASK ;
else
svm - > vcpu . arch . hflags & = ~ HF_GIF_MASK ;
}
static inline bool gif_set ( struct vcpu_svm * svm )
{
if ( vgif_enabled ( svm ) )
return ! ! ( svm - > vmcb - > control . int_ctl & V_GIF_MASK ) ;
else
return ! ! ( svm - > vcpu . arch . hflags & HF_GIF_MASK ) ;
}
/* svm.c */
2020-07-08 00:39:56 +00:00
# define MSR_INVALID 0xffffffffU
2020-03-24 10:41:52 +01:00
2020-12-10 11:09:47 -06:00
extern bool dump_invalid_vmcb ;
2020-12-10 11:09:38 -06:00
2020-03-24 10:41:52 +01:00
u32 svm_msrpm_offset ( u32 msr ) ;
2020-10-01 14:29:54 +03:00
u32 * svm_vcpu_alloc_msrpm ( void ) ;
void svm_vcpu_init_msrpm ( struct kvm_vcpu * vcpu , u32 * msrpm ) ;
void svm_vcpu_free_msrpm ( u32 * msrpm ) ;
2020-10-01 14:29:53 +03:00
int svm_set_efer ( struct kvm_vcpu * vcpu , u64 efer ) ;
2020-03-24 10:41:52 +01:00
void svm_set_cr0 ( struct kvm_vcpu * vcpu , unsigned long cr0 ) ;
2020-10-06 18:44:15 -07:00
void svm_set_cr4 ( struct kvm_vcpu * vcpu , unsigned long cr4 ) ;
2020-03-24 10:41:52 +01:00
void disable_nmi_singlestep ( struct vcpu_svm * svm ) ;
2020-04-23 14:19:26 -04:00
bool svm_smi_blocked ( struct kvm_vcpu * vcpu ) ;
bool svm_nmi_blocked ( struct kvm_vcpu * vcpu ) ;
bool svm_interrupt_blocked ( struct kvm_vcpu * vcpu ) ;
2020-05-22 12:18:27 -04:00
void svm_set_gif ( struct vcpu_svm * svm , bool value ) ;
2021-03-02 14:40:39 -05:00
int svm_invoke_exit_handler ( struct kvm_vcpu * vcpu , u64 exit_code ) ;
2020-12-10 11:10:06 -06:00
void set_msr_interception ( struct kvm_vcpu * vcpu , u32 * msrpm , u32 msr ,
int read , int write ) ;
2020-03-24 10:41:52 +01:00
/* nested.c */
# define NESTED_EXIT_HOST 0 /* Exit handled on host level */
# define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
# define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
2020-06-25 10:03:25 +02:00
static inline bool nested_svm_virtualize_tpr ( struct kvm_vcpu * vcpu )
2020-03-24 10:41:52 +01:00
{
2020-05-13 13:28:23 -04:00
struct vcpu_svm * svm = to_svm ( vcpu ) ;
return is_guest_mode ( vcpu ) & & ( svm - > nested . ctl . int_ctl & V_INTR_MASKING_MASK ) ;
2020-03-24 10:41:52 +01:00
}
2020-04-23 08:17:28 -04:00
static inline bool nested_exit_on_smi ( struct vcpu_svm * svm )
{
2021-11-03 10:05:26 -04:00
return vmcb12_is_intercept ( & svm - > nested . ctl , INTERCEPT_SMI ) ;
2020-04-23 08:17:28 -04:00
}
2020-04-23 18:02:45 -04:00
static inline bool nested_exit_on_intr ( struct vcpu_svm * svm )
{
2021-11-03 10:05:26 -04:00
return vmcb12_is_intercept ( & svm - > nested . ctl , INTERCEPT_INTR ) ;
2020-04-23 18:02:45 -04:00
}
2020-04-23 08:06:43 -04:00
static inline bool nested_exit_on_nmi ( struct vcpu_svm * svm )
{
2021-11-03 10:05:26 -04:00
return vmcb12_is_intercept ( & svm - > nested . ctl , INTERCEPT_NMI ) ;
2020-04-23 08:06:43 -04:00
}
2021-09-13 17:09:51 +03:00
int enter_svm_guest_mode ( struct kvm_vcpu * vcpu ,
u64 vmcb_gpa , struct vmcb * vmcb12 , bool from_vmrun ) ;
KVM: x86: Forcibly leave nested virt when SMM state is toggled
Forcibly leave nested virtualization operation if userspace toggles SMM
state via KVM_SET_VCPU_EVENTS or KVM_SYNC_X86_EVENTS. If userspace
forces the vCPU out of SMM while it's post-VMXON and then injects an SMI,
vmx_enter_smm() will overwrite vmx->nested.smm.vmxon and end up with both
vmxon=false and smm.vmxon=false, but all other nVMX state allocated.
Don't attempt to gracefully handle the transition as (a) most transitions
are nonsencial, e.g. forcing SMM while L2 is running, (b) there isn't
sufficient information to handle all transitions, e.g. SVM wants access
to the SMRAM save state, and (c) KVM_SET_VCPU_EVENTS must precede
KVM_SET_NESTED_STATE during state restore as the latter disallows putting
the vCPU into L2 if SMM is active, and disallows tagging the vCPU as
being post-VMXON in SMM if SMM is not active.
Abuse of KVM_SET_VCPU_EVENTS manifests as a WARN and memory leak in nVMX
due to failure to free vmcs01's shadow VMCS, but the bug goes far beyond
just a memory leak, e.g. toggling SMM on while L2 is active puts the vCPU
in an architecturally impossible state.
WARNING: CPU: 0 PID: 3606 at free_loaded_vmcs arch/x86/kvm/vmx/vmx.c:2665 [inline]
WARNING: CPU: 0 PID: 3606 at free_loaded_vmcs+0x158/0x1a0 arch/x86/kvm/vmx/vmx.c:2656
Modules linked in:
CPU: 1 PID: 3606 Comm: syz-executor725 Not tainted 5.17.0-rc1-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
RIP: 0010:free_loaded_vmcs arch/x86/kvm/vmx/vmx.c:2665 [inline]
RIP: 0010:free_loaded_vmcs+0x158/0x1a0 arch/x86/kvm/vmx/vmx.c:2656
Code: <0f> 0b eb b3 e8 8f 4d 9f 00 e9 f7 fe ff ff 48 89 df e8 92 4d 9f 00
Call Trace:
<TASK>
kvm_arch_vcpu_destroy+0x72/0x2f0 arch/x86/kvm/x86.c:11123
kvm_vcpu_destroy arch/x86/kvm/../../../virt/kvm/kvm_main.c:441 [inline]
kvm_destroy_vcpus+0x11f/0x290 arch/x86/kvm/../../../virt/kvm/kvm_main.c:460
kvm_free_vcpus arch/x86/kvm/x86.c:11564 [inline]
kvm_arch_destroy_vm+0x2e8/0x470 arch/x86/kvm/x86.c:11676
kvm_destroy_vm arch/x86/kvm/../../../virt/kvm/kvm_main.c:1217 [inline]
kvm_put_kvm+0x4fa/0xb00 arch/x86/kvm/../../../virt/kvm/kvm_main.c:1250
kvm_vm_release+0x3f/0x50 arch/x86/kvm/../../../virt/kvm/kvm_main.c:1273
__fput+0x286/0x9f0 fs/file_table.c:311
task_work_run+0xdd/0x1a0 kernel/task_work.c:164
exit_task_work include/linux/task_work.h:32 [inline]
do_exit+0xb29/0x2a30 kernel/exit.c:806
do_group_exit+0xd2/0x2f0 kernel/exit.c:935
get_signal+0x4b0/0x28c0 kernel/signal.c:2862
arch_do_signal_or_restart+0x2a9/0x1c40 arch/x86/kernel/signal.c:868
handle_signal_work kernel/entry/common.c:148 [inline]
exit_to_user_mode_loop kernel/entry/common.c:172 [inline]
exit_to_user_mode_prepare+0x17d/0x290 kernel/entry/common.c:207
__syscall_exit_to_user_mode_work kernel/entry/common.c:289 [inline]
syscall_exit_to_user_mode+0x19/0x60 kernel/entry/common.c:300
do_syscall_64+0x42/0xb0 arch/x86/entry/common.c:86
entry_SYSCALL_64_after_hwframe+0x44/0xae
</TASK>
Cc: stable@vger.kernel.org
Reported-by: syzbot+8112db3ab20e70d50c31@syzkaller.appspotmail.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20220125220358.2091737-1-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-01-25 22:03:58 +00:00
void svm_leave_nested ( struct kvm_vcpu * vcpu ) ;
2020-10-01 14:29:54 +03:00
void svm_free_nested ( struct vcpu_svm * svm ) ;
int svm_allocate_nested ( struct vcpu_svm * svm ) ;
2021-03-02 14:40:39 -05:00
int nested_svm_vmrun ( struct kvm_vcpu * vcpu ) ;
2021-07-19 11:03:22 +02:00
void svm_copy_vmrun_state ( struct vmcb_save_area * to_save ,
struct vmcb_save_area * from_save ) ;
void svm_copy_vmloadsave_state ( struct vmcb * to_vmcb , struct vmcb * from_vmcb ) ;
2020-03-24 10:41:52 +01:00
int nested_svm_vmexit ( struct vcpu_svm * svm ) ;
2021-03-02 09:45:15 -08:00
static inline int nested_svm_simple_vmexit ( struct vcpu_svm * svm , u32 exit_code )
{
svm - > vmcb - > control . exit_code = exit_code ;
svm - > vmcb - > control . exit_info_1 = 0 ;
svm - > vmcb - > control . exit_info_2 = 0 ;
return nested_svm_vmexit ( svm ) ;
}
2020-03-24 10:41:52 +01:00
int nested_svm_exit_handled ( struct vcpu_svm * svm ) ;
2021-03-02 14:40:39 -05:00
int nested_svm_check_permissions ( struct kvm_vcpu * vcpu ) ;
2020-03-24 10:41:52 +01:00
int nested_svm_check_exception ( struct vcpu_svm * svm , unsigned nr ,
bool has_error_code , u32 error_code ) ;
int nested_svm_exit_special ( struct vcpu_svm * svm ) ;
2021-09-14 18:48:24 +03:00
void nested_svm_update_tsc_ratio_msr ( struct kvm_vcpu * vcpu ) ;
void svm_write_tsc_multiplier ( struct kvm_vcpu * vcpu , u64 multiplier ) ;
2021-11-03 10:05:23 -04:00
void nested_copy_vmcb_control_to_cache ( struct vcpu_svm * svm ,
struct vmcb_control_area * control ) ;
2021-11-03 10:05:22 -04:00
void nested_copy_vmcb_save_to_cache ( struct vcpu_svm * svm ,
struct vmcb_save_area * save ) ;
2020-11-17 05:15:41 -05:00
void nested_sync_control_from_vmcb02 ( struct vcpu_svm * svm ) ;
2021-01-13 07:07:52 -05:00
void nested_vmcb02_compute_g_pat ( struct vcpu_svm * svm ) ;
void svm_switch_vmcb ( struct vcpu_svm * svm , struct kvm_vmcb_info * target_vmcb ) ;
2020-03-24 10:41:52 +01:00
2020-04-17 10:24:18 -04:00
extern struct kvm_x86_nested_ops svm_nested_ops ;
2020-03-31 12:17:38 -04:00
/* avic.c */
int avic_ga_log_notifier ( u32 ga_tag ) ;
void avic_vm_destroy ( struct kvm * kvm ) ;
int avic_vm_init ( struct kvm * kvm ) ;
void avic_init_vmcb ( struct vcpu_svm * svm ) ;
2021-03-02 14:40:39 -05:00
int avic_incomplete_ipi_interception ( struct kvm_vcpu * vcpu ) ;
int avic_unaccelerated_access_interception ( struct kvm_vcpu * vcpu ) ;
2020-03-31 12:17:38 -04:00
int avic_init_vcpu ( struct vcpu_svm * svm ) ;
void avic_vcpu_load ( struct kvm_vcpu * vcpu , int cpu ) ;
void avic_vcpu_put ( struct kvm_vcpu * vcpu ) ;
void avic_post_state_restore ( struct kvm_vcpu * vcpu ) ;
void svm_set_virtual_apic_mode ( struct kvm_vcpu * vcpu ) ;
void svm_refresh_apicv_exec_ctrl ( struct kvm_vcpu * vcpu ) ;
bool svm_check_apicv_inhibit_reasons ( ulong bit ) ;
void svm_load_eoi_exitmap ( struct kvm_vcpu * vcpu , u64 * eoi_exit_bitmap ) ;
void svm_hwapic_irr_update ( struct kvm_vcpu * vcpu , int max_irr ) ;
void svm_hwapic_isr_update ( struct kvm_vcpu * vcpu , int max_isr ) ;
int svm_deliver_avic_intr ( struct kvm_vcpu * vcpu , int vec ) ;
bool svm_dy_apicv_has_pending_interrupt ( struct kvm_vcpu * vcpu ) ;
int svm_update_pi_irte ( struct kvm * kvm , unsigned int host_irq ,
uint32_t guest_irq , bool set ) ;
2021-12-08 01:52:33 +00:00
void avic_vcpu_blocking ( struct kvm_vcpu * vcpu ) ;
void avic_vcpu_unblocking ( struct kvm_vcpu * vcpu ) ;
2020-03-31 12:17:38 -04:00
2020-03-24 10:41:54 +01:00
/* sev.c */
2021-04-27 06:16:35 -05:00
# define GHCB_VERSION_MAX 1ULL
# define GHCB_VERSION_MIN 1ULL
2020-12-10 11:09:51 -06:00
2020-03-24 10:41:54 +01:00
extern unsigned int max_sev_asid ;
void sev_vm_destroy ( struct kvm * kvm ) ;
int svm_mem_enc_op ( struct kvm * kvm , void __user * argp ) ;
int svm_register_enc_region ( struct kvm * kvm ,
struct kvm_enc_region * range ) ;
int svm_unregister_enc_region ( struct kvm * kvm ,
struct kvm_enc_region * range ) ;
2021-04-08 22:32:14 +00:00
int svm_vm_copy_asid_from ( struct kvm * kvm , unsigned int source_fd ) ;
2021-10-21 10:43:00 -07:00
int svm_vm_migrate_from ( struct kvm * kvm , unsigned int source_fd ) ;
2020-03-24 10:41:54 +01:00
void pre_sev_run ( struct vcpu_svm * svm , int cpu ) ;
2021-04-21 19:11:15 -07:00
void __init sev_set_cpu_caps ( void ) ;
2020-12-10 11:09:38 -06:00
void __init sev_hardware_setup ( void ) ;
2020-03-24 10:41:54 +01:00
void sev_hardware_teardown ( void ) ;
2021-04-21 19:11:22 -07:00
int sev_cpu_init ( struct svm_cpu_data * sd ) ;
2020-12-10 11:09:40 -06:00
void sev_free_vcpu ( struct kvm_vcpu * vcpu ) ;
2021-03-02 14:40:39 -05:00
int sev_handle_vmgexit ( struct kvm_vcpu * vcpu ) ;
2020-12-10 11:09:54 -06:00
int sev_es_string_io ( struct vcpu_svm * svm , int size , unsigned int port , int in ) ;
2020-12-10 11:10:06 -06:00
void sev_es_init_vmcb ( struct vcpu_svm * svm ) ;
2021-09-20 17:03:02 -07:00
void sev_es_vcpu_reset ( struct vcpu_svm * svm ) ;
KVM: SVM: Add support for booting APs in an SEV-ES guest
Typically under KVM, an AP is booted using the INIT-SIPI-SIPI sequence,
where the guest vCPU register state is updated and then the vCPU is VMRUN
to begin execution of the AP. For an SEV-ES guest, this won't work because
the guest register state is encrypted.
Following the GHCB specification, the hypervisor must not alter the guest
register state, so KVM must track an AP/vCPU boot. Should the guest want
to park the AP, it must use the AP Reset Hold exit event in place of, for
example, a HLT loop.
First AP boot (first INIT-SIPI-SIPI sequence):
Execute the AP (vCPU) as it was initialized and measured by the SEV-ES
support. It is up to the guest to transfer control of the AP to the
proper location.
Subsequent AP boot:
KVM will expect to receive an AP Reset Hold exit event indicating that
the vCPU is being parked and will require an INIT-SIPI-SIPI sequence to
awaken it. When the AP Reset Hold exit event is received, KVM will place
the vCPU into a simulated HLT mode. Upon receiving the INIT-SIPI-SIPI
sequence, KVM will make the vCPU runnable. It is again up to the guest
to then transfer control of the AP to the proper location.
To differentiate between an actual HLT and an AP Reset Hold, a new MP
state is introduced, KVM_MP_STATE_AP_RESET_HOLD, which the vCPU is
placed in upon receiving the AP Reset Hold exit event. Additionally, to
communicate the AP Reset Hold exit event up to userspace (if needed), a
new exit reason is introduced, KVM_EXIT_AP_RESET_HOLD.
A new x86 ops function is introduced, vcpu_deliver_sipi_vector, in order
to accomplish AP booting. For VMX, vcpu_deliver_sipi_vector is set to the
original SIPI delivery function, kvm_vcpu_deliver_sipi_vector(). SVM adds
a new function that, for non SEV-ES guests, invokes the original SIPI
delivery function, kvm_vcpu_deliver_sipi_vector(), but for SEV-ES guests,
implements the logic above.
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Message-Id: <e8fbebe8eb161ceaabdad7c01a5859a78b424d5e.1609791600.git.thomas.lendacky@amd.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-01-04 14:20:01 -06:00
void sev_vcpu_deliver_sipi_vector ( struct kvm_vcpu * vcpu , u8 vector ) ;
2022-01-25 11:11:30 -05:00
void sev_es_prepare_guest_switch ( struct vmcb_save_area * hostsa ) ;
2021-05-06 15:14:41 -05:00
void sev_es_unmap_ghcb ( struct vcpu_svm * svm ) ;
2020-03-24 10:41:54 +01:00
2020-12-10 11:10:08 -06:00
/* vmenter.S */
void __svm_sev_es_vcpu_run ( unsigned long vmcb_pa ) ;
void __svm_vcpu_run ( unsigned long vmcb_pa , unsigned long * regs ) ;
2020-03-24 10:41:52 +01:00
# endif