-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirt.c
317 lines (269 loc) · 9.91 KB
/
virt.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <linux/kvm.h>
#include <asm/processor-flags.h>
#define EFER_SCE 1
#define EFER_LME (1U << 8)
#define EFER_LMA (1U << 10)
#define EFER_NXE (1U << 11)
/* 64-bit page * entry bits */
#define PDE64_PRESENT 1
#define PDE64_RW (1U << 1)
#define PDE64_USER (1U << 2)
#define PDE64_ACCESSED (1U << 5)
#define PDE64_DIRTY (1U << 6)
#define PDE64_PS (1U << 7)
#define PDE64_G (1U << 8)
// TODO all of these should be shared with the linker script somehow
#define KERN_CODE_SIZE 0x10000
#define KERN_CODE_START 0x100000
#define KERN_DATA_SIZE 0x10000
#define KERN_DATA_START 0x200000
#define KERN_PML4_START (KERN_DATA_START + KERN_DATA_SIZE - 0x1000)
#define KERN_PDPT_START (KERN_PML4_START - 0x1000)
#define KERN_PD_START (KERN_PDPT_START - 0x1000)
#define KERN_GDT_SIZE 0x1000
#define KERN_GDT_START (KERN_PD_START - KERN_GDT_SIZE)
#define KERN_TSS_START (KERN_PD_START - 0x1000)
#define KERN_STACK_START (KERN_GDT_START - 0x2000)
#define USER_MEM_SIZE 0x10000
#define USER_MEM_START 0x0
struct gdt_desc {
uint16_t limit0;
uint16_t base0;
unsigned base1:8, s:1, type:4, dpl:2, p:1;
unsigned limit1:4, avl:1, l:1, db:1, g:1, base2:8;
uint32_t base3;
uint32_t zero1;
} __attribute__((packed));
void create_gdt_desc (uint8_t *kern_mem, struct kvm_segment *seg)
{
struct gdt_desc *desc = (struct gdt_desc *)(kern_mem + KERN_DATA_START - KERN_GDT_START + (seg->selector >> 3) * 8);
desc->limit0 = seg->limit & 0xFFFF;
desc->base0 = seg->base & 0xFFFF;
desc->base1 = seg->base >> 16;
desc->s = seg->s;
desc->type = seg->type;
desc->dpl = seg->dpl;
desc->p = seg->present;
desc->limit1 = seg->limit >> 16;
desc->l = seg->l;
desc->db = seg->db;
desc->g = seg->g;
desc->base2 = seg->base >> 24;
if (!seg->s)
desc->base3 = seg->base >> 32;
}
void handleHypercall (struct kvm_run *run)
{
if (run->io.port < 2) // I/O
{
if (run->io.direction == KVM_EXIT_IO_IN) // Input
fprintf (stderr, "INPUT\n");
else
{
FILE *output = run->io.port == 0 ? stdout : stderr;
fprintf (output, "OUTPUT '");
fwrite (((uint8_t *)run) + run->io.data_offset, 1, run->io.size, output);
fprintf (output, "' (%d)\n", *(((uint8_t *)run) + run->io.data_offset));
}
}
}
int main (int argc, char **argv)
{
int fd = open(argv[1], O_RDONLY, 0);
assert (fd != -1);
struct stat st;
stat(argv[1], &st);
size_t code_size = st.st_size;
assert (code_size < KERN_CODE_SIZE);
uint8_t *code_mem = mmap(NULL, code_size, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);
int kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
assert (kvm != -1);
int ret = ioctl(kvm, KVM_GET_API_VERSION, NULL);
assert (ret == 12);
int vmfd = ioctl(kvm, KVM_CREATE_VM, (unsigned long)0);
assert (vmfd != -1);
uint8_t *kern_mem = mmap(NULL, KERN_DATA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
assert (kern_mem);
uint8_t *user_mem = mmap(NULL, USER_MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
assert (user_mem);
#define PRINT_STACK() \
for (int i=0; i<100; i++) \
printf ("%02x ", *(kern_mem + KERN_STACK_START - KERN_DATA_START + i - 100)); \
printf ("\n");
int slots = ioctl(kvm, KVM_CHECK_EXTENSION, KVM_CAP_NR_MEMSLOTS);
assert (slots > 2);
struct kvm_userspace_memory_region kernel_code_region = {
.slot = 0,
.flags = 0,
.guest_phys_addr = KERN_CODE_START,
.memory_size = KERN_CODE_SIZE,
.userspace_addr = (uint64_t)code_mem,
};
struct kvm_userspace_memory_region kernel_data_region = {
.slot = 1,
.flags = 0,
.guest_phys_addr = KERN_DATA_START,
.memory_size = KERN_DATA_SIZE,
.userspace_addr = (uint64_t)kern_mem,
};
struct kvm_userspace_memory_region user_region = {
.slot = 2,
.flags = 0,
.guest_phys_addr = USER_MEM_START,
.memory_size = USER_MEM_SIZE,
.userspace_addr = (uint64_t)user_mem,
};
assert (!ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &kernel_code_region));
assert (!ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &kernel_data_region));
assert (!ioctl(vmfd, KVM_SET_USER_MEMORY_REGION, &user_region));
int vcpufd = ioctl(vmfd, KVM_CREATE_VCPU, (unsigned long)0);
assert (vcpufd != -1);
int mmap_size = ioctl(kvm, KVM_GET_VCPU_MMAP_SIZE, NULL);
assert (mmap_size >= sizeof(struct kvm_run));
struct kvm_run *run = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpufd, 0);
struct kvm_sregs sregs;
ioctl(vcpufd, KVM_GET_SREGS, &sregs);
uint64_t pml4_addr = KERN_PML4_START; // Top level page table
uint64_t *pml4 = (void *)(kern_mem - KERN_DATA_START + pml4_addr);
uint64_t pdpt_addr = KERN_PDPT_START; // Level 3
uint64_t *pdpt = (void *)(kern_mem - KERN_DATA_START + pdpt_addr);
uint64_t pd_addr = KERN_PD_START; // Level 2
uint64_t *pd = (void *)(kern_mem - KERN_DATA_START + pd_addr);
pml4[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pdpt_addr;
pdpt[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | pd_addr;
pd[0] = PDE64_PRESENT | PDE64_RW | PDE64_USER | PDE64_PS;
pd[1] = PDE64_PRESENT | PDE64_RW | PDE64_USER | PDE64_PS;
sregs.cr3 = pml4_addr;
sregs.cr4 = X86_CR4_PAE; // Physical Address Extensions
sregs.cr0 = X86_CR0_PE // protection
| X86_CR0_MP // monitor coprocessor
| X86_CR0_ET // extension type
| X86_CR0_NE // numeric error
| X86_CR0_WP // write protect
| X86_CR0_AM // alignment mask
| X86_CR0_PG; // paging
sregs.efer = EFER_LME // long mode enable
| EFER_LMA; // long mode active
// Need this to indicate everything is long mode
struct kvm_segment seg = {
.base = 0,
.limit = 0xffffffff,
.selector = 1 << 3,
.present = 1,
.type = 11, /* Code: execute, read, accessed */
.dpl = 0,
.db = 0,
.s = 1, /* Code/data */
.l = 1,
.g = 1, /* 4KB granularity */
};
sregs.cs = seg;
create_gdt_desc (kern_mem, &seg);
seg.type = 3; /* Data: read/write, accessed */
seg.selector = 2 << 3;
sregs.ds = sregs.es = sregs.fs = sregs.gs = sregs.ss = seg;
create_gdt_desc (kern_mem, &seg);
struct kvm_segment tss_seg = {
.base = KERN_TSS_START,
.limit = 0x67,
.selector = 0x18,
.present = 1,
.type = 11, /* execute, read, accessed */
.dpl = 0,
.db = 0,
.s = 0,
.l = 0,
.g = 0,
};
sregs.tr = tss_seg;
create_gdt_desc (kern_mem, &tss_seg);
sregs.gdt.base = KERN_GDT_START;
sregs.gdt.limit = KERN_GDT_SIZE;
ioctl(vcpufd, KVM_SET_SREGS, &sregs);
struct kvm_regs regs = {
.rip = KERN_CODE_START,
.rflags = 0x2,
.rsp = KERN_STACK_START
};
ioctl(vcpufd, KVM_SET_REGS, ®s);
#define PRINT_REGS() \
ioctl(vcpufd, KVM_GET_REGS, ®s); \
printf ("Regs: rsp:%llx rbp:%llx rip:%llx\n", regs.rsp, regs.rbp, regs.rip);
while (1) {
assert (ioctl(vcpufd, KVM_RUN, NULL) != -1);
switch (run->exit_reason) {
case KVM_EXIT_UNKNOWN:
printf ("KVM_EXIT_UNKNOWN\n");
break;
case KVM_EXIT_IO:
/* printf ("KVM_EXIT_IO d:%d s:%d p:%d c:%d o:%lld\n", run->io.direction, run->io.size, run->io.port, run->io.count, run->io.data_offset); */
handleHypercall (run);
break;
case KVM_EXIT_DEBUG:
printf ("KVM_EXIT_DEBUG\n");
break;
case KVM_EXIT_HLT:
printf ("KVM_EXIT_HLT\n");
return 0;
case KVM_EXIT_MMIO:
printf ("KVM_EXIT_MMIO addr:%llx data:%x write:%d\n", run->mmio.phys_addr, run->mmio.data[0], run->mmio.is_write);
break;
case KVM_EXIT_IRQ_WINDOW_OPEN:
printf ("KVM_EXIT_IRQ_WINDOW_OPEN\n");
break;
case KVM_EXIT_SHUTDOWN:
printf ("KVM_EXIT_SHUTDOWN\n");
break;
case KVM_EXIT_FAIL_ENTRY:
errx(1, "KVM_EXIT_FAIL_ENTRY: hardware_entry_failure_reason = 0x%llx",
(unsigned long long)run->fail_entry.hardware_entry_failure_reason);
case KVM_EXIT_INTR:
printf ("KVM_EXIT_INTR\n");
break;
case KVM_EXIT_SET_TPR:
printf ("KVM_EXIT_SET_TPR\n");
break;
case KVM_EXIT_TPR_ACCESS:
printf ("KVM_EXIT_TPR_ACCESS\n");
break;
case KVM_EXIT_NMI:
printf ("KVM_EXIT_NMI\n");
break;
case KVM_EXIT_INTERNAL_ERROR:
errx(1, "KVM_EXIT_INTERNAL_ERROR: suberror = 0x%x", run->internal.suberror);
break;
case KVM_EXIT_OSI:
printf ("KVM_EXIT_OSI\n");
break;
case KVM_EXIT_PAPR_HCALL:
printf ("KVM_EXIT_PAPR_HCALL\n");
break;
case KVM_EXIT_WATCHDOG:
printf ("KVM_EXIT_WATCHDOG\n");
break;
case KVM_EXIT_EPR:
printf ("KVM_EXIT_EPR\n");
break;
case KVM_EXIT_SYSTEM_EVENT:
printf ("KVM_EXIT_SYSTEM_EVENT\n");
break;
case KVM_EXIT_IOAPIC_EOI:
printf ("KVM_EXIT_IOAPIC_EOI\n");
break;
case KVM_EXIT_HYPERV:
printf ("KVM_EXIT_HYPERV\n");
break;
default:
printf ("EXIT %d\n", run->exit_reason);
}
}
}