1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Kernel page table mapping 4 * 5 * Copyright (C) 2015 ARM Ltd. 6 */ 7 8 #ifndef __ASM_KERNEL_PGTABLE_H 9 #define __ASM_KERNEL_PGTABLE_H 10 11 #include <asm/boot.h> 12 #include <asm/pgtable-hwdef.h> 13 #include <asm/sparsemem.h> 14 15 /* 16 * The physical and virtual addresses of the start of the kernel image are 17 * equal modulo 2 MiB (per the arm64 booting.txt requirements). Hence we can 18 * use section mapping with 4K (section size = 2M) but not with 16K (section 19 * size = 32M) or 64K (section size = 512M). 20 */ 21 #if defined(PMD_SIZE) && PMD_SIZE <= MIN_KIMG_ALIGN 22 #define SWAPPER_BLOCK_SHIFT PMD_SHIFT 23 #define SWAPPER_SKIP_LEVEL 1 24 #else 25 #define SWAPPER_BLOCK_SHIFT PAGE_SHIFT 26 #define SWAPPER_SKIP_LEVEL 0 27 #endif 28 #define SWAPPER_BLOCK_SIZE (UL(1) << SWAPPER_BLOCK_SHIFT) 29 30 #define SWAPPER_PGTABLE_LEVELS (CONFIG_PGTABLE_LEVELS - SWAPPER_SKIP_LEVEL) 31 #define INIT_IDMAP_PGTABLE_LEVELS (IDMAP_LEVELS - SWAPPER_SKIP_LEVEL) 32 33 #define IDMAP_VA_BITS 48 34 #define IDMAP_LEVELS ARM64_HW_PGTABLE_LEVELS(IDMAP_VA_BITS) 35 #define IDMAP_ROOT_LEVEL (4 - IDMAP_LEVELS) 36 37 /* 38 * A relocatable kernel may execute from an address that differs from the one at 39 * which it was linked. In the worst case, its runtime placement may intersect 40 * with two adjacent PGDIR entries, which means that an additional page table 41 * may be needed at each subordinate level. 42 */ 43 #define EXTRA_PAGE __is_defined(CONFIG_RELOCATABLE) 44 45 /* 46 * LLD may evaluate SPAN_NR_ENTRIES() before _end has assumed its final value, 47 * and if the chosen preliminary value happens to be below KIMAGE_VADDR, the 48 * linker may go off into the weeds due to the negative result, ultimately 49 * leading to a 'assignment to symbol foo does not converge' error. 50 * 51 * Work around this by ensuring that SPAN_NR_ENTRIES() returns '1' in such 52 * transient cases. The final pass will observe the correct result. 53 */ 54 #define SPAN_NR_ENTRIES(vstart, vend, shift) \ 55 ((vend) <= (vstart) ? 1 : (((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1) 56 57 #define EARLY_ENTRIES(lvl, vstart, vend) \ 58 SPAN_NR_ENTRIES(vstart, vend, SWAPPER_BLOCK_SHIFT + lvl * PTDESC_TABLE_SHIFT) 59 60 #define EARLY_LEVEL(lvl, lvls, vstart, vend, add) \ 61 ((lvls) > (lvl) ? EARLY_ENTRIES(lvl, vstart, vend) + (add) : 0) 62 63 #define EARLY_PAGES(lvls, vstart, vend, add) (1 /* PGDIR page */ \ 64 + EARLY_LEVEL(3, (lvls), (vstart), (vend), add) /* each entry needs a next level page table */ \ 65 + EARLY_LEVEL(2, (lvls), (vstart), (vend), add) /* each entry needs a next level page table */ \ 66 + EARLY_LEVEL(1, (lvls), (vstart), (vend), add))/* each entry needs a next level page table */ 67 #define INIT_DIR_SIZE (PAGE_SIZE * (EARLY_PAGES(SWAPPER_PGTABLE_LEVELS, KIMAGE_VADDR, _end, EXTRA_PAGE) \ 68 + EARLY_SEGMENT_EXTRA_PAGES)) 69 70 #define INIT_IDMAP_DIR_PAGES (EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, KIMAGE_VADDR, _end, 1)) 71 #define INIT_IDMAP_DIR_SIZE ((INIT_IDMAP_DIR_PAGES + EARLY_IDMAP_EXTRA_PAGES) * PAGE_SIZE) 72 73 #define INIT_IDMAP_FDT_PAGES (EARLY_PAGES(INIT_IDMAP_PGTABLE_LEVELS, 0UL, UL(MAX_FDT_SIZE), 1) - 1) 74 #define INIT_IDMAP_FDT_SIZE ((INIT_IDMAP_FDT_PAGES + EARLY_IDMAP_EXTRA_FDT_PAGES) * PAGE_SIZE) 75 76 /* The number of segments in the kernel image (text, rodata, inittext, initdata, data+bss) */ 77 #define KERNEL_SEGMENT_COUNT 5 78 79 #if SWAPPER_BLOCK_SIZE > SEGMENT_ALIGN 80 /* 81 * KERNEL_SEGMENT_COUNT counts the permanent kernel VMAs. The early mapping 82 * has one additional split, [_text, _stext). Reserve one more page for the 83 * SWAPPER_BLOCK_SIZE-unaligned boundaries. 84 */ 85 #define EARLY_SEGMENT_EXTRA_PAGES (KERNEL_SEGMENT_COUNT + 2) 86 /* 87 * The initial ID map consists of the kernel image, mapped as two separate 88 * segments, and may appear misaligned wrt the swapper block size. This means 89 * we need 3 additional pages. The DT could straddle a swapper block boundary, 90 * so it may need 2. 91 */ 92 #define EARLY_IDMAP_EXTRA_PAGES 3 93 #define EARLY_IDMAP_EXTRA_FDT_PAGES 2 94 #else 95 #define EARLY_SEGMENT_EXTRA_PAGES 0 96 #define EARLY_IDMAP_EXTRA_PAGES 0 97 #define EARLY_IDMAP_EXTRA_FDT_PAGES 0 98 #endif 99 100 #endif /* __ASM_KERNEL_PGTABLE_H */ 101