1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreTLS |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _TLS_Get_allocation_size(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (C) 2014, 2022 embedded brains GmbH |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/score/tls.h> |
---|
42 | #include <rtems/score/interr.h> |
---|
43 | #include <rtems/score/thread.h> |
---|
44 | |
---|
45 | static uintptr_t _TLS_Allocation_size; |
---|
46 | |
---|
47 | uintptr_t _TLS_Get_allocation_size( void ) |
---|
48 | { |
---|
49 | uintptr_t size; |
---|
50 | uintptr_t allocation_size; |
---|
51 | |
---|
52 | size = _TLS_Get_size(); |
---|
53 | |
---|
54 | if ( size == 0 ) { |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | allocation_size = _TLS_Allocation_size; |
---|
59 | |
---|
60 | if ( allocation_size == 0 ) { |
---|
61 | uintptr_t tls_align; |
---|
62 | uintptr_t stack_align; |
---|
63 | |
---|
64 | /* |
---|
65 | * The TLS area is allocated in the thread storage area. Each allocation |
---|
66 | * shall meet the stack alignment requirement. |
---|
67 | */ |
---|
68 | stack_align = CPU_STACK_ALIGNMENT; |
---|
69 | tls_align = RTEMS_ALIGN_UP( (uintptr_t) _TLS_Alignment, stack_align ); |
---|
70 | |
---|
71 | #ifndef __i386__ |
---|
72 | /* Reserve space for the dynamic thread vector */ |
---|
73 | allocation_size += |
---|
74 | RTEMS_ALIGN_UP( sizeof( TLS_Dynamic_thread_vector ), stack_align ); |
---|
75 | #endif |
---|
76 | |
---|
77 | /* Reserve space for the thread control block */ |
---|
78 | allocation_size += |
---|
79 | #if CPU_THREAD_LOCAL_STORAGE_VARIANT == 11 |
---|
80 | RTEMS_ALIGN_UP( sizeof( TLS_Thread_control_block ), tls_align ); |
---|
81 | #else |
---|
82 | RTEMS_ALIGN_UP( sizeof( TLS_Thread_control_block ), stack_align ); |
---|
83 | #endif |
---|
84 | |
---|
85 | /* Reserve space for the thread-local storage data */ |
---|
86 | allocation_size += |
---|
87 | #if CPU_THREAD_LOCAL_STORAGE_VARIANT == 20 |
---|
88 | RTEMS_ALIGN_UP( size, tls_align ); |
---|
89 | #else |
---|
90 | RTEMS_ALIGN_UP( size, stack_align ); |
---|
91 | #endif |
---|
92 | |
---|
93 | /* |
---|
94 | * The stack allocator does not support aligned allocations. Allocate |
---|
95 | * enough to do the alignment manually. |
---|
96 | */ |
---|
97 | if ( tls_align > stack_align ) { |
---|
98 | _Assert( tls_align % stack_align == 0 ); |
---|
99 | allocation_size += tls_align - stack_align; |
---|
100 | } |
---|
101 | |
---|
102 | if ( _Thread_Maximum_TLS_size != 0 ) { |
---|
103 | if ( allocation_size <= _Thread_Maximum_TLS_size ) { |
---|
104 | _Assert( _Thread_Maximum_TLS_size % CPU_STACK_ALIGNMENT == 0 ); |
---|
105 | allocation_size = _Thread_Maximum_TLS_size; |
---|
106 | } else { |
---|
107 | _Internal_error( INTERNAL_ERROR_TOO_LARGE_TLS_SIZE ); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | _TLS_Allocation_size = allocation_size; |
---|
112 | } |
---|
113 | |
---|
114 | return allocation_size; |
---|
115 | } |
---|