blob: f5af95a0c6b874e44407964c1fc2d073f02a9ac7 [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
Ingo Molnarc47c1b12009-02-09 11:57:45 +01004#include <asm/page.h>
Jeremy Fitzhardinge8d19c992009-02-08 18:46:18 -08005#include <asm/pgtable_types.h>
Suresh Siddhab2bc2732008-09-23 14:00:36 -07006
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -08007/*
8 * Macro to mark a page protection value as UC-
9 */
Juergen Grossd85f3332014-11-03 14:01:53 +010010#define pgprot_noncached(prot) \
11 ((boot_cpu_data.x86 > 3) \
12 ? (__pgprot(pgprot_val(prot) | \
13 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
venkatesh.pallipadi@intel.com8a7b12f2008-12-18 11:41:31 -080014 : (prot))
15
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010016#ifndef __ASSEMBLY__
H. Peter Anvin55a6ca22009-11-23 15:12:07 -080017#include <asm/x86_init.h>
18
Borislav Petkovef6bea62014-01-18 12:48:14 +010019void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd);
Stephen Smalleye1a58322015-10-05 12:55:20 -040020void ptdump_walk_pgd_level_checkwx(void);
21
22#ifdef CONFIG_DEBUG_WX
23#define debug_checkwx() ptdump_walk_pgd_level_checkwx()
24#else
25#define debug_checkwx() do { } while (0)
26#endif
Borislav Petkovef6bea62014-01-18 12:48:14 +010027
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +010028/*
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010029 * ZERO_PAGE is a global shared page that is always zero: used
30 * for zero-mapped memory areas etc..
31 */
Andi Kleen277d5b42013-08-05 15:02:43 -070032extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
33 __visible;
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010034#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
35
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +010036extern spinlock_t pgd_lock;
37extern struct list_head pgd_list;
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +010038
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -070039extern struct mm_struct *pgd_page_get_mm(struct page *page);
40
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080041#ifdef CONFIG_PARAVIRT
42#include <asm/paravirt.h>
43#else /* !CONFIG_PARAVIRT */
44#define set_pte(ptep, pte) native_set_pte(ptep, pte)
45#define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
Andrea Arcangeli2609ae62011-01-13 15:46:37 -080046#define set_pmd_at(mm, addr, pmdp, pmd) native_set_pmd_at(mm, addr, pmdp, pmd)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -080047#define set_pud_at(mm, addr, pudp, pud) native_set_pud_at(mm, addr, pudp, pud)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080048
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080049#define set_pte_atomic(ptep, pte) \
50 native_set_pte_atomic(ptep, pte)
51
52#define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
53
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030054#ifndef __PAGETABLE_P4D_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080055#define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
56#define pgd_clear(pgd) native_pgd_clear(pgd)
57#endif
58
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030059#ifndef set_p4d
60# define set_p4d(p4dp, p4d) native_set_p4d(p4dp, p4d)
61#endif
62
63#ifndef __PAGETABLE_PUD_FOLDED
64#define p4d_clear(p4d) native_p4d_clear(p4d)
65#endif
66
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080067#ifndef set_pud
68# define set_pud(pudp, pud) native_set_pud(pudp, pud)
69#endif
70
Arnd Bergmannd0f33ac2017-03-16 16:40:24 -070071#ifndef __PAGETABLE_PUD_FOLDED
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080072#define pud_clear(pud) native_pud_clear(pud)
73#endif
74
75#define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
76#define pmd_clear(pmd) native_pmd_clear(pmd)
77
78#define pte_update(mm, addr, ptep) do { } while (0)
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080079
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080080#define pgd_val(x) native_pgd_val(x)
81#define __pgd(x) native_make_pgd(x)
82
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +030083#ifndef __PAGETABLE_P4D_FOLDED
84#define p4d_val(x) native_p4d_val(x)
85#define __p4d(x) native_make_p4d(x)
86#endif
87
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -080088#ifndef __PAGETABLE_PUD_FOLDED
89#define pud_val(x) native_pud_val(x)
90#define __pud(x) native_make_pud(x)
91#endif
92
93#ifndef __PAGETABLE_PMD_FOLDED
94#define pmd_val(x) native_pmd_val(x)
95#define __pmd(x) native_make_pmd(x)
96#endif
97
98#define pte_val(x) native_pte_val(x)
99#define __pte(x) native_make_pte(x)
100
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -0800101#define arch_end_context_switch(prev) do {} while(0)
102
Jeremy Fitzhardinge54321d92009-02-11 10:20:05 -0800103#endif /* CONFIG_PARAVIRT */
104
Jeremy Fitzhardinge8405b122008-01-30 13:32:58 +0100105/*
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100106 * The following only work if pte_present() is true.
107 * Undefined behaviour if not..
108 */
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700109static inline int pte_dirty(pte_t pte)
110{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100111 return pte_flags(pte) & _PAGE_DIRTY;
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100112}
113
Dave Hansena927cb82016-02-12 13:02:15 -0800114
115static inline u32 read_pkru(void)
116{
117 if (boot_cpu_has(X86_FEATURE_OSPKE))
118 return __read_pkru();
119 return 0;
120}
121
Xiao Guangrong9e901992016-03-22 16:51:17 +0800122static inline void write_pkru(u32 pkru)
123{
124 if (boot_cpu_has(X86_FEATURE_OSPKE))
125 __write_pkru(pkru);
126}
127
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700128static inline int pte_young(pte_t pte)
129{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100130 return pte_flags(pte) & _PAGE_ACCESSED;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700131}
132
Kirill A. Shutemovc164e032014-12-10 15:44:36 -0800133static inline int pmd_dirty(pmd_t pmd)
134{
135 return pmd_flags(pmd) & _PAGE_DIRTY;
136}
137
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800138static inline int pmd_young(pmd_t pmd)
139{
140 return pmd_flags(pmd) & _PAGE_ACCESSED;
141}
142
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800143static inline int pud_dirty(pud_t pud)
144{
145 return pud_flags(pud) & _PAGE_DIRTY;
146}
147
148static inline int pud_young(pud_t pud)
149{
150 return pud_flags(pud) & _PAGE_ACCESSED;
151}
152
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700153static inline int pte_write(pte_t pte)
154{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100155 return pte_flags(pte) & _PAGE_RW;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700156}
157
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700158static inline int pte_huge(pte_t pte)
159{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100160 return pte_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700161}
162
163static inline int pte_global(pte_t pte)
164{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100165 return pte_flags(pte) & _PAGE_GLOBAL;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700166}
167
168static inline int pte_exec(pte_t pte)
169{
Jeremy Fitzhardingea15af1c2008-05-26 23:31:06 +0100170 return !(pte_flags(pte) & _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700171}
172
Nick Piggin7e675132008-04-28 02:13:00 -0700173static inline int pte_special(pte_t pte)
174{
Mel Gormanc819f372015-02-12 14:58:38 -0800175 return pte_flags(pte) & _PAGE_SPECIAL;
Nick Piggin7e675132008-04-28 02:13:00 -0700176}
177
Hugh Dickins91030ca2008-09-09 16:42:45 +0100178static inline unsigned long pte_pfn(pte_t pte)
179{
180 return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT;
181}
182
Akinobu Mita087975b2009-06-27 15:35:15 +0900183static inline unsigned long pmd_pfn(pmd_t pmd)
184{
Toshi Kanif70abb02015-09-17 12:24:17 -0600185 return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
Akinobu Mita087975b2009-06-27 15:35:15 +0900186}
187
Mel Gorman0ee364e2013-02-11 14:52:36 +0000188static inline unsigned long pud_pfn(pud_t pud)
189{
Toshi Kanif70abb02015-09-17 12:24:17 -0600190 return (pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT;
Mel Gorman0ee364e2013-02-11 14:52:36 +0000191}
192
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300193static inline unsigned long p4d_pfn(p4d_t p4d)
194{
195 return (p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT;
196}
197
198static inline int p4d_large(p4d_t p4d)
199{
200 /* No 512 GiB pages yet */
201 return 0;
202}
203
Hugh Dickins91030ca2008-09-09 16:42:45 +0100204#define pte_page(pte) pfn_to_page(pte_pfn(pte))
205
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700206static inline int pmd_large(pmd_t pte)
207{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700208 return pmd_flags(pte) & _PAGE_PSE;
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700209}
210
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800211#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800212static inline int pmd_trans_huge(pmd_t pmd)
213{
Dan Williams5c7fb562016-01-15 16:56:52 -0800214 return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800215}
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800216
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800217#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
218static inline int pud_trans_huge(pud_t pud)
219{
220 return (pud_val(pud) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE;
221}
222#endif
223
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700224#define has_transparent_hugepage has_transparent_hugepage
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800225static inline int has_transparent_hugepage(void)
226{
Borislav Petkov16bf9222016-03-29 17:42:03 +0200227 return boot_cpu_has(X86_FEATURE_PSE);
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800228}
Dan Williams5c7fb562016-01-15 16:56:52 -0800229
230#ifdef __HAVE_ARCH_PTE_DEVMAP
231static inline int pmd_devmap(pmd_t pmd)
232{
233 return !!(pmd_val(pmd) & _PAGE_DEVMAP);
234}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800235
236#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
237static inline int pud_devmap(pud_t pud)
238{
239 return !!(pud_val(pud) & _PAGE_DEVMAP);
240}
241#else
242static inline int pud_devmap(pud_t pud)
243{
244 return 0;
245}
246#endif
Dan Williams5c7fb562016-01-15 16:56:52 -0800247#endif
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800248#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
249
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800250static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
251{
252 pteval_t v = native_pte_val(pte);
253
254 return native_make_pte(v | set);
255}
256
257static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
258{
259 pteval_t v = native_pte_val(pte);
260
261 return native_make_pte(v & ~clear);
262}
263
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700264static inline pte_t pte_mkclean(pte_t pte)
265{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800266 return pte_clear_flags(pte, _PAGE_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700267}
268
269static inline pte_t pte_mkold(pte_t pte)
270{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800271 return pte_clear_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700272}
273
274static inline pte_t pte_wrprotect(pte_t pte)
275{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800276 return pte_clear_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700277}
278
279static inline pte_t pte_mkexec(pte_t pte)
280{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800281 return pte_clear_flags(pte, _PAGE_NX);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700282}
283
284static inline pte_t pte_mkdirty(pte_t pte)
285{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700286 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700287}
288
289static inline pte_t pte_mkyoung(pte_t pte)
290{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800291 return pte_set_flags(pte, _PAGE_ACCESSED);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700292}
293
294static inline pte_t pte_mkwrite(pte_t pte)
295{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800296 return pte_set_flags(pte, _PAGE_RW);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700297}
298
299static inline pte_t pte_mkhuge(pte_t pte)
300{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800301 return pte_set_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700302}
303
304static inline pte_t pte_clrhuge(pte_t pte)
305{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800306 return pte_clear_flags(pte, _PAGE_PSE);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700307}
308
309static inline pte_t pte_mkglobal(pte_t pte)
310{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800311 return pte_set_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700312}
313
314static inline pte_t pte_clrglobal(pte_t pte)
315{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800316 return pte_clear_flags(pte, _PAGE_GLOBAL);
Joe Perches3cbaeaf2008-03-23 01:03:12 -0700317}
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100318
Nick Piggin7e675132008-04-28 02:13:00 -0700319static inline pte_t pte_mkspecial(pte_t pte)
320{
Jeremy Fitzhardinge65228692009-01-22 14:24:22 -0800321 return pte_set_flags(pte, _PAGE_SPECIAL);
Nick Piggin7e675132008-04-28 02:13:00 -0700322}
323
Dan Williams01c8f1c2016-01-15 16:56:40 -0800324static inline pte_t pte_mkdevmap(pte_t pte)
325{
326 return pte_set_flags(pte, _PAGE_SPECIAL|_PAGE_DEVMAP);
327}
328
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800329static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
330{
331 pmdval_t v = native_pmd_val(pmd);
332
333 return __pmd(v | set);
334}
335
336static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
337{
338 pmdval_t v = native_pmd_val(pmd);
339
340 return __pmd(v & ~clear);
341}
342
343static inline pmd_t pmd_mkold(pmd_t pmd)
344{
345 return pmd_clear_flags(pmd, _PAGE_ACCESSED);
346}
347
Minchan Kim590a4712016-01-15 16:55:20 -0800348static inline pmd_t pmd_mkclean(pmd_t pmd)
349{
350 return pmd_clear_flags(pmd, _PAGE_DIRTY);
351}
352
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800353static inline pmd_t pmd_wrprotect(pmd_t pmd)
354{
355 return pmd_clear_flags(pmd, _PAGE_RW);
356}
357
358static inline pmd_t pmd_mkdirty(pmd_t pmd)
359{
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700360 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800361}
362
Dan Williamsf25748e32016-01-15 16:56:43 -0800363static inline pmd_t pmd_mkdevmap(pmd_t pmd)
364{
365 return pmd_set_flags(pmd, _PAGE_DEVMAP);
366}
367
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800368static inline pmd_t pmd_mkhuge(pmd_t pmd)
369{
370 return pmd_set_flags(pmd, _PAGE_PSE);
371}
372
373static inline pmd_t pmd_mkyoung(pmd_t pmd)
374{
375 return pmd_set_flags(pmd, _PAGE_ACCESSED);
376}
377
378static inline pmd_t pmd_mkwrite(pmd_t pmd)
379{
380 return pmd_set_flags(pmd, _PAGE_RW);
381}
382
383static inline pmd_t pmd_mknotpresent(pmd_t pmd)
384{
Mel Gorman21d9ee32015-02-12 14:58:32 -0800385 return pmd_clear_flags(pmd, _PAGE_PRESENT | _PAGE_PROTNONE);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800386}
387
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800388static inline pud_t pud_set_flags(pud_t pud, pudval_t set)
389{
390 pudval_t v = native_pud_val(pud);
391
392 return __pud(v | set);
393}
394
395static inline pud_t pud_clear_flags(pud_t pud, pudval_t clear)
396{
397 pudval_t v = native_pud_val(pud);
398
399 return __pud(v & ~clear);
400}
401
402static inline pud_t pud_mkold(pud_t pud)
403{
404 return pud_clear_flags(pud, _PAGE_ACCESSED);
405}
406
407static inline pud_t pud_mkclean(pud_t pud)
408{
409 return pud_clear_flags(pud, _PAGE_DIRTY);
410}
411
412static inline pud_t pud_wrprotect(pud_t pud)
413{
414 return pud_clear_flags(pud, _PAGE_RW);
415}
416
417static inline pud_t pud_mkdirty(pud_t pud)
418{
419 return pud_set_flags(pud, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
420}
421
422static inline pud_t pud_mkdevmap(pud_t pud)
423{
424 return pud_set_flags(pud, _PAGE_DEVMAP);
425}
426
427static inline pud_t pud_mkhuge(pud_t pud)
428{
429 return pud_set_flags(pud, _PAGE_PSE);
430}
431
432static inline pud_t pud_mkyoung(pud_t pud)
433{
434 return pud_set_flags(pud, _PAGE_ACCESSED);
435}
436
437static inline pud_t pud_mkwrite(pud_t pud)
438{
439 return pud_set_flags(pud, _PAGE_RW);
440}
441
442static inline pud_t pud_mknotpresent(pud_t pud)
443{
444 return pud_clear_flags(pud, _PAGE_PRESENT | _PAGE_PROTNONE);
445}
446
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700447#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700448static inline int pte_soft_dirty(pte_t pte)
449{
450 return pte_flags(pte) & _PAGE_SOFT_DIRTY;
451}
452
453static inline int pmd_soft_dirty(pmd_t pmd)
454{
455 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
456}
457
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800458static inline int pud_soft_dirty(pud_t pud)
459{
460 return pud_flags(pud) & _PAGE_SOFT_DIRTY;
461}
462
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -0700463static inline pte_t pte_mksoft_dirty(pte_t pte)
464{
465 return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
466}
467
468static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
469{
470 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
471}
472
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800473static inline pud_t pud_mksoft_dirty(pud_t pud)
474{
475 return pud_set_flags(pud, _PAGE_SOFT_DIRTY);
476}
477
Martin Schwidefskya7b76172015-04-22 14:20:47 +0200478static inline pte_t pte_clear_soft_dirty(pte_t pte)
479{
480 return pte_clear_flags(pte, _PAGE_SOFT_DIRTY);
481}
482
483static inline pmd_t pmd_clear_soft_dirty(pmd_t pmd)
484{
485 return pmd_clear_flags(pmd, _PAGE_SOFT_DIRTY);
486}
487
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800488static inline pud_t pud_clear_soft_dirty(pud_t pud)
489{
490 return pud_clear_flags(pud, _PAGE_SOFT_DIRTY);
491}
492
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -0700493#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
494
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800495/*
496 * Mask out unsupported bits in a present pgprot. Non-present pgprots
497 * can use those bits for other purposes, so leave them be.
498 */
499static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
500{
501 pgprotval_t protval = pgprot_val(pgprot);
502
503 if (protval & _PAGE_PRESENT)
504 protval &= __supported_pte_mask;
505
506 return protval;
507}
508
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100509static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
510{
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800511 return __pte(((phys_addr_t)page_nr << PAGE_SHIFT) |
512 massage_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100513}
514
515static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
516{
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800517 return __pmd(((phys_addr_t)page_nr << PAGE_SHIFT) |
518 massage_pgprot(pgprot));
Jeremy Fitzhardinge6fdc05d2008-01-30 13:32:57 +0100519}
520
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800521static inline pud_t pfn_pud(unsigned long page_nr, pgprot_t pgprot)
522{
523 return __pud(((phys_addr_t)page_nr << PAGE_SHIFT) |
524 massage_pgprot(pgprot));
525}
526
Ingo Molnar38472312008-01-30 13:32:57 +0100527static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
528{
529 pteval_t val = pte_val(pte);
530
531 /*
532 * Chop off the NX bit (if present), and add the NX portion of
533 * the newprot (if present):
534 */
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700535 val &= _PAGE_CHG_MASK;
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800536 val |= massage_pgprot(newprot) & ~_PAGE_CHG_MASK;
Ingo Molnar38472312008-01-30 13:32:57 +0100537
538 return __pte(val);
539}
540
Johannes Weinerc489f122011-01-13 15:47:02 -0800541static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
542{
543 pmdval_t val = pmd_val(pmd);
544
545 val &= _HPAGE_CHG_MASK;
546 val |= massage_pgprot(newprot) & ~_HPAGE_CHG_MASK;
547
548 return __pmd(val);
549}
550
Venki Pallipadi1c12c4c2008-05-14 16:05:51 -0700551/* mprotect needs to preserve PAT bits when updating vm_page_prot */
552#define pgprot_modify pgprot_modify
553static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
554{
555 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
556 pgprotval_t addbits = pgprot_val(newprot);
557 return __pgprot(preservebits | addbits);
558}
559
Toshi Kanibbac8c62015-09-17 12:24:18 -0600560#define pte_pgprot(x) __pgprot(pte_flags(x))
561#define pmd_pgprot(x) __pgprot(pmd_flags(x))
562#define pud_pgprot(x) __pgprot(pud_flags(x))
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300563#define p4d_pgprot(x) __pgprot(p4d_flags(x))
Andi Kleenc6ca18e2008-01-30 13:33:51 +0100564
Jeremy Fitzhardingeb5348162009-02-04 18:33:38 -0800565#define canon_pgprot(p) __pgprot(massage_pgprot(p))
Andi Kleen1e8e23b2008-01-30 13:33:53 +0100566
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700567static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
Juergen Grossd85f3332014-11-03 14:01:53 +0100568 enum page_cache_mode pcm,
569 enum page_cache_mode new_pcm)
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800570{
571 /*
H. Peter Anvin55a6ca22009-11-23 15:12:07 -0800572 * PAT type is always WB for untracked ranges, so no need to check.
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700573 */
H. Peter Anvin8a271382009-11-23 14:49:20 -0800574 if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
Suresh Siddha1adcaaf2009-08-17 13:23:50 -0700575 return 1;
576
577 /*
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800578 * Certain new memtypes are not allowed with certain
579 * requested memtype:
580 * - request is uncached, return cannot be write-back
581 * - request is write-combine, return cannot be write-back
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200582 * - request is write-through, return cannot be write-back
583 * - request is write-through, return cannot be write-combine
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800584 */
Juergen Grossd85f3332014-11-03 14:01:53 +0100585 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
586 new_pcm == _PAGE_CACHE_MODE_WB) ||
587 (pcm == _PAGE_CACHE_MODE_WC &&
Toshi Kaniecb2feb2015-06-04 18:55:14 +0200588 new_pcm == _PAGE_CACHE_MODE_WB) ||
589 (pcm == _PAGE_CACHE_MODE_WT &&
590 new_pcm == _PAGE_CACHE_MODE_WB) ||
591 (pcm == _PAGE_CACHE_MODE_WT &&
592 new_pcm == _PAGE_CACHE_MODE_WC)) {
venkatesh.pallipadi@intel.comafc7d202009-01-09 16:13:10 -0800593 return 0;
594 }
595
596 return 1;
597}
598
Tejun Heo458a3e62009-02-24 11:57:21 +0900599pmd_t *populate_extra_pmd(unsigned long vaddr);
600pte_t *populate_extra_pte(unsigned long vaddr);
Jeremy Fitzhardinge46141392008-01-30 13:32:56 +0100601#endif /* __ASSEMBLY__ */
602
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200603#ifdef CONFIG_X86_32
David Howellsa1ce3922012-10-02 18:01:25 +0100604# include <asm/pgtable_32.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200605#else
David Howellsa1ce3922012-10-02 18:01:25 +0100606# include <asm/pgtable_64.h>
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200607#endif
Jeremy Fitzhardinge6c386652008-01-30 13:32:55 +0100608
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800609#ifndef __ASSEMBLY__
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800610#include <linux/mm_types.h>
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -0700611#include <linux/mmdebug.h>
Dave Hansen4cbeb512013-01-22 13:24:31 -0800612#include <linux/log2.h>
Thomas Garnieref37bc32017-03-21 08:17:25 +0100613#include <asm/fixmap.h>
Jeremy Fitzhardingeaca159d2009-02-05 11:30:54 -0800614
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800615static inline int pte_none(pte_t pte)
616{
Dave Hansen97e3c602016-07-07 17:19:12 -0700617 return !(pte.pte & ~(_PAGE_KNL_ERRATUM_MASK));
Jeremy Fitzhardingea034a012009-02-05 11:30:43 -0800618}
619
Jeremy Fitzhardinge8de01da2009-02-05 11:30:44 -0800620#define __HAVE_ARCH_PTE_SAME
621static inline int pte_same(pte_t a, pte_t b)
622{
623 return a.pte == b.pte;
624}
625
Jeremy Fitzhardinge7c683852009-02-05 11:30:45 -0800626static inline int pte_present(pte_t a)
627{
Mel Gormanc46a7c82014-06-04 16:06:30 -0700628 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
629}
630
Dan Williams3565fce2016-01-15 16:56:55 -0800631#ifdef __HAVE_ARCH_PTE_DEVMAP
632static inline int pte_devmap(pte_t a)
633{
634 return (pte_flags(a) & _PAGE_DEVMAP) == _PAGE_DEVMAP;
635}
636#endif
637
Rik van Riel2c3cf552012-10-09 15:31:12 +0200638#define pte_accessible pte_accessible
Rik van Riel20841402013-12-18 17:08:44 -0800639static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
Rik van Riel2c3cf552012-10-09 15:31:12 +0200640{
Rik van Riel20841402013-12-18 17:08:44 -0800641 if (pte_flags(a) & _PAGE_PRESENT)
642 return true;
643
Mel Gorman21d9ee32015-02-12 14:58:32 -0800644 if ((pte_flags(a) & _PAGE_PROTNONE) &&
Rik van Riel20841402013-12-18 17:08:44 -0800645 mm_tlb_flush_pending(mm))
646 return true;
647
648 return false;
Rik van Riel2c3cf552012-10-09 15:31:12 +0200649}
650
Jeremy Fitzhardingeeb636572009-02-06 13:05:56 -0800651static inline int pte_hidden(pte_t pte)
Vegard Nossumdfec0722008-04-04 00:51:41 +0200652{
Jeremy Fitzhardingeeb636572009-02-06 13:05:56 -0800653 return pte_flags(pte) & _PAGE_HIDDEN;
Vegard Nossumdfec0722008-04-04 00:51:41 +0200654}
655
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800656static inline int pmd_present(pmd_t pmd)
657{
Andrea Arcangeli027ef6c2012-10-08 16:33:27 -0700658 /*
659 * Checking for _PAGE_PSE is needed too because
660 * split_huge_page will temporarily clear the present bit (but
661 * the _PAGE_PSE flag will remain set at all times while the
662 * _PAGE_PRESENT bit is clear).
663 */
Mel Gorman21d9ee32015-02-12 14:58:32 -0800664 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
Jeremy Fitzhardinge649e8ef2009-02-05 11:30:50 -0800665}
666
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800667#ifdef CONFIG_NUMA_BALANCING
668/*
669 * These work without NUMA balancing but the kernel does not care. See the
670 * comment in include/asm-generic/pgtable.h
671 */
672static inline int pte_protnone(pte_t pte)
673{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000674 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
675 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800676}
677
678static inline int pmd_protnone(pmd_t pmd)
679{
David Vrabele3a1f6c2015-02-19 13:06:53 +0000680 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
681 == _PAGE_PROTNONE;
Mel Gormane7bb4b6d2015-02-12 14:58:19 -0800682}
683#endif /* CONFIG_NUMA_BALANCING */
684
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800685static inline int pmd_none(pmd_t pmd)
686{
687 /* Only check low word on 32-bit platforms, since it might be
688 out of sync with upper half. */
Dave Hansen97e3c602016-07-07 17:19:12 -0700689 unsigned long val = native_pmd_val(pmd);
690 return (val & ~_PAGE_KNL_ERRATUM_MASK) == 0;
Jeremy Fitzhardinge4fea8012009-02-05 11:30:51 -0800691}
692
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800693static inline unsigned long pmd_page_vaddr(pmd_t pmd)
694{
Toshi Kanif70abb02015-09-17 12:24:17 -0600695 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
Jeremy Fitzhardinge3ffb3562009-02-05 11:30:59 -0800696}
697
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100698/*
699 * Currently stuck as a macro due to indirect forward reference to
700 * linux/mmzone.h's __section_mem_map_addr() definition:
701 */
Toshi Kanif70abb02015-09-17 12:24:17 -0600702#define pmd_page(pmd) \
703 pfn_to_page((pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT)
Jeremy Fitzhardinge20063ca2009-02-05 11:31:00 -0800704
Jeremy Fitzhardingee24d7ee2009-02-05 11:31:01 -0800705/*
706 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
707 *
708 * this macro returns the index of the entry in the pmd page which would
709 * control the given virtual address
710 */
Figo.zhangce0c0f92009-06-28 18:07:39 +0800711static inline unsigned long pmd_index(unsigned long address)
Jeremy Fitzhardingee24d7ee2009-02-05 11:31:01 -0800712{
713 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
714}
715
Jeremy Fitzhardinge97e28172009-02-05 11:31:05 -0800716/*
717 * Conversion functions: convert a page and protection to a page entry,
718 * and a page entry and page directory to the page they refer to.
719 *
720 * (Currently stuck as a macro because of indirect forward reference
721 * to linux/mm.h:page_to_nid())
722 */
723#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
724
Jeremy Fitzhardinge346309c2009-02-05 11:31:06 -0800725/*
726 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
727 *
728 * this function returns the index of the entry in the pte page which would
729 * control the given virtual address
730 */
Figo.zhangce0c0f92009-06-28 18:07:39 +0800731static inline unsigned long pte_index(unsigned long address)
Jeremy Fitzhardinge346309c2009-02-05 11:31:06 -0800732{
733 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
734}
735
Jeremy Fitzhardinge3fbc2442009-02-05 11:31:07 -0800736static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
737{
738 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
739}
740
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800741static inline int pmd_bad(pmd_t pmd)
742{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800743 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
Jeremy Fitzhardinge99510232009-02-05 11:31:11 -0800744}
745
Jeremy Fitzhardingecc290ca2009-02-05 11:31:12 -0800746static inline unsigned long pages_to_mb(unsigned long npg)
747{
748 return npg >> (20 - PAGE_SHIFT);
749}
750
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700751#if CONFIG_PGTABLE_LEVELS > 2
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800752static inline int pud_none(pud_t pud)
753{
Dave Hansen97e3c602016-07-07 17:19:12 -0700754 return (native_pud_val(pud) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
Jeremy Fitzhardingedeb79cf2009-02-05 11:31:13 -0800755}
756
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800757static inline int pud_present(pud_t pud)
758{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800759 return pud_flags(pud) & _PAGE_PRESENT;
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800760}
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800761
762static inline unsigned long pud_page_vaddr(pud_t pud)
763{
Toshi Kanif70abb02015-09-17 12:24:17 -0600764 return (unsigned long)__va(pud_val(pud) & pud_pfn_mask(pud));
Jeremy Fitzhardinge6fff47e2009-02-05 11:30:53 -0800765}
Jeremy Fitzhardingef4769612009-02-05 11:30:55 -0800766
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100767/*
768 * Currently stuck as a macro due to indirect forward reference to
769 * linux/mmzone.h's __section_mem_map_addr() definition:
770 */
Toshi Kanif70abb02015-09-17 12:24:17 -0600771#define pud_page(pud) \
772 pfn_to_page((pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT)
Jeremy Fitzhardinge01ade20d5a2009-02-05 11:31:02 -0800773
774/* Find an entry in the second-level page table.. */
775static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
776{
777 return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address);
778}
Jeremy Fitzhardinge3180fba2009-02-05 11:31:04 -0800779
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800780static inline int pud_large(pud_t pud)
781{
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800782 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
Jeremy Fitzhardinge3f6cbef2009-02-05 11:31:08 -0800783 (_PAGE_PSE | _PAGE_PRESENT);
784}
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800785
786static inline int pud_bad(pud_t pud)
787{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800788 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
Jeremy Fitzhardingea61bb292009-02-05 11:31:10 -0800789}
Jeremy Fitzhardingee2f5bda2009-02-09 00:09:52 -0800790#else
791static inline int pud_large(pud_t pud)
792{
793 return 0;
794}
Kirill A. Shutemov98233362015-04-14 15:46:14 -0700795#endif /* CONFIG_PGTABLE_LEVELS > 2 */
Jeremy Fitzhardinge5ba7c912009-02-05 11:30:48 -0800796
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300797static inline unsigned long pud_index(unsigned long address)
798{
799 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
800}
801
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300802#if CONFIG_PGTABLE_LEVELS > 3
803static inline int p4d_none(p4d_t p4d)
804{
805 return (native_p4d_val(p4d) & ~(_PAGE_KNL_ERRATUM_MASK)) == 0;
806}
807
808static inline int p4d_present(p4d_t p4d)
809{
810 return p4d_flags(p4d) & _PAGE_PRESENT;
811}
812
813static inline unsigned long p4d_page_vaddr(p4d_t p4d)
814{
815 return (unsigned long)__va(p4d_val(p4d) & p4d_pfn_mask(p4d));
816}
817
818/*
819 * Currently stuck as a macro due to indirect forward reference to
820 * linux/mmzone.h's __section_mem_map_addr() definition:
821 */
822#define p4d_page(p4d) \
823 pfn_to_page((p4d_val(p4d) & p4d_pfn_mask(p4d)) >> PAGE_SHIFT)
824
825/* Find an entry in the third-level page table.. */
826static inline pud_t *pud_offset(p4d_t *p4d, unsigned long address)
827{
828 return (pud_t *)p4d_page_vaddr(*p4d) + pud_index(address);
829}
830
831static inline int p4d_bad(p4d_t p4d)
832{
833 return (p4d_flags(p4d) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
834}
835#endif /* CONFIG_PGTABLE_LEVELS > 3 */
836
Kirill A. Shutemovfe1e8c32017-03-13 17:33:04 +0300837static inline unsigned long p4d_index(unsigned long address)
838{
839 return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
840}
841
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300842#if CONFIG_PGTABLE_LEVELS > 4
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800843static inline int pgd_present(pgd_t pgd)
844{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800845 return pgd_flags(pgd) & _PAGE_PRESENT;
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800846}
Jeremy Fitzhardingec5f040b2009-02-05 11:30:52 -0800847
848static inline unsigned long pgd_page_vaddr(pgd_t pgd)
849{
850 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
851}
Jeremy Fitzhardinge777cba12009-02-05 11:30:56 -0800852
Ingo Molnare5f7f2022009-02-09 11:42:57 +0100853/*
854 * Currently stuck as a macro due to indirect forward reference to
855 * linux/mmzone.h's __section_mem_map_addr() definition:
856 */
857#define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT)
Jeremy Fitzhardinge7cfb8102009-02-05 11:30:57 -0800858
859/* to find an entry in a page-table-directory. */
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300860static inline p4d_t *p4d_offset(pgd_t *pgd, unsigned long address)
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800861{
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300862 return (p4d_t *)pgd_page_vaddr(*pgd) + p4d_index(address);
Jeremy Fitzhardinge3d081b12009-02-05 11:30:58 -0800863}
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800864
865static inline int pgd_bad(pgd_t pgd)
866{
Jeremy Fitzhardinge18a7a1992009-02-05 11:31:16 -0800867 return (pgd_flags(pgd) & ~_PAGE_USER) != _KERNPG_TABLE;
Jeremy Fitzhardinge30f10312009-02-05 11:31:09 -0800868}
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800869
870static inline int pgd_none(pgd_t pgd)
871{
Dave Hansen97e3c602016-07-07 17:19:12 -0700872 /*
873 * There is no need to do a workaround for the KNL stray
874 * A/D bit erratum here. PGDs only point to page tables
875 * except on 32-bit non-PAE which is not supported on
876 * KNL.
877 */
Jeremy Fitzhardinge26c8e312009-02-05 11:31:17 -0800878 return !native_pgd_val(pgd);
Jeremy Fitzhardinge7325cc22009-02-05 11:31:14 -0800879}
Kirill A. Shutemovf2a6a702017-03-17 21:55:15 +0300880#endif /* CONFIG_PGTABLE_LEVELS > 4 */
Jeremy Fitzhardinge9f38d7e2009-02-05 11:30:49 -0800881
Thomas Gleixner96a388d2007-10-11 11:20:03 +0200882#endif /* __ASSEMBLY__ */
883
Jeremy Fitzhardingefb15a9b2008-06-25 00:19:06 -0400884/*
885 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
886 *
887 * this macro returns the index of the entry in the pgd page which would
888 * control the given virtual address
889 */
890#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
891
892/*
893 * pgd_offset() returns a (pgd_t *)
894 * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
895 */
896#define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address)))
897/*
898 * a shortcut which implies the use of the kernel's pgd, instead
899 * of a process's
900 */
901#define pgd_offset_k(address) pgd_offset(&init_mm, (address))
902
903
Jeremy Fitzhardinge68db0652008-03-17 16:37:13 -0700904#define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
905#define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
906
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100907#ifndef __ASSEMBLY__
908
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +0530909extern int direct_gbpages;
Yinghai Lu22ddfca2012-11-16 19:38:41 -0800910void init_mem_mapping(void);
Yinghai Lu8d574702012-11-16 19:38:58 -0800911void early_alloc_pgt_buf(void);
Ingo Molnar4270fd82017-01-28 12:45:40 +0100912extern void memblock_find_dma_reserve(void);
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +0530913
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700914#ifdef CONFIG_X86_64
915/* Realmode trampoline initialization. */
916extern pgd_t trampoline_pgd_entry;
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700917static inline void __meminit init_trampoline_default(void)
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700918{
919 /* Default trampoline pgd value */
920 trampoline_pgd_entry = init_level4_pgt[pgd_index(__PAGE_OFFSET)];
921}
Thomas Garnier0483e1f2016-06-21 17:47:02 -0700922# ifdef CONFIG_RANDOMIZE_MEMORY
923void __meminit init_trampoline(void);
924# else
925# define init_trampoline init_trampoline_default
926# endif
Thomas Garnierb234e8a2016-06-21 17:47:01 -0700927#else
928static inline void init_trampoline(void) { }
929#endif
930
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100931/* local pte updates need not use xchg for locking */
932static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
933{
934 pte_t res = *ptep;
935
936 /* Pure native function needs no input for mm, addr */
937 native_pte_clear(NULL, 0, ptep);
938 return res;
939}
940
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -0800941static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
942{
943 pmd_t res = *pmdp;
944
945 native_pmd_clear(pmdp);
946 return res;
947}
948
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800949static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)
950{
951 pud_t res = *pudp;
952
953 native_pud_clear(pudp);
954 return res;
955}
956
Jeremy Fitzhardinge48916452008-01-30 13:32:58 +0100957static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
958 pte_t *ptep , pte_t pte)
959{
960 native_set_pte(ptep, pte);
961}
962
Andrea Arcangeli0a47de52011-01-13 15:46:35 -0800963static inline void native_set_pmd_at(struct mm_struct *mm, unsigned long addr,
964 pmd_t *pmdp , pmd_t pmd)
965{
966 native_set_pmd(pmdp, pmd);
967}
968
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800969static inline void native_set_pud_at(struct mm_struct *mm, unsigned long addr,
970 pud_t *pudp, pud_t pud)
971{
972 native_set_pud(pudp, pud);
973}
974
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100975#ifndef CONFIG_PARAVIRT
976/*
977 * Rules for using pte_update - it must be called after any PTE update which
978 * has not been done using the set_pte / clear_pte interfaces. It is used by
979 * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
980 * updates should either be sets, clears, or set_pte_atomic for P->P
981 * transitions, which means this hook should only be called for user PTEs.
982 * This hook implies a P->P protection or access change has taken place, which
Juergen Grossd6ccc3e2015-11-17 15:51:19 +0100983 * requires a subsequent TLB flush.
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100984 */
985#define pte_update(mm, addr, ptep) do { } while (0)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100986#endif
987
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100988/*
989 * We only update the dirty/accessed state if we set
990 * the dirty bit by hand in the kernel, since the hardware
991 * will do the accessed bit for us, and we don't want to
992 * race with other CPU's that might be updating the dirty
993 * bit at the same time.
994 */
Jeremy Fitzhardingebea41802008-06-25 00:18:57 -0400995struct vm_area_struct;
996
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +0100997#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
Jeremy Fitzhardingeee5aa8d2008-03-17 16:37:03 -0700998extern int ptep_set_access_flags(struct vm_area_struct *vma,
999 unsigned long address, pte_t *ptep,
1000 pte_t entry, int dirty);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001001
1002#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
Jeremy Fitzhardingef9fbf1a2008-03-17 16:37:04 -07001003extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
1004 unsigned long addr, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001005
1006#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
Jeremy Fitzhardingec20311e2008-03-17 16:37:05 -07001007extern int ptep_clear_flush_young(struct vm_area_struct *vma,
1008 unsigned long address, pte_t *ptep);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001009
1010#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001011static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
1012 pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001013{
1014 pte_t pte = native_ptep_get_and_clear(ptep);
1015 pte_update(mm, addr, ptep);
1016 return pte;
1017}
1018
1019#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001020static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
1021 unsigned long addr, pte_t *ptep,
1022 int full)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001023{
1024 pte_t pte;
1025 if (full) {
1026 /*
1027 * Full address destruction in progress; paravirt does not
1028 * care about updates and native needs no locking
1029 */
1030 pte = native_local_ptep_get_and_clear(ptep);
1031 } else {
1032 pte = ptep_get_and_clear(mm, addr, ptep);
1033 }
1034 return pte;
1035}
1036
1037#define __HAVE_ARCH_PTEP_SET_WRPROTECT
Joe Perches3cbaeaf2008-03-23 01:03:12 -07001038static inline void ptep_set_wrprotect(struct mm_struct *mm,
1039 unsigned long addr, pte_t *ptep)
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001040{
Jeremy Fitzhardinged8d89822008-01-30 13:32:58 +01001041 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
Jeremy Fitzhardinge195466d2008-01-30 13:32:58 +01001042 pte_update(mm, addr, ptep);
1043}
1044
Jesper Juhl2ac13462011-12-18 01:32:09 +01001045#define flush_tlb_fix_spurious_fault(vma, address) do { } while (0)
Shaohua Li61c77322010-08-16 09:16:55 +08001046
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001047#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
1048
1049#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
1050extern int pmdp_set_access_flags(struct vm_area_struct *vma,
1051 unsigned long address, pmd_t *pmdp,
1052 pmd_t entry, int dirty);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001053extern int pudp_set_access_flags(struct vm_area_struct *vma,
1054 unsigned long address, pud_t *pudp,
1055 pud_t entry, int dirty);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001056
1057#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
1058extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
1059 unsigned long addr, pmd_t *pmdp);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001060extern int pudp_test_and_clear_young(struct vm_area_struct *vma,
1061 unsigned long addr, pud_t *pudp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001062
1063#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
1064extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
1065 unsigned long address, pmd_t *pmdp);
1066
1067
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001068#define __HAVE_ARCH_PMD_WRITE
1069static inline int pmd_write(pmd_t pmd)
1070{
1071 return pmd_flags(pmd) & _PAGE_RW;
1072}
1073
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001074#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
1075static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001076 pmd_t *pmdp)
1077{
Juergen Grossd6ccc3e2015-11-17 15:51:19 +01001078 return native_pmdp_get_and_clear(pmdp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001079}
1080
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001081#define __HAVE_ARCH_PUDP_HUGE_GET_AND_CLEAR
1082static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,
1083 unsigned long addr, pud_t *pudp)
1084{
1085 return native_pudp_get_and_clear(pudp);
1086}
1087
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001088#define __HAVE_ARCH_PMDP_SET_WRPROTECT
1089static inline void pmdp_set_wrprotect(struct mm_struct *mm,
1090 unsigned long addr, pmd_t *pmdp)
1091{
1092 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp);
Johannes Weinerf2d6bfe2011-01-13 15:47:01 -08001093}
1094
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001095/*
1096 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
1097 *
1098 * dst - pointer to pgd range anwhere on a pgd page
1099 * src - ""
1100 * count - the number of pgds to copy.
1101 *
1102 * dst and src can be on the same page, but the range must not overlap,
1103 * and must not cross a page boundary.
1104 */
1105static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
1106{
1107 memcpy(dst, src, count * sizeof(pgd_t));
1108}
1109
Dave Hansen4cbeb512013-01-22 13:24:31 -08001110#define PTE_SHIFT ilog2(PTRS_PER_PTE)
1111static inline int page_level_shift(enum pg_level level)
1112{
1113 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
1114}
1115static inline unsigned long page_level_size(enum pg_level level)
1116{
1117 return 1UL << page_level_shift(level);
1118}
1119static inline unsigned long page_level_mask(enum pg_level level)
1120{
1121 return ~(page_level_size(level) - 1);
1122}
Jeremy Fitzhardinge85958b42008-03-17 16:37:14 -07001123
Kirill A. Shutemov602e0182012-12-18 12:22:18 -08001124/*
1125 * The x86 doesn't have any external MMU info: the kernel page
1126 * tables contain all the necessary information.
1127 */
1128static inline void update_mmu_cache(struct vm_area_struct *vma,
1129 unsigned long addr, pte_t *ptep)
1130{
1131}
1132static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
1133 unsigned long addr, pmd_t *pmd)
1134{
1135}
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001136static inline void update_mmu_cache_pud(struct vm_area_struct *vma,
1137 unsigned long addr, pud_t *pud)
1138{
1139}
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001140
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001141#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001142static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
1143{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001144 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1145}
1146
1147static inline int pte_swp_soft_dirty(pte_t pte)
1148{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001149 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
1150}
1151
1152static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
1153{
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001154 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
1155}
Cyrill Gorcunov2bf01f92014-06-04 16:08:16 -07001156#endif
Cyrill Gorcunovfa0f2812013-09-11 14:22:47 -07001157
Dave Hansen33a709b2016-02-12 13:02:19 -08001158#define PKRU_AD_BIT 0x1
1159#define PKRU_WD_BIT 0x2
Dave Hansen84594292016-02-12 13:02:36 -08001160#define PKRU_BITS_PER_PKEY 2
Dave Hansen33a709b2016-02-12 13:02:19 -08001161
1162static inline bool __pkru_allows_read(u32 pkru, u16 pkey)
1163{
Dave Hansen84594292016-02-12 13:02:36 -08001164 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
Dave Hansen33a709b2016-02-12 13:02:19 -08001165 return !(pkru & (PKRU_AD_BIT << pkru_pkey_bits));
1166}
1167
1168static inline bool __pkru_allows_write(u32 pkru, u16 pkey)
1169{
Dave Hansen84594292016-02-12 13:02:36 -08001170 int pkru_pkey_bits = pkey * PKRU_BITS_PER_PKEY;
Dave Hansen33a709b2016-02-12 13:02:19 -08001171 /*
1172 * Access-disable disables writes too so we need to check
1173 * both bits here.
1174 */
1175 return !(pkru & ((PKRU_AD_BIT|PKRU_WD_BIT) << pkru_pkey_bits));
1176}
1177
1178static inline u16 pte_flags_pkey(unsigned long pte_flags)
1179{
1180#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
1181 /* ifdef to avoid doing 59-bit shift on 32-bit values */
1182 return (pte_flags & _PAGE_PKEY_MASK) >> _PAGE_BIT_PKEY_BIT0;
1183#else
1184 return 0;
1185#endif
1186}
1187
Thomas Gleixner96a388d2007-10-11 11:20:03 +02001188#include <asm-generic/pgtable.h>
1189#endif /* __ASSEMBLY__ */
1190
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001191#endif /* _ASM_X86_PGTABLE_H */