1 /* Copyright (C) 2002-2013 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
22 #include <stap-probe.h>
26 # include <sys/param.h>
27 # include <bits/pthreadtypes.h>
28 # include <kernel-features.h>
29 # include <tcb-offsets.h>
33 # define LOCK_INSTR /* nothing */
35 # define LOCK_INSTR "lock;"
48 #define SYS_futex __NR_futex
51 #define FUTEX_CMP_REQUEUE 4
52 #define FUTEX_WAKE_OP 5
53 #define FUTEX_LOCK_PI 6
54 #define FUTEX_UNLOCK_PI 7
55 #define FUTEX_TRYLOCK_PI 8
56 #define FUTEX_WAIT_BITSET 9
57 #define FUTEX_WAKE_BITSET 10
58 #define FUTEX_WAIT_REQUEUE_PI 11
59 #define FUTEX_CMP_REQUEUE_PI 12
60 #define FUTEX_PRIVATE_FLAG 128
61 #define FUTEX_CLOCK_REALTIME 256
63 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
65 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
67 /* Values for 'private' parameter of locking macros. Yes, the
68 definition seems to be backwards. But it is not. The bit will be
69 reversed before passing to the system call. */
71 #define LLL_SHARED FUTEX_PRIVATE_FLAG
75 #if !defined NOT_IN_libc || defined IS_IN_rtld
76 /* In libc.so or ld.so all futexes are private. */
77 # ifdef __ASSUME_PRIVATE_FUTEX
78 # define __lll_private_flag(fl, private) \
79 ((fl) | FUTEX_PRIVATE_FLAG)
81 # define __lll_private_flag(fl, private) \
82 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
85 # ifdef __ASSUME_PRIVATE_FUTEX
86 # define __lll_private_flag(fl, private) \
87 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
89 # define __lll_private_flag(fl, private) \
90 (__builtin_constant_p (private) \
92 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
94 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
95 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
96 : "i" (offsetof (struct pthread, header.private_futex))); \
101 /* Initializer for lock. */
102 #define LLL_LOCK_INITIALIZER (0)
103 #define LLL_LOCK_INITIALIZER_LOCKED (1)
104 #define LLL_LOCK_INITIALIZER_WAITERS (2)
106 /* Delay in spinlock loop. */
107 #define BUSY_WAIT_NOP asm ("rep; nop")
110 #define LLL_STUB_UNWIND_INFO_START \
111 ".section .eh_frame,\"a\",@progbits\n" \
112 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
113 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
114 ".byte 0x1 # CIE Version\n\t" \
115 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
116 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
117 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
118 ".byte 0x10 # CIE RA Column\n\t" \
119 ".uleb128 0x1 # Augmentation size\n\t" \
120 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
121 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
124 ".align " LP_SIZE "\n" \
125 "9:\t" ".long 23f-10f # FDE Length\n" \
126 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
127 ".long 1b-. # FDE initial location\n\t" \
128 ".long 6b-1b # FDE address range\n\t" \
129 ".uleb128 0x0 # Augmentation size\n\t" \
130 ".byte 0x16 # DW_CFA_val_expression\n\t" \
131 ".uleb128 0x10\n\t" \
132 ".uleb128 12f-11f\n" \
133 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
135 #define LLL_STUB_UNWIND_INFO_END \
136 ".byte 0x16 # DW_CFA_val_expression\n\t" \
137 ".uleb128 0x10\n\t" \
138 ".uleb128 14f-13f\n" \
139 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
141 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
142 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
144 ".byte 0x16 # DW_CFA_val_expression\n\t" \
145 ".uleb128 0x10\n\t" \
146 ".uleb128 16f-15f\n" \
147 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
149 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
150 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
152 ".byte 0x16 # DW_CFA_val_expression\n\t" \
153 ".uleb128 0x10\n\t" \
154 ".uleb128 20f-17f\n" \
155 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
156 ".sleb128 19f-18f\n\t" \
157 ".byte 0x0d # DW_OP_const4s\n" \
158 "18:\t" ".4byte 4b-.\n\t" \
159 ".byte 0x1c # DW_OP_minus\n\t" \
160 ".byte 0x0d # DW_OP_const4s\n" \
161 "19:\t" ".4byte 24f-.\n\t" \
162 ".byte 0x22 # DW_OP_plus\n" \
163 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
164 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
166 ".byte 0x16 # DW_CFA_val_expression\n\t" \
167 ".uleb128 0x10\n\t" \
168 ".uleb128 22f-21f\n" \
169 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
171 "22:\t" ".align " LP_SIZE "\n" \
172 "23:\t" ".previous\n"
182 #define LLL_STUB_UNWIND_INFO_5 \
183 LLL_STUB_UNWIND_INFO_START \
184 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
185 LLL_STUB_UNWIND_INFO_END
196 #define LLL_STUB_UNWIND_INFO_6 \
197 LLL_STUB_UNWIND_INFO_START \
198 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
199 ".byte 0x16 # DW_CFA_val_expression\n\t" \
200 ".uleb128 0x10\n\t" \
201 ".uleb128 26f-25f\n" \
202 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
204 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
205 LLL_STUB_UNWIND_INFO_END
208 #define lll_futex_wait(futex, val, private) \
209 lll_futex_timed_wait(futex, val, NULL, private)
212 #define lll_futex_timed_wait(futex, val, timeout, private) \
214 register const struct timespec *__to __asm ("r10") = timeout; \
216 register __typeof (val) _val __asm ("edx") = (val); \
217 __asm __volatile ("syscall" \
219 : "0" (SYS_futex), "D" (futex), \
220 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
221 "d" (_val), "r" (__to) \
222 : "memory", "cc", "r11", "cx"); \
227 #define lll_futex_wake(futex, nr, private) \
230 register __typeof (nr) _nr __asm ("edx") = (nr); \
231 LIBC_PROBE (lll_futex_wake, 3, futex, nr, private); \
232 __asm __volatile ("syscall" \
234 : "0" (SYS_futex), "D" (futex), \
235 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
237 : "memory", "cc", "r10", "r11", "cx"); \
241 /* NB: in the lll_trylock macro we simply return the value in %eax
242 after the cmpxchg instruction. In case the operation succeded this
243 value is zero. In case the operation failed, the cmpxchg instruction
244 has loaded the current value of the memory work which is guaranteed
246 #if defined NOT_IN_libc || defined UP
247 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
249 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
251 "lock; cmpxchgl %2, %1\n\t" \
253 "0:\tcmpxchgl %2, %1\n\t" \
257 #define lll_trylock(futex) \
259 __asm __volatile (__lll_trylock_asm \
260 : "=a" (ret), "=m" (futex) \
261 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
262 "0" (LLL_LOCK_INITIALIZER) \
266 #define lll_robust_trylock(futex, id) \
268 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
269 : "=a" (ret), "=m" (futex) \
270 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
274 #define lll_cond_trylock(futex) \
276 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
277 : "=a" (ret), "=m" (futex) \
278 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
279 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
283 #if defined NOT_IN_libc || defined UP
284 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
287 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
289 "lock; cmpxchgl %4, %2\n\t" \
292 "0:\tcmpxchgl %4, %2\n\t" \
296 #define lll_lock(futex, private) \
298 ({ int ignore1, ignore2, ignore3; \
299 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
300 __asm __volatile (__lll_lock_asm_start \
301 ".subsection 1\n\t" \
302 ".type _L_lock_%=, @function\n" \
304 "1:\tlea %2, %%" RDI_LP "\n" \
305 "2:\tsub $128, %%" RSP_LP "\n" \
306 "3:\tcallq __lll_lock_wait_private\n" \
307 "4:\tadd $128, %%" RSP_LP "\n" \
309 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
311 LLL_STUB_UNWIND_INFO_5 \
313 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
315 : "0" (1), "m" (futex), "3" (0) \
316 : "cx", "r11", "cc", "memory"); \
318 __asm __volatile (__lll_lock_asm_start \
319 ".subsection 1\n\t" \
320 ".type _L_lock_%=, @function\n" \
322 "1:\tlea %2, %%" RDI_LP "\n" \
323 "2:\tsub $128, %%" RSP_LP "\n" \
324 "3:\tcallq __lll_lock_wait\n" \
325 "4:\tadd $128, %%" RSP_LP "\n" \
327 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
329 LLL_STUB_UNWIND_INFO_5 \
331 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
333 : "1" (1), "m" (futex), "3" (0), "0" (private) \
334 : "cx", "r11", "cc", "memory"); \
337 #define lll_robust_lock(futex, id, private) \
338 ({ int result, ignore1, ignore2; \
339 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
341 ".subsection 1\n\t" \
342 ".type _L_robust_lock_%=, @function\n" \
343 "_L_robust_lock_%=:\n" \
344 "1:\tlea %2, %%" RDI_LP "\n" \
345 "2:\tsub $128, %%" RSP_LP "\n" \
346 "3:\tcallq __lll_robust_lock_wait\n" \
347 "4:\tadd $128, %%" RSP_LP "\n" \
349 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
351 LLL_STUB_UNWIND_INFO_5 \
353 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
355 : "1" (id), "m" (futex), "3" (0), "0" (private) \
356 : "cx", "r11", "cc", "memory"); \
359 #define lll_cond_lock(futex, private) \
361 ({ int ignore1, ignore2, ignore3; \
362 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
364 ".subsection 1\n\t" \
365 ".type _L_cond_lock_%=, @function\n" \
366 "_L_cond_lock_%=:\n" \
367 "1:\tlea %2, %%" RDI_LP "\n" \
368 "2:\tsub $128, %%" RSP_LP "\n" \
369 "3:\tcallq __lll_lock_wait\n" \
370 "4:\tadd $128, %%" RSP_LP "\n" \
372 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
374 LLL_STUB_UNWIND_INFO_5 \
376 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
378 : "1" (2), "m" (futex), "3" (0), "0" (private) \
379 : "cx", "r11", "cc", "memory"); \
382 #define lll_robust_cond_lock(futex, id, private) \
383 ({ int result, ignore1, ignore2; \
384 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
386 ".subsection 1\n\t" \
387 ".type _L_robust_cond_lock_%=, @function\n" \
388 "_L_robust_cond_lock_%=:\n" \
389 "1:\tlea %2, %%" RDI_LP "\n" \
390 "2:\tsub $128, %%" RSP_LP "\n" \
391 "3:\tcallq __lll_robust_lock_wait\n" \
392 "4:\tadd $128, %%" RSP_LP "\n" \
394 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
396 LLL_STUB_UNWIND_INFO_5 \
398 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
400 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
402 : "cx", "r11", "cc", "memory"); \
405 #define lll_timedlock(futex, timeout, private) \
406 ({ int result, ignore1, ignore2, ignore3; \
407 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
409 ".subsection 1\n\t" \
410 ".type _L_timedlock_%=, @function\n" \
411 "_L_timedlock_%=:\n" \
412 "1:\tlea %4, %%" RDI_LP "\n" \
413 "0:\tmov %8, %%" RDX_LP "\n" \
414 "2:\tsub $128, %%" RSP_LP "\n" \
415 "3:\tcallq __lll_timedlock_wait\n" \
416 "4:\tadd $128, %%" RSP_LP "\n" \
418 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
420 LLL_STUB_UNWIND_INFO_6 \
422 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
423 "=&d" (ignore3), "=m" (futex) \
424 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
426 : "memory", "cx", "cc", "r10", "r11"); \
429 #define lll_robust_timedlock(futex, timeout, id, private) \
430 ({ int result, ignore1, ignore2, ignore3; \
431 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
433 ".subsection 1\n\t" \
434 ".type _L_robust_timedlock_%=, @function\n" \
435 "_L_robust_timedlock_%=:\n" \
436 "1:\tlea %4, %%" RDI_LP "\n" \
437 "0:\tmov %8, %%" RDX_LP "\n" \
438 "2:\tsub $128, %%" RSP_LP "\n" \
439 "3:\tcallq __lll_robust_timedlock_wait\n" \
440 "4:\tadd $128, %%" RSP_LP "\n" \
442 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
444 LLL_STUB_UNWIND_INFO_6 \
446 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
447 "=&d" (ignore3), "=m" (futex) \
448 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
450 : "memory", "cx", "cc", "r10", "r11"); \
453 #if defined NOT_IN_libc || defined UP
454 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
457 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
459 "lock; decl %0\n\t" \
466 #define lll_unlock(futex, private) \
469 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
470 __asm __volatile (__lll_unlock_asm_start \
471 ".subsection 1\n\t" \
472 ".type _L_unlock_%=, @function\n" \
474 "1:\tlea %0, %%" RDI_LP "\n" \
475 "2:\tsub $128, %%" RSP_LP "\n" \
476 "3:\tcallq __lll_unlock_wake_private\n" \
477 "4:\tadd $128, %%" RSP_LP "\n" \
479 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
481 LLL_STUB_UNWIND_INFO_5 \
483 : "=m" (futex), "=&D" (ignore) \
485 : "ax", "cx", "r11", "cc", "memory"); \
487 __asm __volatile (__lll_unlock_asm_start \
488 ".subsection 1\n\t" \
489 ".type _L_unlock_%=, @function\n" \
491 "1:\tlea %0, %%" RDI_LP "\n" \
492 "2:\tsub $128, %%" RSP_LP "\n" \
493 "3:\tcallq __lll_unlock_wake\n" \
494 "4:\tadd $128, %%" RSP_LP "\n" \
496 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
498 LLL_STUB_UNWIND_INFO_5 \
500 : "=m" (futex), "=&D" (ignore) \
501 : "m" (futex), "S" (private) \
502 : "ax", "cx", "r11", "cc", "memory"); \
505 #define lll_robust_unlock(futex, private) \
509 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
511 ".subsection 1\n\t" \
512 ".type _L_robust_unlock_%=, @function\n" \
513 "_L_robust_unlock_%=:\n" \
514 "1:\tlea %0, %%" RDI_LP "\n" \
515 "2:\tsub $128, %%" RSP_LP "\n" \
516 "3:\tcallq __lll_unlock_wake\n" \
517 "4:\tadd $128, %%" RSP_LP "\n" \
519 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
521 LLL_STUB_UNWIND_INFO_5 \
523 : "=m" (futex), "=&D" (ignore) \
524 : "i" (FUTEX_WAITERS), "m" (futex), \
526 : "ax", "cx", "r11", "cc", "memory"); \
530 #define lll_robust_dead(futex, private) \
534 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
536 : "=m" (futex), "=a" (ignore) \
537 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
538 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
539 "1" (__NR_futex), "d" (1) \
540 : "cx", "r11", "cc", "memory"); \
544 /* Returns non-zero if error happened, zero if success. */
545 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
547 register int __nr_move __asm ("r10") = nr_move; \
548 register void *__mutex __asm ("r8") = mutex; \
549 register int __val __asm ("r9") = val; \
550 __asm __volatile ("syscall" \
552 : "0" (__NR_futex), "D" ((void *) ftx), \
553 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
554 private)), "d" (nr_wake), \
555 "r" (__nr_move), "r" (__mutex), "r" (__val) \
556 : "cx", "r11", "cc", "memory"); \
559 #define lll_islocked(futex) \
560 (futex != LLL_LOCK_INITIALIZER)
563 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
564 wakeup when the clone terminates. The memory location contains the
565 thread ID while the clone is running and is reset to zero
568 The macro parameter must not have any side effect. */
569 #define lll_wait_tid(tid) \
572 register __typeof (tid) _tid asm ("edx") = (tid); \
574 __asm __volatile ("xorq %%r10, %%r10\n\t" \
575 "1:\tmovq %2, %%rax\n\t" \
577 "cmpl $0, (%%rdi)\n\t" \
580 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
582 : "memory", "cc", "r10", "r11", "cx"); \
585 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
587 #define lll_timedwait_tid(tid, abstime) \
592 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
595 __result = __lll_timedwait_tid (&tid, abstime); \
599 #endif /* !__ASSEMBLER__ */
601 #endif /* lowlevellock.h */