xref: /linux-6.15/arch/um/include/asm/pgtable.h (revision 41ab5fe7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4  * Copyright 2003 PathScale, Inc.
5  * Derived from include/asm-i386/pgtable.h
6  */
7 
8 #ifndef __UM_PGTABLE_H
9 #define __UM_PGTABLE_H
10 
11 #include <asm/fixmap.h>
12 
13 #define _PAGE_PRESENT	0x001
14 #define _PAGE_NEWPAGE	0x002
15 #define _PAGE_NEWPROT	0x004
16 #define _PAGE_RW	0x020
17 #define _PAGE_USER	0x040
18 #define _PAGE_ACCESSED	0x080
19 #define _PAGE_DIRTY	0x100
20 /* If _PAGE_PRESENT is clear, we use these: */
21 #define _PAGE_PROTNONE	0x010	/* if the user mapped it with PROT_NONE;
22 				   pte_present gives true */
23 
24 /* We borrow bit 10 to store the exclusive marker in swap PTEs. */
25 #define _PAGE_SWP_EXCLUSIVE	0x400
26 
27 #if CONFIG_PGTABLE_LEVELS == 4
28 #include <asm/pgtable-4level.h>
29 #elif CONFIG_PGTABLE_LEVELS == 2
30 #include <asm/pgtable-2level.h>
31 #else
32 #error "Unsupported number of page table levels"
33 #endif
34 
35 extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
36 
37 /* zero page used for uninitialized stuff */
38 extern unsigned long *empty_zero_page;
39 
40 /* Just any arbitrary offset to the start of the vmalloc VM area: the
41  * current 8MB value just means that there will be a 8MB "hole" after the
42  * physical memory until the kernel virtual memory starts.  That means that
43  * any out-of-bounds memory accesses will hopefully be caught.
44  * The vmalloc() routines leaves a hole of 4kB between each vmalloced
45  * area for the same reason. ;)
46  */
47 
48 extern unsigned long end_iomem;
49 
50 #define VMALLOC_OFFSET	(__va_space)
51 #define VMALLOC_START ((end_iomem + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1))
52 #define PKMAP_BASE ((FIXADDR_START - LAST_PKMAP * PAGE_SIZE) & PMD_MASK)
53 #define VMALLOC_END	(FIXADDR_START-2*PAGE_SIZE)
54 #define MODULES_VADDR	VMALLOC_START
55 #define MODULES_END	VMALLOC_END
56 #define MODULES_LEN	(MODULES_VADDR - MODULES_END)
57 
58 #define _PAGE_TABLE	(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED | _PAGE_DIRTY)
59 #define _KERNPG_TABLE	(_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
60 #define _PAGE_CHG_MASK	(PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
61 #define __PAGE_KERNEL_EXEC                                              \
62 	 (_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
63 #define PAGE_NONE	__pgprot(_PAGE_PROTNONE | _PAGE_ACCESSED)
64 #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | _PAGE_ACCESSED)
65 #define PAGE_COPY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
66 #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED)
67 #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED)
68 #define PAGE_KERNEL_EXEC	__pgprot(__PAGE_KERNEL_EXEC)
69 
70 /*
71  * The i386 can't do page protection for execute, and considers that the same
72  * are read.
73  * Also, write permissions imply read permissions. This is the closest we can
74  * get..
75  */
76 
77 /*
78  * ZERO_PAGE is a global shared page that is always zero: used
79  * for zero-mapped memory areas etc..
80  */
81 #define ZERO_PAGE(vaddr) virt_to_page(empty_zero_page)
82 
83 #define pte_clear(mm,addr,xp) pte_set_val(*(xp), (phys_t) 0, __pgprot(_PAGE_NEWPAGE))
84 
85 #define pmd_none(x)	(!((unsigned long)pmd_val(x) & ~_PAGE_NEWPAGE))
86 #define	pmd_bad(x)	((pmd_val(x) & (~PAGE_MASK & ~_PAGE_USER)) != _KERNPG_TABLE)
87 
88 #define pmd_present(x)	(pmd_val(x) & _PAGE_PRESENT)
89 #define pmd_clear(xp)	do { pmd_val(*(xp)) = _PAGE_NEWPAGE; } while (0)
90 
91 #define pmd_newpage(x)  (pmd_val(x) & _PAGE_NEWPAGE)
92 #define pmd_mkuptodate(x) (pmd_val(x) &= ~_PAGE_NEWPAGE)
93 
94 #define pud_newpage(x)  (pud_val(x) & _PAGE_NEWPAGE)
95 #define pud_mkuptodate(x) (pud_val(x) &= ~_PAGE_NEWPAGE)
96 
97 #define p4d_newpage(x)  (p4d_val(x) & _PAGE_NEWPAGE)
98 #define p4d_mkuptodate(x) (p4d_val(x) &= ~_PAGE_NEWPAGE)
99 
100 #define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT)
101 #define pmd_page(pmd) phys_to_page(pmd_val(pmd) & PAGE_MASK)
102 
103 #define pte_page(x) pfn_to_page(pte_pfn(x))
104 
105 #define pte_present(x)	pte_get_bits(x, (_PAGE_PRESENT | _PAGE_PROTNONE))
106 
107 /*
108  * =================================
109  * Flags checking section.
110  * =================================
111  */
112 
113 static inline int pte_none(pte_t pte)
114 {
115 	return pte_is_zero(pte);
116 }
117 
118 /*
119  * The following only work if pte_present() is true.
120  * Undefined behaviour if not..
121  */
122 static inline int pte_read(pte_t pte)
123 {
124 	return((pte_get_bits(pte, _PAGE_USER)) &&
125 	       !(pte_get_bits(pte, _PAGE_PROTNONE)));
126 }
127 
128 static inline int pte_exec(pte_t pte){
129 	return((pte_get_bits(pte, _PAGE_USER)) &&
130 	       !(pte_get_bits(pte, _PAGE_PROTNONE)));
131 }
132 
133 static inline int pte_write(pte_t pte)
134 {
135 	return((pte_get_bits(pte, _PAGE_RW)) &&
136 	       !(pte_get_bits(pte, _PAGE_PROTNONE)));
137 }
138 
139 static inline int pte_dirty(pte_t pte)
140 {
141 	return pte_get_bits(pte, _PAGE_DIRTY);
142 }
143 
144 static inline int pte_young(pte_t pte)
145 {
146 	return pte_get_bits(pte, _PAGE_ACCESSED);
147 }
148 
149 static inline int pte_newpage(pte_t pte)
150 {
151 	return pte_get_bits(pte, _PAGE_NEWPAGE);
152 }
153 
154 static inline int pte_newprot(pte_t pte)
155 {
156 	return(pte_present(pte) && (pte_get_bits(pte, _PAGE_NEWPROT)));
157 }
158 
159 /*
160  * =================================
161  * Flags setting section.
162  * =================================
163  */
164 
165 static inline pte_t pte_mknewprot(pte_t pte)
166 {
167 	pte_set_bits(pte, _PAGE_NEWPROT);
168 	return(pte);
169 }
170 
171 static inline pte_t pte_mkclean(pte_t pte)
172 {
173 	pte_clear_bits(pte, _PAGE_DIRTY);
174 	return(pte);
175 }
176 
177 static inline pte_t pte_mkold(pte_t pte)
178 {
179 	pte_clear_bits(pte, _PAGE_ACCESSED);
180 	return(pte);
181 }
182 
183 static inline pte_t pte_wrprotect(pte_t pte)
184 {
185 	if (likely(pte_get_bits(pte, _PAGE_RW)))
186 		pte_clear_bits(pte, _PAGE_RW);
187 	else
188 		return pte;
189 	return(pte_mknewprot(pte));
190 }
191 
192 static inline pte_t pte_mkread(pte_t pte)
193 {
194 	if (unlikely(pte_get_bits(pte, _PAGE_USER)))
195 		return pte;
196 	pte_set_bits(pte, _PAGE_USER);
197 	return(pte_mknewprot(pte));
198 }
199 
200 static inline pte_t pte_mkdirty(pte_t pte)
201 {
202 	pte_set_bits(pte, _PAGE_DIRTY);
203 	return(pte);
204 }
205 
206 static inline pte_t pte_mkyoung(pte_t pte)
207 {
208 	pte_set_bits(pte, _PAGE_ACCESSED);
209 	return(pte);
210 }
211 
212 static inline pte_t pte_mkwrite_novma(pte_t pte)
213 {
214 	if (unlikely(pte_get_bits(pte,  _PAGE_RW)))
215 		return pte;
216 	pte_set_bits(pte, _PAGE_RW);
217 	return(pte_mknewprot(pte));
218 }
219 
220 static inline pte_t pte_mkuptodate(pte_t pte)
221 {
222 	pte_clear_bits(pte, _PAGE_NEWPAGE);
223 	if(pte_present(pte))
224 		pte_clear_bits(pte, _PAGE_NEWPROT);
225 	return(pte);
226 }
227 
228 static inline pte_t pte_mknewpage(pte_t pte)
229 {
230 	pte_set_bits(pte, _PAGE_NEWPAGE);
231 	return(pte);
232 }
233 
234 static inline void set_pte(pte_t *pteptr, pte_t pteval)
235 {
236 	pte_copy(*pteptr, pteval);
237 
238 	/* If it's a swap entry, it needs to be marked _PAGE_NEWPAGE so
239 	 * fix_range knows to unmap it.  _PAGE_NEWPROT is specific to
240 	 * mapped pages.
241 	 */
242 
243 	*pteptr = pte_mknewpage(*pteptr);
244 	if(pte_present(*pteptr)) *pteptr = pte_mknewprot(*pteptr);
245 }
246 
247 #define PFN_PTE_SHIFT		PAGE_SHIFT
248 
249 static inline void um_tlb_mark_sync(struct mm_struct *mm, unsigned long start,
250 				    unsigned long end)
251 {
252 	if (!mm->context.sync_tlb_range_to) {
253 		mm->context.sync_tlb_range_from = start;
254 		mm->context.sync_tlb_range_to = end;
255 	} else {
256 		if (start < mm->context.sync_tlb_range_from)
257 			mm->context.sync_tlb_range_from = start;
258 		if (end > mm->context.sync_tlb_range_to)
259 			mm->context.sync_tlb_range_to = end;
260 	}
261 }
262 
263 #define set_ptes set_ptes
264 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
265 			    pte_t *ptep, pte_t pte, int nr)
266 {
267 	/* Basically the default implementation */
268 	size_t length = nr * PAGE_SIZE;
269 
270 	for (;;) {
271 		set_pte(ptep, pte);
272 		if (--nr == 0)
273 			break;
274 		ptep++;
275 		pte = __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT));
276 	}
277 
278 	um_tlb_mark_sync(mm, addr, addr + length);
279 }
280 
281 #define __HAVE_ARCH_PTE_SAME
282 static inline int pte_same(pte_t pte_a, pte_t pte_b)
283 {
284 	return !((pte_val(pte_a) ^ pte_val(pte_b)) & ~_PAGE_NEWPAGE);
285 }
286 
287 /*
288  * Conversion functions: convert a page and protection to a page entry,
289  * and a page entry and page directory to the page they refer to.
290  */
291 
292 #define phys_to_page(phys) pfn_to_page(phys_to_pfn(phys))
293 #define __virt_to_page(virt) phys_to_page(__pa(virt))
294 #define page_to_phys(page) pfn_to_phys(page_to_pfn(page))
295 #define virt_to_page(addr) __virt_to_page((const unsigned long) addr)
296 
297 #define mk_pte(page, pgprot) \
298 	({ pte_t pte;					\
299 							\
300 	pte_set_val(pte, page_to_phys(page), (pgprot));	\
301 	if (pte_present(pte))				\
302 		pte_mknewprot(pte_mknewpage(pte));	\
303 	pte;})
304 
305 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
306 {
307 	pte_set_val(pte, (pte_val(pte) & _PAGE_CHG_MASK), newprot);
308 	return pte;
309 }
310 
311 /*
312  * the pmd page can be thought of an array like this: pmd_t[PTRS_PER_PMD]
313  *
314  * this macro returns the index of the entry in the pmd page which would
315  * control the given virtual address
316  */
317 #define pmd_page_vaddr(pmd) ((unsigned long) __va(pmd_val(pmd) & PAGE_MASK))
318 
319 struct mm_struct;
320 extern pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr);
321 
322 #define update_mmu_cache(vma,address,ptep) do {} while (0)
323 #define update_mmu_cache_range(vmf, vma, address, ptep, nr) do {} while (0)
324 
325 /*
326  * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
327  * are !pte_none() && !pte_present().
328  *
329  * Format of swap PTEs:
330  *
331  *   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
332  *   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
333  *   <--------------- offset ----------------> E < type -> 0 0 0 1 0
334  *
335  *   E is the exclusive marker that is not stored in swap entries.
336  *   _PAGE_NEWPAGE (bit 1) is always set to 1 in set_pte().
337  */
338 #define __swp_type(x)			(((x).val >> 5) & 0x1f)
339 #define __swp_offset(x)			((x).val >> 11)
340 
341 #define __swp_entry(type, offset) \
342 	((swp_entry_t) { (((type) & 0x1f) << 5) | ((offset) << 11) })
343 #define __pte_to_swp_entry(pte) \
344 	((swp_entry_t) { pte_val(pte_mkuptodate(pte)) })
345 #define __swp_entry_to_pte(x)		((pte_t) { (x).val })
346 
347 static inline int pte_swp_exclusive(pte_t pte)
348 {
349 	return pte_get_bits(pte, _PAGE_SWP_EXCLUSIVE);
350 }
351 
352 static inline pte_t pte_swp_mkexclusive(pte_t pte)
353 {
354 	pte_set_bits(pte, _PAGE_SWP_EXCLUSIVE);
355 	return pte;
356 }
357 
358 static inline pte_t pte_swp_clear_exclusive(pte_t pte)
359 {
360 	pte_clear_bits(pte, _PAGE_SWP_EXCLUSIVE);
361 	return pte;
362 }
363 
364 #endif
365