mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hao Zhang <hao_zhang_kdev@163.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Hao Zhang <zhanghao1@kylinos.cn>
Subject: Re: [PATCH v4 6/6] KVM: selftests: Extend the invalid nVMX guest state test to cover RSM
Date: Tue, 28 Jul 2026 11:01:57 +0800	[thread overview]
Message-ID: <amgbpV4CW9livlfU@192.168.1.215> (raw)
In-Reply-To: <20260728004351.887076-7-seanjc@google.com>

On Mon, Jul 27, 2026, Sean Christopherson wrote:
> From: Hao Zhang <hao_zhang_kdev@163.com>
> 
> 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 => SMI => RSM => 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.
> 
> Not-yet-signed-off-by: Hao Zhang <hao_zhang_kdev@163.com>
> Co-developed-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>  .../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 ab00265d6c94..6f546deb193a 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"
>  
>  #include <string.h>
> @@ -11,6 +12,22 @@
>  
>  #define ARBITRARY_IO_PORT 0x80
>  
> +/*
> + * 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 descriptor.
> + */
> +#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[] = {
> +	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);
>  }
>  
> +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 = 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 = addr_gpa2hva(vm, SMRAM_GPA + SMRAM64_TR_ATTRIBUTES_OFFSET);
> +	*tr_attrs &= ~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));
>  
>  	test_invalid_l2_guest_state();
> +	test_invalid_l2_guest_state_rsm();
>  }
> -- 
> 2.55.0.229.g6434b31f56-goog
>

Yes, please feel free to change my Not-yet-Signed-off-by to:
  
  Signed-off-by: Hao Zhang <zhanghao1@kylinos.cn>

Thanks,
Hao


      reply	other threads:[~2026-07-28  3:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28  0:43 [PATCH v4 0/6] KVM: nVMX: Synthesize SHUTDOWN on RSM with bad state Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 1/6] KVM: x86: Extract VMX's unhandleable emulation check to common x86 Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 2/6] KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 3/6] KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into .vcpu_needs_initialization() Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 4/6] KVM: selftests: Use port 0x80 in invalid nVMX guest state test Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 5/6] KVM: selftests: Refactor invalid nVMX state test to prepare for RSM testcase Sean Christopherson
2026-07-28  0:43 ` [PATCH v4 6/6] KVM: selftests: Extend the invalid nVMX guest state test to cover RSM Sean Christopherson
2026-07-28  3:01   ` Hao Zhang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=amgbpV4CW9livlfU@192.168.1.215 \
    --to=hao_zhang_kdev@163.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=zhanghao1@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®