1 /* Setup thread stack. Hurd/x86_64 version.
2 Copyright (C) 2000-2023 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
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.
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
13 Lesser General Public License for more details.
14
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 <https://www.gnu.org/licenses/>. */
18
19 #include <stdint.h>
20 #include <assert.h>
21 #include <mach.h>
22 #include <hurd.h>
23
24 #include <thread_state.h>
25 #include <pt-internal.h>
26
27 /* Set up the stack for THREAD. Return the stack pointer
28 for the new thread. */
29 static void *
30 stack_setup (struct __pthread *thread)
31 {
32 error_t err;
33 uintptr_t bottom, top;
34
35 /* Calculate the top of the new stack. */
36 bottom = (uintptr_t) thread->stackaddr;
37 top = bottom + thread->stacksize + round_page (thread->guardsize);
38
39 if (thread->guardsize)
40 {
41 err = __vm_protect (__mach_task_self (), (vm_address_t) bottom,
42 thread->guardsize, 0, 0);
43 assert_perror (err);
44 }
45
46 return (void *) PTR_ALIGN_DOWN_8_16 (top);
47 }
48
49 int
50 __pthread_setup (struct __pthread *thread,
51 void (*entry_point) (struct __pthread *, void *(*)(void *),
52 void *), void *(*start_routine) (void *),
53 void *arg)
54 {
55 error_t err;
56 struct i386_thread_state state;
57 struct i386_fsgs_base_state fsgs_state;
58
59 if (thread->kernel_thread == __hurd_thread_self ())
60 /* Fix up the TCB for the main thread. The C library has already
61 installed a TCB, which we want to keep using. This TCB must not
62 be freed so don't register it in the thread structure. On the
63 other hand, it's not yet possible to reliably release a TCB.
64 Leave the unused one registered so that it doesn't leak. */
65 return 0;
66
67
68 thread->mcontext.pc = entry_point;
69 thread->mcontext.sp = stack_setup (thread);
70
71 /* Set up the state to call entry_point (thread, start_routine, arg) */
72 memset (&state, 0, sizeof (state));
73 state.ursp = (uintptr_t) thread->mcontext.sp;
74 state.rip = (uintptr_t) thread->mcontext.pc;
75 state.rdi = (uintptr_t) thread;
76 state.rsi = (uintptr_t) start_routine;
77 state.rdx = (uintptr_t) arg;
78
79 err = __thread_set_state (thread->kernel_thread, i386_THREAD_STATE,
80 (thread_state_t) &state,
81 i386_THREAD_STATE_COUNT);
82 assert_perror (err);
83
84 /* Set fs_base to the TCB pointer for the thread. */
85 memset (&fsgs_state, 0, sizeof (fsgs_state));
86 fsgs_state.fs_base = (uintptr_t) thread->tcb;
87 err = __thread_set_state (thread->kernel_thread, i386_FSGS_BASE_STATE,
88 (thread_state_t) &fsgs_state,
89 i386_FSGS_BASE_STATE_COUNT);
90 assert_perror (err);
91
92 return 0;
93 }