xref: /linux/tools/testing/selftests/kvm/x86_64/smm_test.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
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 
21 #define VCPU_ID	      1
22 
23 #define PAGE_SIZE  4096
24 
25 #define SMRAM_SIZE 65536
26 #define SMRAM_MEMSLOT ((1 << 16) | 1)
27 #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
28 #define SMRAM_GPA 0x1000000
29 #define SMRAM_STAGE 0xfe
30 
31 #define STR(x) #x
32 #define XSTR(s) STR(s)
33 
34 #define SYNC_PORT 0xe
35 #define DONE 0xff
36 
37 /*
38  * This is compiled as normal 64-bit code, however, SMI handler is executed
39  * in real-address mode. To stay simple we're limiting ourselves to a mode
40  * independent subset of asm here.
41  * SMI handler always report back fixed stage SMRAM_STAGE.
42  */
43 uint8_t smi_handler[] = {
44 	0xb0, SMRAM_STAGE,    /* mov $SMRAM_STAGE, %al */
45 	0xe4, SYNC_PORT,      /* in $SYNC_PORT, %al */
46 	0x0f, 0xaa,           /* rsm */
47 };
48 
49 void sync_with_host(uint64_t phase)
50 {
51 	asm volatile("in $" XSTR(SYNC_PORT)", %%al \n"
52 		     : : "a" (phase));
53 }
54 
55 void self_smi(void)
56 {
57 	wrmsr(APIC_BASE_MSR + (APIC_ICR >> 4),
58 	      APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
59 }
60 
61 void guest_code(struct vmx_pages *vmx_pages)
62 {
63 	uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
64 
65 	sync_with_host(1);
66 
67 	wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE);
68 
69 	sync_with_host(2);
70 
71 	self_smi();
72 
73 	sync_with_host(4);
74 
75 	if (vmx_pages) {
76 		GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
77 
78 		sync_with_host(5);
79 
80 		self_smi();
81 
82 		sync_with_host(7);
83 	}
84 
85 	sync_with_host(DONE);
86 }
87 
88 int main(int argc, char *argv[])
89 {
90 	vm_vaddr_t vmx_pages_gva = 0;
91 
92 	struct kvm_regs regs;
93 	struct kvm_vm *vm;
94 	struct kvm_run *run;
95 	struct kvm_x86_state *state;
96 	int stage, stage_reported;
97 
98 	/* Create VM */
99 	vm = vm_create_default(VCPU_ID, 0, guest_code);
100 
101 	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
102 
103 	run = vcpu_state(vm, VCPU_ID);
104 
105 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
106 				    SMRAM_MEMSLOT, SMRAM_PAGES, 0);
107 	TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
108 		    == SMRAM_GPA, "could not allocate guest physical addresses?");
109 
110 	memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
111 	memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
112 	       sizeof(smi_handler));
113 
114 	vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
115 
116 	if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
117 		vcpu_alloc_vmx(vm, &vmx_pages_gva);
118 		vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
119 	} else {
120 		printf("will skip SMM test with VMX enabled\n");
121 		vcpu_args_set(vm, VCPU_ID, 1, 0);
122 	}
123 
124 	for (stage = 1;; stage++) {
125 		_vcpu_run(vm, VCPU_ID);
126 		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
127 			    "Stage %d: unexpected exit reason: %u (%s),\n",
128 			    stage, run->exit_reason,
129 			    exit_reason_str(run->exit_reason));
130 
131 		memset(&regs, 0, sizeof(regs));
132 		vcpu_regs_get(vm, VCPU_ID, &regs);
133 
134 		stage_reported = regs.rax & 0xff;
135 
136 		if (stage_reported == DONE)
137 			goto done;
138 
139 		TEST_ASSERT(stage_reported == stage ||
140 			    stage_reported == SMRAM_STAGE,
141 			    "Unexpected stage: #%x, got %x",
142 			    stage, stage_reported);
143 
144 		state = vcpu_save_state(vm, VCPU_ID);
145 		kvm_vm_release(vm);
146 		kvm_vm_restart(vm, O_RDWR);
147 		vm_vcpu_add(vm, VCPU_ID, 0, 0);
148 		vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
149 		vcpu_load_state(vm, VCPU_ID, state);
150 		run = vcpu_state(vm, VCPU_ID);
151 		free(state);
152 	}
153 
154 done:
155 	kvm_vm_free(vm);
156 }
157