blob: bbeae4a2bd01a3209e6d68f8f2af918eeb17dff2 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_PGTABLE_H
2#define _ASM_X86_PGTABLE_H
Jeremy Fitzhardinge6c386652008-01-30 13:32:55 +01003
Tom Lendacky21729f82017-07-17 16:10:07 -05004#include <linux/mem_encrypt.h>
Ingo Molnarc47c1b12009-02-09 11:57:45 +01005#include <asm/page.h>
Jeremy Fitzhardinge8d19c992009-02-08 18:46:18 -08006#include <asm/pgtable_types.h>
Suresh Siddhab2bc2732008-09-23 14:00:36 -07007
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -08008/*
9 * Macro to mark a page protection value as UC-
10 */
Juergen Grossd85f3332014-11-03 14:01:53 +010011#define pgprot_noncached(prot) \
12 ((boot_cpu_data.x86 > 3) \
13 ? (__pgprot(pgprot_val(prot) | \
14 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -080015 : (prot))
16
Tom Lendacky21729f82017-07-17 16:10:07 -050017/*
18 * Macros to add or remove encryption attribute
19 */
20#define pgprot_encrypted(prot) __pgprot(__sme_set(pgprot_val(prot)))
21#define pgprot_decrypted(prot) __pgprot(__sme_clr(pgprot_val(prot)))
22
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010023#ifndef __ASSEMBLY__
H. Peter Anvin55a6ca22009-11-23 15:12:07 -080024#include <asm/x86_init.h>
25
Tom Lendackyb9d05202017-07-17 16:10:11 -050026extern pgd_t early_top_pgt[PTRS_PER_PGD];
27int __init __early_make_pgtable(unsigned long address, pmdval_t pmd);
28
Borislav Petkovef6bea62014-01-18 12:48:14 +010029void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd);
Stephen Smalleye1a58322015-10-05 12:55:20 -040030void ptdump_walk_pgd_level_checkwx(void);
31
32#ifdef CONFIG_DEBUG_WX
33#define debug_checkwx() ptdump_walk_pgd_level_checkwx()
34#else
35#define debug_checkwx() do { } while (0)
36#endif
Borislav Petkovef6bea62014-01-18 12:48:14 +010037
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010038/*
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010039 * ZERO_PAGE is a global shared page that is always zero: used
40 * for zero-mapped memory areas etc..
41 */
Andi Kleen277d5b42013-08-05 15:02:43 -070042extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
43 __visible;
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010044#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
45
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +010046extern spinlock_t pgd_lock;
47extern struct list_head pgd_list;
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010048
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -070049extern struct mm_struct *pgd_page_get_mm(struct page *page);
50
Tom Lendacky21729f82017-07-17 16:10:07 -050051extern pmdval_t early_pmd_flags;
52
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080053#ifdef CONFIG_PARAVIRT
54#include <asm/paravirt.h>
55#else /* !CONFIG_PARAVIRT */
56#define set_pte(ptep, pte) native_set_pte(ptep, pte)
57#define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
Andrea Arcangeli2609ae62011-01-13 15:46:37 -080058#define set_pmd_at(mm, addr, pmdp, pmd) native_set_pmd_at(mm, addr, pmdp, pmd)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080059#define set_pud_at(mm, addr, pudp, pud) native_set_pud_at(mm, addr, pudp, pud)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080060
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080061#define set_pte_atomic(ptep, pte) \
62 native_set_pte_atomic(ptep, pte)
63
64#define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
65
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030066#ifndef __PAGETABLE_P4D_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080067#define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
68#define pgd_clear(pgd) native_pgd_clear(pgd)
69#endif
70
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030071#ifndef set_p4d
72# define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d)
73#endif
74
75#ifndef __PAGETABLE_PUD_FOLDED
76#define p4d_clear(p4d) native_p4d_clear(p4d)
77#endif
78
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080079#ifndef set_pud
80# define set_pud(pudp, pud) native_set_pud(pudp, pud)
81#endif
82
Arnd Bergmannd0f33ac2017-03-16 16:40:24 -070083#ifndef __PAGETABLE_PUD_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080084#define pud_clear(pud) native_pud_clear(pud)
85#endif
86
87#define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
88#define pmd_clear(pmd) native_pmd_clear(pmd)
89
90#define pte_update(mm, addr, ptep) do { } while (0)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080091
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080092#define pgd_val(x) native_pgd_val(x)
93#define __pgd(x) native_make_pgd(x)
94
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030095#ifndef __PAGETABLE_P4D_FOLDED
96#define p4d_val(x) native_p4d_val(x)
97#define __p4d(x) native_make_p4d(x)
98#endif
99
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -0800100#ifndef __PAGETABLE_PUD_FOLDED
101#define pud_val(x) native_pud_val(x)
102#define __pud(x) native_make_pud(x)
103#endif
104
105#ifndef __PAGETABLE_PMD_FOLDED
106#define pmd_val(x) native_pmd_val(x)
107#define __pmd(x) native_make_pmd(x)
108#endif
109
110#define pte_val(x) native_pte_val(x)
111#define __pte(x) native_make_pte(x)
112
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800113#define arch_end_context_switch(prev) do {} while(0)
114
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -0800115#endif /* CONFIG_PARAVIRT */
116
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +0100117/*
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100118 * The following only work if pte_present() is true.
119 * Undefined behaviour if not..
120 */
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700121static inline int pte_dirty(pte_t pte)
122{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100123 return pte_flags(pte) & _PAGE_DIRTY;
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100124}
125
Dave Hansena927cb82016-02-12 13:02:15 -0800126
127static inline u32 read_pkru(void)
128{
129 if (boot_cpu_has(X86_FEATURE_OSPKE))
130 return __read_pkru();
131 return 0;
132}
133
Xiao Guangrong9e901992016-03-22 16:51:17 +0800134static inline void write_pkru(u32 pkru)
135{
136 if (boot_cpu_has(X86_FEATURE_OSPKE))
137 __write_pkru(pkru);
138}
139
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700140static inline int pte_young(pte_t pte)
141{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100142 return pte_flags(pte) & _PAGE_ACCESSED;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700143}
144
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800145static inline int pmd_dirty(pmd_t pmd)
146{
147 return pmd_flags(pmd) & _PAGE_DIRTY;
148}
149
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800150static inline int pmd_young(pmd_t pmd)
151{
152 return pmd_flags(pmd) & _PAGE_ACCESSED;
153}
154
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800155static inline int pud_dirty(pud_t pud)
156{
157 return pud_flags(pud) & _PAGE_DIRTY;
158}
159
160static inline int pud_young(pud_t pud)
161{
162 return pud_flags(pud) & _PAGE_ACCESSED;
163}
164
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700165static inline int pte_write(pte_t pte)
166{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100167 return pte_flags(pte) & _PAGE_RW;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700168}
169
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700170static inline int pte_huge(pte_t pte)
171{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100172 return pte_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700173}
174
175static inline int pte_global(pte_t pte)
176{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100177 return pte_flags(pte) & _PAGE_GLOBAL;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700178}
179
180static inline int pte_exec(pte_t pte)
181{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100182 return !(pte_flags(pte) & _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700183}
184
Nick Piggin7e675132008-04-28 02:13:00 -0700185static inline int pte_special(pte_t pte)
186{
Mel Gormanc819f372015-02-12 14:58:38 -0800187 return pte_flags(pte) & _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700188}
189
Hugh Dickins91030ca2008-09-09 16:42:45 +0100190static inline unsigned long pte_pfn(pte_t pte)
191{
192 return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT;
193}
194
Akinobu Mita087975b2009-06-27 15:35:15 +0900195static inline unsigned long pmd_pfn(pmd_t pmd)
196{
Toshi Kanif70abb02015-09-17 12:24:17 -0600197 return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
Akinobu Mita087975b2009-06-27 15:35:15 +0900198}
199
Mel Gorman0ee364e2013-02-11 14:52:36 +0000200static inline unsigned long pud_pfn(pud_t pud)
201{
Toshi Kanif70abb02015-09-17 12:24:17 -0600202 return (pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT;
Mel Gorman0ee364e2013-02-11 14:52:36 +0000203}
204
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300205static inline unsigned long p4d_pfn(p4d_t p4d)
206{
207 return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT;
208}
209
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500210static inline unsigned long pgd_pfn(pgd_t pgd)
211{
212 return (pgd_val(pgd) & PTE_PFN_MASK) >> PAGE_SHIFT;
213}
214
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300215static inline int p4d_large(p4d_t p4d)
216{
217 /* No 512 GiB pages yet */
218 return 0;
219}
220
Hugh Dickins91030ca2008-09-09 16:42:45 +0100221#define pte_page(pte) pfn_to_page(pte_pfn(pte))
222
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700223static inline int pmd_large(pmd_t pte)
224{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700225 return pmd_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700226}
227
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800228#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800229static inline int pmd_trans_huge(pmd_t pmd)
230{
Dan Williams5c7fb562016-01-15 16:56:52 -0800231 return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800232}
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800233
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800234#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
235static inline int pud_trans_huge(pud_t pud)
236{
237 return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
238}
239#endif
240
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700241#define has_transparent_hugepage has_transparent_hugepage
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800242static inline int has_transparent_hugepage(void)
243{
Borislav Petkov16bf9222016-03-29 17:42:03 +0200244 return boot_cpu_has(X86_FEATURE_PSE);
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800245}
Dan Williams5c7fb562016-01-15 16:56:52 -0800246
247#ifdef __HAVE_ARCH_PTE_DEVMAP
248static inline int pmd_devmap(pmd_t pmd)
249{
250 return !!(pmd_val(pmd) & _PAGE_DEVMAP);
251}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800252
253#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
254static inline int pud_devmap(pud_t pud)
255{
256 return !!(pud_val(pud) & _PAGE_DEVMAP);
257}
258#else
259static inline int pud_devmap(pud_t pud)
260{
261 return 0;
262}
263#endif
Kirill A. Shutemove5855132017-06-06 14:31:20 +0300264
265static inline int pgd_devmap(pgd_t pgd)
266{
267 return 0;
268}
Dan Williams5c7fb562016-01-15 16:56:52 -0800269#endif
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800270#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
271
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800272static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
273{
274 pteval_t v = native_pte_val(pte);
275
276 return native_make_pte(v | set);
277}
278
279static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
280{
281 pteval_t v = native_pte_val(pte);
282
283 return native_make_pte(v & ~clear);
284}
285
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700286static inline pte_t pte_mkclean(pte_t pte)
287{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800288 return pte_clear_flags(pte, _PAGE_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700289}
290
291static inline pte_t pte_mkold(pte_t pte)
292{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800293 return pte_clear_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700294}
295
296static inline pte_t pte_wrprotect(pte_t pte)
297{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800298 return pte_clear_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700299}
300
301static inline pte_t pte_mkexec(pte_t pte)
302{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800303 return pte_clear_flags(pte, _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700304}
305
306static inline pte_t pte_mkdirty(pte_t pte)
307{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700308 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700309}
310
311static inline pte_t pte_mkyoung(pte_t pte)
312{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800313 return pte_set_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700314}
315
316static inline pte_t pte_mkwrite(pte_t pte)
317{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800318 return pte_set_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700319}
320
321static inline pte_t pte_mkhuge(pte_t pte)
322{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800323 return pte_set_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700324}
325
326static inline pte_t pte_clrhuge(pte_t pte)
327{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800328 return pte_clear_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700329}
330
331static inline pte_t pte_mkglobal(pte_t pte)
332{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800333 return pte_set_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700334}
335
336static inline pte_t pte_clrglobal(pte_t pte)
337{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800338 return pte_clear_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700339}
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100340
Nick Piggin7e675132008-04-28 02:13:00 -0700341static inline pte_t pte_mkspecial(pte_t pte)
342{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800343 return pte_set_flags(pte, _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700344}
345
Dan Williams01c8f1c2016-01-15 16:56:40 -0800346static inline pte_t pte_mkdevmap(pte_t pte)
347{
348 return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP);
349}
350
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800351static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
352{
353 pmdval_t v = native_pmd_val(pmd);
354
355 return __pmd(v | set);
356}
357
358static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
359{
360 pmdval_t v = native_pmd_val(pmd);
361
362 return __pmd(v & ~clear);
363}
364
365static inline pmd_t pmd_mkold(pmd_t pmd)
366{
367 return pmd_clear_flags(pmd, _PAGE_ACCESSED);
368}
369
Minchan Kim590a4712016-01-15 16:55:20 -0800370static inline pmd_t pmd_mkclean(pmd_t pmd)
371{
372 return pmd_clear_flags(pmd, _PAGE_DIRTY);
373}
374
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800375static inline pmd_t pmd_wrprotect(pmd_t pmd)
376{
377 return pmd_clear_flags(pmd, _PAGE_RW);
378}
379
380static inline pmd_t pmd_mkdirty(pmd_t pmd)
381{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700382 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800383}
384
Dan Williamsf25748e32016-01-15 16:56:43 -0800385static inline pmd_t pmd_mkdevmap(pmd_t pmd)
386{
387 return pmd_set_flags(pmd, _PAGE_DEVMAP);
388}
389
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800390static inline pmd_t pmd_mkhuge(pmd_t pmd)
391{
392 return pmd_set_flags(pmd, _PAGE_PSE);
393}
394
395static inline pmd_t pmd_mkyoung(pmd_t pmd)
396{
397 return pmd_set_flags(pmd, _PAGE_ACCESSED);
398}
399
400static inline pmd_t pmd_mkwrite(pmd_t pmd)
401{
402 return pmd_set_flags(pmd, _PAGE_RW);
403}
404
405static inline pmd_t pmd_mknotpresent(pmd_t pmd)
406{
Mel Gorman21d9ee32015-02-12 14:58:32 -0800407 return pmd_clear_flags(pmd, _PAGE_PRESENT | _PAGE_PROTNONE);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800408}
409
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800410static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
411{
412 pudval_t v = native_pud_val(pud);
413
414 return __pud(v | set);
415}
416
417static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
418{
419 pudval_t v = native_pud_val(pud);
420
421 return __pud(v & ~clear);
422}
423
424static inline pud_t pud_mkold(pud_t pud)
425{
426 return pud_clear_flags(pud, _PAGE_ACCESSED);
427}
428
429static inline pud_t pud_mkclean(pud_t pud)
430{
431 return pud_clear_flags(pud, _PAGE_DIRTY);
432}
433
434static inline pud_t pud_wrprotect(pud_t pud)
435{
436 return pud_clear_flags(pud, _PAGE_RW);
437}
438
439static inline pud_t pud_mkdirty(pud_t pud)
440{
441 return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
442}
443
444static inline pud_t pud_mkdevmap(pud_t pud)
445{
446 return pud_set_flags(pud, _PAGE_DEVMAP);
447}
448
449static inline pud_t pud_mkhuge(pud_t pud)
450{
451 return pud_set_flags(pud, _PAGE_PSE);
452}
453
454static inline pud_t pud_mkyoung(pud_t pud)
455{
456 return pud_set_flags(pud, _PAGE_ACCESSED);
457}
458
459static inline pud_t pud_mkwrite(pud_t pud)
460{
461 return pud_set_flags(pud, _PAGE_RW);
462}
463
464static inline pud_t pud_mknotpresent(pud_t pud)
465{
466 return pud_clear_flags(pud, _PAGE_PRESENT | _PAGE_PROTNONE);
467}
468
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700469#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700470static inline int pte_soft_dirty(pte_t pte)
471{
472 return pte_flags(pte) & _PAGE_SOFT_DIRTY;
473}
474
475static inline int pmd_soft_dirty(pmd_t pmd)
476{
477 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
478}
479
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800480static inline int pud_soft_dirty(pud_t pud)
481{
482 return pud_flags(pud) & _PAGE_SOFT_DIRTY;
483}
484
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700485static inline pte_t pte_mksoft_dirty(pte_t pte)
486{
487 return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
488}
489
490static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
491{
492 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
493}
494
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800495static inline pud_t pud_mksoft_dirty(pud_t pud)
496{
497 return pud_set_flags(pud, _PAGE_SOFT_DIRTY);
498}
499
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200500static inline pte_t pte_clear_soft_dirty(pte_t pte)
501{
502 return pte_clear_flags(pte, _PAGE_SOFT_DIRTY);
503}
504
505static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
506{
507 return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
508}
509
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800510static inline pud_t pud_clear_soft_dirty(pud_t pud)
511{
512 return pud_clear_flags(pud, _PAGE_SOFT_DIRTY);
513}
514
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700515#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
516
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800517/*
518 * Mask out unsupported bits in a present pgprot. Non-present pgprots
519 * can use those bits for other purposes, so leave them be.
520 */
521static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
522{
523 pgprotval_t protval = pgprot_val(pgprot);
524
525 if (protval & _PAGE_PRESENT)
526 protval &= __supported_pte_mask;
527
528 return protval;
529}
530
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100531static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
532{
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800533 return __pte(((phys_addr_t)page_nr << PAGE_SHIFT) |
534 massage_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100535}
536
537static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
538{
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800539 return __pmd(((phys_addr_t)page_nr << PAGE_SHIFT) |
540 massage_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100541}
542
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800543static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
544{
545 return __pud(((phys_addr_t)page_nr << PAGE_SHIFT) |
546 massage_pgprot(pgprot));
547}
548
Ingo Molnar38472312008-01-30 13:32:57 +0100549static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
550{
551 pteval_t val = pte_val(pte);
552
553 /*
554 * Chop off the NX bit (if present), and add the NX portion of
555 * the newprot (if present):
556 */
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700557 val &= _PAGE_CHG_MASK;
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800558 val |= massage_pgprot(newprot) & ~_PAGE_CHG_MASK;
Ingo Molnar38472312008-01-30 13:32:57 +0100559
560 return __pte(val);
561}
562
Johannes Weinerc489f122011-01-13 15:47:02 -0800563static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
564{
565 pmdval_t val = pmd_val(pmd);
566
567 val &= _HPAGE_CHG_MASK;
568 val |= massage_pgprot(newprot) & ~_HPAGE_CHG_MASK;
569
570 return __pmd(val);
571}
572
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700573/* mprotect needs to preserve PAT bits when updating vm_page_prot */
574#define pgprot_modify pgprot_modify
575static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
576{
577 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
578 pgprotval_t addbits = pgprot_val(newprot);
579 return __pgprot(preservebits | addbits);
580}
581
Toshi Kanibbac8c62015-09-17 12:24:18 -0600582#define pte_pgprot(x) __pgprot(pte_flags(x))
583#define pmd_pgprot(x) __pgprot(pmd_flags(x))
584#define pud_pgprot(x) __pgprot(pud_flags(x))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300585#define p4d_pgprot(x) __pgprot(p4d_flags(x))
Andi Kleenc6ca18e2008-01-30 13:33:51 +0100586
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800587#define canon_pgprot(p) __pgprot(massage_pgprot(p))
Andi Kleen1e8e23b2008-01-30 13:33:53 +0100588
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700589static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
Juergen Grossd85f3332014-11-03 14:01:53 +0100590 enum page_cache_mode pcm,
591 enum page_cache_mode new_pcm)
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800592{
593 /*
H. Peter Anvin55a6ca22009-11-23 15:12:07 -0800594 * PAT type is always WB for untracked ranges, so no need to check.
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700595 */
H. Peter Anvin8a271382009-11-23 14:49:20 -0800596 if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700597 return 1;
598
599 /*
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800600 * Certain new memtypes are not allowed with certain
601 * requested memtype:
602 * - request is uncached, return cannot be write-back
603 * - request is write-combine, return cannot be write-back
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200604 * - request is write-through, return cannot be write-back
605 * - request is write-through, return cannot be write-combine
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800606 */
Juergen Grossd85f3332014-11-03 14:01:53 +0100607 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
608 new_pcm == _PAGE_CACHE_MODE_WB) ||
609 (pcm == _PAGE_CACHE_MODE_WC &&
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200610 new_pcm == _PAGE_CACHE_MODE_WB) ||
611 (pcm == _PAGE_CACHE_MODE_WT &&
612 new_pcm == _PAGE_CACHE_MODE_WB) ||
613 (pcm == _PAGE_CACHE_MODE_WT &&
614 new_pcm == _PAGE_CACHE_MODE_WC)) {
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800615 return 0;
616 }
617
618 return 1;
619}
620
Tejun Heo458a3e62009-02-24 11:57:21 +0900621pmd_t *populate_extra_pmd(unsigned long vaddr);
622pte_t *populate_extra_pte(unsigned long vaddr);
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100623#endif /* __ASSEMBLY__ */
624
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200625#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100626# include <asm/pgtable_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200627#else
David Howellsa1ce3922012-10-02 18:01:25 +0100628# include <asm/pgtable_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200629#endif
Jeremy Fitzhardinge6c386652008-01-30 13:32:55 +0100630
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800631#ifndef __ASSEMBLY__
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800632#include <linux/mm_types.h>
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -0700633#include <linux/mmdebug.h>
Dave Hansen4cbeb512013-01-22 13:24:31 -0800634#include <linux/log2.h>
Thomas Garnieref37bc32017-03-21 08:17:25 +0100635#include <asm/fixmap.h>
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800636
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800637static inline int pte_none(pte_t pte)
638{
Dave Hansen97e3c602016-07-07 17:19:12 -0700639 return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK));
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800640}
641
Jeremy Fitzhardinge8de01da2009-02-05 11:30:44 -0800642#define __HAVE_ARCH_PTE_SAME
643static inline int pte_same(pte_t a, pte_t b)
644{
645 return a.pte == b.pte;
646}
647
Jeremy Fitzhardinge7c683852009-02-05 11:30:45 -0800648static inline int pte_present(pte_t a)
649{
Mel Gormanc46a7c82014-06-04 16:06:30 -0700650 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
651}
652
Dan Williams3565fce2016-01-15 16:56:55 -0800653#ifdef __HAVE_ARCH_PTE_DEVMAP
654static inline int pte_devmap(pte_t a)
655{
656 return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP;
657}
658#endif
659
Rik van Riel2c3cf552012-10-09 15:31:12 +0200660#define pte_accessible pte_accessible
Rik van Riel20841402013-12-18 17:08:44 -0800661static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
Rik van Riel2c3cf552012-10-09 15:31:12 +0200662{
Rik van Riel20841402013-12-18 17:08:44 -0800663 if (pte_flags(a) & _PAGE_PRESENT)
664 return true;
665
Mel Gorman21d9ee32015-02-12 14:58:32 -0800666 if ((pte_flags(a) & _PAGE_PROTNONE) &&
Rik van Riel20841402013-12-18 17:08:44 -0800667 mm_tlb_flush_pending(mm))
668 return true;
669
670 return false;
Rik van Riel2c3cf552012-10-09 15:31:12 +0200671}
672
Jeremy Fitzhardingeeb636572009-02-06 13:05:56 -0800673static inline int pte_hidden(pte_t pte)
Vegard Nossumdfec0722008-04-04 00:51:41 +0200674{
Jeremy Fitzhardingeeb636572009-02-06 13:05:56 -0800675 return pte_flags(pte) & _PAGE_HIDDEN;
Vegard Nossumdfec0722008-04-04 00:51:41 +0200676}
677
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800678static inline int pmd_present(pmd_t pmd)
679{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700680 /*
681 * Checking for _PAGE_PSE is needed too because
682 * split_huge_page will temporarily clear the present bit (but
683 * the _PAGE_PSE flag will remain set at all times while the
684 * _PAGE_PRESENT bit is clear).
685 */
Mel Gorman21d9ee32015-02-12 14:58:32 -0800686 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800687}
688
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800689#ifdef CONFIG_NUMA_BALANCING
690/*
691 * These work without NUMA balancing but the kernel does not care. See the
692 * comment in include/asm-generic/pgtable.h
693 */
694static inline int pte_protnone(pte_t pte)
695{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000696 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
697 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800698}
699
700static inline int pmd_protnone(pmd_t pmd)
701{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000702 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
703 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800704}
705#endif /* CONFIG_NUMA_BALANCING */
706
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800707static inline int pmd_none(pmd_t pmd)
708{
709 /* Only check low word on 32-bit platforms, since it might be
710 out of sync with upper half. */
Dave Hansen97e3c602016-07-07 17:19:12 -0700711 unsigned long val = native_pmd_val(pmd);
712 return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0;
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800713}
714
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800715static inline unsigned long pmd_page_vaddr(pmd_t pmd)
716{
Toshi Kanif70abb02015-09-17 12:24:17 -0600717 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800718}
719
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100720/*
721 * Currently stuck as a macro due to indirect forward reference to
722 * linux/mmzone.h's __section_mem_map_addr() definition:
723 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500724#define pmd_page(pmd) pfn_to_page(pmd_pfn(pmd))
Jeremy Fitzhardinge20063ca2009-02-05 11:31:00 -0800725
Jeremy Fitzhardingee24d7ee2009-02-05 11:31:01 -0800726/*
727 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
728 *
729 * this macro returns the index of the entry in the pmd page which would
730 * control the given virtual address
731 */
Figo.zhangce0c0f92009-06-28 18:07:39 +0800732static inline unsigned long pmd_index(unsigned long address)
Jeremy Fitzhardingee24d7ee2009-02-05 11:31:01 -0800733{
734 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
735}
736
Jeremy Fitzhardinge97e28172009-02-05 11:31:05 -0800737/*
738 * Conversion functions: convert a page and protection to a page entry,
739 * and a page entry and page directory to the page they refer to.
740 *
741 * (Currently stuck as a macro because of indirect forward reference
742 * to linux/mm.h:page_to_nid())
743 */
744#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
745
Jeremy Fitzhardinge346309c2009-02-05 11:31:06 -0800746/*
747 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
748 *
749 * this function returns the index of the entry in the pte page which would
750 * control the given virtual address
751 */
Figo.zhangce0c0f92009-06-28 18:07:39 +0800752static inline unsigned long pte_index(unsigned long address)
Jeremy Fitzhardinge346309c2009-02-05 11:31:06 -0800753{
754 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
755}
756
Jeremy Fitzhardinge3fbc2442009-02-05 11:31:07 -0800757static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
758{
759 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
760}
761
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800762static inline int pmd_bad(pmd_t pmd)
763{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800764 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800765}
766
Jeremy Fitzhardingecc290ca2009-02-05 11:31:12 -0800767static inline unsigned long pages_to_mb(unsigned long npg)
768{
769 return npg >> (20 - PAGE_SHIFT);
770}
771
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700772#if CONFIG_PGTABLE_LEVELS > 2
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800773static inline int pud_none(pud_t pud)
774{
Dave Hansen97e3c602016-07-07 17:19:12 -0700775 return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800776}
777
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800778static inline int pud_present(pud_t pud)
779{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800780 return pud_flags(pud) & _PAGE_PRESENT;
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800781}
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800782
783static inline unsigned long pud_page_vaddr(pud_t pud)
784{
Toshi Kanif70abb02015-09-17 12:24:17 -0600785 return (unsigned long)__va(pud_val(pud) & pud_pfn_mask(pud));
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800786}
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800787
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100788/*
789 * Currently stuck as a macro due to indirect forward reference to
790 * linux/mmzone.h's __section_mem_map_addr() definition:
791 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500792#define pud_page(pud) pfn_to_page(pud_pfn(pud))
Jeremy Fitzhardinge01ade20d5a2009-02-05 11:31:02 -0800793
794/* Find an entry in the second-level page table.. */
795static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
796{
797 return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address);
798}
Jeremy Fitzhardinge3180fba2009-02-05 11:31:04 -0800799
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800800static inline int pud_large(pud_t pud)
801{
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800802 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800803 (_PAGE_PSE | _PAGE_PRESENT);
804}
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800805
806static inline int pud_bad(pud_t pud)
807{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800808 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800809}
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800810#else
811static inline int pud_large(pud_t pud)
812{
813 return 0;
814}
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700815#endif /* CONFIG_PGTABLE_LEVELS > 2 */
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800816
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300817static inline unsigned long pud_index(unsigned long address)
818{
819 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
820}
821
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300822#if CONFIG_PGTABLE_LEVELS > 3
823static inline int p4d_none(p4d_t p4d)
824{
825 return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
826}
827
828static inline int p4d_present(p4d_t p4d)
829{
830 return p4d_flags(p4d) & _PAGE_PRESENT;
831}
832
833static inline unsigned long p4d_page_vaddr(p4d_t p4d)
834{
835 return (unsigned long)__va(p4d_val(p4d) & p4d_pfn_mask(p4d));
836}
837
838/*
839 * Currently stuck as a macro due to indirect forward reference to
840 * linux/mmzone.h's __section_mem_map_addr() definition:
841 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500842#define p4d_page(p4d) pfn_to_page(p4d_pfn(p4d))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300843
844/* Find an entry in the third-level page table.. */
845static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
846{
847 return (pud_t *)p4d_page_vaddr(*p4d) + pud_index(address);
848}
849
850static inline int p4d_bad(p4d_t p4d)
851{
852 return (p4d_flags(p4d) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
853}
854#endif /* CONFIG_PGTABLE_LEVELS > 3 */
855
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300856static inline unsigned long p4d_index(unsigned long address)
857{
858 return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
859}
860
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300861#if CONFIG_PGTABLE_LEVELS > 4
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800862static inline int pgd_present(pgd_t pgd)
863{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800864 return pgd_flags(pgd) & _PAGE_PRESENT;
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800865}
Jeremy Fitzhardingec5f040b2009-02-05 11:30:52 -0800866
867static inline unsigned long pgd_page_vaddr(pgd_t pgd)
868{
869 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
870}
Jeremy Fitzhardinge777cba12009-02-05 11:30:56 -0800871
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100872/*
873 * Currently stuck as a macro due to indirect forward reference to
874 * linux/mmzone.h's __section_mem_map_addr() definition:
875 */
Tom Lendackyfd7e3152017-07-17 16:10:06 -0500876#define pgd_page(pgd) pfn_to_page(pgd_pfn(pgd))
Jeremy Fitzhardinge7cfb8102009-02-05 11:30:57 -0800877
878/* to find an entry in a page-table-directory. */
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300879static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800880{
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300881 return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address);
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800882}
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800883
884static inline int pgd_bad(pgd_t pgd)
885{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800886 return (pgd_flags(pgd) & ~_PAGE_USER) != _KERNPG_TABLE;
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800887}
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800888
889static inline int pgd_none(pgd_t pgd)
890{
Dave Hansen97e3c602016-07-07 17:19:12 -0700891 /*
892 * There is no need to do a workaround for the KNL stray
893 * A/D bit erratum here. PGDs only point to page tables
894 * except on 32-bit non-PAE which is not supported on
895 * KNL.
896 */
Jeremy Fitzhardinge26c8e312009-02-05 11:31:17 -0800897 return !native_pgd_val(pgd);
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800898}
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300899#endif /* CONFIG_PGTABLE_LEVELS > 4 */
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800900
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200901#endif /* __ASSEMBLY__ */
902
Jeremy Fitzhardingefb15a9b2008-06-25 00:19:06 -0400903/*
904 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
905 *
906 * this macro returns the index of the entry in the pgd page which would
907 * control the given virtual address
908 */
909#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
910
911/*
912 * pgd_offset() returns a (pgd_t *)
913 * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
914 */
915#define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address)))
916/*
917 * a shortcut which implies the use of the kernel's pgd, instead
918 * of a process's
919 */
920#define pgd_offset_k(address) pgd_offset(&init_mm, (address))
921
922
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700923#define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
924#define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
925
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100926#ifndef __ASSEMBLY__
927
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +0530928extern int direct_gbpages;
Yinghai Lu22ddfca2012-11-16 19:38:41 -0800929void init_mem_mapping(void);
Yinghai Lu8d574702012-11-16 19:38:58 -0800930void early_alloc_pgt_buf(void);
Ingo Molnar4270fd82017-01-28 12:45:40 +0100931extern void memblock_find_dma_reserve(void);
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +0530932
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700933#ifdef CONFIG_X86_64
934/* Realmode trampoline initialization. */
935extern pgd_t trampoline_pgd_entry;
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700936static inline void __meminit init_trampoline_default(void)
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700937{
938 /* Default trampoline pgd value */
Kirill A. Shutemov65ade2f2017-06-06 14:31:27 +0300939 trampoline_pgd_entry = init_top_pgt[pgd_index(__PAGE_OFFSET)];
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700940}
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700941# ifdef CONFIG_RANDOMIZE_MEMORY
942void __meminit init_trampoline(void);
943# else
944# define init_trampoline init_trampoline_default
945# endif
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700946#else
947static inline void init_trampoline(void) { }
948#endif
949
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100950/* local pte updates need not use xchg for locking */
951static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
952{
953 pte_t res = *ptep;
954
955 /* Pure native function needs no input for mm, addr */
956 native_pte_clear(NULL, 0, ptep);
957 return res;
958}
959
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800960static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
961{
962 pmd_t res = *pmdp;
963
964 native_pmd_clear(pmdp);
965 return res;
966}
967
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800968static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)
969{
970 pud_t res = *pudp;
971
972 native_pud_clear(pudp);
973 return res;
974}
975
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100976static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
977 pte_t *ptep , pte_t pte)
978{
979 native_set_pte(ptep, pte);
980}
981
Andrea Arcangeli0a47de52011-01-13 15:46:35 -0800982static inline void native_set_pmd_at(struct mm_struct *mm, unsigned long addr,
983 pmd_t *pmdp , pmd_t pmd)
984{
985 native_set_pmd(pmdp, pmd);
986}
987
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800988static inline void native_set_pud_at(struct mm_struct *mm, unsigned long addr,
989 pud_t *pudp, pud_t pud)
990{
991 native_set_pud(pudp, pud);
992}
993
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100994#ifndef CONFIG_PARAVIRT
995/*
996 * Rules for using pte_update - it must be called after any PTE update which
997 * has not been done using the set_pte / clear_pte interfaces. It is used by
998 * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
999 * updates should either be sets, clears, or set_pte_atomic for P->P
1000 * transitions, which means this hook should only be called for user PTEs.
1001 * This hook implies a P->P protection or access change has taken place, which
Juergen Grossd6ccc3e2015-11-17 15:51:19 +01001002 * requires a subsequent TLB flush.
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001003 */
1004#define pte_update(mm, addr, ptep) do { } while (0)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001005#endif
1006
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001007/*
1008 * We only update the dirty/accessed state if we set
1009 * the dirty bit by hand in the kernel, since the hardware
1010 * will do the accessed bit for us, and we don't want to
1011 * race with other CPU's that might be updating the dirty
1012 * bit at the same time.
1013 */
Jeremy Fitzhardingebea41802008-06-25 00:18:57 -04001014struct vm_area_struct;
1015
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001016#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -07001017extern int ptep_set_access_flags(struct vm_area_struct *vma,
1018 unsigned long address, pte_t *ptep,
1019 pte_t entry, int dirty);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001020
1021#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -07001022extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
1023 unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001024
1025#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -07001026extern int ptep_clear_flush_young(struct vm_area_struct *vma,
1027 unsigned long address, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001028
1029#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001030static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
1031 pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001032{
1033 pte_t pte = native_ptep_get_and_clear(ptep);
1034 pte_update(mm, addr, ptep);
1035 return pte;
1036}
1037
1038#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001039static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
1040 unsigned long addr, pte_t *ptep,
1041 int full)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001042{
1043 pte_t pte;
1044 if (full) {
1045 /*
1046 * Full address destruction in progress; paravirt does not
1047 * care about updates and native needs no locking
1048 */
1049 pte = native_local_ptep_get_and_clear(ptep);
1050 } else {
1051 pte = ptep_get_and_clear(mm, addr, ptep);
1052 }
1053 return pte;
1054}
1055
1056#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001057static inline void ptep_set_wrprotect(struct mm_struct *mm,
1058 unsigned long addr, pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001059{
Jeremy Fitzhardinged8d89822008-01-30 13:32:58 +01001060 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001061 pte_update(mm, addr, ptep);
1062}
1063
Jesper Juhl2ac13462011-12-18 01:32:09 +01001064#define flush_tlb_fix_spurious_fault(vma, address) do { } while (0)
Shaohua Li61c77322010-08-16 09:16:55 +08001065
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001066#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1067
1068#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1069extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1070 unsigned long address, pmd_t *pmdp,
1071 pmd_t entry, int dirty);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001072extern int pudp_set_access_flags(struct vm_area_struct *vma,
1073 unsigned long address, pud_t *pudp,
1074 pud_t entry, int dirty);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001075
1076#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1077extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1078 unsigned long addr, pmd_t *pmdp);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001079extern int pudp_test_and_clear_young(struct vm_area_struct *vma,
1080 unsigned long addr, pud_t *pudp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001081
1082#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1083extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
1084 unsigned long address, pmd_t *pmdp);
1085
1086
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001087#define __HAVE_ARCH_PMD_WRITE
1088static inline int pmd_write(pmd_t pmd)
1089{
1090 return pmd_flags(pmd) & _PAGE_RW;
1091}
1092
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001093#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1094static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001095 pmd_t *pmdp)
1096{
Juergen Grossd6ccc3e2015-11-17 15:51:19 +01001097 return native_pmdp_get_and_clear(pmdp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001098}
1099
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001100#define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
1101static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
1102 unsigned long addr, pud_t *pudp)
1103{
1104 return native_pudp_get_and_clear(pudp);
1105}
1106
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001107#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1108static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1109 unsigned long addr, pmd_t *pmdp)
1110{
1111 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001112}
1113
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001114/*
1115 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
1116 *
1117 * dst - pointer to pgd range anwhere on a pgd page
1118 * src - ""
1119 * count - the number of pgds to copy.
1120 *
1121 * dst and src can be on the same page, but the range must not overlap,
1122 * and must not cross a page boundary.
1123 */
1124static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
1125{
1126 memcpy(dst, src, count * sizeof(pgd_t));
1127}
1128
Dave Hansen4cbeb512013-01-22 13:24:31 -08001129#define PTE_SHIFT ilog2(PTRS_PER_PTE)
1130static inline int page_level_shift(enum pg_level level)
1131{
1132 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
1133}
1134static inline unsigned long page_level_size(enum pg_level level)
1135{
1136 return 1UL << page_level_shift(level);
1137}
1138static inline unsigned long page_level_mask(enum pg_level level)
1139{
1140 return ~(page_level_size(level) - 1);
1141}
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001142
Kirill A. Shutemov602e0182012-12-18 12:22:18 -08001143/*
1144 * The x86 doesn't have any external MMU info: the kernel page
1145 * tables contain all the necessary information.
1146 */
1147static inline void update_mmu_cache(struct vm_area_struct *vma,
1148 unsigned long addr, pte_t *ptep)
1149{
1150}
1151static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1152 unsigned long addr, pmd_t *pmd)
1153{
1154}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001155static inline void update_mmu_cache_pud(struct vm_area_struct *vma,
1156 unsigned long addr, pud_t *pud)
1157{
1158}
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001159
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001160#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001161static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1162{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001163 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1164}
1165
1166static inline int pte_swp_soft_dirty(pte_t pte)
1167{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001168 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
1169}
1170
1171static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1172{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001173 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1174}
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001175#endif
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001176
Dave Hansen33a709b2016-02-12 13:02:19 -08001177#define PKRU_AD_BIT 0x1
1178#define PKRU_WD_BIT 0x2
Dave Hansen84594292016-02-12 13:02:36 -08001179#define PKRU_BITS_PER_PKEY 2
Dave Hansen33a709b2016-02-12 13:02:19 -08001180
1181static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
1182{
Dave Hansen84594292016-02-12 13:02:36 -08001183 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
Dave Hansen33a709b2016-02-12 13:02:19 -08001184 return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
1185}
1186
1187static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
1188{
Dave Hansen84594292016-02-12 13:02:36 -08001189 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
Dave Hansen33a709b2016-02-12 13:02:19 -08001190 /*
1191 * Access-disable disables writes too so we need to check
1192 * both bits here.
1193 */
1194 return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
1195}
1196
1197static inline u16 pte_flags_pkey(unsigned long pte_flags)
1198{
1199#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1200 /* ifdef to avoid doing 59-bit shift on 32-bit values */
1201 return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0;
1202#else
1203 return 0;
1204#endif
1205}
1206
Kirill A. Shutemove5855132017-06-06 14:31:20 +03001207static inline bool __pkru_allows_pkey(u16 pkey, bool write)
1208{
1209 u32 pkru = read_pkru();
1210
1211 if (!__pkru_allows_read(pkru, pkey))
1212 return false;
1213 if (write && !__pkru_allows_write(pkru, pkey))
1214 return false;
1215
1216 return true;
1217}
1218
1219/*
1220 * 'pteval' can come from a PTE, PMD or PUD. We only check
1221 * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
1222 * same value on all 3 types.
1223 */
1224static inline bool __pte_access_permitted(unsigned long pteval, bool write)
1225{
1226 unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
1227
1228 if (write)
1229 need_pte_bits |= _PAGE_RW;
1230
1231 if ((pteval & need_pte_bits) != need_pte_bits)
1232 return 0;
1233
1234 return __pkru_allows_pkey(pte_flags_pkey(pteval), write);
1235}
1236
1237#define pte_access_permitted pte_access_permitted
1238static inline bool pte_access_permitted(pte_t pte, bool write)
1239{
1240 return __pte_access_permitted(pte_val(pte), write);
1241}
1242
1243#define pmd_access_permitted pmd_access_permitted
1244static inline bool pmd_access_permitted(pmd_t pmd, bool write)
1245{
1246 return __pte_access_permitted(pmd_val(pmd), write);
1247}
1248
1249#define pud_access_permitted pud_access_permitted
1250static inline bool pud_access_permitted(pud_t pud, bool write)
1251{
1252 return __pte_access_permitted(pud_val(pud), write);
1253}
1254
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001255#include <asm-generic/pgtable.h>
1256#endif /* __ASSEMBLY__ */
1257
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001258#endif /* _ASM_X86_PGTABLE_H */