xref: /openbmc/linux/tools/testing/selftests/kvm/x86_64/smm_test.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
179904c9dSVitaly Kuznetsov // SPDX-License-Identifier: GPL-2.0
279904c9dSVitaly Kuznetsov /*
379904c9dSVitaly Kuznetsov  * Copyright (C) 2018, Red Hat, Inc.
479904c9dSVitaly Kuznetsov  *
579904c9dSVitaly Kuznetsov  * Tests for SMM.
679904c9dSVitaly Kuznetsov  */
779904c9dSVitaly Kuznetsov #define _GNU_SOURCE /* for program_invocation_short_name */
879904c9dSVitaly Kuznetsov #include <fcntl.h>
979904c9dSVitaly Kuznetsov #include <stdio.h>
1079904c9dSVitaly Kuznetsov #include <stdlib.h>
1179904c9dSVitaly Kuznetsov #include <stdint.h>
1279904c9dSVitaly Kuznetsov #include <string.h>
1379904c9dSVitaly Kuznetsov #include <sys/ioctl.h>
1479904c9dSVitaly Kuznetsov 
1579904c9dSVitaly Kuznetsov #include "test_util.h"
1679904c9dSVitaly Kuznetsov 
1779904c9dSVitaly Kuznetsov #include "kvm_util.h"
1879904c9dSVitaly Kuznetsov 
1979904c9dSVitaly Kuznetsov #include "vmx.h"
208ec107c8SVitaly Kuznetsov #include "svm_util.h"
2179904c9dSVitaly Kuznetsov 
2279904c9dSVitaly Kuznetsov #define SMRAM_SIZE 65536
2379904c9dSVitaly Kuznetsov #define SMRAM_MEMSLOT ((1 << 16) | 1)
2479904c9dSVitaly Kuznetsov #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
2579904c9dSVitaly Kuznetsov #define SMRAM_GPA 0x1000000
2679904c9dSVitaly Kuznetsov #define SMRAM_STAGE 0xfe
2779904c9dSVitaly Kuznetsov 
2879904c9dSVitaly Kuznetsov #define STR(x) #x
2979904c9dSVitaly Kuznetsov #define XSTR(s) STR(s)
3079904c9dSVitaly Kuznetsov 
3179904c9dSVitaly Kuznetsov #define SYNC_PORT 0xe
3279904c9dSVitaly Kuznetsov #define DONE 0xff
3379904c9dSVitaly Kuznetsov 
3479904c9dSVitaly Kuznetsov /*
3579904c9dSVitaly Kuznetsov  * This is compiled as normal 64-bit code, however, SMI handler is executed
3679904c9dSVitaly Kuznetsov  * in real-address mode. To stay simple we're limiting ourselves to a mode
3779904c9dSVitaly Kuznetsov  * independent subset of asm here.
3879904c9dSVitaly Kuznetsov  * SMI handler always report back fixed stage SMRAM_STAGE.
3979904c9dSVitaly Kuznetsov  */
4079904c9dSVitaly Kuznetsov uint8_t smi_handler[] = {
4179904c9dSVitaly Kuznetsov 	0xb0, SMRAM_STAGE,    /* mov $SMRAM_STAGE, %al */
4279904c9dSVitaly Kuznetsov 	0xe4, SYNC_PORT,      /* in $SYNC_PORT, %al */
4379904c9dSVitaly Kuznetsov 	0x0f, 0xaa,           /* rsm */
4479904c9dSVitaly Kuznetsov };
4579904c9dSVitaly Kuznetsov 
sync_with_host(uint64_t phase)46cfb65c15SVitaly Kuznetsov static inline void sync_with_host(uint64_t phase)
4779904c9dSVitaly Kuznetsov {
4879904c9dSVitaly Kuznetsov 	asm volatile("in $" XSTR(SYNC_PORT)", %%al \n"
49cfb65c15SVitaly Kuznetsov 		     : "+a" (phase));
5079904c9dSVitaly Kuznetsov }
5179904c9dSVitaly Kuznetsov 
self_smi(void)52d951b221SVitaly Kuznetsov static void self_smi(void)
5379904c9dSVitaly Kuznetsov {
54768d134dSJim Mattson 	x2apic_write_reg(APIC_ICR,
5579904c9dSVitaly Kuznetsov 			 APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_SMI);
5679904c9dSVitaly Kuznetsov }
5779904c9dSVitaly Kuznetsov 
l2_guest_code(void)58d951b221SVitaly Kuznetsov static void l2_guest_code(void)
5979904c9dSVitaly Kuznetsov {
60d951b221SVitaly Kuznetsov 	sync_with_host(8);
61d951b221SVitaly Kuznetsov 
62d951b221SVitaly Kuznetsov 	sync_with_host(10);
63d951b221SVitaly Kuznetsov 
64d951b221SVitaly Kuznetsov 	vmcall();
65d951b221SVitaly Kuznetsov }
66d951b221SVitaly Kuznetsov 
guest_code(void * arg)67d951b221SVitaly Kuznetsov static void guest_code(void *arg)
68d951b221SVitaly Kuznetsov {
69d951b221SVitaly Kuznetsov 	#define L2_GUEST_STACK_SIZE 64
70d951b221SVitaly Kuznetsov 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
7179904c9dSVitaly Kuznetsov 	uint64_t apicbase = rdmsr(MSR_IA32_APICBASE);
72d951b221SVitaly Kuznetsov 	struct svm_test_data *svm = arg;
73d951b221SVitaly Kuznetsov 	struct vmx_pages *vmx_pages = arg;
7479904c9dSVitaly Kuznetsov 
7579904c9dSVitaly Kuznetsov 	sync_with_host(1);
7679904c9dSVitaly Kuznetsov 
7779904c9dSVitaly Kuznetsov 	wrmsr(MSR_IA32_APICBASE, apicbase | X2APIC_ENABLE);
7879904c9dSVitaly Kuznetsov 
7979904c9dSVitaly Kuznetsov 	sync_with_host(2);
8079904c9dSVitaly Kuznetsov 
8179904c9dSVitaly Kuznetsov 	self_smi();
8279904c9dSVitaly Kuznetsov 
8379904c9dSVitaly Kuznetsov 	sync_with_host(4);
8479904c9dSVitaly Kuznetsov 
858ec107c8SVitaly Kuznetsov 	if (arg) {
8605c2b6e5SSean Christopherson 		if (this_cpu_has(X86_FEATURE_SVM)) {
87d951b221SVitaly Kuznetsov 			generic_svm_setup(svm, l2_guest_code,
88d951b221SVitaly Kuznetsov 					  &l2_guest_stack[L2_GUEST_STACK_SIZE]);
89d951b221SVitaly Kuznetsov 		} else {
90d951b221SVitaly Kuznetsov 			GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
91d951b221SVitaly Kuznetsov 			GUEST_ASSERT(load_vmcs(vmx_pages));
92d951b221SVitaly Kuznetsov 			prepare_vmcs(vmx_pages, l2_guest_code,
93d951b221SVitaly Kuznetsov 				     &l2_guest_stack[L2_GUEST_STACK_SIZE]);
94d951b221SVitaly Kuznetsov 		}
9579904c9dSVitaly Kuznetsov 
9679904c9dSVitaly Kuznetsov 		sync_with_host(5);
9779904c9dSVitaly Kuznetsov 
9879904c9dSVitaly Kuznetsov 		self_smi();
9979904c9dSVitaly Kuznetsov 
10079904c9dSVitaly Kuznetsov 		sync_with_host(7);
101d951b221SVitaly Kuznetsov 
10205c2b6e5SSean Christopherson 		if (this_cpu_has(X86_FEATURE_SVM)) {
103d951b221SVitaly Kuznetsov 			run_guest(svm->vmcb, svm->vmcb_gpa);
104d951b221SVitaly Kuznetsov 			run_guest(svm->vmcb, svm->vmcb_gpa);
105d951b221SVitaly Kuznetsov 		} else {
106d951b221SVitaly Kuznetsov 			vmlaunch();
107d951b221SVitaly Kuznetsov 			vmresume();
108d951b221SVitaly Kuznetsov 		}
109d951b221SVitaly Kuznetsov 
110d951b221SVitaly Kuznetsov 		/* Stages 8-11 are eaten by SMM (SMRAM_STAGE reported instead) */
111d951b221SVitaly Kuznetsov 		sync_with_host(12);
11279904c9dSVitaly Kuznetsov 	}
11379904c9dSVitaly Kuznetsov 
11479904c9dSVitaly Kuznetsov 	sync_with_host(DONE);
11579904c9dSVitaly Kuznetsov }
11679904c9dSVitaly Kuznetsov 
inject_smi(struct kvm_vcpu * vcpu)11720092699SSean Christopherson void inject_smi(struct kvm_vcpu *vcpu)
118d951b221SVitaly Kuznetsov {
119d951b221SVitaly Kuznetsov 	struct kvm_vcpu_events events;
120d951b221SVitaly Kuznetsov 
121768e9a61SSean Christopherson 	vcpu_events_get(vcpu, &events);
122d951b221SVitaly Kuznetsov 
123d951b221SVitaly Kuznetsov 	events.smi.pending = 1;
124d951b221SVitaly Kuznetsov 	events.flags |= KVM_VCPUEVENT_VALID_SMM;
125d951b221SVitaly Kuznetsov 
126768e9a61SSean Christopherson 	vcpu_events_set(vcpu, &events);
127d951b221SVitaly Kuznetsov }
128d951b221SVitaly Kuznetsov 
main(int argc,char * argv[])12979904c9dSVitaly Kuznetsov int main(int argc, char *argv[])
13079904c9dSVitaly Kuznetsov {
1318ec107c8SVitaly Kuznetsov 	vm_vaddr_t nested_gva = 0;
13279904c9dSVitaly Kuznetsov 
13320092699SSean Christopherson 	struct kvm_vcpu *vcpu;
13479904c9dSVitaly Kuznetsov 	struct kvm_regs regs;
13579904c9dSVitaly Kuznetsov 	struct kvm_vm *vm;
13679904c9dSVitaly Kuznetsov 	struct kvm_x86_state *state;
13779904c9dSVitaly Kuznetsov 	int stage, stage_reported;
13879904c9dSVitaly Kuznetsov 
1394b8e1b32SPaolo Bonzini 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_SMM));
1404b8e1b32SPaolo Bonzini 
14179904c9dSVitaly Kuznetsov 	/* Create VM */
14220092699SSean Christopherson 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
14379904c9dSVitaly Kuznetsov 
14479904c9dSVitaly Kuznetsov 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
14579904c9dSVitaly Kuznetsov 				    SMRAM_MEMSLOT, SMRAM_PAGES, 0);
14679904c9dSVitaly Kuznetsov 	TEST_ASSERT(vm_phy_pages_alloc(vm, SMRAM_PAGES, SMRAM_GPA, SMRAM_MEMSLOT)
14779904c9dSVitaly Kuznetsov 		    == SMRAM_GPA, "could not allocate guest physical addresses?");
14879904c9dSVitaly Kuznetsov 
14979904c9dSVitaly Kuznetsov 	memset(addr_gpa2hva(vm, SMRAM_GPA), 0x0, SMRAM_SIZE);
15079904c9dSVitaly Kuznetsov 	memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
15179904c9dSVitaly Kuznetsov 	       sizeof(smi_handler));
15279904c9dSVitaly Kuznetsov 
153768e9a61SSean Christopherson 	vcpu_set_msr(vcpu, MSR_IA32_SMBASE, SMRAM_GPA);
15479904c9dSVitaly Kuznetsov 
1559393cb13SSean Christopherson 	if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
156f21940a3SSean Christopherson 		if (kvm_cpu_has(X86_FEATURE_SVM))
1578ec107c8SVitaly Kuznetsov 			vcpu_alloc_svm(vm, &nested_gva);
1581ecbb337SSean Christopherson 		else if (kvm_cpu_has(X86_FEATURE_VMX))
1598ec107c8SVitaly Kuznetsov 			vcpu_alloc_vmx(vm, &nested_gva);
16079904c9dSVitaly Kuznetsov 	}
16179904c9dSVitaly Kuznetsov 
16241a23ab3SVitaly Kuznetsov 	if (!nested_gva)
16341a23ab3SVitaly Kuznetsov 		pr_info("will skip SMM test with VMX enabled\n");
16441a23ab3SVitaly Kuznetsov 
165768e9a61SSean Christopherson 	vcpu_args_set(vcpu, 1, nested_gva);
16641a23ab3SVitaly Kuznetsov 
16779904c9dSVitaly Kuznetsov 	for (stage = 1;; stage++) {
168768e9a61SSean Christopherson 		vcpu_run(vcpu);
169*c96f57b0SVipin Sharma 		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
17079904c9dSVitaly Kuznetsov 
17179904c9dSVitaly Kuznetsov 		memset(&regs, 0, sizeof(regs));
172768e9a61SSean Christopherson 		vcpu_regs_get(vcpu, &regs);
17379904c9dSVitaly Kuznetsov 
17479904c9dSVitaly Kuznetsov 		stage_reported = regs.rax & 0xff;
17579904c9dSVitaly Kuznetsov 
17679904c9dSVitaly Kuznetsov 		if (stage_reported == DONE)
17779904c9dSVitaly Kuznetsov 			goto done;
17879904c9dSVitaly Kuznetsov 
17979904c9dSVitaly Kuznetsov 		TEST_ASSERT(stage_reported == stage ||
18079904c9dSVitaly Kuznetsov 			    stage_reported == SMRAM_STAGE,
18179904c9dSVitaly Kuznetsov 			    "Unexpected stage: #%x, got %x",
18279904c9dSVitaly Kuznetsov 			    stage, stage_reported);
18379904c9dSVitaly Kuznetsov 
184d951b221SVitaly Kuznetsov 		/*
185d951b221SVitaly Kuznetsov 		 * Enter SMM during L2 execution and check that we correctly
186d951b221SVitaly Kuznetsov 		 * return from it. Do not perform save/restore while in SMM yet.
187d951b221SVitaly Kuznetsov 		 */
188d951b221SVitaly Kuznetsov 		if (stage == 8) {
18920092699SSean Christopherson 			inject_smi(vcpu);
190d951b221SVitaly Kuznetsov 			continue;
191d951b221SVitaly Kuznetsov 		}
192d951b221SVitaly Kuznetsov 
193d951b221SVitaly Kuznetsov 		/*
194d951b221SVitaly Kuznetsov 		 * Perform save/restore while the guest is in SMM triggered
195d951b221SVitaly Kuznetsov 		 * during L2 execution.
196d951b221SVitaly Kuznetsov 		 */
197d951b221SVitaly Kuznetsov 		if (stage == 10)
19820092699SSean Christopherson 			inject_smi(vcpu);
199d951b221SVitaly Kuznetsov 
200768e9a61SSean Christopherson 		state = vcpu_save_state(vcpu);
20179904c9dSVitaly Kuznetsov 		kvm_vm_release(vm);
20220092699SSean Christopherson 
20320092699SSean Christopherson 		vcpu = vm_recreate_with_one_vcpu(vm);
204768e9a61SSean Christopherson 		vcpu_load_state(vcpu, state);
205415a3c33SWei Wang 		kvm_x86_state_cleanup(state);
20679904c9dSVitaly Kuznetsov 	}
20779904c9dSVitaly Kuznetsov 
20879904c9dSVitaly Kuznetsov done:
20979904c9dSVitaly Kuznetsov 	kvm_vm_free(vm);
21079904c9dSVitaly Kuznetsov }
211