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