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