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