xref: /openbmc/linux/arch/x86/include/asm/pgtable.h (revision 5b485efc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PGTABLE_H
3 #define _ASM_X86_PGTABLE_H
4 
5 #include <linux/mem_encrypt.h>
6 #include <asm/page.h>
7 #include <asm/pgtable_types.h>
8 
9 /*
10  * Macro to mark a page protection value as UC-
11  */
12 #define pgprot_noncached(prot)						\
13 	((boot_cpu_data.x86 > 3)					\
14 	 ? (__pgprot(pgprot_val(prot) |					\
15 		     cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS)))	\
16 	 : (prot))
17 
18 #ifndef __ASSEMBLY__
19 #include <linux/spinlock.h>
20 #include <asm/x86_init.h>
21 #include <asm/pkru.h>
22 #include <asm/fpu/api.h>
23 #include <asm/coco.h>
24 #include <asm-generic/pgtable_uffd.h>
25 #include <linux/page_table_check.h>
26 
27 extern pgd_t early_top_pgt[PTRS_PER_PGD];
28 bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd);
29 
30 struct seq_file;
31 void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);
32 void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
33 				   bool user);
34 void ptdump_walk_pgd_level_checkwx(void);
35 void ptdump_walk_user_pgd_level_checkwx(void);
36 
37 /*
38  * Macros to add or remove encryption attribute
39  */
40 #define pgprot_encrypted(prot)	__pgprot(cc_mkenc(pgprot_val(prot)))
41 #define pgprot_decrypted(prot)	__pgprot(cc_mkdec(pgprot_val(prot)))
42 
43 #ifdef CONFIG_DEBUG_WX
44 #define debug_checkwx()		ptdump_walk_pgd_level_checkwx()
45 #define debug_checkwx_user()	ptdump_walk_user_pgd_level_checkwx()
46 #else
47 #define debug_checkwx()		do { } while (0)
48 #define debug_checkwx_user()	do { } while (0)
49 #endif
50 
51 /*
52  * ZERO_PAGE is a global shared page that is always zero: used
53  * for zero-mapped memory areas etc..
54  */
55 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
56 	__visible;
57 #define ZERO_PAGE(vaddr) ((void)(vaddr),virt_to_page(empty_zero_page))
58 
59 extern spinlock_t pgd_lock;
60 extern struct list_head pgd_list;
61 
62 extern struct mm_struct *pgd_page_get_mm(struct page *page);
63 
64 extern pmdval_t early_pmd_flags;
65 
66 #ifdef CONFIG_PARAVIRT_XXL
67 #include <asm/paravirt.h>
68 #else  /* !CONFIG_PARAVIRT_XXL */
69 #define set_pte(ptep, pte)		native_set_pte(ptep, pte)
70 
71 #define set_pte_atomic(ptep, pte)					\
72 	native_set_pte_atomic(ptep, pte)
73 
74 #define set_pmd(pmdp, pmd)		native_set_pmd(pmdp, pmd)
75 
76 #ifndef __PAGETABLE_P4D_FOLDED
77 #define set_pgd(pgdp, pgd)		native_set_pgd(pgdp, pgd)
78 #define pgd_clear(pgd)			(pgtable_l5_enabled() ? native_pgd_clear(pgd) : 0)
79 #endif
80 
81 #ifndef set_p4d
82 # define set_p4d(p4dp, p4d)		native_set_p4d(p4dp, p4d)
83 #endif
84 
85 #ifndef __PAGETABLE_PUD_FOLDED
86 #define p4d_clear(p4d)			native_p4d_clear(p4d)
87 #endif
88 
89 #ifndef set_pud
90 # define set_pud(pudp, pud)		native_set_pud(pudp, pud)
91 #endif
92 
93 #ifndef __PAGETABLE_PUD_FOLDED
94 #define pud_clear(pud)			native_pud_clear(pud)
95 #endif
96 
97 #define pte_clear(mm, addr, ptep)	native_pte_clear(mm, addr, ptep)
98 #define pmd_clear(pmd)			native_pmd_clear(pmd)
99 
100 #define pgd_val(x)	native_pgd_val(x)
101 #define __pgd(x)	native_make_pgd(x)
102 
103 #ifndef __PAGETABLE_P4D_FOLDED
104 #define p4d_val(x)	native_p4d_val(x)
105 #define __p4d(x)	native_make_p4d(x)
106 #endif
107 
108 #ifndef __PAGETABLE_PUD_FOLDED
109 #define pud_val(x)	native_pud_val(x)
110 #define __pud(x)	native_make_pud(x)
111 #endif
112 
113 #ifndef __PAGETABLE_PMD_FOLDED
114 #define pmd_val(x)	native_pmd_val(x)
115 #define __pmd(x)	native_make_pmd(x)
116 #endif
117 
118 #define pte_val(x)	native_pte_val(x)
119 #define __pte(x)	native_make_pte(x)
120 
121 #define arch_end_context_switch(prev)	do {} while(0)
122 #endif	/* CONFIG_PARAVIRT_XXL */
123 
124 /*
125  * The following only work if pte_present() is true.
126  * Undefined behaviour if not..
127  */
pte_dirty(pte_t pte)128 static inline bool pte_dirty(pte_t pte)
129 {
130 	return pte_flags(pte) & _PAGE_DIRTY_BITS;
131 }
132 
pte_shstk(pte_t pte)133 static inline bool pte_shstk(pte_t pte)
134 {
135 	return cpu_feature_enabled(X86_FEATURE_SHSTK) &&
136 	       (pte_flags(pte) & (_PAGE_RW | _PAGE_DIRTY)) == _PAGE_DIRTY;
137 }
138 
pte_young(pte_t pte)139 static inline int pte_young(pte_t pte)
140 {
141 	return pte_flags(pte) & _PAGE_ACCESSED;
142 }
143 
pmd_dirty(pmd_t pmd)144 static inline bool pmd_dirty(pmd_t pmd)
145 {
146 	return pmd_flags(pmd) & _PAGE_DIRTY_BITS;
147 }
148 
pmd_shstk(pmd_t pmd)149 static inline bool pmd_shstk(pmd_t pmd)
150 {
151 	return cpu_feature_enabled(X86_FEATURE_SHSTK) &&
152 	       (pmd_flags(pmd) & (_PAGE_RW | _PAGE_DIRTY | _PAGE_PSE)) ==
153 	       (_PAGE_DIRTY | _PAGE_PSE);
154 }
155 
156 #define pmd_young pmd_young
pmd_young(pmd_t pmd)157 static inline int pmd_young(pmd_t pmd)
158 {
159 	return pmd_flags(pmd) & _PAGE_ACCESSED;
160 }
161 
pud_dirty(pud_t pud)162 static inline bool pud_dirty(pud_t pud)
163 {
164 	return pud_flags(pud) & _PAGE_DIRTY_BITS;
165 }
166 
pud_young(pud_t pud)167 static inline int pud_young(pud_t pud)
168 {
169 	return pud_flags(pud) & _PAGE_ACCESSED;
170 }
171 
pte_write(pte_t pte)172 static inline int pte_write(pte_t pte)
173 {
174 	/*
175 	 * Shadow stack pages are logically writable, but do not have
176 	 * _PAGE_RW.  Check for them separately from _PAGE_RW itself.
177 	 */
178 	return (pte_flags(pte) & _PAGE_RW) || pte_shstk(pte);
179 }
180 
181 #define pmd_write pmd_write
pmd_write(pmd_t pmd)182 static inline int pmd_write(pmd_t pmd)
183 {
184 	/*
185 	 * Shadow stack pages are logically writable, but do not have
186 	 * _PAGE_RW.  Check for them separately from _PAGE_RW itself.
187 	 */
188 	return (pmd_flags(pmd) & _PAGE_RW) || pmd_shstk(pmd);
189 }
190 
191 #define pud_write pud_write
pud_write(pud_t pud)192 static inline int pud_write(pud_t pud)
193 {
194 	return pud_flags(pud) & _PAGE_RW;
195 }
196 
pte_huge(pte_t pte)197 static inline int pte_huge(pte_t pte)
198 {
199 	return pte_flags(pte) & _PAGE_PSE;
200 }
201 
pte_global(pte_t pte)202 static inline int pte_global(pte_t pte)
203 {
204 	return pte_flags(pte) & _PAGE_GLOBAL;
205 }
206 
pte_exec(pte_t pte)207 static inline int pte_exec(pte_t pte)
208 {
209 	return !(pte_flags(pte) & _PAGE_NX);
210 }
211 
pte_special(pte_t pte)212 static inline int pte_special(pte_t pte)
213 {
214 	return pte_flags(pte) & _PAGE_SPECIAL;
215 }
216 
217 /* Entries that were set to PROT_NONE are inverted */
218 
219 static inline u64 protnone_mask(u64 val);
220 
221 #define PFN_PTE_SHIFT	PAGE_SHIFT
222 
pte_pfn(pte_t pte)223 static inline unsigned long pte_pfn(pte_t pte)
224 {
225 	phys_addr_t pfn = pte_val(pte);
226 	pfn ^= protnone_mask(pfn);
227 	return (pfn & PTE_PFN_MASK) >> PAGE_SHIFT;
228 }
229 
pmd_pfn(pmd_t pmd)230 static inline unsigned long pmd_pfn(pmd_t pmd)
231 {
232 	phys_addr_t pfn = pmd_val(pmd);
233 	pfn ^= protnone_mask(pfn);
234 	return (pfn & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
235 }
236 
pud_pfn(pud_t pud)237 static inline unsigned long pud_pfn(pud_t pud)
238 {
239 	phys_addr_t pfn = pud_val(pud);
240 	pfn ^= protnone_mask(pfn);
241 	return (pfn & pud_pfn_mask(pud)) >> PAGE_SHIFT;
242 }
243 
p4d_pfn(p4d_t p4d)244 static inline unsigned long p4d_pfn(p4d_t p4d)
245 {
246 	return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT;
247 }
248 
pgd_pfn(pgd_t pgd)249 static inline unsigned long pgd_pfn(pgd_t pgd)
250 {
251 	return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
252 }
253 
254 #define p4d_leaf	p4d_large
p4d_large(p4d_t p4d)255 static inline int p4d_large(p4d_t p4d)
256 {
257 	/* No 512 GiB pages yet */
258 	return 0;
259 }
260 
261 #define pte_page(pte)	pfn_to_page(pte_pfn(pte))
262 
263 #define pmd_leaf	pmd_large
pmd_large(pmd_t pte)264 static inline int pmd_large(pmd_t pte)
265 {
266 	return pmd_flags(pte) & _PAGE_PSE;
267 }
268 
269 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
270 /* NOTE: when predicate huge page, consider also pmd_devmap, or use pmd_large */
pmd_trans_huge(pmd_t pmd)271 static inline int pmd_trans_huge(pmd_t pmd)
272 {
273 	return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
274 }
275 
276 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pud_trans_huge(pud_t pud)277 static inline int pud_trans_huge(pud_t pud)
278 {
279 	return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
280 }
281 #endif
282 
283 #define has_transparent_hugepage has_transparent_hugepage
has_transparent_hugepage(void)284 static inline int has_transparent_hugepage(void)
285 {
286 	return boot_cpu_has(X86_FEATURE_PSE);
287 }
288 
289 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
pmd_devmap(pmd_t pmd)290 static inline int pmd_devmap(pmd_t pmd)
291 {
292 	return !!(pmd_val(pmd) & _PAGE_DEVMAP);
293 }
294 
295 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
pud_devmap(pud_t pud)296 static inline int pud_devmap(pud_t pud)
297 {
298 	return !!(pud_val(pud) & _PAGE_DEVMAP);
299 }
300 #else
pud_devmap(pud_t pud)301 static inline int pud_devmap(pud_t pud)
302 {
303 	return 0;
304 }
305 #endif
306 
pgd_devmap(pgd_t pgd)307 static inline int pgd_devmap(pgd_t pgd)
308 {
309 	return 0;
310 }
311 #endif
312 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
313 
pte_set_flags(pte_t pte,pteval_t set)314 static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
315 {
316 	pteval_t v = native_pte_val(pte);
317 
318 	return native_make_pte(v | set);
319 }
320 
pte_clear_flags(pte_t pte,pteval_t clear)321 static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
322 {
323 	pteval_t v = native_pte_val(pte);
324 
325 	return native_make_pte(v & ~clear);
326 }
327 
328 /*
329  * Write protection operations can result in Dirty=1,Write=0 PTEs. But in the
330  * case of X86_FEATURE_USER_SHSTK, these PTEs denote shadow stack memory. So
331  * when creating dirty, write-protected memory, a software bit is used:
332  * _PAGE_BIT_SAVED_DIRTY. The following functions take a PTE and transition the
333  * Dirty bit to SavedDirty, and vice-vesra.
334  *
335  * This shifting is only done if needed. In the case of shifting
336  * Dirty->SavedDirty, the condition is if the PTE is Write=0. In the case of
337  * shifting SavedDirty->Dirty, the condition is Write=1.
338  */
mksaveddirty_shift(pgprotval_t v)339 static inline pgprotval_t mksaveddirty_shift(pgprotval_t v)
340 {
341 	pgprotval_t cond = (~v >> _PAGE_BIT_RW) & 1;
342 
343 	v |= ((v >> _PAGE_BIT_DIRTY) & cond) << _PAGE_BIT_SAVED_DIRTY;
344 	v &= ~(cond << _PAGE_BIT_DIRTY);
345 
346 	return v;
347 }
348 
clear_saveddirty_shift(pgprotval_t v)349 static inline pgprotval_t clear_saveddirty_shift(pgprotval_t v)
350 {
351 	pgprotval_t cond = (v >> _PAGE_BIT_RW) & 1;
352 
353 	v |= ((v >> _PAGE_BIT_SAVED_DIRTY) & cond) << _PAGE_BIT_DIRTY;
354 	v &= ~(cond << _PAGE_BIT_SAVED_DIRTY);
355 
356 	return v;
357 }
358 
pte_mksaveddirty(pte_t pte)359 static inline pte_t pte_mksaveddirty(pte_t pte)
360 {
361 	pteval_t v = native_pte_val(pte);
362 
363 	v = mksaveddirty_shift(v);
364 	return native_make_pte(v);
365 }
366 
pte_clear_saveddirty(pte_t pte)367 static inline pte_t pte_clear_saveddirty(pte_t pte)
368 {
369 	pteval_t v = native_pte_val(pte);
370 
371 	v = clear_saveddirty_shift(v);
372 	return native_make_pte(v);
373 }
374 
pte_wrprotect(pte_t pte)375 static inline pte_t pte_wrprotect(pte_t pte)
376 {
377 	pte = pte_clear_flags(pte, _PAGE_RW);
378 
379 	/*
380 	 * Blindly clearing _PAGE_RW might accidentally create
381 	 * a shadow stack PTE (Write=0,Dirty=1). Move the hardware
382 	 * dirty value to the software bit, if present.
383 	 */
384 	return pte_mksaveddirty(pte);
385 }
386 
387 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
pte_uffd_wp(pte_t pte)388 static inline int pte_uffd_wp(pte_t pte)
389 {
390 	return pte_flags(pte) & _PAGE_UFFD_WP;
391 }
392 
pte_mkuffd_wp(pte_t pte)393 static inline pte_t pte_mkuffd_wp(pte_t pte)
394 {
395 	return pte_wrprotect(pte_set_flags(pte, _PAGE_UFFD_WP));
396 }
397 
pte_clear_uffd_wp(pte_t pte)398 static inline pte_t pte_clear_uffd_wp(pte_t pte)
399 {
400 	return pte_clear_flags(pte, _PAGE_UFFD_WP);
401 }
402 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
403 
pte_mkclean(pte_t pte)404 static inline pte_t pte_mkclean(pte_t pte)
405 {
406 	return pte_clear_flags(pte, _PAGE_DIRTY_BITS);
407 }
408 
pte_mkold(pte_t pte)409 static inline pte_t pte_mkold(pte_t pte)
410 {
411 	return pte_clear_flags(pte, _PAGE_ACCESSED);
412 }
413 
pte_mkexec(pte_t pte)414 static inline pte_t pte_mkexec(pte_t pte)
415 {
416 	return pte_clear_flags(pte, _PAGE_NX);
417 }
418 
pte_mkdirty(pte_t pte)419 static inline pte_t pte_mkdirty(pte_t pte)
420 {
421 	pte = pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
422 
423 	return pte_mksaveddirty(pte);
424 }
425 
pte_mkwrite_shstk(pte_t pte)426 static inline pte_t pte_mkwrite_shstk(pte_t pte)
427 {
428 	pte = pte_clear_flags(pte, _PAGE_RW);
429 
430 	return pte_set_flags(pte, _PAGE_DIRTY);
431 }
432 
pte_mkyoung(pte_t pte)433 static inline pte_t pte_mkyoung(pte_t pte)
434 {
435 	return pte_set_flags(pte, _PAGE_ACCESSED);
436 }
437 
pte_mkwrite_novma(pte_t pte)438 static inline pte_t pte_mkwrite_novma(pte_t pte)
439 {
440 	return pte_set_flags(pte, _PAGE_RW);
441 }
442 
443 struct vm_area_struct;
444 pte_t pte_mkwrite(pte_t pte, struct vm_area_struct *vma);
445 #define pte_mkwrite pte_mkwrite
446 
pte_mkhuge(pte_t pte)447 static inline pte_t pte_mkhuge(pte_t pte)
448 {
449 	return pte_set_flags(pte, _PAGE_PSE);
450 }
451 
pte_clrhuge(pte_t pte)452 static inline pte_t pte_clrhuge(pte_t pte)
453 {
454 	return pte_clear_flags(pte, _PAGE_PSE);
455 }
456 
pte_mkglobal(pte_t pte)457 static inline pte_t pte_mkglobal(pte_t pte)
458 {
459 	return pte_set_flags(pte, _PAGE_GLOBAL);
460 }
461 
pte_clrglobal(pte_t pte)462 static inline pte_t pte_clrglobal(pte_t pte)
463 {
464 	return pte_clear_flags(pte, _PAGE_GLOBAL);
465 }
466 
pte_mkspecial(pte_t pte)467 static inline pte_t pte_mkspecial(pte_t pte)
468 {
469 	return pte_set_flags(pte, _PAGE_SPECIAL);
470 }
471 
pte_mkdevmap(pte_t pte)472 static inline pte_t pte_mkdevmap(pte_t pte)
473 {
474 	return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP);
475 }
476 
pmd_set_flags(pmd_t pmd,pmdval_t set)477 static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
478 {
479 	pmdval_t v = native_pmd_val(pmd);
480 
481 	return native_make_pmd(v | set);
482 }
483 
pmd_clear_flags(pmd_t pmd,pmdval_t clear)484 static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
485 {
486 	pmdval_t v = native_pmd_val(pmd);
487 
488 	return native_make_pmd(v & ~clear);
489 }
490 
491 /* See comments above mksaveddirty_shift() */
pmd_mksaveddirty(pmd_t pmd)492 static inline pmd_t pmd_mksaveddirty(pmd_t pmd)
493 {
494 	pmdval_t v = native_pmd_val(pmd);
495 
496 	v = mksaveddirty_shift(v);
497 	return native_make_pmd(v);
498 }
499 
500 /* See comments above mksaveddirty_shift() */
pmd_clear_saveddirty(pmd_t pmd)501 static inline pmd_t pmd_clear_saveddirty(pmd_t pmd)
502 {
503 	pmdval_t v = native_pmd_val(pmd);
504 
505 	v = clear_saveddirty_shift(v);
506 	return native_make_pmd(v);
507 }
508 
pmd_wrprotect(pmd_t pmd)509 static inline pmd_t pmd_wrprotect(pmd_t pmd)
510 {
511 	pmd = pmd_clear_flags(pmd, _PAGE_RW);
512 
513 	/*
514 	 * Blindly clearing _PAGE_RW might accidentally create
515 	 * a shadow stack PMD (RW=0, Dirty=1). Move the hardware
516 	 * dirty value to the software bit.
517 	 */
518 	return pmd_mksaveddirty(pmd);
519 }
520 
521 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
pmd_uffd_wp(pmd_t pmd)522 static inline int pmd_uffd_wp(pmd_t pmd)
523 {
524 	return pmd_flags(pmd) & _PAGE_UFFD_WP;
525 }
526 
pmd_mkuffd_wp(pmd_t pmd)527 static inline pmd_t pmd_mkuffd_wp(pmd_t pmd)
528 {
529 	return pmd_wrprotect(pmd_set_flags(pmd, _PAGE_UFFD_WP));
530 }
531 
pmd_clear_uffd_wp(pmd_t pmd)532 static inline pmd_t pmd_clear_uffd_wp(pmd_t pmd)
533 {
534 	return pmd_clear_flags(pmd, _PAGE_UFFD_WP);
535 }
536 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
537 
pmd_mkold(pmd_t pmd)538 static inline pmd_t pmd_mkold(pmd_t pmd)
539 {
540 	return pmd_clear_flags(pmd, _PAGE_ACCESSED);
541 }
542 
pmd_mkclean(pmd_t pmd)543 static inline pmd_t pmd_mkclean(pmd_t pmd)
544 {
545 	return pmd_clear_flags(pmd, _PAGE_DIRTY_BITS);
546 }
547 
pmd_mkdirty(pmd_t pmd)548 static inline pmd_t pmd_mkdirty(pmd_t pmd)
549 {
550 	pmd = pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
551 
552 	return pmd_mksaveddirty(pmd);
553 }
554 
pmd_mkwrite_shstk(pmd_t pmd)555 static inline pmd_t pmd_mkwrite_shstk(pmd_t pmd)
556 {
557 	pmd = pmd_clear_flags(pmd, _PAGE_RW);
558 
559 	return pmd_set_flags(pmd, _PAGE_DIRTY);
560 }
561 
pmd_mkdevmap(pmd_t pmd)562 static inline pmd_t pmd_mkdevmap(pmd_t pmd)
563 {
564 	return pmd_set_flags(pmd, _PAGE_DEVMAP);
565 }
566 
pmd_mkhuge(pmd_t pmd)567 static inline pmd_t pmd_mkhuge(pmd_t pmd)
568 {
569 	return pmd_set_flags(pmd, _PAGE_PSE);
570 }
571 
pmd_mkyoung(pmd_t pmd)572 static inline pmd_t pmd_mkyoung(pmd_t pmd)
573 {
574 	return pmd_set_flags(pmd, _PAGE_ACCESSED);
575 }
576 
pmd_mkwrite_novma(pmd_t pmd)577 static inline pmd_t pmd_mkwrite_novma(pmd_t pmd)
578 {
579 	return pmd_set_flags(pmd, _PAGE_RW);
580 }
581 
582 pmd_t pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
583 #define pmd_mkwrite pmd_mkwrite
584 
pud_set_flags(pud_t pud,pudval_t set)585 static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
586 {
587 	pudval_t v = native_pud_val(pud);
588 
589 	return native_make_pud(v | set);
590 }
591 
pud_clear_flags(pud_t pud,pudval_t clear)592 static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
593 {
594 	pudval_t v = native_pud_val(pud);
595 
596 	return native_make_pud(v & ~clear);
597 }
598 
599 /* See comments above mksaveddirty_shift() */
pud_mksaveddirty(pud_t pud)600 static inline pud_t pud_mksaveddirty(pud_t pud)
601 {
602 	pudval_t v = native_pud_val(pud);
603 
604 	v = mksaveddirty_shift(v);
605 	return native_make_pud(v);
606 }
607 
608 /* See comments above mksaveddirty_shift() */
pud_clear_saveddirty(pud_t pud)609 static inline pud_t pud_clear_saveddirty(pud_t pud)
610 {
611 	pudval_t v = native_pud_val(pud);
612 
613 	v = clear_saveddirty_shift(v);
614 	return native_make_pud(v);
615 }
616 
pud_mkold(pud_t pud)617 static inline pud_t pud_mkold(pud_t pud)
618 {
619 	return pud_clear_flags(pud, _PAGE_ACCESSED);
620 }
621 
pud_mkclean(pud_t pud)622 static inline pud_t pud_mkclean(pud_t pud)
623 {
624 	return pud_clear_flags(pud, _PAGE_DIRTY_BITS);
625 }
626 
pud_wrprotect(pud_t pud)627 static inline pud_t pud_wrprotect(pud_t pud)
628 {
629 	pud = pud_clear_flags(pud, _PAGE_RW);
630 
631 	/*
632 	 * Blindly clearing _PAGE_RW might accidentally create
633 	 * a shadow stack PUD (RW=0, Dirty=1). Move the hardware
634 	 * dirty value to the software bit.
635 	 */
636 	return pud_mksaveddirty(pud);
637 }
638 
pud_mkdirty(pud_t pud)639 static inline pud_t pud_mkdirty(pud_t pud)
640 {
641 	pud = pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
642 
643 	return pud_mksaveddirty(pud);
644 }
645 
pud_mkdevmap(pud_t pud)646 static inline pud_t pud_mkdevmap(pud_t pud)
647 {
648 	return pud_set_flags(pud, _PAGE_DEVMAP);
649 }
650 
pud_mkhuge(pud_t pud)651 static inline pud_t pud_mkhuge(pud_t pud)
652 {
653 	return pud_set_flags(pud, _PAGE_PSE);
654 }
655 
pud_mkyoung(pud_t pud)656 static inline pud_t pud_mkyoung(pud_t pud)
657 {
658 	return pud_set_flags(pud, _PAGE_ACCESSED);
659 }
660 
pud_mkwrite(pud_t pud)661 static inline pud_t pud_mkwrite(pud_t pud)
662 {
663 	pud = pud_set_flags(pud, _PAGE_RW);
664 
665 	return pud_clear_saveddirty(pud);
666 }
667 
668 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
pte_soft_dirty(pte_t pte)669 static inline int pte_soft_dirty(pte_t pte)
670 {
671 	return pte_flags(pte) & _PAGE_SOFT_DIRTY;
672 }
673 
pmd_soft_dirty(pmd_t pmd)674 static inline int pmd_soft_dirty(pmd_t pmd)
675 {
676 	return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
677 }
678 
pud_soft_dirty(pud_t pud)679 static inline int pud_soft_dirty(pud_t pud)
680 {
681 	return pud_flags(pud) & _PAGE_SOFT_DIRTY;
682 }
683 
pte_mksoft_dirty(pte_t pte)684 static inline pte_t pte_mksoft_dirty(pte_t pte)
685 {
686 	return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
687 }
688 
pmd_mksoft_dirty(pmd_t pmd)689 static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
690 {
691 	return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
692 }
693 
pud_mksoft_dirty(pud_t pud)694 static inline pud_t pud_mksoft_dirty(pud_t pud)
695 {
696 	return pud_set_flags(pud, _PAGE_SOFT_DIRTY);
697 }
698 
pte_clear_soft_dirty(pte_t pte)699 static inline pte_t pte_clear_soft_dirty(pte_t pte)
700 {
701 	return pte_clear_flags(pte, _PAGE_SOFT_DIRTY);
702 }
703 
pmd_clear_soft_dirty(pmd_t pmd)704 static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
705 {
706 	return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
707 }
708 
pud_clear_soft_dirty(pud_t pud)709 static inline pud_t pud_clear_soft_dirty(pud_t pud)
710 {
711 	return pud_clear_flags(pud, _PAGE_SOFT_DIRTY);
712 }
713 
714 #endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
715 
716 /*
717  * Mask out unsupported bits in a present pgprot.  Non-present pgprots
718  * can use those bits for other purposes, so leave them be.
719  */
massage_pgprot(pgprot_t pgprot)720 static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
721 {
722 	pgprotval_t protval = pgprot_val(pgprot);
723 
724 	if (protval & _PAGE_PRESENT)
725 		protval &= __supported_pte_mask;
726 
727 	return protval;
728 }
729 
check_pgprot(pgprot_t pgprot)730 static inline pgprotval_t check_pgprot(pgprot_t pgprot)
731 {
732 	pgprotval_t massaged_val = massage_pgprot(pgprot);
733 
734 	/* mmdebug.h can not be included here because of dependencies */
735 #ifdef CONFIG_DEBUG_VM
736 	WARN_ONCE(pgprot_val(pgprot) != massaged_val,
737 		  "attempted to set unsupported pgprot: %016llx "
738 		  "bits: %016llx supported: %016llx\n",
739 		  (u64)pgprot_val(pgprot),
740 		  (u64)pgprot_val(pgprot) ^ massaged_val,
741 		  (u64)__supported_pte_mask);
742 #endif
743 
744 	return massaged_val;
745 }
746 
pfn_pte(unsigned long page_nr,pgprot_t pgprot)747 static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
748 {
749 	phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
750 	pfn ^= protnone_mask(pgprot_val(pgprot));
751 	pfn &= PTE_PFN_MASK;
752 	return __pte(pfn | check_pgprot(pgprot));
753 }
754 
pfn_pmd(unsigned long page_nr,pgprot_t pgprot)755 static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
756 {
757 	phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
758 	pfn ^= protnone_mask(pgprot_val(pgprot));
759 	pfn &= PHYSICAL_PMD_PAGE_MASK;
760 	return __pmd(pfn | check_pgprot(pgprot));
761 }
762 
pfn_pud(unsigned long page_nr,pgprot_t pgprot)763 static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
764 {
765 	phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
766 	pfn ^= protnone_mask(pgprot_val(pgprot));
767 	pfn &= PHYSICAL_PUD_PAGE_MASK;
768 	return __pud(pfn | check_pgprot(pgprot));
769 }
770 
pmd_mkinvalid(pmd_t pmd)771 static inline pmd_t pmd_mkinvalid(pmd_t pmd)
772 {
773 	return pfn_pmd(pmd_pfn(pmd),
774 		      __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE)));
775 }
776 
777 static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);
778 
pte_modify(pte_t pte,pgprot_t newprot)779 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
780 {
781 	pteval_t val = pte_val(pte), oldval = val;
782 	pte_t pte_result;
783 
784 	/*
785 	 * Chop off the NX bit (if present), and add the NX portion of
786 	 * the newprot (if present):
787 	 */
788 	val &= _PAGE_CHG_MASK;
789 	val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK;
790 	val = flip_protnone_guard(oldval, val, PTE_PFN_MASK);
791 
792 	pte_result = __pte(val);
793 
794 	/*
795 	 * To avoid creating Write=0,Dirty=1 PTEs, pte_modify() needs to avoid:
796 	 *  1. Marking Write=0 PTEs Dirty=1
797 	 *  2. Marking Dirty=1 PTEs Write=0
798 	 *
799 	 * The first case cannot happen because the _PAGE_CHG_MASK will filter
800 	 * out any Dirty bit passed in newprot. Handle the second case by
801 	 * going through the mksaveddirty exercise. Only do this if the old
802 	 * value was Write=1 to avoid doing this on Shadow Stack PTEs.
803 	 */
804 	if (oldval & _PAGE_RW)
805 		pte_result = pte_mksaveddirty(pte_result);
806 	else
807 		pte_result = pte_clear_saveddirty(pte_result);
808 
809 	return pte_result;
810 }
811 
pmd_modify(pmd_t pmd,pgprot_t newprot)812 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
813 {
814 	pmdval_t val = pmd_val(pmd), oldval = val;
815 	pmd_t pmd_result;
816 
817 	val &= (_HPAGE_CHG_MASK & ~_PAGE_DIRTY);
818 	val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK;
819 	val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK);
820 
821 	pmd_result = __pmd(val);
822 
823 	/*
824 	 * To avoid creating Write=0,Dirty=1 PMDs, pte_modify() needs to avoid:
825 	 *  1. Marking Write=0 PMDs Dirty=1
826 	 *  2. Marking Dirty=1 PMDs Write=0
827 	 *
828 	 * The first case cannot happen because the _PAGE_CHG_MASK will filter
829 	 * out any Dirty bit passed in newprot. Handle the second case by
830 	 * going through the mksaveddirty exercise. Only do this if the old
831 	 * value was Write=1 to avoid doing this on Shadow Stack PTEs.
832 	 */
833 	if (oldval & _PAGE_RW)
834 		pmd_result = pmd_mksaveddirty(pmd_result);
835 	else
836 		pmd_result = pmd_clear_saveddirty(pmd_result);
837 
838 	return pmd_result;
839 }
840 
841 /*
842  * mprotect needs to preserve PAT and encryption bits when updating
843  * vm_page_prot
844  */
845 #define pgprot_modify pgprot_modify
pgprot_modify(pgprot_t oldprot,pgprot_t newprot)846 static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
847 {
848 	pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
849 	pgprotval_t addbits = pgprot_val(newprot) & ~_PAGE_CHG_MASK;
850 	return __pgprot(preservebits | addbits);
851 }
852 
853 #define pte_pgprot(x) __pgprot(pte_flags(x))
854 #define pmd_pgprot(x) __pgprot(pmd_flags(x))
855 #define pud_pgprot(x) __pgprot(pud_flags(x))
856 #define p4d_pgprot(x) __pgprot(p4d_flags(x))
857 
858 #define canon_pgprot(p) __pgprot(massage_pgprot(p))
859 
is_new_memtype_allowed(u64 paddr,unsigned long size,enum page_cache_mode pcm,enum page_cache_mode new_pcm)860 static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
861 					 enum page_cache_mode pcm,
862 					 enum page_cache_mode new_pcm)
863 {
864 	/*
865 	 * PAT type is always WB for untracked ranges, so no need to check.
866 	 */
867 	if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
868 		return 1;
869 
870 	/*
871 	 * Certain new memtypes are not allowed with certain
872 	 * requested memtype:
873 	 * - request is uncached, return cannot be write-back
874 	 * - request is write-combine, return cannot be write-back
875 	 * - request is write-through, return cannot be write-back
876 	 * - request is write-through, return cannot be write-combine
877 	 */
878 	if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
879 	     new_pcm == _PAGE_CACHE_MODE_WB) ||
880 	    (pcm == _PAGE_CACHE_MODE_WC &&
881 	     new_pcm == _PAGE_CACHE_MODE_WB) ||
882 	    (pcm == _PAGE_CACHE_MODE_WT &&
883 	     new_pcm == _PAGE_CACHE_MODE_WB) ||
884 	    (pcm == _PAGE_CACHE_MODE_WT &&
885 	     new_pcm == _PAGE_CACHE_MODE_WC)) {
886 		return 0;
887 	}
888 
889 	return 1;
890 }
891 
892 pmd_t *populate_extra_pmd(unsigned long vaddr);
893 pte_t *populate_extra_pte(unsigned long vaddr);
894 
895 #ifdef CONFIG_PAGE_TABLE_ISOLATION
896 pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd);
897 
898 /*
899  * Take a PGD location (pgdp) and a pgd value that needs to be set there.
900  * Populates the user and returns the resulting PGD that must be set in
901  * the kernel copy of the page tables.
902  */
pti_set_user_pgtbl(pgd_t * pgdp,pgd_t pgd)903 static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
904 {
905 	if (!static_cpu_has(X86_FEATURE_PTI))
906 		return pgd;
907 	return __pti_set_user_pgtbl(pgdp, pgd);
908 }
909 #else   /* CONFIG_PAGE_TABLE_ISOLATION */
pti_set_user_pgtbl(pgd_t * pgdp,pgd_t pgd)910 static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
911 {
912 	return pgd;
913 }
914 #endif  /* CONFIG_PAGE_TABLE_ISOLATION */
915 
916 #endif	/* __ASSEMBLY__ */
917 
918 
919 #ifdef CONFIG_X86_32
920 # include <asm/pgtable_32.h>
921 #else
922 # include <asm/pgtable_64.h>
923 #endif
924 
925 #ifndef __ASSEMBLY__
926 #include <linux/mm_types.h>
927 #include <linux/mmdebug.h>
928 #include <linux/log2.h>
929 #include <asm/fixmap.h>
930 
pte_none(pte_t pte)931 static inline int pte_none(pte_t pte)
932 {
933 	return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK));
934 }
935 
936 #define __HAVE_ARCH_PTE_SAME
pte_same(pte_t a,pte_t b)937 static inline int pte_same(pte_t a, pte_t b)
938 {
939 	return a.pte == b.pte;
940 }
941 
pte_next_pfn(pte_t pte)942 static inline pte_t pte_next_pfn(pte_t pte)
943 {
944 	if (__pte_needs_invert(pte_val(pte)))
945 		return __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT));
946 	return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT));
947 }
948 #define pte_next_pfn	pte_next_pfn
949 
pte_present(pte_t a)950 static inline int pte_present(pte_t a)
951 {
952 	return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
953 }
954 
955 #ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
pte_devmap(pte_t a)956 static inline int pte_devmap(pte_t a)
957 {
958 	return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP;
959 }
960 #endif
961 
962 #define pte_accessible pte_accessible
pte_accessible(struct mm_struct * mm,pte_t a)963 static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
964 {
965 	if (pte_flags(a) & _PAGE_PRESENT)
966 		return true;
967 
968 	if ((pte_flags(a) & _PAGE_PROTNONE) &&
969 			atomic_read(&mm->tlb_flush_pending))
970 		return true;
971 
972 	return false;
973 }
974 
pmd_present(pmd_t pmd)975 static inline int pmd_present(pmd_t pmd)
976 {
977 	/*
978 	 * Checking for _PAGE_PSE is needed too because
979 	 * split_huge_page will temporarily clear the present bit (but
980 	 * the _PAGE_PSE flag will remain set at all times while the
981 	 * _PAGE_PRESENT bit is clear).
982 	 */
983 	return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
984 }
985 
986 #ifdef CONFIG_NUMA_BALANCING
987 /*
988  * These work without NUMA balancing but the kernel does not care. See the
989  * comment in include/linux/pgtable.h
990  */
pte_protnone(pte_t pte)991 static inline int pte_protnone(pte_t pte)
992 {
993 	return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
994 		== _PAGE_PROTNONE;
995 }
996 
pmd_protnone(pmd_t pmd)997 static inline int pmd_protnone(pmd_t pmd)
998 {
999 	return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
1000 		== _PAGE_PROTNONE;
1001 }
1002 #endif /* CONFIG_NUMA_BALANCING */
1003 
pmd_none(pmd_t pmd)1004 static inline int pmd_none(pmd_t pmd)
1005 {
1006 	/* Only check low word on 32-bit platforms, since it might be
1007 	   out of sync with upper half. */
1008 	unsigned long val = native_pmd_val(pmd);
1009 	return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0;
1010 }
1011 
pmd_page_vaddr(pmd_t pmd)1012 static inline unsigned long pmd_page_vaddr(pmd_t pmd)
1013 {
1014 	return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
1015 }
1016 
1017 /*
1018  * Currently stuck as a macro due to indirect forward reference to
1019  * linux/mmzone.h's __section_mem_map_addr() definition:
1020  */
1021 #define pmd_page(pmd)	pfn_to_page(pmd_pfn(pmd))
1022 
1023 /*
1024  * Conversion functions: convert a page and protection to a page entry,
1025  * and a page entry and page directory to the page they refer to.
1026  *
1027  * (Currently stuck as a macro because of indirect forward reference
1028  * to linux/mm.h:page_to_nid())
1029  */
1030 #define mk_pte(page, pgprot)						  \
1031 ({									  \
1032 	pgprot_t __pgprot = pgprot;					  \
1033 									  \
1034 	WARN_ON_ONCE((pgprot_val(__pgprot) & (_PAGE_DIRTY | _PAGE_RW)) == \
1035 		    _PAGE_DIRTY);					  \
1036 	pfn_pte(page_to_pfn(page), __pgprot);				  \
1037 })
1038 
pmd_bad(pmd_t pmd)1039 static inline int pmd_bad(pmd_t pmd)
1040 {
1041 	return (pmd_flags(pmd) & ~(_PAGE_USER | _PAGE_ACCESSED)) !=
1042 	       (_KERNPG_TABLE & ~_PAGE_ACCESSED);
1043 }
1044 
pages_to_mb(unsigned long npg)1045 static inline unsigned long pages_to_mb(unsigned long npg)
1046 {
1047 	return npg >> (20 - PAGE_SHIFT);
1048 }
1049 
1050 #if CONFIG_PGTABLE_LEVELS > 2
pud_none(pud_t pud)1051 static inline int pud_none(pud_t pud)
1052 {
1053 	return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
1054 }
1055 
pud_present(pud_t pud)1056 static inline int pud_present(pud_t pud)
1057 {
1058 	return pud_flags(pud) & _PAGE_PRESENT;
1059 }
1060 
pud_pgtable(pud_t pud)1061 static inline pmd_t *pud_pgtable(pud_t pud)
1062 {
1063 	return (pmd_t *)__va(pud_val(pud) & pud_pfn_mask(pud));
1064 }
1065 
1066 /*
1067  * Currently stuck as a macro due to indirect forward reference to
1068  * linux/mmzone.h's __section_mem_map_addr() definition:
1069  */
1070 #define pud_page(pud)	pfn_to_page(pud_pfn(pud))
1071 
1072 #define pud_leaf	pud_large
pud_large(pud_t pud)1073 static inline int pud_large(pud_t pud)
1074 {
1075 	return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
1076 		(_PAGE_PSE | _PAGE_PRESENT);
1077 }
1078 
pud_bad(pud_t pud)1079 static inline int pud_bad(pud_t pud)
1080 {
1081 	return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
1082 }
1083 #else
1084 #define pud_leaf	pud_large
pud_large(pud_t pud)1085 static inline int pud_large(pud_t pud)
1086 {
1087 	return 0;
1088 }
1089 #endif	/* CONFIG_PGTABLE_LEVELS > 2 */
1090 
1091 #if CONFIG_PGTABLE_LEVELS > 3
p4d_none(p4d_t p4d)1092 static inline int p4d_none(p4d_t p4d)
1093 {
1094 	return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
1095 }
1096 
p4d_present(p4d_t p4d)1097 static inline int p4d_present(p4d_t p4d)
1098 {
1099 	return p4d_flags(p4d) & _PAGE_PRESENT;
1100 }
1101 
p4d_pgtable(p4d_t p4d)1102 static inline pud_t *p4d_pgtable(p4d_t p4d)
1103 {
1104 	return (pud_t *)__va(p4d_val(p4d) & p4d_pfn_mask(p4d));
1105 }
1106 
1107 /*
1108  * Currently stuck as a macro due to indirect forward reference to
1109  * linux/mmzone.h's __section_mem_map_addr() definition:
1110  */
1111 #define p4d_page(p4d)	pfn_to_page(p4d_pfn(p4d))
1112 
p4d_bad(p4d_t p4d)1113 static inline int p4d_bad(p4d_t p4d)
1114 {
1115 	unsigned long ignore_flags = _KERNPG_TABLE | _PAGE_USER;
1116 
1117 	if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
1118 		ignore_flags |= _PAGE_NX;
1119 
1120 	return (p4d_flags(p4d) & ~ignore_flags) != 0;
1121 }
1122 #endif  /* CONFIG_PGTABLE_LEVELS > 3 */
1123 
p4d_index(unsigned long address)1124 static inline unsigned long p4d_index(unsigned long address)
1125 {
1126 	return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
1127 }
1128 
1129 #if CONFIG_PGTABLE_LEVELS > 4
pgd_present(pgd_t pgd)1130 static inline int pgd_present(pgd_t pgd)
1131 {
1132 	if (!pgtable_l5_enabled())
1133 		return 1;
1134 	return pgd_flags(pgd) & _PAGE_PRESENT;
1135 }
1136 
pgd_page_vaddr(pgd_t pgd)1137 static inline unsigned long pgd_page_vaddr(pgd_t pgd)
1138 {
1139 	return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
1140 }
1141 
1142 /*
1143  * Currently stuck as a macro due to indirect forward reference to
1144  * linux/mmzone.h's __section_mem_map_addr() definition:
1145  */
1146 #define pgd_page(pgd)	pfn_to_page(pgd_pfn(pgd))
1147 
1148 /* to find an entry in a page-table-directory. */
p4d_offset(pgd_t * pgd,unsigned long address)1149 static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
1150 {
1151 	if (!pgtable_l5_enabled())
1152 		return (p4d_t *)pgd;
1153 	return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address);
1154 }
1155 
pgd_bad(pgd_t pgd)1156 static inline int pgd_bad(pgd_t pgd)
1157 {
1158 	unsigned long ignore_flags = _PAGE_USER;
1159 
1160 	if (!pgtable_l5_enabled())
1161 		return 0;
1162 
1163 	if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
1164 		ignore_flags |= _PAGE_NX;
1165 
1166 	return (pgd_flags(pgd) & ~ignore_flags) != _KERNPG_TABLE;
1167 }
1168 
pgd_none(pgd_t pgd)1169 static inline int pgd_none(pgd_t pgd)
1170 {
1171 	if (!pgtable_l5_enabled())
1172 		return 0;
1173 	/*
1174 	 * There is no need to do a workaround for the KNL stray
1175 	 * A/D bit erratum here.  PGDs only point to page tables
1176 	 * except on 32-bit non-PAE which is not supported on
1177 	 * KNL.
1178 	 */
1179 	return !native_pgd_val(pgd);
1180 }
1181 #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
1182 
1183 #endif	/* __ASSEMBLY__ */
1184 
1185 #define KERNEL_PGD_BOUNDARY	pgd_index(PAGE_OFFSET)
1186 #define KERNEL_PGD_PTRS		(PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
1187 
1188 #ifndef __ASSEMBLY__
1189 
1190 extern int direct_gbpages;
1191 void init_mem_mapping(void);
1192 void early_alloc_pgt_buf(void);
1193 extern void memblock_find_dma_reserve(void);
1194 void __init poking_init(void);
1195 unsigned long init_memory_mapping(unsigned long start,
1196 				  unsigned long end, pgprot_t prot);
1197 
1198 #ifdef CONFIG_X86_64
1199 extern pgd_t trampoline_pgd_entry;
1200 #endif
1201 
1202 /* local pte updates need not use xchg for locking */
native_local_ptep_get_and_clear(pte_t * ptep)1203 static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
1204 {
1205 	pte_t res = *ptep;
1206 
1207 	/* Pure native function needs no input for mm, addr */
1208 	native_pte_clear(NULL, 0, ptep);
1209 	return res;
1210 }
1211 
native_local_pmdp_get_and_clear(pmd_t * pmdp)1212 static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
1213 {
1214 	pmd_t res = *pmdp;
1215 
1216 	native_pmd_clear(pmdp);
1217 	return res;
1218 }
1219 
native_local_pudp_get_and_clear(pud_t * pudp)1220 static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)
1221 {
1222 	pud_t res = *pudp;
1223 
1224 	native_pud_clear(pudp);
1225 	return res;
1226 }
1227 
set_pmd_at(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp,pmd_t pmd)1228 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1229 			      pmd_t *pmdp, pmd_t pmd)
1230 {
1231 	page_table_check_pmd_set(mm, pmdp, pmd);
1232 	set_pmd(pmdp, pmd);
1233 }
1234 
set_pud_at(struct mm_struct * mm,unsigned long addr,pud_t * pudp,pud_t pud)1235 static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
1236 			      pud_t *pudp, pud_t pud)
1237 {
1238 	page_table_check_pud_set(mm, pudp, pud);
1239 	native_set_pud(pudp, pud);
1240 }
1241 
1242 /*
1243  * We only update the dirty/accessed state if we set
1244  * the dirty bit by hand in the kernel, since the hardware
1245  * will do the accessed bit for us, and we don't want to
1246  * race with other CPU's that might be updating the dirty
1247  * bit at the same time.
1248  */
1249 struct vm_area_struct;
1250 
1251 #define  __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
1252 extern int ptep_set_access_flags(struct vm_area_struct *vma,
1253 				 unsigned long address, pte_t *ptep,
1254 				 pte_t entry, int dirty);
1255 
1256 #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
1257 extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
1258 				     unsigned long addr, pte_t *ptep);
1259 
1260 #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
1261 extern int ptep_clear_flush_young(struct vm_area_struct *vma,
1262 				  unsigned long address, pte_t *ptep);
1263 
1264 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
ptep_get_and_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)1265 static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
1266 				       pte_t *ptep)
1267 {
1268 	pte_t pte = native_ptep_get_and_clear(ptep);
1269 	page_table_check_pte_clear(mm, pte);
1270 	return pte;
1271 }
1272 
1273 #define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
ptep_get_and_clear_full(struct mm_struct * mm,unsigned long addr,pte_t * ptep,int full)1274 static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
1275 					    unsigned long addr, pte_t *ptep,
1276 					    int full)
1277 {
1278 	pte_t pte;
1279 	if (full) {
1280 		/*
1281 		 * Full address destruction in progress; paravirt does not
1282 		 * care about updates and native needs no locking
1283 		 */
1284 		pte = native_local_ptep_get_and_clear(ptep);
1285 		page_table_check_pte_clear(mm, pte);
1286 	} else {
1287 		pte = ptep_get_and_clear(mm, addr, ptep);
1288 	}
1289 	return pte;
1290 }
1291 
1292 #define __HAVE_ARCH_PTEP_SET_WRPROTECT
ptep_set_wrprotect(struct mm_struct * mm,unsigned long addr,pte_t * ptep)1293 static inline void ptep_set_wrprotect(struct mm_struct *mm,
1294 				      unsigned long addr, pte_t *ptep)
1295 {
1296 	/*
1297 	 * Avoid accidentally creating shadow stack PTEs
1298 	 * (Write=0,Dirty=1).  Use cmpxchg() to prevent races with
1299 	 * the hardware setting Dirty=1.
1300 	 */
1301 	pte_t old_pte, new_pte;
1302 
1303 	old_pte = READ_ONCE(*ptep);
1304 	do {
1305 		new_pte = pte_wrprotect(old_pte);
1306 	} while (!try_cmpxchg((long *)&ptep->pte, (long *)&old_pte, *(long *)&new_pte));
1307 }
1308 
1309 #define flush_tlb_fix_spurious_fault(vma, address, ptep) do { } while (0)
1310 
1311 #define mk_pmd(page, pgprot)   pfn_pmd(page_to_pfn(page), (pgprot))
1312 
1313 #define  __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1314 extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1315 				 unsigned long address, pmd_t *pmdp,
1316 				 pmd_t entry, int dirty);
1317 extern int pudp_set_access_flags(struct vm_area_struct *vma,
1318 				 unsigned long address, pud_t *pudp,
1319 				 pud_t entry, int dirty);
1320 
1321 #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1322 extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1323 				     unsigned long addr, pmd_t *pmdp);
1324 extern int pudp_test_and_clear_young(struct vm_area_struct *vma,
1325 				     unsigned long addr, pud_t *pudp);
1326 
1327 #define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1328 extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
1329 				  unsigned long address, pmd_t *pmdp);
1330 
1331 
1332 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
pmdp_huge_get_and_clear(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)1333 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
1334 				       pmd_t *pmdp)
1335 {
1336 	pmd_t pmd = native_pmdp_get_and_clear(pmdp);
1337 
1338 	page_table_check_pmd_clear(mm, pmd);
1339 
1340 	return pmd;
1341 }
1342 
1343 #define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
pudp_huge_get_and_clear(struct mm_struct * mm,unsigned long addr,pud_t * pudp)1344 static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
1345 					unsigned long addr, pud_t *pudp)
1346 {
1347 	pud_t pud = native_pudp_get_and_clear(pudp);
1348 
1349 	page_table_check_pud_clear(mm, pud);
1350 
1351 	return pud;
1352 }
1353 
1354 #define __HAVE_ARCH_PMDP_SET_WRPROTECT
pmdp_set_wrprotect(struct mm_struct * mm,unsigned long addr,pmd_t * pmdp)1355 static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1356 				      unsigned long addr, pmd_t *pmdp)
1357 {
1358 	/*
1359 	 * Avoid accidentally creating shadow stack PTEs
1360 	 * (Write=0,Dirty=1).  Use cmpxchg() to prevent races with
1361 	 * the hardware setting Dirty=1.
1362 	 */
1363 	pmd_t old_pmd, new_pmd;
1364 
1365 	old_pmd = READ_ONCE(*pmdp);
1366 	do {
1367 		new_pmd = pmd_wrprotect(old_pmd);
1368 	} while (!try_cmpxchg((long *)pmdp, (long *)&old_pmd, *(long *)&new_pmd));
1369 }
1370 
1371 #ifndef pmdp_establish
1372 #define pmdp_establish pmdp_establish
pmdp_establish(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp,pmd_t pmd)1373 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
1374 		unsigned long address, pmd_t *pmdp, pmd_t pmd)
1375 {
1376 	page_table_check_pmd_set(vma->vm_mm, pmdp, pmd);
1377 	if (IS_ENABLED(CONFIG_SMP)) {
1378 		return xchg(pmdp, pmd);
1379 	} else {
1380 		pmd_t old = *pmdp;
1381 		WRITE_ONCE(*pmdp, pmd);
1382 		return old;
1383 	}
1384 }
1385 #endif
1386 
1387 #define __HAVE_ARCH_PMDP_INVALIDATE_AD
1388 extern pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma,
1389 				unsigned long address, pmd_t *pmdp);
1390 
1391 /*
1392  * Page table pages are page-aligned.  The lower half of the top
1393  * level is used for userspace and the top half for the kernel.
1394  *
1395  * Returns true for parts of the PGD that map userspace and
1396  * false for the parts that map the kernel.
1397  */
pgdp_maps_userspace(void * __ptr)1398 static inline bool pgdp_maps_userspace(void *__ptr)
1399 {
1400 	unsigned long ptr = (unsigned long)__ptr;
1401 
1402 	return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
1403 }
1404 
1405 #define pgd_leaf	pgd_large
pgd_large(pgd_t pgd)1406 static inline int pgd_large(pgd_t pgd) { return 0; }
1407 
1408 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1409 /*
1410  * All top-level PAGE_TABLE_ISOLATION page tables are order-1 pages
1411  * (8k-aligned and 8k in size).  The kernel one is at the beginning 4k and
1412  * the user one is in the last 4k.  To switch between them, you
1413  * just need to flip the 12th bit in their addresses.
1414  */
1415 #define PTI_PGTABLE_SWITCH_BIT	PAGE_SHIFT
1416 
1417 /*
1418  * This generates better code than the inline assembly in
1419  * __set_bit().
1420  */
ptr_set_bit(void * ptr,int bit)1421 static inline void *ptr_set_bit(void *ptr, int bit)
1422 {
1423 	unsigned long __ptr = (unsigned long)ptr;
1424 
1425 	__ptr |= BIT(bit);
1426 	return (void *)__ptr;
1427 }
ptr_clear_bit(void * ptr,int bit)1428 static inline void *ptr_clear_bit(void *ptr, int bit)
1429 {
1430 	unsigned long __ptr = (unsigned long)ptr;
1431 
1432 	__ptr &= ~BIT(bit);
1433 	return (void *)__ptr;
1434 }
1435 
kernel_to_user_pgdp(pgd_t * pgdp)1436 static inline pgd_t *kernel_to_user_pgdp(pgd_t *pgdp)
1437 {
1438 	return ptr_set_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1439 }
1440 
user_to_kernel_pgdp(pgd_t * pgdp)1441 static inline pgd_t *user_to_kernel_pgdp(pgd_t *pgdp)
1442 {
1443 	return ptr_clear_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1444 }
1445 
kernel_to_user_p4dp(p4d_t * p4dp)1446 static inline p4d_t *kernel_to_user_p4dp(p4d_t *p4dp)
1447 {
1448 	return ptr_set_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1449 }
1450 
user_to_kernel_p4dp(p4d_t * p4dp)1451 static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp)
1452 {
1453 	return ptr_clear_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1454 }
1455 #endif /* CONFIG_PAGE_TABLE_ISOLATION */
1456 
1457 /*
1458  * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
1459  *
1460  *  dst - pointer to pgd range anywhere on a pgd page
1461  *  src - ""
1462  *  count - the number of pgds to copy.
1463  *
1464  * dst and src can be on the same page, but the range must not overlap,
1465  * and must not cross a page boundary.
1466  */
clone_pgd_range(pgd_t * dst,pgd_t * src,int count)1467 static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
1468 {
1469 	memcpy(dst, src, count * sizeof(pgd_t));
1470 #ifdef CONFIG_PAGE_TABLE_ISOLATION
1471 	if (!static_cpu_has(X86_FEATURE_PTI))
1472 		return;
1473 	/* Clone the user space pgd as well */
1474 	memcpy(kernel_to_user_pgdp(dst), kernel_to_user_pgdp(src),
1475 	       count * sizeof(pgd_t));
1476 #endif
1477 }
1478 
1479 #define PTE_SHIFT ilog2(PTRS_PER_PTE)
page_level_shift(enum pg_level level)1480 static inline int page_level_shift(enum pg_level level)
1481 {
1482 	return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
1483 }
page_level_size(enum pg_level level)1484 static inline unsigned long page_level_size(enum pg_level level)
1485 {
1486 	return 1UL << page_level_shift(level);
1487 }
page_level_mask(enum pg_level level)1488 static inline unsigned long page_level_mask(enum pg_level level)
1489 {
1490 	return ~(page_level_size(level) - 1);
1491 }
1492 
1493 /*
1494  * The x86 doesn't have any external MMU info: the kernel page
1495  * tables contain all the necessary information.
1496  */
update_mmu_cache(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)1497 static inline void update_mmu_cache(struct vm_area_struct *vma,
1498 		unsigned long addr, pte_t *ptep)
1499 {
1500 }
update_mmu_cache_range(struct vm_fault * vmf,struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr)1501 static inline void update_mmu_cache_range(struct vm_fault *vmf,
1502 		struct vm_area_struct *vma, unsigned long addr,
1503 		pte_t *ptep, unsigned int nr)
1504 {
1505 }
update_mmu_cache_pmd(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmd)1506 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1507 		unsigned long addr, pmd_t *pmd)
1508 {
1509 }
update_mmu_cache_pud(struct vm_area_struct * vma,unsigned long addr,pud_t * pud)1510 static inline void update_mmu_cache_pud(struct vm_area_struct *vma,
1511 		unsigned long addr, pud_t *pud)
1512 {
1513 }
pte_swp_mkexclusive(pte_t pte)1514 static inline pte_t pte_swp_mkexclusive(pte_t pte)
1515 {
1516 	return pte_set_flags(pte, _PAGE_SWP_EXCLUSIVE);
1517 }
1518 
pte_swp_exclusive(pte_t pte)1519 static inline int pte_swp_exclusive(pte_t pte)
1520 {
1521 	return pte_flags(pte) & _PAGE_SWP_EXCLUSIVE;
1522 }
1523 
pte_swp_clear_exclusive(pte_t pte)1524 static inline pte_t pte_swp_clear_exclusive(pte_t pte)
1525 {
1526 	return pte_clear_flags(pte, _PAGE_SWP_EXCLUSIVE);
1527 }
1528 
1529 #ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
pte_swp_mksoft_dirty(pte_t pte)1530 static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1531 {
1532 	return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1533 }
1534 
pte_swp_soft_dirty(pte_t pte)1535 static inline int pte_swp_soft_dirty(pte_t pte)
1536 {
1537 	return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
1538 }
1539 
pte_swp_clear_soft_dirty(pte_t pte)1540 static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1541 {
1542 	return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1543 }
1544 
1545 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_swp_mksoft_dirty(pmd_t pmd)1546 static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1547 {
1548 	return pmd_set_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1549 }
1550 
pmd_swp_soft_dirty(pmd_t pmd)1551 static inline int pmd_swp_soft_dirty(pmd_t pmd)
1552 {
1553 	return pmd_flags(pmd) & _PAGE_SWP_SOFT_DIRTY;
1554 }
1555 
pmd_swp_clear_soft_dirty(pmd_t pmd)1556 static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1557 {
1558 	return pmd_clear_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1559 }
1560 #endif
1561 #endif
1562 
1563 #ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
pte_swp_mkuffd_wp(pte_t pte)1564 static inline pte_t pte_swp_mkuffd_wp(pte_t pte)
1565 {
1566 	return pte_set_flags(pte, _PAGE_SWP_UFFD_WP);
1567 }
1568 
pte_swp_uffd_wp(pte_t pte)1569 static inline int pte_swp_uffd_wp(pte_t pte)
1570 {
1571 	return pte_flags(pte) & _PAGE_SWP_UFFD_WP;
1572 }
1573 
pte_swp_clear_uffd_wp(pte_t pte)1574 static inline pte_t pte_swp_clear_uffd_wp(pte_t pte)
1575 {
1576 	return pte_clear_flags(pte, _PAGE_SWP_UFFD_WP);
1577 }
1578 
pmd_swp_mkuffd_wp(pmd_t pmd)1579 static inline pmd_t pmd_swp_mkuffd_wp(pmd_t pmd)
1580 {
1581 	return pmd_set_flags(pmd, _PAGE_SWP_UFFD_WP);
1582 }
1583 
pmd_swp_uffd_wp(pmd_t pmd)1584 static inline int pmd_swp_uffd_wp(pmd_t pmd)
1585 {
1586 	return pmd_flags(pmd) & _PAGE_SWP_UFFD_WP;
1587 }
1588 
pmd_swp_clear_uffd_wp(pmd_t pmd)1589 static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd)
1590 {
1591 	return pmd_clear_flags(pmd, _PAGE_SWP_UFFD_WP);
1592 }
1593 #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
1594 
pte_flags_pkey(unsigned long pte_flags)1595 static inline u16 pte_flags_pkey(unsigned long pte_flags)
1596 {
1597 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1598 	/* ifdef to avoid doing 59-bit shift on 32-bit values */
1599 	return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0;
1600 #else
1601 	return 0;
1602 #endif
1603 }
1604 
__pkru_allows_pkey(u16 pkey,bool write)1605 static inline bool __pkru_allows_pkey(u16 pkey, bool write)
1606 {
1607 	u32 pkru = read_pkru();
1608 
1609 	if (!__pkru_allows_read(pkru, pkey))
1610 		return false;
1611 	if (write && !__pkru_allows_write(pkru, pkey))
1612 		return false;
1613 
1614 	return true;
1615 }
1616 
1617 /*
1618  * 'pteval' can come from a PTE, PMD or PUD.  We only check
1619  * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
1620  * same value on all 3 types.
1621  */
__pte_access_permitted(unsigned long pteval,bool write)1622 static inline bool __pte_access_permitted(unsigned long pteval, bool write)
1623 {
1624 	unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
1625 
1626 	/*
1627 	 * Write=0,Dirty=1 PTEs are shadow stack, which the kernel
1628 	 * shouldn't generally allow access to, but since they
1629 	 * are already Write=0, the below logic covers both cases.
1630 	 */
1631 	if (write)
1632 		need_pte_bits |= _PAGE_RW;
1633 
1634 	if ((pteval & need_pte_bits) != need_pte_bits)
1635 		return 0;
1636 
1637 	return __pkru_allows_pkey(pte_flags_pkey(pteval), write);
1638 }
1639 
1640 #define pte_access_permitted pte_access_permitted
pte_access_permitted(pte_t pte,bool write)1641 static inline bool pte_access_permitted(pte_t pte, bool write)
1642 {
1643 	return __pte_access_permitted(pte_val(pte), write);
1644 }
1645 
1646 #define pmd_access_permitted pmd_access_permitted
pmd_access_permitted(pmd_t pmd,bool write)1647 static inline bool pmd_access_permitted(pmd_t pmd, bool write)
1648 {
1649 	return __pte_access_permitted(pmd_val(pmd), write);
1650 }
1651 
1652 #define pud_access_permitted pud_access_permitted
pud_access_permitted(pud_t pud,bool write)1653 static inline bool pud_access_permitted(pud_t pud, bool write)
1654 {
1655 	return __pte_access_permitted(pud_val(pud), write);
1656 }
1657 
1658 #define __HAVE_ARCH_PFN_MODIFY_ALLOWED 1
1659 extern bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot);
1660 
arch_has_pfn_modify_check(void)1661 static inline bool arch_has_pfn_modify_check(void)
1662 {
1663 	return boot_cpu_has_bug(X86_BUG_L1TF);
1664 }
1665 
1666 #define arch_has_hw_pte_young arch_has_hw_pte_young
arch_has_hw_pte_young(void)1667 static inline bool arch_has_hw_pte_young(void)
1668 {
1669 	return true;
1670 }
1671 
1672 #define arch_check_zapped_pte arch_check_zapped_pte
1673 void arch_check_zapped_pte(struct vm_area_struct *vma, pte_t pte);
1674 
1675 #define arch_check_zapped_pmd arch_check_zapped_pmd
1676 void arch_check_zapped_pmd(struct vm_area_struct *vma, pmd_t pmd);
1677 
1678 #ifdef CONFIG_XEN_PV
1679 #define arch_has_hw_nonleaf_pmd_young arch_has_hw_nonleaf_pmd_young
arch_has_hw_nonleaf_pmd_young(void)1680 static inline bool arch_has_hw_nonleaf_pmd_young(void)
1681 {
1682 	return !cpu_feature_enabled(X86_FEATURE_XENPV);
1683 }
1684 #endif
1685 
1686 #ifdef CONFIG_PAGE_TABLE_CHECK
pte_user_accessible_page(pte_t pte)1687 static inline bool pte_user_accessible_page(pte_t pte)
1688 {
1689 	return (pte_val(pte) & _PAGE_PRESENT) && (pte_val(pte) & _PAGE_USER);
1690 }
1691 
pmd_user_accessible_page(pmd_t pmd)1692 static inline bool pmd_user_accessible_page(pmd_t pmd)
1693 {
1694 	return pmd_leaf(pmd) && (pmd_val(pmd) & _PAGE_PRESENT) && (pmd_val(pmd) & _PAGE_USER);
1695 }
1696 
pud_user_accessible_page(pud_t pud)1697 static inline bool pud_user_accessible_page(pud_t pud)
1698 {
1699 	return pud_leaf(pud) && (pud_val(pud) & _PAGE_PRESENT) && (pud_val(pud) & _PAGE_USER);
1700 }
1701 #endif
1702 
1703 #endif	/* __ASSEMBLY__ */
1704 
1705 #endif /* _ASM_X86_PGTABLE_H */
1706