[PATCH 12 v2] SPARC BSPs: bsp_early_malloc() routine for startup memory allocation

Daniel Hellstrom daniel at gaisler.com
Wed Mar 28 08:49:27 UTC 2012


If bsp_early_malloc() is called early during boot room will be
allocated after BSS END. If the function is called after boot
is will call malloc() instead. The returned memory is not freeable
and always 8-byte aligned.

If the bsp_early_malloc() isn't called the function is not
dragged in and the workspace will be unmodified in size.

Signed-off-by: Daniel Hellstrom <daniel at gaisler.com>
---
 c/src/lib/libbsp/sparc/erc32/Makefile.am           |    2 +-
 c/src/lib/libbsp/sparc/erc32/include/bsp.h         |    9 +++-
 c/src/lib/libbsp/sparc/leon2/Makefile.am           |    3 +-
 c/src/lib/libbsp/sparc/leon2/include/bsp.h         |    9 +++-
 c/src/lib/libbsp/sparc/leon3/Makefile.am           |    2 +-
 c/src/lib/libbsp/sparc/leon3/include/bsp.h         |    9 +++-
 c/src/lib/libbsp/sparc/shared/bspgetworkarea.c     |   12 +++--
 .../lib/libbsp/sparc/shared/startup/early_malloc.c |   47 ++++++++++++++++++++
 8 files changed, 80 insertions(+), 13 deletions(-)
 create mode 100644 c/src/lib/libbsp/sparc/shared/startup/early_malloc.c

diff --git a/c/src/lib/libbsp/sparc/erc32/Makefile.am b/c/src/lib/libbsp/sparc/erc32/Makefile.am
index cecfe14..e70310d 100644
--- a/c/src/lib/libbsp/sparc/erc32/Makefile.am
+++ b/c/src/lib/libbsp/sparc/erc32/Makefile.am
@@ -38,7 +38,7 @@ libbsp_a_SOURCES += ../../shared/bspclean.c ../../shared/bsplibc.c \
     ../../shared/bspstart.c ../../shared/bootcard.c ../../shared/bspinit.c \
     ../../shared/sbrk.c startup/setvec.c startup/spurious.c \
     startup/erc32mec.c startup/boardinit.S startup/bspidle.c \
-    startup/bspdelay.c
+    startup/bspdelay.c ../../sparc/shared/startup/early_malloc.c
 # ISR Handler
 libbsp_a_SOURCES += ../../sparc/shared/irq_asm.S
 # gnatsupp
diff --git a/c/src/lib/libbsp/sparc/erc32/include/bsp.h b/c/src/lib/libbsp/sparc/erc32/include/bsp.h
index 8655732..a88673e 100644
--- a/c/src/lib/libbsp/sparc/erc32/include/bsp.h
+++ b/c/src/lib/libbsp/sparc/erc32/include/bsp.h
@@ -15,8 +15,6 @@
  *
  *  ERC32 modifications of respective RTEMS file: COPYRIGHT (c) 1995.
  *  European Space Agency.
- *
- *  $Id$
  */
 
 #ifndef _BSP_H
@@ -81,6 +79,13 @@ void BSP_fatal_return( void );
 
 void bsp_spurious_initialize( void );
 
+/* Allocate 8-byte aligned non-freeable pre-malloc() memory. The function
+ * can be called at any time. The work-area will shrink when called before
+ * bsp_get_work_area(). malloc() is called to get memory when this function
+ * is called after bsp_get_work_area().
+ */
+void *bsp_early_malloc(int size);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/c/src/lib/libbsp/sparc/leon2/Makefile.am b/c/src/lib/libbsp/sparc/leon2/Makefile.am
index 9df3c7d..6e2697f 100644
--- a/c/src/lib/libbsp/sparc/leon2/Makefile.am
+++ b/c/src/lib/libbsp/sparc/leon2/Makefile.am
@@ -55,7 +55,8 @@ libbsp_a_SOURCES += ../../shared/bspclean.c ../../shared/bsplibc.c \
     startup/bspstart.c ../../sparc/shared/bsppretaskinghook.c \
     ../../sparc/shared/bspgetworkarea.c ../../shared/bootcard.c \
     ../../shared/sbrk.c startup/setvec.c startup/spurious.c startup/bspidle.c \
-    ../../shared/bspinit.c startup/bspdelay.c
+    ../../shared/bspinit.c startup/bspdelay.c \
+    ../../sparc/shared/startup/early_malloc.c
 # ISR Handler
 libbsp_a_SOURCES += ../../sparc/shared/irq_asm.S
 # gnatsupp
diff --git a/c/src/lib/libbsp/sparc/leon2/include/bsp.h b/c/src/lib/libbsp/sparc/leon2/include/bsp.h
index 994f89b..796290e 100644
--- a/c/src/lib/libbsp/sparc/leon2/include/bsp.h
+++ b/c/src/lib/libbsp/sparc/leon2/include/bsp.h
@@ -15,8 +15,6 @@
  *
  *  ERC32 modifications of respective RTEMS file: COPYRIGHT (c) 1995.
  *  European Space Agency.
- *
- *  $Id$
  */
 
 #ifndef _BSP_H
@@ -100,6 +98,13 @@ void BSP_fatal_return( void );
 
 void bsp_spurious_initialize( void );
 
+/* Allocate 8-byte aligned non-freeable pre-malloc() memory. The function
+ * can be called at any time. The work-area will shrink when called before
+ * bsp_get_work_area(). malloc() is called to get memory when this function
+ * is called after bsp_get_work_area().
+ */
+void *bsp_early_malloc(int size);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/c/src/lib/libbsp/sparc/leon3/Makefile.am b/c/src/lib/libbsp/sparc/leon3/Makefile.am
index 8eae306..dc24051 100644
--- a/c/src/lib/libbsp/sparc/leon3/Makefile.am
+++ b/c/src/lib/libbsp/sparc/leon3/Makefile.am
@@ -38,7 +38,7 @@ libbsp_a_SOURCES += ../../shared/bspclean.c ../../shared/bsplibc.c \
     ../../sparc/shared/bsppretaskinghook.c ../../shared/bsppredriverhook.c \
     ../../sparc/shared/bspgetworkarea.c ../../shared/sbrk.c startup/setvec.c \
     startup/spurious.c startup/bspidle.S startup/bspdelay.c \
-    ../../shared/bspinit.c
+    ../../shared/bspinit.c ../../sparc/shared/startup/early_malloc.c
 
 # ISR Handler
 libbsp_a_SOURCES += ../../sparc/shared/irq_asm.S
diff --git a/c/src/lib/libbsp/sparc/leon3/include/bsp.h b/c/src/lib/libbsp/sparc/leon3/include/bsp.h
index 421d850..07a7412 100644
--- a/c/src/lib/libbsp/sparc/leon3/include/bsp.h
+++ b/c/src/lib/libbsp/sparc/leon3/include/bsp.h
@@ -15,8 +15,6 @@
  *
  *  ERC32 modifications of respective RTEMS file: COPYRIGHT (c) 1995.
  *  European Space Agency.
- *
- *  $Id$
  */
 
 #ifndef _BSP_H
@@ -110,6 +108,13 @@ void BSP_fatal_return( void );
 
 void bsp_spurious_initialize( void );
 
+/* Allocate 8-byte aligned non-freeable pre-malloc() memory. The function
+ * can be called at any time. The work-area will shrink when called before
+ * bsp_get_work_area(). malloc() is called to get memory when this function
+ * is called after bsp_get_work_area().
+ */
+void *bsp_early_malloc(int size);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/c/src/lib/libbsp/sparc/shared/bspgetworkarea.c b/c/src/lib/libbsp/sparc/shared/bspgetworkarea.c
index c6e1491..45d3d0e 100644
--- a/c/src/lib/libbsp/sparc/shared/bspgetworkarea.c
+++ b/c/src/lib/libbsp/sparc/shared/bspgetworkarea.c
@@ -8,8 +8,6 @@
  *  The license and distribution terms for this file may be
  *  found in the file LICENSE in this distribution or at
  *  http://www.rtems.com/license/LICENSE.
- *
- *  $Id$
  */
 
 /* #define BSP_GET_WORK_AREA_DEBUG */
@@ -22,6 +20,9 @@
 /* Tells us where to put the workspace in case remote debugger is present.  */
 extern uint32_t rdb_start;
 
+/* Must be aligned to 8, _end is aligned to 8 */
+unsigned int early_mem = (unsigned int)&end;
+
 /*
  *  This method returns the base address and size of the area which
  *  is to be allocated between the RTEMS Workspace and the C Program
@@ -37,8 +38,11 @@ void bsp_get_work_area(
   /* must be identical to STACK_SIZE in start.S */
   #define STACK_SIZE (16 * 1024)
 
-  *work_area_start = &end;
-  *work_area_size  = (void *)rdb_start - (void *)&end - STACK_SIZE;
+  /* Early dynamic memory allocator is placed just above _end  */
+  *work_area_start = (void *)early_mem;
+  *work_area_size  = (void *)rdb_start - (void *)early_mem - STACK_SIZE;
+  early_mem        = ~0; /* Signal bsp_early_malloc not to be used anymore */
+
   *heap_start      = BSP_BOOTCARD_HEAP_USES_WORK_AREA;
   *heap_size       = BSP_BOOTCARD_HEAP_SIZE_DEFAULT;
 
diff --git a/c/src/lib/libbsp/sparc/shared/startup/early_malloc.c b/c/src/lib/libbsp/sparc/shared/startup/early_malloc.c
new file mode 100644
index 0000000..1f968da
--- /dev/null
+++ b/c/src/lib/libbsp/sparc/shared/startup/early_malloc.c
@@ -0,0 +1,47 @@
+/*
+ *  Early dynamic memory allocation (not freeable) for BSP
+ *  boot routines. Minimum alignment 8 bytes. Memory is
+ *  allocated after _end, it will shrink the workspace.
+ *
+ *  COPYRIGHT (c) 2011.
+ *  Aeroflex 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.com/license/LICENSE.
+ */
+
+#include <bsp.h>
+#include <stdlib.h>
+
+/* Tells us where to put the workspace in case remote debugger is present. */
+extern uint32_t rdb_start;
+
+/* Must be aligned to 8 */
+extern unsigned int early_mem;
+
+/* must be identical to STACK_SIZE in start.S */
+#define STACK_SIZE (16 * 1024)
+
+/* Allocate 8-byte aligned non-freeable pre-malloc() memory. The function
+ * can be called at any time. The work-area will shrink when called before
+ * bsp_get_work_area(). malloc() is called to get memory when this function
+ * is called after bsp_get_work_area().
+ */
+void *bsp_early_malloc(int size)
+{
+	void *start;
+
+	/* Not early anymore? */
+	if (early_mem == ~0)
+		return malloc(size);
+
+	size = (size + 7) & ~0x7;
+	if (rdb_start - STACK_SIZE - early_mem < size)
+		return NULL;
+
+	start = (void *)early_mem;
+	early_mem += size;
+
+	return start;
+}
-- 
1.7.0.4




More information about the devel mailing list