Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * KVM dirty page logging test
0004  *
0005  * Copyright (C) 2018, Red Hat, Inc.
0006  */
0007 
0008 #define _GNU_SOURCE /* for program_invocation_name */
0009 
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <linux/bitmap.h>
0013 #include <linux/bitops.h>
0014 
0015 #include "test_util.h"
0016 #include "kvm_util.h"
0017 #include "processor.h"
0018 #include "vmx.h"
0019 
0020 /* The memory slot index to track dirty pages */
0021 #define TEST_MEM_SLOT_INDEX     1
0022 #define TEST_MEM_PAGES          3
0023 
0024 /* L1 guest test virtual memory offset */
0025 #define GUEST_TEST_MEM          0xc0000000
0026 
0027 /* L2 guest test virtual memory offset */
0028 #define NESTED_TEST_MEM1        0xc0001000
0029 #define NESTED_TEST_MEM2        0xc0002000
0030 
0031 static void l2_guest_code(void)
0032 {
0033     *(volatile uint64_t *)NESTED_TEST_MEM1;
0034     *(volatile uint64_t *)NESTED_TEST_MEM1 = 1;
0035     GUEST_SYNC(true);
0036     GUEST_SYNC(false);
0037 
0038     *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
0039     GUEST_SYNC(true);
0040     *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
0041     GUEST_SYNC(true);
0042     GUEST_SYNC(false);
0043 
0044     /* Exit to L1 and never come back.  */
0045     vmcall();
0046 }
0047 
0048 void l1_guest_code(struct vmx_pages *vmx)
0049 {
0050 #define L2_GUEST_STACK_SIZE 64
0051     unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
0052 
0053     GUEST_ASSERT(vmx->vmcs_gpa);
0054     GUEST_ASSERT(prepare_for_vmx_operation(vmx));
0055     GUEST_ASSERT(load_vmcs(vmx));
0056 
0057     prepare_vmcs(vmx, l2_guest_code,
0058              &l2_guest_stack[L2_GUEST_STACK_SIZE]);
0059 
0060     GUEST_SYNC(false);
0061     GUEST_ASSERT(!vmlaunch());
0062     GUEST_SYNC(false);
0063     GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
0064     GUEST_DONE();
0065 }
0066 
0067 int main(int argc, char *argv[])
0068 {
0069     vm_vaddr_t vmx_pages_gva = 0;
0070     struct vmx_pages *vmx;
0071     unsigned long *bmap;
0072     uint64_t *host_test_mem;
0073 
0074     struct kvm_vcpu *vcpu;
0075     struct kvm_vm *vm;
0076     struct kvm_run *run;
0077     struct ucall uc;
0078     bool done = false;
0079 
0080     TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
0081 
0082     /* Create VM */
0083     vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
0084     vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
0085     vcpu_args_set(vcpu, 1, vmx_pages_gva);
0086     run = vcpu->run;
0087 
0088     /* Add an extra memory slot for testing dirty logging */
0089     vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
0090                     GUEST_TEST_MEM,
0091                     TEST_MEM_SLOT_INDEX,
0092                     TEST_MEM_PAGES,
0093                     KVM_MEM_LOG_DIRTY_PAGES);
0094 
0095     /*
0096      * Add an identity map for GVA range [0xc0000000, 0xc0002000).  This
0097      * affects both L1 and L2.  However...
0098      */
0099     virt_map(vm, GUEST_TEST_MEM, GUEST_TEST_MEM, TEST_MEM_PAGES);
0100 
0101     /*
0102      * ... pages in the L2 GPA range [0xc0001000, 0xc0003000) will map to
0103      * 0xc0000000.
0104      *
0105      * Note that prepare_eptp should be called only L1's GPA map is done,
0106      * meaning after the last call to virt_map.
0107      */
0108     prepare_eptp(vmx, vm, 0);
0109     nested_map_memslot(vmx, vm, 0);
0110     nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, 4096);
0111     nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, 4096);
0112 
0113     bmap = bitmap_zalloc(TEST_MEM_PAGES);
0114     host_test_mem = addr_gpa2hva(vm, GUEST_TEST_MEM);
0115 
0116     while (!done) {
0117         memset(host_test_mem, 0xaa, TEST_MEM_PAGES * 4096);
0118         vcpu_run(vcpu);
0119         TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
0120                 "Unexpected exit reason: %u (%s),\n",
0121                 run->exit_reason,
0122                 exit_reason_str(run->exit_reason));
0123 
0124         switch (get_ucall(vcpu, &uc)) {
0125         case UCALL_ABORT:
0126             REPORT_GUEST_ASSERT(uc);
0127             /* NOT REACHED */
0128         case UCALL_SYNC:
0129             /*
0130              * The nested guest wrote at offset 0x1000 in the memslot, but the
0131              * dirty bitmap must be filled in according to L1 GPA, not L2.
0132              */
0133             kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
0134             if (uc.args[1]) {
0135                 TEST_ASSERT(test_bit(0, bmap), "Page 0 incorrectly reported clean\n");
0136                 TEST_ASSERT(host_test_mem[0] == 1, "Page 0 not written by guest\n");
0137             } else {
0138                 TEST_ASSERT(!test_bit(0, bmap), "Page 0 incorrectly reported dirty\n");
0139                 TEST_ASSERT(host_test_mem[0] == 0xaaaaaaaaaaaaaaaaULL, "Page 0 written by guest\n");
0140             }
0141 
0142             TEST_ASSERT(!test_bit(1, bmap), "Page 1 incorrectly reported dirty\n");
0143             TEST_ASSERT(host_test_mem[4096 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest\n");
0144             TEST_ASSERT(!test_bit(2, bmap), "Page 2 incorrectly reported dirty\n");
0145             TEST_ASSERT(host_test_mem[8192 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest\n");
0146             break;
0147         case UCALL_DONE:
0148             done = true;
0149             break;
0150         default:
0151             TEST_FAIL("Unknown ucall %lu", uc.cmd);
0152         }
0153     }
0154 }