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>
13 #if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
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();
39 printk("%s(%p) CPU#%d stuck at %08x\n",
40 str, lock, cpu, (unsigned int) caller);
41 printk("Writer: PC(%08x):CPU(%x)\n",
42 lock->writer_pc, lock->writer_cpu);
43 printk("Readers: 0[%08x] 1[%08x] 2[%08x] 4[%08x]\n",
44 lock->reader_pc[0], lock->reader_pc[1],
45 lock->reader_pc[2], lock->reader_pc[3]);
49 #define INIT_STUCK 100000000
51 void _do_spin_lock(spinlock_t *lock, char *str)
53 unsigned long caller, val;
54 int stuck = INIT_STUCK;
55 int cpu = smp_processor_id();
60 __asm__ __volatile__("ldstub [%1], %0"
64 membar("#StoreLoad | #StoreStore");
69 show(str, lock, caller);
76 lock->owner_pc = ((unsigned int)caller);
77 lock->owner_cpu = cpu;
78 current->thread.smp_lock_count++;
79 current->thread.smp_lock_pc = ((unsigned int)caller);
82 int _spin_trylock(spinlock_t *lock)
84 unsigned long val, caller;
85 int cpu = smp_processor_id();
88 __asm__ __volatile__("ldstub [%1], %0"
92 membar("#StoreLoad | #StoreStore");
94 lock->owner_pc = ((unsigned int)caller);
95 lock->owner_cpu = cpu;
96 current->thread.smp_lock_count++;
97 current->thread.smp_lock_pc = ((unsigned int)caller);
102 void _do_spin_unlock(spinlock_t *lock)
105 lock->owner_cpu = NO_PROC_ID;
106 membar("#StoreStore | #LoadStore");
108 current->thread.smp_lock_count--;
111 /* Keep INIT_STUCK the same... */
113 void _do_read_lock (rwlock_t *rw, char *str)
115 unsigned long caller, val;
116 int stuck = INIT_STUCK;
117 int cpu = smp_processor_id();
122 /* Wait for any writer to go away. */
123 while (((long)(rw->lock)) < 0) {
126 show_read(str, rw, caller);
131 /* Try once to increment the counter. */
132 __asm__ __volatile__(
134 " brlz,a,pn %%g5, 2f\n"
136 " add %%g5, 1, %%g7\n"
137 " casx [%0], %%g5, %%g7\n"
138 " sub %%g5, %%g7, %0\n"
141 : "g5", "g7", "memory");
142 membar("#StoreLoad | #StoreStore");
145 rw->reader_pc[cpu] = ((unsigned int)caller);
146 current->thread.smp_lock_count++;
147 current->thread.smp_lock_pc = ((unsigned int)caller);
150 void _do_read_unlock (rwlock_t *rw, char *str)
152 unsigned long caller, val;
153 int stuck = INIT_STUCK;
154 int cpu = smp_processor_id();
159 /* Drop our identity _first_. */
160 rw->reader_pc[cpu] = 0;
161 current->thread.smp_lock_count--;
163 /* Spin trying to decrement the counter using casx. */
164 __asm__ __volatile__(
166 " sub %%g5, 1, %%g7\n"
167 " casx [%0], %%g5, %%g7\n"
168 " membar #StoreLoad | #StoreStore\n"
169 " sub %%g5, %%g7, %0\n"
172 : "g5", "g7", "memory");
176 show_read(str, rw, caller);
183 void _do_write_lock (rwlock_t *rw, char *str)
185 unsigned long caller, val;
186 int stuck = INIT_STUCK;
187 int cpu = smp_processor_id();
192 /* Spin while there is another writer. */
193 while (((long)rw->lock) < 0) {
196 show_write(str, rw, caller);
202 /* Try to acuire the write bit. */
203 __asm__ __volatile__(
205 " sllx %%g3, 63, %%g3\n"
207 " brlz,pn %%g5, 1f\n"
208 " or %%g5, %%g3, %%g7\n"
209 " casx [%0], %%g5, %%g7\n"
210 " membar #StoreLoad | #StoreStore\n"
212 " sub %%g5, %%g7, %0\n"
216 : "g3", "g5", "g7", "memory");
218 /* We couldn't get the write bit. */
221 show_write(str, rw, caller);
226 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
227 /* Readers still around, drop the write
228 * lock, spin, and try again.
232 show_write(str, rw, caller);
235 __asm__ __volatile__(
237 " sllx %%g3, 63, %%g3\n"
238 "1: ldx [%0], %%g5\n"
239 " andn %%g5, %%g3, %%g7\n"
240 " casx [%0], %%g5, %%g7\n"
242 " bne,pn %%xcc, 1b\n"
243 " membar #StoreLoad | #StoreStore"
246 : "g3", "g5", "g7", "cc", "memory");
247 while(rw->lock != 0) {
250 show_write(str, rw, caller);
258 /* We have it, say who we are. */
259 rw->writer_pc = ((unsigned int)caller);
260 rw->writer_cpu = cpu;
261 current->thread.smp_lock_count++;
262 current->thread.smp_lock_pc = ((unsigned int)caller);
265 void _do_write_unlock(rwlock_t *rw)
267 unsigned long caller, val;
268 int stuck = INIT_STUCK;
273 /* Drop our identity _first_ */
275 rw->writer_cpu = NO_PROC_ID;
276 current->thread.smp_lock_count--;
278 __asm__ __volatile__(
280 " sllx %%g3, 63, %%g3\n"
282 " andn %%g5, %%g3, %%g7\n"
283 " casx [%0], %%g5, %%g7\n"
284 " membar #StoreLoad | #StoreStore\n"
285 " sub %%g5, %%g7, %0\n"
288 : "g3", "g5", "g7", "memory");
292 show_write("write_unlock", rw, caller);
299 #endif /* CONFIG_SMP && CONFIG_DEBUG_SPINLOCK */