Home | History | Annotate | Line # | Download | only in atomic
atomic.S revision 1.15
      1  1.15  pooka /*	$NetBSD: atomic.S,v 1.15 2009/01/04 18:21:38 pooka 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.1     ad #include <machine/asm.h>
     33   1.1     ad 
     34  1.15  pooka #if defined(_KERNEL) && !defined(_RUMPKERNEL)
     35  1.15  pooka #define _HARDKERNEL
     36  1.15  pooka #endif
     37  1.15  pooka 
     38  1.15  pooka #ifdef _HARDKERNEL
     39   1.1     ad #define	LOCK(n)		.Lpatch/**/n:	lock
     40   1.1     ad #define	ALIAS(f, t)	STRONG_ALIAS(f,t)
     41  1.12   yamt #define	ENDLABEL(a)	_ALIGN_TEXT; LABEL(a)
     42   1.1     ad #else
     43   1.1     ad #define	LOCK(n)		lock
     44   1.1     ad #define	ALIAS(f, t)	WEAK_ALIAS(f,t)
     45  1.12   yamt #define	ENDLABEL(a)	/* nothing */
     46   1.1     ad #endif
     47   1.1     ad 
     48   1.1     ad 	.text
     49   1.1     ad 
     50  1.13    chs ENTRY(_atomic_add_32)
     51   1.1     ad 	movl	4(%esp), %edx
     52   1.1     ad 	movl	8(%esp), %eax
     53   1.1     ad 	LOCK(1)
     54   1.1     ad 	addl	%eax, (%edx)
     55   1.1     ad 	ret
     56   1.1     ad 
     57  1.13    chs ENTRY(_atomic_add_32_nv)
     58   1.1     ad 	movl	4(%esp), %edx
     59   1.1     ad 	movl	8(%esp), %eax
     60   1.1     ad 	movl	%eax, %ecx
     61   1.1     ad 	LOCK(2)
     62   1.1     ad 	xaddl	%eax, (%edx)
     63   1.1     ad 	addl	%ecx, %eax
     64   1.1     ad 	ret
     65   1.1     ad 
     66  1.13    chs ENTRY(_atomic_and_32)
     67   1.1     ad 	movl	4(%esp), %edx
     68   1.1     ad 	movl	8(%esp), %eax
     69   1.1     ad 	LOCK(3)
     70   1.1     ad 	andl	%eax, (%edx)
     71   1.1     ad 	ret
     72   1.1     ad 
     73  1.13    chs ENTRY(_atomic_and_32_nv)
     74   1.1     ad 	movl	4(%esp), %edx
     75   1.1     ad 	movl	(%edx), %eax
     76  1.14     ad 0:
     77   1.1     ad 	movl	%eax, %ecx
     78   1.1     ad 	andl	8(%esp), %ecx
     79   1.1     ad 	LOCK(4)
     80   1.1     ad 	cmpxchgl %ecx, (%edx)
     81  1.14     ad 	jnz	1f
     82   1.1     ad 	movl	%ecx, %eax
     83   1.1     ad 	ret
     84  1.14     ad 1:
     85  1.14     ad 	jmp	0b
     86   1.1     ad 
     87  1.13    chs ENTRY(_atomic_dec_32)
     88   1.1     ad 	movl	4(%esp), %edx
     89   1.1     ad 	LOCK(5)
     90   1.1     ad 	decl	(%edx)
     91   1.1     ad 	ret
     92   1.1     ad 
     93  1.13    chs ENTRY(_atomic_dec_32_nv)
     94   1.1     ad 	movl	4(%esp), %edx
     95   1.1     ad 	movl	$-1, %eax
     96   1.1     ad 	LOCK(6)
     97   1.1     ad 	xaddl	%eax, (%edx)
     98   1.1     ad 	decl	%eax
     99   1.1     ad 	ret
    100   1.1     ad 
    101  1.13    chs ENTRY(_atomic_inc_32)
    102   1.1     ad 	movl	4(%esp), %edx
    103   1.1     ad 	LOCK(7)
    104   1.1     ad 	incl	(%edx)
    105   1.1     ad 	ret
    106   1.1     ad 
    107  1.13    chs ENTRY(_atomic_inc_32_nv)
    108   1.1     ad 	movl	4(%esp), %edx
    109   1.1     ad 	movl	$1, %eax
    110   1.1     ad 	LOCK(8)
    111   1.1     ad 	xaddl	%eax, (%edx)
    112   1.1     ad 	incl	%eax
    113   1.1     ad 	ret
    114   1.1     ad 
    115  1.13    chs ENTRY(_atomic_or_32)
    116   1.1     ad 	movl	4(%esp), %edx
    117   1.1     ad 	movl	8(%esp), %eax
    118   1.1     ad 	LOCK(9)
    119   1.1     ad 	orl	%eax, (%edx)
    120   1.1     ad 	ret
    121   1.1     ad 
    122  1.13    chs ENTRY(_atomic_or_32_nv)
    123   1.1     ad 	movl	4(%esp), %edx
    124   1.1     ad 	movl	(%edx), %eax
    125  1.14     ad 0:
    126   1.1     ad 	movl	%eax, %ecx
    127   1.1     ad 	orl	8(%esp), %ecx
    128   1.1     ad 	LOCK(10)
    129   1.1     ad 	cmpxchgl %ecx, (%edx)
    130  1.14     ad 	jnz	1f
    131   1.1     ad 	movl	%ecx, %eax
    132   1.1     ad 	ret
    133  1.14     ad 1:
    134  1.14     ad 	jmp	0b
    135   1.1     ad 
    136  1.13    chs ENTRY(_atomic_swap_32)
    137   1.1     ad 	movl	4(%esp), %edx
    138   1.1     ad 	movl	8(%esp), %eax
    139   1.1     ad 	xchgl	%eax, (%edx)
    140   1.1     ad 	ret
    141   1.1     ad 
    142  1.13    chs ENTRY(_atomic_cas_32)
    143   1.1     ad 	movl	4(%esp), %edx
    144   1.1     ad 	movl	8(%esp), %eax
    145   1.1     ad 	movl	12(%esp), %ecx
    146   1.1     ad 	LOCK(12)
    147   1.1     ad 	cmpxchgl %ecx, (%edx)
    148   1.1     ad 	/* %eax now contains the old value */
    149   1.1     ad 	ret
    150   1.1     ad 
    151  1.13    chs ENTRY(_atomic_cas_32_ni)
    152   1.9     ad 	movl	4(%esp), %edx
    153   1.9     ad 	movl	8(%esp), %eax
    154   1.9     ad 	movl	12(%esp), %ecx
    155   1.9     ad 	cmpxchgl %ecx, (%edx)
    156   1.9     ad 	/* %eax now contains the old value */
    157   1.9     ad 	ret
    158   1.9     ad 
    159  1.13    chs ENTRY(_membar_consumer)
    160   1.1     ad 	LOCK(13)
    161   1.1     ad 	addl	$0, -4(%esp)
    162   1.1     ad 	ret
    163  1.11   yamt ENDLABEL(membar_consumer_end)
    164   1.1     ad 
    165  1.13    chs ENTRY(_membar_producer)
    166   1.1     ad 	/* A store is enough */
    167   1.1     ad 	movl	$0, -4(%esp)
    168   1.1     ad 	ret
    169  1.11   yamt ENDLABEL(membar_producer_end)
    170   1.1     ad 
    171  1.13    chs ENTRY(_membar_sync)
    172   1.1     ad 	LOCK(14)
    173   1.1     ad 	addl	$0, -4(%esp)
    174   1.1     ad 	ret
    175  1.11   yamt ENDLABEL(membar_sync_end)
    176   1.1     ad 
    177  1.15  pooka #ifdef _HARDKERNEL
    178  1.13    chs ENTRY(_atomic_cas_64)
    179  1.14     ad 	pushf
    180   1.6     ad 	cli
    181   1.6     ad 	pushl	%edi
    182   1.6     ad 	pushl	%ebx
    183   1.6     ad 	movl	12(%esp), %edi
    184   1.6     ad 	movl	16(%esp), %eax
    185   1.6     ad 	movl	20(%esp), %edx
    186   1.6     ad 	movl	24(%esp), %ebx
    187   1.6     ad 	movl	28(%esp), %ecx
    188   1.6     ad 	cmpl	0(%edi), %eax
    189   1.6     ad 	jne	2f
    190   1.6     ad 	cmpl	4(%edi), %edx
    191   1.6     ad 	jne	2f
    192   1.6     ad 	movl	%ebx, 0(%edi)
    193   1.6     ad 	movl	%ecx, 4(%edi)
    194   1.6     ad 1:
    195   1.6     ad 	popl	%ebx
    196   1.6     ad 	popl	%edi
    197  1.14     ad 	popf
    198   1.6     ad 	ret
    199   1.6     ad 2:
    200   1.6     ad 	movl	0(%edi), %eax
    201   1.6     ad 	movl	4(%edi), %edx
    202   1.6     ad 	jmp	1b
    203  1.11   yamt ENDLABEL(_atomic_cas_64_end)
    204   1.6     ad 
    205  1.13    chs ENTRY(_atomic_cas_cx8)
    206   1.6     ad 	pushl	%edi
    207   1.6     ad 	pushl	%ebx
    208   1.6     ad 	movl	12(%esp), %edi
    209   1.6     ad 	movl	16(%esp), %eax
    210   1.6     ad 	movl	20(%esp), %edx
    211   1.6     ad 	movl	24(%esp), %ebx
    212   1.6     ad 	movl	28(%esp), %ecx
    213   1.6     ad 	LOCK(15)
    214   1.6     ad 	cmpxchg8b (%edi)
    215   1.6     ad 	popl	%ebx
    216   1.6     ad 	popl	%edi
    217   1.6     ad 	ret
    218   1.7     ad 	.space	32, 0x90
    219  1.11   yamt ENDLABEL(_atomic_cas_cx8_end)
    220   1.6     ad 
    221  1.13    chs ENTRY(sse2_lfence)
    222   1.1     ad 	lfence
    223   1.1     ad 	ret
    224  1.11   yamt ENDLABEL(sse2_lfence_end)
    225   1.1     ad 
    226  1.13    chs ENTRY(sse2_mfence)
    227   1.1     ad 	mfence
    228   1.1     ad 	ret
    229  1.11   yamt ENDLABEL(sse2_mfence_end)
    230   1.1     ad 
    231   1.1     ad atomic_lockpatch:
    232   1.1     ad 	.globl	atomic_lockpatch
    233   1.1     ad 	.long	.Lpatch1, .Lpatch2, .Lpatch3, .Lpatch4, .Lpatch5
    234   1.1     ad 	.long	.Lpatch6, .Lpatch7, .Lpatch8, .Lpatch9, .Lpatch10
    235   1.6     ad 	.long	.Lpatch12, .Lpatch13, .Lpatch14, .Lpatch15, 0
    236   1.7     ad #else
    237  1.13    chs ENTRY(_atomic_cas_64)
    238   1.7     ad 	pushl	%edi
    239   1.7     ad 	pushl	%ebx
    240   1.7     ad 	movl	12(%esp), %edi
    241   1.7     ad 	movl	16(%esp), %eax
    242   1.7     ad 	movl	20(%esp), %edx
    243   1.7     ad 	movl	24(%esp), %ebx
    244   1.7     ad 	movl	28(%esp), %ecx
    245   1.7     ad 	lock
    246   1.7     ad 	cmpxchg8b (%edi)
    247   1.7     ad 	popl	%ebx
    248   1.7     ad 	popl	%edi
    249   1.7     ad 	ret
    250  1.15  pooka #endif	/* _HARDKERNEL */
    251   1.1     ad 
    252   1.1     ad ALIAS(atomic_add_32,_atomic_add_32)
    253   1.4     ad ALIAS(atomic_add_int,_atomic_add_32)
    254   1.4     ad ALIAS(atomic_add_long,_atomic_add_32)
    255   1.1     ad ALIAS(atomic_add_ptr,_atomic_add_32)
    256   1.1     ad 
    257   1.1     ad ALIAS(atomic_add_32_nv,_atomic_add_32_nv)
    258   1.4     ad ALIAS(atomic_add_int_nv,_atomic_add_32_nv)
    259   1.4     ad ALIAS(atomic_add_long_nv,_atomic_add_32_nv)
    260   1.1     ad ALIAS(atomic_add_ptr_nv,_atomic_add_32_nv)
    261   1.1     ad 
    262   1.1     ad ALIAS(atomic_and_32,_atomic_and_32)
    263   1.1     ad ALIAS(atomic_and_uint,_atomic_and_32)
    264   1.1     ad ALIAS(atomic_and_ulong,_atomic_and_32)
    265   1.1     ad ALIAS(atomic_and_ptr,_atomic_and_32)
    266   1.1     ad 
    267   1.1     ad ALIAS(atomic_and_32_nv,_atomic_and_32_nv)
    268   1.1     ad ALIAS(atomic_and_uint_nv,_atomic_and_32_nv)
    269   1.1     ad ALIAS(atomic_and_ulong_nv,_atomic_and_32_nv)
    270   1.1     ad ALIAS(atomic_and_ptr_nv,_atomic_and_32_nv)
    271   1.1     ad 
    272   1.1     ad ALIAS(atomic_dec_32,_atomic_dec_32)
    273   1.1     ad ALIAS(atomic_dec_uint,_atomic_dec_32)
    274   1.1     ad ALIAS(atomic_dec_ulong,_atomic_dec_32)
    275   1.1     ad ALIAS(atomic_dec_ptr,_atomic_dec_32)
    276   1.1     ad 
    277   1.1     ad ALIAS(atomic_dec_32_nv,_atomic_dec_32_nv)
    278   1.1     ad ALIAS(atomic_dec_uint_nv,_atomic_dec_32_nv)
    279   1.1     ad ALIAS(atomic_dec_ulong_nv,_atomic_dec_32_nv)
    280   1.1     ad ALIAS(atomic_dec_ptr_nv,_atomic_dec_32_nv)
    281   1.1     ad 
    282   1.1     ad ALIAS(atomic_inc_32,_atomic_inc_32)
    283   1.1     ad ALIAS(atomic_inc_uint,_atomic_inc_32)
    284   1.1     ad ALIAS(atomic_inc_ulong,_atomic_inc_32)
    285   1.1     ad ALIAS(atomic_inc_ptr,_atomic_inc_32)
    286   1.1     ad 
    287   1.1     ad ALIAS(atomic_inc_32_nv,_atomic_inc_32_nv)
    288   1.1     ad ALIAS(atomic_inc_uint_nv,_atomic_inc_32_nv)
    289   1.1     ad ALIAS(atomic_inc_ulong_nv,_atomic_inc_32_nv)
    290   1.1     ad ALIAS(atomic_inc_ptr_nv,_atomic_inc_32_nv)
    291   1.1     ad 
    292   1.1     ad ALIAS(atomic_or_32,_atomic_or_32)
    293   1.1     ad ALIAS(atomic_or_uint,_atomic_or_32)
    294   1.1     ad ALIAS(atomic_or_ulong,_atomic_or_32)
    295   1.1     ad ALIAS(atomic_or_ptr,_atomic_or_32)
    296   1.1     ad 
    297   1.1     ad ALIAS(atomic_or_32_nv,_atomic_or_32_nv)
    298   1.1     ad ALIAS(atomic_or_uint_nv,_atomic_or_32_nv)
    299   1.1     ad ALIAS(atomic_or_ulong_nv,_atomic_or_32_nv)
    300   1.1     ad ALIAS(atomic_or_ptr_nv,_atomic_or_32_nv)
    301   1.1     ad 
    302   1.1     ad ALIAS(atomic_swap_32,_atomic_swap_32)
    303   1.1     ad ALIAS(atomic_swap_uint,_atomic_swap_32)
    304   1.1     ad ALIAS(atomic_swap_ulong,_atomic_swap_32)
    305   1.1     ad ALIAS(atomic_swap_ptr,_atomic_swap_32)
    306   1.1     ad 
    307   1.1     ad ALIAS(atomic_cas_32,_atomic_cas_32)
    308   1.1     ad ALIAS(atomic_cas_uint,_atomic_cas_32)
    309   1.1     ad ALIAS(atomic_cas_ulong,_atomic_cas_32)
    310   1.1     ad ALIAS(atomic_cas_ptr,_atomic_cas_32)
    311   1.1     ad 
    312   1.9     ad ALIAS(atomic_cas_32_ni,_atomic_cas_32_ni)
    313   1.9     ad ALIAS(atomic_cas_uint_ni,_atomic_cas_32_ni)
    314   1.9     ad ALIAS(atomic_cas_ulong_ni,_atomic_cas_32_ni)
    315   1.9     ad ALIAS(atomic_cas_ptr_ni,_atomic_cas_32_ni)
    316   1.9     ad 
    317   1.6     ad ALIAS(atomic_cas_64,_atomic_cas_64)
    318   1.9     ad ALIAS(atomic_cas_64_ni,_atomic_cas_64)
    319   1.6     ad 
    320   1.1     ad ALIAS(membar_consumer,_membar_consumer)
    321   1.1     ad ALIAS(membar_producer,_membar_producer)
    322   1.8     ad ALIAS(membar_enter,_membar_consumer)
    323   1.8     ad ALIAS(membar_exit,_membar_producer)
    324   1.1     ad ALIAS(membar_sync,_membar_sync)
    325   1.5     ad 
    326   1.5     ad STRONG_ALIAS(_atomic_add_int,_atomic_add_32)
    327   1.5     ad STRONG_ALIAS(_atomic_add_long,_atomic_add_32)
    328   1.5     ad STRONG_ALIAS(_atomic_add_ptr,_atomic_add_32)
    329   1.5     ad 
    330   1.5     ad STRONG_ALIAS(_atomic_add_int_nv,_atomic_add_32_nv)
    331   1.5     ad STRONG_ALIAS(_atomic_add_long_nv,_atomic_add_32_nv)
    332   1.5     ad STRONG_ALIAS(_atomic_add_ptr_nv,_atomic_add_32_nv)
    333   1.5     ad 
    334   1.5     ad STRONG_ALIAS(_atomic_and_uint,_atomic_and_32)
    335   1.5     ad STRONG_ALIAS(_atomic_and_ulong,_atomic_and_32)
    336   1.5     ad STRONG_ALIAS(_atomic_and_ptr,_atomic_and_32)
    337   1.5     ad 
    338   1.5     ad STRONG_ALIAS(_atomic_and_uint_nv,_atomic_and_32_nv)
    339   1.5     ad STRONG_ALIAS(_atomic_and_ulong_nv,_atomic_and_32_nv)
    340   1.5     ad STRONG_ALIAS(_atomic_and_ptr_nv,_atomic_and_32_nv)
    341   1.5     ad 
    342   1.5     ad STRONG_ALIAS(_atomic_dec_uint,_atomic_dec_32)
    343   1.5     ad STRONG_ALIAS(_atomic_dec_ulong,_atomic_dec_32)
    344   1.5     ad STRONG_ALIAS(_atomic_dec_ptr,_atomic_dec_32)
    345   1.5     ad 
    346   1.5     ad STRONG_ALIAS(_atomic_dec_uint_nv,_atomic_dec_32_nv)
    347   1.5     ad STRONG_ALIAS(_atomic_dec_ulong_nv,_atomic_dec_32_nv)
    348   1.5     ad STRONG_ALIAS(_atomic_dec_ptr_nv,_atomic_dec_32_nv)
    349   1.5     ad 
    350   1.5     ad STRONG_ALIAS(_atomic_inc_uint,_atomic_inc_32)
    351   1.5     ad STRONG_ALIAS(_atomic_inc_ulong,_atomic_inc_32)
    352   1.5     ad STRONG_ALIAS(_atomic_inc_ptr,_atomic_inc_32)
    353   1.5     ad 
    354   1.5     ad STRONG_ALIAS(_atomic_inc_uint_nv,_atomic_inc_32_nv)
    355   1.5     ad STRONG_ALIAS(_atomic_inc_ulong_nv,_atomic_inc_32_nv)
    356   1.5     ad STRONG_ALIAS(_atomic_inc_ptr_nv,_atomic_inc_32_nv)
    357   1.5     ad 
    358   1.5     ad STRONG_ALIAS(_atomic_or_uint,_atomic_or_32)
    359   1.5     ad STRONG_ALIAS(_atomic_or_ulong,_atomic_or_32)
    360   1.5     ad STRONG_ALIAS(_atomic_or_ptr,_atomic_or_32)
    361   1.5     ad 
    362   1.5     ad STRONG_ALIAS(_atomic_or_uint_nv,_atomic_or_32_nv)
    363   1.5     ad STRONG_ALIAS(_atomic_or_ulong_nv,_atomic_or_32_nv)
    364   1.5     ad STRONG_ALIAS(_atomic_or_ptr_nv,_atomic_or_32_nv)
    365   1.5     ad 
    366   1.5     ad STRONG_ALIAS(_atomic_swap_uint,_atomic_swap_32)
    367   1.5     ad STRONG_ALIAS(_atomic_swap_ulong,_atomic_swap_32)
    368   1.5     ad STRONG_ALIAS(_atomic_swap_ptr,_atomic_swap_32)
    369   1.5     ad 
    370   1.5     ad STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32)
    371   1.5     ad STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32)
    372   1.5     ad STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32)
    373   1.8     ad 
    374   1.9     ad STRONG_ALIAS(_atomic_cas_uint_ni,_atomic_cas_32_ni)
    375   1.9     ad STRONG_ALIAS(_atomic_cas_ulong_ni,_atomic_cas_32_ni)
    376   1.9     ad STRONG_ALIAS(_atomic_cas_ptr_ni,_atomic_cas_32_ni)
    377   1.9     ad 
    378   1.8     ad STRONG_ALIAS(_membar_enter,_membar_consumer)
    379   1.8     ad STRONG_ALIAS(_membar_exit,_membar_producer)
    380