Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ucall support. A ucall is a "hypercall to userspace".
0004  *
0005  * Copyright (C) 2018, Red Hat, Inc.
0006  */
0007 #include "kvm_util.h"
0008 
0009 static vm_vaddr_t *ucall_exit_mmio_addr;
0010 
0011 static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
0012 {
0013     if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
0014         return false;
0015 
0016     virt_pg_map(vm, gpa, gpa);
0017 
0018     ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
0019     sync_global_to_guest(vm, ucall_exit_mmio_addr);
0020 
0021     return true;
0022 }
0023 
0024 void ucall_init(struct kvm_vm *vm, void *arg)
0025 {
0026     vm_paddr_t gpa, start, end, step, offset;
0027     unsigned int bits;
0028     bool ret;
0029 
0030     if (arg) {
0031         gpa = (vm_paddr_t)arg;
0032         ret = ucall_mmio_init(vm, gpa);
0033         TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
0034         return;
0035     }
0036 
0037     /*
0038      * Find an address within the allowed physical and virtual address
0039      * spaces, that does _not_ have a KVM memory region associated with
0040      * it. Identity mapping an address like this allows the guest to
0041      * access it, but as KVM doesn't know what to do with it, it
0042      * will assume it's something userspace handles and exit with
0043      * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64.
0044      * Here we start with a guess that the addresses around 5/8th
0045      * of the allowed space are unmapped and then work both down and
0046      * up from there in 1/16th allowed space sized steps.
0047      *
0048      * Note, we need to use VA-bits - 1 when calculating the allowed
0049      * virtual address space for an identity mapping because the upper
0050      * half of the virtual address space is the two's complement of the
0051      * lower and won't match physical addresses.
0052      */
0053     bits = vm->va_bits - 1;
0054     bits = min(vm->pa_bits, bits);
0055     end = 1ul << bits;
0056     start = end * 5 / 8;
0057     step = end / 16;
0058     for (offset = 0; offset < end - start; offset += step) {
0059         if (ucall_mmio_init(vm, start - offset))
0060             return;
0061         if (ucall_mmio_init(vm, start + offset))
0062             return;
0063     }
0064     TEST_FAIL("Can't find a ucall mmio address");
0065 }
0066 
0067 void ucall_uninit(struct kvm_vm *vm)
0068 {
0069     ucall_exit_mmio_addr = 0;
0070     sync_global_to_guest(vm, ucall_exit_mmio_addr);
0071 }
0072 
0073 void ucall(uint64_t cmd, int nargs, ...)
0074 {
0075     struct ucall uc = {};
0076     va_list va;
0077     int i;
0078 
0079     WRITE_ONCE(uc.cmd, cmd);
0080     nargs = min(nargs, UCALL_MAX_ARGS);
0081 
0082     va_start(va, nargs);
0083     for (i = 0; i < nargs; ++i)
0084         WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
0085     va_end(va);
0086 
0087     WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc);
0088 }
0089 
0090 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
0091 {
0092     struct kvm_run *run = vcpu->run;
0093     struct ucall ucall = {};
0094 
0095     if (uc)
0096         memset(uc, 0, sizeof(*uc));
0097 
0098     if (run->exit_reason == KVM_EXIT_MMIO &&
0099         run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
0100         vm_vaddr_t gva;
0101 
0102         TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
0103                 "Unexpected ucall exit mmio address access");
0104         memcpy(&gva, run->mmio.data, sizeof(gva));
0105         memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
0106 
0107         vcpu_run_complete_io(vcpu);
0108         if (uc)
0109             memcpy(uc, &ucall, sizeof(ucall));
0110     }
0111 
0112     return ucall.cmd;
0113 }