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