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