lowlevellock.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Copyright (C) 2002-2004, 2006, 2007, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sysdep.h>
  16. #include <pthread-errnos.h>
  17. #include <bits/kernel-features.h>
  18. #include <lowlevellock.h>
  19. #include <tcb-offsets.h>
  20. .text
  21. #ifdef __ASSUME_PRIVATE_FUTEX
  22. # define LOAD_PRIVATE_FUTEX_WAIT(reg) \
  23. movl $(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), reg
  24. # define LOAD_PRIVATE_FUTEX_WAKE(reg) \
  25. movl $(FUTEX_WAKE | FUTEX_PRIVATE_FLAG), reg
  26. # define LOAD_FUTEX_WAIT(reg) \
  27. xorl $(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), reg
  28. # define LOAD_FUTEX_WAIT_ABS(reg) \
  29. xorl $(FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME), reg
  30. # define LOAD_FUTEX_WAKE(reg) \
  31. xorl $(FUTEX_WAKE | FUTEX_PRIVATE_FLAG), reg
  32. #else
  33. # if FUTEX_WAIT == 0
  34. # define LOAD_PRIVATE_FUTEX_WAIT(reg) \
  35. movl %gs:PRIVATE_FUTEX, reg
  36. # else
  37. # define LOAD_PRIVATE_FUTEX_WAIT(reg) \
  38. movl %gs:PRIVATE_FUTEX, reg ; \
  39. orl $FUTEX_WAIT, reg
  40. # endif
  41. # define LOAD_PRIVATE_FUTEX_WAKE(reg) \
  42. movl %gs:PRIVATE_FUTEX, reg ; \
  43. orl $FUTEX_WAKE, reg
  44. # if FUTEX_WAIT == 0
  45. # define LOAD_FUTEX_WAIT(reg) \
  46. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  47. andl %gs:PRIVATE_FUTEX, reg
  48. # else
  49. # define LOAD_FUTEX_WAIT(reg) \
  50. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  51. andl %gs:PRIVATE_FUTEX, reg ; \
  52. orl $FUTEX_WAIT, reg
  53. # endif
  54. # define LOAD_FUTEX_WAIT_ABS(reg) \
  55. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  56. andl %gs:PRIVATE_FUTEX, reg ; \
  57. orl $FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, reg
  58. # define LOAD_FUTEX_WAKE(reg) \
  59. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  60. andl %gs:PRIVATE_FUTEX, reg ; \
  61. orl $FUTEX_WAKE, reg
  62. #endif
  63. .globl __lll_lock_wait_private
  64. .type __lll_lock_wait_private,@function
  65. .hidden __lll_lock_wait_private
  66. .align 16
  67. __lll_lock_wait_private:
  68. cfi_startproc
  69. pushl %edx
  70. cfi_adjust_cfa_offset(4)
  71. pushl %ebx
  72. cfi_adjust_cfa_offset(4)
  73. pushl %esi
  74. cfi_adjust_cfa_offset(4)
  75. cfi_offset(%edx, -8)
  76. cfi_offset(%ebx, -12)
  77. cfi_offset(%esi, -16)
  78. movl $2, %edx
  79. movl %ecx, %ebx
  80. xorl %esi, %esi /* No timeout. */
  81. LOAD_PRIVATE_FUTEX_WAIT (%ecx)
  82. cmpl %edx, %eax /* NB: %edx == 2 */
  83. jne 2f
  84. 1: movl $SYS_futex, %eax
  85. ENTER_KERNEL
  86. 2: movl %edx, %eax
  87. xchgl %eax, (%ebx) /* NB: lock is implied */
  88. testl %eax, %eax
  89. jnz 1b
  90. popl %esi
  91. cfi_adjust_cfa_offset(-4)
  92. cfi_restore(%esi)
  93. popl %ebx
  94. cfi_adjust_cfa_offset(-4)
  95. cfi_restore(%ebx)
  96. popl %edx
  97. cfi_adjust_cfa_offset(-4)
  98. cfi_restore(%edx)
  99. ret
  100. cfi_endproc
  101. .size __lll_lock_wait_private,.-__lll_lock_wait_private
  102. #ifdef NOT_IN_libc
  103. .globl __lll_lock_wait
  104. .type __lll_lock_wait,@function
  105. .hidden __lll_lock_wait
  106. .align 16
  107. __lll_lock_wait:
  108. cfi_startproc
  109. pushl %edx
  110. cfi_adjust_cfa_offset(4)
  111. pushl %ebx
  112. cfi_adjust_cfa_offset(4)
  113. pushl %esi
  114. cfi_adjust_cfa_offset(4)
  115. cfi_offset(%edx, -8)
  116. cfi_offset(%ebx, -12)
  117. cfi_offset(%esi, -16)
  118. movl %edx, %ebx
  119. movl $2, %edx
  120. xorl %esi, %esi /* No timeout. */
  121. LOAD_FUTEX_WAIT (%ecx)
  122. cmpl %edx, %eax /* NB: %edx == 2 */
  123. jne 2f
  124. 1: movl $SYS_futex, %eax
  125. ENTER_KERNEL
  126. 2: movl %edx, %eax
  127. xchgl %eax, (%ebx) /* NB: lock is implied */
  128. testl %eax, %eax
  129. jnz 1b
  130. popl %esi
  131. cfi_adjust_cfa_offset(-4)
  132. cfi_restore(%esi)
  133. popl %ebx
  134. cfi_adjust_cfa_offset(-4)
  135. cfi_restore(%ebx)
  136. popl %edx
  137. cfi_adjust_cfa_offset(-4)
  138. cfi_restore(%edx)
  139. ret
  140. cfi_endproc
  141. .size __lll_lock_wait,.-__lll_lock_wait
  142. /* %ecx: futex
  143. %esi: flags
  144. %edx: timeout
  145. %eax: futex value
  146. */
  147. .globl __lll_timedlock_wait
  148. .type __lll_timedlock_wait,@function
  149. .hidden __lll_timedlock_wait
  150. .align 16
  151. __lll_timedlock_wait:
  152. cfi_startproc
  153. pushl %ebp
  154. cfi_adjust_cfa_offset(4)
  155. cfi_rel_offset(%ebp, 0)
  156. pushl %ebx
  157. cfi_adjust_cfa_offset(4)
  158. cfi_rel_offset(%ebx, 0)
  159. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  160. # ifdef __PIC__
  161. LOAD_PIC_REG (bx)
  162. cmpl $0, __have_futex_clock_realtime@GOTOFF(%ebx)
  163. # else
  164. cmpl $0, __have_futex_clock_realtime
  165. # endif
  166. je .Lreltmo
  167. # endif
  168. movl %ecx, %ebx
  169. movl %esi, %ecx
  170. movl %edx, %esi
  171. movl $0xffffffff, %ebp
  172. LOAD_FUTEX_WAIT_ABS (%ecx)
  173. movl $2, %edx
  174. cmpl %edx, %eax
  175. jne 2f
  176. 1: movl $SYS_futex, %eax
  177. movl $2, %edx
  178. ENTER_KERNEL
  179. 2: xchgl %edx, (%ebx) /* NB: lock is implied */
  180. testl %edx, %edx
  181. jz 3f
  182. cmpl $-ETIMEDOUT, %eax
  183. je 4f
  184. cmpl $-EINVAL, %eax
  185. jne 1b
  186. 4: movl %eax, %edx
  187. negl %edx
  188. 3: movl %edx, %eax
  189. 7: popl %ebx
  190. cfi_adjust_cfa_offset(-4)
  191. cfi_restore(%ebx)
  192. popl %ebp
  193. cfi_adjust_cfa_offset(-4)
  194. cfi_restore(%ebp)
  195. ret
  196. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  197. .Lreltmo:
  198. /* Check for a valid timeout value. */
  199. cmpl $1000000000, 4(%edx)
  200. jae 3f
  201. pushl %esi
  202. cfi_adjust_cfa_offset(4)
  203. cfi_rel_offset(%esi, 0)
  204. pushl %edi
  205. cfi_adjust_cfa_offset(4)
  206. cfi_rel_offset(%edi, 0)
  207. /* Stack frame for the timespec and timeval structs. */
  208. subl $8, %esp
  209. cfi_adjust_cfa_offset(8)
  210. movl %ecx, %ebp
  211. movl %edx, %edi
  212. movl $2, %edx
  213. xchgl %edx, (%ebp)
  214. test %edx, %edx
  215. je 6f
  216. 1:
  217. /* Get current time. */
  218. movl %esp, %ebx
  219. xorl %ecx, %ecx
  220. movl $__NR_gettimeofday, %eax
  221. ENTER_KERNEL
  222. /* Compute relative timeout. */
  223. movl 4(%esp), %eax
  224. movl $1000, %edx
  225. mul %edx /* Milli seconds to nano seconds. */
  226. movl (%edi), %ecx
  227. movl 4(%edi), %edx
  228. subl (%esp), %ecx
  229. subl %eax, %edx
  230. jns 4f
  231. addl $1000000000, %edx
  232. subl $1, %ecx
  233. 4: testl %ecx, %ecx
  234. js 2f /* Time is already up. */
  235. /* Store relative timeout. */
  236. movl %ecx, (%esp)
  237. movl %edx, 4(%esp)
  238. /* Futex call. */
  239. movl %ebp, %ebx
  240. movl $2, %edx
  241. movl %esp, %esi
  242. movl 16(%esp), %ecx
  243. LOAD_FUTEX_WAIT (%ecx)
  244. movl $SYS_futex, %eax
  245. ENTER_KERNEL
  246. /* NB: %edx == 2 */
  247. xchgl %edx, (%ebp)
  248. testl %edx, %edx
  249. je 6f
  250. cmpl $-ETIMEDOUT, %eax
  251. jne 1b
  252. 2: movl $ETIMEDOUT, %edx
  253. 6: addl $8, %esp
  254. cfi_adjust_cfa_offset(-8)
  255. popl %edi
  256. cfi_adjust_cfa_offset(-4)
  257. cfi_restore(%edi)
  258. popl %esi
  259. cfi_adjust_cfa_offset(-4)
  260. cfi_restore(%esi)
  261. 7: popl %ebx
  262. cfi_adjust_cfa_offset(-4)
  263. cfi_restore(%ebx)
  264. popl %ebp
  265. cfi_adjust_cfa_offset(-4)
  266. cfi_restore(%ebp)
  267. movl %edx, %eax
  268. ret
  269. 3: movl $EINVAL, %edx
  270. jmp 7b
  271. # endif
  272. cfi_endproc
  273. .size __lll_timedlock_wait,.-__lll_timedlock_wait
  274. #endif
  275. .globl __lll_unlock_wake_private
  276. .type __lll_unlock_wake_private,@function
  277. .hidden __lll_unlock_wake_private
  278. .align 16
  279. __lll_unlock_wake_private:
  280. cfi_startproc
  281. pushl %ebx
  282. cfi_adjust_cfa_offset(4)
  283. pushl %ecx
  284. cfi_adjust_cfa_offset(4)
  285. pushl %edx
  286. cfi_adjust_cfa_offset(4)
  287. cfi_offset(%ebx, -8)
  288. cfi_offset(%ecx, -12)
  289. cfi_offset(%edx, -16)
  290. movl %eax, %ebx
  291. movl $0, (%eax)
  292. LOAD_PRIVATE_FUTEX_WAKE (%ecx)
  293. movl $1, %edx /* Wake one thread. */
  294. movl $SYS_futex, %eax
  295. ENTER_KERNEL
  296. popl %edx
  297. cfi_adjust_cfa_offset(-4)
  298. cfi_restore(%edx)
  299. popl %ecx
  300. cfi_adjust_cfa_offset(-4)
  301. cfi_restore(%ecx)
  302. popl %ebx
  303. cfi_adjust_cfa_offset(-4)
  304. cfi_restore(%ebx)
  305. ret
  306. cfi_endproc
  307. .size __lll_unlock_wake_private,.-__lll_unlock_wake_private
  308. #ifdef NOT_IN_libc
  309. .globl __lll_unlock_wake
  310. .type __lll_unlock_wake,@function
  311. .hidden __lll_unlock_wake
  312. .align 16
  313. __lll_unlock_wake:
  314. cfi_startproc
  315. pushl %ebx
  316. cfi_adjust_cfa_offset(4)
  317. pushl %ecx
  318. cfi_adjust_cfa_offset(4)
  319. pushl %edx
  320. cfi_adjust_cfa_offset(4)
  321. cfi_offset(%ebx, -8)
  322. cfi_offset(%ecx, -12)
  323. cfi_offset(%edx, -16)
  324. movl %eax, %ebx
  325. movl $0, (%eax)
  326. LOAD_FUTEX_WAKE (%ecx)
  327. movl $1, %edx /* Wake one thread. */
  328. movl $SYS_futex, %eax
  329. ENTER_KERNEL
  330. popl %edx
  331. cfi_adjust_cfa_offset(-4)
  332. cfi_restore(%edx)
  333. popl %ecx
  334. cfi_adjust_cfa_offset(-4)
  335. cfi_restore(%ecx)
  336. popl %ebx
  337. cfi_adjust_cfa_offset(-4)
  338. cfi_restore(%ebx)
  339. ret
  340. cfi_endproc
  341. .size __lll_unlock_wake,.-__lll_unlock_wake
  342. .globl __lll_timedwait_tid
  343. .type __lll_timedwait_tid,@function
  344. .hidden __lll_timedwait_tid
  345. .align 16
  346. __lll_timedwait_tid:
  347. pushl %edi
  348. pushl %esi
  349. pushl %ebx
  350. pushl %ebp
  351. movl %eax, %ebp
  352. movl %edx, %edi
  353. subl $8, %esp
  354. /* Get current time. */
  355. 2: movl %esp, %ebx
  356. xorl %ecx, %ecx
  357. movl $__NR_gettimeofday, %eax
  358. ENTER_KERNEL
  359. /* Compute relative timeout. */
  360. movl 4(%esp), %eax
  361. movl $1000, %edx
  362. mul %edx /* Milli seconds to nano seconds. */
  363. movl (%edi), %ecx
  364. movl 4(%edi), %edx
  365. subl (%esp), %ecx
  366. subl %eax, %edx
  367. jns 5f
  368. addl $1000000000, %edx
  369. subl $1, %ecx
  370. 5: testl %ecx, %ecx
  371. js 6f /* Time is already up. */
  372. movl %ecx, (%esp) /* Store relative timeout. */
  373. movl %edx, 4(%esp)
  374. movl (%ebp), %edx
  375. testl %edx, %edx
  376. jz 4f
  377. movl %esp, %esi
  378. /* XXX The kernel so far uses global futex for the wakeup at
  379. all times. */
  380. xorl %ecx, %ecx /* movl $FUTEX_WAIT, %ecx */
  381. movl %ebp, %ebx
  382. movl $SYS_futex, %eax
  383. ENTER_KERNEL
  384. cmpl $0, (%ebx)
  385. jne 1f
  386. 4: xorl %eax, %eax
  387. 3: addl $8, %esp
  388. popl %ebp
  389. popl %ebx
  390. popl %esi
  391. popl %edi
  392. ret
  393. 1: cmpl $-ETIMEDOUT, %eax
  394. jne 2b
  395. 6: movl $ETIMEDOUT, %eax
  396. jmp 3b
  397. .size __lll_timedwait_tid,.-__lll_timedwait_tid
  398. #endif