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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
test_util.hkvm_util.hprocessor.hsignal.hstring.hsys/ioctl.hsys/time.hkselftest.h
Detected Declarations
function guest_ud_handlerfunction __run_vcpu_with_invalid_statefunction run_vcpu_with_invalid_statefunction set_timerfunction set_or_clear_invalid_guest_statefunction set_invalid_guest_statefunction clear_invalid_guest_statefunction sigalrm_handlerfunction main
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
- Immediate include surface: `test_util.h`, `kvm_util.h`, `processor.h`, `signal.h`, `string.h`, `sys/ioctl.h`, `sys/time.h`, `kselftest.h`.
- Detected declarations: `function guest_ud_handler`, `function __run_vcpu_with_invalid_state`, `function run_vcpu_with_invalid_state`, `function set_timer`, `function set_or_clear_invalid_guest_state`, `function set_invalid_guest_state`, `function clear_invalid_guest_state`, `function sigalrm_handler`, `function main`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.