]> git.saurik.com Git - apple/libc.git/blob - pthreads/thread_setup.c
28c08f59c4cdc63f5ccab9909068ea404c345428
[apple/libc.git] / pthreads / thread_setup.c
1 /*
2 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both the copyright notice and this permission notice appear in
9 * supporting documentation.
10 *
11 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
12 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
16 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
18 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
19 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 */
22 /*
23 * MkLinux
24 */
25
26 /*
27 * Machine specific support for thread initialization
28 */
29
30 #if defined(__ppc__)
31 #include <architecture/ppc/cframe.h>
32 #endif
33
34 #include "pthread_internals.h"
35
36 /*
37 * Set up the initial state of a MACH thread
38 */
39 void
40 _pthread_setup(pthread_t thread,
41 void (*routine)(pthread_t),
42 void *vsp, int suspended,
43 int needresume)
44 {
45 kern_return_t r;
46 unsigned int count;
47 #if defined(__ppc__)
48 struct ppc_thread_state state = {0};
49 struct ppc_thread_state *ts = &state;
50
51 /*
52 * Set up PowerPC registers.
53 */
54 count = PPC_THREAD_STATE_COUNT;
55 if (suspended) {
56 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
57 PPC_THREAD_STATE,
58 (thread_state_t) &state,
59 &count),
60 r);
61 }
62 ts->srr0 = (int) routine;
63 ts->r1 = (uintptr_t)vsp - C_ARGSAVE_LEN - C_RED_ZONE;
64 ts->r3 = (int)thread;
65 /* Incase of needresume, suspend is always set */
66 if (suspended) {
67 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
68 PPC_THREAD_STATE,
69 (thread_state_t) &state,
70 PPC_THREAD_STATE_COUNT),
71 r);
72 if (needresume)
73 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
74 r);
75 } else {
76 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
77 PPC_THREAD_STATE,
78 (thread_state_t) ts,
79 PPC_THREAD_STATE_COUNT,
80 &thread->kernel_thread),
81 r);
82 }
83 #elif defined(__i386__)
84 i386_thread_state_t state = {0};
85 i386_thread_state_t *ts = &state;
86 int *sp = vsp;
87
88 /*
89 * Set up i386 registers & function call.
90 */
91 count = i386_THREAD_STATE_COUNT;
92 if (suspended) {
93 PTHREAD_MACH_CALL(thread_get_state(thread->kernel_thread,
94 i386_THREAD_STATE,
95 (thread_state_t) &state,
96 &count),
97 r);
98 }
99 ts->eip = (int) routine;
100 *--sp = (int) thread; /* argument to function */
101 *--sp = 0; /* fake return address */
102 ts->esp = (int) sp; /* set stack pointer */
103 /* Incase of needresume, suspend is always set */
104 if (suspended) {
105 PTHREAD_MACH_CALL(thread_set_state(thread->kernel_thread,
106 i386_THREAD_STATE,
107 (thread_state_t) &state,
108 i386_THREAD_STATE_COUNT),
109 r);
110 if (needresume)
111 PTHREAD_MACH_CALL(thread_resume(thread->kernel_thread),
112 r);
113 } else {
114 PTHREAD_MACH_CALL(thread_create_running(mach_task_self(),
115 i386_THREAD_STATE,
116 (thread_state_t) ts,
117 i386_THREAD_STATE_COUNT,
118 &thread->kernel_thread),
119 r);
120 }
121
122 #else
123 #error _pthread_setup not defined for this architecture
124 #endif
125 }