idmap.c 3.0 KB

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