x86: Support IBT and SHSTK in Intel CET [BZ #21598]
[glibc.git] / mach / setup-thread.c
blob79ec4320b32c2220714e51dcb03dbc28644d5954
1 /* Copyright (C) 1991-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <mach.h>
19 #include <thread_state.h>
20 #include <string.h>
21 #include <mach/machine/vm_param.h>
22 #include <ldsodefs.h>
23 #include "sysdep.h" /* Defines stack direction. */
25 #define STACK_SIZE (16 * 1024 * 1024) /* 16MB, arbitrary. */
27 /* Give THREAD a stack and set it to run at PC when resumed.
28 If *STACK_SIZE is nonzero, that size of stack is allocated.
29 If *STACK_BASE is nonzero, that stack location is used.
30 If STACK_BASE is not null it is filled in with the chosen stack base.
31 If STACK_SIZE is not null it is filled in with the chosen stack size.
32 Regardless, an extra page of red zone is allocated off the end; this
33 is not included in *STACK_SIZE. */
35 kern_return_t
36 __mach_setup_thread (task_t task, thread_t thread, void *pc,
37 vm_address_t *stack_base, vm_size_t *stack_size)
39 kern_return_t error;
40 struct machine_thread_state ts;
41 mach_msg_type_number_t tssize = MACHINE_THREAD_STATE_COUNT;
42 vm_address_t stack;
43 vm_size_t size;
44 int anywhere;
45 tcbhead_t *tcb;
47 size = stack_size ? *stack_size ? : STACK_SIZE : STACK_SIZE;
48 stack = stack_base ? *stack_base ? : 0 : 0;
49 anywhere = !stack_base || !*stack_base;
51 error = __vm_allocate (task, &stack, size + __vm_page_size, anywhere);
52 if (error)
53 return error;
55 tcb = _dl_allocate_tls (NULL);
56 if (tcb == NULL)
57 return KERN_RESOURCE_SHORTAGE;
59 if (stack_size)
60 *stack_size = size;
62 memset (&ts, 0, sizeof (ts));
63 MACHINE_THREAD_STATE_SET_PC (&ts, pc);
64 #ifdef STACK_GROWTH_DOWN
65 if (stack_base)
66 *stack_base = stack + __vm_page_size;
67 ts.SP = stack + __vm_page_size + size;
68 #elif defined (STACK_GROWTH_UP)
69 if (stack_base)
70 *stack_base = stack;
71 ts.SP = stack;
72 stack += size;
73 #else
74 #error stack direction unknown
75 #endif
77 /* Create the red zone. */
78 if (error = __vm_protect (task, stack, __vm_page_size, 0, VM_PROT_NONE))
79 return error;
81 if (error = __thread_set_state (thread, MACHINE_NEW_THREAD_STATE_FLAVOR,
82 (natural_t *) &ts, tssize))
83 return error;
85 if (error = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
86 (natural_t *) &ts, &tssize))
87 return error;
88 assert (tssize == MACHINE_THREAD_STATE_COUNT);
90 _hurd_tls_new (thread, &ts, tcb);
92 error = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
93 (natural_t *) &ts, tssize);
95 return error;
98 weak_alias (__mach_setup_thread, mach_setup_thread)