Skip to content

Commit 34f3837

Browse files
MaureenHelmnashif
authored andcommitted
arch: arm: mpu: Lock irqs while reprogramming the nxp mpu
The nxp mpu uses the logical OR of access permissions when multiple region descriptors apply to a given memory access. This means that we must partition the sram into two non-overlapping regions to implement the mpu stack guard. This partitioning gets reconfigured at every context switch, and if an interrupt occurs during this time, it can cause a fault because we do not have a valid mpu region descriptor for the sram. This scenario was observed on frdm_k64f in tests/posix/common before commit 2895da0, which changed timing. In this case, we couldn't even print fault information to the console and the hardware would reset. It looked a lot like a watchdog reset, unless you attached a debugger to see the fault. A similar problem was fixed in commit ec424b7, but this change temporarily disabled the mpu. Fix both cases by locking interrupts during the critical sections, as this is more secure than disabling the mpu. Signed-off-by: Maureen Helm <[email protected]>
1 parent a94f2b0 commit 34f3837

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

arch/arm/core/cortex_m/mpu/nxp_mpu.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,17 @@ static int mpu_configure_regions(const struct k_mem_partition
254254

255255
#if defined(CONFIG_MPU_STACK_GUARD)
256256
if (regions[i]->attr.ap_attr == MPU_REGION_SU_RX) {
257+
unsigned int key;
257258

258259
/* Attempt to configure an MPU Stack Guard region; this
259260
* will require splitting of the underlying SRAM region
260261
* into two SRAM regions, leaving out the guard area to
261262
* be programmed afterwards.
262263
*/
264+
key = irq_lock();
263265
reg_index =
264266
mpu_sram_partitioning(reg_index, regions[i]);
267+
irq_unlock(key);
265268
}
266269
#endif /* CONFIG_MPU_STACK_GUARD */
267270

@@ -322,6 +325,8 @@ static int mpu_configure_static_mpu_regions(const struct k_mem_partition
322325
static int mpu_configure_dynamic_mpu_regions(const struct k_mem_partition
323326
*dynamic_regions[], u8_t regions_num)
324327
{
328+
unsigned int key;
329+
325330
/* Reset MPU regions inside which dynamic memory regions may
326331
* be programmed.
327332
*
@@ -330,10 +335,10 @@ static int mpu_configure_dynamic_mpu_regions(const struct k_mem_partition
330335
* This might trigger memory faults if ISRs occurring during
331336
* re-programming perform access in those areas.
332337
*/
333-
arm_core_mpu_disable();
338+
key = irq_lock();
334339
region_init(mpu_config.sram_region, (const struct nxp_mpu_region *)
335340
&mpu_config.mpu_regions[mpu_config.sram_region]);
336-
arm_core_mpu_enable();
341+
irq_unlock(key);
337342

338343
int mpu_reg_index = static_regions_num;
339344

0 commit comments

Comments
 (0)