1
2
3
4
5
6
7#include "kvm_util.h"
8#include "../kvm_util_internal.h"
9
10static vm_vaddr_t *ucall_exit_mmio_addr;
11
12static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
13{
14 if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
15 return false;
16
17 virt_pg_map(vm, gpa, gpa);
18
19 ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
20 sync_global_to_guest(vm, ucall_exit_mmio_addr);
21
22 return true;
23}
24
25void ucall_init(struct kvm_vm *vm, void *arg)
26{
27 vm_paddr_t gpa, start, end, step, offset;
28 unsigned int bits;
29 bool ret;
30
31 if (arg) {
32 gpa = (vm_paddr_t)arg;
33 ret = ucall_mmio_init(vm, gpa);
34 TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
35 return;
36 }
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 bits = vm->va_bits - 1;
55 bits = vm->pa_bits < bits ? vm->pa_bits : bits;
56 end = 1ul << bits;
57 start = end * 5 / 8;
58 step = end / 16;
59 for (offset = 0; offset < end - start; offset += step) {
60 if (ucall_mmio_init(vm, start - offset))
61 return;
62 if (ucall_mmio_init(vm, start + offset))
63 return;
64 }
65 TEST_FAIL("Can't find a ucall mmio address");
66}
67
68void ucall_uninit(struct kvm_vm *vm)
69{
70 ucall_exit_mmio_addr = 0;
71 sync_global_to_guest(vm, ucall_exit_mmio_addr);
72}
73
74void ucall(uint64_t cmd, int nargs, ...)
75{
76 struct ucall uc = {
77 .cmd = cmd,
78 };
79 va_list va;
80 int i;
81
82 nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS;
83
84 va_start(va, nargs);
85 for (i = 0; i < nargs; ++i)
86 uc.args[i] = va_arg(va, uint64_t);
87 va_end(va);
88
89 *ucall_exit_mmio_addr = (vm_vaddr_t)&uc;
90}
91
92uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
93{
94 struct kvm_run *run = vcpu_state(vm, vcpu_id);
95 struct ucall ucall = {};
96
97 if (uc)
98 memset(uc, 0, sizeof(*uc));
99
100 if (run->exit_reason == KVM_EXIT_MMIO &&
101 run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
102 vm_vaddr_t gva;
103
104 TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
105 "Unexpected ucall exit mmio address access");
106 memcpy(&gva, run->mmio.data, sizeof(gva));
107 memcpy(&ucall, addr_gva2hva(vm, gva), sizeof(ucall));
108
109 vcpu_run_complete_io(vm, vcpu_id);
110 if (uc)
111 memcpy(uc, &ucall, sizeof(ucall));
112 }
113
114 return ucall.cmd;
115}
116