1 // SPDX-License-Identifier: GPL-2.0
3 * ucall support. A ucall is a "hypercall to userspace".
5 * Copyright (C) 2018, Red Hat, Inc.
8 #include "kvm_util_internal.h"
10 #define UCALL_PIO_PORT ((uint16_t)0x1000)
12 static ucall_type_t ucall_type;
13 static vm_vaddr_t *ucall_exit_mmio_addr;
15 static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
17 if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
20 virt_pg_map(vm, gpa, gpa, 0);
22 ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
23 sync_global_to_guest(vm, ucall_exit_mmio_addr);
28 void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg)
31 sync_global_to_guest(vm, ucall_type);
33 if (type == UCALL_PIO)
36 if (type == UCALL_MMIO) {
37 vm_paddr_t gpa, start, end, step;
41 gpa = (vm_paddr_t)arg;
42 ret = ucall_mmio_init(vm, gpa);
43 TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
48 * Find an address within the allowed virtual address space,
49 * that does _not_ have a KVM memory region associated with it.
50 * Identity mapping an address like this allows the guest to
51 * access it, but as KVM doesn't know what to do with it, it
52 * will assume it's something userspace handles and exit with
53 * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64.
54 * Here we start with a guess that the addresses around two
55 * thirds of the VA space are unmapped and then work both down
56 * and up from there in 1/6 VA space sized steps.
58 start = 1ul << (vm->va_bits * 2 / 3);
59 end = 1ul << vm->va_bits;
60 step = 1ul << (vm->va_bits / 6);
61 for (gpa = start; gpa >= 0; gpa -= step) {
62 if (ucall_mmio_init(vm, gpa & ~(vm->page_size - 1)))
65 for (gpa = start + step; gpa < end; gpa += step) {
66 if (ucall_mmio_init(vm, gpa & ~(vm->page_size - 1)))
69 TEST_ASSERT(false, "Can't find a ucall mmio address");
73 void ucall_uninit(struct kvm_vm *vm)
76 sync_global_to_guest(vm, ucall_type);
77 ucall_exit_mmio_addr = 0;
78 sync_global_to_guest(vm, ucall_exit_mmio_addr);
81 static void ucall_pio_exit(struct ucall *uc)
84 asm volatile("in %[port], %%al"
85 : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
89 static void ucall_mmio_exit(struct ucall *uc)
91 *ucall_exit_mmio_addr = (vm_vaddr_t)uc;
94 void ucall(uint64_t cmd, int nargs, ...)
102 nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
105 for (i = 0; i < nargs; ++i)
106 uc.args[i] = va_arg(va, uint64_t);
109 switch (ucall_type) {
114 ucall_mmio_exit(&uc);
119 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
121 struct kvm_run *run = vcpu_state(vm, vcpu_id);
123 memset(uc, 0, sizeof(*uc));
126 if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO &&
127 run->io.port == UCALL_PIO_PORT) {
128 struct kvm_regs regs;
129 vcpu_regs_get(vm, vcpu_id, ®s);
130 memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc));
134 if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
135 run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
137 TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
138 "Unexpected ucall exit mmio address access");
139 gva = *(vm_vaddr_t *)run->mmio.data;
140 memcpy(uc, addr_gva2hva(vm, gva), sizeof(*uc));