Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include "test_util.h"
0003 #include "kvm_util.h"
0004 #include "processor.h"
0005 #include "vmx.h"
0006 
0007 #include <string.h>
0008 #include <sys/ioctl.h>
0009 
0010 #include "kselftest.h"
0011 
0012 #define ARBITRARY_IO_PORT 0x2000
0013 
0014 static struct kvm_vm *vm;
0015 
0016 static void l2_guest_code(void)
0017 {
0018     /*
0019      * Generate an exit to L0 userspace, i.e. main(), via I/O to an
0020      * arbitrary port.
0021      */
0022     asm volatile("inb %%dx, %%al"
0023              : : [port] "d" (ARBITRARY_IO_PORT) : "rax");
0024 }
0025 
0026 static void l1_guest_code(struct vmx_pages *vmx_pages)
0027 {
0028 #define L2_GUEST_STACK_SIZE 64
0029     unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
0030 
0031     GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
0032     GUEST_ASSERT(load_vmcs(vmx_pages));
0033 
0034     /* Prepare the VMCS for L2 execution. */
0035     prepare_vmcs(vmx_pages, l2_guest_code,
0036              &l2_guest_stack[L2_GUEST_STACK_SIZE]);
0037 
0038     /*
0039      * L2 must be run without unrestricted guest, verify that the selftests
0040      * library hasn't enabled it.  Because KVM selftests jump directly to
0041      * 64-bit mode, unrestricted guest support isn't required.
0042      */
0043     GUEST_ASSERT(!(vmreadz(CPU_BASED_VM_EXEC_CONTROL) & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) ||
0044              !(vmreadz(SECONDARY_VM_EXEC_CONTROL) & SECONDARY_EXEC_UNRESTRICTED_GUEST));
0045 
0046     GUEST_ASSERT(!vmlaunch());
0047 
0048     /* L2 should triple fault after main() stuffs invalid guest state. */
0049     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_TRIPLE_FAULT);
0050     GUEST_DONE();
0051 }
0052 
0053 int main(int argc, char *argv[])
0054 {
0055     vm_vaddr_t vmx_pages_gva;
0056     struct kvm_sregs sregs;
0057     struct kvm_vcpu *vcpu;
0058     struct kvm_run *run;
0059     struct ucall uc;
0060 
0061     TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
0062 
0063     vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
0064 
0065     /* Allocate VMX pages and shared descriptors (vmx_pages). */
0066     vcpu_alloc_vmx(vm, &vmx_pages_gva);
0067     vcpu_args_set(vcpu, 1, vmx_pages_gva);
0068 
0069     vcpu_run(vcpu);
0070 
0071     run = vcpu->run;
0072 
0073     /*
0074      * The first exit to L0 userspace should be an I/O access from L2.
0075      * Running L1 should launch L2 without triggering an exit to userspace.
0076      */
0077     TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
0078             "Expected KVM_EXIT_IO, got: %u (%s)\n",
0079             run->exit_reason, exit_reason_str(run->exit_reason));
0080 
0081     TEST_ASSERT(run->io.port == ARBITRARY_IO_PORT,
0082             "Expected IN from port %d from L2, got port %d",
0083             ARBITRARY_IO_PORT, run->io.port);
0084 
0085     /*
0086      * Stuff invalid guest state for L2 by making TR unusuable.  The next
0087      * KVM_RUN should induce a TRIPLE_FAULT in L2 as KVM doesn't support
0088      * emulating invalid guest state for L2.
0089      */
0090     memset(&sregs, 0, sizeof(sregs));
0091     vcpu_sregs_get(vcpu, &sregs);
0092     sregs.tr.unusable = 1;
0093     vcpu_sregs_set(vcpu, &sregs);
0094 
0095     vcpu_run(vcpu);
0096 
0097     switch (get_ucall(vcpu, &uc)) {
0098     case UCALL_DONE:
0099         break;
0100     case UCALL_ABORT:
0101         REPORT_GUEST_ASSERT(uc);
0102     default:
0103         TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
0104     }
0105 }