1 /* Library support for -fsplit-stack. */
2 /* Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor <iant@google.com>.
5 This file is part of GCC.
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
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
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.
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/>. */
28 #include "coretypes.h"
30 #include "libgcc_tm.h"
32 /* If inhibit_libc is defined, we can not compile this file. The
33 effect is that people will not be able to use -fsplit-stack. That
34 is much better than failing the build particularly since people
35 will want to define inhibit_libc while building a compiler which
49 #include "generic-morestack.h"
51 typedef unsigned uintptr_type
__attribute__ ((mode (pointer
)));
53 /* This file contains subroutines that are used by code compiled with
56 /* Declare functions to avoid warnings--there is no header file for
57 these internal functions. We give most of these functions the
58 flatten attribute in order to minimize their stack usage--here we
59 must minimize stack usage even at the cost of code size, and in
60 general inlining everything will do that. */
63 __generic_morestack_set_initial_sp (void *sp
, size_t len
)
64 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
67 __generic_morestack (size_t *frame_size
, void *old_stack
, size_t param_size
)
68 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
71 __generic_releasestack (size_t *pavailable
)
72 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
75 __morestack_block_signals (void)
76 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
79 __morestack_unblock_signals (void)
80 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
83 __generic_findstack (void *stack
)
84 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
87 __morestack_load_mmap (void)
88 __attribute__ ((no_split_stack
, visibility ("hidden")));
91 __morestack_allocate_stack_space (size_t size
)
92 __attribute__ ((visibility ("hidden")));
94 /* These are functions which -fsplit-stack code can call. These are
95 not called by the compiler, and are not hidden. FIXME: These
96 should be in some header file somewhere, somehow. */
99 __splitstack_find (void *, void *, size_t *, void **, void **, void **)
100 __attribute__ ((visibility ("default")));
103 __splitstack_block_signals (int *, int *)
104 __attribute__ ((visibility ("default")));
107 __splitstack_getcontext (void *context
[10])
108 __attribute__ ((no_split_stack
, visibility ("default")));
111 __splitstack_setcontext (void *context
[10])
112 __attribute__ ((no_split_stack
, visibility ("default")));
115 __splitstack_makecontext (size_t, void *context
[10], size_t *)
116 __attribute__ ((visibility ("default")));
119 __splitstack_block_signals_context (void *context
[10], int *, int *)
120 __attribute__ ((visibility ("default")));
123 __splitstack_find_context (void *context
[10], size_t *, void **, void **,
125 __attribute__ ((visibility ("default")));
127 /* These functions must be defined by the processor specific code. */
129 extern void *__morestack_get_guard (void)
130 __attribute__ ((no_split_stack
, visibility ("hidden")));
132 extern void __morestack_set_guard (void *)
133 __attribute__ ((no_split_stack
, visibility ("hidden")));
135 extern void *__morestack_make_guard (void *, size_t)
136 __attribute__ ((no_split_stack
, visibility ("hidden")));
138 /* When we allocate a stack segment we put this header at the
143 /* The previous stack segment--when a function running on this stack
144 segment returns, it will run on the previous one. */
145 struct stack_segment
*prev
;
146 /* The next stack segment, if it has been allocated--when a function
147 is running on this stack segment, the next one is not being
149 struct stack_segment
*next
;
150 /* The total size of this stack segment. */
152 /* The stack address when this stack was created. This is used when
153 popping the stack. */
155 /* A list of memory blocks allocated by dynamic stack
157 struct dynamic_allocation_blocks
*dynamic_allocation
;
158 /* A list of dynamic memory blocks no longer needed. */
159 struct dynamic_allocation_blocks
*free_dynamic_allocation
;
160 /* An extra pointer in case we need some more information some
165 /* This structure holds the (approximate) initial stack pointer and
166 size for the system supplied stack for a thread. This is set when
167 the thread is created. We also store a sigset_t here to hold the
168 signal mask while splitting the stack, since we don't want to store
169 that on the stack. */
173 /* The initial stack pointer. */
175 /* The stack length. */
177 /* A signal mask, put here so that the thread can use it without
178 needing stack space. */
180 /* Non-zero if we should not block signals. This is a reversed flag
181 so that the default zero value is the safe value. The type is
182 uintptr_type because it replaced one of the void * pointers in
184 uintptr_type dont_block_signals
;
185 /* Some extra space for later extensibility. */
189 /* A list of memory blocks allocated by dynamic stack allocation.
190 This is used for code that calls alloca or uses variably sized
193 struct dynamic_allocation_blocks
195 /* The next block in the list. */
196 struct dynamic_allocation_blocks
*next
;
197 /* The size of the allocated memory. */
199 /* The allocated memory. */
203 /* These thread local global variables must be shared by all split
204 stack code across shared library boundaries. Therefore, they have
205 default visibility. They have extensibility fields if needed for
206 new versions. If more radical changes are needed, new code can be
207 written using new variable names, while still using the existing
208 variables in a backward compatible manner. Symbol versioning is
209 also used, although, since these variables are only referenced by
210 code in this file and generic-morestack-thread.c, it is likely that
211 simply using new names will suffice. */
213 /* The first stack segment allocated for this thread. */
215 __thread
struct stack_segment
*__morestack_segments
216 __attribute__ ((visibility ("default")));
218 /* The stack segment that we think we are currently using. This will
219 be correct in normal usage, but will be incorrect if an exception
220 unwinds into a different stack segment or if longjmp jumps to a
221 different stack segment. */
223 __thread
struct stack_segment
*__morestack_current_segment
224 __attribute__ ((visibility ("default")));
226 /* The initial stack pointer and size for this thread. */
228 __thread
struct initial_sp __morestack_initial_sp
229 __attribute__ ((visibility ("default")));
231 /* A static signal mask, to avoid taking up stack space. */
233 static sigset_t __morestack_fullmask
;
235 /* Convert an integer to a decimal string without using much stack
236 space. Return a pointer to the part of the buffer to use. We this
237 instead of sprintf because sprintf will require too much stack
241 print_int (int val
, char *buf
, int buflen
, size_t *print_len
)
247 uval
= (unsigned int) val
;
260 buf
[i
] = '0' + (uval
% 10);
263 while (uval
!= 0 && i
> 0);
272 *print_len
= buflen
- i
;
276 /* Print the string MSG/LEN, the errno number ERR, and a newline on
277 stderr. Then crash. */
280 __morestack_fail (const char *, size_t, int) __attribute__ ((noreturn
));
283 __morestack_fail (const char *msg
, size_t len
, int err
)
286 static const char nl
[] = "\n";
288 union { char *p
; const char *cp
; } const_cast;
291 iov
[0].iov_base
= const_cast.p
;
292 iov
[0].iov_len
= len
;
293 /* We can't call strerror, because it may try to translate the error
294 message, and that would use too much stack space. */
295 iov
[1].iov_base
= print_int (err
, buf
, sizeof buf
, &iov
[1].iov_len
);
296 const_cast.cp
= &nl
[0];
297 iov
[2].iov_base
= const_cast.p
;
298 iov
[2].iov_len
= sizeof nl
- 1;
299 /* FIXME: On systems without writev we need to issue three write
300 calls, or punt on printing errno. For now this is irrelevant
301 since stack splitting only works on GNU/Linux anyhow. */
306 /* Allocate a new stack segment. FRAME_SIZE is the required frame
309 static struct stack_segment
*
310 allocate_segment (size_t frame_size
)
312 static unsigned int static_pagesize
;
313 static int use_guard_page
;
314 unsigned int pagesize
;
315 unsigned int overhead
;
316 unsigned int allocate
;
318 struct stack_segment
*pss
;
320 pagesize
= static_pagesize
;
325 pagesize
= getpagesize ();
327 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
328 p
= __sync_val_compare_and_swap (&static_pagesize
, 0, pagesize
);
330 /* Just hope this assignment is atomic. */
331 static_pagesize
= pagesize
;
335 use_guard_page
= getenv ("SPLIT_STACK_GUARD") != 0;
337 /* FIXME: I'm not sure this assert should be in the released
339 assert (p
== 0 || p
== pagesize
);
342 overhead
= sizeof (struct stack_segment
);
345 if (allocate
< MINSIGSTKSZ
)
346 allocate
= ((MINSIGSTKSZ
+ overhead
+ pagesize
- 1)
348 if (allocate
< frame_size
)
349 allocate
= ((frame_size
+ overhead
+ pagesize
- 1)
353 allocate
+= pagesize
;
355 /* FIXME: If this binary requires an executable stack, then we need
356 to set PROT_EXEC. Unfortunately figuring that out is complicated
357 and target dependent. We would need to use dl_iterate_phdr to
358 see if there is any object which does not have a PT_GNU_STACK
359 phdr, though only for architectures which use that mechanism. */
360 space
= mmap (NULL
, allocate
, PROT_READ
| PROT_WRITE
,
361 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
362 if (space
== MAP_FAILED
)
364 static const char msg
[] =
365 "unable to allocate additional stack space: errno ";
366 __morestack_fail (msg
, sizeof msg
- 1, errno
);
373 #ifdef STACK_GROWS_DOWNWARD
375 space
= (char *) space
+ pagesize
;
377 guard
= space
+ allocate
- pagesize
;
380 mprotect (guard
, pagesize
, PROT_NONE
);
381 allocate
-= pagesize
;
384 pss
= (struct stack_segment
*) space
;
388 pss
->size
= allocate
- overhead
;
389 pss
->dynamic_allocation
= NULL
;
390 pss
->free_dynamic_allocation
= NULL
;
396 /* Free a list of dynamic blocks. */
399 free_dynamic_blocks (struct dynamic_allocation_blocks
*p
)
403 struct dynamic_allocation_blocks
*next
;
412 /* Merge two lists of dynamic blocks. */
414 static struct dynamic_allocation_blocks
*
415 merge_dynamic_blocks (struct dynamic_allocation_blocks
*a
,
416 struct dynamic_allocation_blocks
*b
)
418 struct dynamic_allocation_blocks
**pp
;
424 for (pp
= &a
->next
; *pp
!= NULL
; pp
= &(*pp
)->next
)
430 /* Release stack segments. If FREE_DYNAMIC is non-zero, we also free
431 any dynamic blocks. Otherwise we return them. */
433 struct dynamic_allocation_blocks
*
434 __morestack_release_segments (struct stack_segment
**pp
, int free_dynamic
)
436 struct dynamic_allocation_blocks
*ret
;
437 struct stack_segment
*pss
;
443 struct stack_segment
*next
;
444 unsigned int allocate
;
448 if (pss
->dynamic_allocation
!= NULL
449 || pss
->free_dynamic_allocation
!= NULL
)
453 free_dynamic_blocks (pss
->dynamic_allocation
);
454 free_dynamic_blocks (pss
->free_dynamic_allocation
);
458 ret
= merge_dynamic_blocks (pss
->dynamic_allocation
, ret
);
459 ret
= merge_dynamic_blocks (pss
->free_dynamic_allocation
, ret
);
463 allocate
= pss
->size
+ sizeof (struct stack_segment
);
464 if (munmap (pss
, allocate
) < 0)
466 static const char msg
[] = "munmap of stack space failed: errno ";
467 __morestack_fail (msg
, sizeof msg
- 1, errno
);
477 /* This function is called by a processor specific function to set the
478 initial stack pointer for a thread. The operating system will
479 always create a stack for a thread. Here we record a stack pointer
480 near the base of that stack. The size argument lets the processor
481 specific code estimate how much stack space is available on this
485 __generic_morestack_set_initial_sp (void *sp
, size_t len
)
487 /* The stack pointer most likely starts on a page boundary. Adjust
488 to the nearest 512 byte boundary. It's not essential that we be
489 precise here; getting it wrong will just leave some stack space
491 #ifdef STACK_GROWS_DOWNWARD
492 sp
= (void *) ((((__UINTPTR_TYPE__
) sp
+ 511U) / 512U) * 512U);
494 sp
= (void *) ((((__UINTPTR_TYPE__
) sp
- 511U) / 512U) * 512U);
497 __morestack_initial_sp
.sp
= sp
;
498 __morestack_initial_sp
.len
= len
;
499 sigemptyset (&__morestack_initial_sp
.mask
);
501 sigfillset (&__morestack_fullmask
);
503 /* On Linux, the first two real time signals are used by the NPTL
504 threading library. By taking them out of the set of signals, we
505 avoiding copying the signal mask in pthread_sigmask. More
506 importantly, pthread_sigmask uses less stack space on x86_64. */
507 sigdelset (&__morestack_fullmask
, __SIGRTMIN
);
508 sigdelset (&__morestack_fullmask
, __SIGRTMIN
+ 1);
512 /* This function is called by a processor specific function which is
513 run in the prologue when more stack is needed. The processor
514 specific function handles the details of saving registers and
515 frobbing the actual stack pointer. This function is responsible
516 for allocating a new stack segment and for copying a parameter
517 block from the old stack to the new one. On function entry
518 *PFRAME_SIZE is the size of the required stack frame--the returned
519 stack must be at least this large. On function exit *PFRAME_SIZE
520 is the amount of space remaining on the allocated stack. OLD_STACK
521 points at the parameters the old stack (really the current one
522 while this function is running). OLD_STACK is saved so that it can
523 be returned by a later call to __generic_releasestack. PARAM_SIZE
524 is the size in bytes of parameters to copy to the new stack. This
525 function returns a pointer to the new stack segment, pointing to
526 the memory after the parameters have been copied. The returned
527 value minus the returned *PFRAME_SIZE (or plus if the stack grows
528 upward) is the first address on the stack which should not be used.
530 This function is running on the old stack and has only a limited
531 amount of stack space available. */
534 __generic_morestack (size_t *pframe_size
, void *old_stack
, size_t param_size
)
536 size_t frame_size
= *pframe_size
;
537 struct stack_segment
*current
;
538 struct stack_segment
**pp
;
539 struct dynamic_allocation_blocks
*dynamic
;
545 current
= __morestack_current_segment
;
547 pp
= current
!= NULL
? ¤t
->next
: &__morestack_segments
;
548 if (*pp
!= NULL
&& (*pp
)->size
< frame_size
)
549 dynamic
= __morestack_release_segments (pp
, 0);
556 current
= allocate_segment (frame_size
+ param_size
);
557 current
->prev
= __morestack_current_segment
;
561 current
->old_stack
= old_stack
;
563 __morestack_current_segment
= current
;
567 /* Move the free blocks onto our list. We don't want to call
568 free here, as we are short on stack space. */
569 current
->free_dynamic_allocation
=
570 merge_dynamic_blocks (dynamic
, current
->free_dynamic_allocation
);
573 *pframe_size
= current
->size
- param_size
;
575 #ifdef STACK_GROWS_DOWNWARD
577 char *bottom
= (char *) (current
+ 1) + current
->size
;
578 to
= bottom
- param_size
;
579 ret
= bottom
- param_size
;
583 ret
= (char *) (current
+ 1) + param_size
;
586 /* We don't call memcpy to avoid worrying about the dynamic linker
587 trying to resolve it. */
588 from
= (char *) old_stack
;
589 for (i
= 0; i
< param_size
; i
++)
595 /* This function is called by a processor specific function when it is
596 ready to release a stack segment. We don't actually release the
597 stack segment, we just move back to the previous one. The current
598 stack segment will still be available if we need it in
599 __generic_morestack. This returns a pointer to the new stack
600 segment to use, which is the one saved by a previous call to
601 __generic_morestack. The processor specific function is then
602 responsible for actually updating the stack pointer. This sets
603 *PAVAILABLE to the amount of stack space now available. */
606 __generic_releasestack (size_t *pavailable
)
608 struct stack_segment
*current
;
611 current
= __morestack_current_segment
;
612 old_stack
= current
->old_stack
;
613 current
= current
->prev
;
614 __morestack_current_segment
= current
;
618 #ifdef STACK_GROWS_DOWNWARD
619 *pavailable
= (char *) old_stack
- (char *) (current
+ 1);
621 *pavailable
= (char *) (current
+ 1) + current
->size
- (char *) old_stack
;
628 /* We have popped back to the original stack. */
629 #ifdef STACK_GROWS_DOWNWARD
630 if ((char *) old_stack
>= (char *) __morestack_initial_sp
.sp
)
633 used
= (char *) __morestack_initial_sp
.sp
- (char *) old_stack
;
635 if ((char *) old_stack
<= (char *) __morestack_initial_sp
.sp
)
638 used
= (char *) old_stack
- (char *) __morestack_initial_sp
.sp
;
641 if (used
> __morestack_initial_sp
.len
)
644 *pavailable
= __morestack_initial_sp
.len
- used
;
650 /* Block signals while splitting the stack. This avoids trouble if we
651 try to invoke a signal handler which itself wants to split the
654 extern int pthread_sigmask (int, const sigset_t
*, sigset_t
*)
655 __attribute__ ((weak
));
658 __morestack_block_signals (void)
660 if (__morestack_initial_sp
.dont_block_signals
)
662 else if (pthread_sigmask
)
663 pthread_sigmask (SIG_BLOCK
, &__morestack_fullmask
,
664 &__morestack_initial_sp
.mask
);
666 sigprocmask (SIG_BLOCK
, &__morestack_fullmask
,
667 &__morestack_initial_sp
.mask
);
670 /* Unblock signals while splitting the stack. */
673 __morestack_unblock_signals (void)
675 if (__morestack_initial_sp
.dont_block_signals
)
677 else if (pthread_sigmask
)
678 pthread_sigmask (SIG_SETMASK
, &__morestack_initial_sp
.mask
, NULL
);
680 sigprocmask (SIG_SETMASK
, &__morestack_initial_sp
.mask
, NULL
);
683 /* This function is called to allocate dynamic stack space, for alloca
684 or a variably sized array. This is a regular function with
685 sufficient stack space, so we just use malloc to allocate the
686 space. We attach the allocated blocks to the current stack
687 segment, so that they will eventually be reused or freed. */
690 __morestack_allocate_stack_space (size_t size
)
692 struct stack_segment
*seg
, *current
;
693 struct dynamic_allocation_blocks
*p
;
695 /* We have to block signals to avoid getting confused if we get
696 interrupted by a signal whose handler itself uses alloca or a
697 variably sized array. */
698 __morestack_block_signals ();
700 /* Since we don't want to call free while we are low on stack space,
701 we may have a list of already allocated blocks waiting to be
702 freed. Release them all, unless we find one that is large
703 enough. We don't look at every block to see if one is large
704 enough, just the first one, because we aren't trying to build a
705 memory allocator here, we're just trying to speed up common
708 current
= __morestack_current_segment
;
710 for (seg
= __morestack_segments
; seg
!= NULL
; seg
= seg
->next
)
712 p
= seg
->free_dynamic_allocation
;
717 seg
->free_dynamic_allocation
= p
->next
;
721 free_dynamic_blocks (p
);
722 seg
->free_dynamic_allocation
= NULL
;
729 /* We need to allocate additional memory. */
730 p
= malloc (sizeof (*p
));
734 p
->block
= malloc (size
);
735 if (p
->block
== NULL
)
739 /* If we are still on the initial stack, then we have a space leak.
743 p
->next
= current
->dynamic_allocation
;
744 current
->dynamic_allocation
= p
;
747 __morestack_unblock_signals ();
752 /* Find the stack segment for STACK and return the amount of space
753 available. This is used when unwinding the stack because of an
754 exception, in order to reset the stack guard correctly. */
757 __generic_findstack (void *stack
)
759 struct stack_segment
*pss
;
762 for (pss
= __morestack_current_segment
; pss
!= NULL
; pss
= pss
->prev
)
764 if ((char *) pss
< (char *) stack
765 && (char *) pss
+ pss
->size
> (char *) stack
)
767 __morestack_current_segment
= pss
;
768 #ifdef STACK_GROWS_DOWNWARD
769 return (char *) stack
- (char *) (pss
+ 1);
771 return (char *) (pss
+ 1) + pss
->size
- (char *) stack
;
776 /* We have popped back to the original stack. */
778 if (__morestack_initial_sp
.sp
== NULL
)
781 #ifdef STACK_GROWS_DOWNWARD
782 if ((char *) stack
>= (char *) __morestack_initial_sp
.sp
)
785 used
= (char *) __morestack_initial_sp
.sp
- (char *) stack
;
787 if ((char *) stack
<= (char *) __morestack_initial_sp
.sp
)
790 used
= (char *) stack
- (char *) __morestack_initial_sp
.sp
;
793 if (used
> __morestack_initial_sp
.len
)
796 return __morestack_initial_sp
.len
- used
;
799 /* This function is called at program startup time to make sure that
800 mmap, munmap, and getpagesize are resolved if linking dynamically.
801 We want to resolve them while we have enough stack for them, rather
802 than calling into the dynamic linker while low on stack space. */
805 __morestack_load_mmap (void)
807 /* Call with bogus values to run faster. We don't care if the call
808 fails. Pass __MORESTACK_CURRENT_SEGMENT to make sure that any
809 TLS accessor function is resolved. */
810 mmap (__morestack_current_segment
, 0, PROT_READ
, MAP_ANONYMOUS
, -1, 0);
811 mprotect (NULL
, 0, 0);
812 munmap (0, getpagesize ());
815 /* This function may be used to iterate over the stack segments.
816 This can be called like this.
817 void *next_segment = NULL;
818 void *next_sp = NULL;
819 void *initial_sp = NULL;
822 while ((stack = __splitstack_find (next_segment, next_sp, &stack_size,
823 &next_segment, &next_sp,
824 &initial_sp)) != NULL)
826 // Stack segment starts at stack and is stack_size bytes long.
829 There is no way to iterate over the stack segments of a different
830 thread. However, what is permitted is for one thread to call this
831 with the first two values NULL, to pass next_segment, next_sp, and
832 initial_sp to a different thread, and then to suspend one way or
833 another. A different thread may run the subsequent
834 __morestack_find iterations. Of course, this will only work if the
835 first thread is suspended during the __morestack_find iterations.
836 If not, the second thread will be looking at the stack while it is
837 changing, and anything could happen.
839 FIXME: This should be declared in some header file, but where? */
842 __splitstack_find (void *segment_arg
, void *sp
, size_t *len
,
843 void **next_segment
, void **next_sp
,
846 struct stack_segment
*segment
;
850 if (segment_arg
== (void *) (uintptr_type
) 1)
852 char *isp
= (char *) *initial_sp
;
857 *next_segment
= (void *) (uintptr_type
) 2;
859 #ifdef STACK_GROWS_DOWNWARD
860 if ((char *) sp
>= isp
)
862 *len
= (char *) isp
- (char *) sp
;
865 if ((char *) sp
<= (char *) isp
)
867 *len
= (char *) sp
- (char *) isp
;
871 else if (segment_arg
== (void *) (uintptr_type
) 2)
873 else if (segment_arg
!= NULL
)
874 segment
= (struct stack_segment
*) segment_arg
;
877 *initial_sp
= __morestack_initial_sp
.sp
;
878 segment
= __morestack_current_segment
;
879 sp
= (void *) &segment
;
883 return __splitstack_find ((void *) (uintptr_type
) 1, sp
, len
,
884 next_segment
, next_sp
, initial_sp
);
885 if ((char *) sp
>= (char *) (segment
+ 1)
886 && (char *) sp
<= (char *) (segment
+ 1) + segment
->size
)
888 segment
= segment
->prev
;
892 if (segment
->prev
== NULL
)
893 *next_segment
= (void *) (uintptr_type
) 1;
895 *next_segment
= segment
->prev
;
897 /* The old_stack value is the address of the function parameters of
898 the function which called __morestack. So if f1 called f2 which
899 called __morestack, the stack looks like this:
901 parameters <- old_stack
904 registers pushed by __morestack
906 The registers pushed by __morestack may not be visible on any
907 other stack, if we are being called by a signal handler
908 immediately after the call to __morestack_unblock_signals. We
909 want to adjust our return value to include those registers. This
910 is target dependent. */
912 nsp
= (char *) segment
->old_stack
;
914 #if defined (__x86_64__)
915 nsp
-= 12 * sizeof (void *);
916 #elif defined (__i386__)
917 nsp
-= 6 * sizeof (void *);
919 #error "unrecognized target"
922 *next_sp
= (void *) nsp
;
924 #ifdef STACK_GROWS_DOWNWARD
925 *len
= (char *) (segment
+ 1) + segment
->size
- (char *) sp
;
928 *len
= (char *) sp
- (char *) (segment
+ 1);
929 ret
= (void *) (segment
+ 1);
935 /* Tell the split stack code whether it has to block signals while
936 manipulating the stack. This is for programs in which some threads
937 block all signals. If a thread already blocks signals, there is no
938 need for the split stack code to block them as well. If NEW is not
939 NULL, then if *NEW is non-zero signals will be blocked while
940 splitting the stack, otherwise they will not. If OLD is not NULL,
941 *OLD will be set to the old value. */
944 __splitstack_block_signals (int *new, int *old
)
947 *old
= __morestack_initial_sp
.dont_block_signals
? 0 : 1;
949 __morestack_initial_sp
.dont_block_signals
= *new ? 0 : 1;
952 /* The offsets into the arrays used by __splitstack_getcontext and
953 __splitstack_setcontext. */
955 enum __splitstack_context_offsets
957 MORESTACK_SEGMENTS
= 0,
968 /* Get the current split stack context. This may be used for
969 coroutine switching, similar to getcontext. The argument should
970 have at least 10 void *pointers for extensibility, although we
971 don't currently use all of them. This would normally be called
972 immediately before a call to getcontext or swapcontext or
976 __splitstack_getcontext (void *context
[NUMBER_OFFSETS
])
978 memset (context
, 0, NUMBER_OFFSETS
* sizeof (void *));
979 context
[MORESTACK_SEGMENTS
] = (void *) __morestack_segments
;
980 context
[CURRENT_SEGMENT
] = (void *) __morestack_current_segment
;
981 context
[CURRENT_STACK
] = (void *) &context
;
982 context
[STACK_GUARD
] = __morestack_get_guard ();
983 context
[INITIAL_SP
] = (void *) __morestack_initial_sp
.sp
;
984 context
[INITIAL_SP_LEN
] = (void *) (uintptr_type
) __morestack_initial_sp
.len
;
985 context
[BLOCK_SIGNALS
] = (void *) __morestack_initial_sp
.dont_block_signals
;
988 /* Set the current split stack context. The argument should be a
989 context previously passed to __splitstack_getcontext. This would
990 normally be called immediately after a call to getcontext or
991 swapcontext or setjmp if something jumped to it. */
994 __splitstack_setcontext (void *context
[NUMBER_OFFSETS
])
996 __morestack_segments
= (struct stack_segment
*) context
[MORESTACK_SEGMENTS
];
997 __morestack_current_segment
=
998 (struct stack_segment
*) context
[CURRENT_SEGMENT
];
999 __morestack_set_guard (context
[STACK_GUARD
]);
1000 __morestack_initial_sp
.sp
= context
[INITIAL_SP
];
1001 __morestack_initial_sp
.len
= (size_t) context
[INITIAL_SP_LEN
];
1002 __morestack_initial_sp
.dont_block_signals
=
1003 (uintptr_type
) context
[BLOCK_SIGNALS
];
1006 /* Create a new split stack context. This will allocate a new stack
1007 segment which may be used by a coroutine. STACK_SIZE is the
1008 minimum size of the new stack. The caller is responsible for
1009 actually setting the stack pointer. This would normally be called
1010 before a call to makecontext, and the returned stack pointer and
1011 size would be used to set the uc_stack field. A function called
1012 via makecontext on a stack created by __splitstack_makecontext may
1013 not return. Note that the returned pointer points to the lowest
1014 address in the stack space, and thus may not be the value to which
1015 to set the stack pointer. */
1018 __splitstack_makecontext (size_t stack_size
, void *context
[NUMBER_OFFSETS
],
1021 struct stack_segment
*segment
;
1024 memset (context
, 0, NUMBER_OFFSETS
* sizeof (void *));
1025 segment
= allocate_segment (stack_size
);
1026 context
[MORESTACK_SEGMENTS
] = segment
;
1027 context
[CURRENT_SEGMENT
] = segment
;
1028 #ifdef STACK_GROWS_DOWNWARD
1029 initial_sp
= (void *) ((char *) (segment
+ 1) + segment
->size
);
1031 initial_sp
= (void *) (segment
+ 1);
1033 context
[STACK_GUARD
] = __morestack_make_guard (initial_sp
, segment
->size
);
1034 context
[INITIAL_SP
] = NULL
;
1035 context
[INITIAL_SP_LEN
] = 0;
1036 *size
= segment
->size
;
1037 return (void *) (segment
+ 1);
1040 /* Like __splitstack_block_signals, but operating on CONTEXT, rather
1041 than on the current state. */
1044 __splitstack_block_signals_context (void *context
[NUMBER_OFFSETS
], int *new,
1048 *old
= ((uintptr_type
) context
[BLOCK_SIGNALS
]) != 0 ? 0 : 1;
1050 context
[BLOCK_SIGNALS
] = (void *) (uintptr_type
) (*new ? 0 : 1);
1053 /* Find the stack segments associated with a split stack context.
1054 This will return the address of the first stack segment and set
1055 *STACK_SIZE to its size. It will set next_segment, next_sp, and
1056 initial_sp which may be passed to __splitstack_find to find the
1057 remaining segments. */
1060 __splitstack_find_context (void *context
[NUMBER_OFFSETS
], size_t *stack_size
,
1061 void **next_segment
, void **next_sp
,
1065 struct stack_segment
*segment
;
1067 *initial_sp
= context
[INITIAL_SP
];
1069 sp
= context
[CURRENT_STACK
];
1072 /* Most likely this context was created but was never used. The
1073 value 2 is a code used by __splitstack_find to mean that we
1074 have reached the end of the list of stacks. */
1075 *next_segment
= (void *) (uintptr_type
) 2;
1081 segment
= context
[CURRENT_SEGMENT
];
1082 if (segment
== NULL
)
1084 /* Most likely this context was saved by a thread which was not
1085 created using __splistack_makecontext and which has never
1086 split the stack. The value 1 is a code used by
1087 __splitstack_find to look at the initial stack. */
1088 segment
= (struct stack_segment
*) (uintptr_type
) 1;
1091 return __splitstack_find (segment
, sp
, stack_size
, next_segment
, next_sp
,
1095 #endif /* !defined (inhibit_libc) */