1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018, Red Hat, Inc.
7 #define _GNU_SOURCE /* for program_invocation_short_name */
13 #include <sys/ioctl.h>
15 #include "test_util.h"
22 #define SMRAM_SIZE 65536
23 #define SMRAM_MEMSLOT ((1 << 16) | 1)
24 #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
25 #define SMRAM_GPA 0x1000000
26 #define SMRAM_STAGE 0xfe
29 #define XSTR(s) STR(s)
35 * This is compiled as normal 64-bit code, however, SMI handler is executed
36 * in real-address mode. To stay simple we're limiting ourselves to a mode
37 * independent subset of asm here.
38 * SMI handler always report back fixed stage SMRAM_STAGE.
40 uint8_t smi_handler[] = {
41 0xb0, SMRAM_STAGE, /* mov $SMRAM_STAGE, %al */
42 0xe4, SYNC_PORT, /* in $SYNC_PORT, %al */
46 static inline void sync_with_host(uint64_t phase)
48 asm volatile("in $" XSTR(SYNC_PORT)", %%al \n"
52 static void self_smi(void)
54 x2apic_write_reg(APIC_ICR,
55 APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
58 static void l2_guest_code(void)
67 static void guest_code(void *arg)
69 #define L2_GUEST_STACK_SIZE 64
70 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
71 uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
72 struct svm_test_data *svm = arg;
73 struct vmx_pages *vmx_pages = arg;
77 wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE);
86 if (this_cpu_has(X86_FEATURE_SVM)) {
87 generic_svm_setup(svm, l2_guest_code,
88 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
90 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
91 GUEST_ASSERT(load_vmcs(vmx_pages));
92 prepare_vmcs(vmx_pages, l2_guest_code,
93 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
102 if (this_cpu_has(X86_FEATURE_SVM)) {
103 run_guest(svm->vmcb, svm->vmcb_gpa);
104 run_guest(svm->vmcb, svm->vmcb_gpa);
110 /* Stages 8-11 are eaten by SMM (SMRAM_STAGE reported instead) */
114 sync_with_host(DONE);
117 void inject_smi(struct kvm_vcpu *vcpu)
119 struct kvm_vcpu_events events;
121 vcpu_events_get(vcpu, &events);
123 events.smi.pending = 1;
124 events.flags |= KVM_VCPUEVENT_VALID_SMM;
126 vcpu_events_set(vcpu, &events);
129 int main(int argc, char *argv[])
131 vm_vaddr_t nested_gva = 0;
133 struct kvm_vcpu *vcpu;
134 struct kvm_regs regs;
137 struct kvm_x86_state *state;
138 int stage, stage_reported;
140 TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_SMM));
143 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
147 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
148 SMRAM_MEMSLOT, SMRAM_PAGES, 0);
149 TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
150 == SMRAM_GPA, "could not allocate guest physical addresses?");
152 memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
153 memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
154 sizeof(smi_handler));
156 vcpu_set_msr(vcpu, MSR_IA32_SMBASE, SMRAM_GPA);
158 if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
159 if (kvm_cpu_has(X86_FEATURE_SVM))
160 vcpu_alloc_svm(vm, &nested_gva);
161 else if (kvm_cpu_has(X86_FEATURE_VMX))
162 vcpu_alloc_vmx(vm, &nested_gva);
166 pr_info("will skip SMM test with VMX enabled\n");
168 vcpu_args_set(vcpu, 1, nested_gva);
170 for (stage = 1;; stage++) {
172 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
173 "Stage %d: unexpected exit reason: %u (%s),\n",
174 stage, run->exit_reason,
175 exit_reason_str(run->exit_reason));
177 memset(®s, 0, sizeof(regs));
178 vcpu_regs_get(vcpu, ®s);
180 stage_reported = regs.rax & 0xff;
182 if (stage_reported == DONE)
185 TEST_ASSERT(stage_reported == stage ||
186 stage_reported == SMRAM_STAGE,
187 "Unexpected stage: #%x, got %x",
188 stage, stage_reported);
191 * Enter SMM during L2 execution and check that we correctly
192 * return from it. Do not perform save/restore while in SMM yet.
200 * Perform save/restore while the guest is in SMM triggered
201 * during L2 execution.
206 state = vcpu_save_state(vcpu);
209 vcpu = vm_recreate_with_one_vcpu(vm);
210 vcpu_load_state(vcpu, state);
212 kvm_x86_state_cleanup(state);