1 /* Copyright (C) 2002-2014 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"); \
242 /* NB: in the lll_trylock macro we simply return the value in %eax
243 after the cmpxchg instruction. In case the operation succeded this
244 value is zero. In case the operation failed, the cmpxchg instruction
245 has loaded the current value of the memory work which is guaranteed
247 #if defined NOT_IN_libc || defined UP
248 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
250 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
252 "lock; cmpxchgl %2, %1\n\t" \
254 "0:\tcmpxchgl %2, %1\n\t" \
258 #define lll_trylock(futex) \
260 __asm __volatile (__lll_trylock_asm \
261 : "=a" (ret), "=m" (futex) \
262 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
263 "0" (LLL_LOCK_INITIALIZER) \
267 #define lll_robust_trylock(futex, id) \
269 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
270 : "=a" (ret), "=m" (futex) \
271 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
275 #define lll_cond_trylock(futex) \
277 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
278 : "=a" (ret), "=m" (futex) \
279 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
280 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
284 #if defined NOT_IN_libc || defined UP
285 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
288 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
290 "lock; cmpxchgl %4, %2\n\t" \
293 "0:\tcmpxchgl %4, %2\n\t" \
297 #define lll_lock(futex, private) \
299 ({ int ignore1, ignore2, ignore3; \
300 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
301 __asm __volatile (__lll_lock_asm_start \
302 ".subsection 1\n\t" \
303 ".type _L_lock_%=, @function\n" \
305 "1:\tlea %2, %%" RDI_LP "\n" \
306 "2:\tsub $128, %%" RSP_LP "\n" \
307 "3:\tcallq __lll_lock_wait_private\n" \
308 "4:\tadd $128, %%" RSP_LP "\n" \
310 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
312 LLL_STUB_UNWIND_INFO_5 \
314 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
316 : "0" (1), "m" (futex), "3" (0) \
317 : "cx", "r11", "cc", "memory"); \
319 __asm __volatile (__lll_lock_asm_start \
320 ".subsection 1\n\t" \
321 ".type _L_lock_%=, @function\n" \
323 "1:\tlea %2, %%" RDI_LP "\n" \
324 "2:\tsub $128, %%" RSP_LP "\n" \
325 "3:\tcallq __lll_lock_wait\n" \
326 "4:\tadd $128, %%" RSP_LP "\n" \
328 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
330 LLL_STUB_UNWIND_INFO_5 \
332 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
334 : "1" (1), "m" (futex), "3" (0), "0" (private) \
335 : "cx", "r11", "cc", "memory"); \
338 #define lll_robust_lock(futex, id, private) \
339 ({ int result, ignore1, ignore2; \
340 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
342 ".subsection 1\n\t" \
343 ".type _L_robust_lock_%=, @function\n" \
344 "_L_robust_lock_%=:\n" \
345 "1:\tlea %2, %%" RDI_LP "\n" \
346 "2:\tsub $128, %%" RSP_LP "\n" \
347 "3:\tcallq __lll_robust_lock_wait\n" \
348 "4:\tadd $128, %%" RSP_LP "\n" \
350 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
352 LLL_STUB_UNWIND_INFO_5 \
354 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
356 : "1" (id), "m" (futex), "3" (0), "0" (private) \
357 : "cx", "r11", "cc", "memory"); \
360 #define lll_cond_lock(futex, private) \
362 ({ int ignore1, ignore2, ignore3; \
363 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
365 ".subsection 1\n\t" \
366 ".type _L_cond_lock_%=, @function\n" \
367 "_L_cond_lock_%=:\n" \
368 "1:\tlea %2, %%" RDI_LP "\n" \
369 "2:\tsub $128, %%" RSP_LP "\n" \
370 "3:\tcallq __lll_lock_wait\n" \
371 "4:\tadd $128, %%" RSP_LP "\n" \
373 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
375 LLL_STUB_UNWIND_INFO_5 \
377 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
379 : "1" (2), "m" (futex), "3" (0), "0" (private) \
380 : "cx", "r11", "cc", "memory"); \
383 #define lll_robust_cond_lock(futex, id, private) \
384 ({ int result, ignore1, ignore2; \
385 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
387 ".subsection 1\n\t" \
388 ".type _L_robust_cond_lock_%=, @function\n" \
389 "_L_robust_cond_lock_%=:\n" \
390 "1:\tlea %2, %%" RDI_LP "\n" \
391 "2:\tsub $128, %%" RSP_LP "\n" \
392 "3:\tcallq __lll_robust_lock_wait\n" \
393 "4:\tadd $128, %%" RSP_LP "\n" \
395 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
397 LLL_STUB_UNWIND_INFO_5 \
399 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
401 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
403 : "cx", "r11", "cc", "memory"); \
406 #define lll_timedlock(futex, timeout, private) \
407 ({ int result, ignore1, ignore2, ignore3; \
408 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
410 ".subsection 1\n\t" \
411 ".type _L_timedlock_%=, @function\n" \
412 "_L_timedlock_%=:\n" \
413 "1:\tlea %4, %%" RDI_LP "\n" \
414 "0:\tmov %8, %%" RDX_LP "\n" \
415 "2:\tsub $128, %%" RSP_LP "\n" \
416 "3:\tcallq __lll_timedlock_wait\n" \
417 "4:\tadd $128, %%" RSP_LP "\n" \
419 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
421 LLL_STUB_UNWIND_INFO_6 \
423 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
424 "=&d" (ignore3), "=m" (futex) \
425 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
427 : "memory", "cx", "cc", "r10", "r11"); \
430 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
431 const struct timespec
*timeout
,
432 int private) attribute_hidden
;
434 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
435 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
437 #define lll_robust_timedlock(futex, timeout, id, private) \
438 ({ int result, ignore1, ignore2, ignore3; \
439 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
441 ".subsection 1\n\t" \
442 ".type _L_robust_timedlock_%=, @function\n" \
443 "_L_robust_timedlock_%=:\n" \
444 "1:\tlea %4, %%" RDI_LP "\n" \
445 "0:\tmov %8, %%" RDX_LP "\n" \
446 "2:\tsub $128, %%" RSP_LP "\n" \
447 "3:\tcallq __lll_robust_timedlock_wait\n" \
448 "4:\tadd $128, %%" RSP_LP "\n" \
450 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
452 LLL_STUB_UNWIND_INFO_6 \
454 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
455 "=&d" (ignore3), "=m" (futex) \
456 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
458 : "memory", "cx", "cc", "r10", "r11"); \
461 #if defined NOT_IN_libc || defined UP
462 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
465 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
467 "lock; decl %0\n\t" \
474 #define lll_unlock(futex, private) \
477 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
478 __asm __volatile (__lll_unlock_asm_start \
479 ".subsection 1\n\t" \
480 ".type _L_unlock_%=, @function\n" \
482 "1:\tlea %0, %%" RDI_LP "\n" \
483 "2:\tsub $128, %%" RSP_LP "\n" \
484 "3:\tcallq __lll_unlock_wake_private\n" \
485 "4:\tadd $128, %%" RSP_LP "\n" \
487 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
489 LLL_STUB_UNWIND_INFO_5 \
491 : "=m" (futex), "=&D" (ignore) \
493 : "ax", "cx", "r11", "cc", "memory"); \
495 __asm __volatile (__lll_unlock_asm_start \
496 ".subsection 1\n\t" \
497 ".type _L_unlock_%=, @function\n" \
499 "1:\tlea %0, %%" RDI_LP "\n" \
500 "2:\tsub $128, %%" RSP_LP "\n" \
501 "3:\tcallq __lll_unlock_wake\n" \
502 "4:\tadd $128, %%" RSP_LP "\n" \
504 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
506 LLL_STUB_UNWIND_INFO_5 \
508 : "=m" (futex), "=&D" (ignore) \
509 : "m" (futex), "S" (private) \
510 : "ax", "cx", "r11", "cc", "memory"); \
513 #define lll_robust_unlock(futex, private) \
517 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
519 ".subsection 1\n\t" \
520 ".type _L_robust_unlock_%=, @function\n" \
521 "_L_robust_unlock_%=:\n" \
522 "1:\tlea %0, %%" RDI_LP "\n" \
523 "2:\tsub $128, %%" RSP_LP "\n" \
524 "3:\tcallq __lll_unlock_wake\n" \
525 "4:\tadd $128, %%" RSP_LP "\n" \
527 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
529 LLL_STUB_UNWIND_INFO_5 \
531 : "=m" (futex), "=&D" (ignore) \
532 : "i" (FUTEX_WAITERS), "m" (futex), \
534 : "ax", "cx", "r11", "cc", "memory"); \
538 #define lll_robust_dead(futex, private) \
542 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
544 : "=m" (futex), "=a" (ignore) \
545 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
546 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
547 "1" (__NR_futex), "d" (1) \
548 : "cx", "r11", "cc", "memory"); \
552 /* Returns non-zero if error happened, zero if success. */
553 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
555 register int __nr_move __asm ("r10") = nr_move; \
556 register void *__mutex __asm ("r8") = mutex; \
557 register int __val __asm ("r9") = val; \
558 __asm __volatile ("syscall" \
560 : "0" (__NR_futex), "D" ((void *) ftx), \
561 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
562 private)), "d" (nr_wake), \
563 "r" (__nr_move), "r" (__mutex), "r" (__val) \
564 : "cx", "r11", "cc", "memory"); \
567 #define lll_islocked(futex) \
568 (futex != LLL_LOCK_INITIALIZER)
571 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
572 wakeup when the clone terminates. The memory location contains the
573 thread ID while the clone is running and is reset to zero
576 The macro parameter must not have any side effect. */
577 #define lll_wait_tid(tid) \
580 register __typeof (tid) _tid asm ("edx") = (tid); \
582 __asm __volatile ("xorq %%r10, %%r10\n\t" \
583 "1:\tmovq %2, %%rax\n\t" \
585 "cmpl $0, (%%rdi)\n\t" \
588 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
590 : "memory", "cc", "r10", "r11", "cx"); \
593 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
595 #define lll_timedwait_tid(tid, abstime) \
600 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
603 __result = __lll_timedwait_tid (&tid, abstime); \
607 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
610 extern int __lll_unlock_elision (int *lock
, int private)
613 extern int __lll_trylock_elision (int *lock
, short *adapt_count
)
616 #define lll_lock_elision(futex, adapt_count, private) \
617 __lll_lock_elision (&(futex), &(adapt_count), private)
618 #define lll_unlock_elision(futex, private) \
619 __lll_unlock_elision (&(futex), private)
620 #define lll_trylock_elision(futex, adapt_count) \
621 __lll_trylock_elision (&(futex), &(adapt_count))
623 #endif /* !__ASSEMBLER__ */
625 #endif /* lowlevellock.h */