Blame nptl/descr.h

Packit 6c4009
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _DESCR_H
Packit 6c4009
#define _DESCR_H	1
Packit 6c4009
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <sched.h>
Packit 6c4009
#include <setjmp.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <hp-timing.h>
Packit 6c4009
#include <list_t.h>
Packit 6c4009
#include <lowlevellock.h>
Packit 6c4009
#include <pthreaddef.h>
Packit 6c4009
#include <dl-sysdep.h>
Packit 6c4009
#include "../nptl_db/thread_db.h"
Packit 6c4009
#include <tls.h>
Packit 6c4009
#include <unwind.h>
Packit 6c4009
#include <bits/types/res_state.h>
Packit 6c4009
#include <kernel-features.h>
Packit 6c4009
Packit 6c4009
#ifndef TCB_ALIGNMENT
Packit 6c4009
# define TCB_ALIGNMENT	sizeof (double)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We keep thread specific data in a special data structure, a two-level
Packit 6c4009
   array.  The top-level array contains pointers to dynamically allocated
Packit 6c4009
   arrays of a certain number of data pointers.  So we can implement a
Packit 6c4009
   sparse array.  Each dynamic second-level array has
Packit 6c4009
        PTHREAD_KEY_2NDLEVEL_SIZE
Packit 6c4009
   entries.  This value shouldn't be too large.  */
Packit 6c4009
#define PTHREAD_KEY_2NDLEVEL_SIZE       32
Packit 6c4009
Packit 6c4009
/* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
Packit 6c4009
   keys in each subarray.  */
Packit 6c4009
#define PTHREAD_KEY_1STLEVEL_SIZE \
Packit 6c4009
  ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
Packit 6c4009
   / PTHREAD_KEY_2NDLEVEL_SIZE)
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Internal version of the buffer to store cancellation handler
Packit 6c4009
   information.  */
Packit 6c4009
struct pthread_unwind_buf
Packit 6c4009
{
Packit 6c4009
  struct
Packit 6c4009
  {
Packit 6c4009
    __jmp_buf jmp_buf;
Packit 6c4009
    int mask_was_saved;
Packit 6c4009
  } cancel_jmp_buf[1];
Packit 6c4009
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    /* This is the placeholder of the public version.  */
Packit 6c4009
    void *pad[4];
Packit 6c4009
Packit 6c4009
    struct
Packit 6c4009
    {
Packit 6c4009
      /* Pointer to the previous cleanup buffer.  */
Packit 6c4009
      struct pthread_unwind_buf *prev;
Packit 6c4009
Packit 6c4009
      /* Backward compatibility: state of the old-style cleanup
Packit 6c4009
	 handler at the time of the previous new-style cleanup handler
Packit 6c4009
	 installment.  */
Packit 6c4009
      struct _pthread_cleanup_buffer *cleanup;
Packit 6c4009
Packit 6c4009
      /* Cancellation type before the push call.  */
Packit 6c4009
      int canceltype;
Packit 6c4009
    } data;
Packit 6c4009
  } priv;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Opcodes and data types for communication with the signal handler to
Packit 6c4009
   change user/group IDs.  */
Packit 6c4009
struct xid_command
Packit 6c4009
{
Packit 6c4009
  int syscall_no;
Packit 6c4009
  long int id[3];
Packit 6c4009
  volatile int cntr;
Packit 6c4009
  volatile int error; /* -1: no call yet, 0: success seen, >0: error seen.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Data structure used by the kernel to find robust futexes.  */
Packit 6c4009
struct robust_list_head
Packit 6c4009
{
Packit 6c4009
  void *list;
Packit 6c4009
  long int futex_offset;
Packit 6c4009
  void *list_op_pending;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Data strcture used to handle thread priority protection.  */
Packit 6c4009
struct priority_protection_data
Packit 6c4009
{
Packit 6c4009
  int priomax;
Packit 6c4009
  unsigned int priomap[];
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Thread descriptor data structure.  */
Packit 6c4009
struct pthread
Packit 6c4009
{
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
#if !TLS_DTV_AT_TP
Packit 6c4009
    /* This overlaps the TCB as used for TLS without threads (see tls.h).  */
Packit 6c4009
    tcbhead_t header;
Packit 6c4009
#else
Packit 6c4009
    struct
Packit 6c4009
    {
Packit 6c4009
      /* multiple_threads is enabled either when the process has spawned at
Packit 6c4009
	 least one thread or when a single-threaded process cancels itself.
Packit 6c4009
	 This enables additional code to introduce locking before doing some
Packit 6c4009
	 compare_and_exchange operations and also enable cancellation points.
Packit 6c4009
	 The concepts of multiple threads and cancellation points ideally
Packit 6c4009
	 should be separate, since it is not necessary for multiple threads to
Packit 6c4009
	 have been created for cancellation points to be enabled, as is the
Packit 6c4009
	 case is when single-threaded process cancels itself.
Packit 6c4009
Packit 6c4009
	 Since enabling multiple_threads enables additional code in
Packit 6c4009
	 cancellation points and compare_and_exchange operations, there is a
Packit 6c4009
	 potential for an unneeded performance hit when it is enabled in a
Packit 6c4009
	 single-threaded, self-canceling process.  This is OK though, since a
Packit 6c4009
	 single-threaded process will enable async cancellation only when it
Packit 6c4009
	 looks to cancel itself and is hence going to end anyway.  */
Packit 6c4009
      int multiple_threads;
Packit 6c4009
      int gscope_flag;
Packit 6c4009
    } header;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
    /* This extra padding has no special purpose, and this structure layout
Packit 6c4009
       is private and subject to change without affecting the official ABI.
Packit 6c4009
       We just have it here in case it might be convenient for some
Packit 6c4009
       implementation-specific instrumentation hack or suchlike.  */
Packit 6c4009
    void *__padding[24];
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
  /* This descriptor's link on the `stack_used' or `__stack_user' list.  */
Packit 6c4009
  list_t list;
Packit 6c4009
Packit 6c4009
  /* Thread ID - which is also a 'is this thread descriptor (and
Packit 6c4009
     therefore stack) used' flag.  */
Packit 6c4009
  pid_t tid;
Packit 6c4009
Packit 6c4009
  /* Ununsed.  */
Packit 6c4009
  pid_t pid_ununsed;
Packit 6c4009
Packit 6c4009
  /* List of robust mutexes the thread is holding.  */
Packit 6c4009
#if __PTHREAD_MUTEX_HAVE_PREV
Packit 6c4009
  void *robust_prev;
Packit 6c4009
  struct robust_list_head robust_head;
Packit 6c4009
Packit 6c4009
  /* The list above is strange.  It is basically a double linked list
Packit 6c4009
     but the pointer to the next/previous element of the list points
Packit 6c4009
     in the middle of the object, the __next element.  Whenever
Packit 6c4009
     casting to __pthread_list_t we need to adjust the pointer
Packit 6c4009
     first.
Packit 6c4009
     These operations are effectively concurrent code in that the thread
Packit 6c4009
     can get killed at any point in time and the kernel takes over.  Thus,
Packit 6c4009
     the __next elements are a kind of concurrent list and we need to
Packit 6c4009
     enforce using compiler barriers that the individual operations happen
Packit 6c4009
     in such a way that the kernel always sees a consistent list.  The
Packit 6c4009
     backward links (ie, the __prev elements) are not used by the kernel.
Packit 6c4009
     FIXME We should use relaxed MO atomic operations here and signal fences
Packit 6c4009
     because this kind of concurrency is similar to synchronizing with a
Packit 6c4009
     signal handler.  */
Packit 6c4009
# define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
Packit 6c4009
Packit 6c4009
# define ENQUEUE_MUTEX_BOTH(mutex, val)					      \
Packit 6c4009
  do {									      \
Packit 6c4009
    __pthread_list_t *next = (__pthread_list_t *)			      \
Packit 6c4009
      ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul)   \
Packit 6c4009
       - QUEUE_PTR_ADJUST);						      \
Packit 6c4009
    next->__prev = (void *) &mutex->__data.__list.__next;		      \
Packit 6c4009
    mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF,		      \
Packit 6c4009
						 robust_head.list);	      \
Packit 6c4009
    mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head;	      \
Packit 6c4009
    /* Ensure that the new list entry is ready before we insert it.  */	      \
Packit 6c4009
    __asm ("" ::: "memory");						      \
Packit 6c4009
    THREAD_SETMEM (THREAD_SELF, robust_head.list,			      \
Packit 6c4009
		   (void *) (((uintptr_t) &mutex->__data.__list.__next)	      \
Packit 6c4009
			     | val));					      \
Packit 6c4009
  } while (0)
Packit 6c4009
# define DEQUEUE_MUTEX(mutex) \
Packit 6c4009
  do {									      \
Packit 6c4009
    __pthread_list_t *next = (__pthread_list_t *)			      \
Packit 6c4009
      ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul)	      \
Packit 6c4009
       - QUEUE_PTR_ADJUST);						      \
Packit 6c4009
    next->__prev = mutex->__data.__list.__prev;				      \
Packit 6c4009
    __pthread_list_t *prev = (__pthread_list_t *)			      \
Packit 6c4009
      ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul)	      \
Packit 6c4009
       - QUEUE_PTR_ADJUST);						      \
Packit 6c4009
    prev->__next = mutex->__data.__list.__next;				      \
Packit 6c4009
    /* Ensure that we remove the entry from the list before we change the     \
Packit 6c4009
       __next pointer of the entry, which is read by the kernel.  */	      \
Packit 6c4009
    __asm ("" ::: "memory");						      \
Packit 6c4009
    mutex->__data.__list.__prev = NULL;					      \
Packit 6c4009
    mutex->__data.__list.__next = NULL;					      \
Packit 6c4009
  } while (0)
Packit 6c4009
#else
Packit 6c4009
  union
Packit 6c4009
  {
Packit 6c4009
    __pthread_slist_t robust_list;
Packit 6c4009
    struct robust_list_head robust_head;
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
# define ENQUEUE_MUTEX_BOTH(mutex, val)					      \
Packit 6c4009
  do {									      \
Packit 6c4009
    mutex->__data.__list.__next						      \
Packit 6c4009
      = THREAD_GETMEM (THREAD_SELF, robust_list.__next);		      \
Packit 6c4009
    /* Ensure that the new list entry is ready before we insert it.  */	      \
Packit 6c4009
    __asm ("" ::: "memory");						      \
Packit 6c4009
    THREAD_SETMEM (THREAD_SELF, robust_list.__next,			      \
Packit 6c4009
		   (void *) (((uintptr_t) &mutex->__data.__list) | val));     \
Packit 6c4009
  } while (0)
Packit 6c4009
# define DEQUEUE_MUTEX(mutex) \
Packit 6c4009
  do {									      \
Packit 6c4009
    __pthread_slist_t *runp = (__pthread_slist_t *)			      \
Packit 6c4009
      (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
Packit 6c4009
    if (runp == &mutex->__data.__list)					      \
Packit 6c4009
      THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next);	      \
Packit 6c4009
    else								      \
Packit 6c4009
      {									      \
Packit 6c4009
	__pthread_slist_t *next = (__pthread_slist_t *)		      \
Packit 6c4009
	  (((uintptr_t) runp->__next) & ~1ul);				      \
Packit 6c4009
	while (next != &mutex->__data.__list)				      \
Packit 6c4009
	  {								      \
Packit 6c4009
	    runp = next;						      \
Packit 6c4009
	    next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
Packit 6c4009
	  }								      \
Packit 6c4009
									      \
Packit 6c4009
	runp->__next = next->__next;					      \
Packit 6c4009
	/* Ensure that we remove the entry from the list before we change the \
Packit 6c4009
	   __next pointer of the entry, which is read by the kernel.  */      \
Packit 6c4009
	    __asm ("" ::: "memory");					      \
Packit 6c4009
	mutex->__data.__list.__next = NULL;				      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
#endif
Packit 6c4009
#define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
Packit 6c4009
#define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
Packit 6c4009
Packit 6c4009
  /* List of cleanup buffers.  */
Packit 6c4009
  struct _pthread_cleanup_buffer *cleanup;
Packit 6c4009
Packit 6c4009
  /* Unwind information.  */
Packit 6c4009
  struct pthread_unwind_buf *cleanup_jmp_buf;
Packit 6c4009
#define HAVE_CLEANUP_JMP_BUF
Packit 6c4009
Packit 6c4009
  /* Flags determining processing of cancellation.  */
Packit 6c4009
  int cancelhandling;
Packit 6c4009
  /* Bit set if cancellation is disabled.  */
Packit 6c4009
#define CANCELSTATE_BIT		0
Packit 6c4009
#define CANCELSTATE_BITMASK	(0x01 << CANCELSTATE_BIT)
Packit 6c4009
  /* Bit set if asynchronous cancellation mode is selected.  */
Packit 6c4009
#define CANCELTYPE_BIT		1
Packit 6c4009
#define CANCELTYPE_BITMASK	(0x01 << CANCELTYPE_BIT)
Packit 6c4009
  /* Bit set if canceling has been initiated.  */
Packit 6c4009
#define CANCELING_BIT		2
Packit 6c4009
#define CANCELING_BITMASK	(0x01 << CANCELING_BIT)
Packit 6c4009
  /* Bit set if canceled.  */
Packit 6c4009
#define CANCELED_BIT		3
Packit 6c4009
#define CANCELED_BITMASK	(0x01 << CANCELED_BIT)
Packit 6c4009
  /* Bit set if thread is exiting.  */
Packit 6c4009
#define EXITING_BIT		4
Packit 6c4009
#define EXITING_BITMASK		(0x01 << EXITING_BIT)
Packit 6c4009
  /* Bit set if thread terminated and TCB is freed.  */
Packit 6c4009
#define TERMINATED_BIT		5
Packit 6c4009
#define TERMINATED_BITMASK	(0x01 << TERMINATED_BIT)
Packit 6c4009
  /* Bit set if thread is supposed to change XID.  */
Packit 6c4009
#define SETXID_BIT		6
Packit 6c4009
#define SETXID_BITMASK		(0x01 << SETXID_BIT)
Packit 6c4009
  /* Mask for the rest.  Helps the compiler to optimize.  */
Packit 6c4009
#define CANCEL_RESTMASK		0xffffff80
Packit 6c4009
Packit 6c4009
#define CANCEL_ENABLED_AND_CANCELED(value) \
Packit 6c4009
  (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK	      \
Packit 6c4009
	       | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
Packit 6c4009
#define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
Packit 6c4009
  (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK    \
Packit 6c4009
	       | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK))     \
Packit 6c4009
   == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
Packit 6c4009
Packit 6c4009
  /* Flags.  Including those copied from the thread attribute.  */
Packit 6c4009
  int flags;
Packit 6c4009
Packit 6c4009
  /* We allocate one block of references here.  This should be enough
Packit 6c4009
     to avoid allocating any memory dynamically for most applications.  */
Packit 6c4009
  struct pthread_key_data
Packit 6c4009
  {
Packit 6c4009
    /* Sequence number.  We use uintptr_t to not require padding on
Packit 6c4009
       32- and 64-bit machines.  On 64-bit machines it helps to avoid
Packit 6c4009
       wrapping, too.  */
Packit 6c4009
    uintptr_t seq;
Packit 6c4009
Packit 6c4009
    /* Data pointer.  */
Packit 6c4009
    void *data;
Packit 6c4009
  } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
Packit 6c4009
Packit 6c4009
  /* Two-level array for the thread-specific data.  */
Packit 6c4009
  struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
Packit 6c4009
Packit 6c4009
  /* Flag which is set when specific data is set.  */
Packit 6c4009
  bool specific_used;
Packit 6c4009
Packit 6c4009
  /* True if events must be reported.  */
Packit 6c4009
  bool report_events;
Packit 6c4009
Packit 6c4009
  /* True if the user provided the stack.  */
Packit 6c4009
  bool user_stack;
Packit 6c4009
Packit 6c4009
  /* True if thread must stop at startup time.  */
Packit 6c4009
  bool stopped_start;
Packit 6c4009
Packit 6c4009
  /* The parent's cancel handling at the time of the pthread_create
Packit 6c4009
     call.  This might be needed to undo the effects of a cancellation.  */
Packit 6c4009
  int parent_cancelhandling;
Packit 6c4009
Packit 6c4009
  /* Lock to synchronize access to the descriptor.  */
Packit 6c4009
  int lock;
Packit 6c4009
Packit 6c4009
  /* Lock for synchronizing setxid calls.  */
Packit 6c4009
  unsigned int setxid_futex;
Packit 6c4009
Packit Service 3e830d
#if HP_TIMING_AVAIL
Packit Service 3a2fe0
  hp_timing_t cpuclock_offset_ununsed;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* If the thread waits to join another one the ID of the latter is
Packit 6c4009
     stored here.
Packit 6c4009
Packit 6c4009
     In case a thread is detached this field contains a pointer of the
Packit 6c4009
     TCB if the thread itself.  This is something which cannot happen
Packit 6c4009
     in normal operation.  */
Packit 6c4009
  struct pthread *joinid;
Packit 6c4009
  /* Check whether a thread is detached.  */
Packit 6c4009
#define IS_DETACHED(pd) ((pd)->joinid == (pd))
Packit 6c4009
Packit 6c4009
  /* The result of the thread function.  */
Packit 6c4009
  void *result;
Packit 6c4009
Packit 6c4009
  /* Scheduling parameters for the new thread.  */
Packit 6c4009
  struct sched_param schedparam;
Packit 6c4009
  int schedpolicy;
Packit 6c4009
Packit 6c4009
  /* Start position of the code to be executed and the argument passed
Packit 6c4009
     to the function.  */
Packit 6c4009
  void *(*start_routine) (void *);
Packit 6c4009
  void *arg;
Packit 6c4009
Packit 6c4009
  /* Debug state.  */
Packit 6c4009
  td_eventbuf_t eventbuf;
Packit 6c4009
  /* Next descriptor with a pending event.  */
Packit 6c4009
  struct pthread *nextevent;
Packit 6c4009
Packit 6c4009
  /* Machine-specific unwind info.  */
Packit 6c4009
  struct _Unwind_Exception exc;
Packit 6c4009
Packit 6c4009
  /* If nonzero, pointer to the area allocated for the stack and guard. */
Packit 6c4009
  void *stackblock;
Packit 6c4009
  /* Size of the stackblock area including the guard.  */
Packit 6c4009
  size_t stackblock_size;
Packit 6c4009
  /* Size of the included guard area.  */
Packit 6c4009
  size_t guardsize;
Packit 6c4009
  /* This is what the user specified and what we will report.  */
Packit 6c4009
  size_t reported_guardsize;
Packit 6c4009
Packit 6c4009
  /* Thread Priority Protection data.  */
Packit 6c4009
  struct priority_protection_data *tpp;
Packit 6c4009
Packit 6c4009
  /* Resolver state.  */
Packit 6c4009
  struct __res_state res;
Packit 6c4009
Packit 6c4009
  /* Indicates whether is a C11 thread created by thrd_creat.  */
Packit 6c4009
  bool c11;
Packit 6c4009
Packit 6c4009
  /* This member must be last.  */
Packit 6c4009
  char end_padding[];
Packit 6c4009
Packit 6c4009
#define PTHREAD_STRUCT_END_PADDING \
Packit 6c4009
  (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
Packit 6c4009
} __attribute ((aligned (TCB_ALIGNMENT)));
Packit 6c4009
Packit 6c4009
Packit 6c4009
#endif	/* descr.h */