ARM: MPU Support for PMSAv7 processors
cover ARM: MPU Support for PMSAv7 processors
This patch series adds basic support for the PMSAv7-compliant MPU, which is present on R-class cores.
The initial support implemented here lacks the ability to deal with setting different region configurations for the I and D-side. No hardware is available at the moment that supports this capability. It sets out a staic MPU mapping, keeping one region for probing, one for the whole of memory and one for RAM.
The final patch in the series also creates a region protecting the vectors from being accessed at PL0, reducing the possibility of programming errors in userspace causing the kernel to panic as it is entered from user mode.
This series is the final step in enabling SMP with Cortex-R7 (and hence NOMMU) by configuring the memory as shared.
In order to test these patches, you will need:
i) My nommu fixes series, which incorporates Will's NOMMU and SMP fixes
- http://lists.infradead.org/pipermail/linux-arm-kernel/2013-May/168106.html
ii) The Cortex-R7 support patches posted earlier to this list.
- http://lists.infradead.org/pipermail/linux-arm-kernel/2013-May/168128.html
iii) The multiplatform patches posted earlier to this list.
- http://lists.infradead.org/pipermail/linux-arm-kernel/2013-May/168106.html
Jonathan Austin (8):
ARM: mpu: add PMSA related registers and bitfields to existing
headers
ARM: mpu: add header for MPU register layouts and region data
ARM: mpu: add early bring-up code for the ARMv7 PMSA-compliant MPU
ARM: mpu: add MPU probe and initialisation functions in C
ARM: mpu: Complete initialisation of the MPU after reaching the
C-world
ARM: mpu: add MPU initialisation for secondary cores
ARM: mpu: Allow enabling of the MPU via kconfig
ARM: mpu: protect the vectors page with an MPU region
arch/arm/Kconfig | 4 +- arch/arm/Kconfig-nommu | 11 ++ arch/arm/include/asm/cp15.h | 5 + arch/arm/include/asm/cputype.h | 1 + arch/arm/include/asm/mpu.h | 76 ++++++++++++ arch/arm/include/asm/smp.h | 5 +- arch/arm/kernel/head-nommu.S | 110 +++++++++++++++++ arch/arm/kernel/signal.c | 9 +- arch/arm/kernel/smp.c | 8 +- arch/arm/mm/nommu.c | 257 +++++++++++++++++++++++++++++++++++++++- 10 files changed, 478 insertions(+), 8 deletions(-) create mode 100644 arch/arm/include/asm/mpu.h
Signature · 1 line
1.7.9.5
PATCH 1/8 ARM: mpu: add PMSA related registers and bitfields to existing headers
This patch adds the following definitions relevant to the PMSA:
Add SCTLR bit 17, (CR_BR - Background Region bit) to the list of CR_* bitfields. This bit determines whether to use the architecturally defined memory map
Add the MPUIR to the available registers when using read_cpuid macro. The MPUIR is the MPU type register.
--- arch/arm/include/asm/cp15.h | 5 +++++ arch/arm/include/asm/cputype.h | 1 + 2 files changed, 6 insertions(+)
arch/arm/include/asm/cp15.h +5 -0
@@ -23,6 +23,11 @@#define CR_RR (1 << 14)/* Round Robin cache replacement */ #define CR_L4 (1 << 15)/* LDR pc can set T bit */ #define CR_DT (1 << 16) +#ifdef CONFIG_MMU +#define CR_HA (1 << 17)/* Hardware management of Access Flag */ +#else +#define CR_BR (1 << 17)/* MPU Background region enable (PMSA) */ +#endif #define CR_IT (1 << 18)#define CR_ST (1 << 19)#define CR_FI (1 << 21)/* Fast interrupt (lower latency mode) */
arch/arm/include/asm/cputype.h +1 -0
@@ -8,6 +8,7 @@#define CPUID_CACHETYPE 1#define CPUID_TCM 2#define CPUID_TLBTYPE 3 +#define CPUID_MPUIR 4#define CPUID_MPIDR 5#define CPUID_EXT_PFR0 "c1, 0"
Signature · 1 line
1.7.9.5
PATCH 2/8 ARM: mpu: add header for MPU register layouts and region data
In reply to Jonathan AustinThis commit adds definitions relevant to the ARM v7 PMSA compliant MPU.
The register layouts and region configuration data is made accessible to asm as well as C-code so that it can be used in early bring-up of the MPU.
The mpu region information structs assume that the properties for the I/D side are the same, though the implementation could be trivially extended for future platforms where this is no-longer true.
The MPU_*_REGION defines are used for the basic, static MPU region setup.
--- arch/arm/include/asm/mpu.h | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 arch/arm/include/asm/mpu.h
arch/arm/include/asm/mpu.h new file +72 -0
@@ -0,0 +1,72 @@ +#ifndef __ARM_MPU_H +#define __ARM_MPU_H + +#ifdef CONFIG_ARM_MPU + +/* MPUIR layout */ +#define MPUIR_nU 1 +#define MPUIR_DREGION 8 +#define MPUIR_IREGION 16 +#define MPUIR_DREGION_SZMASK (0xFF << MPUIR_DREGION) +#define MPUIR_IREGION_SZMASK (0xFF << MPUIR_IREGION) + +/* ID_MMFR0 data relevant to MPU */ +#define MMFR0_PMSA (0xF << 4) +#define MMFR0_PMSAv7 (3 << 4) + +/* MPU D/I Size Register fields */ +#define MPU_RSR_SZ 1 +#define MPU_RSR_EN 0 + +/* The D/I RSR value for an enabled region spanning the whole of memory */ +#define MPU_RSR_ALL_MEM 63 + +/* Individual bits in the DR/IR ACR */ +#define MPU_ACR_XN (1 << 12) +#define MPU_ACR_SHARED (1 << 2) + +/* C, B and TEX[2:0] bits only have semantic meanings when grouped */ +#define MPU_RGN_CACHEABLE 0xB +#define MPU_RGN_SHARED_CACHEABLE (MPU_RGN_CACHEABLE | MPU_ACR_SHARED) +#define MPU_RGN_STRONGLY_ORDERED 0 + +/* Main region should only be shared for SMP */ +#ifdef CONFIG_SMP +#define MPU_RGN_NORMAL (MPU_RGN_CACHEABLE | MPU_ACR_SHARED) +#else +#define MPU_RGN_NORMAL MPU_RGN_CACHEABLE +#endif + +/* Access permission bits of ACR (only define those that we use)*/ +#define MPU_AP_PL1RW_PL0RW (0x3 << 8) +#define MPU_AP_PL1RW_PL0R0 (0x2 << 8) +#define MPU_AP_PL1RW_PL0NA (0x1 << 8) + +/* For minimal static MPU region configurations */ +#define MPU_PROBE_REGION 0 +#define MPU_BG_REGION 1 +#define MPU_RAM_REGION 2 + +/* Maximum number of regions Linux is interested in */ +#define MPU_MAX_REGIONS 16 + +#ifndef __ASSEMBLY__ + +struct mpu_rgn { +/* Assume same attributes for d/i-side */ +u32 drbar; +u32 drsr; +u32 dracr; +}; + +struct mpu_rgn_info { +u32 mpuir; +struct mpu_rgn rgns[MPU_MAX_REGIONS ]; +}; +extern struct mpu_rgn_info mpu_rgn_info; + +#endif /* __ASSEMBLY__ */ + +#endif /* CONFIG_ARM_MPU */ + +#endif
Signature · 1 line
1.7.9.5
PATCH 3/8 ARM: mpu: add early bring-up code for the ARMv7 PMSA-compliant MPU
In reply to Jonathan AustinThis patch adds initial support for using the MPU, which is necessary for SMP operation on PMSAv7 processors because it is the only way to ensure memory is shared. This is an initial patch and full SMP support is added later in this series.
The setup of the MPU is performed in a way analagous to that for the MMU:
Very early initialisation before the C environment is brought up, followed by a sanity check and more complete initialisation in C.
This patch provides the simplest possible memory region configuration:MPU_PROBE_REGION: Reserved for probing MPU details, not enabledMPU_BG_REGION: A 'background' region that specifies all memory strongly orderedMPU_RAM_REGION: A single shared, cacheable, normal region for the valid RAM.
In this early initialisation code we simply map the whole of the address space with the BG_REGION and (at least) the kernel with the RAM_REGION. The MPU has region alignment constraints that require us to round past the end of the kernel.
As region 2 has a higher priority than region 1, it overrides the strongly- ordered behaviour for RAM only.
Subsequent patches will add more complete initialisation from the C-world and support for bringing up secondary CPUs.
--- arch/arm/include/asm/mpu.h | 3 ++ arch/arm/kernel/head-nommu.S | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+)
arch/arm/include/asm/mpu.h +3 -0
@@ -50,6 +50,9 @@/* Maximum number of regions Linux is interested in */ #define MPU_MAX_REGIONS 16 +#define MPU_DATA_SIDE 0 +#define MPU_INSTR_SIDE 1 +#ifndef __ASSEMBLY__ struct mpu_rgn {
arch/arm/kernel/head-nommu.S +87 -0
105 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH 4/8 ARM: mpu: add MPU probe and initialisation functions in C
In reply to Jonathan AustinThis patch adds new functions for probing and initialising the ARMv7 PMSA-compliant MPU.
These use the pre-defined and reserved MPU_PROBE_REGION for establishing properties of the MPU, which is necessary because certain probe operations require modifying region properties and reading back the results.
This patch also introduces a minimal sanity_check_meminfo_mpu function, that ensures that the memory set-up passed to the kernel can be used in conjunction with the MPU. The base address of a region must be aligned to the region size, otherwise behavior is unpredictable and region sizes can only be specified as a power-of-two. To simplify the satisfaction of these requirements this implementation currently enforces that all memory is contiguous from PHYS_OFFSET, merging banks that are contiguous but passed in separately.
The functions are added in this patch but wired in to the boot process later in the series.
--- arch/arm/mm/nommu.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+)
arch/arm/mm/nommu.c +249 -0
@@ -8,6 +8,7 @@#include <linux/pagemap.h> #include <linux/io.h> #include <linux/memblock.h> +#include <linux/kernel.h> #include <asm/cacheflush.h> #include <asm/sections.h>
@@ -15,9 +16,257 @@#include <asm/setup.h> #include <asm/traps.h> #include <asm/mach/arch.h> +#include <asm/cputype.h> +#include <asm/mpu.h> #include "mm.h" +#ifdef CONFIG_ARM_MPU +struct mpu_rgn_info mpu_rgn_info; + +/* Region number */ +static void rgnr_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c2, 0" : :"r" (v)); +} + +/* Data-side / unified region attributes */ + +/* Region access control register */ +static void dracr_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 4" : :"r" (v)); +} + +/* Region size register */ +static void drsr_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 2" : :"r" (v)); +} + +/* Region base address register */ +static void drbar_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 0" : :"r" (v)); +} + +static u32 drbar_read (void ) +{ +u32 v; + asm("mrc p15, 0, %0, c6, c1, 0" :"=r" (v)); +return v; +} +/* Optional instruction-side region attributes */ + +/* I-side Region access control register */ +static void iracr_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 5" : :"r" (v)); +} + +/* I-side Region size register */ +static void irsr_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 3" : :"r" (v)); +} + +/* I-side Region base address register */ +static void irbar_write (u32 v) +{ + asm("mcr p15, 0, %0, c6, c1, 1" : :"r" (v)); +} + +static unsigned long irbar_read (void ) +{ +unsigned long v; + asm("mrc p15, 0, %0, c6, c1, 1" :"=r" (v)); +return v; +} + +/* MPU initialisation functions */ +void __init sanity_check_meminfo_mpu (void ) +{ +int i; +struct membank *bank = meminfo.bank; +phys_addr_t phys_offset =PHYS_OFFSET ; +phys_addr_t aligned_region_size, specified_mem_size, rounded_mem_size; + +/* Initially only use memory continuous from PHYS_OFFSET */ +if (bank_phys_start (&bank[0 ]) != phys_offset) +panic ("First memory bank must be contiguous from PHYS_OFFSET" ); + +/* Banks have already been sorted by start address */ +for (i =1 ; i < meminfo.nr_banks; i++) { +if (bank[i].start <=bank_phys_end (&bank[0 ]) && +bank_phys_end (&bank[i]) >bank_phys_end (&bank[0 ])) { + bank[0 ].size =bank_phys_end (&bank[i]) - bank[0 ].start; + }else { +pr_notice ("Ignoring RAM after 0x%.8lx. " +"First non-contiguous (ignored) bank start: 0x%.8lx\n" , + (unsigned long )bank_phys_end (&bank[0 ]), + (unsigned long )bank_phys_start (&bank[i])); +break ; + } + } +/* All contiguous banks are now merged in to the first bank */ + meminfo.nr_banks =1 ; + specified_mem_size = bank[0 ].size; + +/* +* MPU has curious alignment requirements: Size must be power of 2, and +* region start must be aligned to the region size +*/ +if (phys_offset !=0 ) +pr_info ("PHYS_OFFSET != 0 => MPU Region size constrained by alignment requirements\n" ); + +/* +* Maximum aligned region might overflow phys_addr_t if phys_offset is +* 0. Hence we keep everything below 4G until we take the smaller of +* the aligned_region_size and rounded_mem_size, one of which is +* guaranteed to be smaller than the maximum physical address. +*/ + aligned_region_size = (phys_offset -1 ) ^ (phys_offset); +/* Find the max power-of-two sized region that fits inside our bank */ + rounded_mem_size = (1 <<__fls (bank[0 ].size)) -1 ; + +/* The actual region size is the smaller of the two */ + aligned_region_size = aligned_region_size < rounded_mem_size + ? aligned_region_size +1 + : rounded_mem_size +1 ; + +if (aligned_region_size != specified_mem_size) +pr_warn ("Truncating memory from 0x%.8lx to 0x%.8lx (MPU region constraints)" , + (unsigned long )specified_mem_size, + (unsigned long )aligned_region_size); + + meminfo.bank[0 ].size = aligned_region_size; +pr_debug ("MPU Region from 0x%.8lx size 0x%.8lx (end 0x%.8lx))\n" , + (unsigned long )phys_offset, + (unsigned long )aligned_region_size, + (unsigned long )bank_phys_end (&bank[0 ])); + +} + +static int mpu_present (void ) +{ +return ((read_cpuid_ext (CPUID_EXT_MMFR0 ) &MMFR0_PMSA ) == MMFR0_PMSAv7); +} + +static int mpu_max_regions (void ) +{ +/* +* We don't support a different number of I/D side regions so if we +* have separate instruction and data memory maps then return +* whichever side has a smaller number of supported regions. +*/ +u32 dregions, iregions, mpuir; + mpuir =read_cpuid (CPUID_MPUIR ); + + dregions = iregions = (mpuir &MPUIR_DREGION_SZMASK ) >>MPUIR_DREGION ; + +/* Check for separate d-side and i-side memory maps */ +if (mpuir & MPUIR_nU) + iregions = (mpuir &MPUIR_IREGION_SZMASK ) >>MPUIR_IREGION ; + +/* Use the smallest of the two maxima */ +return min (dregions, iregions); +} + +static int mpu_iside_independent (void ) +{ +/* MPUIR.nU specifies whether there is *not* a unified memory map */ +return read_cpuid (CPUID_MPUIR ) & MPUIR_nU; +} + +static int mpu_min_region_order (void ) +{ +u32 drbar_result, irbar_result; +/* We've kept a region free for this probing */ +rgnr_write (MPU_PROBE_REGION ); +isb (); +/* +* As per ARM ARM, write 0xFFFFFFFC to DRBAR to find the minimum +* region order +*/ +drbar_write (0xFFFFFFFC ); + drbar_result = irbar_result =drbar_read (); +drbar_write (0x0 ); +/* If the MPU is non-unified, we use the larger of the two minima*/ +if (mpu_iside_independent ()) { +irbar_write (0xFFFFFFFC ); + irbar_result =irbar_read (); +irbar_write (0x0 ); + } +isb ();/* Ensure that MPU region operations have completed */ +/* Return whichever result is larger */ +return __ffs (max (drbar_result, irbar_result)); +} + +static int mpu_setup_region (unsigned int number,phys_addr_t start, +unsigned int size_order,unsigned int properties) +{ +u32 size_data; + +/* We kept a region free for probing resolution of MPU regions*/ +if (number >mpu_max_regions () || number ==MPU_PROBE_REGION ) +return -ENOENT ; + +if (size_order >32 ) +return -ENOMEM ; + +if (size_order <mpu_min_region_order ()) +return -ENOMEM ; + +/* Writing N to bits 5:1 (RSR_SZ) specifies region size 2^N+1 */ + size_data = ((size_order -1 ) <<MPU_RSR_SZ ) |1 <<MPU_RSR_EN ; + +dsb ();/* Ensure all previous data accesses occur with old mappings */ +rgnr_write (number); +isb (); +drbar_write (start); +dracr_write (properties); +isb ();/* Propagate properties before enabling region */ +drsr_write (size_data); + +/* Check for independent I-side registers */ +if (mpu_iside_independent ()) { +irbar_write (start); +iracr_write (properties); +isb (); +irsr_write (size_data); + } +isb (); + +/* Store region info (we treat i/d side the same, so only store d) */ + mpu_rgn_info.rgns[number].dracr = properties; + mpu_rgn_info.rgns[number].drbar = start; + mpu_rgn_info.rgns[number].drsr = size_data; +return 0 ; +} + +/* +* Set up default MPU regions, doing nothing if there is no MPU +*/ +void __init mpu_setup (void ) +{ +int region_err; +if (!mpu_present ()) +return ; + + region_err =mpu_setup_region (MPU_RAM_REGION ,PHYS_OFFSET , +ilog2 (meminfo.bank[0 ].size), +MPU_AP_PL1RW_PL0RW |MPU_RGN_NORMAL ); +if (region_err) { +panic ("MPU region initialization failure! %d" , region_err); + }else { +pr_info ("Using ARMv7 PMSA Compliant MPU. " +"Region independence: %s, Max regions: %d\n" , +mpu_iside_independent () ?"Yes" :"No" , +mpu_max_regions ()); + } +} +#endif /* CONFIG_ARM_MPU */ +void __init arm_mm_memblock_reserve (void ) {/*
Signature · 1 line
1.7.9.5
PATCH 5/8 ARM: mpu: Complete initialisation of the MPU after reaching the C-world
In reply to Jonathan AustinMuch like with the MMU, MPU initialisation is performed in two stages; the first in the pre-C world and the 'real' initialisation during arch setup.
This patch wires in previously added MPU initialisation functions so that the whole of memory is mapped with the appropriate region properties for 'normal' RAM (the appropriate properties depend on whether the system is SMP).
Stub initialisation functions are added for the case that there MPU support is not configured in to the kernel.
--- arch/arm/mm/nommu.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm/mm/nommu.c +7 -1
@@ -265,6 +265,9 @@ void __init mpu_setup(void) mpu_max_regions()); } } +#else +static void sanity_check_meminfo_mpu (void ) {} +static void __init mpu_setup (void ) {}#endif /* CONFIG_ARM_MPU */ void __init arm_mm_memblock_reserve (void )
@@ -279,7 +282,9 @@ void __init arm_mm_memblock_reserve(void)void __init sanity_check_meminfo (void ) { -phys_addr_t end =bank_phys_end (&meminfo.bank[meminfo.nr_banks -1 ]); +phys_addr_t end; +sanity_check_meminfo_mpu (); + end =bank_phys_end (&meminfo.bank[meminfo.nr_banks -1 ]); high_memory =__va (end -1 ) +1 ; }
@@ -290,6 +295,7 @@ void __init sanity_check_meminfo(void)void __init paging_init (struct machine_desc *mdesc) {early_trap_init ((void *)CONFIG_VECTORS_BASE ); +mpu_setup ();bootmem_init (); }
Signature · 1 line
1.7.9.5
PATCH 6/8 ARM: mpu: add MPU initialisation for secondary cores
In reply to Jonathan AustinThe MPU initialisation on the primary core is performed in two stages, one minimal stage to ensure the CPU can boot and a second one after sanity_check_meminfo. As the memory configuration is known by the time we boot secondary cores only a single step is necessary, provided the values for DRSR are passed to secondaries.
This patch implements this arrangement. The configuration generated for the MPU regions is made available to the secondary core, which can then use the asm MPU intialisation code to program a complete region configuration.
This is necessary for SMP configurations without an MMU, as the MPU initialisation is the only way to ensure that memory is specified as 'shared'.
--- arch/arm/include/asm/smp.h | 5 ++++- arch/arm/kernel/head-nommu.S | 7 +++++++ arch/arm/kernel/smp.c | 8 ++++++-- 3 files changed, 17 insertions(+), 3 deletions(-)
arch/arm/include/asm/smp.h +4 -1
@@ -65,7 +65,10 @@ asmlinkage void secondary_start_kernel(void);* Initial data for bringing up a secondary CPU. */ struct secondary_data { -unsigned long pgdir; +union { +unsigned long mpu_rgn_szr; +unsigned long pgdir; + };unsigned long swapper_pg_dir;void *stack; };
arch/arm/kernel/head-nommu.S +7 -0
@@ -103,6 +103,13 @@ ENTRY(secondary_startup) adr r4, __secondary_data ldmia r4, {r7, r12} + +#ifdef CONFIG_ARM_MPU + /* Use MPU region info supplied by __cpu_up */ + ldr r6, [r7] @ get secondary_data.mpu_szr + bl __setup_mpu @ Initialize the MPU +#endif + adr lr, BSYM(__after_proc_init) @ return address mov r13, r12 @ __secondary_switched address ARM( add pc, r10, #PROCINFO_INITFUNC )
arch/arm/kernel/smp.c +6 -2
@@ -45,6 +45,7 @@#include <asm/smp_plat.h> #include <asm/virt.h> #include <asm/mach/arch.h> +#include <asm/mpu.h> /* * as from 2.5, kernels no longer have an init_tasks structure
@@ -87,6 +88,10 @@ int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)* its stack and the page tables. */ secondary_data.stack =task_stack_page (idle) +THREAD_START_SP ; +#ifdef CONFIG_ARM_MPU + secondary_data.mpu_rgn_szr = mpu_rgn_info.rgns[MPU_RAM_REGION ].drsr; +#endif +#ifdef CONFIG_MMU secondary_data.pgdir =virt_to_phys (idmap_pgd); secondary_data.swapper_pg_dir =virt_to_phys (swapper_pg_dir);
@@ -114,9 +119,8 @@ int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)pr_err ("CPU%u: failed to boot: %d\n" , cpu, ret); } - secondary_data.stack =NULL ; - secondary_data.pgdir =0 ; +memset (&secondary_data,0 ,sizeof (secondary_data));return ret; }
Signature · 1 line
1.7.9.5
PATCH 7/8 ARM: mpu: Allow enabling of the MPU via kconfig
In reply to Jonathan AustinAllows the user to select MPU support when compiling for ARM processors that support the PMSAv7.
This ensures that CONFIG_SMP depends on the MPU in the case that no MMU is present.
CONFIG_SMP_ON_UP is not implemented for nommu, so introduce an MMU dependency there.
--- arch/arm/Kconfig | 4 ++-- arch/arm/Kconfig-nommu | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-)
arch/arm/Kconfig +2 -2
@@ -1413,7 +1413,7 @@ config SMP depends on CPU_V6K || CPU_V7 depends on GENERIC_CLOCKEVENTS depends on HAVE_SMP - depends on MMU + depends on MMU || ARM_MPU select USE_GENERIC_SMP_HELPERS help This enables support for systems with more than one CPU. If you have
@@ -1434,7 +1434,7 @@ config SMP config SMP_ON_UP bool "Allow booting SMP kernel on uniprocessor systems (EXPERIMENTAL)" - depends on SMP && !XIP_KERNEL + depends on SMP && !XIP_KERNEL && MMU default y help SMP kernels contain instructions which fail on non-SMP processors.
arch/arm/Kconfig-nommu +11 -0
@@ -50,3 +50,14 @@ config REMAP_VECTORS_TO_RAM Otherwise, say 'y' here. In this case, the kernel will require external support to redirect the hardware exception vectors to the writable versions located at DRAM_BASE. + +config ARM_MPU + bool 'Use the ARM v7 PMSA Compliant MPU' + default y + help + Some ARM systems without an MMU have instead a Memory Protection + Unit (MPU) that defines the type and permissions for regions of + memory. + + If your CPU has an MPU then you should choose 'y' here unless you + know that you do not want to use the MPU.
Signature · 1 line
1.7.9.5
PATCH 8/8 ARM: mpu: protect the vectors page with an MPU region
In reply to Jonathan AustinWithout an MMU it is possible for userspace programs to start executing code in places that they have no business executing. The MPU allows some level of protection against this.
This patch protects the vectors page from access by userspace processes. Userspace tasks that dereference a null pointer are already protected by an svc at 0x0 that kills them. However when tasks use an offset from a null pointer (eg a function in a null struct) they miss this carefully placed svc and enter the exception vectors in user mode, ending up in the kernel.
This patch causes programs that do this to receive a SEGV instead of happily entering the kernel in user-mode, and hence avoid a 'Bad Mode' panic.
As part of this change it is necessary to make sigreturn happen via the stack when there is not an sa_restorer function. This change is invisible to userspace, and irrelevant to code compiled using a uClibc toolchain, which always uses an sa_restorer function.
Because we don't get to remap the vectors in !MMU kuser_helpers are not in a defined location, and hence aren't usable. This means we don't need to worry about keeping them accessible from PL0
--- arch/arm/include/asm/mpu.h | 1 + arch/arm/kernel/head-nommu.S | 16 ++++++++++++++++ arch/arm/kernel/signal.c | 9 +++++++-- 3 files changed, 24 insertions(+), 2 deletions(-)
arch/arm/include/asm/mpu.h +1 -0
@@ -46,6 +46,7 @@#define MPU_PROBE_REGION 0#define MPU_BG_REGION 1#define MPU_RAM_REGION 2 +#define MPU_VECTORS_REGION 3/* Maximum number of regions Linux is interested in */ #define MPU_MAX_REGIONS 16
arch/arm/kernel/head-nommu.S +16 -0
@@ -21,6 +21,7 @@ #include <asm/cp15.h> #include <asm/thread_info.h> #include <asm/mpu.h> +#include <asm/page.h> /* * Kernel startup entry point.
@@ -184,6 +185,7 @@ ENDPROC(__after_proc_init) * Region 0: Use this for probing the MPU details, so leave disabled. * Region 1: Background region - covers the whole of RAM as strongly ordered * Region 2: Normal, Shared, cacheable for RAM. From PHYS_OFFSET, size from r6 + * Region 3: Normal, shared, inaccessible from PL0 to protect the vectors page * * r6: Value to be written to DRSR (and IRSR if required) for MPU_RAM_REGION */
@@ -228,6 +230,20 @@ ENTRY(__setup_mpu) setup_region r0, r5, r6, MPU_INSTR_SIDE @ 0x0, BG region, enabled 2: isb + /* Vectors region */ + set_region_nr r0, #MPU_VECTORS_REGION + isb + /* Shared, inaccessible to PL0, rw PL1 */ + mov r0, #CONFIG_VECTORS_BASE @ Cover from VECTORS_BASE + ldr r5,=(MPU_AP_PL1RW_PL0NA | MPU_RGN_NORMAL) + /* Writing N to bits 5:1 (RSR_SZ) --> region size 2^N+1 */ + mov r6, #(((PAGE_SHIFT - 1) << MPU_RSR_SZ) | 1 << MPU_RSR_EN) + + setup_region r0, r5, r6, MPU_DATA_SIDE @ VECTORS_BASE, PL0 NA, enabled + beq 3f @ Memory-map not unified + setup_region r0, r5, r6, MPU_INSTR_SIDE @ VECTORS_BASE, PL0 NA, enabled +3: isb + /* Enable the MPU */ mrc p15, 0, r0, c1, c0, 0 @ Read SCTLR bic r0, r0, #CR_BR @ Disable the 'default mem-map'
arch/arm/kernel/signal.c +7 -2
@@ -392,14 +392,19 @@ setup_return(struct pt_regs *regs, struct ksignal *ksig,if (ksig->ka.sa.sa_flags &SA_SIGINFO ) idx +=3 ; +/* +* Put the sigreturn code on the stack no matter which return +* mechanism we use in order to remain ABI compliant +*/ if (__put_user (sigreturn_codes[idx], rc) ||__put_user (sigreturn_codes[idx+1 ], rc+1 ))return 1 ; -if (cpsr &MODE32_BIT ) { +if ((cpsr &MODE32_BIT ) && !IS_ENABLED (CONFIG_ARM_MPU )) {/* * 32-bit code can use the new high-page -* signal return code support. +* signal return code support except when the MPU has +* protected the vectors page from PL0 */ retcode =KERN_SIGRETURN_CODE + (idx <<2 ) + thumb; }else {
Signature · 1 line
1.7.9.5