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
Line 
1/**
2 *  @file
3 *
4 *  @brief Workspace Handler Support
5 *  @ingroup RTEMSScoreWorkspace
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2009.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18  #include "config.h"
19#endif
20
21#include <rtems/score/wkspace.h>
22#include <rtems/score/assert.h>
23#include <rtems/score/heapimpl.h>
24#include <rtems/score/interr.h>
25#include <rtems/score/threadimpl.h>
26#include <rtems/score/tls.h>
27#include <rtems/posix/pthread.h>
28#include <rtems/config.h>
29#include <rtems/sysinit.h>
30
31#include <string.h>
32
33/* #define DEBUG_WORKSPACE */
34#if defined(DEBUG_WORKSPACE)
35  #include <rtems/bspIo.h>
36#endif
37
38Heap_Control _Workspace_Area;
39
40static uintptr_t _Workspace_Space_for_TLS( uintptr_t page_size )
41{
42  uintptr_t tls_size;
43  uintptr_t space;
44
45  tls_size = _TLS_Get_size();
46
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   */
55  if ( tls_size > 0 ) {
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     */
64    space = _Heap_Min_block_size( page_size );
65
66    space += _Thread_Initial_thread_count
67      * _Heap_Size_with_overhead( page_size, tls_alloc, tls_align );
68  } else {
69    space = 0;
70  }
71
72  return space;
73}
74
75static void _Workspace_Initialize( void )
76{
77  _Workspace_Handler_initialization( _Memory_Get(), _Heap_Extend );
78}
79
80RTEMS_SYSINIT_ITEM(
81  _Workspace_Initialize,
82  RTEMS_SYSINIT_WORKSPACE,
83  RTEMS_SYSINIT_ORDER_MIDDLE
84);
85
86void _Workspace_Handler_initialization(
87  const Memory_Information              *mem,
88  Heap_Initialization_or_extend_handler  extend
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
108  for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
109    Memory_Area *area;
110    uintptr_t    free_size;
111
112    area = _Memory_Get_area( mem, i );
113    free_size = _Memory_Get_free_size( area );
114
115    if ( do_zero ) {
116      memset( _Memory_Get_free_begin( area ), 0, free_size );
117    }
118
119    if ( free_size > overhead ) {
120      uintptr_t space_available;
121      uintptr_t size;
122
123      if ( unified ) {
124        size = free_size;
125      } else {
126        if ( remaining > 0 ) {
127          size = remaining < free_size - overhead ?
128            remaining + overhead : free_size;
129        } else {
130          size = 0;
131        }
132      }
133
134      space_available = ( *init_or_extend )(
135        &_Workspace_Area,
136        _Memory_Get_free_begin( area ),
137        size,
138        page_size
139      );
140
141      _Memory_Consume( area, size );
142
143      if ( space_available < remaining ) {
144        remaining -= space_available;
145      } else {
146        remaining = 0;
147      }
148
149      init_or_extend = extend;
150    }
151  }
152
153  if ( remaining > 0 ) {
154    _Internal_error( INTERNAL_ERROR_TOO_LITTLE_WORKSPACE );
155  }
156
157  _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 );
158}
159
160void *_Workspace_Allocate(
161  size_t   size
162)
163{
164  void *memory;
165
166  memory = _Heap_Allocate( &_Workspace_Area, size );
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 ),
173      memory
174    );
175  #endif
176  return memory;
177}
178
179void *_Workspace_Allocate_aligned( size_t size, size_t alignment )
180{
181  return _Heap_Allocate_aligned( &_Workspace_Area, size, alignment );
182}
183
184/*
185 *  _Workspace_Free
186 */
187void _Workspace_Free(
188  void *block
189)
190{
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
199  _Heap_Free( &_Workspace_Area, block );
200}
Note: See TracBrowser for help on using the repository browser.