#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <errno.h>
#include <err.h>
#include <sys/vmm_data.h>
#include <sys/vmm_dev.h>
#include <vmmapi.h>
#include "in_guest.h"
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");
}
const int vmfd = vm_get_device_fd(ctx);
struct vdi_time_info_v1 time_info;
struct vm_data_xfer xfer = {
.vdx_class = VDC_VMM_TIME,
.vdx_version = 1,
.vdx_len = sizeof (struct vdi_time_info_v1),
.vdx_data = &time_info,
};
if (ioctl(vmfd, VM_DATA_READ, &xfer) != 0) {
errx(EXIT_FAILURE, "VMM_DATA_READ of time info failed");
}
uint64_t expect_tsc = 500000000000;
time_info.vt_guest_tsc = expect_tsc;
if (ioctl(vmfd, VM_DATA_WRITE, &xfer) != 0) {
int error;
error = errno;
if (error == EPERM) {
warn("VMM_DATA_WRITE got EPERM: is "
"vmm_allow_state_writes set?");
}
errx(EXIT_FAILURE, "VMM_DATA_WRITE of time info failed");
}
struct vm_entry ventry = { 0 };
struct vm_exit vexit = { 0 };
bool half_read = false;
uint64_t tsc;
do {
const enum vm_exit_kind kind =
test_run_vcpu(vcpu, &ventry, &vexit);
if (kind == VEK_REENTR) {
continue;
} else if (kind != VEK_UNHANDLED) {
test_fail_vmexit(&vexit);
}
uint32_t val;
if (vexit_match_inout(&vexit, false, IOP_TEST_VALUE, 4,
&val)) {
if (!half_read) {
tsc = val;
half_read = true;
ventry_fulfill_inout(&vexit, &ventry, 0);
} else {
tsc |= ((uint64_t)val << 32);
if (tsc >= expect_tsc) {
(void) printf("tsc=%ld\n", tsc);
test_pass();
} else {
test_fail_msg("TSC %lu < %lu\n", tsc,
expect_tsc);
}
}
} else {
test_fail_vmexit(&vexit);
}
} while (true);
}