debuglocks.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* $Id: debuglocks.c,v 1.9 2001/11/17 00:10:48 davem Exp $
  2. * debuglocks.c: Debugging versions of SMP locking primitives.
  3. *
  4. * Copyright (C) 1998 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/config.h>
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/spinlock.h>
  10. #include <asm/system.h>
  11. #ifdef CONFIG_SMP
  12. static inline void show (char *str, spinlock_t *lock, unsigned long caller)
  13. {
  14. int cpu = smp_processor_id();
  15. printk("%s(%p) CPU#%d stuck at %08x, owner PC(%08x):CPU(%x)\n",
  16. str, lock, cpu, (unsigned int) caller,
  17. lock->owner_pc, lock->owner_cpu);
  18. }
  19. static inline void show_read (char *str, rwlock_t *lock, unsigned long caller)
  20. {
  21. int cpu = smp_processor_id();
  22. printk("%s(%p) CPU#%d stuck at %08x, writer PC(%08x):CPU(%x)\n",
  23. str, lock, cpu, (unsigned int) caller,
  24. lock->writer_pc, lock->writer_cpu);
  25. }
  26. static inline void show_write (char *str, rwlock_t *lock, unsigned long caller)
  27. {
  28. int cpu = smp_processor_id();
  29. int i;
  30. printk("%s(%p) CPU#%d stuck at %08x\n",
  31. str, lock, cpu, (unsigned int) caller);
  32. printk("Writer: PC(%08x):CPU(%x)\n",
  33. lock->writer_pc, lock->writer_cpu);
  34. printk("Readers:");
  35. for (i = 0; i < NR_CPUS; i++)
  36. if (lock->reader_pc[i])
  37. printk(" %d[%08x]", i, lock->reader_pc[i]);
  38. printk("\n");
  39. }
  40. #undef INIT_STUCK
  41. #define INIT_STUCK 100000000
  42. void _do_spin_lock(spinlock_t *lock, char *str, unsigned long caller)
  43. {
  44. unsigned long val;
  45. int stuck = INIT_STUCK;
  46. int cpu = get_cpu();
  47. int shown = 0;
  48. again:
  49. __asm__ __volatile__("ldstub [%1], %0"
  50. : "=r" (val)
  51. : "r" (&(lock->lock))
  52. : "memory");
  53. membar_storeload_storestore();
  54. if (val) {
  55. while (lock->lock) {
  56. if (!--stuck) {
  57. if (shown++ <= 2)
  58. show(str, lock, caller);
  59. stuck = INIT_STUCK;
  60. }
  61. rmb();
  62. }
  63. goto again;
  64. }
  65. lock->owner_pc = ((unsigned int)caller);
  66. lock->owner_cpu = cpu;
  67. current->thread.smp_lock_count++;
  68. current->thread.smp_lock_pc = ((unsigned int)caller);
  69. put_cpu();
  70. }
  71. int _do_spin_trylock(spinlock_t *lock, unsigned long caller)
  72. {
  73. unsigned long val;
  74. int cpu = get_cpu();
  75. __asm__ __volatile__("ldstub [%1], %0"
  76. : "=r" (val)
  77. : "r" (&(lock->lock))
  78. : "memory");
  79. membar_storeload_storestore();
  80. if (!val) {
  81. lock->owner_pc = ((unsigned int)caller);
  82. lock->owner_cpu = cpu;
  83. current->thread.smp_lock_count++;
  84. current->thread.smp_lock_pc = ((unsigned int)caller);
  85. }
  86. put_cpu();
  87. return val == 0;
  88. }
  89. void _do_spin_unlock(spinlock_t *lock)
  90. {
  91. lock->owner_pc = 0;
  92. lock->owner_cpu = NO_PROC_ID;
  93. membar_storestore_loadstore();
  94. lock->lock = 0;
  95. current->thread.smp_lock_count--;
  96. }
  97. /* Keep INIT_STUCK the same... */
  98. void _do_read_lock(rwlock_t *rw, char *str, unsigned long caller)
  99. {
  100. unsigned long val;
  101. int stuck = INIT_STUCK;
  102. int cpu = get_cpu();
  103. int shown = 0;
  104. wlock_again:
  105. /* Wait for any writer to go away. */
  106. while (((long)(rw->lock)) < 0) {
  107. if (!--stuck) {
  108. if (shown++ <= 2)
  109. show_read(str, rw, caller);
  110. stuck = INIT_STUCK;
  111. }
  112. rmb();
  113. }
  114. /* Try once to increment the counter. */
  115. __asm__ __volatile__(
  116. " ldx [%0], %%g1\n"
  117. " brlz,a,pn %%g1, 2f\n"
  118. " mov 1, %0\n"
  119. " add %%g1, 1, %%g7\n"
  120. " casx [%0], %%g1, %%g7\n"
  121. " sub %%g1, %%g7, %0\n"
  122. "2:" : "=r" (val)
  123. : "0" (&(rw->lock))
  124. : "g1", "g7", "memory");
  125. membar_storeload_storestore();
  126. if (val)
  127. goto wlock_again;
  128. rw->reader_pc[cpu] = ((unsigned int)caller);
  129. current->thread.smp_lock_count++;
  130. current->thread.smp_lock_pc = ((unsigned int)caller);
  131. put_cpu();
  132. }
  133. void _do_read_unlock(rwlock_t *rw, char *str, unsigned long caller)
  134. {
  135. unsigned long val;
  136. int stuck = INIT_STUCK;
  137. int cpu = get_cpu();
  138. int shown = 0;
  139. /* Drop our identity _first_. */
  140. rw->reader_pc[cpu] = 0;
  141. current->thread.smp_lock_count--;
  142. runlock_again:
  143. /* Spin trying to decrement the counter using casx. */
  144. __asm__ __volatile__(
  145. " membar #StoreLoad | #LoadLoad\n"
  146. " ldx [%0], %%g1\n"
  147. " sub %%g1, 1, %%g7\n"
  148. " casx [%0], %%g1, %%g7\n"
  149. " membar #StoreLoad | #StoreStore\n"
  150. " sub %%g1, %%g7, %0\n"
  151. : "=r" (val)
  152. : "0" (&(rw->lock))
  153. : "g1", "g7", "memory");
  154. if (val) {
  155. if (!--stuck) {
  156. if (shown++ <= 2)
  157. show_read(str, rw, caller);
  158. stuck = INIT_STUCK;
  159. }
  160. goto runlock_again;
  161. }
  162. put_cpu();
  163. }
  164. void _do_write_lock(rwlock_t *rw, char *str, unsigned long caller)
  165. {
  166. unsigned long val;
  167. int stuck = INIT_STUCK;
  168. int cpu = get_cpu();
  169. int shown = 0;
  170. wlock_again:
  171. /* Spin while there is another writer. */
  172. while (((long)rw->lock) < 0) {
  173. if (!--stuck) {
  174. if (shown++ <= 2)
  175. show_write(str, rw, caller);
  176. stuck = INIT_STUCK;
  177. }
  178. rmb();
  179. }
  180. /* Try to acuire the write bit. */
  181. __asm__ __volatile__(
  182. " mov 1, %%g3\n"
  183. " sllx %%g3, 63, %%g3\n"
  184. " ldx [%0], %%g1\n"
  185. " brlz,pn %%g1, 1f\n"
  186. " or %%g1, %%g3, %%g7\n"
  187. " casx [%0], %%g1, %%g7\n"
  188. " membar #StoreLoad | #StoreStore\n"
  189. " ba,pt %%xcc, 2f\n"
  190. " sub %%g1, %%g7, %0\n"
  191. "1: mov 1, %0\n"
  192. "2:" : "=r" (val)
  193. : "0" (&(rw->lock))
  194. : "g3", "g1", "g7", "memory");
  195. if (val) {
  196. /* We couldn't get the write bit. */
  197. if (!--stuck) {
  198. if (shown++ <= 2)
  199. show_write(str, rw, caller);
  200. stuck = INIT_STUCK;
  201. }
  202. goto wlock_again;
  203. }
  204. if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
  205. /* Readers still around, drop the write
  206. * lock, spin, and try again.
  207. */
  208. if (!--stuck) {
  209. if (shown++ <= 2)
  210. show_write(str, rw, caller);
  211. stuck = INIT_STUCK;
  212. }
  213. __asm__ __volatile__(
  214. " mov 1, %%g3\n"
  215. " sllx %%g3, 63, %%g3\n"
  216. "1: ldx [%0], %%g1\n"
  217. " andn %%g1, %%g3, %%g7\n"
  218. " casx [%0], %%g1, %%g7\n"
  219. " cmp %%g1, %%g7\n"
  220. " membar #StoreLoad | #StoreStore\n"
  221. " bne,pn %%xcc, 1b\n"
  222. " nop"
  223. : /* no outputs */
  224. : "r" (&(rw->lock))
  225. : "g3", "g1", "g7", "cc", "memory");
  226. while(rw->lock != 0) {
  227. if (!--stuck) {
  228. if (shown++ <= 2)
  229. show_write(str, rw, caller);
  230. stuck = INIT_STUCK;
  231. }
  232. rmb();
  233. }
  234. goto wlock_again;
  235. }
  236. /* We have it, say who we are. */
  237. rw->writer_pc = ((unsigned int)caller);
  238. rw->writer_cpu = cpu;
  239. current->thread.smp_lock_count++;
  240. current->thread.smp_lock_pc = ((unsigned int)caller);
  241. put_cpu();
  242. }
  243. void _do_write_unlock(rwlock_t *rw, unsigned long caller)
  244. {
  245. unsigned long val;
  246. int stuck = INIT_STUCK;
  247. int shown = 0;
  248. /* Drop our identity _first_ */
  249. rw->writer_pc = 0;
  250. rw->writer_cpu = NO_PROC_ID;
  251. current->thread.smp_lock_count--;
  252. wlock_again:
  253. __asm__ __volatile__(
  254. " membar #StoreLoad | #LoadLoad\n"
  255. " mov 1, %%g3\n"
  256. " sllx %%g3, 63, %%g3\n"
  257. " ldx [%0], %%g1\n"
  258. " andn %%g1, %%g3, %%g7\n"
  259. " casx [%0], %%g1, %%g7\n"
  260. " membar #StoreLoad | #StoreStore\n"
  261. " sub %%g1, %%g7, %0\n"
  262. : "=r" (val)
  263. : "0" (&(rw->lock))
  264. : "g3", "g1", "g7", "memory");
  265. if (val) {
  266. if (!--stuck) {
  267. if (shown++ <= 2)
  268. show_write("write_unlock", rw, caller);
  269. stuck = INIT_STUCK;
  270. }
  271. goto wlock_again;
  272. }
  273. }
  274. int _do_write_trylock(rwlock_t *rw, char *str, unsigned long caller)
  275. {
  276. unsigned long val;
  277. int cpu = get_cpu();
  278. /* Try to acuire the write bit. */
  279. __asm__ __volatile__(
  280. " mov 1, %%g3\n"
  281. " sllx %%g3, 63, %%g3\n"
  282. " ldx [%0], %%g1\n"
  283. " brlz,pn %%g1, 1f\n"
  284. " or %%g1, %%g3, %%g7\n"
  285. " casx [%0], %%g1, %%g7\n"
  286. " membar #StoreLoad | #StoreStore\n"
  287. " ba,pt %%xcc, 2f\n"
  288. " sub %%g1, %%g7, %0\n"
  289. "1: mov 1, %0\n"
  290. "2:" : "=r" (val)
  291. : "0" (&(rw->lock))
  292. : "g3", "g1", "g7", "memory");
  293. if (val) {
  294. put_cpu();
  295. return 0;
  296. }
  297. if ((rw->lock & ((1UL<<63)-1UL)) != 0UL) {
  298. /* Readers still around, drop the write
  299. * lock, return failure.
  300. */
  301. __asm__ __volatile__(
  302. " mov 1, %%g3\n"
  303. " sllx %%g3, 63, %%g3\n"
  304. "1: ldx [%0], %%g1\n"
  305. " andn %%g1, %%g3, %%g7\n"
  306. " casx [%0], %%g1, %%g7\n"
  307. " cmp %%g1, %%g7\n"
  308. " membar #StoreLoad | #StoreStore\n"
  309. " bne,pn %%xcc, 1b\n"
  310. " nop"
  311. : /* no outputs */
  312. : "r" (&(rw->lock))
  313. : "g3", "g1", "g7", "cc", "memory");
  314. put_cpu();
  315. return 0;
  316. }
  317. /* We have it, say who we are. */
  318. rw->writer_pc = ((unsigned int)caller);
  319. rw->writer_cpu = cpu;
  320. current->thread.smp_lock_count++;
  321. current->thread.smp_lock_pc = ((unsigned int)caller);
  322. put_cpu();
  323. return 1;
  324. }
  325. #endif /* CONFIG_SMP */