atomic.S revision 1.7 1 /* $NetBSD: atomic.S,v 1.7 2007/12/20 23:46:10 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 32, 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 #else
239 NENTRY(_atomic_cas_64)
240 pushl %edi
241 pushl %ebx
242 movl 12(%esp), %edi
243 movl 16(%esp), %eax
244 movl 20(%esp), %edx
245 movl 24(%esp), %ebx
246 movl 28(%esp), %ecx
247 lock
248 cmpxchg8b (%edi)
249 popl %ebx
250 popl %edi
251 ret
252 #endif /* _KERNEL */
253
254 ALIAS(atomic_add_32,_atomic_add_32)
255 ALIAS(atomic_add_int,_atomic_add_32)
256 ALIAS(atomic_add_long,_atomic_add_32)
257 ALIAS(atomic_add_ptr,_atomic_add_32)
258
259 ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
260 ALIAS(atomic_add_int_nv,_atomic_add_32_nv)
261 ALIAS(atomic_add_long_nv,_atomic_add_32_nv)
262 ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
263
264 ALIAS(atomic_and_32,_atomic_and_32)
265 ALIAS(atomic_and_uint,_atomic_and_32)
266 ALIAS(atomic_and_ulong,_atomic_and_32)
267 ALIAS(atomic_and_ptr,_atomic_and_32)
268
269 ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
270 ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
271 ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
272 ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
273
274 ALIAS(atomic_dec_32,_atomic_dec_32)
275 ALIAS(atomic_dec_uint,_atomic_dec_32)
276 ALIAS(atomic_dec_ulong,_atomic_dec_32)
277 ALIAS(atomic_dec_ptr,_atomic_dec_32)
278
279 ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
280 ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
281 ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
282 ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
283
284 ALIAS(atomic_inc_32,_atomic_inc_32)
285 ALIAS(atomic_inc_uint,_atomic_inc_32)
286 ALIAS(atomic_inc_ulong,_atomic_inc_32)
287 ALIAS(atomic_inc_ptr,_atomic_inc_32)
288
289 ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
290 ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
291 ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
292 ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
293
294 ALIAS(atomic_or_32,_atomic_or_32)
295 ALIAS(atomic_or_uint,_atomic_or_32)
296 ALIAS(atomic_or_ulong,_atomic_or_32)
297 ALIAS(atomic_or_ptr,_atomic_or_32)
298
299 ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
300 ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
301 ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
302 ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
303
304 ALIAS(atomic_swap_32,_atomic_swap_32)
305 ALIAS(atomic_swap_uint,_atomic_swap_32)
306 ALIAS(atomic_swap_ulong,_atomic_swap_32)
307 ALIAS(atomic_swap_ptr,_atomic_swap_32)
308
309 ALIAS(atomic_cas_32,_atomic_cas_32)
310 ALIAS(atomic_cas_uint,_atomic_cas_32)
311 ALIAS(atomic_cas_ulong,_atomic_cas_32)
312 ALIAS(atomic_cas_ptr,_atomic_cas_32)
313
314 ALIAS(atomic_cas_64,_atomic_cas_64)
315
316 ALIAS(membar_consumer,_membar_consumer)
317 ALIAS(membar_producer,_membar_producer)
318 ALIAS(membar_enter,_membar_enter)
319 ALIAS(membar_exit,_membar_exit)
320 ALIAS(membar_sync,_membar_sync)
321
322 STRONG_ALIAS(_atomic_add_int,_atomic_add_32)
323 STRONG_ALIAS(_atomic_add_long,_atomic_add_32)
324 STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32)
325
326 STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv)
327 STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv)
328 STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv)
329
330 STRONG_ALIAS(_atomic_and_uint,_atomic_and_32)
331 STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32)
332 STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32)
333
334 STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv)
335 STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv)
336 STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv)
337
338 STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32)
339 STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32)
340 STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32)
341
342 STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv)
343 STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv)
344 STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv)
345
346 STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32)
347 STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32)
348 STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32)
349
350 STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv)
351 STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv)
352 STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv)
353
354 STRONG_ALIAS(_atomic_or_uint,_atomic_or_32)
355 STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32)
356 STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32)
357
358 STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv)
359 STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv)
360 STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv)
361
362 STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32)
363 STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32)
364 STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32)
365
366 STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32)
367 STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32)
368 STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32)
369