atomic_cas.S revision 1.1.2.1 1 1.1.2.1 thorpej /* $NetBSD: atomic_cas.S,v 1.1.2.1 2007/04/27 05:57:38 thorpej Exp $ */
2 1.1.2.1 thorpej
3 1.1.2.1 thorpej /*-
4 1.1.2.1 thorpej * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 1.1.2.1 thorpej * All rights reserved.
6 1.1.2.1 thorpej *
7 1.1.2.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 thorpej * by Andrew Doran and Jason R. Thorpe.
9 1.1.2.1 thorpej *
10 1.1.2.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 thorpej * modification, are permitted provided that the following conditions
12 1.1.2.1 thorpej * are met:
13 1.1.2.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.2.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1.2.1 thorpej * must display the following acknowledgement:
20 1.1.2.1 thorpej * This product includes software developed by the NetBSD
21 1.1.2.1 thorpej * Foundation, Inc. and its contributors.
22 1.1.2.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1.2.1 thorpej * from this software without specific prior written permission.
25 1.1.2.1 thorpej *
26 1.1.2.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.1 thorpej */
38 1.1.2.1 thorpej
39 1.1.2.1 thorpej #include "atomic_op_asm.h"
40 1.1.2.1 thorpej
41 1.1.2.1 thorpej #if defined(_KERNEL)
42 1.1.2.1 thorpej
43 1.1.2.1 thorpej #include "opt_multiprocessor.h"
44 1.1.2.1 thorpej
45 1.1.2.1 thorpej #define DISABLE_INTERRUPTS \
46 1.1.2.1 thorpej rd %psr, %o4 ! disable interrupts ;\
47 1.1.2.1 thorpej or %o4, PSR_IPL, %o5 ;\
48 1.1.2.1 thorpej wr %o5, 0, %psr ;\
49 1.1.2.1 thorpej nop ;\
50 1.1.2.1 thorpej nop ;\
51 1.1.2.1 thorpej nop
52 1.1.2.1 thorpej
53 1.1.2.1 thorpej #define RESTORE_INTERRUPTS \
54 1.1.2.1 thorpej wr %o4, 0, %psr ! re-enable interrupts ;\
55 1.1.2.1 thorpej nop ;\
56 1.1.2.1 thorpej nop ;\
57 1.1.2.1 thorpej nop
58 1.1.2.1 thorpej
59 1.1.2.1 thorpej #if defined(MULTIPROCESSOR)
60 1.1.2.1 thorpej .section .bss
61 1.1.2.1 thorpej .align 1024
62 1.1.2.1 thorpej OTYPE(_C_LABEL(_atomic_cas_locktab))
63 1.1.2.1 thorpej _C_LABEL(_atomic_cas_locktab):
64 1.1.2.1 thorpej .space 1024
65 1.1.2.1 thorpej
66 1.1.2.1 thorpej #define ACQUIRE_INTERLOCK \
67 1.1.2.1 thorpej DISABLE_INTERRUPTS ;\
68 1.1.2.1 thorpej srl %o0, 3, %o5 ! get interlock address ;\
69 1.1.2.1 thorpej and %o5, 1023, %o5 ;\
70 1.1.2.1 thorpej set _C_LABEL(_atomic_cas_locktab), %o3 ;\
71 1.1.2.1 thorpej add %o5, %o3, %o5 ;\
72 1.1.2.1 thorpej ;\
73 1.1.2.1 thorpej ! %o5 has interlock address ;\
74 1.1.2.1 thorpej ;\
75 1.1.2.1 thorpej 1: ldstub [%o5], %o3 ! acquire interlock ;\
76 1.1.2.1 thorpej tst %o3 ;\
77 1.1.2.1 thorpej bz,a 2f ;\
78 1.1.2.1 thorpej nop ;\
79 1.1.2.1 thorpej nop ;\
80 1.1.2.1 thorpej nop ;\
81 1.1.2.1 thorpej b,a 1b ! spin ;\
82 1.1.2.1 thorpej nop ;\
83 1.1.2.1 thorpej ;\
84 1.1.2.1 thorpej ! We now hold the interlock ;\
85 1.1.2.1 thorpej 2:
86 1.1.2.1 thorpej
87 1.1.2.1 thorpej #define RELEASE_INTERLOCK \
88 1.1.2.1 thorpej stb %g0, [%o5] ! release interlock ;\
89 1.1.2.1 thorpej RESTORE_INTERRUPTS
90 1.1.2.1 thorpej
91 1.1.2.1 thorpej #else /* ! MULTIPROCESSOR */
92 1.1.2.1 thorpej
93 1.1.2.1 thorpej #define ACQUIRE_INTERLOCK DISABLE_INTERRUPTS
94 1.1.2.1 thorpej
95 1.1.2.1 thorpej #define RELEASE_INTERLOCK RESTORE_INTERRUPTS
96 1.1.2.1 thorpej
97 1.1.2.1 thorpej #endif /* MULTIPROCESSOR */
98 1.1.2.1 thorpej
99 1.1.2.1 thorpej .text
100 1.1.2.1 thorpej
101 1.1.2.1 thorpej /*
102 1.1.2.1 thorpej * The v7 and v8 SPARC doesn't have compare-and-swap, so we block interrupts
103 1.1.2.1 thorpej * and use an interlock.
104 1.1.2.1 thorpej *
105 1.1.2.1 thorpej * XXX On single CPU systems, this should use a restartable sequence:
106 1.1.2.1 thorpej * XXX there we don't need the overhead of interlocking.
107 1.1.2.1 thorpej *
108 1.1.2.1 thorpej * XXX NOTE! The interlock trick only works if EVERYTHING writes to
109 1.1.2.1 thorpej * XXX the memory cell through this code path!
110 1.1.2.1 thorpej *
111 1.1.2.1 thorpej * XXX NOTE! We rely on 8-bit and 16-bit ops to resolve to the same
112 1.1.2.1 thorpej * XXX interlock as their corresponding 32-bit word.
113 1.1.2.1 thorpej */
114 1.1.2.1 thorpej ENTRY_NOPROFILE(_atomic_cas_8)
115 1.1.2.1 thorpej ACQUIRE_INTERLOCK
116 1.1.2.1 thorpej ! %o4 has saved PSR value
117 1.1.2.1 thorpej ! %o5 has interlock address
118 1.1.2.1 thorpej
119 1.1.2.1 thorpej ldub [%o0], %o3 ! get old value
120 1.1.2.1 thorpej cmp %o1, %o3 ! old == new?
121 1.1.2.1 thorpej beq,a 3f ! yes, do the store
122 1.1.2.1 thorpej stb %o2, [%o0] ! (in the delay slot)
123 1.1.2.1 thorpej
124 1.1.2.1 thorpej 3: RELEASE_INTERLOCK
125 1.1.2.1 thorpej
126 1.1.2.1 thorpej retl
127 1.1.2.1 thorpej mov %o3, %o0 ! return old value
128 1.1.2.1 thorpej
129 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_8,_atomic_cas_8)
130 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_uchar,_atomic_cas_8)
131 1.1.2.1 thorpej STRONG_ALIAS(_atomic_cas_uchar,_atomic_cas_8)
132 1.1.2.1 thorpej
133 1.1.2.1 thorpej ENTRY_NOPROFILE(_atomic_cas_16)
134 1.1.2.1 thorpej ACQUIRE_INTERLOCK
135 1.1.2.1 thorpej ! %o4 has saved PSR value
136 1.1.2.1 thorpej ! %o5 has interlock address
137 1.1.2.1 thorpej
138 1.1.2.1 thorpej lduh [%o0], %o3 ! get old value
139 1.1.2.1 thorpej cmp %o1, %o3 ! old == new?
140 1.1.2.1 thorpej beq,a 3f ! yes, do the store
141 1.1.2.1 thorpej sth %o2, [%o0] ! (in the delay slot)
142 1.1.2.1 thorpej
143 1.1.2.1 thorpej 3: RELEASE_INTERLOCK
144 1.1.2.1 thorpej
145 1.1.2.1 thorpej retl
146 1.1.2.1 thorpej mov %o3, %o0 ! return old value
147 1.1.2.1 thorpej
148 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_16,_atomic_cas_16)
149 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_ushort,_atomic_cas_16)
150 1.1.2.1 thorpej STRONG_ALIAS(_atomic_cas_ushort,_atomic_cas_16)
151 1.1.2.1 thorpej
152 1.1.2.1 thorpej ENTRY_NOPROFILE(_atomic_cas_32)
153 1.1.2.1 thorpej ACQUIRE_INTERLOCK
154 1.1.2.1 thorpej ! %o4 has saved PSR value
155 1.1.2.1 thorpej ! %o5 has interlock address
156 1.1.2.1 thorpej
157 1.1.2.1 thorpej ld [%o0], %o3 ! get old value
158 1.1.2.1 thorpej cmp %o1, %o3 ! old == new?
159 1.1.2.1 thorpej beq,a 3f ! yes, do the store
160 1.1.2.1 thorpej st %o2, [%o0] ! (in the delay slot)
161 1.1.2.1 thorpej
162 1.1.2.1 thorpej 3: RELEASE_INTERLOCK
163 1.1.2.1 thorpej
164 1.1.2.1 thorpej retl
165 1.1.2.1 thorpej mov %o3, %o0 ! return old value
166 1.1.2.1 thorpej
167 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_32,_atomic_cas_32)
168 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_uint,_atomic_cas_32)
169 1.1.2.1 thorpej STRONG_ALIAS(_atomic_cas_uint,_atomic_cas_32)
170 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_ulong,_atomic_cas_32)
171 1.1.2.1 thorpej STRONG_ALIAS(_atomic_cas_ulong,_atomic_cas_32)
172 1.1.2.1 thorpej ATOMIC_OP_ALIAS(atomic_cas_ptr,_atomic_cas_32)
173 1.1.2.1 thorpej STRONG_ALIAS(_atomic_cas_ptr,_atomic_cas_32)
174 1.1.2.1 thorpej
175 1.1.2.1 thorpej #else /* _KERNEL */
176 1.1.2.1 thorpej
177 1.1.2.1 thorpej #endif /* _KERNEL */
178