state_test.c 5.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * KVM_GET/SET_* tests
 *
 * Copyright (C) 2018, Red Hat, Inc.
 *
 * Tests for vCPU state save/restore, including nested guest state.
 */
#define _GNU_SOURCE /* for program_invocation_short_name */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#include "test_util.h"

#include "kvm_util.h"
19
#include "processor.h"
20
#include "vmx.h"
21
#include "svm_util.h"
22

23
#define L2_GUEST_STACK_SIZE 256
24

25
void svm_l2_guest_code(void)
26
{
27 28 29
	GUEST_SYNC(4);
	/* Exit to L1 */
	vmcall();
30
	GUEST_SYNC(6);
31 32 33
	/* Done, exit to L1 and never come back.  */
	vmcall();
}
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static void svm_l1_guest_code(struct svm_test_data *svm)
{
	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
	struct vmcb *vmcb = svm->vmcb;

	GUEST_ASSERT(svm->vmcb_gpa);
	/* Prepare for L2 execution. */
	generic_svm_setup(svm, svm_l2_guest_code,
			  &l2_guest_stack[L2_GUEST_STACK_SIZE]);

	GUEST_SYNC(3);
	run_guest(vmcb, svm->vmcb_gpa);
	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
	GUEST_SYNC(5);
	vmcb->save.rip += 3;
	run_guest(vmcb, svm->vmcb_gpa);
	GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
	GUEST_SYNC(7);
}

void vmx_l2_guest_code(void)
{
	GUEST_SYNC(6);

	/* Exit to L1 */
60 61
	vmcall();

62 63
	/* L1 has now set up a shadow VMCS for us.  */
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
64
	GUEST_SYNC(10);
65 66
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
	GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0fffee));
67
	GUEST_SYNC(11);
68 69
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0fffee);
	GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0ffffee));
70
	GUEST_SYNC(12);
71 72 73 74 75

	/* Done, exit to L1 and never come back.  */
	vmcall();
}

76
static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)
77
{
78
	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
79 80 81

	GUEST_ASSERT(vmx_pages->vmcs_gpa);
	GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
82 83
	GUEST_SYNC(3);
	GUEST_ASSERT(load_vmcs(vmx_pages));
84 85
	GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);

86
	GUEST_SYNC(4);
87 88
	GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);

89
	prepare_vmcs(vmx_pages, vmx_l2_guest_code,
90 91
		     &l2_guest_stack[L2_GUEST_STACK_SIZE]);

92
	GUEST_SYNC(5);
93 94 95 96 97 98 99 100 101 102 103
	GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
	GUEST_ASSERT(!vmlaunch());
	GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);

	/* Check that the launched state is preserved.  */
	GUEST_ASSERT(vmlaunch());

	GUEST_ASSERT(!vmresume());
	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);

104
	GUEST_SYNC(7);
105 106 107 108 109 110 111
	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);

	GUEST_ASSERT(!vmresume());
	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);

	vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);

112 113 114 115 116
	vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
	vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);

	GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
	GUEST_ASSERT(vmlaunch());
117
	GUEST_SYNC(8);
118 119 120 121
	GUEST_ASSERT(vmlaunch());
	GUEST_ASSERT(vmresume());

	vmwrite(GUEST_RIP, 0xc0ffee);
122
	GUEST_SYNC(9);
123 124 125
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);

	GUEST_ASSERT(!vmptrld(vmx_pages->vmcs_gpa));
126 127
	GUEST_ASSERT(!vmresume());
	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
128 129 130 131 132

	GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
	GUEST_ASSERT(vmlaunch());
	GUEST_ASSERT(vmresume());
133
	GUEST_SYNC(13);
134 135 136
	GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
	GUEST_ASSERT(vmlaunch());
	GUEST_ASSERT(vmresume());
137 138
}

139
static void __attribute__((__flatten__)) guest_code(void *arg)
140 141 142 143
{
	GUEST_SYNC(1);
	GUEST_SYNC(2);

144 145 146 147 148 149
	if (arg) {
		if (cpu_has_svm())
			svm_l1_guest_code(arg);
		else
			vmx_l1_guest_code(arg);
	}
150

151
	GUEST_DONE();
152 153 154 155
}

int main(int argc, char *argv[])
{
156
	vm_vaddr_t nested_gva = 0;
157

158
	struct kvm_regs regs1, regs2;
159
	struct kvm_vcpu *vcpu;
160 161 162
	struct kvm_vm *vm;
	struct kvm_run *run;
	struct kvm_x86_state *state;
A
Andrew Jones 已提交
163
	struct ucall uc;
164 165 166
	int stage;

	/* Create VM */
167 168
	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
	run = vcpu->run;
169

170
	vcpu_regs_get(vcpu, &regs1);
171

172
	if (kvm_has_cap(KVM_CAP_NESTED_STATE)) {
173
		if (nested_svm_supported())
174
			vcpu_alloc_svm(vm, &nested_gva);
175
		else if (nested_vmx_supported())
176
			vcpu_alloc_vmx(vm, &nested_gva);
177 178
	}

179 180 181
	if (!nested_gva)
		pr_info("will skip nested state checks\n");

182
	vcpu_args_set(vcpu, 1, nested_gva);
183

184
	for (stage = 1;; stage++) {
185
		vcpu_run(vcpu);
186
		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
187 188
			    "Stage %d: unexpected exit reason: %u (%s),\n",
			    stage, run->exit_reason,
189 190
			    exit_reason_str(run->exit_reason));

191
		switch (get_ucall(vcpu, &uc)) {
A
Andrew Jones 已提交
192
		case UCALL_ABORT:
193
			REPORT_GUEST_ASSERT(uc);
194
			/* NOT REACHED */
A
Andrew Jones 已提交
195
		case UCALL_SYNC:
196
			break;
A
Andrew Jones 已提交
197
		case UCALL_DONE:
198 199
			goto done;
		default:
200
			TEST_FAIL("Unknown ucall %lu", uc.cmd);
201 202
		}

A
Andrew Jones 已提交
203 204
		/* UCALL_SYNC is handled here.  */
		TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
205
			    uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
A
Andrew Jones 已提交
206
			    stage, (ulong)uc.args[1]);
207

208
		state = vcpu_save_state(vcpu);
209
		memset(&regs1, 0, sizeof(regs1));
210
		vcpu_regs_get(vcpu, &regs1);
211

212 213 214
		kvm_vm_release(vm);

		/* Restore state in a new VM.  */
215
		vcpu = vm_recreate_with_one_vcpu(vm);
216 217
		vcpu_set_cpuid(vcpu, kvm_get_supported_cpuid());
		vcpu_load_state(vcpu, state);
218
		run = vcpu->run;
219
		kvm_x86_state_cleanup(state);
220 221

		memset(&regs2, 0, sizeof(regs2));
222
		vcpu_regs_get(vcpu, &regs2);
223 224 225 226 227 228 229 230
		TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
			    "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
			    (ulong) regs2.rdi, (ulong) regs2.rsi);
	}

done:
	kvm_vm_free(vm);
}