From 5fc34a6e8c471504b1f63fe0f7d3e729cfb2f68d Mon Sep 17 00:00:00 2001 From: Matias N Date: Mon, 18 Jan 2021 13:42:20 -0300 Subject: [PATCH] nRF52: support stack coloration --- arch/arm/src/nrf52/nrf52_start.c | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/arch/arm/src/nrf52/nrf52_start.c b/arch/arm/src/nrf52/nrf52_start.c index 06a6116820..328f308fa0 100644 --- a/arch/arm/src/nrf52/nrf52_start.c +++ b/arch/arm/src/nrf52/nrf52_start.c @@ -81,6 +81,11 @@ * Private Functions ****************************************************************************/ +#ifdef CONFIG_STACK_COLORATION +static void go_nx_start(void *pv, unsigned int nbytes) + __attribute__ ((naked, no_instrument_function, noreturn)); +#endif + /**************************************************************************** * Name: nrf52_fpuconfig * @@ -168,6 +173,47 @@ static inline void nrf52_fpuconfig(void) # define nrf52_fpuconfig() #endif +/**************************************************************************** + * Name: go_nx_start + * + * Description: + * Set the IDLE stack to the coloration value and jump into nx_start() + * + ****************************************************************************/ + +#ifdef CONFIG_STACK_COLORATION +static void go_nx_start(void *pv, unsigned int nbytes) +{ + /* Set the IDLE stack to the stack coloration value then jump to + * nx_start(). We take extreme care here because were currently + * executing on this stack. + * + * We want to avoid sneak stack access generated by the compiler. + */ + + __asm__ __volatile__ + ( + "\tmovs r1, r1, lsr #2\n" /* R1 = nwords = nbytes >> 2 */ + "\tcmp r1, #0\n" /* Check (nwords == 0) */ + "\tbeq 2f\n" /* (should not happen) */ + + "\tbic r0, r0, #3\n" /* R0 = Aligned stackptr */ + "\tmovw r2, #0xbeef\n" /* R2 = STACK_COLOR = 0xdeadbeef */ + "\tmovt r2, #0xdead\n" + + "1:\n" /* Top of the loop */ + "\tsub r1, r1, #1\n" /* R1 nwords-- */ + "\tcmp r1, #0\n" /* Check (nwords == 0) */ + "\tstr r2, [r0], #4\n" /* Save stack color word, increment stackptr */ + "\tbne 1b\n" /* Bottom of the loop */ + + "2:\n" + "\tmov r14, #0\n" /* LR = return address (none) */ + "\tb nx_start\n" /* Branch to nx_start */ + ); +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -266,9 +312,16 @@ void __start(void) showprogress('\r'); showprogress('\n'); + +#ifdef CONFIG_STACK_COLORATION + /* Set the IDLE stack to the coloration value and jump into nx_start() */ + + go_nx_start((FAR void *)&_ebss, CONFIG_IDLETHREAD_STACKSIZE); +#else nx_start(); /* Shouldn't get here */ for (; ; ); +#endif }