xref: /titanic_54/usr/src/common/atomic/sparc/atomic.s (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
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
5*dfb96a4fSab196087 * Common Development and Distribution License (the "License").
6*dfb96a4fSab196087 * 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 */
217c478bd9Sstevel@tonic-gate/*
22*dfb96a4fSab196087 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate	.ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate	.file	"%M%"
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate#include <sys/asm_linkage.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate#if defined(_KERNEL)
337c478bd9Sstevel@tonic-gate	/*
347c478bd9Sstevel@tonic-gate	 * Legacy kernel interfaces; they will go away (eventually).
357c478bd9Sstevel@tonic-gate	 */
367c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas8,atomic_cas_8,function)
377c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas32,atomic_cas_32,function)
387c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(cas64,atomic_cas_64,function)
397c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(caslong,atomic_cas_ulong,function)
407c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(casptr,atomic_cas_ptr,function)
417c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(atomic_and_long,atomic_and_ulong,function)
427c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(atomic_or_long,atomic_or_ulong,function)
437c478bd9Sstevel@tonic-gate	ANSI_PRAGMA_WEAK2(swapl,atomic_swap_32,function)
447c478bd9Sstevel@tonic-gate#else
457c478bd9Sstevel@tonic-gate	/*
467c478bd9Sstevel@tonic-gate	 * Include the definitions for the libc weak aliases.
477c478bd9Sstevel@tonic-gate	 */
487c478bd9Sstevel@tonic-gate#include "../atomic_asm_weak.h"
497c478bd9Sstevel@tonic-gate#endif
507c478bd9Sstevel@tonic-gate
51*dfb96a4fSab196087	/*
52*dfb96a4fSab196087	 * NOTE: If atomic_inc_8 and atomic_inc_8_nv are ever
53*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
54*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
55*dfb96a4fSab196087	 * from atomic_inc_8_nv.
56*dfb96a4fSab196087	 */
577c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_8)
587c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_8_nv)
597c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uchar)
607c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uchar_nv)
617c478bd9Sstevel@tonic-gate	ba	add_8
627c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
637c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uchar_nv)
647c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uchar)
657c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_8_nv)
667c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_8)
677c478bd9Sstevel@tonic-gate
68*dfb96a4fSab196087	/*
69*dfb96a4fSab196087	 * NOTE: If atomic_dec_8 and atomic_dec_8_nv are ever
70*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
71*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
72*dfb96a4fSab196087	 * from atomic_dec_8_nv.
73*dfb96a4fSab196087	 */
747c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_8)
757c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_8_nv)
767c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uchar)
777c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uchar_nv)
787c478bd9Sstevel@tonic-gate	ba	add_8
797c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
807c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uchar_nv)
817c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uchar)
827c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_8_nv)
837c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_8)
847c478bd9Sstevel@tonic-gate
85*dfb96a4fSab196087	/*
86*dfb96a4fSab196087	 * NOTE: If atomic_add_8 and atomic_add_8_nv are ever
87*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
88*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
89*dfb96a4fSab196087	 * from atomic_add_8_nv.
90*dfb96a4fSab196087	 */
917c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_8)
927c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_8_nv)
937c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_char)
947c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_char_nv)
957c478bd9Sstevel@tonic-gateadd_8:
967c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
977c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
987c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
997c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
1007c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
1017c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
1027c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
1037c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
1047c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
1057c478bd9Sstevel@tonic-gate1:
1067c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o5		! add value to the old value
1077c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5		! clear other bits
1087c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear target bits
1097c478bd9Sstevel@tonic-gate	or	%o4, %o5, %o5		! insert the new value
1107c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
1117c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
1127c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
1137c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
1147c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o5
1157c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
1167c478bd9Sstevel@tonic-gate	retl
1177c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
1187c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_char_nv)
1197c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_char)
1207c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_8_nv)
1217c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_8)
1227c478bd9Sstevel@tonic-gate
123*dfb96a4fSab196087	/*
124*dfb96a4fSab196087	 * NOTE: If atomic_inc_16 and atomic_inc_16_nv are ever
125*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
126*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
127*dfb96a4fSab196087	 * from atomic_inc_16_nv.
128*dfb96a4fSab196087	 */
1297c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_16)
1307c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_16_nv)
1317c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ushort)
1327c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ushort_nv)
1337c478bd9Sstevel@tonic-gate	ba	add_16
1347c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
1357c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ushort_nv)
1367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ushort)
1377c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_16_nv)
1387c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_16)
1397c478bd9Sstevel@tonic-gate
140*dfb96a4fSab196087	/*
141*dfb96a4fSab196087	 * NOTE: If atomic_dec_16 and atomic_dec_16_nv are ever
142*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
143*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
144*dfb96a4fSab196087	 * from atomic_dec_16_nv.
145*dfb96a4fSab196087	 */
1467c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_16)
1477c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_16_nv)
1487c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ushort)
1497c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ushort_nv)
1507c478bd9Sstevel@tonic-gate	ba	add_16
1517c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
1527c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ushort_nv)
1537c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ushort)
1547c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_16_nv)
1557c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_16)
1567c478bd9Sstevel@tonic-gate
157*dfb96a4fSab196087	/*
158*dfb96a4fSab196087	 * NOTE: If atomic_add_16 and atomic_add_16_nv are ever
159*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
160*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
161*dfb96a4fSab196087	 * from atomic_add_16_nv.
162*dfb96a4fSab196087	 */
1637c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_16)
1647c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_16_nv)
1657c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_short)
1667c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_short_nv)
1677c478bd9Sstevel@tonic-gateadd_16:
1687c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
1697c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
1707c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
1717c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
1727c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
1737c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
1747c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
1757c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
1767c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
1777c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
1787c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
1797c478bd9Sstevel@tonic-gate1:
1807c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o5		! add value to the old value
1817c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5		! clear other bits
1827c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear target bits
1837c478bd9Sstevel@tonic-gate	or	%o4, %o5, %o5		! insert the new value
1847c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
1857c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
1867c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
1877c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
1887c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o5
1897c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
1907c478bd9Sstevel@tonic-gate	retl
1917c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
1927c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_short_nv)
1937c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_short)
1947c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_16_nv)
1957c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_16)
1967c478bd9Sstevel@tonic-gate
197*dfb96a4fSab196087	/*
198*dfb96a4fSab196087	 * NOTE: If atomic_inc_32 and atomic_inc_32_nv are ever
199*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
200*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
201*dfb96a4fSab196087	 * from atomic_inc_32_nv.
202*dfb96a4fSab196087	 */
2037c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_32)
2047c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_32_nv)
2057c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uint)
2067c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_uint_nv)
2077c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ulong)
2087c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_ulong_nv)
2097c478bd9Sstevel@tonic-gate	ba	add_32
2107c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
2117c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ulong_nv)
2127c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_ulong)
2137c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uint_nv)
2147c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_uint)
2157c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_32_nv)
2167c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_32)
2177c478bd9Sstevel@tonic-gate
218*dfb96a4fSab196087	/*
219*dfb96a4fSab196087	 * NOTE: If atomic_dec_32 and atomic_dec_32_nv are ever
220*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
221*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
222*dfb96a4fSab196087	 * from atomic_dec_32_nv.
223*dfb96a4fSab196087	 */
2247c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_32)
2257c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_32_nv)
2267c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uint)
2277c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_uint_nv)
2287c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ulong)
2297c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_ulong_nv)
2307c478bd9Sstevel@tonic-gate	ba	add_32
2317c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
2327c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ulong_nv)
2337c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_ulong)
2347c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uint_nv)
2357c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_uint)
2367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_32_nv)
2377c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_32)
2387c478bd9Sstevel@tonic-gate
239*dfb96a4fSab196087	/*
240*dfb96a4fSab196087	 * NOTE: If atomic_add_32 and atomic_add_32_nv are ever
241*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
242*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
243*dfb96a4fSab196087	 * from atomic_add_32_nv.
244*dfb96a4fSab196087	 */
2457c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_32)
2467c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_32_nv)
2477c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_int)
2487c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_int_nv)
2497c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_ptr)
2507c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_ptr_nv)
2517c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_long)
2527c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_long_nv)
2537c478bd9Sstevel@tonic-gateadd_32:
2547c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
2557c478bd9Sstevel@tonic-gate1:
2567c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o3
2577c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
2587c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
2597c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
2607c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
2617c478bd9Sstevel@tonic-gate	retl
2627c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o0		! return new value
2637c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_long_nv)
2647c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_long)
2657c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_ptr_nv)
2667c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_ptr)
2677c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_int_nv)
2687c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_int)
2697c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_32_nv)
2707c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_32)
2717c478bd9Sstevel@tonic-gate
272*dfb96a4fSab196087	/*
273*dfb96a4fSab196087	 * NOTE: If atomic_inc_64 and atomic_inc_64_nv are ever
274*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
275*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
276*dfb96a4fSab196087	 * from atomic_inc_64_nv.
277*dfb96a4fSab196087	 */
2787c478bd9Sstevel@tonic-gate	ENTRY(atomic_inc_64)
2797c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_inc_64_nv)
2807c478bd9Sstevel@tonic-gate	ba	add_64
2817c478bd9Sstevel@tonic-gate	  add	%g0, 1, %o1
2827c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_64_nv)
2837c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_inc_64)
2847c478bd9Sstevel@tonic-gate
285*dfb96a4fSab196087	/*
286*dfb96a4fSab196087	 * NOTE: If atomic_dec_64 and atomic_dec_64_nv are ever
287*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
288*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
289*dfb96a4fSab196087	 * from atomic_dec_64_nv.
290*dfb96a4fSab196087	 */
2917c478bd9Sstevel@tonic-gate	ENTRY(atomic_dec_64)
2927c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_dec_64_nv)
2937c478bd9Sstevel@tonic-gate	ba	add_64
2947c478bd9Sstevel@tonic-gate	  sub	%g0, 1, %o1
2957c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_64_nv)
2967c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_dec_64)
2977c478bd9Sstevel@tonic-gate
298*dfb96a4fSab196087	/*
299*dfb96a4fSab196087	 * NOTE: If atomic_add_64 and atomic_add_64_nv are ever
300*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
301*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
302*dfb96a4fSab196087	 * from atomic_add_64_nv.
303*dfb96a4fSab196087	 */
3047c478bd9Sstevel@tonic-gate	ENTRY(atomic_add_64)
3057c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_add_64_nv)
3067c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
3077c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
3087c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
3097c478bd9Sstevel@tonic-gateadd_64:
3107c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
3117c478bd9Sstevel@tonic-gate1:
3127c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o3
3137c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
3147c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
3157c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
3167c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
3177c478bd9Sstevel@tonic-gate	add	%o2, %o1, %o1		! return lower 32-bits in %o1
3187c478bd9Sstevel@tonic-gate	retl
3197c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
3207c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_64_nv)
3217c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_add_64)
3227c478bd9Sstevel@tonic-gate
323*dfb96a4fSab196087	/*
324*dfb96a4fSab196087	 * NOTE: If atomic_or_8 and atomic_or_8_nv are ever
325*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
326*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
327*dfb96a4fSab196087	 * from atomic_or_8_nv.
328*dfb96a4fSab196087	 */
3297c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_8)
3307c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_8_nv)
3317c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uchar)
3327c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uchar_nv)
3337c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
3347c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
3357c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
3367c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
3377c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
3387c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
3397c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
3407c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
3417c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
3427c478bd9Sstevel@tonic-gate1:
3437c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3447c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
3457c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
3467c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
3477c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
3487c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5
3497c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
3507c478bd9Sstevel@tonic-gate	retl
3517c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
3527c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uchar_nv)
3537c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uchar)
3547c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_8_nv)
3557c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_8)
3567c478bd9Sstevel@tonic-gate
357*dfb96a4fSab196087	/*
358*dfb96a4fSab196087	 * NOTE: If atomic_or_16 and atomic_or_16_nv are ever
359*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
360*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
361*dfb96a4fSab196087	 * from atomic_or_16_nv.
362*dfb96a4fSab196087	 */
3637c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_16)
3647c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_16_nv)
3657c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ushort)
3667c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ushort_nv)
3677c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
3687c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
3697c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
3707c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
3717c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
3727c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
3737c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
3747c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
3757c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
3767c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
3777c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
3787c478bd9Sstevel@tonic-gate1:
3797c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3807c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
3817c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
3827c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
3837c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
3847c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o5		! or in the new value
3857c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
3867c478bd9Sstevel@tonic-gate	retl
3877c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
3887c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ushort_nv)
3897c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ushort)
3907c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_16_nv)
3917c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_16)
3927c478bd9Sstevel@tonic-gate
393*dfb96a4fSab196087	/*
394*dfb96a4fSab196087	 * NOTE: If atomic_or_32 and atomic_or_32_nv are ever
395*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
396*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
397*dfb96a4fSab196087	 * from atomic_or_32_nv.
398*dfb96a4fSab196087	 */
3997c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_32)
4007c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_32_nv)
4017c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uint)
4027c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_uint_nv)
4037c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ulong)
4047c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_ulong_nv)
4057c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
4067c478bd9Sstevel@tonic-gate1:
4077c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o3
4087c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
4097c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
4107c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
4117c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
4127c478bd9Sstevel@tonic-gate	retl
4137c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o0		! return new value
4147c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ulong_nv)
4157c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_ulong)
4167c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uint_nv)
4177c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_uint)
4187c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_32_nv)
4197c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_32)
4207c478bd9Sstevel@tonic-gate
421*dfb96a4fSab196087	/*
422*dfb96a4fSab196087	 * NOTE: If atomic_or_64 and atomic_or_64_nv are ever
423*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
424*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
425*dfb96a4fSab196087	 * from atomic_or_64_nv.
426*dfb96a4fSab196087	 */
4277c478bd9Sstevel@tonic-gate	ENTRY(atomic_or_64)
4287c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_or_64_nv)
4297c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
4307c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
4317c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
4327c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
4337c478bd9Sstevel@tonic-gate1:
4347c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o3
4357c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
4367c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
4377c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
4387c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
4397c478bd9Sstevel@tonic-gate	or	%o2, %o1, %o1		! return lower 32-bits in %o1
4407c478bd9Sstevel@tonic-gate	retl
4417c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
4427c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_64_nv)
4437c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_or_64)
4447c478bd9Sstevel@tonic-gate
445*dfb96a4fSab196087	/*
446*dfb96a4fSab196087	 * NOTE: If atomic_and_8 and atomic_and_8_nv are ever
447*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
448*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
449*dfb96a4fSab196087	 * from atomic_and_8_nv.
450*dfb96a4fSab196087	 */
4517c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_8)
4527c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_8_nv)
4537c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uchar)
4547c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uchar_nv)
4557c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
4567c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
4577c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
4587c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
4597c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
4607c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
4617c478bd9Sstevel@tonic-gate	orn	%o1, %o3, %o1		! all ones in other bytes
4627c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
4637c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
4647c478bd9Sstevel@tonic-gate1:
4657c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5		! and in the new value
4667c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
4677c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
4687c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
4697c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
4707c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5
4717c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
4727c478bd9Sstevel@tonic-gate	retl
4737c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
4747c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uchar_nv)
4757c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uchar)
4767c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_8_nv)
4777c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_8)
4787c478bd9Sstevel@tonic-gate
479*dfb96a4fSab196087	/*
480*dfb96a4fSab196087	 * NOTE: If atomic_and_16 and atomic_and_16_nv are ever
481*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
482*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
483*dfb96a4fSab196087	 * from atomic_and_16_nv.
484*dfb96a4fSab196087	 */
4857c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_16)
4867c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_16_nv)
4877c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ushort)
4887c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ushort_nv)
4897c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
4907c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
4917c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
4927c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
4937c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
4947c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
4957c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
4967c478bd9Sstevel@tonic-gate	orn	%o1, %o3, %o1		! all ones in the other half
4977c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
4987c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
4997c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
5007c478bd9Sstevel@tonic-gate1:
5017c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5		! and in the new value
5027c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
5037c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
5047c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
5057c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
5067c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o5
5077c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
5087c478bd9Sstevel@tonic-gate	retl
5097c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = new value
5107c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ushort_nv)
5117c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ushort)
5127c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_16_nv)
5137c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_16)
5147c478bd9Sstevel@tonic-gate
515*dfb96a4fSab196087	/*
516*dfb96a4fSab196087	 * NOTE: If atomic_and_32 and atomic_and_32_nv are ever
517*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
518*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
519*dfb96a4fSab196087	 * from atomic_and_32_nv.
520*dfb96a4fSab196087	 */
5217c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_32)
5227c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_32_nv)
5237c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uint)
5247c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_uint_nv)
5257c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ulong)
5267c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_ulong_nv)
5277c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
5287c478bd9Sstevel@tonic-gate1:
5297c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o3
5307c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
5317c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
5327c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
5337c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
5347c478bd9Sstevel@tonic-gate	retl
5357c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o0		! return new value
5367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ulong_nv)
5377c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_ulong)
5387c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uint_nv)
5397c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_uint)
5407c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_32_nv)
5417c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_32)
5427c478bd9Sstevel@tonic-gate
543*dfb96a4fSab196087	/*
544*dfb96a4fSab196087	 * NOTE: If atomic_and_64 and atomic_and_64_nv are ever
545*dfb96a4fSab196087	 * separated, you need to also edit the libc sparc platform
546*dfb96a4fSab196087	 * specific mapfile and remove the NODYNSORT attribute
547*dfb96a4fSab196087	 * from atomic_and_64_nv.
548*dfb96a4fSab196087	 */
5497c478bd9Sstevel@tonic-gate	ENTRY(atomic_and_64)
5507c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_and_64_nv)
5517c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
5527c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
5537c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
5547c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
5557c478bd9Sstevel@tonic-gate1:
5567c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o3
5577c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
5587c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
5597c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
5607c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
5617c478bd9Sstevel@tonic-gate	and	%o2, %o1, %o1		! return lower 32-bits in %o1
5627c478bd9Sstevel@tonic-gate	retl
5637c478bd9Sstevel@tonic-gate	srlx	%o1, 32, %o0		! return upper 32-bits in %o0
5647c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_64_nv)
5657c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_and_64)
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_8)
5687c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_uchar)
5697c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
5707c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
5717c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
5727c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
5737c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
5747c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
5757c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
5767c478bd9Sstevel@tonic-gate	sll	%o2, %g1, %o2		! %o2 = shifted to bit offset
5777c478bd9Sstevel@tonic-gate	and	%o2, %o3, %o2		! %o2 = single byte value
5787c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
5797c478bd9Sstevel@tonic-gate	ld	[%o0], %o4		! read old value
5807c478bd9Sstevel@tonic-gate1:
5817c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %o4		! clear target bits
5827c478bd9Sstevel@tonic-gate	or	%o4, %o2, %o5		! insert the new value
5837c478bd9Sstevel@tonic-gate	or	%o4, %o1, %o4		! insert the comparison value
5847c478bd9Sstevel@tonic-gate	cas	[%o0], %o4, %o5
5857c478bd9Sstevel@tonic-gate	cmp	%o4, %o5		! did we succeed?
5867c478bd9Sstevel@tonic-gate	be,pt	%icc, 2f
5877c478bd9Sstevel@tonic-gate	  and	%o5, %o3, %o4		! isolate the old value
5887c478bd9Sstevel@tonic-gate	cmp	%o1, %o4		! should we have succeeded?
5897c478bd9Sstevel@tonic-gate	be,a,pt	%icc, 1b		! yes, try again
5907c478bd9Sstevel@tonic-gate	  mov	%o5, %o4		! %o4 = old value
5917c478bd9Sstevel@tonic-gate2:
5927c478bd9Sstevel@tonic-gate	retl
5937c478bd9Sstevel@tonic-gate	srl	%o4, %g1, %o0		! %o0 = old value
5947c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_uchar)
5957c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_8)
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_16)
5987c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ushort)
5997c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
6007c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
6017c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
6027c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
6037c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
6047c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
6057c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6067c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
6077c478bd9Sstevel@tonic-gate	sll	%o2, %g1, %o2		! %o2 = shifted to bit offset
6087c478bd9Sstevel@tonic-gate	and	%o2, %o3, %o2		! %o2 = single short value
6097c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
6107c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
6117c478bd9Sstevel@tonic-gate	ld	[%o0], %o4		! read old value
6127c478bd9Sstevel@tonic-gate1:
6137c478bd9Sstevel@tonic-gate	andn	%o4, %o3, %o4		! clear target bits
6147c478bd9Sstevel@tonic-gate	or	%o4, %o2, %o5		! insert the new value
6157c478bd9Sstevel@tonic-gate	or	%o4, %o1, %o4		! insert the comparison value
6167c478bd9Sstevel@tonic-gate	cas	[%o0], %o4, %o5
6177c478bd9Sstevel@tonic-gate	cmp	%o4, %o5		! did we succeed?
6187c478bd9Sstevel@tonic-gate	be,pt	%icc, 2f
6197c478bd9Sstevel@tonic-gate	  and	%o5, %o3, %o4		! isolate the old value
6207c478bd9Sstevel@tonic-gate	cmp	%o1, %o4		! should we have succeeded?
6217c478bd9Sstevel@tonic-gate	be,a,pt	%icc, 1b		! yes, try again
6227c478bd9Sstevel@tonic-gate	  mov	%o5, %o4		! %o4 = old value
6237c478bd9Sstevel@tonic-gate2:
6247c478bd9Sstevel@tonic-gate	retl
6257c478bd9Sstevel@tonic-gate	srl	%o4, %g1, %o0		! %o0 = old value
6267c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ushort)
6277c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_16)
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_32)
6307c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_uint)
6317c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ptr)
6327c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_cas_ulong)
6337c478bd9Sstevel@tonic-gate	cas	[%o0], %o1, %o2
6347c478bd9Sstevel@tonic-gate	retl
6357c478bd9Sstevel@tonic-gate	mov	%o2, %o0
6367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ulong)
6377c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_ptr)
6387c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_uint)
6397c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_32)
6407c478bd9Sstevel@tonic-gate
6417c478bd9Sstevel@tonic-gate	ENTRY(atomic_cas_64)
6427c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! cmp's upper 32 in %o1, lower in %o2
6437c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2		! convert 2 32-bit args into 1 64-bit
6447c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1
6457c478bd9Sstevel@tonic-gate	sllx	%o3, 32, %o2		! newval upper 32 in %o3, lower in %o4
6467c478bd9Sstevel@tonic-gate	srl	%o4, 0, %o4		! setup %o2 to have newval
6477c478bd9Sstevel@tonic-gate	add	%o2, %o4, %o2
6487c478bd9Sstevel@tonic-gate	casx	[%o0], %o1, %o2
6497c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o1		! return lower 32-bits in %o1
6507c478bd9Sstevel@tonic-gate	retl
6517c478bd9Sstevel@tonic-gate	srlx	%o2, 32, %o0		! return upper 32-bits in %o0
6527c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_cas_64)
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_8)
6557c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_uchar)
6567c478bd9Sstevel@tonic-gate	and	%o0, 0x3, %o4		! %o4 = byte offset, left-to-right
6577c478bd9Sstevel@tonic-gate	xor	%o4, 0x3, %g1		! %g1 = byte offset, right-to-left
6587c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
6597c478bd9Sstevel@tonic-gate	set	0xff, %o3		! %o3 = mask
6607c478bd9Sstevel@tonic-gate	sll	%o3, %g1, %o3		! %o3 = shifted to bit offset
6617c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6627c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single byte value
6637c478bd9Sstevel@tonic-gate	andn	%o0, 0x3, %o0		! %o0 = word address
6647c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
6657c478bd9Sstevel@tonic-gate1:
6667c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o5		! clear target bits
6677c478bd9Sstevel@tonic-gate	or	%o5, %o1, %o5		! insert the new value
6687c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
6697c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
6707c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
6717c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
6727c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
6737c478bd9Sstevel@tonic-gate	retl
6747c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = old value
6757c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_uchar)
6767c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_8)
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_16)
6797c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ushort)
6807c478bd9Sstevel@tonic-gate	and	%o0, 0x2, %o4		! %o4 = byte offset, left-to-right
6817c478bd9Sstevel@tonic-gate	xor	%o4, 0x2, %g1		! %g1 = byte offset, right-to-left
6827c478bd9Sstevel@tonic-gate	sll	%o4, 3, %o4		! %o4 = bit offset, left-to-right
6837c478bd9Sstevel@tonic-gate	sll	%g1, 3, %g1		! %g1 = bit offset, right-to-left
6847c478bd9Sstevel@tonic-gate	sethi	%hi(0xffff0000), %o3	! %o3 = mask
6857c478bd9Sstevel@tonic-gate	srl	%o3, %o4, %o3		! %o3 = shifted to bit offset
6867c478bd9Sstevel@tonic-gate	sll	%o1, %g1, %o1		! %o1 = shifted to bit offset
6877c478bd9Sstevel@tonic-gate	and	%o1, %o3, %o1		! %o1 = single short value
6887c478bd9Sstevel@tonic-gate	andn	%o0, 0x2, %o0		! %o0 = word address
6897c478bd9Sstevel@tonic-gate	! if low-order bit is 1, we will properly get an alignment fault here
6907c478bd9Sstevel@tonic-gate	ld	[%o0], %o2		! read old value
6917c478bd9Sstevel@tonic-gate1:
6927c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o5		! clear target bits
6937c478bd9Sstevel@tonic-gate	or	%o5, %o1, %o5		! insert the new value
6947c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o5
6957c478bd9Sstevel@tonic-gate	cmp	%o2, %o5
6967c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
6977c478bd9Sstevel@tonic-gate	  mov	%o5, %o2		! %o2 = old value
6987c478bd9Sstevel@tonic-gate	and	%o5, %o3, %o5
6997c478bd9Sstevel@tonic-gate	retl
7007c478bd9Sstevel@tonic-gate	srl	%o5, %g1, %o0		! %o0 = old value
7017c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ushort)
7027c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_16)
7037c478bd9Sstevel@tonic-gate
7047c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_32)
7057c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_uint)
7067c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ptr)
7077c478bd9Sstevel@tonic-gate	ALTENTRY(atomic_swap_ulong)
7087c478bd9Sstevel@tonic-gate	ld	[%o0], %o2
7097c478bd9Sstevel@tonic-gate1:
7107c478bd9Sstevel@tonic-gate	mov	%o1, %o3
7117c478bd9Sstevel@tonic-gate	cas	[%o0], %o2, %o3
7127c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
7137c478bd9Sstevel@tonic-gate	bne,a,pn %icc, 1b
7147c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
7157c478bd9Sstevel@tonic-gate	retl
7167c478bd9Sstevel@tonic-gate	mov	%o3, %o0
7177c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ulong)
7187c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_ptr)
7197c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_uint)
7207c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_32)
7217c478bd9Sstevel@tonic-gate
7227c478bd9Sstevel@tonic-gate	ENTRY(atomic_swap_64)
7237c478bd9Sstevel@tonic-gate	sllx	%o1, 32, %o1		! upper 32 in %o1, lower in %o2
7247c478bd9Sstevel@tonic-gate	srl	%o2, 0, %o2
7257c478bd9Sstevel@tonic-gate	add	%o1, %o2, %o1		! convert 2 32-bit args into 1 64-bit
7267c478bd9Sstevel@tonic-gate	ldx	[%o0], %o2
7277c478bd9Sstevel@tonic-gate1:
7287c478bd9Sstevel@tonic-gate	mov	%o1, %o3
7297c478bd9Sstevel@tonic-gate	casx	[%o0], %o2, %o3
7307c478bd9Sstevel@tonic-gate	cmp	%o2, %o3
7317c478bd9Sstevel@tonic-gate	bne,a,pn %xcc, 1b
7327c478bd9Sstevel@tonic-gate	  mov	%o3, %o2
7337c478bd9Sstevel@tonic-gate	srl	%o3, 0, %o1		! return lower 32-bits in %o1
7347c478bd9Sstevel@tonic-gate	retl
7357c478bd9Sstevel@tonic-gate	srlx	%o3, 32, %o0		! return upper 32-bits in %o0
7367c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_swap_64)
7377c478bd9Sstevel@tonic-gate
7387c478bd9Sstevel@tonic-gate	ENTRY(atomic_set_long_excl)
7397c478bd9Sstevel@tonic-gate	mov	1, %o3
7407c478bd9Sstevel@tonic-gate	slln	%o3, %o1, %o3
7417c478bd9Sstevel@tonic-gate	ldn	[%o0], %o2
7427c478bd9Sstevel@tonic-gate1:
7437c478bd9Sstevel@tonic-gate	andcc	%o2, %o3, %g0		! test if the bit is set
7447c478bd9Sstevel@tonic-gate	bnz,a,pn %ncc, 2f		! if so, then fail out
7457c478bd9Sstevel@tonic-gate	  mov	-1, %o0
7467c478bd9Sstevel@tonic-gate	or	%o2, %o3, %o4		! set the bit, and try to commit it
7477c478bd9Sstevel@tonic-gate	casn	[%o0], %o2, %o4
7487c478bd9Sstevel@tonic-gate	cmp	%o2, %o4
7497c478bd9Sstevel@tonic-gate	bne,a,pn %ncc, 1b		! failed to commit, try again
7507c478bd9Sstevel@tonic-gate	  mov	%o4, %o2
7517c478bd9Sstevel@tonic-gate	mov	%g0, %o0
7527c478bd9Sstevel@tonic-gate2:
7537c478bd9Sstevel@tonic-gate	retl
7547c478bd9Sstevel@tonic-gate	nop
7557c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_set_long_excl)
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate	ENTRY(atomic_clear_long_excl)
7587c478bd9Sstevel@tonic-gate	mov	1, %o3
7597c478bd9Sstevel@tonic-gate	slln	%o3, %o1, %o3
7607c478bd9Sstevel@tonic-gate	ldn	[%o0], %o2
7617c478bd9Sstevel@tonic-gate1:
7627c478bd9Sstevel@tonic-gate	andncc	%o3, %o2, %g0		! test if the bit is clear
7637c478bd9Sstevel@tonic-gate	bnz,a,pn %ncc, 2f		! if so, then fail out
7647c478bd9Sstevel@tonic-gate	  mov	-1, %o0
7657c478bd9Sstevel@tonic-gate	andn	%o2, %o3, %o4		! clear the bit, and try to commit it
7667c478bd9Sstevel@tonic-gate	casn	[%o0], %o2, %o4
7677c478bd9Sstevel@tonic-gate	cmp	%o2, %o4
7687c478bd9Sstevel@tonic-gate	bne,a,pn %ncc, 1b		! failed to commit, try again
7697c478bd9Sstevel@tonic-gate	  mov	%o4, %o2
7707c478bd9Sstevel@tonic-gate	mov	%g0, %o0
7717c478bd9Sstevel@tonic-gate2:
7727c478bd9Sstevel@tonic-gate	retl
7737c478bd9Sstevel@tonic-gate	nop
7747c478bd9Sstevel@tonic-gate	SET_SIZE(atomic_clear_long_excl)
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate#if !defined(_KERNEL)
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate	/*
7797c478bd9Sstevel@tonic-gate	 * Spitfires and Blackbirds have a problem with membars in the
7807c478bd9Sstevel@tonic-gate	 * delay slot (SF_ERRATA_51).  For safety's sake, we assume
7817c478bd9Sstevel@tonic-gate	 * that the whole world needs the workaround.
7827c478bd9Sstevel@tonic-gate	 */
7837c478bd9Sstevel@tonic-gate	ENTRY(membar_enter)
7847c478bd9Sstevel@tonic-gate	membar	#StoreLoad|#StoreStore
7857c478bd9Sstevel@tonic-gate	retl
7867c478bd9Sstevel@tonic-gate	nop
7877c478bd9Sstevel@tonic-gate	SET_SIZE(membar_enter)
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate	ENTRY(membar_exit)
7907c478bd9Sstevel@tonic-gate	membar	#LoadStore|#StoreStore
7917c478bd9Sstevel@tonic-gate	retl
7927c478bd9Sstevel@tonic-gate	nop
7937c478bd9Sstevel@tonic-gate	SET_SIZE(membar_exit)
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate	ENTRY(membar_producer)
7967c478bd9Sstevel@tonic-gate	membar	#StoreStore
7977c478bd9Sstevel@tonic-gate	retl
7987c478bd9Sstevel@tonic-gate	nop
7997c478bd9Sstevel@tonic-gate	SET_SIZE(membar_producer)
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate	ENTRY(membar_consumer)
8027c478bd9Sstevel@tonic-gate	membar	#LoadLoad
8037c478bd9Sstevel@tonic-gate	retl
8047c478bd9Sstevel@tonic-gate	nop
8057c478bd9Sstevel@tonic-gate	SET_SIZE(membar_consumer)
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate#endif	/* !_KERNEL */
808