xref: /netbsd-src/tests/lib/libnvmm/h_mem_assist.c (revision e6b71c148711fd8b0d6ef07729e79923e9aa810f)
1*e6b71c14Sreinoud /*	$NetBSD: h_mem_assist.c,v 1.20 2020/12/27 20:56:14 reinoud Exp $	*/
2db0dfdd0Smaxv 
39159f72fSmaxv /*
44a2e4dc3Smaxv  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
59159f72fSmaxv  * All rights reserved.
69159f72fSmaxv  *
74a2e4dc3Smaxv  * This code is part of the NVMM hypervisor.
89159f72fSmaxv  *
99159f72fSmaxv  * Redistribution and use in source and binary forms, with or without
109159f72fSmaxv  * modification, are permitted provided that the following conditions
119159f72fSmaxv  * are met:
129159f72fSmaxv  * 1. Redistributions of source code must retain the above copyright
139159f72fSmaxv  *    notice, this list of conditions and the following disclaimer.
149159f72fSmaxv  * 2. Redistributions in binary form must reproduce the above copyright
159159f72fSmaxv  *    notice, this list of conditions and the following disclaimer in the
169159f72fSmaxv  *    documentation and/or other materials provided with the distribution.
179159f72fSmaxv  *
184a2e4dc3Smaxv  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194a2e4dc3Smaxv  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
204a2e4dc3Smaxv  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214a2e4dc3Smaxv  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
224a2e4dc3Smaxv  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
234a2e4dc3Smaxv  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
244a2e4dc3Smaxv  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
254a2e4dc3Smaxv  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
264a2e4dc3Smaxv  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
274a2e4dc3Smaxv  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
284a2e4dc3Smaxv  * SUCH DAMAGE.
299159f72fSmaxv  */
309159f72fSmaxv 
319159f72fSmaxv #include <stdio.h>
329159f72fSmaxv #include <stdlib.h>
339159f72fSmaxv #include <stdint.h>
349159f72fSmaxv #include <stdbool.h>
359159f72fSmaxv #include <unistd.h>
369159f72fSmaxv #include <string.h>
379159f72fSmaxv #include <err.h>
389159f72fSmaxv #include <errno.h>
399159f72fSmaxv #include <sys/types.h>
409159f72fSmaxv #include <sys/mman.h>
419159f72fSmaxv #include <machine/segments.h>
429159f72fSmaxv #include <machine/psl.h>
439159f72fSmaxv #include <machine/pte.h>
449159f72fSmaxv #include <x86/specialreg.h>
459159f72fSmaxv 
469159f72fSmaxv #include <nvmm.h>
479159f72fSmaxv 
489159f72fSmaxv #define PAGE_SIZE 4096
499159f72fSmaxv 
509159f72fSmaxv static uint8_t mmiobuf[PAGE_SIZE];
519159f72fSmaxv static uint8_t *instbuf;
529159f72fSmaxv 
53416eaf02Smaxv /* -------------------------------------------------------------------------- */
54416eaf02Smaxv 
55416eaf02Smaxv static void
mem_callback(struct nvmm_mem * mem)56416eaf02Smaxv mem_callback(struct nvmm_mem *mem)
57416eaf02Smaxv {
58416eaf02Smaxv 	size_t off;
59416eaf02Smaxv 
60416eaf02Smaxv 	if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
61416eaf02Smaxv 		printf("Out of page\n");
62416eaf02Smaxv 		exit(-1);
63416eaf02Smaxv 	}
64416eaf02Smaxv 
65416eaf02Smaxv 	off = mem->gpa - 0x1000;
66416eaf02Smaxv 
67416eaf02Smaxv 	printf("-> gpa = %p\n", (void *)mem->gpa);
68416eaf02Smaxv 
69416eaf02Smaxv 	if (mem->write) {
70416eaf02Smaxv 		memcpy(mmiobuf + off, mem->data, mem->size);
71416eaf02Smaxv 	} else {
72416eaf02Smaxv 		memcpy(mem->data, mmiobuf + off, mem->size);
73416eaf02Smaxv 	}
74416eaf02Smaxv }
75416eaf02Smaxv 
76416eaf02Smaxv static int
handle_memory(struct nvmm_machine * mach,struct nvmm_vcpu * vcpu)77416eaf02Smaxv handle_memory(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
78416eaf02Smaxv {
79416eaf02Smaxv 	int ret;
80416eaf02Smaxv 
81416eaf02Smaxv 	ret = nvmm_assist_mem(mach, vcpu);
82416eaf02Smaxv 	if (ret == -1) {
83416eaf02Smaxv 		err(errno, "nvmm_assist_mem");
84416eaf02Smaxv 	}
85416eaf02Smaxv 
86416eaf02Smaxv 	return 0;
87416eaf02Smaxv }
88416eaf02Smaxv 
89416eaf02Smaxv static void
run_machine(struct nvmm_machine * mach,struct nvmm_vcpu * vcpu)90416eaf02Smaxv run_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
91416eaf02Smaxv {
92f9fb7866Smaxv 	struct nvmm_vcpu_exit *exit = vcpu->exit;
93416eaf02Smaxv 
94416eaf02Smaxv 	while (1) {
95416eaf02Smaxv 		if (nvmm_vcpu_run(mach, vcpu) == -1)
96416eaf02Smaxv 			err(errno, "nvmm_vcpu_run");
97416eaf02Smaxv 
98416eaf02Smaxv 		switch (exit->reason) {
99f9fb7866Smaxv 		case NVMM_VCPU_EXIT_NONE:
100416eaf02Smaxv 			break;
101416eaf02Smaxv 
102f9fb7866Smaxv 		case NVMM_VCPU_EXIT_RDMSR:
103416eaf02Smaxv 			/* Stop here. */
104416eaf02Smaxv 			return;
105416eaf02Smaxv 
106f9fb7866Smaxv 		case NVMM_VCPU_EXIT_MEMORY:
107416eaf02Smaxv 			handle_memory(mach, vcpu);
108416eaf02Smaxv 			break;
109416eaf02Smaxv 
110f9fb7866Smaxv 		case NVMM_VCPU_EXIT_SHUTDOWN:
111416eaf02Smaxv 			printf("Shutting down!\n");
112416eaf02Smaxv 			return;
113416eaf02Smaxv 
114416eaf02Smaxv 		default:
1156f40daa3Smaxv 			printf("Invalid VMEXIT: 0x%lx\n", exit->reason);
116416eaf02Smaxv 			return;
117416eaf02Smaxv 		}
118416eaf02Smaxv 	}
119416eaf02Smaxv }
120416eaf02Smaxv 
121e6f32a58Smaxv static struct nvmm_assist_callbacks callbacks = {
122416eaf02Smaxv 	.io = NULL,
123416eaf02Smaxv 	.mem = mem_callback
124416eaf02Smaxv };
125416eaf02Smaxv 
126416eaf02Smaxv /* -------------------------------------------------------------------------- */
127416eaf02Smaxv 
128416eaf02Smaxv struct test {
129416eaf02Smaxv 	const char *name;
130416eaf02Smaxv 	uint8_t *code_begin;
131416eaf02Smaxv 	uint8_t *code_end;
132416eaf02Smaxv 	uint64_t wanted;
133416eaf02Smaxv 	uint64_t off;
134416eaf02Smaxv };
135416eaf02Smaxv 
136416eaf02Smaxv static void
run_test(struct nvmm_machine * mach,struct nvmm_vcpu * vcpu,const struct test * test)137416eaf02Smaxv run_test(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu,
138416eaf02Smaxv     const struct test *test)
139416eaf02Smaxv {
140416eaf02Smaxv 	uint64_t *res;
141416eaf02Smaxv 	size_t size;
142416eaf02Smaxv 
143416eaf02Smaxv 	size = (size_t)test->code_end - (size_t)test->code_begin;
144416eaf02Smaxv 
145416eaf02Smaxv 	memset(mmiobuf, 0, PAGE_SIZE);
146416eaf02Smaxv 	memcpy(instbuf, test->code_begin, size);
147416eaf02Smaxv 
148416eaf02Smaxv 	run_machine(mach, vcpu);
149416eaf02Smaxv 
150416eaf02Smaxv 	res = (uint64_t *)(mmiobuf + test->off);
151416eaf02Smaxv 	if (*res == test->wanted) {
152416eaf02Smaxv 		printf("Test '%s' passed\n", test->name);
153416eaf02Smaxv 	} else {
154416eaf02Smaxv 		printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
155416eaf02Smaxv 		    test->wanted, *res);
156416eaf02Smaxv 	}
157416eaf02Smaxv }
158416eaf02Smaxv 
159416eaf02Smaxv /* -------------------------------------------------------------------------- */
160416eaf02Smaxv 
161416eaf02Smaxv extern uint8_t test1_begin, test1_end;
162416eaf02Smaxv extern uint8_t test2_begin, test2_end;
163416eaf02Smaxv extern uint8_t test3_begin, test3_end;
164416eaf02Smaxv extern uint8_t test4_begin, test4_end;
165416eaf02Smaxv extern uint8_t test5_begin, test5_end;
166416eaf02Smaxv extern uint8_t test6_begin, test6_end;
167416eaf02Smaxv extern uint8_t test7_begin, test7_end;
168416eaf02Smaxv extern uint8_t test8_begin, test8_end;
169416eaf02Smaxv extern uint8_t test9_begin, test9_end;
170416eaf02Smaxv extern uint8_t test10_begin, test10_end;
171416eaf02Smaxv extern uint8_t test11_begin, test11_end;
172416eaf02Smaxv extern uint8_t test12_begin, test12_end;
173416eaf02Smaxv extern uint8_t test13_begin, test13_end;
174416eaf02Smaxv extern uint8_t test14_begin, test14_end;
175c496a7b1Smaxv extern uint8_t test_64bit_15_begin, test_64bit_15_end;
176c496a7b1Smaxv extern uint8_t test_64bit_16_begin, test_64bit_16_end;
177*e6b71c14Sreinoud extern uint8_t test17_begin, test17_end;
178416eaf02Smaxv 
179416eaf02Smaxv static const struct test tests64[] = {
1806f40daa3Smaxv 	{ "64bit test1 - MOV", &test1_begin, &test1_end, 0x3004, 0 },
1816f40daa3Smaxv 	{ "64bit test2 - OR",  &test2_begin, &test2_end, 0x16FF, 0 },
1826f40daa3Smaxv 	{ "64bit test3 - AND", &test3_begin, &test3_end, 0x1FC0, 0 },
1836f40daa3Smaxv 	{ "64bit test4 - XOR", &test4_begin, &test4_end, 0x10CF, 0 },
1846f40daa3Smaxv 	{ "64bit test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00, 0 },
1856f40daa3Smaxv 	{ "64bit test6 - DMO", &test6_begin, &test6_end, 0xFFAB, 0 },
1866f40daa3Smaxv 	{ "64bit test7 - STOS", &test7_begin, &test7_end, 0x00123456, 0 },
1876f40daa3Smaxv 	{ "64bit test8 - LODS", &test8_begin, &test8_end, 0x12345678, 0 },
1886f40daa3Smaxv 	{ "64bit test9 - MOVS", &test9_begin, &test9_end, 0x12345678, 0 },
1896f40daa3Smaxv 	{ "64bit test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078, 0 },
1906f40daa3Smaxv 	{ "64bit test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678, 0 },
1916f40daa3Smaxv 	{ "64bit test12 - CMP", &test12_begin, &test12_end, 0x00000001, 0 },
1926f40daa3Smaxv 	{ "64bit test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF, 0 },
1936f40daa3Smaxv 	{ "64bit test14 - TEST", &test14_begin, &test14_end, 0x00000001, 0 },
1946f40daa3Smaxv 	{ "64bit test15 - XCHG", &test_64bit_15_begin, &test_64bit_15_end, 0x123456, 0 },
1956f40daa3Smaxv 	{ "64bit test16 - XCHG", &test_64bit_16_begin, &test_64bit_16_end,
196c496a7b1Smaxv 	  0x123456, 0 },
197*e6b71c14Sreinoud 	{ "64bit test17 - CMPS", &test17_begin, &test17_end, 0x00001, 0 },
19885f58959Schristos 	{ NULL, NULL, NULL, -1, 0 }
199416eaf02Smaxv };
200416eaf02Smaxv 
2019159f72fSmaxv static void
init_seg(struct nvmm_x64_state_seg * seg,int type,int sel)2029159f72fSmaxv init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
2039159f72fSmaxv {
2049159f72fSmaxv 	seg->selector = sel;
2059159f72fSmaxv 	seg->attrib.type = type;
2067a4f551dSmaxv 	seg->attrib.s = (type & 0b10000) != 0;
2079159f72fSmaxv 	seg->attrib.dpl = 0;
2089159f72fSmaxv 	seg->attrib.p = 1;
2099159f72fSmaxv 	seg->attrib.avl = 1;
2107a4f551dSmaxv 	seg->attrib.l = 1;
2117a4f551dSmaxv 	seg->attrib.def = 0;
2127a4f551dSmaxv 	seg->attrib.g = 1;
213a8470e38Smaxv 	seg->limit = 0x0000FFFF;
2149159f72fSmaxv 	seg->base = 0x00000000;
2159159f72fSmaxv }
2169159f72fSmaxv 
2179159f72fSmaxv static void
reset_machine64(struct nvmm_machine * mach,struct nvmm_vcpu * vcpu)218416eaf02Smaxv reset_machine64(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
2199159f72fSmaxv {
220d1002cd7Smaxv 	struct nvmm_x64_state *state = vcpu->state;
2219159f72fSmaxv 
2226f40daa3Smaxv 	if (nvmm_vcpu_getstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
2236f40daa3Smaxv 		err(errno, "nvmm_vcpu_getstate");
2246f40daa3Smaxv 
225d1002cd7Smaxv 	memset(state, 0, sizeof(*state));
2269159f72fSmaxv 
2279159f72fSmaxv 	/* Default. */
228d1002cd7Smaxv 	state->gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
229d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
230d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
231d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
232d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
233d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
234d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
2359159f72fSmaxv 
2369159f72fSmaxv 	/* Blank. */
237d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_GDT], 0, 0);
238d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_IDT], 0, 0);
239d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
240d1002cd7Smaxv 	init_seg(&state->segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
2419159f72fSmaxv 
2429159f72fSmaxv 	/* Protected mode enabled. */
243d1002cd7Smaxv 	state->crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
2449159f72fSmaxv 
2459159f72fSmaxv 	/* 64bit mode enabled. */
246d1002cd7Smaxv 	state->crs[NVMM_X64_CR_CR4] = CR4_PAE;
247d1002cd7Smaxv 	state->msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
2489159f72fSmaxv 
2499159f72fSmaxv 	/* Stolen from x86/pmap.c */
2509159f72fSmaxv #define	PATENTRY(n, type)	(type << ((n) * 8))
2519159f72fSmaxv #define	PAT_UC		0x0ULL
2529159f72fSmaxv #define	PAT_WC		0x1ULL
2539159f72fSmaxv #define	PAT_WT		0x4ULL
2549159f72fSmaxv #define	PAT_WP		0x5ULL
2559159f72fSmaxv #define	PAT_WB		0x6ULL
2569159f72fSmaxv #define	PAT_UCMINUS	0x7ULL
257d1002cd7Smaxv 	state->msrs[NVMM_X64_MSR_PAT] =
2589159f72fSmaxv 	    PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
2599159f72fSmaxv 	    PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
2609159f72fSmaxv 	    PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
2619159f72fSmaxv 	    PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
2629159f72fSmaxv 
2639159f72fSmaxv 	/* Page tables. */
264d1002cd7Smaxv 	state->crs[NVMM_X64_CR_CR3] = 0x3000;
2659159f72fSmaxv 
266d1002cd7Smaxv 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
2679159f72fSmaxv 
268d1002cd7Smaxv 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
2699159f72fSmaxv 		err(errno, "nvmm_vcpu_setstate");
2709159f72fSmaxv }
2719159f72fSmaxv 
2729159f72fSmaxv static void
map_pages64(struct nvmm_machine * mach)273416eaf02Smaxv map_pages64(struct nvmm_machine *mach)
2749159f72fSmaxv {
2759159f72fSmaxv 	pt_entry_t *L4, *L3, *L2, *L1;
2762603483eShtodd 	int ret;
2779159f72fSmaxv 
2789159f72fSmaxv 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
2799159f72fSmaxv 	    -1, 0);
2809159f72fSmaxv 	if (instbuf == MAP_FAILED)
2819159f72fSmaxv 		err(errno, "mmap");
2829159f72fSmaxv 
2839159f72fSmaxv 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
2849159f72fSmaxv 		err(errno, "nvmm_hva_map");
285e8b93c69Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
286e8b93c69Smaxv 	    PROT_READ|PROT_EXEC);
287e8b93c69Smaxv 	if (ret == -1)
2889159f72fSmaxv 		err(errno, "nvmm_gpa_map");
2899159f72fSmaxv 
2909159f72fSmaxv 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
2919159f72fSmaxv 	    -1, 0);
2929159f72fSmaxv 	if (L4 == MAP_FAILED)
2939159f72fSmaxv 		err(errno, "mmap");
2949159f72fSmaxv 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
2959159f72fSmaxv 	    -1, 0);
2969159f72fSmaxv 	if (L3 == MAP_FAILED)
2979159f72fSmaxv 		err(errno, "mmap");
2989159f72fSmaxv 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
2999159f72fSmaxv 	    -1, 0);
3009159f72fSmaxv 	if (L2 == MAP_FAILED)
3019159f72fSmaxv 		err(errno, "mmap");
3029159f72fSmaxv 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
3039159f72fSmaxv 	    -1, 0);
3049159f72fSmaxv 	if (L1 == MAP_FAILED)
3059159f72fSmaxv 		err(errno, "mmap");
3069159f72fSmaxv 
3079159f72fSmaxv 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
3089159f72fSmaxv 		err(errno, "nvmm_hva_map");
3099159f72fSmaxv 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
3109159f72fSmaxv 		err(errno, "nvmm_hva_map");
3119159f72fSmaxv 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
3129159f72fSmaxv 		err(errno, "nvmm_hva_map");
3139159f72fSmaxv 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
3149159f72fSmaxv 		err(errno, "nvmm_hva_map");
3159159f72fSmaxv 
316e8b93c69Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE,
317e8b93c69Smaxv 	    PROT_READ|PROT_WRITE);
318e8b93c69Smaxv 	if (ret == -1)
3199159f72fSmaxv 		err(errno, "nvmm_gpa_map");
320e8b93c69Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE,
321e8b93c69Smaxv 	    PROT_READ|PROT_WRITE);
322e8b93c69Smaxv 	if (ret == -1)
3239159f72fSmaxv 		err(errno, "nvmm_gpa_map");
324e8b93c69Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE,
325e8b93c69Smaxv 	    PROT_READ|PROT_WRITE);
326e8b93c69Smaxv 	if (ret == -1)
3279159f72fSmaxv 		err(errno, "nvmm_gpa_map");
328e8b93c69Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE,
329e8b93c69Smaxv 	    PROT_READ|PROT_WRITE);
330e8b93c69Smaxv 	if (ret == -1)
3319159f72fSmaxv 		err(errno, "nvmm_gpa_map");
3329159f72fSmaxv 
3339159f72fSmaxv 	memset(L4, 0, PAGE_SIZE);
3349159f72fSmaxv 	memset(L3, 0, PAGE_SIZE);
3359159f72fSmaxv 	memset(L2, 0, PAGE_SIZE);
3369159f72fSmaxv 	memset(L1, 0, PAGE_SIZE);
3379159f72fSmaxv 
338db0dfdd0Smaxv 	L4[0] = PTE_P | PTE_W | 0x4000;
339db0dfdd0Smaxv 	L3[0] = PTE_P | PTE_W | 0x5000;
340db0dfdd0Smaxv 	L2[0] = PTE_P | PTE_W | 0x6000;
341db0dfdd0Smaxv 	L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
342db0dfdd0Smaxv 	L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
3439159f72fSmaxv }
3449159f72fSmaxv 
3459159f72fSmaxv /*
3469159f72fSmaxv  * 0x1000: MMIO address, unmapped
3479159f72fSmaxv  * 0x2000: Instructions, mapped
3489159f72fSmaxv  * 0x3000: L4
3499159f72fSmaxv  * 0x4000: L3
3509159f72fSmaxv  * 0x5000: L2
3519159f72fSmaxv  * 0x6000: L1
3529159f72fSmaxv  */
353416eaf02Smaxv static void
test_vm64(void)354416eaf02Smaxv test_vm64(void)
3559159f72fSmaxv {
3569159f72fSmaxv 	struct nvmm_machine mach;
357d1002cd7Smaxv 	struct nvmm_vcpu vcpu;
3589159f72fSmaxv 	size_t i;
3599159f72fSmaxv 
3609159f72fSmaxv 	if (nvmm_machine_create(&mach) == -1)
3619159f72fSmaxv 		err(errno, "nvmm_machine_create");
362d1002cd7Smaxv 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
3639159f72fSmaxv 		err(errno, "nvmm_vcpu_create");
364e6f32a58Smaxv 	nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks);
365416eaf02Smaxv 	map_pages64(&mach);
3669159f72fSmaxv 
367416eaf02Smaxv 	for (i = 0; tests64[i].name != NULL; i++) {
368416eaf02Smaxv 		reset_machine64(&mach, &vcpu);
369416eaf02Smaxv 		run_test(&mach, &vcpu, &tests64[i]);
3709159f72fSmaxv 	}
3719159f72fSmaxv 
3726f40daa3Smaxv 	if (nvmm_vcpu_destroy(&mach, &vcpu) == -1)
3736f40daa3Smaxv 		err(errno, "nvmm_vcpu_destroy");
374416eaf02Smaxv 	if (nvmm_machine_destroy(&mach) == -1)
375416eaf02Smaxv 		err(errno, "nvmm_machine_destroy");
376416eaf02Smaxv }
377416eaf02Smaxv 
378416eaf02Smaxv /* -------------------------------------------------------------------------- */
379416eaf02Smaxv 
380416eaf02Smaxv extern uint8_t test_16bit_1_begin, test_16bit_1_end;
381416eaf02Smaxv extern uint8_t test_16bit_2_begin, test_16bit_2_end;
382416eaf02Smaxv extern uint8_t test_16bit_3_begin, test_16bit_3_end;
383416eaf02Smaxv extern uint8_t test_16bit_4_begin, test_16bit_4_end;
384416eaf02Smaxv extern uint8_t test_16bit_5_begin, test_16bit_5_end;
385c496a7b1Smaxv extern uint8_t test_16bit_6_begin, test_16bit_6_end;
386416eaf02Smaxv 
387416eaf02Smaxv static const struct test tests16[] = {
388416eaf02Smaxv 	{ "16bit test1 - MOV single", &test_16bit_1_begin, &test_16bit_1_end,
389416eaf02Smaxv 	  0x023, 0x10f1 - 0x1000 },
390416eaf02Smaxv 	{ "16bit test2 - MOV dual", &test_16bit_2_begin, &test_16bit_2_end,
391416eaf02Smaxv 	  0x123, 0x10f3 - 0x1000 },
392416eaf02Smaxv 	{ "16bit test3 - MOV dual+disp", &test_16bit_3_begin, &test_16bit_3_end,
393416eaf02Smaxv 	  0x678, 0x10f1 - 0x1000 },
394416eaf02Smaxv 	{ "16bit test4 - Mixed", &test_16bit_4_begin, &test_16bit_4_end,
395416eaf02Smaxv 	  0x1011, 0x10f6 - 0x1000 },
396416eaf02Smaxv 	{ "16bit test5 - disp16-only", &test_16bit_5_begin, &test_16bit_5_end,
397416eaf02Smaxv 	  0x12, 0x1234 - 0x1000 },
398c496a7b1Smaxv 	{ "16bit test6 - XCHG", &test_16bit_6_begin, &test_16bit_6_end,
399c496a7b1Smaxv 	  0x1234, 0x1234 - 0x1000 },
400416eaf02Smaxv 	{ NULL, NULL, NULL, -1, -1 }
401416eaf02Smaxv };
402416eaf02Smaxv 
403416eaf02Smaxv static void
reset_machine16(struct nvmm_machine * mach,struct nvmm_vcpu * vcpu)404416eaf02Smaxv reset_machine16(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
405416eaf02Smaxv {
406416eaf02Smaxv 	struct nvmm_x64_state *state = vcpu->state;
407416eaf02Smaxv 
408416eaf02Smaxv 	if (nvmm_vcpu_getstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
4096f40daa3Smaxv 		err(errno, "nvmm_vcpu_getstate");
410416eaf02Smaxv 
411416eaf02Smaxv 	state->segs[NVMM_X64_SEG_CS].base = 0;
4126f40daa3Smaxv 	state->segs[NVMM_X64_SEG_CS].limit = 0x2FFF;
413416eaf02Smaxv 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
414416eaf02Smaxv 
415416eaf02Smaxv 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
416416eaf02Smaxv 		err(errno, "nvmm_vcpu_setstate");
417416eaf02Smaxv }
418416eaf02Smaxv 
419416eaf02Smaxv static void
map_pages16(struct nvmm_machine * mach)420416eaf02Smaxv map_pages16(struct nvmm_machine *mach)
421416eaf02Smaxv {
422416eaf02Smaxv 	int ret;
423416eaf02Smaxv 
424416eaf02Smaxv 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
425416eaf02Smaxv 	    -1, 0);
426416eaf02Smaxv 	if (instbuf == MAP_FAILED)
427416eaf02Smaxv 		err(errno, "mmap");
428416eaf02Smaxv 
429416eaf02Smaxv 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
430416eaf02Smaxv 		err(errno, "nvmm_hva_map");
431416eaf02Smaxv 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
432416eaf02Smaxv 	    PROT_READ|PROT_EXEC);
433416eaf02Smaxv 	if (ret == -1)
434416eaf02Smaxv 		err(errno, "nvmm_gpa_map");
435416eaf02Smaxv }
436416eaf02Smaxv 
437416eaf02Smaxv /*
438416eaf02Smaxv  * 0x1000: MMIO address, unmapped
439416eaf02Smaxv  * 0x2000: Instructions, mapped
440416eaf02Smaxv  */
441416eaf02Smaxv static void
test_vm16(void)442416eaf02Smaxv test_vm16(void)
443416eaf02Smaxv {
444416eaf02Smaxv 	struct nvmm_machine mach;
445416eaf02Smaxv 	struct nvmm_vcpu vcpu;
446416eaf02Smaxv 	size_t i;
447416eaf02Smaxv 
448416eaf02Smaxv 	if (nvmm_machine_create(&mach) == -1)
449416eaf02Smaxv 		err(errno, "nvmm_machine_create");
450416eaf02Smaxv 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
451416eaf02Smaxv 		err(errno, "nvmm_vcpu_create");
452e6f32a58Smaxv 	nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks);
453416eaf02Smaxv 	map_pages16(&mach);
454416eaf02Smaxv 
455416eaf02Smaxv 	for (i = 0; tests16[i].name != NULL; i++) {
456416eaf02Smaxv 		reset_machine16(&mach, &vcpu);
457416eaf02Smaxv 		run_test(&mach, &vcpu, &tests16[i]);
458416eaf02Smaxv 	}
459416eaf02Smaxv 
4606f40daa3Smaxv 	if (nvmm_vcpu_destroy(&mach, &vcpu) == -1)
4616f40daa3Smaxv 		err(errno, "nvmm_vcpu_destroy");
462416eaf02Smaxv 	if (nvmm_machine_destroy(&mach) == -1)
463416eaf02Smaxv 		err(errno, "nvmm_machine_destroy");
464416eaf02Smaxv }
465416eaf02Smaxv 
466416eaf02Smaxv /* -------------------------------------------------------------------------- */
467416eaf02Smaxv 
main(int argc,char * argv[])468416eaf02Smaxv int main(int argc, char *argv[])
469416eaf02Smaxv {
470a6418236Smaxv 	if (nvmm_init() == -1)
471a6418236Smaxv 		err(errno, "nvmm_init");
472416eaf02Smaxv 	test_vm64();
473416eaf02Smaxv 	test_vm16();
4749159f72fSmaxv 	return 0;
4759159f72fSmaxv }
476