WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / kvm / x86_64 / smm_test.c
blob613c42c5a9b8d3ad1dc4003320556f1d44704652
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018, Red Hat, Inc.
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>
15 #include "test_util.h"
17 #include "kvm_util.h"
19 #include "vmx.h"
20 #include "svm_util.h"
22 #define VCPU_ID 1
24 #define PAGE_SIZE 4096
26 #define SMRAM_SIZE 65536
27 #define SMRAM_MEMSLOT ((1 << 16) | 1)
28 #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
29 #define SMRAM_GPA 0x1000000
30 #define SMRAM_STAGE 0xfe
32 #define STR(x) #x
33 #define XSTR(s) STR(s)
35 #define SYNC_PORT 0xe
36 #define DONE 0xff
39 * This is compiled as normal 64-bit code, however, SMI handler is executed
40 * in real-address mode. To stay simple we're limiting ourselves to a mode
41 * independent subset of asm here.
42 * SMI handler always report back fixed stage SMRAM_STAGE.
44 uint8_t smi_handler[] = {
45 0xb0, SMRAM_STAGE, /* mov $SMRAM_STAGE, %al */
46 0xe4, SYNC_PORT, /* in $SYNC_PORT, %al */
47 0x0f, 0xaa, /* rsm */
50 static inline void sync_with_host(uint64_t phase)
52 asm volatile("in $" XSTR(SYNC_PORT)", %%al \n"
53 : "+a" (phase));
56 void self_smi(void)
58 wrmsr(APIC_BASE_MSR + (APIC_ICR >> 4),
59 APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
62 void guest_code(void *arg)
64 uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
66 sync_with_host(1);
68 wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE);
70 sync_with_host(2);
72 self_smi();
74 sync_with_host(4);
76 if (arg) {
77 if (cpu_has_svm())
78 generic_svm_setup(arg, NULL, NULL);
79 else
80 GUEST_ASSERT(prepare_for_vmx_operation(arg));
82 sync_with_host(5);
84 self_smi();
86 sync_with_host(7);
89 sync_with_host(DONE);
92 int main(int argc, char *argv[])
94 vm_vaddr_t nested_gva = 0;
96 struct kvm_regs regs;
97 struct kvm_vm *vm;
98 struct kvm_run *run;
99 struct kvm_x86_state *state;
100 int stage, stage_reported;
102 /* Create VM */
103 vm = vm_create_default(VCPU_ID, 0, guest_code);
105 run = vcpu_state(vm, VCPU_ID);
107 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
108 SMRAM_MEMSLOT, SMRAM_PAGES, 0);
109 TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
110 == SMRAM_GPA, "could not allocate guest physical addresses?");
112 memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
113 memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
114 sizeof(smi_handler));
116 vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
118 if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
119 if (nested_svm_supported())
120 vcpu_alloc_svm(vm, &nested_gva);
121 else if (nested_vmx_supported())
122 vcpu_alloc_vmx(vm, &nested_gva);
125 if (!nested_gva)
126 pr_info("will skip SMM test with VMX enabled\n");
128 vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
130 for (stage = 1;; stage++) {
131 _vcpu_run(vm, VCPU_ID);
132 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
133 "Stage %d: unexpected exit reason: %u (%s),\n",
134 stage, run->exit_reason,
135 exit_reason_str(run->exit_reason));
137 memset(&regs, 0, sizeof(regs));
138 vcpu_regs_get(vm, VCPU_ID, &regs);
140 stage_reported = regs.rax & 0xff;
142 if (stage_reported == DONE)
143 goto done;
145 TEST_ASSERT(stage_reported == stage ||
146 stage_reported == SMRAM_STAGE,
147 "Unexpected stage: #%x, got %x",
148 stage, stage_reported);
150 state = vcpu_save_state(vm, VCPU_ID);
151 kvm_vm_release(vm);
152 kvm_vm_restart(vm, O_RDWR);
153 vm_vcpu_add(vm, VCPU_ID);
154 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
155 vcpu_load_state(vm, VCPU_ID, state);
156 run = vcpu_state(vm, VCPU_ID);
157 free(state);
160 done:
161 kvm_vm_free(vm);