xref: /titanic_44/usr/src/common/atomic/sparc/atomic.s (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
17c478bd9Sstevel@tonic-gate/*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5dfb96a4fSab196087 * Common Development and Distribution License (the "License").
6dfb96a4fSab196087 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate/*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*9a70fc3bSMark J. Nelson	.file	"atomic.s"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate#if defined(_KERNEL)
327c478bd9Sstevel@tonic-gate	/*
337c478bd9Sstevel@tonic-gate	 * Legacy kernel interfaces; they will go away (eventually).
347c478bd9Sstevel@tonic-gate	 */
357c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas8,atomic_cas_8,function)
367c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas32,atomic_cas_32,function)
377c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas64,atomic_cas_64,function)
387c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(caslong,atomic_cas_ulong,function)
397c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(casptr,atomic_cas_ptr,function)
407c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(atomic_and_long,atomic_and_ulong,function)
417c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(atomic_or_long,atomic_or_ulong,function)
427c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(swapl,atomic_swap_32,function)
437c478bd9Sstevel@tonic-gate#endif
447c478bd9Sstevel@tonic-gate
45dfb96a4fSab196087	/*
46dfb96a4fSab196087	 * NOTE: If atomic_inc_8 and atomic_inc_8_nv are ever
47dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
48dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
49dfb96a4fSab196087	 * from atomic_inc_8_nv.
50dfb96a4fSab196087	 */
517c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_8)
527c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_8_nv)
537c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uchar)
547c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uchar_nv)
557c478bd9Sstevel@tonic-gate	ba	add_8
567c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
577c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uchar_nv)
587c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uchar)
597c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_8_nv)
607c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_8)
617c478bd9Sstevel@tonic-gate
62dfb96a4fSab196087	/*
63dfb96a4fSab196087	 * NOTE: If atomic_dec_8 and atomic_dec_8_nv are ever
64dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
65dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
66dfb96a4fSab196087	 * from atomic_dec_8_nv.
67dfb96a4fSab196087	 */
687c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_8)
697c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_8_nv)
707c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uchar)
717c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uchar_nv)
727c478bd9Sstevel@tonic-gate	ba	add_8
737c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
747c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uchar_nv)
757c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uchar)
767c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_8_nv)
777c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_8)
787c478bd9Sstevel@tonic-gate
79dfb96a4fSab196087	/*
80dfb96a4fSab196087	 * NOTE: If atomic_add_8 and atomic_add_8_nv are ever
81dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
82dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
83dfb96a4fSab196087	 * from atomic_add_8_nv.
84dfb96a4fSab196087	 */
857c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_8)
867c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_8_nv)
877c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_char)
887c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_char_nv)
897c478bd9Sstevel@tonic-gateadd_8:
907c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
917c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
927c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
937c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
947c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
957c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
967c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
977c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
987c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
997c478bd9Sstevel@tonic-gate1:
1007c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o5		! add value to the old value
1017c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5		! clear other bits
1027c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear target bits
1037c478bd9Sstevel@tonic-gate	or	%o4, %o5, %o5		! insert the new value
1047c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
1057c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
1067c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
1077c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
1087c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o5
1097c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
1107c478bd9Sstevel@tonic-gate	retl
1117c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
1127c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_char_nv)
1137c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_char)
1147c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_8_nv)
1157c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_8)
1167c478bd9Sstevel@tonic-gate
117dfb96a4fSab196087	/*
118dfb96a4fSab196087	 * NOTE: If atomic_inc_16 and atomic_inc_16_nv are ever
119dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
120dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
121dfb96a4fSab196087	 * from atomic_inc_16_nv.
122dfb96a4fSab196087	 */
1237c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_16)
1247c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_16_nv)
1257c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ushort)
1267c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ushort_nv)
1277c478bd9Sstevel@tonic-gate	ba	add_16
1287c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
1297c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ushort_nv)
1307c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ushort)
1317c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_16_nv)
1327c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_16)
1337c478bd9Sstevel@tonic-gate
134dfb96a4fSab196087	/*
135dfb96a4fSab196087	 * NOTE: If atomic_dec_16 and atomic_dec_16_nv are ever
136dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
137dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
138dfb96a4fSab196087	 * from atomic_dec_16_nv.
139dfb96a4fSab196087	 */
1407c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_16)
1417c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_16_nv)
1427c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ushort)
1437c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ushort_nv)
1447c478bd9Sstevel@tonic-gate	ba	add_16
1457c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
1467c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ushort_nv)
1477c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ushort)
1487c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_16_nv)
1497c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_16)
1507c478bd9Sstevel@tonic-gate
151dfb96a4fSab196087	/*
152dfb96a4fSab196087	 * NOTE: If atomic_add_16 and atomic_add_16_nv are ever
153dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
154dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
155dfb96a4fSab196087	 * from atomic_add_16_nv.
156dfb96a4fSab196087	 */
1577c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_16)
1587c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_16_nv)
1597c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_short)
1607c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_short_nv)
1617c478bd9Sstevel@tonic-gateadd_16:
1627c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
1637c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
1647c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
1657c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
1667c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
1677c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
1687c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
1697c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
1707c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
1717c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
1727c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
1737c478bd9Sstevel@tonic-gate1:
1747c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o5		! add value to the old value
1757c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5		! clear other bits
1767c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear target bits
1777c478bd9Sstevel@tonic-gate	or	%o4, %o5, %o5		! insert the new value
1787c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
1797c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
1807c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
1817c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
1827c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o5
1837c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
1847c478bd9Sstevel@tonic-gate	retl
1857c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
1867c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_short_nv)
1877c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_short)
1887c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_16_nv)
1897c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_16)
1907c478bd9Sstevel@tonic-gate
191dfb96a4fSab196087	/*
192dfb96a4fSab196087	 * NOTE: If atomic_inc_32 and atomic_inc_32_nv are ever
193dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
194dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
195dfb96a4fSab196087	 * from atomic_inc_32_nv.
196dfb96a4fSab196087	 */
1977c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_32)
1987c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_32_nv)
1997c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uint)
2007c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uint_nv)
2017c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ulong)
2027c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ulong_nv)
2037c478bd9Sstevel@tonic-gate	ba	add_32
2047c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
2057c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ulong_nv)
2067c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ulong)
2077c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uint_nv)
2087c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uint)
2097c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_32_nv)
2107c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_32)
2117c478bd9Sstevel@tonic-gate
212dfb96a4fSab196087	/*
213dfb96a4fSab196087	 * NOTE: If atomic_dec_32 and atomic_dec_32_nv are ever
214dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
215dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
216dfb96a4fSab196087	 * from atomic_dec_32_nv.
217dfb96a4fSab196087	 */
2187c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_32)
2197c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_32_nv)
2207c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uint)
2217c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uint_nv)
2227c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ulong)
2237c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ulong_nv)
2247c478bd9Sstevel@tonic-gate	ba	add_32
2257c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
2267c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ulong_nv)
2277c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ulong)
2287c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uint_nv)
2297c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uint)
2307c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_32_nv)
2317c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_32)
2327c478bd9Sstevel@tonic-gate
233dfb96a4fSab196087	/*
234dfb96a4fSab196087	 * NOTE: If atomic_add_32 and atomic_add_32_nv are ever
235dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
236dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
237dfb96a4fSab196087	 * from atomic_add_32_nv.
238dfb96a4fSab196087	 */
2397c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_32)
2407c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_32_nv)
2417c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_int)
2427c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_int_nv)
2437c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_ptr)
2447c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_ptr_nv)
2457c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_long)
2467c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_long_nv)
2477c478bd9Sstevel@tonic-gateadd_32:
2487c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
2497c478bd9Sstevel@tonic-gate1:
2507c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o3
2517c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
2527c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
2537c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
2547c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
2557c478bd9Sstevel@tonic-gate	retl
2567c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o0		! return new value
2577c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_long_nv)
2587c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_long)
2597c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_ptr_nv)
2607c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_ptr)
2617c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_int_nv)
2627c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_int)
2637c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_32_nv)
2647c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_32)
2657c478bd9Sstevel@tonic-gate
266dfb96a4fSab196087	/*
267dfb96a4fSab196087	 * NOTE: If atomic_inc_64 and atomic_inc_64_nv are ever
268dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
269dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
270dfb96a4fSab196087	 * from atomic_inc_64_nv.
271dfb96a4fSab196087	 */
2727c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_64)
2737c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_64_nv)
2747c478bd9Sstevel@tonic-gate	ba	add_64
2757c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
2767c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_64_nv)
2777c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_64)
2787c478bd9Sstevel@tonic-gate
279dfb96a4fSab196087	/*
280dfb96a4fSab196087	 * NOTE: If atomic_dec_64 and atomic_dec_64_nv are ever
281dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
282dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
283dfb96a4fSab196087	 * from atomic_dec_64_nv.
284dfb96a4fSab196087	 */
2857c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_64)
2867c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_64_nv)
2877c478bd9Sstevel@tonic-gate	ba	add_64
2887c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
2897c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_64_nv)
2907c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_64)
2917c478bd9Sstevel@tonic-gate
292dfb96a4fSab196087	/*
293dfb96a4fSab196087	 * NOTE: If atomic_add_64 and atomic_add_64_nv are ever
294dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
295dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
296dfb96a4fSab196087	 * from atomic_add_64_nv.
297dfb96a4fSab196087	 */
2987c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_64)
2997c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_64_nv)
3007c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
3017c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
3027c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
3037c478bd9Sstevel@tonic-gateadd_64:
3047c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
3057c478bd9Sstevel@tonic-gate1:
3067c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o3
3077c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
3087c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
3097c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
3107c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
3117c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o1		! return lower 32-bits in %o1
3127c478bd9Sstevel@tonic-gate	retl
3137c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
3147c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_64_nv)
3157c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_64)
3167c478bd9Sstevel@tonic-gate
317dfb96a4fSab196087	/*
318dfb96a4fSab196087	 * NOTE: If atomic_or_8 and atomic_or_8_nv are ever
319dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
320dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
321dfb96a4fSab196087	 * from atomic_or_8_nv.
322dfb96a4fSab196087	 */
3237c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_8)
3247c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_8_nv)
3257c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uchar)
3267c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uchar_nv)
3277c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
3287c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
3297c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
3307c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
3317c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
3327c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
3337c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
3347c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
3357c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
3367c478bd9Sstevel@tonic-gate1:
3377c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3387c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
3397c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
3407c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
3417c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
3427c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5
3437c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
3447c478bd9Sstevel@tonic-gate	retl
3457c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
3467c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uchar_nv)
3477c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uchar)
3487c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_8_nv)
3497c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_8)
3507c478bd9Sstevel@tonic-gate
351dfb96a4fSab196087	/*
352dfb96a4fSab196087	 * NOTE: If atomic_or_16 and atomic_or_16_nv are ever
353dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
354dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
355dfb96a4fSab196087	 * from atomic_or_16_nv.
356dfb96a4fSab196087	 */
3577c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_16)
3587c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_16_nv)
3597c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ushort)
3607c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ushort_nv)
3617c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
3627c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
3637c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
3647c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
3657c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
3667c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
3677c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
3687c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
3697c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
3707c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
3717c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
3727c478bd9Sstevel@tonic-gate1:
3737c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3747c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
3757c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
3767c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
3777c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
3787c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3797c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
3807c478bd9Sstevel@tonic-gate	retl
3817c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
3827c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ushort_nv)
3837c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ushort)
3847c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_16_nv)
3857c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_16)
3867c478bd9Sstevel@tonic-gate
387dfb96a4fSab196087	/*
388dfb96a4fSab196087	 * NOTE: If atomic_or_32 and atomic_or_32_nv are ever
389dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
390dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
391dfb96a4fSab196087	 * from atomic_or_32_nv.
392dfb96a4fSab196087	 */
3937c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_32)
3947c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_32_nv)
3957c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uint)
3967c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uint_nv)
3977c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ulong)
3987c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ulong_nv)
3997c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
4007c478bd9Sstevel@tonic-gate1:
4017c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o3
4027c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
4037c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
4047c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
4057c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
4067c478bd9Sstevel@tonic-gate	retl
4077c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o0		! return new value
4087c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ulong_nv)
4097c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ulong)
4107c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uint_nv)
4117c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uint)
4127c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_32_nv)
4137c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_32)
4147c478bd9Sstevel@tonic-gate
415dfb96a4fSab196087	/*
416dfb96a4fSab196087	 * NOTE: If atomic_or_64 and atomic_or_64_nv are ever
417dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
418dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
419dfb96a4fSab196087	 * from atomic_or_64_nv.
420dfb96a4fSab196087	 */
4217c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_64)
4227c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_64_nv)
4237c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
4247c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
4257c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
4267c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
4277c478bd9Sstevel@tonic-gate1:
4287c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o3
4297c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
4307c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
4317c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
4327c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
4337c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o1		! return lower 32-bits in %o1
4347c478bd9Sstevel@tonic-gate	retl
4357c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
4367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_64_nv)
4377c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_64)
4387c478bd9Sstevel@tonic-gate
439dfb96a4fSab196087	/*
440dfb96a4fSab196087	 * NOTE: If atomic_and_8 and atomic_and_8_nv are ever
441dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
442dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
443dfb96a4fSab196087	 * from atomic_and_8_nv.
444dfb96a4fSab196087	 */
4457c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_8)
4467c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_8_nv)
4477c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uchar)
4487c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uchar_nv)
4497c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
4507c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
4517c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
4527c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
4537c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
4547c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
4557c478bd9Sstevel@tonic-gate	orn	%o1, %o3, %o1		! all ones in other bytes
4567c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
4577c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
4587c478bd9Sstevel@tonic-gate1:
4597c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5		! and in the new value
4607c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
4617c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
4627c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
4637c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
4647c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5
4657c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
4667c478bd9Sstevel@tonic-gate	retl
4677c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
4687c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uchar_nv)
4697c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uchar)
4707c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_8_nv)
4717c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_8)
4727c478bd9Sstevel@tonic-gate
473dfb96a4fSab196087	/*
474dfb96a4fSab196087	 * NOTE: If atomic_and_16 and atomic_and_16_nv are ever
475dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
476dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
477dfb96a4fSab196087	 * from atomic_and_16_nv.
478dfb96a4fSab196087	 */
4797c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_16)
4807c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_16_nv)
4817c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ushort)
4827c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ushort_nv)
4837c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
4847c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
4857c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
4867c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
4877c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
4887c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
4897c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
4907c478bd9Sstevel@tonic-gate	orn	%o1, %o3, %o1		! all ones in the other half
4917c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
4927c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
4937c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
4947c478bd9Sstevel@tonic-gate1:
4957c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5		! and in the new value
4967c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
4977c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
4987c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
4997c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
5007c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5
5017c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
5027c478bd9Sstevel@tonic-gate	retl
5037c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
5047c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ushort_nv)
5057c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ushort)
5067c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_16_nv)
5077c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_16)
5087c478bd9Sstevel@tonic-gate
509dfb96a4fSab196087	/*
510dfb96a4fSab196087	 * NOTE: If atomic_and_32 and atomic_and_32_nv are ever
511dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
512dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
513dfb96a4fSab196087	 * from atomic_and_32_nv.
514dfb96a4fSab196087	 */
5157c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_32)
5167c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_32_nv)
5177c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uint)
5187c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uint_nv)
5197c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ulong)
5207c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ulong_nv)
5217c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
5227c478bd9Sstevel@tonic-gate1:
5237c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o3
5247c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
5257c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
5267c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
5277c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
5287c478bd9Sstevel@tonic-gate	retl
5297c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o0		! return new value
5307c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ulong_nv)
5317c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ulong)
5327c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uint_nv)
5337c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uint)
5347c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_32_nv)
5357c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_32)
5367c478bd9Sstevel@tonic-gate
537dfb96a4fSab196087	/*
538dfb96a4fSab196087	 * NOTE: If atomic_and_64 and atomic_and_64_nv are ever
539dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
540dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
541dfb96a4fSab196087	 * from atomic_and_64_nv.
542dfb96a4fSab196087	 */
5437c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_64)
5447c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_64_nv)
5457c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
5467c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
5477c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
5487c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
5497c478bd9Sstevel@tonic-gate1:
5507c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o3
5517c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
5527c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
5537c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
5547c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
5557c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o1		! return lower 32-bits in %o1
5567c478bd9Sstevel@tonic-gate	retl
5577c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
5587c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_64_nv)
5597c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_64)
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_8)
5627c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_uchar)
5637c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
5647c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
5657c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
5667c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
5677c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
5687c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
5697c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
5707c478bd9Sstevel@tonic-gate	sll	%o2, %g1, %o2		! %o2 = shifted to bit offset
5717c478bd9Sstevel@tonic-gate	and	%o2, %o3, %o2		! %o2 = single byte value
5727c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
5737c478bd9Sstevel@tonic-gate	ld	[%o0], %o4		! read old value
5747c478bd9Sstevel@tonic-gate1:
5757c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %o4		! clear target bits
5767c478bd9Sstevel@tonic-gate	or	%o4, %o2, %o5		! insert the new value
5777c478bd9Sstevel@tonic-gate	or	%o4, %o1, %o4		! insert the comparison value
5787c478bd9Sstevel@tonic-gate	cas	[%o0], %o4, %o5
5797c478bd9Sstevel@tonic-gate	cmp	%o4, %o5		! did we succeed?
5807c478bd9Sstevel@tonic-gate	be,pt	%icc, 2f
5817c478bd9Sstevel@tonic-gate	  and	%o5, %o3, %o4		! isolate the old value
5827c478bd9Sstevel@tonic-gate	cmp	%o1, %o4		! should we have succeeded?
5837c478bd9Sstevel@tonic-gate	be,a,pt	%icc, 1b		! yes, try again
5847c478bd9Sstevel@tonic-gate	  mov	%o5, %o4		! %o4 = old value
5857c478bd9Sstevel@tonic-gate2:
5867c478bd9Sstevel@tonic-gate	retl
5877c478bd9Sstevel@tonic-gate	srl	%o4, %g1, %o0		! %o0 = old value
5887c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_uchar)
5897c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_8)
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_16)
5927c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ushort)
5937c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
5947c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
5957c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
5967c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
5977c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
5987c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
5997c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6007c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
6017c478bd9Sstevel@tonic-gate	sll	%o2, %g1, %o2		! %o2 = shifted to bit offset
6027c478bd9Sstevel@tonic-gate	and	%o2, %o3, %o2		! %o2 = single short value
6037c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
6047c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
6057c478bd9Sstevel@tonic-gate	ld	[%o0], %o4		! read old value
6067c478bd9Sstevel@tonic-gate1:
6077c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %o4		! clear target bits
6087c478bd9Sstevel@tonic-gate	or	%o4, %o2, %o5		! insert the new value
6097c478bd9Sstevel@tonic-gate	or	%o4, %o1, %o4		! insert the comparison value
6107c478bd9Sstevel@tonic-gate	cas	[%o0], %o4, %o5
6117c478bd9Sstevel@tonic-gate	cmp	%o4, %o5		! did we succeed?
6127c478bd9Sstevel@tonic-gate	be,pt	%icc, 2f
6137c478bd9Sstevel@tonic-gate	  and	%o5, %o3, %o4		! isolate the old value
6147c478bd9Sstevel@tonic-gate	cmp	%o1, %o4		! should we have succeeded?
6157c478bd9Sstevel@tonic-gate	be,a,pt	%icc, 1b		! yes, try again
6167c478bd9Sstevel@tonic-gate	  mov	%o5, %o4		! %o4 = old value
6177c478bd9Sstevel@tonic-gate2:
6187c478bd9Sstevel@tonic-gate	retl
6197c478bd9Sstevel@tonic-gate	srl	%o4, %g1, %o0		! %o0 = old value
6207c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ushort)
6217c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_16)
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_32)
6247c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_uint)
6257c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ptr)
6267c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ulong)
6277c478bd9Sstevel@tonic-gate	cas	[%o0], %o1, %o2
6287c478bd9Sstevel@tonic-gate	retl
6297c478bd9Sstevel@tonic-gate	mov	%o2, %o0
6307c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ulong)
6317c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ptr)
6327c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_uint)
6337c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_32)
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_64)
6367c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! cmp's upper 32 in %o1, lower in %o2
6377c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2		! convert 2 32-bit args into 1 64-bit
6387c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1
6397c478bd9Sstevel@tonic-gate	sllx	%o3, 32, %o2		! newval upper 32 in %o3, lower in %o4
6407c478bd9Sstevel@tonic-gate	srl	%o4, 0, %o4		! setup %o2 to have newval
6417c478bd9Sstevel@tonic-gate	add	%o2, %o4, %o2
6427c478bd9Sstevel@tonic-gate	casx	[%o0], %o1, %o2
6437c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o1		! return lower 32-bits in %o1
6447c478bd9Sstevel@tonic-gate	retl
6457c478bd9Sstevel@tonic-gate	srlx	%o2, 32, %o0		! return upper 32-bits in %o0
6467c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_64)
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_8)
6497c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_uchar)
6507c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
6517c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
6527c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
6537c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
6547c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
6557c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6567c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
6577c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
6587c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
6597c478bd9Sstevel@tonic-gate1:
6607c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o5		! clear target bits
6617c478bd9Sstevel@tonic-gate	or	%o5, %o1, %o5		! insert the new value
6627c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
6637c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
6647c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
6657c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
6667c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
6677c478bd9Sstevel@tonic-gate	retl
6687c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = old value
6697c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_uchar)
6707c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_8)
6717c478bd9Sstevel@tonic-gate
6727c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_16)
6737c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ushort)
6747c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
6757c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
6767c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
6777c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
6787c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
6797c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
6807c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6817c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
6827c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
6837c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
6847c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
6857c478bd9Sstevel@tonic-gate1:
6867c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o5		! clear target bits
6877c478bd9Sstevel@tonic-gate	or	%o5, %o1, %o5		! insert the new value
6887c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
6897c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
6907c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
6917c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
6927c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
6937c478bd9Sstevel@tonic-gate	retl
6947c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = old value
6957c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ushort)
6967c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_16)
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_32)
6997c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_uint)
7007c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ptr)
7017c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ulong)
7027c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
7037c478bd9Sstevel@tonic-gate1:
7047c478bd9Sstevel@tonic-gate	mov	%o1, %o3
7057c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
7067c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
7077c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
7087c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
7097c478bd9Sstevel@tonic-gate	retl
7107c478bd9Sstevel@tonic-gate	mov	%o3, %o0
7117c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ulong)
7127c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ptr)
7137c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_uint)
7147c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_32)
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_64)
7177c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
7187c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
7197c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
7207c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
7217c478bd9Sstevel@tonic-gate1:
7227c478bd9Sstevel@tonic-gate	mov	%o1, %o3
7237c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
7247c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
7257c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
7267c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
7277c478bd9Sstevel@tonic-gate	srl	%o3, 0, %o1		! return lower 32-bits in %o1
7287c478bd9Sstevel@tonic-gate	retl
7297c478bd9Sstevel@tonic-gate	srlx	%o3, 32, %o0		! return upper 32-bits in %o0
7307c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_64)
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate	ENTRY(atomic_set_long_excl)
7337c478bd9Sstevel@tonic-gate	mov	1, %o3
7347c478bd9Sstevel@tonic-gate	slln	%o3, %o1, %o3
7357c478bd9Sstevel@tonic-gate	ldn	[%o0], %o2
7367c478bd9Sstevel@tonic-gate1:
7377c478bd9Sstevel@tonic-gate	andcc	%o2, %o3, %g0		! test if the bit is set
7387c478bd9Sstevel@tonic-gate	bnz,a,pn %ncc, 2f		! if so, then fail out
7397c478bd9Sstevel@tonic-gate	  mov	-1, %o0
7407c478bd9Sstevel@tonic-gate	or	%o2, %o3, %o4		! set the bit, and try to commit it
7417c478bd9Sstevel@tonic-gate	casn	[%o0], %o2, %o4
7427c478bd9Sstevel@tonic-gate	cmp	%o2, %o4
7437c478bd9Sstevel@tonic-gate	bne,a,pn %ncc, 1b		! failed to commit, try again
7447c478bd9Sstevel@tonic-gate	  mov	%o4, %o2
7457c478bd9Sstevel@tonic-gate	mov	%g0, %o0
7467c478bd9Sstevel@tonic-gate2:
7477c478bd9Sstevel@tonic-gate	retl
7487c478bd9Sstevel@tonic-gate	nop
7497c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_set_long_excl)
7507c478bd9Sstevel@tonic-gate
7517c478bd9Sstevel@tonic-gate	ENTRY(atomic_clear_long_excl)
7527c478bd9Sstevel@tonic-gate	mov	1, %o3
7537c478bd9Sstevel@tonic-gate	slln	%o3, %o1, %o3
7547c478bd9Sstevel@tonic-gate	ldn	[%o0], %o2
7557c478bd9Sstevel@tonic-gate1:
7567c478bd9Sstevel@tonic-gate	andncc	%o3, %o2, %g0		! test if the bit is clear
7577c478bd9Sstevel@tonic-gate	bnz,a,pn %ncc, 2f		! if so, then fail out
7587c478bd9Sstevel@tonic-gate	  mov	-1, %o0
7597c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear the bit, and try to commit it
7607c478bd9Sstevel@tonic-gate	casn	[%o0], %o2, %o4
7617c478bd9Sstevel@tonic-gate	cmp	%o2, %o4
7627c478bd9Sstevel@tonic-gate	bne,a,pn %ncc, 1b		! failed to commit, try again
7637c478bd9Sstevel@tonic-gate	  mov	%o4, %o2
7647c478bd9Sstevel@tonic-gate	mov	%g0, %o0
7657c478bd9Sstevel@tonic-gate2:
7667c478bd9Sstevel@tonic-gate	retl
7677c478bd9Sstevel@tonic-gate	nop
7687c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_clear_long_excl)
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate#if !defined(_KERNEL)
7717c478bd9Sstevel@tonic-gate
7727c478bd9Sstevel@tonic-gate	/*
7737c478bd9Sstevel@tonic-gate	 * Spitfires and Blackbirds have a problem with membars in the
7747c478bd9Sstevel@tonic-gate	 * delay slot (SF_ERRATA_51).  For safety's sake, we assume
7757c478bd9Sstevel@tonic-gate	 * that the whole world needs the workaround.
7767c478bd9Sstevel@tonic-gate	 */
7777c478bd9Sstevel@tonic-gate	ENTRY(membar_enter)
7787c478bd9Sstevel@tonic-gate	membar	#StoreLoad|#StoreStore
7797c478bd9Sstevel@tonic-gate	retl
7807c478bd9Sstevel@tonic-gate	nop
7817c478bd9Sstevel@tonic-gate	SET_SIZE(membar_enter)
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate	ENTRY(membar_exit)
7847c478bd9Sstevel@tonic-gate	membar	#LoadStore|#StoreStore
7857c478bd9Sstevel@tonic-gate	retl
7867c478bd9Sstevel@tonic-gate	nop
7877c478bd9Sstevel@tonic-gate	SET_SIZE(membar_exit)
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate	ENTRY(membar_producer)
7907c478bd9Sstevel@tonic-gate	membar	#StoreStore
7917c478bd9Sstevel@tonic-gate	retl
7927c478bd9Sstevel@tonic-gate	nop
7937c478bd9Sstevel@tonic-gate	SET_SIZE(membar_producer)
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate	ENTRY(membar_consumer)
7967c478bd9Sstevel@tonic-gate	membar	#LoadLoad
7977c478bd9Sstevel@tonic-gate	retl
7987c478bd9Sstevel@tonic-gate	nop
7997c478bd9Sstevel@tonic-gate	SET_SIZE(membar_consumer)
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate#endif	/* !_KERNEL */
802