xref: /linux/tools/testing/selftests/kvm/lib/x86_64/ucall.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
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_init(struct kvm_vm *vm, void *arg)
12 {
13 }
14 
15 void ucall_uninit(struct kvm_vm *vm)
16 {
17 }
18 
19 void ucall(uint64_t cmd, int nargs, ...)
20 {
21 	struct ucall uc = {
22 		.cmd = cmd,
23 	};
24 	va_list va;
25 	int i;
26 
27 	nargs = min(nargs, UCALL_MAX_ARGS);
28 
29 	va_start(va, nargs);
30 	for (i = 0; i < nargs; ++i)
31 		uc.args[i] = va_arg(va, uint64_t);
32 	va_end(va);
33 
34 	asm volatile("in %[port], %%al"
35 		: : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax", "memory");
36 }
37 
38 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
39 {
40 	struct kvm_run *run = vcpu->run;
41 	struct ucall ucall = {};
42 
43 	if (uc)
44 		memset(uc, 0, sizeof(*uc));
45 
46 	if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) {
47 		struct kvm_regs regs;
48 
49 		vcpu_regs_get(vcpu, &regs);
50 		memcpy(&ucall, addr_gva2hva(vcpu->vm, (vm_vaddr_t)regs.rdi),
51 		       sizeof(ucall));
52 
53 		vcpu_run_complete_io(vcpu);
54 		if (uc)
55 			memcpy(uc, &ucall, sizeof(ucall));
56 	}
57 
58 	return ucall.cmd;
59 }
60