Path: blob/master/tools/testing/selftests/kvm/lib/x86/ucall.c
49657 views
// SPDX-License-Identifier: GPL-2.01/*2* ucall support. A ucall is a "hypercall to userspace".3*4* Copyright (C) 2018, Red Hat, Inc.5*/6#include "kvm_util.h"78#define UCALL_PIO_PORT ((uint16_t)0x1000)910void ucall_arch_do_ucall(vm_vaddr_t uc)11{12/*13* FIXME: Revert this hack (the entire commit that added it) once nVMX14* preserves L2 GPRs across a nested VM-Exit. If a ucall from L2, e.g.15* to do a GUEST_SYNC(), lands the vCPU in L1, any and all GPRs can be16* clobbered by L1. Save and restore non-volatile GPRs (clobbering RBP17* in particular is problematic) along with RDX and RDI (which are18* inputs), and clobber volatile GPRs. *sigh*19*/20#define HORRIFIC_L2_UCALL_CLOBBER_HACK \21"rcx", "rsi", "r8", "r9", "r10", "r11"2223asm volatile("push %%rbp\n\t"24"push %%r15\n\t"25"push %%r14\n\t"26"push %%r13\n\t"27"push %%r12\n\t"28"push %%rbx\n\t"29"push %%rdx\n\t"30"push %%rdi\n\t"31"in %[port], %%al\n\t"32"pop %%rdi\n\t"33"pop %%rdx\n\t"34"pop %%rbx\n\t"35"pop %%r12\n\t"36"pop %%r13\n\t"37"pop %%r14\n\t"38"pop %%r15\n\t"39"pop %%rbp\n\t"40: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory",41HORRIFIC_L2_UCALL_CLOBBER_HACK);42}4344void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)45{46struct kvm_run *run = vcpu->run;4748if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) {49struct kvm_regs regs;5051vcpu_regs_get(vcpu, ®s);52return (void *)regs.rdi;53}54return NULL;55}565758