lowlevelrobustlock.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C 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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <sysdep.h>
  18. #include <pthread-errnos.h>
  19. #include <lowlevellock.h>
  20. #include <lowlevelrobustlock.h>
  21. #include <bits/kernel-features.h>
  22. .text
  23. #define FUTEX_WAITERS 0x80000000
  24. #define FUTEX_OWNER_DIED 0x40000000
  25. #ifdef __ASSUME_PRIVATE_FUTEX
  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. #else
  31. # if FUTEX_WAIT == 0
  32. # define LOAD_FUTEX_WAIT(reg) \
  33. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  34. andl %fs:PRIVATE_FUTEX, reg
  35. # else
  36. # define LOAD_FUTEX_WAIT(reg) \
  37. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  38. andl %fs:PRIVATE_FUTEX, reg ; \
  39. orl $FUTEX_WAIT, reg
  40. # endif
  41. # define LOAD_FUTEX_WAIT_ABS(reg) \
  42. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  43. andl %fs:PRIVATE_FUTEX, reg ; \
  44. orl $FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, reg
  45. #endif
  46. /* For the calculation see asm/vsyscall.h. */
  47. #define VSYSCALL_ADDR_vgettimeofday 0xffffffffff600000
  48. .globl __lll_robust_lock_wait
  49. .type __lll_robust_lock_wait,@function
  50. .hidden __lll_robust_lock_wait
  51. .align 16
  52. __lll_robust_lock_wait:
  53. cfi_startproc
  54. pushq %r10
  55. cfi_adjust_cfa_offset(8)
  56. pushq %rdx
  57. cfi_adjust_cfa_offset(8)
  58. cfi_offset(%r10, -16)
  59. cfi_offset(%rdx, -24)
  60. xorq %r10, %r10 /* No timeout. */
  61. LOAD_FUTEX_WAIT (%esi)
  62. 4: movl %eax, %edx
  63. orl $FUTEX_WAITERS, %edx
  64. testl $FUTEX_OWNER_DIED, %eax
  65. jnz 3f
  66. cmpl %edx, %eax
  67. je 1f
  68. LOCK
  69. cmpxchgl %edx, (%rdi)
  70. jnz 2f
  71. 1: movl $SYS_futex, %eax
  72. syscall
  73. movl (%rdi), %eax
  74. 2: testl %eax, %eax
  75. jne 4b
  76. movl %fs:TID, %edx
  77. orl $FUTEX_WAITERS, %edx
  78. LOCK
  79. cmpxchgl %edx, (%rdi)
  80. jnz 4b
  81. /* NB: %rax == 0 */
  82. 3: popq %rdx
  83. cfi_adjust_cfa_offset(-8)
  84. cfi_restore(%rdx)
  85. popq %r10
  86. cfi_adjust_cfa_offset(-8)
  87. cfi_restore(%r10)
  88. retq
  89. cfi_endproc
  90. .size __lll_robust_lock_wait,.-__lll_robust_lock_wait
  91. .globl __lll_robust_timedlock_wait
  92. .type __lll_robust_timedlock_wait,@function
  93. .hidden __lll_robust_timedlock_wait
  94. .align 16
  95. __lll_robust_timedlock_wait:
  96. cfi_startproc
  97. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  98. # ifdef __PIC__
  99. cmpl $0, __have_futex_clock_realtime(%rip)
  100. # else
  101. cmpl $0, __have_futex_clock_realtime
  102. # endif
  103. je .Lreltmo
  104. # endif
  105. pushq %r9
  106. cfi_adjust_cfa_offset(8)
  107. cfi_rel_offset(%r9, 0)
  108. movq %rdx, %r10
  109. movl $0xffffffff, %r9d
  110. LOAD_FUTEX_WAIT_ABS (%esi)
  111. 1: testl $FUTEX_OWNER_DIED, %eax
  112. jnz 3f
  113. movl %eax, %edx
  114. orl $FUTEX_WAITERS, %edx
  115. cmpl %eax, %edx
  116. je 5f
  117. LOCK
  118. cmpxchgl %edx, (%rdi)
  119. movq $0, %rcx /* Must use mov to avoid changing cc. */
  120. jnz 6f
  121. 5: movl $SYS_futex, %eax
  122. syscall
  123. movl %eax, %ecx
  124. movl (%rdi), %eax
  125. 6: testl %eax, %eax
  126. jne 2f
  127. movl %fs:TID, %edx
  128. orl $FUTEX_WAITERS, %edx
  129. LOCK
  130. cmpxchgl %edx, (%rdi)
  131. jnz 2f
  132. 3: popq %r9
  133. cfi_adjust_cfa_offset(-8)
  134. cfi_restore(%r9)
  135. retq
  136. cfi_adjust_cfa_offset(8)
  137. cfi_rel_offset(%r9, 0)
  138. /* Check whether the time expired. */
  139. 2: cmpl $-ETIMEDOUT, %ecx
  140. je 4f
  141. cmpl $-EINVAL, %ecx
  142. jne 1b
  143. 4: movl %ecx, %eax
  144. negl %eax
  145. jmp 3b
  146. cfi_adjust_cfa_offset(-8)
  147. cfi_restore(%r9)
  148. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  149. .Lreltmo:
  150. /* Check for a valid timeout value. */
  151. cmpq $1000000000, 8(%rdx)
  152. jae 3f
  153. pushq %r8
  154. cfi_adjust_cfa_offset(8)
  155. pushq %r9
  156. cfi_adjust_cfa_offset(8)
  157. pushq %r12
  158. cfi_adjust_cfa_offset(8)
  159. pushq %r13
  160. cfi_adjust_cfa_offset(8)
  161. cfi_offset(%r8, -16)
  162. cfi_offset(%r9, -24)
  163. cfi_offset(%r12, -32)
  164. cfi_offset(%r13, -40)
  165. pushq %rsi
  166. cfi_adjust_cfa_offset(8)
  167. /* Stack frame for the timespec and timeval structs. */
  168. subq $32, %rsp
  169. cfi_adjust_cfa_offset(32)
  170. movq %rdi, %r12
  171. movq %rdx, %r13
  172. 1: movq %rax, 16(%rsp)
  173. /* Get current time. */
  174. movq %rsp, %rdi
  175. xorl %esi, %esi
  176. movq $VSYSCALL_ADDR_vgettimeofday, %rax
  177. /* This is a regular function call, all caller-save registers
  178. might be clobbered. */
  179. callq *%rax
  180. /* Compute relative timeout. */
  181. movq 8(%rsp), %rax
  182. movl $1000, %edi
  183. mul %rdi /* Milli seconds to nano seconds. */
  184. movq (%r13), %rdi
  185. movq 8(%r13), %rsi
  186. subq (%rsp), %rdi
  187. subq %rax, %rsi
  188. jns 4f
  189. addq $1000000000, %rsi
  190. decq %rdi
  191. 4: testq %rdi, %rdi
  192. js 8f /* Time is already up. */
  193. /* Futex call. */
  194. movq %rdi, (%rsp) /* Store relative timeout. */
  195. movq %rsi, 8(%rsp)
  196. movq 16(%rsp), %rdx
  197. movl %edx, %eax
  198. orl $FUTEX_WAITERS, %edx
  199. testl $FUTEX_OWNER_DIED, %eax
  200. jnz 6f
  201. cmpl %eax, %edx
  202. je 2f
  203. LOCK
  204. cmpxchgl %edx, (%r12)
  205. movq $0, %rcx /* Must use mov to avoid changing cc. */
  206. jnz 5f
  207. 2: movq %rsp, %r10
  208. movl 32(%rsp), %esi
  209. LOAD_FUTEX_WAIT (%esi)
  210. movq %r12, %rdi
  211. movl $SYS_futex, %eax
  212. syscall
  213. movq %rax, %rcx
  214. movl (%r12), %eax
  215. 5: testl %eax, %eax
  216. jne 7f
  217. movl %fs:TID, %edx
  218. orl $FUTEX_WAITERS, %edx
  219. LOCK
  220. cmpxchgl %edx, (%r12)
  221. jnz 7f
  222. 6: addq $40, %rsp
  223. cfi_adjust_cfa_offset(-40)
  224. popq %r13
  225. cfi_adjust_cfa_offset(-8)
  226. cfi_restore(%r13)
  227. popq %r12
  228. cfi_adjust_cfa_offset(-8)
  229. cfi_restore(%r12)
  230. popq %r9
  231. cfi_adjust_cfa_offset(-8)
  232. cfi_restore(%r9)
  233. popq %r8
  234. cfi_adjust_cfa_offset(-8)
  235. cfi_restore(%r8)
  236. retq
  237. 3: movl $EINVAL, %eax
  238. retq
  239. cfi_adjust_cfa_offset(72)
  240. cfi_offset(%r8, -16)
  241. cfi_offset(%r9, -24)
  242. cfi_offset(%r12, -32)
  243. cfi_offset(%r13, -40)
  244. /* Check whether the time expired. */
  245. 7: cmpl $-ETIMEDOUT, %ecx
  246. jne 1b
  247. 8: movl $ETIMEDOUT, %eax
  248. jmp 6b
  249. #endif
  250. cfi_endproc
  251. .size __lll_robust_timedlock_wait,.-__lll_robust_timedlock_wait