Home | History | Annotate | Line # | Download | only in libnvmm
h_mem_assist.c revision 1.14
      1 /*	$NetBSD: h_mem_assist.c,v 1.14 2019/10/14 10:39:24 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_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_EXIT_NONE:
    101 			break;
    102 
    103 		case NVMM_EXIT_MSR:
    104 			/* Stop here. */
    105 			return;
    106 
    107 		case NVMM_EXIT_MEMORY:
    108 			handle_memory(mach, vcpu);
    109 			break;
    110 
    111 		case NVMM_EXIT_SHUTDOWN:
    112 			printf("Shutting down!\n");
    113 			return;
    114 
    115 		default:
    116 			printf("Invalid!\n");
    117 			return;
    118 		}
    119 	}
    120 }
    121 
    122 static struct nvmm_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 	{ "test1 - MOV", &test1_begin, &test1_end, 0x3004, 0 },
    181 	{ "test2 - OR",  &test2_begin, &test2_end, 0x16FF, 0 },
    182 	{ "test3 - AND", &test3_begin, &test3_end, 0x1FC0, 0 },
    183 	{ "test4 - XOR", &test4_begin, &test4_end, 0x10CF, 0 },
    184 	{ "test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00, 0 },
    185 	{ "test6 - DMO", &test6_begin, &test6_end, 0xFFAB, 0 },
    186 	{ "test7 - STOS", &test7_begin, &test7_end, 0x00123456, 0 },
    187 	{ "test8 - LODS", &test8_begin, &test8_end, 0x12345678, 0 },
    188 	{ "test9 - MOVS", &test9_begin, &test9_end, 0x12345678, 0 },
    189 	{ "test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078, 0 },
    190 	{ "test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678, 0 },
    191 	{ "test12 - CMP", &test12_begin, &test12_end, 0x00000001, 0 },
    192 	{ "test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF, 0 },
    193 	{ "test14 - TEST", &test14_begin, &test14_end, 0x00000001, 0 },
    194 	{ "test15 - XCHG", &test_64bit_15_begin, &test_64bit_15_end, 0x123456, 0 },
    195 	{ "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 	memset(state, 0, sizeof(*state));
    222 
    223 	/* Default. */
    224 	state->gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
    225 	init_seg(&state->segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
    226 	init_seg(&state->segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
    227 	init_seg(&state->segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
    228 	init_seg(&state->segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
    229 	init_seg(&state->segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
    230 	init_seg(&state->segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
    231 
    232 	/* Blank. */
    233 	init_seg(&state->segs[NVMM_X64_SEG_GDT], 0, 0);
    234 	init_seg(&state->segs[NVMM_X64_SEG_IDT], 0, 0);
    235 	init_seg(&state->segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
    236 	init_seg(&state->segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
    237 
    238 	/* Protected mode enabled. */
    239 	state->crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
    240 
    241 	/* 64bit mode enabled. */
    242 	state->crs[NVMM_X64_CR_CR4] = CR4_PAE;
    243 	state->msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
    244 
    245 	/* Stolen from x86/pmap.c */
    246 #define	PATENTRY(n, type)	(type << ((n) * 8))
    247 #define	PAT_UC		0x0ULL
    248 #define	PAT_WC		0x1ULL
    249 #define	PAT_WT		0x4ULL
    250 #define	PAT_WP		0x5ULL
    251 #define	PAT_WB		0x6ULL
    252 #define	PAT_UCMINUS	0x7ULL
    253 	state->msrs[NVMM_X64_MSR_PAT] =
    254 	    PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
    255 	    PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
    256 	    PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
    257 	    PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
    258 
    259 	/* Page tables. */
    260 	state->crs[NVMM_X64_CR_CR3] = 0x3000;
    261 
    262 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
    263 
    264 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
    265 		err(errno, "nvmm_vcpu_setstate");
    266 }
    267 
    268 static void
    269 map_pages64(struct nvmm_machine *mach)
    270 {
    271 	pt_entry_t *L4, *L3, *L2, *L1;
    272 	int ret;
    273 
    274 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    275 	    -1, 0);
    276 	if (instbuf == MAP_FAILED)
    277 		err(errno, "mmap");
    278 
    279 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
    280 		err(errno, "nvmm_hva_map");
    281 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
    282 	    PROT_READ|PROT_EXEC);
    283 	if (ret == -1)
    284 		err(errno, "nvmm_gpa_map");
    285 
    286 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    287 	    -1, 0);
    288 	if (L4 == MAP_FAILED)
    289 		err(errno, "mmap");
    290 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    291 	    -1, 0);
    292 	if (L3 == MAP_FAILED)
    293 		err(errno, "mmap");
    294 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    295 	    -1, 0);
    296 	if (L2 == MAP_FAILED)
    297 		err(errno, "mmap");
    298 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    299 	    -1, 0);
    300 	if (L1 == MAP_FAILED)
    301 		err(errno, "mmap");
    302 
    303 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
    304 		err(errno, "nvmm_hva_map");
    305 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
    306 		err(errno, "nvmm_hva_map");
    307 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
    308 		err(errno, "nvmm_hva_map");
    309 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
    310 		err(errno, "nvmm_hva_map");
    311 
    312 	ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE,
    313 	    PROT_READ|PROT_WRITE);
    314 	if (ret == -1)
    315 		err(errno, "nvmm_gpa_map");
    316 	ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE,
    317 	    PROT_READ|PROT_WRITE);
    318 	if (ret == -1)
    319 		err(errno, "nvmm_gpa_map");
    320 	ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE,
    321 	    PROT_READ|PROT_WRITE);
    322 	if (ret == -1)
    323 		err(errno, "nvmm_gpa_map");
    324 	ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE,
    325 	    PROT_READ|PROT_WRITE);
    326 	if (ret == -1)
    327 		err(errno, "nvmm_gpa_map");
    328 
    329 	memset(L4, 0, PAGE_SIZE);
    330 	memset(L3, 0, PAGE_SIZE);
    331 	memset(L2, 0, PAGE_SIZE);
    332 	memset(L1, 0, PAGE_SIZE);
    333 
    334 	L4[0] = PTE_P | PTE_W | 0x4000;
    335 	L3[0] = PTE_P | PTE_W | 0x5000;
    336 	L2[0] = PTE_P | PTE_W | 0x6000;
    337 	L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000;
    338 	L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000;
    339 }
    340 
    341 /*
    342  * 0x1000: MMIO address, unmapped
    343  * 0x2000: Instructions, mapped
    344  * 0x3000: L4
    345  * 0x4000: L3
    346  * 0x5000: L2
    347  * 0x6000: L1
    348  */
    349 static void
    350 test_vm64(void)
    351 {
    352 	struct nvmm_machine mach;
    353 	struct nvmm_vcpu vcpu;
    354 	size_t i;
    355 
    356 	if (nvmm_machine_create(&mach) == -1)
    357 		err(errno, "nvmm_machine_create");
    358 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
    359 		err(errno, "nvmm_vcpu_create");
    360 	nvmm_machine_configure(&mach, NVMM_MACH_CONF_CALLBACKS, &callbacks);
    361 	map_pages64(&mach);
    362 
    363 	for (i = 0; tests64[i].name != NULL; i++) {
    364 		reset_machine64(&mach, &vcpu);
    365 		run_test(&mach, &vcpu, &tests64[i]);
    366 	}
    367 
    368 	if (nvmm_machine_destroy(&mach) == -1)
    369 		err(errno, "nvmm_machine_destroy");
    370 }
    371 
    372 /* -------------------------------------------------------------------------- */
    373 
    374 extern uint8_t test_16bit_1_begin, test_16bit_1_end;
    375 extern uint8_t test_16bit_2_begin, test_16bit_2_end;
    376 extern uint8_t test_16bit_3_begin, test_16bit_3_end;
    377 extern uint8_t test_16bit_4_begin, test_16bit_4_end;
    378 extern uint8_t test_16bit_5_begin, test_16bit_5_end;
    379 extern uint8_t test_16bit_6_begin, test_16bit_6_end;
    380 
    381 static const struct test tests16[] = {
    382 	{ "16bit test1 - MOV single", &test_16bit_1_begin, &test_16bit_1_end,
    383 	  0x023, 0x10f1 - 0x1000 },
    384 	{ "16bit test2 - MOV dual", &test_16bit_2_begin, &test_16bit_2_end,
    385 	  0x123, 0x10f3 - 0x1000 },
    386 	{ "16bit test3 - MOV dual+disp", &test_16bit_3_begin, &test_16bit_3_end,
    387 	  0x678, 0x10f1 - 0x1000 },
    388 	{ "16bit test4 - Mixed", &test_16bit_4_begin, &test_16bit_4_end,
    389 	  0x1011, 0x10f6 - 0x1000 },
    390 	{ "16bit test5 - disp16-only", &test_16bit_5_begin, &test_16bit_5_end,
    391 	  0x12, 0x1234 - 0x1000 },
    392 	{ "16bit test6 - XCHG", &test_16bit_6_begin, &test_16bit_6_end,
    393 	  0x1234, 0x1234 - 0x1000 },
    394 	{ NULL, NULL, NULL, -1, -1 }
    395 };
    396 
    397 static void
    398 reset_machine16(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu)
    399 {
    400 	struct nvmm_x64_state *state = vcpu->state;
    401 
    402 	if (nvmm_vcpu_getstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
    403 		err(errno, "nvmm_vcpu_setstate");
    404 
    405 	state->segs[NVMM_X64_SEG_CS].base = 0;
    406 	state->segs[NVMM_X64_SEG_CS].limit = 0xFFFFFFFF;
    407 	state->gprs[NVMM_X64_GPR_RIP] = 0x2000;
    408 
    409 	if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1)
    410 		err(errno, "nvmm_vcpu_setstate");
    411 }
    412 
    413 static void
    414 map_pages16(struct nvmm_machine *mach)
    415 {
    416 	int ret;
    417 
    418 	instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    419 	    -1, 0);
    420 	if (instbuf == MAP_FAILED)
    421 		err(errno, "mmap");
    422 
    423 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
    424 		err(errno, "nvmm_hva_map");
    425 	ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE,
    426 	    PROT_READ|PROT_EXEC);
    427 	if (ret == -1)
    428 		err(errno, "nvmm_gpa_map");
    429 }
    430 
    431 /*
    432  * 0x1000: MMIO address, unmapped
    433  * 0x2000: Instructions, mapped
    434  */
    435 static void
    436 test_vm16(void)
    437 {
    438 	struct nvmm_machine mach;
    439 	struct nvmm_vcpu vcpu;
    440 	size_t i;
    441 
    442 	if (nvmm_machine_create(&mach) == -1)
    443 		err(errno, "nvmm_machine_create");
    444 	if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1)
    445 		err(errno, "nvmm_vcpu_create");
    446 	nvmm_machine_configure(&mach, NVMM_MACH_CONF_CALLBACKS, &callbacks);
    447 	map_pages16(&mach);
    448 
    449 	for (i = 0; tests16[i].name != NULL; i++) {
    450 		reset_machine16(&mach, &vcpu);
    451 		run_test(&mach, &vcpu, &tests16[i]);
    452 	}
    453 
    454 	if (nvmm_machine_destroy(&mach) == -1)
    455 		err(errno, "nvmm_machine_destroy");
    456 }
    457 
    458 /* -------------------------------------------------------------------------- */
    459 
    460 int main(int argc, char *argv[])
    461 {
    462 	test_vm64();
    463 	test_vm16();
    464 	return 0;
    465 }
    466