xref: /OK3568_Linux_fs/kernel/arch/x86/include/asm/pgtable_64.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_X86_PGTABLE_64_H
3*4882a593Smuzhiyun #define _ASM_X86_PGTABLE_64_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/const.h>
6*4882a593Smuzhiyun #include <asm/pgtable_64_types.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef __ASSEMBLY__
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * This file contains the functions and defines necessary to modify and use
12*4882a593Smuzhiyun  * the x86-64 page table tree.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun #include <asm/processor.h>
15*4882a593Smuzhiyun #include <linux/bitops.h>
16*4882a593Smuzhiyun #include <linux/threads.h>
17*4882a593Smuzhiyun #include <asm/fixmap.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun extern p4d_t level4_kernel_pgt[512];
20*4882a593Smuzhiyun extern p4d_t level4_ident_pgt[512];
21*4882a593Smuzhiyun extern pud_t level3_kernel_pgt[512];
22*4882a593Smuzhiyun extern pud_t level3_ident_pgt[512];
23*4882a593Smuzhiyun extern pmd_t level2_kernel_pgt[512];
24*4882a593Smuzhiyun extern pmd_t level2_fixmap_pgt[512];
25*4882a593Smuzhiyun extern pmd_t level2_ident_pgt[512];
26*4882a593Smuzhiyun extern pte_t level1_fixmap_pgt[512 * FIXMAP_PMD_NUM];
27*4882a593Smuzhiyun extern pgd_t init_top_pgt[];
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #define swapper_pg_dir init_top_pgt
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun extern void paging_init(void);
sync_initial_page_table(void)32*4882a593Smuzhiyun static inline void sync_initial_page_table(void) { }
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define pte_ERROR(e)					\
35*4882a593Smuzhiyun 	pr_err("%s:%d: bad pte %p(%016lx)\n",		\
36*4882a593Smuzhiyun 	       __FILE__, __LINE__, &(e), pte_val(e))
37*4882a593Smuzhiyun #define pmd_ERROR(e)					\
38*4882a593Smuzhiyun 	pr_err("%s:%d: bad pmd %p(%016lx)\n",		\
39*4882a593Smuzhiyun 	       __FILE__, __LINE__, &(e), pmd_val(e))
40*4882a593Smuzhiyun #define pud_ERROR(e)					\
41*4882a593Smuzhiyun 	pr_err("%s:%d: bad pud %p(%016lx)\n",		\
42*4882a593Smuzhiyun 	       __FILE__, __LINE__, &(e), pud_val(e))
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #if CONFIG_PGTABLE_LEVELS >= 5
45*4882a593Smuzhiyun #define p4d_ERROR(e)					\
46*4882a593Smuzhiyun 	pr_err("%s:%d: bad p4d %p(%016lx)\n",		\
47*4882a593Smuzhiyun 	       __FILE__, __LINE__, &(e), p4d_val(e))
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define pgd_ERROR(e)					\
51*4882a593Smuzhiyun 	pr_err("%s:%d: bad pgd %p(%016lx)\n",		\
52*4882a593Smuzhiyun 	       __FILE__, __LINE__, &(e), pgd_val(e))
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun struct mm_struct;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define mm_p4d_folded mm_p4d_folded
mm_p4d_folded(struct mm_struct * mm)57*4882a593Smuzhiyun static inline bool mm_p4d_folded(struct mm_struct *mm)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	return !pgtable_l5_enabled();
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte);
63*4882a593Smuzhiyun void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte);
64*4882a593Smuzhiyun 
native_set_pte(pte_t * ptep,pte_t pte)65*4882a593Smuzhiyun static inline void native_set_pte(pte_t *ptep, pte_t pte)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	WRITE_ONCE(*ptep, pte);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
native_pte_clear(struct mm_struct * mm,unsigned long addr,pte_t * ptep)70*4882a593Smuzhiyun static inline void native_pte_clear(struct mm_struct *mm, unsigned long addr,
71*4882a593Smuzhiyun 				    pte_t *ptep)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	native_set_pte(ptep, native_make_pte(0));
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
native_set_pte_atomic(pte_t * ptep,pte_t pte)76*4882a593Smuzhiyun static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	native_set_pte(ptep, pte);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
native_set_pmd(pmd_t * pmdp,pmd_t pmd)81*4882a593Smuzhiyun static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	WRITE_ONCE(*pmdp, pmd);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
native_pmd_clear(pmd_t * pmd)86*4882a593Smuzhiyun static inline void native_pmd_clear(pmd_t *pmd)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	native_set_pmd(pmd, native_make_pmd(0));
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
native_ptep_get_and_clear(pte_t * xp)91*4882a593Smuzhiyun static inline pte_t native_ptep_get_and_clear(pte_t *xp)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun #ifdef CONFIG_SMP
94*4882a593Smuzhiyun 	return native_make_pte(xchg(&xp->pte, 0));
95*4882a593Smuzhiyun #else
96*4882a593Smuzhiyun 	/* native_local_ptep_get_and_clear,
97*4882a593Smuzhiyun 	   but duplicated because of cyclic dependency */
98*4882a593Smuzhiyun 	pte_t ret = *xp;
99*4882a593Smuzhiyun 	native_pte_clear(NULL, 0, xp);
100*4882a593Smuzhiyun 	return ret;
101*4882a593Smuzhiyun #endif
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
native_pmdp_get_and_clear(pmd_t * xp)104*4882a593Smuzhiyun static inline pmd_t native_pmdp_get_and_clear(pmd_t *xp)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun #ifdef CONFIG_SMP
107*4882a593Smuzhiyun 	return native_make_pmd(xchg(&xp->pmd, 0));
108*4882a593Smuzhiyun #else
109*4882a593Smuzhiyun 	/* native_local_pmdp_get_and_clear,
110*4882a593Smuzhiyun 	   but duplicated because of cyclic dependency */
111*4882a593Smuzhiyun 	pmd_t ret = *xp;
112*4882a593Smuzhiyun 	native_pmd_clear(xp);
113*4882a593Smuzhiyun 	return ret;
114*4882a593Smuzhiyun #endif
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
native_set_pud(pud_t * pudp,pud_t pud)117*4882a593Smuzhiyun static inline void native_set_pud(pud_t *pudp, pud_t pud)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	WRITE_ONCE(*pudp, pud);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
native_pud_clear(pud_t * pud)122*4882a593Smuzhiyun static inline void native_pud_clear(pud_t *pud)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	native_set_pud(pud, native_make_pud(0));
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
native_pudp_get_and_clear(pud_t * xp)127*4882a593Smuzhiyun static inline pud_t native_pudp_get_and_clear(pud_t *xp)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun #ifdef CONFIG_SMP
130*4882a593Smuzhiyun 	return native_make_pud(xchg(&xp->pud, 0));
131*4882a593Smuzhiyun #else
132*4882a593Smuzhiyun 	/* native_local_pudp_get_and_clear,
133*4882a593Smuzhiyun 	 * but duplicated because of cyclic dependency
134*4882a593Smuzhiyun 	 */
135*4882a593Smuzhiyun 	pud_t ret = *xp;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	native_pud_clear(xp);
138*4882a593Smuzhiyun 	return ret;
139*4882a593Smuzhiyun #endif
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
native_set_p4d(p4d_t * p4dp,p4d_t p4d)142*4882a593Smuzhiyun static inline void native_set_p4d(p4d_t *p4dp, p4d_t p4d)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	pgd_t pgd;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (pgtable_l5_enabled() || !IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION)) {
147*4882a593Smuzhiyun 		WRITE_ONCE(*p4dp, p4d);
148*4882a593Smuzhiyun 		return;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	pgd = native_make_pgd(native_p4d_val(p4d));
152*4882a593Smuzhiyun 	pgd = pti_set_user_pgtbl((pgd_t *)p4dp, pgd);
153*4882a593Smuzhiyun 	WRITE_ONCE(*p4dp, native_make_p4d(native_pgd_val(pgd)));
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
native_p4d_clear(p4d_t * p4d)156*4882a593Smuzhiyun static inline void native_p4d_clear(p4d_t *p4d)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	native_set_p4d(p4d, native_make_p4d(0));
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
native_set_pgd(pgd_t * pgdp,pgd_t pgd)161*4882a593Smuzhiyun static inline void native_set_pgd(pgd_t *pgdp, pgd_t pgd)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	WRITE_ONCE(*pgdp, pti_set_user_pgtbl(pgdp, pgd));
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
native_pgd_clear(pgd_t * pgd)166*4882a593Smuzhiyun static inline void native_pgd_clear(pgd_t *pgd)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	native_set_pgd(pgd, native_make_pgd(0));
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun  * Conversion functions: convert a page and protection to a page entry,
173*4882a593Smuzhiyun  * and a page entry and page directory to the page they refer to.
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /* PGD - Level 4 access */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /* PUD - Level 3 access */
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /* PMD - Level 2 access */
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /* PTE - Level 1 access */
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun  * Encode and de-code a swap entry
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * |     ...            | 11| 10|  9|8|7|6|5| 4| 3|2| 1|0| <- bit number
188*4882a593Smuzhiyun  * |     ...            |SW3|SW2|SW1|G|L|D|A|CD|WT|U| W|P| <- bit names
189*4882a593Smuzhiyun  * | TYPE (59-63) | ~OFFSET (9-58)  |0|0|X|X| X| X|F|SD|0| <- swp entry
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * G (8) is aliased and used as a PROT_NONE indicator for
192*4882a593Smuzhiyun  * !present ptes.  We need to start storing swap entries above
193*4882a593Smuzhiyun  * there.  We also need to avoid using A and D because of an
194*4882a593Smuzhiyun  * erratum where they can be incorrectly set by hardware on
195*4882a593Smuzhiyun  * non-present PTEs.
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * SD Bits 1-4 are not used in non-present format and available for
198*4882a593Smuzhiyun  * special use described below:
199*4882a593Smuzhiyun  *
200*4882a593Smuzhiyun  * SD (1) in swp entry is used to store soft dirty bit, which helps us
201*4882a593Smuzhiyun  * remember soft dirty over page migration
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * F (2) in swp entry is used to record when a pagetable is
204*4882a593Smuzhiyun  * writeprotected by userfaultfd WP support.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * Bit 7 in swp entry should be 0 because pmd_present checks not only P,
207*4882a593Smuzhiyun  * but also L and G.
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * The offset is inverted by a binary not operation to make the high
210*4882a593Smuzhiyun  * physical bits set.
211*4882a593Smuzhiyun  */
212*4882a593Smuzhiyun #define SWP_TYPE_BITS		5
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun #define SWP_OFFSET_FIRST_BIT	(_PAGE_BIT_PROTNONE + 1)
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /* We always extract/encode the offset by shifting it all the way up, and then down again */
217*4882a593Smuzhiyun #define SWP_OFFSET_SHIFT	(SWP_OFFSET_FIRST_BIT+SWP_TYPE_BITS)
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /* Extract the high bits for type */
222*4882a593Smuzhiyun #define __swp_type(x) ((x).val >> (64 - SWP_TYPE_BITS))
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /* Shift up (to get rid of type), then down to get value */
225*4882a593Smuzhiyun #define __swp_offset(x) (~(x).val << SWP_TYPE_BITS >> SWP_OFFSET_SHIFT)
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun  * Shift the offset up "too far" by TYPE bits, then down again
229*4882a593Smuzhiyun  * The offset is inverted by a binary not operation to make the high
230*4882a593Smuzhiyun  * physical bits set.
231*4882a593Smuzhiyun  */
232*4882a593Smuzhiyun #define __swp_entry(type, offset) ((swp_entry_t) { \
233*4882a593Smuzhiyun 	(~(unsigned long)(offset) << SWP_OFFSET_SHIFT >> SWP_TYPE_BITS) \
234*4882a593Smuzhiyun 	| ((unsigned long)(type) << (64-SWP_TYPE_BITS)) })
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val((pte)) })
237*4882a593Smuzhiyun #define __pmd_to_swp_entry(pmd)		((swp_entry_t) { pmd_val((pmd)) })
238*4882a593Smuzhiyun #define __swp_entry_to_pte(x)		((pte_t) { .pte = (x).val })
239*4882a593Smuzhiyun #define __swp_entry_to_pmd(x)		((pmd_t) { .pmd = (x).val })
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun extern int kern_addr_valid(unsigned long addr);
242*4882a593Smuzhiyun extern void cleanup_highmap(void);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun #define HAVE_ARCH_UNMAPPED_AREA
245*4882a593Smuzhiyun #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun #define PAGE_AGP    PAGE_KERNEL_NOCACHE
248*4882a593Smuzhiyun #define HAVE_PAGE_AGP 1
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /* fs/proc/kcore.c */
251*4882a593Smuzhiyun #define	kc_vaddr_to_offset(v) ((v) & __VIRTUAL_MASK)
252*4882a593Smuzhiyun #define	kc_offset_to_vaddr(o) ((o) | ~__VIRTUAL_MASK)
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun #define __HAVE_ARCH_PTE_SAME
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun #define vmemmap ((struct page *)VMEMMAP_START)
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun extern void init_extra_mapping_uc(unsigned long phys, unsigned long size);
259*4882a593Smuzhiyun extern void init_extra_mapping_wb(unsigned long phys, unsigned long size);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun #define gup_fast_permitted gup_fast_permitted
gup_fast_permitted(unsigned long start,unsigned long end)262*4882a593Smuzhiyun static inline bool gup_fast_permitted(unsigned long start, unsigned long end)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	if (end >> __VIRTUAL_MASK_SHIFT)
265*4882a593Smuzhiyun 		return false;
266*4882a593Smuzhiyun 	return true;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun #include <asm/pgtable-invert.h>
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun #endif /* !__ASSEMBLY__ */
272*4882a593Smuzhiyun #endif /* _ASM_X86_PGTABLE_64_H */
273