1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018, Red Hat, Inc. 4 * 5 * Tests for SMM. 6 */ 7 #define _GNU_SOURCE /* for program_invocation_short_name */ 8 #include <fcntl.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <stdint.h> 12 #include <string.h> 13 #include <sys/ioctl.h> 14 15 #include "test_util.h" 16 17 #include "kvm_util.h" 18 19 #include "vmx.h" 20 #include "svm_util.h" 21 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 27 28 #define STR(x) #x 29 #define XSTR(s) STR(s) 30 31 #define SYNC_PORT 0xe 32 #define DONE 0xff 33 34 /* 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. 39 */ 40 uint8_t smi_handler[] = { 41 0xb0, SMRAM_STAGE, /* mov $SMRAM_STAGE, %al */ 42 0xe4, SYNC_PORT, /* in $SYNC_PORT, %al */ 43 0x0f, 0xaa, /* rsm */ 44 }; 45 46 static inline void sync_with_host(uint64_t phase) 47 { 48 asm volatile("in $" XSTR(SYNC_PORT)", %%al \n" 49 : "+a" (phase)); 50 } 51 52 static void self_smi(void) 53 { 54 x2apic_write_reg(APIC_ICR, 55 APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI); 56 } 57 58 static void l2_guest_code(void) 59 { 60 sync_with_host(8); 61 62 sync_with_host(10); 63 64 vmcall(); 65 } 66 67 static void guest_code(void *arg) 68 { 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; 74 75 sync_with_host(1); 76 77 wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE); 78 79 sync_with_host(2); 80 81 self_smi(); 82 83 sync_with_host(4); 84 85 if (arg) { 86 if (this_cpu_has(X86_FEATURE_SVM)) { 87 generic_svm_setup(svm, l2_guest_code, 88 &l2_guest_stack[L2_GUEST_STACK_SIZE]); 89 } else { 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]); 94 } 95 96 sync_with_host(5); 97 98 self_smi(); 99 100 sync_with_host(7); 101 102 if (this_cpu_has(X86_FEATURE_SVM)) { 103 run_guest(svm->vmcb, svm->vmcb_gpa); 104 run_guest(svm->vmcb, svm->vmcb_gpa); 105 } else { 106 vmlaunch(); 107 vmresume(); 108 } 109 110 /* Stages 8-11 are eaten by SMM (SMRAM_STAGE reported instead) */ 111 sync_with_host(12); 112 } 113 114 sync_with_host(DONE); 115 } 116 117 void inject_smi(struct kvm_vcpu *vcpu) 118 { 119 struct kvm_vcpu_events events; 120 121 vcpu_events_get(vcpu, &events); 122 123 events.smi.pending = 1; 124 events.flags |= KVM_VCPUEVENT_VALID_SMM; 125 126 vcpu_events_set(vcpu, &events); 127 } 128 129 int main(int argc, char *argv[]) 130 { 131 vm_vaddr_t nested_gva = 0; 132 133 struct kvm_vcpu *vcpu; 134 struct kvm_regs regs; 135 struct kvm_vm *vm; 136 struct kvm_run *run; 137 struct kvm_x86_state *state; 138 int stage, stage_reported; 139 140 /* Create VM */ 141 vm = vm_create_with_one_vcpu(&vcpu, guest_code); 142 143 run = vcpu->run; 144 145 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA, 146 SMRAM_MEMSLOT, SMRAM_PAGES, 0); 147 TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT) 148 == SMRAM_GPA, "could not allocate guest physical addresses?"); 149 150 memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE); 151 memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler, 152 sizeof(smi_handler)); 153 154 vcpu_set_msr(vcpu, MSR_IA32_SMBASE, SMRAM_GPA); 155 156 if (kvm_has_cap(KVM_CAP_NESTED_STATE)) { 157 if (kvm_cpu_has(X86_FEATURE_SVM)) 158 vcpu_alloc_svm(vm, &nested_gva); 159 else if (kvm_cpu_has(X86_FEATURE_VMX)) 160 vcpu_alloc_vmx(vm, &nested_gva); 161 } 162 163 if (!nested_gva) 164 pr_info("will skip SMM test with VMX enabled\n"); 165 166 vcpu_args_set(vcpu, 1, nested_gva); 167 168 for (stage = 1;; stage++) { 169 vcpu_run(vcpu); 170 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, 171 "Stage %d: unexpected exit reason: %u (%s),\n", 172 stage, run->exit_reason, 173 exit_reason_str(run->exit_reason)); 174 175 memset(®s, 0, sizeof(regs)); 176 vcpu_regs_get(vcpu, ®s); 177 178 stage_reported = regs.rax & 0xff; 179 180 if (stage_reported == DONE) 181 goto done; 182 183 TEST_ASSERT(stage_reported == stage || 184 stage_reported == SMRAM_STAGE, 185 "Unexpected stage: #%x, got %x", 186 stage, stage_reported); 187 188 /* 189 * Enter SMM during L2 execution and check that we correctly 190 * return from it. Do not perform save/restore while in SMM yet. 191 */ 192 if (stage == 8) { 193 inject_smi(vcpu); 194 continue; 195 } 196 197 /* 198 * Perform save/restore while the guest is in SMM triggered 199 * during L2 execution. 200 */ 201 if (stage == 10) 202 inject_smi(vcpu); 203 204 state = vcpu_save_state(vcpu); 205 kvm_vm_release(vm); 206 207 vcpu = vm_recreate_with_one_vcpu(vm); 208 vcpu_load_state(vcpu, state); 209 run = vcpu->run; 210 kvm_x86_state_cleanup(state); 211 } 212 213 done: 214 kvm_vm_free(vm); 215 } 216