Home | History | Annotate | Line # | Download | only in libnvmm
h_mem_assist.c revision 1.7
      1 /*	$NetBSD: h_mem_assist.c,v 1.7 2019/03/19 19:23:39 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 	if (nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE, 0) == -1)
    135 		err(errno, "nvmm_gpa_map");
    136 
    137 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    138 	    -1, 0);
    139 	if (L4 == MAP_FAILED)
    140 		err(errno, "mmap");
    141 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    142 	    -1, 0);
    143 	if (L3 == MAP_FAILED)
    144 		err(errno, "mmap");
    145 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    146 	    -1, 0);
    147 	if (L2 == MAP_FAILED)
    148 		err(errno, "mmap");
    149 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    150 	    -1, 0);
    151 	if (L1 == MAP_FAILED)
    152 		err(errno, "mmap");
    153 
    154 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
    155 		err(errno, "nvmm_hva_map");
    156 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
    157 		err(errno, "nvmm_hva_map");
    158 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
    159 		err(errno, "nvmm_hva_map");
    160 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
    161 		err(errno, "nvmm_hva_map");
    162 
    163 	if (nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE, 0) == -1)
    164 		err(errno, "nvmm_gpa_map");
    165 	if (nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE, 0) == -1)
    166 		err(errno, "nvmm_gpa_map");
    167 	if (nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE, 0) == -1)
    168 		err(errno, "nvmm_gpa_map");
    169 	if (nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE, 0) == -1)
    170 		err(errno, "nvmm_gpa_map");
    171 
    172 	memset(L4, 0, PAGE_SIZE);
    173 	memset(L3, 0, PAGE_SIZE);
    174 	memset(L2, 0, PAGE_SIZE);
    175 	memset(L1, 0, PAGE_SIZE);
    176 
    177 	L4[0] = PTE_P | PTE_W | 0x4000;
    178 	L3[0] = PTE_P | PTE_W | 0x5000;
    179 	L2[0] = PTE_P | PTE_W | 0x6000;
    180 	L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
    181 	L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
    182 }
    183 
    184 /* -------------------------------------------------------------------------- */
    185 
    186 static void
    187 mem_callback(struct nvmm_mem *mem)
    188 {
    189 	size_t off;
    190 
    191 	if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
    192 		printf("Out of page\n");
    193 		exit(-1);
    194 	}
    195 
    196 	off = mem->gpa - 0x1000;
    197 
    198 	printf("-> gpa = %p\n", (void *)mem->gpa);
    199 
    200 	if (mem->write) {
    201 		memcpy(mmiobuf + off, mem->data, mem->size);
    202 	} else {
    203 		memcpy(mem->data, mmiobuf + off, mem->size);
    204 	}
    205 }
    206 
    207 static int
    208 handle_memory(struct nvmm_machine *mach, struct nvmm_exit *exit)
    209 {
    210 	int ret;
    211 
    212 	ret = nvmm_assist_mem(mach, 0, exit);
    213 	if (ret == -1) {
    214 		err(errno, "nvmm_assist_mem");
    215 	}
    216 
    217 	return 0;
    218 }
    219 
    220 static void
    221 run_machine(struct nvmm_machine *mach)
    222 {
    223 	struct nvmm_exit exit;
    224 
    225 	while (1) {
    226 		if (nvmm_vcpu_run(mach, 0, &exit) == -1)
    227 			err(errno, "nvmm_vcpu_run");
    228 
    229 		switch (exit.reason) {
    230 		case NVMM_EXIT_NONE:
    231 			break;
    232 
    233 		case NVMM_EXIT_MSR:
    234 			/* Stop here. */
    235 			return;
    236 
    237 		case NVMM_EXIT_MEMORY:
    238 			handle_memory(mach, &exit);
    239 			break;
    240 
    241 		case NVMM_EXIT_SHUTDOWN:
    242 			printf("Shutting down!\n");
    243 			return;
    244 
    245 		default:
    246 			printf("Invalid!\n");
    247 			return;
    248 		}
    249 	}
    250 }
    251 
    252 /* -------------------------------------------------------------------------- */
    253 
    254 struct test {
    255 	const char *name;
    256 	uint8_t *code_begin;
    257 	uint8_t *code_end;
    258 	uint64_t wanted;
    259 };
    260 
    261 static void
    262 run_test(struct nvmm_machine *mach, const struct test *test)
    263 {
    264 	uint64_t *res;
    265 	size_t size;
    266 
    267 	size = (size_t)test->code_end - (size_t)test->code_begin;
    268 
    269 	reset_machine(mach);
    270 
    271 	memset(mmiobuf, 0, PAGE_SIZE);
    272 	memcpy(instbuf, test->code_begin, size);
    273 
    274 	run_machine(mach);
    275 
    276 	res = (uint64_t *)mmiobuf;
    277 	if (*res == test->wanted) {
    278 		printf("Test '%s' passed\n", test->name);
    279 	} else {
    280 		printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
    281 		    test->wanted, *res);
    282 	}
    283 }
    284 
    285 /* -------------------------------------------------------------------------- */
    286 
    287 extern uint8_t test1_begin, test1_end;
    288 extern uint8_t test2_begin, test2_end;
    289 extern uint8_t test3_begin, test3_end;
    290 extern uint8_t test4_begin, test4_end;
    291 extern uint8_t test5_begin, test5_end;
    292 extern uint8_t test6_begin, test6_end;
    293 extern uint8_t test7_begin, test7_end;
    294 extern uint8_t test8_begin, test8_end;
    295 extern uint8_t test9_begin, test9_end;
    296 extern uint8_t test10_begin, test10_end;
    297 extern uint8_t test11_begin, test11_end;
    298 extern uint8_t test12_begin, test12_end;
    299 extern uint8_t test13_begin, test13_end;
    300 extern uint8_t test14_begin, test14_end;
    301 
    302 static const struct test tests[] = {
    303 	{ "test1 - MOV", &test1_begin, &test1_end, 0x3004 },
    304 	{ "test2 - OR",  &test2_begin, &test2_end, 0x16FF },
    305 	{ "test3 - AND", &test3_begin, &test3_end, 0x1FC0 },
    306 	{ "test4 - XOR", &test4_begin, &test4_end, 0x10CF },
    307 	{ "test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00 },
    308 	{ "test6 - DMO", &test6_begin, &test6_end, 0xFFAB },
    309 	{ "test7 - STOS", &test7_begin, &test7_end, 0x00123456 },
    310 	{ "test8 - LODS", &test8_begin, &test8_end, 0x12345678 },
    311 	{ "test9 - MOVS", &test9_begin, &test9_end, 0x12345678 },
    312 	{ "test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078 },
    313 	{ "test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678 },
    314 	{ "test12 - CMP", &test12_begin, &test12_end, 0x00000001 },
    315 	{ "test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF },
    316 	{ "test14 - TEST", &test14_begin, &test14_end, 0x00000001 },
    317 	{ NULL, NULL, NULL, -1 }
    318 };
    319 
    320 static const struct nvmm_callbacks callbacks = {
    321 	.io = NULL,
    322 	.mem = mem_callback
    323 };
    324 
    325 /*
    326  * 0x1000: MMIO address, unmapped
    327  * 0x2000: Instructions, mapped
    328  * 0x3000: L4
    329  * 0x4000: L3
    330  * 0x5000: L2
    331  * 0x6000: L1
    332  */
    333 int main(int argc, char *argv[])
    334 {
    335 	struct nvmm_machine mach;
    336 	size_t i;
    337 
    338 	if (nvmm_machine_create(&mach) == -1)
    339 		err(errno, "nvmm_machine_create");
    340 	if (nvmm_vcpu_create(&mach, 0) == -1)
    341 		err(errno, "nvmm_vcpu_create");
    342 	nvmm_callbacks_register(&callbacks);
    343 	map_pages(&mach);
    344 
    345 	for (i = 0; tests[i].name != NULL; i++) {
    346 		run_test(&mach, &tests[i]);
    347 	}
    348 
    349 	return 0;
    350 }
    351