qemu-lock.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  17. */
  18. /* Locking primitives. Most of this code should be redundant -
  19. system emulation doesn't need/use locking, NPTL userspace uses
  20. pthread mutexes, and non-NPTL userspace isn't threadsafe anyway.
  21. In either case a spinlock is probably the wrong kind of lock.
  22. Spinlocks are only good if you know annother CPU has the lock and is
  23. likely to release it soon. In environments where you have more threads
  24. than physical CPUs (the extreme case being a single CPU host) a spinlock
  25. simply wastes CPU until the OS decides to preempt it. */
  26. #if defined(USE_NPTL)
  27. #include <pthread.h>
  28. #define spin_lock pthread_mutex_lock
  29. #define spin_unlock pthread_mutex_unlock
  30. #define spinlock_t pthread_mutex_t
  31. #define SPIN_LOCK_UNLOCKED PTHREAD_MUTEX_INITIALIZER
  32. #else
  33. #if defined(__hppa__)
  34. typedef int spinlock_t[4];
  35. #define SPIN_LOCK_UNLOCKED { 1, 1, 1, 1 }
  36. static inline void resetlock (spinlock_t *p)
  37. {
  38. (*p)[0] = (*p)[1] = (*p)[2] = (*p)[3] = 1;
  39. }
  40. #else
  41. typedef int spinlock_t;
  42. #define SPIN_LOCK_UNLOCKED 0
  43. static inline void resetlock (spinlock_t *p)
  44. {
  45. *p = SPIN_LOCK_UNLOCKED;
  46. }
  47. #endif
  48. #if defined(_ARCH_PPC)
  49. static inline int testandset (int *p)
  50. {
  51. int ret;
  52. __asm__ __volatile__ (
  53. " lwarx %0,0,%1\n"
  54. " xor. %0,%3,%0\n"
  55. " bne $+12\n"
  56. " stwcx. %2,0,%1\n"
  57. " bne- $-16\n"
  58. : "=&r" (ret)
  59. : "r" (p), "r" (1), "r" (0)
  60. : "cr0", "memory");
  61. return ret;
  62. }
  63. #elif defined(__i386__)
  64. static inline int testandset (int *p)
  65. {
  66. long int readval = 0;
  67. __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
  68. : "+m" (*p), "+a" (readval)
  69. : "r" (1)
  70. : "cc");
  71. return readval;
  72. }
  73. #elif defined(__x86_64__)
  74. static inline int testandset (int *p)
  75. {
  76. long int readval = 0;
  77. __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
  78. : "+m" (*p), "+a" (readval)
  79. : "r" (1)
  80. : "cc");
  81. return readval;
  82. }
  83. #elif defined(__s390__)
  84. static inline int testandset (int *p)
  85. {
  86. int ret;
  87. __asm__ __volatile__ ("0: cs %0,%1,0(%2)\n"
  88. " jl 0b"
  89. : "=&d" (ret)
  90. : "r" (1), "a" (p), "0" (*p)
  91. : "cc", "memory" );
  92. return ret;
  93. }
  94. #elif defined(__alpha__)
  95. static inline int testandset (int *p)
  96. {
  97. int ret;
  98. unsigned long one;
  99. __asm__ __volatile__ ("0: mov 1,%2\n"
  100. " ldl_l %0,%1\n"
  101. " stl_c %2,%1\n"
  102. " beq %2,1f\n"
  103. ".subsection 2\n"
  104. "1: br 0b\n"
  105. ".previous"
  106. : "=r" (ret), "=m" (*p), "=r" (one)
  107. : "m" (*p));
  108. return ret;
  109. }
  110. #elif defined(__sparc__)
  111. static inline int testandset (int *p)
  112. {
  113. int ret;
  114. __asm__ __volatile__("ldstub [%1], %0"
  115. : "=r" (ret)
  116. : "r" (p)
  117. : "memory");
  118. return (ret ? 1 : 0);
  119. }
  120. #elif defined(__arm__)
  121. static inline int testandset (int *spinlock)
  122. {
  123. register unsigned int ret;
  124. __asm__ __volatile__("swp %0, %1, [%2]"
  125. : "=r"(ret)
  126. : "0"(1), "r"(spinlock));
  127. return ret;
  128. }
  129. #elif defined(__mc68000)
  130. static inline int testandset (int *p)
  131. {
  132. char ret;
  133. __asm__ __volatile__("tas %1; sne %0"
  134. : "=r" (ret)
  135. : "m" (p)
  136. : "cc","memory");
  137. return ret;
  138. }
  139. #elif defined(__hppa__)
  140. /* Because malloc only guarantees 8-byte alignment for malloc'd data,
  141. and GCC only guarantees 8-byte alignment for stack locals, we can't
  142. be assured of 16-byte alignment for atomic lock data even if we
  143. specify "__attribute ((aligned(16)))" in the type declaration. So,
  144. we use a struct containing an array of four ints for the atomic lock
  145. type and dynamically select the 16-byte aligned int from the array
  146. for the semaphore. */
  147. #define __PA_LDCW_ALIGNMENT 16
  148. static inline void *ldcw_align (void *p) {
  149. unsigned long a = (unsigned long)p;
  150. a = (a + __PA_LDCW_ALIGNMENT - 1) & ~(__PA_LDCW_ALIGNMENT - 1);
  151. return (void *)a;
  152. }
  153. static inline int testandset (spinlock_t *p)
  154. {
  155. unsigned int ret;
  156. p = ldcw_align(p);
  157. __asm__ __volatile__("ldcw 0(%1),%0"
  158. : "=r" (ret)
  159. : "r" (p)
  160. : "memory" );
  161. return !ret;
  162. }
  163. #elif defined(__ia64)
  164. #include <ia64intrin.h>
  165. static inline int testandset (int *p)
  166. {
  167. return __sync_lock_test_and_set (p, 1);
  168. }
  169. #elif defined(__mips__)
  170. static inline int testandset (int *p)
  171. {
  172. int ret;
  173. __asm__ __volatile__ (
  174. " .set push \n"
  175. " .set noat \n"
  176. " .set mips2 \n"
  177. "1: li $1, 1 \n"
  178. " ll %0, %1 \n"
  179. " sc $1, %1 \n"
  180. " beqz $1, 1b \n"
  181. " .set pop "
  182. : "=r" (ret), "+R" (*p)
  183. :
  184. : "memory");
  185. return ret;
  186. }
  187. #else
  188. #error unimplemented CPU support
  189. #endif
  190. #if defined(CONFIG_USER_ONLY)
  191. static inline void spin_lock(spinlock_t *lock)
  192. {
  193. while (testandset(lock));
  194. }
  195. static inline void spin_unlock(spinlock_t *lock)
  196. {
  197. resetlock(lock);
  198. }
  199. static inline int spin_trylock(spinlock_t *lock)
  200. {
  201. return !testandset(lock);
  202. }
  203. #else
  204. static inline void spin_lock(spinlock_t *lock)
  205. {
  206. }
  207. static inline void spin_unlock(spinlock_t *lock)
  208. {
  209. }
  210. static inline int spin_trylock(spinlock_t *lock)
  211. {
  212. return 1;
  213. }
  214. #endif
  215. #endif