map_kernel.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2023 Google LLC
  3. // Author: Ard Biesheuvel <ardb@google.com>
  4. #include <linux/init.h>
  5. #include <linux/libfdt.h>
  6. #include <linux/linkage.h>
  7. #include <linux/types.h>
  8. #include <linux/sizes.h>
  9. #include <linux/string.h>
  10. #include <asm/memory.h>
  11. #include <asm/pgalloc.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/tlbflush.h>
  14. #include "pi.h"
  15. extern const u8 __eh_frame_start[], __eh_frame_end[];
  16. extern void idmap_cpu_replace_ttbr1(void *pgdir);
  17. static void __init map_segment(pgd_t *pg_dir, u64 *pgd, u64 va_offset,
  18. void *start, void *end, pgprot_t prot,
  19. bool may_use_cont, int root_level)
  20. {
  21. map_range(pgd, ((u64)start + va_offset) & ~PAGE_OFFSET,
  22. ((u64)end + va_offset) & ~PAGE_OFFSET, (u64)start,
  23. prot, root_level, (pte_t *)pg_dir, may_use_cont, 0);
  24. }
  25. static void __init unmap_segment(pgd_t *pg_dir, u64 va_offset, void *start,
  26. void *end, int root_level)
  27. {
  28. map_segment(pg_dir, NULL, va_offset, start, end, __pgprot(0),
  29. false, root_level);
  30. }
  31. static void __init map_kernel(u64 kaslr_offset, u64 va_offset, int root_level)
  32. {
  33. bool enable_scs = IS_ENABLED(CONFIG_UNWIND_PATCH_PAC_INTO_SCS);
  34. bool twopass = IS_ENABLED(CONFIG_RELOCATABLE);
  35. u64 pgdp = (u64)init_pg_dir + PAGE_SIZE;
  36. pgprot_t text_prot = PAGE_KERNEL_ROX;
  37. pgprot_t data_prot = PAGE_KERNEL;
  38. pgprot_t prot;
  39. /*
  40. * External debuggers may need to write directly to the text mapping to
  41. * install SW breakpoints. Allow this (only) when explicitly requested
  42. * with rodata=off.
  43. */
  44. if (arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF))
  45. text_prot = PAGE_KERNEL_EXEC;
  46. /*
  47. * We only enable the shadow call stack dynamically if we are running
  48. * on a system that does not implement PAC or BTI. PAC and SCS provide
  49. * roughly the same level of protection, and BTI relies on the PACIASP
  50. * instructions serving as landing pads, preventing us from patching
  51. * those instructions into something else.
  52. */
  53. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && cpu_has_pac())
  54. enable_scs = false;
  55. if (IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) && cpu_has_bti()) {
  56. enable_scs = false;
  57. /*
  58. * If we have a CPU that supports BTI and a kernel built for
  59. * BTI then mark the kernel executable text as guarded pages
  60. * now so we don't have to rewrite the page tables later.
  61. */
  62. text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
  63. }
  64. /* Map all code read-write on the first pass if needed */
  65. twopass |= enable_scs;
  66. prot = twopass ? data_prot : text_prot;
  67. /*
  68. * [_stext, _text) isn't executed after boot and contains some
  69. * non-executable, unpredictable data, so map it non-executable.
  70. */
  71. map_segment(init_pg_dir, &pgdp, va_offset, _text, _stext, data_prot,
  72. false, root_level);
  73. map_segment(init_pg_dir, &pgdp, va_offset, _stext, _etext, prot,
  74. !twopass, root_level);
  75. map_segment(init_pg_dir, &pgdp, va_offset, __start_rodata,
  76. __inittext_begin, data_prot, false, root_level);
  77. map_segment(init_pg_dir, &pgdp, va_offset, __inittext_begin,
  78. __inittext_end, prot, false, root_level);
  79. map_segment(init_pg_dir, &pgdp, va_offset, __initdata_begin,
  80. __initdata_end, data_prot, false, root_level);
  81. map_segment(init_pg_dir, &pgdp, va_offset, _data, _end, data_prot,
  82. true, root_level);
  83. dsb(ishst);
  84. idmap_cpu_replace_ttbr1(init_pg_dir);
  85. if (twopass) {
  86. if (IS_ENABLED(CONFIG_RELOCATABLE))
  87. relocate_kernel(kaslr_offset);
  88. if (enable_scs) {
  89. scs_patch(__eh_frame_start + va_offset,
  90. __eh_frame_end - __eh_frame_start);
  91. asm("ic ialluis");
  92. dynamic_scs_is_enabled = true;
  93. }
  94. /*
  95. * Unmap the text region before remapping it, to avoid
  96. * potential TLB conflicts when creating the contiguous
  97. * descriptors.
  98. */
  99. unmap_segment(init_pg_dir, va_offset, _stext, _etext,
  100. root_level);
  101. dsb(ishst);
  102. isb();
  103. __tlbi(vmalle1);
  104. isb();
  105. /*
  106. * Remap these segments with different permissions
  107. * No new page table allocations should be needed
  108. */
  109. map_segment(init_pg_dir, NULL, va_offset, _stext, _etext,
  110. text_prot, true, root_level);
  111. map_segment(init_pg_dir, NULL, va_offset, __inittext_begin,
  112. __inittext_end, text_prot, false, root_level);
  113. }
  114. /* Copy the root page table to its final location */
  115. memcpy((void *)swapper_pg_dir + va_offset, init_pg_dir, PAGE_SIZE);
  116. dsb(ishst);
  117. idmap_cpu_replace_ttbr1(swapper_pg_dir);
  118. }
  119. static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(u64 ttbr)
  120. {
  121. u64 sctlr = read_sysreg(sctlr_el1);
  122. u64 tcr = read_sysreg(tcr_el1) | TCR_DS;
  123. u64 mmfr0 = read_sysreg(id_aa64mmfr0_el1);
  124. u64 parange = cpuid_feature_extract_unsigned_field(mmfr0,
  125. ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  126. tcr &= ~TCR_IPS_MASK;
  127. tcr |= parange << TCR_IPS_SHIFT;
  128. asm(" msr sctlr_el1, %0 ;"
  129. " isb ;"
  130. " msr ttbr0_el1, %1 ;"
  131. " msr tcr_el1, %2 ;"
  132. " isb ;"
  133. " tlbi vmalle1 ;"
  134. " dsb nsh ;"
  135. " isb ;"
  136. " msr sctlr_el1, %3 ;"
  137. " isb ;"
  138. :: "r"(sctlr & ~SCTLR_ELx_M), "r"(ttbr), "r"(tcr), "r"(sctlr));
  139. }
  140. static void __init remap_idmap_for_lpa2(void)
  141. {
  142. /* clear the bits that change meaning once LPA2 is turned on */
  143. pteval_t mask = PTE_SHARED;
  144. /*
  145. * We have to clear bits [9:8] in all block or page descriptors in the
  146. * initial ID map, as otherwise they will be (mis)interpreted as
  147. * physical address bits once we flick the LPA2 switch (TCR.DS). Since
  148. * we cannot manipulate live descriptors in that way without creating
  149. * potential TLB conflicts, let's create another temporary ID map in a
  150. * LPA2 compatible fashion, and update the initial ID map while running
  151. * from that.
  152. */
  153. create_init_idmap(init_pg_dir, mask);
  154. dsb(ishst);
  155. set_ttbr0_for_lpa2((u64)init_pg_dir);
  156. /*
  157. * Recreate the initial ID map with the same granularity as before.
  158. * Don't bother with the FDT, we no longer need it after this.
  159. */
  160. memset(init_idmap_pg_dir, 0,
  161. (u64)init_idmap_pg_end - (u64)init_idmap_pg_dir);
  162. create_init_idmap(init_idmap_pg_dir, mask);
  163. dsb(ishst);
  164. /* switch back to the updated initial ID map */
  165. set_ttbr0_for_lpa2((u64)init_idmap_pg_dir);
  166. /* wipe the temporary ID map from memory */
  167. memset(init_pg_dir, 0, (u64)init_pg_end - (u64)init_pg_dir);
  168. }
  169. static void __init map_fdt(u64 fdt)
  170. {
  171. static u8 ptes[INIT_IDMAP_FDT_SIZE] __initdata __aligned(PAGE_SIZE);
  172. u64 efdt = fdt + MAX_FDT_SIZE;
  173. u64 ptep = (u64)ptes;
  174. /*
  175. * Map up to MAX_FDT_SIZE bytes, but avoid overlap with
  176. * the kernel image.
  177. */
  178. map_range(&ptep, fdt, (u64)_text > fdt ? min((u64)_text, efdt) : efdt,
  179. fdt, PAGE_KERNEL, IDMAP_ROOT_LEVEL,
  180. (pte_t *)init_idmap_pg_dir, false, 0);
  181. dsb(ishst);
  182. }
  183. asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt)
  184. {
  185. static char const chosen_str[] __initconst = "/chosen";
  186. u64 va_base, pa_base = (u64)&_text;
  187. u64 kaslr_offset = pa_base % MIN_KIMG_ALIGN;
  188. int root_level = 4 - CONFIG_PGTABLE_LEVELS;
  189. int va_bits = VA_BITS;
  190. int chosen;
  191. map_fdt((u64)fdt);
  192. /* Clear BSS and the initial page tables */
  193. memset(__bss_start, 0, (u64)init_pg_end - (u64)__bss_start);
  194. /* Parse the command line for CPU feature overrides */
  195. chosen = fdt_path_offset(fdt, chosen_str);
  196. init_feature_override(boot_status, fdt, chosen);
  197. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && !cpu_has_lva()) {
  198. va_bits = VA_BITS_MIN;
  199. } else if (IS_ENABLED(CONFIG_ARM64_LPA2) && !cpu_has_lpa2()) {
  200. va_bits = VA_BITS_MIN;
  201. root_level++;
  202. }
  203. if (va_bits > VA_BITS_MIN)
  204. sysreg_clear_set(tcr_el1, TCR_T1SZ_MASK, TCR_T1SZ(va_bits));
  205. /*
  206. * The virtual KASLR displacement modulo 2MiB is decided by the
  207. * physical placement of the image, as otherwise, we might not be able
  208. * to create the early kernel mapping using 2 MiB block descriptors. So
  209. * take the low bits of the KASLR offset from the physical address, and
  210. * fill in the high bits from the seed.
  211. */
  212. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  213. u64 kaslr_seed = kaslr_early_init(fdt, chosen);
  214. if (kaslr_seed && kaslr_requires_kpti())
  215. arm64_use_ng_mappings = true;
  216. kaslr_offset |= kaslr_seed & ~(MIN_KIMG_ALIGN - 1);
  217. }
  218. if (IS_ENABLED(CONFIG_ARM64_LPA2) && va_bits > VA_BITS_MIN)
  219. remap_idmap_for_lpa2();
  220. va_base = KIMAGE_VADDR + kaslr_offset;
  221. map_kernel(kaslr_offset, va_base - pa_base, root_level);
  222. }