1
0

kernel-pgtable.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Kernel page table mapping
  3. *
  4. * Copyright (C) 2015 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __ASM_KERNEL_PGTABLE_H
  19. #define __ASM_KERNEL_PGTABLE_H
  20. #include <asm/pgtable.h>
  21. #include <asm/sparsemem.h>
  22. /*
  23. * The linear mapping and the start of memory are both 2M aligned (per
  24. * the arm64 booting.txt requirements). Hence we can use section mapping
  25. * with 4K (section size = 2M) but not with 16K (section size = 32M) or
  26. * 64K (section size = 512M).
  27. */
  28. #ifdef CONFIG_ARM64_4K_PAGES
  29. #define ARM64_SWAPPER_USES_SECTION_MAPS 1
  30. #else
  31. #define ARM64_SWAPPER_USES_SECTION_MAPS 0
  32. #endif
  33. /*
  34. * The idmap and swapper page tables need some space reserved in the kernel
  35. * image. Both require pgd, pud (4 levels only) and pmd tables to (section)
  36. * map the kernel. With the 64K page configuration, swapper and idmap need to
  37. * map to pte level. The swapper also maps the FDT (see __create_page_tables
  38. * for more information). Note that the number of ID map translation levels
  39. * could be increased on the fly if system RAM is out of reach for the default
  40. * VA range, so pages required to map highest possible PA are reserved in all
  41. * cases.
  42. */
  43. #if ARM64_SWAPPER_USES_SECTION_MAPS
  44. #define SWAPPER_PGTABLE_LEVELS (CONFIG_PGTABLE_LEVELS - 1)
  45. #define IDMAP_PGTABLE_LEVELS (ARM64_HW_PGTABLE_LEVELS(PHYS_MASK_SHIFT) - 1)
  46. #else
  47. #define SWAPPER_PGTABLE_LEVELS (CONFIG_PGTABLE_LEVELS)
  48. #define IDMAP_PGTABLE_LEVELS (ARM64_HW_PGTABLE_LEVELS(PHYS_MASK_SHIFT))
  49. #endif
  50. /*
  51. * If KASLR is enabled, then an offset K is added to the kernel address
  52. * space. The bottom 21 bits of this offset are zero to guarantee 2MB
  53. * alignment for PA and VA.
  54. *
  55. * For each pagetable level of the swapper, we know that the shift will
  56. * be larger than 21 (for the 4KB granule case we use section maps thus
  57. * the smallest shift is actually 30) thus there is the possibility that
  58. * KASLR can increase the number of pagetable entries by 1, so we make
  59. * room for this extra entry.
  60. *
  61. * Note KASLR cannot increase the number of required entries for a level
  62. * by more than one because it increments both the virtual start and end
  63. * addresses equally (the extra entry comes from the case where the end
  64. * address is just pushed over a boundary and the start address isn't).
  65. */
  66. #ifdef CONFIG_RANDOMIZE_BASE
  67. #define EARLY_KASLR (1)
  68. #else
  69. #define EARLY_KASLR (0)
  70. #endif
  71. #define EARLY_ENTRIES(vstart, vend, shift) (((vend) >> (shift)) \
  72. - ((vstart) >> (shift)) + 1 + EARLY_KASLR)
  73. #define EARLY_PGDS(vstart, vend) (EARLY_ENTRIES(vstart, vend, PGDIR_SHIFT))
  74. #if SWAPPER_PGTABLE_LEVELS > 3
  75. #define EARLY_PUDS(vstart, vend) (EARLY_ENTRIES(vstart, vend, PUD_SHIFT))
  76. #else
  77. #define EARLY_PUDS(vstart, vend) (0)
  78. #endif
  79. #if SWAPPER_PGTABLE_LEVELS > 2
  80. #define EARLY_PMDS(vstart, vend) (EARLY_ENTRIES(vstart, vend, SWAPPER_TABLE_SHIFT))
  81. #else
  82. #define EARLY_PMDS(vstart, vend) (0)
  83. #endif
  84. #define EARLY_PAGES(vstart, vend) ( 1 /* PGDIR page */ \
  85. + EARLY_PGDS((vstart), (vend)) /* each PGDIR needs a next level page table */ \
  86. + EARLY_PUDS((vstart), (vend)) /* each PUD needs a next level page table */ \
  87. + EARLY_PMDS((vstart), (vend))) /* each PMD needs a next level page table */
  88. #define SWAPPER_DIR_SIZE (PAGE_SIZE * EARLY_PAGES(KIMAGE_VADDR + TEXT_OFFSET, _end))
  89. #define IDMAP_DIR_SIZE (IDMAP_PGTABLE_LEVELS * PAGE_SIZE)
  90. #ifdef CONFIG_ARM64_SW_TTBR0_PAN
  91. #define RESERVED_TTBR0_SIZE (PAGE_SIZE)
  92. #else
  93. #define RESERVED_TTBR0_SIZE (0)
  94. #endif
  95. /* Initial memory map size */
  96. #if ARM64_SWAPPER_USES_SECTION_MAPS
  97. #define SWAPPER_BLOCK_SHIFT SECTION_SHIFT
  98. #define SWAPPER_BLOCK_SIZE SECTION_SIZE
  99. #define SWAPPER_TABLE_SHIFT PUD_SHIFT
  100. #else
  101. #define SWAPPER_BLOCK_SHIFT PAGE_SHIFT
  102. #define SWAPPER_BLOCK_SIZE PAGE_SIZE
  103. #define SWAPPER_TABLE_SHIFT PMD_SHIFT
  104. #endif
  105. /* The size of the initial kernel direct mapping */
  106. #define SWAPPER_INIT_MAP_SIZE (_AC(1, UL) << SWAPPER_TABLE_SHIFT)
  107. /*
  108. * Initial memory map attributes.
  109. */
  110. #define SWAPPER_PTE_FLAGS (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)
  111. #define SWAPPER_PMD_FLAGS (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
  112. #if ARM64_SWAPPER_USES_SECTION_MAPS
  113. #define SWAPPER_MM_MMUFLAGS (PMD_ATTRINDX(MT_NORMAL) | SWAPPER_PMD_FLAGS)
  114. #else
  115. #define SWAPPER_MM_MMUFLAGS (PTE_ATTRINDX(MT_NORMAL) | SWAPPER_PTE_FLAGS)
  116. #endif
  117. /*
  118. * To make optimal use of block mappings when laying out the linear
  119. * mapping, round down the base of physical memory to a size that can
  120. * be mapped efficiently, i.e., either PUD_SIZE (4k granule) or PMD_SIZE
  121. * (64k granule), or a multiple that can be mapped using contiguous bits
  122. * in the page tables: 32 * PMD_SIZE (16k granule)
  123. */
  124. #if defined(CONFIG_ARM64_4K_PAGES)
  125. #define ARM64_MEMSTART_SHIFT PUD_SHIFT
  126. #elif defined(CONFIG_ARM64_16K_PAGES)
  127. #define ARM64_MEMSTART_SHIFT (PMD_SHIFT + 5)
  128. #else
  129. #define ARM64_MEMSTART_SHIFT PMD_SHIFT
  130. #endif
  131. /*
  132. * sparsemem vmemmap imposes an additional requirement on the alignment of
  133. * memstart_addr, due to the fact that the base of the vmemmap region
  134. * has a direct correspondence, and needs to appear sufficiently aligned
  135. * in the virtual address space.
  136. */
  137. #if defined(CONFIG_SPARSEMEM_VMEMMAP) && ARM64_MEMSTART_SHIFT < SECTION_SIZE_BITS
  138. #define ARM64_MEMSTART_ALIGN (1UL << SECTION_SIZE_BITS)
  139. #else
  140. #define ARM64_MEMSTART_ALIGN (1UL << ARM64_MEMSTART_SHIFT)
  141. #endif
  142. #endif /* __ASM_KERNEL_PGTABLE_H */