]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/htl/pt-setup.c
hurd: Bump remaining LGPL2+ htl licences to LGPL 2.1+
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / htl / pt-setup.c
CommitLineData
33574c17
ST
1/* Setup thread stack. Hurd/i386 version.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
ad2b41bf
ST
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
33574c17
ST
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad2b41bf 13 Lesser General Public License for more details.
33574c17 14
ad2b41bf
ST
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
33574c17
ST
18
19#include <stdint.h>
20#include <assert.h>
21#include <mach.h>
22
23#include <pt-internal.h>
24
25/* The stack layout used on the i386 is:
26
27 -----------------
28 | ARG |
29 -----------------
30 | START_ROUTINE |
31 -----------------
32 | 0 |
33 -----------------
34 */
35
36/* Set up the stack for THREAD, such that it appears as if
37 START_ROUTINE and ARG were passed to the new thread's entry-point.
38 Return the stack pointer for the new thread. */
39static void *
40stack_setup (struct __pthread *thread,
41 void *(*start_routine) (void *), void *arg)
42{
43 error_t err;
44 uintptr_t *bottom, *top;
45
46 /* Calculate the top of the new stack. */
47 bottom = thread->stackaddr;
48 top = (uintptr_t *) ((uintptr_t) bottom + thread->stacksize
49 + ((thread->guardsize + __vm_page_size - 1)
50 / __vm_page_size) * __vm_page_size);
51
52 if (start_routine != NULL)
53 {
54 /* And then the call frame. */
55 top -= 3;
56 top = (uintptr_t *) ((uintptr_t) top & ~0xf);
57 top[2] = (uintptr_t) arg; /* Argument to START_ROUTINE. */
58 top[1] = (uintptr_t) start_routine;
59 top[0] = (uintptr_t) thread;
60 *--top = 0; /* Fake return address. */
61 }
62
63 if (thread->guardsize)
64 {
65 err = __vm_protect (__mach_task_self (), (vm_address_t) bottom,
66 thread->guardsize, 0, 0);
67 assert_perror (err);
68 }
69
70 return top;
71}
72
73int
74__pthread_setup (struct __pthread *thread,
75 void (*entry_point) (struct __pthread *, void *(*)(void *),
76 void *), void *(*start_routine) (void *),
77 void *arg)
78{
79 tcbhead_t *tcb;
80 error_t err;
81 mach_port_t ktid;
82
83 thread->mcontext.pc = entry_point;
84 thread->mcontext.sp = stack_setup (thread, start_routine, arg);
85
86 ktid = __mach_thread_self ();
87 if (thread->kernel_thread == ktid)
88 /* Fix up the TCB for the main thread. The C library has already
89 installed a TCB, which we want to keep using. This TCB must not
90 be freed so don't register it in the thread structure. On the
91 other hand, it's not yet possible to reliably release a TCB.
92 Leave the unused one registered so that it doesn't leak. The
93 only thing left to do is to correctly set the `self' member in
94 the already existing TCB. */
95 tcb = THREAD_SELF;
96 else
97 {
98 err = __thread_set_pcsptp (thread->kernel_thread,
99 1, thread->mcontext.pc,
100 1, thread->mcontext.sp,
101 1, thread->tcb);
102 assert_perror (err);
103 tcb = thread->tcb;
104 }
105 __mach_port_deallocate (__mach_task_self (), ktid);
106
107 tcb->self = thread->kernel_thread;
108
109 return 0;
110}