Skip to content

Commit 1d1722e

Browse files
Kevin Chengsean-jc
authored andcommitted
KVM: SVM: Don't allow L1 intercepts for instructions not advertised
If a feature is not advertised in the guest's CPUID, prevent L1 from intercepting the unsupported instructions by clearing the corresponding intercept in KVM's cached vmcb12. When an L2 guest executes an instruction that is not advertised to L1, we expect a #UD exception to be injected by L0. However, the nested svm exit handler first checks if the instruction intercept is set in vmcb12, and if so, synthesizes an exit from L2 to L1 instead of a #UD exception. If a feature is not advertised, the L1 intercept should be ignored. While creating KVM's cached vmcb12, sanitize the intercepts for instructions that are not advertised in the guest CPUID. This effectively ignores the L1 intercept on nested vm exit handling. It also ignores the L1 intercept when computing the intercepts in vmcb02, so if L0 (for some reason) does not intercept the instruction, KVM won't intercept it at all. Signed-off-by: Kevin Cheng <chengkev@google.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251215192510.2300816-1-chengkev@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 01cde4e commit 1d1722e

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

arch/x86/kvm/svm/nested.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,19 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu)
403403
return __nested_vmcb_check_controls(vcpu, ctl);
404404
}
405405

406+
/*
407+
* If a feature is not advertised to L1, clear the corresponding vmcb12
408+
* intercept.
409+
*/
410+
#define __nested_svm_sanitize_intercept(__vcpu, __control, fname, iname) \
411+
do { \
412+
if (!guest_cpu_cap_has(__vcpu, X86_FEATURE_##fname)) \
413+
vmcb12_clr_intercept(__control, INTERCEPT_##iname); \
414+
} while (0)
415+
416+
#define nested_svm_sanitize_intercept(__vcpu, __control, name) \
417+
__nested_svm_sanitize_intercept(__vcpu, __control, name, name)
418+
406419
static
407420
void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
408421
struct vmcb_ctrl_area_cached *to,
@@ -413,6 +426,12 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu,
413426
for (i = 0; i < MAX_INTERCEPT; i++)
414427
to->intercepts[i] = from->intercepts[i];
415428

429+
__nested_svm_sanitize_intercept(vcpu, to, XSAVE, XSETBV);
430+
nested_svm_sanitize_intercept(vcpu, to, INVPCID);
431+
nested_svm_sanitize_intercept(vcpu, to, RDTSCP);
432+
nested_svm_sanitize_intercept(vcpu, to, SKINIT);
433+
nested_svm_sanitize_intercept(vcpu, to, RDPRU);
434+
416435
to->iopm_base_pa = from->iopm_base_pa;
417436
to->msrpm_base_pa = from->msrpm_base_pa;
418437
to->tsc_offset = from->tsc_offset;

arch/x86/kvm/svm/svm.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,28 +434,47 @@ static __always_inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
434434
*/
435435
#define SVM_REGS_LAZY_LOAD_SET (1 << VCPU_EXREG_PDPTR)
436436

437-
static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
437+
static inline void __vmcb_set_intercept(unsigned long *intercepts, u32 bit)
438438
{
439439
WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
440-
__set_bit(bit, (unsigned long *)&control->intercepts);
440+
__set_bit(bit, intercepts);
441441
}
442442

443-
static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
443+
static inline void __vmcb_clr_intercept(unsigned long *intercepts, u32 bit)
444444
{
445445
WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
446-
__clear_bit(bit, (unsigned long *)&control->intercepts);
446+
__clear_bit(bit, intercepts);
447447
}
448448

449-
static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
449+
static inline bool __vmcb_is_intercept(unsigned long *intercepts, u32 bit)
450450
{
451451
WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
452-
return test_bit(bit, (unsigned long *)&control->intercepts);
452+
return test_bit(bit, intercepts);
453+
}
454+
455+
static inline void vmcb_set_intercept(struct vmcb_control_area *control, u32 bit)
456+
{
457+
__vmcb_set_intercept((unsigned long *)&control->intercepts, bit);
458+
}
459+
460+
static inline void vmcb_clr_intercept(struct vmcb_control_area *control, u32 bit)
461+
{
462+
__vmcb_clr_intercept((unsigned long *)&control->intercepts, bit);
463+
}
464+
465+
static inline bool vmcb_is_intercept(struct vmcb_control_area *control, u32 bit)
466+
{
467+
return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit);
468+
}
469+
470+
static inline void vmcb12_clr_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
471+
{
472+
__vmcb_clr_intercept((unsigned long *)&control->intercepts, bit);
453473
}
454474

455475
static inline bool vmcb12_is_intercept(struct vmcb_ctrl_area_cached *control, u32 bit)
456476
{
457-
WARN_ON_ONCE(bit >= 32 * MAX_INTERCEPT);
458-
return test_bit(bit, (unsigned long *)&control->intercepts);
477+
return __vmcb_is_intercept((unsigned long *)&control->intercepts, bit);
459478
}
460479

461480
static inline void set_exception_intercept(struct vcpu_svm *svm, u32 bit)

0 commit comments

Comments
 (0)