From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 1BFB943CE7A for ; Fri, 31 Jul 2026 17:33:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519227; cv=none; b=QHu8gLIbVSOZGrVrPkxpXQU9RXmy8UnEK4seZJzs0/CAkll/STL1U4lNWCkPftc4/p3a8LL4hHXxxjlBTdlJyD2VYJt0QCcG7gfyXjy5xTK9xkqrnvAJGyuPBokD7B2aD9YijemVEBNv3+HZu7y00FovGUPBF0m18SA7k716fYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519227; c=relaxed/simple; bh=5/bWgPygioIqyXafHXY0g1HD0/B9s0wgc+X6hvzcTMc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ukgagx7YvnntdUUPTZr6xHfOw+Fjo6qlHdN7xcclVv/FtpXZaCXgbBPpjIxHG6FG8KjWG4Edh/rbyxXjZz6OBXNeYWsWA3JPcppJ28LiqygX2/3NEHmf1JSIgeH/3gThZM7mFuXaCvs1CRe0qzIWGC8ZNrtzZ5kGorH7teeHeMU= 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=HX4cn4RQ; arc=none smtp.client-ip=209.85.216.72 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="HX4cn4RQ" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-38ce7fabf76so2073129a91.2 for ; Fri, 31 Jul 2026 10:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519224; x=1786124024; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=CoH/ougIGF6yi6WPwn21nwpYwq87thgO5YofxYPMUck=; b=HX4cn4RQ4H8mnHJ6W0/x6HP9QlAFtJzU6J2kuJ0lQ9TU9wQ97Rv8ZQcoevROGUFlEY MYakoLh0ED6uS3YcltSZgtS9Ksg4+i/bIQVY7JQuo49LaRbo8lyO7ICxvoL2vg6xUQtS mwpni94b5K6HxKMgn6tDoQ+VNYkokVuec2MVjEptogY7bPfA5+HBS9OZ4LVX7KuMP0aL r5qmy7PyktO5tn5u42wLmc1wSeP54rTgfrrpMQ7W10luOL+7rm8VSYe6tRGKi66Mnl7O PVIXJcOrKYrtZsmIIFB8CNrO5/vVU1RDwz6gjmRmQmDMg3svpQXSecPy8O93tZwEVBSk ke/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519224; x=1786124024; h=content-type: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:content-type; bh=CoH/ougIGF6yi6WPwn21nwpYwq87thgO5YofxYPMUck=; b=PFEik95W0Qm8XVlDXzgclqTIGBXWRiD85YnFkNbidm1N9sijYqgxh3ibId+g+B14OU tnu/Xl/ztWpmwQDOiSlLvt2kzzPAb6Sds2N+EBcPLuEvnfxWZ37ruIpABr0vvisYl9Jl XqawWXgTgyySU0Dg//KPuy2DCIKE4s6/B0a+NRDnUkFd95UiTyL/jlPcvGNirU2QgKG6 zSCc8S3J8SO2M7hqYda1TcgtXYKfdj7tLNRZgpKAYz/HcPaT1OLCzcdg2rNvKrlRNjpz AmvDE3SBdWqtXAk1p1qM0FNOoY6JUCp7y9wiC4QpT0scRN7LPjf+J/dFz70DMX1OhaNn Qk7w== X-Forwarded-Encrypted: i=1; AHgh+Rr2pUS6rQXyYbInoHp9ZJvKNWs74dmr5EbfvZpWQBIHJKDtPw+Wk9whQ1SlniBGPOkFqy2GFrMw0VNwNOc=@vger.kernel.org X-Gm-Message-State: AOJu0YxbWtv45N6pwpYz9g3286+4TQvc+iVOAzImuzAl/i+aQuzg5ayz 8PINeTfMBy6rvmwNSKcUPDP6Husbs2ZiY5oWgogJM+X3r9kpE7lTyZWZjJtPiPkUMFHyioIUIKI 5N7m0Rw== X-Received: from pjbnp18.prod.google.com ([2002:a17:90b:4c52:b0:38f:245:21b7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d445:b0:38e:5717:9cc8 with SMTP id 98e67ed59e1d1-38fbc56c639mr595891a91.31.1785519223409; Fri, 31 Jul 2026 10:33:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:35 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-2-seanjc@google.com> Subject: [PATCH v5 1/6] KVM: x86: Extract VMX's unhandleable emulation check to common x86 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose VMX's check for unhandleable emulation as its own kvm_x86_ops hook, and move the actual pre-KVM_RUN check into common x86. This will allow sharing the core logic with KVM's RSM emulation without needed to add a post-RSM hook, and is a step towards removing the .vcpu_pre_run() hook entirely. Alternatively, KVM could provide a post-RSM hook as mentioned, but pre/post hooks tend to be unwieldy as the exact "timing" of the call often matters greatly. E.g. in this case, the call must slot in exactly between loading guest state from SMRAM and the hack to force the vCPU out of L2 on SHUTDOWN. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/main.c | 11 +++++++++++ arch/x86/kvm/vmx/vmx.c | 7 +------ arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 5 +++++ 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 5cb132eca3c3..e5b0458afc25 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -68,6 +68,7 @@ KVM_X86_OP(vcpu_run) KVM_X86_OP(handle_exit) KVM_X86_OP(skip_emulated_instruction) KVM_X86_OP_OPTIONAL(update_emulated_instruction) +KVM_X86_OP_OPTIONAL_RET0(unhandleable_emulation_required) KVM_X86_OP(set_interrupt_shadow) KVM_X86_OP(get_interrupt_shadow) KVM_X86_OP(patch_hypercall) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 7a258831616f..7e3cacb2df9b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1593,6 +1593,8 @@ struct kvm_x86_ops { enum exit_fastpath_completion exit_fastpath); int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); void (*update_emulated_instruction)(struct kvm_vcpu *vcpu); + bool (*unhandleable_emulation_required)(struct kvm_vcpu *vcpu); + void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask); u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu); void (*patch_hypercall)(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 04f986e3d439..2a69dc3ac690 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -165,6 +165,16 @@ static int vt_handle_exit(struct kvm_vcpu *vcpu, return vmx_handle_exit(vcpu, fastpath); } =20 +static bool vt_unhandleable_emulation_required(struct kvm_vcpu *vcpu) +{ + if (is_td_vcpu(vcpu)) { + WARN_ON_ONCE(to_vt(vcpu)->emulation_required); + return false; + } + + return vmx_unhandleable_emulation_required(vcpu); +} + static int vt_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { if (unlikely(is_td_vcpu(vcpu))) @@ -944,6 +954,7 @@ struct kvm_x86_ops vt_x86_ops __initdata =3D { .handle_exit =3D vt_op(handle_exit), .skip_emulated_instruction =3D vmx_skip_emulated_instruction, .update_emulated_instruction =3D vmx_update_emulated_instruction, + .unhandleable_emulation_required =3D vt_op(unhandleable_emulation_require= d), .set_interrupt_shadow =3D vt_op(set_interrupt_shadow), .get_interrupt_shadow =3D vt_op(get_interrupt_shadow), .patch_hypercall =3D vt_op(patch_hypercall), diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e4b9ac7fed9f..2337b7ede290 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6035,7 +6035,7 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu) * with unsrestricted guest mode disabled) and KVM can't faithfully emulat= e the * current vCPU state. */ -static bool vmx_unhandleable_emulation_required(struct kvm_vcpu *vcpu) +bool vmx_unhandleable_emulation_required(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); =20 @@ -6112,11 +6112,6 @@ static int handle_invalid_guest_state(struct kvm_vcp= u *vcpu) =20 int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu) { - if (vmx_unhandleable_emulation_required(vcpu)) { - kvm_prepare_emulation_failure_exit(vcpu); - return 0; - } - return 1; } =20 diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index 409858074246..551b4195bc1b 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -31,6 +31,7 @@ int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exi= t_fastpath); void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu); int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu); void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu); +bool vmx_unhandleable_emulation_required(struct kvm_vcpu *vcpu); int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info); #ifdef CONFIG_KVM_SMM int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0f1a829032c0..64a13acc37be 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8867,6 +8867,11 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcp= u) !kvm_apic_init_sipi_allowed(vcpu)) return -EINVAL; =20 + if (kvm_x86_call(unhandleable_emulation_required)(vcpu)) { + kvm_prepare_emulation_failure_exit(vcpu); + return 0; + } + return kvm_x86_call(vcpu_pre_run)(vcpu); } =20 --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 9B6924503F3 for ; Fri, 31 Jul 2026 17:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519227; cv=none; b=plLt8vrfVjKUImhvVK0KdWW1qPjrzFd2TTuQhf/3KPXh+/ow2iNUb0sDqQRuRFgYclu07WbefDJichJzo2SliS6c1SUgcLjdTAAdiTZQrK6wbGNt2b2CTDbYfiGGSLeoYbpRrkk4Hay3oZ86ks3qabNhhdKZz/Zj1VNLgKyP0KE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519227; c=relaxed/simple; bh=pNM5n+IuCxl2iLTtunwdVdAAkFkP0yRDBV9fsMqYoTw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=e0x29VZkJ6EZdAv2jLf1pets3QonsfRpwPR6TbSMthZMduoxCDDIvCXmwDijAFjtEikrRQbqKYL3Vut1A6phxFWNZi7U7unscofCmxGnsKZv8QcqKcNdodIHhedRVsROYP/h9JHMZsRsxBx7NOUmdgiUbd25oUGjuuSarMQAYGo= 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=QrjUuv5y; arc=none smtp.client-ip=209.85.216.72 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="QrjUuv5y" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-38dd87656d3so1691500a91.2 for ; Fri, 31 Jul 2026 10:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519225; x=1786124025; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=NPRf96ka6Bve5osy1MsMtUuS9Eza24x4seftdT78Sdg=; b=QrjUuv5yt43wD9axlDvAcvNp2L3y32oUxWl7ELnuAjoh80pnOZ5BClG1DhNmwELs0w S4wjmWQS6EUNHt11wnYxet06NVXK7FmZncPmFQoJG+erDuV7/5bsC+C+nv2xbTgXmRXv D+NrFcXNxXoPxzhBevbg9b1ujplWS2qX+YI33wkKv64bpDrFmALVM6N7RWreELwEdsNC 4TVeqnZuZ4zcM05hKz8QMW/jy43P46vTHUcY0Cv7GhhetZ5b4oXMMTrnlBnEya3D69Bh 3dkGrgwwFoNhB6nnBxiCCnYmyEzMh/JkR9B/r0xQE3/mYoDqTQwPyv6u8pLrdl6GxrB5 B+5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519225; x=1786124025; h=content-type: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:content-type; bh=NPRf96ka6Bve5osy1MsMtUuS9Eza24x4seftdT78Sdg=; b=MbrtvWox4mrdw8rpA7SiNTNB+3fcDOwCoH4C9uVxuVq0bBGad36GY4ENW666dffkZa 0huBAlLUxCGNC93rCUi8HoVMbJ6HGHCQImwCsJ1D9mPosMLsJHpz93o/6xmLPkRBsWqR chXtLVvRRntVn1+IypqKOI6KW6IhzcGR+7E3ewoks6tgWLobvEp+YNC7zShIIGOn07qP zHUwAYTqNAbaoW6Y7BzmgketjPb78geDQSA3nFPE6cZb7oJcqg0fqRQlTr6xV1++UdFy YEzrwda9/P9DY4jImTvezwnxAl+DTnVzkROE4i9oDXjbZWVyznxz6QcgjObOBmaodhcj D3zw== X-Forwarded-Encrypted: i=1; AHgh+Ro1trZTtmYYwIKzad99G52rB5fRcPHT5A4ES5JCIehD/5OdI31wrcZGICOSuEltvmZZT4rKXeAT5PvyO98=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+wwjZ8fLFZ9jlZc/TIdfJNZGA18tb/GDMliHNavjzR+EXwYj5 irQ2qU3RV8wrSOpHNo6RLBFMrHTNTVQFPhEJ1WiUPgtSORUBFs68RtE9h8zkcwrTIlEKQIHLWd8 sxeYMOw== X-Received: from pjbbj3.prod.google.com ([2002:a17:90b:883:b0:380:6618:e058]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:17c7:b0:38f:1dc:672f with SMTP id 98e67ed59e1d1-38fbc497f62mr587492a91.18.1785519224621; Fri, 31 Jul 2026 10:33:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:36 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-3-seanjc@google.com> Subject: [PATCH v5 2/6] KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Synthesize SHUTDOWN (for L1) if L2 requires unhandleable emulation after loading guest state from SMRAM during RSM to prevent a misbehaving L1 (or userspace via L1) from tripping the sanity check that KVM doesn't try to cancel a pending nested VM-Enter. If SMRAM is modified such that RSM will load what should be impossible state for L2, then KVM will detect that it needs to emulate the current code stream and will abort VM-Entry to L2. And because KVM (rightly) expects such a scenario to be impossible, KVM WARNs and bugs the VM. __ret && !(vcpu->kvm)->vm_bugged WARNING: arch/x86/kvm/vmx/vmx.c:6741 at vmx_handle_exit+0x65/0x790 [kvm_i= ntel], CPU#13: vmx_invalid_nes/2902 Modules linked in: kvm_intel kvm irqbypass [last unloaded: kvm] CPU: 13 UID: 1000 PID: 2902 Comm: vmx_invalid_nes Tainted: G W 7.2= .0-rc2 #124 PREEMPT Tainted: [W]=3DWARN Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:vmx_handle_exit+0x65/0x790 [kvm_intel] Call Trace: kvm_arch_vcpu_ioctl_run+0xdf8/0x1d00 [kvm] kvm_vcpu_ioctl+0x2d5/0x960 [kvm] __x64_sys_ioctl+0x8a/0xd0 do_syscall_64+0xb7/0x570 entry_SYSCALL_64_after_hwframe+0x4b/0x53 Alternatively, KVM could suppress the WARN for the RSM case, but that would still leave the vCPU in a "bad" state that KVM doesn't know how to handle (which is also why KVM rejects attempts to do KVM_RUN when the vCPU is loaded with invalid state). And architecturally, the Intel SDM explicitly states that RSM leads to shutdown if the CPU detects invalid state. Fixes: 2bb8cafea80b ("KVM: vVMX: signal failure for nested VMEntry if emula= tion_required") Reported-by: Hao Zhang Signed-off-by: Sean Christopherson --- arch/x86/kvm/smm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c index a446487bdd5c..656a38dad7e7 100644 --- a/arch/x86/kvm/smm.c +++ b/arch/x86/kvm/smm.c @@ -649,6 +649,10 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt) #endif ret =3D rsm_load_state_32(ctxt, &smram.smram32); =20 + if (ret =3D=3D X86EMUL_CONTINUE && + kvm_x86_call(unhandleable_emulation_required)(vcpu)) + ret =3D X86EMUL_UNHANDLEABLE; + /* * If RSM fails and triggers shutdown, architecturally the shutdown * occurs *before* the transition to guest mode. But due to KVM's --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 1700D44E64E for ; Fri, 31 Jul 2026 17:33:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519230; cv=none; b=iSuIlJp9GR0bmJMqOwzWNfi2N5w2InuVaruis7Rm5UWIYJHu6I1LHGd+e69RzP9niiLEJvMwH0xvsZwRRsexl8i0VaZXs3WxHVQnKmZleyjxkfEu1tmw/exSbElhbW78oy2mEM+OtGSxiSHO7M7MAL+TYYhSZu2zSzOmrPxIBbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519230; c=relaxed/simple; bh=JjorgHnrRqHA3MkLqNR/CwX6y2mrNEKtCoXCr6JiDQE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cFZc6pHvonrOi38v89U9gJeNJxr82UUzwvw49mtBvOnxpYXSU0yNq7SiBvW4PXfzY7s0GAz9aNN2ItdxDR8ocFuQ+2LVMDtRNpudvd8ZLUOHfYlUraz+8J2mNGu8oiXRLX4e18Sl/6fNM2FPpoVSCRmQH4hD5yjAUrhCCmrULYE= 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=ideod2JF; arc=none smtp.client-ip=209.85.210.198 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="ideod2JF" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-8484f26852dso1440839b3a.1 for ; Fri, 31 Jul 2026 10:33:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519226; x=1786124026; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=kx00NjRjiwHJ66IMeM0hhNXW9W7e0zenonV59M82y0I=; b=ideod2JFXlmiSHoRfol/iH+Q6ZJFlolFrjQBBKedbv43NdEZdRNqIvBt27DTtXBxeU 5+FDiBDGp1GNHZDYkB6WRWGKww8R2ntHb365FJim9djbBlzC834wKVo4eK5zLAALzXAt /64pghacpSqj8P9fXQi+DSHFrAjPzd0CAqKTJqKtu/RA9ME5lVxXM23efRw3IZPg9fZd 768K7/v1IWVmvxaespeWRzVTKHPTzrGV/7KtaXKJwHzXlwmXfZaOU6lhVuMCiKwVw+gH chDwgVkFtScF+Dt4NV5T1eO0jSFvBKohkrzkNvWvOPt7wvvX2ZmJFtXcj2eouaakNv6c kDJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519226; x=1786124026; h=content-type: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:content-type; bh=kx00NjRjiwHJ66IMeM0hhNXW9W7e0zenonV59M82y0I=; b=UNvbtXe+3reQENP+1gQRjrvegXRPGPO23BnUzthKyJslzCsZK06dfnJT1YZpVqboCp 4wQhCedGsS2HXXF4aqGas35PC0ombyFFipxO6LA4wy48meyIY2vtlFS2d10ya4X5tsZw JrBVOKSrf7DjO7YJRXuMaQcSyPpRSMgr5UVxqJGAGGzv5MTmcyYvKu1OzN9p49+iyQ/g 7FvUHT/1pSjv2gjePHodtL4/sfbwZSyip90CjkHGaTzqxHkTe4o/JqFVdloFTXMdG3aR 7/hmzZMH9Ep6IaU6B8Xu5vkeo+dL9U3twMmuySrFM++JETi+mgc9sgz95/AE+hKa8BIF GYlQ== X-Forwarded-Encrypted: i=1; AHgh+RpKBEIFQrKNWG4qlet5YIowviuoUfQ+UzBQNQEkXMfBQySIlEPM9qeH3QyojwWsiBxCRV+MUdNqKop+Nr8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4eI1s9YSWeEwmKY5MwpYdO7bMyyE+UAVJX/zpAlt74t3WvBr5 juCXGnzYx/KaZqF6WotZBdvigYMyPtukwXUe2XzLmEHdZoHuVd0JjfCsYxbRlSUt+Q/o/HkbMBQ 4DeLXrw== X-Received: from pfbk15.prod.google.com ([2002:a05:6a00:b00f:b0:84e:4b64:d17b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9284:b0:84e:24df:3870 with SMTP id d2e1a72fcca58-84ee45dfe1fmr529111b3a.0.1785519225828; Fri, 31 Jul 2026 10:33:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:37 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-4-seanjc@google.com> Subject: [PATCH v5 3/6] KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into .vcpu_needs_initialization() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework .vcpu_pre_run() into a more specific .vcpu_needs_initialization() to consolidate the SNP and TDX control flows, and to eliminate the potentially confusing almost-collision between svm_vcpu_pre_run() and pre_sev_run(): the former is SEV specific, but is pre-KVM_RUN, whereas the latter is pre-VMRUN. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/sev.c | 5 +++++ arch/x86/kvm/svm/svm.c | 12 +----------- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 10 ++++------ arch/x86/kvm/vmx/tdx.c | 9 +++------ arch/x86/kvm/vmx/vmx.c | 5 ----- arch/x86/kvm/vmx/x86_ops.h | 3 +-- arch/x86/kvm/x86.c | 5 ++++- 10 files changed, 21 insertions(+), 33 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index e5b0458afc25..e213c9ae3e30 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -63,7 +63,7 @@ KVM_X86_OP_OPTIONAL(flush_remote_tlbs_range) #endif KVM_X86_OP(flush_tlb_gva) KVM_X86_OP(flush_tlb_guest) -KVM_X86_OP(vcpu_pre_run) +KVM_X86_OP_OPTIONAL_RET0(vcpu_needs_initialization) KVM_X86_OP(vcpu_run) KVM_X86_OP(handle_exit) KVM_X86_OP(skip_emulated_instruction) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 7e3cacb2df9b..283847619ff8 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1586,7 +1586,7 @@ struct kvm_x86_ops { */ void (*flush_tlb_guest)(struct kvm_vcpu *vcpu); =20 - int (*vcpu_pre_run)(struct kvm_vcpu *vcpu); + bool (*vcpu_needs_initialization)(struct kvm_vcpu *vcpu); enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu, u64 run_flags); int (*handle_exit)(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 90a08d36d843..fcb41dfde4c0 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3556,6 +3556,11 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) __sev_es_unmap_ghcb(svm); } =20 +bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu) +{ + return to_kvm_sev_info(vcpu->kvm)->need_init; +} + int pre_sev_run(struct vcpu_svm *svm, int cpu) { struct svm_cpu_data *sd =3D per_cpu_ptr(&svm_data, cpu); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c1e6d62512b9..9d607b98bd06 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4400,16 +4400,6 @@ static void svm_cancel_injection(struct kvm_vcpu *vc= pu) svm_complete_interrupts(vcpu); } =20 -static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) -{ -#ifdef CONFIG_KVM_AMD_SEV - if (to_kvm_sev_info(vcpu->kvm)->need_init) - return -EINVAL; -#endif - - return 1; -} - static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); @@ -5376,7 +5366,6 @@ struct kvm_x86_ops svm_x86_ops __initdata =3D { .flush_tlb_gva =3D svm_flush_tlb_gva, .flush_tlb_guest =3D svm_flush_tlb_guest, =20 - .vcpu_pre_run =3D svm_vcpu_pre_run, .vcpu_run =3D svm_vcpu_run, .handle_exit =3D svm_handle_exit, .skip_emulated_instruction =3D svm_skip_emulated_instruction, @@ -5434,6 +5423,7 @@ struct kvm_x86_ops svm_x86_ops __initdata =3D { #endif =20 #ifdef CONFIG_KVM_AMD_SEV + .vcpu_needs_initialization =3D sev_vcpu_needs_initialization, .dev_get_attr =3D sev_dev_get_attr, .mem_enc_ioctl =3D sev_mem_enc_ioctl, .mem_enc_register_region =3D sev_mem_enc_register_region, diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index a327bf751ecd..66b44b54608e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -979,6 +979,7 @@ void sev_es_prepare_switch_to_guest(struct vcpu_svm *sv= m, struct sev_es_save_are void sev_es_unmap_ghcb(struct vcpu_svm *svm); =20 #ifdef CONFIG_KVM_AMD_SEV +bool sev_vcpu_needs_initialization(struct kvm_vcpu *vcpu); int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp); int sev_mem_enc_register_region(struct kvm *kvm, struct kvm_enc_region *range); diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 2a69dc3ac690..0ff3230fd95e 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -140,12 +140,10 @@ static void vt_vcpu_put(struct kvm_vcpu *vcpu) vmx_vcpu_put(vcpu); } =20 -static int vt_vcpu_pre_run(struct kvm_vcpu *vcpu) +static bool vt_vcpu_needs_initialization(struct kvm_vcpu *vcpu) { - if (is_td_vcpu(vcpu)) - return tdx_vcpu_pre_run(vcpu); - - return vmx_vcpu_pre_run(vcpu); + return is_td_vcpu(vcpu) && + tdx_vcpu_needs_initialization(vcpu); } =20 static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) @@ -949,7 +947,7 @@ struct kvm_x86_ops vt_x86_ops __initdata =3D { .flush_tlb_gva =3D vt_op(flush_tlb_gva), .flush_tlb_guest =3D vt_op(flush_tlb_guest), =20 - .vcpu_pre_run =3D vt_op(vcpu_pre_run), + .vcpu_needs_initialization =3D vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run =3D vt_op(vcpu_run), .handle_exit =3D vt_op(handle_exit), .skip_emulated_instruction =3D vmx_skip_emulated_instruction, diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index d1af0a752e97..b272c20586a7 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -897,13 +897,10 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu) tdx->state =3D VCPU_TD_STATE_UNINITIALIZED; } =20 -int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu) +bool tdx_vcpu_needs_initialization(struct kvm_vcpu *vcpu) { - if (unlikely(to_tdx(vcpu)->state !=3D VCPU_TD_STATE_INITIALIZED || - to_kvm_tdx(vcpu->kvm)->state !=3D TD_STATE_RUNNABLE)) - return -EINVAL; - - return 1; + return to_tdx(vcpu)->state !=3D VCPU_TD_STATE_INITIALIZED || + to_kvm_tdx(vcpu->kvm)->state !=3D TD_STATE_RUNNABLE; } =20 static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 2337b7ede290..76160adf8297 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6110,11 +6110,6 @@ static int handle_invalid_guest_state(struct kvm_vcp= u *vcpu) return 1; } =20 -int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu) -{ - return 1; -} - /* * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE * exiting, so only get here on cpu with PAUSE-Loop-Exiting. diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h index 551b4195bc1b..cdb38d940cfb 100644 --- a/arch/x86/kvm/vmx/x86_ops.h +++ b/arch/x86/kvm/vmx/x86_ops.h @@ -21,7 +21,6 @@ int vmx_vm_init(struct kvm *kvm); void vmx_vm_destroy(struct kvm *kvm); int vmx_vcpu_precreate(struct kvm *kvm); int vmx_vcpu_create(struct kvm_vcpu *vcpu); -int vmx_vcpu_pre_run(struct kvm_vcpu *vcpu); fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags); void vmx_vcpu_free(struct kvm_vcpu *vcpu); void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); @@ -138,7 +137,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu); void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event); void tdx_vcpu_free(struct kvm_vcpu *vcpu); void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu); -int tdx_vcpu_pre_run(struct kvm_vcpu *vcpu); +bool tdx_vcpu_needs_initialization(struct kvm_vcpu *vcpu); fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags); void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu); void tdx_vcpu_put(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 64a13acc37be..d94b59140c45 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8867,12 +8867,15 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vc= pu) !kvm_apic_init_sipi_allowed(vcpu)) return -EINVAL; =20 + if (kvm_x86_call(vcpu_needs_initialization)(vcpu)) + return -EINVAL; + if (kvm_x86_call(unhandleable_emulation_required)(vcpu)) { kvm_prepare_emulation_failure_exit(vcpu); return 0; } =20 - return kvm_x86_call(vcpu_pre_run)(vcpu); + return 1; } =20 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (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 70E19450911 for ; Fri, 31 Jul 2026 17:33:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519230; cv=none; b=mXKQBSCQRBfdjVNNP3OR51e41SmOMFRQ7/5R54ub8anGEsIyx8DYyS+74v+iQe2VooFQiECEBr6tE/BndX5dBznKEKd/FJp97FB7cRg4WHYx4tmKFlAMwje3jEN6QsUryNNCkRhnjWKiiAP3rXkvY5cB7XplAsU1GFzkR+PwuVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519230; c=relaxed/simple; bh=/9IaeOvoXcU42Ex+UfeVc8spxZ4BxaqPlkHGOrDK7e0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i7s2tJz5JEI1e9we4mfOFvTN3bSkzsHiHrBZGvb7uHihrSzGfUOWTHQ5YWZaXOyBDcvlTMe+Z/rhxSxmmDaD8wjZOmO4mY+3NdYoS6EJ4YwPbmXCE746mpUsnFBsqOYiJsCQOqHjqnjAhvtNgImlH/wXEHlv8a9tVT4K9+Zch7c= 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=LAihRKru; arc=none smtp.client-ip=209.85.215.197 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="LAihRKru" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-ca7c1e22995so1893579a12.3 for ; Fri, 31 Jul 2026 10:33:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519227; x=1786124027; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=8ZTp5IeGTNDM5j+o13PTbyW/bv5QJuW6fwzK9SHEypE=; b=LAihRKruYB/Aa3uP3CWMZHf3dZ0hO8wythwIAHD7oo1AMD+GDQcg8m43sVXdAmbVWN eUN3UHAxvAfsglGQCcH3hm6crSUmrFP8eKWqK4+tGAhipp5jW3DNJ2WrQ4ijnIjLdGIF TZOoTnNhv6IK2WYCv4bljwzKvcWFjqh9e7E3UrcTk26aPCeX2QdSn4jijHBJrGMZSEJe BT9bCNouTlYCmIj/8wKK81inxgJ9/6wSBIPzJRp0ns4GMbVDNZk1vUvDQQYLpfWP3TUR 6nmV6/7AICuoR1U99QOhJv3m3XmWMc7cNFZ6rqn+vTPlEX299SgHqFiCXf1Iiov7++DS kSRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519227; x=1786124027; h=content-type: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:content-type; bh=8ZTp5IeGTNDM5j+o13PTbyW/bv5QJuW6fwzK9SHEypE=; b=GjMJbfJ8QRaSOoYTqhIyHOk8ldFgu+/jGqTqZP4aymm7UuMHdbhTT9IjJ9a8Urbwh5 KhZR3zRfXQjcjijF5oaUTqk07GQ3UdZ4KtEQullazFu7kW3KsC+8ZBivrwJluwBKWaDP mm/V7/U6vo0fnkh8bdL1LIKlW/kryWzULkiX4P5z1oIBI0E00baX02sRa09a+HzpZhuG 7jggUofrjeYbDeNFF4S55oSRG+VF/ZNzyIk3Kc/WvH9OEsfOdFkB3dizcNQfTLoI2XvY F3XrdUlFsFJDHz0ktHOb6AEOELMQwJLqj0Nfa2n/Ng2iwkm+3b6P3Uuy2zsST6HPyUe0 IBbA== X-Forwarded-Encrypted: i=1; AHgh+Rp+XzQqOulNJv+HdgoC49KvroasT3+YPcr5XCd337vxIU6/iDr30TsHlzL6TaQHzPIsvUQvyC8xhVovcCM=@vger.kernel.org X-Gm-Message-State: AOJu0YxU9WlJw2Ks4kQnP9np3u94/7uZNfxj4pWN0158w/TdkxPJOQu9 ZDx+WzADOyy1HB80hCgTDNGnTDDWbWToGe4ZSlJFCzAm6wASvdZzGaWlJ3TlB6lYKALzIa/v1x0 akFGLYg== X-Received: from pfqz16.prod.google.com ([2002:aa7:9e50:0:b0:847:9be8:84d5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8b93:b0:845:cf73:c1d8 with SMTP id d2e1a72fcca58-84ee47f6f71mr316580b3a.14.1785519226974; Fri, 31 Jul 2026 10:33:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:38 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-5-seanjc@google.com> Subject: [PATCH v5 4/6] KVM: selftests: Use port 0x80 in invalid nVMX guest state test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use port 0x80 instead of 0x2000 to trigger an L2 =3D> L0 VM-Exit in the nVMX invalid guest state test, so that the test can be expanded to cover RSM in the L2 =3D> SMI =3D> L2 path without having to manually encode a MOV to DX = (the immediate form of IN only supports an imm8). Use port 0x80 as it's a well-known port and is used by other tests, e.g. in the coalesced I/O test. Signed-off-by: Sean Christopherson --- .../selftests/kvm/x86/vmx_invalid_nested_guest_state.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state= .c b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c index 578283893ab3..fb9444ca0d7e 100644 --- a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +++ b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c @@ -9,7 +9,7 @@ =20 #include "kselftest.h" =20 -#define ARBITRARY_IO_PORT 0x2000 +#define ARBITRARY_IO_PORT 0x80 =20 static struct kvm_vm *vm; =20 @@ -19,8 +19,8 @@ static void l2_guest_code(void) * Generate an exit to L0 userspace, i.e. main(), via I/O to an * arbitrary port. */ - asm volatile("inb %%dx, %%al" - : : [port] "d" (ARBITRARY_IO_PORT) : "rax"); + asm volatile("inb $" __stringify(ARBITRARY_IO_PORT) ", %%al" + ::: "rax"); } =20 static void l1_guest_code(struct vmx_pages *vmx_pages) --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 04FA545199E for ; Fri, 31 Jul 2026 17:33:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519233; cv=none; b=MPujPZMwGGtdv/CwzLIGf8XEEkRk7B02KpJEN8sARF+WPGjwab6MV74gGQrF/vdFs+YQ9MJfOVk5zwsH8y1yWM3XgB7xTbuHySzyeJclTzcrPgxxHKi6r5YnQRB2xon0/L0HRsy4gd/nzWagcmU6sICK4HizvmHIlQez3NBs6bQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519233; c=relaxed/simple; bh=FWHbA/npxvmx1aU5dy797YrSrZH0OKZmcuOLHUTxquE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tU4t8ngrfpViN3FacA51Lv8XCMIAlaCtewfrizQujzH9hexAnz3spOsT76nn3alIsFSpKvTTYHkbUqU1XylGEBJk54G/mukm+sr4hZazgPsR7vOrL2YCK4WAHUacKiTmxOuynlGSxQyEPF+tAwlkAv0jkk1BYd14PiNRWO+2m9A= 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=sAudN7Ql; arc=none smtp.client-ip=209.85.216.72 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="sAudN7Ql" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-38dc085b0a7so2220459a91.2 for ; Fri, 31 Jul 2026 10:33:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519229; x=1786124029; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=rfrz25N6FEniJQ8FbqR0gm71fG240oUTnfj7H6PgCoc=; b=sAudN7QlU+nbr07bLQuQCy/YkRj8B54Sn+VoeLW8q3/4W5PIruSbe7D/7B6xYHd1rL FAD8LZKWyJHVgyM+yHk4ZtUDo5mKjGYW2x13FKdRS4xGVtf9H5zsbp4cCYp9t38JiJrp n08EkdNt/bxzDkkb8COpVtzq9QbQMF3BULOP0xYZocH/GStsLN3n+qoF0tjBvbOPVszh VCqsA+exgeEWZMS3CkqLKDiSLKUpKt7lGoWGyNJqz96gjMvrziIFSUiCYXn4GUlO4C4e m9U16kQLpaVEyBKVcp6ARS/2LFVXdSguO5EuwhRP73pc9AnWoHsKJKzrQR9QvFDc5HhX 3lnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519229; x=1786124029; h=content-type: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:content-type; bh=rfrz25N6FEniJQ8FbqR0gm71fG240oUTnfj7H6PgCoc=; b=j8QMgOhQRMznnbHG+PGUKxXH9W76g+j4siEnVujhTjzvS1CQ3q5NelAnY56lC/GKV1 eKalUkTUhBYl3Lz/XTsbubZfMHV/uOBiwrsd+doKbDwYmy34JDCS3UxR49rqEcs12ZjX Jy+NVISssYyUTaQrcLMBiEGVktqAfWqOamTiRFvaNOSgp+Y6ewIEruqmJJkmGc2/4RhH roYF4jyINrSBaOu645g70c/XkE6cylXia/TYkYThVdqtCrQQMMOPo2zltyFSqW+mNu2V 04PSngKv/fyASnxHu+r2GbTcZllM0fVLVJpQMU1OLeJkGmlg3H2MRphm8Wvr4xYw+Uuz r7Cg== X-Forwarded-Encrypted: i=1; AHgh+RoSwKMcgAGAjpqvNY2p5WC7nyt6oDncJc8fz+AxSCfOmaQzDS4nRfuZMGGmZAu7F+ngEXwWODQKo0gmsN8=@vger.kernel.org X-Gm-Message-State: AOJu0Yxl81hAL4VBDuoWBcAOO8EtYHLvAfLSrxszmk2VtO4S1e11cJIM KJgSCcwv+GS75UGOlUD6CBpyEFdhO4I2wtAXSlpNdV1YHTdDls8wEFJfXK22xepXxZ+Fnhr/ruC rnmymMQ== X-Received: from pjbay23.prod.google.com ([2002:a17:90b:317:b0:38f:5801:d550]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c4b:b0:387:e0db:3d8f with SMTP id 98e67ed59e1d1-38fbc560758mr613474a91.42.1785519228108; Fri, 31 Jul 2026 10:33:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:39 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-6-seanjc@google.com> Subject: [PATCH v5 5/6] KVM: selftests: Refactor invalid nVMX state test to prepare for RSM testcase From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the invalid nVMX guest state test, extract the creation of the VM and initial running of the vCPU to get to L2 into helpers so that the common code can be reused to extend the test to also cover RSM. Eliminate the unnecessary global "vm", and opportunistically free the VM after the testcase as there's zero reason not to. Opportunistically assert that L2 is never resumed after the I/O exit to L1, e.g. to guard against false passes. Signed-off-by: Sean Christopherson --- .../kvm/x86/vmx_invalid_nested_guest_state.c | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state= .c b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c index fb9444ca0d7e..4b1bb190c2c6 100644 --- a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +++ b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c @@ -11,8 +11,6 @@ =20 #define ARBITRARY_IO_PORT 0x80 =20 -static struct kvm_vm *vm; - static void l2_guest_code(void) { /* @@ -21,6 +19,7 @@ static void l2_guest_code(void) */ asm volatile("inb $" __stringify(ARBITRARY_IO_PORT) ", %%al" ::: "rax"); + GUEST_FAIL("L2 resumed after stuffing invalid guest state"); } =20 static void l1_guest_code(struct vmx_pages *vmx_pages) @@ -46,35 +45,50 @@ static void l1_guest_code(struct vmx_pages *vmx_pages) GUEST_DONE(); } =20 -int main(int argc, char *argv[]) +static void vcpu_run_to_io(struct kvm_vcpu *vcpu, bool want_l2) +{ + struct kvm_run *run =3D vcpu->run; + + vcpu_run(vcpu); + + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + TEST_ASSERT(run->io.port =3D=3D ARBITRARY_IO_PORT && + (!!(run->flags & KVM_RUN_X86_GUEST_MODE) =3D=3D want_l2 || + !kvm_has_cap(KVM_CAP_X86_GUEST_MODE)), + "Expected IN from port 0x%x from L%u, got port 0x%x from L%u", + ARBITRARY_IO_PORT, 1 + want_l2, run->io.port, + 1 + !!(run->flags & KVM_RUN_X86_GUEST_MODE)); +} + +static struct kvm_vm *vm_create_and_run_l2(struct kvm_vcpu **vcpu) { gva_t vmx_pages_gva; - struct kvm_sregs sregs; - struct kvm_vcpu *vcpu; - struct kvm_run *run; - struct ucall uc; + struct kvm_vm *vm; =20 - TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); - - vm =3D vm_create_with_one_vcpu(&vcpu, l1_guest_code); + vm =3D vm_create_with_one_vcpu(vcpu, l1_guest_code); =20 /* Allocate VMX pages and shared descriptors (vmx_pages). */ vcpu_alloc_vmx(vm, &vmx_pages_gva); - vcpu_args_set(vcpu, 1, vmx_pages_gva); - - vcpu_run(vcpu); - - run =3D vcpu->run; + vcpu_args_set(*vcpu, 1, vmx_pages_gva); =20 /* * The first exit to L0 userspace should be an I/O access from L2. * Running L1 should launch L2 without triggering an exit to userspace. */ - TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + vcpu_run_to_io(*vcpu, true); =20 - TEST_ASSERT(run->io.port =3D=3D ARBITRARY_IO_PORT, - "Expected IN from port %d from L2, got port %d", - ARBITRARY_IO_PORT, run->io.port); + return vm; +} + +static void test_invalid_l2_guest_state(void) +{ + struct kvm_sregs sregs; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + + vm =3D vm_create_and_run_l2(&vcpu); =20 /* * Stuff invalid guest state for L2 by making TR unusable. The next @@ -96,4 +110,13 @@ int main(int argc, char *argv[]) default: TEST_FAIL("Unexpected ucall: %lu", uc.cmd); } + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); + + test_invalid_l2_guest_state(); } --=20 2.55.0.508.g3f0d502094-goog From nobody Fri Oct 2 12:19:54 2026 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 8304B4508EB for ; Fri, 31 Jul 2026 17:33:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519233; cv=none; b=P7QgWScV2zCQBK5NgXp1Y5H9BFUtZN8MKKgbY/Bh4XwmDHcIRITAUPQ7gEsOOrJV+CI1RBQFpsXIGP8zWGB8M2hlEr/qYOwnQfJyNb7ZZGGSAGOrO/R4Fckp9b+cT4FkFHB6Z/diOZKIiE4+6bDSAVxV3X6zeSL/1F6lvGQsKJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785519233; c=relaxed/simple; bh=+gogjg/3D4gQHcPEQRIYtYjXFvse8XsAoXE16fKGJuE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TZcLFwRTJ+RoPSs35cDGZQ3BiBAOVx6c0L873JC2teP9AEQrDxBUOWraOvLta6jtEa+8rVP84/xQmwZZJmtO+vkuhvdfQNaaPbYok1XrLpbvc0qsi6Zu8+QQmPewKkHt+cT0qRY7ig764Eei1voGgROzlIQ7qrOv82RKLGGr2yA= 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=CQfjOLfj; arc=none smtp.client-ip=209.85.215.198 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="CQfjOLfj" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cb7d6ba548eso1427477a12.1 for ; Fri, 31 Jul 2026 10:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785519230; x=1786124030; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=OXBC54gDOgEf6948QYc+7NMHsIhOHZjkaHYeMetA6x8=; b=CQfjOLfjsLWberS6pngvlxSL4xKcz0E2CVF/ly/7Fgg4rrNZwxW3PFaRAme52U/lE0 1IlqPZCqu64RYPQPivIltkkknQZl0Hg+GUSFZmj93zJS1U4ZsY2HYD1EGGsuh5nMSyJs do8ZV5nUPQZroOKMQlTWA7sajf24H6FzeYfAgEZPOd+DnNssOEuf9ZdhXjuwDnJ57ItV suh2LXCKLVPnR038L2M65vNHhcIlbgfF4rmZMjWCM7yGVW1d9KuA2nQduCcwzvFrRN3m 9uQUECQoL9fcgroAMnBnw9Xq9m5+chhaj5UbFMz7y7+1ffbCuVB/+nRbMj5130uqvSdr qnaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785519230; x=1786124030; h=content-type: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:content-type; bh=OXBC54gDOgEf6948QYc+7NMHsIhOHZjkaHYeMetA6x8=; b=qOu6tKBHzPjDd4Nebi8+eg3A4kIh7kPlnFN9qrgckGgi8uIRAtKJTbTpYKDt/Gt0uN Iu4LC0Afhl9PyJ/QI8O2TAH00C/hMA5Ho6MoUEuslqhm+5Z3KSK0zthEdzmK8SJplAXM x6z077aK9ixovlU7xwCXV8ghFMz2iFACs5O8Vro9WwwQlDQOCgQtY4DoVzC5jfEl1m9L iRRHf0fhW1TuwlupcB/jneK7treh7+T7vFM4nExFvTn5s9ftZ8pmRh1yj66DyFdK5bTM 0x/tuLWi9Uu2u4GUkAXeNi2R1C5QtYqdP8imPXUnTu3F4KfjxVIm8Y2Nt7v5Mwbu5yay wBlA== X-Forwarded-Encrypted: i=1; AHgh+Rol0Xz/Mr8SRy22gKMLvNS3whrOrz6cdRez9EIMuG+D09PWOOJa8GDOIGKGZDsiW5vboFUOP4vdNrVEbes=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+8LeheJ1YnUOLiUsKy56ttsmDevNlNWQGShH6tolEUJLidlDk +B5E20XbVUDz3lmyIhlLYGWxzgLFyxRwE3yfrQ5bky1K9YoSYzi2M7n89JrND7cEK6eiBB7GeCH yAvFMww== X-Received: from pghm11.prod.google.com ([2002:a63:f60b:0:b0:c8a:604f:6851]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:93a1:b0:3c4:2cf9:2896 with SMTP id adf61e73a8af0-3c92a8af306mr633679637.45.1785519229414; Fri, 31 Jul 2026 10:33:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 31 Jul 2026 10:33:40 -0700 In-Reply-To: <20260731173340.2644656-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: <20260731173340.2644656-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.508.g3f0d502094-goog Message-ID: <20260731173340.2644656-7-seanjc@google.com> Subject: [PATCH v5 6/6] KVM: selftests: Extend the invalid nVMX guest state test to cover RSM From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hao Zhang , Hao Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hao Zhang Extend the invalid nVMX guest state to cover RSM, i.e. to validate that KVM synthesizes SHUTDOWN for L1 if SMRAM is clobbered with invalid guest state during an L2 =3D> SMI =3D> RSM =3D> L2 sequence. Note, unlike the existing testcase, clobbering SMRAM should result in L1, not L2, getting SHUTDOWN / TRIPLE_FAULT, as RSM is architecturally defined to trigger shutdown if the CPU detects invalid state. Signed-off-by: Hao Zhang Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../kvm/x86/vmx_invalid_nested_guest_state.c | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state= .c b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c index 4b1bb190c2c6..c8379124b317 100644 --- a/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +++ b/tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c @@ -2,6 +2,7 @@ #include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "smm.h" #include "vmx.h" =20 #include @@ -11,6 +12,22 @@ =20 #define ARBITRARY_IO_PORT 0x80 =20 +/* + * The 64-bit SMRAM state-save area starts at SMBASE + 0xfe00. TR starts = at + * offset 0xfe90, and attributes is the second 16-bit field in the descrip= tor. + */ +#define SMRAM64_TR_ATTRIBUTES_OFFSET 0xfe92 +#define SMRAM_GPA 0x1000000 + +/* + * SMI handler that runs in 16-bit Real Mode. Syncs with L0 via port I/O,= then + * executes RSM to trigger the consumption of invalid guest state. + */ +static u8 smi_handler[] =3D { + 0xe4, ARBITRARY_IO_PORT, /* IN $ARBITRARY_IO_PORT, %al */ + 0x0f, 0xaa, /* RSM */ +}; + static void l2_guest_code(void) { /* @@ -114,9 +131,45 @@ static void test_invalid_l2_guest_state(void) kvm_vm_free(vm); } =20 +static void test_invalid_l2_guest_state_rsm(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u16 *tr_attrs; + + if (!kvm_has_cap(KVM_CAP_X86_SMM)) + return; + + vm =3D vm_create_and_run_l2(&vcpu); + + /* + * Inject SMI while L2 is active, run the vCPU to get I/O exit from L1, + * then stuff TR in the SMRAM state-save area so that RSM restores + * invalid L2 state. + */ + setup_smram(vm, vcpu, SMRAM_GPA, smi_handler, sizeof(smi_handler)); + inject_smi(vcpu); + + vcpu_run_to_io(vcpu, false); + + /* Clear the present bit in SMRAM to make TR unusable. */ + tr_attrs =3D addr_gpa2hva(vm, SMRAM_GPA + SMRAM64_TR_ATTRIBUTES_OFFSET); + *tr_attrs &=3D ~BIT(7); + + vcpu_run(vcpu); + + /* + * For RSM, L1 gets the SHUTDOWN because RSM is architecturally defined + * to result in shutdown if the CPU detects invalid state in SMRAM. + */ + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN); + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); =20 test_invalid_l2_guest_state(); + test_invalid_l2_guest_state_rsm(); } --=20 2.55.0.508.g3f0d502094-goog