]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
4452a7af | 2 | * Copyright (c) 2004-2006 Apple Computer, Inc. All rights reserved. |
91447636 | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
91447636 | 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@ | |
91447636 A |
27 | */ |
28 | ||
29 | #include <sys/appleapiopts.h> | |
30 | #include <machine/cpu_capabilities.h> | |
31 | #include <machine/commpage.h> | |
32 | ||
33 | /* OSAtomic.h library native implementations. */ | |
34 | ||
35 | .text | |
36 | .align 2, 0x90 | |
37 | ||
38 | // This is a regparm(3) subroutine used by: | |
39 | ||
40 | // bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value); | |
41 | // int32_t OSAtomicAnd32( int32_t mask, int32_t *value); | |
42 | // int32_t OSAtomicOr32( int32_t mask, int32_t *value); | |
43 | // int32_t OSAtomicXor32( int32_t mask, int32_t *value); | |
44 | ||
45 | // It assumes old -> %eax, new -> %edx, value -> %ecx | |
46 | // on success: returns with ZF set | |
47 | // on failure: returns with *value in %eax, ZF clear | |
48 | ||
49 | // The first word of the routine contains the address of the first instruction, | |
50 | // so callers can pass parameters in registers by using the absolute: | |
51 | ||
52 | // call *_COMPARE_AND_SWAP32 | |
53 | ||
54 | // TODO: move the .long onto a separate page to reduce icache pollution (?) | |
55 | ||
56 | Lcompare_and_swap32_mp: | |
57 | .long _COMM_PAGE_COMPARE_AND_SWAP32+4 | |
58 | lock | |
59 | cmpxchgl %edx, (%ecx) | |
60 | ret | |
61 | ||
62 | COMMPAGE_DESCRIPTOR(compare_and_swap32_mp,_COMM_PAGE_COMPARE_AND_SWAP32,0,kUP) | |
63 | ||
64 | Lcompare_and_swap32_up: | |
65 | .long _COMM_PAGE_COMPARE_AND_SWAP32+4 | |
66 | cmpxchgl %edx, (%ecx) | |
67 | ret | |
68 | ||
69 | COMMPAGE_DESCRIPTOR(compare_and_swap32_up,_COMM_PAGE_COMPARE_AND_SWAP32,kUP,0) | |
70 | ||
71 | // This is a subroutine used by: | |
72 | // bool OSAtomicCompareAndSwap64( int64_t old, int64_t new, int64_t *value); | |
73 | ||
74 | // It assumes old -> %eax/%edx, new -> %ebx/%ecx, value -> %esi | |
75 | // on success: returns with ZF set | |
76 | // on failure: returns with *value in %eax/%edx, ZF clear | |
77 | ||
78 | Lcompare_and_swap64_mp: | |
79 | .long _COMM_PAGE_COMPARE_AND_SWAP64+4 | |
80 | lock | |
81 | cmpxchg8b (%esi) | |
82 | ret | |
83 | ||
84 | COMMPAGE_DESCRIPTOR(compare_and_swap64_mp,_COMM_PAGE_COMPARE_AND_SWAP64,0,kUP) | |
85 | ||
86 | Lcompare_and_swap64_up: | |
87 | .long _COMM_PAGE_COMPARE_AND_SWAP64+4 | |
88 | cmpxchg8b (%esi) | |
89 | ret | |
90 | ||
91 | COMMPAGE_DESCRIPTOR(compare_and_swap64_up,_COMM_PAGE_COMPARE_AND_SWAP64,kUP,0) | |
92 | ||
93 | // This is a subroutine used by: | |
94 | // bool OSAtomicTestAndSet( uint32_t n, void *value ); | |
95 | // It assumes n -> %eax, value -> %edx | |
96 | ||
97 | // Returns: old value of bit in CF | |
98 | ||
99 | Lbit_test_and_set_mp: | |
100 | .long _COMM_PAGE_BTS+4 | |
101 | lock | |
4452a7af | 102 | btsl %eax, (%edx) |
91447636 A |
103 | ret |
104 | ||
105 | COMMPAGE_DESCRIPTOR(bit_test_and_set_mp,_COMM_PAGE_BTS,0,kUP) | |
106 | ||
107 | Lbit_test_and_set_up: | |
108 | .long _COMM_PAGE_BTS+4 | |
4452a7af | 109 | btsl %eax, (%edx) |
91447636 A |
110 | ret |
111 | ||
112 | COMMPAGE_DESCRIPTOR(bit_test_and_set_up,_COMM_PAGE_BTS,kUP,0) | |
113 | ||
114 | // This is a subroutine used by: | |
115 | // bool OSAtomicTestAndClear( uint32_t n, void *value ); | |
116 | // It assumes n -> %eax, value -> %edx | |
117 | ||
118 | // Returns: old value of bit in CF | |
119 | ||
120 | Lbit_test_and_clear_mp: | |
121 | .long _COMM_PAGE_BTC+4 | |
122 | lock | |
4452a7af | 123 | btrl %eax, (%edx) |
91447636 A |
124 | ret |
125 | ||
126 | COMMPAGE_DESCRIPTOR(bit_test_and_clear_mp,_COMM_PAGE_BTC,0,kUP) | |
127 | ||
128 | Lbit_test_and_clear_up: | |
129 | .long _COMM_PAGE_BTC+4 | |
4452a7af | 130 | btrl %eax, (%edx) |
91447636 A |
131 | ret |
132 | ||
133 | COMMPAGE_DESCRIPTOR(bit_test_and_clear_up,_COMM_PAGE_BTC,kUP,0) | |
134 | ||
135 | // This is a subroutine used by: | |
136 | // int32_t OSAtomicAdd32( int32_t amt, int32_t *value ); | |
137 | // It assumes amt -> %eax, value -> %edx | |
138 | ||
139 | // Returns: old value in %eax | |
140 | // NB: OSAtomicAdd32 returns the new value, so clients will add amt to %eax | |
141 | ||
142 | Latomic_add32_mp: | |
143 | .long _COMM_PAGE_ATOMIC_ADD32+4 | |
144 | lock | |
145 | xaddl %eax, (%edx) | |
146 | ret | |
147 | ||
148 | COMMPAGE_DESCRIPTOR(atomic_add32_mp,_COMM_PAGE_ATOMIC_ADD32,0,kUP) | |
149 | ||
150 | Latomic_add32_up: | |
151 | .long _COMM_PAGE_ATOMIC_ADD32+4 | |
152 | xaddl %eax, (%edx) | |
153 | ret | |
154 | ||
155 | COMMPAGE_DESCRIPTOR(atomic_add32_up,_COMM_PAGE_ATOMIC_ADD32,kUP,0) | |
4452a7af A |
156 | |
157 | ||
158 | /************************* x86_64 versions follow **************************/ | |
159 | ||
160 | ||
161 | // This is a subroutine used by: | |
162 | ||
163 | // bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value); | |
164 | // int32_t OSAtomicAnd32( int32_t mask, int32_t *value); | |
165 | // int32_t OSAtomicOr32( int32_t mask, int32_t *value); | |
166 | // int32_t OSAtomicXor32( int32_t mask, int32_t *value); | |
167 | ||
168 | // It assumes: old -> %rdi (ie, it follows the ABI parameter conventions) | |
169 | // new -> %rsi | |
170 | // value -> %rdx | |
171 | // on success: returns with ZF set | |
172 | // on failure: returns with *value in %eax, ZF clear | |
173 | ||
174 | .code64 | |
175 | Lcompare_and_swap32_mp_64: | |
176 | movl %edi,%eax // put old value where "cmpxchg" wants it | |
177 | lock | |
178 | cmpxchgl %esi, (%rdx) | |
179 | ret | |
180 | ||
181 | COMMPAGE_DESCRIPTOR(compare_and_swap32_mp_64,_COMM_PAGE_COMPARE_AND_SWAP32,0,kUP) | |
182 | ||
183 | .code64 | |
184 | Lcompare_and_swap32_up_64: | |
185 | movl %edi,%eax // put old value where "cmpxchg" wants it | |
186 | cmpxchgl %esi, (%rdx) | |
187 | ret | |
188 | ||
189 | COMMPAGE_DESCRIPTOR(compare_and_swap32_up_64,_COMM_PAGE_COMPARE_AND_SWAP32,kUP,0) | |
190 | ||
191 | // This is a subroutine used by: | |
192 | // bool OSAtomicCompareAndSwap64( int64_t old, int64_t new, int64_t *value); | |
193 | ||
194 | // It assumes: old -> %rdi (ie, it follows the ABI parameter conventions) | |
195 | // new -> %rsi | |
196 | // value -> %rdx | |
197 | // on success: returns with ZF set | |
198 | // on failure: returns with *value in %rax, ZF clear | |
199 | ||
200 | .code64 | |
201 | Lcompare_and_swap64_mp_64: | |
202 | movq %rdi,%rax // put old value where "cmpxchg" wants it | |
203 | lock | |
204 | cmpxchgq %rsi, (%rdx) | |
205 | ret | |
206 | ||
207 | COMMPAGE_DESCRIPTOR(compare_and_swap64_mp_64,_COMM_PAGE_COMPARE_AND_SWAP64,0,kUP) | |
208 | ||
209 | .code64 | |
210 | Lcompare_and_swap64_up_64: | |
211 | movq %rdi,%rax // put old value where "cmpxchg" wants it | |
212 | cmpxchgq %rsi, (%rdx) | |
213 | ret | |
214 | ||
215 | COMMPAGE_DESCRIPTOR(compare_and_swap64_up_64,_COMM_PAGE_COMPARE_AND_SWAP64,kUP,0) | |
216 | ||
217 | // This is a subroutine used by: | |
218 | // bool OSAtomicTestAndSet( uint32_t n, void *value ); | |
219 | // It is called with standard register conventions: | |
220 | // n = %rdi | |
221 | // value = %rsi | |
222 | // Returns: old value of bit in CF | |
223 | ||
224 | .code64 | |
225 | Lbit_test_and_set_mp_64: | |
226 | lock | |
227 | btsl %edi, (%rsi) | |
228 | ret | |
229 | ||
230 | COMMPAGE_DESCRIPTOR(bit_test_and_set_mp_64,_COMM_PAGE_BTS,0,kUP) | |
231 | ||
232 | .code64 | |
233 | Lbit_test_and_set_up_64: | |
234 | btsl %edi, (%rsi) | |
235 | ret | |
236 | ||
237 | COMMPAGE_DESCRIPTOR(bit_test_and_set_up_64,_COMM_PAGE_BTS,kUP,0) | |
238 | ||
239 | // This is a subroutine used by: | |
240 | // bool OSAtomicTestAndClear( uint32_t n, void *value ); | |
241 | // It is called with standard register conventions: | |
242 | // n = %rdi | |
243 | // value = %rsi | |
244 | // Returns: old value of bit in CF | |
245 | ||
246 | .code64 | |
247 | Lbit_test_and_clear_mp_64: | |
248 | lock | |
249 | btrl %edi, (%rsi) | |
250 | ret | |
251 | ||
252 | COMMPAGE_DESCRIPTOR(bit_test_and_clear_mp_64,_COMM_PAGE_BTC,0,kUP) | |
253 | ||
254 | .code64 | |
255 | Lbit_test_and_clear_up_64: | |
256 | btrl %edi, (%rsi) | |
257 | ret | |
258 | ||
259 | COMMPAGE_DESCRIPTOR(bit_test_and_clear_up_64,_COMM_PAGE_BTC,kUP,0) | |
260 | ||
261 | // This is a subroutine used by: | |
262 | // int32_t OSAtomicAdd32( int32_t amt, int32_t *value ); | |
263 | // It is called with standard register conventions: | |
264 | // amt = %rdi | |
265 | // value = %rsi | |
266 | // Returns: old value in %edi | |
267 | // NB: OSAtomicAdd32 returns the new value, so clients will add amt to %edi | |
268 | ||
269 | .code64 | |
270 | Latomic_add32_mp_64: | |
271 | lock | |
272 | xaddl %edi, (%rsi) | |
273 | ret | |
274 | ||
275 | COMMPAGE_DESCRIPTOR(atomic_add32_mp_64,_COMM_PAGE_ATOMIC_ADD32,0,kUP) | |
276 | ||
277 | .code64 | |
278 | Latomic_add32_up_64: | |
279 | xaddl %edi, (%rsi) | |
280 | ret | |
281 | ||
282 | COMMPAGE_DESCRIPTOR(atomic_add32_up_64,_COMM_PAGE_ATOMIC_ADD32,kUP,0) | |
283 | ||
284 | // This is a subroutine used by: | |
285 | // int64_t OSAtomicAdd64( int64_t amt, int64_t *value ); | |
286 | // It is called with standard register conventions: | |
287 | // amt = %rdi | |
288 | // value = %rsi | |
289 | // Returns: old value in %rdi | |
290 | // NB: OSAtomicAdd64 returns the new value, so clients will add amt to %rdi | |
291 | ||
292 | .code64 | |
293 | Latomic_add64_mp_64: | |
294 | lock | |
295 | xaddq %rdi, (%rsi) | |
296 | ret | |
297 | ||
298 | COMMPAGE_DESCRIPTOR(atomic_add64_mp_64,_COMM_PAGE_ATOMIC_ADD64,0,kUP) | |
299 | ||
300 | .code64 | |
301 | Latomic_add64_up_64: | |
302 | xaddq %rdi, (%rsi) | |
303 | ret | |
304 | ||
305 | COMMPAGE_DESCRIPTOR(atomic_add64_up_64,_COMM_PAGE_ATOMIC_ADD64,kUP,0) |