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 	virt_pg_map(vm, mmio_gpa, mmio_gpa);
18 
19 	vm->ucall_mmio_addr = mmio_gpa;
20 
21 	write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gpa);
22 }
23 
24 void ucall_arch_do_ucall(vm_vaddr_t uc)
25 {
26 	WRITE_ONCE(*ucall_exit_mmio_addr, uc);
27 }
28 
29 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
30 {
31 	struct kvm_run *run = vcpu->run;
32 
33 	if (run->exit_reason == KVM_EXIT_MMIO &&
34 	    run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) {
35 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t),
36 			    "Unexpected ucall exit mmio address access");
37 		return (void *)(*((uint64_t *)run->mmio.data));
38 	}
39 
40 	return NULL;
41 }
42