Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
57d5edf
Documentation
LICENSES
arch
block
certs
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
accounting
arch
bpf
build
cgroup
crypto
firewire
gpio
hv
iio
include
kvm
laptop
leds
lib
memory-model
nfsd
objtool
pci
pcmcia
perf
power
scripts
spi
testing
fault-injection
ktest
nvdimm
radix-tree
scatterlist
selftests
android
bpf
breakpoints
capabilities
cgroup
cpu-hotplug
cpufreq
drivers
efivarfs
exec
filesystems
firmware
ftrace
futex
gpio
ia64
intel_pstate
ipc
kcmp
kmod
kvm
include
lib
aarch64
x86_64
assert.c
elf.c
io.c
kvm_util.c
kvm_util_internal.h
sparsebit.c
ucall.c
x86_64
.gitignore
Makefile
clear_dirty_log_test.c
dirty_log_test.c
lib
locking
media_tests
membarrier
memfd
memory-hotplug
mount
mqueue
net
netfilter
networking
nsfs
ntb
powerpc
prctl
proc
pstore
ptp
ptrace
rcutorture
rseq
rtc
seccomp
sigaltstack
size
sparc64
splice
static_keys
sync
sysctl
tc-testing
timers
uevent
user
vDSO
vm
watchdog
x86
zram
.gitignore
Makefile
gen_kselftest_tar.sh
kselftest.h
kselftest_harness.h
kselftest_install.sh
lib.mk
vsock
thermal
time
usb
virtio
vm
wmi
Makefile
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
kvm
/
lib
/
ucall.c
Copy path
Blame
Blame
Latest commit
History
History
150 lines (128 loc) · 3.78 KB
Breadcrumbs
linux
/
tools
/
testing
/
selftests
/
kvm
/
lib
/
ucall.c
Top
File metadata and controls
Code
Blame
150 lines (128 loc) · 3.78 KB
Raw
// SPDX-License-Identifier: GPL-2.0 /* * ucall support. A ucall is a "hypercall to userspace". * * Copyright (C) 2018, Red Hat, Inc. */ #include "kvm_util.h" #include "kvm_util_internal.h" #define UCALL_PIO_PORT ((uint16_t)0x1000) static ucall_type_t ucall_type; static vm_vaddr_t *ucall_exit_mmio_addr; static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa) { if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1)) return false; virt_pg_map(vm, gpa, gpa, 0); ucall_exit_mmio_addr = (vm_vaddr_t *)gpa; sync_global_to_guest(vm, ucall_exit_mmio_addr); return true; } void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg) { ucall_type = type; sync_global_to_guest(vm, ucall_type); if (type == UCALL_PIO) return; if (type == UCALL_MMIO) { vm_paddr_t gpa, start, end, step, offset; unsigned bits; bool ret; if (arg) { gpa = (vm_paddr_t)arg; ret = ucall_mmio_init(vm, gpa); TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa); return; } /* * Find an address within the allowed physical and virtual address * spaces, that does _not_ have a KVM memory region associated with * it. Identity mapping an address like this allows the guest to * access it, but as KVM doesn't know what to do with it, it * will assume it's something userspace handles and exit with * KVM_EXIT_MMIO. Well, at least that's how it works for AArch64. * Here we start with a guess that the addresses around 5/8th * of the allowed space are unmapped and then work both down and * up from there in 1/16th allowed space sized steps. * * Note, we need to use VA-bits - 1 when calculating the allowed * virtual address space for an identity mapping because the upper * half of the virtual address space is the two's complement of the * lower and won't match physical addresses. */ bits = vm->va_bits - 1; bits = vm->pa_bits < bits ? vm->pa_bits : bits; end = 1ul << bits; start = end * 5 / 8; step = end / 16; for (offset = 0; offset < end - start; offset += step) { if (ucall_mmio_init(vm, start - offset)) return; if (ucall_mmio_init(vm, start + offset)) return; } TEST_ASSERT(false, "Can't find a ucall mmio address"); } } void ucall_uninit(struct kvm_vm *vm) { ucall_type = 0; sync_global_to_guest(vm, ucall_type); ucall_exit_mmio_addr = 0; sync_global_to_guest(vm, ucall_exit_mmio_addr); } static void ucall_pio_exit(struct ucall *uc) { #ifdef __x86_64__ asm volatile("in %[port], %%al" : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax"); #endif } static void ucall_mmio_exit(struct ucall *uc) { *ucall_exit_mmio_addr = (vm_vaddr_t)uc; } void ucall(uint64_t cmd, int nargs, ...) { struct ucall uc = { .cmd = cmd, }; va_list va; int i; nargs = nargs <= UCALL_MAX_ARGS ? nargs : UCALL_MAX_ARGS; va_start(va, nargs); for (i = 0; i < nargs; ++i) uc.args[i] = va_arg(va, uint64_t); va_end(va); switch (ucall_type) { case UCALL_PIO: ucall_pio_exit(&uc); break; case UCALL_MMIO: ucall_mmio_exit(&uc); break; }; } uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc) { struct kvm_run *run = vcpu_state(vm, vcpu_id); memset(uc, 0, sizeof(*uc)); #ifdef __x86_64__ if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) { struct kvm_regs regs; vcpu_regs_get(vm, vcpu_id, ®s); memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc)); return uc->cmd; } #endif if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO && run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) { vm_vaddr_t gva; TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8, "Unexpected ucall exit mmio address access"); gva = *(vm_vaddr_t *)run->mmio.data; memcpy(uc, addr_gva2hva(vm, gva), sizeof(*uc)); } return uc->cmd; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
You can’t perform that action at this time.