]> git.saurik.com Git - apple/libc.git/blob - pthreads/thread_setup.c
53c68a4ca7f57f30c28cfd5d85bcb900fd601ffc
[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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
27 * All Rights Reserved
28 *
29 * Permission to use, copy, modify, and distribute this software and
30 * its documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appears in all copies and
32 * that both the copyright notice and this permission notice appear in
33 * supporting documentation.
34 *
35 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
36 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE.
38 *
39 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
40 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
41 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
42 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
43 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 */
46 /*
47 * MkLinux
48 */
49
50 /*
51 * Machine specific support for thread initialization
52 */
53
54 #if defined(__ppc__) || defined(__ppc64__)
55 #include <architecture/ppc/cframe.h>
56 #endif
57
58 #include "pthread_internals.h"
59
60 /*
61 * Set up the initial state of a MACH thread
62 */
63 void
64 _pthread_setup(pthread_t thread,
65 void (*routine)(pthread_t),
66 void *vsp, int suspended,
67 int needresume)
68 {
69 kern_return_t r;
70 unsigned int count;
71 #if defined(__ppc__) || defined(__ppc64__)
72 struct ppc_thread_state state = {0};
73 struct ppc_thread_state *ts = &state;
74
75 /*
76 * Set up PowerPC registers.
77 */
78 count = PPC_THREAD_STATE_COUNT;
79 if (suspended) {
80 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
81 PPC_THREAD_STATE,
82 (thread_state_t) &state,
83 &count),
84 r);
85 }
86 ts->srr0 = (int) routine;
87 ts->r1 = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
88 ts->r3 = (int)thread;
89 /* Incase of needresume, suspend is always set */
90 if (suspended) {
91 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
92 PPC_THREAD_STATE,
93 (thread_state_t) &state,
94 PPC_THREAD_STATE_COUNT),
95 r);
96 if (needresume)
97 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
98 r);
99 } else {
100 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
101 PPC_THREAD_STATE,
102 (thread_state_t) ts,
103 PPC_THREAD_STATE_COUNT,
104 &thread->kernel_thread),
105 r);
106 }
107 #elif defined(__i386__)
108 i386_thread_state_t state = {0};
109 i386_thread_state_t *ts = &state;
110 int *sp = vsp;
111
112 /*
113 * Set up i386 registers & function call.
114 */
115 count = i386_THREAD_STATE_COUNT;
116 if (suspended) {
117 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
118 i386_THREAD_STATE,
119 (thread_state_t) &state,
120 &count),
121 r);
122 }
123 ts->eip = (int) routine;
124 *--sp = (int) thread; /* argument to function */
125 *--sp = 0; /* fake return address */
126 ts->esp = (int) sp; /* set stack pointer */
127 /* Incase of needresume, suspend is always set */
128 if (suspended) {
129 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
130 i386_THREAD_STATE,
131 (thread_state_t) &state,
132 i386_THREAD_STATE_COUNT),
133 r);
134 if (needresume)
135 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
136 r);
137 } else {
138 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
139 i386_THREAD_STATE,
140 (thread_state_t) ts,
141 i386_THREAD_STATE_COUNT,
142 &thread->kernel_thread),
143 r);
144 }
145
146 #else
147 #error _pthread_setup not defined for this architecture
148 #endif
149 }