1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022, Google LLC.
4  *
5  * Test for KVM_CAP_EXIT_ON_EMULATION_FAILURE.
6  */
7 
8 #define _GNU_SOURCE /* for program_invocation_short_name */
9 
10 #include "flds_emulation.h"
11 
12 #include "test_util.h"
13 
14 #define MMIO_GPA	0x700000000
15 #define MMIO_GVA	MMIO_GPA
16 
17 static void guest_code(void)
18 {
19 	/* Execute flds with an MMIO address to force KVM to emulate it. */
20 	flds(MMIO_GVA);
21 	GUEST_DONE();
22 }
23 
24 int main(int argc, char *argv[])
25 {
26 	struct kvm_vcpu *vcpu;
27 	struct kvm_vm *vm;
28 
29 	/* Tell stdout not to buffer its content */
30 	setbuf(stdout, NULL);
31 
32 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE));
33 
34 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
35 	vm_enable_cap(vm, KVM_CAP_EXIT_ON_EMULATION_FAILURE, 1);
36 	virt_map(vm, MMIO_GVA, MMIO_GPA, 1);
37 
38 	vcpu_run(vcpu);
39 	handle_flds_emulation_failure_exit(vcpu);
40 	vcpu_run(vcpu);
41 	ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
42 
43 	kvm_vm_free(vm);
44 	return 0;
45 }
46