Home | History | Annotate | Line # | Download | only in libgcc
generic-morestack.c revision 1.7
      1 /* Library support for -fsplit-stack.  */
      2 /* Copyright (C) 2009-2018 Free Software Foundation, Inc.
      3    Contributed by Ian Lance Taylor <iant (at) google.com>.
      4 
      5 This file is part of GCC.
      6 
      7 GCC is free software; you can redistribute it and/or modify it under
      8 the terms of the GNU General Public License as published by the Free
      9 Software Foundation; either version 3, or (at your option) any later
     10 version.
     11 
     12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
     13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15 for more details.
     16 
     17 Under Section 7 of GPL version 3, you are granted additional
     18 permissions described in the GCC Runtime Library Exception, version
     19 3.1, as published by the Free Software Foundation.
     20 
     21 You should have received a copy of the GNU General Public License and
     22 a copy of the GCC Runtime Library Exception along with this program;
     23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 <http://www.gnu.org/licenses/>.  */
     25 
     26 /* powerpc 32-bit not supported.  */
     27 #if !defined __powerpc__ || defined __powerpc64__
     28 
     29 #include "tconfig.h"
     30 #include "tsystem.h"
     31 #include "coretypes.h"
     32 #include "tm.h"
     33 #include "libgcc_tm.h"
     34 
     35 /* If inhibit_libc is defined, we can not compile this file.  The
     36    effect is that people will not be able to use -fsplit-stack.  That
     37    is much better than failing the build particularly since people
     38    will want to define inhibit_libc while building a compiler which
     39    can build glibc.  */
     40 
     41 #ifndef inhibit_libc
     42 
     43 #include <assert.h>
     44 #include <errno.h>
     45 #include <signal.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include <sys/mman.h>
     50 #include <sys/uio.h>
     51 
     52 #include "generic-morestack.h"
     53 
     54 typedef unsigned uintptr_type __attribute__ ((mode (pointer)));
     55 
     56 /* This file contains subroutines that are used by code compiled with
     57    -fsplit-stack.  */
     58 
     59 /* Declare functions to avoid warnings--there is no header file for
     60    these internal functions.  We give most of these functions the
     61    flatten attribute in order to minimize their stack usage--here we
     62    must minimize stack usage even at the cost of code size, and in
     63    general inlining everything will do that.  */
     64 
     65 extern void
     66 __generic_morestack_set_initial_sp (void *sp, size_t len)
     67   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     68 
     69 extern void *
     70 __generic_morestack (size_t *frame_size, void *old_stack, size_t param_size)
     71   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     72 
     73 extern void *
     74 __generic_releasestack (size_t *pavailable)
     75   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     76 
     77 extern void
     78 __morestack_block_signals (void)
     79   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     80 
     81 extern void
     82 __morestack_unblock_signals (void)
     83   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     84 
     85 extern size_t
     86 __generic_findstack (void *stack)
     87   __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
     88 
     89 extern void
     90 __morestack_load_mmap (void)
     91   __attribute__ ((no_split_stack, visibility ("hidden")));
     92 
     93 extern void *
     94 __morestack_allocate_stack_space (size_t size)
     95   __attribute__ ((visibility ("hidden")));
     96 
     97 /* These are functions which -fsplit-stack code can call.  These are
     98    not called by the compiler, and are not hidden.  FIXME: These
     99    should be in some header file somewhere, somehow.  */
    100 
    101 extern void *
    102 __splitstack_find (void *, void *, size_t *, void **, void **, void **)
    103   __attribute__ ((visibility ("default")));
    104 
    105 extern void
    106 __splitstack_block_signals (int *, int *)
    107   __attribute__ ((visibility ("default")));
    108 
    109 extern void
    110 __splitstack_getcontext (void *context[10])
    111   __attribute__ ((no_split_stack, visibility ("default")));
    112 
    113 extern void
    114 __splitstack_setcontext (void *context[10])
    115   __attribute__ ((no_split_stack, visibility ("default")));
    116 
    117 extern void *
    118 __splitstack_makecontext (size_t, void *context[10], size_t *)
    119   __attribute__ ((visibility ("default")));
    120 
    121 extern void *
    122 __splitstack_resetcontext (void *context[10], size_t *)
    123   __attribute__ ((visibility ("default")));
    124 
    125 extern void
    126 __splitstack_releasecontext (void *context[10])
    127   __attribute__ ((visibility ("default")));
    128 
    129 extern void
    130 __splitstack_block_signals_context (void *context[10], int *, int *)
    131   __attribute__ ((visibility ("default")));
    132 
    133 extern void *
    134 __splitstack_find_context (void *context[10], size_t *, void **, void **,
    135 			   void **)
    136   __attribute__ ((visibility ("default")));
    137 
    138 /* These functions must be defined by the processor specific code.  */
    139 
    140 extern void *__morestack_get_guard (void)
    141   __attribute__ ((no_split_stack, visibility ("hidden")));
    142 
    143 extern void __morestack_set_guard (void *)
    144   __attribute__ ((no_split_stack, visibility ("hidden")));
    145 
    146 extern void *__morestack_make_guard (void *, size_t)
    147   __attribute__ ((no_split_stack, visibility ("hidden")));
    148 
    149 /* When we allocate a stack segment we put this header at the
    150    start.  */
    151 
    152 struct stack_segment
    153 {
    154   /* The previous stack segment--when a function running on this stack
    155      segment returns, it will run on the previous one.  */
    156   struct stack_segment *prev;
    157   /* The next stack segment, if it has been allocated--when a function
    158      is running on this stack segment, the next one is not being
    159      used.  */
    160   struct stack_segment *next;
    161   /* The total size of this stack segment.  */
    162   size_t size;
    163   /* The stack address when this stack was created.  This is used when
    164      popping the stack.  */
    165   void *old_stack;
    166   /* A list of memory blocks allocated by dynamic stack
    167      allocation.  */
    168   struct dynamic_allocation_blocks *dynamic_allocation;
    169   /* A list of dynamic memory blocks no longer needed.  */
    170   struct dynamic_allocation_blocks *free_dynamic_allocation;
    171   /* An extra pointer in case we need some more information some
    172      day.  */
    173   void *extra;
    174 };
    175 
    176 /* This structure holds the (approximate) initial stack pointer and
    177    size for the system supplied stack for a thread.  This is set when
    178    the thread is created.  We also store a sigset_t here to hold the
    179    signal mask while splitting the stack, since we don't want to store
    180    that on the stack.  */
    181 
    182 struct initial_sp
    183 {
    184   /* The initial stack pointer.  */
    185   void *sp;
    186   /* The stack length.  */
    187   size_t len;
    188   /* A signal mask, put here so that the thread can use it without
    189      needing stack space.  */
    190   sigset_t mask;
    191   /* Non-zero if we should not block signals.  This is a reversed flag
    192      so that the default zero value is the safe value.  The type is
    193      uintptr_type because it replaced one of the void * pointers in
    194      extra.  */
    195   uintptr_type dont_block_signals;
    196   /* Some extra space for later extensibility.  */
    197   void *extra[4];
    198 };
    199 
    200 /* A list of memory blocks allocated by dynamic stack allocation.
    201    This is used for code that calls alloca or uses variably sized
    202    arrays.  */
    203 
    204 struct dynamic_allocation_blocks
    205 {
    206   /* The next block in the list.  */
    207   struct dynamic_allocation_blocks *next;
    208   /* The size of the allocated memory.  */
    209   size_t size;
    210   /* The allocated memory.  */
    211   void *block;
    212 };
    213 
    214 /* These thread local global variables must be shared by all split
    215    stack code across shared library boundaries.  Therefore, they have
    216    default visibility.  They have extensibility fields if needed for
    217    new versions.  If more radical changes are needed, new code can be
    218    written using new variable names, while still using the existing
    219    variables in a backward compatible manner.  Symbol versioning is
    220    also used, although, since these variables are only referenced by
    221    code in this file and generic-morestack-thread.c, it is likely that
    222    simply using new names will suffice.  */
    223 
    224 /* The first stack segment allocated for this thread.  */
    225 
    226 __thread struct stack_segment *__morestack_segments
    227   __attribute__ ((visibility ("default")));
    228 
    229 /* The stack segment that we think we are currently using.  This will
    230    be correct in normal usage, but will be incorrect if an exception
    231    unwinds into a different stack segment or if longjmp jumps to a
    232    different stack segment.  */
    233 
    234 __thread struct stack_segment *__morestack_current_segment
    235   __attribute__ ((visibility ("default")));
    236 
    237 /* The initial stack pointer and size for this thread.  */
    238 
    239 __thread struct initial_sp __morestack_initial_sp
    240   __attribute__ ((visibility ("default")));
    241 
    242 /* A static signal mask, to avoid taking up stack space.  */
    243 
    244 static sigset_t __morestack_fullmask;
    245 
    246 /* Page size, as returned from getpagesize(). Set on startup. */
    247 static unsigned int static_pagesize;
    248 
    249 /* Set on startup to non-zero value if SPLIT_STACK_GUARD env var is set. */
    250 static int use_guard_page;
    251 
    252 /* Convert an integer to a decimal string without using much stack
    253    space.  Return a pointer to the part of the buffer to use.  We this
    254    instead of sprintf because sprintf will require too much stack
    255    space.  */
    256 
    257 static char *
    258 print_int (int val, char *buf, int buflen, size_t *print_len)
    259 {
    260   int is_negative;
    261   int i;
    262   unsigned int uval;
    263 
    264   uval = (unsigned int) val;
    265   if (val >= 0)
    266     is_negative = 0;
    267   else
    268     {
    269       is_negative = 1;
    270       uval = - uval;
    271     }
    272 
    273   i = buflen;
    274   do
    275     {
    276       --i;
    277       buf[i] = '0' + (uval % 10);
    278       uval /= 10;
    279     }
    280   while (uval != 0 && i > 0);
    281 
    282   if (is_negative)
    283     {
    284       if (i > 0)
    285 	--i;
    286       buf[i] = '-';
    287     }
    288 
    289   *print_len = buflen - i;
    290   return buf + i;
    291 }
    292 
    293 /* Print the string MSG/LEN, the errno number ERR, and a newline on
    294    stderr.  Then crash.  */
    295 
    296 void
    297 __morestack_fail (const char *, size_t, int) __attribute__ ((noreturn));
    298 
    299 void
    300 __morestack_fail (const char *msg, size_t len, int err)
    301 {
    302   char buf[24];
    303   static const char nl[] = "\n";
    304   struct iovec iov[3];
    305   union { char *p; const char *cp; } const_cast;
    306 
    307   const_cast.cp = msg;
    308   iov[0].iov_base = const_cast.p;
    309   iov[0].iov_len = len;
    310   /* We can't call strerror, because it may try to translate the error
    311      message, and that would use too much stack space.  */
    312   iov[1].iov_base = print_int (err, buf, sizeof buf, &iov[1].iov_len);
    313   const_cast.cp = &nl[0];
    314   iov[2].iov_base = const_cast.p;
    315   iov[2].iov_len = sizeof nl - 1;
    316   /* FIXME: On systems without writev we need to issue three write
    317      calls, or punt on printing errno.  For now this is irrelevant
    318      since stack splitting only works on GNU/Linux anyhow.  */
    319   writev (2, iov, 3);
    320   abort ();
    321 }
    322 
    323 /* Allocate a new stack segment.  FRAME_SIZE is the required frame
    324    size.  */
    325 
    326 static struct stack_segment *
    327 allocate_segment (size_t frame_size)
    328 {
    329   unsigned int pagesize;
    330   unsigned int overhead;
    331   unsigned int allocate;
    332   void *space;
    333   struct stack_segment *pss;
    334 
    335   pagesize = static_pagesize;
    336   overhead = sizeof (struct stack_segment);
    337 
    338   allocate = pagesize;
    339   if (allocate < MINSIGSTKSZ)
    340     allocate = ((MINSIGSTKSZ + overhead + pagesize - 1)
    341 		& ~ (pagesize - 1));
    342   if (allocate < frame_size)
    343     allocate = ((frame_size + overhead + pagesize - 1)
    344 		& ~ (pagesize - 1));
    345 
    346   if (use_guard_page)
    347     allocate += pagesize;
    348 
    349   /* FIXME: If this binary requires an executable stack, then we need
    350      to set PROT_EXEC.  Unfortunately figuring that out is complicated
    351      and target dependent.  We would need to use dl_iterate_phdr to
    352      see if there is any object which does not have a PT_GNU_STACK
    353      phdr, though only for architectures which use that mechanism.  */
    354   space = mmap (NULL, allocate, PROT_READ | PROT_WRITE,
    355 		MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    356   if (space == MAP_FAILED)
    357     {
    358       static const char msg[] =
    359 	"unable to allocate additional stack space: errno ";
    360       __morestack_fail (msg, sizeof msg - 1, errno);
    361     }
    362 
    363   if (use_guard_page)
    364     {
    365       void *guard;
    366 
    367 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    368       guard = space;
    369       space = (char *) space + pagesize;
    370 #else
    371       guard = space + allocate - pagesize;
    372 #endif
    373 
    374       mprotect (guard, pagesize, PROT_NONE);
    375       allocate -= pagesize;
    376     }
    377 
    378   pss = (struct stack_segment *) space;
    379 
    380   pss->prev = NULL;
    381   pss->next = NULL;
    382   pss->size = allocate - overhead;
    383   pss->dynamic_allocation = NULL;
    384   pss->free_dynamic_allocation = NULL;
    385   pss->extra = NULL;
    386 
    387   return pss;
    388 }
    389 
    390 /* Free a list of dynamic blocks.  */
    391 
    392 static void
    393 free_dynamic_blocks (struct dynamic_allocation_blocks *p)
    394 {
    395   while (p != NULL)
    396     {
    397       struct dynamic_allocation_blocks *next;
    398 
    399       next = p->next;
    400       free (p->block);
    401       free (p);
    402       p = next;
    403     }
    404 }
    405 
    406 /* Merge two lists of dynamic blocks.  */
    407 
    408 static struct dynamic_allocation_blocks *
    409 merge_dynamic_blocks (struct dynamic_allocation_blocks *a,
    410 		      struct dynamic_allocation_blocks *b)
    411 {
    412   struct dynamic_allocation_blocks **pp;
    413 
    414   if (a == NULL)
    415     return b;
    416   if (b == NULL)
    417     return a;
    418   for (pp = &a->next; *pp != NULL; pp = &(*pp)->next)
    419     ;
    420   *pp = b;
    421   return a;
    422 }
    423 
    424 /* Release stack segments.  If FREE_DYNAMIC is non-zero, we also free
    425    any dynamic blocks.  Otherwise we return them.  */
    426 
    427 struct dynamic_allocation_blocks *
    428 __morestack_release_segments (struct stack_segment **pp, int free_dynamic)
    429 {
    430   struct dynamic_allocation_blocks *ret;
    431   struct stack_segment *pss;
    432 
    433   ret = NULL;
    434   pss = *pp;
    435   while (pss != NULL)
    436     {
    437       struct stack_segment *next;
    438       unsigned int allocate;
    439 
    440       next = pss->next;
    441 
    442       if (pss->dynamic_allocation != NULL
    443 	  || pss->free_dynamic_allocation != NULL)
    444 	{
    445 	  if (free_dynamic)
    446 	    {
    447 	      free_dynamic_blocks (pss->dynamic_allocation);
    448 	      free_dynamic_blocks (pss->free_dynamic_allocation);
    449 	    }
    450 	  else
    451 	    {
    452 	      ret = merge_dynamic_blocks (pss->dynamic_allocation, ret);
    453 	      ret = merge_dynamic_blocks (pss->free_dynamic_allocation, ret);
    454 	    }
    455 	}
    456 
    457       allocate = pss->size + sizeof (struct stack_segment);
    458       if (munmap (pss, allocate) < 0)
    459 	{
    460 	  static const char msg[] = "munmap of stack space failed: errno ";
    461 	  __morestack_fail (msg, sizeof msg - 1, errno);
    462 	}
    463 
    464       pss = next;
    465     }
    466   *pp = NULL;
    467 
    468   return ret;
    469 }
    470 
    471 /* This function is called by a processor specific function to set the
    472    initial stack pointer for a thread.  The operating system will
    473    always create a stack for a thread.  Here we record a stack pointer
    474    near the base of that stack.  The size argument lets the processor
    475    specific code estimate how much stack space is available on this
    476    initial stack.  */
    477 
    478 void
    479 __generic_morestack_set_initial_sp (void *sp, size_t len)
    480 {
    481   /* The stack pointer most likely starts on a page boundary.  Adjust
    482      to the nearest 512 byte boundary.  It's not essential that we be
    483      precise here; getting it wrong will just leave some stack space
    484      unused.  */
    485 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    486   sp = (void *) ((((__UINTPTR_TYPE__) sp + 511U) / 512U) * 512U);
    487 #else
    488   sp = (void *) ((((__UINTPTR_TYPE__) sp - 511U) / 512U) * 512U);
    489 #endif
    490 
    491   __morestack_initial_sp.sp = sp;
    492   __morestack_initial_sp.len = len;
    493   sigemptyset (&__morestack_initial_sp.mask);
    494 
    495   sigfillset (&__morestack_fullmask);
    496 #if defined(__GLIBC__) && defined(__linux__)
    497   /* In glibc, the first two real time signals are used by the NPTL
    498      threading library.  By taking them out of the set of signals, we
    499      avoiding copying the signal mask in pthread_sigmask.  More
    500      importantly, pthread_sigmask uses less stack space on x86_64.  */
    501   sigdelset (&__morestack_fullmask, __SIGRTMIN);
    502   sigdelset (&__morestack_fullmask, __SIGRTMIN + 1);
    503 #endif
    504 }
    505 
    506 /* This function is called by a processor specific function which is
    507    run in the prologue when more stack is needed.  The processor
    508    specific function handles the details of saving registers and
    509    frobbing the actual stack pointer.  This function is responsible
    510    for allocating a new stack segment and for copying a parameter
    511    block from the old stack to the new one.  On function entry
    512    *PFRAME_SIZE is the size of the required stack frame--the returned
    513    stack must be at least this large.  On function exit *PFRAME_SIZE
    514    is the amount of space remaining on the allocated stack.  OLD_STACK
    515    points at the parameters the old stack (really the current one
    516    while this function is running).  OLD_STACK is saved so that it can
    517    be returned by a later call to __generic_releasestack.  PARAM_SIZE
    518    is the size in bytes of parameters to copy to the new stack.  This
    519    function returns a pointer to the new stack segment, pointing to
    520    the memory after the parameters have been copied.  The returned
    521    value minus the returned *PFRAME_SIZE (or plus if the stack grows
    522    upward) is the first address on the stack which should not be used.
    523 
    524    This function is running on the old stack and has only a limited
    525    amount of stack space available.  */
    526 
    527 void *
    528 __generic_morestack (size_t *pframe_size, void *old_stack, size_t param_size)
    529 {
    530   size_t frame_size = *pframe_size;
    531   struct stack_segment *current;
    532   struct stack_segment **pp;
    533   struct dynamic_allocation_blocks *dynamic;
    534   char *from;
    535   char *to;
    536   void *ret;
    537   size_t i;
    538   size_t aligned;
    539 
    540   current = __morestack_current_segment;
    541 
    542   pp = current != NULL ? &current->next : &__morestack_segments;
    543   if (*pp != NULL && (*pp)->size < frame_size)
    544     dynamic = __morestack_release_segments (pp, 0);
    545   else
    546     dynamic = NULL;
    547   current = *pp;
    548 
    549   if (current == NULL)
    550     {
    551       current = allocate_segment (frame_size + param_size);
    552       current->prev = __morestack_current_segment;
    553       *pp = current;
    554     }
    555 
    556   current->old_stack = old_stack;
    557 
    558   __morestack_current_segment = current;
    559 
    560   if (dynamic != NULL)
    561     {
    562       /* Move the free blocks onto our list.  We don't want to call
    563 	 free here, as we are short on stack space.  */
    564       current->free_dynamic_allocation =
    565 	merge_dynamic_blocks (dynamic, current->free_dynamic_allocation);
    566     }
    567 
    568   *pframe_size = current->size - param_size;
    569 
    570   /* Align the returned stack to a 32-byte boundary.  */
    571   aligned = (param_size + 31) & ~ (size_t) 31;
    572 
    573 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    574   {
    575     char *bottom = (char *) (current + 1) + current->size;
    576     to = bottom - aligned;
    577     ret = bottom - aligned;
    578   }
    579 #else
    580   to = current + 1;
    581   to += aligned - param_size;
    582   ret = (char *) (current + 1) + aligned;
    583 #endif
    584 
    585   /* We don't call memcpy to avoid worrying about the dynamic linker
    586      trying to resolve it.  */
    587   from = (char *) old_stack;
    588   for (i = 0; i < param_size; i++)
    589     *to++ = *from++;
    590 
    591   return ret;
    592 }
    593 
    594 /* This function is called by a processor specific function when it is
    595    ready to release a stack segment.  We don't actually release the
    596    stack segment, we just move back to the previous one.  The current
    597    stack segment will still be available if we need it in
    598    __generic_morestack.  This returns a pointer to the new stack
    599    segment to use, which is the one saved by a previous call to
    600    __generic_morestack.  The processor specific function is then
    601    responsible for actually updating the stack pointer.  This sets
    602    *PAVAILABLE to the amount of stack space now available.  */
    603 
    604 void *
    605 __generic_releasestack (size_t *pavailable)
    606 {
    607   struct stack_segment *current;
    608   void *old_stack;
    609 
    610   current = __morestack_current_segment;
    611   old_stack = current->old_stack;
    612   current = current->prev;
    613   __morestack_current_segment = current;
    614 
    615   if (current != NULL)
    616     {
    617 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    618       *pavailable = (char *) old_stack - (char *) (current + 1);
    619 #else
    620       *pavailable = (char *) (current + 1) + current->size - (char *) old_stack;
    621 #endif
    622     }
    623   else
    624     {
    625       size_t used;
    626 
    627       /* We have popped back to the original stack.  */
    628 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    629       if ((char *) old_stack >= (char *) __morestack_initial_sp.sp)
    630 	used = 0;
    631       else
    632 	used = (char *) __morestack_initial_sp.sp - (char *) old_stack;
    633 #else
    634       if ((char *) old_stack <= (char *) __morestack_initial_sp.sp)
    635 	used = 0;
    636       else
    637 	used = (char *) old_stack - (char *) __morestack_initial_sp.sp;
    638 #endif
    639 
    640       if (used > __morestack_initial_sp.len)
    641 	*pavailable = 0;
    642       else
    643 	*pavailable = __morestack_initial_sp.len - used;
    644     }
    645 
    646   return old_stack;
    647 }
    648 
    649 /* Block signals while splitting the stack.  This avoids trouble if we
    650    try to invoke a signal handler which itself wants to split the
    651    stack.  */
    652 
    653 extern int pthread_sigmask (int, const sigset_t *, sigset_t *)
    654   __attribute__ ((weak));
    655 
    656 void
    657 __morestack_block_signals (void)
    658 {
    659   if (__morestack_initial_sp.dont_block_signals)
    660     ;
    661   else if (pthread_sigmask)
    662     pthread_sigmask (SIG_BLOCK, &__morestack_fullmask,
    663 		     &__morestack_initial_sp.mask);
    664   else
    665     sigprocmask (SIG_BLOCK, &__morestack_fullmask,
    666 		 &__morestack_initial_sp.mask);
    667 }
    668 
    669 /* Unblock signals while splitting the stack.  */
    670 
    671 void
    672 __morestack_unblock_signals (void)
    673 {
    674   if (__morestack_initial_sp.dont_block_signals)
    675     ;
    676   else if (pthread_sigmask)
    677     pthread_sigmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
    678   else
    679     sigprocmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
    680 }
    681 
    682 /* This function is called to allocate dynamic stack space, for alloca
    683    or a variably sized array.  This is a regular function with
    684    sufficient stack space, so we just use malloc to allocate the
    685    space.  We attach the allocated blocks to the current stack
    686    segment, so that they will eventually be reused or freed.  */
    687 
    688 void *
    689 __morestack_allocate_stack_space (size_t size)
    690 {
    691   struct stack_segment *seg, *current;
    692   struct dynamic_allocation_blocks *p;
    693 
    694   /* We have to block signals to avoid getting confused if we get
    695      interrupted by a signal whose handler itself uses alloca or a
    696      variably sized array.  */
    697   __morestack_block_signals ();
    698 
    699   /* Since we don't want to call free while we are low on stack space,
    700      we may have a list of already allocated blocks waiting to be
    701      freed.  Release them all, unless we find one that is large
    702      enough.  We don't look at every block to see if one is large
    703      enough, just the first one, because we aren't trying to build a
    704      memory allocator here, we're just trying to speed up common
    705      cases.  */
    706 
    707   current = __morestack_current_segment;
    708   p = NULL;
    709   for (seg = __morestack_segments; seg != NULL; seg = seg->next)
    710     {
    711       p = seg->free_dynamic_allocation;
    712       if (p != NULL)
    713 	{
    714 	  if (p->size >= size)
    715 	    {
    716 	      seg->free_dynamic_allocation = p->next;
    717 	      break;
    718 	    }
    719 
    720 	  free_dynamic_blocks (p);
    721 	  seg->free_dynamic_allocation = NULL;
    722 	  p = NULL;
    723 	}
    724     }
    725 
    726   if (p == NULL)
    727     {
    728       /* We need to allocate additional memory.  */
    729       p = malloc (sizeof (*p));
    730       if (p == NULL)
    731 	abort ();
    732       p->size = size;
    733       p->block = malloc (size);
    734       if (p->block == NULL)
    735 	abort ();
    736     }
    737 
    738   /* If we are still on the initial stack, then we have a space leak.
    739      FIXME.  */
    740   if (current != NULL)
    741     {
    742       p->next = current->dynamic_allocation;
    743       current->dynamic_allocation = p;
    744     }
    745 
    746   __morestack_unblock_signals ();
    747 
    748   return p->block;
    749 }
    750 
    751 /* Find the stack segment for STACK and return the amount of space
    752    available.  This is used when unwinding the stack because of an
    753    exception, in order to reset the stack guard correctly.  */
    754 
    755 size_t
    756 __generic_findstack (void *stack)
    757 {
    758   struct stack_segment *pss;
    759   size_t used;
    760 
    761   for (pss = __morestack_current_segment; pss != NULL; pss = pss->prev)
    762     {
    763       if ((char *) pss < (char *) stack
    764 	  && (char *) pss + pss->size > (char *) stack)
    765 	{
    766 	  __morestack_current_segment = pss;
    767 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    768 	  return (char *) stack - (char *) (pss + 1);
    769 #else
    770 	  return (char *) (pss + 1) + pss->size - (char *) stack;
    771 #endif
    772 	}
    773     }
    774 
    775   /* We have popped back to the original stack.  */
    776 
    777   if (__morestack_initial_sp.sp == NULL)
    778     return 0;
    779 
    780 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    781   if ((char *) stack >= (char *) __morestack_initial_sp.sp)
    782     used = 0;
    783   else
    784     used = (char *) __morestack_initial_sp.sp - (char *) stack;
    785 #else
    786   if ((char *) stack <= (char *) __morestack_initial_sp.sp)
    787     used = 0;
    788   else
    789     used = (char *) stack - (char *) __morestack_initial_sp.sp;
    790 #endif
    791 
    792   if (used > __morestack_initial_sp.len)
    793     return 0;
    794   else
    795     return __morestack_initial_sp.len - used;
    796 }
    797 
    798 /* This function is called at program startup time to make sure that
    799    mmap, munmap, and getpagesize are resolved if linking dynamically.
    800    We want to resolve them while we have enough stack for them, rather
    801    than calling into the dynamic linker while low on stack space.
    802    Similarly, invoke getenv here to check for split-stack related control
    803    variables, since doing do as part of the __morestack path can result
    804    in unwanted use of SSE/AVX registers (see GCC PR 86213). */
    805 
    806 void
    807 __morestack_load_mmap (void)
    808 {
    809   /* Call with bogus values to run faster.  We don't care if the call
    810      fails.  Pass __MORESTACK_CURRENT_SEGMENT to make sure that any
    811      TLS accessor function is resolved.  */
    812   mmap (__morestack_current_segment, 0, PROT_READ, MAP_ANONYMOUS, -1, 0);
    813   mprotect (NULL, 0, 0);
    814   munmap (0, static_pagesize);
    815 
    816   /* Initialize these values here, so as to avoid dynamic linker
    817      activity as part of a __morestack call. */
    818   static_pagesize = getpagesize();
    819   use_guard_page = getenv ("SPLIT_STACK_GUARD") != 0;
    820 }
    821 
    822 /* This function may be used to iterate over the stack segments.
    823    This can be called like this.
    824      void *next_segment = NULL;
    825      void *next_sp = NULL;
    826      void *initial_sp = NULL;
    827      void *stack;
    828      size_t stack_size;
    829      while ((stack = __splitstack_find (next_segment, next_sp, &stack_size,
    830                                         &next_segment, &next_sp,
    831 					&initial_sp)) != NULL)
    832        {
    833          // Stack segment starts at stack and is stack_size bytes long.
    834        }
    835 
    836    There is no way to iterate over the stack segments of a different
    837    thread.  However, what is permitted is for one thread to call this
    838    with the first two values NULL, to pass next_segment, next_sp, and
    839    initial_sp to a different thread, and then to suspend one way or
    840    another.  A different thread may run the subsequent
    841    __morestack_find iterations.  Of course, this will only work if the
    842    first thread is suspended during the __morestack_find iterations.
    843    If not, the second thread will be looking at the stack while it is
    844    changing, and anything could happen.
    845 
    846    FIXME: This should be declared in some header file, but where?  */
    847 
    848 void *
    849 __splitstack_find (void *segment_arg, void *sp, size_t *len,
    850 		   void **next_segment, void **next_sp,
    851 		   void **initial_sp)
    852 {
    853   struct stack_segment *segment;
    854   void *ret;
    855   char *nsp;
    856 
    857   if (segment_arg == (void *) (uintptr_type) 1)
    858     {
    859       char *isp = (char *) *initial_sp;
    860 
    861       if (isp == NULL)
    862 	return NULL;
    863 
    864       *next_segment = (void *) (uintptr_type) 2;
    865       *next_sp = NULL;
    866 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    867       if ((char *) sp >= isp)
    868 	return NULL;
    869       *len = (char *) isp - (char *) sp;
    870       return sp;
    871 #else
    872       if ((char *) sp <= (char *) isp)
    873 	return NULL;
    874       *len = (char *) sp - (char *) isp;
    875       return (void *) isp;
    876 #endif
    877     }
    878   else if (segment_arg == (void *) (uintptr_type) 2)
    879     return NULL;
    880   else if (segment_arg != NULL)
    881     segment = (struct stack_segment *) segment_arg;
    882   else
    883     {
    884       *initial_sp = __morestack_initial_sp.sp;
    885       segment = __morestack_current_segment;
    886       sp = (void *) &segment;
    887       while (1)
    888 	{
    889 	  if (segment == NULL)
    890 	    return __splitstack_find ((void *) (uintptr_type) 1, sp, len,
    891 				      next_segment, next_sp, initial_sp);
    892 	  if ((char *) sp >= (char *) (segment + 1)
    893 	      && (char *) sp <= (char *) (segment + 1) + segment->size)
    894 	    break;
    895 	  segment = segment->prev;
    896 	}
    897     }
    898 
    899   if (segment->prev == NULL)
    900     *next_segment = (void *) (uintptr_type) 1;
    901   else
    902     *next_segment = segment->prev;
    903 
    904   /* The old_stack value is the address of the function parameters of
    905      the function which called __morestack.  So if f1 called f2 which
    906      called __morestack, the stack looks like this:
    907 
    908          parameters       <- old_stack
    909          return in f1
    910 	 return in f2
    911 	 registers pushed by __morestack
    912 
    913      The registers pushed by __morestack may not be visible on any
    914      other stack, if we are being called by a signal handler
    915      immediately after the call to __morestack_unblock_signals.  We
    916      want to adjust our return value to include those registers.  This
    917      is target dependent.  */
    918 
    919   nsp = (char *) segment->old_stack;
    920 
    921   if (nsp == NULL)
    922     {
    923       /* We've reached the top of the stack.  */
    924       *next_segment = (void *) (uintptr_type) 2;
    925     }
    926   else
    927     {
    928 #if defined (__x86_64__)
    929       nsp -= 12 * sizeof (void *);
    930 #elif defined (__i386__)
    931       nsp -= 6 * sizeof (void *);
    932 #elif defined __powerpc64__
    933 #elif defined __s390x__
    934       nsp -= 2 * 160;
    935 #elif defined __s390__
    936       nsp -= 2 * 96;
    937 #else
    938 #error "unrecognized target"
    939 #endif
    940 
    941       *next_sp = (void *) nsp;
    942     }
    943 
    944 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
    945   *len = (char *) (segment + 1) + segment->size - (char *) sp;
    946   ret = (void *) sp;
    947 #else
    948   *len = (char *) sp - (char *) (segment + 1);
    949   ret = (void *) (segment + 1);
    950 #endif
    951 
    952   return ret;
    953 }
    954 
    955 /* Tell the split stack code whether it has to block signals while
    956    manipulating the stack.  This is for programs in which some threads
    957    block all signals.  If a thread already blocks signals, there is no
    958    need for the split stack code to block them as well.  If NEW is not
    959    NULL, then if *NEW is non-zero signals will be blocked while
    960    splitting the stack, otherwise they will not.  If OLD is not NULL,
    961    *OLD will be set to the old value.  */
    962 
    963 void
    964 __splitstack_block_signals (int *new, int *old)
    965 {
    966   if (old != NULL)
    967     *old = __morestack_initial_sp.dont_block_signals ? 0 : 1;
    968   if (new != NULL)
    969     __morestack_initial_sp.dont_block_signals = *new ? 0 : 1;
    970 }
    971 
    972 /* The offsets into the arrays used by __splitstack_getcontext and
    973    __splitstack_setcontext.  */
    974 
    975 enum __splitstack_context_offsets
    976 {
    977   MORESTACK_SEGMENTS = 0,
    978   CURRENT_SEGMENT = 1,
    979   CURRENT_STACK = 2,
    980   STACK_GUARD = 3,
    981   INITIAL_SP = 4,
    982   INITIAL_SP_LEN = 5,
    983   BLOCK_SIGNALS = 6,
    984 
    985   NUMBER_OFFSETS = 10
    986 };
    987 
    988 /* Get the current split stack context.  This may be used for
    989    coroutine switching, similar to getcontext.  The argument should
    990    have at least 10 void *pointers for extensibility, although we
    991    don't currently use all of them.  This would normally be called
    992    immediately before a call to getcontext or swapcontext or
    993    setjmp.  */
    994 
    995 void
    996 __splitstack_getcontext (void *context[NUMBER_OFFSETS])
    997 {
    998   memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
    999   context[MORESTACK_SEGMENTS] = (void *) __morestack_segments;
   1000   context[CURRENT_SEGMENT] = (void *) __morestack_current_segment;
   1001   context[CURRENT_STACK] = (void *) &context;
   1002   context[STACK_GUARD] = __morestack_get_guard ();
   1003   context[INITIAL_SP] = (void *) __morestack_initial_sp.sp;
   1004   context[INITIAL_SP_LEN] = (void *) (uintptr_type) __morestack_initial_sp.len;
   1005   context[BLOCK_SIGNALS] = (void *) __morestack_initial_sp.dont_block_signals;
   1006 }
   1007 
   1008 /* Set the current split stack context.  The argument should be a
   1009    context previously passed to __splitstack_getcontext.  This would
   1010    normally be called immediately after a call to getcontext or
   1011    swapcontext or setjmp if something jumped to it.  */
   1012 
   1013 void
   1014 __splitstack_setcontext (void *context[NUMBER_OFFSETS])
   1015 {
   1016   __morestack_segments = (struct stack_segment *) context[MORESTACK_SEGMENTS];
   1017   __morestack_current_segment =
   1018     (struct stack_segment *) context[CURRENT_SEGMENT];
   1019   __morestack_set_guard (context[STACK_GUARD]);
   1020   __morestack_initial_sp.sp = context[INITIAL_SP];
   1021   __morestack_initial_sp.len = (size_t) context[INITIAL_SP_LEN];
   1022   __morestack_initial_sp.dont_block_signals =
   1023     (uintptr_type) context[BLOCK_SIGNALS];
   1024 }
   1025 
   1026 /* Create a new split stack context.  This will allocate a new stack
   1027    segment which may be used by a coroutine.  STACK_SIZE is the
   1028    minimum size of the new stack.  The caller is responsible for
   1029    actually setting the stack pointer.  This would normally be called
   1030    before a call to makecontext, and the returned stack pointer and
   1031    size would be used to set the uc_stack field.  A function called
   1032    via makecontext on a stack created by __splitstack_makecontext may
   1033    not return.  Note that the returned pointer points to the lowest
   1034    address in the stack space, and thus may not be the value to which
   1035    to set the stack pointer.  */
   1036 
   1037 void *
   1038 __splitstack_makecontext (size_t stack_size, void *context[NUMBER_OFFSETS],
   1039 			  size_t *size)
   1040 {
   1041   struct stack_segment *segment;
   1042   void *initial_sp;
   1043 
   1044   memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
   1045   segment = allocate_segment (stack_size);
   1046   context[MORESTACK_SEGMENTS] = segment;
   1047   context[CURRENT_SEGMENT] = segment;
   1048 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
   1049   initial_sp = (void *) ((char *) (segment + 1) + segment->size);
   1050 #else
   1051   initial_sp = (void *) (segment + 1);
   1052 #endif
   1053   context[STACK_GUARD] = __morestack_make_guard (initial_sp, segment->size);
   1054   context[INITIAL_SP] = NULL;
   1055   context[INITIAL_SP_LEN] = 0;
   1056   *size = segment->size;
   1057   return (void *) (segment + 1);
   1058 }
   1059 
   1060 /* Given an existing split stack context, reset it back to the start
   1061    of the stack.  Return the stack pointer and size, appropriate for
   1062    use with makecontext.  This may be used if a coroutine exits, in
   1063    order to reuse the stack segments for a new coroutine.  */
   1064 
   1065 void *
   1066 __splitstack_resetcontext (void *context[10], size_t *size)
   1067 {
   1068   struct stack_segment *segment;
   1069   void *initial_sp;
   1070   size_t initial_size;
   1071   void *ret;
   1072 
   1073   /* Reset the context assuming that MORESTACK_SEGMENTS, INITIAL_SP
   1074      and INITIAL_SP_LEN are correct.  */
   1075 
   1076   segment = context[MORESTACK_SEGMENTS];
   1077   context[CURRENT_SEGMENT] = segment;
   1078   context[CURRENT_STACK] = NULL;
   1079   if (segment == NULL)
   1080     {
   1081       initial_sp = context[INITIAL_SP];
   1082       initial_size = (uintptr_type) context[INITIAL_SP_LEN];
   1083       ret = initial_sp;
   1084 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
   1085       ret = (void *) ((char *) ret - initial_size);
   1086 #endif
   1087     }
   1088   else
   1089     {
   1090 #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
   1091       initial_sp = (void *) ((char *) (segment + 1) + segment->size);
   1092 #else
   1093       initial_sp = (void *) (segment + 1);
   1094 #endif
   1095       initial_size = segment->size;
   1096       ret = (void *) (segment + 1);
   1097     }
   1098   context[STACK_GUARD] = __morestack_make_guard (initial_sp, initial_size);
   1099   context[BLOCK_SIGNALS] = NULL;
   1100   *size = initial_size;
   1101   return ret;
   1102 }
   1103 
   1104 /* Release all the memory associated with a splitstack context.  This
   1105    may be used if a coroutine exits and the associated stack should be
   1106    freed.  */
   1107 
   1108 void
   1109 __splitstack_releasecontext (void *context[10])
   1110 {
   1111   __morestack_release_segments (((struct stack_segment **)
   1112 				 &context[MORESTACK_SEGMENTS]),
   1113 				1);
   1114 }
   1115 
   1116 /* Like __splitstack_block_signals, but operating on CONTEXT, rather
   1117    than on the current state.  */
   1118 
   1119 void
   1120 __splitstack_block_signals_context (void *context[NUMBER_OFFSETS], int *new,
   1121 				    int *old)
   1122 {
   1123   if (old != NULL)
   1124     *old = ((uintptr_type) context[BLOCK_SIGNALS]) != 0 ? 0 : 1;
   1125   if (new != NULL)
   1126     context[BLOCK_SIGNALS] = (void *) (uintptr_type) (*new ? 0 : 1);
   1127 }
   1128 
   1129 /* Find the stack segments associated with a split stack context.
   1130    This will return the address of the first stack segment and set
   1131    *STACK_SIZE to its size.  It will set next_segment, next_sp, and
   1132    initial_sp which may be passed to __splitstack_find to find the
   1133    remaining segments.  */
   1134 
   1135 void *
   1136 __splitstack_find_context (void *context[NUMBER_OFFSETS], size_t *stack_size,
   1137 			   void **next_segment, void **next_sp,
   1138 			   void **initial_sp)
   1139 {
   1140   void *sp;
   1141   struct stack_segment *segment;
   1142 
   1143   *initial_sp = context[INITIAL_SP];
   1144 
   1145   sp = context[CURRENT_STACK];
   1146   if (sp == NULL)
   1147     {
   1148       /* Most likely this context was created but was never used.  The
   1149 	 value 2 is a code used by __splitstack_find to mean that we
   1150 	 have reached the end of the list of stacks.  */
   1151       *next_segment = (void *) (uintptr_type) 2;
   1152       *next_sp = NULL;
   1153       *initial_sp = NULL;
   1154       return NULL;
   1155     }
   1156 
   1157   segment = context[CURRENT_SEGMENT];
   1158   if (segment == NULL)
   1159     {
   1160       /* Most likely this context was saved by a thread which was not
   1161 	 created using __splistack_makecontext and which has never
   1162 	 split the stack.  The value 1 is a code used by
   1163 	 __splitstack_find to look at the initial stack.  */
   1164       segment = (struct stack_segment *) (uintptr_type) 1;
   1165     }
   1166 
   1167   return __splitstack_find (segment, sp, stack_size, next_segment, next_sp,
   1168 			    initial_sp);
   1169 }
   1170 
   1171 #endif /* !defined (inhibit_libc) */
   1172 #endif /* not powerpc 32-bit */
   1173