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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/kvm/x86/vmx_exception_with_invalid_guest_state.c
Extension
.c
Size
3615 bytes
Lines
143
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-only
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"

#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>

#include "kselftest.h"

static void guest_ud_handler(struct ex_regs *regs)
{
	/* Loop on the ud2 until guest state is made invalid. */
}

static void guest_code(void)
{
	asm volatile("ud2");
}

static void __run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
{
	struct kvm_run *run = vcpu->run;

	vcpu_run(vcpu);

	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
	TEST_ASSERT(run->emulation_failure.suberror == KVM_INTERNAL_ERROR_EMULATION,
		    "Expected emulation failure, got %d",
		    run->emulation_failure.suberror);
}

static void run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
{
	/*
	 * Always run twice to verify KVM handles the case where _KVM_ queues
	 * an exception with invalid state and then exits to userspace, i.e.
	 * that KVM doesn't explode if userspace ignores the initial error.
	 */
	__run_vcpu_with_invalid_state(vcpu);
	__run_vcpu_with_invalid_state(vcpu);
}

static void set_timer(void)
{
	struct itimerval timer;

	timer.it_value.tv_sec  = 0;
	timer.it_value.tv_usec = 200;
	timer.it_interval = timer.it_value;
	TEST_ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
}

static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
{
	static struct kvm_sregs sregs;

	if (!sregs.cr0)
		vcpu_sregs_get(vcpu, &sregs);
	sregs.tr.unusable = !!set;
	vcpu_sregs_set(vcpu, &sregs);
}

static void set_invalid_guest_state(struct kvm_vcpu *vcpu)
{
	set_or_clear_invalid_guest_state(vcpu, true);
}

static void clear_invalid_guest_state(struct kvm_vcpu *vcpu)
{
	set_or_clear_invalid_guest_state(vcpu, false);
}

static struct kvm_vcpu *get_set_sigalrm_vcpu(struct kvm_vcpu *__vcpu)
{
	static struct kvm_vcpu *vcpu = NULL;

	if (__vcpu)
		vcpu = __vcpu;
	return vcpu;
}

static void sigalrm_handler(int sig)
{
	struct kvm_vcpu *vcpu = get_set_sigalrm_vcpu(NULL);
	struct kvm_vcpu_events events;

	TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig);

Annotation

Implementation Notes