Home | History | Annotate | Line # | Download | only in libnvmm
h_mem_assist.c revision 1.8
      1 /*	$NetBSD: h_mem_assist.c,v 1.8 2019/03/21 20:21:41 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018 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 static void
     55 init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
     56 {
     57 	seg->selector = sel;
     58 	seg->attrib.type = type;
     59 	seg->attrib.s = (type & 0b10000) != 0;
     60 	seg->attrib.dpl = 0;
     61 	seg->attrib.p = 1;
     62 	seg->attrib.avl = 1;
     63 	seg->attrib.l = 1;
     64 	seg->attrib.def = 0;
     65 	seg->attrib.g = 1;
     66 	seg->limit = 0x0000FFFF;
     67 	seg->base = 0x00000000;
     68 }
     69 
     70 static void
     71 reset_machine(struct nvmm_machine *mach)
     72 {
     73 	struct nvmm_x64_state state;
     74 
     75 	memset(&state, 0, sizeof(state));
     76 
     77 	/* Default. */
     78 	state.gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
     79 	init_seg(&state.segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
     80 	init_seg(&state.segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     81 	init_seg(&state.segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     82 	init_seg(&state.segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     83 	init_seg(&state.segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     84 	init_seg(&state.segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     85 
     86 	/* Blank. */
     87 	init_seg(&state.segs[NVMM_X64_SEG_GDT], 0, 0);
     88 	init_seg(&state.segs[NVMM_X64_SEG_IDT], 0, 0);
     89 	init_seg(&state.segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
     90 	init_seg(&state.segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
     91 
     92 	/* Protected mode enabled. */
     93 	state.crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
     94 
     95 	/* 64bit mode enabled. */
     96 	state.crs[NVMM_X64_CR_CR4] = CR4_PAE;
     97 	state.msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
     98 
     99 	/* Stolen from x86/pmap.c */
    100 #define	PATENTRY(n, type)	(type << ((n) * 8))
    101 #define	PAT_UC		0x0ULL
    102 #define	PAT_WC		0x1ULL
    103 #define	PAT_WT		0x4ULL
    104 #define	PAT_WP		0x5ULL
    105 #define	PAT_WB		0x6ULL
    106 #define	PAT_UCMINUS	0x7ULL
    107 	state.msrs[NVMM_X64_MSR_PAT] =
    108 	    PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
    109 	    PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
    110 	    PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
    111 	    PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
    112 
    113 	/* Page tables. */
    114 	state.crs[NVMM_X64_CR_CR3] = 0x3000;
    115 
    116 	state.gprs[NVMM_X64_GPR_RIP] = 0x2000;
    117 
    118 	if (nvmm_vcpu_setstate(mach, 0, &state, NVMM_X64_STATE_ALL) == -1)
    119 		err(errno, "nvmm_vcpu_setstate");
    120 }
    121 
    122 static void
    123 map_pages(struct nvmm_machine *mach)
    124 {
    125 	pt_entry_t *L4, *L3, *L2, *L1;
    126 
    127 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    128 	    -1, 0);
    129 	if (instbuf == MAP_FAILED)
    130 		err(errno, "mmap");
    131 
    132 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
    133 		err(errno, "nvmm_hva_map");
    134 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
    135 	    PROT_READ|PROT_EXEC);
    136 	if (ret == -1)
    137 		err(errno, "nvmm_gpa_map");
    138 
    139 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    140 	    -1, 0);
    141 	if (L4 == MAP_FAILED)
    142 		err(errno, "mmap");
    143 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    144 	    -1, 0);
    145 	if (L3 == MAP_FAILED)
    146 		err(errno, "mmap");
    147 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    148 	    -1, 0);
    149 	if (L2 == MAP_FAILED)
    150 		err(errno, "mmap");
    151 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    152 	    -1, 0);
    153 	if (L1 == MAP_FAILED)
    154 		err(errno, "mmap");
    155 
    156 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
    157 		err(errno, "nvmm_hva_map");
    158 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
    159 		err(errno, "nvmm_hva_map");
    160 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
    161 		err(errno, "nvmm_hva_map");
    162 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
    163 		err(errno, "nvmm_hva_map");
    164 
    165 	ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE,
    166 	    PROT_READ|PROT_WRITE);
    167 	if (ret == -1)
    168 		err(errno, "nvmm_gpa_map");
    169 	ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE,
    170 	    PROT_READ|PROT_WRITE);
    171 	if (ret == -1)
    172 		err(errno, "nvmm_gpa_map");
    173 	ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE,
    174 	    PROT_READ|PROT_WRITE);
    175 	if (ret == -1)
    176 		err(errno, "nvmm_gpa_map");
    177 	ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE,
    178 	    PROT_READ|PROT_WRITE);
    179 	if (ret == -1)
    180 		err(errno, "nvmm_gpa_map");
    181 
    182 	memset(L4, 0, PAGE_SIZE);
    183 	memset(L3, 0, PAGE_SIZE);
    184 	memset(L2, 0, PAGE_SIZE);
    185 	memset(L1, 0, PAGE_SIZE);
    186 
    187 	L4[0] = PTE_P | PTE_W | 0x4000;
    188 	L3[0] = PTE_P | PTE_W | 0x5000;
    189 	L2[0] = PTE_P | PTE_W | 0x6000;
    190 	L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
    191 	L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
    192 }
    193 
    194 /* -------------------------------------------------------------------------- */
    195 
    196 static void
    197 mem_callback(struct nvmm_mem *mem)
    198 {
    199 	size_t off;
    200 
    201 	if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
    202 		printf("Out of page\n");
    203 		exit(-1);
    204 	}
    205 
    206 	off = mem->gpa - 0x1000;
    207 
    208 	printf("-> gpa = %p\n", (void *)mem->gpa);
    209 
    210 	if (mem->write) {
    211 		memcpy(mmiobuf + off, mem->data, mem->size);
    212 	} else {
    213 		memcpy(mem->data, mmiobuf + off, mem->size);
    214 	}
    215 }
    216 
    217 static int
    218 handle_memory(struct nvmm_machine *mach, struct nvmm_exit *exit)
    219 {
    220 	int ret;
    221 
    222 	ret = nvmm_assist_mem(mach, 0, exit);
    223 	if (ret == -1) {
    224 		err(errno, "nvmm_assist_mem");
    225 	}
    226 
    227 	return 0;
    228 }
    229 
    230 static void
    231 run_machine(struct nvmm_machine *mach)
    232 {
    233 	struct nvmm_exit exit;
    234 
    235 	while (1) {
    236 		if (nvmm_vcpu_run(mach, 0, &exit) == -1)
    237 			err(errno, "nvmm_vcpu_run");
    238 
    239 		switch (exit.reason) {
    240 		case NVMM_EXIT_NONE:
    241 			break;
    242 
    243 		case NVMM_EXIT_MSR:
    244 			/* Stop here. */
    245 			return;
    246 
    247 		case NVMM_EXIT_MEMORY:
    248 			handle_memory(mach, &exit);
    249 			break;
    250 
    251 		case NVMM_EXIT_SHUTDOWN:
    252 			printf("Shutting down!\n");
    253 			return;
    254 
    255 		default:
    256 			printf("Invalid!\n");
    257 			return;
    258 		}
    259 	}
    260 }
    261 
    262 /* -------------------------------------------------------------------------- */
    263 
    264 struct test {
    265 	const char *name;
    266 	uint8_t *code_begin;
    267 	uint8_t *code_end;
    268 	uint64_t wanted;
    269 };
    270 
    271 static void
    272 run_test(struct nvmm_machine *mach, const struct test *test)
    273 {
    274 	uint64_t *res;
    275 	size_t size;
    276 
    277 	size = (size_t)test->code_end - (size_t)test->code_begin;
    278 
    279 	reset_machine(mach);
    280 
    281 	memset(mmiobuf, 0, PAGE_SIZE);
    282 	memcpy(instbuf, test->code_begin, size);
    283 
    284 	run_machine(mach);
    285 
    286 	res = (uint64_t *)mmiobuf;
    287 	if (*res == test->wanted) {
    288 		printf("Test '%s' passed\n", test->name);
    289 	} else {
    290 		printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
    291 		    test->wanted, *res);
    292 	}
    293 }
    294 
    295 /* -------------------------------------------------------------------------- */
    296 
    297 extern uint8_t test1_begin, test1_end;
    298 extern uint8_t test2_begin, test2_end;
    299 extern uint8_t test3_begin, test3_end;
    300 extern uint8_t test4_begin, test4_end;
    301 extern uint8_t test5_begin, test5_end;
    302 extern uint8_t test6_begin, test6_end;
    303 extern uint8_t test7_begin, test7_end;
    304 extern uint8_t test8_begin, test8_end;
    305 extern uint8_t test9_begin, test9_end;
    306 extern uint8_t test10_begin, test10_end;
    307 extern uint8_t test11_begin, test11_end;
    308 extern uint8_t test12_begin, test12_end;
    309 extern uint8_t test13_begin, test13_end;
    310 extern uint8_t test14_begin, test14_end;
    311 
    312 static const struct test tests[] = {
    313 	{ "test1 - MOV", &test1_begin, &test1_end, 0x3004 },
    314 	{ "test2 - OR",  &test2_begin, &test2_end, 0x16FF },
    315 	{ "test3 - AND", &test3_begin, &test3_end, 0x1FC0 },
    316 	{ "test4 - XOR", &test4_begin, &test4_end, 0x10CF },
    317 	{ "test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00 },
    318 	{ "test6 - DMO", &test6_begin, &test6_end, 0xFFAB },
    319 	{ "test7 - STOS", &test7_begin, &test7_end, 0x00123456 },
    320 	{ "test8 - LODS", &test8_begin, &test8_end, 0x12345678 },
    321 	{ "test9 - MOVS", &test9_begin, &test9_end, 0x12345678 },
    322 	{ "test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078 },
    323 	{ "test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678 },
    324 	{ "test12 - CMP", &test12_begin, &test12_end, 0x00000001 },
    325 	{ "test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF },
    326 	{ "test14 - TEST", &test14_begin, &test14_end, 0x00000001 },
    327 	{ NULL, NULL, NULL, -1 }
    328 };
    329 
    330 static const struct nvmm_callbacks callbacks = {
    331 	.io = NULL,
    332 	.mem = mem_callback
    333 };
    334 
    335 /*
    336  * 0x1000: MMIO address, unmapped
    337  * 0x2000: Instructions, mapped
    338  * 0x3000: L4
    339  * 0x4000: L3
    340  * 0x5000: L2
    341  * 0x6000: L1
    342  */
    343 int main(int argc, char *argv[])
    344 {
    345 	struct nvmm_machine mach;
    346 	size_t i;
    347 
    348 	if (nvmm_machine_create(&mach) == -1)
    349 		err(errno, "nvmm_machine_create");
    350 	if (nvmm_vcpu_create(&mach, 0) == -1)
    351 		err(errno, "nvmm_vcpu_create");
    352 	nvmm_callbacks_register(&callbacks);
    353 	map_pages(&mach);
    354 
    355 	for (i = 0; tests[i].name != NULL; i++) {
    356 		run_test(&mach, &tests[i]);
    357 	}
    358 
    359 	return 0;
    360 }
    361