1 /* $Id: debuglocks.c,v 1.5 2000/05/09 17:40:14 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 /* To enable this code, just define SPIN_LOCK_DEBUG in asm/spinlock.h */
16 #ifdef SPIN_LOCK_DEBUG
18 #define GET_CALLER(PC) __asm__ __volatile__("mov %%i7, %0" : "=r" (PC))
20 static inline void show (char *str, spinlock_t *lock, unsigned long caller)
22 int cpu = smp_processor_id();
24 printk("%s(%p) CPU#%d stuck at %08x, owner PC(%08x):CPU(%x)\n",
25 str, lock, cpu, (unsigned int) caller,
26 lock->owner_pc, lock->owner_cpu);
29 static inline void show_read (char *str, rwlock_t *lock, unsigned long caller)
31 int cpu = smp_processor_id();
33 printk("%s(%p) CPU#%d stuck at %08x, writer PC(%08x):CPU(%x)\n",
34 str, lock, cpu, (unsigned int) caller,
35 lock->writer_pc, lock->writer_cpu);
38 static inline void show_write (char *str, rwlock_t *lock, unsigned long caller)
40 int cpu = smp_processor_id();
42 printk("%s(%p) CPU#%d stuck at %08x\n",
43 str, lock, cpu, (unsigned int) caller);
44 printk("Writer: PC(%08x):CPU(%x)\n",
45 lock->writer_pc, lock->writer_cpu);
46 printk("Readers: 0[%08x] 1[%08x] 2[%08x] 4[%08x]\n",
47 lock->reader_pc[0], lock->reader_pc[1],
48 lock->reader_pc[2], lock->reader_pc[3]);
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 = smp_processor_id();
62 __asm__ __volatile__("ldstub [%1], %0"
66 membar("#StoreLoad | #StoreStore");
70 show(str, lock, caller);
77 lock->owner_pc = ((unsigned int)caller);
78 lock->owner_cpu = cpu;
81 int _spin_trylock(spinlock_t *lock)
83 unsigned long val, caller;
84 int cpu = smp_processor_id();
87 __asm__ __volatile__("ldstub [%1], %0"
91 membar("#StoreLoad | #StoreStore");
93 lock->owner_pc = ((unsigned int)caller);
94 lock->owner_cpu = cpu;
99 void _do_spin_unlock(spinlock_t *lock)
102 lock->owner_cpu = NO_PROC_ID;
103 membar("#StoreStore | #LoadStore");
107 /* Keep INIT_STUCK the same... */
109 void _do_read_lock (rwlock_t *rw, char *str)
111 unsigned long caller, val;
112 int stuck = INIT_STUCK;
113 int cpu = smp_processor_id();
117 /* Wait for any writer to go away. */
118 while (((long)(rw->lock)) < 0) {
120 show_read(str, rw, caller);
125 /* Try once to increment the counter. */
126 __asm__ __volatile__("
131 casx [%0], %%g5, %%g7
135 : "g5", "g7", "memory");
136 membar("#StoreLoad | #StoreStore");
139 rw->reader_pc[cpu] = ((unsigned int)caller);
142 void _do_read_unlock (rwlock_t *rw, char *str)
144 unsigned long caller, val;
145 int stuck = INIT_STUCK;
146 int cpu = smp_processor_id();
150 /* Drop our identity _first_. */
151 rw->reader_pc[cpu] = 0;
153 /* Spin trying to decrement the counter using casx. */
154 __asm__ __volatile__("
157 casx [%0], %%g5, %%g7
158 membar #StoreLoad | #StoreStore
162 : "g5", "g7", "memory");
165 show_read(str, rw, caller);
172 void _do_write_lock (rwlock_t *rw, char *str)
174 unsigned long caller, val;
175 int stuck = INIT_STUCK;
176 int cpu = smp_processor_id();
180 /* Spin while there is another writer. */
181 while (((long)rw->lock) < 0) {
183 show_write(str, rw, caller);
189 /* Try to acuire the write bit. */
190 __asm__ __volatile__("
196 casx [%0], %%g5, %%g7
197 membar #StoreLoad | #StoreStore
203 : "g3", "g5", "g7", "memory");
205 /* We couldn't get the write bit. */
207 show_write(str, rw, caller);
212 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
213 /* Readers still around, drop the write
214 * lock, spin, and try again.
217 show_write(str, rw, caller);
220 __asm__ __volatile__("
224 andn %%g5, %%g3, %%g7
225 casx [%0], %%g5, %%g7
228 membar #StoreLoad | #StoreStore"
231 : "g3", "g5", "g7", "cc", "memory");
232 while(rw->lock != 0) {
234 show_write(str, rw, caller);
242 /* We have it, say who we are. */
243 rw->writer_pc = ((unsigned int)caller);
244 rw->writer_cpu = cpu;
247 void _do_write_unlock(rwlock_t *rw)
249 unsigned long caller, val;
250 int stuck = INIT_STUCK;
254 /* Drop our identity _first_ */
256 rw->writer_cpu = NO_PROC_ID;
258 __asm__ __volatile__("
262 andn %%g5, %%g3, %%g7
263 casx [%0], %%g5, %%g7
264 membar #StoreLoad | #StoreStore
268 : "g3", "g5", "g7", "memory");
271 show_write("write_unlock", rw, caller);
278 #endif /* SPIN_LOCK_DEBUG */
279 #endif /* CONFIG_SMP */