~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/kvm/x86_64/vmx_apic_access_test.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * vmx_apic_access_test
  4  *
  5  * Copyright (C) 2020, Google LLC.
  6  *
  7  * This work is licensed under the terms of the GNU GPL, version 2.
  8  *
  9  * The first subtest simply checks to see that an L2 guest can be
 10  * launched with a valid APIC-access address that is backed by a
 11  * page of L1 physical memory.
 12  *
 13  * The second subtest sets the APIC-access address to a (valid) L1
 14  * physical address that is not backed by memory. KVM can't handle
 15  * this situation, so resuming L2 should result in a KVM exit for
 16  * internal error (emulation). This is not an architectural
 17  * requirement. It is just a shortcoming of KVM. The internal error
 18  * is unfortunate, but it's better than what used to happen!
 19  */
 20 
 21 #include "test_util.h"
 22 #include "kvm_util.h"
 23 #include "processor.h"
 24 #include "vmx.h"
 25 
 26 #include <string.h>
 27 #include <sys/ioctl.h>
 28 
 29 #include "kselftest.h"
 30 
 31 static void l2_guest_code(void)
 32 {
 33         /* Exit to L1 */
 34         __asm__ __volatile__("vmcall");
 35 }
 36 
 37 static void l1_guest_code(struct vmx_pages *vmx_pages, unsigned long high_gpa)
 38 {
 39 #define L2_GUEST_STACK_SIZE 64
 40         unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
 41         uint32_t control;
 42 
 43         GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
 44         GUEST_ASSERT(load_vmcs(vmx_pages));
 45 
 46         /* Prepare the VMCS for L2 execution. */
 47         prepare_vmcs(vmx_pages, l2_guest_code,
 48                      &l2_guest_stack[L2_GUEST_STACK_SIZE]);
 49         control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
 50         control |= CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
 51         vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
 52         control = vmreadz(SECONDARY_VM_EXEC_CONTROL);
 53         control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
 54         vmwrite(SECONDARY_VM_EXEC_CONTROL, control);
 55         vmwrite(APIC_ACCESS_ADDR, vmx_pages->apic_access_gpa);
 56 
 57         /* Try to launch L2 with the memory-backed APIC-access address. */
 58         GUEST_SYNC(vmreadz(APIC_ACCESS_ADDR));
 59         GUEST_ASSERT(!vmlaunch());
 60         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
 61 
 62         vmwrite(APIC_ACCESS_ADDR, high_gpa);
 63 
 64         /* Try to resume L2 with the unbacked APIC-access address. */
 65         GUEST_SYNC(vmreadz(APIC_ACCESS_ADDR));
 66         GUEST_ASSERT(!vmresume());
 67         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
 68 
 69         GUEST_DONE();
 70 }
 71 
 72 int main(int argc, char *argv[])
 73 {
 74         unsigned long apic_access_addr = ~0ul;
 75         vm_vaddr_t vmx_pages_gva;
 76         unsigned long high_gpa;
 77         struct vmx_pages *vmx;
 78         bool done = false;
 79 
 80         struct kvm_vcpu *vcpu;
 81         struct kvm_vm *vm;
 82 
 83         TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
 84 
 85         vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
 86 
 87         high_gpa = (vm->max_gfn - 1) << vm->page_shift;
 88 
 89         vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
 90         prepare_virtualize_apic_accesses(vmx, vm);
 91         vcpu_args_set(vcpu, 2, vmx_pages_gva, high_gpa);
 92 
 93         while (!done) {
 94                 volatile struct kvm_run *run = vcpu->run;
 95                 struct ucall uc;
 96 
 97                 vcpu_run(vcpu);
 98                 if (apic_access_addr == high_gpa) {
 99                         TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
100                         TEST_ASSERT(run->internal.suberror ==
101                                     KVM_INTERNAL_ERROR_EMULATION,
102                                     "Got internal suberror other than KVM_INTERNAL_ERROR_EMULATION: %u",
103                                     run->internal.suberror);
104                         break;
105                 }
106                 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
107 
108                 switch (get_ucall(vcpu, &uc)) {
109                 case UCALL_ABORT:
110                         REPORT_GUEST_ASSERT(uc);
111                         /* NOT REACHED */
112                 case UCALL_SYNC:
113                         apic_access_addr = uc.args[1];
114                         break;
115                 case UCALL_DONE:
116                         done = true;
117                         break;
118                 default:
119                         TEST_ASSERT(false, "Unknown ucall %lu", uc.cmd);
120                 }
121         }
122         kvm_vm_free(vm);
123         return 0;
124 }
125 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php