[rtems commit] bsps: Rework work area initialization

Sebastian Huber sebh at rtems.org
Tue Feb 4 06:03:21 UTC 2020


Module:    rtems
Branch:    master
Commit:    eea21eaca117ecd98afea164e1808d6530ef487f
Changeset: http://git.rtems.org/rtems/commit/?id=eea21eaca117ecd98afea164e1808d6530ef487f

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Dec 13 06:18:36 2019 +0100

bsps: Rework work area initialization

The work area initialization was done by the BSP through
bsp_work_area_initialize(). This approach predated the system
initialization through the system initialization linker set. The
workspace and C program heap were unconditionally initialized.  The aim
is to support RTEMS application configurations which do not need the
workspace and C program heap.  In these configurations, the workspace
and C prgram heap should not get initialized.

Change all bsp_work_area_initialize() to implement _Memory_Get()
instead.  Move the dirty memory, sbrk(), per-CPU data, workspace, and
malloc() heap initialization into separate system initialization steps.
This makes it also easier to test the individual initialization steps.

This change adds a dependency to _Heap_Extend() to all BSPs.  This
dependency will be removed in a follow up change.

Update #3838.

---

 bsps/arm/altera-cyclone-v/start/bspgetworkarea.c |  87 +++++++++-------
 bsps/arm/imx/start/bspstarthooks.c               |  28 ++++--
 bsps/arm/raspberrypi/start/bspstarthooks.c       |  30 ++++--
 bsps/i386/pc386/start/bspgetworkarea.c           |  35 +++++--
 bsps/include/bsp/bootcard.h                      |  62 +-----------
 bsps/powerpc/mpc55xxevb/start/bspgetworkarea.c   |  27 ++---
 bsps/powerpc/qoriq/start/mmu-config.c            |  27 ++++-
 bsps/powerpc/shared/start/bspgetworkarea.c       |  30 ++++--
 bsps/powerpc/shared/start/sbrk.c                 |  63 +++++++++---
 bsps/powerpc/tqm8xx/start/bspgetworkarea.c       |  26 ++++-
 bsps/shared/start/bootcard.c                     |  11 +-
 bsps/shared/start/bspgetworkarea-default.c       |  46 ++++++---
 bsps/sparc/shared/start/bspgetworkarea.c         |  36 +++++--
 cpukit/include/rtems/confdefs.h                  |  15 ---
 cpukit/include/rtems/malloc.h                    |   6 +-
 cpukit/include/rtems/score/wkspace.h             |   9 +-
 cpukit/include/rtems/sysinit.h                   |   7 +-
 cpukit/libcsupport/src/malloc_initialize.c       |  57 ++++++++---
 cpukit/sapi/src/exinit.c                         |  13 +++
 cpukit/score/src/smp.c                           |  42 ++++++++
 cpukit/score/src/wkspace.c                       | 123 ++++-------------------
 testsuites/libtests/malloc04/init.c              |  32 +++---
 testsuites/smptests/smpfatal09/init.c            |  33 +++++-
 testsuites/sptests/spfatal09/init.c              |  34 ++++++-
 testsuites/sptests/spfatal12/init.c              |  30 +++++-
 testsuites/sptests/spsysinit01/init.c            |  12 +--
 26 files changed, 550 insertions(+), 371 deletions(-)

diff --git a/bsps/arm/altera-cyclone-v/start/bspgetworkarea.c b/bsps/arm/altera-cyclone-v/start/bspgetworkarea.c
index 5e1b473..87d512b 100644
--- a/bsps/arm/altera-cyclone-v/start/bspgetworkarea.c
+++ b/bsps/arm/altera-cyclone-v/start/bspgetworkarea.c
@@ -5,7 +5,7 @@
  */
 
 /*
- * Copyright (c) 2017 embedded brains GmbH
+ * Copyright (c) 2017, 2019 embedded brains GmbH
  *
  * The license and distribution terms for this file may be
  * found in the file LICENSE in this distribution or at
@@ -13,15 +13,18 @@
  */
 
 #include <bsp/bootcard.h>
-#include <bsp/arm-cp15-start.h>
 #include <bsp/fdt.h>
 #include <bsp/linker-symbols.h>
 
+#ifdef BSP_FDT_IS_SUPPORTED
+
+#include <bsp/arm-cp15-start.h>
+
 #include <libcpu/arm-cp15.h>
 
 #include <libfdt.h>
 
-#ifdef BSP_FDT_IS_SUPPORTED
+#include <rtems/sysinit.h>
 
 #define AREA_COUNT_MAX 16
 
@@ -29,7 +32,7 @@ static const char memory_path[] = "/memory";
 
 static const char reserved_memory_path[] = "/reserved-memory";
 
-static void adjust_memory_size(const void *fdt, Heap_Area *area)
+static void adjust_memory_size(const void *fdt, Memory_Area *area)
 {
   int node;
 
@@ -70,13 +73,13 @@ static void adjust_memory_size(const void *fdt, Heap_Area *area)
         && (uintptr_t) bsp_section_nocache_end
           < (uintptr_t) bsp_section_work_end
     ) {
-      area->size += size - (uintptr_t) bsp_section_work_end;
+      _Memory_Set_end(area, (void *) (begin + size));
     }
   }
 }
 
-static Heap_Area *find_area(
-  Heap_Area *areas,
+static Memory_Area *find_area(
+  Memory_Area *areas,
   size_t area_count,
   uint32_t begin
 )
@@ -87,8 +90,8 @@ static Heap_Area *find_area(
     uintptr_t b;
     uintptr_t e;
 
-    b = (uintptr_t) areas[i].begin;
-    e = b + (uintptr_t) areas[i].size;
+    b = (uintptr_t) _Memory_Get_begin(&areas[i]);
+    e = (uintptr_t) _Memory_Get_end(&areas[i]);
 
     if (b <= begin && begin < e) {
       return &areas[i];
@@ -100,7 +103,7 @@ static Heap_Area *find_area(
 
 static size_t remove_reserved_memory(
   const void *fdt,
-  Heap_Area *areas,
+  Memory_Area *areas,
   size_t area_count
 )
 {
@@ -118,11 +121,10 @@ static size_t remove_reserved_memory(
     while (node >= 0) {
       int len;
       const void *val;
-      uintptr_t area_begin;
       uintptr_t area_end;
       uintptr_t hole_begin;
       uintptr_t hole_end;
-      Heap_Area *area;
+      Memory_Area *area;
 
       val = fdt_getprop(fdt, node, "reg", &len);
       if (len == 8) {
@@ -133,9 +135,8 @@ static size_t remove_reserved_memory(
       }
 
       area = find_area(areas, area_count, hole_begin);
-      area_begin = (uintptr_t) area->begin;
-      area_end = area_begin + (uintptr_t) area->size;
-      area->size = hole_begin - area_begin;
+      area_end = (uintptr_t) _Memory_Get_end(area);
+      _Memory_Set_end(area, (void *) hole_end);
 
       if (hole_end <= area_end) {
         if (area_count >= AREA_COUNT_MAX) {
@@ -144,8 +145,7 @@ static size_t remove_reserved_memory(
 
         area = &areas[area_count];
         ++area_count;
-        area->begin = (void *) hole_end;
-        area->size = area_end - hole_end;
+        _Memory_Initialize(area, (void *) hole_end, (void *) area_end);
       }
 
       node = fdt_next_subnode(fdt, node);
@@ -155,39 +155,52 @@ static size_t remove_reserved_memory(
   return area_count;
 }
 
-#else /* !BSP_FDT_IS_SUPPORTED */
+static Memory_Area _Memory_Areas[AREA_COUNT_MAX];
 
-#define AREA_COUNT_MAX 1
-
-#endif /* BSP_FDT_IS_SUPPORTED */
-
-void bsp_work_area_initialize(void)
+static void bsp_memory_initialize(void)
 {
-  Heap_Area areas[AREA_COUNT_MAX];
   size_t area_count;
-#ifdef BSP_FDT_IS_SUPPORTED
   const void *fdt;
   size_t i;
-#endif
 
-  areas[0].begin = bsp_section_work_begin;
-  areas[0].size = (uintptr_t) bsp_section_work_size;
-  area_count = 1;
+  _Memory_Initialize(
+    &_Memory_Areas[0],
+    bsp_section_work_begin,
+    bsp_section_work_end
+  );
 
-#ifdef BSP_FDT_IS_SUPPORTED
+  area_count = 1;
   fdt = bsp_fdt_get();
-
-  adjust_memory_size(fdt, &areas[0]);
-  area_count = remove_reserved_memory(fdt, areas, area_count);
+  adjust_memory_size(fdt, &_Memory_Areas[0]);
+  area_count = remove_reserved_memory(fdt, &_Memory_Areas[0], area_count);
 
   for (i = 0; i < area_count; ++i) {
     arm_cp15_set_translation_table_entries(
-      areas[i].begin,
-      (void *) ((uintptr_t) areas[i].begin + areas[i].size),
+      _Memory_Get_begin(&_Memory_Areas[i]),
+      _Memory_Get_end(&_Memory_Areas[i]),
       ARMV7_MMU_READ_WRITE_CACHED
     );
   }
-#endif
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+#else /* !BSP_FDT_IS_SUPPORTED */
+
+static Memory_Area _Memory_Areas[] = {
+  MEMORY_INITIALIZER(bsp_section_work_begin, bsp_section_work_end)
+};
+
+#endif /* BSP_FDT_IS_SUPPORTED */
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER( _Memory_Areas );
 
-  bsp_work_area_initialize_with_table(areas, area_count);
+const Memory_Information *_Memory_Get( void )
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/arm/imx/start/bspstarthooks.c b/bsps/arm/imx/start/bspstarthooks.c
index 30e03dd..868da5a 100644
--- a/bsps/arm/imx/start/bspstarthooks.c
+++ b/bsps/arm/imx/start/bspstarthooks.c
@@ -22,6 +22,8 @@
 #include <bsp/arm-cp15-start.h>
 #include <bsp/arm-a9mpcore-start.h>
 
+#include <rtems/sysinit.h>
+
 #include <libfdt.h>
 
 BSP_START_DATA_SECTION static arm_cp15_start_section_config
@@ -106,13 +108,27 @@ BSP_START_TEXT_SECTION void bsp_start_hook_1(void)
   bsp_start_clear_bss();
 }
 
-void bsp_work_area_initialize(void)
+static Memory_Area _Memory_Areas[1];
+
+static void bsp_memory_initialize(void)
 {
-  uintptr_t begin;
-  uintptr_t end;
+  _Memory_Initialize(
+    &_Memory_Areas[0],
+    imx_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].begin,
+    imx_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].end
+  );
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
 
-  begin = imx_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].begin;
-  end = imx_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].end;
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
 
-  bsp_work_area_initialize_default((void *) begin, end - begin);
+const Memory_Information *_Memory_Get(void)
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/arm/raspberrypi/start/bspstarthooks.c b/bsps/arm/raspberrypi/start/bspstarthooks.c
index c2a9707..fd6aa53 100644
--- a/bsps/arm/raspberrypi/start/bspstarthooks.c
+++ b/bsps/arm/raspberrypi/start/bspstarthooks.c
@@ -31,6 +31,8 @@
 #include <bsp/linker-symbols.h>
 #include <bsp/arm-cp15-start.h>
 
+#include <rtems/sysinit.h>
+
 #ifdef RTEMS_SMP
 #include <rtems/score/smp.h>
 #endif
@@ -176,15 +178,27 @@ void BSP_START_TEXT_SECTION bsp_start_hook_1(void)
   rpi_video_init();
 }
 
-void bsp_work_area_initialize(void)
+static Memory_Area _Memory_Areas[1];
+
+static void bsp_memory_initialize(void)
 {
-  uintptr_t begin;
-  uintptr_t end;
+  _Memory_Initialize(
+    &_Memory_Areas[0],
+    raspberrypi_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].begin,
+    raspberrypi_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX].end
+  );
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
 
-  begin = raspberrypi_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX]
-          .begin;
-  end = raspberrypi_mmu_config_table[ARMV7_CP15_START_WORKSPACE_ENTRY_INDEX]
-        .end;
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
 
-  bsp_work_area_initialize_default((void *) begin, end - begin);
+const Memory_Information *_Memory_Get(void)
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/i386/pc386/start/bspgetworkarea.c b/bsps/i386/pc386/start/bspgetworkarea.c
index f869c38..4838398 100644
--- a/bsps/i386/pc386/start/bspgetworkarea.c
+++ b/bsps/i386/pc386/start/bspgetworkarea.c
@@ -1,8 +1,4 @@
 /*
- *  This routine is an implementation of the bsp_work_area_initialize()
- *  that can be used by all m68k BSPs following linkcmds conventions
- *  regarding heap, stack, and workspace allocation.
- *
  *  COPYRIGHT (c) 1989-2008.
  *  On-Line Applications Research Corporation (OAR).
  *
@@ -15,6 +11,8 @@
 #include <bsp.h>
 #include <bsp/bootcard.h>
 
+#include <rtems/sysinit.h>
+
 #ifdef BSP_GET_WORK_AREA_DEBUG
   #include <rtems/bspIo.h>
 #endif
@@ -116,17 +114,32 @@ static void bsp_size_memory(void)
   bsp_mem_size = topAddr;
 }
 
-void bsp_work_area_initialize(void)
-{
-  void *area_start;
-  uintptr_t area_size;
+static Memory_Area _Memory_Areas[ 1 ];
 
+static void bsp_memory_initialize( void )
+{
   /*
    *  We need to determine how much memory there is in the system.
    */
   bsp_size_memory();
 
-  area_start = (void *) rtemsWorkAreaStart;
-  area_size  = (uintptr_t) bsp_mem_size - (uintptr_t) rtemsWorkAreaStart;
-  bsp_work_area_initialize_default( area_start, area_size );
+  _Memory_Initialize_by_size(
+    &_Memory_Areas[0],
+    (void *) rtemsWorkAreaStart,
+    (uintptr_t) bsp_mem_size - (uintptr_t) rtemsWorkAreaStart
+  );
+}
+
+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/include/bsp/bootcard.h b/bsps/include/bsp/bootcard.h
index 6121ff9..e3eed8d 100644
--- a/bsps/include/bsp/bootcard.h
+++ b/bsps/include/bsp/bootcard.h
@@ -21,11 +21,10 @@
 #ifndef LIBBSP_SHARED_BOOTCARD_H
 #define LIBBSP_SHARED_BOOTCARD_H
 
-#include <string.h>
-
 #include <rtems/config.h>
 #include <rtems/bspIo.h>
 #include <rtems/malloc.h>
+#include <rtems/score/memory.h>
 #include <rtems/score/wkspace.h>
 
 #include <bspopts.h>
@@ -71,65 +70,6 @@ void bsp_reset(void);
  */
 void boot_card(const char *cmdline) RTEMS_NO_RETURN;
 
-#ifdef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK
-  /**
-   * @brief Gives the BSP a chance to reduce the work area size with sbrk()
-   * adding more later.
-   *
-   * bsp_sbrk_init() may reduce the work area size passed in. The routine
-   * returns the 'sbrk_amount' to be used when extending the heap.  Note that
-   * the return value may be zero.
-   *
-   * In case the @a area size is altered, then the remaining size of the
-   * @a area must be greater than or equal to @a min_size.
-   */
-  ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size);
-#endif
-
-static inline void bsp_work_area_initialize_default(
-  void *area_begin,
-  uintptr_t area_size
-)
-{
-  Heap_Area area = {
-    .begin = area_begin,
-    .size = area_size
-  };
-
-  #if BSP_DIRTY_MEMORY == 1
-    memset(area.begin, 0xCF,  area.size);
-  #endif
-
-  #ifdef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK
-    {
-      uintptr_t overhead = _Heap_Area_overhead(CPU_HEAP_ALIGNMENT);
-      uintptr_t work_space_size = rtems_configuration_get_work_space_size();
-      ptrdiff_t sbrk_amount = bsp_sbrk_init(
-        &area,
-        work_space_size
-          + overhead
-          + (rtems_configuration_get_unified_work_area() ? 0 : overhead)
-      );
-
-      rtems_heap_set_sbrk_amount(sbrk_amount);
-    }
-  #endif
-
-  _Workspace_Handler_initialization(&area, 1, NULL);
-  RTEMS_Malloc_Initialize(&area, 1, NULL);
-}
-
-static inline void bsp_work_area_initialize_with_table(
-  Heap_Area *areas,
-  size_t area_count
-)
-{
-  _Workspace_Handler_initialization(areas, area_count, _Heap_Extend);
-  RTEMS_Malloc_Initialize(areas, area_count, _Heap_Extend);
-}
-
-void bsp_work_area_initialize(void);
-
 struct Per_CPU_Control;
 
 /**
diff --git a/bsps/powerpc/mpc55xxevb/start/bspgetworkarea.c b/bsps/powerpc/mpc55xxevb/start/bspgetworkarea.c
index cb7cf6c..915d13e 100644
--- a/bsps/powerpc/mpc55xxevb/start/bspgetworkarea.c
+++ b/bsps/powerpc/mpc55xxevb/start/bspgetworkarea.c
@@ -23,22 +23,17 @@
 #include <bsp/linker-symbols.h>
 
 LINKER_SYMBOL(bsp_section_work_bonus_begin);
-LINKER_SYMBOL(bsp_section_work_bonus_size);
+LINKER_SYMBOL(bsp_section_work_bonus_end);
 
-void bsp_work_area_initialize(void)
-{
-  Heap_Area areas [] = {
-    {
-      bsp_section_work_begin,
-      (uintptr_t) bsp_section_work_size
-    }, {
-      bsp_section_work_bonus_begin,
-      (uintptr_t) bsp_section_work_bonus_size
-    }
-  };
+static Memory_Area _Memory_Areas[] = {
+  MEMORY_INITIALIZER(bsp_section_work_begin, bsp_section_work_end),
+  MEMORY_INITIALIZER(bsp_section_work_bonus_begin, bsp_section_work_bonus_end)
+};
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
 
-  bsp_work_area_initialize_with_table(
-    areas,
-    sizeof(areas) / sizeof(areas [0])
-  );
+const Memory_Information *_Memory_Get(void)
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/powerpc/qoriq/start/mmu-config.c b/bsps/powerpc/qoriq/start/mmu-config.c
index d13325f..296b071 100644
--- a/bsps/powerpc/qoriq/start/mmu-config.c
+++ b/bsps/powerpc/qoriq/start/mmu-config.c
@@ -32,6 +32,7 @@
 #include <libfdt.h>
 
 #include <rtems/config.h>
+#include <rtems/sysinit.h>
 
 #define TEXT __attribute__((section(".bsp_start_text")))
 #define DATA __attribute__((section(".bsp_start_data")))
@@ -338,11 +339,29 @@ void TEXT qoriq_mmu_config(bool boot_processor, int first_tlb, int scratch_tlb)
 	qoriq_mmu_write_to_tlb1(&context, first_tlb);
 }
 
-void TEXT bsp_work_area_initialize(void)
+static Memory_Area _Memory_Areas[1];
+
+static const Memory_Information _Memory_Information =
+	MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
+
+static void bsp_memory_initialize(void)
 {
 	const entry *we = &config[WORKSPACE_ENTRY_INDEX];
-	uintptr_t begin = we->begin;
-	uintptr_t end = begin + we->size;
 
-	bsp_work_area_initialize_default((void *) begin, end - begin);
+	_Memory_Initialize_by_size(
+		&_Memory_Areas[0],
+		(void *) we->begin,
+		we->size
+	);
+}
+
+RTEMS_SYSINIT_ITEM(
+	bsp_memory_initialize,
+	RTEMS_SYSINIT_MEMORY,
+	RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+const Memory_Information *_Memory_Get(void)
+{
+	return &_Memory_Information;
 }
diff --git a/bsps/powerpc/shared/start/bspgetworkarea.c b/bsps/powerpc/shared/start/bspgetworkarea.c
index 8873ffe..3d26a41 100644
--- a/bsps/powerpc/shared/start/bspgetworkarea.c
+++ b/bsps/powerpc/shared/start/bspgetworkarea.c
@@ -9,15 +9,33 @@
 
 #include <libcpu/powerpc-utility.h>
 
+#include <rtems/sysinit.h>
+
 LINKER_SYMBOL(__rtems_end)
 
-void bsp_work_area_initialize(void)
+static Memory_Area _Memory_Areas[1];
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
+
+static void bsp_memory_initialize(void)
 {
-  uintptr_t work_size;
-  uintptr_t work_area;
+  uintptr_t size;
+  uintptr_t begin;
 
-  work_area = (uintptr_t)__rtems_end;
-  work_size = (uintptr_t)BSP_mem_size - work_area;
+  begin = (uintptr_t)__rtems_end;
+  size = (uintptr_t)BSP_mem_size - begin;
 
-  bsp_work_area_initialize_default((void *) work_area, work_size);
+  _Memory_Initialize_by_size(&_Memory_Areas[0], (void *) begin, size);
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+const Memory_Information *_Memory_Get(void)
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/powerpc/shared/start/sbrk.c b/bsps/powerpc/shared/start/sbrk.c
index f17a151..95104ba 100644
--- a/bsps/powerpc/shared/start/sbrk.c
+++ b/bsps/powerpc/shared/start/sbrk.c
@@ -64,8 +64,13 @@
 #include <errno.h>
 #include <unistd.h>
 
+#include <bsp.h>
 #include <bsp/bootcard.h>
 
+#include <rtems/sysinit.h>
+
+#ifdef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK
+
 #define INVALID_REMAINING_START ((uintptr_t) -1)
 
 static uintptr_t remaining_start = INVALID_REMAINING_START;
@@ -83,22 +88,35 @@ extern uintptr_t        BSP_sbrk_policy[] __attribute__((weak));
 
 #define LIMIT_32M  0x02000000
 
-ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size)
+/**
+ * @brief Gives the BSP a chance to reduce the work area size with sbrk()
+ * adding more later.
+ *
+ * bsp_sbrk_init() may reduce the work area size passed in. The routine
+ * returns the 'sbrk_amount' to be used when extending the heap.  Note that
+ * the return value may be zero.
+ *
+ * In case the @a mem area size is altered, then the remaining size of the
+ * @a mem area must be greater than or equal to @a min_size.
+ */
+static ptrdiff_t bsp_sbrk_init(const Memory_Information *mem, uintptr_t min_size)
 {
   uintptr_t         rval = 0;
   uintptr_t         policy;
   uintptr_t         remaining_end;
+  Memory_Area      *area;
 
-  remaining_start = (uintptr_t) area->begin;
-  remaining_size  = area->size;
-  remaining_end   = remaining_start + remaining_size;
+  area = &mem->areas[ 0 ];
+  remaining_start = (uintptr_t) _Memory_Get_free_begin(area);
+  remaining_size  = _Memory_Get_free_size(area);
+  remaining_end   = (uintptr_t) _Memory_Get_end(area);
 
   if (remaining_start < LIMIT_32M &&
       remaining_end > LIMIT_32M &&
       min_size <= LIMIT_32M - remaining_start) {
     /* clip at LIMIT_32M */
     rval = remaining_end - LIMIT_32M;
-    area->size = LIMIT_32M - remaining_start;
+    _Memory_Set_end(area, (void *) (LIMIT_32M - remaining_start));
     remaining_start = LIMIT_32M;
     remaining_size  = rval;
   }
@@ -106,9 +124,9 @@ ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size)
   policy = (0 == BSP_sbrk_policy[0] ? (uintptr_t)(-1) : BSP_sbrk_policy[0]);
   switch ( policy ) {
       case (uintptr_t)(-1):
-        area->size      += rval;
-        remaining_start  = (uintptr_t) area->begin + area->size;
-        remaining_size   = 0;
+        _Memory_Set_end(area, (const char *) _Memory_Get_end(area) + rval);
+        remaining_start = (uintptr_t) _Memory_Get_end(area);
+        remaining_size  = 0;
       break;
 
       case 0:
@@ -124,12 +142,7 @@ ptrdiff_t bsp_sbrk_init(Heap_Area *area, uintptr_t min_size)
   return (ptrdiff_t) (rval <= PTRDIFF_MAX ? rval : rval / 2);
 }
 
-/*
- * This is just so the sbrk test can force its magic. All normal applications
- * should just use the default implementation in this file.
- */
-void *sbrk(ptrdiff_t incr) __attribute__ (( weak, alias("bsp_sbrk") ));
-static void *bsp_sbrk(ptrdiff_t incr)
+void *sbrk(ptrdiff_t incr)
 {
   void *rval=(void*)-1;
 
@@ -145,3 +158,25 @@ static void *bsp_sbrk(ptrdiff_t incr)
   #endif
   return rval;
 }
+
+static void bsp_sbrk_initialize(void)
+{
+  uintptr_t overhead = _Heap_Area_overhead(CPU_HEAP_ALIGNMENT);
+  uintptr_t work_space_size = rtems_configuration_get_work_space_size();
+  ptrdiff_t sbrk_amount = bsp_sbrk_init(
+    _Memory_Get(),
+    work_space_size
+      + overhead
+      + (rtems_configuration_get_unified_work_area() ? 0 : overhead)
+  );
+
+  rtems_heap_set_sbrk_amount(sbrk_amount);
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_sbrk_initialize,
+  RTEMS_SYSINIT_SBRK,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+#endif /* CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK */
diff --git a/bsps/powerpc/tqm8xx/start/bspgetworkarea.c b/bsps/powerpc/tqm8xx/start/bspgetworkarea.c
index 4a0a4db..e5cd59e 100644
--- a/bsps/powerpc/tqm8xx/start/bspgetworkarea.c
+++ b/bsps/powerpc/tqm8xx/start/bspgetworkarea.c
@@ -24,11 +24,27 @@
 #include <bsp/bootcard.h>
 #include <bsp/linker-symbols.h>
 
-void bsp_work_area_initialize(void)
+#include <rtems/sysinit.h>
+
+static Memory_Area _Memory_Areas[1];
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER(_Memory_Areas);
+
+static void bsp_memory_initialize(void)
 {
-  char *ram_end = (char *) (TQM_BD_INFO.sdram_size - (uint32_t)TopRamReserved);
-  void *area_start = bsp_section_work_begin;
-  uintptr_t area_size = (uintptr_t) ram_end - (uintptr_t) area_start;
+  void *end = (void *) (TQM_BD_INFO.sdram_size - (uintptr_t)TopRamReserved);
 
-  bsp_work_area_initialize_default( area_start, area_size );
+  _Memory_Initialize(&_Memory_Areas[0], bsp_section_work_begin, end);
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+const Memory_Information *_Memory_Get(void)
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/shared/start/bootcard.c b/bsps/shared/start/bootcard.c
index a6ad195..2eef1ea 100644
--- a/bsps/shared/start/bootcard.c
+++ b/bsps/shared/start/bootcard.c
@@ -24,11 +24,18 @@
  */
 const char *bsp_boot_cmdline;
 
+#if BSP_DIRTY_MEMORY == 1
+static void bsp_dirty_memory(void)
+{
+  _Memory_Fill( _Memory_Get(), 0xcf );
+}
+
 RTEMS_SYSINIT_ITEM(
-  bsp_work_area_initialize,
-  RTEMS_SYSINIT_BSP_WORK_AREAS,
+  bsp_dirty_memory,
+  RTEMS_SYSINIT_DIRTY_MEMORY,
   RTEMS_SYSINIT_ORDER_MIDDLE
 );
+#endif
 
 RTEMS_SYSINIT_ITEM(
   bsp_start,
diff --git a/bsps/shared/start/bspgetworkarea-default.c b/bsps/shared/start/bspgetworkarea-default.c
index 18d4063..a7c4c04 100644
--- a/bsps/shared/start/bspgetworkarea-default.c
+++ b/bsps/shared/start/bspgetworkarea-default.c
@@ -1,7 +1,7 @@
 /**
  * @file
  *
- * This routine is an implementation of the bsp_work_area_initialize()
+ * This routine is an implementation of the _Memory_Get()
  * that can be used by all BSPs following linkcmds conventions
  * regarding heap, stack, and workspace allocation.
  */
@@ -33,23 +33,37 @@ extern char WorkAreaBase[];
  *  We may get the size information from U-Boot or the linker scripts.
  */
 #ifdef USE_UBOOT
-  #include <bsp/u-boot.h>
-#else
-  extern char RamBase[];
-  extern char RamSize[];
-#endif
+#include <bsp/u-boot.h>
+#include <rtems/sysinit.h>
+
+static Memory_Area _Memory_Areas[ 1 ];
 
-void bsp_work_area_initialize(void)
+static void bsp_memory_initialize( void )
 {
-  uintptr_t work_base = (uintptr_t) WorkAreaBase;
-  uintptr_t ram_end;
+  char *end;
+
+  end = (char *) bsp_uboot_board_info.bi_memstart
+    + bsp_uboot_board_info.bi_memsize;
+  _Memory_Initialize( &_Memory_Areas[ 0 ], WorkAreaBase, end );
+}
 
-  #ifdef USE_UBOOT
-    ram_end = (uintptr_t) bsp_uboot_board_info.bi_memstart +
-                          bsp_uboot_board_info.bi_memsize;
-  #else
-    ram_end = (uintptr_t)RamBase + (uintptr_t)RamSize;
-  #endif
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+#else /* !USE_UBOOT */
+extern char RamEnd[];
 
-  bsp_work_area_initialize_default( (void *) work_base, ram_end - work_base );
+static Memory_Area _Memory_Areas[] = {
+  MEMORY_INITIALIZER(WorkAreaBase, RamEnd)
+};
+#endif /* USE_UBOOT */
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER( _Memory_Areas );
+
+const Memory_Information *_Memory_Get( void )
+{
+  return &_Memory_Information;
 }
diff --git a/bsps/sparc/shared/start/bspgetworkarea.c b/bsps/sparc/shared/start/bspgetworkarea.c
index 8e097c8..3fbeb82 100644
--- a/bsps/sparc/shared/start/bspgetworkarea.c
+++ b/bsps/sparc/shared/start/bspgetworkarea.c
@@ -13,19 +13,33 @@
 #include <bsp.h>
 #include <bsp/bootcard.h>
 
+#include <rtems/sysinit.h>
+
 /* Tells us where to put the workspace in case remote debugger is present.  */
-extern uint32_t rdb_start;
+extern uintptr_t rdb_start;
 
-/*
- *  This method returns the base address and size of the area which
- *  is to be allocated between the RTEMS Workspace and the C Program
- *  Heap.
- */
-void bsp_work_area_initialize(void)
+static Memory_Area _Memory_Areas[ 1 ];
+
+static const Memory_Information _Memory_Information =
+  MEMORY_INFORMATION_INITIALIZER( _Memory_Areas );
+
+static void bsp_memory_initialize( void )
 {
-  /* Early dynamic memory allocator is placed just above _end  */
-  void *work_area_start = (void *)&end;
-  uintptr_t work_area_size = (uintptr_t)rdb_start - (uintptr_t)work_area_start;
+  void      *begin;
+  uintptr_t  size;
 
-  bsp_work_area_initialize_default(work_area_start, work_area_size);
+  begin = &end;
+  size = rdb_start - (uintptr_t)begin;
+  _Memory_Initialize_by_size( &_Memory_Areas[ 0 ], begin, size );
+}
+
+RTEMS_SYSINIT_ITEM(
+  bsp_memory_initialize,
+  RTEMS_SYSINIT_MEMORY,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
+const Memory_Information *_Memory_Get( void )
+{
+  return &_Memory_Information;
 }
diff --git a/cpukit/include/rtems/confdefs.h b/cpukit/include/rtems/confdefs.h
index c044f48..2605b51 100644
--- a/cpukit/include/rtems/confdefs.h
+++ b/cpukit/include/rtems/confdefs.h
@@ -1260,21 +1260,6 @@ extern rtems_initialization_tasks_table Initialization_tasks[];
 
 #ifdef CONFIGURE_INIT
   /**
-   * By default, RTEMS uses separate heaps for the RTEMS Workspace and
-   * the C Program Heap.  The application can choose optionally to combine
-   * these to provide one larger memory pool. This is particularly
-   * useful in combination with the unlimited objects configuration.
-   */
-  #ifdef CONFIGURE_UNIFIED_WORK_AREAS
-    Heap_Control  *RTEMS_Malloc_Heap = &_Workspace_Area;
-  #else
-    Heap_Control   RTEMS_Malloc_Area;
-    Heap_Control  *RTEMS_Malloc_Heap = &RTEMS_Malloc_Area;
-  #endif
-#endif
-
-#ifdef CONFIGURE_INIT
-  /**
    * This configures the sbrk() support for the malloc family.
    * By default it is assumed that the BSP provides all available
    * RAM to the malloc family implementation so sbrk()'ing to get
diff --git a/cpukit/include/rtems/malloc.h b/cpukit/include/rtems/malloc.h
index 7cdce1f..34bdbcb 100644
--- a/cpukit/include/rtems/malloc.h
+++ b/cpukit/include/rtems/malloc.h
@@ -19,6 +19,7 @@
 #include <rtems.h>
 #include <rtems/bspIo.h>
 #include <rtems/libcsupport.h> /* for malloc_walk() */
+#include <rtems/score/memory.h>
 
 #include <stdint.h>
 
@@ -43,9 +44,8 @@ extern "C" {
 extern Heap_Control *RTEMS_Malloc_Heap;
 
 void RTEMS_Malloc_Initialize(
-  const Heap_Area *areas,
-  size_t area_count,
-  Heap_Initialization_or_extend_handler extend
+  const Memory_Information              *mem,
+  Heap_Initialization_or_extend_handler  extend
 );
 
 extern ptrdiff_t RTEMS_Malloc_Sbrk_amount;
diff --git a/cpukit/include/rtems/score/wkspace.h b/cpukit/include/rtems/score/wkspace.h
index 8428c9f..8d0d3bc 100644
--- a/cpukit/include/rtems/score/wkspace.h
+++ b/cpukit/include/rtems/score/wkspace.h
@@ -24,6 +24,7 @@
 
 #include <rtems/score/heap.h>
 #include <rtems/score/interr.h>
+#include <rtems/score/memory.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -53,14 +54,12 @@ extern Heap_Control _Workspace_Area;
  *
  * This routine performs the initialization necessary for this handler.
  *
- * @param areas The heap area for the new workspace.
- * @param area_count The number of areas for the allocation.
+ * @param mem The memory information
  * @param extend The extension handler for the new workspace.
  */
 void _Workspace_Handler_initialization(
-  Heap_Area *areas,
-  size_t area_count,
-  Heap_Initialization_or_extend_handler extend
+  const Memory_Information              *mem,
+  Heap_Initialization_or_extend_handler  extend
 );
 
 /**
diff --git a/cpukit/include/rtems/sysinit.h b/cpukit/include/rtems/sysinit.h
index 087de59..7edd313 100644
--- a/cpukit/include/rtems/sysinit.h
+++ b/cpukit/include/rtems/sysinit.h
@@ -28,8 +28,13 @@ extern "C" {
  */
 #define RTEMS_SYSINIT_RECORD                     000100
 #define RTEMS_SYSINIT_BSP_EARLY                  000140
+#define RTEMS_SYSINIT_MEMORY                     000180
+#define RTEMS_SYSINIT_DIRTY_MEMORY               0001c0
 #define RTEMS_SYSINIT_ISR_STACK                  000200
-#define RTEMS_SYSINIT_BSP_WORK_AREAS             000200
+#define RTEMS_SYSINIT_PER_CPU_DATA               000220
+#define RTEMS_SYSINIT_SBRK                       000240
+#define RTEMS_SYSINIT_WORKSPACE                  000260
+#define RTEMS_SYSINIT_MALLOC                     000280
 #define RTEMS_SYSINIT_BSP_START                  000300
 #define RTEMS_SYSINIT_CPU_COUNTER                000400
 #define RTEMS_SYSINIT_INITIAL_EXTENSIONS         000500
diff --git a/cpukit/libcsupport/src/malloc_initialize.c b/cpukit/libcsupport/src/malloc_initialize.c
index dc94b48..520960d 100644
--- a/cpukit/libcsupport/src/malloc_initialize.c
+++ b/cpukit/libcsupport/src/malloc_initialize.c
@@ -18,33 +18,59 @@
 #endif
 
 #include <rtems/malloc.h>
+#include <rtems/score/wkspace.h>
+#include <rtems/sysinit.h>
 
 #include "malloc_p.h"
 
+Heap_Control *RTEMS_Malloc_Heap;
+
+static void _Malloc_Initialize( void )
+{
+  RTEMS_Malloc_Initialize( _Memory_Get(), _Heap_Extend );
+}
+
+RTEMS_SYSINIT_ITEM(
+  _Malloc_Initialize,
+  RTEMS_SYSINIT_MALLOC,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
+
 #ifdef RTEMS_NEWLIB
+static Heap_Control _Malloc_Heap;
+
 void RTEMS_Malloc_Initialize(
-  const Heap_Area *areas,
-  size_t area_count,
-  Heap_Initialization_or_extend_handler extend
+  const Memory_Information              *mem,
+  Heap_Initialization_or_extend_handler  extend
 )
 {
-  Heap_Control *heap = RTEMS_Malloc_Heap;
+  if ( rtems_configuration_get_unified_work_area() ) {
+    RTEMS_Malloc_Heap = &_Workspace_Area;
+  } else {
+    Heap_Control                          *heap;
+    Heap_Initialization_or_extend_handler  init_or_extend;
+    uintptr_t                              page_size;
+    size_t                                 i;
+
+    heap = &_Malloc_Heap;
+    RTEMS_Malloc_Heap = heap;
+    init_or_extend = _Heap_Initialize;
+    page_size = CPU_HEAP_ALIGNMENT;
 
-  if ( !rtems_configuration_get_unified_work_area() ) {
-    Heap_Initialization_or_extend_handler init_or_extend = _Heap_Initialize;
-    uintptr_t page_size = CPU_HEAP_ALIGNMENT;
-    size_t i;
+    for (i = 0; i < _Memory_Get_count( mem ); ++i) {
+      Memory_Area *area;
+      uintptr_t    space_available;
 
-    for (i = 0; i < area_count; ++i) {
-      const Heap_Area *area = &areas [i];
-      uintptr_t space_available = (*init_or_extend)(
+      area = _Memory_Get_area( mem, i );
+      space_available = ( *init_or_extend )(
         heap,
-        area->begin,
-        area->size,
+        _Memory_Get_free_begin( area ),
+        _Memory_Get_free_size( area ),
         page_size
       );
 
       if ( space_available > 0 ) {
+        _Memory_Consume( area, _Memory_Get_free_size( area ) );
         init_or_extend = extend;
       }
     }
@@ -56,9 +82,8 @@ void RTEMS_Malloc_Initialize(
 }
 #else
 void RTEMS_Malloc_Initialize(
-  Heap_Area *areas,
-  size_t area_count,
-  Heap_Initialization_or_extend_handler extend
+  const Memory_Information              *mem,
+  Heap_Initialization_or_extend_handler  extend
 )
 {
   /* FIXME: Dummy function */
diff --git a/cpukit/sapi/src/exinit.c b/cpukit/sapi/src/exinit.c
index 54e4451..196c9be 100644
--- a/cpukit/sapi/src/exinit.c
+++ b/cpukit/sapi/src/exinit.c
@@ -30,6 +30,7 @@
 #include <rtems/score/heap.h>
 #include <rtems/score/interr.h>
 #include <rtems/score/isr.h>
+#include <rtems/score/percpudata.h>
 #include <rtems/score/priority.h>
 #include <rtems/score/schedulerimpl.h>
 #include <rtems/score/smpimpl.h>
@@ -57,6 +58,18 @@ _Objects_Information_table[ OBJECTS_APIS_LAST + 1 ] = {
   &_POSIX_Objects[ 0 ]
 };
 
+RTEMS_LINKER_RWSET(
+  _Per_CPU_Data,
+#if defined(RTEMS_SMP)
+  /*
+   * In SMP configurations, prevent false cache line sharing of per-processor
+   * data with a proper alignment.
+   */
+  RTEMS_ALIGNED( CPU_CACHE_LINE_BYTES )
+#endif
+  char
+);
+
 static void rtems_initialize_data_structures(void)
 {
   /*
diff --git a/cpukit/score/src/smp.c b/cpukit/score/src/smp.c
index 306d1ea..b6394a8 100644
--- a/cpukit/score/src/smp.c
+++ b/cpukit/score/src/smp.c
@@ -20,9 +20,14 @@
 
 #include <rtems/score/smpimpl.h>
 #include <rtems/score/assert.h>
+#include <rtems/score/memory.h>
+#include <rtems/score/percpudata.h>
 #include <rtems/score/schedulerimpl.h>
 #include <rtems/score/threadimpl.h>
 #include <rtems/config.h>
+#include <rtems/sysinit.h>
+
+#include <string.h>
 
 #if CPU_USE_DEFERRED_FP_SWITCH == TRUE
   #error "deferred FP switch not implemented for SMP"
@@ -252,3 +257,40 @@ void _SMP_Send_message_multicast(
     }
   }
 }
+
+static void _Per_CPU_Data_initialize( void )
+{
+  uintptr_t size;
+
+  size = RTEMS_LINKER_SET_SIZE( _Per_CPU_Data );
+
+  if ( size > 0 ) {
+    const Memory_Information *mem;
+    Per_CPU_Control          *cpu;
+    uint32_t                  cpu_index;
+    uint32_t                  cpu_max;
+
+    mem = _Memory_Get();
+    cpu = _Per_CPU_Get_by_index( 0 );
+    cpu->data = RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data );
+
+    cpu_max = rtems_configuration_get_maximum_processors();
+
+    for ( cpu_index = 1 ; cpu_index < cpu_max ; ++cpu_index ) {
+      cpu = _Per_CPU_Get_by_index( cpu_index );
+      cpu->data = _Memory_Allocate( mem, size, CPU_CACHE_LINE_BYTES );
+
+      if( cpu->data == NULL ) {
+        _Internal_error( INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA );
+      }
+
+      memcpy( cpu->data, RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data ), size);
+    }
+  }
+}
+
+RTEMS_SYSINIT_ITEM(
+  _Per_CPU_Data_initialize,
+  RTEMS_SYSINIT_PER_CPU_DATA,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
diff --git a/cpukit/score/src/wkspace.c b/cpukit/score/src/wkspace.c
index d363b8d..41c6cd3 100644
--- a/cpukit/score/src/wkspace.c
+++ b/cpukit/score/src/wkspace.c
@@ -22,11 +22,11 @@
 #include <rtems/score/assert.h>
 #include <rtems/score/heapimpl.h>
 #include <rtems/score/interr.h>
-#include <rtems/score/percpudata.h>
 #include <rtems/score/threadimpl.h>
 #include <rtems/score/tls.h>
 #include <rtems/posix/pthread.h>
 #include <rtems/config.h>
+#include <rtems/sysinit.h>
 
 #include <string.h>
 
@@ -35,18 +35,6 @@
   #include <rtems/bspIo.h>
 #endif
 
-RTEMS_LINKER_RWSET(
-  _Per_CPU_Data,
-#if defined(RTEMS_SMP)
-  /*
-   * In SMP configurations, prevent false cache line sharing of per-processor
-   * data with a proper alignment.
-   */
-  RTEMS_ALIGNED( CPU_CACHE_LINE_BYTES )
-#endif
-  char
-);
-
 Heap_Control _Workspace_Area;
 
 static uintptr_t _Workspace_Space_for_TLS( uintptr_t page_size )
@@ -84,85 +72,20 @@ static uintptr_t _Workspace_Space_for_TLS( uintptr_t page_size )
   return space;
 }
 
-#ifdef RTEMS_SMP
-static void *_Workspace_Allocate_from_areas(
-  Heap_Area *areas,
-  size_t     area_count,
-  uintptr_t  size,
-  uintptr_t  alignment
-)
+static void _Workspace_Initialize( void )
 {
-  size_t i;
-
-  for ( i = 0; i < area_count; ++i ) {
-    Heap_Area *area;
-    uintptr_t  alloc_begin;
-    uintptr_t  alloc_size;
-
-    area = &areas[ i ];
-    alloc_begin = (uintptr_t) area->begin;
-    alloc_begin = ( alloc_begin + alignment - 1 ) & ~( alignment - 1 );
-    alloc_size = size;
-    alloc_size += alloc_begin - (uintptr_t) area->begin;
-
-    if ( area->size >= alloc_size ) {
-      area->begin = (void *) ( alloc_begin + size );
-      area->size -= alloc_size;
-
-      return (void *) alloc_begin;
-    }
-  }
-
-  return NULL;
+  _Workspace_Handler_initialization( _Memory_Get(), _Heap_Extend );
 }
-#endif
-
-static void _Workspace_Allocate_per_CPU_data(
-  Heap_Area *areas,
-  size_t area_count
-)
-{
-#ifdef RTEMS_SMP
-  uintptr_t size;
 
-  size = RTEMS_LINKER_SET_SIZE( _Per_CPU_Data );
-
-  if ( size > 0 ) {
-    Per_CPU_Control *cpu;
-    uint32_t         cpu_index;
-    uint32_t         cpu_max;
-
-    cpu = _Per_CPU_Get_by_index( 0 );
-    cpu->data = RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data );
-
-    cpu_max = rtems_configuration_get_maximum_processors();
-
-    for ( cpu_index = 1 ; cpu_index < cpu_max ; ++cpu_index ) {
-      cpu = _Per_CPU_Get_by_index( cpu_index );
-      cpu->data = _Workspace_Allocate_from_areas(
-        areas,
-        area_count,
-        size,
-        CPU_CACHE_LINE_BYTES
-      );
-
-      if( cpu->data == NULL ) {
-        _Internal_error( INTERNAL_ERROR_NO_MEMORY_FOR_PER_CPU_DATA );
-      }
-
-      memcpy( cpu->data, RTEMS_LINKER_SET_BEGIN( _Per_CPU_Data ), size);
-    }
-  }
-#else
-  (void) areas;
-  (void) area_count;
-#endif
-}
+RTEMS_SYSINIT_ITEM(
+  _Workspace_Initialize,
+  RTEMS_SYSINIT_WORKSPACE,
+  RTEMS_SYSINIT_ORDER_MIDDLE
+);
 
 void _Workspace_Handler_initialization(
-  Heap_Area *areas,
-  size_t area_count,
-  Heap_Initialization_or_extend_handler extend
+  const Memory_Information              *mem,
+  Heap_Initialization_or_extend_handler  extend
 )
 {
   Heap_Initialization_or_extend_handler init_or_extend;
@@ -173,10 +96,7 @@ void _Workspace_Handler_initialization(
   uintptr_t                             overhead;
   size_t                                i;
 
-  _Workspace_Allocate_per_CPU_data( areas, area_count );
-
   page_size = CPU_HEAP_ALIGNMENT;
-
   remaining = rtems_configuration_get_work_space_size();
   remaining += _Workspace_Space_for_TLS( page_size );
 
@@ -185,25 +105,27 @@ void _Workspace_Handler_initialization(
   unified = rtems_configuration_get_unified_work_area();
   overhead = _Heap_Area_overhead( page_size );
 
-  for ( i = 0; i < area_count; ++i ) {
-    Heap_Area *area;
+  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
+    Memory_Area *area;
+    uintptr_t    free_size;
 
-    area = &areas[ i ];
+    area = _Memory_Get_area( mem, i );
+    free_size = _Memory_Get_free_size( area );
 
     if ( do_zero ) {
-      memset( area->begin, 0, area->size );
+      memset( _Memory_Get_free_begin( area ), 0, free_size );
     }
 
-    if ( area->size > overhead ) {
+    if ( free_size > overhead ) {
       uintptr_t space_available;
       uintptr_t size;
 
       if ( unified ) {
-        size = area->size;
+        size = free_size;
       } else {
         if ( remaining > 0 ) {
-          size = remaining < area->size - overhead ?
-            remaining + overhead : area->size;
+          size = remaining < free_size - overhead ?
+            remaining + overhead : free_size;
         } else {
           size = 0;
         }
@@ -211,13 +133,12 @@ void _Workspace_Handler_initialization(
 
       space_available = ( *init_or_extend )(
         &_Workspace_Area,
-        area->begin,
+        _Memory_Get_free_begin( area ),
         size,
         page_size
       );
 
-      area->begin = (char *) area->begin + size;
-      area->size -= size;
+      _Memory_Consume( area, size );
 
       if ( space_available < remaining ) {
         remaining -= space_available;
diff --git a/testsuites/libtests/malloc04/init.c b/testsuites/libtests/malloc04/init.c
index 632ea28..b568c83 100644
--- a/testsuites/libtests/malloc04/init.c
+++ b/testsuites/libtests/malloc04/init.c
@@ -78,7 +78,12 @@ rtems_task Init(
 )
 {
   Heap_Control *real_heap;
-  Heap_Area area;
+  Memory_Area area;
+  Memory_Information mem = {
+    .count = 0,
+    .areas = &area
+  };
+
   void *p;
 
   TEST_BEGIN();
@@ -93,9 +98,8 @@ rtems_task Init(
 
   sbrk_count = 0;
   offset     = 256;
-  area.begin = &Malloc_Heap [0];
-  area.size  = offset;
-  RTEMS_Malloc_Initialize( &area, 1, NULL );
+  _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset );
+  RTEMS_Malloc_Initialize( &mem, NULL );
 
   errno = 0;
   p = malloc( 256 );
@@ -109,9 +113,8 @@ rtems_task Init(
 
   sbrk_count = 0;
   offset     = 256;
-  area.begin = &Malloc_Heap [0];
-  area.size  = offset;
-  RTEMS_Malloc_Initialize( &area, 1, NULL );
+  _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset );
+  RTEMS_Malloc_Initialize( &mem, NULL );
 
   p = malloc(1);
   rtems_test_assert( p != NULL );
@@ -125,9 +128,8 @@ rtems_task Init(
 
   sbrk_count = 0;
   offset     = 256;
-  area.begin = &Malloc_Heap [0];
-  area.size  = offset;
-  RTEMS_Malloc_Initialize( &area, 1, NULL );
+  _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset );
+  RTEMS_Malloc_Initialize( &mem, NULL );
 
   errno = 0;
   p = malloc( sizeof( Malloc_Heap ) );
@@ -139,9 +141,8 @@ rtems_task Init(
 
   sbrk_count = 0;
   offset     = 256;
-  area.begin = &Malloc_Heap [0];
-  area.size  = offset;
-  RTEMS_Malloc_Initialize( &area, 1, NULL );
+  _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset );
+  RTEMS_Malloc_Initialize( &mem, NULL );
 
   p = malloc( 128 );
   rtems_test_assert( p != NULL );
@@ -155,9 +156,8 @@ rtems_task Init(
 
   sbrk_count = -1;
   offset     = 256;
-  area.begin = &Malloc_Heap [0];
-  area.size  = offset;
-  RTEMS_Malloc_Initialize( &area, 1, NULL );
+  _Memory_Initialize_by_size( &area, &Malloc_Heap[ 0 ], offset );
+  RTEMS_Malloc_Initialize( &mem, NULL );
 
   errno = 0;
   p = malloc( 256 );
diff --git a/testsuites/smptests/smpfatal09/init.c b/testsuites/smptests/smpfatal09/init.c
index ecbb54d..3ef9392 100644
--- a/testsuites/smptests/smpfatal09/init.c
+++ b/testsuites/smptests/smpfatal09/init.c
@@ -31,7 +31,8 @@
 
 #include <rtems.h>
 #include <rtems/score/percpudata.h>
-#include <rtems/score/wkspace.h>
+#include <rtems/score/memory.h>
+#include <rtems/sysinit.h>
 
 #include <tmacros.h>
 
@@ -41,7 +42,26 @@ const char rtems_test_name[] = "SMPFATAL 9";
 
 static void Init( rtems_task_argument arg )
 {
-  Heap_Area area = { .begin = NULL, .size = 0 };
+  (void) arg;
+}
+
+static void consume_all_memory( void )
+{
+  const Memory_Information *mem;
+  size_t                    i;
+
+  mem = _Memory_Get();
+
+  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
+    Memory_Area *area;
+
+    area = _Memory_Get_area( mem, i );
+    _Memory_Consume( area, _Memory_Get_free_size( area ) );
+  }
+}
+
+static void begin_test( void )
+{
   int i;
 
   TEST_BEGIN();
@@ -49,10 +69,15 @@ static void Init( rtems_task_argument arg )
   RTEMS_OBFUSCATE_VARIABLE( i );
   rtems_test_assert( i == 123 );
 
-  _Workspace_Handler_initialization( &area, 1, NULL );
-  rtems_test_assert( 0 );
+  consume_all_memory();
 }
 
+RTEMS_SYSINIT_ITEM(
+  begin_test,
+  RTEMS_SYSINIT_PER_CPU_DATA,
+  RTEMS_SYSINIT_ORDER_FIRST
+);
+
 static void fatal_extension(
   rtems_fatal_source source,
   bool always_set_to_false,
diff --git a/testsuites/sptests/spfatal09/init.c b/testsuites/sptests/spfatal09/init.c
index 5aca7a9..df462bc 100644
--- a/testsuites/sptests/spfatal09/init.c
+++ b/testsuites/sptests/spfatal09/init.c
@@ -15,18 +15,44 @@
  *  http://www.rtems.org/license/LICENSE.
  */
 
-#include <rtems/malloc.h>
-#include <rtems/libcsupport.h>
+#include <rtems/score/memory.h>
+#include <rtems/sysinit.h>
+
+#include <stdlib.h>
 
 #define FATAL_ERROR_TEST_NAME            "9"
 #define FATAL_ERROR_DESCRIPTION          "Bad heap address to malloc"
 #define FATAL_ERROR_EXPECTED_SOURCE      INTERNAL_ERROR_CORE
 #define FATAL_ERROR_EXPECTED_ERROR       INTERNAL_ERROR_NO_MEMORY_FOR_HEAP
 
-static void force_error(void)
+static void force_error( void )
 {
-  RTEMS_Malloc_Initialize( NULL, 0, NULL );
+  void *p;
+
   /* we will not run this far */
+  p = malloc( 1 );
+  RTEMS_OBFUSCATE_VARIABLE( p );
 }
 
+static void consume_all_memory( void )
+{
+  const Memory_Information *mem;
+  size_t                    i;
+
+  mem = _Memory_Get();
+
+  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
+    Memory_Area *area;
+
+    area = _Memory_Get_area( mem, i );
+    _Memory_Consume( area, _Memory_Get_free_size( area ) );
+  }
+}
+
+RTEMS_SYSINIT_ITEM(
+  consume_all_memory,
+  RTEMS_SYSINIT_MALLOC,
+  RTEMS_SYSINIT_ORDER_FIRST
+);
+
 #include "../spfatal_support/spfatalimpl.h"
diff --git a/testsuites/sptests/spfatal12/init.c b/testsuites/sptests/spfatal12/init.c
index c3c1afd..3fbd6d1 100644
--- a/testsuites/sptests/spfatal12/init.c
+++ b/testsuites/sptests/spfatal12/init.c
@@ -15,6 +15,8 @@
  */
 
 #include <rtems/score/wkspace.h>
+#include <rtems/score/memory.h>
+#include <rtems/sysinit.h>
 
 #define FATAL_ERROR_TEST_NAME            "12"
 #define FATAL_ERROR_DESCRIPTION  \
@@ -22,12 +24,34 @@
 #define FATAL_ERROR_EXPECTED_SOURCE      INTERNAL_ERROR_CORE
 #define FATAL_ERROR_EXPECTED_ERROR       INTERNAL_ERROR_TOO_LITTLE_WORKSPACE
 
-static void force_error(void)
+static void force_error( void )
 {
-  Heap_Area area = { .begin = NULL, .size = 0 };
+  void *p;
 
-  _Workspace_Handler_initialization( &area, 1, NULL );
   /* we will not run this far */
+  p = _Workspace_Allocate( 1 );
+  RTEMS_OBFUSCATE_VARIABLE( p );
 }
 
+static void consume_all_memory( void )
+{
+  const Memory_Information *mem;
+  size_t                    i;
+
+  mem = _Memory_Get();
+
+  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
+    Memory_Area *area;
+
+    area = _Memory_Get_area( mem, i );
+    _Memory_Consume( area, _Memory_Get_free_size( area ) );
+  }
+}
+
+RTEMS_SYSINIT_ITEM(
+  consume_all_memory,
+  RTEMS_SYSINIT_WORKSPACE,
+  RTEMS_SYSINIT_ORDER_FIRST
+);
+
 #include "../spfatal_support/spfatalimpl.h"
diff --git a/testsuites/sptests/spsysinit01/init.c b/testsuites/sptests/spsysinit01/init.c
index 20d48c6..05629f7 100644
--- a/testsuites/sptests/spsysinit01/init.c
+++ b/testsuites/sptests/spsysinit01/init.c
@@ -71,8 +71,8 @@
 const char rtems_test_name[] = "SPSYSINIT 1";
 
 typedef enum {
-  BSP_WORK_AREAS_PRE,
-  BSP_WORK_AREAS_POST,
+  WORKSPACE_PRE,
+  WORKSPACE_POST,
   BSP_START_PRE,
   BSP_START_POST,
   CPU_COUNTER_PRE,
@@ -200,16 +200,16 @@ static bool info_is_init(const Objects_Information *info, size_t count)
   return _Chain_Node_count_unprotected(&info->Inactive) == count;
 }
 
-FIRST(RTEMS_SYSINIT_BSP_WORK_AREAS)
+FIRST(RTEMS_SYSINIT_WORKSPACE)
 {
   assert(_Workspace_Area.area_begin == 0);
-  next_step(BSP_WORK_AREAS_PRE);
+  next_step(WORKSPACE_PRE);
 }
 
-LAST(RTEMS_SYSINIT_BSP_WORK_AREAS)
+LAST(RTEMS_SYSINIT_WORKSPACE)
 {
   assert(_Workspace_Area.area_begin != 0);
-  next_step(BSP_WORK_AREAS_POST);
+  next_step(WORKSPACE_POST);
 }
 
 FIRST(RTEMS_SYSINIT_BSP_START)



More information about the vc mailing list