]> git.saurik.com Git - apple/libc.git/blob - pthreads/thread_setup.c
70aa4cd64573a074f32e4fd8e4c934a37558c699
[apple/libc.git] / pthreads / thread_setup.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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 */
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
52 #if defined(__ppc__)
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 */
61 void
62 _pthread_setup(pthread_t thread,
63 void (*routine)(pthread_t),
64 void *vsp, int suspended,
65 int needresume)
66 {
67 kern_return_t r;
68 unsigned int count;
69 #if defined(__ppc__)
70 struct ppc_thread_state state = {0};
71 struct ppc_thread_state *ts = &state;
72
73 /*
74 * Set up PowerPC registers.
75 */
76 count = PPC_THREAD_STATE_COUNT;
77 if (suspended) {
78 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
79 PPC_THREAD_STATE,
80 (thread_state_t) &state,
81 &count),
82 r);
83 }
84 ts->srr0 = (int) routine;
85 ts->r1 = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
86 ts->r3 = (int)thread;
87 /* Incase of needresume, suspend is always set */
88 if (suspended) {
89 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
90 PPC_THREAD_STATE,
91 (thread_state_t) &state,
92 PPC_THREAD_STATE_COUNT),
93 r);
94 if (needresume)
95 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
96 r);
97 } else {
98 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
99 PPC_THREAD_STATE,
100 (thread_state_t) ts,
101 PPC_THREAD_STATE_COUNT,
102 &thread->kernel_thread),
103 r);
104 }
105 #elif defined(__i386__)
106 i386_thread_state_t state = {0};
107 i386_thread_state_t *ts = &state;
108 int *sp = vsp;
109
110 /*
111 * Set up i386 registers & function call.
112 */
113 count = i386_THREAD_STATE_COUNT;
114 if (suspended) {
115 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
116 i386_THREAD_STATE,
117 (thread_state_t) &state,
118 &count),
119 r);
120 }
121 ts->eip = (int) routine;
122 *--sp = (int) thread; /* argument to function */
123 *--sp = 0; /* fake return address */
124 ts->esp = (int) sp; /* set stack pointer */
125 /* Incase of needresume, suspend is always set */
126 if (suspended) {
127 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
128 i386_THREAD_STATE,
129 (thread_state_t) &state,
130 i386_THREAD_STATE_COUNT),
131 r);
132 if (needresume)
133 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
134 r);
135 } else {
136 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
137 i386_THREAD_STATE,
138 (thread_state_t) ts,
139 i386_THREAD_STATE_COUNT,
140 &thread->kernel_thread),
141 r);
142 }
143
144 #else
145 #error _pthread_setup not defined for this architecture
146 #endif
147 }