12040f414SThomas Huth // SPDX-License-Identifier: GPL-2.0
22040f414SThomas Huth /*
32040f414SThomas Huth * ucall support. A ucall is a "hypercall to userspace".
42040f414SThomas Huth *
52040f414SThomas Huth * Copyright (C) 2018, Red Hat, Inc.
62040f414SThomas Huth */
72040f414SThomas Huth #include "kvm_util.h"
82040f414SThomas Huth
9*b35f4c73SSean Christopherson vm_vaddr_t *ucall_exit_mmio_addr;
102040f414SThomas Huth
ucall_arch_init(struct kvm_vm * vm,vm_paddr_t mmio_gpa)11dc88244bSSean Christopherson void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
122040f414SThomas Huth {
137a161425SOliver Upton vm_vaddr_t mmio_gva = vm_vaddr_unused_gap(vm, vm->page_size, KVM_UTIL_MIN_VADDR);
147a161425SOliver Upton
157a161425SOliver Upton virt_map(vm, mmio_gva, mmio_gpa, 1);
162040f414SThomas Huth
1728a65567SSean Christopherson vm->ucall_mmio_addr = mmio_gpa;
182040f414SThomas Huth
197a161425SOliver Upton write_guest_global(vm, ucall_exit_mmio_addr, (vm_vaddr_t *)mmio_gva);
202040f414SThomas Huth }
212040f414SThomas Huth
ucall_arch_get_ucall(struct kvm_vcpu * vcpu)22ef38871eSSean Christopherson void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
232040f414SThomas Huth {
24768e9a61SSean Christopherson struct kvm_run *run = vcpu->run;
2585f2a432SAaron Lewis
262040f414SThomas Huth if (run->exit_reason == KVM_EXIT_MMIO &&
2703b47505SSean Christopherson run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) {
28426729b2SPeter Gonda TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t),
292040f414SThomas Huth "Unexpected ucall exit mmio address access");
30426729b2SPeter Gonda return (void *)(*((uint64_t *)run->mmio.data));
312040f414SThomas Huth }
322040f414SThomas Huth
33ef38871eSSean Christopherson return NULL;
342040f414SThomas Huth }
35