]> git.saurik.com Git - apple/libc.git/blame - pthreads/thread_setup.c
Libc-391.5.22.tar.gz
[apple/libc.git] / pthreads / thread_setup.c
CommitLineData
9385eb3d
A
1/*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
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
A
53#include <architecture/ppc/cframe.h>
54#endif
55
56#include "pthread_internals.h"
57
58/*
59 * Set up the initial state of a MACH thread
60 */
61void
62_pthread_setup(pthread_t thread,
63 void (*routine)(pthread_t),
5b2abdfb
A
64 void *vsp, int suspended,
65 int needresume)
e9ce8d39
A
66{
67 kern_return_t r;
68 unsigned int count;
59e0d9fe 69#if defined(__ppc__) || defined(__ppc64__)
3d9156a7
A
70#if defined(__ppc__)
71 ppc_thread_state_t state = {0};
72 ppc_thread_state_t *ts = &state;
73 thread_state_flavor_t flavor = PPC_THREAD_STATE;
74 count = PPC_THREAD_STATE_COUNT;
75#elif defined(__ppc64__)
76 ppc_thread_state64_t state = {0};
77 ppc_thread_state64_t *ts = &state;
78 thread_state_flavor_t flavor = PPC_THREAD_STATE64;
79 count = PPC_THREAD_STATE64_COUNT;
80#endif
e9ce8d39
A
81 /*
82 * Set up PowerPC registers.
83 */
5b2abdfb
A
84 if (suspended) {
85 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
3d9156a7 86 flavor,
5b2abdfb
A
87 (thread_state_t) &state,
88 &count),
89 r);
90 }
3d9156a7 91 ts->srr0 = (uintptr_t)routine;
5b2abdfb 92 ts->r1 = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
3d9156a7 93 ts->r3 = (uintptr_t)thread;
5b2abdfb
A
94 /* Incase of needresume, suspend is always set */
95 if (suspended) {
96 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
3d9156a7 97 flavor,
5b2abdfb 98 (thread_state_t) &state,
3d9156a7 99 count),
5b2abdfb
A
100 r);
101 if (needresume)
102 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
103 r);
104 } else {
105 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
3d9156a7 106 flavor,
5b2abdfb 107 (thread_state_t) ts,
3d9156a7 108 count,
5b2abdfb
A
109 &thread->kernel_thread),
110 r);
111 }
e9ce8d39 112#elif defined(__i386__)
5b2abdfb 113 i386_thread_state_t state = {0};
e9ce8d39 114 i386_thread_state_t *ts = &state;
5b2abdfb 115 int *sp = vsp;
e9ce8d39
A
116
117 /*
118 * Set up i386 registers & function call.
119 */
120 count = i386_THREAD_STATE_COUNT;
5b2abdfb
A
121 if (suspended) {
122 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
123 i386_THREAD_STATE,
124 (thread_state_t) &state,
125 &count),
126 r);
127 }
e9ce8d39 128 ts->eip = (int) routine;
3d9156a7
A
129
130 /*
131 ** We need to simulate a 16-byte aligned stack frame as if we had
132 ** executed a call instruction. Since we're "pushing" one argument,
133 ** we need to adjust the pointer by 12 bytes (3 * sizeof (int *))
134 */
135
136 sp -= 3; /* make sure stack is aligned */
137 *--sp = (int) thread; /* argument to function */
138 *--sp = 0; /* fake return address */
139 ts->esp = (int) sp; /* set stack pointer */
5b2abdfb
A
140 /* Incase of needresume, suspend is always set */
141 if (suspended) {
142 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
143 i386_THREAD_STATE,
144 (thread_state_t) &state,
145 i386_THREAD_STATE_COUNT),
146 r);
147 if (needresume)
148 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
149 r);
150 } else {
151 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
152 i386_THREAD_STATE,
153 (thread_state_t) ts,
154 i386_THREAD_STATE_COUNT,
155 &thread->kernel_thread),
156 r);
157 }
e9ce8d39 158
8e029c65
A
159#elif defined(__x86_64__)
160 x86_thread_state64_t state = {0};
161 x86_thread_state64_t *ts = &state;
162 uintptr_t *sp = vsp;
163
164 /*
165 * Set up x86-64 registers & function call.
166 */
167 count = x86_THREAD_STATE64_COUNT;
168 if (suspended) {
169 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
170 x86_THREAD_STATE64,
171 (thread_state_t) &state,
172 &count),
173 r);
174 }
175 ts->rip = (uintptr_t) routine;
176
177 /*
178 ** We need to simulate a 16-byte aligned stack frame as if we had
179 ** executed a call instruction. The stack should already be aligned
180 ** before it comes to us and we don't need to push any arguments,
181 ** so we shouldn't need to change it.
182 */
183
184 ts->rdi = (uintptr_t) thread; /* argument to function */
185 *--sp = 0; /* fake return address */
186 ts->rsp = (uintptr_t) sp; /* set stack pointer */
187 /* Incase of needresume, suspend is always set */
188 if (suspended) {
189 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
190 x86_THREAD_STATE64,
191 (thread_state_t) &state,
192 x86_THREAD_STATE64_COUNT),
193 r);
194 if (needresume)
195 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
196 r);
197 } else {
198 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
199 x86_THREAD_STATE64,
200 (thread_state_t) ts,
201 x86_THREAD_STATE64_COUNT,
202 &thread->kernel_thread),
203 r);
204 }
205
e9ce8d39
A
206#else
207#error _pthread_setup not defined for this architecture
208#endif
209}