Skip to content

Commit da02877

Browse files
committed
ARM: LPAE: Page table maintenance for the 3-level format
This patch modifies the pgd/pmd/pte manipulation functions to support the 3-level page table format. Since there is no need for an 'ext' argument to cpu_set_pte_ext(), this patch conditionally defines a different prototype for this function when CONFIG_ARM_LPAE. The patch also introduces the L_PGD_SWAPPER flag to mark pgd entries pointing to pmd tables pre-allocated in the swapper_pg_dir and avoid trying to free them at run-time. This flag is 0 with the classic page table format. Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent dcfdae0 commit da02877

5 files changed

Lines changed: 150 additions & 7 deletions

File tree

‎arch/arm/include/asm/pgalloc.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,35 @@
2525
#define _PAGE_USER_TABLE (PMD_TYPE_TABLE | PMD_BIT4 | PMD_DOMAIN(DOMAIN_USER))
2626
#define _PAGE_KERNEL_TABLE (PMD_TYPE_TABLE | PMD_BIT4 | PMD_DOMAIN(DOMAIN_KERNEL))
2727

28+
#ifdef CONFIG_ARM_LPAE
29+
30+
static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
31+
{
32+
return (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
33+
}
34+
35+
static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
36+
{
37+
BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
38+
free_page((unsigned long)pmd);
39+
}
40+
41+
static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
42+
{
43+
set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
44+
}
45+
46+
#else /* !CONFIG_ARM_LPAE */
47+
2848
/*
2949
* Since we have only two-level page tables, these are trivial
3050
*/
3151
#define pmd_alloc_one(mm,addr) ({ BUG(); ((pmd_t *)2); })
3252
#define pmd_free(mm, pmd) do { } while (0)
3353
#define pud_populate(mm,pmd,pte) BUG()
3454

55+
#endif /* CONFIG_ARM_LPAE */
56+
3557
extern pgd_t *pgd_alloc(struct mm_struct *mm);
3658
extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
3759

@@ -109,7 +131,9 @@ static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t pte,
109131
{
110132
pmdval_t pmdval = (pte + PTE_HWTABLE_OFF) | prot;
111133
pmdp[0] = __pmd(pmdval);
134+
#ifndef CONFIG_ARM_LPAE
112135
pmdp[1] = __pmd(pmdval + 256 * sizeof(pte_t));
136+
#endif
113137
flush_pmd_entry(pmdp);
114138
}
115139

‎arch/arm/include/asm/pgtable-3level.h‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,57 @@
9999
#define L_PTE_MT_DEV_CACHED (_AT(pteval_t, 3) << 2) /* normal inner write-back */
100100
#define L_PTE_MT_MASK (_AT(pteval_t, 7) << 2)
101101

102+
/*
103+
* Software PGD flags.
104+
*/
105+
#define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55) /* swapper_pg_dir entry */
106+
107+
#ifndef __ASSEMBLY__
108+
109+
#define pud_none(pud) (!pud_val(pud))
110+
#define pud_bad(pud) (!(pud_val(pud) & 2))
111+
#define pud_present(pud) (pud_val(pud))
112+
113+
#define pud_clear(pudp) \
114+
do { \
115+
*pudp = __pud(0); \
116+
clean_pmd_entry(pudp); \
117+
} while (0)
118+
119+
#define set_pud(pudp, pud) \
120+
do { \
121+
*pudp = pud; \
122+
flush_pmd_entry(pudp); \
123+
} while (0)
124+
125+
static inline pmd_t *pud_page_vaddr(pud_t pud)
126+
{
127+
return __va(pud_val(pud) & PHYS_MASK & (s32)PAGE_MASK);
128+
}
129+
130+
/* Find an entry in the second-level page table.. */
131+
#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
132+
static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
133+
{
134+
return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(addr);
135+
}
136+
137+
#define pmd_bad(pmd) (!(pmd_val(pmd) & 2))
138+
139+
#define copy_pmd(pmdpd,pmdps) \
140+
do { \
141+
*pmdpd = *pmdps; \
142+
flush_pmd_entry(pmdpd); \
143+
} while (0)
144+
145+
#define pmd_clear(pmdp) \
146+
do { \
147+
*pmdp = __pmd(0); \
148+
clean_pmd_entry(pmdp); \
149+
} while (0)
150+
151+
#define set_pte_ext(ptep,pte,ext) cpu_set_pte_ext(ptep,__pte(pte_val(pte)|(ext)))
152+
153+
#endif /* __ASSEMBLY__ */
154+
102155
#endif /* _ASM_PGTABLE_3LEVEL_H */

‎arch/arm/include/asm/proc-fns.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ extern struct processor {
6565
* Set a possibly extended PTE. Non-extended PTEs should
6666
* ignore 'ext'.
6767
*/
68+
#ifdef CONFIG_ARM_LPAE
69+
void (*set_pte_ext)(pte_t *ptep, pte_t pte);
70+
#else
6871
void (*set_pte_ext)(pte_t *ptep, pte_t pte, unsigned int ext);
72+
#endif
6973

7074
/* Suspend/resume */
7175
unsigned int suspend_size;
@@ -79,7 +83,11 @@ extern void cpu_proc_fin(void);
7983
extern int cpu_do_idle(void);
8084
extern void cpu_dcache_clean_area(void *, int);
8185
extern void cpu_do_switch_mm(unsigned long pgd_phys, struct mm_struct *mm);
86+
#ifdef CONFIG_ARM_LPAE
87+
extern void cpu_set_pte_ext(pte_t *ptep, pte_t pte);
88+
#else
8289
extern void cpu_set_pte_ext(pte_t *ptep, pte_t pte, unsigned int ext);
90+
#endif
8391
extern void cpu_reset(unsigned long addr) __attribute__((noreturn));
8492

8593
/* These three are private to arch/arm/kernel/suspend.c */
@@ -107,6 +115,18 @@ extern void cpu_resume(void);
107115

108116
#define cpu_switch_mm(pgd,mm) cpu_do_switch_mm(virt_to_phys(pgd),mm)
109117

118+
#ifdef CONFIG_ARM_LPAE
119+
#define cpu_get_pgd() \
120+
({ \
121+
unsigned long pg, pg2; \
122+
__asm__("mrrc p15, 0, %0, %1, c2" \
123+
: "=r" (pg), "=r" (pg2) \
124+
: \
125+
: "cc"); \
126+
pg &= ~(PTRS_PER_PGD*sizeof(pgd_t)-1); \
127+
(pgd_t *)phys_to_virt(pg); \
128+
})
129+
#else
110130
#define cpu_get_pgd() \
111131
({ \
112132
unsigned long pg; \
@@ -115,6 +135,7 @@ extern void cpu_resume(void);
115135
pg &= ~0x3fff; \
116136
(pgd_t *)phys_to_virt(pg); \
117137
})
138+
#endif
118139

119140
#endif
120141

‎arch/arm/mm/ioremap.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void __check_kvm_seq(struct mm_struct *mm)
6464
} while (seq != init_mm.context.kvm_seq);
6565
}
6666

67-
#ifndef CONFIG_SMP
67+
#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
6868
/*
6969
* Section support is unsafe on SMP - If you iounmap and ioremap a region,
7070
* the other CPUs will not see this change until their next context switch.
@@ -202,11 +202,13 @@ void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
202202
unsigned long addr;
203203
struct vm_struct * area;
204204

205+
#ifndef CONFIG_ARM_LPAE
205206
/*
206207
* High mappings must be supersection aligned
207208
*/
208209
if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
209210
return NULL;
211+
#endif
210212

211213
/*
212214
* Don't allow RAM to be mapped - this causes problems with ARMv6+
@@ -228,7 +230,7 @@ void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
228230
return NULL;
229231
addr = (unsigned long)area->addr;
230232

231-
#ifndef CONFIG_SMP
233+
#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
232234
if (DOMAIN_IO == 0 &&
233235
(((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
234236
cpu_is_xsc3()) && pfn >= 0x100000 &&
@@ -320,7 +322,7 @@ __arm_ioremap_exec(unsigned long phys_addr, size_t size, bool cached)
320322
void __iounmap(volatile void __iomem *io_addr)
321323
{
322324
void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
323-
#ifndef CONFIG_SMP
325+
#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
324326
struct vm_struct **p, *tmp;
325327

326328
/*

‎arch/arm/mm/pgd.c‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@
1010
#include <linux/mm.h>
1111
#include <linux/gfp.h>
1212
#include <linux/highmem.h>
13+
#include <linux/slab.h>
1314

1415
#include <asm/pgalloc.h>
1516
#include <asm/page.h>
1617
#include <asm/tlbflush.h>
1718

1819
#include "mm.h"
1920

21+
#ifdef CONFIG_ARM_LPAE
22+
#define __pgd_alloc() kmalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL)
23+
#define __pgd_free(pgd) kfree(pgd)
24+
#else
25+
#define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
26+
#define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
27+
#endif
28+
2029
/*
2130
* need to get a 16k page for level 1
2231
*/
@@ -27,7 +36,7 @@ pgd_t *pgd_alloc(struct mm_struct *mm)
2736
pmd_t *new_pmd, *init_pmd;
2837
pte_t *new_pte, *init_pte;
2938

30-
new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2);
39+
new_pgd = __pgd_alloc();
3140
if (!new_pgd)
3241
goto no_pgd;
3342

@@ -42,10 +51,25 @@ pgd_t *pgd_alloc(struct mm_struct *mm)
4251

4352
clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t));
4453

54+
#ifdef CONFIG_ARM_LPAE
55+
/*
56+
* Allocate PMD table for modules and pkmap mappings.
57+
*/
58+
new_pud = pud_alloc(mm, new_pgd + pgd_index(MODULES_VADDR),
59+
MODULES_VADDR);
60+
if (!new_pud)
61+
goto no_pud;
62+
63+
new_pmd = pmd_alloc(mm, new_pud, 0);
64+
if (!new_pmd)
65+
goto no_pmd;
66+
#endif
67+
4568
if (!vectors_high()) {
4669
/*
4770
* On ARM, first page must always be allocated since it
48-
* contains the machine vectors.
71+
* contains the machine vectors. The vectors are always high
72+
* with LPAE.
4973
*/
5074
new_pud = pud_alloc(mm, new_pgd, 0);
5175
if (!new_pud)
@@ -74,7 +98,7 @@ pgd_t *pgd_alloc(struct mm_struct *mm)
7498
no_pmd:
7599
pud_free(mm, new_pud);
76100
no_pud:
77-
free_pages((unsigned long)new_pgd, 2);
101+
__pgd_free(new_pgd);
78102
no_pgd:
79103
return NULL;
80104
}
@@ -111,5 +135,24 @@ void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
111135
pgd_clear(pgd);
112136
pud_free(mm, pud);
113137
no_pgd:
114-
free_pages((unsigned long) pgd_base, 2);
138+
#ifdef CONFIG_ARM_LPAE
139+
/*
140+
* Free modules/pkmap or identity pmd tables.
141+
*/
142+
for (pgd = pgd_base; pgd < pgd_base + PTRS_PER_PGD; pgd++) {
143+
if (pgd_none_or_clear_bad(pgd))
144+
continue;
145+
if (pgd_val(*pgd) & L_PGD_SWAPPER)
146+
continue;
147+
pud = pud_offset(pgd, 0);
148+
if (pud_none_or_clear_bad(pud))
149+
continue;
150+
pmd = pmd_offset(pud, 0);
151+
pud_clear(pud);
152+
pmd_free(mm, pmd);
153+
pgd_clear(pgd);
154+
pud_free(mm, pud);
155+
}
156+
#endif
157+
__pgd_free(pgd_base);
115158
}

0 commit comments

Comments
 (0)