initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / sparc64 / lib / debuglocks.c
blob9fb4dedc89e13934f84bf093bde4ca2145f01825
1 /* $Id: debuglocks.c,v 1.9 2001/11/17 00:10:48 davem Exp $
2 * debuglocks.c: Debugging versions of SMP locking primitives.
4 * Copyright (C) 1998 David S. Miller (davem@redhat.com)
5 */
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/spinlock.h>
11 #include <asm/system.h>
13 #ifdef CONFIG_SMP
15 #define GET_CALLER(PC) __asm__ __volatile__("mov %%i7, %0" : "=r" (PC))
17 static inline void show (char *str, spinlock_t *lock, unsigned long caller)
19 int cpu = smp_processor_id();
21 printk("%s(%p) CPU#%d stuck at %08x, owner PC(%08x):CPU(%x)\n",
22 str, lock, cpu, (unsigned int) caller,
23 lock->owner_pc, lock->owner_cpu);
26 static inline void show_read (char *str, rwlock_t *lock, unsigned long caller)
28 int cpu = smp_processor_id();
30 printk("%s(%p) CPU#%d stuck at %08x, writer PC(%08x):CPU(%x)\n",
31 str, lock, cpu, (unsigned int) caller,
32 lock->writer_pc, lock->writer_cpu);
35 static inline void show_write (char *str, rwlock_t *lock, unsigned long caller)
37 int cpu = smp_processor_id();
38 int i;
40 printk("%s(%p) CPU#%d stuck at %08x\n",
41 str, lock, cpu, (unsigned int) caller);
42 printk("Writer: PC(%08x):CPU(%x)\n",
43 lock->writer_pc, lock->writer_cpu);
44 printk("Readers:");
45 for (i = 0; i < NR_CPUS; i++)
46 if (lock->reader_pc[i])
47 printk(" %d[%08x]", i, lock->reader_pc[i]);
48 printk("\n");
51 #undef INIT_STUCK
52 #define INIT_STUCK 100000000
54 void _do_spin_lock(spinlock_t *lock, char *str)
56 unsigned long caller, val;
57 int stuck = INIT_STUCK;
58 int cpu = get_cpu();
59 int shown = 0;
61 GET_CALLER(caller);
62 again:
63 __asm__ __volatile__("ldstub [%1], %0"
64 : "=r" (val)
65 : "r" (&(lock->lock))
66 : "memory");
67 membar("#StoreLoad | #StoreStore");
68 if (val) {
69 while (lock->lock) {
70 if (!--stuck) {
71 if (shown++ <= 2)
72 show(str, lock, caller);
73 stuck = INIT_STUCK;
75 membar("#LoadLoad");
77 goto again;
79 lock->owner_pc = ((unsigned int)caller);
80 lock->owner_cpu = cpu;
81 current->thread.smp_lock_count++;
82 current->thread.smp_lock_pc = ((unsigned int)caller);
84 put_cpu();
87 int _do_spin_trylock(spinlock_t *lock)
89 unsigned long val, caller;
90 int cpu = get_cpu();
92 GET_CALLER(caller);
93 __asm__ __volatile__("ldstub [%1], %0"
94 : "=r" (val)
95 : "r" (&(lock->lock))
96 : "memory");
97 membar("#StoreLoad | #StoreStore");
98 if (!val) {
99 lock->owner_pc = ((unsigned int)caller);
100 lock->owner_cpu = cpu;
101 current->thread.smp_lock_count++;
102 current->thread.smp_lock_pc = ((unsigned int)caller);
105 put_cpu();
107 return val == 0;
110 void _do_spin_unlock(spinlock_t *lock)
112 lock->owner_pc = 0;
113 lock->owner_cpu = NO_PROC_ID;
114 membar("#StoreStore | #LoadStore");
115 lock->lock = 0;
116 current->thread.smp_lock_count--;
119 /* Keep INIT_STUCK the same... */
121 void _do_read_lock (rwlock_t *rw, char *str)
123 unsigned long caller, val;
124 int stuck = INIT_STUCK;
125 int cpu = get_cpu();
126 int shown = 0;
128 GET_CALLER(caller);
129 wlock_again:
130 /* Wait for any writer to go away. */
131 while (((long)(rw->lock)) < 0) {
132 if (!--stuck) {
133 if (shown++ <= 2)
134 show_read(str, rw, caller);
135 stuck = INIT_STUCK;
137 membar("#LoadLoad");
139 /* Try once to increment the counter. */
140 __asm__ __volatile__(
141 " ldx [%0], %%g5\n"
142 " brlz,a,pn %%g5, 2f\n"
143 " mov 1, %0\n"
144 " add %%g5, 1, %%g7\n"
145 " casx [%0], %%g5, %%g7\n"
146 " sub %%g5, %%g7, %0\n"
147 "2:" : "=r" (val)
148 : "0" (&(rw->lock))
149 : "g5", "g7", "memory");
150 membar("#StoreLoad | #StoreStore");
151 if (val)
152 goto wlock_again;
153 rw->reader_pc[cpu] = ((unsigned int)caller);
154 current->thread.smp_lock_count++;
155 current->thread.smp_lock_pc = ((unsigned int)caller);
157 put_cpu();
160 void _do_read_unlock (rwlock_t *rw, char *str)
162 unsigned long caller, val;
163 int stuck = INIT_STUCK;
164 int cpu = get_cpu();
165 int shown = 0;
167 GET_CALLER(caller);
169 /* Drop our identity _first_. */
170 rw->reader_pc[cpu] = 0;
171 current->thread.smp_lock_count--;
172 runlock_again:
173 /* Spin trying to decrement the counter using casx. */
174 __asm__ __volatile__(
175 " ldx [%0], %%g5\n"
176 " sub %%g5, 1, %%g7\n"
177 " casx [%0], %%g5, %%g7\n"
178 " membar #StoreLoad | #StoreStore\n"
179 " sub %%g5, %%g7, %0\n"
180 : "=r" (val)
181 : "0" (&(rw->lock))
182 : "g5", "g7", "memory");
183 if (val) {
184 if (!--stuck) {
185 if (shown++ <= 2)
186 show_read(str, rw, caller);
187 stuck = INIT_STUCK;
189 goto runlock_again;
192 put_cpu();
195 void _do_write_lock (rwlock_t *rw, char *str)
197 unsigned long caller, val;
198 int stuck = INIT_STUCK;
199 int cpu = get_cpu();
200 int shown = 0;
202 GET_CALLER(caller);
203 wlock_again:
204 /* Spin while there is another writer. */
205 while (((long)rw->lock) < 0) {
206 if (!--stuck) {
207 if (shown++ <= 2)
208 show_write(str, rw, caller);
209 stuck = INIT_STUCK;
211 membar("#LoadLoad");
214 /* Try to acuire the write bit. */
215 __asm__ __volatile__(
216 " mov 1, %%g3\n"
217 " sllx %%g3, 63, %%g3\n"
218 " ldx [%0], %%g5\n"
219 " brlz,pn %%g5, 1f\n"
220 " or %%g5, %%g3, %%g7\n"
221 " casx [%0], %%g5, %%g7\n"
222 " membar #StoreLoad | #StoreStore\n"
223 " ba,pt %%xcc, 2f\n"
224 " sub %%g5, %%g7, %0\n"
225 "1: mov 1, %0\n"
226 "2:" : "=r" (val)
227 : "0" (&(rw->lock))
228 : "g3", "g5", "g7", "memory");
229 if (val) {
230 /* We couldn't get the write bit. */
231 if (!--stuck) {
232 if (shown++ <= 2)
233 show_write(str, rw, caller);
234 stuck = INIT_STUCK;
236 goto wlock_again;
238 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
239 /* Readers still around, drop the write
240 * lock, spin, and try again.
242 if (!--stuck) {
243 if (shown++ <= 2)
244 show_write(str, rw, caller);
245 stuck = INIT_STUCK;
247 __asm__ __volatile__(
248 " mov 1, %%g3\n"
249 " sllx %%g3, 63, %%g3\n"
250 "1: ldx [%0], %%g5\n"
251 " andn %%g5, %%g3, %%g7\n"
252 " casx [%0], %%g5, %%g7\n"
253 " cmp %%g5, %%g7\n"
254 " bne,pn %%xcc, 1b\n"
255 " membar #StoreLoad | #StoreStore"
256 : /* no outputs */
257 : "r" (&(rw->lock))
258 : "g3", "g5", "g7", "cc", "memory");
259 while(rw->lock != 0) {
260 if (!--stuck) {
261 if (shown++ <= 2)
262 show_write(str, rw, caller);
263 stuck = INIT_STUCK;
265 membar("#LoadLoad");
267 goto wlock_again;
270 /* We have it, say who we are. */
271 rw->writer_pc = ((unsigned int)caller);
272 rw->writer_cpu = cpu;
273 current->thread.smp_lock_count++;
274 current->thread.smp_lock_pc = ((unsigned int)caller);
276 put_cpu();
279 void _do_write_unlock(rwlock_t *rw)
281 unsigned long caller, val;
282 int stuck = INIT_STUCK;
283 int shown = 0;
285 GET_CALLER(caller);
287 /* Drop our identity _first_ */
288 rw->writer_pc = 0;
289 rw->writer_cpu = NO_PROC_ID;
290 current->thread.smp_lock_count--;
291 wlock_again:
292 __asm__ __volatile__(
293 " mov 1, %%g3\n"
294 " sllx %%g3, 63, %%g3\n"
295 " ldx [%0], %%g5\n"
296 " andn %%g5, %%g3, %%g7\n"
297 " casx [%0], %%g5, %%g7\n"
298 " membar #StoreLoad | #StoreStore\n"
299 " sub %%g5, %%g7, %0\n"
300 : "=r" (val)
301 : "0" (&(rw->lock))
302 : "g3", "g5", "g7", "memory");
303 if (val) {
304 if (!--stuck) {
305 if (shown++ <= 2)
306 show_write("write_unlock", rw, caller);
307 stuck = INIT_STUCK;
309 goto wlock_again;
313 int _do_write_trylock (rwlock_t *rw, char *str)
315 unsigned long caller, val;
316 int cpu = get_cpu();
318 GET_CALLER(caller);
320 /* Try to acuire the write bit. */
321 __asm__ __volatile__(
322 " mov 1, %%g3\n"
323 " sllx %%g3, 63, %%g3\n"
324 " ldx [%0], %%g5\n"
325 " brlz,pn %%g5, 1f\n"
326 " or %%g5, %%g3, %%g7\n"
327 " casx [%0], %%g5, %%g7\n"
328 " membar #StoreLoad | #StoreStore\n"
329 " ba,pt %%xcc, 2f\n"
330 " sub %%g5, %%g7, %0\n"
331 "1: mov 1, %0\n"
332 "2:" : "=r" (val)
333 : "0" (&(rw->lock))
334 : "g3", "g5", "g7", "memory");
336 if (val) {
337 put_cpu();
338 return 0;
341 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
342 /* Readers still around, drop the write
343 * lock, return failure.
345 __asm__ __volatile__(
346 " mov 1, %%g3\n"
347 " sllx %%g3, 63, %%g3\n"
348 "1: ldx [%0], %%g5\n"
349 " andn %%g5, %%g3, %%g7\n"
350 " casx [%0], %%g5, %%g7\n"
351 " cmp %%g5, %%g7\n"
352 " bne,pn %%xcc, 1b\n"
353 " membar #StoreLoad | #StoreStore"
354 : /* no outputs */
355 : "r" (&(rw->lock))
356 : "g3", "g5", "g7", "cc", "memory");
358 put_cpu();
360 return 0;
363 /* We have it, say who we are. */
364 rw->writer_pc = ((unsigned int)caller);
365 rw->writer_cpu = cpu;
366 current->thread.smp_lock_count++;
367 current->thread.smp_lock_pc = ((unsigned int)caller);
369 put_cpu();
371 return 1;
374 #endif /* CONFIG_SMP */