]> git.saurik.com Git - apple/libpthread.git/blob - src/pthread_asm.s
90afe461bfec6a8168ae7a1c616940160d91f9d8
[apple/libpthread.git] / src / pthread_asm.s
1 /*
2 * Copyright (c) 2012 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 #include "offsets.h"
25
26 #if defined(__x86_64__)
27
28 #include <mach/i386/syscall_sw.h>
29
30 #ifndef VARIANT_DYLD
31
32 .align 2, 0x90
33 .globl _start_wqthread
34 _start_wqthread:
35 // This routine is never called directly by user code, jumped from kernel
36 push %rbp
37 mov %rsp,%rbp
38 sub $24,%rsp // align the stack
39 call __pthread_wqthread
40 leave
41 ret
42
43 .align 2, 0x90
44 .globl _thread_start
45 _thread_start:
46 // This routine is never called directly by user code, jumped from kernel
47 push %rbp
48 mov %rsp,%rbp
49 sub $24,%rsp // align the stack
50 call __pthread_start
51 leave
52 ret
53
54 .align 2, 0x90
55 .globl _thread_chkstk_darwin
56 _thread_chkstk_darwin:
57 .globl ____chkstk_darwin
58 ____chkstk_darwin: // %rax == alloca size
59 pushq %rcx
60 leaq 0x10(%rsp), %rcx
61
62 // validate that the frame pointer is on our stack (no alt stack)
63 cmpq %rcx, %gs:_PTHREAD_STRUCT_DIRECT_STACKADDR_OFFSET
64 jb Lprobe
65 cmpq %rcx, %gs:_PTHREAD_STRUCT_DIRECT_STACKBOTTOM_OFFSET
66 jae Lprobe
67
68 // validate alloca size
69 subq %rax, %rcx
70 jb Lcrash
71 cmpq %rcx, %gs:_PTHREAD_STRUCT_DIRECT_STACKBOTTOM_OFFSET
72 ja Lcrash
73
74 popq %rcx
75 retq
76
77 Lprobe:
78 // probe the stack when it's not ours (altstack or some shenanigan)
79 cmpq $0x1000, %rax
80 jb Lend
81 pushq %rax
82 Lloop:
83 subq $0x1000, %rcx
84 testq %rcx, (%rcx)
85 subq $0x1000, %rax
86 cmpq $0x1000, %rax
87 ja Lloop
88 popq %rax
89 Lend:
90 subq %rax, %rcx
91 testq %rcx, (%rcx)
92
93 popq %rcx
94 retq
95
96 Lcrash:
97 ud2
98
99 #endif
100
101 #elif defined(__i386__)
102
103 #include <mach/i386/syscall_sw.h>
104
105 #ifndef VARIANT_DYLD
106
107 .align 2, 0x90
108 .globl _start_wqthread
109 _start_wqthread:
110 // This routine is never called directly by user code, jumped from kernel
111 push %ebp
112 mov %esp,%ebp
113 sub $28,%esp // align the stack
114 mov %esi,20(%esp) //arg5
115 mov %edi,16(%esp) //arg5
116 mov %edx,12(%esp) //arg4
117 mov %ecx,8(%esp) //arg3
118 mov %ebx,4(%esp) //arg2
119 mov %eax,(%esp) //arg1
120 call __pthread_wqthread
121 leave
122 ret
123
124 .align 2, 0x90
125 .globl _thread_start
126 _thread_start:
127 // This routine is never called directly by user code, jumped from kernel
128 push %ebp
129 mov %esp,%ebp
130 sub $28,%esp // align the stack
131 mov %esi,20(%esp) //arg6
132 mov %edi,16(%esp) //arg5
133 mov %edx,12(%esp) //arg4
134 mov %ecx,8(%esp) //arg3
135 mov %ebx,4(%esp) //arg2
136 mov %eax,(%esp) //arg1
137 call __pthread_start
138 leave
139 ret
140
141 .align 2, 0x90
142 .globl _thread_chkstk_darwin
143 _thread_chkstk_darwin:
144 .globl ____chkstk_darwin
145 ____chkstk_darwin: // %eax == alloca size
146 pushl %ecx
147 pushl %edx
148 leal 0xc(%esp), %ecx
149
150 // validate that the frame pointer is on our stack (no alt stack)
151 movl %gs:0x0, %edx // pthread_self()
152 cmpl %ecx, _PTHREAD_STRUCT_DIRECT_STACKADDR_OFFSET(%edx)
153 jb Lprobe
154 movl _PTHREAD_STRUCT_DIRECT_STACKBOTTOM_OFFSET(%edx), %edx
155 cmpl %ecx, %edx
156 jae Lprobe
157
158 // validate alloca size
159 subl %eax, %ecx
160 jb Lcrash
161 cmpl %ecx, %edx
162 ja Lcrash
163
164 popl %edx
165 popl %ecx
166 retl
167
168 Lprobe:
169 // probe the stack when it's not ours (altstack or some shenanigan)
170 cmpl $0x1000, %eax
171 jb Lend
172 pushl %eax
173 Lloop:
174 subl $0x1000, %ecx
175 testl %ecx, (%ecx)
176 subl $0x1000, %eax
177 cmpl $0x1000, %eax
178 ja Lloop
179 popl %eax
180 Lend:
181 subl %eax, %ecx
182 testl %ecx, (%ecx)
183
184 popl %edx
185 popl %ecx
186 retl
187
188 Lcrash:
189 ud2
190
191 #endif
192
193 #elif defined(__arm__)
194
195 #include <mach/arm/syscall_sw.h>
196
197 #ifndef VARIANT_DYLD
198
199 // This routine is never called directly by user code, jumped from kernel
200 // args 0 to 3 are already in the regs 0 to 3
201 // should set stack with the 2 extra args before calling pthread_wqthread()
202 // arg4 is in r[4]
203 // arg5 is in r[5]
204
205 .text
206 .align 2
207 .globl _start_wqthread
208 _start_wqthread:
209 #if __ARM_ARCH_7K__
210 /* align stack to 16 bytes before calling C */
211 sub sp, sp, #8
212 #endif
213 stmfd sp!, {r4, r5}
214 bl __pthread_wqthread
215 // Stackshots will show the routine that happens to link immediately following
216 // _start_wqthread. So we add an extra instruction (nop) to make stackshots
217 // more readable.
218 nop
219
220 .text
221 .align 2
222 .globl _thread_start
223 _thread_start:
224 #if __ARM_ARCH_7K__
225 /* align stack to 16 bytes before calling C */
226 sub sp, sp, #8
227 #endif
228 stmfd sp!, {r4, r5}
229 bl __pthread_start
230 // See above
231 nop
232
233 #endif
234
235 #else
236 #error Unsupported architecture
237 #endif