]> Git Repo - J-u-boot.git/blobdiff - common/init/board_init.c
Merge https://gitlab.denx.de/u-boot/custodians/u-boot-riscv
[J-u-boot.git] / common / init / board_init.c
index 4a391beba98092ab93715ca86973580170afcbe6..8c8a5eac05717aa7cb665c565c9c0bffaa93e884 100644 (file)
@@ -1,13 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Code shared between SPL and U-Boot proper
  *
  * Copyright (c) 2015 Google, Inc
  * Written by Simon Glass <[email protected]>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <bootstage.h>
+#include <init.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -19,6 +20,36 @@ __weak void arch_setup_gd(struct global_data *gd_ptr)
 }
 #endif /* !CONFIG_X86 && !CONFIG_ARM */
 
+/**
+ * This function is called from board_init_f_init_reserve to set up
+ * gd->start_addr_sp for stack protection if not already set otherwise
+ */
+__weak void board_init_f_init_stack_protection_addr(ulong base)
+{
+#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
+       /* set up stack pointer for stack usage if not set yet */
+       if (!gd->start_addr_sp)
+               gd->start_addr_sp = base;
+#endif
+}
+
+/**
+ * This function is called after the position of the initial stack is
+ * determined in gd->start_addr_sp. Boards can override it to set up
+ * stack-checking markers.
+ */
+__weak void board_init_f_init_stack_protection(void)
+{
+#if CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE)
+       ulong stack_bottom = gd->start_addr_sp -
+               CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK);
+
+       /* substact some safety margin (0x20) since stack is in use here */
+       memset((void *)stack_bottom, CONFIG_VAL(SYS_STACK_F_CHECK_BYTE),
+              CONFIG_VAL(SIZE_LIMIT_PROVIDE_STACK) - 0x20);
+#endif
+}
+
 /*
  * Allocate reserved space for use as 'globals' from 'top' address and
  * return 'bottom' address of allocated space
@@ -113,6 +144,10 @@ void board_init_f_init_reserve(ulong base)
 #if !defined(CONFIG_ARM)
        arch_setup_gd(gd_ptr);
 #endif
+
+       if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
+               board_init_f_init_stack_protection_addr(base);
+
        /* next alloc will be higher by one GD plus 16-byte alignment */
        base += roundup(sizeof(struct global_data), 16);
 
@@ -124,9 +159,10 @@ void board_init_f_init_reserve(ulong base)
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
        /* go down one 'early malloc arena' */
        gd->malloc_base = base;
-       /* next alloc will be higher by one 'early malloc arena' size */
-       base += CONFIG_VAL(SYS_MALLOC_F_LEN);
 #endif
+
+       if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
+               board_init_f_init_stack_protection();
 }
 
 /*
This page took 0.029677 seconds and 4 git commands to generate.