4 * Copyright (C) 2018, Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2.
8 * Tests for vCPU state save/restore, including nested guest state.
10 #define _GNU_SOURCE /* for program_invocation_short_name */
15 #include <sys/ioctl.h>
17 #include "test_util.h"
25 static bool have_nested_state
;
27 void l2_guest_code(void)
34 /* L1 has now set up a shadow VMCS for us. */
35 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0ffee);
37 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0ffee);
38 GUEST_ASSERT(!vmwrite(GUEST_RIP
, 0xc0fffee));
40 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0fffee);
41 GUEST_ASSERT(!vmwrite(GUEST_RIP
, 0xc0ffffee));
44 /* Done, exit to L1 and never come back. */
48 void l1_guest_code(struct vmx_pages
*vmx_pages
)
50 #define L2_GUEST_STACK_SIZE 64
51 unsigned long l2_guest_stack
[L2_GUEST_STACK_SIZE
];
53 GUEST_ASSERT(vmx_pages
->vmcs_gpa
);
54 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages
));
55 GUEST_ASSERT(vmptrstz() == vmx_pages
->vmcs_gpa
);
58 GUEST_ASSERT(vmptrstz() == vmx_pages
->vmcs_gpa
);
60 prepare_vmcs(vmx_pages
, l2_guest_code
,
61 &l2_guest_stack
[L2_GUEST_STACK_SIZE
]);
64 GUEST_ASSERT(vmptrstz() == vmx_pages
->vmcs_gpa
);
65 GUEST_ASSERT(!vmlaunch());
66 GUEST_ASSERT(vmptrstz() == vmx_pages
->vmcs_gpa
);
67 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
69 /* Check that the launched state is preserved. */
70 GUEST_ASSERT(vmlaunch());
72 GUEST_ASSERT(!vmresume());
73 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
76 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
78 GUEST_ASSERT(!vmresume());
79 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
81 vmwrite(GUEST_RIP
, vmreadz(GUEST_RIP
) + 3);
83 vmwrite(SECONDARY_VM_EXEC_CONTROL
, SECONDARY_EXEC_SHADOW_VMCS
);
84 vmwrite(VMCS_LINK_POINTER
, vmx_pages
->shadow_vmcs_gpa
);
86 GUEST_ASSERT(!vmptrld(vmx_pages
->shadow_vmcs_gpa
));
87 GUEST_ASSERT(vmlaunch());
89 GUEST_ASSERT(vmlaunch());
90 GUEST_ASSERT(vmresume());
92 vmwrite(GUEST_RIP
, 0xc0ffee);
94 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0ffee);
96 GUEST_ASSERT(!vmptrld(vmx_pages
->vmcs_gpa
));
97 GUEST_ASSERT(!vmresume());
98 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
100 GUEST_ASSERT(!vmptrld(vmx_pages
->shadow_vmcs_gpa
));
101 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0ffffee);
102 GUEST_ASSERT(vmlaunch());
103 GUEST_ASSERT(vmresume());
105 GUEST_ASSERT(vmreadz(GUEST_RIP
) == 0xc0ffffee);
106 GUEST_ASSERT(vmlaunch());
107 GUEST_ASSERT(vmresume());
110 void guest_code(struct vmx_pages
*vmx_pages
)
116 l1_guest_code(vmx_pages
);
121 int main(int argc
, char *argv
[])
123 struct vmx_pages
*vmx_pages
= NULL
;
124 vm_vaddr_t vmx_pages_gva
= 0;
126 struct kvm_regs regs1
, regs2
;
129 struct kvm_x86_state
*state
;
132 struct kvm_cpuid_entry2
*entry
= kvm_get_supported_cpuid_entry(1);
135 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
136 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
137 run
= vcpu_state(vm
, VCPU_ID
);
139 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
141 if (kvm_check_cap(KVM_CAP_NESTED_STATE
)) {
142 vmx_pages
= vcpu_alloc_vmx(vm
, &vmx_pages_gva
);
143 vcpu_args_set(vm
, VCPU_ID
, 1, vmx_pages_gva
);
145 printf("will skip nested state checks\n");
146 vcpu_args_set(vm
, VCPU_ID
, 1, 0);
149 for (stage
= 1;; stage
++) {
150 _vcpu_run(vm
, VCPU_ID
);
151 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
152 "Unexpected exit reason: %u (%s),\n",
154 exit_reason_str(run
->exit_reason
));
156 memset(®s1
, 0, sizeof(regs1
));
157 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
158 switch (run
->io
.port
) {
159 case GUEST_PORT_ABORT
:
160 TEST_ASSERT(false, "%s at %s:%d", (const char *) regs1
.rdi
,
161 __FILE__
, regs1
.rsi
);
163 case GUEST_PORT_SYNC
:
165 case GUEST_PORT_DONE
:
168 TEST_ASSERT(false, "Unknown port 0x%x.", run
->io
.port
);
171 /* PORT_SYNC is handled here. */
172 TEST_ASSERT(!strcmp((const char *)regs1
.rdi
, "hello") &&
173 regs1
.rsi
== stage
, "Unexpected register values vmexit #%lx, got %lx",
174 stage
, (ulong
) regs1
.rsi
);
176 state
= vcpu_save_state(vm
, VCPU_ID
);
179 /* Restore state in a new VM. */
180 kvm_vm_restart(vm
, O_RDWR
);
181 vm_vcpu_add(vm
, VCPU_ID
, 0, 0);
182 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
183 vcpu_load_state(vm
, VCPU_ID
, state
);
184 run
= vcpu_state(vm
, VCPU_ID
);
187 memset(®s2
, 0, sizeof(regs2
));
188 vcpu_regs_get(vm
, VCPU_ID
, ®s2
);
189 TEST_ASSERT(!memcmp(®s1
, ®s2
, sizeof(regs2
)),
190 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
191 (ulong
) regs2
.rdi
, (ulong
) regs2
.rsi
);