From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B0613016F2 for ; Wed, 4 Mar 2026 00:30:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772584221; cv=none; b=cIsANvCAcp8ZTTeWzGlQ9w0G89qmisPv7RePPGaSxJ+J46j+0QgCYCdJzQPLr6XCAMx89mwaNsbSa6ZRwUGkeQLwYBDMoroXrj/uGuBJ94jZlJjV34jYOgubXpx59jrCRe9iHWiqd+ovsUmyn5sMnriZ1fZu4lTvem36V0vD/9c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772584221; c=relaxed/simple; bh=gj5RrXvjF9gGzH/8mhVzBo2MAAP1PcFar51lLz5JUCo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iMjf2e9oIJPHAKWXqpvdeU4S38tG/gv5mtVanUwA4jbUoTkEnBSKFq4AjN8zuSpyvPb4X7vM4wOXLl3KLBp7PKp3qhedBOtJmtRhBuRP6qOTugf0Ey/uSub1l6TK4oFbjvAV48A3iSBGEI5KzccQk3lJwROyAtCs53PuSO+tcoc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OZLN+mrW; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OZLN+mrW" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-35842aa350fso28481654a91.0 for ; Tue, 03 Mar 2026 16:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772584216; x=1773189016; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=IjLyd2RWGNGV5RqBT9JABbG50LnPdxLHn8xgFqmPo6c=; b=OZLN+mrWvs3FfrKwMl0U7RTi4J445jkh8sHIvoclDKUNCg6Sre40xGfxAlhkaNGC0C PJ4ych7se+BlbTPwSWbi/HbLoJIyJoh+nngNi1DhQ+6fkx1beezlrm3Q7tX79wT3i2bD ScbcKyE6VqK4prFzRRBBjKyFbkzLwzEAVBS42Q/dX6RUcguCq2bUVFqf9bOxXAqLNN7E FZetMQQfHPXuOYUS6WoiN41VrTflLYcbOh9B/aGbR2/WrTqXIiTlqur4Rs6Vu7prblEY tiYt+s8dN0uji+1ZR+6R2D5KtnSUClLE7bIOZiNO0YlbPBuV6LNbRppferWp3A+Ez89m Rv6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772584216; x=1773189016; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IjLyd2RWGNGV5RqBT9JABbG50LnPdxLHn8xgFqmPo6c=; b=ugoWwOpcF+59H0nFIqoRb196kXac4QPubZQhTtLXo1E+JcQFbfYaxgfCcYHfmrg+aa pqvgPKSjNbb2PSkcg805WYBGTdSl48cpU3VziJSyQSZZNxHv5hwkpw6FVI4BL4sv+pII AKw1IJcPSX+VGaCFv26rMETwvK/6mW1R0y4Q/ezSpjvucCqFPUedRs6hC+GDQjXC47UZ gplTOGK0c9S0XSCugQVQ7w4SdH4H3LVbVGC6cIVZKMlOOog9OkJxzZrbZUZAWpWOo2Zo IVRxFgTe1BVqb4yW2saOUOQiSUA2L+ZEDhzk7uHc32P4Bi1DEivUrJEDRDhBMPdL6Ki2 5b5A== X-Forwarded-Encrypted: i=1; AJvYcCXhPnuFUd8KR6mATFHT8YoYdTFviMTqvmDQgDLT+M1p3/OCZmlQL1aCwsATcLuZn8sLVX+mrpKZ8WxdctY=@vger.kernel.org X-Gm-Message-State: AOJu0YzLCMl00uRETwdoX5QXfvqZs2MrA/eQ9GVNnznAM+HcwkJZNmYb 4zhMdFFzyQpTnUQDZyMBfLmTQ7bAbWrmopPjPBaRqKVBazSe51AmY7hf5ipJlD4RKduV9pWUtOz npsG+pA== X-Received: from pjbrs4.prod.google.com ([2002:a17:90b:2b84:b0:359:9111:25a5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1643:b0:356:24f0:af0c with SMTP id 98e67ed59e1d1-359a6a4d3famr310677a91.17.1772584215513; Tue, 03 Mar 2026 16:30:15 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 3 Mar 2026 16:30:10 -0800 In-Reply-To: <20260304003010.1108257-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260304003010.1108257-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260304003010.1108257-3-seanjc@google.com> Subject: [PATCH v5 2/2] KVM: SVM: Recalc instructions intercepts when EFER.SVME is toggled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kevin Cheng Content-Type: text/plain; charset="UTF-8" From: Kevin Cheng The AMD APM states that VMRUN, VMLOAD, VMSAVE, CLGI, VMMCALL, and INVLPGA instructions should generate a #UD when EFER.SVME is cleared. Currently, when VMLOAD, VMSAVE, or CLGI are executed in L1 with EFER.SVME cleared, no #UD is generated in certain cases. This is because the intercepts for these instructions are cleared based on whether or not vls or vgif is enabled. The #UD fails to be generated when the intercepts are absent. Fix the missing #UD generation by ensuring that all relevant instructions have intercepts set when SVME.EFER is disabled. VMMCALL is special because KVM's ABI is that VMCALL/VMMCALL are always supported for L1 and never fault. Signed-off-by: Kevin Cheng [sean: isolate Intel CPU "compatibility" in EFER.SVME=1 path] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 5975a1e14ac9..07b595487caf 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -244,6 +244,8 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm)) set_exception_intercept(svm, GP_VECTOR); } + + kvm_make_request(KVM_REQ_RECALC_INTERCEPTS, vcpu); } svm->vmcb->save.efer = efer | EFER_SVME; @@ -1042,27 +1044,31 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) } /* - * No need to toggle VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK here, it is - * always set if vls is enabled. If the intercepts are set, the bit is - * meaningless anyway. + * Intercept instructions that #UD if EFER.SVME=0, as SVME must be set + * even when running the guest, i.e. hardware will only ever see + * EFER.SVME=1. + * + * No need to toggle any of the vgif/vls/etc. enable bits here, as they + * are set when the VMCB is initialized and never cleared (if the + * relevant intercepts are set, the enablements are meaningless anyway). */ - if (guest_cpuid_is_intel_compatible(vcpu)) { + if (!(vcpu->arch.efer & EFER_SVME)) { svm_set_intercept(svm, INTERCEPT_VMLOAD); svm_set_intercept(svm, INTERCEPT_VMSAVE); + svm_set_intercept(svm, INTERCEPT_CLGI); + svm_set_intercept(svm, INTERCEPT_STGI); } else { /* * If hardware supports Virtual VMLOAD VMSAVE then enable it * in VMCB and clear intercepts to avoid #VMEXIT. */ - if (vls) { + if (guest_cpuid_is_intel_compatible(vcpu)) { + svm_set_intercept(svm, INTERCEPT_VMLOAD); + svm_set_intercept(svm, INTERCEPT_VMSAVE); + } else if (vls) { svm_clr_intercept(svm, INTERCEPT_VMLOAD); svm_clr_intercept(svm, INTERCEPT_VMSAVE); } - } - - if (vgif) { - svm_clr_intercept(svm, INTERCEPT_STGI); - svm_clr_intercept(svm, INTERCEPT_CLGI); /* * Process pending events when clearing STGI/CLGI intercepts if @@ -1070,8 +1076,13 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) * that KVM re-evaluates if the intercept needs to be set again * to track when GIF is re-enabled (e.g. for NMI injection). */ - if (svm_has_pending_gif_event(svm)) - kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); + if (vgif) { + svm_clr_intercept(svm, INTERCEPT_CLGI); + svm_clr_intercept(svm, INTERCEPT_STGI); + + if (svm_has_pending_gif_event(svm)) + kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); + } } if (kvm_need_rdpmc_intercept(vcpu)) -- 2.53.0.473.g4a7958ca14-goog