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