Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.1 150 lines 3.9 kB view raw
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#include "kvm_util_internal.h" 9 10#define UCALL_PIO_PORT ((uint16_t)0x1000) 11 12static ucall_type_t ucall_type; 13static vm_vaddr_t *ucall_exit_mmio_addr; 14 15static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa) 16{ 17 if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1)) 18 return false; 19 20 virt_pg_map(vm, gpa, gpa, 0); 21 22 ucall_exit_mmio_addr = (vm_vaddr_t *)gpa; 23 sync_global_to_guest(vm, ucall_exit_mmio_addr); 24 25 return true; 26} 27 28void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg) 29{ 30 ucall_type = type; 31 sync_global_to_guest(vm, ucall_type); 32 33 if (type == UCALL_PIO) 34 return; 35 36 if (type == UCALL_MMIO) { 37 vm_paddr_t gpa, start, end, step, offset; 38 unsigned bits; 39 bool ret; 40 41 if (arg) { 42 gpa = (vm_paddr_t)arg; 43 ret = ucall_mmio_init(vm, gpa); 44 TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa); 45 return; 46 } 47 48 /* 49 * Find an address within the allowed physical and virtual address 50 * spaces, that does _not_ have a KVM memory region associated with 51 * it. Identity mapping an address like this allows the guest to 52 * access it, but as KVM doesn't know what to do with it, it 53 * will assume it's something userspace handles and exit with 54 * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64. 55 * Here we start with a guess that the addresses around 5/8th 56 * of the allowed space are unmapped and then work both down and 57 * up from there in 1/16th allowed space sized steps. 58 * 59 * Note, we need to use VA-bits - 1 when calculating the allowed 60 * virtual address space for an identity mapping because the upper 61 * half of the virtual address space is the two's complement of the 62 * lower and won't match physical addresses. 63 */ 64 bits = vm->va_bits - 1; 65 bits = vm->pa_bits < bits ? vm->pa_bits : bits; 66 end = 1ul << bits; 67 start = end * 5 / 8; 68 step = end / 16; 69 for (offset = 0; offset < end - start; offset += step) { 70 if (ucall_mmio_init(vm, start - offset)) 71 return; 72 if (ucall_mmio_init(vm, start + offset)) 73 return; 74 } 75 TEST_ASSERT(false, "Can't find a ucall mmio address"); 76 } 77} 78 79void ucall_uninit(struct kvm_vm *vm) 80{ 81 ucall_type = 0; 82 sync_global_to_guest(vm, ucall_type); 83 ucall_exit_mmio_addr = 0; 84 sync_global_to_guest(vm, ucall_exit_mmio_addr); 85} 86 87static void ucall_pio_exit(struct ucall *uc) 88{ 89#ifdef __x86_64__ 90 asm volatile("in %[port], %%al" 91 : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax"); 92#endif 93} 94 95static void ucall_mmio_exit(struct ucall *uc) 96{ 97 *ucall_exit_mmio_addr = (vm_vaddr_t)uc; 98} 99 100void ucall(uint64_t cmd, int nargs, ...) 101{ 102 struct ucall uc = { 103 .cmd = cmd, 104 }; 105 va_list va; 106 int i; 107 108 nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; 109 110 va_start(va, nargs); 111 for (i = 0; i < nargs; ++i) 112 uc.args[i] = va_arg(va, uint64_t); 113 va_end(va); 114 115 switch (ucall_type) { 116 case UCALL_PIO: 117 ucall_pio_exit(&uc); 118 break; 119 case UCALL_MMIO: 120 ucall_mmio_exit(&uc); 121 break; 122 }; 123} 124 125uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) 126{ 127 struct kvm_run *run = vcpu_state(vm, vcpu_id); 128 129 memset(uc, 0, sizeof(*uc)); 130 131#ifdef __x86_64__ 132 if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO && 133 run->io.port == UCALL_PIO_PORT) { 134 struct kvm_regs regs; 135 vcpu_regs_get(vm, vcpu_id, &regs); 136 memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc)); 137 return uc->cmd; 138 } 139#endif 140 if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO && 141 run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) { 142 vm_vaddr_t gva; 143 TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8, 144 "Unexpected ucall exit mmio address access"); 145 gva = *(vm_vaddr_t *)run->mmio.data; 146 memcpy(uc, addr_gva2hva(vm, gva), sizeof(*uc)); 147 } 148 149 return uc->cmd; 150}