1 /* Definition for thread-local data handling. NPTL/RISC-V version. 2 Copyright (C) 2011-2022 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library. If not, see 17 <https://www.gnu.org/licenses/>. */ 18 19 #ifndef _RISCV_TLS_H 20 #define _RISCV_TLS_H 1 21 22 #include <dl-sysdep.h> 23 24 #ifndef __ASSEMBLER__ 25 # include <stdbool.h> 26 # include <stddef.h> 27 # include <stdint.h> 28 # include <dl-dtv.h> 29 30 register void *__thread_self asm ("tp"); 31 # define READ_THREAD_POINTER() ({ __thread_self; }) 32 33 /* Get system call information. */ 34 # include <sysdep.h> 35 36 /* The TP points to the start of the thread blocks. */ 37 # define TLS_DTV_AT_TP 1 38 # define TLS_TCB_AT_TP 0 39 40 /* Get the thread descriptor definition. */ 41 # include <nptl/descr.h> 42 43 typedef struct 44 { 45 dtv_t *dtv; 46 void *private; 47 } tcbhead_t; 48 49 /* This is the size of the initial TCB. Because our TCB is before the thread 50 pointer, we don't need this. */ 51 # define TLS_INIT_TCB_SIZE 0 52 53 /* This is the size of the TCB. Because our TCB is before the thread 54 pointer, we don't need this. */ 55 # define TLS_TCB_SIZE 0 56 57 /* This is the size we need before TCB - actually, it includes the TCB. */ 58 # define TLS_PRE_TCB_SIZE \ 59 (sizeof (struct pthread) \ 60 + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1) \ 61 & ~(__alignof (struct pthread) - 1))) 62 63 /* The thread pointer tp points to the end of the TCB. 64 The pthread_descr structure is immediately in front of the TCB. */ 65 # define TLS_TCB_OFFSET 0 66 67 /* Install the dtv pointer. The pointer passed is to the element with 68 index -1 which contain the length. */ 69 # define INSTALL_DTV(tcbp, dtvp) \ 70 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1) 71 72 /* Install new dtv for current thread. */ 73 # define INSTALL_NEW_DTV(dtv) \ 74 (THREAD_DTV() = (dtv)) 75 76 /* Return dtv of given thread descriptor. */ 77 # define GET_DTV(tcbp) \ 78 (((tcbhead_t *) (tcbp))[-1].dtv) 79 80 /* Code to initially initialize the thread pointer. */ 81 # define TLS_INIT_TP(tcbp) \ 82 ({ __thread_self = (char*)tcbp + TLS_TCB_OFFSET; NULL; }) 83 84 /* Return the address of the dtv for the current thread. */ 85 # define THREAD_DTV() \ 86 (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv) 87 88 /* Return the thread descriptor for the current thread. */ 89 # define THREAD_SELF \ 90 ((struct pthread *) (READ_THREAD_POINTER () \ 91 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) 92 93 /* Value passed to 'clone' for initialization of the thread register. */ 94 # define TLS_DEFINE_INIT_TP(tp, pd) \ 95 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE 96 97 /* Informs libthread_db that the thread pointer is register 4, which is used 98 * to know how to do THREAD_SELF. */ 99 # define DB_THREAD_SELF \ 100 REGISTER (64, 64, 4 * 8, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) 101 102 /* Access to data in the thread descriptor is easy. */ 103 # include <tcb-access.h> 104 105 /* l_tls_offset == 0 is perfectly valid, so we have to use some different 106 value to mean unset l_tls_offset. */ 107 # define NO_TLS_OFFSET -1 108 109 /* Get and set the global scope generation counter in struct pthread. */ 110 # define THREAD_GSCOPE_FLAG_UNUSED 0 111 # define THREAD_GSCOPE_FLAG_USED 1 112 # define THREAD_GSCOPE_FLAG_WAIT 2 113 # define THREAD_GSCOPE_RESET_FLAG() \ 114 do \ 115 { int __res \ 116 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \ 117 THREAD_GSCOPE_FLAG_UNUSED); \ 118 if (__res == THREAD_GSCOPE_FLAG_WAIT) \ 119 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ 120 } \ 121 while (0) 122 # define THREAD_GSCOPE_SET_FLAG() \ 123 do \ 124 { \ 125 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \ 126 atomic_write_barrier (); \ 127 } \ 128 while (0) 129 130 #endif /* __ASSEMBLER__ */ 131 132 #endif /* tls.h */ 133