Commit 8357b9e1 authored by Maxim Levitsky's avatar Maxim Levitsky Committed by Paolo Bonzini
Browse files

KVM: selftests: add svm part to triple_fault_test



Add a SVM implementation to triple_fault_test to test that
emulated/injected shutdown works.

Since instead of the VMX, the SVM allows the hypervisor to avoid
intercepting shutdown in guest, don't intercept shutdown to test that
KVM suports this correctly.

Signed-off-by: default avatarMaxim Levitsky <mlevitsk@redhat.com>
Message-Id: <20221103141351.50662-9-mlevitsk@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 92e7d5c8
Loading
Loading
Loading
Loading
+56 −17
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
#include "kvm_util.h"
#include "processor.h"
#include "vmx.h"
#include "svm_util.h"

#include <string.h>
#include <sys/ioctl.h>
@@ -20,11 +21,12 @@ static void l2_guest_code(void)
		     : : [port] "d" (ARBITRARY_IO_PORT) : "rax");
}

void l1_guest_code(struct vmx_pages *vmx)
{
#define L2_GUEST_STACK_SIZE 64
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];

void l1_guest_code_vmx(struct vmx_pages *vmx)
{

	GUEST_ASSERT(vmx->vmcs_gpa);
	GUEST_ASSERT(prepare_for_vmx_operation(vmx));
	GUEST_ASSERT(load_vmcs(vmx));
@@ -38,24 +40,53 @@ void l1_guest_code(struct vmx_pages *vmx)
	GUEST_DONE();
}

void l1_guest_code_svm(struct svm_test_data *svm)
{
	struct vmcb *vmcb = svm->vmcb;

	generic_svm_setup(svm, l2_guest_code,
			&l2_guest_stack[L2_GUEST_STACK_SIZE]);

	/* don't intercept shutdown to test the case of SVM allowing to do so */
	vmcb->control.intercept &= ~(BIT(INTERCEPT_SHUTDOWN));

	run_guest(vmcb, svm->vmcb_gpa);

	/* should not reach here, L1 should crash  */
	GUEST_ASSERT(0);
}

int main(void)
{
	struct kvm_vcpu *vcpu;
	struct kvm_run *run;
	struct kvm_vcpu_events events;
	vm_vaddr_t vmx_pages_gva;
	struct ucall uc;

	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
	bool has_vmx = kvm_cpu_has(X86_FEATURE_VMX);
	bool has_svm = kvm_cpu_has(X86_FEATURE_SVM);

	TEST_REQUIRE(has_vmx || has_svm);

	TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_TRIPLE_FAULT_EVENT));

	vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
	vm_enable_cap(vm, KVM_CAP_X86_TRIPLE_FAULT_EVENT, 1);

	run = vcpu->run;
	if (has_vmx) {
		vm_vaddr_t vmx_pages_gva;

		vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code_vmx);
		vcpu_alloc_vmx(vm, &vmx_pages_gva);
		vcpu_args_set(vcpu, 1, vmx_pages_gva);
	} else {
		vm_vaddr_t svm_gva;

		vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code_svm);
		vcpu_alloc_svm(vm, &svm_gva);
		vcpu_args_set(vcpu, 1, svm_gva);
	}

	vm_enable_cap(vm, KVM_CAP_X86_TRIPLE_FAULT_EVENT, 1);
	run = vcpu->run;
	vcpu_run(vcpu);

	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
@@ -78,6 +109,13 @@ int main(void)
		    "No triple fault pending");
	vcpu_run(vcpu);


	if (has_svm) {
		TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
			    "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s)\n",
			    run->exit_reason,
			    exit_reason_str(run->exit_reason));
	} else {
		switch (get_ucall(vcpu, &uc)) {
		case UCALL_DONE:
			break;
@@ -86,5 +124,6 @@ int main(void)
		default:
			TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
		}

	}
	return 0;
}