Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / system / util / atomic.s
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: atomic.s
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
#if defined(ARCH_X64)
.text
.align 16
.globl atomic_add_32
.type atomic_add_32, @function
atomic_add_32:
movl %esi,%eax
lock
xaddl %eax,(%rdi)
ret
.size atomic_add_32, [.-atomic_add_32]
.align 16
.globl atomic_add_64
.type atomic_add_64, @function
atomic_add_64:
movq %rsi,%rax
lock
xaddq %rax,(%rdi)
ret
.size atomic_add_64, [.-atomic_add_64]
#else
#include <sys/asm_linkage.h>
!
! atomic ADD, SUB, AND, OR, XOR operations, both 32 and 64 bit versions.
!
! currently these return the "previous" value, but would the "new" value
! be more useful ???
!
! compile with "ASFLAGS= -P -D_ASM" using /usr/ccs/bin/as
!
ENTRY(atomic_add_32)
! %o0 = address of counter, %o1 = value to add to it
1: ld [%o0], %o2
add %o1, %o2, %o3
cas [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_add_32)
! which is more useful ???
! add %o1, %o2, %o3 ! return updated value,
ENTRY(atomic_sub_32)
! %o0 = address of counter, %o1 = value to add to it
1: ld [%o0], %o2
sub %o1, %o2, %o3
cas [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_sub_32)
ENTRY(atomic_and_32)
! %o0 = address of counter, %o1 = value to add to it
1: ld [%o0], %o2
and %o1, %o2, %o3
cas [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_and_32)
ENTRY(atomic_or_32)
! %o0 = address of counter, %o1 = value to add to it
1: ld [%o0], %o2
or %o1, %o2, %o3
cas [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_or_32)
ENTRY(atomic_xor_32)
! %o0 = address of counter, %o1 = value to add to it
1: ld [%o0], %o2
xor %o1, %o2, %o3
cas [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_xor_32)
#if 1 /* change to ifdef arch-v9 when we figure out how... */
ENTRY(atomic_add_64)
! %o0 = address of counter, %o1 = value to add to it
1: ldx [%o0], %o2
add %o1, %o2, %o3
casx [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_add_64)
ENTRY(atomic_sub_64)
! %o0 = address of counter, %o1 = value to add to it
1: ldx [%o0], %o2
sub %o1, %o2, %o3
casx [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_sub_64)
ENTRY(atomic_and_64)
! %o0 = address of counter, %o1 = value to add to it
1: ldx [%o0], %o2
and %o1, %o2, %o3
casx [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_and_64)
ENTRY(atomic_or_64)
! %o0 = address of counter, %o1 = value to add to it
1: ldx [%o0], %o2
or %o1, %o2, %o3
casx [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_or_64)
ENTRY(atomic_xor_64)
! %o0 = address of counter, %o1 = value to add to it
1: ldx [%o0], %o2
xor %o1, %o2, %o3
casx [%o0], %o2, %o3 ! swap [%o0] and %o3 _IFF_ [%o0] == %o2
cmp %o2, %o3
bne 1b
nop ! there's no "annul if taken" flavor of branch
jmpl %o7+8, %g0
mov %o3, %o0 ! return previous value
SET_SIZE(atomic_xor_64)
#endif
#endif