Home | History | Annotate | Line # | Download | only in atomic
atomic.S revision 1.1
      1 /*	$NetBSD: atomic.S,v 1.1 2007/11/28 01:33:47 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,b)	.align  b; LABEL(a)
     45 #else
     46 #define	LOCK(n)		lock
     47 #define	ALIAS(f, t)	WEAK_ALIAS(f,t)
     48 #define	END(a,b)	/* 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 	LOCK(11)
    139 	xchgl	%eax, (%edx)
    140 	ret
    141 
    142 NENTRY(_atomic_cas_32)
    143 	movl	4(%esp), %edx
    144 	movl	8(%esp), %eax
    145 	movl	12(%esp), %ecx
    146 	LOCK(12)
    147 	cmpxchgl %ecx, (%edx)
    148 	/* %eax now contains the old value */
    149 	ret
    150 
    151 NENTRY(_membar_consumer)
    152 	LOCK(13)
    153 	addl	$0, -4(%esp)
    154 	ret
    155 END(membar_consumer_end, 8)
    156 
    157 NENTRY(_membar_producer)
    158 	/* A store is enough */
    159 	movl	$0, -4(%esp)
    160 	ret
    161 END(membar_producer_end, 8)
    162 
    163 NENTRY(_membar_enter)
    164 	/* A store is enough */
    165 	movl	$0, -4(%esp)
    166 	ret
    167 END(membar_enter_end, 8)
    168 
    169 NENTRY(_membar_exit)
    170 	/* A store is enough */
    171 	movl	$0, -4(%esp)
    172 	ret
    173 END(membar_exit_end, 8)
    174 
    175 NENTRY(_membar_sync)
    176 	LOCK(14)
    177 	addl	$0, -4(%esp)
    178 	ret
    179 END(membar_sync_end, 8)
    180 
    181 #ifdef _KERNEL
    182 NENTRY(membar_nop)
    183 	/* Nothing */
    184 	ret
    185 END(membar_nop_end, 8)
    186 
    187 NENTRY(sse2_lfence)
    188 	lfence
    189 	ret
    190 END(sse2_lfence_end, 8)
    191 
    192 NENTRY(sse2_mfence)
    193 	mfence
    194 	ret
    195 END(sse2_mfence_end, 8)
    196 
    197 atomic_lockpatch:
    198 	.globl	atomic_lockpatch
    199 	.long	.Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5
    200 	.long	.Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10
    201 	.long	.Lpatch11, .Lpatch12, .Lpatch13, .Lpatch14, 0
    202 #endif	/* _KERNEL */
    203 
    204 ALIAS(atomic_add_32,_atomic_add_32)
    205 ALIAS(atomic_add_uint,_atomic_add_32)
    206 ALIAS(atomic_add_ulong,_atomic_add_32)
    207 ALIAS(atomic_add_ptr,_atomic_add_32)
    208 
    209 ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
    210 ALIAS(atomic_add_uint_nv,_atomic_add_32_nv)
    211 ALIAS(atomic_add_ulong_nv,_atomic_add_32_nv)
    212 ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
    213 
    214 ALIAS(atomic_and_32,_atomic_and_32)
    215 ALIAS(atomic_and_uint,_atomic_and_32)
    216 ALIAS(atomic_and_ulong,_atomic_and_32)
    217 ALIAS(atomic_and_ptr,_atomic_and_32)
    218 
    219 ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
    220 ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
    221 ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
    222 ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
    223 
    224 ALIAS(atomic_dec_32,_atomic_dec_32)
    225 ALIAS(atomic_dec_uint,_atomic_dec_32)
    226 ALIAS(atomic_dec_ulong,_atomic_dec_32)
    227 ALIAS(atomic_dec_ptr,_atomic_dec_32)
    228 
    229 ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
    230 ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
    231 ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
    232 ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
    233 
    234 ALIAS(atomic_inc_32,_atomic_inc_32)
    235 ALIAS(atomic_inc_uint,_atomic_inc_32)
    236 ALIAS(atomic_inc_ulong,_atomic_inc_32)
    237 ALIAS(atomic_inc_ptr,_atomic_inc_32)
    238 
    239 ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
    240 ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
    241 ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
    242 ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
    243 
    244 ALIAS(atomic_or_32,_atomic_or_32)
    245 ALIAS(atomic_or_uint,_atomic_or_32)
    246 ALIAS(atomic_or_ulong,_atomic_or_32)
    247 ALIAS(atomic_or_ptr,_atomic_or_32)
    248 
    249 ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
    250 ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
    251 ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
    252 ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
    253 
    254 ALIAS(atomic_swap_32,_atomic_swap_32)
    255 ALIAS(atomic_swap_uint,_atomic_swap_32)
    256 ALIAS(atomic_swap_ulong,_atomic_swap_32)
    257 ALIAS(atomic_swap_ptr,_atomic_swap_32)
    258 
    259 ALIAS(atomic_cas_32,_atomic_cas_32)
    260 ALIAS(atomic_cas_uint,_atomic_cas_32)
    261 ALIAS(atomic_cas_ulong,_atomic_cas_32)
    262 ALIAS(atomic_cas_ptr,_atomic_cas_32)
    263 
    264 ALIAS(membar_consumer,_membar_consumer)
    265 ALIAS(membar_producer,_membar_producer)
    266 ALIAS(membar_enter,_membar_enter)
    267 ALIAS(membar_exit,_membar_exit)
    268 ALIAS(membar_sync,_membar_sync)
    269