#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "test_util.h"
#include "kvm_util.h"
#include "smm.h"
#include "hyperv.h"
#include "vmx.h"
#define SMRAM_GPA 0x1000000
#define SMRAM_STAGE 0xfe
#define SYNC_PORT 0xe
#define STR(x) #x
#define XSTR(s) STR(s)
static uint8_t smi_handler[] = {
0xb0, SMRAM_STAGE,
0xe4, SYNC_PORT,
0x0f, 0xaa,
};
static inline void sync_with_host(uint64_t phase)
{
asm volatile("in $" XSTR(SYNC_PORT) ", %%al \n"
: "+a" (phase));
}
static void l2_guest_code(void)
{
sync_with_host(1);
vmcall();
}
static void guest_code(struct vmx_pages *vmx_pages,
struct hyperv_test_pages *hv_pages)
{
#define L2_GUEST_STACK_SIZE 64
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
enable_vp_assist(hv_pages->vp_assist_gpa, hv_pages->vp_assist);
evmcs_enable();
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
GUEST_ASSERT(load_evmcs(hv_pages));
prepare_vmcs(vmx_pages, l2_guest_code,
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
GUEST_ASSERT(!vmlaunch());
sync_with_host(2);
}
int main(int argc, char *argv[])
{
vm_vaddr_t vmx_pages_gva = 0, hv_pages_gva = 0;
struct hyperv_test_pages *hv;
struct hv_enlightened_vmcs *evmcs;
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;
struct kvm_regs regs;
int stage_reported;
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS));
TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_SMM));
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
setup_smram(vm, vcpu, SMRAM_GPA, smi_handler, sizeof(smi_handler));
vcpu_set_hv_cpuid(vcpu);
vcpu_enable_evmcs(vcpu);
vcpu_alloc_vmx(vm, &vmx_pages_gva);
hv = vcpu_alloc_hyperv_test_pages(vm, &hv_pages_gva);
vcpu_args_set(vcpu, 2, vmx_pages_gva, hv_pages_gva);
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
vcpu_regs_get(vcpu, ®s);
stage_reported = regs.rax & 0xff;
TEST_ASSERT(stage_reported == 1,
"Expected stage 1, got %d", stage_reported);
inject_smi(vcpu);
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
vcpu_regs_get(vcpu, ®s);
stage_reported = regs.rax & 0xff;
TEST_ASSERT(stage_reported == SMRAM_STAGE,
"Expected SMM handler stage %#x, got %#x",
SMRAM_STAGE, stage_reported);
evmcs = hv->enlightened_vmcs_hva;
evmcs->pin_based_vm_exec_control |= PIN_BASED_VIRTUAL_NMIS;
evmcs->hv_clean_fields = 0;
union {
struct kvm_nested_state state;
char state_[16384];
} nested_state_buf;
memset(&nested_state_buf, 0, sizeof(nested_state_buf));
nested_state_buf.state.size = sizeof(nested_state_buf);
vcpu_nested_state_get(vcpu, &nested_state_buf.state);
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);
kvm_vm_free(vm);
return 0;
}