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