1  /* Definition for thread-local data handling.  NPTL/MIPS version.
       2     Copyright (C) 2005-2023 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 _TLS_H
      20  #define _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  /* Get system call information.  */
      31  # include <sysdep.h>
      32  
      33  #ifdef __mips16
      34  /* MIPS16 uses GCC builtin to access the TP.  */
      35  # define READ_THREAD_POINTER() (__builtin_thread_pointer ())
      36  #else
      37  /* Note: rd must be $v1 to be ABI-conformant.  */
      38  # if defined (__mips_isa_rev) &&  __mips_isa_rev >= 2
      39  #  define READ_THREAD_POINTER() \
      40       ({ void *__result;							      \
      41          asm volatile ("rdhwr\t%0, $29" : "=v" (__result));	      	      \
      42          __result; })
      43  # else
      44  #  define READ_THREAD_POINTER() \
      45       ({ void *__result;							      \
      46          asm volatile (".set\tpush\n\t.set\tmips32r2\n\t"			      \
      47  		      "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result));	      \
      48          __result; })
      49  # endif
      50  #endif
      51  
      52  #else /* __ASSEMBLER__ */
      53  # if __mips_isa_rev >= 2
      54  #  define READ_THREAD_POINTER(rd) rdhwr	rd, $29
      55  # else
      56  #  define READ_THREAD_POINTER(rd) \
      57  	 .set	push;							      \
      58  	 .set	mips32r2;						      \
      59  	 rdhwr	rd, $29;						      \
      60  	 .set	pop
      61  # endif
      62  #endif /* __ASSEMBLER__ */
      63  
      64  
      65  #ifndef __ASSEMBLER__
      66  
      67  /* The TP points to the start of the thread blocks.  */
      68  # define TLS_DTV_AT_TP	1
      69  # define TLS_TCB_AT_TP	0
      70  
      71  /* Get the thread descriptor definition.  */
      72  # include <nptl/descr.h>
      73  
      74  typedef struct
      75  {
      76    dtv_t *dtv;
      77    void *private;
      78  } tcbhead_t;
      79  
      80  /* This is the size of the initial TCB.  Because our TCB is before the thread
      81     pointer, we don't need this.  */
      82  # define TLS_INIT_TCB_SIZE	0
      83  
      84  /* This is the size of the TCB.  Because our TCB is before the thread
      85     pointer, we don't need this.  */
      86  # define TLS_TCB_SIZE		0
      87  
      88  /* This is the size we need before TCB - actually, it includes the TCB.  */
      89  # define TLS_PRE_TCB_SIZE \
      90    (sizeof (struct pthread)						      \
      91     + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1)		      \
      92        & ~(__alignof (struct pthread) - 1)))
      93  
      94  /* The thread pointer (in hardware register $29) points to the end of
      95     the TCB + 0x7000, as for PowerPC.  The pthread_descr structure is
      96     immediately in front of the TCB.  */
      97  # define TLS_TCB_OFFSET	0x7000
      98  
      99  /* Install the dtv pointer.  The pointer passed is to the element with
     100     index -1 which contain the length.  */
     101  # define INSTALL_DTV(tcbp, dtvp) \
     102    (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
     103  
     104  /* Install new dtv for current thread.  */
     105  # define INSTALL_NEW_DTV(dtv) \
     106    (THREAD_DTV() = (dtv))
     107  
     108  /* Return dtv of given thread descriptor.  */
     109  # define GET_DTV(tcbp) \
     110    (((tcbhead_t *) (tcbp))[-1].dtv)
     111  
     112  /* Code to initially initialize the thread pointer.  This might need
     113     special attention since 'errno' is not yet available and if the
     114     operation can cause a failure 'errno' must not be touched.  */
     115  # define TLS_INIT_TP(tcbp) \
     116    ({ long int result_var;						\
     117       result_var = INTERNAL_SYSCALL_CALL (set_thread_area, 		\
     118  				    (char *) (tcbp) + TLS_TCB_OFFSET);	\
     119       !INTERNAL_SYSCALL_ERROR_P (result_var); })
     120  
     121  /* Value passed to 'clone' for initialization of the thread register.  */
     122  # define TLS_DEFINE_INIT_TP(tp, pd) \
     123    void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
     124  
     125  /* Return the address of the dtv for the current thread.  */
     126  # define THREAD_DTV() \
     127    (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
     128  
     129  /* Return the thread descriptor for the current thread.  */
     130  # define THREAD_SELF \
     131   ((struct pthread *) (READ_THREAD_POINTER ()			     \
     132  		      - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
     133  
     134  /* Magic for libthread_db to know how to do THREAD_SELF.  */
     135  # define DB_THREAD_SELF \
     136    CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
     137  
     138  /* Access to data in the thread descriptor is easy.  */
     139  # include <tcb-access.h>
     140  
     141  /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
     142     different value to mean unset l_tls_offset.  */
     143  # define NO_TLS_OFFSET		-1
     144  
     145  /* Get and set the global scope generation counter in struct pthread.  */
     146  #define THREAD_GSCOPE_FLAG_UNUSED 0
     147  #define THREAD_GSCOPE_FLAG_USED   1
     148  #define THREAD_GSCOPE_FLAG_WAIT   2
     149  #define THREAD_GSCOPE_RESET_FLAG() \
     150    do									     \
     151      { int __res								     \
     152  	= atomic_exchange_release (&THREAD_SELF->header.gscope_flag,	     \
     153  			       THREAD_GSCOPE_FLAG_UNUSED);		     \
     154        if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
     155  	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
     156      }									     \
     157    while (0)
     158  #define THREAD_GSCOPE_SET_FLAG() \
     159    do									     \
     160      {									     \
     161        THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
     162        atomic_write_barrier ();						     \
     163      }									     \
     164    while (0)
     165  
     166  #endif /* __ASSEMBLER__ */
     167  
     168  #endif	/* tls.h */