1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ucall support. A ucall is a "hypercall to userspace". 4 * 5 * Copyright (C) 2018, Red Hat, Inc. 6 */ 7 #include "kvm_util.h" 8 9 static vm_vaddr_t *ucall_exit_mmio_addr; 10 11 static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa) 12 { 13 if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1)) 14 return false; 15 16 virt_pg_map(vm, gpa, gpa); 17 18 ucall_exit_mmio_addr = (vm_vaddr_t *)gpa; 19 sync_global_to_guest(vm, ucall_exit_mmio_addr); 20 21 return true; 22 } 23 24 void ucall_init(struct kvm_vm *vm, void *arg) 25 { 26 vm_paddr_t gpa, start, end, step, offset; 27 unsigned int bits; 28 bool ret; 29 30 if (arg) { 31 gpa = (vm_paddr_t)arg; 32 ret = ucall_mmio_init(vm, gpa); 33 TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa); 34 return; 35 } 36 37 /* 38 * Find an address within the allowed physical and virtual address 39 * spaces, that does _not_ have a KVM memory region associated with 40 * it. Identity mapping an address like this allows the guest to 41 * access it, but as KVM doesn't know what to do with it, it 42 * will assume it's something userspace handles and exit with 43 * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64. 44 * Here we start with a guess that the addresses around 5/8th 45 * of the allowed space are unmapped and then work both down and 46 * up from there in 1/16th allowed space sized steps. 47 * 48 * Note, we need to use VA-bits - 1 when calculating the allowed 49 * virtual address space for an identity mapping because the upper 50 * half of the virtual address space is the two's complement of the 51 * lower and won't match physical addresses. 52 */ 53 bits = vm->va_bits - 1; 54 bits = min(vm->pa_bits, bits); 55 end = 1ul << bits; 56 start = end * 5 / 8; 57 step = end / 16; 58 for (offset = 0; offset < end - start; offset += step) { 59 if (ucall_mmio_init(vm, start - offset)) 60 return; 61 if (ucall_mmio_init(vm, start + offset)) 62 return; 63 } 64 TEST_FAIL("Can't find a ucall mmio address"); 65 } 66 67 void ucall_uninit(struct kvm_vm *vm) 68 { 69 ucall_exit_mmio_addr = 0; 70 sync_global_to_guest(vm, ucall_exit_mmio_addr); 71 } 72 73 void ucall(uint64_t cmd, int nargs, ...) 74 { 75 struct ucall uc = {}; 76 va_list va; 77 int i; 78 79 WRITE_ONCE(uc.cmd, cmd); 80 nargs = min(nargs, UCALL_MAX_ARGS); 81 82 va_start(va, nargs); 83 for (i = 0; i < nargs; ++i) 84 WRITE_ONCE(uc.args[i], va_arg(va, uint64_t)); 85 va_end(va); 86 87 WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc); 88 } 89 90 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) 91 { 92 struct kvm_run *run = vcpu->run; 93 struct ucall ucall = {}; 94 95 if (uc) 96 memset(uc, 0, sizeof(*uc)); 97 98 if (run->exit_reason == KVM_EXIT_MMIO && 99 run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) { 100 vm_vaddr_t gva; 101 102 TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8, 103 "Unexpected ucall exit mmio address access"); 104 memcpy(&gva, run->mmio.data, sizeof(gva)); 105 memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall)); 106 107 vcpu_run_complete_io(vcpu); 108 if (uc) 109 memcpy(uc, &ucall, sizeof(ucall)); 110 } 111 112 return ucall.cmd; 113 } 114