ARM: 8115/1: LPAE: reduce damage caused by idmap to virtual memory layout
[deliverable/linux.git] / arch / arm / mm / idmap.c
CommitLineData
9e9a367c 1#include <linux/module.h>
614dd058 2#include <linux/kernel.h>
9e9a367c 3#include <linux/slab.h>
614dd058
RK
4
5#include <asm/cputype.h>
8903826d 6#include <asm/idmap.h>
614dd058
RK
7#include <asm/pgalloc.h>
8#include <asm/pgtable.h>
8903826d 9#include <asm/sections.h>
9f97da78 10#include <asm/system_info.h>
8903826d
WD
11
12pgd_t *idmap_pgd;
4dc9a817 13phys_addr_t (*arch_virt_to_idmap) (unsigned long x);
614dd058 14
ae2de101
CM
15#ifdef CONFIG_ARM_LPAE
16static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
17 unsigned long prot)
18{
19 pmd_t *pmd;
20 unsigned long next;
21
22 if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
23 pmd = pmd_alloc_one(&init_mm, addr);
24 if (!pmd) {
25 pr_warning("Failed to allocate identity pmd.\n");
26 return;
27 }
811a2407
KK
28 /*
29 * Copy the original PMD to ensure that the PMD entries for
30 * the kernel image are preserved.
31 */
32 if (!pud_none(*pud))
33 memcpy(pmd, pmd_offset(pud, 0),
34 PTRS_PER_PMD * sizeof(pmd_t));
ae2de101
CM
35 pud_populate(&init_mm, pud, pmd);
36 pmd += pmd_index(addr);
37 } else
38 pmd = pmd_offset(pud, addr);
39
40 do {
41 next = pmd_addr_end(addr, end);
42 *pmd = __pmd((addr & PMD_MASK) | prot);
43 flush_pmd_entry(pmd);
44 } while (pmd++, addr = next, addr != end);
45}
46#else /* !CONFIG_ARM_LPAE */
516295e5 47static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
af3813d6
RK
48 unsigned long prot)
49{
516295e5 50 pmd_t *pmd = pmd_offset(pud, addr);
af3813d6
RK
51
52 addr = (addr & PMD_MASK) | prot;
53 pmd[0] = __pmd(addr);
54 addr += SECTION_SIZE;
55 pmd[1] = __pmd(addr);
56 flush_pmd_entry(pmd);
57}
ae2de101 58#endif /* CONFIG_ARM_LPAE */
af3813d6 59
516295e5
RK
60static 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
66 do {
67 next = pud_addr_end(addr, end);
68 idmap_add_pmd(pud, addr, next, prot);
69 } while (pud++, addr = next, addr != end);
70}
71
9e9a367c
CD
72static void identity_mapping_add(pgd_t *pgd, const char *text_start,
73 const char *text_end, unsigned long prot)
614dd058 74{
9e9a367c
CD
75 unsigned long addr, end;
76 unsigned long next;
77
4dc9a817
SS
78 addr = virt_to_idmap(text_start);
79 end = virt_to_idmap(text_end);
c1a5f4f6 80 pr_info("Setting up static identity map for 0x%lx - 0x%lx\n", addr, end);
9e9a367c
CD
81
82 prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
614dd058 83
614dd058
RK
84 if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
85 prot |= PMD_BIT4;
86
af3813d6
RK
87 pgd += pgd_index(addr);
88 do {
89 next = pgd_addr_end(addr, end);
516295e5 90 idmap_add_pud(pgd, addr, next, prot);
af3813d6 91 } while (pgd++, addr = next, addr != end);
614dd058
RK
92}
93
8903826d
WD
94extern char __idmap_text_start[], __idmap_text_end[];
95
96static int __init init_static_idmap(void)
97{
8903826d
WD
98 idmap_pgd = pgd_alloc(&init_mm);
99 if (!idmap_pgd)
100 return -ENOMEM;
101
9e9a367c
CD
102 identity_mapping_add(idmap_pgd, __idmap_text_start,
103 __idmap_text_end, 0);
8903826d 104
e4067855
NP
105 /* Flush L1 for the hardware to see this page table content */
106 flush_cache_louis();
107
2fb41059 108 return 0;
8903826d 109}
4e8ee7de 110early_initcall(init_static_idmap);
8903826d 111
614dd058 112/*
2c8951ab
WD
113 * In order to soft-boot, we need to switch to a 1:1 mapping for the
114 * cpu_reset functions. This will then ensure that we have predictable
115 * results when turning off the mmu.
614dd058 116 */
5aafec15 117void setup_mm_for_reboot(void)
614dd058 118{
2c8951ab
WD
119 /* Switch to the identity mapping. */
120 cpu_switch_mm(idmap_pgd, &init_mm);
89c7e4b8 121 local_flush_bp_all();
2c8951ab 122
e4067855
NP
123#ifdef CONFIG_CPU_HAS_ASID
124 /*
125 * We don't have a clean ASID for the identity mapping, which
126 * may clash with virtual addresses of the previous page tables
127 * and therefore potentially in the TLB.
128 */
614dd058 129 local_flush_tlb_all();
e4067855 130#endif
614dd058 131}
This page took 0.175431 seconds and 5 git commands to generate.