WIP FPC-III support
[linux/fpc-iii.git] / arch / x86 / include / asm / pgtable.h
bloba02c67291cfcbdf69048030323e1c4d4f2aee907
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PGTABLE_H
3 #define _ASM_X86_PGTABLE_H
5 #include <linux/mem_encrypt.h>
6 #include <asm/page.h>
7 #include <asm/pgtable_types.h>
9 /*
10 * Macro to mark a page protection value as UC-
12 #define pgprot_noncached(prot) \
13 ((boot_cpu_data.x86 > 3) \
14 ? (__pgprot(pgprot_val(prot) | \
15 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
16 : (prot))
19 * Macros to add or remove encryption attribute
21 #define pgprot_encrypted(prot) __pgprot(__sme_set(pgprot_val(prot)))
22 #define pgprot_decrypted(prot) __pgprot(__sme_clr(pgprot_val(prot)))
24 #ifndef __ASSEMBLY__
25 #include <asm/x86_init.h>
26 #include <asm/fpu/xstate.h>
27 #include <asm/fpu/api.h>
28 #include <asm-generic/pgtable_uffd.h>
30 extern pgd_t early_top_pgt[PTRS_PER_PGD];
31 bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd);
33 void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);
34 void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
35 bool user);
36 void ptdump_walk_pgd_level_checkwx(void);
37 void ptdump_walk_user_pgd_level_checkwx(void);
39 #ifdef CONFIG_DEBUG_WX
40 #define debug_checkwx() ptdump_walk_pgd_level_checkwx()
41 #define debug_checkwx_user() ptdump_walk_user_pgd_level_checkwx()
42 #else
43 #define debug_checkwx() do { } while (0)
44 #define debug_checkwx_user() do { } while (0)
45 #endif
48 * ZERO_PAGE is a global shared page that is always zero: used
49 * for zero-mapped memory areas etc..
51 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
52 __visible;
53 #define ZERO_PAGE(vaddr) ((void)(vaddr),virt_to_page(empty_zero_page))
55 extern spinlock_t pgd_lock;
56 extern struct list_head pgd_list;
58 extern struct mm_struct *pgd_page_get_mm(struct page *page);
60 extern pmdval_t early_pmd_flags;
62 #ifdef CONFIG_PARAVIRT_XXL
63 #include <asm/paravirt.h>
64 #else /* !CONFIG_PARAVIRT_XXL */
65 #define set_pte(ptep, pte) native_set_pte(ptep, pte)
67 #define set_pte_atomic(ptep, pte) \
68 native_set_pte_atomic(ptep, pte)
70 #define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
72 #ifndef __PAGETABLE_P4D_FOLDED
73 #define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
74 #define pgd_clear(pgd) (pgtable_l5_enabled() ? native_pgd_clear(pgd) : 0)
75 #endif
77 #ifndef set_p4d
78 # define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d)
79 #endif
81 #ifndef __PAGETABLE_PUD_FOLDED
82 #define p4d_clear(p4d) native_p4d_clear(p4d)
83 #endif
85 #ifndef set_pud
86 # define set_pud(pudp, pud) native_set_pud(pudp, pud)
87 #endif
89 #ifndef __PAGETABLE_PUD_FOLDED
90 #define pud_clear(pud) native_pud_clear(pud)
91 #endif
93 #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
94 #define pmd_clear(pmd) native_pmd_clear(pmd)
96 #define pgd_val(x) native_pgd_val(x)
97 #define __pgd(x) native_make_pgd(x)
99 #ifndef __PAGETABLE_P4D_FOLDED
100 #define p4d_val(x) native_p4d_val(x)
101 #define __p4d(x) native_make_p4d(x)
102 #endif
104 #ifndef __PAGETABLE_PUD_FOLDED
105 #define pud_val(x) native_pud_val(x)
106 #define __pud(x) native_make_pud(x)
107 #endif
109 #ifndef __PAGETABLE_PMD_FOLDED
110 #define pmd_val(x) native_pmd_val(x)
111 #define __pmd(x) native_make_pmd(x)
112 #endif
114 #define pte_val(x) native_pte_val(x)
115 #define __pte(x) native_make_pte(x)
117 #define arch_end_context_switch(prev) do {} while(0)
118 #endif /* CONFIG_PARAVIRT_XXL */
121 * The following only work if pte_present() is true.
122 * Undefined behaviour if not..
124 static inline int pte_dirty(pte_t pte)
126 return pte_flags(pte) & _PAGE_DIRTY;
130 static inline u32 read_pkru(void)
132 if (boot_cpu_has(X86_FEATURE_OSPKE))
133 return rdpkru();
134 return 0;
137 static inline void write_pkru(u32 pkru)
139 struct pkru_state *pk;
141 if (!boot_cpu_has(X86_FEATURE_OSPKE))
142 return;
144 pk = get_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
147 * The PKRU value in xstate needs to be in sync with the value that is
148 * written to the CPU. The FPU restore on return to userland would
149 * otherwise load the previous value again.
151 fpregs_lock();
152 if (pk)
153 pk->pkru = pkru;
154 __write_pkru(pkru);
155 fpregs_unlock();
158 static inline int pte_young(pte_t pte)
160 return pte_flags(pte) & _PAGE_ACCESSED;
163 static inline int pmd_dirty(pmd_t pmd)
165 return pmd_flags(pmd) & _PAGE_DIRTY;
168 static inline int pmd_young(pmd_t pmd)
170 return pmd_flags(pmd) & _PAGE_ACCESSED;
173 static inline int pud_dirty(pud_t pud)
175 return pud_flags(pud) & _PAGE_DIRTY;
178 static inline int pud_young(pud_t pud)
180 return pud_flags(pud) & _PAGE_ACCESSED;
183 static inline int pte_write(pte_t pte)
185 return pte_flags(pte) & _PAGE_RW;
188 static inline int pte_huge(pte_t pte)
190 return pte_flags(pte) & _PAGE_PSE;
193 static inline int pte_global(pte_t pte)
195 return pte_flags(pte) & _PAGE_GLOBAL;
198 static inline int pte_exec(pte_t pte)
200 return !(pte_flags(pte) & _PAGE_NX);
203 static inline int pte_special(pte_t pte)
205 return pte_flags(pte) & _PAGE_SPECIAL;
208 /* Entries that were set to PROT_NONE are inverted */
210 static inline u64 protnone_mask(u64 val);
212 static inline unsigned long pte_pfn(pte_t pte)
214 phys_addr_t pfn = pte_val(pte);
215 pfn ^= protnone_mask(pfn);
216 return (pfn & PTE_PFN_MASK) >> PAGE_SHIFT;
219 static inline unsigned long pmd_pfn(pmd_t pmd)
221 phys_addr_t pfn = pmd_val(pmd);
222 pfn ^= protnone_mask(pfn);
223 return (pfn & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
226 static inline unsigned long pud_pfn(pud_t pud)
228 phys_addr_t pfn = pud_val(pud);
229 pfn ^= protnone_mask(pfn);
230 return (pfn & pud_pfn_mask(pud)) >> PAGE_SHIFT;
233 static inline unsigned long p4d_pfn(p4d_t p4d)
235 return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT;
238 static inline unsigned long pgd_pfn(pgd_t pgd)
240 return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
243 #define p4d_leaf p4d_large
244 static inline int p4d_large(p4d_t p4d)
246 /* No 512 GiB pages yet */
247 return 0;
250 #define pte_page(pte) pfn_to_page(pte_pfn(pte))
252 #define pmd_leaf pmd_large
253 static inline int pmd_large(pmd_t pte)
255 return pmd_flags(pte) & _PAGE_PSE;
258 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
259 /* NOTE: when predicate huge page, consider also pmd_devmap, or use pmd_large */
260 static inline int pmd_trans_huge(pmd_t pmd)
262 return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
265 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
266 static inline int pud_trans_huge(pud_t pud)
268 return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
270 #endif
272 #define has_transparent_hugepage has_transparent_hugepage
273 static inline int has_transparent_hugepage(void)
275 return boot_cpu_has(X86_FEATURE_PSE);
278 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
279 static inline int pmd_devmap(pmd_t pmd)
281 return !!(pmd_val(pmd) & _PAGE_DEVMAP);
284 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
285 static inline int pud_devmap(pud_t pud)
287 return !!(pud_val(pud) & _PAGE_DEVMAP);
289 #else
290 static inline int pud_devmap(pud_t pud)
292 return 0;
294 #endif
296 static inline int pgd_devmap(pgd_t pgd)
298 return 0;
300 #endif
301 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
303 static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
305 pteval_t v = native_pte_val(pte);
307 return native_make_pte(v | set);
310 static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
312 pteval_t v = native_pte_val(pte);
314 return native_make_pte(v & ~clear);
317 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
318 static inline int pte_uffd_wp(pte_t pte)
320 return pte_flags(pte) & _PAGE_UFFD_WP;
323 static inline pte_t pte_mkuffd_wp(pte_t pte)
325 return pte_set_flags(pte, _PAGE_UFFD_WP);
328 static inline pte_t pte_clear_uffd_wp(pte_t pte)
330 return pte_clear_flags(pte, _PAGE_UFFD_WP);
332 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
334 static inline pte_t pte_mkclean(pte_t pte)
336 return pte_clear_flags(pte, _PAGE_DIRTY);
339 static inline pte_t pte_mkold(pte_t pte)
341 return pte_clear_flags(pte, _PAGE_ACCESSED);
344 static inline pte_t pte_wrprotect(pte_t pte)
346 return pte_clear_flags(pte, _PAGE_RW);
349 static inline pte_t pte_mkexec(pte_t pte)
351 return pte_clear_flags(pte, _PAGE_NX);
354 static inline pte_t pte_mkdirty(pte_t pte)
356 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
359 static inline pte_t pte_mkyoung(pte_t pte)
361 return pte_set_flags(pte, _PAGE_ACCESSED);
364 static inline pte_t pte_mkwrite(pte_t pte)
366 return pte_set_flags(pte, _PAGE_RW);
369 static inline pte_t pte_mkhuge(pte_t pte)
371 return pte_set_flags(pte, _PAGE_PSE);
374 static inline pte_t pte_clrhuge(pte_t pte)
376 return pte_clear_flags(pte, _PAGE_PSE);
379 static inline pte_t pte_mkglobal(pte_t pte)
381 return pte_set_flags(pte, _PAGE_GLOBAL);
384 static inline pte_t pte_clrglobal(pte_t pte)
386 return pte_clear_flags(pte, _PAGE_GLOBAL);
389 static inline pte_t pte_mkspecial(pte_t pte)
391 return pte_set_flags(pte, _PAGE_SPECIAL);
394 static inline pte_t pte_mkdevmap(pte_t pte)
396 return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP);
399 static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
401 pmdval_t v = native_pmd_val(pmd);
403 return native_make_pmd(v | set);
406 static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
408 pmdval_t v = native_pmd_val(pmd);
410 return native_make_pmd(v & ~clear);
413 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
414 static inline int pmd_uffd_wp(pmd_t pmd)
416 return pmd_flags(pmd) & _PAGE_UFFD_WP;
419 static inline pmd_t pmd_mkuffd_wp(pmd_t pmd)
421 return pmd_set_flags(pmd, _PAGE_UFFD_WP);
424 static inline pmd_t pmd_clear_uffd_wp(pmd_t pmd)
426 return pmd_clear_flags(pmd, _PAGE_UFFD_WP);
428 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
430 static inline pmd_t pmd_mkold(pmd_t pmd)
432 return pmd_clear_flags(pmd, _PAGE_ACCESSED);
435 static inline pmd_t pmd_mkclean(pmd_t pmd)
437 return pmd_clear_flags(pmd, _PAGE_DIRTY);
440 static inline pmd_t pmd_wrprotect(pmd_t pmd)
442 return pmd_clear_flags(pmd, _PAGE_RW);
445 static inline pmd_t pmd_mkdirty(pmd_t pmd)
447 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
450 static inline pmd_t pmd_mkdevmap(pmd_t pmd)
452 return pmd_set_flags(pmd, _PAGE_DEVMAP);
455 static inline pmd_t pmd_mkhuge(pmd_t pmd)
457 return pmd_set_flags(pmd, _PAGE_PSE);
460 static inline pmd_t pmd_mkyoung(pmd_t pmd)
462 return pmd_set_flags(pmd, _PAGE_ACCESSED);
465 static inline pmd_t pmd_mkwrite(pmd_t pmd)
467 return pmd_set_flags(pmd, _PAGE_RW);
470 static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
472 pudval_t v = native_pud_val(pud);
474 return native_make_pud(v | set);
477 static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
479 pudval_t v = native_pud_val(pud);
481 return native_make_pud(v & ~clear);
484 static inline pud_t pud_mkold(pud_t pud)
486 return pud_clear_flags(pud, _PAGE_ACCESSED);
489 static inline pud_t pud_mkclean(pud_t pud)
491 return pud_clear_flags(pud, _PAGE_DIRTY);
494 static inline pud_t pud_wrprotect(pud_t pud)
496 return pud_clear_flags(pud, _PAGE_RW);
499 static inline pud_t pud_mkdirty(pud_t pud)
501 return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
504 static inline pud_t pud_mkdevmap(pud_t pud)
506 return pud_set_flags(pud, _PAGE_DEVMAP);
509 static inline pud_t pud_mkhuge(pud_t pud)
511 return pud_set_flags(pud, _PAGE_PSE);
514 static inline pud_t pud_mkyoung(pud_t pud)
516 return pud_set_flags(pud, _PAGE_ACCESSED);
519 static inline pud_t pud_mkwrite(pud_t pud)
521 return pud_set_flags(pud, _PAGE_RW);
524 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
525 static inline int pte_soft_dirty(pte_t pte)
527 return pte_flags(pte) & _PAGE_SOFT_DIRTY;
530 static inline int pmd_soft_dirty(pmd_t pmd)
532 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
535 static inline int pud_soft_dirty(pud_t pud)
537 return pud_flags(pud) & _PAGE_SOFT_DIRTY;
540 static inline pte_t pte_mksoft_dirty(pte_t pte)
542 return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
545 static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
547 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
550 static inline pud_t pud_mksoft_dirty(pud_t pud)
552 return pud_set_flags(pud, _PAGE_SOFT_DIRTY);
555 static inline pte_t pte_clear_soft_dirty(pte_t pte)
557 return pte_clear_flags(pte, _PAGE_SOFT_DIRTY);
560 static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
562 return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
565 static inline pud_t pud_clear_soft_dirty(pud_t pud)
567 return pud_clear_flags(pud, _PAGE_SOFT_DIRTY);
570 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
573 * Mask out unsupported bits in a present pgprot. Non-present pgprots
574 * can use those bits for other purposes, so leave them be.
576 static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
578 pgprotval_t protval = pgprot_val(pgprot);
580 if (protval & _PAGE_PRESENT)
581 protval &= __supported_pte_mask;
583 return protval;
586 static inline pgprotval_t check_pgprot(pgprot_t pgprot)
588 pgprotval_t massaged_val = massage_pgprot(pgprot);
590 /* mmdebug.h can not be included here because of dependencies */
591 #ifdef CONFIG_DEBUG_VM
592 WARN_ONCE(pgprot_val(pgprot) != massaged_val,
593 "attempted to set unsupported pgprot: %016llx "
594 "bits: %016llx supported: %016llx\n",
595 (u64)pgprot_val(pgprot),
596 (u64)pgprot_val(pgprot) ^ massaged_val,
597 (u64)__supported_pte_mask);
598 #endif
600 return massaged_val;
603 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
605 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
606 pfn ^= protnone_mask(pgprot_val(pgprot));
607 pfn &= PTE_PFN_MASK;
608 return __pte(pfn | check_pgprot(pgprot));
611 static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
613 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
614 pfn ^= protnone_mask(pgprot_val(pgprot));
615 pfn &= PHYSICAL_PMD_PAGE_MASK;
616 return __pmd(pfn | check_pgprot(pgprot));
619 static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
621 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
622 pfn ^= protnone_mask(pgprot_val(pgprot));
623 pfn &= PHYSICAL_PUD_PAGE_MASK;
624 return __pud(pfn | check_pgprot(pgprot));
627 static inline pmd_t pmd_mkinvalid(pmd_t pmd)
629 return pfn_pmd(pmd_pfn(pmd),
630 __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE)));
633 static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);
635 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
637 pteval_t val = pte_val(pte), oldval = val;
640 * Chop off the NX bit (if present), and add the NX portion of
641 * the newprot (if present):
643 val &= _PAGE_CHG_MASK;
644 val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK;
645 val = flip_protnone_guard(oldval, val, PTE_PFN_MASK);
646 return __pte(val);
649 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
651 pmdval_t val = pmd_val(pmd), oldval = val;
653 val &= _HPAGE_CHG_MASK;
654 val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK;
655 val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK);
656 return __pmd(val);
660 * mprotect needs to preserve PAT and encryption bits when updating
661 * vm_page_prot
663 #define pgprot_modify pgprot_modify
664 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
666 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
667 pgprotval_t addbits = pgprot_val(newprot) & ~_PAGE_CHG_MASK;
668 return __pgprot(preservebits | addbits);
671 #define pte_pgprot(x) __pgprot(pte_flags(x))
672 #define pmd_pgprot(x) __pgprot(pmd_flags(x))
673 #define pud_pgprot(x) __pgprot(pud_flags(x))
674 #define p4d_pgprot(x) __pgprot(p4d_flags(x))
676 #define canon_pgprot(p) __pgprot(massage_pgprot(p))
678 static inline pgprot_t arch_filter_pgprot(pgprot_t prot)
680 return canon_pgprot(prot);
683 static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
684 enum page_cache_mode pcm,
685 enum page_cache_mode new_pcm)
688 * PAT type is always WB for untracked ranges, so no need to check.
690 if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
691 return 1;
694 * Certain new memtypes are not allowed with certain
695 * requested memtype:
696 * - request is uncached, return cannot be write-back
697 * - request is write-combine, return cannot be write-back
698 * - request is write-through, return cannot be write-back
699 * - request is write-through, return cannot be write-combine
701 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
702 new_pcm == _PAGE_CACHE_MODE_WB) ||
703 (pcm == _PAGE_CACHE_MODE_WC &&
704 new_pcm == _PAGE_CACHE_MODE_WB) ||
705 (pcm == _PAGE_CACHE_MODE_WT &&
706 new_pcm == _PAGE_CACHE_MODE_WB) ||
707 (pcm == _PAGE_CACHE_MODE_WT &&
708 new_pcm == _PAGE_CACHE_MODE_WC)) {
709 return 0;
712 return 1;
715 pmd_t *populate_extra_pmd(unsigned long vaddr);
716 pte_t *populate_extra_pte(unsigned long vaddr);
718 #ifdef CONFIG_PAGE_TABLE_ISOLATION
719 pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd);
722 * Take a PGD location (pgdp) and a pgd value that needs to be set there.
723 * Populates the user and returns the resulting PGD that must be set in
724 * the kernel copy of the page tables.
726 static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
728 if (!static_cpu_has(X86_FEATURE_PTI))
729 return pgd;
730 return __pti_set_user_pgtbl(pgdp, pgd);
732 #else /* CONFIG_PAGE_TABLE_ISOLATION */
733 static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
735 return pgd;
737 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
739 #endif /* __ASSEMBLY__ */
742 #ifdef CONFIG_X86_32
743 # include <asm/pgtable_32.h>
744 #else
745 # include <asm/pgtable_64.h>
746 #endif
748 #ifndef __ASSEMBLY__
749 #include <linux/mm_types.h>
750 #include <linux/mmdebug.h>
751 #include <linux/log2.h>
752 #include <asm/fixmap.h>
754 static inline int pte_none(pte_t pte)
756 return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK));
759 #define __HAVE_ARCH_PTE_SAME
760 static inline int pte_same(pte_t a, pte_t b)
762 return a.pte == b.pte;
765 static inline int pte_present(pte_t a)
767 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
770 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
771 static inline int pte_devmap(pte_t a)
773 return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP;
775 #endif
777 #define pte_accessible pte_accessible
778 static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
780 if (pte_flags(a) & _PAGE_PRESENT)
781 return true;
783 if ((pte_flags(a) & _PAGE_PROTNONE) &&
784 mm_tlb_flush_pending(mm))
785 return true;
787 return false;
790 static inline int pmd_present(pmd_t pmd)
793 * Checking for _PAGE_PSE is needed too because
794 * split_huge_page will temporarily clear the present bit (but
795 * the _PAGE_PSE flag will remain set at all times while the
796 * _PAGE_PRESENT bit is clear).
798 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
801 #ifdef CONFIG_NUMA_BALANCING
803 * These work without NUMA balancing but the kernel does not care. See the
804 * comment in include/linux/pgtable.h
806 static inline int pte_protnone(pte_t pte)
808 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
809 == _PAGE_PROTNONE;
812 static inline int pmd_protnone(pmd_t pmd)
814 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
815 == _PAGE_PROTNONE;
817 #endif /* CONFIG_NUMA_BALANCING */
819 static inline int pmd_none(pmd_t pmd)
821 /* Only check low word on 32-bit platforms, since it might be
822 out of sync with upper half. */
823 unsigned long val = native_pmd_val(pmd);
824 return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0;
827 static inline unsigned long pmd_page_vaddr(pmd_t pmd)
829 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
833 * Currently stuck as a macro due to indirect forward reference to
834 * linux/mmzone.h's __section_mem_map_addr() definition:
836 #define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
839 * Conversion functions: convert a page and protection to a page entry,
840 * and a page entry and page directory to the page they refer to.
842 * (Currently stuck as a macro because of indirect forward reference
843 * to linux/mm.h:page_to_nid())
845 #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
847 static inline int pmd_bad(pmd_t pmd)
849 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
852 static inline unsigned long pages_to_mb(unsigned long npg)
854 return npg >> (20 - PAGE_SHIFT);
857 #if CONFIG_PGTABLE_LEVELS > 2
858 static inline int pud_none(pud_t pud)
860 return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
863 static inline int pud_present(pud_t pud)
865 return pud_flags(pud) & _PAGE_PRESENT;
868 static inline unsigned long pud_page_vaddr(pud_t pud)
870 return (unsigned long)__va(pud_val(pud) & pud_pfn_mask(pud));
874 * Currently stuck as a macro due to indirect forward reference to
875 * linux/mmzone.h's __section_mem_map_addr() definition:
877 #define pud_page(pud) pfn_to_page(pud_pfn(pud))
879 #define pud_leaf pud_large
880 static inline int pud_large(pud_t pud)
882 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
883 (_PAGE_PSE | _PAGE_PRESENT);
886 static inline int pud_bad(pud_t pud)
888 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
890 #else
891 #define pud_leaf pud_large
892 static inline int pud_large(pud_t pud)
894 return 0;
896 #endif /* CONFIG_PGTABLE_LEVELS > 2 */
898 #if CONFIG_PGTABLE_LEVELS > 3
899 static inline int p4d_none(p4d_t p4d)
901 return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
904 static inline int p4d_present(p4d_t p4d)
906 return p4d_flags(p4d) & _PAGE_PRESENT;
909 static inline unsigned long p4d_page_vaddr(p4d_t p4d)
911 return (unsigned long)__va(p4d_val(p4d) & p4d_pfn_mask(p4d));
915 * Currently stuck as a macro due to indirect forward reference to
916 * linux/mmzone.h's __section_mem_map_addr() definition:
918 #define p4d_page(p4d) pfn_to_page(p4d_pfn(p4d))
920 static inline int p4d_bad(p4d_t p4d)
922 unsigned long ignore_flags = _KERNPG_TABLE | _PAGE_USER;
924 if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
925 ignore_flags |= _PAGE_NX;
927 return (p4d_flags(p4d) & ~ignore_flags) != 0;
929 #endif /* CONFIG_PGTABLE_LEVELS > 3 */
931 static inline unsigned long p4d_index(unsigned long address)
933 return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
936 #if CONFIG_PGTABLE_LEVELS > 4
937 static inline int pgd_present(pgd_t pgd)
939 if (!pgtable_l5_enabled())
940 return 1;
941 return pgd_flags(pgd) & _PAGE_PRESENT;
944 static inline unsigned long pgd_page_vaddr(pgd_t pgd)
946 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
950 * Currently stuck as a macro due to indirect forward reference to
951 * linux/mmzone.h's __section_mem_map_addr() definition:
953 #define pgd_page(pgd) pfn_to_page(pgd_pfn(pgd))
955 /* to find an entry in a page-table-directory. */
956 static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
958 if (!pgtable_l5_enabled())
959 return (p4d_t *)pgd;
960 return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address);
963 static inline int pgd_bad(pgd_t pgd)
965 unsigned long ignore_flags = _PAGE_USER;
967 if (!pgtable_l5_enabled())
968 return 0;
970 if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
971 ignore_flags |= _PAGE_NX;
973 return (pgd_flags(pgd) & ~ignore_flags) != _KERNPG_TABLE;
976 static inline int pgd_none(pgd_t pgd)
978 if (!pgtable_l5_enabled())
979 return 0;
981 * There is no need to do a workaround for the KNL stray
982 * A/D bit erratum here. PGDs only point to page tables
983 * except on 32-bit non-PAE which is not supported on
984 * KNL.
986 return !native_pgd_val(pgd);
988 #endif /* CONFIG_PGTABLE_LEVELS > 4 */
990 #endif /* __ASSEMBLY__ */
992 #define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
993 #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
995 #ifndef __ASSEMBLY__
997 extern int direct_gbpages;
998 void init_mem_mapping(void);
999 void early_alloc_pgt_buf(void);
1000 extern void memblock_find_dma_reserve(void);
1001 void __init poking_init(void);
1002 unsigned long init_memory_mapping(unsigned long start,
1003 unsigned long end, pgprot_t prot);
1005 #ifdef CONFIG_X86_64
1006 extern pgd_t trampoline_pgd_entry;
1007 #endif
1009 /* local pte updates need not use xchg for locking */
1010 static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
1012 pte_t res = *ptep;
1014 /* Pure native function needs no input for mm, addr */
1015 native_pte_clear(NULL, 0, ptep);
1016 return res;
1019 static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
1021 pmd_t res = *pmdp;
1023 native_pmd_clear(pmdp);
1024 return res;
1027 static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)
1029 pud_t res = *pudp;
1031 native_pud_clear(pudp);
1032 return res;
1035 static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1036 pte_t *ptep, pte_t pte)
1038 set_pte(ptep, pte);
1041 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1042 pmd_t *pmdp, pmd_t pmd)
1044 set_pmd(pmdp, pmd);
1047 static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
1048 pud_t *pudp, pud_t pud)
1050 native_set_pud(pudp, pud);
1054 * We only update the dirty/accessed state if we set
1055 * the dirty bit by hand in the kernel, since the hardware
1056 * will do the accessed bit for us, and we don't want to
1057 * race with other CPU's that might be updating the dirty
1058 * bit at the same time.
1060 struct vm_area_struct;
1062 #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
1063 extern int ptep_set_access_flags(struct vm_area_struct *vma,
1064 unsigned long address, pte_t *ptep,
1065 pte_t entry, int dirty);
1067 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1068 extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
1069 unsigned long addr, pte_t *ptep);
1071 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
1072 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
1073 unsigned long address, pte_t *ptep);
1075 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
1076 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
1077 pte_t *ptep)
1079 pte_t pte = native_ptep_get_and_clear(ptep);
1080 return pte;
1083 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
1084 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
1085 unsigned long addr, pte_t *ptep,
1086 int full)
1088 pte_t pte;
1089 if (full) {
1091 * Full address destruction in progress; paravirt does not
1092 * care about updates and native needs no locking
1094 pte = native_local_ptep_get_and_clear(ptep);
1095 } else {
1096 pte = ptep_get_and_clear(mm, addr, ptep);
1098 return pte;
1101 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
1102 static inline void ptep_set_wrprotect(struct mm_struct *mm,
1103 unsigned long addr, pte_t *ptep)
1105 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
1108 #define flush_tlb_fix_spurious_fault(vma, address) do { } while (0)
1110 #define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1112 #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1113 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1114 unsigned long address, pmd_t *pmdp,
1115 pmd_t entry, int dirty);
1116 extern int pudp_set_access_flags(struct vm_area_struct *vma,
1117 unsigned long address, pud_t *pudp,
1118 pud_t entry, int dirty);
1120 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1121 extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1122 unsigned long addr, pmd_t *pmdp);
1123 extern int pudp_test_and_clear_young(struct vm_area_struct *vma,
1124 unsigned long addr, pud_t *pudp);
1126 #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1127 extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
1128 unsigned long address, pmd_t *pmdp);
1131 #define pmd_write pmd_write
1132 static inline int pmd_write(pmd_t pmd)
1134 return pmd_flags(pmd) & _PAGE_RW;
1137 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1138 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
1139 pmd_t *pmdp)
1141 return native_pmdp_get_and_clear(pmdp);
1144 #define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
1145 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
1146 unsigned long addr, pud_t *pudp)
1148 return native_pudp_get_and_clear(pudp);
1151 #define __HAVE_ARCH_PMDP_SET_WRPROTECT
1152 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1153 unsigned long addr, pmd_t *pmdp)
1155 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp);
1158 #define pud_write pud_write
1159 static inline int pud_write(pud_t pud)
1161 return pud_flags(pud) & _PAGE_RW;
1164 #ifndef pmdp_establish
1165 #define pmdp_establish pmdp_establish
1166 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
1167 unsigned long address, pmd_t *pmdp, pmd_t pmd)
1169 if (IS_ENABLED(CONFIG_SMP)) {
1170 return xchg(pmdp, pmd);
1171 } else {
1172 pmd_t old = *pmdp;
1173 WRITE_ONCE(*pmdp, pmd);
1174 return old;
1177 #endif
1179 * Page table pages are page-aligned. The lower half of the top
1180 * level is used for userspace and the top half for the kernel.
1182 * Returns true for parts of the PGD that map userspace and
1183 * false for the parts that map the kernel.
1185 static inline bool pgdp_maps_userspace(void *__ptr)
1187 unsigned long ptr = (unsigned long)__ptr;
1189 return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
1192 #define pgd_leaf pgd_large
1193 static inline int pgd_large(pgd_t pgd) { return 0; }
1195 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1197 * All top-level PAGE_TABLE_ISOLATION page tables are order-1 pages
1198 * (8k-aligned and 8k in size). The kernel one is at the beginning 4k and
1199 * the user one is in the last 4k. To switch between them, you
1200 * just need to flip the 12th bit in their addresses.
1202 #define PTI_PGTABLE_SWITCH_BIT PAGE_SHIFT
1205 * This generates better code than the inline assembly in
1206 * __set_bit().
1208 static inline void *ptr_set_bit(void *ptr, int bit)
1210 unsigned long __ptr = (unsigned long)ptr;
1212 __ptr |= BIT(bit);
1213 return (void *)__ptr;
1215 static inline void *ptr_clear_bit(void *ptr, int bit)
1217 unsigned long __ptr = (unsigned long)ptr;
1219 __ptr &= ~BIT(bit);
1220 return (void *)__ptr;
1223 static inline pgd_t *kernel_to_user_pgdp(pgd_t *pgdp)
1225 return ptr_set_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1228 static inline pgd_t *user_to_kernel_pgdp(pgd_t *pgdp)
1230 return ptr_clear_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1233 static inline p4d_t *kernel_to_user_p4dp(p4d_t *p4dp)
1235 return ptr_set_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1238 static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp)
1240 return ptr_clear_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1242 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
1245 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
1247 * dst - pointer to pgd range anwhere on a pgd page
1248 * src - ""
1249 * count - the number of pgds to copy.
1251 * dst and src can be on the same page, but the range must not overlap,
1252 * and must not cross a page boundary.
1254 static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
1256 memcpy(dst, src, count * sizeof(pgd_t));
1257 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1258 if (!static_cpu_has(X86_FEATURE_PTI))
1259 return;
1260 /* Clone the user space pgd as well */
1261 memcpy(kernel_to_user_pgdp(dst), kernel_to_user_pgdp(src),
1262 count * sizeof(pgd_t));
1263 #endif
1266 #define PTE_SHIFT ilog2(PTRS_PER_PTE)
1267 static inline int page_level_shift(enum pg_level level)
1269 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
1271 static inline unsigned long page_level_size(enum pg_level level)
1273 return 1UL << page_level_shift(level);
1275 static inline unsigned long page_level_mask(enum pg_level level)
1277 return ~(page_level_size(level) - 1);
1281 * The x86 doesn't have any external MMU info: the kernel page
1282 * tables contain all the necessary information.
1284 static inline void update_mmu_cache(struct vm_area_struct *vma,
1285 unsigned long addr, pte_t *ptep)
1288 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1289 unsigned long addr, pmd_t *pmd)
1292 static inline void update_mmu_cache_pud(struct vm_area_struct *vma,
1293 unsigned long addr, pud_t *pud)
1297 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
1298 static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1300 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1303 static inline int pte_swp_soft_dirty(pte_t pte)
1305 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
1308 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1310 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1313 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1314 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1316 return pmd_set_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1319 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1321 return pmd_flags(pmd) & _PAGE_SWP_SOFT_DIRTY;
1324 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1326 return pmd_clear_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1328 #endif
1329 #endif
1331 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
1332 static inline pte_t pte_swp_mkuffd_wp(pte_t pte)
1334 return pte_set_flags(pte, _PAGE_SWP_UFFD_WP);
1337 static inline int pte_swp_uffd_wp(pte_t pte)
1339 return pte_flags(pte) & _PAGE_SWP_UFFD_WP;
1342 static inline pte_t pte_swp_clear_uffd_wp(pte_t pte)
1344 return pte_clear_flags(pte, _PAGE_SWP_UFFD_WP);
1347 static inline pmd_t pmd_swp_mkuffd_wp(pmd_t pmd)
1349 return pmd_set_flags(pmd, _PAGE_SWP_UFFD_WP);
1352 static inline int pmd_swp_uffd_wp(pmd_t pmd)
1354 return pmd_flags(pmd) & _PAGE_SWP_UFFD_WP;
1357 static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd)
1359 return pmd_clear_flags(pmd, _PAGE_SWP_UFFD_WP);
1361 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
1363 #define PKRU_AD_BIT 0x1
1364 #define PKRU_WD_BIT 0x2
1365 #define PKRU_BITS_PER_PKEY 2
1367 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1368 extern u32 init_pkru_value;
1369 #else
1370 #define init_pkru_value 0
1371 #endif
1373 static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
1375 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
1376 return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
1379 static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
1381 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
1383 * Access-disable disables writes too so we need to check
1384 * both bits here.
1386 return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
1389 static inline u16 pte_flags_pkey(unsigned long pte_flags)
1391 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1392 /* ifdef to avoid doing 59-bit shift on 32-bit values */
1393 return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0;
1394 #else
1395 return 0;
1396 #endif
1399 static inline bool __pkru_allows_pkey(u16 pkey, bool write)
1401 u32 pkru = read_pkru();
1403 if (!__pkru_allows_read(pkru, pkey))
1404 return false;
1405 if (write && !__pkru_allows_write(pkru, pkey))
1406 return false;
1408 return true;
1412 * 'pteval' can come from a PTE, PMD or PUD. We only check
1413 * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
1414 * same value on all 3 types.
1416 static inline bool __pte_access_permitted(unsigned long pteval, bool write)
1418 unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
1420 if (write)
1421 need_pte_bits |= _PAGE_RW;
1423 if ((pteval & need_pte_bits) != need_pte_bits)
1424 return 0;
1426 return __pkru_allows_pkey(pte_flags_pkey(pteval), write);
1429 #define pte_access_permitted pte_access_permitted
1430 static inline bool pte_access_permitted(pte_t pte, bool write)
1432 return __pte_access_permitted(pte_val(pte), write);
1435 #define pmd_access_permitted pmd_access_permitted
1436 static inline bool pmd_access_permitted(pmd_t pmd, bool write)
1438 return __pte_access_permitted(pmd_val(pmd), write);
1441 #define pud_access_permitted pud_access_permitted
1442 static inline bool pud_access_permitted(pud_t pud, bool write)
1444 return __pte_access_permitted(pud_val(pud), write);
1447 #define __HAVE_ARCH_PFN_MODIFY_ALLOWED 1
1448 extern bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot);
1450 static inline bool arch_has_pfn_modify_check(void)
1452 return boot_cpu_has_bug(X86_BUG_L1TF);
1455 #define arch_faults_on_old_pte arch_faults_on_old_pte
1456 static inline bool arch_faults_on_old_pte(void)
1458 return false;
1461 #endif /* __ASSEMBLY__ */
1463 #endif /* _ASM_X86_PGTABLE_H */