atomic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Atomic operations used inside libc. Linux/SH version.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C 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.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <stdint.h>
  17. typedef int8_t atomic8_t;
  18. typedef uint8_t uatomic8_t;
  19. typedef int_fast8_t atomic_fast8_t;
  20. typedef uint_fast8_t uatomic_fast8_t;
  21. typedef int16_t atomic16_t;
  22. typedef uint16_t uatomic16_t;
  23. typedef int_fast16_t atomic_fast16_t;
  24. typedef uint_fast16_t uatomic_fast16_t;
  25. typedef int32_t atomic32_t;
  26. typedef uint32_t uatomic32_t;
  27. typedef int_fast32_t atomic_fast32_t;
  28. typedef uint_fast32_t uatomic_fast32_t;
  29. typedef int64_t atomic64_t;
  30. typedef uint64_t uatomic64_t;
  31. typedef int_fast64_t atomic_fast64_t;
  32. typedef uint_fast64_t uatomic_fast64_t;
  33. typedef intptr_t atomicptr_t;
  34. typedef uintptr_t uatomicptr_t;
  35. typedef intmax_t atomic_max_t;
  36. typedef uintmax_t uatomic_max_t;
  37. /* SH kernel has implemented a gUSA ("g" User Space Atomicity) support
  38. for the user space atomicity. The atomicity macros use this scheme.
  39. Reference:
  40. Niibe Yutaka, "gUSA: Simple and Efficient User Space Atomicity
  41. Emulation with Little Kernel Modification", Linux Conference 2002,
  42. Japan. http://lc.linux.or.jp/lc2002/papers/niibe0919h.pdf (in
  43. Japanese).
  44. B.N. Bershad, D. Redell, and J. Ellis, "Fast Mutual Exclusion for
  45. Uniprocessors", Proceedings of the Fifth Architectural Support for
  46. Programming Languages and Operating Systems (ASPLOS), pp. 223-233,
  47. October 1992. http://www.cs.washington.edu/homes/bershad/Papers/Rcs.ps
  48. SuperH ABI:
  49. r15: -(size of atomic instruction sequence) < 0
  50. r0: end point
  51. r1: saved stack pointer
  52. */
  53. #define __arch_compare_and_exchange_val_8_acq(mem, newval, oldval) \
  54. ({ __typeof (*(mem)) __result; \
  55. __asm __volatile ("\
  56. .align 2\n\
  57. mova 1f,r0\n\
  58. nop\n\
  59. mov r15,r1\n\
  60. mov #-8,r15\n\
  61. 0: mov.b @%1,%0\n\
  62. cmp/eq %0,%3\n\
  63. bf 1f\n\
  64. mov.b %2,@%1\n\
  65. 1: mov r1,r15"\
  66. : "=&r" (__result) : "r" (mem), "r" (newval), "r" (oldval) \
  67. : "r0", "r1", "t", "memory"); \
  68. __result; })
  69. #define __arch_compare_and_exchange_val_16_acq(mem, newval, oldval) \
  70. ({ __typeof (*(mem)) __result; \
  71. __asm __volatile ("\
  72. .align 2\n\
  73. mova 1f,r0\n\
  74. nop\n\
  75. mov r15,r1\n\
  76. mov #-8,r15\n\
  77. 0: mov.w @%1,%0\n\
  78. cmp/eq %0,%3\n\
  79. bf 1f\n\
  80. mov.w %2,@%1\n\
  81. 1: mov r1,r15"\
  82. : "=&r" (__result) : "r" (mem), "r" (newval), "r" (oldval) \
  83. : "r0", "r1", "t", "memory"); \
  84. __result; })
  85. #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
  86. ({ __typeof (*(mem)) __result; \
  87. __asm __volatile ("\
  88. .align 2\n\
  89. mova 1f,r0\n\
  90. nop\n\
  91. mov r15,r1\n\
  92. mov #-8,r15\n\
  93. 0: mov.l @%1,%0\n\
  94. cmp/eq %0,%3\n\
  95. bf 1f\n\
  96. mov.l %2,@%1\n\
  97. 1: mov r1,r15"\
  98. : "=&r" (__result) : "r" (mem), "r" (newval), "r" (oldval) \
  99. : "r0", "r1", "t", "memory"); \
  100. __result; })
  101. /* XXX We do not really need 64-bit compare-and-exchange. At least
  102. not in the moment. Using it would mean causing portability
  103. problems since not many other 32-bit architectures have support for
  104. such an operation. So don't define any code for now. */
  105. # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
  106. (abort (), (__typeof (*mem)) 0)
  107. #define atomic_exchange_and_add(mem, value) \
  108. ({ __typeof (*(mem)) __result, __tmp, __value = (value); \
  109. if (sizeof (*(mem)) == 1) \
  110. __asm __volatile ("\
  111. .align 2\n\
  112. mova 1f,r0\n\
  113. mov r15,r1\n\
  114. mov #-6,r15\n\
  115. 0: mov.b @%2,%0\n\
  116. add %0,%1\n\
  117. mov.b %1,@%2\n\
  118. 1: mov r1,r15"\
  119. : "=&r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  120. : "r0", "r1", "memory"); \
  121. else if (sizeof (*(mem)) == 2) \
  122. __asm __volatile ("\
  123. .align 2\n\
  124. mova 1f,r0\n\
  125. mov r15,r1\n\
  126. mov #-6,r15\n\
  127. 0: mov.w @%2,%0\n\
  128. add %0,%1\n\
  129. mov.w %1,@%2\n\
  130. 1: mov r1,r15"\
  131. : "=&r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  132. : "r0", "r1", "memory"); \
  133. else if (sizeof (*(mem)) == 4) \
  134. __asm __volatile ("\
  135. .align 2\n\
  136. mova 1f,r0\n\
  137. mov r15,r1\n\
  138. mov #-6,r15\n\
  139. 0: mov.l @%2,%0\n\
  140. add %0,%1\n\
  141. mov.l %1,@%2\n\
  142. 1: mov r1,r15"\
  143. : "=&r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  144. : "r0", "r1", "memory"); \
  145. else \
  146. { \
  147. __typeof (mem) memp = (mem); \
  148. do \
  149. __result = *memp; \
  150. while (__arch_compare_and_exchange_val_64_acq \
  151. (memp, __result + __value, __result) == __result); \
  152. (void) __value; \
  153. } \
  154. __result; })
  155. #define atomic_add(mem, value) \
  156. (void) ({ __typeof (*(mem)) __tmp, __value = (value); \
  157. if (sizeof (*(mem)) == 1) \
  158. __asm __volatile ("\
  159. .align 2\n\
  160. mova 1f,r0\n\
  161. mov r15,r1\n\
  162. mov #-6,r15\n\
  163. 0: mov.b @%1,r2\n\
  164. add r2,%0\n\
  165. mov.b %0,@%1\n\
  166. 1: mov r1,r15"\
  167. : "=&r" (__tmp) : "r" (mem), "0" (__value) \
  168. : "r0", "r1", "r2", "memory"); \
  169. else if (sizeof (*(mem)) == 2) \
  170. __asm __volatile ("\
  171. .align 2\n\
  172. mova 1f,r0\n\
  173. mov r15,r1\n\
  174. mov #-6,r15\n\
  175. 0: mov.w @%1,r2\n\
  176. add r2,%0\n\
  177. mov.w %0,@%1\n\
  178. 1: mov r1,r15"\
  179. : "=&r" (__tmp) : "r" (mem), "0" (__value) \
  180. : "r0", "r1", "r2", "memory"); \
  181. else if (sizeof (*(mem)) == 4) \
  182. __asm __volatile ("\
  183. .align 2\n\
  184. mova 1f,r0\n\
  185. mov r15,r1\n\
  186. mov #-6,r15\n\
  187. 0: mov.l @%1,r2\n\
  188. add r2,%0\n\
  189. mov.l %0,@%1\n\
  190. 1: mov r1,r15"\
  191. : "=&r" (__tmp) : "r" (mem), "0" (__value) \
  192. : "r0", "r1", "r2", "memory"); \
  193. else \
  194. { \
  195. __typeof (*(mem)) oldval; \
  196. __typeof (mem) memp = (mem); \
  197. do \
  198. oldval = *memp; \
  199. while (__arch_compare_and_exchange_val_64_acq \
  200. (memp, oldval + __value, oldval) == oldval); \
  201. (void) __value; \
  202. } \
  203. })
  204. #define atomic_add_negative(mem, value) \
  205. ({ unsigned char __result; \
  206. __typeof (*(mem)) __tmp, __value = (value); \
  207. if (sizeof (*(mem)) == 1) \
  208. __asm __volatile ("\
  209. .align 2\n\
  210. mova 1f,r0\n\
  211. mov r15,r1\n\
  212. mov #-6,r15\n\
  213. 0: mov.b @%2,r2\n\
  214. add r2,%1\n\
  215. mov.b %1,@%2\n\
  216. 1: mov r1,r15\n\
  217. shal %1\n\
  218. movt %0"\
  219. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  220. : "r0", "r1", "r2", "t", "memory"); \
  221. else if (sizeof (*(mem)) == 2) \
  222. __asm __volatile ("\
  223. .align 2\n\
  224. mova 1f,r0\n\
  225. mov r15,r1\n\
  226. mov #-6,r15\n\
  227. 0: mov.w @%2,r2\n\
  228. add r2,%1\n\
  229. mov.w %1,@%2\n\
  230. 1: mov r1,r15\n\
  231. shal %1\n\
  232. movt %0"\
  233. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  234. : "r0", "r1", "r2", "t", "memory"); \
  235. else if (sizeof (*(mem)) == 4) \
  236. __asm __volatile ("\
  237. .align 2\n\
  238. mova 1f,r0\n\
  239. mov r15,r1\n\
  240. mov #-6,r15\n\
  241. 0: mov.l @%2,r2\n\
  242. add r2,%1\n\
  243. mov.l %1,@%2\n\
  244. 1: mov r1,r15\n\
  245. shal %1\n\
  246. movt %0"\
  247. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  248. : "r0", "r1", "r2", "t", "memory"); \
  249. else \
  250. abort (); \
  251. __result; })
  252. #define atomic_add_zero(mem, value) \
  253. ({ unsigned char __result; \
  254. __typeof (*(mem)) __tmp, __value = (value); \
  255. if (sizeof (*(mem)) == 1) \
  256. __asm __volatile ("\
  257. .align 2\n\
  258. mova 1f,r0\n\
  259. mov r15,r1\n\
  260. mov #-6,r15\n\
  261. 0: mov.b @%2,r2\n\
  262. add r2,%1\n\
  263. mov.b %1,@%2\n\
  264. 1: mov r1,r15\n\
  265. tst %1,%1\n\
  266. movt %0"\
  267. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  268. : "r0", "r1", "r2", "t", "memory"); \
  269. else if (sizeof (*(mem)) == 2) \
  270. __asm __volatile ("\
  271. .align 2\n\
  272. mova 1f,r0\n\
  273. mov r15,r1\n\
  274. mov #-6,r15\n\
  275. 0: mov.w @%2,r2\n\
  276. add r2,%1\n\
  277. mov.w %1,@%2\n\
  278. 1: mov r1,r15\n\
  279. tst %1,%1\n\
  280. movt %0"\
  281. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  282. : "r0", "r1", "r2", "t", "memory"); \
  283. else if (sizeof (*(mem)) == 4) \
  284. __asm __volatile ("\
  285. .align 2\n\
  286. mova 1f,r0\n\
  287. mov r15,r1\n\
  288. mov #-6,r15\n\
  289. 0: mov.l @%2,r2\n\
  290. add r2,%1\n\
  291. mov.l %1,@%2\n\
  292. 1: mov r1,r15\n\
  293. tst %1,%1\n\
  294. movt %0"\
  295. : "=r" (__result), "=&r" (__tmp) : "r" (mem), "1" (__value) \
  296. : "r0", "r1", "r2", "t", "memory"); \
  297. else \
  298. abort (); \
  299. __result; })
  300. #define atomic_increment_and_test(mem) atomic_add_zero((mem), 1)
  301. #define atomic_decrement_and_test(mem) atomic_add_zero((mem), -1)
  302. #define atomic_bit_set(mem, bit) \
  303. (void) ({ unsigned int __mask = 1 << (bit); \
  304. if (sizeof (*(mem)) == 1) \
  305. __asm __volatile ("\
  306. .align 2\n\
  307. mova 1f,r0\n\
  308. mov r15,r1\n\
  309. mov #-6,r15\n\
  310. 0: mov.b @%0,r2\n\
  311. or %1,r2\n\
  312. mov.b r2,@%0\n\
  313. 1: mov r1,r15"\
  314. : : "r" (mem), "r" (__mask) \
  315. : "r0", "r1", "r2", "memory"); \
  316. else if (sizeof (*(mem)) == 2) \
  317. __asm __volatile ("\
  318. .align 2\n\
  319. mova 1f,r0\n\
  320. mov r15,r1\n\
  321. mov #-6,r15\n\
  322. 0: mov.w @%0,r2\n\
  323. or %1,r2\n\
  324. mov.w r2,@%0\n\
  325. 1: mov r1,r15"\
  326. : : "r" (mem), "r" (__mask) \
  327. : "r0", "r1", "r2", "memory"); \
  328. else if (sizeof (*(mem)) == 4) \
  329. __asm __volatile ("\
  330. .align 2\n\
  331. mova 1f,r0\n\
  332. mov r15,r1\n\
  333. mov #-6,r15\n\
  334. 0: mov.l @%0,r2\n\
  335. or %1,r2\n\
  336. mov.l r2,@%0\n\
  337. 1: mov r1,r15"\
  338. : : "r" (mem), "r" (__mask) \
  339. : "r0", "r1", "r2", "memory"); \
  340. else \
  341. abort (); \
  342. })
  343. #define atomic_bit_test_set(mem, bit) \
  344. ({ unsigned int __mask = 1 << (bit); \
  345. unsigned int __result = __mask; \
  346. if (sizeof (*(mem)) == 1) \
  347. __asm __volatile ("\
  348. .align 2\n\
  349. mova 1f,r0\n\
  350. nop\n\
  351. mov r15,r1\n\
  352. mov #-8,r15\n\
  353. 0: mov.b @%2,r2\n\
  354. or r2,%1\n\
  355. and r2,%0\n\
  356. mov.b %1,@%2\n\
  357. 1: mov r1,r15"\
  358. : "=&r" (__result), "=&r" (__mask) \
  359. : "r" (mem), "0" (__result), "1" (__mask) \
  360. : "r0", "r1", "r2", "memory"); \
  361. else if (sizeof (*(mem)) == 2) \
  362. __asm __volatile ("\
  363. .align 2\n\
  364. mova 1f,r0\n\
  365. nop\n\
  366. mov r15,r1\n\
  367. mov #-8,r15\n\
  368. 0: mov.w @%2,r2\n\
  369. or r2,%1\n\
  370. and r2,%0\n\
  371. mov.w %1,@%2\n\
  372. 1: mov r1,r15"\
  373. : "=&r" (__result), "=&r" (__mask) \
  374. : "r" (mem), "0" (__result), "1" (__mask) \
  375. : "r0", "r1", "r2", "memory"); \
  376. else if (sizeof (*(mem)) == 4) \
  377. __asm __volatile ("\
  378. .align 2\n\
  379. mova 1f,r0\n\
  380. nop\n\
  381. mov r15,r1\n\
  382. mov #-8,r15\n\
  383. 0: mov.l @%2,r2\n\
  384. or r2,%1\n\
  385. and r2,%0\n\
  386. mov.l %1,@%2\n\
  387. 1: mov r1,r15"\
  388. : "=&r" (__result), "=&r" (__mask) \
  389. : "r" (mem), "0" (__result), "1" (__mask) \
  390. : "r0", "r1", "r2", "memory"); \
  391. else \
  392. abort (); \
  393. __result; })