blob: 8a9432fb3802b3f60e12331b23e43b78f48cddc8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_PGTABLE_H
3#define _ASM_X86_PGTABLE_H
Jeremy Fitzhardinge6c386652008-01-30 13:32:55 +01004
Tom Lendacky21729f82017-07-17 16:10:07 -05005#include <linux/mem_encrypt.h>
Ingo Molnarc47c1b12009-02-09 11:57:45 +01006#include <asm/page.h>
Jeremy Fitzhardinge8d19c992009-02-08 18:46:18 -08007#include <asm/pgtable_types.h>
Suresh Siddhab2bc2732008-09-23 14:00:36 -07008
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -08009/*
10 * Macro to mark a page protection value as UC-
11 */
Juergen Grossd85f3332014-11-03 14:01:53 +010012#define pgprot_noncached(prot) \
13 ((boot_cpu_data.x86 > 3) \
14 ? (__pgprot(pgprot_val(prot) | \
15 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -080016 : (prot))
17
Tom Lendacky21729f82017-07-17 16:10:07 -050018/*
19 * Macros to add or remove encryption attribute
20 */
21#define pgprot_encrypted(prot) __pgprot(__sme_set(pgprot_val(prot)))
22#define pgprot_decrypted(prot) __pgprot(__sme_clr(pgprot_val(prot)))
23
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010024#ifndef __ASSEMBLY__
Sebastian Andrzej Siewior35fa7452021-11-02 17:52:24 +010025#include <linux/spinlock.h>
H. Peter Anvin55a6ca22009-11-23 15:12:07 -080026#include <asm/x86_init.h>
Dave Hansen784a46612021-06-23 14:02:05 +020027#include <asm/pkru.h>
Sebastian Andrzej Siewior0d714db2019-04-03 18:41:48 +020028#include <asm/fpu/api.h>
Andrea Arcangeli5a281062020-04-06 20:05:33 -070029#include <asm-generic/pgtable_uffd.h>
Pasha Tatashind283d422022-01-14 14:06:41 -080030#include <linux/page_table_check.h>
H. Peter Anvin55a6ca22009-11-23 15:12:07 -080031
Tom Lendackyb9d05202017-07-17 16:10:11 -050032extern pgd_t early_top_pgt[PTRS_PER_PGD];
Joerg Roedel4b47cdb2020-09-07 15:15:36 +020033bool __init __early_make_pgtable(unsigned long address, pmdval_t pmd);
Tom Lendackyb9d05202017-07-17 16:10:11 -050034
Steven Pricee4552482020-02-03 17:36:11 -080035void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);
Steven Pricec5cfae12020-02-03 17:36:16 -080036void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
37 bool user);
Stephen Smalleye1a58322015-10-05 12:55:20 -040038void ptdump_walk_pgd_level_checkwx(void);
Joerg Roedeld878efc2018-08-08 13:16:40 +020039void ptdump_walk_user_pgd_level_checkwx(void);
Stephen Smalleye1a58322015-10-05 12:55:20 -040040
41#ifdef CONFIG_DEBUG_WX
Joerg Roedeld878efc2018-08-08 13:16:40 +020042#define debug_checkwx() ptdump_walk_pgd_level_checkwx()
43#define debug_checkwx_user() ptdump_walk_user_pgd_level_checkwx()
Stephen Smalleye1a58322015-10-05 12:55:20 -040044#else
Joerg Roedeld878efc2018-08-08 13:16:40 +020045#define debug_checkwx() do { } while (0)
46#define debug_checkwx_user() do { } while (0)
Stephen Smalleye1a58322015-10-05 12:55:20 -040047#endif
Borislav Petkovef6bea62014-01-18 12:48:14 +010048
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010049/*
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010050 * ZERO_PAGE is a global shared page that is always zero: used
51 * for zero-mapped memory areas etc..
52 */
Andi Kleen277d5b42013-08-05 15:02:43 -070053extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
54 __visible;
Linus Torvalds80871482019-04-29 09:51:29 -070055#define ZERO_PAGE(vaddr) ((void)(vaddr),virt_to_page(empty_zero_page))
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010056
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +010057extern spinlock_t pgd_lock;
58extern struct list_head pgd_list;
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010059
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -070060extern struct mm_struct *pgd_page_get_mm(struct page *page);
61
Tom Lendacky21729f82017-07-17 16:10:07 -050062extern pmdval_t early_pmd_flags;
63
Juergen Grossfdc02692018-08-28 09:40:25 +020064#ifdef CONFIG_PARAVIRT_XXL
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080065#include <asm/paravirt.h>
Juergen Grossfdc02692018-08-28 09:40:25 +020066#else /* !CONFIG_PARAVIRT_XXL */
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080067#define set_pte(ptep, pte) native_set_pte(ptep, pte)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080068
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080069#define set_pte_atomic(ptep, pte) \
70 native_set_pte_atomic(ptep, pte)
71
72#define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
73
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030074#ifndef __PAGETABLE_P4D_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080075#define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +030076#define pgd_clear(pgd) (pgtable_l5_enabled() ? native_pgd_clear(pgd) : 0)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080077#endif
78
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030079#ifndef set_p4d
80# define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d)
81#endif
82
83#ifndef __PAGETABLE_PUD_FOLDED
84#define p4d_clear(p4d) native_p4d_clear(p4d)
85#endif
86
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080087#ifndef set_pud
88# define set_pud(pudp, pud) native_set_pud(pudp, pud)
89#endif
90
Arnd Bergmannd0f33ac2017-03-16 16:40:24 -070091#ifndef __PAGETABLE_PUD_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080092#define pud_clear(pud) native_pud_clear(pud)
93#endif
94
95#define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
96#define pmd_clear(pmd) native_pmd_clear(pmd)
97
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080098#define pgd_val(x) native_pgd_val(x)
99#define __pgd(x) native_make_pgd(x)
100
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300101#ifndef __PAGETABLE_P4D_FOLDED
102#define p4d_val(x) native_p4d_val(x)
103#define __p4d(x) native_make_p4d(x)
104#endif
105
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -0800106#ifndef __PAGETABLE_PUD_FOLDED
107#define pud_val(x) native_pud_val(x)
108#define __pud(x) native_make_pud(x)
109#endif
110
111#ifndef __PAGETABLE_PMD_FOLDED
112#define pmd_val(x) native_pmd_val(x)
113#define __pmd(x) native_make_pmd(x)
114#endif
115
116#define pte_val(x) native_pte_val(x)
117#define __pte(x) native_make_pte(x)
118
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800119#define arch_end_context_switch(prev) do {} while(0)
Juergen Gross9bad5652018-08-28 09:40:23 +0200120#endif /* CONFIG_PARAVIRT_XXL */
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -0800121
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +0100122/*
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100123 * The following only work if pte_present() is true.
124 * Undefined behaviour if not..
125 */
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700126static inline int pte_dirty(pte_t pte)
127{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100128 return pte_flags(pte) & _PAGE_DIRTY;
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100129}
130
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700131static inline int pte_young(pte_t pte)
132{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100133 return pte_flags(pte) & _PAGE_ACCESSED;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700134}
135
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800136static inline int pmd_dirty(pmd_t pmd)
137{
138 return pmd_flags(pmd) & _PAGE_DIRTY;
139}
140
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800141static inline int pmd_young(pmd_t pmd)
142{
143 return pmd_flags(pmd) & _PAGE_ACCESSED;
144}
145
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800146static inline int pud_dirty(pud_t pud)
147{
148 return pud_flags(pud) & _PAGE_DIRTY;
149}
150
151static inline int pud_young(pud_t pud)
152{
153 return pud_flags(pud) & _PAGE_ACCESSED;
154}
155
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700156static inline int pte_write(pte_t pte)
157{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100158 return pte_flags(pte) & _PAGE_RW;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700159}
160
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700161static inline int pte_huge(pte_t pte)
162{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100163 return pte_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700164}
165
166static inline int pte_global(pte_t pte)
167{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100168 return pte_flags(pte) & _PAGE_GLOBAL;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700169}
170
171static inline int pte_exec(pte_t pte)
172{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100173 return !(pte_flags(pte) & _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700174}
175
Nick Piggin7e675132008-04-28 02:13:00 -0700176static inline int pte_special(pte_t pte)
177{
Mel Gormanc819f372015-02-12 14:58:38 -0800178 return pte_flags(pte) & _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700179}
180
Andi Kleen6b28bac2018-06-13 15:48:24 -0700181/* Entries that were set to PROT_NONE are inverted */
182
183static inline u64 protnone_mask(u64 val);
184
Hugh Dickins91030ca2008-09-09 16:42:45 +0100185static inline unsigned long pte_pfn(pte_t pte)
186{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200187 phys_addr_t pfn = pte_val(pte);
Andi Kleen6b28bac2018-06-13 15:48:24 -0700188 pfn ^= protnone_mask(pfn);
189 return (pfn & PTE_PFN_MASK) >> PAGE_SHIFT;
Hugh Dickins91030ca2008-09-09 16:42:45 +0100190}
191
Akinobu Mita087975b2009-06-27 15:35:15 +0900192static inline unsigned long pmd_pfn(pmd_t pmd)
193{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200194 phys_addr_t pfn = pmd_val(pmd);
Andi Kleen6b28bac2018-06-13 15:48:24 -0700195 pfn ^= protnone_mask(pfn);
196 return (pfn & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
Akinobu Mita087975b2009-06-27 15:35:15 +0900197}
198
Mel Gorman0ee364e2013-02-11 14:52:36 +0000199static inline unsigned long pud_pfn(pud_t pud)
200{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200201 phys_addr_t pfn = pud_val(pud);
Andi Kleen6b28bac2018-06-13 15:48:24 -0700202 pfn ^= protnone_mask(pfn);
203 return (pfn & pud_pfn_mask(pud)) >> PAGE_SHIFT;
Mel Gorman0ee364e2013-02-11 14:52:36 +0000204}
205
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300206static inline unsigned long p4d_pfn(p4d_t p4d)
207{
208 return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT;
209}
210
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500211static inline unsigned long pgd_pfn(pgd_t pgd)
212{
213 return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
214}
215
Steven Price757b2a4a2020-02-03 17:35:41 -0800216#define p4d_leaf p4d_large
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300217static inline int p4d_large(p4d_t p4d)
218{
219 /* No 512 GiB pages yet */
220 return 0;
221}
222
Hugh Dickins91030ca2008-09-09 16:42:45 +0100223#define pte_page(pte) pfn_to_page(pte_pfn(pte))
224
Steven Price757b2a4a2020-02-03 17:35:41 -0800225#define pmd_leaf pmd_large
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700226static inline int pmd_large(pmd_t pte)
227{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700228 return pmd_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700229}
230
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800231#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Fan Yang5bfea2d2020-06-04 18:22:07 +0800232/* NOTE: when predicate huge page, consider also pmd_devmap, or use pmd_large */
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800233static inline int pmd_trans_huge(pmd_t pmd)
234{
Dan Williams5c7fb562016-01-15 16:56:52 -0800235 return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800236}
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800237
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800238#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
239static inline int pud_trans_huge(pud_t pud)
240{
241 return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
242}
243#endif
244
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700245#define has_transparent_hugepage has_transparent_hugepage
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800246static inline int has_transparent_hugepage(void)
247{
Borislav Petkov16bf9222016-03-29 17:42:03 +0200248 return boot_cpu_has(X86_FEATURE_PSE);
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800249}
Dan Williams5c7fb562016-01-15 16:56:52 -0800250
Robin Murphy17596732019-07-16 16:30:47 -0700251#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
Dan Williams5c7fb562016-01-15 16:56:52 -0800252static inline int pmd_devmap(pmd_t pmd)
253{
254 return !!(pmd_val(pmd) & _PAGE_DEVMAP);
255}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800256
257#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
258static inline int pud_devmap(pud_t pud)
259{
260 return !!(pud_val(pud) & _PAGE_DEVMAP);
261}
262#else
263static inline int pud_devmap(pud_t pud)
264{
265 return 0;
266}
267#endif
Kirill A. Shutemove5855132017-06-06 14:31:20 +0300268
269static inline int pgd_devmap(pgd_t pgd)
270{
271 return 0;
272}
Dan Williams5c7fb562016-01-15 16:56:52 -0800273#endif
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800274#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
275
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800276static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
277{
278 pteval_t v = native_pte_val(pte);
279
280 return native_make_pte(v | set);
281}
282
283static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
284{
285 pteval_t v = native_pte_val(pte);
286
287 return native_make_pte(v & ~clear);
288}
289
Andrea Arcangeli5a281062020-04-06 20:05:33 -0700290#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
291static inline int pte_uffd_wp(pte_t pte)
292{
293 return pte_flags(pte) & _PAGE_UFFD_WP;
294}
295
296static inline pte_t pte_mkuffd_wp(pte_t pte)
297{
298 return pte_set_flags(pte, _PAGE_UFFD_WP);
299}
300
301static inline pte_t pte_clear_uffd_wp(pte_t pte)
302{
303 return pte_clear_flags(pte, _PAGE_UFFD_WP);
304}
305#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
306
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700307static inline pte_t pte_mkclean(pte_t pte)
308{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800309 return pte_clear_flags(pte, _PAGE_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700310}
311
312static inline pte_t pte_mkold(pte_t pte)
313{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800314 return pte_clear_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700315}
316
317static inline pte_t pte_wrprotect(pte_t pte)
318{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800319 return pte_clear_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700320}
321
322static inline pte_t pte_mkexec(pte_t pte)
323{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800324 return pte_clear_flags(pte, _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700325}
326
327static inline pte_t pte_mkdirty(pte_t pte)
328{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700329 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700330}
331
332static inline pte_t pte_mkyoung(pte_t pte)
333{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800334 return pte_set_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700335}
336
337static inline pte_t pte_mkwrite(pte_t pte)
338{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800339 return pte_set_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700340}
341
342static inline pte_t pte_mkhuge(pte_t pte)
343{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800344 return pte_set_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700345}
346
347static inline pte_t pte_clrhuge(pte_t pte)
348{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800349 return pte_clear_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700350}
351
352static inline pte_t pte_mkglobal(pte_t pte)
353{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800354 return pte_set_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700355}
356
357static inline pte_t pte_clrglobal(pte_t pte)
358{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800359 return pte_clear_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700360}
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100361
Nick Piggin7e675132008-04-28 02:13:00 -0700362static inline pte_t pte_mkspecial(pte_t pte)
363{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800364 return pte_set_flags(pte, _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700365}
366
Dan Williams01c8f1c2016-01-15 16:56:40 -0800367static inline pte_t pte_mkdevmap(pte_t pte)
368{
369 return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP);
370}
371
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800372static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
373{
374 pmdval_t v = native_pmd_val(pmd);
375
Jan Beulich842cef92018-02-19 07:48:11 -0700376 return native_make_pmd(v | set);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800377}
378
379static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
380{
381 pmdval_t v = native_pmd_val(pmd);
382
Jan Beulich842cef92018-02-19 07:48:11 -0700383 return native_make_pmd(v & ~clear);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800384}
385
Andrea Arcangeli5a281062020-04-06 20:05:33 -0700386#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
387static inline int pmd_uffd_wp(pmd_t pmd)
388{
389 return pmd_flags(pmd) & _PAGE_UFFD_WP;
390}
391
392static inline pmd_t pmd_mkuffd_wp(pmd_t pmd)
393{
394 return pmd_set_flags(pmd, _PAGE_UFFD_WP);
395}
396
397static inline pmd_t pmd_clear_uffd_wp(pmd_t pmd)
398{
399 return pmd_clear_flags(pmd, _PAGE_UFFD_WP);
400}
401#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
402
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800403static inline pmd_t pmd_mkold(pmd_t pmd)
404{
405 return pmd_clear_flags(pmd, _PAGE_ACCESSED);
406}
407
Minchan Kim590a4712016-01-15 16:55:20 -0800408static inline pmd_t pmd_mkclean(pmd_t pmd)
409{
410 return pmd_clear_flags(pmd, _PAGE_DIRTY);
411}
412
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800413static inline pmd_t pmd_wrprotect(pmd_t pmd)
414{
415 return pmd_clear_flags(pmd, _PAGE_RW);
416}
417
418static inline pmd_t pmd_mkdirty(pmd_t pmd)
419{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700420 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800421}
422
Dan Williamsf25748e32016-01-15 16:56:43 -0800423static inline pmd_t pmd_mkdevmap(pmd_t pmd)
424{
425 return pmd_set_flags(pmd, _PAGE_DEVMAP);
426}
427
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800428static inline pmd_t pmd_mkhuge(pmd_t pmd)
429{
430 return pmd_set_flags(pmd, _PAGE_PSE);
431}
432
433static inline pmd_t pmd_mkyoung(pmd_t pmd)
434{
435 return pmd_set_flags(pmd, _PAGE_ACCESSED);
436}
437
438static inline pmd_t pmd_mkwrite(pmd_t pmd)
439{
440 return pmd_set_flags(pmd, _PAGE_RW);
441}
442
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800443static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
444{
445 pudval_t v = native_pud_val(pud);
446
Jan Beulich842cef92018-02-19 07:48:11 -0700447 return native_make_pud(v | set);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800448}
449
450static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
451{
452 pudval_t v = native_pud_val(pud);
453
Jan Beulich842cef92018-02-19 07:48:11 -0700454 return native_make_pud(v & ~clear);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800455}
456
457static inline pud_t pud_mkold(pud_t pud)
458{
459 return pud_clear_flags(pud, _PAGE_ACCESSED);
460}
461
462static inline pud_t pud_mkclean(pud_t pud)
463{
464 return pud_clear_flags(pud, _PAGE_DIRTY);
465}
466
467static inline pud_t pud_wrprotect(pud_t pud)
468{
469 return pud_clear_flags(pud, _PAGE_RW);
470}
471
472static inline pud_t pud_mkdirty(pud_t pud)
473{
474 return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
475}
476
477static inline pud_t pud_mkdevmap(pud_t pud)
478{
479 return pud_set_flags(pud, _PAGE_DEVMAP);
480}
481
482static inline pud_t pud_mkhuge(pud_t pud)
483{
484 return pud_set_flags(pud, _PAGE_PSE);
485}
486
487static inline pud_t pud_mkyoung(pud_t pud)
488{
489 return pud_set_flags(pud, _PAGE_ACCESSED);
490}
491
492static inline pud_t pud_mkwrite(pud_t pud)
493{
494 return pud_set_flags(pud, _PAGE_RW);
495}
496
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700497#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700498static inline int pte_soft_dirty(pte_t pte)
499{
500 return pte_flags(pte) & _PAGE_SOFT_DIRTY;
501}
502
503static inline int pmd_soft_dirty(pmd_t pmd)
504{
505 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
506}
507
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800508static inline int pud_soft_dirty(pud_t pud)
509{
510 return pud_flags(pud) & _PAGE_SOFT_DIRTY;
511}
512
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700513static inline pte_t pte_mksoft_dirty(pte_t pte)
514{
515 return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
516}
517
518static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
519{
520 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
521}
522
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800523static inline pud_t pud_mksoft_dirty(pud_t pud)
524{
525 return pud_set_flags(pud, _PAGE_SOFT_DIRTY);
526}
527
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200528static inline pte_t pte_clear_soft_dirty(pte_t pte)
529{
530 return pte_clear_flags(pte, _PAGE_SOFT_DIRTY);
531}
532
533static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
534{
535 return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
536}
537
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800538static inline pud_t pud_clear_soft_dirty(pud_t pud)
539{
540 return pud_clear_flags(pud, _PAGE_SOFT_DIRTY);
541}
542
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700543#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
544
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800545/*
546 * Mask out unsupported bits in a present pgprot. Non-present pgprots
547 * can use those bits for other purposes, so leave them be.
548 */
549static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
550{
551 pgprotval_t protval = pgprot_val(pgprot);
552
553 if (protval & _PAGE_PRESENT)
554 protval &= __supported_pte_mask;
555
556 return protval;
557}
558
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700559static inline pgprotval_t check_pgprot(pgprot_t pgprot)
560{
561 pgprotval_t massaged_val = massage_pgprot(pgprot);
562
563 /* mmdebug.h can not be included here because of dependencies */
564#ifdef CONFIG_DEBUG_VM
565 WARN_ONCE(pgprot_val(pgprot) != massaged_val,
566 "attempted to set unsupported pgprot: %016llx "
567 "bits: %016llx supported: %016llx\n",
568 (u64)pgprot_val(pgprot),
569 (u64)pgprot_val(pgprot) ^ massaged_val,
570 (u64)__supported_pte_mask);
571#endif
572
573 return massaged_val;
574}
575
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100576static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
577{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200578 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
Andi Kleen6b28bac2018-06-13 15:48:24 -0700579 pfn ^= protnone_mask(pgprot_val(pgprot));
580 pfn &= PTE_PFN_MASK;
581 return __pte(pfn | check_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100582}
583
584static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
585{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200586 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
Andi Kleen6b28bac2018-06-13 15:48:24 -0700587 pfn ^= protnone_mask(pgprot_val(pgprot));
588 pfn &= PHYSICAL_PMD_PAGE_MASK;
589 return __pmd(pfn | check_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100590}
591
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800592static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
593{
Michal Hockoe14d7df2018-06-27 17:46:50 +0200594 phys_addr_t pfn = (phys_addr_t)page_nr << PAGE_SHIFT;
Andi Kleen6b28bac2018-06-13 15:48:24 -0700595 pfn ^= protnone_mask(pgprot_val(pgprot));
596 pfn &= PHYSICAL_PUD_PAGE_MASK;
597 return __pud(pfn | check_pgprot(pgprot));
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800598}
599
Anshuman Khandual86ec2da2020-06-03 16:03:45 -0700600static inline pmd_t pmd_mkinvalid(pmd_t pmd)
Andi Kleen0768f912018-08-07 15:09:37 -0700601{
602 return pfn_pmd(pmd_pfn(pmd),
603 __pgprot(pmd_flags(pmd) & ~(_PAGE_PRESENT|_PAGE_PROTNONE)));
604}
605
Andi Kleen6b28bac2018-06-13 15:48:24 -0700606static inline u64 flip_protnone_guard(u64 oldval, u64 val, u64 mask);
607
Ingo Molnar38472312008-01-30 13:32:57 +0100608static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
609{
Andi Kleen6b28bac2018-06-13 15:48:24 -0700610 pteval_t val = pte_val(pte), oldval = val;
Ingo Molnar38472312008-01-30 13:32:57 +0100611
612 /*
613 * Chop off the NX bit (if present), and add the NX portion of
614 * the newprot (if present):
615 */
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700616 val &= _PAGE_CHG_MASK;
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700617 val |= check_pgprot(newprot) & ~_PAGE_CHG_MASK;
Andi Kleen6b28bac2018-06-13 15:48:24 -0700618 val = flip_protnone_guard(oldval, val, PTE_PFN_MASK);
Ingo Molnar38472312008-01-30 13:32:57 +0100619 return __pte(val);
620}
621
Johannes Weinerc489f122011-01-13 15:47:02 -0800622static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
623{
Andi Kleen6b28bac2018-06-13 15:48:24 -0700624 pmdval_t val = pmd_val(pmd), oldval = val;
Johannes Weinerc489f122011-01-13 15:47:02 -0800625
626 val &= _HPAGE_CHG_MASK;
Dave Hansenfb43d6c2018-04-06 13:55:09 -0700627 val |= check_pgprot(newprot) & ~_HPAGE_CHG_MASK;
Andi Kleen6b28bac2018-06-13 15:48:24 -0700628 val = flip_protnone_guard(oldval, val, PHYSICAL_PMD_PAGE_MASK);
Johannes Weinerc489f122011-01-13 15:47:02 -0800629 return __pmd(val);
630}
631
Thomas Hellstrom6db73f12020-03-04 12:45:26 +0100632/*
633 * mprotect needs to preserve PAT and encryption bits when updating
634 * vm_page_prot
635 */
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700636#define pgprot_modify pgprot_modify
637static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
638{
639 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
Thomas Hellstrom6db73f12020-03-04 12:45:26 +0100640 pgprotval_t addbits = pgprot_val(newprot) & ~_PAGE_CHG_MASK;
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700641 return __pgprot(preservebits | addbits);
642}
643
Toshi Kanibbac8c62015-09-17 12:24:18 -0600644#define pte_pgprot(x) __pgprot(pte_flags(x))
645#define pmd_pgprot(x) __pgprot(pmd_flags(x))
646#define pud_pgprot(x) __pgprot(pud_flags(x))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300647#define p4d_pgprot(x) __pgprot(p4d_flags(x))
Andi Kleenc6ca18e2008-01-30 13:33:51 +0100648
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800649#define canon_pgprot(p) __pgprot(massage_pgprot(p))
Andi Kleen1e8e23b2008-01-30 13:33:53 +0100650
Dave Hansen316d0972018-04-20 15:20:28 -0700651static inline pgprot_t arch_filter_pgprot(pgprot_t prot)
652{
653 return canon_pgprot(prot);
654}
655
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700656static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
Juergen Grossd85f3332014-11-03 14:01:53 +0100657 enum page_cache_mode pcm,
658 enum page_cache_mode new_pcm)
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800659{
660 /*
H. Peter Anvin55a6ca22009-11-23 15:12:07 -0800661 * PAT type is always WB for untracked ranges, so no need to check.
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700662 */
H. Peter Anvin8a271382009-11-23 14:49:20 -0800663 if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700664 return 1;
665
666 /*
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800667 * Certain new memtypes are not allowed with certain
668 * requested memtype:
669 * - request is uncached, return cannot be write-back
670 * - request is write-combine, return cannot be write-back
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200671 * - request is write-through, return cannot be write-back
672 * - request is write-through, return cannot be write-combine
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800673 */
Juergen Grossd85f3332014-11-03 14:01:53 +0100674 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
675 new_pcm == _PAGE_CACHE_MODE_WB) ||
676 (pcm == _PAGE_CACHE_MODE_WC &&
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200677 new_pcm == _PAGE_CACHE_MODE_WB) ||
678 (pcm == _PAGE_CACHE_MODE_WT &&
679 new_pcm == _PAGE_CACHE_MODE_WB) ||
680 (pcm == _PAGE_CACHE_MODE_WT &&
681 new_pcm == _PAGE_CACHE_MODE_WC)) {
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800682 return 0;
683 }
684
685 return 1;
686}
687
Tejun Heo458a3e62009-02-24 11:57:21 +0900688pmd_t *populate_extra_pmd(unsigned long vaddr);
689pte_t *populate_extra_pte(unsigned long vaddr);
Joerg Roedelfcbbd972018-07-18 11:40:56 +0200690
691#ifdef CONFIG_PAGE_TABLE_ISOLATION
692pgd_t __pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd);
693
694/*
695 * Take a PGD location (pgdp) and a pgd value that needs to be set there.
696 * Populates the user and returns the resulting PGD that must be set in
697 * the kernel copy of the page tables.
698 */
699static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
700{
701 if (!static_cpu_has(X86_FEATURE_PTI))
702 return pgd;
703 return __pti_set_user_pgtbl(pgdp, pgd);
704}
705#else /* CONFIG_PAGE_TABLE_ISOLATION */
706static inline pgd_t pti_set_user_pgtbl(pgd_t *pgdp, pgd_t pgd)
707{
708 return pgd;
709}
710#endif /* CONFIG_PAGE_TABLE_ISOLATION */
711
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100712#endif /* __ASSEMBLY__ */
713
Joerg Roedelfcbbd972018-07-18 11:40:56 +0200714
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200715#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100716# include <asm/pgtable_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200717#else
David Howellsa1ce3922012-10-02 18:01:25 +0100718# include <asm/pgtable_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200719#endif
Jeremy Fitzhardinge6c386652008-01-30 13:32:55 +0100720
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800721#ifndef __ASSEMBLY__
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800722#include <linux/mm_types.h>
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -0700723#include <linux/mmdebug.h>
Dave Hansen4cbeb512013-01-22 13:24:31 -0800724#include <linux/log2.h>
Thomas Garnieref37bc32017-03-21 08:17:25 +0100725#include <asm/fixmap.h>
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800726
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800727static inline int pte_none(pte_t pte)
728{
Dave Hansen97e3c602016-07-07 17:19:12 -0700729 return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK));
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800730}
731
Jeremy Fitzhardinge8de01da2009-02-05 11:30:44 -0800732#define __HAVE_ARCH_PTE_SAME
733static inline int pte_same(pte_t a, pte_t b)
734{
735 return a.pte == b.pte;
736}
737
Jeremy Fitzhardinge7c683852009-02-05 11:30:45 -0800738static inline int pte_present(pte_t a)
739{
Mel Gormanc46a7c82014-06-04 16:06:30 -0700740 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
741}
742
Robin Murphy17596732019-07-16 16:30:47 -0700743#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
Dan Williams3565fce2016-01-15 16:56:55 -0800744static inline int pte_devmap(pte_t a)
745{
746 return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP;
747}
748#endif
749
Rik van Riel2c3cf552012-10-09 15:31:12 +0200750#define pte_accessible pte_accessible
Rik van Riel20841402013-12-18 17:08:44 -0800751static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
Rik van Riel2c3cf552012-10-09 15:31:12 +0200752{
Rik van Riel20841402013-12-18 17:08:44 -0800753 if (pte_flags(a) & _PAGE_PRESENT)
754 return true;
755
Mel Gorman21d9ee32015-02-12 14:58:32 -0800756 if ((pte_flags(a) & _PAGE_PROTNONE) &&
Arnd Bergmann36090de2022-01-14 14:06:10 -0800757 atomic_read(&mm->tlb_flush_pending))
Rik van Riel20841402013-12-18 17:08:44 -0800758 return true;
759
760 return false;
Rik van Riel2c3cf552012-10-09 15:31:12 +0200761}
762
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800763static inline int pmd_present(pmd_t pmd)
764{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700765 /*
766 * Checking for _PAGE_PSE is needed too because
767 * split_huge_page will temporarily clear the present bit (but
768 * the _PAGE_PSE flag will remain set at all times while the
769 * _PAGE_PRESENT bit is clear).
770 */
Mel Gorman21d9ee32015-02-12 14:58:32 -0800771 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800772}
773
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800774#ifdef CONFIG_NUMA_BALANCING
775/*
776 * These work without NUMA balancing but the kernel does not care. See the
Mike Rapoportca5999f2020-06-08 21:32:38 -0700777 * comment in include/linux/pgtable.h
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800778 */
779static inline int pte_protnone(pte_t pte)
780{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000781 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
782 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800783}
784
785static inline int pmd_protnone(pmd_t pmd)
786{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000787 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
788 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800789}
790#endif /* CONFIG_NUMA_BALANCING */
791
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800792static inline int pmd_none(pmd_t pmd)
793{
794 /* Only check low word on 32-bit platforms, since it might be
795 out of sync with upper half. */
Dave Hansen97e3c602016-07-07 17:19:12 -0700796 unsigned long val = native_pmd_val(pmd);
797 return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0;
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800798}
799
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800800static inline unsigned long pmd_page_vaddr(pmd_t pmd)
801{
Toshi Kanif70abb02015-09-17 12:24:17 -0600802 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800803}
804
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100805/*
806 * Currently stuck as a macro due to indirect forward reference to
807 * linux/mmzone.h's __section_mem_map_addr() definition:
808 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500809#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
Jeremy Fitzhardinge20063ca2009-02-05 11:31:00 -0800810
Jeremy Fitzhardingee24d7ee2009-02-05 11:31:01 -0800811/*
Jeremy Fitzhardinge97e28172009-02-05 11:31:05 -0800812 * Conversion functions: convert a page and protection to a page entry,
813 * and a page entry and page directory to the page they refer to.
814 *
815 * (Currently stuck as a macro because of indirect forward reference
816 * to linux/mm.h:page_to_nid())
817 */
818#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
819
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800820static inline int pmd_bad(pmd_t pmd)
821{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800822 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800823}
824
Jeremy Fitzhardingecc290ca2009-02-05 11:31:12 -0800825static inline unsigned long pages_to_mb(unsigned long npg)
826{
827 return npg >> (20 - PAGE_SHIFT);
828}
829
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700830#if CONFIG_PGTABLE_LEVELS > 2
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800831static inline int pud_none(pud_t pud)
832{
Dave Hansen97e3c602016-07-07 17:19:12 -0700833 return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800834}
835
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800836static inline int pud_present(pud_t pud)
837{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800838 return pud_flags(pud) & _PAGE_PRESENT;
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800839}
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800840
Aneesh Kumar K.V9cf6fa22021-07-07 18:09:53 -0700841static inline pmd_t *pud_pgtable(pud_t pud)
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800842{
Aneesh Kumar K.V9cf6fa22021-07-07 18:09:53 -0700843 return (pmd_t *)__va(pud_val(pud) & pud_pfn_mask(pud));
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800844}
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800845
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100846/*
847 * Currently stuck as a macro due to indirect forward reference to
848 * linux/mmzone.h's __section_mem_map_addr() definition:
849 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500850#define pud_page(pud) pfn_to_page(pud_pfn(pud))
Jeremy Fitzhardinge01ade20d5a2009-02-05 11:31:02 -0800851
Steven Price757b2a4a2020-02-03 17:35:41 -0800852#define pud_leaf pud_large
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800853static inline int pud_large(pud_t pud)
854{
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800855 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800856 (_PAGE_PSE | _PAGE_PRESENT);
857}
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800858
859static inline int pud_bad(pud_t pud)
860{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800861 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800862}
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800863#else
Steven Price757b2a4a2020-02-03 17:35:41 -0800864#define pud_leaf pud_large
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800865static inline int pud_large(pud_t pud)
866{
867 return 0;
868}
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700869#endif /* CONFIG_PGTABLE_LEVELS > 2 */
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800870
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300871#if CONFIG_PGTABLE_LEVELS > 3
872static inline int p4d_none(p4d_t p4d)
873{
874 return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
875}
876
877static inline int p4d_present(p4d_t p4d)
878{
879 return p4d_flags(p4d) & _PAGE_PRESENT;
880}
881
Aneesh Kumar K.Vdc4875f2021-07-07 18:09:56 -0700882static inline pud_t *p4d_pgtable(p4d_t p4d)
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300883{
Aneesh Kumar K.Vdc4875f2021-07-07 18:09:56 -0700884 return (pud_t *)__va(p4d_val(p4d) & p4d_pfn_mask(p4d));
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300885}
886
887/*
888 * Currently stuck as a macro due to indirect forward reference to
889 * linux/mmzone.h's __section_mem_map_addr() definition:
890 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500891#define p4d_page(p4d) pfn_to_page(p4d_pfn(p4d))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300892
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300893static inline int p4d_bad(p4d_t p4d)
894{
Dave Hansen1c4de1f2017-12-04 15:07:38 +0100895 unsigned long ignore_flags = _KERNPG_TABLE | _PAGE_USER;
896
897 if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
898 ignore_flags |= _PAGE_NX;
899
900 return (p4d_flags(p4d) & ~ignore_flags) != 0;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300901}
902#endif /* CONFIG_PGTABLE_LEVELS > 3 */
903
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300904static inline unsigned long p4d_index(unsigned long address)
905{
906 return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
907}
908
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300909#if CONFIG_PGTABLE_LEVELS > 4
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800910static inline int pgd_present(pgd_t pgd)
911{
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300912 if (!pgtable_l5_enabled())
Kirill A. Shutemov98219dd2018-02-14 21:25:40 +0300913 return 1;
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800914 return pgd_flags(pgd) & _PAGE_PRESENT;
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800915}
Jeremy Fitzhardingec5f040b2009-02-05 11:30:52 -0800916
917static inline unsigned long pgd_page_vaddr(pgd_t pgd)
918{
919 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
920}
Jeremy Fitzhardinge777cba12009-02-05 11:30:56 -0800921
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100922/*
923 * Currently stuck as a macro due to indirect forward reference to
924 * linux/mmzone.h's __section_mem_map_addr() definition:
925 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500926#define pgd_page(pgd) pfn_to_page(pgd_pfn(pgd))
Jeremy Fitzhardinge7cfb8102009-02-05 11:30:57 -0800927
928/* to find an entry in a page-table-directory. */
Kirill A. Shutemovb8c1e422018-06-26 13:03:41 +0300929static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800930{
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300931 if (!pgtable_l5_enabled())
Kirill A. Shutemov98219dd2018-02-14 21:25:40 +0300932 return (p4d_t *)pgd;
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300933 return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address);
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800934}
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800935
936static inline int pgd_bad(pgd_t pgd)
937{
Dave Hansen1c4de1f2017-12-04 15:07:38 +0100938 unsigned long ignore_flags = _PAGE_USER;
939
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300940 if (!pgtable_l5_enabled())
Kirill A. Shutemov98219dd2018-02-14 21:25:40 +0300941 return 0;
942
Dave Hansen1c4de1f2017-12-04 15:07:38 +0100943 if (IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
944 ignore_flags |= _PAGE_NX;
945
946 return (pgd_flags(pgd) & ~ignore_flags) != _KERNPG_TABLE;
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800947}
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800948
949static inline int pgd_none(pgd_t pgd)
950{
Kirill A. Shutemoved7588d2018-05-18 13:35:24 +0300951 if (!pgtable_l5_enabled())
Kirill A. Shutemov98219dd2018-02-14 21:25:40 +0300952 return 0;
Dave Hansen97e3c602016-07-07 17:19:12 -0700953 /*
954 * There is no need to do a workaround for the KNL stray
955 * A/D bit erratum here. PGDs only point to page tables
956 * except on 32-bit non-PAE which is not supported on
957 * KNL.
958 */
Jeremy Fitzhardinge26c8e312009-02-05 11:31:17 -0800959 return !native_pgd_val(pgd);
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800960}
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300961#endif /* CONFIG_PGTABLE_LEVELS > 4 */
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800962
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200963#endif /* __ASSEMBLY__ */
964
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700965#define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
966#define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
967
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100968#ifndef __ASSEMBLY__
969
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +0530970extern int direct_gbpages;
Yinghai Lu22ddfca2012-11-16 19:38:41 -0800971void init_mem_mapping(void);
Yinghai Lu8d574702012-11-16 19:38:58 -0800972void early_alloc_pgt_buf(void);
Ingo Molnar4270fd82017-01-28 12:45:40 +0100973extern void memblock_find_dma_reserve(void);
Benjamin Thiel56ce9372020-06-06 14:37:43 +0200974void __init poking_init(void);
975unsigned long init_memory_mapping(unsigned long start,
976 unsigned long end, pgprot_t prot);
Mike Rapoport88107d32020-06-08 21:33:01 -0700977
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700978#ifdef CONFIG_X86_64
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700979extern pgd_t trampoline_pgd_entry;
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700980#endif
981
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100982/* local pte updates need not use xchg for locking */
983static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
984{
985 pte_t res = *ptep;
986
987 /* Pure native function needs no input for mm, addr */
988 native_pte_clear(NULL, 0, ptep);
989 return res;
990}
991
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800992static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
993{
994 pmd_t res = *pmdp;
995
996 native_pmd_clear(pmdp);
997 return res;
998}
999
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001000static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)
1001{
1002 pud_t res = *pudp;
1003
1004 native_pud_clear(pudp);
1005 return res;
1006}
1007
Juergen Grosse1ac3e62020-08-15 12:06:40 +02001008static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
1009 pte_t *ptep, pte_t pte)
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +01001010{
Pasha Tatashind283d422022-01-14 14:06:41 -08001011 page_table_check_pte_set(mm, addr, ptep, pte);
Juergen Grosse1ac3e62020-08-15 12:06:40 +02001012 set_pte(ptep, pte);
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +01001013}
1014
Juergen Gross87930012017-09-04 12:25:27 +02001015static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
1016 pmd_t *pmdp, pmd_t pmd)
Andrea Arcangeli0a47de52011-01-13 15:46:35 -08001017{
Pasha Tatashind283d422022-01-14 14:06:41 -08001018 page_table_check_pmd_set(mm, addr, pmdp, pmd);
Juergen Gross20e55bc2019-02-10 08:40:56 +01001019 set_pmd(pmdp, pmd);
Andrea Arcangeli0a47de52011-01-13 15:46:35 -08001020}
1021
Juergen Gross87930012017-09-04 12:25:27 +02001022static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
1023 pud_t *pudp, pud_t pud)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001024{
Pasha Tatashind283d422022-01-14 14:06:41 -08001025 page_table_check_pud_set(mm, addr, pudp, pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001026 native_set_pud(pudp, pud);
1027}
1028
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001029/*
1030 * We only update the dirty/accessed state if we set
1031 * the dirty bit by hand in the kernel, since the hardware
1032 * will do the accessed bit for us, and we don't want to
1033 * race with other CPU's that might be updating the dirty
1034 * bit at the same time.
1035 */
Jeremy Fitzhardingebea41802008-06-25 00:18:57 -04001036struct vm_area_struct;
1037
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001038#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -07001039extern int ptep_set_access_flags(struct vm_area_struct *vma,
1040 unsigned long address, pte_t *ptep,
1041 pte_t entry, int dirty);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001042
1043#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -07001044extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
1045 unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001046
1047#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -07001048extern int ptep_clear_flush_young(struct vm_area_struct *vma,
1049 unsigned long address, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001050
1051#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001052static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
1053 pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001054{
1055 pte_t pte = native_ptep_get_and_clear(ptep);
Pasha Tatashind283d422022-01-14 14:06:41 -08001056 page_table_check_pte_clear(mm, addr, pte);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001057 return pte;
1058}
1059
1060#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001061static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
1062 unsigned long addr, pte_t *ptep,
1063 int full)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001064{
1065 pte_t pte;
1066 if (full) {
1067 /*
1068 * Full address destruction in progress; paravirt does not
1069 * care about updates and native needs no locking
1070 */
1071 pte = native_local_ptep_get_and_clear(ptep);
Pasha Tatashind283d422022-01-14 14:06:41 -08001072 page_table_check_pte_clear(mm, addr, pte);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001073 } else {
1074 pte = ptep_get_and_clear(mm, addr, ptep);
1075 }
1076 return pte;
1077}
1078
Pasha Tatashind283d422022-01-14 14:06:41 -08001079#define __HAVE_ARCH_PTEP_CLEAR
1080static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,
1081 pte_t *ptep)
1082{
1083 if (IS_ENABLED(CONFIG_PAGE_TABLE_CHECK))
1084 ptep_get_and_clear(mm, addr, ptep);
1085 else
1086 pte_clear(mm, addr, ptep);
1087}
1088
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001089#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001090static inline void ptep_set_wrprotect(struct mm_struct *mm,
1091 unsigned long addr, pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001092{
Jeremy Fitzhardinged8d89822008-01-30 13:32:58 +01001093 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001094}
1095
Jesper Juhl2ac13462011-12-18 01:32:09 +01001096#define flush_tlb_fix_spurious_fault(vma, address) do { } while (0)
Shaohua Li61c77322010-08-16 09:16:55 +08001097
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001098#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1099
1100#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1101extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1102 unsigned long address, pmd_t *pmdp,
1103 pmd_t entry, int dirty);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001104extern int pudp_set_access_flags(struct vm_area_struct *vma,
1105 unsigned long address, pud_t *pudp,
1106 pud_t entry, int dirty);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001107
1108#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1109extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1110 unsigned long addr, pmd_t *pmdp);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001111extern int pudp_test_and_clear_young(struct vm_area_struct *vma,
1112 unsigned long addr, pud_t *pudp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001113
1114#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1115extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
1116 unsigned long address, pmd_t *pmdp);
1117
1118
Dan Williamse4e40e02017-11-29 16:10:10 -08001119#define pmd_write pmd_write
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001120static inline int pmd_write(pmd_t pmd)
1121{
1122 return pmd_flags(pmd) & _PAGE_RW;
1123}
1124
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001125#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1126static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001127 pmd_t *pmdp)
1128{
Pasha Tatashind283d422022-01-14 14:06:41 -08001129 pmd_t pmd = native_pmdp_get_and_clear(pmdp);
1130
1131 page_table_check_pmd_clear(mm, addr, pmd);
1132
1133 return pmd;
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001134}
1135
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001136#define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
1137static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
1138 unsigned long addr, pud_t *pudp)
1139{
Pasha Tatashind283d422022-01-14 14:06:41 -08001140 pud_t pud = native_pudp_get_and_clear(pudp);
1141
1142 page_table_check_pud_clear(mm, addr, pud);
1143
1144 return pud;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001145}
1146
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001147#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1148static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1149 unsigned long addr, pmd_t *pmdp)
1150{
1151 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001152}
1153
Dan Williams15018992017-11-29 16:10:06 -08001154#define pud_write pud_write
1155static inline int pud_write(pud_t pud)
1156{
1157 return pud_flags(pud) & _PAGE_RW;
1158}
1159
Kirill A. Shutemov86fa9492018-01-31 16:18:13 -08001160#ifndef pmdp_establish
1161#define pmdp_establish pmdp_establish
1162static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
1163 unsigned long address, pmd_t *pmdp, pmd_t pmd)
1164{
Pasha Tatashind283d422022-01-14 14:06:41 -08001165 page_table_check_pmd_set(vma->vm_mm, address, pmdp, pmd);
Kirill A. Shutemov86fa9492018-01-31 16:18:13 -08001166 if (IS_ENABLED(CONFIG_SMP)) {
1167 return xchg(pmdp, pmd);
1168 } else {
1169 pmd_t old = *pmdp;
Nadav Amit9bc4f282018-09-02 11:14:50 -07001170 WRITE_ONCE(*pmdp, pmd);
Kirill A. Shutemov86fa9492018-01-31 16:18:13 -08001171 return old;
1172 }
1173}
1174#endif
Joerg Roedel76e258a2018-07-18 11:40:57 +02001175/*
1176 * Page table pages are page-aligned. The lower half of the top
1177 * level is used for userspace and the top half for the kernel.
1178 *
1179 * Returns true for parts of the PGD that map userspace and
1180 * false for the parts that map the kernel.
1181 */
1182static inline bool pgdp_maps_userspace(void *__ptr)
1183{
1184 unsigned long ptr = (unsigned long)__ptr;
1185
1186 return (((ptr & ~PAGE_MASK) / sizeof(pgd_t)) < PGD_KERNEL_START);
1187}
1188
Steven Price757b2a4a2020-02-03 17:35:41 -08001189#define pgd_leaf pgd_large
Joerg Roedel76e258a2018-07-18 11:40:57 +02001190static inline int pgd_large(pgd_t pgd) { return 0; }
Kirill A. Shutemov86fa9492018-01-31 16:18:13 -08001191
Joerg Roedel8372d662018-07-18 11:40:55 +02001192#ifdef CONFIG_PAGE_TABLE_ISOLATION
1193/*
1194 * All top-level PAGE_TABLE_ISOLATION page tables are order-1 pages
1195 * (8k-aligned and 8k in size). The kernel one is at the beginning 4k and
1196 * the user one is in the last 4k. To switch between them, you
1197 * just need to flip the 12th bit in their addresses.
1198 */
1199#define PTI_PGTABLE_SWITCH_BIT PAGE_SHIFT
1200
1201/*
1202 * This generates better code than the inline assembly in
1203 * __set_bit().
1204 */
1205static inline void *ptr_set_bit(void *ptr, int bit)
1206{
1207 unsigned long __ptr = (unsigned long)ptr;
1208
1209 __ptr |= BIT(bit);
1210 return (void *)__ptr;
1211}
1212static inline void *ptr_clear_bit(void *ptr, int bit)
1213{
1214 unsigned long __ptr = (unsigned long)ptr;
1215
1216 __ptr &= ~BIT(bit);
1217 return (void *)__ptr;
1218}
1219
1220static inline pgd_t *kernel_to_user_pgdp(pgd_t *pgdp)
1221{
1222 return ptr_set_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1223}
1224
1225static inline pgd_t *user_to_kernel_pgdp(pgd_t *pgdp)
1226{
1227 return ptr_clear_bit(pgdp, PTI_PGTABLE_SWITCH_BIT);
1228}
1229
1230static inline p4d_t *kernel_to_user_p4dp(p4d_t *p4dp)
1231{
1232 return ptr_set_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1233}
1234
1235static inline p4d_t *user_to_kernel_p4dp(p4d_t *p4dp)
1236{
1237 return ptr_clear_bit(p4dp, PTI_PGTABLE_SWITCH_BIT);
1238}
1239#endif /* CONFIG_PAGE_TABLE_ISOLATION */
1240
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001241/*
1242 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
1243 *
Ingo Molnard9f6e122021-03-18 15:28:01 +01001244 * dst - pointer to pgd range anywhere on a pgd page
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001245 * src - ""
1246 * count - the number of pgds to copy.
1247 *
1248 * dst and src can be on the same page, but the range must not overlap,
1249 * and must not cross a page boundary.
1250 */
1251static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
1252{
Dave Hansenfc2fbc82017-12-04 15:07:40 +01001253 memcpy(dst, src, count * sizeof(pgd_t));
1254#ifdef CONFIG_PAGE_TABLE_ISOLATION
1255 if (!static_cpu_has(X86_FEATURE_PTI))
1256 return;
1257 /* Clone the user space pgd as well */
1258 memcpy(kernel_to_user_pgdp(dst), kernel_to_user_pgdp(src),
1259 count * sizeof(pgd_t));
1260#endif
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001261}
1262
Dave Hansen4cbeb512013-01-22 13:24:31 -08001263#define PTE_SHIFT ilog2(PTRS_PER_PTE)
1264static inline int page_level_shift(enum pg_level level)
1265{
1266 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
1267}
1268static inline unsigned long page_level_size(enum pg_level level)
1269{
1270 return 1UL << page_level_shift(level);
1271}
1272static inline unsigned long page_level_mask(enum pg_level level)
1273{
1274 return ~(page_level_size(level) - 1);
1275}
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001276
Kirill A. Shutemov602e0182012-12-18 12:22:18 -08001277/*
1278 * The x86 doesn't have any external MMU info: the kernel page
1279 * tables contain all the necessary information.
1280 */
1281static inline void update_mmu_cache(struct vm_area_struct *vma,
1282 unsigned long addr, pte_t *ptep)
1283{
1284}
1285static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1286 unsigned long addr, pmd_t *pmd)
1287{
1288}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001289static inline void update_mmu_cache_pud(struct vm_area_struct *vma,
1290 unsigned long addr, pud_t *pud)
1291{
1292}
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001293
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001294#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001295static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1296{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001297 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1298}
1299
1300static inline int pte_swp_soft_dirty(pte_t pte)
1301{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001302 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
1303}
1304
1305static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1306{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001307 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1308}
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001309
1310#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1311static inline pmd_t pmd_swp_mksoft_dirty(pmd_t pmd)
1312{
1313 return pmd_set_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1314}
1315
1316static inline int pmd_swp_soft_dirty(pmd_t pmd)
1317{
1318 return pmd_flags(pmd) & _PAGE_SWP_SOFT_DIRTY;
1319}
1320
1321static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)
1322{
1323 return pmd_clear_flags(pmd, _PAGE_SWP_SOFT_DIRTY);
1324}
1325#endif
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001326#endif
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001327
Andrea Arcangeli5a281062020-04-06 20:05:33 -07001328#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
1329static inline pte_t pte_swp_mkuffd_wp(pte_t pte)
1330{
1331 return pte_set_flags(pte, _PAGE_SWP_UFFD_WP);
1332}
1333
1334static inline int pte_swp_uffd_wp(pte_t pte)
1335{
1336 return pte_flags(pte) & _PAGE_SWP_UFFD_WP;
1337}
1338
1339static inline pte_t pte_swp_clear_uffd_wp(pte_t pte)
1340{
1341 return pte_clear_flags(pte, _PAGE_SWP_UFFD_WP);
1342}
Peter Xu2e3d5dc2020-04-06 20:05:57 -07001343
1344static inline pmd_t pmd_swp_mkuffd_wp(pmd_t pmd)
1345{
1346 return pmd_set_flags(pmd, _PAGE_SWP_UFFD_WP);
1347}
1348
1349static inline int pmd_swp_uffd_wp(pmd_t pmd)
1350{
1351 return pmd_flags(pmd) & _PAGE_SWP_UFFD_WP;
1352}
1353
1354static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd)
1355{
1356 return pmd_clear_flags(pmd, _PAGE_SWP_UFFD_WP);
1357}
Andrea Arcangeli5a281062020-04-06 20:05:33 -07001358#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */
1359
Dave Hansen33a709b2016-02-12 13:02:19 -08001360static inline u16 pte_flags_pkey(unsigned long pte_flags)
1361{
1362#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1363 /* ifdef to avoid doing 59-bit shift on 32-bit values */
1364 return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0;
1365#else
1366 return 0;
1367#endif
1368}
1369
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001370static inline bool __pkru_allows_pkey(u16 pkey, bool write)
1371{
1372 u32 pkru = read_pkru();
1373
1374 if (!__pkru_allows_read(pkru, pkey))
1375 return false;
1376 if (write && !__pkru_allows_write(pkru, pkey))
1377 return false;
1378
1379 return true;
1380}
1381
1382/*
1383 * 'pteval' can come from a PTE, PMD or PUD. We only check
1384 * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
1385 * same value on all 3 types.
1386 */
1387static inline bool __pte_access_permitted(unsigned long pteval, bool write)
1388{
1389 unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
1390
1391 if (write)
1392 need_pte_bits |= _PAGE_RW;
1393
1394 if ((pteval & need_pte_bits) != need_pte_bits)
1395 return 0;
1396
1397 return __pkru_allows_pkey(pte_flags_pkey(pteval), write);
1398}
1399
1400#define pte_access_permitted pte_access_permitted
1401static inline bool pte_access_permitted(pte_t pte, bool write)
1402{
1403 return __pte_access_permitted(pte_val(pte), write);
1404}
1405
1406#define pmd_access_permitted pmd_access_permitted
1407static inline bool pmd_access_permitted(pmd_t pmd, bool write)
1408{
1409 return __pte_access_permitted(pmd_val(pmd), write);
1410}
1411
1412#define pud_access_permitted pud_access_permitted
1413static inline bool pud_access_permitted(pud_t pud, bool write)
1414{
1415 return __pte_access_permitted(pud_val(pud), write);
1416}
1417
Andi Kleen42e40892018-06-13 15:48:27 -07001418#define __HAVE_ARCH_PFN_MODIFY_ALLOWED 1
1419extern bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot);
1420
1421static inline bool arch_has_pfn_modify_check(void)
1422{
1423 return boot_cpu_has_bug(X86_BUG_L1TF);
1424}
1425
Jia Hef2c4e592019-10-11 22:09:38 +08001426#define arch_faults_on_old_pte arch_faults_on_old_pte
1427static inline bool arch_faults_on_old_pte(void)
1428{
1429 return false;
1430}
1431
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001432#endif /* __ASSEMBLY__ */
1433
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001434#endif /* _ASM_X86_PGTABLE_H */