Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_PGTABLE_H |
| 3 | #define _ASM_X86_PGTABLE_H |
Jeremy Fitzhardinge | 6c38665 | 2008-01-30 13:32:55 +0100 | [diff] [blame] | 4 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 5 | #include <linux/mem_encrypt.h> |
Ingo Molnar | c47c1b1 | 2009-02-09 11:57:45 +0100 | [diff] [blame] | 6 | #include <asm/page.h> |
Jeremy Fitzhardinge | 8d19c99 | 2009-02-08 18:46:18 -0800 | [diff] [blame] | 7 | #include <asm/pgtable_types.h> |
Suresh Siddha | b2bc273 | 2008-09-23 14:00:36 -0700 | [diff] [blame] | 8 | |
venkatesh.pallipadi@intel.com | 8a7b12f | 2008-12-18 11:41:31 -0800 | [diff] [blame] | 9 | /* |
| 10 | * Macro to mark a page protection value as UC- |
| 11 | */ |
Juergen Gross | d85f333 | 2014-11-03 14:01:53 +0100 | [diff] [blame] | 12 | #define pgprot_noncached(prot) \ |
| 13 | ((boot_cpu_data.x86 > 3) \ |
| 14 | ? (__pgprot(pgprot_val(prot) | \ |
| 15 | cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \ |
venkatesh.pallipadi@intel.com | 8a7b12f | 2008-12-18 11:41:31 -0800 | [diff] [blame] | 16 | : (prot)) |
| 17 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 18 | /* |
| 19 | * Macros to add or remove encryption attribute |
| 20 | */ |
| 21 | #define pgprot_encrypted(prot) __pgprot(__sme_set(pgprot_val(prot))) |
| 22 | #define pgprot_decrypted(prot) __pgprot(__sme_clr(pgprot_val(prot))) |
| 23 | |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 24 | #ifndef __ASSEMBLY__ |
Sebastian Andrzej Siewior | 35fa745 | 2021-11-02 17:52:24 +0100 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
H. Peter Anvin | 55a6ca2 | 2009-11-23 15:12:07 -0800 | [diff] [blame] | 26 | #include <asm/x86_init.h> |
Dave Hansen | 784a4661 | 2021-06-23 14:02:05 +0200 | [diff] [blame] | 27 | #include <asm/pkru.h> |
Sebastian Andrzej Siewior | 0d714db | 2019-04-03 18:41:48 +0200 | [diff] [blame] | 28 | #include <asm/fpu/api.h> |
Andrea Arcangeli | 5a28106 | 2020-04-06 20:05:33 -0700 | [diff] [blame] | 29 | #include <asm-generic/pgtable_uffd.h> |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 30 | #include <linux/page_table_check.h> |
H. Peter Anvin | 55a6ca2 | 2009-11-23 15:12:07 -0800 | [diff] [blame] | 31 | |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 32 | extern pgd_t early_top_pgt[PTRS_PER_PGD]; |
Joerg Roedel | 4b47cdb | 2020-09-07 15:15:36 +0200 | [diff] [blame] | 33 | bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd); |
Tom Lendacky | b9d0520 | 2017-07-17 16:10:11 -0500 | [diff] [blame] | 34 | |
Steven Price | e455248 | 2020-02-03 17:36:11 -0800 | [diff] [blame] | 35 | void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm); |
Steven Price | c5cfae1 | 2020-02-03 17:36:16 -0800 | [diff] [blame] | 36 | void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm, |
| 37 | bool user); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 38 | void ptdump_walk_pgd_level_checkwx(void); |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 39 | void ptdump_walk_user_pgd_level_checkwx(void); |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 40 | |
| 41 | #ifdef CONFIG_DEBUG_WX |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 42 | #define debug_checkwx() ptdump_walk_pgd_level_checkwx() |
| 43 | #define debug_checkwx_user() ptdump_walk_user_pgd_level_checkwx() |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 44 | #else |
Joerg Roedel | d878efc | 2018-08-08 13:16:40 +0200 | [diff] [blame] | 45 | #define debug_checkwx() do { } while (0) |
| 46 | #define debug_checkwx_user() do { } while (0) |
Stephen Smalley | e1a5832 | 2015-10-05 12:55:20 -0400 | [diff] [blame] | 47 | #endif |
Borislav Petkov | ef6bea6 | 2014-01-18 12:48:14 +0100 | [diff] [blame] | 48 | |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 49 | /* |
Jeremy Fitzhardinge | 8405b12 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 50 | * ZERO_PAGE is a global shared page that is always zero: used |
| 51 | * for zero-mapped memory areas etc.. |
| 52 | */ |
Andi Kleen | 277d5b4 | 2013-08-05 15:02:43 -0700 | [diff] [blame] | 53 | extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] |
| 54 | __visible; |
Linus Torvalds | 8087148 | 2019-04-29 09:51:29 -0700 | [diff] [blame] | 55 | #define ZERO_PAGE(vaddr) ((void)(vaddr),virt_to_page(empty_zero_page)) |
Jeremy Fitzhardinge | 8405b12 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 56 | |
Jeremy Fitzhardinge | e3ed910 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 57 | extern spinlock_t pgd_lock; |
| 58 | extern struct list_head pgd_list; |
Jeremy Fitzhardinge | 8405b12 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 59 | |
Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 60 | extern struct mm_struct *pgd_page_get_mm(struct page *page); |
| 61 | |
Tom Lendacky | 21729f8 | 2017-07-17 16:10:07 -0500 | [diff] [blame] | 62 | extern pmdval_t early_pmd_flags; |
| 63 | |
Juergen Gross | fdc0269 | 2018-08-28 09:40:25 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_PARAVIRT_XXL |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 65 | #include <asm/paravirt.h> |
Juergen Gross | fdc0269 | 2018-08-28 09:40:25 +0200 | [diff] [blame] | 66 | #else /* !CONFIG_PARAVIRT_XXL */ |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 67 | #define set_pte(ptep, pte) native_set_pte(ptep, pte) |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 68 | |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 69 | #define set_pte_atomic(ptep, pte) \ |
| 70 | native_set_pte_atomic(ptep, pte) |
| 71 | |
| 72 | #define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd) |
| 73 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 74 | #ifndef __PAGETABLE_P4D_FOLDED |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 75 | #define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd) |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 76 | #define pgd_clear(pgd) (pgtable_l5_enabled() ? native_pgd_clear(pgd) : 0) |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 77 | #endif |
| 78 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 79 | #ifndef set_p4d |
| 80 | # define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d) |
| 81 | #endif |
| 82 | |
| 83 | #ifndef __PAGETABLE_PUD_FOLDED |
| 84 | #define p4d_clear(p4d) native_p4d_clear(p4d) |
| 85 | #endif |
| 86 | |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 87 | #ifndef set_pud |
| 88 | # define set_pud(pudp, pud) native_set_pud(pudp, pud) |
| 89 | #endif |
| 90 | |
Arnd Bergmann | d0f33ac | 2017-03-16 16:40:24 -0700 | [diff] [blame] | 91 | #ifndef __PAGETABLE_PUD_FOLDED |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 92 | #define pud_clear(pud) native_pud_clear(pud) |
| 93 | #endif |
| 94 | |
| 95 | #define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep) |
| 96 | #define pmd_clear(pmd) native_pmd_clear(pmd) |
| 97 | |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 98 | #define pgd_val(x) native_pgd_val(x) |
| 99 | #define __pgd(x) native_make_pgd(x) |
| 100 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 101 | #ifndef __PAGETABLE_P4D_FOLDED |
| 102 | #define p4d_val(x) native_p4d_val(x) |
| 103 | #define __p4d(x) native_make_p4d(x) |
| 104 | #endif |
| 105 | |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 106 | #ifndef __PAGETABLE_PUD_FOLDED |
| 107 | #define pud_val(x) native_pud_val(x) |
| 108 | #define __pud(x) native_make_pud(x) |
| 109 | #endif |
| 110 | |
| 111 | #ifndef __PAGETABLE_PMD_FOLDED |
| 112 | #define pmd_val(x) native_pmd_val(x) |
| 113 | #define __pmd(x) native_make_pmd(x) |
| 114 | #endif |
| 115 | |
| 116 | #define pte_val(x) native_pte_val(x) |
| 117 | #define __pte(x) native_make_pte(x) |
| 118 | |
Jeremy Fitzhardinge | 224101e | 2009-02-18 11:18:57 -0800 | [diff] [blame] | 119 | #define arch_end_context_switch(prev) do {} while(0) |
Juergen Gross | 9bad565 | 2018-08-28 09:40:23 +0200 | [diff] [blame] | 120 | #endif /* CONFIG_PARAVIRT_XXL */ |
Jeremy Fitzhardinge | 54321d9 | 2009-02-11 10:20:05 -0800 | [diff] [blame] | 121 | |
Jeremy Fitzhardinge | 8405b12 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 122 | /* |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 123 | * The following only work if pte_present() is true. |
| 124 | * Undefined behaviour if not.. |
| 125 | */ |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 126 | static inline int pte_dirty(pte_t pte) |
| 127 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 128 | return pte_flags(pte) & _PAGE_DIRTY; |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 131 | static inline int pte_young(pte_t pte) |
| 132 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 133 | return pte_flags(pte) & _PAGE_ACCESSED; |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Kirill A. Shutemov | c164e03 | 2014-12-10 15:44:36 -0800 | [diff] [blame] | 136 | static inline int pmd_dirty(pmd_t pmd) |
| 137 | { |
| 138 | return pmd_flags(pmd) & _PAGE_DIRTY; |
| 139 | } |
| 140 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 141 | static inline int pmd_young(pmd_t pmd) |
| 142 | { |
| 143 | return pmd_flags(pmd) & _PAGE_ACCESSED; |
| 144 | } |
| 145 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 146 | static inline int pud_dirty(pud_t pud) |
| 147 | { |
| 148 | return pud_flags(pud) & _PAGE_DIRTY; |
| 149 | } |
| 150 | |
| 151 | static inline int pud_young(pud_t pud) |
| 152 | { |
| 153 | return pud_flags(pud) & _PAGE_ACCESSED; |
| 154 | } |
| 155 | |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 156 | static inline int pte_write(pte_t pte) |
| 157 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 158 | return pte_flags(pte) & _PAGE_RW; |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 161 | static inline int pte_huge(pte_t pte) |
| 162 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 163 | return pte_flags(pte) & _PAGE_PSE; |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static inline int pte_global(pte_t pte) |
| 167 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 168 | return pte_flags(pte) & _PAGE_GLOBAL; |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static inline int pte_exec(pte_t pte) |
| 172 | { |
Jeremy Fitzhardinge | a15af1c | 2008-05-26 23:31:06 +0100 | [diff] [blame] | 173 | return !(pte_flags(pte) & _PAGE_NX); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 176 | static inline int pte_special(pte_t pte) |
| 177 | { |
Mel Gorman | c819f37 | 2015-02-12 14:58:38 -0800 | [diff] [blame] | 178 | return pte_flags(pte) & _PAGE_SPECIAL; |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 181 | /* Entries that were set to PROT_NONE are inverted */ |
| 182 | |
| 183 | static inline u64 protnone_mask(u64 val); |
| 184 | |
Hugh Dickins | 91030ca | 2008-09-09 16:42:45 +0100 | [diff] [blame] | 185 | static inline unsigned long pte_pfn(pte_t pte) |
| 186 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 187 | phys_addr_t pfn = pte_val(pte); |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 188 | pfn ^= protnone_mask(pfn); |
| 189 | return (pfn & PTE_PFN_MASK) >> PAGE_SHIFT; |
Hugh Dickins | 91030ca | 2008-09-09 16:42:45 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 192 | static inline unsigned long pmd_pfn(pmd_t pmd) |
| 193 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 194 | phys_addr_t pfn = pmd_val(pmd); |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 195 | pfn ^= protnone_mask(pfn); |
| 196 | return (pfn & pmd_pfn_mask(pmd)) >> PAGE_SHIFT; |
Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 197 | } |
| 198 | |
Mel Gorman | 0ee364e | 2013-02-11 14:52:36 +0000 | [diff] [blame] | 199 | static inline unsigned long pud_pfn(pud_t pud) |
| 200 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 201 | phys_addr_t pfn = pud_val(pud); |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 202 | pfn ^= protnone_mask(pfn); |
| 203 | return (pfn & pud_pfn_mask(pud)) >> PAGE_SHIFT; |
Mel Gorman | 0ee364e | 2013-02-11 14:52:36 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Kirill A. Shutemov | fe1e8c3 | 2017-03-13 17:33:04 +0300 | [diff] [blame] | 206 | static inline unsigned long p4d_pfn(p4d_t p4d) |
| 207 | { |
| 208 | return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT; |
| 209 | } |
| 210 | |
Tom Lendacky | fd7e315 | 2017-07-17 16:10:06 -0500 | [diff] [blame] | 211 | static inline unsigned long pgd_pfn(pgd_t pgd) |
| 212 | { |
| 213 | return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT; |
| 214 | } |
| 215 | |
Steven Price | 757b2a4a | 2020-02-03 17:35:41 -0800 | [diff] [blame] | 216 | #define p4d_leaf p4d_large |
Kirill A. Shutemov | fe1e8c3 | 2017-03-13 17:33:04 +0300 | [diff] [blame] | 217 | static inline int p4d_large(p4d_t p4d) |
| 218 | { |
| 219 | /* No 512 GiB pages yet */ |
| 220 | return 0; |
| 221 | } |
| 222 | |
Hugh Dickins | 91030ca | 2008-09-09 16:42:45 +0100 | [diff] [blame] | 223 | #define pte_page(pte) pfn_to_page(pte_pfn(pte)) |
| 224 | |
Steven Price | 757b2a4a | 2020-02-03 17:35:41 -0800 | [diff] [blame] | 225 | #define pmd_leaf pmd_large |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 226 | static inline int pmd_large(pmd_t pte) |
| 227 | { |
Andrea Arcangeli | 027ef6c | 2012-10-08 16:33:27 -0700 | [diff] [blame] | 228 | return pmd_flags(pte) & _PAGE_PSE; |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 231 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Fan Yang | 5bfea2d | 2020-06-04 18:22:07 +0800 | [diff] [blame] | 232 | /* NOTE: when predicate huge page, consider also pmd_devmap, or use pmd_large */ |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 233 | static inline int pmd_trans_huge(pmd_t pmd) |
| 234 | { |
Dan Williams | 5c7fb56 | 2016-01-15 16:56:52 -0800 | [diff] [blame] | 235 | return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 236 | } |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 237 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 238 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 239 | static inline int pud_trans_huge(pud_t pud) |
| 240 | { |
| 241 | return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; |
| 242 | } |
| 243 | #endif |
| 244 | |
Hugh Dickins | fd8cfd3 | 2016-05-19 17:13:00 -0700 | [diff] [blame] | 245 | #define has_transparent_hugepage has_transparent_hugepage |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 246 | static inline int has_transparent_hugepage(void) |
| 247 | { |
Borislav Petkov | 16bf922 | 2016-03-29 17:42:03 +0200 | [diff] [blame] | 248 | return boot_cpu_has(X86_FEATURE_PSE); |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 249 | } |
Dan Williams | 5c7fb56 | 2016-01-15 16:56:52 -0800 | [diff] [blame] | 250 | |
Robin Murphy | 1759673 | 2019-07-16 16:30:47 -0700 | [diff] [blame] | 251 | #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP |
Dan Williams | 5c7fb56 | 2016-01-15 16:56:52 -0800 | [diff] [blame] | 252 | static inline int pmd_devmap(pmd_t pmd) |
| 253 | { |
| 254 | return !!(pmd_val(pmd) & _PAGE_DEVMAP); |
| 255 | } |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 256 | |
| 257 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
| 258 | static inline int pud_devmap(pud_t pud) |
| 259 | { |
| 260 | return !!(pud_val(pud) & _PAGE_DEVMAP); |
| 261 | } |
| 262 | #else |
| 263 | static inline int pud_devmap(pud_t pud) |
| 264 | { |
| 265 | return 0; |
| 266 | } |
| 267 | #endif |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 268 | |
| 269 | static inline int pgd_devmap(pgd_t pgd) |
| 270 | { |
| 271 | return 0; |
| 272 | } |
Dan Williams | 5c7fb56 | 2016-01-15 16:56:52 -0800 | [diff] [blame] | 273 | #endif |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 274 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 275 | |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 276 | static inline pte_t pte_set_flags(pte_t pte, pteval_t set) |
| 277 | { |
| 278 | pteval_t v = native_pte_val(pte); |
| 279 | |
| 280 | return native_make_pte(v | set); |
| 281 | } |
| 282 | |
| 283 | static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear) |
| 284 | { |
| 285 | pteval_t v = native_pte_val(pte); |
| 286 | |
| 287 | return native_make_pte(v & ~clear); |
| 288 | } |
| 289 | |
Andrea Arcangeli | 5a28106 | 2020-04-06 20:05:33 -0700 | [diff] [blame] | 290 | #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP |
| 291 | static inline int pte_uffd_wp(pte_t pte) |
| 292 | { |
| 293 | return pte_flags(pte) & _PAGE_UFFD_WP; |
| 294 | } |
| 295 | |
| 296 | static inline pte_t pte_mkuffd_wp(pte_t pte) |
| 297 | { |
| 298 | return pte_set_flags(pte, _PAGE_UFFD_WP); |
| 299 | } |
| 300 | |
| 301 | static inline pte_t pte_clear_uffd_wp(pte_t pte) |
| 302 | { |
| 303 | return pte_clear_flags(pte, _PAGE_UFFD_WP); |
| 304 | } |
| 305 | #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ |
| 306 | |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 307 | static inline pte_t pte_mkclean(pte_t pte) |
| 308 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 309 | return pte_clear_flags(pte, _PAGE_DIRTY); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static inline pte_t pte_mkold(pte_t pte) |
| 313 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 314 | return pte_clear_flags(pte, _PAGE_ACCESSED); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static inline pte_t pte_wrprotect(pte_t pte) |
| 318 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 319 | return pte_clear_flags(pte, _PAGE_RW); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static inline pte_t pte_mkexec(pte_t pte) |
| 323 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 324 | return pte_clear_flags(pte, _PAGE_NX); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static inline pte_t pte_mkdirty(pte_t pte) |
| 328 | { |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 329 | return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static inline pte_t pte_mkyoung(pte_t pte) |
| 333 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 334 | return pte_set_flags(pte, _PAGE_ACCESSED); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static inline pte_t pte_mkwrite(pte_t pte) |
| 338 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 339 | return pte_set_flags(pte, _PAGE_RW); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static inline pte_t pte_mkhuge(pte_t pte) |
| 343 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 344 | return pte_set_flags(pte, _PAGE_PSE); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static inline pte_t pte_clrhuge(pte_t pte) |
| 348 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 349 | return pte_clear_flags(pte, _PAGE_PSE); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static inline pte_t pte_mkglobal(pte_t pte) |
| 353 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 354 | return pte_set_flags(pte, _PAGE_GLOBAL); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static inline pte_t pte_clrglobal(pte_t pte) |
| 358 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 359 | return pte_clear_flags(pte, _PAGE_GLOBAL); |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 360 | } |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 361 | |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 362 | static inline pte_t pte_mkspecial(pte_t pte) |
| 363 | { |
Jeremy Fitzhardinge | 6522869 | 2009-01-22 14:24:22 -0800 | [diff] [blame] | 364 | return pte_set_flags(pte, _PAGE_SPECIAL); |
Nick Piggin | 7e67513 | 2008-04-28 02:13:00 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 367 | static inline pte_t pte_mkdevmap(pte_t pte) |
| 368 | { |
| 369 | return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP); |
| 370 | } |
| 371 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 372 | static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set) |
| 373 | { |
| 374 | pmdval_t v = native_pmd_val(pmd); |
| 375 | |
Jan Beulich | 842cef9 | 2018-02-19 07:48:11 -0700 | [diff] [blame] | 376 | return native_make_pmd(v | set); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear) |
| 380 | { |
| 381 | pmdval_t v = native_pmd_val(pmd); |
| 382 | |
Jan Beulich | 842cef9 | 2018-02-19 07:48:11 -0700 | [diff] [blame] | 383 | return native_make_pmd(v & ~clear); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Andrea Arcangeli | 5a28106 | 2020-04-06 20:05:33 -0700 | [diff] [blame] | 386 | #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP |
| 387 | static inline int pmd_uffd_wp(pmd_t pmd) |
| 388 | { |
| 389 | return pmd_flags(pmd) & _PAGE_UFFD_WP; |
| 390 | } |
| 391 | |
| 392 | static inline pmd_t pmd_mkuffd_wp(pmd_t pmd) |
| 393 | { |
| 394 | return pmd_set_flags(pmd, _PAGE_UFFD_WP); |
| 395 | } |
| 396 | |
| 397 | static inline pmd_t pmd_clear_uffd_wp(pmd_t pmd) |
| 398 | { |
| 399 | return pmd_clear_flags(pmd, _PAGE_UFFD_WP); |
| 400 | } |
| 401 | #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ |
| 402 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 403 | static inline pmd_t pmd_mkold(pmd_t pmd) |
| 404 | { |
| 405 | return pmd_clear_flags(pmd, _PAGE_ACCESSED); |
| 406 | } |
| 407 | |
Minchan Kim | 590a471 | 2016-01-15 16:55:20 -0800 | [diff] [blame] | 408 | static inline pmd_t pmd_mkclean(pmd_t pmd) |
| 409 | { |
| 410 | return pmd_clear_flags(pmd, _PAGE_DIRTY); |
| 411 | } |
| 412 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 413 | static inline pmd_t pmd_wrprotect(pmd_t pmd) |
| 414 | { |
| 415 | return pmd_clear_flags(pmd, _PAGE_RW); |
| 416 | } |
| 417 | |
| 418 | static inline pmd_t pmd_mkdirty(pmd_t pmd) |
| 419 | { |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 420 | return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Dan Williams | f25748e3 | 2016-01-15 16:56:43 -0800 | [diff] [blame] | 423 | static inline pmd_t pmd_mkdevmap(pmd_t pmd) |
| 424 | { |
| 425 | return pmd_set_flags(pmd, _PAGE_DEVMAP); |
| 426 | } |
| 427 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 428 | static inline pmd_t pmd_mkhuge(pmd_t pmd) |
| 429 | { |
| 430 | return pmd_set_flags(pmd, _PAGE_PSE); |
| 431 | } |
| 432 | |
| 433 | static inline pmd_t pmd_mkyoung(pmd_t pmd) |
| 434 | { |
| 435 | return pmd_set_flags(pmd, _PAGE_ACCESSED); |
| 436 | } |
| 437 | |
| 438 | static inline pmd_t pmd_mkwrite(pmd_t pmd) |
| 439 | { |
| 440 | return pmd_set_flags(pmd, _PAGE_RW); |
| 441 | } |
| 442 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 443 | static inline pud_t pud_set_flags(pud_t pud, pudval_t set) |
| 444 | { |
| 445 | pudval_t v = native_pud_val(pud); |
| 446 | |
Jan Beulich | 842cef9 | 2018-02-19 07:48:11 -0700 | [diff] [blame] | 447 | return native_make_pud(v | set); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear) |
| 451 | { |
| 452 | pudval_t v = native_pud_val(pud); |
| 453 | |
Jan Beulich | 842cef9 | 2018-02-19 07:48:11 -0700 | [diff] [blame] | 454 | return native_make_pud(v & ~clear); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static inline pud_t pud_mkold(pud_t pud) |
| 458 | { |
| 459 | return pud_clear_flags(pud, _PAGE_ACCESSED); |
| 460 | } |
| 461 | |
| 462 | static inline pud_t pud_mkclean(pud_t pud) |
| 463 | { |
| 464 | return pud_clear_flags(pud, _PAGE_DIRTY); |
| 465 | } |
| 466 | |
| 467 | static inline pud_t pud_wrprotect(pud_t pud) |
| 468 | { |
| 469 | return pud_clear_flags(pud, _PAGE_RW); |
| 470 | } |
| 471 | |
| 472 | static inline pud_t pud_mkdirty(pud_t pud) |
| 473 | { |
| 474 | return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); |
| 475 | } |
| 476 | |
| 477 | static inline pud_t pud_mkdevmap(pud_t pud) |
| 478 | { |
| 479 | return pud_set_flags(pud, _PAGE_DEVMAP); |
| 480 | } |
| 481 | |
| 482 | static inline pud_t pud_mkhuge(pud_t pud) |
| 483 | { |
| 484 | return pud_set_flags(pud, _PAGE_PSE); |
| 485 | } |
| 486 | |
| 487 | static inline pud_t pud_mkyoung(pud_t pud) |
| 488 | { |
| 489 | return pud_set_flags(pud, _PAGE_ACCESSED); |
| 490 | } |
| 491 | |
| 492 | static inline pud_t pud_mkwrite(pud_t pud) |
| 493 | { |
| 494 | return pud_set_flags(pud, _PAGE_RW); |
| 495 | } |
| 496 | |
Cyrill Gorcunov | 2bf01f9 | 2014-06-04 16:08:16 -0700 | [diff] [blame] | 497 | #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 498 | static inline int pte_soft_dirty(pte_t pte) |
| 499 | { |
| 500 | return pte_flags(pte) & _PAGE_SOFT_DIRTY; |
| 501 | } |
| 502 | |
| 503 | static inline int pmd_soft_dirty(pmd_t pmd) |
| 504 | { |
| 505 | return pmd_flags(pmd) & _PAGE_SOFT_DIRTY; |
| 506 | } |
| 507 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 508 | static inline int pud_soft_dirty(pud_t pud) |
| 509 | { |
| 510 | return pud_flags(pud) & _PAGE_SOFT_DIRTY; |
| 511 | } |
| 512 | |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 513 | static inline pte_t pte_mksoft_dirty(pte_t pte) |
| 514 | { |
| 515 | return pte_set_flags(pte, _PAGE_SOFT_DIRTY); |
| 516 | } |
| 517 | |
| 518 | static inline pmd_t pmd_mksoft_dirty(pmd_t pmd) |
| 519 | { |
| 520 | return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY); |
| 521 | } |
| 522 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 523 | static inline pud_t pud_mksoft_dirty(pud_t pud) |
| 524 | { |
| 525 | return pud_set_flags(pud, _PAGE_SOFT_DIRTY); |
| 526 | } |
| 527 | |
Martin Schwidefsky | a7b7617 | 2015-04-22 14:20:47 +0200 | [diff] [blame] | 528 | static inline pte_t pte_clear_soft_dirty(pte_t pte) |
| 529 | { |
| 530 | return pte_clear_flags(pte, _PAGE_SOFT_DIRTY); |
| 531 | } |
| 532 | |
| 533 | static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd) |
| 534 | { |
| 535 | return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY); |
| 536 | } |
| 537 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 538 | static inline pud_t pud_clear_soft_dirty(pud_t pud) |
| 539 | { |
| 540 | return pud_clear_flags(pud, _PAGE_SOFT_DIRTY); |
| 541 | } |
| 542 | |
Cyrill Gorcunov | 2bf01f9 | 2014-06-04 16:08:16 -0700 | [diff] [blame] | 543 | #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */ |
| 544 | |
Jeremy Fitzhardinge | b534816 | 2009-02-04 18:33:38 -0800 | [diff] [blame] | 545 | /* |
| 546 | * Mask out unsupported bits in a present pgprot. Non-present pgprots |
| 547 | * can use those bits for other purposes, so leave them be. |
| 548 | */ |
| 549 | static inline pgprotval_t massage_pgprot(pgprot_t pgprot) |
| 550 | { |
| 551 | pgprotval_t protval = pgprot_val(pgprot); |
| 552 | |
| 553 | if (protval & _PAGE_PRESENT) |
| 554 | protval &= __supported_pte_mask; |
| 555 | |
| 556 | return protval; |
| 557 | } |
| 558 | |
Dave Hansen | fb43d6c | 2018-04-06 13:55:09 -0700 | [diff] [blame] | 559 | static inline pgprotval_t check_pgprot(pgprot_t pgprot) |
| 560 | { |
| 561 | pgprotval_t massaged_val = massage_pgprot(pgprot); |
| 562 | |
| 563 | /* mmdebug.h can not be included here because of dependencies */ |
| 564 | #ifdef CONFIG_DEBUG_VM |
| 565 | WARN_ONCE(pgprot_val(pgprot) != massaged_val, |
| 566 | "attempted to set unsupported pgprot: %016llx " |
| 567 | "bits: %016llx supported: %016llx\n", |
| 568 | (u64)pgprot_val(pgprot), |
| 569 | (u64)pgprot_val(pgprot) ^ massaged_val, |
| 570 | (u64)__supported_pte_mask); |
| 571 | #endif |
| 572 | |
| 573 | return massaged_val; |
| 574 | } |
| 575 | |
Jeremy Fitzhardinge | 6fdc05d | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 576 | static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot) |
| 577 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 578 | phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT; |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 579 | pfn ^= protnone_mask(pgprot_val(pgprot)); |
| 580 | pfn &= PTE_PFN_MASK; |
| 581 | return __pte(pfn | check_pgprot(pgprot)); |
Jeremy Fitzhardinge | 6fdc05d | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot) |
| 585 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 586 | phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT; |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 587 | pfn ^= protnone_mask(pgprot_val(pgprot)); |
| 588 | pfn &= PHYSICAL_PMD_PAGE_MASK; |
| 589 | return __pmd(pfn | check_pgprot(pgprot)); |
Jeremy Fitzhardinge | 6fdc05d | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 592 | static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot) |
| 593 | { |
Michal Hocko | e14d7df | 2018-06-27 17:46:50 +0200 | [diff] [blame] | 594 | phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT; |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 595 | pfn ^= protnone_mask(pgprot_val(pgprot)); |
| 596 | pfn &= PHYSICAL_PUD_PAGE_MASK; |
| 597 | return __pud(pfn | check_pgprot(pgprot)); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Anshuman Khandual | 86ec2da | 2020-06-03 16:03:45 -0700 | [diff] [blame] | 600 | static inline pmd_t pmd_mkinvalid(pmd_t pmd) |
Andi Kleen | 0768f91 | 2018-08-07 15:09:37 -0700 | [diff] [blame] | 601 | { |
| 602 | return pfn_pmd(pmd_pfn(pmd), |
| 603 | __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE))); |
| 604 | } |
| 605 | |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 606 | static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask); |
| 607 | |
Ingo Molnar | 3847231 | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 608 | static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) |
| 609 | { |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 610 | pteval_t val = pte_val(pte), oldval = val; |
Ingo Molnar | 3847231 | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * Chop off the NX bit (if present), and add the NX portion of |
| 614 | * the newprot (if present): |
| 615 | */ |
Venki Pallipadi | 1c12c4c | 2008-05-14 16:05:51 -0700 | [diff] [blame] | 616 | val &= _PAGE_CHG_MASK; |
Dave Hansen | fb43d6c | 2018-04-06 13:55:09 -0700 | [diff] [blame] | 617 | val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK; |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 618 | val = flip_protnone_guard(oldval, val, PTE_PFN_MASK); |
Ingo Molnar | 3847231 | 2008-01-30 13:32:57 +0100 | [diff] [blame] | 619 | return __pte(val); |
| 620 | } |
| 621 | |
Johannes Weiner | c489f12 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 622 | static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) |
| 623 | { |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 624 | pmdval_t val = pmd_val(pmd), oldval = val; |
Johannes Weiner | c489f12 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 625 | |
| 626 | val &= _HPAGE_CHG_MASK; |
Dave Hansen | fb43d6c | 2018-04-06 13:55:09 -0700 | [diff] [blame] | 627 | val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK; |
Andi Kleen | 6b28bac | 2018-06-13 15:48:24 -0700 | [diff] [blame] | 628 | val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK); |
Johannes Weiner | c489f12 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 629 | return __pmd(val); |
| 630 | } |
| 631 | |
Thomas Hellstrom | 6db73f1 | 2020-03-04 12:45:26 +0100 | [diff] [blame] | 632 | /* |
| 633 | * mprotect needs to preserve PAT and encryption bits when updating |
| 634 | * vm_page_prot |
| 635 | */ |
Venki Pallipadi | 1c12c4c | 2008-05-14 16:05:51 -0700 | [diff] [blame] | 636 | #define pgprot_modify pgprot_modify |
| 637 | static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot) |
| 638 | { |
| 639 | pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK; |
Thomas Hellstrom | 6db73f1 | 2020-03-04 12:45:26 +0100 | [diff] [blame] | 640 | pgprotval_t addbits = pgprot_val(newprot) & ~_PAGE_CHG_MASK; |
Venki Pallipadi | 1c12c4c | 2008-05-14 16:05:51 -0700 | [diff] [blame] | 641 | return __pgprot(preservebits | addbits); |
| 642 | } |
| 643 | |
Toshi Kani | bbac8c6 | 2015-09-17 12:24:18 -0600 | [diff] [blame] | 644 | #define pte_pgprot(x) __pgprot(pte_flags(x)) |
| 645 | #define pmd_pgprot(x) __pgprot(pmd_flags(x)) |
| 646 | #define pud_pgprot(x) __pgprot(pud_flags(x)) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 647 | #define p4d_pgprot(x) __pgprot(p4d_flags(x)) |
Andi Kleen | c6ca18e | 2008-01-30 13:33:51 +0100 | [diff] [blame] | 648 | |
Jeremy Fitzhardinge | b534816 | 2009-02-04 18:33:38 -0800 | [diff] [blame] | 649 | #define canon_pgprot(p) __pgprot(massage_pgprot(p)) |
Andi Kleen | 1e8e23b | 2008-01-30 13:33:53 +0100 | [diff] [blame] | 650 | |
Dave Hansen | 316d097 | 2018-04-20 15:20:28 -0700 | [diff] [blame] | 651 | static inline pgprot_t arch_filter_pgprot(pgprot_t prot) |
| 652 | { |
| 653 | return canon_pgprot(prot); |
| 654 | } |
| 655 | |
Suresh Siddha | 1adcaaf | 2009-08-17 13:23:50 -0700 | [diff] [blame] | 656 | static inline int is_new_memtype_allowed(u64 paddr, unsigned long size, |
Juergen Gross | d85f333 | 2014-11-03 14:01:53 +0100 | [diff] [blame] | 657 | enum page_cache_mode pcm, |
| 658 | enum page_cache_mode new_pcm) |
venkatesh.pallipadi@intel.com | afc7d20 | 2009-01-09 16:13:10 -0800 | [diff] [blame] | 659 | { |
| 660 | /* |
H. Peter Anvin | 55a6ca2 | 2009-11-23 15:12:07 -0800 | [diff] [blame] | 661 | * PAT type is always WB for untracked ranges, so no need to check. |
Suresh Siddha | 1adcaaf | 2009-08-17 13:23:50 -0700 | [diff] [blame] | 662 | */ |
H. Peter Anvin | 8a27138 | 2009-11-23 14:49:20 -0800 | [diff] [blame] | 663 | if (x86_platform.is_untracked_pat_range(paddr, paddr + size)) |
Suresh Siddha | 1adcaaf | 2009-08-17 13:23:50 -0700 | [diff] [blame] | 664 | return 1; |
| 665 | |
| 666 | /* |
venkatesh.pallipadi@intel.com | afc7d20 | 2009-01-09 16:13:10 -0800 | [diff] [blame] | 667 | * Certain new memtypes are not allowed with certain |
| 668 | * requested memtype: |
| 669 | * - request is uncached, return cannot be write-back |
| 670 | * - request is write-combine, return cannot be write-back |
Toshi Kani | ecb2feb | 2015-06-04 18:55:14 +0200 | [diff] [blame] | 671 | * - request is write-through, return cannot be write-back |
| 672 | * - request is write-through, return cannot be write-combine |
venkatesh.pallipadi@intel.com | afc7d20 | 2009-01-09 16:13:10 -0800 | [diff] [blame] | 673 | */ |
Juergen Gross | d85f333 | 2014-11-03 14:01:53 +0100 | [diff] [blame] | 674 | if ((pcm == _PAGE_CACHE_MODE_UC_MINUS && |
| 675 | new_pcm == _PAGE_CACHE_MODE_WB) || |
| 676 | (pcm == _PAGE_CACHE_MODE_WC && |
Toshi Kani | ecb2feb | 2015-06-04 18:55:14 +0200 | [diff] [blame] | 677 | new_pcm == _PAGE_CACHE_MODE_WB) || |
| 678 | (pcm == _PAGE_CACHE_MODE_WT && |
| 679 | new_pcm == _PAGE_CACHE_MODE_WB) || |
| 680 | (pcm == _PAGE_CACHE_MODE_WT && |
| 681 | new_pcm == _PAGE_CACHE_MODE_WC)) { |
venkatesh.pallipadi@intel.com | afc7d20 | 2009-01-09 16:13:10 -0800 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | return 1; |
| 686 | } |
| 687 | |
Tejun Heo | 458a3e6 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 688 | pmd_t *populate_extra_pmd(unsigned long vaddr); |
| 689 | pte_t *populate_extra_pte(unsigned long vaddr); |
Joerg Roedel | fcbbd97 | 2018-07-18 11:40:56 +0200 | [diff] [blame] | 690 | |
| 691 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 692 | pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd); |
| 693 | |
| 694 | /* |
| 695 | * Take a PGD location (pgdp) and a pgd value that needs to be set there. |
| 696 | * Populates the user and returns the resulting PGD that must be set in |
| 697 | * the kernel copy of the page tables. |
| 698 | */ |
| 699 | static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) |
| 700 | { |
| 701 | if (!static_cpu_has(X86_FEATURE_PTI)) |
| 702 | return pgd; |
| 703 | return __pti_set_user_pgtbl(pgdp, pgd); |
| 704 | } |
| 705 | #else /* CONFIG_PAGE_TABLE_ISOLATION */ |
| 706 | static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd) |
| 707 | { |
| 708 | return pgd; |
| 709 | } |
| 710 | #endif /* CONFIG_PAGE_TABLE_ISOLATION */ |
| 711 | |
Jeremy Fitzhardinge | 4614139 | 2008-01-30 13:32:56 +0100 | [diff] [blame] | 712 | #endif /* __ASSEMBLY__ */ |
| 713 | |
Joerg Roedel | fcbbd97 | 2018-07-18 11:40:56 +0200 | [diff] [blame] | 714 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 715 | #ifdef CONFIG_X86_32 |
David Howells | a1ce392 | 2012-10-02 18:01:25 +0100 | [diff] [blame] | 716 | # include <asm/pgtable_32.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 717 | #else |
David Howells | a1ce392 | 2012-10-02 18:01:25 +0100 | [diff] [blame] | 718 | # include <asm/pgtable_64.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 719 | #endif |
Jeremy Fitzhardinge | 6c38665 | 2008-01-30 13:32:55 +0100 | [diff] [blame] | 720 | |
Jeremy Fitzhardinge | aca159d | 2009-02-05 11:30:54 -0800 | [diff] [blame] | 721 | #ifndef __ASSEMBLY__ |
Jeremy Fitzhardinge | f476961 | 2009-02-05 11:30:55 -0800 | [diff] [blame] | 722 | #include <linux/mm_types.h> |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 723 | #include <linux/mmdebug.h> |
Dave Hansen | 4cbeb51 | 2013-01-22 13:24:31 -0800 | [diff] [blame] | 724 | #include <linux/log2.h> |
Thomas Garnier | ef37bc3 | 2017-03-21 08:17:25 +0100 | [diff] [blame] | 725 | #include <asm/fixmap.h> |
Jeremy Fitzhardinge | aca159d | 2009-02-05 11:30:54 -0800 | [diff] [blame] | 726 | |
Jeremy Fitzhardinge | a034a01 | 2009-02-05 11:30:43 -0800 | [diff] [blame] | 727 | static inline int pte_none(pte_t pte) |
| 728 | { |
Dave Hansen | 97e3c60 | 2016-07-07 17:19:12 -0700 | [diff] [blame] | 729 | return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK)); |
Jeremy Fitzhardinge | a034a01 | 2009-02-05 11:30:43 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Jeremy Fitzhardinge | 8de01da | 2009-02-05 11:30:44 -0800 | [diff] [blame] | 732 | #define __HAVE_ARCH_PTE_SAME |
| 733 | static inline int pte_same(pte_t a, pte_t b) |
| 734 | { |
| 735 | return a.pte == b.pte; |
| 736 | } |
| 737 | |
Jeremy Fitzhardinge | 7c68385 | 2009-02-05 11:30:45 -0800 | [diff] [blame] | 738 | static inline int pte_present(pte_t a) |
| 739 | { |
Mel Gorman | c46a7c8 | 2014-06-04 16:06:30 -0700 | [diff] [blame] | 740 | return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE); |
| 741 | } |
| 742 | |
Robin Murphy | 1759673 | 2019-07-16 16:30:47 -0700 | [diff] [blame] | 743 | #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP |
Dan Williams | 3565fce | 2016-01-15 16:56:55 -0800 | [diff] [blame] | 744 | static inline int pte_devmap(pte_t a) |
| 745 | { |
| 746 | return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP; |
| 747 | } |
| 748 | #endif |
| 749 | |
Rik van Riel | 2c3cf55 | 2012-10-09 15:31:12 +0200 | [diff] [blame] | 750 | #define pte_accessible pte_accessible |
Rik van Riel | 2084140 | 2013-12-18 17:08:44 -0800 | [diff] [blame] | 751 | static inline bool pte_accessible(struct mm_struct *mm, pte_t a) |
Rik van Riel | 2c3cf55 | 2012-10-09 15:31:12 +0200 | [diff] [blame] | 752 | { |
Rik van Riel | 2084140 | 2013-12-18 17:08:44 -0800 | [diff] [blame] | 753 | if (pte_flags(a) & _PAGE_PRESENT) |
| 754 | return true; |
| 755 | |
Mel Gorman | 21d9ee3 | 2015-02-12 14:58:32 -0800 | [diff] [blame] | 756 | if ((pte_flags(a) & _PAGE_PROTNONE) && |
Arnd Bergmann | 36090de | 2022-01-14 14:06:10 -0800 | [diff] [blame] | 757 | atomic_read(&mm->tlb_flush_pending)) |
Rik van Riel | 2084140 | 2013-12-18 17:08:44 -0800 | [diff] [blame] | 758 | return true; |
| 759 | |
| 760 | return false; |
Rik van Riel | 2c3cf55 | 2012-10-09 15:31:12 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Jeremy Fitzhardinge | 649e8ef | 2009-02-05 11:30:50 -0800 | [diff] [blame] | 763 | static inline int pmd_present(pmd_t pmd) |
| 764 | { |
Andrea Arcangeli | 027ef6c | 2012-10-08 16:33:27 -0700 | [diff] [blame] | 765 | /* |
| 766 | * Checking for _PAGE_PSE is needed too because |
| 767 | * split_huge_page will temporarily clear the present bit (but |
| 768 | * the _PAGE_PSE flag will remain set at all times while the |
| 769 | * _PAGE_PRESENT bit is clear). |
| 770 | */ |
Mel Gorman | 21d9ee3 | 2015-02-12 14:58:32 -0800 | [diff] [blame] | 771 | return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE); |
Jeremy Fitzhardinge | 649e8ef | 2009-02-05 11:30:50 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Mel Gorman | e7bb4b6d | 2015-02-12 14:58:19 -0800 | [diff] [blame] | 774 | #ifdef CONFIG_NUMA_BALANCING |
| 775 | /* |
| 776 | * These work without NUMA balancing but the kernel does not care. See the |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 777 | * comment in include/linux/pgtable.h |
Mel Gorman | e7bb4b6d | 2015-02-12 14:58:19 -0800 | [diff] [blame] | 778 | */ |
| 779 | static inline int pte_protnone(pte_t pte) |
| 780 | { |
David Vrabel | e3a1f6c | 2015-02-19 13:06:53 +0000 | [diff] [blame] | 781 | return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT)) |
| 782 | == _PAGE_PROTNONE; |
Mel Gorman | e7bb4b6d | 2015-02-12 14:58:19 -0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static inline int pmd_protnone(pmd_t pmd) |
| 786 | { |
David Vrabel | e3a1f6c | 2015-02-19 13:06:53 +0000 | [diff] [blame] | 787 | return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT)) |
| 788 | == _PAGE_PROTNONE; |
Mel Gorman | e7bb4b6d | 2015-02-12 14:58:19 -0800 | [diff] [blame] | 789 | } |
| 790 | #endif /* CONFIG_NUMA_BALANCING */ |
| 791 | |
Jeremy Fitzhardinge | 4fea801 | 2009-02-05 11:30:51 -0800 | [diff] [blame] | 792 | static inline int pmd_none(pmd_t pmd) |
| 793 | { |
| 794 | /* Only check low word on 32-bit platforms, since it might be |
| 795 | out of sync with upper half. */ |
Dave Hansen | 97e3c60 | 2016-07-07 17:19:12 -0700 | [diff] [blame] | 796 | unsigned long val = native_pmd_val(pmd); |
| 797 | return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0; |
Jeremy Fitzhardinge | 4fea801 | 2009-02-05 11:30:51 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Jeremy Fitzhardinge | 3ffb356 | 2009-02-05 11:30:59 -0800 | [diff] [blame] | 800 | static inline unsigned long pmd_page_vaddr(pmd_t pmd) |
| 801 | { |
Toshi Kani | f70abb0 | 2015-09-17 12:24:17 -0600 | [diff] [blame] | 802 | return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd)); |
Jeremy Fitzhardinge | 3ffb356 | 2009-02-05 11:30:59 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Ingo Molnar | e5f7f202 | 2009-02-09 11:42:57 +0100 | [diff] [blame] | 805 | /* |
| 806 | * Currently stuck as a macro due to indirect forward reference to |
| 807 | * linux/mmzone.h's __section_mem_map_addr() definition: |
| 808 | */ |
Tom Lendacky | fd7e315 | 2017-07-17 16:10:06 -0500 | [diff] [blame] | 809 | #define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd)) |
Jeremy Fitzhardinge | 20063ca | 2009-02-05 11:31:00 -0800 | [diff] [blame] | 810 | |
Jeremy Fitzhardinge | e24d7ee | 2009-02-05 11:31:01 -0800 | [diff] [blame] | 811 | /* |
Jeremy Fitzhardinge | 97e2817 | 2009-02-05 11:31:05 -0800 | [diff] [blame] | 812 | * Conversion functions: convert a page and protection to a page entry, |
| 813 | * and a page entry and page directory to the page they refer to. |
| 814 | * |
| 815 | * (Currently stuck as a macro because of indirect forward reference |
| 816 | * to linux/mm.h:page_to_nid()) |
| 817 | */ |
| 818 | #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot)) |
| 819 | |
Jeremy Fitzhardinge | 9951023 | 2009-02-05 11:31:11 -0800 | [diff] [blame] | 820 | static inline int pmd_bad(pmd_t pmd) |
| 821 | { |
Jeremy Fitzhardinge | 18a7a199 | 2009-02-05 11:31:16 -0800 | [diff] [blame] | 822 | return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE; |
Jeremy Fitzhardinge | 9951023 | 2009-02-05 11:31:11 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Jeremy Fitzhardinge | cc290ca | 2009-02-05 11:31:12 -0800 | [diff] [blame] | 825 | static inline unsigned long pages_to_mb(unsigned long npg) |
| 826 | { |
| 827 | return npg >> (20 - PAGE_SHIFT); |
| 828 | } |
| 829 | |
Kirill A. Shutemov | 9823336 | 2015-04-14 15:46:14 -0700 | [diff] [blame] | 830 | #if CONFIG_PGTABLE_LEVELS > 2 |
Jeremy Fitzhardinge | deb79cf | 2009-02-05 11:31:13 -0800 | [diff] [blame] | 831 | static inline int pud_none(pud_t pud) |
| 832 | { |
Dave Hansen | 97e3c60 | 2016-07-07 17:19:12 -0700 | [diff] [blame] | 833 | return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0; |
Jeremy Fitzhardinge | deb79cf | 2009-02-05 11:31:13 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Jeremy Fitzhardinge | 5ba7c91 | 2009-02-05 11:30:48 -0800 | [diff] [blame] | 836 | static inline int pud_present(pud_t pud) |
| 837 | { |
Jeremy Fitzhardinge | 18a7a199 | 2009-02-05 11:31:16 -0800 | [diff] [blame] | 838 | return pud_flags(pud) & _PAGE_PRESENT; |
Jeremy Fitzhardinge | 5ba7c91 | 2009-02-05 11:30:48 -0800 | [diff] [blame] | 839 | } |
Jeremy Fitzhardinge | 6fff47e | 2009-02-05 11:30:53 -0800 | [diff] [blame] | 840 | |
Aneesh Kumar K.V | 9cf6fa2 | 2021-07-07 18:09:53 -0700 | [diff] [blame] | 841 | static inline pmd_t *pud_pgtable(pud_t pud) |
Jeremy Fitzhardinge | 6fff47e | 2009-02-05 11:30:53 -0800 | [diff] [blame] | 842 | { |
Aneesh Kumar K.V | 9cf6fa2 | 2021-07-07 18:09:53 -0700 | [diff] [blame] | 843 | return (pmd_t *)__va(pud_val(pud) & pud_pfn_mask(pud)); |
Jeremy Fitzhardinge | 6fff47e | 2009-02-05 11:30:53 -0800 | [diff] [blame] | 844 | } |
Jeremy Fitzhardinge | f476961 | 2009-02-05 11:30:55 -0800 | [diff] [blame] | 845 | |
Ingo Molnar | e5f7f202 | 2009-02-09 11:42:57 +0100 | [diff] [blame] | 846 | /* |
| 847 | * Currently stuck as a macro due to indirect forward reference to |
| 848 | * linux/mmzone.h's __section_mem_map_addr() definition: |
| 849 | */ |
Tom Lendacky | fd7e315 | 2017-07-17 16:10:06 -0500 | [diff] [blame] | 850 | #define pud_page(pud) pfn_to_page(pud_pfn(pud)) |
Jeremy Fitzhardinge | 01ade20d5a | 2009-02-05 11:31:02 -0800 | [diff] [blame] | 851 | |
Steven Price | 757b2a4a | 2020-02-03 17:35:41 -0800 | [diff] [blame] | 852 | #define pud_leaf pud_large |
Jeremy Fitzhardinge | 3f6cbef | 2009-02-05 11:31:08 -0800 | [diff] [blame] | 853 | static inline int pud_large(pud_t pud) |
| 854 | { |
Jeremy Fitzhardinge | e2f5bda | 2009-02-09 00:09:52 -0800 | [diff] [blame] | 855 | return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) == |
Jeremy Fitzhardinge | 3f6cbef | 2009-02-05 11:31:08 -0800 | [diff] [blame] | 856 | (_PAGE_PSE | _PAGE_PRESENT); |
| 857 | } |
Jeremy Fitzhardinge | a61bb29 | 2009-02-05 11:31:10 -0800 | [diff] [blame] | 858 | |
| 859 | static inline int pud_bad(pud_t pud) |
| 860 | { |
Jeremy Fitzhardinge | 18a7a199 | 2009-02-05 11:31:16 -0800 | [diff] [blame] | 861 | return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0; |
Jeremy Fitzhardinge | a61bb29 | 2009-02-05 11:31:10 -0800 | [diff] [blame] | 862 | } |
Jeremy Fitzhardinge | e2f5bda | 2009-02-09 00:09:52 -0800 | [diff] [blame] | 863 | #else |
Steven Price | 757b2a4a | 2020-02-03 17:35:41 -0800 | [diff] [blame] | 864 | #define pud_leaf pud_large |
Jeremy Fitzhardinge | e2f5bda | 2009-02-09 00:09:52 -0800 | [diff] [blame] | 865 | static inline int pud_large(pud_t pud) |
| 866 | { |
| 867 | return 0; |
| 868 | } |
Kirill A. Shutemov | 9823336 | 2015-04-14 15:46:14 -0700 | [diff] [blame] | 869 | #endif /* CONFIG_PGTABLE_LEVELS > 2 */ |
Jeremy Fitzhardinge | 5ba7c91 | 2009-02-05 11:30:48 -0800 | [diff] [blame] | 870 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 871 | #if CONFIG_PGTABLE_LEVELS > 3 |
| 872 | static inline int p4d_none(p4d_t p4d) |
| 873 | { |
| 874 | return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0; |
| 875 | } |
| 876 | |
| 877 | static inline int p4d_present(p4d_t p4d) |
| 878 | { |
| 879 | return p4d_flags(p4d) & _PAGE_PRESENT; |
| 880 | } |
| 881 | |
Aneesh Kumar K.V | dc4875f | 2021-07-07 18:09:56 -0700 | [diff] [blame] | 882 | static inline pud_t *p4d_pgtable(p4d_t p4d) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 883 | { |
Aneesh Kumar K.V | dc4875f | 2021-07-07 18:09:56 -0700 | [diff] [blame] | 884 | return (pud_t *)__va(p4d_val(p4d) & p4d_pfn_mask(p4d)); |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Currently stuck as a macro due to indirect forward reference to |
| 889 | * linux/mmzone.h's __section_mem_map_addr() definition: |
| 890 | */ |
Tom Lendacky | fd7e315 | 2017-07-17 16:10:06 -0500 | [diff] [blame] | 891 | #define p4d_page(p4d) pfn_to_page(p4d_pfn(p4d)) |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 892 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 893 | static inline int p4d_bad(p4d_t p4d) |
| 894 | { |
Dave Hansen | 1c4de1f | 2017-12-04 15:07:38 +0100 | [diff] [blame] | 895 | unsigned long ignore_flags = _KERNPG_TABLE | _PAGE_USER; |
| 896 | |
| 897 | if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION)) |
| 898 | ignore_flags |= _PAGE_NX; |
| 899 | |
| 900 | return (p4d_flags(p4d) & ~ignore_flags) != 0; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 901 | } |
| 902 | #endif /* CONFIG_PGTABLE_LEVELS > 3 */ |
| 903 | |
Kirill A. Shutemov | fe1e8c3 | 2017-03-13 17:33:04 +0300 | [diff] [blame] | 904 | static inline unsigned long p4d_index(unsigned long address) |
| 905 | { |
| 906 | return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1); |
| 907 | } |
| 908 | |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 909 | #if CONFIG_PGTABLE_LEVELS > 4 |
Jeremy Fitzhardinge | 9f38d7e | 2009-02-05 11:30:49 -0800 | [diff] [blame] | 910 | static inline int pgd_present(pgd_t pgd) |
| 911 | { |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 912 | if (!pgtable_l5_enabled()) |
Kirill A. Shutemov | 98219dd | 2018-02-14 21:25:40 +0300 | [diff] [blame] | 913 | return 1; |
Jeremy Fitzhardinge | 18a7a199 | 2009-02-05 11:31:16 -0800 | [diff] [blame] | 914 | return pgd_flags(pgd) & _PAGE_PRESENT; |
Jeremy Fitzhardinge | 9f38d7e | 2009-02-05 11:30:49 -0800 | [diff] [blame] | 915 | } |
Jeremy Fitzhardinge | c5f040b | 2009-02-05 11:30:52 -0800 | [diff] [blame] | 916 | |
| 917 | static inline unsigned long pgd_page_vaddr(pgd_t pgd) |
| 918 | { |
| 919 | return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK); |
| 920 | } |
Jeremy Fitzhardinge | 777cba1 | 2009-02-05 11:30:56 -0800 | [diff] [blame] | 921 | |
Ingo Molnar | e5f7f202 | 2009-02-09 11:42:57 +0100 | [diff] [blame] | 922 | /* |
| 923 | * Currently stuck as a macro due to indirect forward reference to |
| 924 | * linux/mmzone.h's __section_mem_map_addr() definition: |
| 925 | */ |
Tom Lendacky | fd7e315 | 2017-07-17 16:10:06 -0500 | [diff] [blame] | 926 | #define pgd_page(pgd) pfn_to_page(pgd_pfn(pgd)) |
Jeremy Fitzhardinge | 7cfb810 | 2009-02-05 11:30:57 -0800 | [diff] [blame] | 927 | |
| 928 | /* to find an entry in a page-table-directory. */ |
Kirill A. Shutemov | b8c1e42 | 2018-06-26 13:03:41 +0300 | [diff] [blame] | 929 | static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address) |
Jeremy Fitzhardinge | 3d081b1 | 2009-02-05 11:30:58 -0800 | [diff] [blame] | 930 | { |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 931 | if (!pgtable_l5_enabled()) |
Kirill A. Shutemov | 98219dd | 2018-02-14 21:25:40 +0300 | [diff] [blame] | 932 | return (p4d_t *)pgd; |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 933 | return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address); |
Jeremy Fitzhardinge | 3d081b1 | 2009-02-05 11:30:58 -0800 | [diff] [blame] | 934 | } |
Jeremy Fitzhardinge | 30f1031 | 2009-02-05 11:31:09 -0800 | [diff] [blame] | 935 | |
| 936 | static inline int pgd_bad(pgd_t pgd) |
| 937 | { |
Dave Hansen | 1c4de1f | 2017-12-04 15:07:38 +0100 | [diff] [blame] | 938 | unsigned long ignore_flags = _PAGE_USER; |
| 939 | |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 940 | if (!pgtable_l5_enabled()) |
Kirill A. Shutemov | 98219dd | 2018-02-14 21:25:40 +0300 | [diff] [blame] | 941 | return 0; |
| 942 | |
Dave Hansen | 1c4de1f | 2017-12-04 15:07:38 +0100 | [diff] [blame] | 943 | if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION)) |
| 944 | ignore_flags |= _PAGE_NX; |
| 945 | |
| 946 | return (pgd_flags(pgd) & ~ignore_flags) != _KERNPG_TABLE; |
Jeremy Fitzhardinge | 30f1031 | 2009-02-05 11:31:09 -0800 | [diff] [blame] | 947 | } |
Jeremy Fitzhardinge | 7325cc2 | 2009-02-05 11:31:14 -0800 | [diff] [blame] | 948 | |
| 949 | static inline int pgd_none(pgd_t pgd) |
| 950 | { |
Kirill A. Shutemov | ed7588d | 2018-05-18 13:35:24 +0300 | [diff] [blame] | 951 | if (!pgtable_l5_enabled()) |
Kirill A. Shutemov | 98219dd | 2018-02-14 21:25:40 +0300 | [diff] [blame] | 952 | return 0; |
Dave Hansen | 97e3c60 | 2016-07-07 17:19:12 -0700 | [diff] [blame] | 953 | /* |
| 954 | * There is no need to do a workaround for the KNL stray |
| 955 | * A/D bit erratum here. PGDs only point to page tables |
| 956 | * except on 32-bit non-PAE which is not supported on |
| 957 | * KNL. |
| 958 | */ |
Jeremy Fitzhardinge | 26c8e31 | 2009-02-05 11:31:17 -0800 | [diff] [blame] | 959 | return !native_pgd_val(pgd); |
Jeremy Fitzhardinge | 7325cc2 | 2009-02-05 11:31:14 -0800 | [diff] [blame] | 960 | } |
Kirill A. Shutemov | f2a6a70 | 2017-03-17 21:55:15 +0300 | [diff] [blame] | 961 | #endif /* CONFIG_PGTABLE_LEVELS > 4 */ |
Jeremy Fitzhardinge | 9f38d7e | 2009-02-05 11:30:49 -0800 | [diff] [blame] | 962 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 963 | #endif /* __ASSEMBLY__ */ |
| 964 | |
Jeremy Fitzhardinge | 68db065 | 2008-03-17 16:37:13 -0700 | [diff] [blame] | 965 | #define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET) |
| 966 | #define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY) |
| 967 | |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 968 | #ifndef __ASSEMBLY__ |
| 969 | |
Jaswinder Singh Rajput | 2c1b284 | 2009-04-11 00:03:10 +0530 | [diff] [blame] | 970 | extern int direct_gbpages; |
Yinghai Lu | 22ddfca | 2012-11-16 19:38:41 -0800 | [diff] [blame] | 971 | void init_mem_mapping(void); |
Yinghai Lu | 8d57470 | 2012-11-16 19:38:58 -0800 | [diff] [blame] | 972 | void early_alloc_pgt_buf(void); |
Ingo Molnar | 4270fd8 | 2017-01-28 12:45:40 +0100 | [diff] [blame] | 973 | extern void memblock_find_dma_reserve(void); |
Benjamin Thiel | 56ce937 | 2020-06-06 14:37:43 +0200 | [diff] [blame] | 974 | void __init poking_init(void); |
| 975 | unsigned long init_memory_mapping(unsigned long start, |
| 976 | unsigned long end, pgprot_t prot); |
Mike Rapoport | 88107d3 | 2020-06-08 21:33:01 -0700 | [diff] [blame] | 977 | |
Thomas Garnier | b234e8a | 2016-06-21 17:47:01 -0700 | [diff] [blame] | 978 | #ifdef CONFIG_X86_64 |
Thomas Garnier | b234e8a | 2016-06-21 17:47:01 -0700 | [diff] [blame] | 979 | extern pgd_t trampoline_pgd_entry; |
Thomas Garnier | b234e8a | 2016-06-21 17:47:01 -0700 | [diff] [blame] | 980 | #endif |
| 981 | |
Jeremy Fitzhardinge | 4891645 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 982 | /* local pte updates need not use xchg for locking */ |
| 983 | static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep) |
| 984 | { |
| 985 | pte_t res = *ptep; |
| 986 | |
| 987 | /* Pure native function needs no input for mm, addr */ |
| 988 | native_pte_clear(NULL, 0, ptep); |
| 989 | return res; |
| 990 | } |
| 991 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 992 | static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp) |
| 993 | { |
| 994 | pmd_t res = *pmdp; |
| 995 | |
| 996 | native_pmd_clear(pmdp); |
| 997 | return res; |
| 998 | } |
| 999 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1000 | static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp) |
| 1001 | { |
| 1002 | pud_t res = *pudp; |
| 1003 | |
| 1004 | native_pud_clear(pudp); |
| 1005 | return res; |
| 1006 | } |
| 1007 | |
Juergen Gross | e1ac3e6 | 2020-08-15 12:06:40 +0200 | [diff] [blame] | 1008 | static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, |
| 1009 | pte_t *ptep, pte_t pte) |
Jeremy Fitzhardinge | 4891645 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1010 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1011 | page_table_check_pte_set(mm, addr, ptep, pte); |
Juergen Gross | e1ac3e6 | 2020-08-15 12:06:40 +0200 | [diff] [blame] | 1012 | set_pte(ptep, pte); |
Jeremy Fitzhardinge | 4891645 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 1015 | static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, |
| 1016 | pmd_t *pmdp, pmd_t pmd) |
Andrea Arcangeli | 0a47de5 | 2011-01-13 15:46:35 -0800 | [diff] [blame] | 1017 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1018 | page_table_check_pmd_set(mm, addr, pmdp, pmd); |
Juergen Gross | 20e55bc | 2019-02-10 08:40:56 +0100 | [diff] [blame] | 1019 | set_pmd(pmdp, pmd); |
Andrea Arcangeli | 0a47de5 | 2011-01-13 15:46:35 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 1022 | static inline void set_pud_at(struct mm_struct *mm, unsigned long addr, |
| 1023 | pud_t *pudp, pud_t pud) |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1024 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1025 | page_table_check_pud_set(mm, addr, pudp, pud); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1026 | native_set_pud(pudp, pud); |
| 1027 | } |
| 1028 | |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * We only update the dirty/accessed state if we set |
| 1031 | * the dirty bit by hand in the kernel, since the hardware |
| 1032 | * will do the accessed bit for us, and we don't want to |
| 1033 | * race with other CPU's that might be updating the dirty |
| 1034 | * bit at the same time. |
| 1035 | */ |
Jeremy Fitzhardinge | bea4180 | 2008-06-25 00:18:57 -0400 | [diff] [blame] | 1036 | struct vm_area_struct; |
| 1037 | |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1038 | #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS |
Jeremy Fitzhardinge | ee5aa8d | 2008-03-17 16:37:03 -0700 | [diff] [blame] | 1039 | extern int ptep_set_access_flags(struct vm_area_struct *vma, |
| 1040 | unsigned long address, pte_t *ptep, |
| 1041 | pte_t entry, int dirty); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1042 | |
| 1043 | #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG |
Jeremy Fitzhardinge | f9fbf1a | 2008-03-17 16:37:04 -0700 | [diff] [blame] | 1044 | extern int ptep_test_and_clear_young(struct vm_area_struct *vma, |
| 1045 | unsigned long addr, pte_t *ptep); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1046 | |
| 1047 | #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH |
Jeremy Fitzhardinge | c20311e | 2008-03-17 16:37:05 -0700 | [diff] [blame] | 1048 | extern int ptep_clear_flush_young(struct vm_area_struct *vma, |
| 1049 | unsigned long address, pte_t *ptep); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1050 | |
| 1051 | #define __HAVE_ARCH_PTEP_GET_AND_CLEAR |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 1052 | static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 1053 | pte_t *ptep) |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1054 | { |
| 1055 | pte_t pte = native_ptep_get_and_clear(ptep); |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1056 | page_table_check_pte_clear(mm, addr, pte); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1057 | return pte; |
| 1058 | } |
| 1059 | |
| 1060 | #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 1061 | static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, |
| 1062 | unsigned long addr, pte_t *ptep, |
| 1063 | int full) |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1064 | { |
| 1065 | pte_t pte; |
| 1066 | if (full) { |
| 1067 | /* |
| 1068 | * Full address destruction in progress; paravirt does not |
| 1069 | * care about updates and native needs no locking |
| 1070 | */ |
| 1071 | pte = native_local_ptep_get_and_clear(ptep); |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1072 | page_table_check_pte_clear(mm, addr, pte); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1073 | } else { |
| 1074 | pte = ptep_get_and_clear(mm, addr, ptep); |
| 1075 | } |
| 1076 | return pte; |
| 1077 | } |
| 1078 | |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1079 | #define __HAVE_ARCH_PTEP_CLEAR |
| 1080 | static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, |
| 1081 | pte_t *ptep) |
| 1082 | { |
| 1083 | if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK)) |
| 1084 | ptep_get_and_clear(mm, addr, ptep); |
| 1085 | else |
| 1086 | pte_clear(mm, addr, ptep); |
| 1087 | } |
| 1088 | |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1089 | #define __HAVE_ARCH_PTEP_SET_WRPROTECT |
Joe Perches | 3cbaeaf | 2008-03-23 01:03:12 -0700 | [diff] [blame] | 1090 | static inline void ptep_set_wrprotect(struct mm_struct *mm, |
| 1091 | unsigned long addr, pte_t *ptep) |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1092 | { |
Jeremy Fitzhardinge | d8d8982 | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1093 | clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); |
Jeremy Fitzhardinge | 195466d | 2008-01-30 13:32:58 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
Jesper Juhl | 2ac1346 | 2011-12-18 01:32:09 +0100 | [diff] [blame] | 1096 | #define flush_tlb_fix_spurious_fault(vma, address) do { } while (0) |
Shaohua Li | 61c7732 | 2010-08-16 09:16:55 +0800 | [diff] [blame] | 1097 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1098 | #define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot)) |
| 1099 | |
| 1100 | #define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS |
| 1101 | extern int pmdp_set_access_flags(struct vm_area_struct *vma, |
| 1102 | unsigned long address, pmd_t *pmdp, |
| 1103 | pmd_t entry, int dirty); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1104 | extern int pudp_set_access_flags(struct vm_area_struct *vma, |
| 1105 | unsigned long address, pud_t *pudp, |
| 1106 | pud_t entry, int dirty); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1107 | |
| 1108 | #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG |
| 1109 | extern int pmdp_test_and_clear_young(struct vm_area_struct *vma, |
| 1110 | unsigned long addr, pmd_t *pmdp); |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1111 | extern int pudp_test_and_clear_young(struct vm_area_struct *vma, |
| 1112 | unsigned long addr, pud_t *pudp); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1113 | |
| 1114 | #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH |
| 1115 | extern int pmdp_clear_flush_young(struct vm_area_struct *vma, |
| 1116 | unsigned long address, pmd_t *pmdp); |
| 1117 | |
| 1118 | |
Dan Williams | e4e40e0 | 2017-11-29 16:10:10 -0800 | [diff] [blame] | 1119 | #define pmd_write pmd_write |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1120 | static inline int pmd_write(pmd_t pmd) |
| 1121 | { |
| 1122 | return pmd_flags(pmd) & _PAGE_RW; |
| 1123 | } |
| 1124 | |
Aneesh Kumar K.V | 8809aa2 | 2015-06-24 16:57:44 -0700 | [diff] [blame] | 1125 | #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR |
| 1126 | static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr, |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1127 | pmd_t *pmdp) |
| 1128 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1129 | pmd_t pmd = native_pmdp_get_and_clear(pmdp); |
| 1130 | |
| 1131 | page_table_check_pmd_clear(mm, addr, pmd); |
| 1132 | |
| 1133 | return pmd; |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1136 | #define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR |
| 1137 | static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, |
| 1138 | unsigned long addr, pud_t *pudp) |
| 1139 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1140 | pud_t pud = native_pudp_get_and_clear(pudp); |
| 1141 | |
| 1142 | page_table_check_pud_clear(mm, addr, pud); |
| 1143 | |
| 1144 | return pud; |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1147 | #define __HAVE_ARCH_PMDP_SET_WRPROTECT |
| 1148 | static inline void pmdp_set_wrprotect(struct mm_struct *mm, |
| 1149 | unsigned long addr, pmd_t *pmdp) |
| 1150 | { |
| 1151 | clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); |
Johannes Weiner | f2d6bfe | 2011-01-13 15:47:01 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Dan Williams | 1501899 | 2017-11-29 16:10:06 -0800 | [diff] [blame] | 1154 | #define pud_write pud_write |
| 1155 | static inline int pud_write(pud_t pud) |
| 1156 | { |
| 1157 | return pud_flags(pud) & _PAGE_RW; |
| 1158 | } |
| 1159 | |
Kirill A. Shutemov | 86fa949 | 2018-01-31 16:18:13 -0800 | [diff] [blame] | 1160 | #ifndef pmdp_establish |
| 1161 | #define pmdp_establish pmdp_establish |
| 1162 | static inline pmd_t pmdp_establish(struct vm_area_struct *vma, |
| 1163 | unsigned long address, pmd_t *pmdp, pmd_t pmd) |
| 1164 | { |
Pasha Tatashin | d283d42 | 2022-01-14 14:06:41 -0800 | [diff] [blame] | 1165 | page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd); |
Kirill A. Shutemov | 86fa949 | 2018-01-31 16:18:13 -0800 | [diff] [blame] | 1166 | if (IS_ENABLED(CONFIG_SMP)) { |
| 1167 | return xchg(pmdp, pmd); |
| 1168 | } else { |
| 1169 | pmd_t old = *pmdp; |
Nadav Amit | 9bc4f28 | 2018-09-02 11:14:50 -0700 | [diff] [blame] | 1170 | WRITE_ONCE(*pmdp, pmd); |
Kirill A. Shutemov | 86fa949 | 2018-01-31 16:18:13 -0800 | [diff] [blame] | 1171 | return old; |
| 1172 | } |
| 1173 | } |
| 1174 | #endif |
Joerg Roedel | 76e258a | 2018-07-18 11:40:57 +0200 | [diff] [blame] | 1175 | /* |
| 1176 | * Page table pages are page-aligned. The lower half of the top |
| 1177 | * level is used for userspace and the top half for the kernel. |
| 1178 | * |
| 1179 | * Returns true for parts of the PGD that map userspace and |
| 1180 | * false for the parts that map the kernel. |
| 1181 | */ |
| 1182 | static inline bool pgdp_maps_userspace(void *__ptr) |
| 1183 | { |
| 1184 | unsigned long ptr = (unsigned long)__ptr; |
| 1185 | |
| 1186 | return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START); |
| 1187 | } |
| 1188 | |
Steven Price | 757b2a4a | 2020-02-03 17:35:41 -0800 | [diff] [blame] | 1189 | #define pgd_leaf pgd_large |
Joerg Roedel | 76e258a | 2018-07-18 11:40:57 +0200 | [diff] [blame] | 1190 | static inline int pgd_large(pgd_t pgd) { return 0; } |
Kirill A. Shutemov | 86fa949 | 2018-01-31 16:18:13 -0800 | [diff] [blame] | 1191 | |
Joerg Roedel | 8372d66 | 2018-07-18 11:40:55 +0200 | [diff] [blame] | 1192 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 1193 | /* |
| 1194 | * All top-level PAGE_TABLE_ISOLATION page tables are order-1 pages |
| 1195 | * (8k-aligned and 8k in size). The kernel one is at the beginning 4k and |
| 1196 | * the user one is in the last 4k. To switch between them, you |
| 1197 | * just need to flip the 12th bit in their addresses. |
| 1198 | */ |
| 1199 | #define PTI_PGTABLE_SWITCH_BIT PAGE_SHIFT |
| 1200 | |
| 1201 | /* |
| 1202 | * This generates better code than the inline assembly in |
| 1203 | * __set_bit(). |
| 1204 | */ |
| 1205 | static inline void *ptr_set_bit(void *ptr, int bit) |
| 1206 | { |
| 1207 | unsigned long __ptr = (unsigned long)ptr; |
| 1208 | |
| 1209 | __ptr |= BIT(bit); |
| 1210 | return (void *)__ptr; |
| 1211 | } |
| 1212 | static inline void *ptr_clear_bit(void *ptr, int bit) |
| 1213 | { |
| 1214 | unsigned long __ptr = (unsigned long)ptr; |
| 1215 | |
| 1216 | __ptr &= ~BIT(bit); |
| 1217 | return (void *)__ptr; |
| 1218 | } |
| 1219 | |
| 1220 | static inline pgd_t *kernel_to_user_pgdp(pgd_t *pgdp) |
| 1221 | { |
| 1222 | return ptr_set_bit(pgdp, PTI_PGTABLE_SWITCH_BIT); |
| 1223 | } |
| 1224 | |
| 1225 | static inline pgd_t *user_to_kernel_pgdp(pgd_t *pgdp) |
| 1226 | { |
| 1227 | return ptr_clear_bit(pgdp, PTI_PGTABLE_SWITCH_BIT); |
| 1228 | } |
| 1229 | |
| 1230 | static inline p4d_t *kernel_to_user_p4dp(p4d_t *p4dp) |
| 1231 | { |
| 1232 | return ptr_set_bit(p4dp, PTI_PGTABLE_SWITCH_BIT); |
| 1233 | } |
| 1234 | |
| 1235 | static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp) |
| 1236 | { |
| 1237 | return ptr_clear_bit(p4dp, PTI_PGTABLE_SWITCH_BIT); |
| 1238 | } |
| 1239 | #endif /* CONFIG_PAGE_TABLE_ISOLATION */ |
| 1240 | |
Jeremy Fitzhardinge | 85958b4 | 2008-03-17 16:37:14 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * clone_pgd_range(pgd_t *dst, pgd_t *src, int count); |
| 1243 | * |
Ingo Molnar | d9f6e12 | 2021-03-18 15:28:01 +0100 | [diff] [blame] | 1244 | * dst - pointer to pgd range anywhere on a pgd page |
Jeremy Fitzhardinge | 85958b4 | 2008-03-17 16:37:14 -0700 | [diff] [blame] | 1245 | * src - "" |
| 1246 | * count - the number of pgds to copy. |
| 1247 | * |
| 1248 | * dst and src can be on the same page, but the range must not overlap, |
| 1249 | * and must not cross a page boundary. |
| 1250 | */ |
| 1251 | static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count) |
| 1252 | { |
Dave Hansen | fc2fbc8 | 2017-12-04 15:07:40 +0100 | [diff] [blame] | 1253 | memcpy(dst, src, count * sizeof(pgd_t)); |
| 1254 | #ifdef CONFIG_PAGE_TABLE_ISOLATION |
| 1255 | if (!static_cpu_has(X86_FEATURE_PTI)) |
| 1256 | return; |
| 1257 | /* Clone the user space pgd as well */ |
| 1258 | memcpy(kernel_to_user_pgdp(dst), kernel_to_user_pgdp(src), |
| 1259 | count * sizeof(pgd_t)); |
| 1260 | #endif |
Jeremy Fitzhardinge | 85958b4 | 2008-03-17 16:37:14 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Dave Hansen | 4cbeb51 | 2013-01-22 13:24:31 -0800 | [diff] [blame] | 1263 | #define PTE_SHIFT ilog2(PTRS_PER_PTE) |
| 1264 | static inline int page_level_shift(enum pg_level level) |
| 1265 | { |
| 1266 | return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT; |
| 1267 | } |
| 1268 | static inline unsigned long page_level_size(enum pg_level level) |
| 1269 | { |
| 1270 | return 1UL << page_level_shift(level); |
| 1271 | } |
| 1272 | static inline unsigned long page_level_mask(enum pg_level level) |
| 1273 | { |
| 1274 | return ~(page_level_size(level) - 1); |
| 1275 | } |
Jeremy Fitzhardinge | 85958b4 | 2008-03-17 16:37:14 -0700 | [diff] [blame] | 1276 | |
Kirill A. Shutemov | 602e018 | 2012-12-18 12:22:18 -0800 | [diff] [blame] | 1277 | /* |
| 1278 | * The x86 doesn't have any external MMU info: the kernel page |
| 1279 | * tables contain all the necessary information. |
| 1280 | */ |
| 1281 | static inline void update_mmu_cache(struct vm_area_struct *vma, |
| 1282 | unsigned long addr, pte_t *ptep) |
| 1283 | { |
| 1284 | } |
| 1285 | static inline void update_mmu_cache_pmd(struct vm_area_struct *vma, |
| 1286 | unsigned long addr, pmd_t *pmd) |
| 1287 | { |
| 1288 | } |
Matthew Wilcox | a00cc7d | 2017-02-24 14:57:02 -0800 | [diff] [blame] | 1289 | static inline void update_mmu_cache_pud(struct vm_area_struct *vma, |
| 1290 | unsigned long addr, pud_t *pud) |
| 1291 | { |
| 1292 | } |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 1293 | |
Cyrill Gorcunov | 2bf01f9 | 2014-06-04 16:08:16 -0700 | [diff] [blame] | 1294 | #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 1295 | static inline pte_t pte_swp_mksoft_dirty(pte_t pte) |
| 1296 | { |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 1297 | return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY); |
| 1298 | } |
| 1299 | |
| 1300 | static inline int pte_swp_soft_dirty(pte_t pte) |
| 1301 | { |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 1302 | return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY; |
| 1303 | } |
| 1304 | |
| 1305 | static inline pte_t pte_swp_clear_soft_dirty(pte_t pte) |
| 1306 | { |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 1307 | return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY); |
| 1308 | } |
Naoya Horiguchi | ab6e3d0 | 2017-09-08 16:11:04 -0700 | [diff] [blame] | 1309 | |
| 1310 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
| 1311 | static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd) |
| 1312 | { |
| 1313 | return pmd_set_flags(pmd, _PAGE_SWP_SOFT_DIRTY); |
| 1314 | } |
| 1315 | |
| 1316 | static inline int pmd_swp_soft_dirty(pmd_t pmd) |
| 1317 | { |
| 1318 | return pmd_flags(pmd) & _PAGE_SWP_SOFT_DIRTY; |
| 1319 | } |
| 1320 | |
| 1321 | static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) |
| 1322 | { |
| 1323 | return pmd_clear_flags(pmd, _PAGE_SWP_SOFT_DIRTY); |
| 1324 | } |
| 1325 | #endif |
Cyrill Gorcunov | 2bf01f9 | 2014-06-04 16:08:16 -0700 | [diff] [blame] | 1326 | #endif |
Cyrill Gorcunov | fa0f281 | 2013-09-11 14:22:47 -0700 | [diff] [blame] | 1327 | |
Andrea Arcangeli | 5a28106 | 2020-04-06 20:05:33 -0700 | [diff] [blame] | 1328 | #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP |
| 1329 | static inline pte_t pte_swp_mkuffd_wp(pte_t pte) |
| 1330 | { |
| 1331 | return pte_set_flags(pte, _PAGE_SWP_UFFD_WP); |
| 1332 | } |
| 1333 | |
| 1334 | static inline int pte_swp_uffd_wp(pte_t pte) |
| 1335 | { |
| 1336 | return pte_flags(pte) & _PAGE_SWP_UFFD_WP; |
| 1337 | } |
| 1338 | |
| 1339 | static inline pte_t pte_swp_clear_uffd_wp(pte_t pte) |
| 1340 | { |
| 1341 | return pte_clear_flags(pte, _PAGE_SWP_UFFD_WP); |
| 1342 | } |
Peter Xu | 2e3d5dc | 2020-04-06 20:05:57 -0700 | [diff] [blame] | 1343 | |
| 1344 | static inline pmd_t pmd_swp_mkuffd_wp(pmd_t pmd) |
| 1345 | { |
| 1346 | return pmd_set_flags(pmd, _PAGE_SWP_UFFD_WP); |
| 1347 | } |
| 1348 | |
| 1349 | static inline int pmd_swp_uffd_wp(pmd_t pmd) |
| 1350 | { |
| 1351 | return pmd_flags(pmd) & _PAGE_SWP_UFFD_WP; |
| 1352 | } |
| 1353 | |
| 1354 | static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd) |
| 1355 | { |
| 1356 | return pmd_clear_flags(pmd, _PAGE_SWP_UFFD_WP); |
| 1357 | } |
Andrea Arcangeli | 5a28106 | 2020-04-06 20:05:33 -0700 | [diff] [blame] | 1358 | #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ |
| 1359 | |
Dave Hansen | 33a709b | 2016-02-12 13:02:19 -0800 | [diff] [blame] | 1360 | static inline u16 pte_flags_pkey(unsigned long pte_flags) |
| 1361 | { |
| 1362 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
| 1363 | /* ifdef to avoid doing 59-bit shift on 32-bit values */ |
| 1364 | return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0; |
| 1365 | #else |
| 1366 | return 0; |
| 1367 | #endif |
| 1368 | } |
| 1369 | |
Kirill A. Shutemov | e585513 | 2017-06-06 14:31:20 +0300 | [diff] [blame] | 1370 | static inline bool __pkru_allows_pkey(u16 pkey, bool write) |
| 1371 | { |
| 1372 | u32 pkru = read_pkru(); |
| 1373 | |
| 1374 | if (!__pkru_allows_read(pkru, pkey)) |
| 1375 | return false; |
| 1376 | if (write && !__pkru_allows_write(pkru, pkey)) |
| 1377 | return false; |
| 1378 | |
| 1379 | return true; |
| 1380 | } |
| 1381 | |
| 1382 | /* |
| 1383 | * 'pteval' can come from a PTE, PMD or PUD. We only check |
| 1384 | * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the |
| 1385 | * same value on all 3 types. |
| 1386 | */ |
| 1387 | static inline bool __pte_access_permitted(unsigned long pteval, bool write) |
| 1388 | { |
| 1389 | unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; |
| 1390 | |
| 1391 | if (write) |
| 1392 | need_pte_bits |= _PAGE_RW; |
| 1393 | |
| 1394 | if ((pteval & need_pte_bits) != need_pte_bits) |
| 1395 | return 0; |
| 1396 | |
| 1397 | return __pkru_allows_pkey(pte_flags_pkey(pteval), write); |
| 1398 | } |
| 1399 | |
| 1400 | #define pte_access_permitted pte_access_permitted |
| 1401 | static inline bool pte_access_permitted(pte_t pte, bool write) |
| 1402 | { |
| 1403 | return __pte_access_permitted(pte_val(pte), write); |
| 1404 | } |
| 1405 | |
| 1406 | #define pmd_access_permitted pmd_access_permitted |
| 1407 | static inline bool pmd_access_permitted(pmd_t pmd, bool write) |
| 1408 | { |
| 1409 | return __pte_access_permitted(pmd_val(pmd), write); |
| 1410 | } |
| 1411 | |
| 1412 | #define pud_access_permitted pud_access_permitted |
| 1413 | static inline bool pud_access_permitted(pud_t pud, bool write) |
| 1414 | { |
| 1415 | return __pte_access_permitted(pud_val(pud), write); |
| 1416 | } |
| 1417 | |
Andi Kleen | 42e4089 | 2018-06-13 15:48:27 -0700 | [diff] [blame] | 1418 | #define __HAVE_ARCH_PFN_MODIFY_ALLOWED 1 |
| 1419 | extern bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot); |
| 1420 | |
| 1421 | static inline bool arch_has_pfn_modify_check(void) |
| 1422 | { |
| 1423 | return boot_cpu_has_bug(X86_BUG_L1TF); |
| 1424 | } |
| 1425 | |
Jia He | f2c4e59 | 2019-10-11 22:09:38 +0800 | [diff] [blame] | 1426 | #define arch_faults_on_old_pte arch_faults_on_old_pte |
| 1427 | static inline bool arch_faults_on_old_pte(void) |
| 1428 | { |
| 1429 | return false; |
| 1430 | } |
| 1431 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 1432 | #endif /* __ASSEMBLY__ */ |
| 1433 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1434 | #endif /* _ASM_X86_PGTABLE_H */ |