#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"
#include "cpuid_guest_state.h"
static const struct vcpu_cpuid_entry test_entries[] = {
{
.vce_function = 0,
.vce_eax = TEST_CPUID_0_EAX,
.vce_ebx = TEST_CPUID_0_EBX,
.vce_ecx = TEST_CPUID_0_ECX,
.vce_edx = TEST_CPUID_0_EDX,
},
{
.vce_function = 1,
.vce_eax = TEST_CPUID_1_EAX,
.vce_ebx = TEST_CPUID_1_EBX,
.vce_ecx = TEST_CPUID_1_ECX,
.vce_edx = TEST_CPUID_1_EDX,
},
{
.vce_function = 0xD,
.vce_index = 0,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = TEST_CPUID_D_0_EAX,
.vce_ebx = 0,
.vce_ecx = XSAVE_AREA_SIZE_MAX,
.vce_edx = 0,
},
{
.vce_function = 0xD,
.vce_index = 1,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = TEST_CPUID_D_1_EAX,
.vce_ebx = 0,
.vce_ecx = 0,
.vce_edx = 0,
}
};
static const struct vcpu_cpuid_entry fallback_test_entries[] = {
{
.vce_function = 0,
.vce_eax = TEST_CPUID_0_EAX,
.vce_ebx = TEST_CPUID_0_EBX,
.vce_ecx = TEST_CPUID_0_ECX,
.vce_edx = TEST_CPUID_0_EDX,
},
{
.vce_function = 0xD,
.vce_index = 0,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = TEST_CPUID_D_0_EAX,
.vce_ebx = 0,
.vce_ecx = XSAVE_AREA_SIZE_MAX,
.vce_edx = 0,
},
{
.vce_function = 0xD,
.vce_index = 1,
.vce_flags = VCE_FLAG_MATCH_INDEX,
.vce_eax = 0x0000000F,
.vce_ebx = 0,
.vce_ecx = 0,
.vce_edx = 0,
}
};
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");
}
struct vm_entry ventry = { 0 };
struct vm_exit vexit = { 0 };
enum vm_exit_kind kind;
int vmfd = vm_get_device_fd(ctx);
struct {
char *name;
bool pass_expected;
} phases[4] = {
{"legacy emulation mode", false},
{"explicit mode", false},
{"explicit mode w/Intel fallback enabled", false},
{"fallback variations", true}
};
for (int i = 0; i < ARRAY_SIZE(phases); i++) {
kind = test_run_vcpu(vcpu, &ventry, &vexit);
switch (kind) {
case VEK_REENTR:
test_fail_msg("unexpected exit in %s", phases[i].name);
goto done;
case VEK_TEST_PASS:
if (phases[i].pass_expected) {
test_pass();
} else {
test_fail_msg("unexpected pass from %s",
phases[i].name);
}
goto done;
case VEK_TEST_FAIL:
test_fail_msg("failed result in %s, %rip: %x",
phases[i].name, vexit.rip);
goto done;
case VEK_UNHANDLED: {
uint32_t finished_phase;
if (!vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
&finished_phase)) {
test_fail_vmexit(&vexit);
goto done;
}
if (finished_phase != i) {
test_fail_vmexit(&vexit);
goto done;
}
break;
}
default:
test_fail_vmexit(&vexit);
break;
}
struct vm_vcpu_cpuid_config cfg = {
.vvcc_vcpuid = 0,
.vvcc_flags = 0,
.vvcc_nent = ARRAY_SIZE(test_entries),
.vvcc_entries = (struct vcpu_cpuid_entry *)test_entries,
};
switch (i) {
case 0:
break;
case 1:
cfg.vvcc_flags = VCC_FLAG_INTEL_FALLBACK;
break;
case 2:
cfg.vvcc_flags = VCC_FLAG_INTEL_FALLBACK;
cfg.vvcc_nent = ARRAY_SIZE(fallback_test_entries);
cfg.vvcc_entries =
(struct vcpu_cpuid_entry *)fallback_test_entries;
break;
default:
test_fail_msg("phase %d fell through without passing",
i);
goto done;
}
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);
}
done:
return (0);
}