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. phys_addr_t (*arch_virt_to_idmap) (unsigned long x);
  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_idmap(text_start);
  62. end = virt_to_idmap(text_end);
  63. pr_info("Setting up static identity map for 0x%lx - 0x%lx\n", addr, end);
  64. prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
  65. if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
  66. prot |= PMD_BIT4;
  67. pgd += pgd_index(addr);
  68. do {
  69. next = pgd_addr_end(addr, end);
  70. idmap_add_pud(pgd, addr, next, prot);
  71. } while (pgd++, addr = next, addr != end);
  72. }
  73. extern char __idmap_text_start[], __idmap_text_end[];
  74. static int __init init_static_idmap(void)
  75. {
  76. idmap_pgd = pgd_alloc(&init_mm);
  77. if (!idmap_pgd)
  78. return -ENOMEM;
  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. }