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 
9 /*
10  * ucall_exit_mmio_addr holds per-VM values (global data is duplicated by each
11  * VM), it must not be accessed from host code.
12  */
13 static vm_vaddr_t *ucall_exit_mmio_addr;
14 
15 void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
16 {
17 	vm_vaddr_t mmio_gva = vm_vaddr_unused_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR);
18 
19 	virt_map(vm, mmio_gva, mmio_gpa, 1);
20 
21 	vm->ucall_mmio_addr = mmio_gpa;
22 
23 	write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gva);
24 }
25 
26 void ucall_arch_do_ucall(vm_vaddr_t uc)
27 {
28 	WRITE_ONCE(*ucall_exit_mmio_addr, uc);
29 }
30 
31 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
32 {
33 	struct kvm_run *run = vcpu->run;
34 
35 	if (run->exit_reason == KVM_EXIT_MMIO &&
36 	    run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) {
37 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t),
38 			    "Unexpected ucall exit mmio address access");
39 		return (void *)(*((uint64_t *)run->mmio.data));
40 	}
41 
42 	return NULL;
43 }
44