Home | History | Annotate | Line # | Download | only in libnvmm
h_io_assist.c revision 1.2
      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 #define IO_SIZE	128
     49 
     50 static char iobuf[IO_SIZE];
     51 
     52 static char *databuf;
     53 static uint8_t *instbuf;
     54 
     55 static void
     56 init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
     57 {
     58 	seg->selector = sel;
     59 	seg->attrib.type = type;
     60 	seg->attrib.dpl = 0;
     61 	seg->attrib.p = 1;
     62 	seg->attrib.avl = 1;
     63 	seg->attrib.lng = 1;
     64 	seg->attrib.def32 = 0;
     65 	seg->attrib.gran = 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 	databuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    132 	    -1, 0);
    133 	if (databuf == MAP_FAILED)
    134 		err(errno, "mmap");
    135 
    136 	if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
    137 		err(errno, "nvmm_hva_map");
    138 	if (nvmm_hva_map(mach, (uintptr_t)databuf, PAGE_SIZE) == -1)
    139 		err(errno, "nvmm_hva_map");
    140 	if (nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE, 0) == -1)
    141 		err(errno, "nvmm_gpa_map");
    142 	if (nvmm_gpa_map(mach, (uintptr_t)databuf, 0x1000, PAGE_SIZE, 0) == -1)
    143 		err(errno, "nvmm_gpa_map");
    144 
    145 	L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    146 	    -1, 0);
    147 	if (L4 == MAP_FAILED)
    148 		err(errno, "mmap");
    149 	L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    150 	    -1, 0);
    151 	if (L3 == MAP_FAILED)
    152 		err(errno, "mmap");
    153 	L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    154 	    -1, 0);
    155 	if (L2 == MAP_FAILED)
    156 		err(errno, "mmap");
    157 	L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
    158 	    -1, 0);
    159 	if (L1 == MAP_FAILED)
    160 		err(errno, "mmap");
    161 
    162 	if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
    163 		err(errno, "nvmm_hva_map");
    164 	if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
    165 		err(errno, "nvmm_hva_map");
    166 	if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
    167 		err(errno, "nvmm_hva_map");
    168 	if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
    169 		err(errno, "nvmm_hva_map");
    170 
    171 	if (nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE, 0) == -1)
    172 		err(errno, "nvmm_gpa_map");
    173 	if (nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE, 0) == -1)
    174 		err(errno, "nvmm_gpa_map");
    175 	if (nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE, 0) == -1)
    176 		err(errno, "nvmm_gpa_map");
    177 	if (nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE, 0) == -1)
    178 		err(errno, "nvmm_gpa_map");
    179 
    180 	memset(L4, 0, PAGE_SIZE);
    181 	memset(L3, 0, PAGE_SIZE);
    182 	memset(L2, 0, PAGE_SIZE);
    183 	memset(L1, 0, PAGE_SIZE);
    184 
    185 	L4[0] = PG_V | PG_RW | 0x4000;
    186 	L3[0] = PG_V | PG_RW | 0x5000;
    187 	L2[0] = PG_V | PG_RW | 0x6000;
    188 	L1[0x2000 / PAGE_SIZE] = PG_V | PG_RW | 0x2000;
    189 	L1[0x1000 / PAGE_SIZE] = PG_V | PG_RW | 0x1000;
    190 }
    191 
    192 /* -------------------------------------------------------------------------- */
    193 
    194 static size_t iobuf_off = 0;
    195 
    196 static void
    197 io_callback(struct nvmm_io *io)
    198 {
    199 	if (io->port != 123) {
    200 		printf("Wrong port\n");
    201 		exit(-1);
    202 	}
    203 
    204 	if (io->in) {
    205 		memcpy(io->data, iobuf + iobuf_off, io->size);
    206 	} else {
    207 		memcpy(iobuf + iobuf_off, io->data, io->size);
    208 	}
    209 	iobuf_off += io->size;
    210 
    211 }
    212 
    213 static int
    214 handle_io(struct nvmm_machine *mach, struct nvmm_exit *exit)
    215 {
    216 	int ret;
    217 
    218 	ret = nvmm_assist_io(mach, 0, exit);
    219 	if (ret == -1) {
    220 		err(errno, "nvmm_assist_io");
    221 	}
    222 
    223 	return 0;
    224 }
    225 
    226 static void
    227 run_machine(struct nvmm_machine *mach)
    228 {
    229 	struct nvmm_exit exit;
    230 
    231 	while (1) {
    232 		if (nvmm_vcpu_run(mach, 0, &exit) == -1)
    233 			err(errno, "nvmm_vcpu_run");
    234 
    235 		switch (exit.reason) {
    236 		case NVMM_EXIT_NONE:
    237 			break;
    238 
    239 		case NVMM_EXIT_MSR:
    240 			/* Stop here. */
    241 			return;
    242 
    243 		case NVMM_EXIT_IO:
    244 			handle_io(mach, &exit);
    245 			break;
    246 
    247 		case NVMM_EXIT_SHUTDOWN:
    248 			printf("Shutting down!\n");
    249 			return;
    250 
    251 		default:
    252 			printf("Invalid!\n");
    253 			return;
    254 		}
    255 	}
    256 }
    257 
    258 /* -------------------------------------------------------------------------- */
    259 
    260 struct test {
    261 	const char *name;
    262 	uint8_t *code_begin;
    263 	uint8_t *code_end;
    264 	const char *wanted;
    265 	bool in;
    266 };
    267 
    268 static void
    269 run_test(struct nvmm_machine *mach, const struct test *test)
    270 {
    271 	size_t size;
    272 	char *res;
    273 
    274 	size = (size_t)test->code_end - (size_t)test->code_begin;
    275 
    276 	reset_machine(mach);
    277 
    278 	iobuf_off = 0;
    279 	memset(iobuf, 0, IO_SIZE);
    280 	memset(databuf, 0, PAGE_SIZE);
    281 	memcpy(instbuf, test->code_begin, size);
    282 
    283 	if (test->in) {
    284 		strcpy(iobuf, test->wanted);
    285 	} else {
    286 		strcpy(databuf, test->wanted);
    287 	}
    288 
    289 	run_machine(mach);
    290 
    291 	if (test->in) {
    292 		res = databuf;
    293 	} else {
    294 		res = iobuf;
    295 	}
    296 
    297 	if (!strcmp(res, test->wanted)) {
    298 		printf("Test '%s' passed\n", test->name);
    299 	} else {
    300 		printf("Test '%s' failed, wanted '%s', got '%s'\n", test->name,
    301 		    test->wanted, res);
    302 	}
    303 }
    304 
    305 /* -------------------------------------------------------------------------- */
    306 
    307 extern uint8_t test1_begin, test1_end;
    308 extern uint8_t test2_begin, test2_end;
    309 extern uint8_t test3_begin, test3_end;
    310 extern uint8_t test4_begin, test4_end;
    311 extern uint8_t test5_begin, test5_end;
    312 extern uint8_t test6_begin, test6_end;
    313 extern uint8_t test7_begin, test7_end;
    314 extern uint8_t test8_begin, test8_end;
    315 extern uint8_t test9_begin, test9_end;
    316 extern uint8_t test10_begin, test10_end;
    317 extern uint8_t test11_begin, test11_end;
    318 extern uint8_t test12_begin, test12_end;
    319 
    320 static const struct test tests[] = {
    321 	{ "test1 - INB", &test1_begin, &test1_end, "12", true },
    322 	{ "test2 - INW", &test2_begin, &test2_end, "1234", true },
    323 	{ "test3 - INL", &test3_begin, &test3_end, "12345678", true },
    324 	{ "test4 - INSB+REP", &test4_begin, &test4_end, "12345", true },
    325 	{ "test5 - INSW+REP", &test5_begin, &test5_end,
    326 	  "Comment est votre blanquette", true },
    327 	{ "test6 - INSL+REP", &test6_begin, &test6_end,
    328 	  "123456789abcdefghijklmnopqrs", true },
    329 	{ "test7 - OUTB", &test7_begin, &test7_end, "12", false },
    330 	{ "test8 - OUTW", &test8_begin, &test8_end, "1234", false },
    331 	{ "test9 - OUTL", &test9_begin, &test9_end, "12345678", false },
    332 	{ "test10 - OUTSB+REP", &test10_begin, &test10_end, "12345", false },
    333 	{ "test11 - OUTSW+REP", &test11_begin, &test11_end,
    334 	  "Ah, Herr Bramard", false },
    335 	{ "test12 - OUTSL+REP", &test12_begin, &test12_end,
    336 	  "123456789abcdefghijklmnopqrs", false },
    337 	{ NULL, NULL, NULL, NULL, false }
    338 };
    339 
    340 static const struct nvmm_callbacks callbacks = {
    341 	.io = io_callback,
    342 	.mem = NULL
    343 };
    344 
    345 /*
    346  * 0x1000: Data, mapped
    347  * 0x2000: Instructions, mapped
    348  * 0x3000: L4
    349  * 0x4000: L3
    350  * 0x5000: L2
    351  * 0x6000: L1
    352  */
    353 int main(int argc, char *argv[])
    354 {
    355 	struct nvmm_machine mach;
    356 	size_t i;
    357 
    358 	if (nvmm_machine_create(&mach) == -1)
    359 		err(errno, "nvmm_machine_create");
    360 	if (nvmm_vcpu_create(&mach, 0) == -1)
    361 		err(errno, "nvmm_vcpu_create");
    362 	nvmm_callbacks_register(&callbacks);
    363 	map_pages(&mach);
    364 
    365 	for (i = 0; tests[i].name != NULL; i++) {
    366 		run_test(&mach, &tests[i]);
    367 	}
    368 
    369 	return 0;
    370 }
    371