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