1
2
3
4
5
6
7#define _GNU_SOURCE
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 VCPU_ID 1
23
24#define PAGE_SIZE 4096
25
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
31
32#define STR(x) #x
33#define XSTR(s) STR(s)
34
35#define SYNC_PORT 0xe
36#define DONE 0xff
37
38
39
40
41
42
43
44uint8_t smi_handler[] = {
45 0xb0, SMRAM_STAGE,
46 0xe4, SYNC_PORT,
47 0x0f, 0xaa,
48};
49
50static inline void sync_with_host(uint64_t phase)
51{
52 asm volatile("in $" XSTR(SYNC_PORT)", %%al \n"
53 : "+a" (phase));
54}
55
56void self_smi(void)
57{
58 wrmsr(APIC_BASE_MSR + (APIC_ICR >> 4),
59 APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
60}
61
62void guest_code(void *arg)
63{
64 uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
65
66 sync_with_host(1);
67
68 wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE);
69
70 sync_with_host(2);
71
72 self_smi();
73
74 sync_with_host(4);
75
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));
81
82 sync_with_host(5);
83
84 self_smi();
85
86 sync_with_host(7);
87 }
88
89 sync_with_host(DONE);
90}
91
92int main(int argc, char *argv[])
93{
94 vm_vaddr_t nested_gva = 0;
95
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;
101
102
103 vm = vm_create_default(VCPU_ID, 0, guest_code);
104
105 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
106
107 run = vcpu_state(vm, VCPU_ID);
108
109 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
110 SMRAM_MEMSLOT, SMRAM_PAGES, 0);
111 TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
112 == SMRAM_GPA, "could not allocate guest physical addresses?");
113
114 memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
115 memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
116 sizeof(smi_handler));
117
118 vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
119
120 if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
121 if (nested_svm_supported())
122 vcpu_alloc_svm(vm, &nested_gva);
123 else if (nested_vmx_supported())
124 vcpu_alloc_vmx(vm, &nested_gva);
125 }
126
127 if (!nested_gva)
128 pr_info("will skip SMM test with VMX enabled\n");
129
130 vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
131
132 for (stage = 1;; stage++) {
133 _vcpu_run(vm, VCPU_ID);
134 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
135 "Stage %d: unexpected exit reason: %u (%s),\n",
136 stage, run->exit_reason,
137 exit_reason_str(run->exit_reason));
138
139 memset(®s, 0, sizeof(regs));
140 vcpu_regs_get(vm, VCPU_ID, ®s);
141
142 stage_reported = regs.rax & 0xff;
143
144 if (stage_reported == DONE)
145 goto done;
146
147 TEST_ASSERT(stage_reported == stage ||
148 stage_reported == SMRAM_STAGE,
149 "Unexpected stage: #%x, got %x",
150 stage, stage_reported);
151
152 state = vcpu_save_state(vm, VCPU_ID);
153 kvm_vm_release(vm);
154 kvm_vm_restart(vm, O_RDWR);
155 vm_vcpu_add(vm, VCPU_ID);
156 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
157 vcpu_load_state(vm, VCPU_ID, state);
158 run = vcpu_state(vm, VCPU_ID);
159 free(state);
160 }
161
162done:
163 kvm_vm_free(vm);
164}
165