1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  *
5  * Derived from MIPS:
6  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2003 Ralf Baechle
7  * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
8  */
9 #ifndef _ASM_PGTABLE_H
10 #define _ASM_PGTABLE_H
11 
12 #include <linux/compiler.h>
13 #include <asm/addrspace.h>
14 #include <asm/page.h>
15 #include <asm/pgtable-bits.h>
16 
17 #if CONFIG_PGTABLE_LEVELS == 2
18 #include <asm-generic/pgtable-nopmd.h>
19 #elif CONFIG_PGTABLE_LEVELS == 3
20 #include <asm-generic/pgtable-nopud.h>
21 #else
22 #include <asm-generic/pgtable-nop4d.h>
23 #endif
24 
25 #if CONFIG_PGTABLE_LEVELS == 2
26 #define PGDIR_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
27 #elif CONFIG_PGTABLE_LEVELS == 3
28 #define PMD_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
29 #define PMD_SIZE	(1UL << PMD_SHIFT)
30 #define PMD_MASK	(~(PMD_SIZE-1))
31 #define PGDIR_SHIFT	(PMD_SHIFT + (PAGE_SHIFT - 3))
32 #elif CONFIG_PGTABLE_LEVELS == 4
33 #define PMD_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
34 #define PMD_SIZE	(1UL << PMD_SHIFT)
35 #define PMD_MASK	(~(PMD_SIZE-1))
36 #define PUD_SHIFT	(PMD_SHIFT + (PAGE_SHIFT - 3))
37 #define PUD_SIZE	(1UL << PUD_SHIFT)
38 #define PUD_MASK	(~(PUD_SIZE-1))
39 #define PGDIR_SHIFT	(PUD_SHIFT + (PAGE_SHIFT - 3))
40 #endif
41 
42 #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
43 #define PGDIR_MASK	(~(PGDIR_SIZE-1))
44 
45 #define VA_BITS		(PGDIR_SHIFT + (PAGE_SHIFT - 3))
46 
47 #define PTRS_PER_PGD	(PAGE_SIZE >> 3)
48 #if CONFIG_PGTABLE_LEVELS > 3
49 #define PTRS_PER_PUD	(PAGE_SIZE >> 3)
50 #endif
51 #if CONFIG_PGTABLE_LEVELS > 2
52 #define PTRS_PER_PMD	(PAGE_SIZE >> 3)
53 #endif
54 #define PTRS_PER_PTE	(PAGE_SIZE >> 3)
55 
56 #define USER_PTRS_PER_PGD       ((TASK_SIZE64 / PGDIR_SIZE)?(TASK_SIZE64 / PGDIR_SIZE):1)
57 
58 #ifndef __ASSEMBLY__
59 
60 #include <linux/mm_types.h>
61 #include <linux/mmzone.h>
62 #include <asm/fixmap.h>
63 #include <asm/sparsemem.h>
64 
65 struct mm_struct;
66 struct vm_area_struct;
67 
68 /*
69  * ZERO_PAGE is a global shared page that is always zero; used
70  * for zero-mapped memory areas etc..
71  */
72 
73 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
74 
75 #define ZERO_PAGE(vaddr)	virt_to_page(empty_zero_page)
76 
77 /*
78  * TLB refill handlers may also map the vmalloc area into xkvrange.
79  * Avoid the first couple of pages so NULL pointer dereferences will
80  * still reliably trap.
81  */
82 #define MODULES_VADDR	(vm_map_base + PCI_IOSIZE + (2 * PAGE_SIZE))
83 #define MODULES_END	(MODULES_VADDR + SZ_256M)
84 
85 #ifdef CONFIG_KFENCE
86 #define KFENCE_AREA_SIZE	(((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 + 2) * PAGE_SIZE)
87 #else
88 #define KFENCE_AREA_SIZE	0
89 #endif
90 
91 #define VMALLOC_START	MODULES_END
92 
93 #ifndef CONFIG_KASAN
94 #define VMALLOC_END	\
95 	(vm_map_base +	\
96 	 min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, (1UL << cpu_vabits)) - PMD_SIZE - VMEMMAP_SIZE - KFENCE_AREA_SIZE)
97 #else
98 #define VMALLOC_END	\
99 	(vm_map_base +	\
100 	 min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, (1UL << cpu_vabits) / 2) - PMD_SIZE - VMEMMAP_SIZE - KFENCE_AREA_SIZE)
101 #endif
102 
103 #define vmemmap		((struct page *)((VMALLOC_END + PMD_SIZE) & PMD_MASK))
104 #define VMEMMAP_END	((unsigned long)vmemmap + VMEMMAP_SIZE - 1)
105 
106 #define KFENCE_AREA_START	(VMEMMAP_END + 1)
107 #define KFENCE_AREA_END		(KFENCE_AREA_START + KFENCE_AREA_SIZE - 1)
108 
109 #define ptep_get(ptep) READ_ONCE(*(ptep))
110 #define pmdp_get(pmdp) READ_ONCE(*(pmdp))
111 
112 #define pte_ERROR(e) \
113 	pr_err("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e))
114 #ifndef __PAGETABLE_PMD_FOLDED
115 #define pmd_ERROR(e) \
116 	pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
117 #endif
118 #ifndef __PAGETABLE_PUD_FOLDED
119 #define pud_ERROR(e) \
120 	pr_err("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
121 #endif
122 #define pgd_ERROR(e) \
123 	pr_err("%s:%d: bad pgd %016lx.\n", __FILE__, __LINE__, pgd_val(e))
124 
125 extern pte_t invalid_pte_table[PTRS_PER_PTE];
126 
127 #ifndef __PAGETABLE_PUD_FOLDED
128 
129 typedef struct { unsigned long pud; } pud_t;
130 #define pud_val(x)	((x).pud)
131 #define __pud(x)	((pud_t) { (x) })
132 
133 extern pud_t invalid_pud_table[PTRS_PER_PUD];
134 
135 /*
136  * Empty pgd/p4d entries point to the invalid_pud_table.
137  */
p4d_none(p4d_t p4d)138 static inline int p4d_none(p4d_t p4d)
139 {
140 	return p4d_val(p4d) == (unsigned long)invalid_pud_table;
141 }
142 
p4d_bad(p4d_t p4d)143 static inline int p4d_bad(p4d_t p4d)
144 {
145 	return p4d_val(p4d) & ~PAGE_MASK;
146 }
147 
p4d_present(p4d_t p4d)148 static inline int p4d_present(p4d_t p4d)
149 {
150 	return p4d_val(p4d) != (unsigned long)invalid_pud_table;
151 }
152 
p4d_pgtable(p4d_t p4d)153 static inline pud_t *p4d_pgtable(p4d_t p4d)
154 {
155 	return (pud_t *)p4d_val(p4d);
156 }
157 
set_p4d(p4d_t * p4d,p4d_t p4dval)158 static inline void set_p4d(p4d_t *p4d, p4d_t p4dval)
159 {
160 	WRITE_ONCE(*p4d, p4dval);
161 }
162 
p4d_clear(p4d_t * p4dp)163 static inline void p4d_clear(p4d_t *p4dp)
164 {
165 	set_p4d(p4dp, __p4d((unsigned long)invalid_pud_table));
166 }
167 
168 #define p4d_phys(p4d)		PHYSADDR(p4d_val(p4d))
169 #define p4d_page(p4d)		(pfn_to_page(p4d_phys(p4d) >> PAGE_SHIFT))
170 
171 #endif
172 
173 #ifndef __PAGETABLE_PMD_FOLDED
174 
175 typedef struct { unsigned long pmd; } pmd_t;
176 #define pmd_val(x)	((x).pmd)
177 #define __pmd(x)	((pmd_t) { (x) })
178 
179 extern pmd_t invalid_pmd_table[PTRS_PER_PMD];
180 
181 /*
182  * Empty pud entries point to the invalid_pmd_table.
183  */
pud_none(pud_t pud)184 static inline int pud_none(pud_t pud)
185 {
186 	return pud_val(pud) == (unsigned long)invalid_pmd_table;
187 }
188 
pud_bad(pud_t pud)189 static inline int pud_bad(pud_t pud)
190 {
191 	return pud_val(pud) & ~PAGE_MASK;
192 }
193 
pud_present(pud_t pud)194 static inline int pud_present(pud_t pud)
195 {
196 	return pud_val(pud) != (unsigned long)invalid_pmd_table;
197 }
198 
pud_pgtable(pud_t pud)199 static inline pmd_t *pud_pgtable(pud_t pud)
200 {
201 	return (pmd_t *)pud_val(pud);
202 }
203 
set_pud(pud_t * pud,pud_t pudval)204 static inline void set_pud(pud_t *pud, pud_t pudval)
205 {
206 	WRITE_ONCE(*pud, pudval);
207 }
208 
pud_clear(pud_t * pudp)209 static inline void pud_clear(pud_t *pudp)
210 {
211 	set_pud(pudp, __pud((unsigned long)invalid_pmd_table));
212 }
213 
214 #define pud_phys(pud)		PHYSADDR(pud_val(pud))
215 #define pud_page(pud)		(pfn_to_page(pud_phys(pud) >> PAGE_SHIFT))
216 
217 #endif
218 
219 /*
220  * Empty pmd entries point to the invalid_pte_table.
221  */
pmd_none(pmd_t pmd)222 static inline int pmd_none(pmd_t pmd)
223 {
224 	return pmd_val(pmd) == (unsigned long)invalid_pte_table;
225 }
226 
pmd_bad(pmd_t pmd)227 static inline int pmd_bad(pmd_t pmd)
228 {
229 	return (pmd_val(pmd) & ~PAGE_MASK);
230 }
231 
pmd_present(pmd_t pmd)232 static inline int pmd_present(pmd_t pmd)
233 {
234 	if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
235 		return !!(pmd_val(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PRESENT_INVALID));
236 
237 	return pmd_val(pmd) != (unsigned long)invalid_pte_table;
238 }
239 
set_pmd(pmd_t * pmd,pmd_t pmdval)240 static inline void set_pmd(pmd_t *pmd, pmd_t pmdval)
241 {
242 	WRITE_ONCE(*pmd, pmdval);
243 }
244 
pmd_clear(pmd_t * pmdp)245 static inline void pmd_clear(pmd_t *pmdp)
246 {
247 	set_pmd(pmdp, __pmd((unsigned long)invalid_pte_table));
248 }
249 
250 #define pmd_phys(pmd)		PHYSADDR(pmd_val(pmd))
251 
252 #ifndef CONFIG_TRANSPARENT_HUGEPAGE
253 #define pmd_page(pmd)		(pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
254 #endif /* CONFIG_TRANSPARENT_HUGEPAGE  */
255 
256 #define pmd_page_vaddr(pmd)	pmd_val(pmd)
257 
258 extern pmd_t mk_pmd(struct page *page, pgprot_t prot);
259 extern void set_pmd_at(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, pmd_t pmd);
260 
261 #define pte_page(x)		pfn_to_page(pte_pfn(x))
262 #define pte_pfn(x)		((unsigned long)(((x).pte & _PFN_MASK) >> PFN_PTE_SHIFT))
263 #define pfn_pte(pfn, prot)	__pte(((pfn) << PFN_PTE_SHIFT) | pgprot_val(prot))
264 #define pfn_pmd(pfn, prot)	__pmd(((pfn) << PFN_PTE_SHIFT) | pgprot_val(prot))
265 
266 /*
267  * Initialize a new pgd / pud / pmd table with invalid pointers.
268  */
269 extern void pgd_init(void *addr);
270 extern void pud_init(void *addr);
271 extern void pmd_init(void *addr);
272 extern void kernel_pte_init(void *addr);
273 
274 /*
275  * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
276  * are !pte_none() && !pte_present().
277  *
278  * Format of swap PTEs:
279  *
280  *   6 6 6 6 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3
281  *   3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
282  *   <--------------------------- offset ---------------------------
283  *
284  *   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
285  *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
286  *   --------------> E <--- type ---> <---------- zeroes ---------->
287  *
288  *   E is the exclusive marker that is not stored in swap entries.
289  *   The zero'ed bits include _PAGE_PRESENT and _PAGE_PROTNONE.
290  */
mk_swap_pte(unsigned long type,unsigned long offset)291 static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
292 { pte_t pte; pte_val(pte) = ((type & 0x7f) << 16) | (offset << 24); return pte; }
293 
294 #define __swp_type(x)		(((x).val >> 16) & 0x7f)
295 #define __swp_offset(x)		((x).val >> 24)
296 #define __swp_entry(type, offset) ((swp_entry_t) { pte_val(mk_swap_pte((type), (offset))) })
297 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
298 #define __swp_entry_to_pte(x)	((pte_t) { (x).val })
299 #define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) })
300 #define __swp_entry_to_pmd(x)	((pmd_t) { (x).val | _PAGE_HUGE })
301 
pte_swp_exclusive(pte_t pte)302 static inline int pte_swp_exclusive(pte_t pte)
303 {
304 	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
305 }
306 
pte_swp_mkexclusive(pte_t pte)307 static inline pte_t pte_swp_mkexclusive(pte_t pte)
308 {
309 	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
310 	return pte;
311 }
312 
pte_swp_clear_exclusive(pte_t pte)313 static inline pte_t pte_swp_clear_exclusive(pte_t pte)
314 {
315 	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
316 	return pte;
317 }
318 
319 extern void paging_init(void);
320 
321 #define pte_none(pte)		(!(pte_val(pte) & ~_PAGE_GLOBAL))
322 #define pte_present(pte)	(pte_val(pte) & (_PAGE_PRESENT | _PAGE_PROTNONE))
323 #define pte_no_exec(pte)	(pte_val(pte) & _PAGE_NO_EXEC)
324 
set_pte(pte_t * ptep,pte_t pteval)325 static inline void set_pte(pte_t *ptep, pte_t pteval)
326 {
327 	WRITE_ONCE(*ptep, pteval);
328 
329 #ifdef CONFIG_SMP
330 	if (pte_val(pteval) & _PAGE_GLOBAL)
331 		DBAR(0b11000); /* o_wrw = 0b11000 */
332 #endif
333 }
334 
pte_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)335 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
336 {
337 	pte_t pte = ptep_get(ptep);
338 	pte_val(pte) &= _PAGE_GLOBAL;
339 	set_pte(ptep, pte);
340 }
341 
342 #define PGD_T_LOG2	(__builtin_ffs(sizeof(pgd_t)) - 1)
343 #define PMD_T_LOG2	(__builtin_ffs(sizeof(pmd_t)) - 1)
344 #define PTE_T_LOG2	(__builtin_ffs(sizeof(pte_t)) - 1)
345 
346 extern pgd_t swapper_pg_dir[];
347 extern pgd_t invalid_pg_dir[];
348 
349 /*
350  * The following only work if pte_present() is true.
351  * Undefined behaviour if not..
352  */
pte_write(pte_t pte)353 static inline int pte_write(pte_t pte)	{ return pte_val(pte) & _PAGE_WRITE; }
pte_young(pte_t pte)354 static inline int pte_young(pte_t pte)	{ return pte_val(pte) & _PAGE_ACCESSED; }
pte_dirty(pte_t pte)355 static inline int pte_dirty(pte_t pte)	{ return pte_val(pte) & (_PAGE_DIRTY | _PAGE_MODIFIED); }
356 
pte_mkold(pte_t pte)357 static inline pte_t pte_mkold(pte_t pte)
358 {
359 	pte_val(pte) &= ~_PAGE_ACCESSED;
360 	return pte;
361 }
362 
pte_mkyoung(pte_t pte)363 static inline pte_t pte_mkyoung(pte_t pte)
364 {
365 	pte_val(pte) |= _PAGE_ACCESSED;
366 	return pte;
367 }
368 
pte_mkclean(pte_t pte)369 static inline pte_t pte_mkclean(pte_t pte)
370 {
371 	pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_MODIFIED);
372 	return pte;
373 }
374 
pte_mkdirty(pte_t pte)375 static inline pte_t pte_mkdirty(pte_t pte)
376 {
377 	pte_val(pte) |= _PAGE_MODIFIED;
378 	if (pte_val(pte) & _PAGE_WRITE)
379 		pte_val(pte) |= _PAGE_DIRTY;
380 	return pte;
381 }
382 
pte_mkwrite_novma(pte_t pte)383 static inline pte_t pte_mkwrite_novma(pte_t pte)
384 {
385 	pte_val(pte) |= _PAGE_WRITE;
386 	if (pte_val(pte) & _PAGE_MODIFIED)
387 		pte_val(pte) |= _PAGE_DIRTY;
388 	return pte;
389 }
390 
pte_wrprotect(pte_t pte)391 static inline pte_t pte_wrprotect(pte_t pte)
392 {
393 	pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
394 	return pte;
395 }
396 
pte_huge(pte_t pte)397 static inline int pte_huge(pte_t pte)	{ return pte_val(pte) & _PAGE_HUGE; }
398 
pte_mkhuge(pte_t pte)399 static inline pte_t pte_mkhuge(pte_t pte)
400 {
401 	pte_val(pte) |= _PAGE_HUGE;
402 	return pte;
403 }
404 
405 #if defined(CONFIG_ARCH_HAS_PTE_SPECIAL)
pte_special(pte_t pte)406 static inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
pte_mkspecial(pte_t pte)407 static inline pte_t pte_mkspecial(pte_t pte)	{ pte_val(pte) |= _PAGE_SPECIAL; return pte; }
408 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
409 
pte_devmap(pte_t pte)410 static inline int pte_devmap(pte_t pte)		{ return !!(pte_val(pte) & _PAGE_DEVMAP); }
pte_mkdevmap(pte_t pte)411 static inline pte_t pte_mkdevmap(pte_t pte)	{ pte_val(pte) |= _PAGE_DEVMAP; return pte; }
412 
413 #define pte_accessible pte_accessible
pte_accessible(struct mm_struct * mm,pte_t a)414 static inline unsigned long pte_accessible(struct mm_struct *mm, pte_t a)
415 {
416 	if (pte_val(a) & _PAGE_PRESENT)
417 		return true;
418 
419 	if ((pte_val(a) & _PAGE_PROTNONE) &&
420 			atomic_read(&mm->tlb_flush_pending))
421 		return true;
422 
423 	return false;
424 }
425 
426 /*
427  * Conversion functions: convert a page and protection to a page entry,
428  * and a page entry and page directory to the page they refer to.
429  */
430 #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
431 
pte_modify(pte_t pte,pgprot_t newprot)432 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
433 {
434 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) |
435 		     (pgprot_val(newprot) & ~_PAGE_CHG_MASK));
436 }
437 
438 extern void __update_tlb(struct vm_area_struct *vma,
439 			unsigned long address, pte_t *ptep);
440 
update_mmu_cache_range(struct vm_fault * vmf,struct vm_area_struct * vma,unsigned long address,pte_t * ptep,unsigned int nr)441 static inline void update_mmu_cache_range(struct vm_fault *vmf,
442 		struct vm_area_struct *vma, unsigned long address,
443 		pte_t *ptep, unsigned int nr)
444 {
445 	for (;;) {
446 		__update_tlb(vma, address, ptep);
447 		if (--nr == 0)
448 			break;
449 		address += PAGE_SIZE;
450 		ptep++;
451 	}
452 }
453 #define update_mmu_cache(vma, addr, ptep) \
454 	update_mmu_cache_range(NULL, vma, addr, ptep, 1)
455 
456 #define update_mmu_tlb_range(vma, addr, ptep, nr) \
457 	update_mmu_cache_range(NULL, vma, addr, ptep, nr)
458 
update_mmu_cache_pmd(struct vm_area_struct * vma,unsigned long address,pmd_t * pmdp)459 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
460 			unsigned long address, pmd_t *pmdp)
461 {
462 	__update_tlb(vma, address, (pte_t *)pmdp);
463 }
464 
pmd_pfn(pmd_t pmd)465 static inline unsigned long pmd_pfn(pmd_t pmd)
466 {
467 	return (pmd_val(pmd) & _PFN_MASK) >> PFN_PTE_SHIFT;
468 }
469 
470 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
471 
472 /* We don't have hardware dirty/accessed bits, generic_pmdp_establish is fine.*/
473 #define pmdp_establish generic_pmdp_establish
474 
pmd_trans_huge(pmd_t pmd)475 static inline int pmd_trans_huge(pmd_t pmd)
476 {
477 	return !!(pmd_val(pmd) & _PAGE_HUGE) && pmd_present(pmd);
478 }
479 
pmd_mkhuge(pmd_t pmd)480 static inline pmd_t pmd_mkhuge(pmd_t pmd)
481 {
482 	pmd_val(pmd) = (pmd_val(pmd) & ~(_PAGE_GLOBAL)) |
483 		((pmd_val(pmd) & _PAGE_GLOBAL) << (_PAGE_HGLOBAL_SHIFT - _PAGE_GLOBAL_SHIFT));
484 	pmd_val(pmd) |= _PAGE_HUGE;
485 
486 	return pmd;
487 }
488 
489 #define pmd_write pmd_write
pmd_write(pmd_t pmd)490 static inline int pmd_write(pmd_t pmd)
491 {
492 	return !!(pmd_val(pmd) & _PAGE_WRITE);
493 }
494 
pmd_mkwrite_novma(pmd_t pmd)495 static inline pmd_t pmd_mkwrite_novma(pmd_t pmd)
496 {
497 	pmd_val(pmd) |= _PAGE_WRITE;
498 	if (pmd_val(pmd) & _PAGE_MODIFIED)
499 		pmd_val(pmd) |= _PAGE_DIRTY;
500 	return pmd;
501 }
502 
pmd_wrprotect(pmd_t pmd)503 static inline pmd_t pmd_wrprotect(pmd_t pmd)
504 {
505 	pmd_val(pmd) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
506 	return pmd;
507 }
508 
509 #define pmd_dirty pmd_dirty
pmd_dirty(pmd_t pmd)510 static inline int pmd_dirty(pmd_t pmd)
511 {
512 	return !!(pmd_val(pmd) & (_PAGE_DIRTY | _PAGE_MODIFIED));
513 }
514 
pmd_mkclean(pmd_t pmd)515 static inline pmd_t pmd_mkclean(pmd_t pmd)
516 {
517 	pmd_val(pmd) &= ~(_PAGE_DIRTY | _PAGE_MODIFIED);
518 	return pmd;
519 }
520 
pmd_mkdirty(pmd_t pmd)521 static inline pmd_t pmd_mkdirty(pmd_t pmd)
522 {
523 	pmd_val(pmd) |= _PAGE_MODIFIED;
524 	if (pmd_val(pmd) & _PAGE_WRITE)
525 		pmd_val(pmd) |= _PAGE_DIRTY;
526 	return pmd;
527 }
528 
529 #define pmd_young pmd_young
pmd_young(pmd_t pmd)530 static inline int pmd_young(pmd_t pmd)
531 {
532 	return !!(pmd_val(pmd) & _PAGE_ACCESSED);
533 }
534 
pmd_mkold(pmd_t pmd)535 static inline pmd_t pmd_mkold(pmd_t pmd)
536 {
537 	pmd_val(pmd) &= ~_PAGE_ACCESSED;
538 	return pmd;
539 }
540 
pmd_mkyoung(pmd_t pmd)541 static inline pmd_t pmd_mkyoung(pmd_t pmd)
542 {
543 	pmd_val(pmd) |= _PAGE_ACCESSED;
544 	return pmd;
545 }
546 
pmd_devmap(pmd_t pmd)547 static inline int pmd_devmap(pmd_t pmd)
548 {
549 	return !!(pmd_val(pmd) & _PAGE_DEVMAP);
550 }
551 
pmd_mkdevmap(pmd_t pmd)552 static inline pmd_t pmd_mkdevmap(pmd_t pmd)
553 {
554 	pmd_val(pmd) |= _PAGE_DEVMAP;
555 	return pmd;
556 }
557 
pmd_page(pmd_t pmd)558 static inline struct page *pmd_page(pmd_t pmd)
559 {
560 	if (pmd_trans_huge(pmd))
561 		return pfn_to_page(pmd_pfn(pmd));
562 
563 	return pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT);
564 }
565 
pmd_modify(pmd_t pmd,pgprot_t newprot)566 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
567 {
568 	pmd_val(pmd) = (pmd_val(pmd) & _HPAGE_CHG_MASK) |
569 				(pgprot_val(newprot) & ~_HPAGE_CHG_MASK);
570 	return pmd;
571 }
572 
pmd_mkinvalid(pmd_t pmd)573 static inline pmd_t pmd_mkinvalid(pmd_t pmd)
574 {
575 	pmd_val(pmd) |= _PAGE_PRESENT_INVALID;
576 	pmd_val(pmd) &= ~(_PAGE_PRESENT | _PAGE_VALID | _PAGE_DIRTY | _PAGE_PROTNONE);
577 
578 	return pmd;
579 }
580 
581 /*
582  * The generic version pmdp_huge_get_and_clear uses a version of pmd_clear() with a
583  * different prototype.
584  */
585 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
pmdp_huge_get_and_clear(struct mm_struct * mm,unsigned long address,pmd_t * pmdp)586 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
587 					    unsigned long address, pmd_t *pmdp)
588 {
589 	pmd_t old = pmdp_get(pmdp);
590 
591 	pmd_clear(pmdp);
592 
593 	return old;
594 }
595 
596 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
597 
598 #ifdef CONFIG_NUMA_BALANCING
pte_protnone(pte_t pte)599 static inline long pte_protnone(pte_t pte)
600 {
601 	return (pte_val(pte) & _PAGE_PROTNONE);
602 }
603 
pmd_protnone(pmd_t pmd)604 static inline long pmd_protnone(pmd_t pmd)
605 {
606 	return (pmd_val(pmd) & _PAGE_PROTNONE);
607 }
608 #endif /* CONFIG_NUMA_BALANCING */
609 
610 #define pmd_leaf(pmd)		((pmd_val(pmd) & _PAGE_HUGE) != 0)
611 #define pud_leaf(pud)		((pud_val(pud) & _PAGE_HUGE) != 0)
612 
613 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
614 #define pud_devmap(pud)		(0)
615 #define pgd_devmap(pgd)		(0)
616 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
617 
618 /*
619  * We provide our own get_unmapped area to cope with the virtual aliasing
620  * constraints placed on us by the cache architecture.
621  */
622 #define HAVE_ARCH_UNMAPPED_AREA
623 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
624 
625 #endif /* !__ASSEMBLY__ */
626 
627 #endif /* _ASM_PGTABLE_H */
628