1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2019 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <picotls.h>
37 #include <string.h>
38 #include <stdint.h>
39 
40 /*
41  * The TLS block consists of initialized data immediately followed by
42  * zero filled data
43  *
44  * These addresses must be defined by the loader configuration file
45  */
46 
47 extern char __tdata_source[];	/* Source of TLS initialization data (in ROM) */
48 
49 extern char __tdata_start[];    /* Start of static tdata area */
50 extern char __tdata_end[];      /* End of static tdata area */
51 extern char __tbss_start[];     /* Start of static zero-initialized TLS data */
52 extern char __tbss_end[];       /* End of static zero-initialized TLS data */
53 
54 #ifdef __PICOLIBC_CRT_RUNTIME_SIZE
55 #define __tdata_size (__tdata_end - __tdata_start)
56 #define __tbss_size (__tbss_end - __tbss_start)
57 #define __tbss_offset (__tbss_start - __tdata_start)
58 #else
59 extern char __tdata_size[];	/* Size of TLS initized data */
60 extern char __tbss_size[];	/* Size of TLS zero-filled data */
61 extern char __tbss_offset[];    /* Offset from tdata to tbss */
62 #endif
63 
64 void
_init_tls(void * __tls)65 _init_tls(void *__tls)
66 {
67 	char *tls = __tls;
68 
69 	/* Copy tls initialized data */
70 	memcpy(tls, __tdata_source, (uintptr_t) __tdata_size);
71 
72 	/* Clear tls zero data */
73 	memset(tls + (uintptr_t) __tbss_offset, '\0', (uintptr_t) __tbss_size);
74 }
75