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