1 1.12 maxv /* $NetBSD: h_io_assist.c,v 1.12 2020/09/05 07:22:26 maxv Exp $ */ 2 1.4 maxv 3 1.1 maxv /* 4 1.12 maxv * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net 5 1.1 maxv * All rights reserved. 6 1.1 maxv * 7 1.12 maxv * This code is part of the NVMM hypervisor. 8 1.1 maxv * 9 1.1 maxv * Redistribution and use in source and binary forms, with or without 10 1.1 maxv * modification, are permitted provided that the following conditions 11 1.1 maxv * are met: 12 1.1 maxv * 1. Redistributions of source code must retain the above copyright 13 1.1 maxv * notice, this list of conditions and the following disclaimer. 14 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 maxv * notice, this list of conditions and the following disclaimer in the 16 1.1 maxv * documentation and/or other materials provided with the distribution. 17 1.1 maxv * 18 1.12 maxv * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 1.12 maxv * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 1.12 maxv * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 1.12 maxv * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 1.12 maxv * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 1.12 maxv * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 1.12 maxv * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 1.12 maxv * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 1.12 maxv * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 1.12 maxv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 1.12 maxv * SUCH DAMAGE. 29 1.1 maxv */ 30 1.1 maxv 31 1.1 maxv #include <stdio.h> 32 1.1 maxv #include <stdlib.h> 33 1.1 maxv #include <stdint.h> 34 1.1 maxv #include <stdbool.h> 35 1.1 maxv #include <unistd.h> 36 1.1 maxv #include <string.h> 37 1.1 maxv #include <err.h> 38 1.1 maxv #include <errno.h> 39 1.1 maxv #include <sys/types.h> 40 1.1 maxv #include <sys/mman.h> 41 1.1 maxv #include <machine/segments.h> 42 1.1 maxv #include <machine/psl.h> 43 1.1 maxv #include <machine/pte.h> 44 1.1 maxv #include <x86/specialreg.h> 45 1.1 maxv 46 1.1 maxv #include <nvmm.h> 47 1.1 maxv 48 1.1 maxv #define PAGE_SIZE 4096 49 1.1 maxv #define IO_SIZE 128 50 1.1 maxv 51 1.1 maxv static char iobuf[IO_SIZE]; 52 1.1 maxv 53 1.2 maxv static char *databuf; 54 1.1 maxv static uint8_t *instbuf; 55 1.1 maxv 56 1.1 maxv static void 57 1.1 maxv init_seg(struct nvmm_x64_state_seg *seg, int type, int sel) 58 1.1 maxv { 59 1.1 maxv seg->selector = sel; 60 1.1 maxv seg->attrib.type = type; 61 1.3 maxv seg->attrib.s = (type & 0b10000) != 0; 62 1.1 maxv seg->attrib.dpl = 0; 63 1.1 maxv seg->attrib.p = 1; 64 1.1 maxv seg->attrib.avl = 1; 65 1.3 maxv seg->attrib.l = 1; 66 1.3 maxv seg->attrib.def = 0; 67 1.3 maxv seg->attrib.g = 1; 68 1.1 maxv seg->limit = 0x0000FFFF; 69 1.1 maxv seg->base = 0x00000000; 70 1.1 maxv } 71 1.1 maxv 72 1.1 maxv static void 73 1.8 maxv reset_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu) 74 1.1 maxv { 75 1.8 maxv struct nvmm_x64_state *state = vcpu->state; 76 1.1 maxv 77 1.8 maxv memset(state, 0, sizeof(*state)); 78 1.1 maxv 79 1.1 maxv /* Default. */ 80 1.8 maxv state->gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO; 81 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL)); 82 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL)); 83 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL)); 84 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL)); 85 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL)); 86 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL)); 87 1.1 maxv 88 1.1 maxv /* Blank. */ 89 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_GDT], 0, 0); 90 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_IDT], 0, 0); 91 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0); 92 1.8 maxv init_seg(&state->segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0); 93 1.1 maxv 94 1.1 maxv /* Protected mode enabled. */ 95 1.8 maxv state->crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM; 96 1.1 maxv 97 1.1 maxv /* 64bit mode enabled. */ 98 1.8 maxv state->crs[NVMM_X64_CR_CR4] = CR4_PAE; 99 1.8 maxv state->msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA; 100 1.1 maxv 101 1.1 maxv /* Stolen from x86/pmap.c */ 102 1.1 maxv #define PATENTRY(n, type) (type << ((n) * 8)) 103 1.1 maxv #define PAT_UC 0x0ULL 104 1.1 maxv #define PAT_WC 0x1ULL 105 1.1 maxv #define PAT_WT 0x4ULL 106 1.1 maxv #define PAT_WP 0x5ULL 107 1.1 maxv #define PAT_WB 0x6ULL 108 1.1 maxv #define PAT_UCMINUS 0x7ULL 109 1.8 maxv state->msrs[NVMM_X64_MSR_PAT] = 110 1.1 maxv PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) | 111 1.1 maxv PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) | 112 1.1 maxv PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) | 113 1.1 maxv PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC); 114 1.1 maxv 115 1.1 maxv /* Page tables. */ 116 1.8 maxv state->crs[NVMM_X64_CR_CR3] = 0x3000; 117 1.1 maxv 118 1.8 maxv state->gprs[NVMM_X64_GPR_RIP] = 0x2000; 119 1.1 maxv 120 1.8 maxv if (nvmm_vcpu_setstate(mach, vcpu, NVMM_X64_STATE_ALL) == -1) 121 1.1 maxv err(errno, "nvmm_vcpu_setstate"); 122 1.1 maxv } 123 1.1 maxv 124 1.1 maxv static void 125 1.1 maxv map_pages(struct nvmm_machine *mach) 126 1.1 maxv { 127 1.1 maxv pt_entry_t *L4, *L3, *L2, *L1; 128 1.5 maxv int ret; 129 1.1 maxv 130 1.1 maxv instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 131 1.1 maxv -1, 0); 132 1.1 maxv if (instbuf == MAP_FAILED) 133 1.1 maxv err(errno, "mmap"); 134 1.1 maxv databuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 135 1.1 maxv -1, 0); 136 1.1 maxv if (databuf == MAP_FAILED) 137 1.1 maxv err(errno, "mmap"); 138 1.1 maxv 139 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1) 140 1.1 maxv err(errno, "nvmm_hva_map"); 141 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)databuf, PAGE_SIZE) == -1) 142 1.1 maxv err(errno, "nvmm_hva_map"); 143 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE, 144 1.5 maxv PROT_READ|PROT_EXEC); 145 1.5 maxv if (ret == -1) 146 1.1 maxv err(errno, "nvmm_gpa_map"); 147 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)databuf, 0x1000, PAGE_SIZE, 148 1.5 maxv PROT_READ|PROT_WRITE); 149 1.5 maxv if (ret == -1) 150 1.1 maxv err(errno, "nvmm_gpa_map"); 151 1.1 maxv 152 1.1 maxv L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 153 1.1 maxv -1, 0); 154 1.1 maxv if (L4 == MAP_FAILED) 155 1.1 maxv err(errno, "mmap"); 156 1.1 maxv L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 157 1.1 maxv -1, 0); 158 1.1 maxv if (L3 == MAP_FAILED) 159 1.1 maxv err(errno, "mmap"); 160 1.1 maxv L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 161 1.1 maxv -1, 0); 162 1.1 maxv if (L2 == MAP_FAILED) 163 1.1 maxv err(errno, "mmap"); 164 1.1 maxv L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 165 1.1 maxv -1, 0); 166 1.1 maxv if (L1 == MAP_FAILED) 167 1.1 maxv err(errno, "mmap"); 168 1.1 maxv 169 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1) 170 1.1 maxv err(errno, "nvmm_hva_map"); 171 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1) 172 1.1 maxv err(errno, "nvmm_hva_map"); 173 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1) 174 1.1 maxv err(errno, "nvmm_hva_map"); 175 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1) 176 1.1 maxv err(errno, "nvmm_hva_map"); 177 1.1 maxv 178 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE, 179 1.5 maxv PROT_READ|PROT_WRITE); 180 1.5 maxv if (ret == -1) 181 1.1 maxv err(errno, "nvmm_gpa_map"); 182 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE, 183 1.5 maxv PROT_READ|PROT_WRITE); 184 1.5 maxv if (ret == -1) 185 1.1 maxv err(errno, "nvmm_gpa_map"); 186 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE, 187 1.5 maxv PROT_READ|PROT_WRITE); 188 1.5 maxv if (ret == -1) 189 1.1 maxv err(errno, "nvmm_gpa_map"); 190 1.5 maxv ret = nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE, 191 1.6 htodd PROT_READ|PROT_WRITE); 192 1.5 maxv if (ret == -1) 193 1.1 maxv err(errno, "nvmm_gpa_map"); 194 1.1 maxv 195 1.1 maxv memset(L4, 0, PAGE_SIZE); 196 1.1 maxv memset(L3, 0, PAGE_SIZE); 197 1.1 maxv memset(L2, 0, PAGE_SIZE); 198 1.1 maxv memset(L1, 0, PAGE_SIZE); 199 1.1 maxv 200 1.4 maxv L4[0] = PTE_P | PTE_W | 0x4000; 201 1.4 maxv L3[0] = PTE_P | PTE_W | 0x5000; 202 1.4 maxv L2[0] = PTE_P | PTE_W | 0x6000; 203 1.4 maxv L1[0x2000 / PAGE_SIZE] = PTE_P | PTE_W | 0x2000; 204 1.4 maxv L1[0x1000 / PAGE_SIZE] = PTE_P | PTE_W | 0x1000; 205 1.1 maxv } 206 1.1 maxv 207 1.1 maxv /* -------------------------------------------------------------------------- */ 208 1.1 maxv 209 1.1 maxv static size_t iobuf_off = 0; 210 1.1 maxv 211 1.1 maxv static void 212 1.1 maxv io_callback(struct nvmm_io *io) 213 1.1 maxv { 214 1.1 maxv if (io->port != 123) { 215 1.1 maxv printf("Wrong port\n"); 216 1.1 maxv exit(-1); 217 1.1 maxv } 218 1.1 maxv 219 1.1 maxv if (io->in) { 220 1.1 maxv memcpy(io->data, iobuf + iobuf_off, io->size); 221 1.1 maxv } else { 222 1.1 maxv memcpy(iobuf + iobuf_off, io->data, io->size); 223 1.1 maxv } 224 1.1 maxv iobuf_off += io->size; 225 1.1 maxv 226 1.1 maxv } 227 1.1 maxv 228 1.1 maxv static int 229 1.8 maxv handle_io(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu) 230 1.1 maxv { 231 1.1 maxv int ret; 232 1.1 maxv 233 1.8 maxv ret = nvmm_assist_io(mach, vcpu); 234 1.1 maxv if (ret == -1) { 235 1.1 maxv err(errno, "nvmm_assist_io"); 236 1.1 maxv } 237 1.1 maxv 238 1.1 maxv return 0; 239 1.1 maxv } 240 1.1 maxv 241 1.1 maxv static void 242 1.8 maxv run_machine(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu) 243 1.1 maxv { 244 1.9 maxv struct nvmm_vcpu_exit *exit = vcpu->exit; 245 1.1 maxv 246 1.1 maxv while (1) { 247 1.8 maxv if (nvmm_vcpu_run(mach, vcpu) == -1) 248 1.1 maxv err(errno, "nvmm_vcpu_run"); 249 1.1 maxv 250 1.8 maxv switch (exit->reason) { 251 1.9 maxv case NVMM_VCPU_EXIT_NONE: 252 1.1 maxv break; 253 1.1 maxv 254 1.9 maxv case NVMM_VCPU_EXIT_RDMSR: 255 1.1 maxv /* Stop here. */ 256 1.1 maxv return; 257 1.1 maxv 258 1.9 maxv case NVMM_VCPU_EXIT_IO: 259 1.8 maxv handle_io(mach, vcpu); 260 1.1 maxv break; 261 1.1 maxv 262 1.9 maxv case NVMM_VCPU_EXIT_SHUTDOWN: 263 1.1 maxv printf("Shutting down!\n"); 264 1.1 maxv return; 265 1.1 maxv 266 1.1 maxv default: 267 1.1 maxv printf("Invalid!\n"); 268 1.1 maxv return; 269 1.1 maxv } 270 1.1 maxv } 271 1.1 maxv } 272 1.1 maxv 273 1.1 maxv /* -------------------------------------------------------------------------- */ 274 1.1 maxv 275 1.1 maxv struct test { 276 1.1 maxv const char *name; 277 1.1 maxv uint8_t *code_begin; 278 1.1 maxv uint8_t *code_end; 279 1.1 maxv const char *wanted; 280 1.1 maxv bool in; 281 1.1 maxv }; 282 1.1 maxv 283 1.1 maxv static void 284 1.8 maxv run_test(struct nvmm_machine *mach, struct nvmm_vcpu *vcpu, 285 1.8 maxv const struct test *test) 286 1.1 maxv { 287 1.1 maxv size_t size; 288 1.1 maxv char *res; 289 1.1 maxv 290 1.1 maxv size = (size_t)test->code_end - (size_t)test->code_begin; 291 1.1 maxv 292 1.8 maxv reset_machine(mach, vcpu); 293 1.1 maxv 294 1.1 maxv iobuf_off = 0; 295 1.1 maxv memset(iobuf, 0, IO_SIZE); 296 1.1 maxv memset(databuf, 0, PAGE_SIZE); 297 1.1 maxv memcpy(instbuf, test->code_begin, size); 298 1.1 maxv 299 1.1 maxv if (test->in) { 300 1.1 maxv strcpy(iobuf, test->wanted); 301 1.1 maxv } else { 302 1.1 maxv strcpy(databuf, test->wanted); 303 1.1 maxv } 304 1.1 maxv 305 1.8 maxv run_machine(mach, vcpu); 306 1.1 maxv 307 1.1 maxv if (test->in) { 308 1.1 maxv res = databuf; 309 1.1 maxv } else { 310 1.1 maxv res = iobuf; 311 1.1 maxv } 312 1.1 maxv 313 1.1 maxv if (!strcmp(res, test->wanted)) { 314 1.1 maxv printf("Test '%s' passed\n", test->name); 315 1.1 maxv } else { 316 1.1 maxv printf("Test '%s' failed, wanted '%s', got '%s'\n", test->name, 317 1.1 maxv test->wanted, res); 318 1.1 maxv } 319 1.1 maxv } 320 1.1 maxv 321 1.1 maxv /* -------------------------------------------------------------------------- */ 322 1.1 maxv 323 1.1 maxv extern uint8_t test1_begin, test1_end; 324 1.1 maxv extern uint8_t test2_begin, test2_end; 325 1.1 maxv extern uint8_t test3_begin, test3_end; 326 1.1 maxv extern uint8_t test4_begin, test4_end; 327 1.1 maxv extern uint8_t test5_begin, test5_end; 328 1.1 maxv extern uint8_t test6_begin, test6_end; 329 1.1 maxv extern uint8_t test7_begin, test7_end; 330 1.1 maxv extern uint8_t test8_begin, test8_end; 331 1.1 maxv extern uint8_t test9_begin, test9_end; 332 1.1 maxv extern uint8_t test10_begin, test10_end; 333 1.1 maxv extern uint8_t test11_begin, test11_end; 334 1.1 maxv extern uint8_t test12_begin, test12_end; 335 1.1 maxv 336 1.1 maxv static const struct test tests[] = { 337 1.1 maxv { "test1 - INB", &test1_begin, &test1_end, "12", true }, 338 1.1 maxv { "test2 - INW", &test2_begin, &test2_end, "1234", true }, 339 1.1 maxv { "test3 - INL", &test3_begin, &test3_end, "12345678", true }, 340 1.1 maxv { "test4 - INSB+REP", &test4_begin, &test4_end, "12345", true }, 341 1.1 maxv { "test5 - INSW+REP", &test5_begin, &test5_end, 342 1.1 maxv "Comment est votre blanquette", true }, 343 1.1 maxv { "test6 - INSL+REP", &test6_begin, &test6_end, 344 1.1 maxv "123456789abcdefghijklmnopqrs", true }, 345 1.1 maxv { "test7 - OUTB", &test7_begin, &test7_end, "12", false }, 346 1.1 maxv { "test8 - OUTW", &test8_begin, &test8_end, "1234", false }, 347 1.1 maxv { "test9 - OUTL", &test9_begin, &test9_end, "12345678", false }, 348 1.1 maxv { "test10 - OUTSB+REP", &test10_begin, &test10_end, "12345", false }, 349 1.1 maxv { "test11 - OUTSW+REP", &test11_begin, &test11_end, 350 1.1 maxv "Ah, Herr Bramard", false }, 351 1.1 maxv { "test12 - OUTSL+REP", &test12_begin, &test12_end, 352 1.1 maxv "123456789abcdefghijklmnopqrs", false }, 353 1.1 maxv { NULL, NULL, NULL, NULL, false } 354 1.1 maxv }; 355 1.1 maxv 356 1.10 maxv static struct nvmm_assist_callbacks callbacks = { 357 1.1 maxv .io = io_callback, 358 1.1 maxv .mem = NULL 359 1.1 maxv }; 360 1.1 maxv 361 1.1 maxv /* 362 1.1 maxv * 0x1000: Data, mapped 363 1.1 maxv * 0x2000: Instructions, mapped 364 1.1 maxv * 0x3000: L4 365 1.1 maxv * 0x4000: L3 366 1.1 maxv * 0x5000: L2 367 1.1 maxv * 0x6000: L1 368 1.1 maxv */ 369 1.1 maxv int main(int argc, char *argv[]) 370 1.1 maxv { 371 1.1 maxv struct nvmm_machine mach; 372 1.8 maxv struct nvmm_vcpu vcpu; 373 1.1 maxv size_t i; 374 1.1 maxv 375 1.11 maxv if (nvmm_init() == -1) 376 1.11 maxv err(errno, "nvmm_init"); 377 1.1 maxv if (nvmm_machine_create(&mach) == -1) 378 1.1 maxv err(errno, "nvmm_machine_create"); 379 1.8 maxv if (nvmm_vcpu_create(&mach, 0, &vcpu) == -1) 380 1.1 maxv err(errno, "nvmm_vcpu_create"); 381 1.10 maxv nvmm_vcpu_configure(&mach, &vcpu, NVMM_VCPU_CONF_CALLBACKS, &callbacks); 382 1.1 maxv map_pages(&mach); 383 1.1 maxv 384 1.1 maxv for (i = 0; tests[i].name != NULL; i++) { 385 1.8 maxv run_test(&mach, &vcpu, &tests[i]); 386 1.1 maxv } 387 1.1 maxv 388 1.1 maxv return 0; 389 1.1 maxv } 390