idmap.c 3.4 KB

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