]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
4452a7af | 2 | * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved. |
55e303ae | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
55e303ae | 5 | * |
8f6c56a5 A |
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. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
55e303ae A |
27 | */ |
28 | ||
29 | #include <sys/appleapiopts.h> | |
30 | #include <machine/cpu_capabilities.h> | |
31 | #include <machine/commpage.h> | |
4452a7af A |
32 | #include <mach/i386/syscall_sw.h> |
33 | ||
55e303ae | 34 | |
55e303ae A |
35 | |
36 | #define MP_SPIN_TRIES 1024 | |
37 | ||
38 | .text | |
39 | .align 4, 0x90 | |
40 | ||
41 | Lspin_lock_try_up: | |
42 | movl 4(%esp), %ecx | |
43 | xorl %eax, %eax | |
91447636 A |
44 | orl $-1, %edx |
45 | cmpxchgl %edx, (%ecx) | |
55e303ae A |
46 | setz %dl |
47 | movzbl %dl, %eax | |
48 | ret | |
49 | ||
50 | COMMPAGE_DESCRIPTOR(spin_lock_try_up,_COMM_PAGE_SPINLOCK_TRY,kUP,0) | |
51 | ||
4452a7af | 52 | |
55e303ae A |
53 | .align 4, 0x90 |
54 | Lspin_lock_try_mp: | |
55 | movl 4(%esp), %ecx | |
56 | xorl %eax, %eax | |
91447636 | 57 | orl $-1, %edx |
55e303ae | 58 | lock |
91447636 | 59 | cmpxchgl %edx, (%ecx) |
55e303ae A |
60 | setz %dl |
61 | movzbl %dl, %eax | |
62 | ret | |
63 | ||
64 | COMMPAGE_DESCRIPTOR(spin_lock_try_mp,_COMM_PAGE_SPINLOCK_TRY,0,kUP) | |
65 | ||
55e303ae A |
66 | |
67 | .align 4, 0x90 | |
68 | Lspin_lock_up: | |
69 | movl 4(%esp), %ecx | |
70 | xorl %eax, %eax | |
91447636 A |
71 | orl $-1, %edx |
72 | cmpxchgl %edx, (%ecx) | |
4452a7af | 73 | jnz,pn 1f /* predict not taken */ |
55e303ae | 74 | ret |
4452a7af A |
75 | 1: |
76 | /* failed to get lock so relinquish the processor immediately on UP */ | |
77 | pushl $1 /* 1 ms */ | |
78 | pushl $1 /* SWITCH_OPTION_DEPRESS */ | |
79 | pushl $0 /* THREAD_NULL */ | |
80 | pushl $0 /* push dummy stack ret addr */ | |
81 | movl $-61,%eax /* SYSCALL_THREAD_SWITCH */ | |
82 | int $(MACH_INT) | |
83 | addl $16, %esp /* adjust stack*/ | |
84 | jmp Lspin_lock_up | |
55e303ae A |
85 | |
86 | COMMPAGE_DESCRIPTOR(spin_lock_up,_COMM_PAGE_SPINLOCK_LOCK,kUP,0) | |
87 | ||
4452a7af | 88 | |
55e303ae A |
89 | .align 4, 0x90 |
90 | Lspin_lock_mp: | |
91 | movl 4(%esp), %ecx | |
92 | xorl %eax, %eax | |
93 | 0: | |
91447636 | 94 | orl $-1, %edx |
55e303ae | 95 | lock |
91447636 | 96 | cmpxchgl %edx, (%ecx) |
4452a7af | 97 | jnz,pn 1f /* predict not taken */ |
55e303ae A |
98 | ret |
99 | 1: | |
100 | xorl %eax, %eax | |
101 | movl $(MP_SPIN_TRIES), %edx | |
102 | 2: | |
103 | pause | |
104 | cmpl %eax, (%ecx) | |
4452a7af | 105 | jz,pt 0b /* favor success and slow down spin loop */ |
55e303ae | 106 | decl %edx |
4452a7af A |
107 | jnz,pn 2b /* slow down spin loop with a mispredict */ |
108 | /* failed to get lock after spinning so relinquish */ | |
109 | pushl $1 /* 1 ms */ | |
110 | pushl $1 /* SWITCH_OPTION_DEPRESS */ | |
111 | pushl $0 /* THREAD_NULL */ | |
112 | pushl $0 /* push dummy stack ret addr */ | |
113 | movl $-61,%eax /* SYSCALL_THREAD_SWITCH */ | |
114 | int $(MACH_INT) | |
115 | addl $16, %esp /* adjust stack*/ | |
116 | jmp Lspin_lock_mp | |
55e303ae A |
117 | |
118 | COMMPAGE_DESCRIPTOR(spin_lock_mp,_COMM_PAGE_SPINLOCK_LOCK,0,kUP) | |
119 | ||
4452a7af | 120 | |
55e303ae A |
121 | .align 4, 0x90 |
122 | Lspin_unlock: | |
123 | movl 4(%esp), %ecx | |
124 | movl $0, (%ecx) | |
125 | ret | |
126 | ||
127 | COMMPAGE_DESCRIPTOR(spin_unlock,_COMM_PAGE_SPINLOCK_UNLOCK,0,0) | |
128 | ||
4452a7af A |
129 | |
130 | /* ============================ 64-bit versions follow ===================== */ | |
131 | ||
132 | ||
133 | .text | |
134 | .code64 | |
89b3af67 | 135 | .align 4, 0x90 |
89b3af67 | 136 | |
4452a7af A |
137 | Lspin_lock_try_up_64: |
138 | xorl %eax, %eax | |
139 | orl $-1, %edx | |
140 | cmpxchgl %edx, (%rdi) | |
141 | setz %dl | |
142 | movzbl %dl, %eax | |
143 | ret | |
144 | ||
145 | COMMPAGE_DESCRIPTOR(spin_lock_try_up_64,_COMM_PAGE_SPINLOCK_TRY,kUP,0) | |
146 | ||
147 | ||
148 | .align 4, 0x90 | |
149 | Lspin_lock_try_mp_64: | |
150 | xorl %eax, %eax | |
151 | orl $-1, %edx | |
152 | lock | |
153 | cmpxchgl %edx, (%rdi) | |
154 | setz %dl | |
155 | movzbl %dl, %eax | |
156 | ret | |
157 | ||
158 | COMMPAGE_DESCRIPTOR(spin_lock_try_mp_64,_COMM_PAGE_SPINLOCK_TRY,0,kUP) | |
159 | ||
160 | ||
161 | .align 4, 0x90 | |
162 | Lspin_lock_up_64: | |
163 | movq %rdi,%r8 | |
164 | 0: | |
165 | xorl %eax, %eax | |
166 | orl $-1, %edx | |
167 | cmpxchgl %edx, (%r8) | |
168 | jnz,pn 1f /* predict not taken */ | |
169 | ret | |
170 | 1: | |
171 | /* failed to get lock so relinquish the processor immediately on UP */ | |
172 | xorl %edi,%edi /* THREAD_NULL */ | |
173 | movl $1,%esi /* SWITCH_OPTION_DEPRESS */ | |
174 | movl $1,%edx /* 1 ms */ | |
175 | movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */ | |
176 | syscall | |
177 | jmp 0b | |
178 | ||
179 | COMMPAGE_DESCRIPTOR(spin_lock_up_64,_COMM_PAGE_SPINLOCK_LOCK,kUP,0) | |
180 | ||
181 | ||
182 | ||
183 | .align 4, 0x90 | |
184 | Lspin_lock_mp_64: | |
185 | movq %rdi,%r8 | |
186 | 0: | |
187 | xorl %eax, %eax | |
188 | orl $-1, %edx | |
189 | lock | |
190 | cmpxchgl %edx, (%r8) | |
191 | jnz,pn 1f /* predict not taken */ | |
192 | ret | |
193 | 1: | |
194 | xorl %eax, %eax | |
195 | movl $(MP_SPIN_TRIES), %edx | |
196 | 2: /* spin for awhile before relinquish */ | |
197 | pause | |
198 | cmpl %eax, (%r8) | |
199 | jz 0b | |
200 | decl %edx | |
201 | jnz 2b | |
202 | /* failed to get lock after spinning so relinquish */ | |
203 | xorl %edi,%edi /* THREAD_NULL */ | |
204 | movl $1,%esi /* SWITCH_OPTION_DEPRESS */ | |
205 | movl $1,%edx /* 1 ms */ | |
206 | movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */ | |
207 | syscall | |
208 | jmp 0b | |
209 | ||
210 | COMMPAGE_DESCRIPTOR(spin_lock_mp_64,_COMM_PAGE_SPINLOCK_LOCK,0,kUP) | |
211 | ||
212 | ||
213 | .align 4, 0x90 | |
214 | Lspin_unlock_64: | |
215 | movl $0, (%rdi) | |
216 | ret | |
217 | ||
218 | COMMPAGE_DESCRIPTOR(spin_unlock_64,_COMM_PAGE_SPINLOCK_UNLOCK,0,0) |