RTEMS 6.1-rc4
Loading...
Searching...
No Matches
tls.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
12/*
13 * Copyright (C) 2014, 2023 embedded brains GmbH & Co. KG
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#ifndef _RTEMS_SCORE_TLS_H
38#define _RTEMS_SCORE_TLS_H
39
40#include <rtems/score/cpuimpl.h>
41
42#include <string.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif /* __cplusplus */
47
65typedef struct {
69 const char *data_begin;
70
74 const char *data_size;
75
79 const char *bss_begin;
80
84 const char *bss_size;
85
89 const char *size;
90
94 const char *alignment;
96
106extern const volatile TLS_Configuration _TLS_Configuration;
107
108typedef struct {
109 /*
110 * FIXME: Not sure if the generation number type is correct for all
111 * architectures.
112 */
113 uint32_t generation_number;
114
115 void *tls_blocks[1];
117
119#if defined(__i386__) || defined(__x86_64__)
120 struct TLS_Thread_control_block *tcb;
121#else /* !(__i386__ || __x86_64__) */
123/*
124 * GCC under AArch64/LP64 expects a 16 byte TCB at the beginning of the TLS
125 * data segment and indexes into it accordingly for TLS variable addresses.
126 */
127#if CPU_SIZEOF_POINTER == 4 || defined(AARCH64_MULTILIB_ARCH_V8)
128 uintptr_t reserved;
129#endif
130#endif /* __i386__ || __x86_64__ */
132
133typedef struct {
134 uintptr_t module;
135 uintptr_t offset;
136} TLS_Index;
137
145static inline uintptr_t _TLS_Get_thread_control_block_area_size(
146 const volatile TLS_Configuration *config
147)
148{
149#if CPU_THREAD_LOCAL_STORAGE_VARIANT == 11
150 uintptr_t alignment;
151
152 alignment = (uintptr_t) config->alignment;
153
154 return RTEMS_ALIGN_UP( sizeof( TLS_Thread_control_block ), alignment );
155#else
156 (void) config;
157 return sizeof( TLS_Thread_control_block );
158#endif
159}
160
167uintptr_t _TLS_Get_allocation_size( void );
168
176static inline void _TLS_Copy_and_clear(
177 const volatile TLS_Configuration *config,
178 void *tls_data
179)
180{
181 tls_data =
182 memcpy( tls_data, config->data_begin, (uintptr_t) config->data_size );
183
184 memset(
185 (char *) tls_data +
186 (uintptr_t) config->bss_begin - (uintptr_t) config->data_begin,
187 0,
188 (uintptr_t) config->bss_size
189 );
190}
191
201static inline void _TLS_Initialize_TCB_and_DTV(
202 void *tls_data,
205)
206{
207#if defined(__i386__) || defined(__x86_64__)
208 (void) dtv;
209 tcb->tcb = tcb;
210#else
211 tcb->dtv = dtv;
212 dtv->generation_number = 1;
213 dtv->tls_blocks[0] = tls_data;
214#endif
215}
216
226static inline void *_TLS_Initialize_area( void *tls_area )
227{
228 const volatile TLS_Configuration *config;
229 uintptr_t alignment;
230 void *tls_data;
233 void *return_value;
234#if CPU_THREAD_LOCAL_STORAGE_VARIANT == 11
235 uintptr_t tcb_size;
236#endif
237#if CPU_THREAD_LOCAL_STORAGE_VARIANT == 20
238 uintptr_t size;
239 uintptr_t alignment_2;
240#endif
241
243 alignment = (uintptr_t) config->alignment;
244
245#if defined(__i386__) || defined(__x86_64__)
246 dtv = NULL;
247#else
248 dtv = (TLS_Dynamic_thread_vector *) tls_area;
249 tls_area = (char *) tls_area + sizeof( *dtv );
250#endif
251
252#if CPU_THREAD_LOCAL_STORAGE_VARIANT == 10
253 tls_data = (void *)
254 RTEMS_ALIGN_UP( (uintptr_t) tls_area + sizeof( *tcb ), alignment );
255 tcb = (TLS_Thread_control_block *) ((char *) tls_data - sizeof( *tcb ));
256 return_value = tls_data;
257#elif CPU_THREAD_LOCAL_STORAGE_VARIANT == 11
258 tcb_size = RTEMS_ALIGN_UP( sizeof( *tcb ), alignment );
259 tls_data = (void *)
260 RTEMS_ALIGN_UP( (uintptr_t) tls_area + tcb_size, alignment );
261 tcb = (TLS_Thread_control_block *) ((char *) tls_data - tcb_size);
262 return_value = tcb;
263#elif CPU_THREAD_LOCAL_STORAGE_VARIANT == 20
264 alignment_2 = RTEMS_ALIGN_UP( alignment, CPU_SIZEOF_POINTER );
265 tls_area = (void *) RTEMS_ALIGN_UP( (uintptr_t) tls_area, alignment_2 );
266 size = (uintptr_t) config->size;
268 ((char *) tls_area + RTEMS_ALIGN_UP( size, alignment_2 ));
269 tls_data = (char *) tcb - RTEMS_ALIGN_UP( size, alignment );
270 return_value = tcb;
271#else
272#error "unexpected CPU_THREAD_LOCAL_STORAGE_VARIANT value"
273#endif
274
275 _TLS_Initialize_TCB_and_DTV( tls_data, tcb, dtv );
276 _TLS_Copy_and_clear( config, tls_data );
277
278 return return_value;
279}
280
283#ifdef __cplusplus
284}
285#endif /* __cplusplus */
286
287#endif /* _RTEMS_SCORE_TLS_H */
#define RTEMS_ALIGN_UP(_value, _alignment)
Aligns up the value to the alignment.
Definition: basedefs.h:141
const volatile TLS_Configuration _TLS_Configuration
Provides the TLS configuration.
Definition: tlsallocsize.c:67
uintptr_t _TLS_Get_allocation_size(void)
Gets the allocation size of the thread-local storage area in bytes.
Definition: tlsallocsize.c:78
Represents the TLS configuration.
Definition: tls.h:65
const char * size
This member is initialized to _TLS_Size.
Definition: tls.h:89
const char * alignment
This member is initialized to _TLS_Alignment.
Definition: tls.h:94
const char * data_begin
This member is initialized to _TLS_Data_begin.
Definition: tls.h:69
const char * bss_size
This member is initialized to _TLS_BSS_size.
Definition: tls.h:84
const char * bss_begin
This member is initialized to _TLS_BSS_begin.
Definition: tls.h:79
const char * data_size
This member is initialized to _TLS_Data_size.
Definition: tls.h:74
Definition: tls.h:108
Definition: tls.h:133
Definition: tls.h:118
Definition: deflate.c:114
Definition: rtl-mdreloc-aarch64.c:86