]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/include/asm/pgtable.h
Merge tag 'v4.3-rc3' into x86/mm, to pick up fixes before applying new changes
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / include / asm / pgtable.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_PGTABLE_H
2#define _ASM_X86_PGTABLE_H
6c386655 3
c47c1b1f 4#include <asm/page.h>
1adcaafe 5#include <asm/e820.h>
c47c1b1f 6
8d19c99f 7#include <asm/pgtable_types.h>
b2bc2731 8
8a7b12f7 9/*
10 * Macro to mark a page protection value as UC-
11 */
d85f3334
JG
12#define pgprot_noncached(prot) \
13 ((boot_cpu_data.x86 > 3) \
14 ? (__pgprot(pgprot_val(prot) | \
15 cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS))) \
8a7b12f7 16 : (prot))
17
4614139c 18#ifndef __ASSEMBLY__
55a6ca25
PA
19#include <asm/x86_init.h>
20
ef6bea6d
BP
21void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd);
22
8405b122
JF
23/*
24 * ZERO_PAGE is a global shared page that is always zero: used
25 * for zero-mapped memory areas etc..
26 */
277d5b40
AK
27extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
28 __visible;
8405b122
JF
29#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
30
e3ed910d
JF
31extern spinlock_t pgd_lock;
32extern struct list_head pgd_list;
8405b122 33
617d34d9
JF
34extern struct mm_struct *pgd_page_get_mm(struct page *page);
35
54321d94
JF
36#ifdef CONFIG_PARAVIRT
37#include <asm/paravirt.h>
38#else /* !CONFIG_PARAVIRT */
39#define set_pte(ptep, pte) native_set_pte(ptep, pte)
40#define set_pte_at(mm, addr, ptep, pte) native_set_pte_at(mm, addr, ptep, pte)
2609ae6d 41#define set_pmd_at(mm, addr, pmdp, pmd) native_set_pmd_at(mm, addr, pmdp, pmd)
54321d94 42
54321d94
JF
43#define set_pte_atomic(ptep, pte) \
44 native_set_pte_atomic(ptep, pte)
45
46#define set_pmd(pmdp, pmd) native_set_pmd(pmdp, pmd)
47
48#ifndef __PAGETABLE_PUD_FOLDED
49#define set_pgd(pgdp, pgd) native_set_pgd(pgdp, pgd)
50#define pgd_clear(pgd) native_pgd_clear(pgd)
51#endif
52
53#ifndef set_pud
54# define set_pud(pudp, pud) native_set_pud(pudp, pud)
55#endif
56
57#ifndef __PAGETABLE_PMD_FOLDED
58#define pud_clear(pud) native_pud_clear(pud)
59#endif
60
61#define pte_clear(mm, addr, ptep) native_pte_clear(mm, addr, ptep)
62#define pmd_clear(pmd) native_pmd_clear(pmd)
63
64#define pte_update(mm, addr, ptep) do { } while (0)
65#define pte_update_defer(mm, addr, ptep) do { } while (0)
2609ae6d
AA
66#define pmd_update(mm, addr, ptep) do { } while (0)
67#define pmd_update_defer(mm, addr, ptep) do { } while (0)
54321d94 68
54321d94
JF
69#define pgd_val(x) native_pgd_val(x)
70#define __pgd(x) native_make_pgd(x)
71
72#ifndef __PAGETABLE_PUD_FOLDED
73#define pud_val(x) native_pud_val(x)
74#define __pud(x) native_make_pud(x)
75#endif
76
77#ifndef __PAGETABLE_PMD_FOLDED
78#define pmd_val(x) native_pmd_val(x)
79#define __pmd(x) native_make_pmd(x)
80#endif
81
82#define pte_val(x) native_pte_val(x)
83#define __pte(x) native_make_pte(x)
84
224101ed
JF
85#define arch_end_context_switch(prev) do {} while(0)
86
54321d94
JF
87#endif /* CONFIG_PARAVIRT */
88
4614139c
JF
89/*
90 * The following only work if pte_present() is true.
91 * Undefined behaviour if not..
92 */
3cbaeafe
JP
93static inline int pte_dirty(pte_t pte)
94{
a15af1c9 95 return pte_flags(pte) & _PAGE_DIRTY;
3cbaeafe
JP
96}
97
98static inline int pte_young(pte_t pte)
99{
a15af1c9 100 return pte_flags(pte) & _PAGE_ACCESSED;
3cbaeafe
JP
101}
102
c164e038
KS
103static inline int pmd_dirty(pmd_t pmd)
104{
105 return pmd_flags(pmd) & _PAGE_DIRTY;
106}
3cbaeafe 107
f2d6bfe9
JW
108static inline int pmd_young(pmd_t pmd)
109{
110 return pmd_flags(pmd) & _PAGE_ACCESSED;
111}
112
3cbaeafe
JP
113static inline int pte_write(pte_t pte)
114{
a15af1c9 115 return pte_flags(pte) & _PAGE_RW;
3cbaeafe
JP
116}
117
3cbaeafe
JP
118static inline int pte_huge(pte_t pte)
119{
a15af1c9 120 return pte_flags(pte) & _PAGE_PSE;
4614139c
JF
121}
122
3cbaeafe
JP
123static inline int pte_global(pte_t pte)
124{
a15af1c9 125 return pte_flags(pte) & _PAGE_GLOBAL;
3cbaeafe
JP
126}
127
128static inline int pte_exec(pte_t pte)
129{
a15af1c9 130 return !(pte_flags(pte) & _PAGE_NX);
3cbaeafe
JP
131}
132
7e675137
NP
133static inline int pte_special(pte_t pte)
134{
c819f37e 135 return pte_flags(pte) & _PAGE_SPECIAL;
7e675137
NP
136}
137
91030ca1
HD
138static inline unsigned long pte_pfn(pte_t pte)
139{
140 return (pte_val(pte) & PTE_PFN_MASK) >> PAGE_SHIFT;
141}
142
087975b0
AM
143static inline unsigned long pmd_pfn(pmd_t pmd)
144{
f70abb0f 145 return (pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT;
087975b0
AM
146}
147
0ee364eb
MG
148static inline unsigned long pud_pfn(pud_t pud)
149{
f70abb0f 150 return (pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT;
0ee364eb
MG
151}
152
91030ca1
HD
153#define pte_page(pte) pfn_to_page(pte_pfn(pte))
154
3cbaeafe
JP
155static inline int pmd_large(pmd_t pte)
156{
027ef6c8 157 return pmd_flags(pte) & _PAGE_PSE;
3cbaeafe
JP
158}
159
f2d6bfe9
JW
160#ifdef CONFIG_TRANSPARENT_HUGEPAGE
161static inline int pmd_trans_splitting(pmd_t pmd)
162{
163 return pmd_val(pmd) & _PAGE_SPLITTING;
164}
165
166static inline int pmd_trans_huge(pmd_t pmd)
167{
168 return pmd_val(pmd) & _PAGE_PSE;
169}
4b7167b9
AA
170
171static inline int has_transparent_hugepage(void)
172{
173 return cpu_has_pse;
174}
f2d6bfe9
JW
175#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
176
6522869c
JF
177static inline pte_t pte_set_flags(pte_t pte, pteval_t set)
178{
179 pteval_t v = native_pte_val(pte);
180
181 return native_make_pte(v | set);
182}
183
184static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
185{
186 pteval_t v = native_pte_val(pte);
187
188 return native_make_pte(v & ~clear);
189}
190
3cbaeafe
JP
191static inline pte_t pte_mkclean(pte_t pte)
192{
6522869c 193 return pte_clear_flags(pte, _PAGE_DIRTY);
3cbaeafe
JP
194}
195
196static inline pte_t pte_mkold(pte_t pte)
197{
6522869c 198 return pte_clear_flags(pte, _PAGE_ACCESSED);
3cbaeafe
JP
199}
200
201static inline pte_t pte_wrprotect(pte_t pte)
202{
6522869c 203 return pte_clear_flags(pte, _PAGE_RW);
3cbaeafe
JP
204}
205
206static inline pte_t pte_mkexec(pte_t pte)
207{
6522869c 208 return pte_clear_flags(pte, _PAGE_NX);
3cbaeafe
JP
209}
210
211static inline pte_t pte_mkdirty(pte_t pte)
212{
0f8975ec 213 return pte_set_flags(pte, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
3cbaeafe
JP
214}
215
216static inline pte_t pte_mkyoung(pte_t pte)
217{
6522869c 218 return pte_set_flags(pte, _PAGE_ACCESSED);
3cbaeafe
JP
219}
220
221static inline pte_t pte_mkwrite(pte_t pte)
222{
6522869c 223 return pte_set_flags(pte, _PAGE_RW);
3cbaeafe
JP
224}
225
226static inline pte_t pte_mkhuge(pte_t pte)
227{
6522869c 228 return pte_set_flags(pte, _PAGE_PSE);
3cbaeafe
JP
229}
230
231static inline pte_t pte_clrhuge(pte_t pte)
232{
6522869c 233 return pte_clear_flags(pte, _PAGE_PSE);
3cbaeafe
JP
234}
235
236static inline pte_t pte_mkglobal(pte_t pte)
237{
6522869c 238 return pte_set_flags(pte, _PAGE_GLOBAL);
3cbaeafe
JP
239}
240
241static inline pte_t pte_clrglobal(pte_t pte)
242{
6522869c 243 return pte_clear_flags(pte, _PAGE_GLOBAL);
3cbaeafe 244}
4614139c 245
7e675137
NP
246static inline pte_t pte_mkspecial(pte_t pte)
247{
6522869c 248 return pte_set_flags(pte, _PAGE_SPECIAL);
7e675137
NP
249}
250
f2d6bfe9
JW
251static inline pmd_t pmd_set_flags(pmd_t pmd, pmdval_t set)
252{
253 pmdval_t v = native_pmd_val(pmd);
254
255 return __pmd(v | set);
256}
257
258static inline pmd_t pmd_clear_flags(pmd_t pmd, pmdval_t clear)
259{
260 pmdval_t v = native_pmd_val(pmd);
261
262 return __pmd(v & ~clear);
263}
264
265static inline pmd_t pmd_mkold(pmd_t pmd)
266{
267 return pmd_clear_flags(pmd, _PAGE_ACCESSED);
268}
269
270static inline pmd_t pmd_wrprotect(pmd_t pmd)
271{
272 return pmd_clear_flags(pmd, _PAGE_RW);
273}
274
275static inline pmd_t pmd_mkdirty(pmd_t pmd)
276{
0f8975ec 277 return pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY);
f2d6bfe9
JW
278}
279
280static inline pmd_t pmd_mkhuge(pmd_t pmd)
281{
282 return pmd_set_flags(pmd, _PAGE_PSE);
283}
284
285static inline pmd_t pmd_mkyoung(pmd_t pmd)
286{
287 return pmd_set_flags(pmd, _PAGE_ACCESSED);
288}
289
290static inline pmd_t pmd_mkwrite(pmd_t pmd)
291{
292 return pmd_set_flags(pmd, _PAGE_RW);
293}
294
295static inline pmd_t pmd_mknotpresent(pmd_t pmd)
296{
21d9ee3e 297 return pmd_clear_flags(pmd, _PAGE_PRESENT | _PAGE_PROTNONE);
f2d6bfe9
JW
298}
299
2bf01f9f 300#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
0f8975ec
PE
301static inline int pte_soft_dirty(pte_t pte)
302{
303 return pte_flags(pte) & _PAGE_SOFT_DIRTY;
304}
305
306static inline int pmd_soft_dirty(pmd_t pmd)
307{
308 return pmd_flags(pmd) & _PAGE_SOFT_DIRTY;
309}
310
311static inline pte_t pte_mksoft_dirty(pte_t pte)
312{
313 return pte_set_flags(pte, _PAGE_SOFT_DIRTY);
314}
315
316static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
317{
318 return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
319}
320
2bf01f9f
CG
321#endif /* CONFIG_HAVE_ARCH_SOFT_DIRTY */
322
b534816b
JF
323/*
324 * Mask out unsupported bits in a present pgprot. Non-present pgprots
325 * can use those bits for other purposes, so leave them be.
326 */
327static inline pgprotval_t massage_pgprot(pgprot_t pgprot)
328{
329 pgprotval_t protval = pgprot_val(pgprot);
330
331 if (protval & _PAGE_PRESENT)
332 protval &= __supported_pte_mask;
333
334 return protval;
335}
336
6fdc05d4
JF
337static inline pte_t pfn_pte(unsigned long page_nr, pgprot_t pgprot)
338{
b534816b
JF
339 return __pte(((phys_addr_t)page_nr << PAGE_SHIFT) |
340 massage_pgprot(pgprot));
6fdc05d4
JF
341}
342
343static inline pmd_t pfn_pmd(unsigned long page_nr, pgprot_t pgprot)
344{
b534816b
JF
345 return __pmd(((phys_addr_t)page_nr << PAGE_SHIFT) |
346 massage_pgprot(pgprot));
6fdc05d4
JF
347}
348
38472311
IM
349static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
350{
351 pteval_t val = pte_val(pte);
352
353 /*
354 * Chop off the NX bit (if present), and add the NX portion of
355 * the newprot (if present):
356 */
1c12c4cf 357 val &= _PAGE_CHG_MASK;
b534816b 358 val |= massage_pgprot(newprot) & ~_PAGE_CHG_MASK;
38472311
IM
359
360 return __pte(val);
361}
362
c489f125
JW
363static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
364{
365 pmdval_t val = pmd_val(pmd);
366
367 val &= _HPAGE_CHG_MASK;
368 val |= massage_pgprot(newprot) & ~_HPAGE_CHG_MASK;
369
370 return __pmd(val);
371}
372
1c12c4cf
VP
373/* mprotect needs to preserve PAT bits when updating vm_page_prot */
374#define pgprot_modify pgprot_modify
375static inline pgprot_t pgprot_modify(pgprot_t oldprot, pgprot_t newprot)
376{
377 pgprotval_t preservebits = pgprot_val(oldprot) & _PAGE_CHG_MASK;
378 pgprotval_t addbits = pgprot_val(newprot);
379 return __pgprot(preservebits | addbits);
380}
381
bbac8c6d
TK
382#define pte_pgprot(x) __pgprot(pte_flags(x))
383#define pmd_pgprot(x) __pgprot(pmd_flags(x))
384#define pud_pgprot(x) __pgprot(pud_flags(x))
c6ca18eb 385
b534816b 386#define canon_pgprot(p) __pgprot(massage_pgprot(p))
1e8e23bc 387
1adcaafe 388static inline int is_new_memtype_allowed(u64 paddr, unsigned long size,
d85f3334
JG
389 enum page_cache_mode pcm,
390 enum page_cache_mode new_pcm)
afc7d20c 391{
1adcaafe 392 /*
55a6ca25 393 * PAT type is always WB for untracked ranges, so no need to check.
1adcaafe 394 */
8a271389 395 if (x86_platform.is_untracked_pat_range(paddr, paddr + size))
1adcaafe
SS
396 return 1;
397
afc7d20c 398 /*
399 * Certain new memtypes are not allowed with certain
400 * requested memtype:
401 * - request is uncached, return cannot be write-back
402 * - request is write-combine, return cannot be write-back
ecb2feba
TK
403 * - request is write-through, return cannot be write-back
404 * - request is write-through, return cannot be write-combine
afc7d20c 405 */
d85f3334
JG
406 if ((pcm == _PAGE_CACHE_MODE_UC_MINUS &&
407 new_pcm == _PAGE_CACHE_MODE_WB) ||
408 (pcm == _PAGE_CACHE_MODE_WC &&
ecb2feba
TK
409 new_pcm == _PAGE_CACHE_MODE_WB) ||
410 (pcm == _PAGE_CACHE_MODE_WT &&
411 new_pcm == _PAGE_CACHE_MODE_WB) ||
412 (pcm == _PAGE_CACHE_MODE_WT &&
413 new_pcm == _PAGE_CACHE_MODE_WC)) {
afc7d20c 414 return 0;
415 }
416
417 return 1;
418}
419
458a3e64
TH
420pmd_t *populate_extra_pmd(unsigned long vaddr);
421pte_t *populate_extra_pte(unsigned long vaddr);
4614139c
JF
422#endif /* __ASSEMBLY__ */
423
96a388de 424#ifdef CONFIG_X86_32
a1ce3928 425# include <asm/pgtable_32.h>
96a388de 426#else
a1ce3928 427# include <asm/pgtable_64.h>
96a388de 428#endif
6c386655 429
aca159db 430#ifndef __ASSEMBLY__
f476961c 431#include <linux/mm_types.h>
fa0f281c 432#include <linux/mmdebug.h>
4cbeb51b 433#include <linux/log2.h>
aca159db 434
a034a010
JF
435static inline int pte_none(pte_t pte)
436{
437 return !pte.pte;
438}
439
8de01da3
JF
440#define __HAVE_ARCH_PTE_SAME
441static inline int pte_same(pte_t a, pte_t b)
442{
443 return a.pte == b.pte;
444}
445
7c683851 446static inline int pte_present(pte_t a)
c46a7c81
MG
447{
448 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
449}
450
2c3cf556 451#define pte_accessible pte_accessible
20841405 452static inline bool pte_accessible(struct mm_struct *mm, pte_t a)
2c3cf556 453{
20841405
RR
454 if (pte_flags(a) & _PAGE_PRESENT)
455 return true;
456
21d9ee3e 457 if ((pte_flags(a) & _PAGE_PROTNONE) &&
20841405
RR
458 mm_tlb_flush_pending(mm))
459 return true;
460
461 return false;
2c3cf556
RR
462}
463
eb63657e 464static inline int pte_hidden(pte_t pte)
dfec072e 465{
eb63657e 466 return pte_flags(pte) & _PAGE_HIDDEN;
dfec072e
VN
467}
468
649e8ef6
JF
469static inline int pmd_present(pmd_t pmd)
470{
027ef6c8
AA
471 /*
472 * Checking for _PAGE_PSE is needed too because
473 * split_huge_page will temporarily clear the present bit (but
474 * the _PAGE_PSE flag will remain set at all times while the
475 * _PAGE_PRESENT bit is clear).
476 */
21d9ee3e 477 return pmd_flags(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
649e8ef6
JF
478}
479
e7bb4b6d
MG
480#ifdef CONFIG_NUMA_BALANCING
481/*
482 * These work without NUMA balancing but the kernel does not care. See the
483 * comment in include/asm-generic/pgtable.h
484 */
485static inline int pte_protnone(pte_t pte)
486{
e3a1f6ca
DV
487 return (pte_flags(pte) & (_PAGE_PROTNONE | _PAGE_PRESENT))
488 == _PAGE_PROTNONE;
e7bb4b6d
MG
489}
490
491static inline int pmd_protnone(pmd_t pmd)
492{
e3a1f6ca
DV
493 return (pmd_flags(pmd) & (_PAGE_PROTNONE | _PAGE_PRESENT))
494 == _PAGE_PROTNONE;
e7bb4b6d
MG
495}
496#endif /* CONFIG_NUMA_BALANCING */
497
4fea801a
JF
498static inline int pmd_none(pmd_t pmd)
499{
500 /* Only check low word on 32-bit platforms, since it might be
501 out of sync with upper half. */
26c8e317 502 return (unsigned long)native_pmd_val(pmd) == 0;
4fea801a
JF
503}
504
3ffb3564
JF
505static inline unsigned long pmd_page_vaddr(pmd_t pmd)
506{
f70abb0f 507 return (unsigned long)__va(pmd_val(pmd) & pmd_pfn_mask(pmd));
3ffb3564
JF
508}
509
e5f7f202
IM
510/*
511 * Currently stuck as a macro due to indirect forward reference to
512 * linux/mmzone.h's __section_mem_map_addr() definition:
513 */
f70abb0f
TK
514#define pmd_page(pmd) \
515 pfn_to_page((pmd_val(pmd) & pmd_pfn_mask(pmd)) >> PAGE_SHIFT)
20063ca4 516
e24d7eee
JF
517/*
518 * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
519 *
520 * this macro returns the index of the entry in the pmd page which would
521 * control the given virtual address
522 */
ce0c0f9e 523static inline unsigned long pmd_index(unsigned long address)
e24d7eee
JF
524{
525 return (address >> PMD_SHIFT) & (PTRS_PER_PMD - 1);
526}
527
97e2817d
JF
528/*
529 * Conversion functions: convert a page and protection to a page entry,
530 * and a page entry and page directory to the page they refer to.
531 *
532 * (Currently stuck as a macro because of indirect forward reference
533 * to linux/mm.h:page_to_nid())
534 */
535#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
536
346309cf
JF
537/*
538 * the pte page can be thought of an array like this: pte_t[PTRS_PER_PTE]
539 *
540 * this function returns the index of the entry in the pte page which would
541 * control the given virtual address
542 */
ce0c0f9e 543static inline unsigned long pte_index(unsigned long address)
346309cf
JF
544{
545 return (address >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
546}
547
3fbc2444
JF
548static inline pte_t *pte_offset_kernel(pmd_t *pmd, unsigned long address)
549{
550 return (pte_t *)pmd_page_vaddr(*pmd) + pte_index(address);
551}
552
99510238
JF
553static inline int pmd_bad(pmd_t pmd)
554{
18a7a199 555 return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
99510238
JF
556}
557
cc290ca3
JF
558static inline unsigned long pages_to_mb(unsigned long npg)
559{
560 return npg >> (20 - PAGE_SHIFT);
561}
562
98233368 563#if CONFIG_PGTABLE_LEVELS > 2
deb79cfb
JF
564static inline int pud_none(pud_t pud)
565{
26c8e317 566 return native_pud_val(pud) == 0;
deb79cfb
JF
567}
568
5ba7c913
JF
569static inline int pud_present(pud_t pud)
570{
18a7a199 571 return pud_flags(pud) & _PAGE_PRESENT;
5ba7c913 572}
6fff47e3
JF
573
574static inline unsigned long pud_page_vaddr(pud_t pud)
575{
f70abb0f 576 return (unsigned long)__va(pud_val(pud) & pud_pfn_mask(pud));
6fff47e3 577}
f476961c 578
e5f7f202
IM
579/*
580 * Currently stuck as a macro due to indirect forward reference to
581 * linux/mmzone.h's __section_mem_map_addr() definition:
582 */
f70abb0f
TK
583#define pud_page(pud) \
584 pfn_to_page((pud_val(pud) & pud_pfn_mask(pud)) >> PAGE_SHIFT)
01ade20d
JF
585
586/* Find an entry in the second-level page table.. */
587static inline pmd_t *pmd_offset(pud_t *pud, unsigned long address)
588{
589 return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(address);
590}
3180fba0 591
3f6cbef1
JF
592static inline int pud_large(pud_t pud)
593{
e2f5bda9 594 return (pud_val(pud) & (_PAGE_PSE | _PAGE_PRESENT)) ==
3f6cbef1
JF
595 (_PAGE_PSE | _PAGE_PRESENT);
596}
a61bb29a
JF
597
598static inline int pud_bad(pud_t pud)
599{
18a7a199 600 return (pud_flags(pud) & ~(_KERNPG_TABLE | _PAGE_USER)) != 0;
a61bb29a 601}
e2f5bda9
JF
602#else
603static inline int pud_large(pud_t pud)
604{
605 return 0;
606}
98233368 607#endif /* CONFIG_PGTABLE_LEVELS > 2 */
5ba7c913 608
98233368 609#if CONFIG_PGTABLE_LEVELS > 3
9f38d7e8
JF
610static inline int pgd_present(pgd_t pgd)
611{
18a7a199 612 return pgd_flags(pgd) & _PAGE_PRESENT;
9f38d7e8 613}
c5f040b1
JF
614
615static inline unsigned long pgd_page_vaddr(pgd_t pgd)
616{
617 return (unsigned long)__va((unsigned long)pgd_val(pgd) & PTE_PFN_MASK);
618}
777cba16 619
e5f7f202
IM
620/*
621 * Currently stuck as a macro due to indirect forward reference to
622 * linux/mmzone.h's __section_mem_map_addr() definition:
623 */
624#define pgd_page(pgd) pfn_to_page(pgd_val(pgd) >> PAGE_SHIFT)
7cfb8102
JF
625
626/* to find an entry in a page-table-directory. */
ce0c0f9e 627static inline unsigned long pud_index(unsigned long address)
7cfb8102
JF
628{
629 return (address >> PUD_SHIFT) & (PTRS_PER_PUD - 1);
630}
3d081b18
JF
631
632static inline pud_t *pud_offset(pgd_t *pgd, unsigned long address)
633{
634 return (pud_t *)pgd_page_vaddr(*pgd) + pud_index(address);
635}
30f10316
JF
636
637static inline int pgd_bad(pgd_t pgd)
638{
18a7a199 639 return (pgd_flags(pgd) & ~_PAGE_USER) != _KERNPG_TABLE;
30f10316 640}
7325cc2e
JF
641
642static inline int pgd_none(pgd_t pgd)
643{
26c8e317 644 return !native_pgd_val(pgd);
7325cc2e 645}
98233368 646#endif /* CONFIG_PGTABLE_LEVELS > 3 */
9f38d7e8 647
4614139c
JF
648#endif /* __ASSEMBLY__ */
649
fb15a9b3
JF
650/*
651 * the pgd page can be thought of an array like this: pgd_t[PTRS_PER_PGD]
652 *
653 * this macro returns the index of the entry in the pgd page which would
654 * control the given virtual address
655 */
656#define pgd_index(address) (((address) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
657
658/*
659 * pgd_offset() returns a (pgd_t *)
660 * pgd_index() is used get the offset into the pgd page's array of pgd_t's;
661 */
662#define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address)))
663/*
664 * a shortcut which implies the use of the kernel's pgd, instead
665 * of a process's
666 */
667#define pgd_offset_k(address) pgd_offset(&init_mm, (address))
668
669
68db065c
JF
670#define KERNEL_PGD_BOUNDARY pgd_index(PAGE_OFFSET)
671#define KERNEL_PGD_PTRS (PTRS_PER_PGD - KERNEL_PGD_BOUNDARY)
672
195466dc
JF
673#ifndef __ASSEMBLY__
674
2c1b284e 675extern int direct_gbpages;
22ddfcaa 676void init_mem_mapping(void);
8d57470d 677void early_alloc_pgt_buf(void);
2c1b284e 678
4891645e
JF
679/* local pte updates need not use xchg for locking */
680static inline pte_t native_local_ptep_get_and_clear(pte_t *ptep)
681{
682 pte_t res = *ptep;
683
684 /* Pure native function needs no input for mm, addr */
685 native_pte_clear(NULL, 0, ptep);
686 return res;
687}
688
f2d6bfe9
JW
689static inline pmd_t native_local_pmdp_get_and_clear(pmd_t *pmdp)
690{
691 pmd_t res = *pmdp;
692
693 native_pmd_clear(pmdp);
694 return res;
695}
696
4891645e
JF
697static inline void native_set_pte_at(struct mm_struct *mm, unsigned long addr,
698 pte_t *ptep , pte_t pte)
699{
700 native_set_pte(ptep, pte);
701}
702
0a47de52
AA
703static inline void native_set_pmd_at(struct mm_struct *mm, unsigned long addr,
704 pmd_t *pmdp , pmd_t pmd)
705{
706 native_set_pmd(pmdp, pmd);
707}
708
195466dc
JF
709#ifndef CONFIG_PARAVIRT
710/*
711 * Rules for using pte_update - it must be called after any PTE update which
712 * has not been done using the set_pte / clear_pte interfaces. It is used by
713 * shadow mode hypervisors to resynchronize the shadow page tables. Kernel PTE
714 * updates should either be sets, clears, or set_pte_atomic for P->P
715 * transitions, which means this hook should only be called for user PTEs.
716 * This hook implies a P->P protection or access change has taken place, which
717 * requires a subsequent TLB flush. The notification can optionally be delayed
718 * until the TLB flush event by using the pte_update_defer form of the
719 * interface, but care must be taken to assure that the flush happens while
720 * still holding the same page table lock so that the shadow and primary pages
721 * do not become out of sync on SMP.
722 */
723#define pte_update(mm, addr, ptep) do { } while (0)
724#define pte_update_defer(mm, addr, ptep) do { } while (0)
725#endif
726
195466dc
JF
727/*
728 * We only update the dirty/accessed state if we set
729 * the dirty bit by hand in the kernel, since the hardware
730 * will do the accessed bit for us, and we don't want to
731 * race with other CPU's that might be updating the dirty
732 * bit at the same time.
733 */
bea41808
JF
734struct vm_area_struct;
735
195466dc 736#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
ee5aa8d3
JF
737extern int ptep_set_access_flags(struct vm_area_struct *vma,
738 unsigned long address, pte_t *ptep,
739 pte_t entry, int dirty);
195466dc
JF
740
741#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
f9fbf1a3
JF
742extern int ptep_test_and_clear_young(struct vm_area_struct *vma,
743 unsigned long addr, pte_t *ptep);
195466dc
JF
744
745#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
c20311e1
JF
746extern int ptep_clear_flush_young(struct vm_area_struct *vma,
747 unsigned long address, pte_t *ptep);
195466dc
JF
748
749#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
3cbaeafe
JP
750static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
751 pte_t *ptep)
195466dc
JF
752{
753 pte_t pte = native_ptep_get_and_clear(ptep);
754 pte_update(mm, addr, ptep);
755 return pte;
756}
757
758#define __HAVE_ARCH_PTEP_GET_AND_CLEAR_FULL
3cbaeafe
JP
759static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
760 unsigned long addr, pte_t *ptep,
761 int full)
195466dc
JF
762{
763 pte_t pte;
764 if (full) {
765 /*
766 * Full address destruction in progress; paravirt does not
767 * care about updates and native needs no locking
768 */
769 pte = native_local_ptep_get_and_clear(ptep);
770 } else {
771 pte = ptep_get_and_clear(mm, addr, ptep);
772 }
773 return pte;
774}
775
776#define __HAVE_ARCH_PTEP_SET_WRPROTECT
3cbaeafe
JP
777static inline void ptep_set_wrprotect(struct mm_struct *mm,
778 unsigned long addr, pte_t *ptep)
195466dc 779{
d8d89827 780 clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
195466dc
JF
781 pte_update(mm, addr, ptep);
782}
783
2ac13462 784#define flush_tlb_fix_spurious_fault(vma, address) do { } while (0)
61c77326 785
f2d6bfe9
JW
786#define mk_pmd(page, pgprot) pfn_pmd(page_to_pfn(page), (pgprot))
787
788#define __HAVE_ARCH_PMDP_SET_ACCESS_FLAGS
789extern int pmdp_set_access_flags(struct vm_area_struct *vma,
790 unsigned long address, pmd_t *pmdp,
791 pmd_t entry, int dirty);
792
793#define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
794extern int pmdp_test_and_clear_young(struct vm_area_struct *vma,
795 unsigned long addr, pmd_t *pmdp);
796
797#define __HAVE_ARCH_PMDP_CLEAR_YOUNG_FLUSH
798extern int pmdp_clear_flush_young(struct vm_area_struct *vma,
799 unsigned long address, pmd_t *pmdp);
800
801
802#define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
803extern void pmdp_splitting_flush(struct vm_area_struct *vma,
804 unsigned long addr, pmd_t *pmdp);
805
806#define __HAVE_ARCH_PMD_WRITE
807static inline int pmd_write(pmd_t pmd)
808{
809 return pmd_flags(pmd) & _PAGE_RW;
810}
811
8809aa2d
AK
812#define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
813static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long addr,
f2d6bfe9
JW
814 pmd_t *pmdp)
815{
816 pmd_t pmd = native_pmdp_get_and_clear(pmdp);
817 pmd_update(mm, addr, pmdp);
818 return pmd;
819}
820
821#define __HAVE_ARCH_PMDP_SET_WRPROTECT
822static inline void pmdp_set_wrprotect(struct mm_struct *mm,
823 unsigned long addr, pmd_t *pmdp)
824{
825 clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp);
826 pmd_update(mm, addr, pmdp);
827}
828
85958b46
JF
829/*
830 * clone_pgd_range(pgd_t *dst, pgd_t *src, int count);
831 *
832 * dst - pointer to pgd range anwhere on a pgd page
833 * src - ""
834 * count - the number of pgds to copy.
835 *
836 * dst and src can be on the same page, but the range must not overlap,
837 * and must not cross a page boundary.
838 */
839static inline void clone_pgd_range(pgd_t *dst, pgd_t *src, int count)
840{
841 memcpy(dst, src, count * sizeof(pgd_t));
842}
843
4cbeb51b
DH
844#define PTE_SHIFT ilog2(PTRS_PER_PTE)
845static inline int page_level_shift(enum pg_level level)
846{
847 return (PAGE_SHIFT - PTE_SHIFT) + level * PTE_SHIFT;
848}
849static inline unsigned long page_level_size(enum pg_level level)
850{
851 return 1UL << page_level_shift(level);
852}
853static inline unsigned long page_level_mask(enum pg_level level)
854{
855 return ~(page_level_size(level) - 1);
856}
85958b46 857
602e0186
KS
858/*
859 * The x86 doesn't have any external MMU info: the kernel page
860 * tables contain all the necessary information.
861 */
862static inline void update_mmu_cache(struct vm_area_struct *vma,
863 unsigned long addr, pte_t *ptep)
864{
865}
866static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
867 unsigned long addr, pmd_t *pmd)
868{
869}
85958b46 870
2bf01f9f 871#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY
fa0f281c
CG
872static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
873{
fa0f281c
CG
874 return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
875}
876
877static inline int pte_swp_soft_dirty(pte_t pte)
878{
fa0f281c
CG
879 return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
880}
881
882static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
883{
fa0f281c
CG
884 return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
885}
2bf01f9f 886#endif
fa0f281c 887
195466dc
JF
888#include <asm-generic/pgtable.h>
889#endif /* __ASSEMBLY__ */
890
1965aae3 891#endif /* _ASM_X86_PGTABLE_H */