1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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;"
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_PRIVATE_FLAG 128
57 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
59 /* Values for 'private' parameter of locking macros. Yes, the
60 definition seems to be backwards. But it is not. The bit will be
61 reversed before passing to the system call. */
63 #define LLL_SHARED FUTEX_PRIVATE_FLAG
67 #if !defined NOT_IN_libc || defined IS_IN_rtld
68 /* In libc.so or ld.so all futexes are private. */
69 # ifdef __ASSUME_PRIVATE_FUTEX
70 # define __lll_private_flag(fl, private) \
71 ((fl) | FUTEX_PRIVATE_FLAG)
73 # define __lll_private_flag(fl, private) \
74 ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
77 # ifdef __ASSUME_PRIVATE_FUTEX
78 # define __lll_private_flag(fl, private) \
79 (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
81 # define __lll_private_flag(fl, private) \
82 (__builtin_constant_p (private) \
84 ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex)) \
86 : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG); \
87 asm ("andl %%fs:%P1, %0" : "+r" (__fl) \
88 : "i" (offsetof (struct pthread, header.private_futex))); \
93 /* Initializer for lock. */
94 #define LLL_LOCK_INITIALIZER (0)
95 #define LLL_LOCK_INITIALIZER_LOCKED (1)
96 #define LLL_LOCK_INITIALIZER_WAITERS (2)
98 /* Delay in spinlock loop. */
99 #define BUSY_WAIT_NOP asm ("rep; nop")
102 #define LLL_STUB_UNWIND_INFO_START \
103 ".section .eh_frame,\"a\",@progbits\n" \
104 "7:\t" ".long 9f-8f # Length of Common Information Entry\n" \
105 "8:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
106 ".byte 0x1 # CIE Version\n\t" \
107 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
108 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
109 ".sleb128 -8 # CIE Data Alignment Factor\n\t" \
110 ".byte 0x10 # CIE RA Column\n\t" \
111 ".uleb128 0x1 # Augmentation size\n\t" \
112 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
113 ".byte 0x12 # DW_CFA_def_cfa_sf\n\t" \
117 "9:\t" ".long 23f-10f # FDE Length\n" \
118 "10:\t" ".long 10b-7b # FDE CIE offset\n\t" \
119 ".long 1b-. # FDE initial location\n\t" \
120 ".long 6b-1b # FDE address range\n\t" \
121 ".uleb128 0x0 # Augmentation size\n\t" \
122 ".byte 0x16 # DW_CFA_val_expression\n\t" \
123 ".uleb128 0x10\n\t" \
124 ".uleb128 12f-11f\n" \
125 "11:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
127 #define LLL_STUB_UNWIND_INFO_END \
128 ".byte 0x16 # DW_CFA_val_expression\n\t" \
129 ".uleb128 0x10\n\t" \
130 ".uleb128 14f-13f\n" \
131 "13:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
133 "14:\t" ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t" \
134 ".byte 0x0e # DW_CFA_def_cfa_offset\n\t" \
136 ".byte 0x16 # DW_CFA_val_expression\n\t" \
137 ".uleb128 0x10\n\t" \
138 ".uleb128 16f-15f\n" \
139 "15:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
141 "16:\t" ".byte 0x40 + (4b-3b-1) # 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 20f-17f\n" \
147 "17:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
148 ".sleb128 19f-18f\n\t" \
149 ".byte 0x0d # DW_OP_const4s\n" \
150 "18:\t" ".4byte 4b-.\n\t" \
151 ".byte 0x1c # DW_OP_minus\n\t" \
152 ".byte 0x0d # DW_OP_const4s\n" \
153 "19:\t" ".4byte 24f-.\n\t" \
154 ".byte 0x22 # DW_OP_plus\n" \
155 "20:\t" ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t" \
156 ".byte 0x13 # DW_CFA_def_cfa_offset_sf\n\t" \
158 ".byte 0x16 # DW_CFA_val_expression\n\t" \
159 ".uleb128 0x10\n\t" \
160 ".uleb128 22f-21f\n" \
161 "21:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
163 "22:\t" ".align 8\n" \
164 "23:\t" ".previous\n"
174 #define LLL_STUB_UNWIND_INFO_5 \
175 LLL_STUB_UNWIND_INFO_START \
176 "12:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
177 LLL_STUB_UNWIND_INFO_END
188 #define LLL_STUB_UNWIND_INFO_6 \
189 LLL_STUB_UNWIND_INFO_START \
190 "12:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
191 ".byte 0x16 # DW_CFA_val_expression\n\t" \
192 ".uleb128 0x10\n\t" \
193 ".uleb128 26f-25f\n" \
194 "25:\t" ".byte 0x80 # DW_OP_breg16\n\t" \
196 "26:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
197 LLL_STUB_UNWIND_INFO_END
200 #define lll_futex_wait(futex, val, private) \
201 lll_futex_timed_wait(futex, val, NULL, private)
204 #define lll_futex_timed_wait(futex, val, timeout, private) \
206 register const struct timespec *__to __asm ("r10") = timeout; \
208 register __typeof (val) _val __asm ("edx") = (val); \
209 __asm __volatile ("syscall" \
211 : "0" (SYS_futex), "D" (futex), \
212 "S" (__lll_private_flag (FUTEX_WAIT, private)), \
213 "d" (_val), "r" (__to) \
214 : "memory", "cc", "r11", "cx"); \
219 #define lll_futex_wake(futex, nr, private) \
222 register __typeof (nr) _nr __asm ("edx") = (nr); \
223 __asm __volatile ("syscall" \
225 : "0" (SYS_futex), "D" (futex), \
226 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
228 : "memory", "cc", "r10", "r11", "cx"); \
232 /* NB: in the lll_trylock macro we simply return the value in %eax
233 after the cmpxchg instruction. In case the operation succeded this
234 value is zero. In case the operation failed, the cmpxchg instruction
235 has loaded the current value of the memory work which is guaranteed
237 #if defined NOT_IN_libc || defined UP
238 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
240 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
242 "lock; cmpxchgl %2, %1\n\t" \
244 "0:\tcmpxchgl %2, %1\n\t" \
248 #define lll_trylock(futex) \
250 __asm __volatile (__lll_trylock_asm \
251 : "=a" (ret), "=m" (futex) \
252 : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex), \
253 "0" (LLL_LOCK_INITIALIZER) \
257 #define lll_robust_trylock(futex, id) \
259 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
260 : "=a" (ret), "=m" (futex) \
261 : "r" (id), "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
265 #define lll_cond_trylock(futex) \
267 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
268 : "=a" (ret), "=m" (futex) \
269 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
270 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
274 #if defined NOT_IN_libc || defined UP
275 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t" \
278 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
280 "lock; cmpxchgl %4, %2\n\t" \
283 "0:\tcmpxchgl %4, %2\n\t" \
287 #define lll_lock(futex, private) \
289 ({ int ignore1, ignore2, ignore3; \
290 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
291 __asm __volatile (__lll_lock_asm_start \
292 ".subsection 1\n\t" \
293 ".type _L_lock_%=, @function\n" \
295 "1:\tleaq %2, %%rdi\n" \
296 "2:\tsubq $128, %%rsp\n" \
297 "3:\tcallq __lll_lock_wait_private\n" \
298 "4:\taddq $128, %%rsp\n" \
300 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
302 LLL_STUB_UNWIND_INFO_5 \
304 : "=S" (ignore1), "=&D" (ignore2), "=m" (futex), \
306 : "0" (1), "m" (futex), "3" (0) \
307 : "cx", "r11", "cc", "memory"); \
309 __asm __volatile (__lll_lock_asm_start \
310 ".subsection 1\n\t" \
311 ".type _L_lock_%=, @function\n" \
313 "1:\tleaq %2, %%rdi\n" \
314 "2:\tsubq $128, %%rsp\n" \
315 "3:\tcallq __lll_lock_wait\n" \
316 "4:\taddq $128, %%rsp\n" \
318 "6:\t.size _L_lock_%=, 6b-1b\n\t" \
320 LLL_STUB_UNWIND_INFO_5 \
322 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
324 : "1" (1), "m" (futex), "3" (0), "0" (private) \
325 : "cx", "r11", "cc", "memory"); \
328 #define lll_robust_lock(futex, id, private) \
329 ({ int result, ignore1, ignore2; \
330 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
332 ".subsection 1\n\t" \
333 ".type _L_robust_lock_%=, @function\n" \
334 "_L_robust_lock_%=:\n" \
335 "1:\tleaq %2, %%rdi\n" \
336 "2:\tsubq $128, %%rsp\n" \
337 "3:\tcallq __lll_robust_lock_wait\n" \
338 "4:\taddq $128, %%rsp\n" \
340 "6:\t.size _L_robust_lock_%=, 6b-1b\n\t" \
342 LLL_STUB_UNWIND_INFO_5 \
344 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
346 : "1" (id), "m" (futex), "3" (0), "0" (private) \
347 : "cx", "r11", "cc", "memory"); \
350 #define lll_cond_lock(futex, private) \
352 ({ int ignore1, ignore2, ignore3; \
353 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
355 ".subsection 1\n\t" \
356 ".type _L_cond_lock_%=, @function\n" \
357 "_L_cond_lock_%=:\n" \
358 "1:\tleaq %2, %%rdi\n" \
359 "2:\tsubq $128, %%rsp\n" \
360 "3:\tcallq __lll_lock_wait\n" \
361 "4:\taddq $128, %%rsp\n" \
363 "6:\t.size _L_cond_lock_%=, 6b-1b\n\t" \
365 LLL_STUB_UNWIND_INFO_5 \
367 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
369 : "1" (2), "m" (futex), "3" (0), "0" (private) \
370 : "cx", "r11", "cc", "memory"); \
373 #define lll_robust_cond_lock(futex, id, private) \
374 ({ int result, ignore1, ignore2; \
375 __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t" \
377 ".subsection 1\n\t" \
378 ".type _L_robust_cond_lock_%=, @function\n" \
379 "_L_robust_cond_lock_%=:\n" \
380 "1:\tleaq %2, %%rdi\n" \
381 "2:\tsubq $128, %%rsp\n" \
382 "3:\tcallq __lll_robust_lock_wait\n" \
383 "4:\taddq $128, %%rsp\n" \
385 "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t" \
387 LLL_STUB_UNWIND_INFO_5 \
389 : "=S" (ignore1), "=D" (ignore2), "=m" (futex), \
391 : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0), \
393 : "cx", "r11", "cc", "memory"); \
396 #define lll_timedlock(futex, timeout, private) \
397 ({ int result, ignore1, ignore2, ignore3; \
398 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
400 ".subsection 1\n\t" \
401 ".type _L_timedlock_%=, @function\n" \
402 "_L_timedlock_%=:\n" \
403 "1:\tleaq %4, %%rdi\n" \
404 "0:\tmovq %8, %%rdx\n" \
405 "2:\tsubq $128, %%rsp\n" \
406 "3:\tcallq __lll_timedlock_wait\n" \
407 "4:\taddq $128, %%rsp\n" \
409 "6:\t.size _L_timedlock_%=, 6b-1b\n\t" \
411 LLL_STUB_UNWIND_INFO_6 \
413 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
414 "=&d" (ignore3), "=m" (futex) \
415 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
417 : "memory", "cx", "cc", "r10", "r11"); \
420 #define lll_robust_timedlock(futex, timeout, id, private) \
421 ({ int result, ignore1, ignore2, ignore3; \
422 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t" \
424 ".subsection 1\n\t" \
425 ".type _L_robust_timedlock_%=, @function\n" \
426 "_L_robust_timedlock_%=:\n" \
427 "1:\tleaq %4, %%rdi\n" \
428 "0:\tmovq %8, %%rdx\n" \
429 "2:\tsubq $128, %%rsp\n" \
430 "3:\tcallq __lll_robust_timedlock_wait\n" \
431 "4:\taddq $128, %%rsp\n" \
433 "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t" \
435 LLL_STUB_UNWIND_INFO_6 \
437 : "=a" (result), "=D" (ignore1), "=S" (ignore2), \
438 "=&d" (ignore3), "=m" (futex) \
439 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
441 : "memory", "cx", "cc", "r10", "r11"); \
444 #if defined NOT_IN_libc || defined UP
445 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t" \
448 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
450 "lock; decl %0\n\t" \
457 #define lll_unlock(futex, private) \
460 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
461 __asm __volatile (__lll_unlock_asm_start \
462 ".subsection 1\n\t" \
463 ".type _L_unlock_%=, @function\n" \
465 "1:\tleaq %0, %%rdi\n" \
466 "2:\tsubq $128, %%rsp\n" \
467 "3:\tcallq __lll_unlock_wake_private\n" \
468 "4:\taddq $128, %%rsp\n" \
470 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
472 LLL_STUB_UNWIND_INFO_5 \
474 : "=m" (futex), "=&D" (ignore) \
476 : "ax", "cx", "r11", "cc", "memory"); \
478 __asm __volatile (__lll_unlock_asm_start \
479 ".subsection 1\n\t" \
480 ".type _L_unlock_%=, @function\n" \
482 "1:\tleaq %0, %%rdi\n" \
483 "2:\tsubq $128, %%rsp\n" \
484 "3:\tcallq __lll_unlock_wake\n" \
485 "4:\taddq $128, %%rsp\n" \
487 "6:\t.size _L_unlock_%=, 6b-1b\n\t" \
489 LLL_STUB_UNWIND_INFO_5 \
491 : "=m" (futex), "=&D" (ignore) \
492 : "m" (futex), "S" (private) \
493 : "ax", "cx", "r11", "cc", "memory"); \
496 #define lll_robust_unlock(futex, private) \
500 __asm __volatile (LOCK_INSTR "andl %2, %0\n\t" \
502 ".subsection 1\n\t" \
503 ".type _L_robust_unlock_%=, @function\n" \
504 "_L_robust_unlock_%=:\n" \
505 "1:\tleaq %0, %%rdi\n" \
506 "2:\tsubq $128, %%rsp\n" \
507 "3:\tcallq __lll_unlock_wake\n" \
508 "4:\taddq $128, %%rsp\n" \
510 "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t" \
512 LLL_STUB_UNWIND_INFO_5 \
514 : "=m" (futex), "=&D" (ignore) \
515 : "i" (FUTEX_WAITERS), "m" (futex), \
517 : "ax", "cx", "r11", "cc", "memory"); \
521 #define lll_robust_dead(futex, private) \
525 __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t" \
527 : "=m" (futex), "=a" (ignore) \
528 : "D" (&(futex)), "i" (FUTEX_OWNER_DIED), \
529 "S" (__lll_private_flag (FUTEX_WAKE, private)), \
530 "1" (__NR_futex), "d" (1) \
531 : "cx", "r11", "cc", "memory"); \
535 /* Returns non-zero if error happened, zero if success. */
536 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
538 register int __nr_move __asm ("r10") = nr_move; \
539 register void *__mutex __asm ("r8") = mutex; \
540 register int __val __asm ("r9") = val; \
541 __asm __volatile ("syscall" \
543 : "0" (__NR_futex), "D" ((void *) ftx), \
544 "S" (__lll_private_flag (FUTEX_CMP_REQUEUE, \
545 private)), "d" (nr_wake), \
546 "r" (__nr_move), "r" (__mutex), "r" (__val) \
547 : "cx", "r11", "cc", "memory"); \
550 #define lll_islocked(futex) \
551 (futex != LLL_LOCK_INITIALIZER)
554 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
555 wakeup when the clone terminates. The memory location contains the
556 thread ID while the clone is running and is reset to zero
559 The macro parameter must not have any side effect. */
560 #define lll_wait_tid(tid) \
563 register __typeof (tid) _tid asm ("edx") = (tid); \
565 __asm __volatile ("xorq %%r10, %%r10\n\t" \
566 "1:\tmovq %2, %%rax\n\t" \
568 "cmpl $0, (%%rdi)\n\t" \
571 : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid), \
573 : "memory", "cc", "r10", "r11", "cx"); \
576 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
578 #define lll_timedwait_tid(tid, abstime) \
583 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
586 __result = __lll_timedwait_tid (&tid, abstime); \
590 #endif /* !__ASSEMBLER__ */
592 #endif /* lowlevellock.h */