1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Workspace Handler Support |
---|
5 | * @ingroup ScoreWorkspace |
---|
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/heapimpl.h> |
---|
23 | #include <rtems/score/interr.h> |
---|
24 | #include <rtems/score/threadimpl.h> |
---|
25 | #include <rtems/score/tls.h> |
---|
26 | #include <rtems/config.h> |
---|
27 | |
---|
28 | #include <string.h> /* for memset */ |
---|
29 | |
---|
30 | /* #define DEBUG_WORKSPACE */ |
---|
31 | #if defined(DEBUG_WORKSPACE) |
---|
32 | #include <rtems/bspIo.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | Heap_Control _Workspace_Area; |
---|
36 | |
---|
37 | static uint32_t _Get_maximum_thread_count(void) |
---|
38 | { |
---|
39 | uint32_t thread_count = 0; |
---|
40 | |
---|
41 | thread_count += _Thread_Get_maximum_internal_threads(); |
---|
42 | |
---|
43 | thread_count += rtems_resource_maximum_per_allocation( |
---|
44 | Configuration_RTEMS_API.maximum_tasks |
---|
45 | ); |
---|
46 | |
---|
47 | #if defined(RTEMS_POSIX_API) |
---|
48 | thread_count += rtems_resource_maximum_per_allocation( |
---|
49 | Configuration_POSIX_API.maximum_threads |
---|
50 | ); |
---|
51 | #endif |
---|
52 | |
---|
53 | return thread_count; |
---|
54 | } |
---|
55 | |
---|
56 | void _Workspace_Handler_initialization( |
---|
57 | Heap_Area *areas, |
---|
58 | size_t area_count, |
---|
59 | Heap_Initialization_or_extend_handler extend |
---|
60 | ) |
---|
61 | { |
---|
62 | Heap_Initialization_or_extend_handler init_or_extend = _Heap_Initialize; |
---|
63 | uintptr_t remaining = rtems_configuration_get_work_space_size(); |
---|
64 | bool do_zero = rtems_configuration_get_do_zero_of_workspace(); |
---|
65 | bool unified = rtems_configuration_get_unified_work_area(); |
---|
66 | uintptr_t page_size = CPU_HEAP_ALIGNMENT; |
---|
67 | uintptr_t overhead = _Heap_Area_overhead( page_size ); |
---|
68 | uintptr_t tls_size = _TLS_Get_size(); |
---|
69 | size_t i; |
---|
70 | |
---|
71 | /* |
---|
72 | * In case we have a non-zero TLS size, then we need a TLS area for each |
---|
73 | * thread. These areas are allocated from the workspace. Ensure that the |
---|
74 | * workspace is large enough to fulfill all requests known at configuration |
---|
75 | * time (so excluding the unlimited option). It is not possible to estimate |
---|
76 | * the TLS size in the configuration at compile-time. The TLS size is |
---|
77 | * determined at application link-time. |
---|
78 | */ |
---|
79 | if ( tls_size > 0 ) { |
---|
80 | uintptr_t tls_align = _TLS_Heap_align_up( (uintptr_t) _TLS_Alignment ); |
---|
81 | uintptr_t tls_alloc = _TLS_Get_allocation_size( tls_size, tls_align ); |
---|
82 | |
---|
83 | /* |
---|
84 | * Memory allocated with an alignment constraint is allocated from the end |
---|
85 | * of a free block. The last allocation may need one free block of minimum |
---|
86 | * size. |
---|
87 | */ |
---|
88 | remaining += _Heap_Min_block_size( page_size ); |
---|
89 | |
---|
90 | remaining += _Get_maximum_thread_count() |
---|
91 | * _Heap_Size_with_overhead( page_size, tls_alloc, tls_align ); |
---|
92 | } |
---|
93 | |
---|
94 | for (i = 0; i < area_count; ++i) { |
---|
95 | Heap_Area *area = &areas [i]; |
---|
96 | |
---|
97 | if ( do_zero ) { |
---|
98 | memset( area->begin, 0, area->size ); |
---|
99 | } |
---|
100 | |
---|
101 | if ( area->size > overhead ) { |
---|
102 | uintptr_t space_available; |
---|
103 | uintptr_t size; |
---|
104 | |
---|
105 | if ( unified ) { |
---|
106 | size = area->size; |
---|
107 | } else { |
---|
108 | if ( remaining > 0 ) { |
---|
109 | size = remaining < area->size - overhead ? |
---|
110 | remaining + overhead : area->size; |
---|
111 | } else { |
---|
112 | size = 0; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | space_available = (*init_or_extend)( |
---|
117 | &_Workspace_Area, |
---|
118 | area->begin, |
---|
119 | size, |
---|
120 | page_size |
---|
121 | ); |
---|
122 | |
---|
123 | area->begin = (char *) area->begin + size; |
---|
124 | area->size -= size; |
---|
125 | |
---|
126 | if ( space_available < remaining ) { |
---|
127 | remaining -= space_available; |
---|
128 | } else { |
---|
129 | remaining = 0; |
---|
130 | } |
---|
131 | |
---|
132 | init_or_extend = extend; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | if ( remaining > 0 ) { |
---|
137 | _Terminate( |
---|
138 | INTERNAL_ERROR_CORE, |
---|
139 | true, |
---|
140 | INTERNAL_ERROR_TOO_LITTLE_WORKSPACE |
---|
141 | ); |
---|
142 | } |
---|
143 | |
---|
144 | _Heap_Protection_set_delayed_free_fraction( &_Workspace_Area, 1 ); |
---|
145 | } |
---|
146 | |
---|
147 | void *_Workspace_Allocate( |
---|
148 | size_t size |
---|
149 | ) |
---|
150 | { |
---|
151 | void *memory; |
---|
152 | |
---|
153 | memory = _Heap_Allocate( &_Workspace_Area, size ); |
---|
154 | #if defined(DEBUG_WORKSPACE) |
---|
155 | printk( |
---|
156 | "Workspace_Allocate(%d) from %p/%p -> %p\n", |
---|
157 | size, |
---|
158 | __builtin_return_address( 0 ), |
---|
159 | __builtin_return_address( 1 ), |
---|
160 | memory |
---|
161 | ); |
---|
162 | #endif |
---|
163 | return memory; |
---|
164 | } |
---|
165 | |
---|
166 | void *_Workspace_Allocate_aligned( size_t size, size_t alignment ) |
---|
167 | { |
---|
168 | return _Heap_Allocate_aligned( &_Workspace_Area, size, alignment ); |
---|
169 | } |
---|
170 | |
---|
171 | /* |
---|
172 | * _Workspace_Free |
---|
173 | */ |
---|
174 | void _Workspace_Free( |
---|
175 | void *block |
---|
176 | ) |
---|
177 | { |
---|
178 | #if defined(DEBUG_WORKSPACE) |
---|
179 | printk( |
---|
180 | "Workspace_Free(%p) from %p/%p\n", |
---|
181 | block, |
---|
182 | __builtin_return_address( 0 ), |
---|
183 | __builtin_return_address( 1 ) |
---|
184 | ); |
---|
185 | #endif |
---|
186 | _Heap_Free( &_Workspace_Area, block ); |
---|
187 | } |
---|
188 | |
---|
189 | void *_Workspace_Allocate_or_fatal_error( |
---|
190 | size_t size |
---|
191 | ) |
---|
192 | { |
---|
193 | void *memory; |
---|
194 | |
---|
195 | memory = _Heap_Allocate( &_Workspace_Area, size ); |
---|
196 | #if defined(DEBUG_WORKSPACE) |
---|
197 | printk( |
---|
198 | "Workspace_Allocate_or_fatal_error(%d) from %p/%p -> %p\n", |
---|
199 | size, |
---|
200 | __builtin_return_address( 0 ), |
---|
201 | __builtin_return_address( 1 ), |
---|
202 | memory |
---|
203 | ); |
---|
204 | #endif |
---|
205 | |
---|
206 | if ( memory == NULL ) |
---|
207 | _Terminate( |
---|
208 | INTERNAL_ERROR_CORE, |
---|
209 | true, |
---|
210 | INTERNAL_ERROR_WORKSPACE_ALLOCATION |
---|
211 | ); |
---|
212 | |
---|
213 | return memory; |
---|
214 | } |
---|