diff --git a/core/include/thread.h b/core/include/thread.h index 89b179dfd..17e51dea2 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -189,7 +189,7 @@ struct _thread { msg_t *msg_array; /**< memory holding messages */ #endif -#if defined DEVELHELP || defined(SCHED_TEST_STACK) +#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD) char *stack_start; /**< thread's stack start address */ #endif #ifdef DEVELHELP diff --git a/core/sched.c b/core/sched.c index 30f5fb3c4..d7114ba99 100644 --- a/core/sched.c +++ b/core/sched.c @@ -29,6 +29,10 @@ #include "irq.h" #include "log.h" +#ifdef MODULE_MPU_STACK_GUARD +#include "mpu.h" +#endif + #ifdef MODULE_SCHEDSTATISTICS #include "xtimer.h" #endif @@ -115,6 +119,16 @@ int __attribute__((used)) sched_run(void) sched_active_pid = next_thread->pid; sched_active_thread = (volatile thread_t *) next_thread; +#ifdef MODULE_MPU_STACK_GUARD + mpu_configure( + 1, /* MPU region 1 */ + (uintptr_t)sched_active_thread->stack_start + 31, /* Base Address (rounded up) */ + MPU_ATTR(1, AP_RO_RO, 0, 1, 0, 1, MPU_SIZE_32B) /* Attributes and Size */ + ); + + mpu_enable(); +#endif + DEBUG("sched_run: done, changed sched_active_thread.\n"); return 1; diff --git a/core/thread.c b/core/thread.c index ca50b3e76..0009463ff 100644 --- a/core/thread.c +++ b/core/thread.c @@ -211,7 +211,7 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags, cb->pid = pid; cb->sp = thread_stack_init(function, arg, stack, stacksize); -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) +#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD) cb->stack_start = stack; #endif diff --git a/tests/sizeof_tcb/main.c b/tests/sizeof_tcb/main.c index 33eb85cd8..fe30ab011 100644 --- a/tests/sizeof_tcb/main.c +++ b/tests/sizeof_tcb/main.c @@ -48,7 +48,7 @@ int main(void) #ifdef DEVELHELP P(name); #endif -#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) +#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD) P(stack_start); #endif