h_io_assist.c revision 1.11 1 /* $NetBSD: h_io_assist.c,v 1.11 2019/10/27 07:08:15 maxv Exp $ */
2
3 /*
4 * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Maxime Villard.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/mman.h>
42 #include <machine/segments.h>
43 #include <machine/psl.h>
44 #include <machine/pte.h>
45 #include <x86/specialreg.h>
46
47 #include <nvmm.h>
48
49 #define PAGE_SIZE 4096
50 #define IO_SIZE 128
51
52 static char iobuf[IO_SIZE];
53
54 static char *databuf;
55 static uint8_t *instbuf;
56
57 static void
58 init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
59 {
60 seg->selector = sel;
61 seg->attrib.type = type;
62 seg->attrib.s = (type & 0b10000) != 0;
63 seg->attrib.dpl = 0;
64 seg->attrib.p = 1;
65 seg->attrib.avl = 1;
66 seg->attrib.l = 1;
67 seg->attrib.def = 0;
68 seg->attrib.g = 1;
69 seg->limit = 0x0000FFFF;
70 seg->base = 0x00000000;
71 }
72
73 static void
74 reset_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
75 {
76 struct nvmm_x64_state *state = vcpu->state;
77
78 memset(state, 0, sizeof(*state));
79
80 /* Default. */
81 state->gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
82 init_seg(&state->segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
83 init_seg(&state->segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
84 init_seg(&state->segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
85 init_seg(&state->segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
86 init_seg(&state->segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
87 init_seg(&state->segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
88
89 /* Blank. */
90 init_seg(&state->segs[NVMM_X64_SEG_GDT], 0, 0);
91 init_seg(&state->segs[NVMM_X64_SEG_IDT], 0, 0);
92 init_seg(&state->segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
93 init_seg(&state->segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
94
95 /* Protected mode enabled. */
96 state->crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
97
98 /* 64bit mode enabled. */
99 state->crs[NVMM_X64_CR_CR4] = CR4_PAE;
100 state->msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
101
102 /* Stolen from x86/pmap.c */
103 #define PATENTRY(n, type) (type << ((n) * 8))
104 #define PAT_UC 0x0ULL
105 #define PAT_WC 0x1ULL
106 #define PAT_WT 0x4ULL
107 #define PAT_WP 0x5ULL
108 #define PAT_WB 0x6ULL
109 #define PAT_UCMINUS 0x7ULL
110 state->msrs[NVMM_X64_MSR_PAT] =
111 PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
112 PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
113 PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
114 PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
115
116 /* Page tables. */
117 state->crs[NVMM_X64_CR_CR3] = 0x3000;
118
119 state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
120
121 if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
122 err(errno, "nvmm_vcpu_setstate");
123 }
124
125 static void
126 map_pages(struct nvmm_machine *mach)
127 {
128 pt_entry_t *L4, *L3, *L2, *L1;
129 int ret;
130
131 instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
132 -1, 0);
133 if (instbuf == MAP_FAILED)
134 err(errno, "mmap");
135 databuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
136 -1, 0);
137 if (databuf == MAP_FAILED)
138 err(errno, "mmap");
139
140 if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
141 err(errno, "nvmm_hva_map");
142 if (nvmm_hva_map(mach, (uintptr_t)databuf, PAGE_SIZE) == -1)
143 err(errno, "nvmm_hva_map");
144 ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
145 PROT_READ|PROT_EXEC);
146 if (ret == -1)
147 err(errno, "nvmm_gpa_map");
148 ret = nvmm_gpa_map(mach, (uintptr_t)databuf, 0x1000, PAGE_SIZE,
149 PROT_READ|PROT_WRITE);
150 if (ret == -1)
151 err(errno, "nvmm_gpa_map");
152
153 L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
154 -1, 0);
155 if (L4 == MAP_FAILED)
156 err(errno, "mmap");
157 L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
158 -1, 0);
159 if (L3 == MAP_FAILED)
160 err(errno, "mmap");
161 L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
162 -1, 0);
163 if (L2 == MAP_FAILED)
164 err(errno, "mmap");
165 L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
166 -1, 0);
167 if (L1 == MAP_FAILED)
168 err(errno, "mmap");
169
170 if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
171 err(errno, "nvmm_hva_map");
172 if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
173 err(errno, "nvmm_hva_map");
174 if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
175 err(errno, "nvmm_hva_map");
176 if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
177 err(errno, "nvmm_hva_map");
178
179 ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE,
180 PROT_READ|PROT_WRITE);
181 if (ret == -1)
182 err(errno, "nvmm_gpa_map");
183 ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE,
184 PROT_READ|PROT_WRITE);
185 if (ret == -1)
186 err(errno, "nvmm_gpa_map");
187 ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE,
188 PROT_READ|PROT_WRITE);
189 if (ret == -1)
190 err(errno, "nvmm_gpa_map");
191 ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE,
192 PROT_READ|PROT_WRITE);
193 if (ret == -1)
194 err(errno, "nvmm_gpa_map");
195
196 memset(L4, 0, PAGE_SIZE);
197 memset(L3, 0, PAGE_SIZE);
198 memset(L2, 0, PAGE_SIZE);
199 memset(L1, 0, PAGE_SIZE);
200
201 L4[0] = PTE_P | PTE_W | 0x4000;
202 L3[0] = PTE_P | PTE_W | 0x5000;
203 L2[0] = PTE_P | PTE_W | 0x6000;
204 L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
205 L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
206 }
207
208 /* -------------------------------------------------------------------------- */
209
210 static size_t iobuf_off = 0;
211
212 static void
213 io_callback(struct nvmm_io *io)
214 {
215 if (io->port != 123) {
216 printf("Wrong port\n");
217 exit(-1);
218 }
219
220 if (io->in) {
221 memcpy(io->data, iobuf + iobuf_off, io->size);
222 } else {
223 memcpy(iobuf + iobuf_off, io->data, io->size);
224 }
225 iobuf_off += io->size;
226
227 }
228
229 static int
230 handle_io(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
231 {
232 int ret;
233
234 ret = nvmm_assist_io(mach, vcpu);
235 if (ret == -1) {
236 err(errno, "nvmm_assist_io");
237 }
238
239 return 0;
240 }
241
242 static void
243 run_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
244 {
245 struct nvmm_vcpu_exit *exit = vcpu->exit;
246
247 while (1) {
248 if (nvmm_vcpu_run(mach, vcpu) == -1)
249 err(errno, "nvmm_vcpu_run");
250
251 switch (exit->reason) {
252 case NVMM_VCPU_EXIT_NONE:
253 break;
254
255 case NVMM_VCPU_EXIT_RDMSR:
256 /* Stop here. */
257 return;
258
259 case NVMM_VCPU_EXIT_IO:
260 handle_io(mach, vcpu);
261 break;
262
263 case NVMM_VCPU_EXIT_SHUTDOWN:
264 printf("Shutting down!\n");
265 return;
266
267 default:
268 printf("Invalid!\n");
269 return;
270 }
271 }
272 }
273
274 /* -------------------------------------------------------------------------- */
275
276 struct test {
277 const char *name;
278 uint8_t *code_begin;
279 uint8_t *code_end;
280 const char *wanted;
281 bool in;
282 };
283
284 static void
285 run_test(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu,
286 const struct test *test)
287 {
288 size_t size;
289 char *res;
290
291 size = (size_t)test->code_end - (size_t)test->code_begin;
292
293 reset_machine(mach, vcpu);
294
295 iobuf_off = 0;
296 memset(iobuf, 0, IO_SIZE);
297 memset(databuf, 0, PAGE_SIZE);
298 memcpy(instbuf, test->code_begin, size);
299
300 if (test->in) {
301 strcpy(iobuf, test->wanted);
302 } else {
303 strcpy(databuf, test->wanted);
304 }
305
306 run_machine(mach, vcpu);
307
308 if (test->in) {
309 res = databuf;
310 } else {
311 res = iobuf;
312 }
313
314 if (!strcmp(res, test->wanted)) {
315 printf("Test '%s' passed\n", test->name);
316 } else {
317 printf("Test '%s' failed, wanted '%s', got '%s'\n", test->name,
318 test->wanted, res);
319 }
320 }
321
322 /* -------------------------------------------------------------------------- */
323
324 extern uint8_t test1_begin, test1_end;
325 extern uint8_t test2_begin, test2_end;
326 extern uint8_t test3_begin, test3_end;
327 extern uint8_t test4_begin, test4_end;
328 extern uint8_t test5_begin, test5_end;
329 extern uint8_t test6_begin, test6_end;
330 extern uint8_t test7_begin, test7_end;
331 extern uint8_t test8_begin, test8_end;
332 extern uint8_t test9_begin, test9_end;
333 extern uint8_t test10_begin, test10_end;
334 extern uint8_t test11_begin, test11_end;
335 extern uint8_t test12_begin, test12_end;
336
337 static const struct test tests[] = {
338 { "test1 - INB", &test1_begin, &test1_end, "12", true },
339 { "test2 - INW", &test2_begin, &test2_end, "1234", true },
340 { "test3 - INL", &test3_begin, &test3_end, "12345678", true },
341 { "test4 - INSB+REP", &test4_begin, &test4_end, "12345", true },
342 { "test5 - INSW+REP", &test5_begin, &test5_end,
343 "Comment est votre blanquette", true },
344 { "test6 - INSL+REP", &test6_begin, &test6_end,
345 "123456789abcdefghijklmnopqrs", true },
346 { "test7 - OUTB", &test7_begin, &test7_end, "12", false },
347 { "test8 - OUTW", &test8_begin, &test8_end, "1234", false },
348 { "test9 - OUTL", &test9_begin, &test9_end, "12345678", false },
349 { "test10 - OUTSB+REP", &test10_begin, &test10_end, "12345", false },
350 { "test11 - OUTSW+REP", &test11_begin, &test11_end,
351 "Ah, Herr Bramard", false },
352 { "test12 - OUTSL+REP", &test12_begin, &test12_end,
353 "123456789abcdefghijklmnopqrs", false },
354 { NULL, NULL, NULL, NULL, false }
355 };
356
357 static struct nvmm_assist_callbacks callbacks = {
358 .io = io_callback,
359 .mem = NULL
360 };
361
362 /*
363 * 0x1000: Data, mapped
364 * 0x2000: Instructions, mapped
365 * 0x3000: L4
366 * 0x4000: L3
367 * 0x5000: L2
368 * 0x6000: L1
369 */
370 int main(int argc, char *argv[])
371 {
372 struct nvmm_machine mach;
373 struct nvmm_vcpu vcpu;
374 size_t i;
375
376 if (nvmm_init() == -1)
377 err(errno, "nvmm_init");
378 if (nvmm_machine_create(&mach) == -1)
379 err(errno, "nvmm_machine_create");
380 if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
381 err(errno, "nvmm_vcpu_create");
382 nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks);
383 map_pages(&mach);
384
385 for (i = 0; tests[i].name != NULL; i++) {
386 run_test(&mach, &vcpu, &tests[i]);
387 }
388
389 return 0;
390 }
391