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)
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>
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();
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);
45 for (i = 0; i < NR_CPUS; i++)
46 if (lock->reader_pc[i])
47 printk(" %d[%08x]", i, lock->reader_pc[i]);
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;
63 __asm__ __volatile__("ldstub [%1], %0"
67 membar("#StoreLoad | #StoreStore");
72 show(str, lock, caller);
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);
87 int _do_spin_trylock(spinlock_t *lock)
89 unsigned long val, caller;
93 __asm__ __volatile__("ldstub [%1], %0"
97 membar("#StoreLoad | #StoreStore");
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);
110 void _do_spin_unlock(spinlock_t *lock)
113 lock->owner_cpu = NO_PROC_ID;
114 membar("#StoreStore | #LoadStore");
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;
130 /* Wait for any writer to go away. */
131 while (((long)(rw->lock)) < 0) {
134 show_read(str, rw, caller);
139 /* Try once to increment the counter. */
140 __asm__ __volatile__(
142 " brlz,a,pn %%g1, 2f\n"
144 " add %%g1, 1, %%g7\n"
145 " casx [%0], %%g1, %%g7\n"
146 " sub %%g1, %%g7, %0\n"
149 : "g1", "g7", "memory");
150 membar("#StoreLoad | #StoreStore");
153 rw->reader_pc[cpu] = ((unsigned int)caller);
154 current->thread.smp_lock_count++;
155 current->thread.smp_lock_pc = ((unsigned int)caller);
160 void _do_read_unlock (rwlock_t *rw, char *str)
162 unsigned long caller, val;
163 int stuck = INIT_STUCK;
169 /* Drop our identity _first_. */
170 rw->reader_pc[cpu] = 0;
171 current->thread.smp_lock_count--;
173 /* Spin trying to decrement the counter using casx. */
174 __asm__ __volatile__(
175 " membar #StoreLoad | #LoadLoad\n"
177 " sub %%g1, 1, %%g7\n"
178 " casx [%0], %%g1, %%g7\n"
179 " membar #StoreLoad | #StoreStore\n"
180 " sub %%g1, %%g7, %0\n"
183 : "g1", "g7", "memory");
187 show_read(str, rw, caller);
196 void _do_write_lock (rwlock_t *rw, char *str)
198 unsigned long caller, val;
199 int stuck = INIT_STUCK;
205 /* Spin while there is another writer. */
206 while (((long)rw->lock) < 0) {
209 show_write(str, rw, caller);
215 /* Try to acuire the write bit. */
216 __asm__ __volatile__(
218 " sllx %%g3, 63, %%g3\n"
220 " brlz,pn %%g1, 1f\n"
221 " or %%g1, %%g3, %%g7\n"
222 " casx [%0], %%g1, %%g7\n"
223 " membar #StoreLoad | #StoreStore\n"
225 " sub %%g1, %%g7, %0\n"
229 : "g3", "g1", "g7", "memory");
231 /* We couldn't get the write bit. */
234 show_write(str, rw, caller);
239 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
240 /* Readers still around, drop the write
241 * lock, spin, and try again.
245 show_write(str, rw, caller);
248 __asm__ __volatile__(
250 " sllx %%g3, 63, %%g3\n"
251 "1: ldx [%0], %%g1\n"
252 " andn %%g1, %%g3, %%g7\n"
253 " casx [%0], %%g1, %%g7\n"
255 " membar #StoreLoad | #StoreStore\n"
256 " bne,pn %%xcc, 1b\n"
260 : "g3", "g1", "g7", "cc", "memory");
261 while(rw->lock != 0) {
264 show_write(str, rw, caller);
272 /* We have it, say who we are. */
273 rw->writer_pc = ((unsigned int)caller);
274 rw->writer_cpu = cpu;
275 current->thread.smp_lock_count++;
276 current->thread.smp_lock_pc = ((unsigned int)caller);
281 void _do_write_unlock(rwlock_t *rw)
283 unsigned long caller, val;
284 int stuck = INIT_STUCK;
289 /* Drop our identity _first_ */
291 rw->writer_cpu = NO_PROC_ID;
292 current->thread.smp_lock_count--;
294 __asm__ __volatile__(
295 " membar #StoreLoad | #LoadLoad\n"
297 " sllx %%g3, 63, %%g3\n"
299 " andn %%g1, %%g3, %%g7\n"
300 " casx [%0], %%g1, %%g7\n"
301 " membar #StoreLoad | #StoreStore\n"
302 " sub %%g1, %%g7, %0\n"
305 : "g3", "g1", "g7", "memory");
309 show_write("write_unlock", rw, caller);
316 int _do_write_trylock (rwlock_t *rw, char *str)
318 unsigned long caller, val;
323 /* Try to acuire the write bit. */
324 __asm__ __volatile__(
326 " sllx %%g3, 63, %%g3\n"
328 " brlz,pn %%g1, 1f\n"
329 " or %%g1, %%g3, %%g7\n"
330 " casx [%0], %%g1, %%g7\n"
331 " membar #StoreLoad | #StoreStore\n"
333 " sub %%g1, %%g7, %0\n"
337 : "g3", "g1", "g7", "memory");
344 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
345 /* Readers still around, drop the write
346 * lock, return failure.
348 __asm__ __volatile__(
350 " sllx %%g3, 63, %%g3\n"
351 "1: ldx [%0], %%g1\n"
352 " andn %%g1, %%g3, %%g7\n"
353 " casx [%0], %%g1, %%g7\n"
355 " membar #StoreLoad | #StoreStore\n"
356 " bne,pn %%xcc, 1b\n"
360 : "g3", "g1", "g7", "cc", "memory");
367 /* We have it, say who we are. */
368 rw->writer_pc = ((unsigned int)caller);
369 rw->writer_cpu = cpu;
370 current->thread.smp_lock_count++;
371 current->thread.smp_lock_pc = ((unsigned int)caller);
378 #endif /* CONFIG_SMP */