1 /* Copyright (C) 2002-2004, 2006-2008, 2009, 2012
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _LOWLEVELLOCK_H
21 #define _LOWLEVELLOCK_H 1
25 # include <sys/param.h>
26 # include <bits/pthreadtypes.h>
27 # include <kernel-features.h>
28 # include <tcb-offsets.h>
32 # define LOCK_INSTR /* nothing */
34 # define LOCK_INSTR "lock;"
47 #define SYS_futex __NR_futex
50 #define FUTEX_CMP_REQUEUE 4
51 #define FUTEX_WAKE_OP 5
52 #define FUTEX_LOCK_PI 6
53 #define FUTEX_UNLOCK_PI 7
54 #define FUTEX_TRYLOCK_PI 8
55 #define FUTEX_WAIT_BITSET 9
56 #define FUTEX_WAKE_BITSET 10
57 #define FUTEX_WAIT_REQUEUE_PI 11
58 #define FUTEX_CMP_REQUEUE_PI 12
59 #define FUTEX_PRIVATE_FLAG 128
60 #define FUTEX_CLOCK_REALTIME 256
62 #define FUTEX_BITSET_MATCH_ANY 0xffffffff
64 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
66 /* Values for 'private' parameter of locking macros. Yes, the
67 definition seems to be backwards. But it is not. The bit will be
68 reversed before passing to the system call. */
70 #define LLL_SHARED FUTEX_PRIVATE_FLAG
74 #if !defined NOT_IN_libc || defined IS_IN_rtld
75 /* In libc.so or ld.so all futexes are private. */
76 # ifdef __ASSUME_PRIVATE_FUTEX
77 # define __lll_private_flag(fl, private) \
78 ((fl) | FUTEX_PRIVATE_FLAG)
80 # define __lll_private_flag(fl, private) \
81 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
84 # ifdef __ASSUME_PRIVATE_FUTEX
85 # define __lll_private_flag(fl, private) \
86 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
88 # define __lll_private_flag(fl, private) \
89 (__builtin_constant_p (private) \
91 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
93 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
94 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
95 : "i" (offsetof (struct pthread, header.private_futex))); \
100 /* Initializer for lock. */
101 #define LLL_LOCK_INITIALIZER (0)
102 #define LLL_LOCK_INITIALIZER_LOCKED (1)
103 #define LLL_LOCK_INITIALIZER_WAITERS (2)
105 /* Delay in spinlock loop. */
106 #define BUSY_WAIT_NOP asm ("rep; nop")
109 #define LLL_STUB_UNWIND_INFO_START \
110 ".section .eh_frame,\"a\",@progbits\n" \
111 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
112 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
113 ".byte 0x1 # CIE Version\n\t" \
114 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
115 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
116 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
117 ".byte 0x10 # CIE RA Column\n\t" \
118 ".uleb128 0x1 # Augmentation size\n\t" \
119 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
120 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
123 ".align " LP_SIZE "\n" \
124 "9:\t" ".long 23f-10f # FDE Length\n" \
125 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
126 ".long 1b-. # FDE initial location\n\t" \
127 ".long 6b-1b # FDE address range\n\t" \
128 ".uleb128 0x0 # Augmentation size\n\t" \
129 ".byte 0x16 # DW_CFA_val_expression\n\t" \
130 ".uleb128 0x10\n\t" \
131 ".uleb128 12f-11f\n" \
132 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
134 #define LLL_STUB_UNWIND_INFO_END \
135 ".byte 0x16 # DW_CFA_val_expression\n\t" \
136 ".uleb128 0x10\n\t" \
137 ".uleb128 14f-13f\n" \
138 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
140 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
141 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
143 ".byte 0x16 # DW_CFA_val_expression\n\t" \
144 ".uleb128 0x10\n\t" \
145 ".uleb128 16f-15f\n" \
146 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
148 "16:\t" ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t" \
149 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
151 ".byte 0x16 # DW_CFA_val_expression\n\t" \
152 ".uleb128 0x10\n\t" \
153 ".uleb128 20f-17f\n" \
154 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
155 ".sleb128 19f-18f\n\t" \
156 ".byte 0x0d # DW_OP_const4s\n" \
157 "18:\t" ".4byte 4b-.\n\t" \
158 ".byte 0x1c # DW_OP_minus\n\t" \
159 ".byte 0x0d # DW_OP_const4s\n" \
160 "19:\t" ".4byte 24f-.\n\t" \
161 ".byte 0x22 # DW_OP_plus\n" \
162 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
163 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
165 ".byte 0x16 # DW_CFA_val_expression\n\t" \
166 ".uleb128 0x10\n\t" \
167 ".uleb128 22f-21f\n" \
168 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
170 "22:\t" ".align " LP_SIZE "\n" \
171 "23:\t" ".previous\n"
181 #define LLL_STUB_UNWIND_INFO_5 \
182 LLL_STUB_UNWIND_INFO_START \
183 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
184 LLL_STUB_UNWIND_INFO_END
195 #define LLL_STUB_UNWIND_INFO_6 \
196 LLL_STUB_UNWIND_INFO_START \
197 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
198 ".byte 0x16 # DW_CFA_val_expression\n\t" \
199 ".uleb128 0x10\n\t" \
200 ".uleb128 26f-25f\n" \
201 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
203 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
204 LLL_STUB_UNWIND_INFO_END
207 #define lll_futex_wait(futex, val, private) \
208 lll_futex_timed_wait(futex, val, NULL, private)
211 #define lll_futex_timed_wait(futex, val, timeout, private) \
213 register const struct timespec *__to __asm ("r10") = timeout; \
215 register __typeof (val) _val __asm ("edx") = (val); \
216 __asm __volatile ("syscall" \
218 : "0" (SYS_futex), "D" (futex), \
219 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
220 "d" (_val), "r" (__to) \
221 : "memory", "cc", "r11", "cx"); \
226 #define lll_futex_wake(futex, nr, private) \
229 register __typeof (nr) _nr __asm ("edx") = (nr); \
230 __asm __volatile ("syscall" \
232 : "0" (SYS_futex), "D" (futex), \
233 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
235 : "memory", "cc", "r10", "r11", "cx"); \
239 /* NB: in the lll_trylock macro we simply return the value in %eax
240 after the cmpxchg instruction. In case the operation succeded this
241 value is zero. In case the operation failed, the cmpxchg instruction
242 has loaded the current value of the memory work which is guaranteed
244 #if defined NOT_IN_libc || defined UP
245 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
247 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
249 "lock; cmpxchgl %2, %1\n\t" \
251 "0:\tcmpxchgl %2, %1\n\t" \
255 #define lll_trylock(futex) \
257 __asm __volatile (__lll_trylock_asm \
258 : "=a" (ret), "=m" (futex) \
259 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
260 "0" (LLL_LOCK_INITIALIZER) \
264 #define lll_robust_trylock(futex, id) \
266 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
267 : "=a" (ret), "=m" (futex) \
268 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
272 #define lll_cond_trylock(futex) \
274 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
275 : "=a" (ret), "=m" (futex) \
276 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
277 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
281 #if defined NOT_IN_libc || defined UP
282 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
285 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
287 "lock; cmpxchgl %4, %2\n\t" \
290 "0:\tcmpxchgl %4, %2\n\t" \
294 #define lll_lock(futex, private) \
296 ({ int ignore1, ignore2, ignore3; \
297 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
298 __asm __volatile (__lll_lock_asm_start \
299 ".subsection 1\n\t" \
300 ".type _L_lock_%=, @function\n" \
302 "1:\tlea %2, %%" RDI_LP "\n" \
303 "2:\tsub $128, %%" RSP_LP "\n" \
304 "3:\tcallq __lll_lock_wait_private\n" \
305 "4:\tadd $128, %%" RSP_LP "\n" \
307 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
309 LLL_STUB_UNWIND_INFO_5 \
311 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
313 : "0" (1), "m" (futex), "3" (0) \
314 : "cx", "r11", "cc", "memory"); \
316 __asm __volatile (__lll_lock_asm_start \
317 ".subsection 1\n\t" \
318 ".type _L_lock_%=, @function\n" \
320 "1:\tlea %2, %%" RDI_LP "\n" \
321 "2:\tsub $128, %%" RSP_LP "\n" \
322 "3:\tcallq __lll_lock_wait\n" \
323 "4:\tadd $128, %%" RSP_LP "\n" \
325 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
327 LLL_STUB_UNWIND_INFO_5 \
329 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
331 : "1" (1), "m" (futex), "3" (0), "0" (private) \
332 : "cx", "r11", "cc", "memory"); \
335 #define lll_robust_lock(futex, id, private) \
336 ({ int result, ignore1, ignore2; \
337 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
339 ".subsection 1\n\t" \
340 ".type _L_robust_lock_%=, @function\n" \
341 "_L_robust_lock_%=:\n" \
342 "1:\tlea %2, %%" RDI_LP "\n" \
343 "2:\tsub $128, %%" RSP_LP "\n" \
344 "3:\tcallq __lll_robust_lock_wait\n" \
345 "4:\tadd $128, %%" RSP_LP "\n" \
347 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
349 LLL_STUB_UNWIND_INFO_5 \
351 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
353 : "1" (id), "m" (futex), "3" (0), "0" (private) \
354 : "cx", "r11", "cc", "memory"); \
357 #define lll_cond_lock(futex, private) \
359 ({ int ignore1, ignore2, ignore3; \
360 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
362 ".subsection 1\n\t" \
363 ".type _L_cond_lock_%=, @function\n" \
364 "_L_cond_lock_%=:\n" \
365 "1:\tlea %2, %%" RDI_LP "\n" \
366 "2:\tsub $128, %%" RSP_LP "\n" \
367 "3:\tcallq __lll_lock_wait\n" \
368 "4:\tadd $128, %%" RSP_LP "\n" \
370 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
372 LLL_STUB_UNWIND_INFO_5 \
374 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
376 : "1" (2), "m" (futex), "3" (0), "0" (private) \
377 : "cx", "r11", "cc", "memory"); \
380 #define lll_robust_cond_lock(futex, id, private) \
381 ({ int result, ignore1, ignore2; \
382 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
384 ".subsection 1\n\t" \
385 ".type _L_robust_cond_lock_%=, @function\n" \
386 "_L_robust_cond_lock_%=:\n" \
387 "1:\tlea %2, %%" RDI_LP "\n" \
388 "2:\tsub $128, %%" RSP_LP "\n" \
389 "3:\tcallq __lll_robust_lock_wait\n" \
390 "4:\tadd $128, %%" RSP_LP "\n" \
392 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
394 LLL_STUB_UNWIND_INFO_5 \
396 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
398 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
400 : "cx", "r11", "cc", "memory"); \
403 #define lll_timedlock(futex, timeout, private) \
404 ({ int result, ignore1, ignore2, ignore3; \
405 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
407 ".subsection 1\n\t" \
408 ".type _L_timedlock_%=, @function\n" \
409 "_L_timedlock_%=:\n" \
410 "1:\tlea %4, %%" RDI_LP "\n" \
411 "0:\tmov %8, %%" RDX_LP "\n" \
412 "2:\tsub $128, %%" RSP_LP "\n" \
413 "3:\tcallq __lll_timedlock_wait\n" \
414 "4:\tadd $128, %%" RSP_LP "\n" \
416 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
418 LLL_STUB_UNWIND_INFO_6 \
420 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
421 "=&d" (ignore3), "=m" (futex) \
422 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
424 : "memory", "cx", "cc", "r10", "r11"); \
427 #define lll_robust_timedlock(futex, timeout, id, private) \
428 ({ int result, ignore1, ignore2, ignore3; \
429 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
431 ".subsection 1\n\t" \
432 ".type _L_robust_timedlock_%=, @function\n" \
433 "_L_robust_timedlock_%=:\n" \
434 "1:\tlea %4, %%" RDI_LP "\n" \
435 "0:\tmov %8, %%" RDX_LP "\n" \
436 "2:\tsub $128, %%" RSP_LP "\n" \
437 "3:\tcallq __lll_robust_timedlock_wait\n" \
438 "4:\tadd $128, %%" RSP_LP "\n" \
440 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
442 LLL_STUB_UNWIND_INFO_6 \
444 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
445 "=&d" (ignore3), "=m" (futex) \
446 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
448 : "memory", "cx", "cc", "r10", "r11"); \
451 #if defined NOT_IN_libc || defined UP
452 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
455 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
457 "lock; decl %0\n\t" \
464 #define lll_unlock(futex, private) \
467 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
468 __asm __volatile (__lll_unlock_asm_start \
469 ".subsection 1\n\t" \
470 ".type _L_unlock_%=, @function\n" \
472 "1:\tlea %0, %%" RDI_LP "\n" \
473 "2:\tsub $128, %%" RSP_LP "\n" \
474 "3:\tcallq __lll_unlock_wake_private\n" \
475 "4:\tadd $128, %%" RSP_LP "\n" \
477 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
479 LLL_STUB_UNWIND_INFO_5 \
481 : "=m" (futex), "=&D" (ignore) \
483 : "ax", "cx", "r11", "cc", "memory"); \
485 __asm __volatile (__lll_unlock_asm_start \
486 ".subsection 1\n\t" \
487 ".type _L_unlock_%=, @function\n" \
489 "1:\tlea %0, %%" RDI_LP "\n" \
490 "2:\tsub $128, %%" RSP_LP "\n" \
491 "3:\tcallq __lll_unlock_wake\n" \
492 "4:\tadd $128, %%" RSP_LP "\n" \
494 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
496 LLL_STUB_UNWIND_INFO_5 \
498 : "=m" (futex), "=&D" (ignore) \
499 : "m" (futex), "S" (private) \
500 : "ax", "cx", "r11", "cc", "memory"); \
503 #define lll_robust_unlock(futex, private) \
507 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
509 ".subsection 1\n\t" \
510 ".type _L_robust_unlock_%=, @function\n" \
511 "_L_robust_unlock_%=:\n" \
512 "1:\tlea %0, %%" RDI_LP "\n" \
513 "2:\tsub $128, %%" RSP_LP "\n" \
514 "3:\tcallq __lll_unlock_wake\n" \
515 "4:\tadd $128, %%" RSP_LP "\n" \
517 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
519 LLL_STUB_UNWIND_INFO_5 \
521 : "=m" (futex), "=&D" (ignore) \
522 : "i" (FUTEX_WAITERS), "m" (futex), \
524 : "ax", "cx", "r11", "cc", "memory"); \
528 #define lll_robust_dead(futex, private) \
532 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
534 : "=m" (futex), "=a" (ignore) \
535 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
536 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
537 "1" (__NR_futex), "d" (1) \
538 : "cx", "r11", "cc", "memory"); \
542 /* Returns non-zero if error happened, zero if success. */
543 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
545 register int __nr_move __asm ("r10") = nr_move; \
546 register void *__mutex __asm ("r8") = mutex; \
547 register int __val __asm ("r9") = val; \
548 __asm __volatile ("syscall" \
550 : "0" (__NR_futex), "D" ((void *) ftx), \
551 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
552 private)), "d" (nr_wake), \
553 "r" (__nr_move), "r" (__mutex), "r" (__val) \
554 : "cx", "r11", "cc", "memory"); \
557 #define lll_islocked(futex) \
558 (futex != LLL_LOCK_INITIALIZER)
561 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
562 wakeup when the clone terminates. The memory location contains the
563 thread ID while the clone is running and is reset to zero
566 The macro parameter must not have any side effect. */
567 #define lll_wait_tid(tid) \
570 register __typeof (tid) _tid asm ("edx") = (tid); \
572 __asm __volatile ("xorq %%r10, %%r10\n\t" \
573 "1:\tmovq %2, %%rax\n\t" \
575 "cmpl $0, (%%rdi)\n\t" \
578 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
580 : "memory", "cc", "r10", "r11", "cx"); \
583 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
585 #define lll_timedwait_tid(tid, abstime) \
590 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
593 __result = __lll_timedwait_tid (&tid, abstime); \
597 #endif /* !__ASSEMBLER__ */
599 #endif /* lowlevellock.h */