Subversion Repositories HelenOS

Rev

Rev 3022 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
447 decky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
447 decky 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1653 cejka 27
 */
28
 
1781 jermar 29
/** @addtogroup libc
1653 cejka 30
 * @{
31
 */
32
/** @file
2586 jermar 33
 *
34
 * Support for thread-local storage, as described in:
35
 *  Drepper U.: ELF Handling For Thread-Local Storage, 2005
36
 *
37
 * Only static model is supported.
447 decky 38
 */
39
 
2586 jermar 40
#include <tls.h>
41
#include <malloc.h>
1129 palkovsky 42
#include <string.h>
2586 jermar 43
#include <align.h>
447 decky 44
 
2061 jermar 45
/** Create TLS (Thread Local Storage) data structures.
1129 palkovsky 46
 *
2061 jermar 47
 * The code requires, that sections .tdata and .tbss are adjacent. It may be
48
 * changed in the future.
49
 *
50
 * @return Pointer to TCB.
1129 palkovsky 51
 */
2061 jermar 52
tcb_t *__make_tls(void)
1113 palkovsky 53
{
1129 palkovsky 54
    void *data;
55
    tcb_t *tcb;
56
    size_t tls_size = &_tbss_end - &_tdata_start;
57
 
58
    tcb = __alloc_tls(&data, tls_size);
59
 
2061 jermar 60
    /*
61
     * Copy thread local data from the initialization image.
62
     */
1129 palkovsky 63
    memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
2061 jermar 64
    /*
65
     * Zero out the thread local uninitialized data.
66
     */
2436 jermar 67
    memset(data + (&_tbss_start - &_tdata_start), 0,
68
        &_tbss_end - &_tbss_start);
2061 jermar 69
 
1129 palkovsky 70
    return tcb;
1113 palkovsky 71
}
72
 
1129 palkovsky 73
void __free_tls(tcb_t *tcb)
1113 palkovsky 74
{
1129 palkovsky 75
    size_t tls_size = &_tbss_end - &_tdata_start;
76
    __free_tls_arch(tcb, tls_size);
1113 palkovsky 77
}
78
 
2586 jermar 79
#ifdef CONFIG_TLS_VARIANT_1
80
/** Allocate TLS variant 1 data structures.
1081 jermar 81
 *
2586 jermar 82
 * @param data      Start of TLS section. This is an output argument.
83
 * @param size      Size of tdata + tbss section.
84
 * @return      Pointer to tcb_t structure.
1081 jermar 85
 */
2586 jermar 86
tcb_t *tls_alloc_variant_1(void **data, size_t size)
1079 jermar 87
{
2586 jermar 88
    tcb_t *result;
1113 palkovsky 89
 
2586 jermar 90
    result = malloc(sizeof(tcb_t) + size);
91
    *data = ((void *)result) + sizeof(tcb_t);
92
    return result;
1079 jermar 93
}
1065 jermar 94
 
2586 jermar 95
/** Free TLS variant I data structures.
1081 jermar 96
 *
2586 jermar 97
 * @param tcb       Pointer to TCB structure.
98
 * @param size      This argument is ignored.
1081 jermar 99
 */
2586 jermar 100
void tls_free_variant_1(tcb_t *tcb, size_t size)
1065 jermar 101
{
2586 jermar 102
    free(tcb);
713 decky 103
}
2586 jermar 104
#endif
713 decky 105
 
2586 jermar 106
#ifdef CONFIG_TLS_VARIANT_2
107
/** Allocate TLS variant II data structures.
1081 jermar 108
 *
2586 jermar 109
 * @param data      Pointer to pointer to thread local data. This is
110
 *          actually an output argument.
111
 * @param size      Size of thread local data.
112
 * @return      Pointer to TCB structure.
1081 jermar 113
 */
2586 jermar 114
tcb_t * tls_alloc_variant_2(void **data, size_t size)
1065 jermar 115
{
2586 jermar 116
    tcb_t *tcb;
117
 
118
    size = ALIGN_UP(size, &_tls_alignment);
4055 trochtova 119
    *data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size);
1427 palkovsky 120
 
2586 jermar 121
    tcb = (tcb_t *) (*data + size);
122
    tcb->self = tcb;
2188 decky 123
 
2586 jermar 124
    return tcb;
2188 decky 125
}
126
 
2586 jermar 127
/** Free TLS variant II data structures.
2188 decky 128
 *
2586 jermar 129
 * @param tcb       Pointer to TCB structure.
130
 * @param size      Size of thread local data.
2188 decky 131
 */
2586 jermar 132
void tls_free_variant_2(tcb_t *tcb, size_t size)
2188 decky 133
{
2586 jermar 134
    size = ALIGN_UP(size, &_tls_alignment);
135
    void *start = ((void *) tcb) - size;
136
    free(start);
2188 decky 137
}
2586 jermar 138
#endif
2188 decky 139
 
1781 jermar 140
/** @}
1653 cejka 141
 */