Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Kernel page table mapping
0004  *
0005  * Copyright (C) 2015 ARM Ltd.
0006  */
0007 
0008 #ifndef __ASM_KERNEL_PGTABLE_H
0009 #define __ASM_KERNEL_PGTABLE_H
0010 
0011 #include <asm/boot.h>
0012 #include <asm/pgtable-hwdef.h>
0013 #include <asm/sparsemem.h>
0014 
0015 /*
0016  * The linear mapping and the start of memory are both 2M aligned (per
0017  * the arm64 booting.txt requirements). Hence we can use section mapping
0018  * with 4K (section size = 2M) but not with 16K (section size = 32M) or
0019  * 64K (section size = 512M).
0020  */
0021 #ifdef CONFIG_ARM64_4K_PAGES
0022 #define ARM64_KERNEL_USES_PMD_MAPS 1
0023 #else
0024 #define ARM64_KERNEL_USES_PMD_MAPS 0
0025 #endif
0026 
0027 /*
0028  * The idmap and swapper page tables need some space reserved in the kernel
0029  * image. Both require pgd, pud (4 levels only) and pmd tables to (section)
0030  * map the kernel. With the 64K page configuration, swapper and idmap need to
0031  * map to pte level. The swapper also maps the FDT (see __create_page_tables
0032  * for more information). Note that the number of ID map translation levels
0033  * could be increased on the fly if system RAM is out of reach for the default
0034  * VA range, so pages required to map highest possible PA are reserved in all
0035  * cases.
0036  */
0037 #if ARM64_KERNEL_USES_PMD_MAPS
0038 #define SWAPPER_PGTABLE_LEVELS  (CONFIG_PGTABLE_LEVELS - 1)
0039 #else
0040 #define SWAPPER_PGTABLE_LEVELS  (CONFIG_PGTABLE_LEVELS)
0041 #endif
0042 
0043 
0044 /*
0045  * If KASLR is enabled, then an offset K is added to the kernel address
0046  * space. The bottom 21 bits of this offset are zero to guarantee 2MB
0047  * alignment for PA and VA.
0048  *
0049  * For each pagetable level of the swapper, we know that the shift will
0050  * be larger than 21 (for the 4KB granule case we use section maps thus
0051  * the smallest shift is actually 30) thus there is the possibility that
0052  * KASLR can increase the number of pagetable entries by 1, so we make
0053  * room for this extra entry.
0054  *
0055  * Note KASLR cannot increase the number of required entries for a level
0056  * by more than one because it increments both the virtual start and end
0057  * addresses equally (the extra entry comes from the case where the end
0058  * address is just pushed over a boundary and the start address isn't).
0059  */
0060 
0061 #ifdef CONFIG_RANDOMIZE_BASE
0062 #define EARLY_KASLR (1)
0063 #else
0064 #define EARLY_KASLR (0)
0065 #endif
0066 
0067 #define EARLY_ENTRIES(vstart, vend, shift, add) \
0068     ((((vend) - 1) >> (shift)) - ((vstart) >> (shift)) + 1 + add)
0069 
0070 #define EARLY_PGDS(vstart, vend, add) (EARLY_ENTRIES(vstart, vend, PGDIR_SHIFT, add))
0071 
0072 #if SWAPPER_PGTABLE_LEVELS > 3
0073 #define EARLY_PUDS(vstart, vend, add) (EARLY_ENTRIES(vstart, vend, PUD_SHIFT, add))
0074 #else
0075 #define EARLY_PUDS(vstart, vend, add) (0)
0076 #endif
0077 
0078 #if SWAPPER_PGTABLE_LEVELS > 2
0079 #define EARLY_PMDS(vstart, vend, add) (EARLY_ENTRIES(vstart, vend, SWAPPER_TABLE_SHIFT, add))
0080 #else
0081 #define EARLY_PMDS(vstart, vend, add) (0)
0082 #endif
0083 
0084 #define EARLY_PAGES(vstart, vend, add) ( 1          /* PGDIR page */                \
0085             + EARLY_PGDS((vstart), (vend), add)     /* each PGDIR needs a next level page table */  \
0086             + EARLY_PUDS((vstart), (vend), add) /* each PUD needs a next level page table */    \
0087             + EARLY_PMDS((vstart), (vend), add))    /* each PMD needs a next level page table */
0088 #define INIT_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR, _end, EARLY_KASLR))
0089 
0090 /* the initial ID map may need two extra pages if it needs to be extended */
0091 #if VA_BITS < 48
0092 #define INIT_IDMAP_DIR_SIZE ((INIT_IDMAP_DIR_PAGES + 2) * PAGE_SIZE)
0093 #else
0094 #define INIT_IDMAP_DIR_SIZE (INIT_IDMAP_DIR_PAGES * PAGE_SIZE)
0095 #endif
0096 #define INIT_IDMAP_DIR_PAGES    EARLY_PAGES(KIMAGE_VADDR, _end + MAX_FDT_SIZE + SWAPPER_BLOCK_SIZE, 1)
0097 
0098 /* Initial memory map size */
0099 #if ARM64_KERNEL_USES_PMD_MAPS
0100 #define SWAPPER_BLOCK_SHIFT PMD_SHIFT
0101 #define SWAPPER_BLOCK_SIZE  PMD_SIZE
0102 #define SWAPPER_TABLE_SHIFT PUD_SHIFT
0103 #else
0104 #define SWAPPER_BLOCK_SHIFT PAGE_SHIFT
0105 #define SWAPPER_BLOCK_SIZE  PAGE_SIZE
0106 #define SWAPPER_TABLE_SHIFT PMD_SHIFT
0107 #endif
0108 
0109 /*
0110  * Initial memory map attributes.
0111  */
0112 #define SWAPPER_PTE_FLAGS   (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)
0113 #define SWAPPER_PMD_FLAGS   (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
0114 
0115 #if ARM64_KERNEL_USES_PMD_MAPS
0116 #define SWAPPER_RW_MMUFLAGS (PMD_ATTRINDX(MT_NORMAL) | SWAPPER_PMD_FLAGS)
0117 #define SWAPPER_RX_MMUFLAGS (SWAPPER_RW_MMUFLAGS | PMD_SECT_RDONLY)
0118 #else
0119 #define SWAPPER_RW_MMUFLAGS (PTE_ATTRINDX(MT_NORMAL) | SWAPPER_PTE_FLAGS)
0120 #define SWAPPER_RX_MMUFLAGS (SWAPPER_RW_MMUFLAGS | PTE_RDONLY)
0121 #endif
0122 
0123 /*
0124  * To make optimal use of block mappings when laying out the linear
0125  * mapping, round down the base of physical memory to a size that can
0126  * be mapped efficiently, i.e., either PUD_SIZE (4k granule) or PMD_SIZE
0127  * (64k granule), or a multiple that can be mapped using contiguous bits
0128  * in the page tables: 32 * PMD_SIZE (16k granule)
0129  */
0130 #if defined(CONFIG_ARM64_4K_PAGES)
0131 #define ARM64_MEMSTART_SHIFT        PUD_SHIFT
0132 #elif defined(CONFIG_ARM64_16K_PAGES)
0133 #define ARM64_MEMSTART_SHIFT        CONT_PMD_SHIFT
0134 #else
0135 #define ARM64_MEMSTART_SHIFT        PMD_SHIFT
0136 #endif
0137 
0138 /*
0139  * sparsemem vmemmap imposes an additional requirement on the alignment of
0140  * memstart_addr, due to the fact that the base of the vmemmap region
0141  * has a direct correspondence, and needs to appear sufficiently aligned
0142  * in the virtual address space.
0143  */
0144 #if ARM64_MEMSTART_SHIFT < SECTION_SIZE_BITS
0145 #define ARM64_MEMSTART_ALIGN    (1UL << SECTION_SIZE_BITS)
0146 #else
0147 #define ARM64_MEMSTART_ALIGN    (1UL << ARM64_MEMSTART_SHIFT)
0148 #endif
0149 
0150 #endif  /* __ASM_KERNEL_PGTABLE_H */