idmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/slab.h>
  5. #include <linux/mm_types.h>
  6. #include <linux/pgtable.h>
  7. #include <asm/cputype.h>
  8. #include <asm/idmap.h>
  9. #include <asm/hwcap.h>
  10. #include <asm/pgalloc.h>
  11. #include <asm/sections.h>
  12. #include <asm/system_info.h>
  13. /*
  14. * Note: accesses outside of the kernel image and the identity map area
  15. * are not supported on any CPU using the idmap tables as its current
  16. * page tables.
  17. */
  18. pgd_t *idmap_pgd __ro_after_init;
  19. long long arch_phys_to_idmap_offset __ro_after_init;
  20. #ifdef CONFIG_ARM_LPAE
  21. static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
  22. unsigned long prot)
  23. {
  24. pmd_t *pmd;
  25. unsigned long next;
  26. if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
  27. pmd = pmd_alloc_one(&init_mm, addr);
  28. if (!pmd) {
  29. pr_warn("Failed to allocate identity pmd.\n");
  30. return;
  31. }
  32. /*
  33. * Copy the original PMD to ensure that the PMD entries for
  34. * the kernel image are preserved.
  35. */
  36. if (!pud_none(*pud))
  37. memcpy(pmd, pmd_offset(pud, 0),
  38. PTRS_PER_PMD * sizeof(pmd_t));
  39. pud_populate(&init_mm, pud, pmd);
  40. pmd += pmd_index(addr);
  41. } else
  42. pmd = pmd_offset(pud, addr);
  43. do {
  44. next = pmd_addr_end(addr, end);
  45. *pmd = __pmd((addr & PMD_MASK) | prot);
  46. flush_pmd_entry(pmd);
  47. } while (pmd++, addr = next, addr != end);
  48. }
  49. #else /* !CONFIG_ARM_LPAE */
  50. static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
  51. unsigned long prot)
  52. {
  53. pmd_t *pmd = pmd_offset(pud, addr);
  54. addr = (addr & PMD_MASK) | prot;
  55. pmd[0] = __pmd(addr);
  56. addr += SECTION_SIZE;
  57. pmd[1] = __pmd(addr);
  58. flush_pmd_entry(pmd);
  59. }
  60. #endif /* CONFIG_ARM_LPAE */
  61. static void idmap_add_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
  62. unsigned long prot)
  63. {
  64. p4d_t *p4d = p4d_offset(pgd, addr);
  65. pud_t *pud = pud_offset(p4d, addr);
  66. unsigned long next;
  67. do {
  68. next = pud_addr_end(addr, end);
  69. idmap_add_pmd(pud, addr, next, prot);
  70. } while (pud++, addr = next, addr != end);
  71. }
  72. static void identity_mapping_add(pgd_t *pgd, const char *text_start,
  73. const char *text_end, unsigned long prot)
  74. {
  75. unsigned long addr, end;
  76. unsigned long next;
  77. addr = virt_to_idmap(text_start);
  78. end = virt_to_idmap(text_end);
  79. pr_info("Setting up static identity map for 0x%lx - 0x%lx\n", addr, end);
  80. prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
  81. if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale_family())
  82. prot |= PMD_BIT4;
  83. pgd += pgd_index(addr);
  84. do {
  85. next = pgd_addr_end(addr, end);
  86. idmap_add_pud(pgd, addr, next, prot);
  87. } while (pgd++, addr = next, addr != end);
  88. }
  89. extern char __idmap_text_start[], __idmap_text_end[];
  90. static int __init init_static_idmap(void)
  91. {
  92. idmap_pgd = pgd_alloc(&init_mm);
  93. if (!idmap_pgd)
  94. return -ENOMEM;
  95. identity_mapping_add(idmap_pgd, __idmap_text_start,
  96. __idmap_text_end, 0);
  97. /* Flush L1 for the hardware to see this page table content */
  98. if (!(elf_hwcap & HWCAP_LPAE))
  99. flush_cache_louis();
  100. return 0;
  101. }
  102. early_initcall(init_static_idmap);
  103. /*
  104. * In order to soft-boot, we need to switch to a 1:1 mapping for the
  105. * cpu_reset functions. This will then ensure that we have predictable
  106. * results when turning off the mmu.
  107. */
  108. void setup_mm_for_reboot(void)
  109. {
  110. /* Switch to the identity mapping. */
  111. cpu_switch_mm(idmap_pgd, &init_mm);
  112. local_flush_bp_all();
  113. #ifdef CONFIG_CPU_HAS_ASID
  114. /*
  115. * We don't have a clean ASID for the identity mapping, which
  116. * may clash with virtual addresses of the previous page tables
  117. * and therefore potentially in the TLB.
  118. */
  119. local_flush_tlb_all();
  120. #endif
  121. }