1 | /* System dependent pthreads code. Hurd version. |
2 | Copyright (C) 2000-2024 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 <assert.h> |
20 | #include <stddef.h> |
21 | #include <stdint.h> |
22 | |
23 | #include <mach.h> |
24 | #include <mach/mig_support.h> |
25 | |
26 | #include <pt-internal.h> |
27 | #include <pthreadP.h> |
28 | |
29 | /* Initial thread structure used temporarily during initialization, so various |
30 | * functions can already work at least basically. */ |
31 | static struct __pthread init_thread; |
32 | |
33 | static void |
34 | reset_pthread_total (void) |
35 | { |
36 | /* Only current thread remains */ |
37 | __pthread_total = 1; |
38 | } |
39 | |
40 | /* This function is called from the Hurd-specific startup code. It |
41 | should return a new stack pointer for the main thread. The caller |
42 | will switch to this new stack before doing anything serious. */ |
43 | static void |
44 | _init_routine (void *stack) |
45 | { |
46 | struct __pthread *thread; |
47 | int err; |
48 | pthread_attr_t attr, *attrp = 0; |
49 | |
50 | if (GL (dl_pthread_threads) != NULL) |
51 | /* Already initialized */ |
52 | return; |
53 | |
54 | /* Initialize early thread structure. */ |
55 | init_thread.thread = 1; |
56 | ___pthread_self = &init_thread; |
57 | |
58 | /* Initialize the library. */ |
59 | ___pthread_init (); |
60 | |
61 | if (stack != NULL) |
62 | { |
63 | /* We are given a stack, use it. */ |
64 | |
65 | /* Get the stack area information */ |
66 | vm_address_t addr = (vm_address_t) stack; |
67 | vm_size_t vm_size; |
68 | vm_prot_t prot, max_prot; |
69 | vm_inherit_t inherit; |
70 | boolean_t is_shared; |
71 | memory_object_name_t obj; |
72 | vm_offset_t offset; |
73 | |
74 | if (__vm_region (__mach_task_self (), &addr, |
75 | &vm_size, &prot, &max_prot, &inherit, &is_shared, |
76 | &obj, &offset) == KERN_SUCCESS) |
77 | __mach_port_deallocate (__mach_task_self (), obj); |
78 | else |
79 | { |
80 | /* Uh. Assume at least a page. */ |
81 | vm_size = __vm_page_size; |
82 | #if _STACK_GROWS_DOWN |
83 | addr = (vm_address_t) stack - vm_size; |
84 | #else |
85 | addr = (vm_address_t) stack + vm_size; |
86 | #endif |
87 | } |
88 | |
89 | /* Avoid allocating another stack */ |
90 | attrp = &attr; |
91 | __pthread_attr_init (attr: attrp); |
92 | __pthread_attr_setstack (attrp, (void *) addr, vm_size); |
93 | } |
94 | |
95 | /* Create the pthread structure for the main thread (i.e. us). */ |
96 | err = __pthread_create_internal (&thread, attrp, 0, 0); |
97 | assert_perror (err); |
98 | |
99 | /* XXX The caller copies the command line arguments and the environment |
100 | to the new stack. Pretend it wasn't allocated so that it remains |
101 | valid if the main thread terminates. */ |
102 | thread->stack = 0; |
103 | thread->tcb = THREAD_SELF; |
104 | |
105 | #ifndef PAGESIZE |
106 | __pthread_default_attr.__guardsize = __vm_page_size; |
107 | #endif |
108 | |
109 | /* Copy over the thread-specific state */ |
110 | assert (!init_thread.thread_specifics); |
111 | memcpy (dest: &thread->static_thread_specifics, |
112 | src: &init_thread.static_thread_specifics, |
113 | n: sizeof (thread->static_thread_specifics)); |
114 | |
115 | ___pthread_self = thread; |
116 | |
117 | /* Decrease the number of threads, to take into account that the |
118 | signal thread (which will be created by the glibc startup code |
119 | when we return from here) shouldn't be seen as a user thread. */ |
120 | __pthread_total--; |
121 | |
122 | __pthread_atfork (NULL, NULL, child: reset_pthread_total); |
123 | |
124 | GL(dl_init_static_tls) = &__pthread_init_static_tls; |
125 | |
126 | /* Make MiG code thread aware. */ |
127 | __mig_init (thread->stackaddr); |
128 | } |
129 | |
130 | void |
131 | __pthread_initialize_minimal (void) |
132 | { |
133 | _init_routine (stack: __libc_stack_end); |
134 | } |
135 | |
136 | #ifdef SHARED |
137 | __attribute__ ((constructor)) |
138 | static void |
139 | dynamic_init_routine (void) |
140 | { |
141 | _init_routine (__libc_stack_end); |
142 | } |
143 | #endif |
144 | |