]> git.saurik.com Git - apple/libpthread.git/blob - src/thread_setup.c
libpthread-330.201.1.tar.gz
[apple/libpthread.git] / src / thread_setup.c
1 /*
2 * Copyright (c) 2000-2003, 2008, 2012 Apple 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 #include "internal.h"
49
50 #if !defined(__OPEN_SOURCE__) && TARGET_OS_OSX // 40703288
51 /*
52 * Machine specific support for thread initialization
53 */
54
55 // NOTE: no resolvers, so this file must not contain any atomic operations
56
57 PTHREAD_NOEXPORT void _pthread_setup_suspended(pthread_t th, void (*f)(pthread_t), void *sp);
58
59 /*
60 * Set up the initial state of a MACH thread
61 */
62 void
63 _pthread_setup_suspended(pthread_t thread,
64 void (*routine)(pthread_t),
65 void *vsp)
66 {
67 #if defined(__i386__)
68 i386_thread_state_t state = { };
69 thread_state_flavor_t flavor = x86_THREAD_STATE32;
70 mach_msg_type_number_t count = i386_THREAD_STATE_COUNT;
71 #elif defined(__x86_64__)
72 x86_thread_state64_t state = { };
73 thread_state_flavor_t flavor = x86_THREAD_STATE64;
74 mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
75 #else
76 #error _pthread_setup not defined for this architecture
77 #endif
78
79 (void)thread_get_state(_pthread_kernel_thread(thread),
80 flavor, (thread_state_t)&state, &count);
81
82 #if defined(__i386__)
83 uintptr_t *sp = vsp;
84
85 state.__eip = (uintptr_t)routine;
86
87 // We need to simulate a 16-byte aligned stack frame as if we had
88 // executed a call instruction. Since we're "pushing" one argument,
89 // we need to adjust the pointer by 12 bytes (3 * sizeof (int *))
90 sp -= 3; // make sure stack is aligned
91 *--sp = (uintptr_t)thread; // argument to function
92 *--sp = 0; // fake return address
93 state.__esp = (uintptr_t)sp; // set stack pointer
94 #elif defined(__x86_64__)
95 uintptr_t *sp = vsp;
96
97 state.__rip = (uintptr_t)routine;
98
99 // We need to simulate a 16-byte aligned stack frame as if we had
100 // executed a call instruction. The stack should already be aligned
101 // before it comes to us and we don't need to push any arguments,
102 // so we shouldn't need to change it.
103 state.__rdi = (uintptr_t)thread; // argument to function
104 *--sp = 0; // fake return address
105 state.__rsp = (uintptr_t)sp; // set stack pointer
106 #else
107 #error _pthread_setup_suspended not defined for this architecture
108 #endif
109
110 (void)thread_set_state(_pthread_kernel_thread(thread), flavor, (thread_state_t)&state, count);
111 }
112 #endif // !defined(__OPEN_SOURCE__) && TARGET_OS_OSX