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 #define UCALL_PIO_PORT ((uint16_t)0x1000) 10 11 void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) 12 { 13 } 14 15 void ucall_arch_do_ucall(vm_vaddr_t uc) 16 { 17 /* 18 * FIXME: Revert this hack (the entire commit that added it) once nVMX 19 * preserves L2 GPRs across a nested VM-Exit. If a ucall from L2, e.g. 20 * to do a GUEST_SYNC(), lands the vCPU in L1, any and all GPRs can be 21 * clobbered by L1. Save and restore non-volatile GPRs (clobbering RBP 22 * in particular is problematic) along with RDX and RDI (which are 23 * inputs), and clobber volatile GPRs. *sigh* 24 */ 25 #define HORRIFIC_L2_UCALL_CLOBBER_HACK \ 26 "rcx", "rsi", "r8", "r9", "r10", "r11" 27 28 asm volatile("push %%rbp\n\t" 29 "push %%r15\n\t" 30 "push %%r14\n\t" 31 "push %%r13\n\t" 32 "push %%r12\n\t" 33 "push %%rbx\n\t" 34 "push %%rdx\n\t" 35 "push %%rdi\n\t" 36 "in %[port], %%al\n\t" 37 "pop %%rdi\n\t" 38 "pop %%rdx\n\t" 39 "pop %%rbx\n\t" 40 "pop %%r12\n\t" 41 "pop %%r13\n\t" 42 "pop %%r14\n\t" 43 "pop %%r15\n\t" 44 "pop %%rbp\n\t" 45 : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory", 46 HORRIFIC_L2_UCALL_CLOBBER_HACK); 47 } 48 49 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) 50 { 51 struct kvm_run *run = vcpu->run; 52 53 if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) { 54 struct kvm_regs regs; 55 56 vcpu_regs_get(vcpu, ®s); 57 return (void *)regs.rdi; 58 } 59 return NULL; 60 } 61