Boot: Set stack pointer limit for bootloader

Set/reset MSPLIM register in bootloader's main to be
able to detect a stack overflow.

Change-Id: I1598471ade26387619e506b7f7bcfd742b54000e
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/bl2_main.c b/bl2/ext/mcuboot/bl2_main.c
index 79e5eec..b63cc78 100644
--- a/bl2/ext/mcuboot/bl2_main.c
+++ b/bl2/ext/mcuboot/bl2_main.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2012-2014 Wind River Systems, Inc.
- * Copyright (c) 2017-2018 Arm Limited.
+ * Copyright (c) 2017-2019 Arm Limited.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,6 +34,13 @@
 __asm("  .global __ARM_use_no_argv\n");
 #endif
 
+/* Macros to pick linker symbols */
+#define REGION(a, b, c) a##b##c
+#define REGION_NAME(a, b, c) REGION(a, b, c)
+#define REGION_DECLARE(a, b, c) extern uint32_t REGION_NAME(a, b, c)
+
+REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base);
+
 /* Flash device name must be specified by target */
 extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
 
@@ -85,6 +92,12 @@
 
     stdio_uninit();
 
+    /* Restore the Main Stack Pointer Limit register's reset value
+     * before passing execution to runtime firmware to make the
+     * bootloader transparent to it.
+     */
+    __set_MSPLIM(0);
+
     __set_MSP(vt->msp);
     __DSB();
     __ISB();
@@ -94,9 +107,13 @@
 
 int main(void)
 {
+    uint32_t msp_stack_bottom =
+            (uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
     struct boot_rsp rsp;
     int rc;
 
+    __set_MSPLIM(msp_stack_bottom);
+
     stdio_init();
 
     BOOT_LOG_INF("Starting bootloader");
diff --git a/platform/ext/target/mps2/an519/gcc/mps2_an519_bl2.ld b/platform/ext/target/mps2/an519/gcc/mps2_an519_bl2.ld
index bf066f6..e40adcf 100644
--- a/platform/ext/target/mps2/an519/gcc/mps2_an519_bl2.ld
+++ b/platform/ext/target/mps2/an519/gcc/mps2_an519_bl2.ld
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2018 ARM Limited
+; * Copyright (c) 2018-2019 Arm Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -171,6 +171,7 @@
     {
         . += __msp_stack_size__;
     } > RAM
+    Image$$ARM_LIB_STACK$$ZI$$Base = ADDR(.msp_stack);
     Image$$ARM_LIB_STACK$$ZI$$Limit = ADDR(.msp_stack) + SIZEOF(.msp_stack);
 
     .heap : ALIGN(8)
diff --git a/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld b/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
index 16f0d8c..6e03b29 100644
--- a/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
+++ b/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2017-2018 ARM Limited
+; * Copyright (c) 2017-2019 Arm Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -171,6 +171,7 @@
     {
         . += __msp_stack_size__;
     } > RAM
+    Image$$ARM_LIB_STACK$$ZI$$Base = ADDR(.msp_stack);
     Image$$ARM_LIB_STACK$$ZI$$Limit = ADDR(.msp_stack) + SIZEOF(.msp_stack);
 
     .heap : ALIGN(8)
diff --git a/platform/ext/target/musca_a/Device/Source/gcc/musca_bl2.ld b/platform/ext/target/musca_a/Device/Source/gcc/musca_bl2.ld
index ec3b908..afd6878 100644
--- a/platform/ext/target/musca_a/Device/Source/gcc/musca_bl2.ld
+++ b/platform/ext/target/musca_a/Device/Source/gcc/musca_bl2.ld
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2018 ARM Limited
+; * Copyright (c) 2018-2019 Arm Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -200,6 +200,7 @@
     {
         . += __msp_stack_size__;
     } > RAM
+    Image$$ARM_LIB_STACK$$ZI$$Base = ADDR(.msp_stack);
     Image$$ARM_LIB_STACK$$ZI$$Limit = ADDR(.msp_stack) + SIZEOF(.msp_stack);
 
     .heap : ALIGN(8)
diff --git a/platform/ext/target/musca_b1/Device/Source/gcc/musca_bl2.ld b/platform/ext/target/musca_b1/Device/Source/gcc/musca_bl2.ld
index c3ab950..d2afb86 100644
--- a/platform/ext/target/musca_b1/Device/Source/gcc/musca_bl2.ld
+++ b/platform/ext/target/musca_b1/Device/Source/gcc/musca_bl2.ld
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2009-2018 ARM Limited
+; * Copyright (c) 2009-2019 Arm Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -171,6 +171,7 @@
     {
         . += __msp_stack_size__;
     } > RAM
+    Image$$ARM_LIB_STACK$$ZI$$Base = ADDR(.msp_stack);
     Image$$ARM_LIB_STACK$$ZI$$Limit = ADDR(.msp_stack) + SIZEOF(.msp_stack);
 
     .heap : ALIGN(8)