linux/tools/testing/selftests/kvm/lib/ucall.c
<<
>>
Prefs
   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;
  38                bool ret;
  39
  40                if (arg) {
  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);
  44                        return;
  45                }
  46
  47                /*
  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.
  57                 */
  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)))
  63                                return;
  64                }
  65                for (gpa = start + step; gpa < end; gpa += step) {
  66                        if (ucall_mmio_init(vm, gpa & ~(vm->page_size - 1)))
  67                                return;
  68                }
  69                TEST_ASSERT(false, "Can't find a ucall mmio address");
  70        }
  71}
  72
  73void ucall_uninit(struct kvm_vm *vm)
  74{
  75        ucall_type = 0;
  76        sync_global_to_guest(vm, ucall_type);
  77        ucall_exit_mmio_addr = 0;
  78        sync_global_to_guest(vm, ucall_exit_mmio_addr);
  79}
  80
  81static void ucall_pio_exit(struct ucall *uc)
  82{
  83#ifdef __x86_64__
  84        asm volatile("in %[port], %%al"
  85                : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
  86#endif
  87}
  88
  89static void ucall_mmio_exit(struct ucall *uc)
  90{
  91        *ucall_exit_mmio_addr = (vm_vaddr_t)uc;
  92}
  93
  94void ucall(uint64_t cmd, int nargs, ...)
  95{
  96        struct ucall uc = {
  97                .cmd = cmd,
  98        };
  99        va_list va;
 100        int i;
 101
 102        nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
 103
 104        va_start(va, nargs);
 105        for (i = 0; i < nargs; ++i)
 106                uc.args[i] = va_arg(va, uint64_t);
 107        va_end(va);
 108
 109        switch (ucall_type) {
 110        case UCALL_PIO:
 111                ucall_pio_exit(&uc);
 112                break;
 113        case UCALL_MMIO:
 114                ucall_mmio_exit(&uc);
 115                break;
 116        };
 117}
 118
 119uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
 120{
 121        struct kvm_run *run = vcpu_state(vm, vcpu_id);
 122
 123        memset(uc, 0, sizeof(*uc));
 124
 125#ifdef __x86_64__
 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, &regs);
 130                memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc));
 131                return uc->cmd;
 132        }
 133#endif
 134        if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
 135            run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
 136                vm_vaddr_t gva;
 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));
 141        }
 142
 143        return uc->cmd;
 144}
 145