-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvmrun.c
195 lines (172 loc) · 4.61 KB
/
kvmrun.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/ptrace.h>
#include <linux/kvm.h>
static const char *kvm_exit_reasons[] = {
"KVM_EXIT_UNKNOWN",
"KVM_EXIT_EXCEPTION",
"KVM_EXIT_IO",
"KVM_EXIT_HYPERCALL",
"KVM_EXIT_DEBUG",
"KVM_EXIT_HLT",
"KVM_EXIT_MMIO",
"KVM_EXIT_IRQ_WINDOW_OPEN",
"KVM_EXIT_SHUTDOWN",
"KVM_EXIT_FAIL_ENTRY",
"KVM_EXIT_INTR",
"KVM_EXIT_SET_TPR",
"KVM_EXIT_TPR_ACCESS",
"KVM_EXIT_S390_SIEIC",
"KVM_EXIT_S390_RESET",
"KVM_EXIT_DCR",
"KVM_EXIT_NMI",
"KVM_EXIT_INTERNAL_ERROR",
"KVM_EXIT_OSI",
"KVM_EXIT_PAPR_HCALL",
"KVM_EXIT_S390_UCONTROL",
"KVM_EXIT_WATCHDOG",
"KVM_EXIT_S390_TSCH",
"KVM_EXIT_EPR",
"KVM_EXIT_SYSTEM_EVENT",
"KVM_EXIT_S390_STSI",
"KVM_EXIT_IOAPIC_EOI",
"KVM_EXIT_HYPERV",
"KVM_EXIT_ARM_NISV",
};
#define CONSOLE_FIFO_ADDR 0x01000000
int main(int argc, char *argv[])
{
struct kvm_userspace_memory_region region = {};
struct kvm_run *vcpu_kvm_run;
int kvmfd, vmfd, vcpufd, codefd;
int ret, offset = 0;
void *memory;
if (argc != 2) {
printf("usage: %s <binary-file>\n", argv[0]);
return -EINVAL;
}
codefd = open(argv[1], O_RDONLY);
if (codefd < 0) {
perror("Unable to open file");
return -EIO;
}
/* KVM interface */
kvmfd = open("/dev/kvm", O_RDWR | O_CLOEXEC);
if (kvmfd < 0) {
perror("Unable to open /dev/kvm");
return -EINVAL;
}
/* Create a new virtual machine */
vmfd = ioctl(kvmfd, KVM_CREATE_VM, (unsigned long)0);
if (vmfd < 0) {
perror("Unable to create VM");
return -EIO;
}
/* Anonymous mapping to map an area of the process's virtual memory
* in page sized unit. "physical" address space as seen by the VM.
*/
memory = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (memory == NULL) {
perror("Unable to mmap");
return -ENOMEM;
}
/* Copy guest code into guest memory */
while ((ret = read(codefd, memory + offset, 0x1000)) > 0)
offset += ret;
close(codefd);
/* Tell KVM about this memory, mapped to VM physical address 0
* since vCPU PC is reset to 0 on init.
*/
region.slot = 0;
region.guest_phys_addr = 0x0;
region.memory_size = 0x1000,
region.userspace_addr = (uint64_t)memory,
ret = ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, ®ion);
if (ret < 0) {
perror("Unable to set VM memory");
return -EIO;
}
/* Create one vCPU (virtual CPU) */
vcpufd = ioctl(vmfd, KVM_CREATE_VCPU, (unsigned long)0);
if (vcpufd < 0) {
perror("Unable to create vCPU");
return -EIO;
}
/* A kvm_run data structure is used to communicate information about
* the vCPU between the kernel and user space. Its size usually exceeds
* size of the kvm_run structure since the kernel will also use that
* space to store other transient structures. Size can be retrieved via
* KVM_GET_VCPU_MMAP_SIZE ioctl.
*/
ret = ioctl(kvmfd, KVM_GET_VCPU_MMAP_SIZE, NULL);
vcpu_kvm_run = mmap(NULL, ret, PROT_READ | PROT_WRITE,
MAP_SHARED, vcpufd, 0);
if (vcpu_kvm_run == NULL) {
perror("Unable to mmap vcpufd");
return -EIO;
}
/* Initialize vCPU */
#if defined(__arm__) || defined(__aarch64__)
{
struct kvm_vcpu_init vcpu_init;
/* Initialize registers, target, etc... */
ioctl(vmfd, KVM_ARM_PREFERRED_TARGET, &vcpu_init);
ret = ioctl(vcpufd, KVM_ARM_VCPU_INIT, &vcpu_init);
if (ret < 0) {
perror("vCPU init error");
return -EIO;
}
}
#elif defined(__x86_64__)
{
struct kvm_sregs sregs;
/* The value of the CS register at reset is FFFF change to 0 */
ioctl(vcpufd, KVM_GET_SREGS, &sregs);
sregs.cs.base = 0;
sregs.cs.selector = 0;
ioctl(vcpufd, KVM_SET_SREGS, &sregs);
/* IP point to start, reset RFLAGS */
struct kvm_regs regs = {.rip = 0x0000, .rflags = 0x2 };
ioctl(vcpufd, KVM_SET_REGS, ®s);
}
#endif
/* Let's go, run the vCPU */
while (1) {
ret = ioctl(vcpufd, KVM_RUN, NULL);
if (ret < 0) {
perror("KVM_RUN error");
return -EIO;
}
/* information stored in the shared kvm_run struct */
/*printf("vmexit reason: %s\n",
kvm_exit_reasons[vcpu_kvm_run->exit_reason]);*/
switch (vcpu_kvm_run->exit_reason) {
case KVM_EXIT_INTERNAL_ERROR:
printf("Error!\n");
return -EIO;
case KVM_EXIT_SHUTDOWN:
case KVM_EXIT_HLT:
printf("Guest halted!\n");
return 0;
case KVM_EXIT_MMIO:
if (vcpu_kvm_run->mmio.phys_addr == CONSOLE_FIFO_ADDR
&& vcpu_kvm_run->mmio.is_write) {
/* Trap & Emulate console write */
putc(vcpu_kvm_run->mmio.data[0], stdout);
break;
}
/* fall through */
default:
printf("unhandled vmexit, reason: %s\n",
kvm_exit_reasons[vcpu_kvm_run->exit_reason]);
break;
}
}
}