Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * svm_vmcall_test
0004  *
0005  * Copyright (C) 2020, Red Hat, Inc.
0006  *
0007  * Nested SVM testing: VMCALL
0008  */
0009 
0010 #include "test_util.h"
0011 #include "kvm_util.h"
0012 #include "processor.h"
0013 #include "svm_util.h"
0014 
0015 static void l2_guest_code(struct svm_test_data *svm)
0016 {
0017     __asm__ __volatile__("vmcall");
0018 }
0019 
0020 static void l1_guest_code(struct svm_test_data *svm)
0021 {
0022     #define L2_GUEST_STACK_SIZE 64
0023     unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
0024     struct vmcb *vmcb = svm->vmcb;
0025 
0026     /* Prepare for L2 execution. */
0027     generic_svm_setup(svm, l2_guest_code,
0028               &l2_guest_stack[L2_GUEST_STACK_SIZE]);
0029 
0030     run_guest(vmcb, svm->vmcb_gpa);
0031 
0032     GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
0033     GUEST_DONE();
0034 }
0035 
0036 int main(int argc, char *argv[])
0037 {
0038     struct kvm_vcpu *vcpu;
0039     vm_vaddr_t svm_gva;
0040     struct kvm_vm *vm;
0041 
0042     TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
0043 
0044     vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
0045 
0046     vcpu_alloc_svm(vm, &svm_gva);
0047     vcpu_args_set(vcpu, 1, svm_gva);
0048 
0049     for (;;) {
0050         volatile struct kvm_run *run = vcpu->run;
0051         struct ucall uc;
0052 
0053         vcpu_run(vcpu);
0054         TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
0055                 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
0056                 run->exit_reason,
0057                 exit_reason_str(run->exit_reason));
0058 
0059         switch (get_ucall(vcpu, &uc)) {
0060         case UCALL_ABORT:
0061             REPORT_GUEST_ASSERT(uc);
0062             /* NOT REACHED */
0063         case UCALL_SYNC:
0064             break;
0065         case UCALL_DONE:
0066             goto done;
0067         default:
0068             TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
0069         }
0070     }
0071 done:
0072     kvm_vm_free(vm);
0073     return 0;
0074 }