From 22b5de86a521b87131ddfd27eca67723836e7be2 Mon Sep 17 00:00:00 2001 From: Ian Martin <ian@locicontrols.com> Date: Mon, 24 Oct 2016 12:40:44 -0400 Subject: [PATCH] add mpu_stack_guard pseudomodule (just Cortex-M for now) triggers an exception during stack overflow, but at a cost of 32-63 bytes of RAM per thread. --- Makefile.pseudomodules | 1 + cpu/cortexm_common/vectors_cortexm.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Makefile.pseudomodules b/Makefile.pseudomodules index e90712cf7d..9e02789345 100644 --- a/Makefile.pseudomodules +++ b/Makefile.pseudomodules @@ -34,6 +34,7 @@ PSEUDOMODULES += lwip_stats PSEUDOMODULES += lwip_tcp PSEUDOMODULES += lwip_udp PSEUDOMODULES += lwip_udplite +PSEUDOMODULES += mpu_stack_guard PSEUDOMODULES += netdev_default PSEUDOMODULES += netif PSEUDOMODULES += netstats diff --git a/cpu/cortexm_common/vectors_cortexm.c b/cpu/cortexm_common/vectors_cortexm.c index a5b5c38129..74e584cd27 100644 --- a/cpu/cortexm_common/vectors_cortexm.c +++ b/cpu/cortexm_common/vectors_cortexm.c @@ -28,9 +28,14 @@ #include "cpu.h" #include "kernel_init.h" #include "board.h" +#include "mpu.h" #include "panic.h" #include "vectors_cortexm.h" +#ifndef SRAM_BASE +#define SRAM_BASE 0 +#endif + /** * @brief Memory markers, defined in the linker script * @{ @@ -48,6 +53,8 @@ extern uint32_t _sram; extern uint32_t _eram; /** @} */ +static const uintptr_t stack_base = (uintptr_t)&_sstack; + /** * @brief Allocation of the interrupt stack */ @@ -94,6 +101,18 @@ void reset_handler_default(void) *(dst++) = 0; } +#ifdef MODULE_MPU_STACK_GUARD + if (stack_base != SRAM_BASE) { + mpu_configure( + 0, /* MPU region 0 */ + stack_base + 31, /* Base Address (rounded up) */ + MPU_ATTR(1, AP_RO_RO, 0, 1, 0, 1, MPU_SIZE_32B) /* Attributes and Size */ + ); + + mpu_enable(); + } +#endif + post_startup(); /* initialize the board (which also initiates CPU initialization) */ -- GitLab