Home | History | Annotate | Line # | Download | only in libnvmm
h_mem_assist.c revision 1.4
      1 /*
      2  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Maxime Villard.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <stdint.h>
     33 #include <stdbool.h>
     34 #include <unistd.h>
     35 #include <string.h>
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <sys/types.h>
     39 #include <sys/mman.h>
     40 #include <machine/segments.h>
     41 #include <machine/psl.h>
     42 #include <machine/pte.h>
     43 #include <x86/specialreg.h>
     44 
     45 #include <nvmm.h>
     46 
     47 #define PAGE_SIZE 4096
     48 
     49 static uint8_t mmiobuf[PAGE_SIZE];
     50 static uint8_t *instbuf;
     51 
     52 static void
     53 init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
     54 {
     55 	seg->selector = sel;
     56 	seg->attrib.type = type;
     57 	seg->attrib.dpl = 0;
     58 	seg->attrib.p = 1;
     59 	seg->attrib.avl = 1;
     60 	seg->attrib.lng = 1;
     61 	seg->attrib.def32 = 0;
     62 	seg->attrib.gran = 1;
     63 	seg->limit = 0x0000FFFF;
     64 	seg->base = 0x00000000;
     65 }
     66 
     67 static void
     68 reset_machine(struct nvmm_machine *mach)
     69 {
     70 	struct nvmm_x64_state state;
     71 
     72 	memset(&state, 0, sizeof(state));
     73 
     74 	/* Default. */
     75 	state.gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
     76 	init_seg(&state.segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
     77 	init_seg(&state.segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     78 	init_seg(&state.segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     79 	init_seg(&state.segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     80 	init_seg(&state.segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     81 	init_seg(&state.segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
     82 
     83 	/* Blank. */
     84 	init_seg(&state.segs[NVMM_X64_SEG_GDT], 0, 0);
     85 	init_seg(&state.segs[NVMM_X64_SEG_IDT], 0, 0);
     86 	init_seg(&state.segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
     87 	init_seg(&state.segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
     88 
     89 	/* Protected mode enabled. */
     90 	state.crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
     91 
     92 	/* 64bit mode enabled. */
     93 	state.crs[NVMM_X64_CR_CR4] = CR4_PAE;
     94 	state.msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
     95 
     96 	/* Stolen from x86/pmap.c */
     97 #define	PATENTRY(n, type)	(type << ((n) * 8))
     98 #define	PAT_UC		0x0ULL
     99 #define	PAT_WC		0x1ULL
    100 #define	PAT_WT		0x4ULL
    101 #define	PAT_WP		0x5ULL
    102 #define	PAT_WB		0x6ULL
    103 #define	PAT_UCMINUS	0x7ULL
    104 	state.msrs[NVMM_X64_MSR_PAT] =
    105 	    PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
    106 	    PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
    107 	    PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
    108 	    PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
    109 
    110 	/* Page tables. */
    111 	state.crs[NVMM_X64_CR_CR3] = 0x3000;
    112 
    113 	state.gprs[NVMM_X64_GPR_RIP] = 0x2000;
    114 
    115 	if (nvmm_vcpu_setstate(mach, 0, &state, NVMM_X64_STATE_ALL) == -1)
    116 		err(errno, "nvmm_vcpu_setstate");
    117 }
    118 
    119 static void
    120 map_pages(struct nvmm_machine *mach)
    121 {
    122 	pt_entry_t *L4, *L3, *L2, *L1;
    123 
    124 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    125 	    -1, 0);
    126 	if (instbuf == MAP_FAILED)
    127 		err(errno, "mmap");
    128 
    129 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
    130 		err(errno, "nvmm_hva_map");
    131 	if (nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE, 0) == -1)
    132 		err(errno, "nvmm_gpa_map");
    133 
    134 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    135 	    -1, 0);
    136 	if (L4 == MAP_FAILED)
    137 		err(errno, "mmap");
    138 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    139 	    -1, 0);
    140 	if (L3 == MAP_FAILED)
    141 		err(errno, "mmap");
    142 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    143 	    -1, 0);
    144 	if (L2 == MAP_FAILED)
    145 		err(errno, "mmap");
    146 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    147 	    -1, 0);
    148 	if (L1 == MAP_FAILED)
    149 		err(errno, "mmap");
    150 
    151 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
    152 		err(errno, "nvmm_hva_map");
    153 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
    154 		err(errno, "nvmm_hva_map");
    155 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
    156 		err(errno, "nvmm_hva_map");
    157 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
    158 		err(errno, "nvmm_hva_map");
    159 
    160 	if (nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE, 0) == -1)
    161 		err(errno, "nvmm_gpa_map");
    162 	if (nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE, 0) == -1)
    163 		err(errno, "nvmm_gpa_map");
    164 	if (nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE, 0) == -1)
    165 		err(errno, "nvmm_gpa_map");
    166 	if (nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE, 0) == -1)
    167 		err(errno, "nvmm_gpa_map");
    168 
    169 	memset(L4, 0, PAGE_SIZE);
    170 	memset(L3, 0, PAGE_SIZE);
    171 	memset(L2, 0, PAGE_SIZE);
    172 	memset(L1, 0, PAGE_SIZE);
    173 
    174 	L4[0] = PG_V | PG_RW | 0x4000;
    175 	L3[0] = PG_V | PG_RW | 0x5000;
    176 	L2[0] = PG_V | PG_RW | 0x6000;
    177 	L1[0x2000 / PAGE_SIZE] = PG_V | PG_RW | 0x2000;
    178 	L1[0x1000 / PAGE_SIZE] = PG_V | PG_RW | 0x1000;
    179 }
    180 
    181 /* -------------------------------------------------------------------------- */
    182 
    183 static void
    184 mem_callback(struct nvmm_mem *mem)
    185 {
    186 	size_t off;
    187 
    188 	if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
    189 		printf("Out of page\n");
    190 		exit(-1);
    191 	}
    192 
    193 	off = mem->gpa - 0x1000;
    194 
    195 	printf("-> gpa = %p\n", (void *)mem->gpa);
    196 
    197 	if (mem->write) {
    198 		memcpy(mmiobuf + off, mem->data, mem->size);
    199 	} else {
    200 		memcpy(mem->data, mmiobuf + off, mem->size);
    201 	}
    202 }
    203 
    204 static int
    205 handle_memory(struct nvmm_machine *mach, struct nvmm_exit *exit)
    206 {
    207 	int ret;
    208 
    209 	ret = nvmm_assist_mem(mach, 0, exit);
    210 	if (ret == -1) {
    211 		err(errno, "nvmm_assist_mem");
    212 	}
    213 
    214 	return 0;
    215 }
    216 
    217 static void
    218 run_machine(struct nvmm_machine *mach)
    219 {
    220 	struct nvmm_exit exit;
    221 
    222 	while (1) {
    223 		if (nvmm_vcpu_run(mach, 0, &exit) == -1)
    224 			err(errno, "nvmm_vcpu_run");
    225 
    226 		switch (exit.reason) {
    227 		case NVMM_EXIT_NONE:
    228 			break;
    229 
    230 		case NVMM_EXIT_MSR:
    231 			/* Stop here. */
    232 			return;
    233 
    234 		case NVMM_EXIT_MEMORY:
    235 			handle_memory(mach, &exit);
    236 			break;
    237 
    238 		case NVMM_EXIT_SHUTDOWN:
    239 			printf("Shutting down!\n");
    240 			return;
    241 
    242 		default:
    243 			printf("Invalid!\n");
    244 			return;
    245 		}
    246 	}
    247 }
    248 
    249 /* -------------------------------------------------------------------------- */
    250 
    251 struct test {
    252 	const char *name;
    253 	uint8_t *code_begin;
    254 	uint8_t *code_end;
    255 	uint64_t wanted;
    256 };
    257 
    258 static void
    259 run_test(struct nvmm_machine *mach, const struct test *test)
    260 {
    261 	uint64_t *res;
    262 	size_t size;
    263 
    264 	size = (size_t)test->code_end - (size_t)test->code_begin;
    265 
    266 	reset_machine(mach);
    267 
    268 	memset(mmiobuf, 0, PAGE_SIZE);
    269 	memcpy(instbuf, test->code_begin, size);
    270 
    271 	run_machine(mach);
    272 
    273 	res = (uint64_t *)mmiobuf;
    274 	if (*res == test->wanted) {
    275 		printf("Test '%s' passed\n", test->name);
    276 	} else {
    277 		printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
    278 		    test->wanted, *res);
    279 	}
    280 }
    281 
    282 /* -------------------------------------------------------------------------- */
    283 
    284 extern uint8_t test1_begin, test1_end;
    285 extern uint8_t test2_begin, test2_end;
    286 extern uint8_t test3_begin, test3_end;
    287 extern uint8_t test4_begin, test4_end;
    288 extern uint8_t test5_begin, test5_end;
    289 extern uint8_t test6_begin, test6_end;
    290 extern uint8_t test7_begin, test7_end;
    291 extern uint8_t test8_begin, test8_end;
    292 extern uint8_t test9_begin, test9_end;
    293 extern uint8_t test10_begin, test10_end;
    294 extern uint8_t test11_begin, test11_end;
    295 
    296 static const struct test tests[] = {
    297 	{ "test1 - MOV", &test1_begin, &test1_end, 0x3004 },
    298 	{ "test2 - OR",  &test2_begin, &test2_end, 0x14FF },
    299 	{ "test3 - AND", &test3_begin, &test3_end, 0x1FC0 },
    300 	{ "test4 - XOR", &test4_begin, &test4_end, 0x10CF },
    301 	{ "test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00 },
    302 	{ "test6 - DMO", &test6_begin, &test6_end, 0xFFAB },
    303 	{ "test7 - STOS", &test7_begin, &test7_end, 0x00123456 },
    304 	{ "test8 - LODS", &test8_begin, &test8_end, 0x12345678 },
    305 	{ "test9 - MOVS", &test9_begin, &test9_end, 0x12345678 },
    306 	{ "test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078 },
    307 	{ "test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678 },
    308 	{ NULL, NULL, NULL, -1 }
    309 };
    310 
    311 static const struct nvmm_callbacks callbacks = {
    312 	.io = NULL,
    313 	.mem = mem_callback
    314 };
    315 
    316 /*
    317  * 0x1000: MMIO address, unmapped
    318  * 0x2000: Instructions, mapped
    319  * 0x3000: L4
    320  * 0x4000: L3
    321  * 0x5000: L2
    322  * 0x6000: L1
    323  */
    324 int main(int argc, char *argv[])
    325 {
    326 	struct nvmm_machine mach;
    327 	size_t i;
    328 
    329 	if (nvmm_machine_create(&mach) == -1)
    330 		err(errno, "nvmm_machine_create");
    331 	if (nvmm_vcpu_create(&mach, 0) == -1)
    332 		err(errno, "nvmm_vcpu_create");
    333 	nvmm_callbacks_register(&callbacks);
    334 	map_pages(&mach);
    335 
    336 	for (i = 0; tests[i].name != NULL; i++) {
    337 		run_test(&mach, &tests[i]);
    338 	}
    339 
    340 	return 0;
    341 }
    342