|
@@ -26,6 +26,8 @@
|
|
|
#include <linux/memblock.h>
|
|
|
#include <linux/fs.h>
|
|
|
#include <linux/io.h>
|
|
|
+#include <linux/slab.h>
|
|
|
+#include <linux/stop_machine.h>
|
|
|
|
|
|
#include <asm/cputype.h>
|
|
|
#include <asm/fixmap.h>
|
|
@@ -45,80 +47,6 @@
|
|
|
struct page *empty_zero_page;
|
|
|
EXPORT_SYMBOL(empty_zero_page);
|
|
|
|
|
|
-struct cachepolicy {
|
|
|
- const char policy[16];
|
|
|
- u64 mair;
|
|
|
- u64 tcr;
|
|
|
-};
|
|
|
-
|
|
|
-static struct cachepolicy cache_policies[] __initdata = {
|
|
|
- {
|
|
|
- .policy = "uncached",
|
|
|
- .mair = 0x44, /* inner, outer non-cacheable */
|
|
|
- .tcr = TCR_IRGN_NC | TCR_ORGN_NC,
|
|
|
- }, {
|
|
|
- .policy = "writethrough",
|
|
|
- .mair = 0xaa, /* inner, outer write-through, read-allocate */
|
|
|
- .tcr = TCR_IRGN_WT | TCR_ORGN_WT,
|
|
|
- }, {
|
|
|
- .policy = "writeback",
|
|
|
- .mair = 0xee, /* inner, outer write-back, read-allocate */
|
|
|
- .tcr = TCR_IRGN_WBnWA | TCR_ORGN_WBnWA,
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-/*
|
|
|
- * These are useful for identifying cache coherency problems by allowing the
|
|
|
- * cache or the cache and writebuffer to be turned off. It changes the Normal
|
|
|
- * memory caching attributes in the MAIR_EL1 register.
|
|
|
- */
|
|
|
-static int __init early_cachepolicy(char *p)
|
|
|
-{
|
|
|
- int i;
|
|
|
- u64 tmp;
|
|
|
-
|
|
|
- for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
|
|
|
- int len = strlen(cache_policies[i].policy);
|
|
|
-
|
|
|
- if (memcmp(p, cache_policies[i].policy, len) == 0)
|
|
|
- break;
|
|
|
- }
|
|
|
- if (i == ARRAY_SIZE(cache_policies)) {
|
|
|
- pr_err("ERROR: unknown or unsupported cache policy: %s\n", p);
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- flush_cache_all();
|
|
|
-
|
|
|
- /*
|
|
|
- * Modify MT_NORMAL attributes in MAIR_EL1.
|
|
|
- */
|
|
|
- asm volatile(
|
|
|
- " mrs %0, mair_el1\n"
|
|
|
- " bfi %0, %1, %2, #8\n"
|
|
|
- " msr mair_el1, %0\n"
|
|
|
- " isb\n"
|
|
|
- : "=&r" (tmp)
|
|
|
- : "r" (cache_policies[i].mair), "i" (MT_NORMAL * 8));
|
|
|
-
|
|
|
- /*
|
|
|
- * Modify TCR PTW cacheability attributes.
|
|
|
- */
|
|
|
- asm volatile(
|
|
|
- " mrs %0, tcr_el1\n"
|
|
|
- " bic %0, %0, %2\n"
|
|
|
- " orr %0, %0, %1\n"
|
|
|
- " msr tcr_el1, %0\n"
|
|
|
- " isb\n"
|
|
|
- : "=&r" (tmp)
|
|
|
- : "r" (cache_policies[i].tcr), "r" (TCR_IRGN_MASK | TCR_ORGN_MASK));
|
|
|
-
|
|
|
- flush_cache_all();
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-early_param("cachepolicy", early_cachepolicy);
|
|
|
-
|
|
|
pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
|
|
|
unsigned long size, pgprot_t vma_prot)
|
|
|
{
|
|
@@ -133,19 +61,42 @@ EXPORT_SYMBOL(phys_mem_access_prot);
|
|
|
static void __init *early_alloc(unsigned long sz)
|
|
|
{
|
|
|
void *ptr = __va(memblock_alloc(sz, sz));
|
|
|
+ BUG_ON(!ptr);
|
|
|
memset(ptr, 0, sz);
|
|
|
return ptr;
|
|
|
}
|
|
|
|
|
|
-static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
|
|
|
+/*
|
|
|
+ * remap a PMD into pages
|
|
|
+ */
|
|
|
+static void split_pmd(pmd_t *pmd, pte_t *pte)
|
|
|
+{
|
|
|
+ unsigned long pfn = pmd_pfn(*pmd);
|
|
|
+ int i = 0;
|
|
|
+
|
|
|
+ do {
|
|
|
+ /*
|
|
|
+ * Need to have the least restrictive permissions available
|
|
|
+ * permissions will be fixed up later
|
|
|
+ */
|
|
|
+ set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
|
|
|
+ pfn++;
|
|
|
+ } while (pte++, i++, i < PTRS_PER_PTE);
|
|
|
+}
|
|
|
+
|
|
|
+static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
|
|
|
unsigned long end, unsigned long pfn,
|
|
|
- pgprot_t prot)
|
|
|
+ pgprot_t prot,
|
|
|
+ void *(*alloc)(unsigned long size))
|
|
|
{
|
|
|
pte_t *pte;
|
|
|
|
|
|
- if (pmd_none(*pmd)) {
|
|
|
- pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
|
|
|
+ if (pmd_none(*pmd) || pmd_sect(*pmd)) {
|
|
|
+ pte = alloc(PTRS_PER_PTE * sizeof(pte_t));
|
|
|
+ if (pmd_sect(*pmd))
|
|
|
+ split_pmd(pmd, pte);
|
|
|
__pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
|
|
|
+ flush_tlb_all();
|
|
|
}
|
|
|
BUG_ON(pmd_bad(*pmd));
|
|
|
|
|
@@ -156,30 +107,42 @@ static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
|
|
|
} while (pte++, addr += PAGE_SIZE, addr != end);
|
|
|
}
|
|
|
|
|
|
-static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
|
|
|
- unsigned long end, phys_addr_t phys,
|
|
|
- int map_io)
|
|
|
+void split_pud(pud_t *old_pud, pmd_t *pmd)
|
|
|
+{
|
|
|
+ unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT;
|
|
|
+ pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr);
|
|
|
+ int i = 0;
|
|
|
+
|
|
|
+ do {
|
|
|
+ set_pmd(pmd, __pmd(addr | prot));
|
|
|
+ addr += PMD_SIZE;
|
|
|
+ } while (pmd++, i++, i < PTRS_PER_PMD);
|
|
|
+}
|
|
|
+
|
|
|
+static void alloc_init_pmd(struct mm_struct *mm, pud_t *pud,
|
|
|
+ unsigned long addr, unsigned long end,
|
|
|
+ phys_addr_t phys, pgprot_t prot,
|
|
|
+ void *(*alloc)(unsigned long size))
|
|
|
{
|
|
|
pmd_t *pmd;
|
|
|
unsigned long next;
|
|
|
- pmdval_t prot_sect;
|
|
|
- pgprot_t prot_pte;
|
|
|
-
|
|
|
- if (map_io) {
|
|
|
- prot_sect = PROT_SECT_DEVICE_nGnRE;
|
|
|
- prot_pte = __pgprot(PROT_DEVICE_nGnRE);
|
|
|
- } else {
|
|
|
- prot_sect = PROT_SECT_NORMAL_EXEC;
|
|
|
- prot_pte = PAGE_KERNEL_EXEC;
|
|
|
- }
|
|
|
|
|
|
/*
|
|
|
* Check for initial section mappings in the pgd/pud and remove them.
|
|
|
*/
|
|
|
- if (pud_none(*pud) || pud_bad(*pud)) {
|
|
|
- pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
|
|
|
- pud_populate(&init_mm, pud, pmd);
|
|
|
+ if (pud_none(*pud) || pud_sect(*pud)) {
|
|
|
+ pmd = alloc(PTRS_PER_PMD * sizeof(pmd_t));
|
|
|
+ if (pud_sect(*pud)) {
|
|
|
+ /*
|
|
|
+ * need to have the 1G of mappings continue to be
|
|
|
+ * present
|
|
|
+ */
|
|
|
+ split_pud(pud, pmd);
|
|
|
+ }
|
|
|
+ pud_populate(mm, pud, pmd);
|
|
|
+ flush_tlb_all();
|
|
|
}
|
|
|
+ BUG_ON(pud_bad(*pud));
|
|
|
|
|
|
pmd = pmd_offset(pud, addr);
|
|
|
do {
|
|
@@ -187,31 +150,51 @@ static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
|
|
|
/* try section mapping first */
|
|
|
if (((addr | next | phys) & ~SECTION_MASK) == 0) {
|
|
|
pmd_t old_pmd =*pmd;
|
|
|
- set_pmd(pmd, __pmd(phys | prot_sect));
|
|
|
+ set_pmd(pmd, __pmd(phys |
|
|
|
+ pgprot_val(mk_sect_prot(prot))));
|
|
|
/*
|
|
|
* Check for previous table entries created during
|
|
|
* boot (__create_page_tables) and flush them.
|
|
|
*/
|
|
|
- if (!pmd_none(old_pmd))
|
|
|
+ if (!pmd_none(old_pmd)) {
|
|
|
flush_tlb_all();
|
|
|
+ if (pmd_table(old_pmd)) {
|
|
|
+ phys_addr_t table = __pa(pte_offset_map(&old_pmd, 0));
|
|
|
+ if (!WARN_ON_ONCE(slab_is_available()))
|
|
|
+ memblock_free(table, PAGE_SIZE);
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
|
|
|
- prot_pte);
|
|
|
+ prot, alloc);
|
|
|
}
|
|
|
phys += next - addr;
|
|
|
} while (pmd++, addr = next, addr != end);
|
|
|
}
|
|
|
|
|
|
-static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
|
|
|
- unsigned long end, phys_addr_t phys,
|
|
|
- int map_io)
|
|
|
+static inline bool use_1G_block(unsigned long addr, unsigned long next,
|
|
|
+ unsigned long phys)
|
|
|
+{
|
|
|
+ if (PAGE_SHIFT != 12)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ if (((addr | next | phys) & ~PUD_MASK) != 0)
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+static void alloc_init_pud(struct mm_struct *mm, pgd_t *pgd,
|
|
|
+ unsigned long addr, unsigned long end,
|
|
|
+ phys_addr_t phys, pgprot_t prot,
|
|
|
+ void *(*alloc)(unsigned long size))
|
|
|
{
|
|
|
pud_t *pud;
|
|
|
unsigned long next;
|
|
|
|
|
|
if (pgd_none(*pgd)) {
|
|
|
- pud = early_alloc(PTRS_PER_PUD * sizeof(pud_t));
|
|
|
- pgd_populate(&init_mm, pgd, pud);
|
|
|
+ pud = alloc(PTRS_PER_PUD * sizeof(pud_t));
|
|
|
+ pgd_populate(mm, pgd, pud);
|
|
|
}
|
|
|
BUG_ON(pgd_bad(*pgd));
|
|
|
|
|
@@ -222,10 +205,10 @@ static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
|
|
|
/*
|
|
|
* For 4K granule only, attempt to put down a 1GB block
|
|
|
*/
|
|
|
- if (!map_io && (PAGE_SHIFT == 12) &&
|
|
|
- ((addr | next | phys) & ~PUD_MASK) == 0) {
|
|
|
+ if (use_1G_block(addr, next, phys)) {
|
|
|
pud_t old_pud = *pud;
|
|
|
- set_pud(pud, __pud(phys | PROT_SECT_NORMAL_EXEC));
|
|
|
+ set_pud(pud, __pud(phys |
|
|
|
+ pgprot_val(mk_sect_prot(prot))));
|
|
|
|
|
|
/*
|
|
|
* If we have an old value for a pud, it will
|
|
@@ -235,12 +218,15 @@ static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
|
|
|
* Look up the old pmd table and free it.
|
|
|
*/
|
|
|
if (!pud_none(old_pud)) {
|
|
|
- phys_addr_t table = __pa(pmd_offset(&old_pud, 0));
|
|
|
- memblock_free(table, PAGE_SIZE);
|
|
|
flush_tlb_all();
|
|
|
+ if (pud_table(old_pud)) {
|
|
|
+ phys_addr_t table = __pa(pmd_offset(&old_pud, 0));
|
|
|
+ if (!WARN_ON_ONCE(slab_is_available()))
|
|
|
+ memblock_free(table, PAGE_SIZE);
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
- alloc_init_pmd(pud, addr, next, phys, map_io);
|
|
|
+ alloc_init_pmd(mm, pud, addr, next, phys, prot, alloc);
|
|
|
}
|
|
|
phys += next - addr;
|
|
|
} while (pud++, addr = next, addr != end);
|
|
@@ -250,9 +236,10 @@ static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
|
|
|
* Create the page directory entries and any necessary page tables for the
|
|
|
* mapping specified by 'md'.
|
|
|
*/
|
|
|
-static void __init __create_mapping(pgd_t *pgd, phys_addr_t phys,
|
|
|
- unsigned long virt, phys_addr_t size,
|
|
|
- int map_io)
|
|
|
+static void __create_mapping(struct mm_struct *mm, pgd_t *pgd,
|
|
|
+ phys_addr_t phys, unsigned long virt,
|
|
|
+ phys_addr_t size, pgprot_t prot,
|
|
|
+ void *(*alloc)(unsigned long size))
|
|
|
{
|
|
|
unsigned long addr, length, end, next;
|
|
|
|
|
@@ -262,31 +249,95 @@ static void __init __create_mapping(pgd_t *pgd, phys_addr_t phys,
|
|
|
end = addr + length;
|
|
|
do {
|
|
|
next = pgd_addr_end(addr, end);
|
|
|
- alloc_init_pud(pgd, addr, next, phys, map_io);
|
|
|
+ alloc_init_pud(mm, pgd, addr, next, phys, prot, alloc);
|
|
|
phys += next - addr;
|
|
|
} while (pgd++, addr = next, addr != end);
|
|
|
}
|
|
|
|
|
|
-static void __init create_mapping(phys_addr_t phys, unsigned long virt,
|
|
|
- phys_addr_t size)
|
|
|
+static void *late_alloc(unsigned long size)
|
|
|
+{
|
|
|
+ void *ptr;
|
|
|
+
|
|
|
+ BUG_ON(size > PAGE_SIZE);
|
|
|
+ ptr = (void *)__get_free_page(PGALLOC_GFP);
|
|
|
+ BUG_ON(!ptr);
|
|
|
+ return ptr;
|
|
|
+}
|
|
|
+
|
|
|
+static void __ref create_mapping(phys_addr_t phys, unsigned long virt,
|
|
|
+ phys_addr_t size, pgprot_t prot)
|
|
|
{
|
|
|
if (virt < VMALLOC_START) {
|
|
|
pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
|
|
|
&phys, virt);
|
|
|
return;
|
|
|
}
|
|
|
- __create_mapping(pgd_offset_k(virt & PAGE_MASK), phys, virt, size, 0);
|
|
|
+ __create_mapping(&init_mm, pgd_offset_k(virt & PAGE_MASK), phys, virt,
|
|
|
+ size, prot, early_alloc);
|
|
|
+}
|
|
|
+
|
|
|
+void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
|
|
|
+ unsigned long virt, phys_addr_t size,
|
|
|
+ pgprot_t prot)
|
|
|
+{
|
|
|
+ __create_mapping(mm, pgd_offset(mm, virt), phys, virt, size, prot,
|
|
|
+ late_alloc);
|
|
|
}
|
|
|
|
|
|
-void __init create_id_mapping(phys_addr_t addr, phys_addr_t size, int map_io)
|
|
|
+static void create_mapping_late(phys_addr_t phys, unsigned long virt,
|
|
|
+ phys_addr_t size, pgprot_t prot)
|
|
|
{
|
|
|
- if ((addr >> PGDIR_SHIFT) >= ARRAY_SIZE(idmap_pg_dir)) {
|
|
|
- pr_warn("BUG: not creating id mapping for %pa\n", &addr);
|
|
|
+ if (virt < VMALLOC_START) {
|
|
|
+ pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
|
|
|
+ &phys, virt);
|
|
|
return;
|
|
|
}
|
|
|
- __create_mapping(&idmap_pg_dir[pgd_index(addr)],
|
|
|
- addr, addr, size, map_io);
|
|
|
+
|
|
|
+ return __create_mapping(&init_mm, pgd_offset_k(virt & PAGE_MASK),
|
|
|
+ phys, virt, size, prot, late_alloc);
|
|
|
+}
|
|
|
+
|
|
|
+#ifdef CONFIG_DEBUG_RODATA
|
|
|
+static void __init __map_memblock(phys_addr_t start, phys_addr_t end)
|
|
|
+{
|
|
|
+ /*
|
|
|
+ * Set up the executable regions using the existing section mappings
|
|
|
+ * for now. This will get more fine grained later once all memory
|
|
|
+ * is mapped
|
|
|
+ */
|
|
|
+ unsigned long kernel_x_start = round_down(__pa(_stext), SECTION_SIZE);
|
|
|
+ unsigned long kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
|
|
|
+
|
|
|
+ if (end < kernel_x_start) {
|
|
|
+ create_mapping(start, __phys_to_virt(start),
|
|
|
+ end - start, PAGE_KERNEL);
|
|
|
+ } else if (start >= kernel_x_end) {
|
|
|
+ create_mapping(start, __phys_to_virt(start),
|
|
|
+ end - start, PAGE_KERNEL);
|
|
|
+ } else {
|
|
|
+ if (start < kernel_x_start)
|
|
|
+ create_mapping(start, __phys_to_virt(start),
|
|
|
+ kernel_x_start - start,
|
|
|
+ PAGE_KERNEL);
|
|
|
+ create_mapping(kernel_x_start,
|
|
|
+ __phys_to_virt(kernel_x_start),
|
|
|
+ kernel_x_end - kernel_x_start,
|
|
|
+ PAGE_KERNEL_EXEC);
|
|
|
+ if (kernel_x_end < end)
|
|
|
+ create_mapping(kernel_x_end,
|
|
|
+ __phys_to_virt(kernel_x_end),
|
|
|
+ end - kernel_x_end,
|
|
|
+ PAGE_KERNEL);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
+#else
|
|
|
+static void __init __map_memblock(phys_addr_t start, phys_addr_t end)
|
|
|
+{
|
|
|
+ create_mapping(start, __phys_to_virt(start), end - start,
|
|
|
+ PAGE_KERNEL_EXEC);
|
|
|
+}
|
|
|
+#endif
|
|
|
|
|
|
static void __init map_mem(void)
|
|
|
{
|
|
@@ -332,14 +383,53 @@ static void __init map_mem(void)
|
|
|
memblock_set_current_limit(limit);
|
|
|
}
|
|
|
#endif
|
|
|
-
|
|
|
- create_mapping(start, __phys_to_virt(start), end - start);
|
|
|
+ __map_memblock(start, end);
|
|
|
}
|
|
|
|
|
|
/* Limit no longer required. */
|
|
|
memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
|
|
|
}
|
|
|
|
|
|
+void __init fixup_executable(void)
|
|
|
+{
|
|
|
+#ifdef CONFIG_DEBUG_RODATA
|
|
|
+ /* now that we are actually fully mapped, make the start/end more fine grained */
|
|
|
+ if (!IS_ALIGNED((unsigned long)_stext, SECTION_SIZE)) {
|
|
|
+ unsigned long aligned_start = round_down(__pa(_stext),
|
|
|
+ SECTION_SIZE);
|
|
|
+
|
|
|
+ create_mapping(aligned_start, __phys_to_virt(aligned_start),
|
|
|
+ __pa(_stext) - aligned_start,
|
|
|
+ PAGE_KERNEL);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!IS_ALIGNED((unsigned long)__init_end, SECTION_SIZE)) {
|
|
|
+ unsigned long aligned_end = round_up(__pa(__init_end),
|
|
|
+ SECTION_SIZE);
|
|
|
+ create_mapping(__pa(__init_end), (unsigned long)__init_end,
|
|
|
+ aligned_end - __pa(__init_end),
|
|
|
+ PAGE_KERNEL);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+#ifdef CONFIG_DEBUG_RODATA
|
|
|
+void mark_rodata_ro(void)
|
|
|
+{
|
|
|
+ create_mapping_late(__pa(_stext), (unsigned long)_stext,
|
|
|
+ (unsigned long)_etext - (unsigned long)_stext,
|
|
|
+ PAGE_KERNEL_EXEC | PTE_RDONLY);
|
|
|
+
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+void fixup_init(void)
|
|
|
+{
|
|
|
+ create_mapping_late(__pa(__init_begin), (unsigned long)__init_begin,
|
|
|
+ (unsigned long)__init_end - (unsigned long)__init_begin,
|
|
|
+ PAGE_KERNEL);
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
* paging_init() sets up the page tables, initialises the zone memory
|
|
|
* maps and sets up the zero page.
|
|
@@ -349,13 +439,7 @@ void __init paging_init(void)
|
|
|
void *zero_page;
|
|
|
|
|
|
map_mem();
|
|
|
-
|
|
|
- /*
|
|
|
- * Finally flush the caches and tlb to ensure that we're in a
|
|
|
- * consistent state.
|
|
|
- */
|
|
|
- flush_cache_all();
|
|
|
- flush_tlb_all();
|
|
|
+ fixup_executable();
|
|
|
|
|
|
/* allocate the zero page. */
|
|
|
zero_page = early_alloc(PAGE_SIZE);
|