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