1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Xen mmu operations
5 *
6 * This file contains the various mmu fetch and update operations.
7 * The most important job they must perform is the mapping between the
8 * domain's pfn and the overall machine mfns.
9 *
10 * Xen allows guests to directly update the pagetable, in a controlled
11 * fashion. In other words, the guest modifies the same pagetable
12 * that the CPU actually uses, which eliminates the overhead of having
13 * a separate shadow pagetable.
14 *
15 * In order to allow this, it falls on the guest domain to map its
16 * notion of a "physical" pfn - which is just a domain-local linear
17 * address - into a real "machine address" which the CPU's MMU can
18 * use.
19 *
20 * A pgd_t/pmd_t/pte_t will typically contain an mfn, and so can be
21 * inserted directly into the pagetable. When creating a new
22 * pte/pmd/pgd, it converts the passed pfn into an mfn. Conversely,
23 * when reading the content back with __(pgd|pmd|pte)_val, it converts
24 * the mfn back into a pfn.
25 *
26 * The other constraint is that all pages which make up a pagetable
27 * must be mapped read-only in the guest. This prevents uncontrolled
28 * guest updates to the pagetable. Xen strictly enforces this, and
29 * will disallow any pagetable update which will end up mapping a
30 * pagetable page RW, and will disallow using any writable page as a
31 * pagetable.
32 *
33 * Naively, when loading %cr3 with the base of a new pagetable, Xen
34 * would need to validate the whole pagetable before going on.
35 * Naturally, this is quite slow. The solution is to "pin" a
36 * pagetable, which enforces all the constraints on the pagetable even
37 * when it is not actively in use. This menas that Xen can be assured
38 * that it is still valid when you do load it into %cr3, and doesn't
39 * need to revalidate it.
40 *
41 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
42 */
43 #include <linux/sched/mm.h>
44 #include <linux/debugfs.h>
45 #include <linux/bug.h>
46 #include <linux/vmalloc.h>
47 #include <linux/export.h>
48 #include <linux/init.h>
49 #include <linux/gfp.h>
50 #include <linux/memblock.h>
51 #include <linux/seq_file.h>
52 #include <linux/crash_dump.h>
53 #include <linux/pgtable.h>
54 #ifdef CONFIG_KEXEC_CORE
55 #include <linux/kexec.h>
56 #endif
57
58 #include <trace/events/xen.h>
59
60 #include <asm/tlbflush.h>
61 #include <asm/fixmap.h>
62 #include <asm/mmu_context.h>
63 #include <asm/setup.h>
64 #include <asm/paravirt.h>
65 #include <asm/e820/api.h>
66 #include <asm/linkage.h>
67 #include <asm/page.h>
68 #include <asm/init.h>
69 #include <asm/memtype.h>
70 #include <asm/smp.h>
71 #include <asm/tlb.h>
72
73 #include <asm/xen/hypercall.h>
74 #include <asm/xen/hypervisor.h>
75
76 #include <xen/xen.h>
77 #include <xen/page.h>
78 #include <xen/interface/xen.h>
79 #include <xen/interface/hvm/hvm_op.h>
80 #include <xen/interface/version.h>
81 #include <xen/interface/memory.h>
82 #include <xen/hvc-console.h>
83 #include <xen/swiotlb-xen.h>
84
85 #include "multicalls.h"
86 #include "mmu.h"
87 #include "debugfs.h"
88
89 /*
90 * Prototypes for functions called via PV_CALLEE_SAVE_REGS_THUNK() in order
91 * to avoid warnings with "-Wmissing-prototypes".
92 */
93 pteval_t xen_pte_val(pte_t pte);
94 pgdval_t xen_pgd_val(pgd_t pgd);
95 pmdval_t xen_pmd_val(pmd_t pmd);
96 pudval_t xen_pud_val(pud_t pud);
97 p4dval_t xen_p4d_val(p4d_t p4d);
98 pte_t xen_make_pte(pteval_t pte);
99 pgd_t xen_make_pgd(pgdval_t pgd);
100 pmd_t xen_make_pmd(pmdval_t pmd);
101 pud_t xen_make_pud(pudval_t pud);
102 p4d_t xen_make_p4d(p4dval_t p4d);
103 pte_t xen_make_pte_init(pteval_t pte);
104
105 #ifdef CONFIG_X86_VSYSCALL_EMULATION
106 /* l3 pud for userspace vsyscall mapping */
107 static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
108 #endif
109
110 /*
111 * Protects atomic reservation decrease/increase against concurrent increases.
112 * Also protects non-atomic updates of current_pages and balloon lists.
113 */
114 static DEFINE_SPINLOCK(xen_reservation_lock);
115
116 /* Protected by xen_reservation_lock. */
117 #define MIN_CONTIG_ORDER 9 /* 2MB */
118 static unsigned int discontig_frames_order = MIN_CONTIG_ORDER;
119 static unsigned long discontig_frames_early[1UL << MIN_CONTIG_ORDER] __initdata;
120 static unsigned long *discontig_frames __refdata = discontig_frames_early;
121 static bool discontig_frames_dyn;
122
alloc_discontig_frames(unsigned int order)123 static int alloc_discontig_frames(unsigned int order)
124 {
125 unsigned long *new_array, *old_array;
126 unsigned int old_order;
127 unsigned long flags;
128
129 BUG_ON(order < MIN_CONTIG_ORDER);
130 BUILD_BUG_ON(sizeof(discontig_frames_early) != PAGE_SIZE);
131
132 new_array = (unsigned long *)__get_free_pages(GFP_KERNEL,
133 order - MIN_CONTIG_ORDER);
134 if (!new_array)
135 return -ENOMEM;
136
137 spin_lock_irqsave(&xen_reservation_lock, flags);
138
139 old_order = discontig_frames_order;
140
141 if (order > discontig_frames_order || !discontig_frames_dyn) {
142 if (!discontig_frames_dyn)
143 old_array = NULL;
144 else
145 old_array = discontig_frames;
146
147 discontig_frames = new_array;
148 discontig_frames_order = order;
149 discontig_frames_dyn = true;
150 } else {
151 old_array = new_array;
152 }
153
154 spin_unlock_irqrestore(&xen_reservation_lock, flags);
155
156 free_pages((unsigned long)old_array, old_order - MIN_CONTIG_ORDER);
157
158 return 0;
159 }
160
161 /*
162 * Note about cr3 (pagetable base) values:
163 *
164 * xen_cr3 contains the current logical cr3 value; it contains the
165 * last set cr3. This may not be the current effective cr3, because
166 * its update may be being lazily deferred. However, a vcpu looking
167 * at its own cr3 can use this value knowing that it everything will
168 * be self-consistent.
169 *
170 * xen_current_cr3 contains the actual vcpu cr3; it is set once the
171 * hypercall to set the vcpu cr3 is complete (so it may be a little
172 * out of date, but it will never be set early). If one vcpu is
173 * looking at another vcpu's cr3 value, it should use this variable.
174 */
175 DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
176 DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
177
178 static phys_addr_t xen_pt_base, xen_pt_size __initdata;
179
180 static DEFINE_STATIC_KEY_FALSE(xen_struct_pages_ready);
181
182 /*
183 * Just beyond the highest usermode address. STACK_TOP_MAX has a
184 * redzone above it, so round it up to a PGD boundary.
185 */
186 #define USER_LIMIT ((STACK_TOP_MAX + PGDIR_SIZE - 1) & PGDIR_MASK)
187
make_lowmem_page_readonly(void * vaddr)188 void make_lowmem_page_readonly(void *vaddr)
189 {
190 pte_t *pte, ptev;
191 unsigned long address = (unsigned long)vaddr;
192 unsigned int level;
193
194 pte = lookup_address(address, &level);
195 if (pte == NULL)
196 return; /* vaddr missing */
197
198 ptev = pte_wrprotect(*pte);
199
200 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
201 BUG();
202 }
203
make_lowmem_page_readwrite(void * vaddr)204 void make_lowmem_page_readwrite(void *vaddr)
205 {
206 pte_t *pte, ptev;
207 unsigned long address = (unsigned long)vaddr;
208 unsigned int level;
209
210 pte = lookup_address(address, &level);
211 if (pte == NULL)
212 return; /* vaddr missing */
213
214 ptev = pte_mkwrite_novma(*pte);
215
216 if (HYPERVISOR_update_va_mapping(address, ptev, 0))
217 BUG();
218 }
219
220
221 /*
222 * During early boot all page table pages are pinned, but we do not have struct
223 * pages, so return true until struct pages are ready.
224 */
xen_page_pinned(void * ptr)225 static bool xen_page_pinned(void *ptr)
226 {
227 if (static_branch_likely(&xen_struct_pages_ready)) {
228 struct page *page = virt_to_page(ptr);
229
230 return PagePinned(page);
231 }
232 return true;
233 }
234
xen_extend_mmu_update(const struct mmu_update * update)235 static void xen_extend_mmu_update(const struct mmu_update *update)
236 {
237 struct multicall_space mcs;
238 struct mmu_update *u;
239
240 mcs = xen_mc_extend_args(__HYPERVISOR_mmu_update, sizeof(*u));
241
242 if (mcs.mc != NULL) {
243 mcs.mc->args[1]++;
244 } else {
245 mcs = __xen_mc_entry(sizeof(*u));
246 MULTI_mmu_update(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
247 }
248
249 u = mcs.args;
250 *u = *update;
251 }
252
xen_extend_mmuext_op(const struct mmuext_op * op)253 static void xen_extend_mmuext_op(const struct mmuext_op *op)
254 {
255 struct multicall_space mcs;
256 struct mmuext_op *u;
257
258 mcs = xen_mc_extend_args(__HYPERVISOR_mmuext_op, sizeof(*u));
259
260 if (mcs.mc != NULL) {
261 mcs.mc->args[1]++;
262 } else {
263 mcs = __xen_mc_entry(sizeof(*u));
264 MULTI_mmuext_op(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
265 }
266
267 u = mcs.args;
268 *u = *op;
269 }
270
xen_set_pmd_hyper(pmd_t * ptr,pmd_t val)271 static void xen_set_pmd_hyper(pmd_t *ptr, pmd_t val)
272 {
273 struct mmu_update u;
274
275 preempt_disable();
276
277 xen_mc_batch();
278
279 /* ptr may be ioremapped for 64-bit pagetable setup */
280 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
281 u.val = pmd_val_ma(val);
282 xen_extend_mmu_update(&u);
283
284 xen_mc_issue(XEN_LAZY_MMU);
285
286 preempt_enable();
287 }
288
xen_set_pmd(pmd_t * ptr,pmd_t val)289 static void xen_set_pmd(pmd_t *ptr, pmd_t val)
290 {
291 trace_xen_mmu_set_pmd(ptr, val);
292
293 /* If page is not pinned, we can just update the entry
294 directly */
295 if (!xen_page_pinned(ptr)) {
296 *ptr = val;
297 return;
298 }
299
300 xen_set_pmd_hyper(ptr, val);
301 }
302
303 /*
304 * Associate a virtual page frame with a given physical page frame
305 * and protection flags for that frame.
306 */
set_pte_mfn(unsigned long vaddr,unsigned long mfn,pgprot_t flags)307 void __init set_pte_mfn(unsigned long vaddr, unsigned long mfn, pgprot_t flags)
308 {
309 if (HYPERVISOR_update_va_mapping(vaddr, mfn_pte(mfn, flags),
310 UVMF_INVLPG))
311 BUG();
312 }
313
xen_batched_set_pte(pte_t * ptep,pte_t pteval)314 static bool xen_batched_set_pte(pte_t *ptep, pte_t pteval)
315 {
316 struct mmu_update u;
317
318 if (xen_get_lazy_mode() != XEN_LAZY_MMU)
319 return false;
320
321 xen_mc_batch();
322
323 u.ptr = virt_to_machine(ptep).maddr | MMU_NORMAL_PT_UPDATE;
324 u.val = pte_val_ma(pteval);
325 xen_extend_mmu_update(&u);
326
327 xen_mc_issue(XEN_LAZY_MMU);
328
329 return true;
330 }
331
__xen_set_pte(pte_t * ptep,pte_t pteval)332 static inline void __xen_set_pte(pte_t *ptep, pte_t pteval)
333 {
334 if (!xen_batched_set_pte(ptep, pteval)) {
335 /*
336 * Could call native_set_pte() here and trap and
337 * emulate the PTE write, but a hypercall is much cheaper.
338 */
339 struct mmu_update u;
340
341 u.ptr = virt_to_machine(ptep).maddr | MMU_NORMAL_PT_UPDATE;
342 u.val = pte_val_ma(pteval);
343 HYPERVISOR_mmu_update(&u, 1, NULL, DOMID_SELF);
344 }
345 }
346
xen_set_pte(pte_t * ptep,pte_t pteval)347 static void xen_set_pte(pte_t *ptep, pte_t pteval)
348 {
349 trace_xen_mmu_set_pte(ptep, pteval);
350 __xen_set_pte(ptep, pteval);
351 }
352
xen_ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)353 pte_t xen_ptep_modify_prot_start(struct vm_area_struct *vma,
354 unsigned long addr, pte_t *ptep)
355 {
356 /* Just return the pte as-is. We preserve the bits on commit */
357 trace_xen_mmu_ptep_modify_prot_start(vma->vm_mm, addr, ptep, *ptep);
358 return *ptep;
359 }
360
xen_ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t pte)361 void xen_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
362 pte_t *ptep, pte_t pte)
363 {
364 struct mmu_update u;
365
366 trace_xen_mmu_ptep_modify_prot_commit(vma->vm_mm, addr, ptep, pte);
367 xen_mc_batch();
368
369 u.ptr = virt_to_machine(ptep).maddr | MMU_PT_UPDATE_PRESERVE_AD;
370 u.val = pte_val_ma(pte);
371 xen_extend_mmu_update(&u);
372
373 xen_mc_issue(XEN_LAZY_MMU);
374 }
375
376 /* Assume pteval_t is equivalent to all the other *val_t types. */
pte_mfn_to_pfn(pteval_t val)377 static pteval_t pte_mfn_to_pfn(pteval_t val)
378 {
379 if (val & _PAGE_PRESENT) {
380 unsigned long mfn = (val & XEN_PTE_MFN_MASK) >> PAGE_SHIFT;
381 unsigned long pfn = mfn_to_pfn(mfn);
382
383 pteval_t flags = val & PTE_FLAGS_MASK;
384 if (unlikely(pfn == ~0))
385 val = flags & ~_PAGE_PRESENT;
386 else
387 val = ((pteval_t)pfn << PAGE_SHIFT) | flags;
388 }
389
390 return val;
391 }
392
pte_pfn_to_mfn(pteval_t val)393 static pteval_t pte_pfn_to_mfn(pteval_t val)
394 {
395 if (val & _PAGE_PRESENT) {
396 unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
397 pteval_t flags = val & PTE_FLAGS_MASK;
398 unsigned long mfn;
399
400 mfn = __pfn_to_mfn(pfn);
401
402 /*
403 * If there's no mfn for the pfn, then just create an
404 * empty non-present pte. Unfortunately this loses
405 * information about the original pfn, so
406 * pte_mfn_to_pfn is asymmetric.
407 */
408 if (unlikely(mfn == INVALID_P2M_ENTRY)) {
409 mfn = 0;
410 flags = 0;
411 } else
412 mfn &= ~(FOREIGN_FRAME_BIT | IDENTITY_FRAME_BIT);
413 val = ((pteval_t)mfn << PAGE_SHIFT) | flags;
414 }
415
416 return val;
417 }
418
xen_pte_val(pte_t pte)419 __visible pteval_t xen_pte_val(pte_t pte)
420 {
421 pteval_t pteval = pte.pte;
422
423 return pte_mfn_to_pfn(pteval);
424 }
425 PV_CALLEE_SAVE_REGS_THUNK(xen_pte_val);
426
xen_pgd_val(pgd_t pgd)427 __visible pgdval_t xen_pgd_val(pgd_t pgd)
428 {
429 return pte_mfn_to_pfn(pgd.pgd);
430 }
431 PV_CALLEE_SAVE_REGS_THUNK(xen_pgd_val);
432
xen_make_pte(pteval_t pte)433 __visible pte_t xen_make_pte(pteval_t pte)
434 {
435 pte = pte_pfn_to_mfn(pte);
436
437 return native_make_pte(pte);
438 }
439 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
440
xen_make_pgd(pgdval_t pgd)441 __visible pgd_t xen_make_pgd(pgdval_t pgd)
442 {
443 pgd = pte_pfn_to_mfn(pgd);
444 return native_make_pgd(pgd);
445 }
446 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pgd);
447
xen_pmd_val(pmd_t pmd)448 __visible pmdval_t xen_pmd_val(pmd_t pmd)
449 {
450 return pte_mfn_to_pfn(pmd.pmd);
451 }
452 PV_CALLEE_SAVE_REGS_THUNK(xen_pmd_val);
453
xen_set_pud_hyper(pud_t * ptr,pud_t val)454 static void xen_set_pud_hyper(pud_t *ptr, pud_t val)
455 {
456 struct mmu_update u;
457
458 preempt_disable();
459
460 xen_mc_batch();
461
462 /* ptr may be ioremapped for 64-bit pagetable setup */
463 u.ptr = arbitrary_virt_to_machine(ptr).maddr;
464 u.val = pud_val_ma(val);
465 xen_extend_mmu_update(&u);
466
467 xen_mc_issue(XEN_LAZY_MMU);
468
469 preempt_enable();
470 }
471
xen_set_pud(pud_t * ptr,pud_t val)472 static void xen_set_pud(pud_t *ptr, pud_t val)
473 {
474 trace_xen_mmu_set_pud(ptr, val);
475
476 /* If page is not pinned, we can just update the entry
477 directly */
478 if (!xen_page_pinned(ptr)) {
479 *ptr = val;
480 return;
481 }
482
483 xen_set_pud_hyper(ptr, val);
484 }
485
xen_make_pmd(pmdval_t pmd)486 __visible pmd_t xen_make_pmd(pmdval_t pmd)
487 {
488 pmd = pte_pfn_to_mfn(pmd);
489 return native_make_pmd(pmd);
490 }
491 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pmd);
492
xen_pud_val(pud_t pud)493 __visible pudval_t xen_pud_val(pud_t pud)
494 {
495 return pte_mfn_to_pfn(pud.pud);
496 }
497 PV_CALLEE_SAVE_REGS_THUNK(xen_pud_val);
498
xen_make_pud(pudval_t pud)499 __visible pud_t xen_make_pud(pudval_t pud)
500 {
501 pud = pte_pfn_to_mfn(pud);
502
503 return native_make_pud(pud);
504 }
505 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pud);
506
xen_get_user_pgd(pgd_t * pgd)507 static pgd_t *xen_get_user_pgd(pgd_t *pgd)
508 {
509 pgd_t *pgd_page = (pgd_t *)(((unsigned long)pgd) & PAGE_MASK);
510 unsigned offset = pgd - pgd_page;
511 pgd_t *user_ptr = NULL;
512
513 if (offset < pgd_index(USER_LIMIT)) {
514 struct page *page = virt_to_page(pgd_page);
515 user_ptr = (pgd_t *)page->private;
516 if (user_ptr)
517 user_ptr += offset;
518 }
519
520 return user_ptr;
521 }
522
__xen_set_p4d_hyper(p4d_t * ptr,p4d_t val)523 static void __xen_set_p4d_hyper(p4d_t *ptr, p4d_t val)
524 {
525 struct mmu_update u;
526
527 u.ptr = virt_to_machine(ptr).maddr;
528 u.val = p4d_val_ma(val);
529 xen_extend_mmu_update(&u);
530 }
531
532 /*
533 * Raw hypercall-based set_p4d, intended for in early boot before
534 * there's a page structure. This implies:
535 * 1. The only existing pagetable is the kernel's
536 * 2. It is always pinned
537 * 3. It has no user pagetable attached to it
538 */
xen_set_p4d_hyper(p4d_t * ptr,p4d_t val)539 static void __init xen_set_p4d_hyper(p4d_t *ptr, p4d_t val)
540 {
541 preempt_disable();
542
543 xen_mc_batch();
544
545 __xen_set_p4d_hyper(ptr, val);
546
547 xen_mc_issue(XEN_LAZY_MMU);
548
549 preempt_enable();
550 }
551
xen_set_p4d(p4d_t * ptr,p4d_t val)552 static void xen_set_p4d(p4d_t *ptr, p4d_t val)
553 {
554 pgd_t *user_ptr = xen_get_user_pgd((pgd_t *)ptr);
555 pgd_t pgd_val;
556
557 trace_xen_mmu_set_p4d(ptr, (p4d_t *)user_ptr, val);
558
559 /* If page is not pinned, we can just update the entry
560 directly */
561 if (!xen_page_pinned(ptr)) {
562 *ptr = val;
563 if (user_ptr) {
564 WARN_ON(xen_page_pinned(user_ptr));
565 pgd_val.pgd = p4d_val_ma(val);
566 *user_ptr = pgd_val;
567 }
568 return;
569 }
570
571 /* If it's pinned, then we can at least batch the kernel and
572 user updates together. */
573 xen_mc_batch();
574
575 __xen_set_p4d_hyper(ptr, val);
576 if (user_ptr)
577 __xen_set_p4d_hyper((p4d_t *)user_ptr, val);
578
579 xen_mc_issue(XEN_LAZY_MMU);
580 }
581
582 #if CONFIG_PGTABLE_LEVELS >= 5
xen_p4d_val(p4d_t p4d)583 __visible p4dval_t xen_p4d_val(p4d_t p4d)
584 {
585 return pte_mfn_to_pfn(p4d.p4d);
586 }
587 PV_CALLEE_SAVE_REGS_THUNK(xen_p4d_val);
588
xen_make_p4d(p4dval_t p4d)589 __visible p4d_t xen_make_p4d(p4dval_t p4d)
590 {
591 p4d = pte_pfn_to_mfn(p4d);
592
593 return native_make_p4d(p4d);
594 }
595 PV_CALLEE_SAVE_REGS_THUNK(xen_make_p4d);
596 #endif /* CONFIG_PGTABLE_LEVELS >= 5 */
597
xen_pmd_walk(struct mm_struct * mm,pmd_t * pmd,void (* func)(struct mm_struct * mm,struct page *,enum pt_level),bool last,unsigned long limit)598 static void xen_pmd_walk(struct mm_struct *mm, pmd_t *pmd,
599 void (*func)(struct mm_struct *mm, struct page *,
600 enum pt_level),
601 bool last, unsigned long limit)
602 {
603 int i, nr;
604
605 nr = last ? pmd_index(limit) + 1 : PTRS_PER_PMD;
606 for (i = 0; i < nr; i++) {
607 if (!pmd_none(pmd[i]))
608 (*func)(mm, pmd_page(pmd[i]), PT_PTE);
609 }
610 }
611
xen_pud_walk(struct mm_struct * mm,pud_t * pud,void (* func)(struct mm_struct * mm,struct page *,enum pt_level),bool last,unsigned long limit)612 static void xen_pud_walk(struct mm_struct *mm, pud_t *pud,
613 void (*func)(struct mm_struct *mm, struct page *,
614 enum pt_level),
615 bool last, unsigned long limit)
616 {
617 int i, nr;
618
619 nr = last ? pud_index(limit) + 1 : PTRS_PER_PUD;
620 for (i = 0; i < nr; i++) {
621 pmd_t *pmd;
622
623 if (pud_none(pud[i]))
624 continue;
625
626 pmd = pmd_offset(&pud[i], 0);
627 if (PTRS_PER_PMD > 1)
628 (*func)(mm, virt_to_page(pmd), PT_PMD);
629 xen_pmd_walk(mm, pmd, func, last && i == nr - 1, limit);
630 }
631 }
632
xen_p4d_walk(struct mm_struct * mm,p4d_t * p4d,void (* func)(struct mm_struct * mm,struct page *,enum pt_level),bool last,unsigned long limit)633 static void xen_p4d_walk(struct mm_struct *mm, p4d_t *p4d,
634 void (*func)(struct mm_struct *mm, struct page *,
635 enum pt_level),
636 bool last, unsigned long limit)
637 {
638 pud_t *pud;
639
640
641 if (p4d_none(*p4d))
642 return;
643
644 pud = pud_offset(p4d, 0);
645 if (PTRS_PER_PUD > 1)
646 (*func)(mm, virt_to_page(pud), PT_PUD);
647 xen_pud_walk(mm, pud, func, last, limit);
648 }
649
650 /*
651 * (Yet another) pagetable walker. This one is intended for pinning a
652 * pagetable. This means that it walks a pagetable and calls the
653 * callback function on each page it finds making up the page table,
654 * at every level. It walks the entire pagetable, but it only bothers
655 * pinning pte pages which are below limit. In the normal case this
656 * will be STACK_TOP_MAX, but at boot we need to pin up to
657 * FIXADDR_TOP.
658 *
659 * We must skip the Xen hole in the middle of the address space, just after
660 * the big x86-64 virtual hole.
661 */
__xen_pgd_walk(struct mm_struct * mm,pgd_t * pgd,void (* func)(struct mm_struct * mm,struct page *,enum pt_level),unsigned long limit)662 static void __xen_pgd_walk(struct mm_struct *mm, pgd_t *pgd,
663 void (*func)(struct mm_struct *mm, struct page *,
664 enum pt_level),
665 unsigned long limit)
666 {
667 int i, nr;
668 unsigned hole_low = 0, hole_high = 0;
669
670 /* The limit is the last byte to be touched */
671 limit--;
672 BUG_ON(limit >= FIXADDR_TOP);
673
674 /*
675 * 64-bit has a great big hole in the middle of the address
676 * space, which contains the Xen mappings.
677 */
678 hole_low = pgd_index(GUARD_HOLE_BASE_ADDR);
679 hole_high = pgd_index(GUARD_HOLE_END_ADDR);
680
681 nr = pgd_index(limit) + 1;
682 for (i = 0; i < nr; i++) {
683 p4d_t *p4d;
684
685 if (i >= hole_low && i < hole_high)
686 continue;
687
688 if (pgd_none(pgd[i]))
689 continue;
690
691 p4d = p4d_offset(&pgd[i], 0);
692 xen_p4d_walk(mm, p4d, func, i == nr - 1, limit);
693 }
694
695 /* Do the top level last, so that the callbacks can use it as
696 a cue to do final things like tlb flushes. */
697 (*func)(mm, virt_to_page(pgd), PT_PGD);
698 }
699
xen_pgd_walk(struct mm_struct * mm,void (* func)(struct mm_struct * mm,struct page *,enum pt_level),unsigned long limit)700 static void xen_pgd_walk(struct mm_struct *mm,
701 void (*func)(struct mm_struct *mm, struct page *,
702 enum pt_level),
703 unsigned long limit)
704 {
705 __xen_pgd_walk(mm, mm->pgd, func, limit);
706 }
707
708 /* If we're using split pte locks, then take the page's lock and
709 return a pointer to it. Otherwise return NULL. */
xen_pte_lock(struct page * page,struct mm_struct * mm)710 static spinlock_t *xen_pte_lock(struct page *page, struct mm_struct *mm)
711 {
712 spinlock_t *ptl = NULL;
713
714 #if USE_SPLIT_PTE_PTLOCKS
715 ptl = ptlock_ptr(page_ptdesc(page));
716 spin_lock_nest_lock(ptl, &mm->page_table_lock);
717 #endif
718
719 return ptl;
720 }
721
xen_pte_unlock(void * v)722 static void xen_pte_unlock(void *v)
723 {
724 spinlock_t *ptl = v;
725 spin_unlock(ptl);
726 }
727
xen_do_pin(unsigned level,unsigned long pfn)728 static void xen_do_pin(unsigned level, unsigned long pfn)
729 {
730 struct mmuext_op op;
731
732 op.cmd = level;
733 op.arg1.mfn = pfn_to_mfn(pfn);
734
735 xen_extend_mmuext_op(&op);
736 }
737
xen_pin_page(struct mm_struct * mm,struct page * page,enum pt_level level)738 static void xen_pin_page(struct mm_struct *mm, struct page *page,
739 enum pt_level level)
740 {
741 unsigned pgfl = TestSetPagePinned(page);
742
743 if (!pgfl) {
744 void *pt = lowmem_page_address(page);
745 unsigned long pfn = page_to_pfn(page);
746 struct multicall_space mcs = __xen_mc_entry(0);
747 spinlock_t *ptl;
748
749 /*
750 * We need to hold the pagetable lock between the time
751 * we make the pagetable RO and when we actually pin
752 * it. If we don't, then other users may come in and
753 * attempt to update the pagetable by writing it,
754 * which will fail because the memory is RO but not
755 * pinned, so Xen won't do the trap'n'emulate.
756 *
757 * If we're using split pte locks, we can't hold the
758 * entire pagetable's worth of locks during the
759 * traverse, because we may wrap the preempt count (8
760 * bits). The solution is to mark RO and pin each PTE
761 * page while holding the lock. This means the number
762 * of locks we end up holding is never more than a
763 * batch size (~32 entries, at present).
764 *
765 * If we're not using split pte locks, we needn't pin
766 * the PTE pages independently, because we're
767 * protected by the overall pagetable lock.
768 */
769 ptl = NULL;
770 if (level == PT_PTE)
771 ptl = xen_pte_lock(page, mm);
772
773 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
774 pfn_pte(pfn, PAGE_KERNEL_RO),
775 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
776
777 if (ptl) {
778 xen_do_pin(MMUEXT_PIN_L1_TABLE, pfn);
779
780 /* Queue a deferred unlock for when this batch
781 is completed. */
782 xen_mc_callback(xen_pte_unlock, ptl);
783 }
784 }
785 }
786
787 /* This is called just after a mm has been created, but it has not
788 been used yet. We need to make sure that its pagetable is all
789 read-only, and can be pinned. */
__xen_pgd_pin(struct mm_struct * mm,pgd_t * pgd)790 static void __xen_pgd_pin(struct mm_struct *mm, pgd_t *pgd)
791 {
792 pgd_t *user_pgd = xen_get_user_pgd(pgd);
793
794 trace_xen_mmu_pgd_pin(mm, pgd);
795
796 xen_mc_batch();
797
798 __xen_pgd_walk(mm, pgd, xen_pin_page, USER_LIMIT);
799
800 xen_do_pin(MMUEXT_PIN_L4_TABLE, PFN_DOWN(__pa(pgd)));
801
802 if (user_pgd) {
803 xen_pin_page(mm, virt_to_page(user_pgd), PT_PGD);
804 xen_do_pin(MMUEXT_PIN_L4_TABLE,
805 PFN_DOWN(__pa(user_pgd)));
806 }
807
808 xen_mc_issue(0);
809 }
810
xen_pgd_pin(struct mm_struct * mm)811 static void xen_pgd_pin(struct mm_struct *mm)
812 {
813 __xen_pgd_pin(mm, mm->pgd);
814 }
815
816 /*
817 * On save, we need to pin all pagetables to make sure they get their
818 * mfns turned into pfns. Search the list for any unpinned pgds and pin
819 * them (unpinned pgds are not currently in use, probably because the
820 * process is under construction or destruction).
821 *
822 * Expected to be called in stop_machine() ("equivalent to taking
823 * every spinlock in the system"), so the locking doesn't really
824 * matter all that much.
825 */
xen_mm_pin_all(void)826 void xen_mm_pin_all(void)
827 {
828 struct page *page;
829
830 spin_lock(&init_mm.page_table_lock);
831 spin_lock(&pgd_lock);
832
833 list_for_each_entry(page, &pgd_list, lru) {
834 if (!PagePinned(page)) {
835 __xen_pgd_pin(&init_mm, (pgd_t *)page_address(page));
836 SetPageSavePinned(page);
837 }
838 }
839
840 spin_unlock(&pgd_lock);
841 spin_unlock(&init_mm.page_table_lock);
842 }
843
xen_mark_pinned(struct mm_struct * mm,struct page * page,enum pt_level level)844 static void __init xen_mark_pinned(struct mm_struct *mm, struct page *page,
845 enum pt_level level)
846 {
847 SetPagePinned(page);
848 }
849
850 /*
851 * The init_mm pagetable is really pinned as soon as its created, but
852 * that's before we have page structures to store the bits. So do all
853 * the book-keeping now once struct pages for allocated pages are
854 * initialized. This happens only after memblock_free_all() is called.
855 */
xen_after_bootmem(void)856 static void __init xen_after_bootmem(void)
857 {
858 static_branch_enable(&xen_struct_pages_ready);
859 #ifdef CONFIG_X86_VSYSCALL_EMULATION
860 SetPagePinned(virt_to_page(level3_user_vsyscall));
861 #endif
862 xen_pgd_walk(&init_mm, xen_mark_pinned, FIXADDR_TOP);
863
864 if (alloc_discontig_frames(MIN_CONTIG_ORDER))
865 BUG();
866 }
867
xen_unpin_page(struct mm_struct * mm,struct page * page,enum pt_level level)868 static void xen_unpin_page(struct mm_struct *mm, struct page *page,
869 enum pt_level level)
870 {
871 unsigned pgfl = TestClearPagePinned(page);
872
873 if (pgfl) {
874 void *pt = lowmem_page_address(page);
875 unsigned long pfn = page_to_pfn(page);
876 spinlock_t *ptl = NULL;
877 struct multicall_space mcs;
878
879 /*
880 * Do the converse to pin_page. If we're using split
881 * pte locks, we must be holding the lock for while
882 * the pte page is unpinned but still RO to prevent
883 * concurrent updates from seeing it in this
884 * partially-pinned state.
885 */
886 if (level == PT_PTE) {
887 ptl = xen_pte_lock(page, mm);
888
889 if (ptl)
890 xen_do_pin(MMUEXT_UNPIN_TABLE, pfn);
891 }
892
893 mcs = __xen_mc_entry(0);
894
895 MULTI_update_va_mapping(mcs.mc, (unsigned long)pt,
896 pfn_pte(pfn, PAGE_KERNEL),
897 level == PT_PGD ? UVMF_TLB_FLUSH : 0);
898
899 if (ptl) {
900 /* unlock when batch completed */
901 xen_mc_callback(xen_pte_unlock, ptl);
902 }
903 }
904 }
905
906 /* Release a pagetables pages back as normal RW */
__xen_pgd_unpin(struct mm_struct * mm,pgd_t * pgd)907 static void __xen_pgd_unpin(struct mm_struct *mm, pgd_t *pgd)
908 {
909 pgd_t *user_pgd = xen_get_user_pgd(pgd);
910
911 trace_xen_mmu_pgd_unpin(mm, pgd);
912
913 xen_mc_batch();
914
915 xen_do_pin(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
916
917 if (user_pgd) {
918 xen_do_pin(MMUEXT_UNPIN_TABLE,
919 PFN_DOWN(__pa(user_pgd)));
920 xen_unpin_page(mm, virt_to_page(user_pgd), PT_PGD);
921 }
922
923 __xen_pgd_walk(mm, pgd, xen_unpin_page, USER_LIMIT);
924
925 xen_mc_issue(0);
926 }
927
xen_pgd_unpin(struct mm_struct * mm)928 static void xen_pgd_unpin(struct mm_struct *mm)
929 {
930 __xen_pgd_unpin(mm, mm->pgd);
931 }
932
933 /*
934 * On resume, undo any pinning done at save, so that the rest of the
935 * kernel doesn't see any unexpected pinned pagetables.
936 */
xen_mm_unpin_all(void)937 void xen_mm_unpin_all(void)
938 {
939 struct page *page;
940
941 spin_lock(&init_mm.page_table_lock);
942 spin_lock(&pgd_lock);
943
944 list_for_each_entry(page, &pgd_list, lru) {
945 if (PageSavePinned(page)) {
946 BUG_ON(!PagePinned(page));
947 __xen_pgd_unpin(&init_mm, (pgd_t *)page_address(page));
948 ClearPageSavePinned(page);
949 }
950 }
951
952 spin_unlock(&pgd_lock);
953 spin_unlock(&init_mm.page_table_lock);
954 }
955
xen_enter_mmap(struct mm_struct * mm)956 static void xen_enter_mmap(struct mm_struct *mm)
957 {
958 spin_lock(&mm->page_table_lock);
959 xen_pgd_pin(mm);
960 spin_unlock(&mm->page_table_lock);
961 }
962
drop_mm_ref_this_cpu(void * info)963 static void drop_mm_ref_this_cpu(void *info)
964 {
965 struct mm_struct *mm = info;
966
967 if (this_cpu_read(cpu_tlbstate.loaded_mm) == mm)
968 leave_mm(smp_processor_id());
969
970 /*
971 * If this cpu still has a stale cr3 reference, then make sure
972 * it has been flushed.
973 */
974 if (this_cpu_read(xen_current_cr3) == __pa(mm->pgd))
975 xen_mc_flush();
976 }
977
978 #ifdef CONFIG_SMP
979 /*
980 * Another cpu may still have their %cr3 pointing at the pagetable, so
981 * we need to repoint it somewhere else before we can unpin it.
982 */
xen_drop_mm_ref(struct mm_struct * mm)983 static void xen_drop_mm_ref(struct mm_struct *mm)
984 {
985 cpumask_var_t mask;
986 unsigned cpu;
987
988 drop_mm_ref_this_cpu(mm);
989
990 /* Get the "official" set of cpus referring to our pagetable. */
991 if (!alloc_cpumask_var(&mask, GFP_ATOMIC)) {
992 for_each_online_cpu(cpu) {
993 if (per_cpu(xen_current_cr3, cpu) != __pa(mm->pgd))
994 continue;
995 smp_call_function_single(cpu, drop_mm_ref_this_cpu, mm, 1);
996 }
997 return;
998 }
999
1000 /*
1001 * It's possible that a vcpu may have a stale reference to our
1002 * cr3, because its in lazy mode, and it hasn't yet flushed
1003 * its set of pending hypercalls yet. In this case, we can
1004 * look at its actual current cr3 value, and force it to flush
1005 * if needed.
1006 */
1007 cpumask_clear(mask);
1008 for_each_online_cpu(cpu) {
1009 if (per_cpu(xen_current_cr3, cpu) == __pa(mm->pgd))
1010 cpumask_set_cpu(cpu, mask);
1011 }
1012
1013 smp_call_function_many(mask, drop_mm_ref_this_cpu, mm, 1);
1014 free_cpumask_var(mask);
1015 }
1016 #else
xen_drop_mm_ref(struct mm_struct * mm)1017 static void xen_drop_mm_ref(struct mm_struct *mm)
1018 {
1019 drop_mm_ref_this_cpu(mm);
1020 }
1021 #endif
1022
1023 /*
1024 * While a process runs, Xen pins its pagetables, which means that the
1025 * hypervisor forces it to be read-only, and it controls all updates
1026 * to it. This means that all pagetable updates have to go via the
1027 * hypervisor, which is moderately expensive.
1028 *
1029 * Since we're pulling the pagetable down, we switch to use init_mm,
1030 * unpin old process pagetable and mark it all read-write, which
1031 * allows further operations on it to be simple memory accesses.
1032 *
1033 * The only subtle point is that another CPU may be still using the
1034 * pagetable because of lazy tlb flushing. This means we need need to
1035 * switch all CPUs off this pagetable before we can unpin it.
1036 */
xen_exit_mmap(struct mm_struct * mm)1037 static void xen_exit_mmap(struct mm_struct *mm)
1038 {
1039 get_cpu(); /* make sure we don't move around */
1040 xen_drop_mm_ref(mm);
1041 put_cpu();
1042
1043 spin_lock(&mm->page_table_lock);
1044
1045 /* pgd may not be pinned in the error exit path of execve */
1046 if (xen_page_pinned(mm->pgd))
1047 xen_pgd_unpin(mm);
1048
1049 spin_unlock(&mm->page_table_lock);
1050 }
1051
1052 static void xen_post_allocator_init(void);
1053
pin_pagetable_pfn(unsigned cmd,unsigned long pfn)1054 static void __init pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
1055 {
1056 struct mmuext_op op;
1057
1058 op.cmd = cmd;
1059 op.arg1.mfn = pfn_to_mfn(pfn);
1060 if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
1061 BUG();
1062 }
1063
xen_cleanhighmap(unsigned long vaddr,unsigned long vaddr_end)1064 static void __init xen_cleanhighmap(unsigned long vaddr,
1065 unsigned long vaddr_end)
1066 {
1067 unsigned long kernel_end = roundup((unsigned long)_brk_end, PMD_SIZE) - 1;
1068 pmd_t *pmd = level2_kernel_pgt + pmd_index(vaddr);
1069
1070 /* NOTE: The loop is more greedy than the cleanup_highmap variant.
1071 * We include the PMD passed in on _both_ boundaries. */
1072 for (; vaddr <= vaddr_end && (pmd < (level2_kernel_pgt + PTRS_PER_PMD));
1073 pmd++, vaddr += PMD_SIZE) {
1074 if (pmd_none(*pmd))
1075 continue;
1076 if (vaddr < (unsigned long) _text || vaddr > kernel_end)
1077 set_pmd(pmd, __pmd(0));
1078 }
1079 /* In case we did something silly, we should crash in this function
1080 * instead of somewhere later and be confusing. */
1081 xen_mc_flush();
1082 }
1083
1084 /*
1085 * Make a page range writeable and free it.
1086 */
xen_free_ro_pages(unsigned long paddr,unsigned long size)1087 static void __init xen_free_ro_pages(unsigned long paddr, unsigned long size)
1088 {
1089 void *vaddr = __va(paddr);
1090 void *vaddr_end = vaddr + size;
1091
1092 for (; vaddr < vaddr_end; vaddr += PAGE_SIZE)
1093 make_lowmem_page_readwrite(vaddr);
1094
1095 memblock_phys_free(paddr, size);
1096 }
1097
xen_cleanmfnmap_free_pgtbl(void * pgtbl,bool unpin)1098 static void __init xen_cleanmfnmap_free_pgtbl(void *pgtbl, bool unpin)
1099 {
1100 unsigned long pa = __pa(pgtbl) & PHYSICAL_PAGE_MASK;
1101
1102 if (unpin)
1103 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(pa));
1104 ClearPagePinned(virt_to_page(__va(pa)));
1105 xen_free_ro_pages(pa, PAGE_SIZE);
1106 }
1107
xen_cleanmfnmap_pmd(pmd_t * pmd,bool unpin)1108 static void __init xen_cleanmfnmap_pmd(pmd_t *pmd, bool unpin)
1109 {
1110 unsigned long pa;
1111 pte_t *pte_tbl;
1112 int i;
1113
1114 if (pmd_large(*pmd)) {
1115 pa = pmd_val(*pmd) & PHYSICAL_PAGE_MASK;
1116 xen_free_ro_pages(pa, PMD_SIZE);
1117 return;
1118 }
1119
1120 pte_tbl = pte_offset_kernel(pmd, 0);
1121 for (i = 0; i < PTRS_PER_PTE; i++) {
1122 if (pte_none(pte_tbl[i]))
1123 continue;
1124 pa = pte_pfn(pte_tbl[i]) << PAGE_SHIFT;
1125 xen_free_ro_pages(pa, PAGE_SIZE);
1126 }
1127 set_pmd(pmd, __pmd(0));
1128 xen_cleanmfnmap_free_pgtbl(pte_tbl, unpin);
1129 }
1130
xen_cleanmfnmap_pud(pud_t * pud,bool unpin)1131 static void __init xen_cleanmfnmap_pud(pud_t *pud, bool unpin)
1132 {
1133 unsigned long pa;
1134 pmd_t *pmd_tbl;
1135 int i;
1136
1137 if (pud_leaf(*pud)) {
1138 pa = pud_val(*pud) & PHYSICAL_PAGE_MASK;
1139 xen_free_ro_pages(pa, PUD_SIZE);
1140 return;
1141 }
1142
1143 pmd_tbl = pmd_offset(pud, 0);
1144 for (i = 0; i < PTRS_PER_PMD; i++) {
1145 if (pmd_none(pmd_tbl[i]))
1146 continue;
1147 xen_cleanmfnmap_pmd(pmd_tbl + i, unpin);
1148 }
1149 set_pud(pud, __pud(0));
1150 xen_cleanmfnmap_free_pgtbl(pmd_tbl, unpin);
1151 }
1152
xen_cleanmfnmap_p4d(p4d_t * p4d,bool unpin)1153 static void __init xen_cleanmfnmap_p4d(p4d_t *p4d, bool unpin)
1154 {
1155 unsigned long pa;
1156 pud_t *pud_tbl;
1157 int i;
1158
1159 if (p4d_large(*p4d)) {
1160 pa = p4d_val(*p4d) & PHYSICAL_PAGE_MASK;
1161 xen_free_ro_pages(pa, P4D_SIZE);
1162 return;
1163 }
1164
1165 pud_tbl = pud_offset(p4d, 0);
1166 for (i = 0; i < PTRS_PER_PUD; i++) {
1167 if (pud_none(pud_tbl[i]))
1168 continue;
1169 xen_cleanmfnmap_pud(pud_tbl + i, unpin);
1170 }
1171 set_p4d(p4d, __p4d(0));
1172 xen_cleanmfnmap_free_pgtbl(pud_tbl, unpin);
1173 }
1174
1175 /*
1176 * Since it is well isolated we can (and since it is perhaps large we should)
1177 * also free the page tables mapping the initial P->M table.
1178 */
xen_cleanmfnmap(unsigned long vaddr)1179 static void __init xen_cleanmfnmap(unsigned long vaddr)
1180 {
1181 pgd_t *pgd;
1182 p4d_t *p4d;
1183 bool unpin;
1184
1185 unpin = (vaddr == 2 * PGDIR_SIZE);
1186 vaddr &= PMD_MASK;
1187 pgd = pgd_offset_k(vaddr);
1188 p4d = p4d_offset(pgd, 0);
1189 if (!p4d_none(*p4d))
1190 xen_cleanmfnmap_p4d(p4d, unpin);
1191 }
1192
xen_pagetable_p2m_free(void)1193 static void __init xen_pagetable_p2m_free(void)
1194 {
1195 unsigned long size;
1196 unsigned long addr;
1197
1198 size = PAGE_ALIGN(xen_start_info->nr_pages * sizeof(unsigned long));
1199
1200 /* No memory or already called. */
1201 if ((unsigned long)xen_p2m_addr == xen_start_info->mfn_list)
1202 return;
1203
1204 /* using __ka address and sticking INVALID_P2M_ENTRY! */
1205 memset((void *)xen_start_info->mfn_list, 0xff, size);
1206
1207 addr = xen_start_info->mfn_list;
1208 /*
1209 * We could be in __ka space.
1210 * We roundup to the PMD, which means that if anybody at this stage is
1211 * using the __ka address of xen_start_info or
1212 * xen_start_info->shared_info they are in going to crash. Fortunately
1213 * we have already revectored in xen_setup_kernel_pagetable.
1214 */
1215 size = roundup(size, PMD_SIZE);
1216
1217 if (addr >= __START_KERNEL_map) {
1218 xen_cleanhighmap(addr, addr + size);
1219 size = PAGE_ALIGN(xen_start_info->nr_pages *
1220 sizeof(unsigned long));
1221 memblock_free((void *)addr, size);
1222 } else {
1223 xen_cleanmfnmap(addr);
1224 }
1225 }
1226
xen_pagetable_cleanhighmap(void)1227 static void __init xen_pagetable_cleanhighmap(void)
1228 {
1229 unsigned long size;
1230 unsigned long addr;
1231
1232 /* At this stage, cleanup_highmap has already cleaned __ka space
1233 * from _brk_limit way up to the max_pfn_mapped (which is the end of
1234 * the ramdisk). We continue on, erasing PMD entries that point to page
1235 * tables - do note that they are accessible at this stage via __va.
1236 * As Xen is aligning the memory end to a 4MB boundary, for good
1237 * measure we also round up to PMD_SIZE * 2 - which means that if
1238 * anybody is using __ka address to the initial boot-stack - and try
1239 * to use it - they are going to crash. The xen_start_info has been
1240 * taken care of already in xen_setup_kernel_pagetable. */
1241 addr = xen_start_info->pt_base;
1242 size = xen_start_info->nr_pt_frames * PAGE_SIZE;
1243
1244 xen_cleanhighmap(addr, roundup(addr + size, PMD_SIZE * 2));
1245 xen_start_info->pt_base = (unsigned long)__va(__pa(xen_start_info->pt_base));
1246 }
1247
xen_pagetable_p2m_setup(void)1248 static void __init xen_pagetable_p2m_setup(void)
1249 {
1250 xen_vmalloc_p2m_tree();
1251
1252 xen_pagetable_p2m_free();
1253
1254 xen_pagetable_cleanhighmap();
1255
1256 /* And revector! Bye bye old array */
1257 xen_start_info->mfn_list = (unsigned long)xen_p2m_addr;
1258 }
1259
xen_pagetable_init(void)1260 static void __init xen_pagetable_init(void)
1261 {
1262 /*
1263 * The majority of further PTE writes is to pagetables already
1264 * announced as such to Xen. Hence it is more efficient to use
1265 * hypercalls for these updates.
1266 */
1267 pv_ops.mmu.set_pte = __xen_set_pte;
1268
1269 paging_init();
1270 xen_post_allocator_init();
1271
1272 xen_pagetable_p2m_setup();
1273
1274 /* Allocate and initialize top and mid mfn levels for p2m structure */
1275 xen_build_mfn_list_list();
1276
1277 /* Remap memory freed due to conflicts with E820 map */
1278 xen_remap_memory();
1279 xen_setup_mfn_list_list();
1280 }
1281
xen_write_cr2(unsigned long cr2)1282 static noinstr void xen_write_cr2(unsigned long cr2)
1283 {
1284 this_cpu_read(xen_vcpu)->arch.cr2 = cr2;
1285 }
1286
xen_flush_tlb(void)1287 static noinline void xen_flush_tlb(void)
1288 {
1289 struct mmuext_op *op;
1290 struct multicall_space mcs;
1291
1292 preempt_disable();
1293
1294 mcs = xen_mc_entry(sizeof(*op));
1295
1296 op = mcs.args;
1297 op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
1298 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1299
1300 xen_mc_issue(XEN_LAZY_MMU);
1301
1302 preempt_enable();
1303 }
1304
xen_flush_tlb_one_user(unsigned long addr)1305 static void xen_flush_tlb_one_user(unsigned long addr)
1306 {
1307 struct mmuext_op *op;
1308 struct multicall_space mcs;
1309
1310 trace_xen_mmu_flush_tlb_one_user(addr);
1311
1312 preempt_disable();
1313
1314 mcs = xen_mc_entry(sizeof(*op));
1315 op = mcs.args;
1316 op->cmd = MMUEXT_INVLPG_LOCAL;
1317 op->arg1.linear_addr = addr & PAGE_MASK;
1318 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
1319
1320 xen_mc_issue(XEN_LAZY_MMU);
1321
1322 preempt_enable();
1323 }
1324
xen_flush_tlb_multi(const struct cpumask * cpus,const struct flush_tlb_info * info)1325 static void xen_flush_tlb_multi(const struct cpumask *cpus,
1326 const struct flush_tlb_info *info)
1327 {
1328 struct {
1329 struct mmuext_op op;
1330 DECLARE_BITMAP(mask, NR_CPUS);
1331 } *args;
1332 struct multicall_space mcs;
1333 const size_t mc_entry_size = sizeof(args->op) +
1334 sizeof(args->mask[0]) * BITS_TO_LONGS(num_possible_cpus());
1335
1336 trace_xen_mmu_flush_tlb_multi(cpus, info->mm, info->start, info->end);
1337
1338 if (cpumask_empty(cpus))
1339 return; /* nothing to do */
1340
1341 mcs = xen_mc_entry(mc_entry_size);
1342 args = mcs.args;
1343 args->op.arg2.vcpumask = to_cpumask(args->mask);
1344
1345 /* Remove any offline CPUs */
1346 cpumask_and(to_cpumask(args->mask), cpus, cpu_online_mask);
1347
1348 args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
1349 if (info->end != TLB_FLUSH_ALL &&
1350 (info->end - info->start) <= PAGE_SIZE) {
1351 args->op.cmd = MMUEXT_INVLPG_MULTI;
1352 args->op.arg1.linear_addr = info->start;
1353 }
1354
1355 MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
1356
1357 xen_mc_issue(XEN_LAZY_MMU);
1358 }
1359
xen_read_cr3(void)1360 static unsigned long xen_read_cr3(void)
1361 {
1362 return this_cpu_read(xen_cr3);
1363 }
1364
set_current_cr3(void * v)1365 static void set_current_cr3(void *v)
1366 {
1367 this_cpu_write(xen_current_cr3, (unsigned long)v);
1368 }
1369
__xen_write_cr3(bool kernel,unsigned long cr3)1370 static void __xen_write_cr3(bool kernel, unsigned long cr3)
1371 {
1372 struct mmuext_op op;
1373 unsigned long mfn;
1374
1375 trace_xen_mmu_write_cr3(kernel, cr3);
1376
1377 if (cr3)
1378 mfn = pfn_to_mfn(PFN_DOWN(cr3));
1379 else
1380 mfn = 0;
1381
1382 WARN_ON(mfn == 0 && kernel);
1383
1384 op.cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
1385 op.arg1.mfn = mfn;
1386
1387 xen_extend_mmuext_op(&op);
1388
1389 if (kernel) {
1390 this_cpu_write(xen_cr3, cr3);
1391
1392 /* Update xen_current_cr3 once the batch has actually
1393 been submitted. */
1394 xen_mc_callback(set_current_cr3, (void *)cr3);
1395 }
1396 }
xen_write_cr3(unsigned long cr3)1397 static void xen_write_cr3(unsigned long cr3)
1398 {
1399 pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
1400
1401 BUG_ON(preemptible());
1402
1403 xen_mc_batch(); /* disables interrupts */
1404
1405 /* Update while interrupts are disabled, so its atomic with
1406 respect to ipis */
1407 this_cpu_write(xen_cr3, cr3);
1408
1409 __xen_write_cr3(true, cr3);
1410
1411 if (user_pgd)
1412 __xen_write_cr3(false, __pa(user_pgd));
1413 else
1414 __xen_write_cr3(false, 0);
1415
1416 xen_mc_issue(XEN_LAZY_CPU); /* interrupts restored */
1417 }
1418
1419 /*
1420 * At the start of the day - when Xen launches a guest, it has already
1421 * built pagetables for the guest. We diligently look over them
1422 * in xen_setup_kernel_pagetable and graft as appropriate them in the
1423 * init_top_pgt and its friends. Then when we are happy we load
1424 * the new init_top_pgt - and continue on.
1425 *
1426 * The generic code starts (start_kernel) and 'init_mem_mapping' sets
1427 * up the rest of the pagetables. When it has completed it loads the cr3.
1428 * N.B. that baremetal would start at 'start_kernel' (and the early
1429 * #PF handler would create bootstrap pagetables) - so we are running
1430 * with the same assumptions as what to do when write_cr3 is executed
1431 * at this point.
1432 *
1433 * Since there are no user-page tables at all, we have two variants
1434 * of xen_write_cr3 - the early bootup (this one), and the late one
1435 * (xen_write_cr3). The reason we have to do that is that in 64-bit
1436 * the Linux kernel and user-space are both in ring 3 while the
1437 * hypervisor is in ring 0.
1438 */
xen_write_cr3_init(unsigned long cr3)1439 static void __init xen_write_cr3_init(unsigned long cr3)
1440 {
1441 BUG_ON(preemptible());
1442
1443 xen_mc_batch(); /* disables interrupts */
1444
1445 /* Update while interrupts are disabled, so its atomic with
1446 respect to ipis */
1447 this_cpu_write(xen_cr3, cr3);
1448
1449 __xen_write_cr3(true, cr3);
1450
1451 xen_mc_issue(XEN_LAZY_CPU); /* interrupts restored */
1452 }
1453
xen_pgd_alloc(struct mm_struct * mm)1454 static int xen_pgd_alloc(struct mm_struct *mm)
1455 {
1456 pgd_t *pgd = mm->pgd;
1457 struct page *page = virt_to_page(pgd);
1458 pgd_t *user_pgd;
1459 int ret = -ENOMEM;
1460
1461 BUG_ON(PagePinned(virt_to_page(pgd)));
1462 BUG_ON(page->private != 0);
1463
1464 user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
1465 page->private = (unsigned long)user_pgd;
1466
1467 if (user_pgd != NULL) {
1468 #ifdef CONFIG_X86_VSYSCALL_EMULATION
1469 user_pgd[pgd_index(VSYSCALL_ADDR)] =
1470 __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
1471 #endif
1472 ret = 0;
1473 }
1474
1475 BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
1476
1477 return ret;
1478 }
1479
xen_pgd_free(struct mm_struct * mm,pgd_t * pgd)1480 static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
1481 {
1482 pgd_t *user_pgd = xen_get_user_pgd(pgd);
1483
1484 if (user_pgd)
1485 free_page((unsigned long)user_pgd);
1486 }
1487
1488 /*
1489 * Init-time set_pte while constructing initial pagetables, which
1490 * doesn't allow RO page table pages to be remapped RW.
1491 *
1492 * If there is no MFN for this PFN then this page is initially
1493 * ballooned out so clear the PTE (as in decrease_reservation() in
1494 * drivers/xen/balloon.c).
1495 *
1496 * Many of these PTE updates are done on unpinned and writable pages
1497 * and doing a hypercall for these is unnecessary and expensive. At
1498 * this point it is rarely possible to tell if a page is pinned, so
1499 * mostly write the PTE directly and rely on Xen trapping and
1500 * emulating any updates as necessary.
1501 */
xen_set_pte_init(pte_t * ptep,pte_t pte)1502 static void __init xen_set_pte_init(pte_t *ptep, pte_t pte)
1503 {
1504 if (unlikely(is_early_ioremap_ptep(ptep)))
1505 __xen_set_pte(ptep, pte);
1506 else
1507 native_set_pte(ptep, pte);
1508 }
1509
xen_make_pte_init(pteval_t pte)1510 __visible pte_t xen_make_pte_init(pteval_t pte)
1511 {
1512 unsigned long pfn;
1513
1514 /*
1515 * Pages belonging to the initial p2m list mapped outside the default
1516 * address range must be mapped read-only. This region contains the
1517 * page tables for mapping the p2m list, too, and page tables MUST be
1518 * mapped read-only.
1519 */
1520 pfn = (pte & PTE_PFN_MASK) >> PAGE_SHIFT;
1521 if (xen_start_info->mfn_list < __START_KERNEL_map &&
1522 pfn >= xen_start_info->first_p2m_pfn &&
1523 pfn < xen_start_info->first_p2m_pfn + xen_start_info->nr_p2m_frames)
1524 pte &= ~_PAGE_RW;
1525
1526 pte = pte_pfn_to_mfn(pte);
1527 return native_make_pte(pte);
1528 }
1529 PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte_init);
1530
1531 /* Early in boot, while setting up the initial pagetable, assume
1532 everything is pinned. */
xen_alloc_pte_init(struct mm_struct * mm,unsigned long pfn)1533 static void __init xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
1534 {
1535 #ifdef CONFIG_FLATMEM
1536 BUG_ON(mem_map); /* should only be used early */
1537 #endif
1538 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
1539 pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
1540 }
1541
1542 /* Used for pmd and pud */
xen_alloc_pmd_init(struct mm_struct * mm,unsigned long pfn)1543 static void __init xen_alloc_pmd_init(struct mm_struct *mm, unsigned long pfn)
1544 {
1545 #ifdef CONFIG_FLATMEM
1546 BUG_ON(mem_map); /* should only be used early */
1547 #endif
1548 make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
1549 }
1550
1551 /* Early release_pte assumes that all pts are pinned, since there's
1552 only init_mm and anything attached to that is pinned. */
xen_release_pte_init(unsigned long pfn)1553 static void __init xen_release_pte_init(unsigned long pfn)
1554 {
1555 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
1556 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1557 }
1558
xen_release_pmd_init(unsigned long pfn)1559 static void __init xen_release_pmd_init(unsigned long pfn)
1560 {
1561 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
1562 }
1563
__pin_pagetable_pfn(unsigned cmd,unsigned long pfn)1564 static inline void __pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
1565 {
1566 struct multicall_space mcs;
1567 struct mmuext_op *op;
1568
1569 mcs = __xen_mc_entry(sizeof(*op));
1570 op = mcs.args;
1571 op->cmd = cmd;
1572 op->arg1.mfn = pfn_to_mfn(pfn);
1573
1574 MULTI_mmuext_op(mcs.mc, mcs.args, 1, NULL, DOMID_SELF);
1575 }
1576
__set_pfn_prot(unsigned long pfn,pgprot_t prot)1577 static inline void __set_pfn_prot(unsigned long pfn, pgprot_t prot)
1578 {
1579 struct multicall_space mcs;
1580 unsigned long addr = (unsigned long)__va(pfn << PAGE_SHIFT);
1581
1582 mcs = __xen_mc_entry(0);
1583 MULTI_update_va_mapping(mcs.mc, (unsigned long)addr,
1584 pfn_pte(pfn, prot), 0);
1585 }
1586
1587 /* This needs to make sure the new pte page is pinned iff its being
1588 attached to a pinned pagetable. */
xen_alloc_ptpage(struct mm_struct * mm,unsigned long pfn,unsigned level)1589 static inline void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn,
1590 unsigned level)
1591 {
1592 bool pinned = xen_page_pinned(mm->pgd);
1593
1594 trace_xen_mmu_alloc_ptpage(mm, pfn, level, pinned);
1595
1596 if (pinned) {
1597 struct page *page = pfn_to_page(pfn);
1598
1599 pinned = false;
1600 if (static_branch_likely(&xen_struct_pages_ready)) {
1601 pinned = PagePinned(page);
1602 SetPagePinned(page);
1603 }
1604
1605 xen_mc_batch();
1606
1607 __set_pfn_prot(pfn, PAGE_KERNEL_RO);
1608
1609 if (level == PT_PTE && USE_SPLIT_PTE_PTLOCKS && !pinned)
1610 __pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
1611
1612 xen_mc_issue(XEN_LAZY_MMU);
1613 }
1614 }
1615
xen_alloc_pte(struct mm_struct * mm,unsigned long pfn)1616 static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
1617 {
1618 xen_alloc_ptpage(mm, pfn, PT_PTE);
1619 }
1620
xen_alloc_pmd(struct mm_struct * mm,unsigned long pfn)1621 static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
1622 {
1623 xen_alloc_ptpage(mm, pfn, PT_PMD);
1624 }
1625
1626 /* This should never happen until we're OK to use struct page */
xen_release_ptpage(unsigned long pfn,unsigned level)1627 static inline void xen_release_ptpage(unsigned long pfn, unsigned level)
1628 {
1629 struct page *page = pfn_to_page(pfn);
1630 bool pinned = PagePinned(page);
1631
1632 trace_xen_mmu_release_ptpage(pfn, level, pinned);
1633
1634 if (pinned) {
1635 xen_mc_batch();
1636
1637 if (level == PT_PTE && USE_SPLIT_PTE_PTLOCKS)
1638 __pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
1639
1640 __set_pfn_prot(pfn, PAGE_KERNEL);
1641
1642 xen_mc_issue(XEN_LAZY_MMU);
1643
1644 ClearPagePinned(page);
1645 }
1646 }
1647
xen_release_pte(unsigned long pfn)1648 static void xen_release_pte(unsigned long pfn)
1649 {
1650 xen_release_ptpage(pfn, PT_PTE);
1651 }
1652
xen_release_pmd(unsigned long pfn)1653 static void xen_release_pmd(unsigned long pfn)
1654 {
1655 xen_release_ptpage(pfn, PT_PMD);
1656 }
1657
xen_alloc_pud(struct mm_struct * mm,unsigned long pfn)1658 static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
1659 {
1660 xen_alloc_ptpage(mm, pfn, PT_PUD);
1661 }
1662
xen_release_pud(unsigned long pfn)1663 static void xen_release_pud(unsigned long pfn)
1664 {
1665 xen_release_ptpage(pfn, PT_PUD);
1666 }
1667
1668 /*
1669 * Like __va(), but returns address in the kernel mapping (which is
1670 * all we have until the physical memory mapping has been set up.
1671 */
__ka(phys_addr_t paddr)1672 static void * __init __ka(phys_addr_t paddr)
1673 {
1674 return (void *)(paddr + __START_KERNEL_map);
1675 }
1676
1677 /* Convert a machine address to physical address */
m2p(phys_addr_t maddr)1678 static unsigned long __init m2p(phys_addr_t maddr)
1679 {
1680 phys_addr_t paddr;
1681
1682 maddr &= XEN_PTE_MFN_MASK;
1683 paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
1684
1685 return paddr;
1686 }
1687
1688 /* Convert a machine address to kernel virtual */
m2v(phys_addr_t maddr)1689 static void * __init m2v(phys_addr_t maddr)
1690 {
1691 return __ka(m2p(maddr));
1692 }
1693
1694 /* Set the page permissions on an identity-mapped pages */
set_page_prot_flags(void * addr,pgprot_t prot,unsigned long flags)1695 static void __init set_page_prot_flags(void *addr, pgprot_t prot,
1696 unsigned long flags)
1697 {
1698 unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
1699 pte_t pte = pfn_pte(pfn, prot);
1700
1701 if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, flags))
1702 BUG();
1703 }
set_page_prot(void * addr,pgprot_t prot)1704 static void __init set_page_prot(void *addr, pgprot_t prot)
1705 {
1706 return set_page_prot_flags(addr, prot, UVMF_NONE);
1707 }
1708
xen_setup_machphys_mapping(void)1709 void __init xen_setup_machphys_mapping(void)
1710 {
1711 struct xen_machphys_mapping mapping;
1712
1713 if (HYPERVISOR_memory_op(XENMEM_machphys_mapping, &mapping) == 0) {
1714 machine_to_phys_mapping = (unsigned long *)mapping.v_start;
1715 machine_to_phys_nr = mapping.max_mfn + 1;
1716 } else {
1717 machine_to_phys_nr = MACH2PHYS_NR_ENTRIES;
1718 }
1719 }
1720
convert_pfn_mfn(void * v)1721 static void __init convert_pfn_mfn(void *v)
1722 {
1723 pte_t *pte = v;
1724 int i;
1725
1726 /* All levels are converted the same way, so just treat them
1727 as ptes. */
1728 for (i = 0; i < PTRS_PER_PTE; i++)
1729 pte[i] = xen_make_pte(pte[i].pte);
1730 }
check_pt_base(unsigned long * pt_base,unsigned long * pt_end,unsigned long addr)1731 static void __init check_pt_base(unsigned long *pt_base, unsigned long *pt_end,
1732 unsigned long addr)
1733 {
1734 if (*pt_base == PFN_DOWN(__pa(addr))) {
1735 set_page_prot_flags((void *)addr, PAGE_KERNEL, UVMF_INVLPG);
1736 clear_page((void *)addr);
1737 (*pt_base)++;
1738 }
1739 if (*pt_end == PFN_DOWN(__pa(addr))) {
1740 set_page_prot_flags((void *)addr, PAGE_KERNEL, UVMF_INVLPG);
1741 clear_page((void *)addr);
1742 (*pt_end)--;
1743 }
1744 }
1745 /*
1746 * Set up the initial kernel pagetable.
1747 *
1748 * We can construct this by grafting the Xen provided pagetable into
1749 * head_64.S's preconstructed pagetables. We copy the Xen L2's into
1750 * level2_ident_pgt, and level2_kernel_pgt. This means that only the
1751 * kernel has a physical mapping to start with - but that's enough to
1752 * get __va working. We need to fill in the rest of the physical
1753 * mapping once some sort of allocator has been set up.
1754 */
xen_setup_kernel_pagetable(pgd_t * pgd,unsigned long max_pfn)1755 void __init xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
1756 {
1757 pud_t *l3;
1758 pmd_t *l2;
1759 unsigned long addr[3];
1760 unsigned long pt_base, pt_end;
1761 unsigned i;
1762
1763 /* max_pfn_mapped is the last pfn mapped in the initial memory
1764 * mappings. Considering that on Xen after the kernel mappings we
1765 * have the mappings of some pages that don't exist in pfn space, we
1766 * set max_pfn_mapped to the last real pfn mapped. */
1767 if (xen_start_info->mfn_list < __START_KERNEL_map)
1768 max_pfn_mapped = xen_start_info->first_p2m_pfn;
1769 else
1770 max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->mfn_list));
1771
1772 pt_base = PFN_DOWN(__pa(xen_start_info->pt_base));
1773 pt_end = pt_base + xen_start_info->nr_pt_frames;
1774
1775 /* Zap identity mapping */
1776 init_top_pgt[0] = __pgd(0);
1777
1778 /* Pre-constructed entries are in pfn, so convert to mfn */
1779 /* L4[273] -> level3_ident_pgt */
1780 /* L4[511] -> level3_kernel_pgt */
1781 convert_pfn_mfn(init_top_pgt);
1782
1783 /* L3_i[0] -> level2_ident_pgt */
1784 convert_pfn_mfn(level3_ident_pgt);
1785 /* L3_k[510] -> level2_kernel_pgt */
1786 /* L3_k[511] -> level2_fixmap_pgt */
1787 convert_pfn_mfn(level3_kernel_pgt);
1788
1789 /* L3_k[511][508-FIXMAP_PMD_NUM ... 507] -> level1_fixmap_pgt */
1790 convert_pfn_mfn(level2_fixmap_pgt);
1791
1792 /* We get [511][511] and have Xen's version of level2_kernel_pgt */
1793 l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
1794 l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
1795
1796 addr[0] = (unsigned long)pgd;
1797 addr[1] = (unsigned long)l3;
1798 addr[2] = (unsigned long)l2;
1799 /* Graft it onto L4[273][0]. Note that we creating an aliasing problem:
1800 * Both L4[273][0] and L4[511][510] have entries that point to the same
1801 * L2 (PMD) tables. Meaning that if you modify it in __va space
1802 * it will be also modified in the __ka space! (But if you just
1803 * modify the PMD table to point to other PTE's or none, then you
1804 * are OK - which is what cleanup_highmap does) */
1805 copy_page(level2_ident_pgt, l2);
1806 /* Graft it onto L4[511][510] */
1807 copy_page(level2_kernel_pgt, l2);
1808
1809 /*
1810 * Zap execute permission from the ident map. Due to the sharing of
1811 * L1 entries we need to do this in the L2.
1812 */
1813 if (__supported_pte_mask & _PAGE_NX) {
1814 for (i = 0; i < PTRS_PER_PMD; ++i) {
1815 if (pmd_none(level2_ident_pgt[i]))
1816 continue;
1817 level2_ident_pgt[i] = pmd_set_flags(level2_ident_pgt[i], _PAGE_NX);
1818 }
1819 }
1820
1821 /* Copy the initial P->M table mappings if necessary. */
1822 i = pgd_index(xen_start_info->mfn_list);
1823 if (i && i < pgd_index(__START_KERNEL_map))
1824 init_top_pgt[i] = ((pgd_t *)xen_start_info->pt_base)[i];
1825
1826 /* Make pagetable pieces RO */
1827 set_page_prot(init_top_pgt, PAGE_KERNEL_RO);
1828 set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
1829 set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
1830 set_page_prot(level2_ident_pgt, PAGE_KERNEL_RO);
1831 set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
1832 set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
1833
1834 for (i = 0; i < FIXMAP_PMD_NUM; i++) {
1835 set_page_prot(level1_fixmap_pgt + i * PTRS_PER_PTE,
1836 PAGE_KERNEL_RO);
1837 }
1838
1839 /* Pin down new L4 */
1840 pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
1841 PFN_DOWN(__pa_symbol(init_top_pgt)));
1842
1843 /* Unpin Xen-provided one */
1844 pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
1845
1846 #ifdef CONFIG_X86_VSYSCALL_EMULATION
1847 /* Pin user vsyscall L3 */
1848 set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
1849 pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE,
1850 PFN_DOWN(__pa_symbol(level3_user_vsyscall)));
1851 #endif
1852
1853 /*
1854 * At this stage there can be no user pgd, and no page structure to
1855 * attach it to, so make sure we just set kernel pgd.
1856 */
1857 xen_mc_batch();
1858 __xen_write_cr3(true, __pa(init_top_pgt));
1859 xen_mc_issue(XEN_LAZY_CPU);
1860
1861 /* We can't that easily rip out L3 and L2, as the Xen pagetables are
1862 * set out this way: [L4], [L1], [L2], [L3], [L1], [L1] ... for
1863 * the initial domain. For guests using the toolstack, they are in:
1864 * [L4], [L3], [L2], [L1], [L1], order .. So for dom0 we can only
1865 * rip out the [L4] (pgd), but for guests we shave off three pages.
1866 */
1867 for (i = 0; i < ARRAY_SIZE(addr); i++)
1868 check_pt_base(&pt_base, &pt_end, addr[i]);
1869
1870 /* Our (by three pages) smaller Xen pagetable that we are using */
1871 xen_pt_base = PFN_PHYS(pt_base);
1872 xen_pt_size = (pt_end - pt_base) * PAGE_SIZE;
1873 memblock_reserve(xen_pt_base, xen_pt_size);
1874
1875 /* Revector the xen_start_info */
1876 xen_start_info = (struct start_info *)__va(__pa(xen_start_info));
1877 }
1878
1879 /*
1880 * Read a value from a physical address.
1881 */
xen_read_phys_ulong(phys_addr_t addr)1882 static unsigned long __init xen_read_phys_ulong(phys_addr_t addr)
1883 {
1884 unsigned long *vaddr;
1885 unsigned long val;
1886
1887 vaddr = early_memremap_ro(addr, sizeof(val));
1888 val = *vaddr;
1889 early_memunmap(vaddr, sizeof(val));
1890 return val;
1891 }
1892
1893 /*
1894 * Translate a virtual address to a physical one without relying on mapped
1895 * page tables. Don't rely on big pages being aligned in (guest) physical
1896 * space!
1897 */
xen_early_virt_to_phys(unsigned long vaddr)1898 static phys_addr_t __init xen_early_virt_to_phys(unsigned long vaddr)
1899 {
1900 phys_addr_t pa;
1901 pgd_t pgd;
1902 pud_t pud;
1903 pmd_t pmd;
1904 pte_t pte;
1905
1906 pa = read_cr3_pa();
1907 pgd = native_make_pgd(xen_read_phys_ulong(pa + pgd_index(vaddr) *
1908 sizeof(pgd)));
1909 if (!pgd_present(pgd))
1910 return 0;
1911
1912 pa = pgd_val(pgd) & PTE_PFN_MASK;
1913 pud = native_make_pud(xen_read_phys_ulong(pa + pud_index(vaddr) *
1914 sizeof(pud)));
1915 if (!pud_present(pud))
1916 return 0;
1917 pa = pud_val(pud) & PTE_PFN_MASK;
1918 if (pud_leaf(pud))
1919 return pa + (vaddr & ~PUD_MASK);
1920
1921 pmd = native_make_pmd(xen_read_phys_ulong(pa + pmd_index(vaddr) *
1922 sizeof(pmd)));
1923 if (!pmd_present(pmd))
1924 return 0;
1925 pa = pmd_val(pmd) & PTE_PFN_MASK;
1926 if (pmd_large(pmd))
1927 return pa + (vaddr & ~PMD_MASK);
1928
1929 pte = native_make_pte(xen_read_phys_ulong(pa + pte_index(vaddr) *
1930 sizeof(pte)));
1931 if (!pte_present(pte))
1932 return 0;
1933 pa = pte_pfn(pte) << PAGE_SHIFT;
1934
1935 return pa | (vaddr & ~PAGE_MASK);
1936 }
1937
1938 /*
1939 * Find a new area for the hypervisor supplied p2m list and relocate the p2m to
1940 * this area.
1941 */
xen_relocate_p2m(void)1942 void __init xen_relocate_p2m(void)
1943 {
1944 phys_addr_t size, new_area, pt_phys, pmd_phys, pud_phys;
1945 unsigned long p2m_pfn, p2m_pfn_end, n_frames, pfn, pfn_end;
1946 int n_pte, n_pt, n_pmd, n_pud, idx_pte, idx_pt, idx_pmd, idx_pud;
1947 pte_t *pt;
1948 pmd_t *pmd;
1949 pud_t *pud;
1950 pgd_t *pgd;
1951 unsigned long *new_p2m;
1952
1953 size = PAGE_ALIGN(xen_start_info->nr_pages * sizeof(unsigned long));
1954 n_pte = roundup(size, PAGE_SIZE) >> PAGE_SHIFT;
1955 n_pt = roundup(size, PMD_SIZE) >> PMD_SHIFT;
1956 n_pmd = roundup(size, PUD_SIZE) >> PUD_SHIFT;
1957 n_pud = roundup(size, P4D_SIZE) >> P4D_SHIFT;
1958 n_frames = n_pte + n_pt + n_pmd + n_pud;
1959
1960 new_area = xen_find_free_area(PFN_PHYS(n_frames));
1961 if (!new_area) {
1962 xen_raw_console_write("Can't find new memory area for p2m needed due to E820 map conflict\n");
1963 BUG();
1964 }
1965
1966 /*
1967 * Setup the page tables for addressing the new p2m list.
1968 * We have asked the hypervisor to map the p2m list at the user address
1969 * PUD_SIZE. It may have done so, or it may have used a kernel space
1970 * address depending on the Xen version.
1971 * To avoid any possible virtual address collision, just use
1972 * 2 * PUD_SIZE for the new area.
1973 */
1974 pud_phys = new_area;
1975 pmd_phys = pud_phys + PFN_PHYS(n_pud);
1976 pt_phys = pmd_phys + PFN_PHYS(n_pmd);
1977 p2m_pfn = PFN_DOWN(pt_phys) + n_pt;
1978
1979 pgd = __va(read_cr3_pa());
1980 new_p2m = (unsigned long *)(2 * PGDIR_SIZE);
1981 for (idx_pud = 0; idx_pud < n_pud; idx_pud++) {
1982 pud = early_memremap(pud_phys, PAGE_SIZE);
1983 clear_page(pud);
1984 for (idx_pmd = 0; idx_pmd < min(n_pmd, PTRS_PER_PUD);
1985 idx_pmd++) {
1986 pmd = early_memremap(pmd_phys, PAGE_SIZE);
1987 clear_page(pmd);
1988 for (idx_pt = 0; idx_pt < min(n_pt, PTRS_PER_PMD);
1989 idx_pt++) {
1990 pt = early_memremap(pt_phys, PAGE_SIZE);
1991 clear_page(pt);
1992 for (idx_pte = 0;
1993 idx_pte < min(n_pte, PTRS_PER_PTE);
1994 idx_pte++) {
1995 pt[idx_pte] = pfn_pte(p2m_pfn,
1996 PAGE_KERNEL);
1997 p2m_pfn++;
1998 }
1999 n_pte -= PTRS_PER_PTE;
2000 early_memunmap(pt, PAGE_SIZE);
2001 make_lowmem_page_readonly(__va(pt_phys));
2002 pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE,
2003 PFN_DOWN(pt_phys));
2004 pmd[idx_pt] = __pmd(_PAGE_TABLE | pt_phys);
2005 pt_phys += PAGE_SIZE;
2006 }
2007 n_pt -= PTRS_PER_PMD;
2008 early_memunmap(pmd, PAGE_SIZE);
2009 make_lowmem_page_readonly(__va(pmd_phys));
2010 pin_pagetable_pfn(MMUEXT_PIN_L2_TABLE,
2011 PFN_DOWN(pmd_phys));
2012 pud[idx_pmd] = __pud(_PAGE_TABLE | pmd_phys);
2013 pmd_phys += PAGE_SIZE;
2014 }
2015 n_pmd -= PTRS_PER_PUD;
2016 early_memunmap(pud, PAGE_SIZE);
2017 make_lowmem_page_readonly(__va(pud_phys));
2018 pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(pud_phys));
2019 set_pgd(pgd + 2 + idx_pud, __pgd(_PAGE_TABLE | pud_phys));
2020 pud_phys += PAGE_SIZE;
2021 }
2022
2023 /* Now copy the old p2m info to the new area. */
2024 memcpy(new_p2m, xen_p2m_addr, size);
2025 xen_p2m_addr = new_p2m;
2026
2027 /* Release the old p2m list and set new list info. */
2028 p2m_pfn = PFN_DOWN(xen_early_virt_to_phys(xen_start_info->mfn_list));
2029 BUG_ON(!p2m_pfn);
2030 p2m_pfn_end = p2m_pfn + PFN_DOWN(size);
2031
2032 if (xen_start_info->mfn_list < __START_KERNEL_map) {
2033 pfn = xen_start_info->first_p2m_pfn;
2034 pfn_end = xen_start_info->first_p2m_pfn +
2035 xen_start_info->nr_p2m_frames;
2036 set_pgd(pgd + 1, __pgd(0));
2037 } else {
2038 pfn = p2m_pfn;
2039 pfn_end = p2m_pfn_end;
2040 }
2041
2042 memblock_phys_free(PFN_PHYS(pfn), PAGE_SIZE * (pfn_end - pfn));
2043 while (pfn < pfn_end) {
2044 if (pfn == p2m_pfn) {
2045 pfn = p2m_pfn_end;
2046 continue;
2047 }
2048 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
2049 pfn++;
2050 }
2051
2052 xen_start_info->mfn_list = (unsigned long)xen_p2m_addr;
2053 xen_start_info->first_p2m_pfn = PFN_DOWN(new_area);
2054 xen_start_info->nr_p2m_frames = n_frames;
2055 }
2056
xen_reserve_special_pages(void)2057 void __init xen_reserve_special_pages(void)
2058 {
2059 phys_addr_t paddr;
2060
2061 memblock_reserve(__pa(xen_start_info), PAGE_SIZE);
2062 if (xen_start_info->store_mfn) {
2063 paddr = PFN_PHYS(mfn_to_pfn(xen_start_info->store_mfn));
2064 memblock_reserve(paddr, PAGE_SIZE);
2065 }
2066 if (!xen_initial_domain()) {
2067 paddr = PFN_PHYS(mfn_to_pfn(xen_start_info->console.domU.mfn));
2068 memblock_reserve(paddr, PAGE_SIZE);
2069 }
2070 }
2071
xen_pt_check_e820(void)2072 void __init xen_pt_check_e820(void)
2073 {
2074 xen_chk_is_e820_usable(xen_pt_base, xen_pt_size, "page table");
2075 }
2076
2077 static unsigned char dummy_mapping[PAGE_SIZE] __page_aligned_bss;
2078
xen_set_fixmap(unsigned idx,phys_addr_t phys,pgprot_t prot)2079 static void xen_set_fixmap(unsigned idx, phys_addr_t phys, pgprot_t prot)
2080 {
2081 pte_t pte;
2082 unsigned long vaddr;
2083
2084 phys >>= PAGE_SHIFT;
2085
2086 switch (idx) {
2087 case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
2088 #ifdef CONFIG_X86_VSYSCALL_EMULATION
2089 case VSYSCALL_PAGE:
2090 #endif
2091 /* All local page mappings */
2092 pte = pfn_pte(phys, prot);
2093 break;
2094
2095 #ifdef CONFIG_X86_LOCAL_APIC
2096 case FIX_APIC_BASE: /* maps dummy local APIC */
2097 pte = pfn_pte(PFN_DOWN(__pa(dummy_mapping)), PAGE_KERNEL);
2098 break;
2099 #endif
2100
2101 #ifdef CONFIG_X86_IO_APIC
2102 case FIX_IO_APIC_BASE_0 ... FIX_IO_APIC_BASE_END:
2103 /*
2104 * We just don't map the IO APIC - all access is via
2105 * hypercalls. Keep the address in the pte for reference.
2106 */
2107 pte = pfn_pte(PFN_DOWN(__pa(dummy_mapping)), PAGE_KERNEL);
2108 break;
2109 #endif
2110
2111 case FIX_PARAVIRT_BOOTMAP:
2112 /* This is an MFN, but it isn't an IO mapping from the
2113 IO domain */
2114 pte = mfn_pte(phys, prot);
2115 break;
2116
2117 default:
2118 /* By default, set_fixmap is used for hardware mappings */
2119 pte = mfn_pte(phys, prot);
2120 break;
2121 }
2122
2123 vaddr = __fix_to_virt(idx);
2124 if (HYPERVISOR_update_va_mapping(vaddr, pte, UVMF_INVLPG))
2125 BUG();
2126
2127 #ifdef CONFIG_X86_VSYSCALL_EMULATION
2128 /* Replicate changes to map the vsyscall page into the user
2129 pagetable vsyscall mapping. */
2130 if (idx == VSYSCALL_PAGE)
2131 set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
2132 #endif
2133 }
2134
xen_enter_lazy_mmu(void)2135 static void xen_enter_lazy_mmu(void)
2136 {
2137 enter_lazy(XEN_LAZY_MMU);
2138 }
2139
xen_flush_lazy_mmu(void)2140 static void xen_flush_lazy_mmu(void)
2141 {
2142 preempt_disable();
2143
2144 if (xen_get_lazy_mode() == XEN_LAZY_MMU) {
2145 arch_leave_lazy_mmu_mode();
2146 arch_enter_lazy_mmu_mode();
2147 }
2148
2149 preempt_enable();
2150 }
2151
xen_post_allocator_init(void)2152 static void __init xen_post_allocator_init(void)
2153 {
2154 pv_ops.mmu.set_pte = xen_set_pte;
2155 pv_ops.mmu.set_pmd = xen_set_pmd;
2156 pv_ops.mmu.set_pud = xen_set_pud;
2157 pv_ops.mmu.set_p4d = xen_set_p4d;
2158
2159 /* This will work as long as patching hasn't happened yet
2160 (which it hasn't) */
2161 pv_ops.mmu.alloc_pte = xen_alloc_pte;
2162 pv_ops.mmu.alloc_pmd = xen_alloc_pmd;
2163 pv_ops.mmu.release_pte = xen_release_pte;
2164 pv_ops.mmu.release_pmd = xen_release_pmd;
2165 pv_ops.mmu.alloc_pud = xen_alloc_pud;
2166 pv_ops.mmu.release_pud = xen_release_pud;
2167 pv_ops.mmu.make_pte = PV_CALLEE_SAVE(xen_make_pte);
2168
2169 pv_ops.mmu.write_cr3 = &xen_write_cr3;
2170 }
2171
xen_leave_lazy_mmu(void)2172 static void xen_leave_lazy_mmu(void)
2173 {
2174 preempt_disable();
2175 xen_mc_flush();
2176 leave_lazy(XEN_LAZY_MMU);
2177 preempt_enable();
2178 }
2179
2180 static const typeof(pv_ops) xen_mmu_ops __initconst = {
2181 .mmu = {
2182 .read_cr2 = __PV_IS_CALLEE_SAVE(xen_read_cr2),
2183 .write_cr2 = xen_write_cr2,
2184
2185 .read_cr3 = xen_read_cr3,
2186 .write_cr3 = xen_write_cr3_init,
2187
2188 .flush_tlb_user = xen_flush_tlb,
2189 .flush_tlb_kernel = xen_flush_tlb,
2190 .flush_tlb_one_user = xen_flush_tlb_one_user,
2191 .flush_tlb_multi = xen_flush_tlb_multi,
2192 .tlb_remove_table = tlb_remove_table,
2193
2194 .pgd_alloc = xen_pgd_alloc,
2195 .pgd_free = xen_pgd_free,
2196
2197 .alloc_pte = xen_alloc_pte_init,
2198 .release_pte = xen_release_pte_init,
2199 .alloc_pmd = xen_alloc_pmd_init,
2200 .release_pmd = xen_release_pmd_init,
2201
2202 .set_pte = xen_set_pte_init,
2203 .set_pmd = xen_set_pmd_hyper,
2204
2205 .ptep_modify_prot_start = xen_ptep_modify_prot_start,
2206 .ptep_modify_prot_commit = xen_ptep_modify_prot_commit,
2207
2208 .pte_val = PV_CALLEE_SAVE(xen_pte_val),
2209 .pgd_val = PV_CALLEE_SAVE(xen_pgd_val),
2210
2211 .make_pte = PV_CALLEE_SAVE(xen_make_pte_init),
2212 .make_pgd = PV_CALLEE_SAVE(xen_make_pgd),
2213
2214 .set_pud = xen_set_pud_hyper,
2215
2216 .make_pmd = PV_CALLEE_SAVE(xen_make_pmd),
2217 .pmd_val = PV_CALLEE_SAVE(xen_pmd_val),
2218
2219 .pud_val = PV_CALLEE_SAVE(xen_pud_val),
2220 .make_pud = PV_CALLEE_SAVE(xen_make_pud),
2221 .set_p4d = xen_set_p4d_hyper,
2222
2223 .alloc_pud = xen_alloc_pmd_init,
2224 .release_pud = xen_release_pmd_init,
2225
2226 #if CONFIG_PGTABLE_LEVELS >= 5
2227 .p4d_val = PV_CALLEE_SAVE(xen_p4d_val),
2228 .make_p4d = PV_CALLEE_SAVE(xen_make_p4d),
2229 #endif
2230
2231 .enter_mmap = xen_enter_mmap,
2232 .exit_mmap = xen_exit_mmap,
2233
2234 .lazy_mode = {
2235 .enter = xen_enter_lazy_mmu,
2236 .leave = xen_leave_lazy_mmu,
2237 .flush = xen_flush_lazy_mmu,
2238 },
2239
2240 .set_fixmap = xen_set_fixmap,
2241 },
2242 };
2243
xen_init_mmu_ops(void)2244 void __init xen_init_mmu_ops(void)
2245 {
2246 x86_init.paging.pagetable_init = xen_pagetable_init;
2247 x86_init.hyper.init_after_bootmem = xen_after_bootmem;
2248
2249 pv_ops.mmu = xen_mmu_ops.mmu;
2250
2251 memset(dummy_mapping, 0xff, PAGE_SIZE);
2252 }
2253
2254 #define VOID_PTE (mfn_pte(0, __pgprot(0)))
xen_zap_pfn_range(unsigned long vaddr,unsigned int order,unsigned long * in_frames,unsigned long * out_frames)2255 static void xen_zap_pfn_range(unsigned long vaddr, unsigned int order,
2256 unsigned long *in_frames,
2257 unsigned long *out_frames)
2258 {
2259 int i;
2260 struct multicall_space mcs;
2261
2262 xen_mc_batch();
2263 for (i = 0; i < (1UL<<order); i++, vaddr += PAGE_SIZE) {
2264 mcs = __xen_mc_entry(0);
2265
2266 if (in_frames)
2267 in_frames[i] = virt_to_mfn((void *)vaddr);
2268
2269 MULTI_update_va_mapping(mcs.mc, vaddr, VOID_PTE, 0);
2270 __set_phys_to_machine(virt_to_pfn((void *)vaddr), INVALID_P2M_ENTRY);
2271
2272 if (out_frames)
2273 out_frames[i] = virt_to_pfn((void *)vaddr);
2274 }
2275 xen_mc_issue(0);
2276 }
2277
2278 /*
2279 * Update the pfn-to-mfn mappings for a virtual address range, either to
2280 * point to an array of mfns, or contiguously from a single starting
2281 * mfn.
2282 */
xen_remap_exchanged_ptes(unsigned long vaddr,int order,unsigned long * mfns,unsigned long first_mfn)2283 static void xen_remap_exchanged_ptes(unsigned long vaddr, int order,
2284 unsigned long *mfns,
2285 unsigned long first_mfn)
2286 {
2287 unsigned i, limit;
2288 unsigned long mfn;
2289
2290 xen_mc_batch();
2291
2292 limit = 1u << order;
2293 for (i = 0; i < limit; i++, vaddr += PAGE_SIZE) {
2294 struct multicall_space mcs;
2295 unsigned flags;
2296
2297 mcs = __xen_mc_entry(0);
2298 if (mfns)
2299 mfn = mfns[i];
2300 else
2301 mfn = first_mfn + i;
2302
2303 if (i < (limit - 1))
2304 flags = 0;
2305 else {
2306 if (order == 0)
2307 flags = UVMF_INVLPG | UVMF_ALL;
2308 else
2309 flags = UVMF_TLB_FLUSH | UVMF_ALL;
2310 }
2311
2312 MULTI_update_va_mapping(mcs.mc, vaddr,
2313 mfn_pte(mfn, PAGE_KERNEL), flags);
2314
2315 set_phys_to_machine(virt_to_pfn((void *)vaddr), mfn);
2316 }
2317
2318 xen_mc_issue(0);
2319 }
2320
2321 /*
2322 * Perform the hypercall to exchange a region of our pfns to point to
2323 * memory with the required contiguous alignment. Takes the pfns as
2324 * input, and populates mfns as output.
2325 *
2326 * Returns a success code indicating whether the hypervisor was able to
2327 * satisfy the request or not.
2328 */
xen_exchange_memory(unsigned long extents_in,unsigned int order_in,unsigned long * pfns_in,unsigned long extents_out,unsigned int order_out,unsigned long * mfns_out,unsigned int address_bits)2329 static int xen_exchange_memory(unsigned long extents_in, unsigned int order_in,
2330 unsigned long *pfns_in,
2331 unsigned long extents_out,
2332 unsigned int order_out,
2333 unsigned long *mfns_out,
2334 unsigned int address_bits)
2335 {
2336 long rc;
2337 int success;
2338
2339 struct xen_memory_exchange exchange = {
2340 .in = {
2341 .nr_extents = extents_in,
2342 .extent_order = order_in,
2343 .extent_start = pfns_in,
2344 .domid = DOMID_SELF
2345 },
2346 .out = {
2347 .nr_extents = extents_out,
2348 .extent_order = order_out,
2349 .extent_start = mfns_out,
2350 .address_bits = address_bits,
2351 .domid = DOMID_SELF
2352 }
2353 };
2354
2355 BUG_ON(extents_in << order_in != extents_out << order_out);
2356
2357 rc = HYPERVISOR_memory_op(XENMEM_exchange, &exchange);
2358 success = (exchange.nr_exchanged == extents_in);
2359
2360 BUG_ON(!success && ((exchange.nr_exchanged != 0) || (rc == 0)));
2361 BUG_ON(success && (rc != 0));
2362
2363 return success;
2364 }
2365
xen_create_contiguous_region(phys_addr_t pstart,unsigned int order,unsigned int address_bits,dma_addr_t * dma_handle)2366 int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
2367 unsigned int address_bits,
2368 dma_addr_t *dma_handle)
2369 {
2370 unsigned long *in_frames, out_frame;
2371 unsigned long flags;
2372 int success;
2373 unsigned long vstart = (unsigned long)phys_to_virt(pstart);
2374
2375 if (unlikely(order > discontig_frames_order)) {
2376 if (!discontig_frames_dyn)
2377 return -ENOMEM;
2378
2379 if (alloc_discontig_frames(order))
2380 return -ENOMEM;
2381 }
2382
2383 memset((void *) vstart, 0, PAGE_SIZE << order);
2384
2385 spin_lock_irqsave(&xen_reservation_lock, flags);
2386
2387 in_frames = discontig_frames;
2388
2389 /* 1. Zap current PTEs, remembering MFNs. */
2390 xen_zap_pfn_range(vstart, order, in_frames, NULL);
2391
2392 /* 2. Get a new contiguous memory extent. */
2393 out_frame = virt_to_pfn((void *)vstart);
2394 success = xen_exchange_memory(1UL << order, 0, in_frames,
2395 1, order, &out_frame,
2396 address_bits);
2397
2398 /* 3. Map the new extent in place of old pages. */
2399 if (success)
2400 xen_remap_exchanged_ptes(vstart, order, NULL, out_frame);
2401 else
2402 xen_remap_exchanged_ptes(vstart, order, in_frames, 0);
2403
2404 spin_unlock_irqrestore(&xen_reservation_lock, flags);
2405
2406 *dma_handle = virt_to_machine(vstart).maddr;
2407 return success ? 0 : -ENOMEM;
2408 }
2409
xen_destroy_contiguous_region(phys_addr_t pstart,unsigned int order)2410 void xen_destroy_contiguous_region(phys_addr_t pstart, unsigned int order)
2411 {
2412 unsigned long *out_frames, in_frame;
2413 unsigned long flags;
2414 int success;
2415 unsigned long vstart;
2416
2417 if (unlikely(order > discontig_frames_order))
2418 return;
2419
2420 vstart = (unsigned long)phys_to_virt(pstart);
2421 memset((void *) vstart, 0, PAGE_SIZE << order);
2422
2423 spin_lock_irqsave(&xen_reservation_lock, flags);
2424
2425 out_frames = discontig_frames;
2426
2427 /* 1. Find start MFN of contiguous extent. */
2428 in_frame = virt_to_mfn((void *)vstart);
2429
2430 /* 2. Zap current PTEs. */
2431 xen_zap_pfn_range(vstart, order, NULL, out_frames);
2432
2433 /* 3. Do the exchange for non-contiguous MFNs. */
2434 success = xen_exchange_memory(1, order, &in_frame, 1UL << order,
2435 0, out_frames, 0);
2436
2437 /* 4. Map new pages in place of old pages. */
2438 if (success)
2439 xen_remap_exchanged_ptes(vstart, order, out_frames, 0);
2440 else
2441 xen_remap_exchanged_ptes(vstart, order, NULL, in_frame);
2442
2443 spin_unlock_irqrestore(&xen_reservation_lock, flags);
2444 }
2445
xen_flush_tlb_all(void)2446 static noinline void xen_flush_tlb_all(void)
2447 {
2448 struct mmuext_op *op;
2449 struct multicall_space mcs;
2450
2451 preempt_disable();
2452
2453 mcs = xen_mc_entry(sizeof(*op));
2454
2455 op = mcs.args;
2456 op->cmd = MMUEXT_TLB_FLUSH_ALL;
2457 MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
2458
2459 xen_mc_issue(XEN_LAZY_MMU);
2460
2461 preempt_enable();
2462 }
2463
2464 #define REMAP_BATCH_SIZE 16
2465
2466 struct remap_data {
2467 xen_pfn_t *pfn;
2468 bool contiguous;
2469 bool no_translate;
2470 pgprot_t prot;
2471 struct mmu_update *mmu_update;
2472 };
2473
remap_area_pfn_pte_fn(pte_t * ptep,unsigned long addr,void * data)2474 static int remap_area_pfn_pte_fn(pte_t *ptep, unsigned long addr, void *data)
2475 {
2476 struct remap_data *rmd = data;
2477 pte_t pte = pte_mkspecial(mfn_pte(*rmd->pfn, rmd->prot));
2478
2479 /*
2480 * If we have a contiguous range, just update the pfn itself,
2481 * else update pointer to be "next pfn".
2482 */
2483 if (rmd->contiguous)
2484 (*rmd->pfn)++;
2485 else
2486 rmd->pfn++;
2487
2488 rmd->mmu_update->ptr = virt_to_machine(ptep).maddr;
2489 rmd->mmu_update->ptr |= rmd->no_translate ?
2490 MMU_PT_UPDATE_NO_TRANSLATE :
2491 MMU_NORMAL_PT_UPDATE;
2492 rmd->mmu_update->val = pte_val_ma(pte);
2493 rmd->mmu_update++;
2494
2495 return 0;
2496 }
2497
xen_remap_pfn(struct vm_area_struct * vma,unsigned long addr,xen_pfn_t * pfn,int nr,int * err_ptr,pgprot_t prot,unsigned int domid,bool no_translate)2498 int xen_remap_pfn(struct vm_area_struct *vma, unsigned long addr,
2499 xen_pfn_t *pfn, int nr, int *err_ptr, pgprot_t prot,
2500 unsigned int domid, bool no_translate)
2501 {
2502 int err = 0;
2503 struct remap_data rmd;
2504 struct mmu_update mmu_update[REMAP_BATCH_SIZE];
2505 unsigned long range;
2506 int mapped = 0;
2507
2508 BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
2509
2510 rmd.pfn = pfn;
2511 rmd.prot = prot;
2512 /*
2513 * We use the err_ptr to indicate if there we are doing a contiguous
2514 * mapping or a discontiguous mapping.
2515 */
2516 rmd.contiguous = !err_ptr;
2517 rmd.no_translate = no_translate;
2518
2519 while (nr) {
2520 int index = 0;
2521 int done = 0;
2522 int batch = min(REMAP_BATCH_SIZE, nr);
2523 int batch_left = batch;
2524
2525 range = (unsigned long)batch << PAGE_SHIFT;
2526
2527 rmd.mmu_update = mmu_update;
2528 err = apply_to_page_range(vma->vm_mm, addr, range,
2529 remap_area_pfn_pte_fn, &rmd);
2530 if (err)
2531 goto out;
2532
2533 /*
2534 * We record the error for each page that gives an error, but
2535 * continue mapping until the whole set is done
2536 */
2537 do {
2538 int i;
2539
2540 err = HYPERVISOR_mmu_update(&mmu_update[index],
2541 batch_left, &done, domid);
2542
2543 /*
2544 * @err_ptr may be the same buffer as @gfn, so
2545 * only clear it after each chunk of @gfn is
2546 * used.
2547 */
2548 if (err_ptr) {
2549 for (i = index; i < index + done; i++)
2550 err_ptr[i] = 0;
2551 }
2552 if (err < 0) {
2553 if (!err_ptr)
2554 goto out;
2555 err_ptr[i] = err;
2556 done++; /* Skip failed frame. */
2557 } else
2558 mapped += done;
2559 batch_left -= done;
2560 index += done;
2561 } while (batch_left);
2562
2563 nr -= batch;
2564 addr += range;
2565 if (err_ptr)
2566 err_ptr += batch;
2567 cond_resched();
2568 }
2569 out:
2570
2571 xen_flush_tlb_all();
2572
2573 return err < 0 ? err : mapped;
2574 }
2575 EXPORT_SYMBOL_GPL(xen_remap_pfn);
2576
2577 #ifdef CONFIG_KEXEC_CORE
paddr_vmcoreinfo_note(void)2578 phys_addr_t paddr_vmcoreinfo_note(void)
2579 {
2580 if (xen_pv_domain())
2581 return virt_to_machine(vmcoreinfo_note).maddr;
2582 else
2583 return __pa(vmcoreinfo_note);
2584 }
2585 #endif /* CONFIG_KEXEC_CORE */
2586