Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/module.h>
0003 #include <linux/kernel.h>
0004 #include <linux/slab.h>
0005 #include <linux/mm_types.h>
0006 #include <linux/pgtable.h>
0007 
0008 #include <asm/cputype.h>
0009 #include <asm/idmap.h>
0010 #include <asm/hwcap.h>
0011 #include <asm/pgalloc.h>
0012 #include <asm/sections.h>
0013 #include <asm/system_info.h>
0014 
0015 /*
0016  * Note: accesses outside of the kernel image and the identity map area
0017  * are not supported on any CPU using the idmap tables as its current
0018  * page tables.
0019  */
0020 pgd_t *idmap_pgd __ro_after_init;
0021 long long arch_phys_to_idmap_offset __ro_after_init;
0022 
0023 #ifdef CONFIG_ARM_LPAE
0024 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
0025     unsigned long prot)
0026 {
0027     pmd_t *pmd;
0028     unsigned long next;
0029 
0030     if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
0031         pmd = pmd_alloc_one(&init_mm, addr);
0032         if (!pmd) {
0033             pr_warn("Failed to allocate identity pmd.\n");
0034             return;
0035         }
0036         /*
0037          * Copy the original PMD to ensure that the PMD entries for
0038          * the kernel image are preserved.
0039          */
0040         if (!pud_none(*pud))
0041             memcpy(pmd, pmd_offset(pud, 0),
0042                    PTRS_PER_PMD * sizeof(pmd_t));
0043         pud_populate(&init_mm, pud, pmd);
0044         pmd += pmd_index(addr);
0045     } else
0046         pmd = pmd_offset(pud, addr);
0047 
0048     do {
0049         next = pmd_addr_end(addr, end);
0050         *pmd = __pmd((addr & PMD_MASK) | prot);
0051         flush_pmd_entry(pmd);
0052     } while (pmd++, addr = next, addr != end);
0053 }
0054 #else   /* !CONFIG_ARM_LPAE */
0055 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
0056     unsigned long prot)
0057 {
0058     pmd_t *pmd = pmd_offset(pud, addr);
0059 
0060     addr = (addr & PMD_MASK) | prot;
0061     pmd[0] = __pmd(addr);
0062     addr += SECTION_SIZE;
0063     pmd[1] = __pmd(addr);
0064     flush_pmd_entry(pmd);
0065 }
0066 #endif  /* CONFIG_ARM_LPAE */
0067 
0068 static void idmap_add_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
0069     unsigned long prot)
0070 {
0071     p4d_t *p4d = p4d_offset(pgd, addr);
0072     pud_t *pud = pud_offset(p4d, addr);
0073     unsigned long next;
0074 
0075     do {
0076         next = pud_addr_end(addr, end);
0077         idmap_add_pmd(pud, addr, next, prot);
0078     } while (pud++, addr = next, addr != end);
0079 }
0080 
0081 static void identity_mapping_add(pgd_t *pgd, const char *text_start,
0082                  const char *text_end, unsigned long prot)
0083 {
0084     unsigned long addr, end;
0085     unsigned long next;
0086 
0087     addr = virt_to_idmap(text_start);
0088     end = virt_to_idmap(text_end);
0089     pr_info("Setting up static identity map for 0x%lx - 0x%lx\n", addr, end);
0090 
0091     prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
0092 
0093     if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale_family())
0094         prot |= PMD_BIT4;
0095 
0096     pgd += pgd_index(addr);
0097     do {
0098         next = pgd_addr_end(addr, end);
0099         idmap_add_pud(pgd, addr, next, prot);
0100     } while (pgd++, addr = next, addr != end);
0101 }
0102 
0103 extern char  __idmap_text_start[], __idmap_text_end[];
0104 
0105 static int __init init_static_idmap(void)
0106 {
0107     idmap_pgd = pgd_alloc(&init_mm);
0108     if (!idmap_pgd)
0109         return -ENOMEM;
0110 
0111     identity_mapping_add(idmap_pgd, __idmap_text_start,
0112                  __idmap_text_end, 0);
0113 
0114     /* Flush L1 for the hardware to see this page table content */
0115     if (!(elf_hwcap & HWCAP_LPAE))
0116         flush_cache_louis();
0117 
0118     return 0;
0119 }
0120 early_initcall(init_static_idmap);
0121 
0122 /*
0123  * In order to soft-boot, we need to switch to a 1:1 mapping for the
0124  * cpu_reset functions. This will then ensure that we have predictable
0125  * results when turning off the mmu.
0126  */
0127 void setup_mm_for_reboot(void)
0128 {
0129     /* Switch to the identity mapping. */
0130     cpu_switch_mm(idmap_pgd, &init_mm);
0131     local_flush_bp_all();
0132 
0133 #ifdef CONFIG_CPU_HAS_ASID
0134     /*
0135      * We don't have a clean ASID for the identity mapping, which
0136      * may clash with virtual addresses of the previous page tables
0137      * and therefore potentially in the TLB.
0138      */
0139     local_flush_tlb_all();
0140 #endif
0141 }