1*9ff0e37cSIsaku Yamahata // SPDX-License-Identifier: GPL-2.0
2*9ff0e37cSIsaku Yamahata /*
3*9ff0e37cSIsaku Yamahata * Copyright (C) 2024, Intel, Inc
4*9ff0e37cSIsaku Yamahata *
5*9ff0e37cSIsaku Yamahata * Author:
6*9ff0e37cSIsaku Yamahata * Isaku Yamahata <isaku.yamahata at gmail.com>
7*9ff0e37cSIsaku Yamahata */
8*9ff0e37cSIsaku Yamahata #include <linux/sizes.h>
9*9ff0e37cSIsaku Yamahata
10*9ff0e37cSIsaku Yamahata #include <test_util.h>
11*9ff0e37cSIsaku Yamahata #include <kvm_util.h>
12*9ff0e37cSIsaku Yamahata #include <processor.h>
13*9ff0e37cSIsaku Yamahata
14*9ff0e37cSIsaku Yamahata /* Arbitrarily chosen values */
15*9ff0e37cSIsaku Yamahata #define TEST_SIZE (SZ_2M + PAGE_SIZE)
16*9ff0e37cSIsaku Yamahata #define TEST_NPAGES (TEST_SIZE / PAGE_SIZE)
17*9ff0e37cSIsaku Yamahata #define TEST_SLOT 10
18*9ff0e37cSIsaku Yamahata
guest_code(uint64_t base_gpa)19*9ff0e37cSIsaku Yamahata static void guest_code(uint64_t base_gpa)
20*9ff0e37cSIsaku Yamahata {
21*9ff0e37cSIsaku Yamahata volatile uint64_t val __used;
22*9ff0e37cSIsaku Yamahata int i;
23*9ff0e37cSIsaku Yamahata
24*9ff0e37cSIsaku Yamahata for (i = 0; i < TEST_NPAGES; i++) {
25*9ff0e37cSIsaku Yamahata uint64_t *src = (uint64_t *)(base_gpa + i * PAGE_SIZE);
26*9ff0e37cSIsaku Yamahata
27*9ff0e37cSIsaku Yamahata val = *src;
28*9ff0e37cSIsaku Yamahata }
29*9ff0e37cSIsaku Yamahata
30*9ff0e37cSIsaku Yamahata GUEST_DONE();
31*9ff0e37cSIsaku Yamahata }
32*9ff0e37cSIsaku Yamahata
pre_fault_memory(struct kvm_vcpu * vcpu,u64 gpa,u64 size,u64 left)33*9ff0e37cSIsaku Yamahata static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 gpa, u64 size,
34*9ff0e37cSIsaku Yamahata u64 left)
35*9ff0e37cSIsaku Yamahata {
36*9ff0e37cSIsaku Yamahata struct kvm_pre_fault_memory range = {
37*9ff0e37cSIsaku Yamahata .gpa = gpa,
38*9ff0e37cSIsaku Yamahata .size = size,
39*9ff0e37cSIsaku Yamahata .flags = 0,
40*9ff0e37cSIsaku Yamahata };
41*9ff0e37cSIsaku Yamahata u64 prev;
42*9ff0e37cSIsaku Yamahata int ret, save_errno;
43*9ff0e37cSIsaku Yamahata
44*9ff0e37cSIsaku Yamahata do {
45*9ff0e37cSIsaku Yamahata prev = range.size;
46*9ff0e37cSIsaku Yamahata ret = __vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range);
47*9ff0e37cSIsaku Yamahata save_errno = errno;
48*9ff0e37cSIsaku Yamahata TEST_ASSERT((range.size < prev) ^ (ret < 0),
49*9ff0e37cSIsaku Yamahata "%sexpecting range.size to change on %s",
50*9ff0e37cSIsaku Yamahata ret < 0 ? "not " : "",
51*9ff0e37cSIsaku Yamahata ret < 0 ? "failure" : "success");
52*9ff0e37cSIsaku Yamahata } while (ret >= 0 ? range.size : save_errno == EINTR);
53*9ff0e37cSIsaku Yamahata
54*9ff0e37cSIsaku Yamahata TEST_ASSERT(range.size == left,
55*9ff0e37cSIsaku Yamahata "Completed with %lld bytes left, expected %" PRId64,
56*9ff0e37cSIsaku Yamahata range.size, left);
57*9ff0e37cSIsaku Yamahata
58*9ff0e37cSIsaku Yamahata if (left == 0)
59*9ff0e37cSIsaku Yamahata __TEST_ASSERT_VM_VCPU_IOCTL(!ret, "KVM_PRE_FAULT_MEMORY", ret, vcpu->vm);
60*9ff0e37cSIsaku Yamahata else
61*9ff0e37cSIsaku Yamahata /* No memory slot causes RET_PF_EMULATE. it results in -ENOENT. */
62*9ff0e37cSIsaku Yamahata __TEST_ASSERT_VM_VCPU_IOCTL(ret && save_errno == ENOENT,
63*9ff0e37cSIsaku Yamahata "KVM_PRE_FAULT_MEMORY", ret, vcpu->vm);
64*9ff0e37cSIsaku Yamahata }
65*9ff0e37cSIsaku Yamahata
__test_pre_fault_memory(unsigned long vm_type,bool private)66*9ff0e37cSIsaku Yamahata static void __test_pre_fault_memory(unsigned long vm_type, bool private)
67*9ff0e37cSIsaku Yamahata {
68*9ff0e37cSIsaku Yamahata const struct vm_shape shape = {
69*9ff0e37cSIsaku Yamahata .mode = VM_MODE_DEFAULT,
70*9ff0e37cSIsaku Yamahata .type = vm_type,
71*9ff0e37cSIsaku Yamahata };
72*9ff0e37cSIsaku Yamahata struct kvm_vcpu *vcpu;
73*9ff0e37cSIsaku Yamahata struct kvm_run *run;
74*9ff0e37cSIsaku Yamahata struct kvm_vm *vm;
75*9ff0e37cSIsaku Yamahata struct ucall uc;
76*9ff0e37cSIsaku Yamahata
77*9ff0e37cSIsaku Yamahata uint64_t guest_test_phys_mem;
78*9ff0e37cSIsaku Yamahata uint64_t guest_test_virt_mem;
79*9ff0e37cSIsaku Yamahata uint64_t alignment, guest_page_size;
80*9ff0e37cSIsaku Yamahata
81*9ff0e37cSIsaku Yamahata vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
82*9ff0e37cSIsaku Yamahata
83*9ff0e37cSIsaku Yamahata alignment = guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size;
84*9ff0e37cSIsaku Yamahata guest_test_phys_mem = (vm->max_gfn - TEST_NPAGES) * guest_page_size;
85*9ff0e37cSIsaku Yamahata #ifdef __s390x__
86*9ff0e37cSIsaku Yamahata alignment = max(0x100000UL, guest_page_size);
87*9ff0e37cSIsaku Yamahata #else
88*9ff0e37cSIsaku Yamahata alignment = SZ_2M;
89*9ff0e37cSIsaku Yamahata #endif
90*9ff0e37cSIsaku Yamahata guest_test_phys_mem = align_down(guest_test_phys_mem, alignment);
91*9ff0e37cSIsaku Yamahata guest_test_virt_mem = guest_test_phys_mem & ((1ULL << (vm->va_bits - 1)) - 1);
92*9ff0e37cSIsaku Yamahata
93*9ff0e37cSIsaku Yamahata vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
94*9ff0e37cSIsaku Yamahata guest_test_phys_mem, TEST_SLOT, TEST_NPAGES,
95*9ff0e37cSIsaku Yamahata private ? KVM_MEM_GUEST_MEMFD : 0);
96*9ff0e37cSIsaku Yamahata virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, TEST_NPAGES);
97*9ff0e37cSIsaku Yamahata
98*9ff0e37cSIsaku Yamahata if (private)
99*9ff0e37cSIsaku Yamahata vm_mem_set_private(vm, guest_test_phys_mem, TEST_SIZE);
100*9ff0e37cSIsaku Yamahata pre_fault_memory(vcpu, guest_test_phys_mem, SZ_2M, 0);
101*9ff0e37cSIsaku Yamahata pre_fault_memory(vcpu, guest_test_phys_mem + SZ_2M, PAGE_SIZE * 2, PAGE_SIZE);
102*9ff0e37cSIsaku Yamahata pre_fault_memory(vcpu, guest_test_phys_mem + TEST_SIZE, PAGE_SIZE, PAGE_SIZE);
103*9ff0e37cSIsaku Yamahata
104*9ff0e37cSIsaku Yamahata vcpu_args_set(vcpu, 1, guest_test_virt_mem);
105*9ff0e37cSIsaku Yamahata vcpu_run(vcpu);
106*9ff0e37cSIsaku Yamahata
107*9ff0e37cSIsaku Yamahata run = vcpu->run;
108*9ff0e37cSIsaku Yamahata TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
109*9ff0e37cSIsaku Yamahata "Wanted KVM_EXIT_IO, got exit reason: %u (%s)",
110*9ff0e37cSIsaku Yamahata run->exit_reason, exit_reason_str(run->exit_reason));
111*9ff0e37cSIsaku Yamahata
112*9ff0e37cSIsaku Yamahata switch (get_ucall(vcpu, &uc)) {
113*9ff0e37cSIsaku Yamahata case UCALL_ABORT:
114*9ff0e37cSIsaku Yamahata REPORT_GUEST_ASSERT(uc);
115*9ff0e37cSIsaku Yamahata break;
116*9ff0e37cSIsaku Yamahata case UCALL_DONE:
117*9ff0e37cSIsaku Yamahata break;
118*9ff0e37cSIsaku Yamahata default:
119*9ff0e37cSIsaku Yamahata TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
120*9ff0e37cSIsaku Yamahata break;
121*9ff0e37cSIsaku Yamahata }
122*9ff0e37cSIsaku Yamahata
123*9ff0e37cSIsaku Yamahata kvm_vm_free(vm);
124*9ff0e37cSIsaku Yamahata }
125*9ff0e37cSIsaku Yamahata
test_pre_fault_memory(unsigned long vm_type,bool private)126*9ff0e37cSIsaku Yamahata static void test_pre_fault_memory(unsigned long vm_type, bool private)
127*9ff0e37cSIsaku Yamahata {
128*9ff0e37cSIsaku Yamahata if (vm_type && !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) {
129*9ff0e37cSIsaku Yamahata pr_info("Skipping tests for vm_type 0x%lx\n", vm_type);
130*9ff0e37cSIsaku Yamahata return;
131*9ff0e37cSIsaku Yamahata }
132*9ff0e37cSIsaku Yamahata
133*9ff0e37cSIsaku Yamahata __test_pre_fault_memory(vm_type, private);
134*9ff0e37cSIsaku Yamahata }
135*9ff0e37cSIsaku Yamahata
main(int argc,char * argv[])136*9ff0e37cSIsaku Yamahata int main(int argc, char *argv[])
137*9ff0e37cSIsaku Yamahata {
138*9ff0e37cSIsaku Yamahata TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
139*9ff0e37cSIsaku Yamahata
140*9ff0e37cSIsaku Yamahata test_pre_fault_memory(0, false);
141*9ff0e37cSIsaku Yamahata #ifdef __x86_64__
142*9ff0e37cSIsaku Yamahata test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, false);
143*9ff0e37cSIsaku Yamahata test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, true);
144*9ff0e37cSIsaku Yamahata #endif
145*9ff0e37cSIsaku Yamahata return 0;
146*9ff0e37cSIsaku Yamahata }
147