]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/testing/selftests/kvm/lib/ucall.c
kvm: selftests: ucall: improve ucall placement in memory, fix unsigned comparison
[linux.git] / tools / testing / selftests / kvm / lib / ucall.c
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
12 static ucall_type_t ucall_type;
13 static vm_vaddr_t *ucall_exit_mmio_addr;
14
15 static 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
28 void 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                 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/12 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 / 12);
61                 for (offset = 0; offset < end - start; offset += step) {
62                         if (ucall_mmio_init(vm, (gpa - offset) & ~(vm->page_size - 1)))
63                                 return;
64                         if (ucall_mmio_init(vm, (gpa + offset) & ~(vm->page_size - 1)))
65                                 return;
66                 }
67                 TEST_ASSERT(false, "Can't find a ucall mmio address");
68         }
69 }
70
71 void ucall_uninit(struct kvm_vm *vm)
72 {
73         ucall_type = 0;
74         sync_global_to_guest(vm, ucall_type);
75         ucall_exit_mmio_addr = 0;
76         sync_global_to_guest(vm, ucall_exit_mmio_addr);
77 }
78
79 static void ucall_pio_exit(struct ucall *uc)
80 {
81 #ifdef __x86_64__
82         asm volatile("in %[port], %%al"
83                 : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
84 #endif
85 }
86
87 static void ucall_mmio_exit(struct ucall *uc)
88 {
89         *ucall_exit_mmio_addr = (vm_vaddr_t)uc;
90 }
91
92 void ucall(uint64_t cmd, int nargs, ...)
93 {
94         struct ucall uc = {
95                 .cmd = cmd,
96         };
97         va_list va;
98         int i;
99
100         nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
101
102         va_start(va, nargs);
103         for (i = 0; i < nargs; ++i)
104                 uc.args[i] = va_arg(va, uint64_t);
105         va_end(va);
106
107         switch (ucall_type) {
108         case UCALL_PIO:
109                 ucall_pio_exit(&uc);
110                 break;
111         case UCALL_MMIO:
112                 ucall_mmio_exit(&uc);
113                 break;
114         };
115 }
116
117 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
118 {
119         struct kvm_run *run = vcpu_state(vm, vcpu_id);
120
121         memset(uc, 0, sizeof(*uc));
122
123 #ifdef __x86_64__
124         if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO &&
125             run->io.port == UCALL_PIO_PORT) {
126                 struct kvm_regs regs;
127                 vcpu_regs_get(vm, vcpu_id, &regs);
128                 memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc));
129                 return uc->cmd;
130         }
131 #endif
132         if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
133             run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
134                 vm_vaddr_t gva;
135                 TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
136                             "Unexpected ucall exit mmio address access");
137                 gva = *(vm_vaddr_t *)run->mmio.data;
138                 memcpy(uc, addr_gva2hva(vm, gva), sizeof(*uc));
139         }
140
141         return uc->cmd;
142 }