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