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