h_mem_assist.c revision 1.5 1 1.1 maxv /*
2 1.1 maxv * Copyright (c) 2018 The NetBSD Foundation, Inc.
3 1.1 maxv * All rights reserved.
4 1.1 maxv *
5 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
6 1.1 maxv * by Maxime Villard.
7 1.1 maxv *
8 1.1 maxv * Redistribution and use in source and binary forms, with or without
9 1.1 maxv * modification, are permitted provided that the following conditions
10 1.1 maxv * are met:
11 1.1 maxv * 1. Redistributions of source code must retain the above copyright
12 1.1 maxv * notice, this list of conditions and the following disclaimer.
13 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer in the
15 1.1 maxv * documentation and/or other materials provided with the distribution.
16 1.1 maxv *
17 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
28 1.1 maxv */
29 1.1 maxv
30 1.1 maxv #include <stdio.h>
31 1.1 maxv #include <stdlib.h>
32 1.1 maxv #include <stdint.h>
33 1.1 maxv #include <stdbool.h>
34 1.1 maxv #include <unistd.h>
35 1.1 maxv #include <string.h>
36 1.1 maxv #include <err.h>
37 1.1 maxv #include <errno.h>
38 1.1 maxv #include <sys/types.h>
39 1.1 maxv #include <sys/mman.h>
40 1.1 maxv #include <machine/segments.h>
41 1.1 maxv #include <machine/psl.h>
42 1.1 maxv #include <machine/pte.h>
43 1.1 maxv #include <x86/specialreg.h>
44 1.1 maxv
45 1.1 maxv #include <nvmm.h>
46 1.1 maxv
47 1.1 maxv #define PAGE_SIZE 4096
48 1.1 maxv
49 1.1 maxv static uint8_t mmiobuf[PAGE_SIZE];
50 1.1 maxv static uint8_t *instbuf;
51 1.1 maxv
52 1.1 maxv static void
53 1.1 maxv init_seg(struct nvmm_x64_state_seg *seg, int type, int sel)
54 1.1 maxv {
55 1.1 maxv seg->selector = sel;
56 1.1 maxv seg->attrib.type = type;
57 1.1 maxv seg->attrib.dpl = 0;
58 1.1 maxv seg->attrib.p = 1;
59 1.1 maxv seg->attrib.avl = 1;
60 1.1 maxv seg->attrib.lng = 1;
61 1.1 maxv seg->attrib.def32 = 0;
62 1.1 maxv seg->attrib.gran = 1;
63 1.3 maxv seg->limit = 0x0000FFFF;
64 1.1 maxv seg->base = 0x00000000;
65 1.1 maxv }
66 1.1 maxv
67 1.1 maxv static void
68 1.1 maxv reset_machine(struct nvmm_machine *mach)
69 1.1 maxv {
70 1.1 maxv struct nvmm_x64_state state;
71 1.1 maxv
72 1.1 maxv memset(&state, 0, sizeof(state));
73 1.1 maxv
74 1.1 maxv /* Default. */
75 1.1 maxv state.gprs[NVMM_X64_GPR_RFLAGS] = PSL_MBO;
76 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_CS], SDT_MEMERA, GSEL(GCODE_SEL, SEL_KPL));
77 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_SS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
78 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_DS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
79 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_ES], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
80 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_FS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
81 1.1 maxv init_seg(&state.segs[NVMM_X64_SEG_GS], SDT_MEMRWA, GSEL(GDATA_SEL, SEL_KPL));
82 1.1 maxv
83 1.1 maxv /* Blank. */
84 1.3 maxv init_seg(&state.segs[NVMM_X64_SEG_GDT], 0, 0);
85 1.3 maxv init_seg(&state.segs[NVMM_X64_SEG_IDT], 0, 0);
86 1.3 maxv init_seg(&state.segs[NVMM_X64_SEG_LDT], SDT_SYSLDT, 0);
87 1.3 maxv init_seg(&state.segs[NVMM_X64_SEG_TR], SDT_SYS386BSY, 0);
88 1.1 maxv
89 1.1 maxv /* Protected mode enabled. */
90 1.1 maxv state.crs[NVMM_X64_CR_CR0] = CR0_PG|CR0_PE|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM;
91 1.1 maxv
92 1.1 maxv /* 64bit mode enabled. */
93 1.1 maxv state.crs[NVMM_X64_CR_CR4] = CR4_PAE;
94 1.1 maxv state.msrs[NVMM_X64_MSR_EFER] = EFER_LME | EFER_SCE | EFER_LMA;
95 1.1 maxv
96 1.1 maxv /* Stolen from x86/pmap.c */
97 1.1 maxv #define PATENTRY(n, type) (type << ((n) * 8))
98 1.1 maxv #define PAT_UC 0x0ULL
99 1.1 maxv #define PAT_WC 0x1ULL
100 1.1 maxv #define PAT_WT 0x4ULL
101 1.1 maxv #define PAT_WP 0x5ULL
102 1.1 maxv #define PAT_WB 0x6ULL
103 1.1 maxv #define PAT_UCMINUS 0x7ULL
104 1.1 maxv state.msrs[NVMM_X64_MSR_PAT] =
105 1.1 maxv PATENTRY(0, PAT_WB) | PATENTRY(1, PAT_WT) |
106 1.1 maxv PATENTRY(2, PAT_UCMINUS) | PATENTRY(3, PAT_UC) |
107 1.1 maxv PATENTRY(4, PAT_WB) | PATENTRY(5, PAT_WT) |
108 1.1 maxv PATENTRY(6, PAT_UCMINUS) | PATENTRY(7, PAT_UC);
109 1.1 maxv
110 1.1 maxv /* Page tables. */
111 1.1 maxv state.crs[NVMM_X64_CR_CR3] = 0x3000;
112 1.1 maxv
113 1.1 maxv state.gprs[NVMM_X64_GPR_RIP] = 0x2000;
114 1.1 maxv
115 1.1 maxv if (nvmm_vcpu_setstate(mach, 0, &state, NVMM_X64_STATE_ALL) == -1)
116 1.1 maxv err(errno, "nvmm_vcpu_setstate");
117 1.1 maxv }
118 1.1 maxv
119 1.1 maxv static void
120 1.1 maxv map_pages(struct nvmm_machine *mach)
121 1.1 maxv {
122 1.1 maxv pt_entry_t *L4, *L3, *L2, *L1;
123 1.1 maxv
124 1.1 maxv instbuf = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
125 1.1 maxv -1, 0);
126 1.1 maxv if (instbuf == MAP_FAILED)
127 1.1 maxv err(errno, "mmap");
128 1.1 maxv
129 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)instbuf, PAGE_SIZE) == -1)
130 1.1 maxv err(errno, "nvmm_hva_map");
131 1.1 maxv if (nvmm_gpa_map(mach, (uintptr_t)instbuf, 0x2000, PAGE_SIZE, 0) == -1)
132 1.1 maxv err(errno, "nvmm_gpa_map");
133 1.1 maxv
134 1.1 maxv L4 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
135 1.1 maxv -1, 0);
136 1.1 maxv if (L4 == MAP_FAILED)
137 1.1 maxv err(errno, "mmap");
138 1.1 maxv L3 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
139 1.1 maxv -1, 0);
140 1.1 maxv if (L3 == MAP_FAILED)
141 1.1 maxv err(errno, "mmap");
142 1.1 maxv L2 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
143 1.1 maxv -1, 0);
144 1.1 maxv if (L2 == MAP_FAILED)
145 1.1 maxv err(errno, "mmap");
146 1.1 maxv L1 = mmap(NULL, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
147 1.1 maxv -1, 0);
148 1.1 maxv if (L1 == MAP_FAILED)
149 1.1 maxv err(errno, "mmap");
150 1.1 maxv
151 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L4, PAGE_SIZE) == -1)
152 1.1 maxv err(errno, "nvmm_hva_map");
153 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L3, PAGE_SIZE) == -1)
154 1.1 maxv err(errno, "nvmm_hva_map");
155 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L2, PAGE_SIZE) == -1)
156 1.1 maxv err(errno, "nvmm_hva_map");
157 1.1 maxv if (nvmm_hva_map(mach, (uintptr_t)L1, PAGE_SIZE) == -1)
158 1.1 maxv err(errno, "nvmm_hva_map");
159 1.1 maxv
160 1.1 maxv if (nvmm_gpa_map(mach, (uintptr_t)L4, 0x3000, PAGE_SIZE, 0) == -1)
161 1.1 maxv err(errno, "nvmm_gpa_map");
162 1.1 maxv if (nvmm_gpa_map(mach, (uintptr_t)L3, 0x4000, PAGE_SIZE, 0) == -1)
163 1.1 maxv err(errno, "nvmm_gpa_map");
164 1.1 maxv if (nvmm_gpa_map(mach, (uintptr_t)L2, 0x5000, PAGE_SIZE, 0) == -1)
165 1.1 maxv err(errno, "nvmm_gpa_map");
166 1.1 maxv if (nvmm_gpa_map(mach, (uintptr_t)L1, 0x6000, PAGE_SIZE, 0) == -1)
167 1.1 maxv err(errno, "nvmm_gpa_map");
168 1.1 maxv
169 1.1 maxv memset(L4, 0, PAGE_SIZE);
170 1.1 maxv memset(L3, 0, PAGE_SIZE);
171 1.1 maxv memset(L2, 0, PAGE_SIZE);
172 1.1 maxv memset(L1, 0, PAGE_SIZE);
173 1.1 maxv
174 1.1 maxv L4[0] = PG_V | PG_RW | 0x4000;
175 1.1 maxv L3[0] = PG_V | PG_RW | 0x5000;
176 1.1 maxv L2[0] = PG_V | PG_RW | 0x6000;
177 1.1 maxv L1[0x2000 / PAGE_SIZE] = PG_V | PG_RW | 0x2000;
178 1.1 maxv L1[0x1000 / PAGE_SIZE] = PG_V | PG_RW | 0x1000;
179 1.1 maxv }
180 1.1 maxv
181 1.1 maxv /* -------------------------------------------------------------------------- */
182 1.1 maxv
183 1.1 maxv static void
184 1.1 maxv mem_callback(struct nvmm_mem *mem)
185 1.1 maxv {
186 1.1 maxv size_t off;
187 1.1 maxv
188 1.1 maxv if (mem->gpa < 0x1000 || mem->gpa + mem->size > 0x1000 + PAGE_SIZE) {
189 1.1 maxv printf("Out of page\n");
190 1.1 maxv exit(-1);
191 1.1 maxv }
192 1.1 maxv
193 1.1 maxv off = mem->gpa - 0x1000;
194 1.1 maxv
195 1.1 maxv printf("-> gpa = %p\n", (void *)mem->gpa);
196 1.1 maxv
197 1.1 maxv if (mem->write) {
198 1.1 maxv memcpy(mmiobuf + off, mem->data, mem->size);
199 1.1 maxv } else {
200 1.1 maxv memcpy(mem->data, mmiobuf + off, mem->size);
201 1.1 maxv }
202 1.1 maxv }
203 1.2 maxv
204 1.1 maxv static int
205 1.1 maxv handle_memory(struct nvmm_machine *mach, struct nvmm_exit *exit)
206 1.1 maxv {
207 1.1 maxv int ret;
208 1.1 maxv
209 1.2 maxv ret = nvmm_assist_mem(mach, 0, exit);
210 1.1 maxv if (ret == -1) {
211 1.1 maxv err(errno, "nvmm_assist_mem");
212 1.1 maxv }
213 1.1 maxv
214 1.1 maxv return 0;
215 1.1 maxv }
216 1.1 maxv
217 1.1 maxv static void
218 1.1 maxv run_machine(struct nvmm_machine *mach)
219 1.1 maxv {
220 1.1 maxv struct nvmm_exit exit;
221 1.1 maxv
222 1.1 maxv while (1) {
223 1.1 maxv if (nvmm_vcpu_run(mach, 0, &exit) == -1)
224 1.1 maxv err(errno, "nvmm_vcpu_run");
225 1.1 maxv
226 1.1 maxv switch (exit.reason) {
227 1.1 maxv case NVMM_EXIT_NONE:
228 1.1 maxv break;
229 1.1 maxv
230 1.1 maxv case NVMM_EXIT_MSR:
231 1.1 maxv /* Stop here. */
232 1.1 maxv return;
233 1.1 maxv
234 1.1 maxv case NVMM_EXIT_MEMORY:
235 1.1 maxv handle_memory(mach, &exit);
236 1.1 maxv break;
237 1.1 maxv
238 1.1 maxv case NVMM_EXIT_SHUTDOWN:
239 1.1 maxv printf("Shutting down!\n");
240 1.1 maxv return;
241 1.1 maxv
242 1.1 maxv default:
243 1.1 maxv printf("Invalid!\n");
244 1.1 maxv return;
245 1.1 maxv }
246 1.1 maxv }
247 1.1 maxv }
248 1.1 maxv
249 1.1 maxv /* -------------------------------------------------------------------------- */
250 1.1 maxv
251 1.1 maxv struct test {
252 1.1 maxv const char *name;
253 1.1 maxv uint8_t *code_begin;
254 1.1 maxv uint8_t *code_end;
255 1.1 maxv uint64_t wanted;
256 1.1 maxv };
257 1.1 maxv
258 1.1 maxv static void
259 1.2 maxv run_test(struct nvmm_machine *mach, const struct test *test)
260 1.1 maxv {
261 1.1 maxv uint64_t *res;
262 1.1 maxv size_t size;
263 1.1 maxv
264 1.1 maxv size = (size_t)test->code_end - (size_t)test->code_begin;
265 1.1 maxv
266 1.1 maxv reset_machine(mach);
267 1.1 maxv
268 1.1 maxv memset(mmiobuf, 0, PAGE_SIZE);
269 1.1 maxv memcpy(instbuf, test->code_begin, size);
270 1.1 maxv
271 1.1 maxv run_machine(mach);
272 1.1 maxv
273 1.1 maxv res = (uint64_t *)mmiobuf;
274 1.1 maxv if (*res == test->wanted) {
275 1.1 maxv printf("Test '%s' passed\n", test->name);
276 1.1 maxv } else {
277 1.1 maxv printf("Test '%s' failed, wanted 0x%lx, got 0x%lx\n", test->name,
278 1.1 maxv test->wanted, *res);
279 1.1 maxv }
280 1.1 maxv }
281 1.1 maxv
282 1.1 maxv /* -------------------------------------------------------------------------- */
283 1.1 maxv
284 1.1 maxv extern uint8_t test1_begin, test1_end;
285 1.1 maxv extern uint8_t test2_begin, test2_end;
286 1.1 maxv extern uint8_t test3_begin, test3_end;
287 1.1 maxv extern uint8_t test4_begin, test4_end;
288 1.1 maxv extern uint8_t test5_begin, test5_end;
289 1.1 maxv extern uint8_t test6_begin, test6_end;
290 1.1 maxv extern uint8_t test7_begin, test7_end;
291 1.1 maxv extern uint8_t test8_begin, test8_end;
292 1.2 maxv extern uint8_t test9_begin, test9_end;
293 1.4 maxv extern uint8_t test10_begin, test10_end;
294 1.4 maxv extern uint8_t test11_begin, test11_end;
295 1.5 maxv extern uint8_t test12_begin, test12_end;
296 1.5 maxv extern uint8_t test13_begin, test13_end;
297 1.5 maxv extern uint8_t test14_begin, test14_end;
298 1.1 maxv
299 1.2 maxv static const struct test tests[] = {
300 1.1 maxv { "test1 - MOV", &test1_begin, &test1_end, 0x3004 },
301 1.5 maxv { "test2 - OR", &test2_begin, &test2_end, 0x16FF },
302 1.1 maxv { "test3 - AND", &test3_begin, &test3_end, 0x1FC0 },
303 1.1 maxv { "test4 - XOR", &test4_begin, &test4_end, 0x10CF },
304 1.1 maxv { "test5 - Address Sizes", &test5_begin, &test5_end, 0x1F00 },
305 1.1 maxv { "test6 - DMO", &test6_begin, &test6_end, 0xFFAB },
306 1.1 maxv { "test7 - STOS", &test7_begin, &test7_end, 0x00123456 },
307 1.1 maxv { "test8 - LODS", &test8_begin, &test8_end, 0x12345678 },
308 1.2 maxv { "test9 - MOVS", &test9_begin, &test9_end, 0x12345678 },
309 1.4 maxv { "test10 - MOVZXB", &test10_begin, &test10_end, 0x00000078 },
310 1.4 maxv { "test11 - MOVZXW", &test11_begin, &test11_end, 0x00005678 },
311 1.5 maxv { "test12 - CMP", &test12_begin, &test12_end, 0x00000001 },
312 1.5 maxv { "test13 - SUB", &test13_begin, &test13_end, 0x0000000F0000A0FF },
313 1.5 maxv { "test14 - TEST", &test14_begin, &test14_end, 0x00000001 },
314 1.1 maxv { NULL, NULL, NULL, -1 }
315 1.1 maxv };
316 1.1 maxv
317 1.2 maxv static const struct nvmm_callbacks callbacks = {
318 1.2 maxv .io = NULL,
319 1.2 maxv .mem = mem_callback
320 1.2 maxv };
321 1.2 maxv
322 1.1 maxv /*
323 1.1 maxv * 0x1000: MMIO address, unmapped
324 1.1 maxv * 0x2000: Instructions, mapped
325 1.1 maxv * 0x3000: L4
326 1.1 maxv * 0x4000: L3
327 1.1 maxv * 0x5000: L2
328 1.1 maxv * 0x6000: L1
329 1.1 maxv */
330 1.1 maxv int main(int argc, char *argv[])
331 1.1 maxv {
332 1.1 maxv struct nvmm_machine mach;
333 1.1 maxv size_t i;
334 1.1 maxv
335 1.1 maxv if (nvmm_machine_create(&mach) == -1)
336 1.1 maxv err(errno, "nvmm_machine_create");
337 1.1 maxv if (nvmm_vcpu_create(&mach, 0) == -1)
338 1.1 maxv err(errno, "nvmm_vcpu_create");
339 1.2 maxv nvmm_callbacks_register(&callbacks);
340 1.1 maxv map_pages(&mach);
341 1.1 maxv
342 1.1 maxv for (i = 0; tests[i].name != NULL; i++) {
343 1.1 maxv run_test(&mach, &tests[i]);
344 1.1 maxv }
345 1.1 maxv
346 1.1 maxv return 0;
347 1.1 maxv }
348