idmap.c 3.6 KB

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