atomic.S revision 1.6 1 /* $NetBSD: atomic.S,v 1.6 2007/12/20 01:27:24 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <machine/asm.h>
40
41 #ifdef _KERNEL
42 #define LOCK(n) .Lpatch/**/n: lock
43 #define ALIAS(f, t) STRONG_ALIAS(f,t)
44 #define END(a) _ALIGN_TEXT; LABEL(a)
45 #else
46 #define LOCK(n) lock
47 #define ALIAS(f, t) WEAK_ALIAS(f,t)
48 #define END(a) /* nothing */
49 #endif
50
51 .text
52
53 NENTRY(_atomic_add_32)
54 movl 4(%esp), %edx
55 movl 8(%esp), %eax
56 LOCK(1)
57 addl %eax, (%edx)
58 ret
59
60 NENTRY(_atomic_add_32_nv)
61 movl 4(%esp), %edx
62 movl 8(%esp), %eax
63 movl %eax, %ecx
64 LOCK(2)
65 xaddl %eax, (%edx)
66 addl %ecx, %eax
67 ret
68
69 NENTRY(_atomic_and_32)
70 movl 4(%esp), %edx
71 movl 8(%esp), %eax
72 LOCK(3)
73 andl %eax, (%edx)
74 ret
75
76 NENTRY(_atomic_and_32_nv)
77 movl 4(%esp), %edx
78 movl (%edx), %eax
79 1:
80 movl %eax, %ecx
81 andl 8(%esp), %ecx
82 LOCK(4)
83 cmpxchgl %ecx, (%edx)
84 jnz 1b
85 movl %ecx, %eax
86 ret
87
88 NENTRY(_atomic_dec_32)
89 movl 4(%esp), %edx
90 LOCK(5)
91 decl (%edx)
92 ret
93
94 NENTRY(_atomic_dec_32_nv)
95 movl 4(%esp), %edx
96 movl $-1, %eax
97 LOCK(6)
98 xaddl %eax, (%edx)
99 decl %eax
100 ret
101
102 NENTRY(_atomic_inc_32)
103 movl 4(%esp), %edx
104 LOCK(7)
105 incl (%edx)
106 ret
107
108 NENTRY(_atomic_inc_32_nv)
109 movl 4(%esp), %edx
110 movl $1, %eax
111 LOCK(8)
112 xaddl %eax, (%edx)
113 incl %eax
114 ret
115
116 NENTRY(_atomic_or_32)
117 movl 4(%esp), %edx
118 movl 8(%esp), %eax
119 LOCK(9)
120 orl %eax, (%edx)
121 ret
122
123 NENTRY(_atomic_or_32_nv)
124 movl 4(%esp), %edx
125 movl (%edx), %eax
126 1:
127 movl %eax, %ecx
128 orl 8(%esp), %ecx
129 LOCK(10)
130 cmpxchgl %ecx, (%edx)
131 jnz 1b
132 movl %ecx, %eax
133 ret
134
135 NENTRY(_atomic_swap_32)
136 movl 4(%esp), %edx
137 movl 8(%esp), %eax
138 xchgl %eax, (%edx)
139 ret
140
141 NENTRY(_atomic_cas_32)
142 movl 4(%esp), %edx
143 movl 8(%esp), %eax
144 movl 12(%esp), %ecx
145 LOCK(12)
146 cmpxchgl %ecx, (%edx)
147 /* %eax now contains the old value */
148 ret
149
150 NENTRY(_membar_consumer)
151 LOCK(13)
152 addl $0, -4(%esp)
153 ret
154 END(membar_consumer_end)
155
156 NENTRY(_membar_producer)
157 /* A store is enough */
158 movl $0, -4(%esp)
159 ret
160 END(membar_producer_end)
161
162 NENTRY(_membar_enter)
163 /* A store is enough */
164 movl $0, -4(%esp)
165 ret
166 END(membar_enter_end)
167
168 NENTRY(_membar_exit)
169 /* A store is enough */
170 movl $0, -4(%esp)
171 ret
172 END(membar_exit_end)
173
174 NENTRY(_membar_sync)
175 LOCK(14)
176 addl $0, -4(%esp)
177 ret
178 END(membar_sync_end)
179
180 #ifdef _KERNEL
181 NENTRY(_atomic_cas_64)
182 cli
183 pushl %edi
184 pushl %ebx
185 movl 12(%esp), %edi
186 movl 16(%esp), %eax
187 movl 20(%esp), %edx
188 movl 24(%esp), %ebx
189 movl 28(%esp), %ecx
190 cmpl 0(%edi), %eax
191 jne 2f
192 cmpl 4(%edi), %edx
193 jne 2f
194 movl %ebx, 0(%edi)
195 movl %ecx, 4(%edi)
196 1:
197 popl %ebx
198 popl %edi
199 sti
200 ret
201 2:
202 movl 0(%edi), %eax
203 movl 4(%edi), %edx
204 jmp 1b
205 END(_atomic_cas_64_end)
206
207 NENTRY(_atomic_cas_cx8)
208 pushl %edi
209 pushl %ebx
210 movl 12(%esp), %edi
211 movl 16(%esp), %eax
212 movl 20(%esp), %edx
213 movl 24(%esp), %ebx
214 movl 28(%esp), %ecx
215 LOCK(15)
216 cmpxchg8b (%edi)
217 popl %ebx
218 popl %edi
219 ret
220 .space 16, 0x90
221 END(_atomic_cas_cx8_end)
222
223 NENTRY(sse2_lfence)
224 lfence
225 ret
226 END(sse2_lfence_end)
227
228 NENTRY(sse2_mfence)
229 mfence
230 ret
231 END(sse2_mfence_end)
232
233 atomic_lockpatch:
234 .globl atomic_lockpatch
235 .long .Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5
236 .long .Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10
237 .long .Lpatch12, .Lpatch13, .Lpatch14, .Lpatch15, 0
238 #endif /* _KERNEL */
239
240 ALIAS(atomic_add_32,_atomic_add_32)
241 ALIAS(atomic_add_int,_atomic_add_32)
242 ALIAS(atomic_add_long,_atomic_add_32)
243 ALIAS(atomic_add_ptr,_atomic_add_32)
244
245 ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
246 ALIAS(atomic_add_int_nv,_atomic_add_32_nv)
247 ALIAS(atomic_add_long_nv,_atomic_add_32_nv)
248 ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
249
250 ALIAS(atomic_and_32,_atomic_and_32)
251 ALIAS(atomic_and_uint,_atomic_and_32)
252 ALIAS(atomic_and_ulong,_atomic_and_32)
253 ALIAS(atomic_and_ptr,_atomic_and_32)
254
255 ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
256 ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
257 ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
258 ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
259
260 ALIAS(atomic_dec_32,_atomic_dec_32)
261 ALIAS(atomic_dec_uint,_atomic_dec_32)
262 ALIAS(atomic_dec_ulong,_atomic_dec_32)
263 ALIAS(atomic_dec_ptr,_atomic_dec_32)
264
265 ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
266 ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
267 ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
268 ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
269
270 ALIAS(atomic_inc_32,_atomic_inc_32)
271 ALIAS(atomic_inc_uint,_atomic_inc_32)
272 ALIAS(atomic_inc_ulong,_atomic_inc_32)
273 ALIAS(atomic_inc_ptr,_atomic_inc_32)
274
275 ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
276 ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
277 ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
278 ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
279
280 ALIAS(atomic_or_32,_atomic_or_32)
281 ALIAS(atomic_or_uint,_atomic_or_32)
282 ALIAS(atomic_or_ulong,_atomic_or_32)
283 ALIAS(atomic_or_ptr,_atomic_or_32)
284
285 ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
286 ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
287 ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
288 ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
289
290 ALIAS(atomic_swap_32,_atomic_swap_32)
291 ALIAS(atomic_swap_uint,_atomic_swap_32)
292 ALIAS(atomic_swap_ulong,_atomic_swap_32)
293 ALIAS(atomic_swap_ptr,_atomic_swap_32)
294
295 ALIAS(atomic_cas_32,_atomic_cas_32)
296 ALIAS(atomic_cas_uint,_atomic_cas_32)
297 ALIAS(atomic_cas_ulong,_atomic_cas_32)
298 ALIAS(atomic_cas_ptr,_atomic_cas_32)
299
300 ALIAS(atomic_cas_64,_atomic_cas_64)
301
302 ALIAS(membar_consumer,_membar_consumer)
303 ALIAS(membar_producer,_membar_producer)
304 ALIAS(membar_enter,_membar_enter)
305 ALIAS(membar_exit,_membar_exit)
306 ALIAS(membar_sync,_membar_sync)
307
308 STRONG_ALIAS(_atomic_add_int,_atomic_add_32)
309 STRONG_ALIAS(_atomic_add_long,_atomic_add_32)
310 STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32)
311
312 STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv)
313 STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv)
314 STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv)
315
316 STRONG_ALIAS(_atomic_and_uint,_atomic_and_32)
317 STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32)
318 STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32)
319
320 STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv)
321 STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv)
322 STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv)
323
324 STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32)
325 STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32)
326 STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32)
327
328 STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv)
329 STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv)
330 STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv)
331
332 STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32)
333 STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32)
334 STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32)
335
336 STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv)
337 STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv)
338 STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv)
339
340 STRONG_ALIAS(_atomic_or_uint,_atomic_or_32)
341 STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32)
342 STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32)
343
344 STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv)
345 STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv)
346 STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv)
347
348 STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32)
349 STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32)
350 STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32)
351
352 STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32)
353 STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32)
354 STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32)
355