source: rtems/cpukit/score/src/wkspace.c @ eea21eac

5
Last change on this file since eea21eac was eea21eac, checked in by Sebastian Huber <sebastian.huber@…>, on 12/13/19 at 05:18:36

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.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[f2f63d1]1/**
2 *  @file
[ac7d5ef0]3 *
[f2f63d1]4 *  @brief Workspace Handler Support
[4c20da4b]5 *  @ingroup RTEMSScoreWorkspace
[f2f63d1]6 */
7
8/*
[e63b79d]9 *  COPYRIGHT (c) 1989-2009.
[ac7d5ef0]10 *  On-Line Applications Research Corporation (OAR).
11 *
[98e4ebf5]12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
[c499856]14 *  http://www.rtems.org/license/LICENSE.
[ac7d5ef0]15 */
16
[a8eed23]17#if HAVE_CONFIG_H
[e6f7f81]18  #include "config.h"
[a8eed23]19#endif
20
[5e9b32b]21#include <rtems/score/wkspace.h>
[cfc4231d]22#include <rtems/score/assert.h>
[e6f7f81]23#include <rtems/score/heapimpl.h>
[5e9b32b]24#include <rtems/score/interr.h>
[022851a]25#include <rtems/score/threadimpl.h>
26#include <rtems/score/tls.h>
[e97806a]27#include <rtems/posix/pthread.h>
[e6f7f81]28#include <rtems/config.h>
[eea21eac]29#include <rtems/sysinit.h>
[976162a6]30
[cfc4231d]31#include <string.h>
[ac7d5ef0]32
[e63b79d]33/* #define DEBUG_WORKSPACE */
34#if defined(DEBUG_WORKSPACE)
35  #include <rtems/bspIo.h>
36#endif
37
[358bd740]38Heap_Control _Workspace_Area;
39
[cfc4231d]40static uintptr_t _Workspace_Space_for_TLS( uintptr_t page_size )
[8faca06]41{
[cfc4231d]42  uintptr_t tls_size;
43  uintptr_t space;
44
45  tls_size = _TLS_Get_size();
[47a3cd8]46
[884a6c59]47  /*
48   * In case we have a non-zero TLS size, then we need a TLS area for each
49   * thread.  These areas are allocated from the workspace.  Ensure that the
50   * workspace is large enough to fulfill all requests known at configuration
51   * time (so excluding the unlimited option).  It is not possible to estimate
52   * the TLS size in the configuration at compile-time.  The TLS size is
53   * determined at application link-time.
54   */
[022851a]55  if ( tls_size > 0 ) {
[6cf45cb]56    uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment );
57    uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align );
58
59    /*
60     * Memory allocated with an alignment constraint is allocated from the end
61     * of a free block.  The last allocation may need one free block of minimum
62     * size.
63     */
[cfc4231d]64    space = _Heap_Min_block_size( page_size );
[022851a]65
[21275b58]66    space += _Thread_Initial_thread_count
[6cf45cb]67      * _Heap_Size_with_overhead( page_size, tls_alloc, tls_align );
[cfc4231d]68  } else {
69    space = 0;
70  }
71
72  return space;
73}
74
[eea21eac]75static void _Workspace_Initialize( void )
[cfc4231d]76{
[eea21eac]77  _Workspace_Handler_initialization( _Memory_Get(), _Heap_Extend );
[cfc4231d]78}
79
[eea21eac]80RTEMS_SYSINIT_ITEM(
81  _Workspace_Initialize,
82  RTEMS_SYSINIT_WORKSPACE,
83  RTEMS_SYSINIT_ORDER_MIDDLE
84);
[cfc4231d]85
86void _Workspace_Handler_initialization(
[eea21eac]87  const Memory_Information              *mem,
88  Heap_Initialization_or_extend_handler  extend
[cfc4231d]89)
90{
91  Heap_Initialization_or_extend_handler init_or_extend;
92  uintptr_t                             remaining;
93  bool                                  do_zero;
94  bool                                  unified;
95  uintptr_t                             page_size;
96  uintptr_t                             overhead;
97  size_t                                i;
98
99  page_size = CPU_HEAP_ALIGNMENT;
100  remaining = rtems_configuration_get_work_space_size();
101  remaining += _Workspace_Space_for_TLS( page_size );
102
103  init_or_extend = _Heap_Initialize;
104  do_zero = rtems_configuration_get_do_zero_of_workspace();
105  unified = rtems_configuration_get_unified_work_area();
106  overhead = _Heap_Area_overhead( page_size );
107
[eea21eac]108  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
109    Memory_Area *area;
110    uintptr_t    free_size;
[022851a]111
[eea21eac]112    area = _Memory_Get_area( mem, i );
113    free_size = _Memory_Get_free_size( area );
[47a3cd8]114
115    if ( do_zero ) {
[eea21eac]116      memset( _Memory_Get_free_begin( area ), 0, free_size );
[47a3cd8]117    }
118
[eea21eac]119    if ( free_size > overhead ) {
[47a3cd8]120      uintptr_t space_available;
121      uintptr_t size;
122
123      if ( unified ) {
[eea21eac]124        size = free_size;
[47a3cd8]125      } else {
126        if ( remaining > 0 ) {
[eea21eac]127          size = remaining < free_size - overhead ?
128            remaining + overhead : free_size;
[47a3cd8]129        } else {
130          size = 0;
131        }
132      }
133
[cfc4231d]134      space_available = ( *init_or_extend )(
[47a3cd8]135        &_Workspace_Area,
[eea21eac]136        _Memory_Get_free_begin( area ),
[47a3cd8]137        size,
138        page_size
139      );
140
[eea21eac]141      _Memory_Consume( area, size );
[05279b84]142
[47a3cd8]143      if ( space_available < remaining ) {
144        remaining -= space_available;
145      } else {
146        remaining = 0;
147      }
[05279b84]148
[47a3cd8]149      init_or_extend = extend;
150    }
151  }
[05279b84]152
[47a3cd8]153  if ( remaining > 0 ) {
[3a659b04]154    _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE );
[47a3cd8]155  }
[2a713e3]156
157  _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 );
[8faca06]158}
159
[5b33dc80]160void *_Workspace_Allocate(
[7ff6115]161  size_t   size
[5b33dc80]162)
163{
[a2fa532d]164  void *memory;
[e63b79d]165
[a2fa532d]166  memory = _Heap_Allocate( &_Workspace_Area, size );
[e63b79d]167  #if defined(DEBUG_WORKSPACE)
168    printk(
169      "Workspace_Allocate(%d) from %p/%p -> %p\n",
170      size,
171      __builtin_return_address( 0 ),
172      __builtin_return_address( 1 ),
[a2fa532d]173      memory
[e63b79d]174    );
175  #endif
[a2fa532d]176  return memory;
[5b33dc80]177}
[ac7d5ef0]178
[a850d04]179void *_Workspace_Allocate_aligned( size_t size, size_t alignment )
180{
181  return _Heap_Allocate_aligned( &_Workspace_Area, size, alignment );
182}
183
[5b33dc80]184/*
[9d4fa67]185 *  _Workspace_Free
[5b33dc80]186 */
[a0323a9f]187void _Workspace_Free(
[5b33dc80]188  void *block
189)
190{
[e63b79d]191  #if defined(DEBUG_WORKSPACE)
192    printk(
193      "Workspace_Free(%p) from %p/%p\n",
194      block,
195      __builtin_return_address( 0 ),
196      __builtin_return_address( 1 )
197    );
198  #endif
[8249054]199  _Heap_Free( &_Workspace_Area, block );
[5b33dc80]200}
Note: See TracBrowser for help on using the repository browser.