2 * Copyright (c) 2000-2003, 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
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.
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
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.
49 * Machine specific support for thread initialization
52 #if defined(__ppc__) || defined(__ppc64__)
53 #include <architecture/ppc/cframe.h>
54 #elif defined(__arm__)
55 #include <architecture/arm/cframe.h>
58 #include "pthread_internals.h"
61 * Set up the initial state of a MACH thread
64 _pthread_setup(pthread_t thread
,
65 void (*routine
)(pthread_t
),
66 void *vsp
, int suspended
,
71 #if defined(__ppc__) || defined(__ppc64__)
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
;
84 * Set up PowerPC registers.
87 PTHREAD_MACH_CALL(thread_get_state(thread
->kernel_thread
,
89 (thread_state_t
) &state
,
93 ts
->srr0
= (uintptr_t)routine
;
94 ts
->r1
= (uintptr_t)vsp
- C_ARGSAVE_LEN
- C_RED_ZONE
;
95 ts
->r3
= (uintptr_t)thread
;
96 /* Incase of needresume, suspend is always set */
98 PTHREAD_MACH_CALL(thread_set_state(thread
->kernel_thread
,
100 (thread_state_t
) &state
,
104 PTHREAD_MACH_CALL(thread_resume(thread
->kernel_thread
),
107 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
111 &thread
->kernel_thread
),
114 #elif defined(__i386__)
115 i386_thread_state_t state
= {0};
116 i386_thread_state_t
*ts
= &state
;
120 * Set up i386 registers & function call.
122 count
= i386_THREAD_STATE_COUNT
;
124 PTHREAD_MACH_CALL(thread_get_state(thread
->kernel_thread
,
126 (thread_state_t
) &state
,
130 ts
->eip
= (int) routine
;
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 *))
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 */
142 /* Incase of needresume, suspend is always set */
144 PTHREAD_MACH_CALL(thread_set_state(thread
->kernel_thread
,
146 (thread_state_t
) &state
,
147 i386_THREAD_STATE_COUNT
),
150 PTHREAD_MACH_CALL(thread_resume(thread
->kernel_thread
),
153 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
156 i386_THREAD_STATE_COUNT
,
157 &thread
->kernel_thread
),
161 #elif defined(__x86_64__)
162 x86_thread_state64_t state
= {0};
163 x86_thread_state64_t
*ts
= &state
;
167 * Set up x86-64 registers & function call.
169 count
= x86_THREAD_STATE64_COUNT
;
171 PTHREAD_MACH_CALL(thread_get_state(thread
->kernel_thread
,
173 (thread_state_t
) &state
,
177 ts
->rip
= (uintptr_t) routine
;
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.
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 */
191 PTHREAD_MACH_CALL(thread_set_state(thread
->kernel_thread
,
193 (thread_state_t
) &state
,
194 x86_THREAD_STATE64_COUNT
),
197 PTHREAD_MACH_CALL(thread_resume(thread
->kernel_thread
),
200 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
203 x86_THREAD_STATE64_COUNT
,
204 &thread
->kernel_thread
),
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
;
215 PTHREAD_MACH_CALL(thread_get_state(thread
->kernel_thread
,
217 (thread_state_t
) &state
,
222 ts
->pc
= (uintptr_t)routine
;
226 ts
->cpsr
|= 0x20; /* PSR_THUMB */
229 ts
->sp
= (uintptr_t)vsp
- C_ARGSAVE_LEN
- C_RED_ZONE
;
230 ts
->r
[0] = (uintptr_t)thread
;
232 /* Incase of needresume, suspend is always set */
234 PTHREAD_MACH_CALL(thread_set_state(thread
->kernel_thread
,
236 (thread_state_t
) &state
,
240 PTHREAD_MACH_CALL(thread_resume(thread
->kernel_thread
),
243 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
247 &thread
->kernel_thread
),
251 #error _pthread_setup not defined for this architecture