]> git.saurik.com Git - apple/libc.git/blame - pthreads/thread_setup.c
Libc-763.11.tar.gz
[apple/libc.git] / pthreads / thread_setup.c
CommitLineData
9385eb3d 1/*
b5d655f7 2 * Copyright (c) 2000-2003, 2008 Apple Inc. All rights reserved.
9385eb3d
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9385eb3d
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
e9ce8d39
A
23/*
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 *
43 */
44/*
45 * MkLinux
46 */
47
48/*
49 * Machine specific support for thread initialization
50 */
51
59e0d9fe 52#if defined(__ppc__) || defined(__ppc64__)
e9ce8d39 53#include <architecture/ppc/cframe.h>
b5d655f7
A
54#elif defined(__arm__)
55#include <architecture/arm/cframe.h>
e9ce8d39
A
56#endif
57
58#include "pthread_internals.h"
59
60/*
61 * Set up the initial state of a MACH thread
62 */
63void
64_pthread_setup(pthread_t thread,
65 void (*routine)(pthread_t),
5b2abdfb
A
66 void *vsp, int suspended,
67 int needresume)
e9ce8d39
A
68{
69 kern_return_t r;
70 unsigned int count;
59e0d9fe 71#if defined(__ppc__) || defined(__ppc64__)
3d9156a7
A
72#if defined(__ppc__)
73 ppc_thread_state_t state = {0};
74 ppc_thread_state_t *ts = &state;
75 thread_state_flavor_t flavor = PPC_THREAD_STATE;
76 count = PPC_THREAD_STATE_COUNT;
77#elif defined(__ppc64__)
78 ppc_thread_state64_t state = {0};
79 ppc_thread_state64_t *ts = &state;
80 thread_state_flavor_t flavor = PPC_THREAD_STATE64;
81 count = PPC_THREAD_STATE64_COUNT;
82#endif
e9ce8d39
A
83 /*
84 * Set up PowerPC registers.
85 */
5b2abdfb
A
86 if (suspended) {
87 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
3d9156a7 88 flavor,
5b2abdfb
A
89 (thread_state_t) &state,
90 &count),
91 r);
92 }
3d9156a7 93 ts->srr0 = (uintptr_t)routine;
5b2abdfb 94 ts->r1 = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
3d9156a7 95 ts->r3 = (uintptr_t)thread;
5b2abdfb
A
96 /* Incase of needresume, suspend is always set */
97 if (suspended) {
98 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
3d9156a7 99 flavor,
5b2abdfb 100 (thread_state_t) &state,
3d9156a7 101 count),
5b2abdfb
A
102 r);
103 if (needresume)
104 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
105 r);
106 } else {
107 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
3d9156a7 108 flavor,
5b2abdfb 109 (thread_state_t) ts,
3d9156a7 110 count,
5b2abdfb
A
111 &thread->kernel_thread),
112 r);
113 }
e9ce8d39 114#elif defined(__i386__)
5b2abdfb 115 i386_thread_state_t state = {0};
e9ce8d39 116 i386_thread_state_t *ts = &state;
5b2abdfb 117 int *sp = vsp;
e9ce8d39
A
118
119 /*
120 * Set up i386 registers & function call.
121 */
122 count = i386_THREAD_STATE_COUNT;
5b2abdfb
A
123 if (suspended) {
124 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
125 i386_THREAD_STATE,
126 (thread_state_t) &state,
127 &count),
128 r);
129 }
e9ce8d39 130 ts->eip = (int) routine;
3d9156a7
A
131
132 /*
133 ** We need to simulate a 16-byte aligned stack frame as if we had
134 ** executed a call instruction. Since we're "pushing" one argument,
135 ** we need to adjust the pointer by 12 bytes (3 * sizeof (int *))
136 */
137
138 sp -= 3; /* make sure stack is aligned */
139 *--sp = (int) thread; /* argument to function */
140 *--sp = 0; /* fake return address */
141 ts->esp = (int) sp; /* set stack pointer */
5b2abdfb
A
142 /* Incase of needresume, suspend is always set */
143 if (suspended) {
144 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
145 i386_THREAD_STATE,
146 (thread_state_t) &state,
147 i386_THREAD_STATE_COUNT),
148 r);
149 if (needresume)
150 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
151 r);
152 } else {
153 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
154 i386_THREAD_STATE,
155 (thread_state_t) ts,
156 i386_THREAD_STATE_COUNT,
157 &thread->kernel_thread),
158 r);
159 }
e9ce8d39 160
8e029c65
A
161#elif defined(__x86_64__)
162 x86_thread_state64_t state = {0};
163 x86_thread_state64_t *ts = &state;
164 uintptr_t *sp = vsp;
165
166 /*
167 * Set up x86-64 registers & function call.
168 */
169 count = x86_THREAD_STATE64_COUNT;
170 if (suspended) {
171 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
172 x86_THREAD_STATE64,
173 (thread_state_t) &state,
174 &count),
175 r);
176 }
177 ts->rip = (uintptr_t) routine;
178
179 /*
180 ** We need to simulate a 16-byte aligned stack frame as if we had
181 ** executed a call instruction. The stack should already be aligned
182 ** before it comes to us and we don't need to push any arguments,
183 ** so we shouldn't need to change it.
184 */
185
186 ts->rdi = (uintptr_t) thread; /* argument to function */
187 *--sp = 0; /* fake return address */
188 ts->rsp = (uintptr_t) sp; /* set stack pointer */
189 /* Incase of needresume, suspend is always set */
190 if (suspended) {
191 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
192 x86_THREAD_STATE64,
193 (thread_state_t) &state,
194 x86_THREAD_STATE64_COUNT),
195 r);
196 if (needresume)
197 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
198 r);
199 } else {
200 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
201 x86_THREAD_STATE64,
202 (thread_state_t) ts,
203 x86_THREAD_STATE64_COUNT,
204 &thread->kernel_thread),
205 r);
206 }
207
b5d655f7
A
208#elif defined(__arm__)
209 arm_thread_state_t state = {0};
210 arm_thread_state_t *ts = &state;
211 thread_state_flavor_t flavor = ARM_THREAD_STATE;
212 count = ARM_THREAD_STATE_COUNT;
213
214 if (suspended) {
215 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
216 flavor,
217 (thread_state_t) &state,
218 &count),
219 r);
220 }
221
222 ts->pc = (uintptr_t)routine;
223
224 if (ts->pc & 1) {
225 ts->pc &= ~1;
226 ts->cpsr |= 0x20; /* PSR_THUMB */
227 }
228
229 ts->sp = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
230 ts->r[0] = (uintptr_t)thread;
231
232 /* Incase of needresume, suspend is always set */
233 if (suspended) {
234 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
235 flavor,
236 (thread_state_t) &state,
237 count),
238 r);
239 if (needresume)
240 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
241 r);
242 } else {
243 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
244 flavor,
245 (thread_state_t) ts,
246 count,
247 &thread->kernel_thread),
248 r);
249 }
e9ce8d39
A
250#else
251#error _pthread_setup not defined for this architecture
252#endif
253}