1 #ifndef _ASM_X86_PGTABLE_H 2 #define _ASM_X86_PGTABLE_H 3 4 #include <asm/page.h> 5 #include <asm/pgtable_types.h> 6 7 /* 8 * Macro to mark a page protection value as UC- 9 */ 10 #define pgprot_noncached(prot) \ 11 ((boot_cpu_data.x86 > 3) \ 12 ? (__pgprot(pgprot_val(prot) | \ 13 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \ 14 : (prot)) 15 16 #ifndef __ASSEMBLY__ 17 #include <asm/x86_init.h> 18 19 void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd); 20 void ptdump_walk_pgd_level_checkwx(void); 21 22 #ifdef CONFIG_DEBUG_WX 23 #define debug_checkwx() ptdump_walk_pgd_level_checkwx() 24 #else 25 #define debug_checkwx() do { } while (0) 26 #endif 27 28 /* 29 * ZERO_PAGE is a global shared page that is always zero: used 30 * for zero-mapped memory areas etc.. 31 */ 32 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] 33 __visible; 34 #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page)) 35 36 extern spinlock_t pgd_lock; 37 extern struct list_head pgd_list; 38 39 extern struct mm_struct *pgd_page_get_mm(struct page *page); 40 41 #ifdef CONFIG_PARAVIRT 42 #include <asm/paravirt.h> 43 #else /* !CONFIG_PARAVIRT */ 44 #define set_pte(ptep, pte) native_set_pte(ptep, pte) 45 #define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte) 46 #define set_pmd_at(mm, addr, pmdp, pmd) native_set_pmd_at(mm, addr, pmdp, pmd) 47 #define set_pud_at(mm, addr, pudp, pud) native_set_pud_at(mm, addr, pudp, pud) 48 49 #define set_pte_atomic(ptep, pte) \ 50 native_set_pte_atomic(ptep, pte) 51 52 #define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd) 53 54 #ifndef __PAGETABLE_P4D_FOLDED 55 #define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd) 56 #define pgd_clear(pgd) native_pgd_clear(pgd) 57 #endif 58 59 #ifndef set_p4d 60 # define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d) 61 #endif 62 63 #ifndef __PAGETABLE_PUD_FOLDED 64 #define p4d_clear(p4d) native_p4d_clear(p4d) 65 #endif 66 67 #ifndef set_pud 68 # define set_pud(pudp, pud) native_set_pud(pudp, pud) 69 #endif 70 71 #ifndef __PAGETABLE_PUD_FOLDED 72 #define pud_clear(pud) native_pud_clear(pud) 73 #endif 74 75 #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep) 76 #define pmd_clear(pmd) native_pmd_clear(pmd) 77 78 #define pte_update(mm, addr, ptep) do { } while (0) 79 80 #define pgd_val(x) native_pgd_val(x) 81 #define __pgd(x) native_make_pgd(x) 82 83 #ifndef __PAGETABLE_P4D_FOLDED 84 #define p4d_val(x) native_p4d_val(x) 85 #define __p4d(x) native_make_p4d(x) 86 #endif 87 88 #ifndef __PAGETABLE_PUD_FOLDED 89 #define pud_val(x) native_pud_val(x) 90 #define __pud(x) native_make_pud(x) 91 #endif 92 93 #ifndef __PAGETABLE_PMD_FOLDED 94 #define pmd_val(x) native_pmd_val(x) 95 #define __pmd(x) native_make_pmd(x) 96 #endif 97 98 #define pte_val(x) native_pte_val(x) 99 #define __pte(x) native_make_pte(x) 100 101 #define arch_end_context_switch(prev) do {} while(0) 102 103 #endif /* CONFIG_PARAVIRT */ 104 105 /* 106 * The following only work if pte_present() is true. 107 * Undefined behaviour if not.. 108 */ 109 static inline int pte_dirty(pte_t pte) 110 { 111 return pte_flags(pte) & _PAGE_DIRTY; 112 } 113 114 115 static inline u32 read_pkru(void) 116 { 117 if (boot_cpu_has(X86_FEATURE_OSPKE)) 118 return __read_pkru(); 119 return 0; 120 } 121 122 static inline void write_pkru(u32 pkru) 123 { 124 if (boot_cpu_has(X86_FEATURE_OSPKE)) 125 __write_pkru(pkru); 126 } 127 128 static inline int pte_young(pte_t pte) 129 { 130 return pte_flags(pte) & _PAGE_ACCESSED; 131 } 132 133 static inline int pmd_dirty(pmd_t pmd) 134 { 135 return pmd_flags(pmd) & _PAGE_DIRTY; 136 } 137 138 static inline int pmd_young(pmd_t pmd) 139 { 140 return pmd_flags(pmd) & _PAGE_ACCESSED; 141 } 142 143 static inline int pud_dirty(pud_t pud) 144 { 145 return pud_flags(pud) & _PAGE_DIRTY; 146 } 147 148 static inline int pud_young(pud_t pud) 149 { 150 return pud_flags(pud) & _PAGE_ACCESSED; 151 } 152 153 static inline int pte_write(pte_t pte) 154 { 155 return pte_flags(pte) & _PAGE_RW; 156 } 157 158 static inline int pte_huge(pte_t pte) 159 { 160 return pte_flags(pte) & _PAGE_PSE; 161 } 162 163 static inline int pte_global(pte_t pte) 164 { 165 return pte_flags(pte) & _PAGE_GLOBAL; 166 } 167 168 static inline int pte_exec(pte_t pte) 169 { 170 return !(pte_flags(pte) & _PAGE_NX); 171 } 172 173 static inline int pte_special(pte_t pte) 174 { 175 return pte_flags(pte) & _PAGE_SPECIAL; 176 } 177 178 static inline unsigned long pte_pfn(pte_t pte) 179 { 180 return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT; 181 } 182 183 static inline unsigned long pmd_pfn(pmd_t pmd) 184 { 185 return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; 186 } 187 188 static inline unsigned long pud_pfn(pud_t pud) 189 { 190 return (pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT; 191 } 192 193 static inline unsigned long p4d_pfn(p4d_t p4d) 194 { 195 return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT; 196 } 197 198 static inline int p4d_large(p4d_t p4d) 199 { 200 /* No 512 GiB pages yet */ 201 return 0; 202 } 203 204 #define pte_page(pte) pfn_to_page(pte_pfn(pte)) 205 206 static inline int pmd_large(pmd_t pte) 207 { 208 return pmd_flags(pte) & _PAGE_PSE; 209 } 210 211 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 212 static inline int pmd_trans_huge(pmd_t pmd) 213 { 214 return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; 215 } 216 217 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 218 static inline int pud_trans_huge(pud_t pud) 219 { 220 return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; 221 } 222 #endif 223 224 #define has_transparent_hugepage has_transparent_hugepage 225 static inline int has_transparent_hugepage(void) 226 { 227 return boot_cpu_has(X86_FEATURE_PSE); 228 } 229 230 #ifdef __HAVE_ARCH_PTE_DEVMAP 231 static inline int pmd_devmap(pmd_t pmd) 232 { 233 return !!(pmd_val(pmd) & _PAGE_DEVMAP); 234 } 235 236 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD 237 static inline int pud_devmap(pud_t pud) 238 { 239 return !!(pud_val(pud) & _PAGE_DEVMAP); 240 } 241 #else 242 static inline int pud_devmap(pud_t pud) 243 { 244 return 0; 245 } 246 #endif 247 #endif 248 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 249 250 static inline pte_t pte_set_flags(pte_t pte, pteval_t set) 251 { 252 pteval_t v = native_pte_val(pte); 253 254 return native_make_pte(v | set); 255 } 256 257 static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear) 258 { 259 pteval_t v = native_pte_val(pte); 260 261 return native_make_pte(v & ~clear); 262 } 263 264 static inline pte_t pte_mkclean(pte_t pte) 265 { 266 return pte_clear_flags(pte, _PAGE_DIRTY); 267 } 268 269 static inline pte_t pte_mkold(pte_t pte) 270 { 271 return pte_clear_flags(pte, _PAGE_ACCESSED); 272 } 273 274 static inline pte_t pte_wrprotect(pte_t pte) 275 { 276 return pte_clear_flags(pte, _PAGE_RW); 277 } 278 279 static inline pte_t pte_mkexec(pte_t pte) 280 { 281 return pte_clear_flags(pte, _PAGE_NX); 282 } 283 284 static inline pte_t pte_mkdirty(pte_t pte) 285 { 286 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); 287 } 288 289 static inline pte_t pte_mkyoung(pte_t pte) 290 { 291 return pte_set_flags(pte, _PAGE_ACCESSED); 292 } 293 294 static inline pte_t pte_mkwrite(pte_t pte) 295 { 296 return pte_set_flags(pte, _PAGE_RW); 297 } 298 299 static inline pte_t pte_mkhuge(pte_t pte) 300 { 301 return pte_set_flags(pte, _PAGE_PSE); 302 } 303 304 static inline pte_t pte_clrhuge(pte_t pte) 305 { 306 return pte_clear_flags(pte, _PAGE_PSE); 307 } 308 309 static inline pte_t pte_mkglobal(pte_t pte) 310 { 311 return pte_set_flags(pte, _PAGE_GLOBAL); 312 } 313 314 static inline pte_t pte_clrglobal(pte_t pte) 315 { 316 return pte_clear_flags(pte, _PAGE_GLOBAL); 317 } 318 319 static inline pte_t pte_mkspecial(pte_t pte) 320 { 321 return pte_set_flags(pte, _PAGE_SPECIAL); 322 } 323 324 static inline pte_t pte_mkdevmap(pte_t pte) 325 { 326 return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP); 327 } 328 329 static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set) 330 { 331 pmdval_t v = native_pmd_val(pmd); 332 333 return __pmd(v | set); 334 } 335 336 static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear) 337 { 338 pmdval_t v = native_pmd_val(pmd); 339 340 return __pmd(v & ~clear); 341 } 342 343 static inline pmd_t pmd_mkold(pmd_t pmd) 344 { 345 return pmd_clear_flags(pmd, _PAGE_ACCESSED); 346 } 347 348 static inline pmd_t pmd_mkclean(pmd_t pmd) 349 { 350 return pmd_clear_flags(pmd, _PAGE_DIRTY); 351 } 352 353 static inline pmd_t pmd_wrprotect(pmd_t pmd) 354 { 355 return pmd_clear_flags(pmd, _PAGE_RW); 356 } 357 358 static inline pmd_t pmd_mkdirty(pmd_t pmd) 359 { 360 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); 361 } 362 363 static inline pmd_t pmd_mkdevmap(pmd_t pmd) 364 { 365 return pmd_set_flags(pmd, _PAGE_DEVMAP); 366 } 367 368 static inline pmd_t pmd_mkhuge(pmd_t pmd) 369 { 370 return pmd_set_flags(pmd, _PAGE_PSE); 371 } 372 373 static inline pmd_t pmd_mkyoung(pmd_t pmd) 374 { 375 return pmd_set_flags(pmd, _PAGE_ACCESSED); 376 } 377 378 static inline pmd_t pmd_mkwrite(pmd_t pmd) 379 { 380 return pmd_set_flags(pmd, _PAGE_RW); 381 } 382 383 static inline pmd_t pmd_mknotpresent(pmd_t pmd) 384 { 385 return pmd_clear_flags(pmd, _PAGE_PRESENT | _PAGE_PROTNONE); 386 } 387 388 static inline pud_t pud_set_flags(pud_t pud, pudval_t set) 389 { 390 pudval_t v = native_pud_val(pud); 391 392 return __pud(v | set); 393 } 394 395 static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear) 396 { 397 pudval_t v = native_pud_val(pud); 398 399 return __pud(v & ~clear); 400 } 401 402 static inline pud_t pud_mkold(pud_t pud) 403 { 404 return pud_clear_flags(pud, _PAGE_ACCESSED); 405 } 406 407 static inline pud_t pud_mkclean(pud_t pud) 408 { 409 return pud_clear_flags(pud, _PAGE_DIRTY); 410 } 411 412 static inline pud_t pud_wrprotect(pud_t pud) 413 { 414 return pud_clear_flags(pud, _PAGE_RW); 415 } 416 417 static inline pud_t pud_mkdirty(pud_t pud) 418 { 419 return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); 420 } 421 422 static inline pud_t pud_mkdevmap(pud_t pud) 423 { 424 return pud_set_flags(pud, _PAGE_DEVMAP); 425 } 426 427 static inline pud_t pud_mkhuge(pud_t pud) 428 { 429 return pud_set_flags(pud, _PAGE_PSE); 430 } 431 432 static inline pud_t pud_mkyoung(pud_t pud) 433 { 434 return pud_set_flags(pud, _PAGE_ACCESSED); 435 } 436 437 static inline pud_t pud_mkwrite(pud_t pud) 438 { 439 return pud_set_flags(pud, _PAGE_RW); 440 } 441 442 static inline pud_t pud_mknotpresent(pud_t pud) 443 { 444 return pud_clear_flags(pud, _PAGE_PRESENT | _PAGE_PROTNONE); 445 } 446 447 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY 448 static inline int pte_soft_dirty(pte_t pte) 449 { 450 return pte_flags(pte) & _PAGE_SOFT_DIRTY; 451 } 452 453 static inline int pmd_soft_dirty(pmd_t pmd) 454 { 455 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY; 456 } 457 458 static inline int pud_soft_dirty(pud_t pud) 459 { 460 return pud_flags(pud) & _PAGE_SOFT_DIRTY; 461 } 462 463 static inline pte_t pte_mksoft_dirty(pte_t pte) 464 { 465 return pte_set_flags(pte, _PAGE_SOFT_DIRTY); 466 } 467 468 static inline pmd_t pmd_mksoft_dirty(pmd_t pmd) 469 { 470 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY); 471 } 472 473 static inline pud_t pud_mksoft_dirty(pud_t pud) 474 { 475 return pud_set_flags(pud, _PAGE_SOFT_DIRTY); 476 } 477 478 static inline pte_t pte_clear_soft_dirty(pte_t pte) 479 { 480 return pte_clear_flags(pte, _PAGE_SOFT_DIRTY); 481 } 482 483 static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd) 484 { 485 return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY); 486 } 487 488 static inline pud_t pud_clear_soft_dirty(pud_t pud) 489 { 490 return pud_clear_flags(pud, _PAGE_SOFT_DIRTY); 491 } 492 493 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */ 494 495 /* 496 * Mask out unsupported bits in a present pgprot. Non-present pgprots 497 * can use those bits for other purposes, so leave them be. 498 */ 499 static inline pgprotval_t massage_pgprot(pgprot_t pgprot) 500 { 501 pgprotval_t protval = pgprot_val(pgprot); 502 503 if (protval & _PAGE_PRESENT) 504 protval &= __supported_pte_mask; 505 506 return protval; 507 } 508 509 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot) 510 { 511 return __pte(((phys_addr_t)page_nr << PAGE_SHIFT) | 512 massage_pgprot(pgprot)); 513 } 514 515 static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot) 516 { 517 return __pmd(((phys_addr_t)page_nr << PAGE_SHIFT) | 518 massage_pgprot(pgprot)); 519 } 520 521 static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot) 522 { 523 return __pud(((phys_addr_t)page_nr << PAGE_SHIFT) | 524 massage_pgprot(pgprot)); 525 } 526 527 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) 528 { 529 pteval_t val = pte_val(pte); 530 531 /* 532 * Chop off the NX bit (if present), and add the NX portion of 533 * the newprot (if present): 534 */ 535 val &= _PAGE_CHG_MASK; 536 val |= massage_pgprot(newprot) & ~_PAGE_CHG_MASK; 537 538 return __pte(val); 539 } 540 541 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) 542 { 543 pmdval_t val = pmd_val(pmd); 544 545 val &= _HPAGE_CHG_MASK; 546 val |= massage_pgprot(newprot) & ~_HPAGE_CHG_MASK; 547 548 return __pmd(val); 549 } 550 551 /* mprotect needs to preserve PAT bits when updating vm_page_prot */ 552 #define pgprot_modify pgprot_modify 553 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot) 554 { 555 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK; 556 pgprotval_t addbits = pgprot_val(newprot); 557 return __pgprot(preservebits | addbits); 558 } 559 560 #define pte_pgprot(x) __pgprot(pte_flags(x)) 561 #define pmd_pgprot(x) __pgprot(pmd_flags(x)) 562 #define pud_pgprot(x) __pgprot(pud_flags(x)) 563 #define p4d_pgprot(x) __pgprot(p4d_flags(x)) 564 565 #define canon_pgprot(p) __pgprot(massage_pgprot(p)) 566 567 static inline int is_new_memtype_allowed(u64 paddr, unsigned long size, 568 enum page_cache_mode pcm, 569 enum page_cache_mode new_pcm) 570 { 571 /* 572 * PAT type is always WB for untracked ranges, so no need to check. 573 */ 574 if (x86_platform.is_untracked_pat_range(paddr, paddr + size)) 575 return 1; 576 577 /* 578 * Certain new memtypes are not allowed with certain 579 * requested memtype: 580 * - request is uncached, return cannot be write-back 581 * - request is write-combine, return cannot be write-back 582 * - request is write-through, return cannot be write-back 583 * - request is write-through, return cannot be write-combine 584 */ 585 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS && 586 new_pcm == _PAGE_CACHE_MODE_WB) || 587 (pcm == _PAGE_CACHE_MODE_WC && 588 new_pcm == _PAGE_CACHE_MODE_WB) || 589 (pcm == _PAGE_CACHE_MODE_WT && 590 new_pcm == _PAGE_CACHE_MODE_WB) || 591 (pcm == _PAGE_CACHE_MODE_WT && 592 new_pcm == _PAGE_CACHE_MODE_WC)) { 593 return 0; 594 } 595 596 return 1; 597 } 598 599 pmd_t *populate_extra_pmd(unsigned long vaddr); 600 pte_t *populate_extra_pte(unsigned long vaddr); 601 #endif /* __ASSEMBLY__ */ 602 603 #ifdef CONFIG_X86_32 604 # include <asm/pgtable_32.h> 605 #else 606 # include <asm/pgtable_64.h> 607 #endif 608 609 #ifndef __ASSEMBLY__ 610 #include <linux/mm_types.h> 611 #include <linux/mmdebug.h> 612 #include <linux/log2.h> 613 #include <asm/fixmap.h> 614 615 static inline int pte_none(pte_t pte) 616 { 617 return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK)); 618 } 619 620 #define __HAVE_ARCH_PTE_SAME 621 static inline int pte_same(pte_t a, pte_t b) 622 { 623 return a.pte == b.pte; 624 } 625 626 static inline int pte_present(pte_t a) 627 { 628 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE); 629 } 630 631 #ifdef __HAVE_ARCH_PTE_DEVMAP 632 static inline int pte_devmap(pte_t a) 633 { 634 return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP; 635 } 636 #endif 637 638 #define pte_accessible pte_accessible 639 static inline bool pte_accessible(struct mm_struct *mm, pte_t a) 640 { 641 if (pte_flags(a) & _PAGE_PRESENT) 642 return true; 643 644 if ((pte_flags(a) & _PAGE_PROTNONE) && 645 mm_tlb_flush_pending(mm)) 646 return true; 647 648 return false; 649 } 650 651 static inline int pte_hidden(pte_t pte) 652 { 653 return pte_flags(pte) & _PAGE_HIDDEN; 654 } 655 656 static inline int pmd_present(pmd_t pmd) 657 { 658 /* 659 * Checking for _PAGE_PSE is needed too because 660 * split_huge_page will temporarily clear the present bit (but 661 * the _PAGE_PSE flag will remain set at all times while the 662 * _PAGE_PRESENT bit is clear). 663 */ 664 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE); 665 } 666 667 #ifdef CONFIG_NUMA_BALANCING 668 /* 669 * These work without NUMA balancing but the kernel does not care. See the 670 * comment in include/asm-generic/pgtable.h 671 */ 672 static inline int pte_protnone(pte_t pte) 673 { 674 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT)) 675 == _PAGE_PROTNONE; 676 } 677 678 static inline int pmd_protnone(pmd_t pmd) 679 { 680 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT)) 681 == _PAGE_PROTNONE; 682 } 683 #endif /* CONFIG_NUMA_BALANCING */ 684 685 static inline int pmd_none(pmd_t pmd) 686 { 687 /* Only check low word on 32-bit platforms, since it might be 688 out of sync with upper half. */ 689 unsigned long val = native_pmd_val(pmd); 690 return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0; 691 } 692 693 static inline unsigned long pmd_page_vaddr(pmd_t pmd) 694 { 695 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd)); 696 } 697 698 /* 699 * Currently stuck as a macro due to indirect forward reference to 700 * linux/mmzone.h's __section_mem_map_addr() definition: 701 */ 702 #define pmd_page(pmd) \ 703 pfn_to_page((pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT) 704 705 /* 706 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD] 707 * 708 * this macro returns the index of the entry in the pmd page which would 709 * control the given virtual address 710 */ 711 static inline unsigned long pmd_index(unsigned long address) 712 { 713 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1); 714 } 715 716 /* 717 * Conversion functions: convert a page and protection to a page entry, 718 * and a page entry and page directory to the page they refer to. 719 * 720 * (Currently stuck as a macro because of indirect forward reference 721 * to linux/mm.h:page_to_nid()) 722 */ 723 #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot)) 724 725 /* 726 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE] 727 * 728 * this function returns the index of the entry in the pte page which would 729 * control the given virtual address 730 */ 731 static inline unsigned long pte_index(unsigned long address) 732 { 733 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1); 734 } 735 736 static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address) 737 { 738 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address); 739 } 740 741 static inline int pmd_bad(pmd_t pmd) 742 { 743 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE; 744 } 745 746 static inline unsigned long pages_to_mb(unsigned long npg) 747 { 748 return npg >> (20 - PAGE_SHIFT); 749 } 750 751 #if CONFIG_PGTABLE_LEVELS > 2 752 static inline int pud_none(pud_t pud) 753 { 754 return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0; 755 } 756 757 static inline int pud_present(pud_t pud) 758 { 759 return pud_flags(pud) & _PAGE_PRESENT; 760 } 761 762 static inline unsigned long pud_page_vaddr(pud_t pud) 763 { 764 return (unsigned long)__va(pud_val(pud) & pud_pfn_mask(pud)); 765 } 766 767 /* 768 * Currently stuck as a macro due to indirect forward reference to 769 * linux/mmzone.h's __section_mem_map_addr() definition: 770 */ 771 #define pud_page(pud) \ 772 pfn_to_page((pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT) 773 774 /* Find an entry in the second-level page table.. */ 775 static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address) 776 { 777 return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address); 778 } 779 780 static inline int pud_large(pud_t pud) 781 { 782 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) == 783 (_PAGE_PSE | _PAGE_PRESENT); 784 } 785 786 static inline int pud_bad(pud_t pud) 787 { 788 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0; 789 } 790 #else 791 static inline int pud_large(pud_t pud) 792 { 793 return 0; 794 } 795 #endif /* CONFIG_PGTABLE_LEVELS > 2 */ 796 797 static inline unsigned long pud_index(unsigned long address) 798 { 799 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1); 800 } 801 802 #if CONFIG_PGTABLE_LEVELS > 3 803 static inline int p4d_none(p4d_t p4d) 804 { 805 return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0; 806 } 807 808 static inline int p4d_present(p4d_t p4d) 809 { 810 return p4d_flags(p4d) & _PAGE_PRESENT; 811 } 812 813 static inline unsigned long p4d_page_vaddr(p4d_t p4d) 814 { 815 return (unsigned long)__va(p4d_val(p4d) & p4d_pfn_mask(p4d)); 816 } 817 818 /* 819 * Currently stuck as a macro due to indirect forward reference to 820 * linux/mmzone.h's __section_mem_map_addr() definition: 821 */ 822 #define p4d_page(p4d) \ 823 pfn_to_page((p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT) 824 825 /* Find an entry in the third-level page table.. */ 826 static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address) 827 { 828 return (pud_t *)p4d_page_vaddr(*p4d) + pud_index(address); 829 } 830 831 static inline int p4d_bad(p4d_t p4d) 832 { 833 return (p4d_flags(p4d) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0; 834 } 835 #endif /* CONFIG_PGTABLE_LEVELS > 3 */ 836 837 static inline unsigned long p4d_index(unsigned long address) 838 { 839 return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1); 840 } 841 842 #if CONFIG_PGTABLE_LEVELS > 4 843 static inline int pgd_present(pgd_t pgd) 844 { 845 return pgd_flags(pgd) & _PAGE_PRESENT; 846 } 847 848 static inline unsigned long pgd_page_vaddr(pgd_t pgd) 849 { 850 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK); 851 } 852 853 /* 854 * Currently stuck as a macro due to indirect forward reference to 855 * linux/mmzone.h's __section_mem_map_addr() definition: 856 */ 857 #define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT) 858 859 /* to find an entry in a page-table-directory. */ 860 static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address) 861 { 862 return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address); 863 } 864 865 static inline int pgd_bad(pgd_t pgd) 866 { 867 return (pgd_flags(pgd) & ~_PAGE_USER) != _KERNPG_TABLE; 868 } 869 870 static inline int pgd_none(pgd_t pgd) 871 { 872 /* 873 * There is no need to do a workaround for the KNL stray 874 * A/D bit erratum here. PGDs only point to page tables 875 * except on 32-bit non-PAE which is not supported on 876 * KNL. 877 */ 878 return !native_pgd_val(pgd); 879 } 880 #endif /* CONFIG_PGTABLE_LEVELS > 4 */ 881 882 #endif /* __ASSEMBLY__ */ 883 884 /* 885 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD] 886 * 887 * this macro returns the index of the entry in the pgd page which would 888 * control the given virtual address 889 */ 890 #define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1)) 891 892 /* 893 * pgd_offset() returns a (pgd_t *) 894 * pgd_index() is used get the offset into the pgd page's array of pgd_t's; 895 */ 896 #define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address))) 897 /* 898 * a shortcut which implies the use of the kernel's pgd, instead 899 * of a process's 900 */ 901 #define pgd_offset_k(address) pgd_offset(&init_mm, (address)) 902 903 904 #define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET) 905 #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY) 906 907 #ifndef __ASSEMBLY__ 908 909 extern int direct_gbpages; 910 void init_mem_mapping(void); 911 void early_alloc_pgt_buf(void); 912 extern void memblock_find_dma_reserve(void); 913 914 #ifdef CONFIG_X86_64 915 /* Realmode trampoline initialization. */ 916 extern pgd_t trampoline_pgd_entry; 917 static inline void __meminit init_trampoline_default(void) 918 { 919 /* Default trampoline pgd value */ 920 trampoline_pgd_entry = init_level4_pgt[pgd_index(__PAGE_OFFSET)]; 921 } 922 # ifdef CONFIG_RANDOMIZE_MEMORY 923 void __meminit init_trampoline(void); 924 # else 925 # define init_trampoline init_trampoline_default 926 # endif 927 #else 928 static inline void init_trampoline(void) { } 929 #endif 930 931 /* local pte updates need not use xchg for locking */ 932 static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep) 933 { 934 pte_t res = *ptep; 935 936 /* Pure native function needs no input for mm, addr */ 937 native_pte_clear(NULL, 0, ptep); 938 return res; 939 } 940 941 static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp) 942 { 943 pmd_t res = *pmdp; 944 945 native_pmd_clear(pmdp); 946 return res; 947 } 948 949 static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp) 950 { 951 pud_t res = *pudp; 952 953 native_pud_clear(pudp); 954 return res; 955 } 956 957 static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr, 958 pte_t *ptep , pte_t pte) 959 { 960 native_set_pte(ptep, pte); 961 } 962 963 static inline void native_set_pmd_at(struct mm_struct *mm, unsigned long addr, 964 pmd_t *pmdp , pmd_t pmd) 965 { 966 native_set_pmd(pmdp, pmd); 967 } 968 969 static inline void native_set_pud_at(struct mm_struct *mm, unsigned long addr, 970 pud_t *pudp, pud_t pud) 971 { 972 native_set_pud(pudp, pud); 973 } 974 975 #ifndef CONFIG_PARAVIRT 976 /* 977 * Rules for using pte_update - it must be called after any PTE update which 978 * has not been done using the set_pte / clear_pte interfaces. It is used by 979 * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE 980 * updates should either be sets, clears, or set_pte_atomic for P->P 981 * transitions, which means this hook should only be called for user PTEs. 982 * This hook implies a P->P protection or access change has taken place, which 983 * requires a subsequent TLB flush. 984 */ 985 #define pte_update(mm, addr, ptep) do { } while (0) 986 #endif 987 988 /* 989 * We only update the dirty/accessed state if we set 990 * the dirty bit by hand in the kernel, since the hardware 991 * will do the accessed bit for us, and we don't want to 992 * race with other CPU's that might be updating the dirty 993 * bit at the same time. 994 */ 995 struct vm_area_struct; 996 997 #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS 998 extern int ptep_set_access_flags(struct vm_area_struct *vma, 999 unsigned long address, pte_t *ptep, 1000 pte_t entry, int dirty); 1001 1002 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG 1003 extern int ptep_test_and_clear_young(struct vm_area_struct *vma, 1004 unsigned long addr, pte_t *ptep); 1005 1006 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH 1007 extern int ptep_clear_flush_young(struct vm_area_struct *vma, 1008 unsigned long address, pte_t *ptep); 1009 1010 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR 1011 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, 1012 pte_t *ptep) 1013 { 1014 pte_t pte = native_ptep_get_and_clear(ptep); 1015 pte_update(mm, addr, ptep); 1016 return pte; 1017 } 1018 1019 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL 1020 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, 1021 unsigned long addr, pte_t *ptep, 1022 int full) 1023 { 1024 pte_t pte; 1025 if (full) { 1026 /* 1027 * Full address destruction in progress; paravirt does not 1028 * care about updates and native needs no locking 1029 */ 1030 pte = native_local_ptep_get_and_clear(ptep); 1031 } else { 1032 pte = ptep_get_and_clear(mm, addr, ptep); 1033 } 1034 return pte; 1035 } 1036 1037 #define __HAVE_ARCH_PTEP_SET_WRPROTECT 1038 static inline void ptep_set_wrprotect(struct mm_struct *mm, 1039 unsigned long addr, pte_t *ptep) 1040 { 1041 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); 1042 pte_update(mm, addr, ptep); 1043 } 1044 1045 #define flush_tlb_fix_spurious_fault(vma, address) do { } while (0) 1046 1047 #define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot)) 1048 1049 #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS 1050 extern int pmdp_set_access_flags(struct vm_area_struct *vma, 1051 unsigned long address, pmd_t *pmdp, 1052 pmd_t entry, int dirty); 1053 extern int pudp_set_access_flags(struct vm_area_struct *vma, 1054 unsigned long address, pud_t *pudp, 1055 pud_t entry, int dirty); 1056 1057 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG 1058 extern int pmdp_test_and_clear_young(struct vm_area_struct *vma, 1059 unsigned long addr, pmd_t *pmdp); 1060 extern int pudp_test_and_clear_young(struct vm_area_struct *vma, 1061 unsigned long addr, pud_t *pudp); 1062 1063 #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH 1064 extern int pmdp_clear_flush_young(struct vm_area_struct *vma, 1065 unsigned long address, pmd_t *pmdp); 1066 1067 1068 #define __HAVE_ARCH_PMD_WRITE 1069 static inline int pmd_write(pmd_t pmd) 1070 { 1071 return pmd_flags(pmd) & _PAGE_RW; 1072 } 1073 1074 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR 1075 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr, 1076 pmd_t *pmdp) 1077 { 1078 return native_pmdp_get_and_clear(pmdp); 1079 } 1080 1081 #define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR 1082 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, 1083 unsigned long addr, pud_t *pudp) 1084 { 1085 return native_pudp_get_and_clear(pudp); 1086 } 1087 1088 #define __HAVE_ARCH_PMDP_SET_WRPROTECT 1089 static inline void pmdp_set_wrprotect(struct mm_struct *mm, 1090 unsigned long addr, pmd_t *pmdp) 1091 { 1092 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); 1093 } 1094 1095 /* 1096 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count); 1097 * 1098 * dst - pointer to pgd range anwhere on a pgd page 1099 * src - "" 1100 * count - the number of pgds to copy. 1101 * 1102 * dst and src can be on the same page, but the range must not overlap, 1103 * and must not cross a page boundary. 1104 */ 1105 static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count) 1106 { 1107 memcpy(dst, src, count * sizeof(pgd_t)); 1108 } 1109 1110 #define PTE_SHIFT ilog2(PTRS_PER_PTE) 1111 static inline int page_level_shift(enum pg_level level) 1112 { 1113 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT; 1114 } 1115 static inline unsigned long page_level_size(enum pg_level level) 1116 { 1117 return 1UL << page_level_shift(level); 1118 } 1119 static inline unsigned long page_level_mask(enum pg_level level) 1120 { 1121 return ~(page_level_size(level) - 1); 1122 } 1123 1124 /* 1125 * The x86 doesn't have any external MMU info: the kernel page 1126 * tables contain all the necessary information. 1127 */ 1128 static inline void update_mmu_cache(struct vm_area_struct *vma, 1129 unsigned long addr, pte_t *ptep) 1130 { 1131 } 1132 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma, 1133 unsigned long addr, pmd_t *pmd) 1134 { 1135 } 1136 static inline void update_mmu_cache_pud(struct vm_area_struct *vma, 1137 unsigned long addr, pud_t *pud) 1138 { 1139 } 1140 1141 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY 1142 static inline pte_t pte_swp_mksoft_dirty(pte_t pte) 1143 { 1144 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY); 1145 } 1146 1147 static inline int pte_swp_soft_dirty(pte_t pte) 1148 { 1149 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY; 1150 } 1151 1152 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte) 1153 { 1154 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY); 1155 } 1156 #endif 1157 1158 #define PKRU_AD_BIT 0x1 1159 #define PKRU_WD_BIT 0x2 1160 #define PKRU_BITS_PER_PKEY 2 1161 1162 static inline bool __pkru_allows_read(u32 pkru, u16 pkey) 1163 { 1164 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY; 1165 return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits)); 1166 } 1167 1168 static inline bool __pkru_allows_write(u32 pkru, u16 pkey) 1169 { 1170 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY; 1171 /* 1172 * Access-disable disables writes too so we need to check 1173 * both bits here. 1174 */ 1175 return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits)); 1176 } 1177 1178 static inline u16 pte_flags_pkey(unsigned long pte_flags) 1179 { 1180 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS 1181 /* ifdef to avoid doing 59-bit shift on 32-bit values */ 1182 return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0; 1183 #else 1184 return 0; 1185 #endif 1186 } 1187 1188 #include <asm-generic/pgtable.h> 1189 #endif /* __ASSEMBLY__ */ 1190 1191 #endif /* _ASM_X86_PGTABLE_H */ 1192