#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <libgen.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/debug.h>
#include <sys/vmm.h>
#include <sys/vmm_dev.h>
#include <vmmapi.h>
#include "in_guest.h"
static const struct vcpu_cpuid_entry test_entries[] = {
{
.vce_function = 0,
.vce_eax = 5,
.vce_ebx = 0x74737552,
.vce_edx = 0x4f206465,
.vce_ecx = 0x65646978,
},
{
.vce_function = 1,
.vce_eax = 0x100,
},
{
.vce_function = 3,
.vce_index = 0,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x300,
},
{
.vce_function = 3,
.vce_index = 1,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x301,
},
{
.vce_function = 4,
.vce_index = 0,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x400,
},
{
.vce_function = 4,
.vce_index = 2,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x402,
},
{
.vce_function = 5,
.vce_eax = 5,
.vce_ebx = 5,
.vce_edx = 5,
.vce_ecx = 5,
},
{
.vce_function = 0x80000000,
.vce_eax = 0x80000001,
},
{
.vce_function = 0x80000001,
.vce_index = 0x0,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x8000,
},
{
.vce_function = 0x80000001,
.vce_index = 0x1,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x8001,
},
};
int
main(int argc, char *argv[])
{
const char *test_suite_name = basename(argv[0]);
struct vmctx *ctx = NULL;
struct vcpu *vcpu;
int err;
ctx = test_initialize(test_suite_name);
if ((vcpu = vm_vcpu_open(ctx, 0)) == NULL) {
test_fail_errno(errno, "Could not open vcpu0");
}
err = test_setup_vcpu(vcpu, MEM_LOC_PAYLOAD, MEM_LOC_STACK);
if (err != 0) {
test_fail_errno(err, "Could not initialize vcpu0");
}
int vmfd = vm_get_device_fd(ctx);
struct vm_vcpu_cpuid_config cfg = {
.vvcc_vcpuid = 0,
.vvcc_flags = VCC_FLAG_INTEL_FALLBACK,
.vvcc_nent = ARRAY_SIZE(test_entries),
.vvcc_entries = (struct vcpu_cpuid_entry *)test_entries,
};
err = ioctl(vmfd, VM_SET_CPUID, &cfg);
if (err != 0) {
test_fail_errno(err, "ioctl(VM_SET_CPUID) failed");
}
struct vm_entry ventry = { 0 };
struct vm_exit vexit = { 0 };
do {
const enum vm_exit_kind kind =
test_run_vcpu(vcpu, &ventry, &vexit);
switch (kind) {
case VEK_REENTR:
break;
case VEK_TEST_PASS:
test_pass();
break;
case VEK_TEST_FAIL:
test_fail_msg("failed result %rip: %x", vexit.rip);
break;
case VEK_UNHANDLED: {
uint32_t val;
if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
&val)) {
cfg.vvcc_flags = 0;
err = ioctl(vmfd, VM_SET_CPUID, &cfg);
if (err != 0) {
test_fail_errno(err,
"ioctl(VM_SET_CPUID) failed");
}
ventry_fulfill_inout(&vexit, &ventry, 0);
} else {
test_fail_vmexit(&vexit);
}
break;
}
default:
test_fail_vmexit(&vexit);
break;
}
} while (true);
}