[PATCH 3/4 v2] bsp/riscv: work area size based on stack pointer

Daniel Hellstrom daniel at gaisler.com
Fri Feb 12 08:50:11 UTC 2021


From: Martin Aberg <maberg at gaisler.com>

Remember the initial stack pointer in start.S. It can later be used to
determine top of RAM.
---
 bsps/riscv/include/bsp/start.h                     | 65 ++++++++++++++++++++++
 bsps/riscv/shared/start/bspgetworkarea-fromstack.c | 53 ++++++++++++++++++
 bsps/riscv/shared/start/start.S                    | 25 +++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 bsps/riscv/include/bsp/start.h
 create mode 100644 bsps/riscv/shared/start/bspgetworkarea-fromstack.c

diff --git a/bsps/riscv/include/bsp/start.h b/bsps/riscv/include/bsp/start.h
new file mode 100644
index 0000000..6c9d57d
--- /dev/null
+++ b/bsps/riscv/include/bsp/start.h
@@ -0,0 +1,65 @@
+/**
+ * @file
+ *
+ * @ingroup RTEMSBSPsRISCVSharedStart
+ *
+ * @brief RISC-V start definitions.
+ */
+
+/*
+ * Copyright (c) 2021 Cobham Gaisler AB.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef LIBBSP_RISCV_SHARED_START_H
+#define LIBBSP_RISCV_SHARED_START_H
+
+/**
+ * @defgroup RTEMSBSPsRISCVSharedStart Start Support
+ *
+ * @ingroup RTEMSBSPsRISCVShared
+ *
+ * @brief Start support.
+ *
+ * @{
+ */
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * This variable is initialized by the first CPU entering the BSP start code.
+ * The value is the stack pointer at entry.
+ */
+extern uintptr_t riscv_start_stack_pointer;
+
+#ifdef __cplusplus
+}
+#endif
+
+/** @} */
+
+#endif /* LIBBSP_RISCV_SHARED_START_H */
diff --git a/bsps/riscv/shared/start/bspgetworkarea-fromstack.c b/bsps/riscv/shared/start/bspgetworkarea-fromstack.c
new file mode 100644
index 0000000..6885a77
--- /dev/null
+++ b/bsps/riscv/shared/start/bspgetworkarea-fromstack.c
@@ -0,0 +1,53 @@
+/*
+ *  This set of routines are the BSP specific initialization
+ *  support routines.
+ *
+ *  COPYRIGHT (c) 1989-2020.
+ *  On-Line Applications Research Corporation (OAR),
+ *  Cobham Gaisler AB.
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.org/license/LICENSE.
+ */
+
+#include <bsp.h>
+#include <bsp/start.h>
+#include <bsp/bootcard.h>
+
+#include <rtems/sysinit.h>
+
+/*
+ *  These are provided by the linkcmds for ALL of the BSPs which use this file.
+ */
+extern char WorkAreaBase[];
+extern char RamEnd[];
+
+static Memory_Area _Memory_Areas[ 1 ];
+
+static void bsp_memory_initialize( void )
+{
+  char *end;
+
+  /* top of RAM inidicated by initial stack pointer */
+  end = (char *) riscv_start_stack_pointer;
+  if (end == 0) {
+    /* fall back to linker symbol if not set */
+    end = RamEnd;
+  }
+  _Memory_Initialize( &_Memory_Areas[ 0 ], WorkAreaBase, end );
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER( _Memory_Areas );
+
+const Memory_Information *_Memory_Get( void )
+{
+  return &_Memory_Information;
+}
diff --git a/bsps/riscv/shared/start/start.S b/bsps/riscv/shared/start/start.S
index 04a62a2..6f40279 100644
--- a/bsps/riscv/shared/start/start.S
+++ b/bsps/riscv/shared/start/start.S
@@ -35,6 +35,16 @@
 #include <bsp/linker-symbols.h>
 #include <bspopts.h>
 
+#if __riscv_xlen == 32
+#define PTR_ALIGN 2
+#define PTR_SIZE 4
+#define PTR_VALUE .word
+#elif __riscv_xlen == 64
+#define PTR_ALIGN 3
+#define PTR_SIZE 8
+#define PTR_VALUE .dword
+#endif
+
 PUBLIC(_start)
 
 	.section	.bsp_start_text, "wax", @progbits
@@ -59,6 +69,9 @@ SYM(_start):
 	LADDR	t0, _RISCV_Exception_handler
 	csrw	mtvec, t0
 
+	/* Save stack pointer so it can mark end of work area later on */
+	mv	t3, sp
+
 	/* Load stack pointer and branch to secondary processor start if necessary */
 #ifdef RTEMS_SMP
 	LADDR	sp, _ISR_Stack_area_begin
@@ -74,6 +87,9 @@ SYM(_start):
 	LADDR	sp, _ISR_Stack_area_end
 #endif
 
+	LADDR	t0, riscv_start_stack_pointer
+	SREG	t3, 0(t0)
+
 #ifdef BSP_START_COPY_FDT_FROM_U_BOOT
 	mv	a0, a1
 	call	bsp_fdt_copy
@@ -145,3 +161,12 @@ SYM(_start):
 #endif
 
 #endif /* RTEMS_SMP */
+
+	.section	.data, "aw"
+	.align	PTR_ALIGN
+
+	.globl	riscv_start_stack_pointer
+	.type	riscv_start_stack_pointer, @object
+	.size	riscv_start_stack_pointer, PTR_SIZE
+riscv_start_stack_pointer:
+	PTR_VALUE	0
-- 
2.7.4



More information about the devel mailing list