lowlevellock.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Copyright (C) 2002-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 %fs:PRIVATE_FUTEX, reg
  36. # else
  37. # define LOAD_PRIVATE_FUTEX_WAIT(reg) \
  38. movl %fs:PRIVATE_FUTEX, reg ; \
  39. orl $FUTEX_WAIT, reg
  40. # endif
  41. # define LOAD_PRIVATE_FUTEX_WAKE(reg) \
  42. movl %fs: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 %fs:PRIVATE_FUTEX, reg
  48. # else
  49. # define LOAD_FUTEX_WAIT(reg) \
  50. xorl $FUTEX_PRIVATE_FLAG, reg ; \
  51. andl %fs: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 %fs: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 %fs:PRIVATE_FUTEX, reg ; \
  61. orl $FUTEX_WAKE, reg
  62. #endif
  63. /* For the calculation see asm/vsyscall.h. */
  64. #define VSYSCALL_ADDR_vgettimeofday 0xffffffffff600000
  65. .globl __lll_lock_wait_private
  66. .type __lll_lock_wait_private,@function
  67. .hidden __lll_lock_wait_private
  68. .align 16
  69. __lll_lock_wait_private:
  70. cfi_startproc
  71. pushq %r10
  72. cfi_adjust_cfa_offset(8)
  73. pushq %rdx
  74. cfi_adjust_cfa_offset(8)
  75. cfi_offset(%r10, -16)
  76. cfi_offset(%rdx, -24)
  77. xorq %r10, %r10 /* No timeout. */
  78. movl $2, %edx
  79. LOAD_PRIVATE_FUTEX_WAIT (%esi)
  80. cmpl %edx, %eax /* NB: %edx == 2 */
  81. jne 2f
  82. 1: movl $SYS_futex, %eax
  83. syscall
  84. 2: movl %edx, %eax
  85. xchgl %eax, (%rdi) /* NB: lock is implied */
  86. testl %eax, %eax
  87. jnz 1b
  88. popq %rdx
  89. cfi_adjust_cfa_offset(-8)
  90. cfi_restore(%rdx)
  91. popq %r10
  92. cfi_adjust_cfa_offset(-8)
  93. cfi_restore(%r10)
  94. retq
  95. cfi_endproc
  96. .size __lll_lock_wait_private,.-__lll_lock_wait_private
  97. #ifdef NOT_IN_libc
  98. .globl __lll_lock_wait
  99. .type __lll_lock_wait,@function
  100. .hidden __lll_lock_wait
  101. .align 16
  102. __lll_lock_wait:
  103. cfi_startproc
  104. pushq %r10
  105. cfi_adjust_cfa_offset(8)
  106. pushq %rdx
  107. cfi_adjust_cfa_offset(8)
  108. cfi_offset(%r10, -16)
  109. cfi_offset(%rdx, -24)
  110. xorq %r10, %r10 /* No timeout. */
  111. movl $2, %edx
  112. LOAD_FUTEX_WAIT (%esi)
  113. cmpl %edx, %eax /* NB: %edx == 2 */
  114. jne 2f
  115. 1: movl $SYS_futex, %eax
  116. syscall
  117. 2: movl %edx, %eax
  118. xchgl %eax, (%rdi) /* NB: lock is implied */
  119. testl %eax, %eax
  120. jnz 1b
  121. popq %rdx
  122. cfi_adjust_cfa_offset(-8)
  123. cfi_restore(%rdx)
  124. popq %r10
  125. cfi_adjust_cfa_offset(-8)
  126. cfi_restore(%r10)
  127. retq
  128. cfi_endproc
  129. .size __lll_lock_wait,.-__lll_lock_wait
  130. /* %rdi: futex
  131. %rsi: flags
  132. %rdx: timeout
  133. %eax: futex value
  134. */
  135. .globl __lll_timedlock_wait
  136. .type __lll_timedlock_wait,@function
  137. .hidden __lll_timedlock_wait
  138. .align 16
  139. __lll_timedlock_wait:
  140. cfi_startproc
  141. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  142. # ifdef __PIC__
  143. cmpl $0, __have_futex_clock_realtime@GOTOFF(%rip)
  144. # else
  145. cmpl $0, __have_futex_clock_realtime
  146. # endif
  147. je .Lreltmo
  148. # endif
  149. pushq %r9
  150. cfi_adjust_cfa_offset(8)
  151. cfi_rel_offset(%r9, 0)
  152. movq %rdx, %r10
  153. movl $0xffffffff, %r9d
  154. LOAD_FUTEX_WAIT_ABS (%esi)
  155. movl $2, %edx
  156. cmpl %edx, %eax
  157. jne 2f
  158. 1: movl $SYS_futex, %eax
  159. movl $2, %edx
  160. syscall
  161. 2: xchgl %edx, (%rdi) /* NB: lock is implied */
  162. testl %edx, %edx
  163. jz 3f
  164. cmpl $-ETIMEDOUT, %eax
  165. je 4f
  166. cmpl $-EINVAL, %eax
  167. jne 1b
  168. 4: movl %eax, %edx
  169. negl %edx
  170. 3: movl %edx, %eax
  171. popq %r9
  172. cfi_adjust_cfa_offset(-8)
  173. cfi_restore(%r9)
  174. retq
  175. # ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  176. .Lreltmo:
  177. /* Check for a valid timeout value. */
  178. cmpq $1000000000, 8(%rdx)
  179. jae 3f
  180. pushq %r8
  181. cfi_adjust_cfa_offset(8)
  182. pushq %r9
  183. cfi_adjust_cfa_offset(8)
  184. pushq %r12
  185. cfi_adjust_cfa_offset(8)
  186. pushq %r13
  187. cfi_adjust_cfa_offset(8)
  188. pushq %r14
  189. cfi_adjust_cfa_offset(8)
  190. cfi_offset(%r8, -16)
  191. cfi_offset(%r9, -24)
  192. cfi_offset(%r12, -32)
  193. cfi_offset(%r13, -40)
  194. cfi_offset(%r14, -48)
  195. pushq %rsi
  196. cfi_adjust_cfa_offset(8)
  197. /* Stack frame for the timespec and timeval structs. */
  198. subq $24, %rsp
  199. cfi_adjust_cfa_offset(24)
  200. movq %rdi, %r12
  201. movq %rdx, %r13
  202. movl $2, %edx
  203. xchgl %edx, (%r12)
  204. testl %edx, %edx
  205. je 6f
  206. 1:
  207. /* Get current time. */
  208. movq %rsp, %rdi
  209. xorl %esi, %esi
  210. movq $VSYSCALL_ADDR_vgettimeofday, %rax
  211. /* This is a regular function call, all caller-save registers
  212. might be clobbered. */
  213. callq *%rax
  214. /* Compute relative timeout. */
  215. movq 8(%rsp), %rax
  216. movl $1000, %edi
  217. mul %rdi /* Milli seconds to nano seconds. */
  218. movq (%r13), %rdi
  219. movq 8(%r13), %rsi
  220. subq (%rsp), %rdi
  221. subq %rax, %rsi
  222. jns 4f
  223. addq $1000000000, %rsi
  224. decq %rdi
  225. 4: testq %rdi, %rdi
  226. js 2f /* Time is already up. */
  227. /* Store relative timeout. */
  228. movq %rdi, (%rsp)
  229. movq %rsi, 8(%rsp)
  230. /* Futex call. */
  231. movl $2, %edx
  232. movl $1, %eax
  233. movq %rsp, %r10
  234. movl 24(%rsp), %esi
  235. LOAD_FUTEX_WAIT (%esi)
  236. movq %r12, %rdi
  237. movl $SYS_futex, %eax
  238. syscall
  239. /* NB: %edx == 2 */
  240. xchgl %edx, (%r12)
  241. testl %edx, %edx
  242. je 6f
  243. cmpl $-ETIMEDOUT, %eax
  244. jne 1b
  245. 2: movl $ETIMEDOUT, %edx
  246. 6: addq $32, %rsp
  247. cfi_adjust_cfa_offset(-32)
  248. popq %r14
  249. cfi_adjust_cfa_offset(-8)
  250. cfi_restore(%r14)
  251. popq %r13
  252. cfi_adjust_cfa_offset(-8)
  253. cfi_restore(%r13)
  254. popq %r12
  255. cfi_adjust_cfa_offset(-8)
  256. cfi_restore(%r12)
  257. popq %r9
  258. cfi_adjust_cfa_offset(-8)
  259. cfi_restore(%r9)
  260. popq %r8
  261. cfi_adjust_cfa_offset(-8)
  262. cfi_restore(%r8)
  263. movl %edx, %eax
  264. retq
  265. 3: movl $EINVAL, %eax
  266. retq
  267. # endif
  268. cfi_endproc
  269. .size __lll_timedlock_wait,.-__lll_timedlock_wait
  270. #endif
  271. .globl __lll_unlock_wake_private
  272. .type __lll_unlock_wake_private,@function
  273. .hidden __lll_unlock_wake_private
  274. .align 16
  275. __lll_unlock_wake_private:
  276. cfi_startproc
  277. pushq %rsi
  278. cfi_adjust_cfa_offset(8)
  279. pushq %rdx
  280. cfi_adjust_cfa_offset(8)
  281. cfi_offset(%rsi, -16)
  282. cfi_offset(%rdx, -24)
  283. movl $0, (%rdi)
  284. LOAD_PRIVATE_FUTEX_WAKE (%esi)
  285. movl $1, %edx /* Wake one thread. */
  286. movl $SYS_futex, %eax
  287. syscall
  288. popq %rdx
  289. cfi_adjust_cfa_offset(-8)
  290. cfi_restore(%rdx)
  291. popq %rsi
  292. cfi_adjust_cfa_offset(-8)
  293. cfi_restore(%rsi)
  294. retq
  295. cfi_endproc
  296. .size __lll_unlock_wake_private,.-__lll_unlock_wake_private
  297. #ifdef NOT_IN_libc
  298. .globl __lll_unlock_wake
  299. .type __lll_unlock_wake,@function
  300. .hidden __lll_unlock_wake
  301. .align 16
  302. __lll_unlock_wake:
  303. cfi_startproc
  304. pushq %rsi
  305. cfi_adjust_cfa_offset(8)
  306. pushq %rdx
  307. cfi_adjust_cfa_offset(8)
  308. cfi_offset(%rsi, -16)
  309. cfi_offset(%rdx, -24)
  310. movl $0, (%rdi)
  311. LOAD_FUTEX_WAKE (%esi)
  312. movl $1, %edx /* Wake one thread. */
  313. movl $SYS_futex, %eax
  314. syscall
  315. popq %rdx
  316. cfi_adjust_cfa_offset(-8)
  317. cfi_restore(%rdx)
  318. popq %rsi
  319. cfi_adjust_cfa_offset(-8)
  320. cfi_restore(%rsi)
  321. retq
  322. cfi_endproc
  323. .size __lll_unlock_wake,.-__lll_unlock_wake
  324. .globl __lll_timedwait_tid
  325. .type __lll_timedwait_tid,@function
  326. .hidden __lll_timedwait_tid
  327. .align 16
  328. __lll_timedwait_tid:
  329. cfi_startproc
  330. pushq %r12
  331. cfi_adjust_cfa_offset(8)
  332. pushq %r13
  333. cfi_adjust_cfa_offset(8)
  334. cfi_offset(%r12, -16)
  335. cfi_offset(%r13, -24)
  336. movq %rdi, %r12
  337. movq %rsi, %r13
  338. subq $16, %rsp
  339. cfi_adjust_cfa_offset(16)
  340. /* Get current time. */
  341. 2: movq %rsp, %rdi
  342. xorl %esi, %esi
  343. movq $VSYSCALL_ADDR_vgettimeofday, %rax
  344. callq *%rax
  345. /* Compute relative timeout. */
  346. movq 8(%rsp), %rax
  347. movl $1000, %edi
  348. mul %rdi /* Milli seconds to nano seconds. */
  349. movq (%r13), %rdi
  350. movq 8(%r13), %rsi
  351. subq (%rsp), %rdi
  352. subq %rax, %rsi
  353. jns 5f
  354. addq $1000000000, %rsi
  355. decq %rdi
  356. 5: testq %rdi, %rdi
  357. js 6f /* Time is already up. */
  358. movq %rdi, (%rsp) /* Store relative timeout. */
  359. movq %rsi, 8(%rsp)
  360. movl (%r12), %edx
  361. testl %edx, %edx
  362. jz 4f
  363. movq %rsp, %r10
  364. /* XXX The kernel so far uses global futex for the wakeup at
  365. all times. */
  366. #if FUTEX_WAIT == 0
  367. xorl %esi, %esi
  368. #else
  369. movl $FUTEX_WAIT, %esi
  370. #endif
  371. movq %r12, %rdi
  372. movl $SYS_futex, %eax
  373. syscall
  374. cmpl $0, (%rdi)
  375. jne 1f
  376. 4: xorl %eax, %eax
  377. 8: addq $16, %rsp
  378. cfi_adjust_cfa_offset(-16)
  379. popq %r13
  380. cfi_adjust_cfa_offset(-8)
  381. cfi_restore(%r13)
  382. popq %r12
  383. cfi_adjust_cfa_offset(-8)
  384. cfi_restore(%r12)
  385. retq
  386. cfi_adjust_cfa_offset(32)
  387. 1: cmpq $-ETIMEDOUT, %rax
  388. jne 2b
  389. 6: movl $ETIMEDOUT, %eax
  390. jmp 8b
  391. cfi_endproc
  392. .size __lll_timedwait_tid,.-__lll_timedwait_tid
  393. #endif