Skip to content

Commit c79b954

Browse files
Jungseok Leectmarinas
authored andcommitted
arm64: mm: Implement 4 levels of translation tables
This patch implements 4 levels of translation tables since 3 levels of page tables with 4KB pages cannot support 40-bit physical address space described in [1] due to the following issue. It is a restriction that kernel logical memory map with 4KB + 3 levels (0xffffffc000000000-0xffffffffffffffff) cannot cover RAM region from 544GB to 1024GB in [1]. Specifically, ARM64 kernel fails to create mapping for this region in map_mem function since __phys_to_virt for this region reaches to address overflow. If SoC design follows the document, [1], over 32GB RAM would be placed from 544GB. Even 64GB system is supposed to use the region from 544GB to 576GB for only 32GB RAM. Naturally, it would reach to enable 4 levels of page tables to avoid hacking __virt_to_phys and __phys_to_virt. However, it is recommended 4 levels of page table should be only enabled if memory map is too sparse or there is about 512GB RAM. References ---------- [1]: Principles of ARM Memory Maps, White Paper, Issue C Signed-off-by: Jungseok Lee <jays.lee@samsung.com> Reviewed-by: Sungjinn Chung <sungjinn.chung@samsung.com> Acked-by: Kukjin Kim <kgene.kim@samsung.com> Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org> Reviewed-by: Steve Capper <steve.capper@linaro.org> [catalin.marinas@arm.com: MEMBLOCK_INITIAL_LIMIT removed, same as PUD_SIZE] [catalin.marinas@arm.com: early_ioremap_init() updated for 4 levels] [catalin.marinas@arm.com: 48-bit VA depends on BROKEN until KVM is fixed] Signed-off-by: Catalin Marinas <catalin.marinas@arm.com> Tested-by: Jungseok Lee <jungseoklee85@gmail.com>
1 parent 57e0139 commit c79b954

11 files changed

Lines changed: 147 additions & 17 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,27 @@ config ARM64_VA_BITS_42
195195
bool "42-bit"
196196
depends on ARM64_64K_PAGES
197197

198+
config ARM64_VA_BITS_48
199+
bool "48-bit"
200+
depends on BROKEN
201+
198202
endchoice
199203

200204
config ARM64_VA_BITS
201205
int
202206
default 39 if ARM64_VA_BITS_39
203207
default 42 if ARM64_VA_BITS_42
208+
default 48 if ARM64_VA_BITS_48
204209

205210
config ARM64_2_LEVELS
206211
def_bool y if ARM64_64K_PAGES && ARM64_VA_BITS_42
207212

208213
config ARM64_3_LEVELS
209214
def_bool y if ARM64_4K_PAGES && ARM64_VA_BITS_39
210215

216+
config ARM64_4_LEVELS
217+
def_bool y if ARM64_4K_PAGES && ARM64_VA_BITS_48
218+
211219
config CPU_BIG_ENDIAN
212220
bool "Build big-endian kernel"
213221
help

‎arch/arm64/include/asm/page.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@
3333

3434
/*
3535
* The idmap and swapper page tables need some space reserved in the kernel
36-
* image. Both require a pgd and a next level table to (section) map the
37-
* kernel. The the swapper also maps the FDT (see __create_page_tables for
36+
* image. Both require pgd, pud (4 levels only) and pmd tables to (section)
37+
* map the kernel. The swapper also maps the FDT (see __create_page_tables for
3838
* more information).
3939
*/
40+
#ifdef CONFIG_ARM64_4_LEVELS
41+
#define SWAPPER_DIR_SIZE (3 * PAGE_SIZE)
42+
#define IDMAP_DIR_SIZE (3 * PAGE_SIZE)
43+
#else
4044
#define SWAPPER_DIR_SIZE (2 * PAGE_SIZE)
4145
#define IDMAP_DIR_SIZE (2 * PAGE_SIZE)
46+
#endif
4247

4348
#ifndef __ASSEMBLY__
4449

4550
#ifdef CONFIG_ARM64_2_LEVELS
4651
#include <asm/pgtable-2level-types.h>
47-
#else
52+
#elif defined(CONFIG_ARM64_3_LEVELS)
4853
#include <asm/pgtable-3level-types.h>
54+
#else
55+
#include <asm/pgtable-4level-types.h>
4956
#endif
5057

5158
extern void __cpu_clear_user_page(void *p, unsigned long user);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
4646

4747
#endif /* CONFIG_ARM64_2_LEVELS */
4848

49+
#ifdef CONFIG_ARM64_4_LEVELS
50+
51+
static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
52+
{
53+
return (pud_t *)get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
54+
}
55+
56+
static inline void pud_free(struct mm_struct *mm, pud_t *pud)
57+
{
58+
BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
59+
free_page((unsigned long)pud);
60+
}
61+
62+
static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, pud_t *pud)
63+
{
64+
set_pgd(pgd, __pgd(__pa(pud) | PUD_TYPE_TABLE));
65+
}
66+
67+
#endif /* CONFIG_ARM64_4_LEVELS */
68+
4969
extern pgd_t *pgd_alloc(struct mm_struct *mm);
5070
extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
5171

‎arch/arm64/include/asm/pgtable-hwdef.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818

1919
#ifdef CONFIG_ARM64_2_LEVELS
2020
#include <asm/pgtable-2level-hwdef.h>
21-
#else
21+
#elif defined(CONFIG_ARM64_3_LEVELS)
2222
#include <asm/pgtable-3level-hwdef.h>
23+
#else
24+
#include <asm/pgtable-4level-hwdef.h>
2325
#endif
2426

2527
/*
2628
* Hardware page table definitions.
2729
*
2830
* Level 1 descriptor (PUD).
2931
*/
30-
32+
#define PUD_TYPE_TABLE (_AT(pudval_t, 3) << 0)
3133
#define PUD_TABLE_BIT (_AT(pgdval_t, 1) << 1)
3234
#define PUD_TYPE_MASK (_AT(pgdval_t, 3) << 0)
3335
#define PUD_TYPE_SECT (_AT(pgdval_t, 1) << 0)

‎arch/arm64/include/asm/pgtable.h‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
* VMALLOC and SPARSEMEM_VMEMMAP ranges.
3636
*/
3737
#define VMALLOC_START (UL(0xffffffffffffffff) << VA_BITS)
38+
#ifndef CONFIG_ARM64_4_LEVELS
3839
#define VMALLOC_END (PAGE_OFFSET - UL(0x400000000) - SZ_64K)
40+
#else
41+
#define VMALLOC_END (PAGE_OFFSET - UL(0x40000000000) - SZ_64K)
42+
#endif
3943

4044
#define vmemmap ((struct page *)(VMALLOC_END + SZ_64K))
4145

@@ -44,12 +48,16 @@
4448
#ifndef __ASSEMBLY__
4549
extern void __pte_error(const char *file, int line, unsigned long val);
4650
extern void __pmd_error(const char *file, int line, unsigned long val);
51+
extern void __pud_error(const char *file, int line, unsigned long val);
4752
extern void __pgd_error(const char *file, int line, unsigned long val);
4853

4954
#define pte_ERROR(pte) __pte_error(__FILE__, __LINE__, pte_val(pte))
5055
#ifndef CONFIG_ARM64_2_LEVELS
5156
#define pmd_ERROR(pmd) __pmd_error(__FILE__, __LINE__, pmd_val(pmd))
5257
#endif
58+
#ifdef CONFIG_ARM64_4_LEVELS
59+
#define pud_ERROR(pud) __pud_error(__FILE__, __LINE__, pud_val(pud))
60+
#endif
5361
#define pgd_ERROR(pgd) __pgd_error(__FILE__, __LINE__, pgd_val(pgd))
5462

5563
#ifdef CONFIG_SMP
@@ -347,6 +355,30 @@ static inline pmd_t *pud_page_vaddr(pud_t pud)
347355

348356
#endif /* CONFIG_ARM64_2_LEVELS */
349357

358+
#ifdef CONFIG_ARM64_4_LEVELS
359+
360+
#define pgd_none(pgd) (!pgd_val(pgd))
361+
#define pgd_bad(pgd) (!(pgd_val(pgd) & 2))
362+
#define pgd_present(pgd) (pgd_val(pgd))
363+
364+
static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
365+
{
366+
*pgdp = pgd;
367+
dsb(ishst);
368+
}
369+
370+
static inline void pgd_clear(pgd_t *pgdp)
371+
{
372+
set_pgd(pgdp, __pgd(0));
373+
}
374+
375+
static inline pud_t *pgd_page_vaddr(pgd_t pgd)
376+
{
377+
return __va(pgd_val(pgd) & PHYS_MASK & (s32)PAGE_MASK);
378+
}
379+
380+
#endif /* CONFIG_ARM64_4_LEVELS */
381+
350382
/* to find an entry in a page-table-directory */
351383
#define pgd_index(addr) (((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
352384

@@ -355,6 +387,14 @@ static inline pmd_t *pud_page_vaddr(pud_t pud)
355387
/* to find an entry in a kernel page-table-directory */
356388
#define pgd_offset_k(addr) pgd_offset(&init_mm, addr)
357389

390+
#ifdef CONFIG_ARM64_4_LEVELS
391+
#define pud_index(addr) (((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
392+
static inline pud_t *pud_offset(pgd_t *pgd, unsigned long addr)
393+
{
394+
return (pud_t *)pgd_page_vaddr(*pgd) + pud_index(addr);
395+
}
396+
#endif
397+
358398
/* Find an entry in the second-level page table.. */
359399
#ifndef CONFIG_ARM64_2_LEVELS
360400
#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))

‎arch/arm64/include/asm/tlb.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,
100100
}
101101
#endif
102102

103+
#ifdef CONFIG_ARM64_4_LEVELS
104+
static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,
105+
unsigned long addr)
106+
{
107+
tlb_add_flush(tlb, addr);
108+
tlb_remove_page(tlb, virt_to_page(pudp));
109+
}
110+
#endif
111+
103112
static inline void __tlb_remove_pmd_tlb_entry(struct mmu_gather *tlb, pmd_t *pmdp,
104113
unsigned long address)
105114
{

‎arch/arm64/kernel/head.S‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,16 +476,42 @@ ENDPROC(__calc_phys_offset)
476476
.quad PAGE_OFFSET
477477

478478
/*
479-
* Macro to populate the PGD for the corresponding block entry in the next
480-
* level (tbl) for the given virtual address.
479+
* Macro to populate the PUD for the corresponding block entry in the next
480+
* level (tbl) for the given virtual address in case of 4 levels.
481481
*
482-
* Preserves: pgd, tbl, virt
483-
* Corrupts: tmp1, tmp2
482+
* Preserves: pgd, virt
483+
* Corrupts: tbl, tmp1, tmp2
484+
* Returns: pud
484485
*/
485-
.macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2
486+
.macro create_pud_entry, pgd, tbl, virt, pud, tmp1, tmp2
487+
#ifdef CONFIG_ARM64_4_LEVELS
488+
add \tbl, \tbl, #PAGE_SIZE // bump tbl 1 page up.
489+
// to make room for pud
490+
add \pud, \pgd, #PAGE_SIZE // pgd points to pud which
491+
// follows pgd
492+
lsr \tmp1, \virt, #PUD_SHIFT
493+
and \tmp1, \tmp1, #PTRS_PER_PUD - 1 // PUD index
494+
orr \tmp2, \tbl, #3 // PUD entry table type
495+
str \tmp2, [\pud, \tmp1, lsl #3]
496+
#else
497+
mov \pud, \tbl
498+
#endif
499+
.endm
500+
501+
/*
502+
* Macro to populate the PGD (and possibily PUD) for the corresponding
503+
* block entry in the next level (tbl) for the given virtual address.
504+
*
505+
* Preserves: pgd, virt
506+
* Corrupts: tmp1, tmp2, tmp3
507+
* Returns: tbl -> page where block mappings can be placed
508+
* (changed to make room for pud with 4 levels, preserved otherwise)
509+
*/
510+
.macro create_pgd_entry, pgd, tbl, virt, tmp1, tmp2, tmp3
511+
create_pud_entry \pgd, \tbl, \virt, \tmp3, \tmp1, \tmp2
486512
lsr \tmp1, \virt, #PGDIR_SHIFT
487513
and \tmp1, \tmp1, #PTRS_PER_PGD - 1 // PGD index
488-
orr \tmp2, \tbl, #3 // PGD entry table type
514+
orr \tmp2, \tmp3, #3 // PGD entry table type
489515
str \tmp2, [\pgd, \tmp1, lsl #3]
490516
.endm
491517

@@ -550,7 +576,7 @@ __create_page_tables:
550576
add x0, x25, #PAGE_SIZE // section table address
551577
ldr x3, =KERNEL_START
552578
add x3, x3, x28 // __pa(KERNEL_START)
553-
create_pgd_entry x25, x0, x3, x5, x6
579+
create_pgd_entry x25, x0, x3, x1, x5, x6
554580
ldr x6, =KERNEL_END
555581
mov x5, x3 // __pa(KERNEL_START)
556582
add x6, x6, x28 // __pa(KERNEL_END)
@@ -561,7 +587,7 @@ __create_page_tables:
561587
*/
562588
add x0, x26, #PAGE_SIZE // section table address
563589
mov x5, #PAGE_OFFSET
564-
create_pgd_entry x26, x0, x5, x3, x6
590+
create_pgd_entry x26, x0, x5, x1, x3, x6
565591
ldr x6, =KERNEL_END
566592
mov x3, x24 // phys offset
567593
create_block_map x0, x7, x3, x5, x6

‎arch/arm64/kernel/traps.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ void __pmd_error(const char *file, int line, unsigned long val)
339339
pr_crit("%s:%d: bad pmd %016lx.\n", file, line, val);
340340
}
341341

342+
void __pud_error(const char *file, int line, unsigned long val)
343+
{
344+
pr_crit("%s:%d: bad pud %016lx.\n", file, line, val);
345+
}
346+
342347
void __pgd_error(const char *file, int line, unsigned long val)
343348
{
344349
pr_crit("%s:%d: bad pgd %016lx.\n", file, line, val);

‎arch/arm64/mm/fault.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void show_pte(struct mm_struct *mm, unsigned long addr)
6262
break;
6363

6464
pud = pud_offset(pgd, addr);
65+
printk(", *pud=%016llx", pud_val(*pud));
6566
if (pud_none(*pud) || pud_bad(*pud))
6667
break;
6768

‎arch/arm64/mm/ioremap.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
104104
EXPORT_SYMBOL(ioremap_cache);
105105

106106
static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
107-
#ifndef CONFIG_ARM64_64K_PAGES
107+
#ifndef CONFIG_ARM64_2_LEVELS
108108
static pte_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss;
109109
#endif
110+
#ifdef CONFIG_ARM64_4_LEVELS
111+
static pte_t bm_pud[PTRS_PER_PUD] __page_aligned_bss;
112+
#endif
110113

111114
static inline pud_t * __init early_ioremap_pud(unsigned long addr)
112115
{
@@ -144,6 +147,7 @@ void __init early_ioremap_init(void)
144147
unsigned long addr = fix_to_virt(FIX_BTMAP_BEGIN);
145148

146149
pgd = pgd_offset_k(addr);
150+
pgd_populate(&init_mm, pgd, bm_pud);
147151
pud = pud_offset(pgd, addr);
148152
pud_populate(&init_mm, pud, bm_pmd);
149153
pmd = pmd_offset(pud, addr);

0 commit comments

Comments
 (0)