tools/testing/selftests/kvm/x86/evmcs_smm_controls_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/kvm/x86/evmcs_smm_controls_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/x86/evmcs_smm_controls_test.c
Extension
.c
Size
4043 bytes
Lines
151
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2026, Red Hat, Inc.
 *
 * Test that vmx_leave_smm() validates vmcs12 controls before re-entering
 * nested guest mode on RSM.
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#include "test_util.h"
#include "kvm_util.h"
#include "smm.h"
#include "hyperv.h"
#include "vmx.h"

#define SMRAM_GPA	0x1000000
#define SMRAM_STAGE	0xfe

#define SYNC_PORT	0xe

#define STR(x) #x
#define XSTR(s) STR(s)

/*
 * SMI handler: runs in real-address mode.
 * Reports SMRAM_STAGE via port IO, then does RSM.
 */
static u8 smi_handler[] = {
	0xb0, SMRAM_STAGE,    /* mov $SMRAM_STAGE, %al */
	0xe4, SYNC_PORT,      /* in $SYNC_PORT, %al */
	0x0f, 0xaa,           /* rsm */
};

static inline void sync_with_host(u64 phase)
{
	asm volatile("in $" XSTR(SYNC_PORT) ", %%al \n"
		     : "+a" (phase));
}

static void l2_guest_code(void)
{
	sync_with_host(1);

	/* After SMI+RSM with invalid controls, we should not reach here. */
	vmcall();
}

static void guest_code(struct vmx_pages *vmx_pages,
		       struct hyperv_test_pages *hv_pages)
{
#define L2_GUEST_STACK_SIZE 64
	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];

	/* Set up Hyper-V enlightenments and eVMCS */
	wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
	enable_vp_assist(hv_pages->vp_assist_gpa, hv_pages->vp_assist);
	evmcs_enable();

	GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
	GUEST_ASSERT(load_evmcs(hv_pages));
	prepare_vmcs(vmx_pages, l2_guest_code,
		     &l2_guest_stack[L2_GUEST_STACK_SIZE]);

	GUEST_ASSERT(!vmlaunch());

	/* L2 exits via vmcall if test fails */
	sync_with_host(2);
}

int main(int argc, char *argv[])
{
	gva_t vmx_pages_gva = 0, hv_pages_gva = 0;
	struct hyperv_test_pages *hv;
	struct hv_enlightened_vmcs *evmcs;
	struct kvm_vcpu *vcpu;
	struct kvm_vm *vm;
	struct kvm_regs regs;
	int stage_reported;

	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
	TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS));
	TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_SMM));

	vm = vm_create_with_one_vcpu(&vcpu, guest_code);

Annotation

Implementation Notes