x86_xpmap.c revision 1.68 1 1.68 maxv /* $NetBSD: x86_xpmap.c,v 1.68 2016/12/16 19:52:22 maxv Exp $ */
2 1.2 bouyer
3 1.2 bouyer /*
4 1.2 bouyer * Copyright (c) 2006 Mathieu Ropert <mro (at) adviseo.fr>
5 1.2 bouyer *
6 1.2 bouyer * Permission to use, copy, modify, and distribute this software for any
7 1.2 bouyer * purpose with or without fee is hereby granted, provided that the above
8 1.2 bouyer * copyright notice and this permission notice appear in all copies.
9 1.2 bouyer *
10 1.2 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.2 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.2 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.2 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.2 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.2 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.2 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.2 bouyer */
18 1.2 bouyer
19 1.2 bouyer /*
20 1.2 bouyer * Copyright (c) 2006, 2007 Manuel Bouyer.
21 1.2 bouyer *
22 1.2 bouyer * Redistribution and use in source and binary forms, with or without
23 1.2 bouyer * modification, are permitted provided that the following conditions
24 1.2 bouyer * are met:
25 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
26 1.2 bouyer * notice, this list of conditions and the following disclaimer.
27 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
28 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
29 1.2 bouyer * documentation and/or other materials provided with the distribution.
30 1.2 bouyer *
31 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 1.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 1.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 1.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 1.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 1.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 1.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 1.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 1.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 1.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.2 bouyer */
42 1.2 bouyer
43 1.2 bouyer /*
44 1.2 bouyer * Copyright (c) 2004 Christian Limpach.
45 1.2 bouyer * All rights reserved.
46 1.2 bouyer *
47 1.2 bouyer * Redistribution and use in source and binary forms, with or without
48 1.2 bouyer * modification, are permitted provided that the following conditions
49 1.2 bouyer * are met:
50 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
51 1.2 bouyer * notice, this list of conditions and the following disclaimer.
52 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
53 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
54 1.2 bouyer * documentation and/or other materials provided with the distribution.
55 1.2 bouyer *
56 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57 1.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58 1.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59 1.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60 1.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61 1.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62 1.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63 1.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64 1.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65 1.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 1.2 bouyer */
67 1.2 bouyer
68 1.2 bouyer #include <sys/cdefs.h>
69 1.68 maxv __KERNEL_RCSID(0, "$NetBSD: x86_xpmap.c,v 1.68 2016/12/16 19:52:22 maxv Exp $");
70 1.2 bouyer
71 1.2 bouyer #include "opt_xen.h"
72 1.4 bouyer #include "opt_ddb.h"
73 1.4 bouyer #include "ksyms.h"
74 1.2 bouyer
75 1.2 bouyer #include <sys/param.h>
76 1.2 bouyer #include <sys/systm.h>
77 1.38 cherry #include <sys/mutex.h>
78 1.42 bouyer #include <sys/cpu.h>
79 1.2 bouyer
80 1.2 bouyer #include <uvm/uvm.h>
81 1.2 bouyer
82 1.42 bouyer #include <x86/pmap.h>
83 1.2 bouyer #include <machine/gdt.h>
84 1.2 bouyer #include <xen/xenfunc.h>
85 1.2 bouyer
86 1.2 bouyer #include <dev/isa/isareg.h>
87 1.2 bouyer #include <machine/isa_machdep.h>
88 1.2 bouyer
89 1.2 bouyer #undef XENDEBUG
90 1.2 bouyer /* #define XENDEBUG_SYNC */
91 1.2 bouyer
92 1.2 bouyer #ifdef XENDEBUG
93 1.2 bouyer #define XENPRINTF(x) printf x
94 1.2 bouyer #define XENPRINTK2(x) /* printk x */
95 1.2 bouyer static char XBUF[256];
96 1.2 bouyer #else
97 1.2 bouyer #define XENPRINTF(x)
98 1.2 bouyer #define XENPRINTK2(x)
99 1.2 bouyer #endif
100 1.2 bouyer
101 1.2 bouyer volatile shared_info_t *HYPERVISOR_shared_info;
102 1.11 jym /* Xen requires the start_info struct to be page aligned */
103 1.11 jym union start_info_union start_info_union __aligned(PAGE_SIZE);
104 1.6 bouyer unsigned long *xpmap_phys_to_machine_mapping;
105 1.37 cherry kmutex_t pte_lock;
106 1.68 maxv vaddr_t xen_dummy_page;
107 1.2 bouyer
108 1.2 bouyer void xen_failsafe_handler(void);
109 1.2 bouyer
110 1.2 bouyer #define HYPERVISOR_mmu_update_self(req, count, success_count) \
111 1.2 bouyer HYPERVISOR_mmu_update((req), (count), (success_count), DOMID_SELF)
112 1.2 bouyer
113 1.64 maxv extern volatile struct xencons_interface *xencons_interface; /* XXX */
114 1.64 maxv extern struct xenstore_domain_interface *xenstore_interface; /* XXX */
115 1.64 maxv
116 1.64 maxv static void xen_bt_set_readonly(vaddr_t);
117 1.66 maxv static void xen_bootstrap_tables(vaddr_t, vaddr_t, size_t, size_t, bool);
118 1.64 maxv
119 1.65 maxv vaddr_t xen_locore(void);
120 1.64 maxv
121 1.48 bouyer /*
122 1.48 bouyer * kcpuset internally uses an array of uint32_t while xen uses an array of
123 1.48 bouyer * u_long. As we're little-endian we can cast one to the other.
124 1.48 bouyer */
125 1.48 bouyer typedef union {
126 1.48 bouyer #ifdef _LP64
127 1.48 bouyer uint32_t xcpum_km[2];
128 1.48 bouyer #else
129 1.48 bouyer uint32_t xcpum_km[1];
130 1.64 maxv #endif
131 1.64 maxv u_long xcpum_xm;
132 1.48 bouyer } xcpumask_t;
133 1.48 bouyer
134 1.2 bouyer void
135 1.2 bouyer xen_failsafe_handler(void)
136 1.2 bouyer {
137 1.2 bouyer
138 1.2 bouyer panic("xen_failsafe_handler called!\n");
139 1.2 bouyer }
140 1.2 bouyer
141 1.2 bouyer void
142 1.2 bouyer xen_set_ldt(vaddr_t base, uint32_t entries)
143 1.2 bouyer {
144 1.2 bouyer vaddr_t va;
145 1.2 bouyer vaddr_t end;
146 1.4 bouyer pt_entry_t *ptp;
147 1.2 bouyer int s;
148 1.2 bouyer
149 1.2 bouyer #ifdef __x86_64__
150 1.2 bouyer end = base + (entries << 3);
151 1.2 bouyer #else
152 1.2 bouyer end = base + entries * sizeof(union descriptor);
153 1.2 bouyer #endif
154 1.2 bouyer
155 1.2 bouyer for (va = base; va < end; va += PAGE_SIZE) {
156 1.2 bouyer KASSERT(va >= VM_MIN_KERNEL_ADDRESS);
157 1.2 bouyer ptp = kvtopte(va);
158 1.19 jym XENPRINTF(("xen_set_ldt %#" PRIxVADDR " %d %p\n",
159 1.19 jym base, entries, ptp));
160 1.4 bouyer pmap_pte_clearbits(ptp, PG_RW);
161 1.2 bouyer }
162 1.2 bouyer s = splvm();
163 1.2 bouyer xpq_queue_set_ldt(base, entries);
164 1.2 bouyer splx(s);
165 1.2 bouyer }
166 1.2 bouyer
167 1.2 bouyer #ifdef XENDEBUG
168 1.2 bouyer void xpq_debug_dump(void);
169 1.2 bouyer #endif
170 1.2 bouyer
171 1.2 bouyer #define XPQUEUE_SIZE 2048
172 1.35 cherry static mmu_update_t xpq_queue_array[MAXCPUS][XPQUEUE_SIZE];
173 1.35 cherry static int xpq_idx_array[MAXCPUS];
174 1.30 cherry
175 1.41 cherry #ifdef i386
176 1.41 cherry extern union descriptor tmpgdt[];
177 1.66 maxv #endif
178 1.66 maxv
179 1.2 bouyer void
180 1.35 cherry xpq_flush_queue(void)
181 1.30 cherry {
182 1.35 cherry int i, ok = 0, ret;
183 1.30 cherry
184 1.41 cherry mmu_update_t *xpq_queue = xpq_queue_array[curcpu()->ci_cpuid];
185 1.41 cherry int xpq_idx = xpq_idx_array[curcpu()->ci_cpuid];
186 1.2 bouyer
187 1.2 bouyer XENPRINTK2(("flush queue %p entries %d\n", xpq_queue, xpq_idx));
188 1.2 bouyer for (i = 0; i < xpq_idx; i++)
189 1.19 jym XENPRINTK2(("%d: 0x%08" PRIx64 " 0x%08" PRIx64 "\n", i,
190 1.19 jym xpq_queue[i].ptr, xpq_queue[i].val));
191 1.23 jym
192 1.35 cherry retry:
193 1.23 jym ret = HYPERVISOR_mmu_update_self(xpq_queue, xpq_idx, &ok);
194 1.23 jym
195 1.23 jym if (xpq_idx != 0 && ret < 0) {
196 1.39 bouyer struct cpu_info *ci;
197 1.39 bouyer CPU_INFO_ITERATOR cii;
198 1.39 bouyer
199 1.39 bouyer printf("xpq_flush_queue: %d entries (%d successful) on "
200 1.39 bouyer "cpu%d (%ld)\n",
201 1.41 cherry xpq_idx, ok, curcpu()->ci_index, curcpu()->ci_cpuid);
202 1.35 cherry
203 1.35 cherry if (ok != 0) {
204 1.35 cherry xpq_queue += ok;
205 1.35 cherry xpq_idx -= ok;
206 1.35 cherry ok = 0;
207 1.35 cherry goto retry;
208 1.35 cherry }
209 1.35 cherry
210 1.39 bouyer for (CPU_INFO_FOREACH(cii, ci)) {
211 1.39 bouyer xpq_queue = xpq_queue_array[ci->ci_cpuid];
212 1.39 bouyer xpq_idx = xpq_idx_array[ci->ci_cpuid];
213 1.39 bouyer printf("cpu%d (%ld):\n", ci->ci_index, ci->ci_cpuid);
214 1.39 bouyer for (i = 0; i < xpq_idx; i++) {
215 1.39 bouyer printf(" 0x%016" PRIx64 ": 0x%016" PRIx64 "\n",
216 1.39 bouyer xpq_queue[i].ptr, xpq_queue[i].val);
217 1.39 bouyer }
218 1.39 bouyer #ifdef __x86_64__
219 1.39 bouyer for (i = 0; i < PDIR_SLOT_PTE; i++) {
220 1.39 bouyer if (ci->ci_kpm_pdir[i] == 0)
221 1.39 bouyer continue;
222 1.39 bouyer printf(" kpm_pdir[%d]: 0x%" PRIx64 "\n",
223 1.39 bouyer i, ci->ci_kpm_pdir[i]);
224 1.39 bouyer }
225 1.39 bouyer #endif
226 1.39 bouyer }
227 1.23 jym panic("HYPERVISOR_mmu_update failed, ret: %d\n", ret);
228 1.2 bouyer }
229 1.41 cherry xpq_idx_array[curcpu()->ci_cpuid] = 0;
230 1.2 bouyer }
231 1.2 bouyer
232 1.2 bouyer static inline void
233 1.2 bouyer xpq_increment_idx(void)
234 1.2 bouyer {
235 1.2 bouyer
236 1.41 cherry if (__predict_false(++xpq_idx_array[curcpu()->ci_cpuid] == XPQUEUE_SIZE))
237 1.2 bouyer xpq_flush_queue();
238 1.2 bouyer }
239 1.2 bouyer
240 1.2 bouyer void
241 1.2 bouyer xpq_queue_machphys_update(paddr_t ma, paddr_t pa)
242 1.2 bouyer {
243 1.35 cherry
244 1.41 cherry mmu_update_t *xpq_queue = xpq_queue_array[curcpu()->ci_cpuid];
245 1.41 cherry int xpq_idx = xpq_idx_array[curcpu()->ci_cpuid];
246 1.35 cherry
247 1.6 bouyer XENPRINTK2(("xpq_queue_machphys_update ma=0x%" PRIx64 " pa=0x%" PRIx64
248 1.6 bouyer "\n", (int64_t)ma, (int64_t)pa));
249 1.35 cherry
250 1.2 bouyer xpq_queue[xpq_idx].ptr = ma | MMU_MACHPHYS_UPDATE;
251 1.45 jym xpq_queue[xpq_idx].val = pa >> PAGE_SHIFT;
252 1.2 bouyer xpq_increment_idx();
253 1.2 bouyer #ifdef XENDEBUG_SYNC
254 1.2 bouyer xpq_flush_queue();
255 1.2 bouyer #endif
256 1.2 bouyer }
257 1.2 bouyer
258 1.2 bouyer void
259 1.6 bouyer xpq_queue_pte_update(paddr_t ptr, pt_entry_t val)
260 1.2 bouyer {
261 1.2 bouyer
262 1.41 cherry mmu_update_t *xpq_queue = xpq_queue_array[curcpu()->ci_cpuid];
263 1.41 cherry int xpq_idx = xpq_idx_array[curcpu()->ci_cpuid];
264 1.35 cherry
265 1.6 bouyer KASSERT((ptr & 3) == 0);
266 1.2 bouyer xpq_queue[xpq_idx].ptr = (paddr_t)ptr | MMU_NORMAL_PT_UPDATE;
267 1.2 bouyer xpq_queue[xpq_idx].val = val;
268 1.2 bouyer xpq_increment_idx();
269 1.2 bouyer #ifdef XENDEBUG_SYNC
270 1.2 bouyer xpq_flush_queue();
271 1.2 bouyer #endif
272 1.2 bouyer }
273 1.2 bouyer
274 1.2 bouyer void
275 1.2 bouyer xpq_queue_pt_switch(paddr_t pa)
276 1.2 bouyer {
277 1.2 bouyer struct mmuext_op op;
278 1.2 bouyer xpq_flush_queue();
279 1.2 bouyer
280 1.6 bouyer XENPRINTK2(("xpq_queue_pt_switch: 0x%" PRIx64 " 0x%" PRIx64 "\n",
281 1.6 bouyer (int64_t)pa, (int64_t)pa));
282 1.2 bouyer op.cmd = MMUEXT_NEW_BASEPTR;
283 1.2 bouyer op.arg1.mfn = pa >> PAGE_SHIFT;
284 1.2 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
285 1.2 bouyer panic("xpq_queue_pt_switch");
286 1.2 bouyer }
287 1.2 bouyer
288 1.2 bouyer void
289 1.24 jym xpq_queue_pin_table(paddr_t pa, int lvl)
290 1.2 bouyer {
291 1.2 bouyer struct mmuext_op op;
292 1.29 cherry
293 1.2 bouyer xpq_flush_queue();
294 1.2 bouyer
295 1.24 jym XENPRINTK2(("xpq_queue_pin_l%d_table: %#" PRIxPADDR "\n",
296 1.24 jym lvl + 1, pa));
297 1.2 bouyer
298 1.6 bouyer op.arg1.mfn = pa >> PAGE_SHIFT;
299 1.24 jym op.cmd = lvl;
300 1.6 bouyer
301 1.6 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
302 1.6 bouyer panic("xpq_queue_pin_table");
303 1.6 bouyer }
304 1.6 bouyer
305 1.2 bouyer void
306 1.2 bouyer xpq_queue_unpin_table(paddr_t pa)
307 1.2 bouyer {
308 1.2 bouyer struct mmuext_op op;
309 1.29 cherry
310 1.2 bouyer xpq_flush_queue();
311 1.2 bouyer
312 1.24 jym XENPRINTK2(("xpq_queue_unpin_table: %#" PRIxPADDR "\n", pa));
313 1.2 bouyer op.arg1.mfn = pa >> PAGE_SHIFT;
314 1.2 bouyer op.cmd = MMUEXT_UNPIN_TABLE;
315 1.2 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
316 1.2 bouyer panic("xpq_queue_unpin_table");
317 1.2 bouyer }
318 1.2 bouyer
319 1.2 bouyer void
320 1.2 bouyer xpq_queue_set_ldt(vaddr_t va, uint32_t entries)
321 1.2 bouyer {
322 1.2 bouyer struct mmuext_op op;
323 1.29 cherry
324 1.2 bouyer xpq_flush_queue();
325 1.2 bouyer
326 1.2 bouyer XENPRINTK2(("xpq_queue_set_ldt\n"));
327 1.2 bouyer KASSERT(va == (va & ~PAGE_MASK));
328 1.2 bouyer op.cmd = MMUEXT_SET_LDT;
329 1.2 bouyer op.arg1.linear_addr = va;
330 1.2 bouyer op.arg2.nr_ents = entries;
331 1.2 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
332 1.2 bouyer panic("xpq_queue_set_ldt");
333 1.2 bouyer }
334 1.2 bouyer
335 1.2 bouyer void
336 1.8 cegger xpq_queue_tlb_flush(void)
337 1.2 bouyer {
338 1.2 bouyer struct mmuext_op op;
339 1.29 cherry
340 1.2 bouyer xpq_flush_queue();
341 1.2 bouyer
342 1.2 bouyer XENPRINTK2(("xpq_queue_tlb_flush\n"));
343 1.2 bouyer op.cmd = MMUEXT_TLB_FLUSH_LOCAL;
344 1.2 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
345 1.2 bouyer panic("xpq_queue_tlb_flush");
346 1.2 bouyer }
347 1.2 bouyer
348 1.2 bouyer void
349 1.8 cegger xpq_flush_cache(void)
350 1.2 bouyer {
351 1.52 jnemeth int s = splvm();
352 1.29 cherry
353 1.2 bouyer xpq_flush_queue();
354 1.2 bouyer
355 1.2 bouyer XENPRINTK2(("xpq_queue_flush_cache\n"));
356 1.52 jnemeth asm("wbinvd":::"memory");
357 1.29 cherry splx(s); /* XXX: removeme */
358 1.2 bouyer }
359 1.2 bouyer
360 1.2 bouyer void
361 1.2 bouyer xpq_queue_invlpg(vaddr_t va)
362 1.2 bouyer {
363 1.2 bouyer struct mmuext_op op;
364 1.2 bouyer xpq_flush_queue();
365 1.2 bouyer
366 1.19 jym XENPRINTK2(("xpq_queue_invlpg %#" PRIxVADDR "\n", va));
367 1.2 bouyer op.cmd = MMUEXT_INVLPG_LOCAL;
368 1.2 bouyer op.arg1.linear_addr = (va & ~PAGE_MASK);
369 1.2 bouyer if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
370 1.2 bouyer panic("xpq_queue_invlpg");
371 1.2 bouyer }
372 1.2 bouyer
373 1.29 cherry void
374 1.43 rmind xen_mcast_invlpg(vaddr_t va, kcpuset_t *kc)
375 1.29 cherry {
376 1.48 bouyer xcpumask_t xcpumask;
377 1.29 cherry mmuext_op_t op;
378 1.29 cherry
379 1.49 rmind kcpuset_export_u32(kc, &xcpumask.xcpum_km[0], sizeof(xcpumask));
380 1.44 rmind
381 1.29 cherry /* Flush pending page updates */
382 1.29 cherry xpq_flush_queue();
383 1.29 cherry
384 1.29 cherry op.cmd = MMUEXT_INVLPG_MULTI;
385 1.29 cherry op.arg1.linear_addr = va;
386 1.48 bouyer op.arg2.vcpumask = &xcpumask.xcpum_xm;
387 1.29 cherry
388 1.29 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
389 1.29 cherry panic("xpq_queue_invlpg_all");
390 1.29 cherry }
391 1.29 cherry
392 1.29 cherry return;
393 1.29 cherry }
394 1.29 cherry
395 1.29 cherry void
396 1.29 cherry xen_bcast_invlpg(vaddr_t va)
397 1.29 cherry {
398 1.29 cherry mmuext_op_t op;
399 1.29 cherry
400 1.29 cherry /* Flush pending page updates */
401 1.29 cherry xpq_flush_queue();
402 1.29 cherry
403 1.29 cherry op.cmd = MMUEXT_INVLPG_ALL;
404 1.29 cherry op.arg1.linear_addr = va;
405 1.29 cherry
406 1.29 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
407 1.29 cherry panic("xpq_queue_invlpg_all");
408 1.29 cherry }
409 1.29 cherry
410 1.29 cherry return;
411 1.29 cherry }
412 1.29 cherry
413 1.29 cherry /* This is a synchronous call. */
414 1.29 cherry void
415 1.43 rmind xen_mcast_tlbflush(kcpuset_t *kc)
416 1.29 cherry {
417 1.48 bouyer xcpumask_t xcpumask;
418 1.29 cherry mmuext_op_t op;
419 1.29 cherry
420 1.49 rmind kcpuset_export_u32(kc, &xcpumask.xcpum_km[0], sizeof(xcpumask));
421 1.44 rmind
422 1.29 cherry /* Flush pending page updates */
423 1.29 cherry xpq_flush_queue();
424 1.29 cherry
425 1.29 cherry op.cmd = MMUEXT_TLB_FLUSH_MULTI;
426 1.48 bouyer op.arg2.vcpumask = &xcpumask.xcpum_xm;
427 1.29 cherry
428 1.29 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
429 1.29 cherry panic("xpq_queue_invlpg_all");
430 1.29 cherry }
431 1.29 cherry
432 1.29 cherry return;
433 1.29 cherry }
434 1.29 cherry
435 1.29 cherry /* This is a synchronous call. */
436 1.29 cherry void
437 1.29 cherry xen_bcast_tlbflush(void)
438 1.29 cherry {
439 1.29 cherry mmuext_op_t op;
440 1.29 cherry
441 1.29 cherry /* Flush pending page updates */
442 1.29 cherry xpq_flush_queue();
443 1.29 cherry
444 1.29 cherry op.cmd = MMUEXT_TLB_FLUSH_ALL;
445 1.29 cherry
446 1.29 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
447 1.29 cherry panic("xpq_queue_invlpg_all");
448 1.29 cherry }
449 1.29 cherry
450 1.29 cherry return;
451 1.29 cherry }
452 1.29 cherry
453 1.29 cherry /* This is a synchronous call. */
454 1.29 cherry void
455 1.43 rmind xen_vcpu_mcast_invlpg(vaddr_t sva, vaddr_t eva, kcpuset_t *kc)
456 1.29 cherry {
457 1.29 cherry KASSERT(eva > sva);
458 1.29 cherry
459 1.29 cherry /* Flush pending page updates */
460 1.29 cherry xpq_flush_queue();
461 1.29 cherry
462 1.29 cherry /* Align to nearest page boundary */
463 1.29 cherry sva &= ~PAGE_MASK;
464 1.29 cherry eva &= ~PAGE_MASK;
465 1.29 cherry
466 1.29 cherry for ( ; sva <= eva; sva += PAGE_SIZE) {
467 1.43 rmind xen_mcast_invlpg(sva, kc);
468 1.29 cherry }
469 1.29 cherry
470 1.29 cherry return;
471 1.29 cherry }
472 1.29 cherry
473 1.29 cherry /* This is a synchronous call. */
474 1.29 cherry void
475 1.29 cherry xen_vcpu_bcast_invlpg(vaddr_t sva, vaddr_t eva)
476 1.29 cherry {
477 1.29 cherry KASSERT(eva > sva);
478 1.29 cherry
479 1.29 cherry /* Flush pending page updates */
480 1.29 cherry xpq_flush_queue();
481 1.29 cherry
482 1.29 cherry /* Align to nearest page boundary */
483 1.29 cherry sva &= ~PAGE_MASK;
484 1.29 cherry eva &= ~PAGE_MASK;
485 1.29 cherry
486 1.29 cherry for ( ; sva <= eva; sva += PAGE_SIZE) {
487 1.29 cherry xen_bcast_invlpg(sva);
488 1.29 cherry }
489 1.29 cherry
490 1.29 cherry return;
491 1.29 cherry }
492 1.29 cherry
493 1.53 cherry /* Copy a page */
494 1.53 cherry void
495 1.53 cherry xen_copy_page(paddr_t srcpa, paddr_t dstpa)
496 1.53 cherry {
497 1.53 cherry mmuext_op_t op;
498 1.53 cherry
499 1.53 cherry op.cmd = MMUEXT_COPY_PAGE;
500 1.53 cherry op.arg1.mfn = xpmap_ptom(dstpa) >> PAGE_SHIFT;
501 1.53 cherry op.arg2.src_mfn = xpmap_ptom(srcpa) >> PAGE_SHIFT;
502 1.53 cherry
503 1.53 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
504 1.53 cherry panic(__func__);
505 1.53 cherry }
506 1.53 cherry }
507 1.53 cherry
508 1.53 cherry /* Zero a physical page */
509 1.53 cherry void
510 1.53 cherry xen_pagezero(paddr_t pa)
511 1.53 cherry {
512 1.53 cherry mmuext_op_t op;
513 1.53 cherry
514 1.53 cherry op.cmd = MMUEXT_CLEAR_PAGE;
515 1.53 cherry op.arg1.mfn = xpmap_ptom(pa) >> PAGE_SHIFT;
516 1.53 cherry
517 1.53 cherry if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0) {
518 1.53 cherry panic(__func__);
519 1.53 cherry }
520 1.53 cherry }
521 1.53 cherry
522 1.2 bouyer int
523 1.6 bouyer xpq_update_foreign(paddr_t ptr, pt_entry_t val, int dom)
524 1.2 bouyer {
525 1.2 bouyer mmu_update_t op;
526 1.2 bouyer int ok;
527 1.29 cherry
528 1.2 bouyer xpq_flush_queue();
529 1.2 bouyer
530 1.6 bouyer op.ptr = ptr;
531 1.2 bouyer op.val = val;
532 1.2 bouyer if (HYPERVISOR_mmu_update(&op, 1, &ok, dom) < 0)
533 1.2 bouyer return EFAULT;
534 1.2 bouyer return (0);
535 1.2 bouyer }
536 1.2 bouyer
537 1.2 bouyer #ifdef XENDEBUG
538 1.2 bouyer void
539 1.8 cegger xpq_debug_dump(void)
540 1.2 bouyer {
541 1.2 bouyer int i;
542 1.2 bouyer
543 1.41 cherry mmu_update_t *xpq_queue = xpq_queue_array[curcpu()->ci_cpuid];
544 1.41 cherry int xpq_idx = xpq_idx_array[curcpu()->ci_cpuid];
545 1.35 cherry
546 1.2 bouyer XENPRINTK2(("idx: %d\n", xpq_idx));
547 1.2 bouyer for (i = 0; i < xpq_idx; i++) {
548 1.13 cegger snprintf(XBUF, sizeof(XBUF), "%" PRIx64 " %08" PRIx64,
549 1.19 jym xpq_queue[i].ptr, xpq_queue[i].val);
550 1.2 bouyer if (++i < xpq_idx)
551 1.13 cegger snprintf(XBUF + strlen(XBUF),
552 1.13 cegger sizeof(XBUF) - strlen(XBUF),
553 1.13 cegger "%" PRIx64 " %08" PRIx64,
554 1.19 jym xpq_queue[i].ptr, xpq_queue[i].val);
555 1.2 bouyer if (++i < xpq_idx)
556 1.13 cegger snprintf(XBUF + strlen(XBUF),
557 1.13 cegger sizeof(XBUF) - strlen(XBUF),
558 1.13 cegger "%" PRIx64 " %08" PRIx64,
559 1.19 jym xpq_queue[i].ptr, xpq_queue[i].val);
560 1.2 bouyer if (++i < xpq_idx)
561 1.13 cegger snprintf(XBUF + strlen(XBUF),
562 1.13 cegger sizeof(XBUF) - strlen(XBUF),
563 1.13 cegger "%" PRIx64 " %08" PRIx64,
564 1.19 jym xpq_queue[i].ptr, xpq_queue[i].val);
565 1.2 bouyer XENPRINTK2(("%d: %s\n", xpq_idx, XBUF));
566 1.2 bouyer }
567 1.2 bouyer }
568 1.2 bouyer #endif
569 1.2 bouyer
570 1.2 bouyer
571 1.2 bouyer #if L2_SLOT_KERNBASE > 0
572 1.2 bouyer #define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
573 1.2 bouyer #else
574 1.2 bouyer #define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1)
575 1.2 bouyer #endif
576 1.2 bouyer
577 1.6 bouyer #ifdef PAE
578 1.6 bouyer /*
579 1.66 maxv * For PAE, we consider a single contiguous L2 "superpage" of 4 pages, all of
580 1.64 maxv * them mapped by the L3 page. We also need a shadow page for L3[3].
581 1.6 bouyer */
582 1.6 bouyer static const int l2_4_count = 6;
583 1.36 cherry #elif defined(__x86_64__)
584 1.36 cherry static const int l2_4_count = PTP_LEVELS;
585 1.6 bouyer #else
586 1.6 bouyer static const int l2_4_count = PTP_LEVELS - 1;
587 1.6 bouyer #endif
588 1.6 bouyer
589 1.64 maxv /*
590 1.64 maxv * Xen locore: get rid of the Xen bootstrap tables. Build and switch to new page
591 1.64 maxv * tables.
592 1.68 maxv *
593 1.68 maxv * Virtual address space of the kernel when leaving this function:
594 1.68 maxv * +--------------+------------------+-------------+------------+---------------
595 1.68 maxv * | KERNEL IMAGE | BOOTSTRAP TABLES | PROC0 UAREA | DUMMY PAGE | HYPER. SHARED
596 1.68 maxv * +--------------+------------------+-------------+------------+---------------
597 1.68 maxv *
598 1.68 maxv * ------+-----------------+-------------+
599 1.68 maxv * INFO | EARLY ZERO PAGE | ISA I/O MEM |
600 1.68 maxv * ------+-----------------+-------------+
601 1.68 maxv *
602 1.68 maxv * DUMMY PAGE is either a PDG for amd64 or a GDT for i386.
603 1.68 maxv *
604 1.68 maxv * (HYPER. SHARED INFO + EARLY ZERO PAGE + ISA I/O MEM) have no physical
605 1.68 maxv * addresses preallocated.
606 1.64 maxv */
607 1.2 bouyer vaddr_t
608 1.65 maxv xen_locore(void)
609 1.2 bouyer {
610 1.64 maxv size_t count, oldcount, mapsize;
611 1.2 bouyer vaddr_t bootstrap_tables, init_tables;
612 1.2 bouyer
613 1.54 bouyer xen_init_features();
614 1.54 bouyer
615 1.55 maxv memset(xpq_idx_array, 0, sizeof(xpq_idx_array));
616 1.35 cherry
617 1.6 bouyer xpmap_phys_to_machine_mapping =
618 1.6 bouyer (unsigned long *)xen_start_info.mfn_list;
619 1.2 bouyer
620 1.2 bouyer /* Space after Xen boostrap tables should be free */
621 1.64 maxv init_tables = xen_start_info.pt_base;
622 1.64 maxv bootstrap_tables = init_tables +
623 1.64 maxv (xen_start_info.nr_pt_frames * PAGE_SIZE);
624 1.2 bouyer
625 1.4 bouyer /*
626 1.64 maxv * Calculate how much space we need. First, everything mapped before
627 1.64 maxv * the Xen bootstrap tables.
628 1.4 bouyer */
629 1.4 bouyer mapsize = init_tables - KERNTEXTOFF;
630 1.4 bouyer /* after the tables we'll have:
631 1.4 bouyer * - UAREA
632 1.4 bouyer * - dummy user PGD (x86_64)
633 1.4 bouyer * - HYPERVISOR_shared_info
634 1.40 bouyer * - early_zerop
635 1.4 bouyer * - ISA I/O mem (if needed)
636 1.4 bouyer */
637 1.55 maxv mapsize += UPAGES * PAGE_SIZE;
638 1.4 bouyer #ifdef __x86_64__
639 1.55 maxv mapsize += PAGE_SIZE;
640 1.4 bouyer #endif
641 1.55 maxv mapsize += PAGE_SIZE;
642 1.55 maxv mapsize += PAGE_SIZE;
643 1.2 bouyer #ifdef DOM0OPS
644 1.10 cegger if (xendomain_is_dom0()) {
645 1.4 bouyer mapsize += IOM_SIZE;
646 1.4 bouyer }
647 1.4 bouyer #endif
648 1.4 bouyer
649 1.64 maxv /*
650 1.64 maxv * At this point, mapsize doesn't include the table size.
651 1.64 maxv */
652 1.4 bouyer #ifdef __x86_64__
653 1.4 bouyer count = TABLE_L2_ENTRIES;
654 1.4 bouyer #else
655 1.66 maxv count = (mapsize + (NBPD_L2 - 1)) >> L2_SHIFT;
656 1.64 maxv #endif
657 1.64 maxv
658 1.64 maxv /*
659 1.64 maxv * Now compute how many L2 pages we need exactly. This is useful only
660 1.64 maxv * on i386, since the initial count for amd64 is already enough.
661 1.64 maxv */
662 1.66 maxv while (KERNTEXTOFF + mapsize + (count + l2_4_count) * PAGE_SIZE >
663 1.66 maxv KERNBASE + (count << L2_SHIFT)) {
664 1.4 bouyer count++;
665 1.2 bouyer }
666 1.64 maxv
667 1.4 bouyer #ifndef __x86_64__
668 1.5 bouyer /*
669 1.64 maxv * One more L2 page: we'll allocate several pages after kva_start
670 1.5 bouyer * in pmap_bootstrap() before pmap_growkernel(), which have not been
671 1.5 bouyer * counted here. It's not a big issue to allocate one more L2 as
672 1.5 bouyer * pmap_growkernel() will be called anyway.
673 1.5 bouyer */
674 1.5 bouyer count++;
675 1.4 bouyer nkptp[1] = count;
676 1.2 bouyer #endif
677 1.2 bouyer
678 1.4 bouyer /*
679 1.64 maxv * Install bootstrap pages. We may need more L2 pages than will
680 1.64 maxv * have the final table here, as it's installed after the final table.
681 1.4 bouyer */
682 1.4 bouyer oldcount = count;
683 1.4 bouyer
684 1.4 bouyer bootstrap_again:
685 1.64 maxv
686 1.2 bouyer /*
687 1.2 bouyer * Xen space we'll reclaim may not be enough for our new page tables,
688 1.64 maxv * move bootstrap tables if necessary.
689 1.2 bouyer */
690 1.4 bouyer if (bootstrap_tables < init_tables + ((count + l2_4_count) * PAGE_SIZE))
691 1.2 bouyer bootstrap_tables = init_tables +
692 1.64 maxv ((count + l2_4_count) * PAGE_SIZE);
693 1.64 maxv
694 1.66 maxv /*
695 1.66 maxv * Make sure the number of L2 pages we have is enough to map everything
696 1.66 maxv * from KERNBASE to the bootstrap tables themselves.
697 1.66 maxv */
698 1.4 bouyer if (bootstrap_tables + ((oldcount + l2_4_count) * PAGE_SIZE) >
699 1.66 maxv KERNBASE + (oldcount << L2_SHIFT)) {
700 1.4 bouyer oldcount++;
701 1.4 bouyer goto bootstrap_again;
702 1.4 bouyer }
703 1.2 bouyer
704 1.2 bouyer /* Create temporary tables */
705 1.64 maxv xen_bootstrap_tables(init_tables, bootstrap_tables,
706 1.66 maxv xen_start_info.nr_pt_frames, oldcount, false);
707 1.2 bouyer
708 1.2 bouyer /* Create final tables */
709 1.2 bouyer xen_bootstrap_tables(bootstrap_tables, init_tables,
710 1.66 maxv oldcount + l2_4_count, count, true);
711 1.2 bouyer
712 1.68 maxv /* Zero out PROC0 UAREA and DUMMY PAGE. */
713 1.4 bouyer memset((void *)(init_tables + ((count + l2_4_count) * PAGE_SIZE)), 0,
714 1.55 maxv (UPAGES + 1) * PAGE_SIZE);
715 1.28 rmind
716 1.28 rmind /* Finally, flush TLB. */
717 1.28 rmind xpq_queue_tlb_flush();
718 1.28 rmind
719 1.4 bouyer return (init_tables + ((count + l2_4_count) * PAGE_SIZE));
720 1.2 bouyer }
721 1.2 bouyer
722 1.2 bouyer /*
723 1.55 maxv * Build a new table and switch to it.
724 1.55 maxv * old_count is # of old tables (including PGD, PDTPE and PDE).
725 1.55 maxv * new_count is # of new tables (PTE only).
726 1.55 maxv * We assume the areas don't overlap.
727 1.2 bouyer */
728 1.2 bouyer static void
729 1.64 maxv xen_bootstrap_tables(vaddr_t old_pgd, vaddr_t new_pgd, size_t old_count,
730 1.66 maxv size_t new_count, bool final)
731 1.2 bouyer {
732 1.2 bouyer pd_entry_t *pdtpe, *pde, *pte;
733 1.50 mrg pd_entry_t *bt_pgd;
734 1.6 bouyer paddr_t addr;
735 1.61 bouyer vaddr_t page, avail, map_end;
736 1.2 bouyer int i;
737 1.61 bouyer extern char __rodata_start;
738 1.2 bouyer extern char __data_start;
739 1.61 bouyer extern char __kernel_end;
740 1.40 bouyer extern char *early_zerop; /* from pmap.c */
741 1.61 bouyer pt_entry_t pg_nx;
742 1.61 bouyer u_int descs[4];
743 1.2 bouyer
744 1.61 bouyer /*
745 1.61 bouyer * Set the NX/XD bit, if available. descs[3] = %edx.
746 1.61 bouyer */
747 1.61 bouyer x86_cpuid(0x80000001, descs);
748 1.61 bouyer pg_nx = (descs[3] & CPUID_NOX) ? PG_NX : 0;
749 1.55 maxv
750 1.2 bouyer /*
751 1.66 maxv * Layout of RW area after the kernel image:
752 1.55 maxv * xencons_interface (if present)
753 1.55 maxv * xenstore_interface (if present)
754 1.55 maxv * table pages (new_count + l2_4_count entries)
755 1.64 maxv * Extra mappings (only when final is true):
756 1.55 maxv * UAREA
757 1.64 maxv * dummy user PGD (x86_64 only) / GDT page (i386 only)
758 1.55 maxv * HYPERVISOR_shared_info
759 1.55 maxv * early_zerop
760 1.55 maxv * ISA I/O mem (if needed)
761 1.2 bouyer */
762 1.55 maxv map_end = new_pgd + ((new_count + l2_4_count) * PAGE_SIZE);
763 1.2 bouyer if (final) {
764 1.68 maxv map_end += UPAGES * PAGE_SIZE;
765 1.68 maxv xen_dummy_page = (vaddr_t)map_end;
766 1.68 maxv map_end += PAGE_SIZE;
767 1.4 bouyer HYPERVISOR_shared_info = (shared_info_t *)map_end;
768 1.55 maxv map_end += PAGE_SIZE;
769 1.40 bouyer early_zerop = (char *)map_end;
770 1.55 maxv map_end += PAGE_SIZE;
771 1.2 bouyer }
772 1.55 maxv
773 1.4 bouyer /*
774 1.64 maxv * We always set atdevbase, as it's used by init386 to find the first
775 1.4 bouyer * available VA. map_end is updated only if we are dom0, so
776 1.4 bouyer * atdevbase -> atdevbase + IOM_SIZE will be mapped only in
777 1.4 bouyer * this case.
778 1.4 bouyer */
779 1.66 maxv if (final) {
780 1.4 bouyer atdevbase = map_end;
781 1.2 bouyer #ifdef DOM0OPS
782 1.66 maxv if (xendomain_is_dom0()) {
783 1.66 maxv /* ISA I/O mem */
784 1.66 maxv map_end += IOM_SIZE;
785 1.66 maxv }
786 1.66 maxv #endif
787 1.2 bouyer }
788 1.2 bouyer
789 1.61 bouyer __PRINTK(("xen_bootstrap_tables map_end 0x%lx\n", map_end));
790 1.19 jym __PRINTK(("console %#lx ", xen_start_info.console_mfn));
791 1.19 jym __PRINTK(("xenstore %#" PRIx32 "\n", xen_start_info.store_mfn));
792 1.2 bouyer
793 1.2 bouyer /*
794 1.55 maxv * Create bootstrap page tables. What we need:
795 1.2 bouyer * - a PGD (level 4)
796 1.2 bouyer * - a PDTPE (level 3)
797 1.55 maxv * - a PDE (level 2)
798 1.2 bouyer * - some PTEs (level 1)
799 1.2 bouyer */
800 1.2 bouyer
801 1.55 maxv bt_pgd = (pd_entry_t *)new_pgd;
802 1.55 maxv memset(bt_pgd, 0, PAGE_SIZE);
803 1.2 bouyer avail = new_pgd + PAGE_SIZE;
804 1.55 maxv
805 1.4 bouyer #if PTP_LEVELS > 3
806 1.64 maxv /* Per-cpu L4 */
807 1.36 cherry pd_entry_t *bt_cpu_pgd = bt_pgd;
808 1.64 maxv /* pmap_kernel() "shadow" L4 */
809 1.55 maxv bt_pgd = (pd_entry_t *)avail;
810 1.36 cherry memset(bt_pgd, 0, PAGE_SIZE);
811 1.36 cherry avail += PAGE_SIZE;
812 1.36 cherry
813 1.64 maxv /* Install L3 */
814 1.55 maxv pdtpe = (pd_entry_t *)avail;
815 1.55 maxv memset(pdtpe, 0, PAGE_SIZE);
816 1.2 bouyer avail += PAGE_SIZE;
817 1.2 bouyer
818 1.55 maxv addr = ((u_long)pdtpe) - KERNBASE;
819 1.67 maxv bt_pgd[pl4_pi(KERNTEXTOFF)] = bt_cpu_pgd[pl4_pi(KERNTEXTOFF)] =
820 1.66 maxv xpmap_ptom_masked(addr) | PG_k | PG_V | PG_RW;
821 1.4 bouyer #else
822 1.4 bouyer pdtpe = bt_pgd;
823 1.64 maxv #endif
824 1.2 bouyer
825 1.4 bouyer #if PTP_LEVELS > 2
826 1.2 bouyer /* Level 2 */
827 1.55 maxv pde = (pd_entry_t *)avail;
828 1.2 bouyer memset(pde, 0, PAGE_SIZE);
829 1.2 bouyer avail += PAGE_SIZE;
830 1.2 bouyer
831 1.55 maxv addr = ((u_long)pde) - KERNBASE;
832 1.67 maxv pdtpe[pl3_pi(KERNTEXTOFF)] =
833 1.6 bouyer xpmap_ptom_masked(addr) | PG_k | PG_V | PG_RW;
834 1.6 bouyer #elif defined(PAE)
835 1.64 maxv /* Our PAE-style level 2: 5 contigous pages (4 L2 + 1 shadow) */
836 1.55 maxv pde = (pd_entry_t *)avail;
837 1.6 bouyer memset(pde, 0, PAGE_SIZE * 5);
838 1.6 bouyer avail += PAGE_SIZE * 5;
839 1.55 maxv addr = ((u_long)pde) - KERNBASE;
840 1.64 maxv
841 1.6 bouyer /*
842 1.64 maxv * Enter L2 pages in L3. The real L2 kernel PD will be the last one
843 1.64 maxv * (so that pde[L2_SLOT_KERN] always points to the shadow).
844 1.6 bouyer */
845 1.6 bouyer for (i = 0; i < 3; i++, addr += PAGE_SIZE) {
846 1.6 bouyer /*
847 1.64 maxv * Xen doesn't want RW mappings in L3 entries, it'll add it
848 1.6 bouyer * itself.
849 1.6 bouyer */
850 1.6 bouyer pdtpe[i] = xpmap_ptom_masked(addr) | PG_k | PG_V;
851 1.6 bouyer }
852 1.6 bouyer addr += PAGE_SIZE;
853 1.6 bouyer pdtpe[3] = xpmap_ptom_masked(addr) | PG_k | PG_V;
854 1.64 maxv #else
855 1.4 bouyer pde = bt_pgd;
856 1.64 maxv #endif
857 1.2 bouyer
858 1.2 bouyer /* Level 1 */
859 1.2 bouyer page = KERNTEXTOFF;
860 1.2 bouyer for (i = 0; i < new_count; i ++) {
861 1.6 bouyer vaddr_t cur_page = page;
862 1.2 bouyer
863 1.55 maxv pte = (pd_entry_t *)avail;
864 1.2 bouyer avail += PAGE_SIZE;
865 1.2 bouyer
866 1.2 bouyer memset(pte, 0, PAGE_SIZE);
867 1.55 maxv while (pl2_pi(page) == pl2_pi(cur_page)) {
868 1.2 bouyer if (page >= map_end) {
869 1.2 bouyer /* not mapped at all */
870 1.2 bouyer pte[pl1_pi(page)] = 0;
871 1.2 bouyer page += PAGE_SIZE;
872 1.2 bouyer continue;
873 1.2 bouyer }
874 1.2 bouyer pte[pl1_pi(page)] = xpmap_ptom_masked(page - KERNBASE);
875 1.2 bouyer if (page == (vaddr_t)HYPERVISOR_shared_info) {
876 1.2 bouyer pte[pl1_pi(page)] = xen_start_info.shared_info;
877 1.2 bouyer }
878 1.7 bouyer if ((xpmap_ptom_masked(page - KERNBASE) >> PAGE_SHIFT)
879 1.12 cegger == xen_start_info.console.domU.mfn) {
880 1.2 bouyer xencons_interface = (void *)page;
881 1.19 jym pte[pl1_pi(page)] = xen_start_info.console_mfn;
882 1.6 bouyer pte[pl1_pi(page)] <<= PAGE_SHIFT;
883 1.2 bouyer }
884 1.7 bouyer if ((xpmap_ptom_masked(page - KERNBASE) >> PAGE_SHIFT)
885 1.7 bouyer == xen_start_info.store_mfn) {
886 1.2 bouyer xenstore_interface = (void *)page;
887 1.6 bouyer pte[pl1_pi(page)] = xen_start_info.store_mfn;
888 1.6 bouyer pte[pl1_pi(page)] <<= PAGE_SHIFT;
889 1.2 bouyer }
890 1.2 bouyer #ifdef DOM0OPS
891 1.2 bouyer if (page >= (vaddr_t)atdevbase &&
892 1.2 bouyer page < (vaddr_t)atdevbase + IOM_SIZE) {
893 1.2 bouyer pte[pl1_pi(page)] =
894 1.2 bouyer IOM_BEGIN + (page - (vaddr_t)atdevbase);
895 1.61 bouyer pte[pl1_pi(page)] |= pg_nx;
896 1.2 bouyer }
897 1.2 bouyer #endif
898 1.61 bouyer
899 1.4 bouyer pte[pl1_pi(page)] |= PG_k | PG_V;
900 1.61 bouyer if (page < (vaddr_t)&__rodata_start) {
901 1.61 bouyer /* Map the kernel text RX. */
902 1.56 maxv pte[pl1_pi(page)] |= PG_RO;
903 1.61 bouyer } else if (page >= (vaddr_t)&__rodata_start &&
904 1.61 bouyer page < (vaddr_t)&__data_start) {
905 1.61 bouyer /* Map the kernel rodata R. */
906 1.61 bouyer pte[pl1_pi(page)] |= PG_RO | pg_nx;
907 1.55 maxv } else if (page >= old_pgd &&
908 1.55 maxv page < old_pgd + (old_count * PAGE_SIZE)) {
909 1.61 bouyer /* Map the old page tables R. */
910 1.61 bouyer pte[pl1_pi(page)] |= PG_RO | pg_nx;
911 1.2 bouyer } else if (page >= new_pgd &&
912 1.6 bouyer page < new_pgd + ((new_count + l2_4_count) * PAGE_SIZE)) {
913 1.61 bouyer /* Map the new page tables R. */
914 1.61 bouyer pte[pl1_pi(page)] |= PG_RO | pg_nx;
915 1.41 cherry #ifdef i386
916 1.41 cherry } else if (page == (vaddr_t)tmpgdt) {
917 1.41 cherry /*
918 1.64 maxv * Map bootstrap gdt R/O. Later, we will re-add
919 1.64 maxv * this page to uvm after making it writable.
920 1.41 cherry */
921 1.41 cherry pte[pl1_pi(page)] = 0;
922 1.41 cherry page += PAGE_SIZE;
923 1.41 cherry continue;
924 1.64 maxv #endif
925 1.61 bouyer } else if (page >= (vaddr_t)&__data_start &&
926 1.61 bouyer page < (vaddr_t)&__kernel_end) {
927 1.61 bouyer /* Map the kernel data+bss RW. */
928 1.61 bouyer pte[pl1_pi(page)] |= PG_RW | pg_nx;
929 1.2 bouyer } else {
930 1.62 maxv /* Map the page RW. */
931 1.62 maxv pte[pl1_pi(page)] |= PG_RW | pg_nx;
932 1.2 bouyer }
933 1.64 maxv
934 1.2 bouyer page += PAGE_SIZE;
935 1.2 bouyer }
936 1.2 bouyer
937 1.64 maxv addr = ((u_long)pte) - KERNBASE;
938 1.2 bouyer pde[pl2_pi(cur_page)] =
939 1.4 bouyer xpmap_ptom_masked(addr) | PG_k | PG_RW | PG_V;
940 1.64 maxv
941 1.2 bouyer /* Mark readonly */
942 1.64 maxv xen_bt_set_readonly((vaddr_t)pte);
943 1.2 bouyer }
944 1.2 bouyer
945 1.2 bouyer /* Install recursive page tables mapping */
946 1.6 bouyer #ifdef PAE
947 1.6 bouyer /*
948 1.64 maxv * We need a shadow page for the kernel's L2 page.
949 1.6 bouyer * The real L2 kernel PD will be the last one (so that
950 1.64 maxv * pde[L2_SLOT_KERN] always points to the shadow).
951 1.6 bouyer */
952 1.6 bouyer memcpy(&pde[L2_SLOT_KERN + NPDPG], &pde[L2_SLOT_KERN], PAGE_SIZE);
953 1.36 cherry cpu_info_primary.ci_kpm_pdir = &pde[L2_SLOT_KERN + NPDPG];
954 1.36 cherry cpu_info_primary.ci_kpm_pdirpa =
955 1.36 cherry (vaddr_t) cpu_info_primary.ci_kpm_pdir - KERNBASE;
956 1.6 bouyer
957 1.6 bouyer /*
958 1.64 maxv * We don't enter a recursive entry from the L3 PD. Instead, we enter
959 1.64 maxv * the first 4 L2 pages, which includes the kernel's L2 shadow. But we
960 1.64 maxv * have to enter the shadow after switching %cr3, or Xen will refcount
961 1.64 maxv * some PTEs with the wrong type.
962 1.6 bouyer */
963 1.6 bouyer addr = (u_long)pde - KERNBASE;
964 1.6 bouyer for (i = 0; i < 3; i++, addr += PAGE_SIZE) {
965 1.63 maxv pde[PDIR_SLOT_PTE + i] = xpmap_ptom_masked(addr) | PG_k | PG_V |
966 1.63 maxv pg_nx;
967 1.6 bouyer }
968 1.6 bouyer #if 0
969 1.6 bouyer addr += PAGE_SIZE; /* point to shadow L2 */
970 1.6 bouyer pde[PDIR_SLOT_PTE + 3] = xpmap_ptom_masked(addr) | PG_k | PG_V;
971 1.6 bouyer #endif
972 1.14 jym /* Mark tables RO, and pin the kernel's shadow as L2 */
973 1.6 bouyer addr = (u_long)pde - KERNBASE;
974 1.6 bouyer for (i = 0; i < 5; i++, addr += PAGE_SIZE) {
975 1.6 bouyer xen_bt_set_readonly(((vaddr_t)pde) + PAGE_SIZE * i);
976 1.64 maxv #if 0
977 1.6 bouyer if (i == 2 || i == 3)
978 1.6 bouyer continue;
979 1.24 jym xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
980 1.6 bouyer #endif
981 1.6 bouyer }
982 1.6 bouyer if (final) {
983 1.6 bouyer addr = (u_long)pde - KERNBASE + 3 * PAGE_SIZE;
984 1.24 jym xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
985 1.6 bouyer }
986 1.6 bouyer #if 0
987 1.6 bouyer addr = (u_long)pde - KERNBASE + 2 * PAGE_SIZE;
988 1.24 jym xpq_queue_pin_l2_table(xpmap_ptom_masked(addr));
989 1.6 bouyer #endif
990 1.6 bouyer #else /* PAE */
991 1.61 bouyer
992 1.61 bouyer /* Recursive entry in pmap_kernel(). */
993 1.61 bouyer bt_pgd[PDIR_SLOT_PTE] = xpmap_ptom_masked((paddr_t)bt_pgd - KERNBASE)
994 1.61 bouyer | PG_k | PG_RO | PG_V | pg_nx;
995 1.36 cherry #ifdef __x86_64__
996 1.61 bouyer /* Recursive entry in higher-level per-cpu PD. */
997 1.61 bouyer bt_cpu_pgd[PDIR_SLOT_PTE] = xpmap_ptom_masked((paddr_t)bt_cpu_pgd - KERNBASE)
998 1.61 bouyer | PG_k | PG_RO | PG_V | pg_nx;
999 1.61 bouyer #endif
1000 1.61 bouyer
1001 1.2 bouyer /* Mark tables RO */
1002 1.64 maxv xen_bt_set_readonly((vaddr_t)pde);
1003 1.6 bouyer #endif
1004 1.6 bouyer #if PTP_LEVELS > 2 || defined(PAE)
1005 1.64 maxv xen_bt_set_readonly((vaddr_t)pdtpe);
1006 1.4 bouyer #endif
1007 1.4 bouyer #if PTP_LEVELS > 3
1008 1.2 bouyer xen_bt_set_readonly(new_pgd);
1009 1.4 bouyer #endif
1010 1.61 bouyer
1011 1.2 bouyer /* Pin the PGD */
1012 1.24 jym #ifdef __x86_64__
1013 1.24 jym xpq_queue_pin_l4_table(xpmap_ptom_masked(new_pgd - KERNBASE));
1014 1.24 jym #elif PAE
1015 1.6 bouyer xpq_queue_pin_l3_table(xpmap_ptom_masked(new_pgd - KERNBASE));
1016 1.6 bouyer #else
1017 1.24 jym xpq_queue_pin_l2_table(xpmap_ptom_masked(new_pgd - KERNBASE));
1018 1.6 bouyer #endif
1019 1.21 jym
1020 1.4 bouyer /* Save phys. addr of PDP, for libkvm. */
1021 1.6 bouyer #ifdef PAE
1022 1.21 jym PDPpaddr = (u_long)pde - KERNBASE; /* PDP is the L2 with PAE */
1023 1.21 jym #else
1024 1.36 cherry PDPpaddr = (u_long)bt_pgd - KERNBASE;
1025 1.21 jym #endif
1026 1.21 jym
1027 1.2 bouyer /* Switch to new tables */
1028 1.2 bouyer xpq_queue_pt_switch(xpmap_ptom_masked(new_pgd - KERNBASE));
1029 1.21 jym
1030 1.6 bouyer #ifdef PAE
1031 1.6 bouyer if (final) {
1032 1.64 maxv /* Save the address of the L3 page */
1033 1.21 jym cpu_info_primary.ci_pae_l3_pdir = pdtpe;
1034 1.21 jym cpu_info_primary.ci_pae_l3_pdirpa = (new_pgd - KERNBASE);
1035 1.21 jym
1036 1.64 maxv /* Now enter the kernel's PTE mappings */
1037 1.64 maxv addr = (u_long)pde - KERNBASE + PAGE_SIZE * 3;
1038 1.6 bouyer xpq_queue_pte_update(
1039 1.64 maxv xpmap_ptom(((vaddr_t)&pde[PDIR_SLOT_PTE + 3]) - KERNBASE),
1040 1.6 bouyer xpmap_ptom_masked(addr) | PG_k | PG_V);
1041 1.6 bouyer xpq_flush_queue();
1042 1.6 bouyer }
1043 1.36 cherry #elif defined(__x86_64__)
1044 1.36 cherry if (final) {
1045 1.66 maxv /* Save the address of the real per-cpu L4 page. */
1046 1.36 cherry cpu_info_primary.ci_kpm_pdir = bt_cpu_pgd;
1047 1.66 maxv cpu_info_primary.ci_kpm_pdirpa = ((paddr_t)bt_cpu_pgd - KERNBASE);
1048 1.36 cherry }
1049 1.6 bouyer #endif
1050 1.51 christos __USE(pdtpe);
1051 1.6 bouyer
1052 1.66 maxv /*
1053 1.66 maxv * Now we can safely reclaim the space taken by the old tables.
1054 1.66 maxv */
1055 1.66 maxv
1056 1.2 bouyer /* Unpin old PGD */
1057 1.2 bouyer xpq_queue_unpin_table(xpmap_ptom_masked(old_pgd - KERNBASE));
1058 1.66 maxv
1059 1.2 bouyer /* Mark old tables RW */
1060 1.2 bouyer page = old_pgd;
1061 1.66 maxv addr = xpmap_mtop((paddr_t)pde[pl2_pi(page)] & PG_FRAME);
1062 1.64 maxv pte = (pd_entry_t *)((u_long)addr + KERNBASE);
1063 1.2 bouyer pte += pl1_pi(page);
1064 1.2 bouyer while (page < old_pgd + (old_count * PAGE_SIZE) && page < map_end) {
1065 1.66 maxv addr = xpmap_ptom(((u_long)pte) - KERNBASE);
1066 1.6 bouyer xpq_queue_pte_update(addr, *pte | PG_RW);
1067 1.2 bouyer page += PAGE_SIZE;
1068 1.2 bouyer /*
1069 1.55 maxv * Our PTEs are contiguous so it's safe to just "++" here.
1070 1.2 bouyer */
1071 1.2 bouyer pte++;
1072 1.2 bouyer }
1073 1.2 bouyer xpq_flush_queue();
1074 1.2 bouyer }
1075 1.2 bouyer
1076 1.2 bouyer
1077 1.2 bouyer /*
1078 1.2 bouyer * Bootstrap helper functions
1079 1.2 bouyer */
1080 1.2 bouyer
1081 1.2 bouyer /*
1082 1.2 bouyer * Mark a page readonly
1083 1.2 bouyer * XXX: assuming vaddr = paddr + KERNBASE
1084 1.2 bouyer */
1085 1.2 bouyer
1086 1.2 bouyer static void
1087 1.64 maxv xen_bt_set_readonly(vaddr_t page)
1088 1.2 bouyer {
1089 1.2 bouyer pt_entry_t entry;
1090 1.2 bouyer
1091 1.2 bouyer entry = xpmap_ptom_masked(page - KERNBASE);
1092 1.4 bouyer entry |= PG_k | PG_V;
1093 1.2 bouyer
1094 1.64 maxv HYPERVISOR_update_va_mapping(page, entry, UVMF_INVLPG);
1095 1.2 bouyer }
1096 1.4 bouyer
1097 1.4 bouyer #ifdef __x86_64__
1098 1.4 bouyer void
1099 1.4 bouyer xen_set_user_pgd(paddr_t page)
1100 1.4 bouyer {
1101 1.4 bouyer struct mmuext_op op;
1102 1.4 bouyer int s = splvm();
1103 1.4 bouyer
1104 1.4 bouyer xpq_flush_queue();
1105 1.4 bouyer op.cmd = MMUEXT_NEW_USER_BASEPTR;
1106 1.46 jym op.arg1.mfn = xpmap_ptom_masked(page) >> PAGE_SHIFT;
1107 1.64 maxv if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF) < 0)
1108 1.4 bouyer panic("xen_set_user_pgd: failed to install new user page"
1109 1.19 jym " directory %#" PRIxPADDR, page);
1110 1.4 bouyer splx(s);
1111 1.4 bouyer }
1112 1.4 bouyer #endif /* __x86_64__ */
1113