atomic.S revision 1.24 1 1.24 bouyer /* $NetBSD: atomic.S,v 1.24 2020/04/25 15:26:16 bouyer 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 *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.16 pooka #include <sys/param.h>
33 1.1 ad #include <machine/asm.h>
34 1.21 christos /*
35 1.21 christos * __HAVE_ constants should not be in <machine/types.h>
36 1.21 christos * because we can't use them from assembly. OTOH we
37 1.21 christos * only need __HAVE_ATOMIC64_OPS here, and we don't.
38 1.21 christos */
39 1.18 pooka #ifdef _KERNEL
40 1.18 pooka #define ALIAS(f, t) STRONG_ALIAS(f,t)
41 1.18 pooka #else
42 1.18 pooka #define ALIAS(f, t) WEAK_ALIAS(f,t)
43 1.18 pooka #endif
44 1.18 pooka
45 1.15 pooka #ifdef _HARDKERNEL
46 1.23 bouyer #include "opt_xen.h"
47 1.19 joerg #define LOCK(n) .Lpatch ## n: lock
48 1.12 yamt #define ENDLABEL(a) _ALIGN_TEXT; LABEL(a)
49 1.1 ad #else
50 1.1 ad #define LOCK(n) lock
51 1.12 yamt #define ENDLABEL(a) /* nothing */
52 1.1 ad #endif
53 1.1 ad
54 1.1 ad .text
55 1.1 ad
56 1.13 chs ENTRY(_atomic_add_32)
57 1.1 ad movl 4(%esp), %edx
58 1.1 ad movl 8(%esp), %eax
59 1.1 ad LOCK(1)
60 1.1 ad addl %eax, (%edx)
61 1.1 ad ret
62 1.22 uebayasi END(_atomic_add_32)
63 1.1 ad
64 1.13 chs ENTRY(_atomic_add_32_nv)
65 1.1 ad movl 4(%esp), %edx
66 1.1 ad movl 8(%esp), %eax
67 1.1 ad movl %eax, %ecx
68 1.1 ad LOCK(2)
69 1.1 ad xaddl %eax, (%edx)
70 1.1 ad addl %ecx, %eax
71 1.1 ad ret
72 1.22 uebayasi END(_atomic_add_32_nv)
73 1.1 ad
74 1.13 chs ENTRY(_atomic_and_32)
75 1.1 ad movl 4(%esp), %edx
76 1.1 ad movl 8(%esp), %eax
77 1.1 ad LOCK(3)
78 1.1 ad andl %eax, (%edx)
79 1.1 ad ret
80 1.22 uebayasi END(_atomic_and_32)
81 1.1 ad
82 1.13 chs ENTRY(_atomic_and_32_nv)
83 1.1 ad movl 4(%esp), %edx
84 1.1 ad movl (%edx), %eax
85 1.14 ad 0:
86 1.1 ad movl %eax, %ecx
87 1.1 ad andl 8(%esp), %ecx
88 1.1 ad LOCK(4)
89 1.1 ad cmpxchgl %ecx, (%edx)
90 1.14 ad jnz 1f
91 1.1 ad movl %ecx, %eax
92 1.1 ad ret
93 1.14 ad 1:
94 1.14 ad jmp 0b
95 1.22 uebayasi END(_atomic_and_32_nv)
96 1.1 ad
97 1.13 chs ENTRY(_atomic_dec_32)
98 1.1 ad movl 4(%esp), %edx
99 1.1 ad LOCK(5)
100 1.1 ad decl (%edx)
101 1.1 ad ret
102 1.22 uebayasi END(_atomic_dec_32)
103 1.1 ad
104 1.13 chs ENTRY(_atomic_dec_32_nv)
105 1.1 ad movl 4(%esp), %edx
106 1.1 ad movl $-1, %eax
107 1.1 ad LOCK(6)
108 1.1 ad xaddl %eax, (%edx)
109 1.1 ad decl %eax
110 1.1 ad ret
111 1.22 uebayasi END(_atomic_dec_32_nv)
112 1.1 ad
113 1.13 chs ENTRY(_atomic_inc_32)
114 1.1 ad movl 4(%esp), %edx
115 1.1 ad LOCK(7)
116 1.1 ad incl (%edx)
117 1.1 ad ret
118 1.22 uebayasi END(_atomic_inc_32)
119 1.1 ad
120 1.13 chs ENTRY(_atomic_inc_32_nv)
121 1.1 ad movl 4(%esp), %edx
122 1.1 ad movl $1, %eax
123 1.1 ad LOCK(8)
124 1.1 ad xaddl %eax, (%edx)
125 1.1 ad incl %eax
126 1.1 ad ret
127 1.22 uebayasi END(_atomic_inc_32_nv)
128 1.1 ad
129 1.13 chs ENTRY(_atomic_or_32)
130 1.1 ad movl 4(%esp), %edx
131 1.1 ad movl 8(%esp), %eax
132 1.1 ad LOCK(9)
133 1.1 ad orl %eax, (%edx)
134 1.1 ad ret
135 1.22 uebayasi END(_atomic_or_32)
136 1.1 ad
137 1.13 chs ENTRY(_atomic_or_32_nv)
138 1.1 ad movl 4(%esp), %edx
139 1.1 ad movl (%edx), %eax
140 1.14 ad 0:
141 1.1 ad movl %eax, %ecx
142 1.1 ad orl 8(%esp), %ecx
143 1.1 ad LOCK(10)
144 1.1 ad cmpxchgl %ecx, (%edx)
145 1.14 ad jnz 1f
146 1.1 ad movl %ecx, %eax
147 1.1 ad ret
148 1.14 ad 1:
149 1.14 ad jmp 0b
150 1.22 uebayasi END(_atomic_or_32_nv)
151 1.1 ad
152 1.13 chs ENTRY(_atomic_swap_32)
153 1.1 ad movl 4(%esp), %edx
154 1.1 ad movl 8(%esp), %eax
155 1.1 ad xchgl %eax, (%edx)
156 1.1 ad ret
157 1.22 uebayasi END(_atomic_swap_32)
158 1.1 ad
159 1.13 chs ENTRY(_atomic_cas_32)
160 1.1 ad movl 4(%esp), %edx
161 1.1 ad movl 8(%esp), %eax
162 1.1 ad movl 12(%esp), %ecx
163 1.1 ad LOCK(12)
164 1.1 ad cmpxchgl %ecx, (%edx)
165 1.1 ad /* %eax now contains the old value */
166 1.1 ad ret
167 1.22 uebayasi END(_atomic_cas_32)
168 1.1 ad
169 1.13 chs ENTRY(_atomic_cas_32_ni)
170 1.9 ad movl 4(%esp), %edx
171 1.9 ad movl 8(%esp), %eax
172 1.9 ad movl 12(%esp), %ecx
173 1.9 ad cmpxchgl %ecx, (%edx)
174 1.9 ad /* %eax now contains the old value */
175 1.9 ad ret
176 1.22 uebayasi END(_atomic_cas_32_ni)
177 1.9 ad
178 1.13 chs ENTRY(_membar_consumer)
179 1.1 ad LOCK(13)
180 1.1 ad addl $0, -4(%esp)
181 1.1 ad ret
182 1.22 uebayasi END(_membar_consumer)
183 1.11 yamt ENDLABEL(membar_consumer_end)
184 1.1 ad
185 1.13 chs ENTRY(_membar_producer)
186 1.1 ad /* A store is enough */
187 1.1 ad movl $0, -4(%esp)
188 1.1 ad ret
189 1.22 uebayasi END(_membar_producer)
190 1.11 yamt ENDLABEL(membar_producer_end)
191 1.1 ad
192 1.13 chs ENTRY(_membar_sync)
193 1.1 ad LOCK(14)
194 1.1 ad addl $0, -4(%esp)
195 1.1 ad ret
196 1.22 uebayasi END(_membar_sync)
197 1.11 yamt ENDLABEL(membar_sync_end)
198 1.1 ad
199 1.21 christos #if defined(__HAVE_ATOMIC64_OPS) || defined(_KERNEL)
200 1.24 bouyer #ifdef XENPV
201 1.23 bouyer STRONG_ALIAS(_atomic_cas_64,_atomic_cas_cx8)
202 1.23 bouyer #else
203 1.21 christos ENTRY(_atomic_cas_64)
204 1.15 pooka #ifdef _HARDKERNEL
205 1.14 ad pushf
206 1.6 ad cli
207 1.21 christos #endif /* _HARDKERNEL */
208 1.6 ad pushl %edi
209 1.6 ad pushl %ebx
210 1.6 ad movl 12(%esp), %edi
211 1.6 ad movl 16(%esp), %eax
212 1.6 ad movl 20(%esp), %edx
213 1.6 ad movl 24(%esp), %ebx
214 1.6 ad movl 28(%esp), %ecx
215 1.6 ad cmpl 0(%edi), %eax
216 1.6 ad jne 2f
217 1.6 ad cmpl 4(%edi), %edx
218 1.6 ad jne 2f
219 1.6 ad movl %ebx, 0(%edi)
220 1.6 ad movl %ecx, 4(%edi)
221 1.6 ad 1:
222 1.6 ad popl %ebx
223 1.6 ad popl %edi
224 1.21 christos #ifdef _HARDKERNEL
225 1.14 ad popf
226 1.21 christos #endif /* _HARDKERNEL */
227 1.6 ad ret
228 1.6 ad 2:
229 1.6 ad movl 0(%edi), %eax
230 1.6 ad movl 4(%edi), %edx
231 1.6 ad jmp 1b
232 1.22 uebayasi END(_atomic_cas_64)
233 1.11 yamt ENDLABEL(_atomic_cas_64_end)
234 1.23 bouyer #endif /* !XEN */
235 1.6 ad
236 1.13 chs ENTRY(_atomic_cas_cx8)
237 1.6 ad pushl %edi
238 1.6 ad pushl %ebx
239 1.6 ad movl 12(%esp), %edi
240 1.6 ad movl 16(%esp), %eax
241 1.6 ad movl 20(%esp), %edx
242 1.6 ad movl 24(%esp), %ebx
243 1.6 ad movl 28(%esp), %ecx
244 1.6 ad LOCK(15)
245 1.6 ad cmpxchg8b (%edi)
246 1.6 ad popl %ebx
247 1.6 ad popl %edi
248 1.6 ad ret
249 1.21 christos #ifdef _HARDKERNEL
250 1.17 enami #ifdef GPROF
251 1.17 enami .space 16, 0x90
252 1.17 enami #else
253 1.7 ad .space 32, 0x90
254 1.17 enami #endif
255 1.21 christos #endif /* _HARDKERNEL */
256 1.22 uebayasi END(_atomic_cas_cx8)
257 1.11 yamt ENDLABEL(_atomic_cas_cx8_end)
258 1.21 christos #endif /* __HAVE_ATOMIC64_OPS || _KERNEL */
259 1.6 ad
260 1.21 christos #ifdef _HARDKERNEL
261 1.13 chs ENTRY(sse2_lfence)
262 1.1 ad lfence
263 1.1 ad ret
264 1.22 uebayasi END(sse2_lfence)
265 1.11 yamt ENDLABEL(sse2_lfence_end)
266 1.1 ad
267 1.13 chs ENTRY(sse2_mfence)
268 1.1 ad mfence
269 1.1 ad ret
270 1.22 uebayasi END(sse2_mfence)
271 1.11 yamt ENDLABEL(sse2_mfence_end)
272 1.1 ad
273 1.1 ad atomic_lockpatch:
274 1.1 ad .globl atomic_lockpatch
275 1.1 ad .long .Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5
276 1.1 ad .long .Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10
277 1.6 ad .long .Lpatch12, .Lpatch13, .Lpatch14, .Lpatch15, 0
278 1.15 pooka #endif /* _HARDKERNEL */
279 1.1 ad
280 1.1 ad ALIAS(atomic_add_32,_atomic_add_32)
281 1.4 ad ALIAS(atomic_add_int,_atomic_add_32)
282 1.4 ad ALIAS(atomic_add_long,_atomic_add_32)
283 1.1 ad ALIAS(atomic_add_ptr,_atomic_add_32)
284 1.1 ad
285 1.1 ad ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
286 1.4 ad ALIAS(atomic_add_int_nv,_atomic_add_32_nv)
287 1.4 ad ALIAS(atomic_add_long_nv,_atomic_add_32_nv)
288 1.1 ad ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
289 1.1 ad
290 1.1 ad ALIAS(atomic_and_32,_atomic_and_32)
291 1.1 ad ALIAS(atomic_and_uint,_atomic_and_32)
292 1.1 ad ALIAS(atomic_and_ulong,_atomic_and_32)
293 1.1 ad ALIAS(atomic_and_ptr,_atomic_and_32)
294 1.1 ad
295 1.1 ad ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
296 1.1 ad ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
297 1.1 ad ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
298 1.1 ad ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
299 1.1 ad
300 1.1 ad ALIAS(atomic_dec_32,_atomic_dec_32)
301 1.1 ad ALIAS(atomic_dec_uint,_atomic_dec_32)
302 1.1 ad ALIAS(atomic_dec_ulong,_atomic_dec_32)
303 1.1 ad ALIAS(atomic_dec_ptr,_atomic_dec_32)
304 1.1 ad
305 1.1 ad ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
306 1.1 ad ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
307 1.1 ad ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
308 1.1 ad ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
309 1.1 ad
310 1.1 ad ALIAS(atomic_inc_32,_atomic_inc_32)
311 1.1 ad ALIAS(atomic_inc_uint,_atomic_inc_32)
312 1.1 ad ALIAS(atomic_inc_ulong,_atomic_inc_32)
313 1.1 ad ALIAS(atomic_inc_ptr,_atomic_inc_32)
314 1.1 ad
315 1.1 ad ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
316 1.1 ad ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
317 1.1 ad ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
318 1.1 ad ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
319 1.1 ad
320 1.1 ad ALIAS(atomic_or_32,_atomic_or_32)
321 1.1 ad ALIAS(atomic_or_uint,_atomic_or_32)
322 1.1 ad ALIAS(atomic_or_ulong,_atomic_or_32)
323 1.1 ad ALIAS(atomic_or_ptr,_atomic_or_32)
324 1.1 ad
325 1.1 ad ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
326 1.1 ad ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
327 1.1 ad ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
328 1.1 ad ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
329 1.1 ad
330 1.1 ad ALIAS(atomic_swap_32,_atomic_swap_32)
331 1.1 ad ALIAS(atomic_swap_uint,_atomic_swap_32)
332 1.1 ad ALIAS(atomic_swap_ulong,_atomic_swap_32)
333 1.1 ad ALIAS(atomic_swap_ptr,_atomic_swap_32)
334 1.1 ad
335 1.1 ad ALIAS(atomic_cas_32,_atomic_cas_32)
336 1.1 ad ALIAS(atomic_cas_uint,_atomic_cas_32)
337 1.1 ad ALIAS(atomic_cas_ulong,_atomic_cas_32)
338 1.1 ad ALIAS(atomic_cas_ptr,_atomic_cas_32)
339 1.1 ad
340 1.9 ad ALIAS(atomic_cas_32_ni,_atomic_cas_32_ni)
341 1.9 ad ALIAS(atomic_cas_uint_ni,_atomic_cas_32_ni)
342 1.9 ad ALIAS(atomic_cas_ulong_ni,_atomic_cas_32_ni)
343 1.9 ad ALIAS(atomic_cas_ptr_ni,_atomic_cas_32_ni)
344 1.9 ad
345 1.21 christos #if defined(__HAVE_ATOMIC64_OPS) || defined(_KERNEL)
346 1.6 ad ALIAS(atomic_cas_64,_atomic_cas_64)
347 1.9 ad ALIAS(atomic_cas_64_ni,_atomic_cas_64)
348 1.20 martin ALIAS(__sync_val_compare_and_swap_8,_atomic_cas_64)
349 1.21 christos #endif /* __HAVE_ATOMIC64_OPS || _KERNEL */
350 1.6 ad
351 1.1 ad ALIAS(membar_consumer,_membar_consumer)
352 1.1 ad ALIAS(membar_producer,_membar_producer)
353 1.8 ad ALIAS(membar_enter,_membar_consumer)
354 1.8 ad ALIAS(membar_exit,_membar_producer)
355 1.1 ad ALIAS(membar_sync,_membar_sync)
356 1.5 ad
357 1.5 ad STRONG_ALIAS(_atomic_add_int,_atomic_add_32)
358 1.5 ad STRONG_ALIAS(_atomic_add_long,_atomic_add_32)
359 1.5 ad STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32)
360 1.5 ad
361 1.5 ad STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv)
362 1.5 ad STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv)
363 1.5 ad STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv)
364 1.5 ad
365 1.5 ad STRONG_ALIAS(_atomic_and_uint,_atomic_and_32)
366 1.5 ad STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32)
367 1.5 ad STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32)
368 1.5 ad
369 1.5 ad STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv)
370 1.5 ad STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv)
371 1.5 ad STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv)
372 1.5 ad
373 1.5 ad STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32)
374 1.5 ad STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32)
375 1.5 ad STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32)
376 1.5 ad
377 1.5 ad STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv)
378 1.5 ad STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv)
379 1.5 ad STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv)
380 1.5 ad
381 1.5 ad STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32)
382 1.5 ad STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32)
383 1.5 ad STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32)
384 1.5 ad
385 1.5 ad STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv)
386 1.5 ad STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv)
387 1.5 ad STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv)
388 1.5 ad
389 1.5 ad STRONG_ALIAS(_atomic_or_uint,_atomic_or_32)
390 1.5 ad STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32)
391 1.5 ad STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32)
392 1.5 ad
393 1.5 ad STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv)
394 1.5 ad STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv)
395 1.5 ad STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv)
396 1.5 ad
397 1.5 ad STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32)
398 1.5 ad STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32)
399 1.5 ad STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32)
400 1.5 ad
401 1.5 ad STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32)
402 1.5 ad STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32)
403 1.5 ad STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32)
404 1.8 ad
405 1.9 ad STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32_ni)
406 1.9 ad STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32_ni)
407 1.9 ad STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32_ni)
408 1.9 ad
409 1.8 ad STRONG_ALIAS(_membar_enter,_membar_consumer)
410 1.8 ad STRONG_ALIAS(_membar_exit,_membar_producer)
411