1 /* Copyright (C) 2002-2004, 2006-2008, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LOWLEVELLOCK_H
20 #define _LOWLEVELLOCK_H 1
24 # include <sys/param.h>
25 # include <bits/pthreadtypes.h>
26 # include <bits/kernel-features.h>
27 # include <tcb-offsets.h>
31 # define LOCK_INSTR /* nothing */
33 # define LOCK_INSTR "lock;"
48 #define FUTEX_CMP_REQUEUE 4
49 #define FUTEX_WAKE_OP 5
50 #define FUTEX_LOCK_PI 6
51 #define FUTEX_UNLOCK_PI 7
52 #define FUTEX_TRYLOCK_PI 8
53 #define FUTEX_WAIT_BITSET 9
54 #define FUTEX_WAKE_BITSET 10
55 #define FUTEX_WAIT_REQUEUE_PI 11
56 #define FUTEX_CMP_REQUEUE_PI 12
57 #define FUTEX_PRIVATE_FLAG 128
58 #define FUTEX_CLOCK_REALTIME 256
60 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
62 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
64 /* Values for 'private' parameter of locking macros. Yes, the
65 definition seems to be backwards. But it is not. The bit will be
66 reversed before passing to the system call. */
68 #define LLL_SHARED FUTEX_PRIVATE_FLAG
72 #if !defined NOT_IN_libc || defined IS_IN_rtld
73 /* In libc.so or ld.so all futexes are private. */
74 # ifdef __ASSUME_PRIVATE_FUTEX
75 # define __lll_private_flag(fl, private) \
76 ((fl) | FUTEX_PRIVATE_FLAG)
78 # define __lll_private_flag(fl, private) \
79 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
82 # ifdef __ASSUME_PRIVATE_FUTEX
83 # define __lll_private_flag(fl, private) \
84 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
86 # define __lll_private_flag(fl, private) \
87 (__builtin_constant_p (private) \
89 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
91 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
92 __asm__ ("andl %%fs:%P1, %0" : "+r" (__fl) \
93 : "i" (offsetof (struct pthread, header.private_futex))); \
98 /* Initializer for lock. */
99 #define LLL_LOCK_INITIALIZER (0)
100 #define LLL_LOCK_INITIALIZER_LOCKED (1)
101 #define LLL_LOCK_INITIALIZER_WAITERS (2)
103 /* Delay in spinlock loop. */
104 #define BUSY_WAIT_NOP __asm__ ("rep; nop")
107 #define LLL_STUB_UNWIND_INFO_START \
108 ".section .eh_frame,\"a\",@progbits\n" \
109 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
110 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
111 ".byte 0x1 # CIE Version\n\t" \
112 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
113 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
114 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
115 ".byte 0x10 # CIE RA Column\n\t" \
116 ".uleb128 0x1 # Augmentation size\n\t" \
117 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
118 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
122 "9:\t" ".long 23f-10f # FDE Length\n" \
123 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
124 ".long 1b-. # FDE initial location\n\t" \
125 ".long 6b-1b # FDE address range\n\t" \
126 ".uleb128 0x0 # Augmentation size\n\t" \
127 ".byte 0x16 # DW_CFA_val_expression\n\t" \
128 ".uleb128 0x10\n\t" \
129 ".uleb128 12f-11f\n" \
130 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
132 #define LLL_STUB_UNWIND_INFO_END \
133 ".byte 0x16 # DW_CFA_val_expression\n\t" \
134 ".uleb128 0x10\n\t" \
135 ".uleb128 14f-13f\n" \
136 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
138 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
139 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
141 ".byte 0x16 # DW_CFA_val_expression\n\t" \
142 ".uleb128 0x10\n\t" \
143 ".uleb128 16f-15f\n" \
144 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
146 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
147 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
149 ".byte 0x16 # DW_CFA_val_expression\n\t" \
150 ".uleb128 0x10\n\t" \
151 ".uleb128 20f-17f\n" \
152 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
153 ".sleb128 19f-18f\n\t" \
154 ".byte 0x0d # DW_OP_const4s\n" \
155 "18:\t" ".4byte 4b-.\n\t" \
156 ".byte 0x1c # DW_OP_minus\n\t" \
157 ".byte 0x0d # DW_OP_const4s\n" \
158 "19:\t" ".4byte 24f-.\n\t" \
159 ".byte 0x22 # DW_OP_plus\n" \
160 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
161 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
163 ".byte 0x16 # DW_CFA_val_expression\n\t" \
164 ".uleb128 0x10\n\t" \
165 ".uleb128 22f-21f\n" \
166 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
168 "22:\t" ".align 8\n" \
169 "23:\t" ".previous\n"
179 #define LLL_STUB_UNWIND_INFO_5 \
180 LLL_STUB_UNWIND_INFO_START \
181 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
182 LLL_STUB_UNWIND_INFO_END
193 #define LLL_STUB_UNWIND_INFO_6 \
194 LLL_STUB_UNWIND_INFO_START \
195 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
196 ".byte 0x16 # DW_CFA_val_expression\n\t" \
197 ".uleb128 0x10\n\t" \
198 ".uleb128 26f-25f\n" \
199 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
201 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
202 LLL_STUB_UNWIND_INFO_END
205 #define lll_futex_wait(futex, val, private) \
206 lll_futex_timed_wait(futex, val, NULL, private)
209 #define lll_futex_timed_wait(futex, val, timeout, private) \
211 register const struct timespec *__to __asm__ ("r10") = timeout; \
213 register __typeof (val) _val __asm__ ("edx") = (val); \
214 __asm__ __volatile__ ("syscall" \
216 : "0" (SYS_futex), "D" (futex), \
217 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
218 "d" (_val), "r" (__to) \
219 : "memory", "cc", "r11", "cx"); \
224 #define lll_futex_wake(futex, nr, private) \
227 register __typeof (nr) _nr __asm__ ("edx") = (nr); \
228 __asm__ __volatile__ ("syscall" \
230 : "0" (SYS_futex), "D" (futex), \
231 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
233 : "memory", "cc", "r10", "r11", "cx"); \
237 /* NB: in the lll_trylock macro we simply return the value in %eax
238 after the cmpxchg instruction. In case the operation succeded this
239 value is zero. In case the operation failed, the cmpxchg instruction
240 has loaded the current value of the memory work which is guaranteed
242 #if defined NOT_IN_libc || defined UP
243 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
245 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
247 "lock; cmpxchgl %2, %1\n\t" \
249 "0:\tcmpxchgl %2, %1\n\t" \
253 #define lll_trylock(futex) \
255 __asm__ __volatile__ (__lll_trylock_asm \
256 : "=a" (ret), "=m" (futex) \
257 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
258 "0" (LLL_LOCK_INITIALIZER) \
262 #define lll_robust_trylock(futex, id) \
264 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %2, %1" \
265 : "=a" (ret), "=m" (futex) \
266 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
270 #define lll_cond_trylock(futex) \
272 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %2, %1" \
273 : "=a" (ret), "=m" (futex) \
274 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
275 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
279 #if defined NOT_IN_libc || defined UP
280 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
283 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
285 "lock; cmpxchgl %4, %2\n\t" \
288 "0:\tcmpxchgl %4, %2\n\t" \
292 #define lll_lock(futex, private) \
294 ({ int ignore1, ignore2, ignore3; \
295 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
296 __asm__ __volatile__ (__lll_lock_asm_start \
297 ".subsection 1\n\t" \
298 ".type _L_lock_%=, @function\n" \
300 "1:\tleaq %2, %%rdi\n" \
301 "2:\tsubq $128, %%rsp\n" \
302 "3:\tcallq __lll_lock_wait_private\n" \
303 "4:\taddq $128, %%rsp\n" \
305 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
307 LLL_STUB_UNWIND_INFO_5 \
309 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
311 : "0" (1), "m" (futex), "3" (0) \
312 : "cx", "r11", "cc", "memory"); \
314 __asm__ __volatile__ (__lll_lock_asm_start \
315 ".subsection 1\n\t" \
316 ".type _L_lock_%=, @function\n" \
318 "1:\tleaq %2, %%rdi\n" \
319 "2:\tsubq $128, %%rsp\n" \
320 "3:\tcallq __lll_lock_wait\n" \
321 "4:\taddq $128, %%rsp\n" \
323 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
325 LLL_STUB_UNWIND_INFO_5 \
327 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
329 : "1" (1), "m" (futex), "3" (0), "0" (private) \
330 : "cx", "r11", "cc", "memory"); \
333 #define lll_robust_lock(futex, id, private) \
334 ({ int __ret, ignore1, ignore2; \
335 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
337 ".subsection 1\n\t" \
338 ".type _L_robust_lock_%=, @function\n" \
339 "_L_robust_lock_%=:\n" \
340 "1:\tleaq %2, %%rdi\n" \
341 "2:\tsubq $128, %%rsp\n" \
342 "3:\tcallq __lll_robust_lock_wait\n" \
343 "4:\taddq $128, %%rsp\n" \
345 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
347 LLL_STUB_UNWIND_INFO_5 \
349 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
351 : "1" (id), "m" (futex), "3" (0), "0" (private) \
352 : "cx", "r11", "cc", "memory"); \
355 #define lll_cond_lock(futex, private) \
357 ({ int ignore1, ignore2, ignore3; \
358 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
360 ".subsection 1\n\t" \
361 ".type _L_cond_lock_%=, @function\n" \
362 "_L_cond_lock_%=:\n" \
363 "1:\tleaq %2, %%rdi\n" \
364 "2:\tsubq $128, %%rsp\n" \
365 "3:\tcallq __lll_lock_wait\n" \
366 "4:\taddq $128, %%rsp\n" \
368 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
370 LLL_STUB_UNWIND_INFO_5 \
372 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
374 : "1" (2), "m" (futex), "3" (0), "0" (private) \
375 : "cx", "r11", "cc", "memory"); \
378 #define lll_robust_cond_lock(futex, id, private) \
379 ({ int __ret, ignore1, ignore2; \
380 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
382 ".subsection 1\n\t" \
383 ".type _L_robust_cond_lock_%=, @function\n" \
384 "_L_robust_cond_lock_%=:\n" \
385 "1:\tleaq %2, %%rdi\n" \
386 "2:\tsubq $128, %%rsp\n" \
387 "3:\tcallq __lll_robust_lock_wait\n" \
388 "4:\taddq $128, %%rsp\n" \
390 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
392 LLL_STUB_UNWIND_INFO_5 \
394 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
396 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
398 : "cx", "r11", "cc", "memory"); \
401 #define lll_timedlock(futex, timeout, private) \
402 ({ int __ret, ignore1, ignore2, ignore3; \
403 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
405 ".subsection 1\n\t" \
406 ".type _L_timedlock_%=, @function\n" \
407 "_L_timedlock_%=:\n" \
408 "1:\tleaq %4, %%rdi\n" \
409 "0:\tmovq %8, %%rdx\n" \
410 "2:\tsubq $128, %%rsp\n" \
411 "3:\tcallq __lll_timedlock_wait\n" \
412 "4:\taddq $128, %%rsp\n" \
414 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
416 LLL_STUB_UNWIND_INFO_6 \
418 : "=a" (__ret), "=D" (ignore1), "=S" (ignore2), \
419 "=&d" (ignore3), "=m" (futex) \
420 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
422 : "memory", "cx", "cc", "r10", "r11"); \
425 #define lll_robust_timedlock(futex, timeout, id, private) \
426 ({ int __ret, ignore1, ignore2, ignore3; \
427 __asm__ __volatile__ (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
429 ".subsection 1\n\t" \
430 ".type _L_robust_timedlock_%=, @function\n" \
431 "_L_robust_timedlock_%=:\n" \
432 "1:\tleaq %4, %%rdi\n" \
433 "0:\tmovq %8, %%rdx\n" \
434 "2:\tsubq $128, %%rsp\n" \
435 "3:\tcallq __lll_robust_timedlock_wait\n" \
436 "4:\taddq $128, %%rsp\n" \
438 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
440 LLL_STUB_UNWIND_INFO_6 \
442 : "=a" (__ret), "=D" (ignore1), "=S" (ignore2), \
443 "=&d" (ignore3), "=m" (futex) \
444 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
446 : "memory", "cx", "cc", "r10", "r11"); \
449 #if defined NOT_IN_libc || defined UP
450 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
453 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
455 "lock; decl %0\n\t" \
462 #define lll_unlock(futex, private) \
465 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
466 __asm__ __volatile__ (__lll_unlock_asm_start \
467 ".subsection 1\n\t" \
468 ".type _L_unlock_%=, @function\n" \
470 "1:\tleaq %0, %%rdi\n" \
471 "2:\tsubq $128, %%rsp\n" \
472 "3:\tcallq __lll_unlock_wake_private\n" \
473 "4:\taddq $128, %%rsp\n" \
475 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
477 LLL_STUB_UNWIND_INFO_5 \
479 : "=m" (futex), "=&D" (ignore) \
481 : "ax", "cx", "r11", "cc", "memory"); \
483 __asm__ __volatile__ (__lll_unlock_asm_start \
484 ".subsection 1\n\t" \
485 ".type _L_unlock_%=, @function\n" \
487 "1:\tleaq %0, %%rdi\n" \
488 "2:\tsubq $128, %%rsp\n" \
489 "3:\tcallq __lll_unlock_wake\n" \
490 "4:\taddq $128, %%rsp\n" \
492 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
494 LLL_STUB_UNWIND_INFO_5 \
496 : "=m" (futex), "=&D" (ignore) \
497 : "m" (futex), "S" (private) \
498 : "ax", "cx", "r11", "cc", "memory"); \
501 #define lll_robust_unlock(futex, private) \
505 __asm__ __volatile__ (LOCK_INSTR "andl %2, %0\n\t" \
507 ".subsection 1\n\t" \
508 ".type _L_robust_unlock_%=, @function\n" \
509 "_L_robust_unlock_%=:\n" \
510 "1:\tleaq %0, %%rdi\n" \
511 "2:\tsubq $128, %%rsp\n" \
512 "3:\tcallq __lll_unlock_wake\n" \
513 "4:\taddq $128, %%rsp\n" \
515 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
517 LLL_STUB_UNWIND_INFO_5 \
519 : "=m" (futex), "=&D" (ignore) \
520 : "i" (FUTEX_WAITERS), "m" (futex), \
522 : "ax", "cx", "r11", "cc", "memory"); \
526 #define lll_robust_dead(futex, private) \
530 __asm__ __volatile__ (LOCK_INSTR "orl %3, (%2)\n\t" \
532 : "=m" (futex), "=a" (ignore) \
533 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
534 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
535 "1" (__NR_futex), "d" (1) \
536 : "cx", "r11", "cc", "memory"); \
540 /* Returns non-zero if error happened, zero if success. */
541 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
543 register int __nr_move __asm__ ("r10") = nr_move; \
544 register void *__mutex __asm__ ("r8") = mutex; \
545 register int __val __asm__ ("r9") = val; \
546 __asm__ __volatile__ ("syscall" \
548 : "0" (__NR_futex), "D" ((void *) ftx), \
549 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
550 private)), "d" (nr_wake), \
551 "r" (__nr_move), "r" (__mutex), "r" (__val) \
552 : "cx", "r11", "cc", "memory"); \
555 #define lll_islocked(futex) \
556 (futex != LLL_LOCK_INITIALIZER)
559 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
560 wakeup when the clone terminates. The memory location contains the
561 thread ID while the clone is running and is reset to zero
564 The macro parameter must not have any side effect. */
565 #define lll_wait_tid(tid) \
568 register __typeof (tid) _tid __asm__ ("edx") = (tid); \
570 __asm__ __volatile__ ("xorq %%r10, %%r10\n\t" \
571 "1:\tmovq %2, %%rax\n\t" \
573 "cmpl $0, (%%rdi)\n\t" \
576 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
578 : "memory", "cc", "r10", "r11", "cx"); \
581 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
583 #define lll_timedwait_tid(tid, abstime) \
588 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
591 __ret = __lll_timedwait_tid (&tid, abstime); \
595 #endif /* !__ASSEMBLER__ */
597 #endif /* lowlevellock.h */