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;
58 int cpu = smp_processor_id();
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);
85 int _spin_trylock(spinlock_t *lock)
87 unsigned long val, caller;
88 int cpu = smp_processor_id();
91 __asm__ __volatile__("ldstub [%1], %0"
95 membar("#StoreLoad | #StoreStore");
97 lock->owner_pc = ((unsigned int)caller);
98 lock->owner_cpu = cpu;
99 current->thread.smp_lock_count++;
100 current->thread.smp_lock_pc = ((unsigned int)caller);
105 void _do_spin_unlock(spinlock_t *lock)
108 lock->owner_cpu = NO_PROC_ID;
109 membar("#StoreStore | #LoadStore");
111 current->thread.smp_lock_count--;
114 /* Keep INIT_STUCK the same... */
116 void _do_read_lock (rwlock_t *rw, char *str)
118 unsigned long caller, val;
119 int stuck = INIT_STUCK;
120 int cpu = smp_processor_id();
125 /* Wait for any writer to go away. */
126 while (((long)(rw->lock)) < 0) {
129 show_read(str, rw, caller);
134 /* Try once to increment the counter. */
135 __asm__ __volatile__(
137 " brlz,a,pn %%g5, 2f\n"
139 " add %%g5, 1, %%g7\n"
140 " casx [%0], %%g5, %%g7\n"
141 " sub %%g5, %%g7, %0\n"
144 : "g5", "g7", "memory");
145 membar("#StoreLoad | #StoreStore");
148 rw->reader_pc[cpu] = ((unsigned int)caller);
149 current->thread.smp_lock_count++;
150 current->thread.smp_lock_pc = ((unsigned int)caller);
153 void _do_read_unlock (rwlock_t *rw, char *str)
155 unsigned long caller, val;
156 int stuck = INIT_STUCK;
157 int cpu = smp_processor_id();
162 /* Drop our identity _first_. */
163 rw->reader_pc[cpu] = 0;
164 current->thread.smp_lock_count--;
166 /* Spin trying to decrement the counter using casx. */
167 __asm__ __volatile__(
169 " sub %%g5, 1, %%g7\n"
170 " casx [%0], %%g5, %%g7\n"
171 " membar #StoreLoad | #StoreStore\n"
172 " sub %%g5, %%g7, %0\n"
175 : "g5", "g7", "memory");
179 show_read(str, rw, caller);
186 void _do_write_lock (rwlock_t *rw, char *str)
188 unsigned long caller, val;
189 int stuck = INIT_STUCK;
190 int cpu = smp_processor_id();
195 /* Spin while there is another writer. */
196 while (((long)rw->lock) < 0) {
199 show_write(str, rw, caller);
205 /* Try to acuire the write bit. */
206 __asm__ __volatile__(
208 " sllx %%g3, 63, %%g3\n"
210 " brlz,pn %%g5, 1f\n"
211 " or %%g5, %%g3, %%g7\n"
212 " casx [%0], %%g5, %%g7\n"
213 " membar #StoreLoad | #StoreStore\n"
215 " sub %%g5, %%g7, %0\n"
219 : "g3", "g5", "g7", "memory");
221 /* We couldn't get the write bit. */
224 show_write(str, rw, caller);
229 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
230 /* Readers still around, drop the write
231 * lock, spin, and try again.
235 show_write(str, rw, caller);
238 __asm__ __volatile__(
240 " sllx %%g3, 63, %%g3\n"
241 "1: ldx [%0], %%g5\n"
242 " andn %%g5, %%g3, %%g7\n"
243 " casx [%0], %%g5, %%g7\n"
245 " bne,pn %%xcc, 1b\n"
246 " membar #StoreLoad | #StoreStore"
249 : "g3", "g5", "g7", "cc", "memory");
250 while(rw->lock != 0) {
253 show_write(str, rw, caller);
261 /* We have it, say who we are. */
262 rw->writer_pc = ((unsigned int)caller);
263 rw->writer_cpu = cpu;
264 current->thread.smp_lock_count++;
265 current->thread.smp_lock_pc = ((unsigned int)caller);
268 void _do_write_unlock(rwlock_t *rw)
270 unsigned long caller, val;
271 int stuck = INIT_STUCK;
276 /* Drop our identity _first_ */
278 rw->writer_cpu = NO_PROC_ID;
279 current->thread.smp_lock_count--;
281 __asm__ __volatile__(
283 " sllx %%g3, 63, %%g3\n"
285 " andn %%g5, %%g3, %%g7\n"
286 " casx [%0], %%g5, %%g7\n"
287 " membar #StoreLoad | #StoreStore\n"
288 " sub %%g5, %%g7, %0\n"
291 : "g3", "g5", "g7", "memory");
295 show_write("write_unlock", rw, caller);
302 int _do_write_trylock (rwlock_t *rw, char *str)
304 unsigned long caller, val;
305 int cpu = smp_processor_id();
309 /* Try to acuire the write bit. */
310 __asm__ __volatile__(
312 " sllx %%g3, 63, %%g3\n"
314 " brlz,pn %%g5, 1f\n"
315 " or %%g5, %%g3, %%g7\n"
316 " casx [%0], %%g5, %%g7\n"
317 " membar #StoreLoad | #StoreStore\n"
319 " sub %%g5, %%g7, %0\n"
323 : "g3", "g5", "g7", "memory");
328 if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
329 /* Readers still around, drop the write
330 * lock, return failure.
332 __asm__ __volatile__(
334 " sllx %%g3, 63, %%g3\n"
335 "1: ldx [%0], %%g5\n"
336 " andn %%g5, %%g3, %%g7\n"
337 " casx [%0], %%g5, %%g7\n"
339 " bne,pn %%xcc, 1b\n"
340 " membar #StoreLoad | #StoreStore"
343 : "g3", "g5", "g7", "cc", "memory");
348 /* We have it, say who we are. */
349 rw->writer_pc = ((unsigned int)caller);
350 rw->writer_cpu = cpu;
351 current->thread.smp_lock_count++;
352 current->thread.smp_lock_pc = ((unsigned int)caller);
357 #endif /* CONFIG_SMP */