xref: /openbmc/linux/arch/x86/include/asm/pgalloc.h (revision 1355c31e)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_PGALLOC_H
31965aae3SH. Peter Anvin #define _ASM_X86_PGALLOC_H
4bb898558SAl Viro 
5bb898558SAl Viro #include <linux/threads.h>
6bb898558SAl Viro #include <linux/mm.h>		/* for struct page */
7bb898558SAl Viro #include <linux/pagemap.h>
8bb898558SAl Viro 
95fba4af4SMike Rapoport #define __HAVE_ARCH_PTE_ALLOC_ONE
101355c31eSMike Rapoport #include <asm-generic/pgalloc.h>
115fba4af4SMike Rapoport 
12bb898558SAl Viro static inline int  __paravirt_pgd_alloc(struct mm_struct *mm) { return 0; }
13bb898558SAl Viro 
14fdc0269eSJuergen Gross #ifdef CONFIG_PARAVIRT_XXL
15bb898558SAl Viro #include <asm/paravirt.h>
16bb898558SAl Viro #else
17bb898558SAl Viro #define paravirt_pgd_alloc(mm)	__paravirt_pgd_alloc(mm)
18bb898558SAl Viro static inline void paravirt_pgd_free(struct mm_struct *mm, pgd_t *pgd) {}
19bb898558SAl Viro static inline void paravirt_alloc_pte(struct mm_struct *mm, unsigned long pfn)	{}
20bb898558SAl Viro static inline void paravirt_alloc_pmd(struct mm_struct *mm, unsigned long pfn)	{}
21bb898558SAl Viro static inline void paravirt_alloc_pmd_clone(unsigned long pfn, unsigned long clonepfn,
22bb898558SAl Viro 					    unsigned long start, unsigned long count) {}
23bb898558SAl Viro static inline void paravirt_alloc_pud(struct mm_struct *mm, unsigned long pfn)	{}
24335437fbSKirill A. Shutemov static inline void paravirt_alloc_p4d(struct mm_struct *mm, unsigned long pfn)	{}
25bb898558SAl Viro static inline void paravirt_release_pte(unsigned long pfn) {}
26bb898558SAl Viro static inline void paravirt_release_pmd(unsigned long pfn) {}
27bb898558SAl Viro static inline void paravirt_release_pud(unsigned long pfn) {}
28335437fbSKirill A. Shutemov static inline void paravirt_release_p4d(unsigned long pfn) {}
29bb898558SAl Viro #endif
30bb898558SAl Viro 
31bb898558SAl Viro /*
3214315592SIan Campbell  * Flags to use when allocating a user page table page.
3314315592SIan Campbell  */
3414315592SIan Campbell extern gfp_t __userpte_alloc_gfp;
3514315592SIan Campbell 
36d9e9a641SDave Hansen #ifdef CONFIG_PAGE_TABLE_ISOLATION
37d9e9a641SDave Hansen /*
38d9e9a641SDave Hansen  * Instead of one PGD, we acquire two PGDs.  Being order-1, it is
39d9e9a641SDave Hansen  * both 8k in size and 8k-aligned.  That lets us just flip bit 12
40d9e9a641SDave Hansen  * in a pointer to swap between the two 4k halves.
41d9e9a641SDave Hansen  */
42d9e9a641SDave Hansen #define PGD_ALLOCATION_ORDER 1
43d9e9a641SDave Hansen #else
44d9e9a641SDave Hansen #define PGD_ALLOCATION_ORDER 0
45d9e9a641SDave Hansen #endif
46d9e9a641SDave Hansen 
4714315592SIan Campbell /*
48bb898558SAl Viro  * Allocate and free page tables.
49bb898558SAl Viro  */
50bb898558SAl Viro extern pgd_t *pgd_alloc(struct mm_struct *);
51bb898558SAl Viro extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
52bb898558SAl Viro 
534cf58924SJoel Fernandes (Google) extern pgtable_t pte_alloc_one(struct mm_struct *);
54bb898558SAl Viro 
559e1b32caSBenjamin Herrenschmidt extern void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte);
569e1b32caSBenjamin Herrenschmidt 
579e1b32caSBenjamin Herrenschmidt static inline void __pte_free_tlb(struct mmu_gather *tlb, struct page *pte,
589e1b32caSBenjamin Herrenschmidt 				  unsigned long address)
599e1b32caSBenjamin Herrenschmidt {
609e1b32caSBenjamin Herrenschmidt 	___pte_free_tlb(tlb, pte);
619e1b32caSBenjamin Herrenschmidt }
62bb898558SAl Viro 
63bb898558SAl Viro static inline void pmd_populate_kernel(struct mm_struct *mm,
64bb898558SAl Viro 				       pmd_t *pmd, pte_t *pte)
65bb898558SAl Viro {
66bb898558SAl Viro 	paravirt_alloc_pte(mm, __pa(pte) >> PAGE_SHIFT);
67bb898558SAl Viro 	set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
68bb898558SAl Viro }
69bb898558SAl Viro 
700a9fe8caSDan Williams static inline void pmd_populate_kernel_safe(struct mm_struct *mm,
710a9fe8caSDan Williams 				       pmd_t *pmd, pte_t *pte)
720a9fe8caSDan Williams {
730a9fe8caSDan Williams 	paravirt_alloc_pte(mm, __pa(pte) >> PAGE_SHIFT);
740a9fe8caSDan Williams 	set_pmd_safe(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
750a9fe8caSDan Williams }
760a9fe8caSDan Williams 
77bb898558SAl Viro static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
78bb898558SAl Viro 				struct page *pte)
79bb898558SAl Viro {
80bb898558SAl Viro 	unsigned long pfn = page_to_pfn(pte);
81bb898558SAl Viro 
82bb898558SAl Viro 	paravirt_alloc_pte(mm, pfn);
83bb898558SAl Viro 	set_pmd(pmd, __pmd(((pteval_t)pfn << PAGE_SHIFT) | _PAGE_TABLE));
84bb898558SAl Viro }
85bb898558SAl Viro 
86bb898558SAl Viro #define pmd_pgtable(pmd) pmd_page(pmd)
87bb898558SAl Viro 
8898233368SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS > 2
899e1b32caSBenjamin Herrenschmidt extern void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd);
909e1b32caSBenjamin Herrenschmidt 
919e1b32caSBenjamin Herrenschmidt static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
92b595076aSUwe Kleine-König 				  unsigned long address)
939e1b32caSBenjamin Herrenschmidt {
949e1b32caSBenjamin Herrenschmidt 	___pmd_free_tlb(tlb, pmd);
959e1b32caSBenjamin Herrenschmidt }
96bb898558SAl Viro 
97bb898558SAl Viro #ifdef CONFIG_X86_PAE
98bb898558SAl Viro extern void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd);
99bb898558SAl Viro #else	/* !CONFIG_X86_PAE */
100bb898558SAl Viro static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
101bb898558SAl Viro {
102bb898558SAl Viro 	paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
103bb898558SAl Viro 	set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd)));
104bb898558SAl Viro }
1050a9fe8caSDan Williams 
1060a9fe8caSDan Williams static inline void pud_populate_safe(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
1070a9fe8caSDan Williams {
1080a9fe8caSDan Williams 	paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
1090a9fe8caSDan Williams 	set_pud_safe(pud, __pud(_PAGE_TABLE | __pa(pmd)));
1100a9fe8caSDan Williams }
111bb898558SAl Viro #endif	/* CONFIG_X86_PAE */
112bb898558SAl Viro 
11398233368SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS > 3
114f2a6a705SKirill A. Shutemov static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
115bb898558SAl Viro {
116bb898558SAl Viro 	paravirt_alloc_pud(mm, __pa(pud) >> PAGE_SHIFT);
117f2a6a705SKirill A. Shutemov 	set_p4d(p4d, __p4d(_PAGE_TABLE | __pa(pud)));
118bb898558SAl Viro }
119bb898558SAl Viro 
1200a9fe8caSDan Williams static inline void p4d_populate_safe(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
1210a9fe8caSDan Williams {
1220a9fe8caSDan Williams 	paravirt_alloc_pud(mm, __pa(pud) >> PAGE_SHIFT);
1230a9fe8caSDan Williams 	set_p4d_safe(p4d, __p4d(_PAGE_TABLE | __pa(pud)));
1240a9fe8caSDan Williams }
1250a9fe8caSDan Williams 
126bb898558SAl Viro static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
127bb898558SAl Viro {
1283e79ec7dSVladimir Davydov 	gfp_t gfp = GFP_KERNEL_ACCOUNT;
1293e79ec7dSVladimir Davydov 
1303e79ec7dSVladimir Davydov 	if (mm == &init_mm)
1313e79ec7dSVladimir Davydov 		gfp &= ~__GFP_ACCOUNT;
1323e79ec7dSVladimir Davydov 	return (pud_t *)get_zeroed_page(gfp);
133bb898558SAl Viro }
134bb898558SAl Viro 
135bb898558SAl Viro static inline void pud_free(struct mm_struct *mm, pud_t *pud)
136bb898558SAl Viro {
137bb898558SAl Viro 	BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
138bb898558SAl Viro 	free_page((unsigned long)pud);
139bb898558SAl Viro }
140bb898558SAl Viro 
1419e1b32caSBenjamin Herrenschmidt extern void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud);
1429e1b32caSBenjamin Herrenschmidt 
1439e1b32caSBenjamin Herrenschmidt static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
1449e1b32caSBenjamin Herrenschmidt 				  unsigned long address)
1459e1b32caSBenjamin Herrenschmidt {
1469e1b32caSBenjamin Herrenschmidt 	___pud_free_tlb(tlb, pud);
1479e1b32caSBenjamin Herrenschmidt }
1489e1b32caSBenjamin Herrenschmidt 
149f2a6a705SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS > 4
150f2a6a705SKirill A. Shutemov static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)
151f2a6a705SKirill A. Shutemov {
152ed7588d5SKirill A. Shutemov 	if (!pgtable_l5_enabled())
15398219ddaSKirill A. Shutemov 		return;
154f2a6a705SKirill A. Shutemov 	paravirt_alloc_p4d(mm, __pa(p4d) >> PAGE_SHIFT);
155f2a6a705SKirill A. Shutemov 	set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(p4d)));
156f2a6a705SKirill A. Shutemov }
157f2a6a705SKirill A. Shutemov 
1580a9fe8caSDan Williams static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)
1590a9fe8caSDan Williams {
1600a9fe8caSDan Williams 	if (!pgtable_l5_enabled())
1610a9fe8caSDan Williams 		return;
1620a9fe8caSDan Williams 	paravirt_alloc_p4d(mm, __pa(p4d) >> PAGE_SHIFT);
1630a9fe8caSDan Williams 	set_pgd_safe(pgd, __pgd(_PAGE_TABLE | __pa(p4d)));
1640a9fe8caSDan Williams }
1650a9fe8caSDan Williams 
166f2a6a705SKirill A. Shutemov static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
167f2a6a705SKirill A. Shutemov {
168f2a6a705SKirill A. Shutemov 	gfp_t gfp = GFP_KERNEL_ACCOUNT;
169f2a6a705SKirill A. Shutemov 
170f2a6a705SKirill A. Shutemov 	if (mm == &init_mm)
171f2a6a705SKirill A. Shutemov 		gfp &= ~__GFP_ACCOUNT;
172f2a6a705SKirill A. Shutemov 	return (p4d_t *)get_zeroed_page(gfp);
173f2a6a705SKirill A. Shutemov }
174f2a6a705SKirill A. Shutemov 
175f2a6a705SKirill A. Shutemov static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
176f2a6a705SKirill A. Shutemov {
1770e311d23SAndrey Ryabinin 	if (!pgtable_l5_enabled())
1780e311d23SAndrey Ryabinin 		return;
1790e311d23SAndrey Ryabinin 
180f2a6a705SKirill A. Shutemov 	BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
181f2a6a705SKirill A. Shutemov 	free_page((unsigned long)p4d);
182f2a6a705SKirill A. Shutemov }
183f2a6a705SKirill A. Shutemov 
184f2a6a705SKirill A. Shutemov extern void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d);
185f2a6a705SKirill A. Shutemov 
186f2a6a705SKirill A. Shutemov static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,
187f2a6a705SKirill A. Shutemov 				  unsigned long address)
188f2a6a705SKirill A. Shutemov {
189ed7588d5SKirill A. Shutemov 	if (pgtable_l5_enabled())
190f2a6a705SKirill A. Shutemov 		___p4d_free_tlb(tlb, p4d);
191f2a6a705SKirill A. Shutemov }
192f2a6a705SKirill A. Shutemov 
193f2a6a705SKirill A. Shutemov #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
19498233368SKirill A. Shutemov #endif	/* CONFIG_PGTABLE_LEVELS > 3 */
19598233368SKirill A. Shutemov #endif	/* CONFIG_PGTABLE_LEVELS > 2 */
196bb898558SAl Viro 
1971965aae3SH. Peter Anvin #endif /* _ASM_X86_PGALLOC_H */
198