xref: /netbsd-src/tests/lib/libnvmm/h_mem_assist.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: h_mem_assist.c,v 1.18 2019/11/22 10:26:32 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 
51 static uint8_t mmiobuf[PAGE_SIZE];
52 static uint8_t *instbuf;
53 
54 /* -------------------------------------------------------------------------- */
55 
56 static void
57 mem_callback(struct nvmm_mem *mem)
58 {
59 	size_t off;
60 
61 	if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
62 		printf("Out of page\n");
63 		exit(-1);
64 	}
65 
66 	off = mem->gpa - 0x1000;
67 
68 	printf("-> gpa = %p\n", (void *)mem->gpa);
69 
70 	if (mem->write) {
71 		memcpy(mmiobuf + off, mem->data, mem->size);
72 	} else {
73 		memcpy(mem->data, mmiobuf + off, mem->size);
74 	}
75 }
76 
77 static int
78 handle_memory(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
79 {
80 	int ret;
81 
82 	ret = nvmm_assist_mem(mach, vcpu);
83 	if (ret == -1) {
84 		err(errno, "nvmm_assist_mem");
85 	}
86 
87 	return 0;
88 }
89 
90 static void
91 run_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
92 {
93 	struct nvmm_vcpu_exit *exit = vcpu->exit;
94 
95 	while (1) {
96 		if (nvmm_vcpu_run(mach, vcpu) == -1)
97 			err(errno, "nvmm_vcpu_run");
98 
99 		switch (exit->reason) {
100 		case NVMM_VCPU_EXIT_NONE:
101 			break;
102 
103 		case NVMM_VCPU_EXIT_RDMSR:
104 			/* Stop here. */
105 			return;
106 
107 		case NVMM_VCPU_EXIT_MEMORY:
108 			handle_memory(mach, vcpu);
109 			break;
110 
111 		case NVMM_VCPU_EXIT_SHUTDOWN:
112 			printf("Shutting down!\n");
113 			return;
114 
115 		default:
116 			printf("Invalid VMEXIT: 0x%lx\n", exit->reason);
117 			return;
118 		}
119 	}
120 }
121 
122 static struct nvmm_assist_callbacks callbacks = {
123 	.io = NULL,
124 	.mem = mem_callback
125 };
126 
127 /* -------------------------------------------------------------------------- */
128 
129 struct test {
130 	const char *name;
131 	uint8_t *code_begin;
132 	uint8_t *code_end;
133 	uint64_t wanted;
134 	uint64_t off;
135 };
136 
137 static void
138 run_test(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu,
139     const struct test *test)
140 {
141 	uint64_t *res;
142 	size_t size;
143 
144 	size = (size_t)test->code_end - (size_t)test->code_begin;
145 
146 	memset(mmiobuf, 0, PAGE_SIZE);
147 	memcpy(instbuf, test->code_begin, size);
148 
149 	run_machine(mach, vcpu);
150 
151 	res = (uint64_t *)(mmiobuf + test->off);
152 	if (*res == test->wanted) {
153 		printf("Test '%s' passed\n", test->name);
154 	} else {
155 		printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
156 		    test->wanted, *res);
157 	}
158 }
159 
160 /* -------------------------------------------------------------------------- */
161 
162 extern uint8_t test1_begin, test1_end;
163 extern uint8_t test2_begin, test2_end;
164 extern uint8_t test3_begin, test3_end;
165 extern uint8_t test4_begin, test4_end;
166 extern uint8_t test5_begin, test5_end;
167 extern uint8_t test6_begin, test6_end;
168 extern uint8_t test7_begin, test7_end;
169 extern uint8_t test8_begin, test8_end;
170 extern uint8_t test9_begin, test9_end;
171 extern uint8_t test10_begin, test10_end;
172 extern uint8_t test11_begin, test11_end;
173 extern uint8_t test12_begin, test12_end;
174 extern uint8_t test13_begin, test13_end;
175 extern uint8_t test14_begin, test14_end;
176 extern uint8_t test_64bit_15_begin, test_64bit_15_end;
177 extern uint8_t test_64bit_16_begin, test_64bit_16_end;
178 
179 static const struct test tests64[] = {
180 	{ "64bit test1 - MOV", &test1_begin, &test1_end, 0x3004, 0 },
181 	{ "64bit test2 - OR",  &test2_begin, &test2_end, 0x16FF, 0 },
182 	{ "64bit test3 - AND", &test3_begin, &test3_end, 0x1FC0, 0 },
183 	{ "64bit test4 - XOR", &test4_begin, &test4_end, 0x10CF, 0 },
184 	{ "64bit test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00, 0 },
185 	{ "64bit test6 - DMO", &test6_begin, &test6_end, 0xFFAB, 0 },
186 	{ "64bit test7 - STOS", &test7_begin, &test7_end, 0x00123456, 0 },
187 	{ "64bit test8 - LODS", &test8_begin, &test8_end, 0x12345678, 0 },
188 	{ "64bit test9 - MOVS", &test9_begin, &test9_end, 0x12345678, 0 },
189 	{ "64bit test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078, 0 },
190 	{ "64bit test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678, 0 },
191 	{ "64bit test12 - CMP", &test12_begin, &test12_end, 0x00000001, 0 },
192 	{ "64bit test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF, 0 },
193 	{ "64bit test14 - TEST", &test14_begin, &test14_end, 0x00000001, 0 },
194 	{ "64bit test15 - XCHG", &test_64bit_15_begin, &test_64bit_15_end, 0x123456, 0 },
195 	{ "64bit test16 - XCHG", &test_64bit_16_begin, &test_64bit_16_end,
196 	  0x123456, 0 },
197 	{ NULL, NULL, NULL, -1, 0 }
198 };
199 
200 static void
201 init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
202 {
203 	seg->selector = sel;
204 	seg->attrib.type = type;
205 	seg->attrib.s = (type & 0b10000) != 0;
206 	seg->attrib.dpl = 0;
207 	seg->attrib.p = 1;
208 	seg->attrib.avl = 1;
209 	seg->attrib.l = 1;
210 	seg->attrib.def = 0;
211 	seg->attrib.g = 1;
212 	seg->limit = 0x0000FFFF;
213 	seg->base = 0x00000000;
214 }
215 
216 static void
217 reset_machine64(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
218 {
219 	struct nvmm_x64_state *state = vcpu->state;
220 
221 	if (nvmm_vcpu_getstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
222 		err(errno, "nvmm_vcpu_getstate");
223 
224 	memset(state, 0, sizeof(*state));
225 
226 	/* Default. */
227 	state->gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
228 	init_seg(&state->segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
229 	init_seg(&state->segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
230 	init_seg(&state->segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
231 	init_seg(&state->segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
232 	init_seg(&state->segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
233 	init_seg(&state->segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
234 
235 	/* Blank. */
236 	init_seg(&state->segs[NVMM_X64_SEG_GDT], 0, 0);
237 	init_seg(&state->segs[NVMM_X64_SEG_IDT], 0, 0);
238 	init_seg(&state->segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
239 	init_seg(&state->segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
240 
241 	/* Protected mode enabled. */
242 	state->crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
243 
244 	/* 64bit mode enabled. */
245 	state->crs[NVMM_X64_CR_CR4] = CR4_PAE;
246 	state->msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
247 
248 	/* Stolen from x86/pmap.c */
249 #define	PATENTRY(n, type)	(type << ((n) * 8))
250 #define	PAT_UC		0x0ULL
251 #define	PAT_WC		0x1ULL
252 #define	PAT_WT		0x4ULL
253 #define	PAT_WP		0x5ULL
254 #define	PAT_WB		0x6ULL
255 #define	PAT_UCMINUS	0x7ULL
256 	state->msrs[NVMM_X64_MSR_PAT] =
257 	    PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
258 	    PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
259 	    PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
260 	    PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
261 
262 	/* Page tables. */
263 	state->crs[NVMM_X64_CR_CR3] = 0x3000;
264 
265 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
266 
267 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
268 		err(errno, "nvmm_vcpu_setstate");
269 }
270 
271 static void
272 map_pages64(struct nvmm_machine *mach)
273 {
274 	pt_entry_t *L4, *L3, *L2, *L1;
275 	int ret;
276 
277 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
278 	    -1, 0);
279 	if (instbuf == MAP_FAILED)
280 		err(errno, "mmap");
281 
282 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
283 		err(errno, "nvmm_hva_map");
284 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
285 	    PROT_READ|PROT_EXEC);
286 	if (ret == -1)
287 		err(errno, "nvmm_gpa_map");
288 
289 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
290 	    -1, 0);
291 	if (L4 == MAP_FAILED)
292 		err(errno, "mmap");
293 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
294 	    -1, 0);
295 	if (L3 == MAP_FAILED)
296 		err(errno, "mmap");
297 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
298 	    -1, 0);
299 	if (L2 == MAP_FAILED)
300 		err(errno, "mmap");
301 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
302 	    -1, 0);
303 	if (L1 == MAP_FAILED)
304 		err(errno, "mmap");
305 
306 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
307 		err(errno, "nvmm_hva_map");
308 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
309 		err(errno, "nvmm_hva_map");
310 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
311 		err(errno, "nvmm_hva_map");
312 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
313 		err(errno, "nvmm_hva_map");
314 
315 	ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE,
316 	    PROT_READ|PROT_WRITE);
317 	if (ret == -1)
318 		err(errno, "nvmm_gpa_map");
319 	ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE,
320 	    PROT_READ|PROT_WRITE);
321 	if (ret == -1)
322 		err(errno, "nvmm_gpa_map");
323 	ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE,
324 	    PROT_READ|PROT_WRITE);
325 	if (ret == -1)
326 		err(errno, "nvmm_gpa_map");
327 	ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE,
328 	    PROT_READ|PROT_WRITE);
329 	if (ret == -1)
330 		err(errno, "nvmm_gpa_map");
331 
332 	memset(L4, 0, PAGE_SIZE);
333 	memset(L3, 0, PAGE_SIZE);
334 	memset(L2, 0, PAGE_SIZE);
335 	memset(L1, 0, PAGE_SIZE);
336 
337 	L4[0] = PTE_P | PTE_W | 0x4000;
338 	L3[0] = PTE_P | PTE_W | 0x5000;
339 	L2[0] = PTE_P | PTE_W | 0x6000;
340 	L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
341 	L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
342 }
343 
344 /*
345  * 0x1000: MMIO address, unmapped
346  * 0x2000: Instructions, mapped
347  * 0x3000: L4
348  * 0x4000: L3
349  * 0x5000: L2
350  * 0x6000: L1
351  */
352 static void
353 test_vm64(void)
354 {
355 	struct nvmm_machine mach;
356 	struct nvmm_vcpu vcpu;
357 	size_t i;
358 
359 	if (nvmm_machine_create(&mach) == -1)
360 		err(errno, "nvmm_machine_create");
361 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
362 		err(errno, "nvmm_vcpu_create");
363 	nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks);
364 	map_pages64(&mach);
365 
366 	for (i = 0; tests64[i].name != NULL; i++) {
367 		reset_machine64(&mach, &vcpu);
368 		run_test(&mach, &vcpu, &tests64[i]);
369 	}
370 
371 	if (nvmm_vcpu_destroy(&mach, &vcpu) == -1)
372 		err(errno, "nvmm_vcpu_destroy");
373 	if (nvmm_machine_destroy(&mach) == -1)
374 		err(errno, "nvmm_machine_destroy");
375 }
376 
377 /* -------------------------------------------------------------------------- */
378 
379 extern uint8_t test_16bit_1_begin, test_16bit_1_end;
380 extern uint8_t test_16bit_2_begin, test_16bit_2_end;
381 extern uint8_t test_16bit_3_begin, test_16bit_3_end;
382 extern uint8_t test_16bit_4_begin, test_16bit_4_end;
383 extern uint8_t test_16bit_5_begin, test_16bit_5_end;
384 extern uint8_t test_16bit_6_begin, test_16bit_6_end;
385 
386 static const struct test tests16[] = {
387 	{ "16bit test1 - MOV single", &test_16bit_1_begin, &test_16bit_1_end,
388 	  0x023, 0x10f1 - 0x1000 },
389 	{ "16bit test2 - MOV dual", &test_16bit_2_begin, &test_16bit_2_end,
390 	  0x123, 0x10f3 - 0x1000 },
391 	{ "16bit test3 - MOV dual+disp", &test_16bit_3_begin, &test_16bit_3_end,
392 	  0x678, 0x10f1 - 0x1000 },
393 	{ "16bit test4 - Mixed", &test_16bit_4_begin, &test_16bit_4_end,
394 	  0x1011, 0x10f6 - 0x1000 },
395 	{ "16bit test5 - disp16-only", &test_16bit_5_begin, &test_16bit_5_end,
396 	  0x12, 0x1234 - 0x1000 },
397 	{ "16bit test6 - XCHG", &test_16bit_6_begin, &test_16bit_6_end,
398 	  0x1234, 0x1234 - 0x1000 },
399 	{ NULL, NULL, NULL, -1, -1 }
400 };
401 
402 static void
403 reset_machine16(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
404 {
405 	struct nvmm_x64_state *state = vcpu->state;
406 
407 	if (nvmm_vcpu_getstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
408 		err(errno, "nvmm_vcpu_getstate");
409 
410 	state->segs[NVMM_X64_SEG_CS].base = 0;
411 	state->segs[NVMM_X64_SEG_CS].limit = 0x2FFF;
412 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
413 
414 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
415 		err(errno, "nvmm_vcpu_setstate");
416 }
417 
418 static void
419 map_pages16(struct nvmm_machine *mach)
420 {
421 	int ret;
422 
423 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
424 	    -1, 0);
425 	if (instbuf == MAP_FAILED)
426 		err(errno, "mmap");
427 
428 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
429 		err(errno, "nvmm_hva_map");
430 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
431 	    PROT_READ|PROT_EXEC);
432 	if (ret == -1)
433 		err(errno, "nvmm_gpa_map");
434 }
435 
436 /*
437  * 0x1000: MMIO address, unmapped
438  * 0x2000: Instructions, mapped
439  */
440 static void
441 test_vm16(void)
442 {
443 	struct nvmm_machine mach;
444 	struct nvmm_vcpu vcpu;
445 	size_t i;
446 
447 	if (nvmm_machine_create(&mach) == -1)
448 		err(errno, "nvmm_machine_create");
449 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
450 		err(errno, "nvmm_vcpu_create");
451 	nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks);
452 	map_pages16(&mach);
453 
454 	for (i = 0; tests16[i].name != NULL; i++) {
455 		reset_machine16(&mach, &vcpu);
456 		run_test(&mach, &vcpu, &tests16[i]);
457 	}
458 
459 	if (nvmm_vcpu_destroy(&mach, &vcpu) == -1)
460 		err(errno, "nvmm_vcpu_destroy");
461 	if (nvmm_machine_destroy(&mach) == -1)
462 		err(errno, "nvmm_machine_destroy");
463 }
464 
465 /* -------------------------------------------------------------------------- */
466 
467 int main(int argc, char *argv[])
468 {
469 	if (nvmm_init() == -1)
470 		err(errno, "nvmm_init");
471 	test_vm64();
472 	test_vm16();
473 	return 0;
474 }
475