atomic.S revision 1.3 1 /* $NetBSD: atomic.S,v 1.3 2007/11/28 18:02:29 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(sse2_lfence)
182 lfence
183 ret
184 END(sse2_lfence_end)
185
186 NENTRY(sse2_mfence)
187 mfence
188 ret
189 END(sse2_mfence_end)
190
191 atomic_lockpatch:
192 .globl atomic_lockpatch
193 .long .Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5
194 .long .Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10
195 .long .Lpatch12, .Lpatch13, .Lpatch14, 0
196 #endif /* _KERNEL */
197
198 ALIAS(atomic_add_32,_atomic_add_32)
199 ALIAS(atomic_add_uint,_atomic_add_32)
200 ALIAS(atomic_add_ulong,_atomic_add_32)
201 ALIAS(atomic_add_ptr,_atomic_add_32)
202
203 ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
204 ALIAS(atomic_add_uint_nv,_atomic_add_32_nv)
205 ALIAS(atomic_add_ulong_nv,_atomic_add_32_nv)
206 ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
207
208 ALIAS(atomic_and_32,_atomic_and_32)
209 ALIAS(atomic_and_uint,_atomic_and_32)
210 ALIAS(atomic_and_ulong,_atomic_and_32)
211 ALIAS(atomic_and_ptr,_atomic_and_32)
212
213 ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
214 ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
215 ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
216 ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
217
218 ALIAS(atomic_dec_32,_atomic_dec_32)
219 ALIAS(atomic_dec_uint,_atomic_dec_32)
220 ALIAS(atomic_dec_ulong,_atomic_dec_32)
221 ALIAS(atomic_dec_ptr,_atomic_dec_32)
222
223 ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
224 ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
225 ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
226 ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
227
228 ALIAS(atomic_inc_32,_atomic_inc_32)
229 ALIAS(atomic_inc_uint,_atomic_inc_32)
230 ALIAS(atomic_inc_ulong,_atomic_inc_32)
231 ALIAS(atomic_inc_ptr,_atomic_inc_32)
232
233 ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
234 ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
235 ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
236 ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
237
238 ALIAS(atomic_or_32,_atomic_or_32)
239 ALIAS(atomic_or_uint,_atomic_or_32)
240 ALIAS(atomic_or_ulong,_atomic_or_32)
241 ALIAS(atomic_or_ptr,_atomic_or_32)
242
243 ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
244 ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
245 ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
246 ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
247
248 ALIAS(atomic_swap_32,_atomic_swap_32)
249 ALIAS(atomic_swap_uint,_atomic_swap_32)
250 ALIAS(atomic_swap_ulong,_atomic_swap_32)
251 ALIAS(atomic_swap_ptr,_atomic_swap_32)
252
253 ALIAS(atomic_cas_32,_atomic_cas_32)
254 ALIAS(atomic_cas_uint,_atomic_cas_32)
255 ALIAS(atomic_cas_ulong,_atomic_cas_32)
256 ALIAS(atomic_cas_ptr,_atomic_cas_32)
257
258 ALIAS(membar_consumer,_membar_consumer)
259 ALIAS(membar_producer,_membar_producer)
260 ALIAS(membar_enter,_membar_enter)
261 ALIAS(membar_exit,_membar_exit)
262 ALIAS(membar_sync,_membar_sync)
263