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;"
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
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 %%gs:%P1, %0" : "+r" (__fl) \
95 : "i" (offsetof (struct pthread, header.private_futex))); \
100 #ifndef __ASSEMBLER__
102 /* Initializer for compatibility lock. */
103 #define LLL_LOCK_INITIALIZER (0)
104 #define LLL_LOCK_INITIALIZER_LOCKED (1)
105 #define LLL_LOCK_INITIALIZER_WAITERS (2)
109 # define LLL_EBX_LOAD "xchgl %2, %%ebx\n"
110 # define LLL_EBX_REG "D"
112 # define LLL_EBX_LOAD
113 # define LLL_EBX_REG "b"
116 #ifdef I386_USE_SYSENTER
118 # define LLL_ENTER_KERNEL "call *%%gs:%P6\n\t"
120 # define LLL_ENTER_KERNEL "call *_dl_sysinfo\n\t"
123 # define LLL_ENTER_KERNEL "int $0x80\n\t"
126 /* Delay in spinlock loop. */
127 #define BUSY_WAIT_NOP asm ("rep; nop")
130 #define LLL_STUB_UNWIND_INFO_START \
131 ".section .eh_frame,\"a\",@progbits\n" \
132 "5:\t" ".long 7f-6f # Length of Common Information Entry\n" \
133 "6:\t" ".long 0x0 # CIE Identifier Tag\n\t" \
134 ".byte 0x1 # CIE Version\n\t" \
135 ".ascii \"zR\\0\" # CIE Augmentation\n\t" \
136 ".uleb128 0x1 # CIE Code Alignment Factor\n\t" \
137 ".sleb128 -4 # CIE Data Alignment Factor\n\t" \
138 ".byte 0x8 # CIE RA Column\n\t" \
139 ".uleb128 0x1 # Augmentation size\n\t" \
140 ".byte 0x1b # FDE Encoding (pcrel sdata4)\n\t" \
141 ".byte 0xc # DW_CFA_def_cfa\n\t" \
145 "7:\t" ".long 17f-8f # FDE Length\n" \
146 "8:\t" ".long 8b-5b # FDE CIE offset\n\t" \
147 ".long 1b-. # FDE initial location\n\t" \
148 ".long 4b-1b # FDE address range\n\t" \
149 ".uleb128 0x0 # Augmentation size\n\t" \
150 ".byte 0x16 # DW_CFA_val_expression\n\t" \
152 ".uleb128 10f-9f\n" \
153 "9:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
155 #define LLL_STUB_UNWIND_INFO_END \
156 ".byte 0x16 # DW_CFA_val_expression\n\t" \
158 ".uleb128 12f-11f\n" \
159 "11:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
161 "12:\t" ".byte 0x40 + (3b-2b-1) # DW_CFA_advance_loc\n\t" \
162 ".byte 0x16 # DW_CFA_val_expression\n\t" \
164 ".uleb128 16f-13f\n" \
165 "13:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
166 ".sleb128 15f-14f\n\t" \
167 ".byte 0x0d # DW_OP_const4s\n" \
168 "14:\t" ".4byte 3b-.\n\t" \
169 ".byte 0x1c # DW_OP_minus\n\t" \
170 ".byte 0x0d # DW_OP_const4s\n" \
171 "15:\t" ".4byte 18f-.\n\t" \
172 ".byte 0x22 # DW_OP_plus\n" \
173 "16:\t" ".align 4\n" \
174 "17:\t" ".previous\n"
182 #define LLL_STUB_UNWIND_INFO_3 \
183 LLL_STUB_UNWIND_INFO_START \
184 "10:\t" ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t" \
185 LLL_STUB_UNWIND_INFO_END
194 #define LLL_STUB_UNWIND_INFO_4 \
195 LLL_STUB_UNWIND_INFO_START \
196 "10:\t" ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t" \
197 ".byte 0x16 # DW_CFA_val_expression\n\t" \
199 ".uleb128 20f-19f\n" \
200 "19:\t" ".byte 0x78 # DW_OP_breg8\n\t" \
202 "20:\t" ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t" \
203 LLL_STUB_UNWIND_INFO_END
206 #define lll_futex_wait(futex, val, private) \
207 lll_futex_timed_wait (futex, val, NULL, private)
210 #define lll_futex_timed_wait(futex, val, timeout, private) \
213 register __typeof (val) _val asm ("edx") = (val); \
214 __asm __volatile (LLL_EBX_LOAD \
218 : "0" (SYS_futex), LLL_EBX_REG (futex), "S" (timeout), \
219 "c" (__lll_private_flag (FUTEX_WAIT, private)), \
220 "d" (_val), "i" (offsetof (tcbhead_t, sysinfo)) \
226 #define lll_futex_wake(futex, nr, private) \
229 register __typeof (nr) _nr asm ("edx") = (nr); \
230 LIBC_PROBE (lll_futex_wake, 3, futex, nr, private); \
231 __asm __volatile (LLL_EBX_LOAD \
235 : "0" (SYS_futex), LLL_EBX_REG (futex), \
236 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
238 "i" (0) /* phony, to align next arg's number */, \
239 "i" (offsetof (tcbhead_t, sysinfo))); \
244 /* NB: in the lll_trylock macro we simply return the value in %eax
245 after the cmpxchg instruction. In case the operation succeded this
246 value is zero. In case the operation failed, the cmpxchg instruction
247 has loaded the current value of the memory work which is guaranteed
249 #if defined NOT_IN_libc || defined UP
250 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
252 # define __lll_trylock_asm "cmpl $0, %%gs:%P5\n\t" \
255 "0:\tcmpxchgl %2, %1"
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), \
264 "i" (MULTIPLE_THREADS_OFFSET) \
268 #define lll_robust_trylock(futex, id) \
270 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
271 : "=a" (ret), "=m" (futex) \
272 : "r" (id), "m" (futex), \
273 "0" (LLL_LOCK_INITIALIZER) \
278 #define lll_cond_trylock(futex) \
280 __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
281 : "=a" (ret), "=m" (futex) \
282 : "r" (LLL_LOCK_INITIALIZER_WAITERS), \
283 "m" (futex), "0" (LLL_LOCK_INITIALIZER) \
287 #if defined NOT_IN_libc || defined UP
288 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %1, %2\n\t"
290 # define __lll_lock_asm_start "cmpl $0, %%gs:%P6\n\t" \
293 "0:\tcmpxchgl %1, %2\n\t"
296 #define lll_lock(futex, private) \
298 ({ int ignore1, ignore2; \
299 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
300 __asm __volatile (__lll_lock_asm_start \
301 "jnz _L_lock_%=\n\t" \
302 ".subsection 1\n\t" \
303 ".type _L_lock_%=,@function\n" \
305 "1:\tleal %2, %%ecx\n" \
306 "2:\tcall __lll_lock_wait_private\n" \
308 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
310 LLL_STUB_UNWIND_INFO_3 \
312 : "=a" (ignore1), "=c" (ignore2), "=m" (futex) \
313 : "0" (0), "1" (1), "m" (futex), \
314 "i" (MULTIPLE_THREADS_OFFSET) \
319 __asm __volatile (__lll_lock_asm_start \
320 "jnz _L_lock_%=\n\t" \
321 ".subsection 1\n\t" \
322 ".type _L_lock_%=,@function\n" \
324 "1:\tleal %2, %%edx\n" \
325 "0:\tmovl %8, %%ecx\n" \
326 "2:\tcall __lll_lock_wait\n" \
328 "4:\t.size _L_lock_%=, 4b-1b\n\t" \
330 LLL_STUB_UNWIND_INFO_4 \
332 : "=a" (ignore1), "=c" (ignore2), \
333 "=m" (futex), "=&d" (ignore3) \
334 : "1" (1), "m" (futex), \
335 "i" (MULTIPLE_THREADS_OFFSET), "0" (0), \
336 "g" ((int) (private)) \
341 #define lll_robust_lock(futex, id, private) \
342 ({ int result, ignore1, ignore2; \
343 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
344 "jnz _L_robust_lock_%=\n\t" \
345 ".subsection 1\n\t" \
346 ".type _L_robust_lock_%=,@function\n" \
347 "_L_robust_lock_%=:\n" \
348 "1:\tleal %2, %%edx\n" \
349 "0:\tmovl %7, %%ecx\n" \
350 "2:\tcall __lll_robust_lock_wait\n" \
352 "4:\t.size _L_robust_lock_%=, 4b-1b\n\t" \
354 LLL_STUB_UNWIND_INFO_4 \
356 : "=a" (result), "=c" (ignore1), "=m" (futex), \
358 : "0" (0), "1" (id), "m" (futex), "g" ((int) (private))\
363 /* Special version of lll_lock which causes the unlock function to
364 always wakeup waiters. */
365 #define lll_cond_lock(futex, private) \
367 ({ int ignore1, ignore2, ignore3; \
368 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
369 "jnz _L_cond_lock_%=\n\t" \
370 ".subsection 1\n\t" \
371 ".type _L_cond_lock_%=,@function\n" \
372 "_L_cond_lock_%=:\n" \
373 "1:\tleal %2, %%edx\n" \
374 "0:\tmovl %7, %%ecx\n" \
375 "2:\tcall __lll_lock_wait\n" \
377 "4:\t.size _L_cond_lock_%=, 4b-1b\n\t" \
379 LLL_STUB_UNWIND_INFO_4 \
381 : "=a" (ignore1), "=c" (ignore2), "=m" (futex), \
383 : "0" (0), "1" (2), "m" (futex), "g" ((int) (private))\
388 #define lll_robust_cond_lock(futex, id, private) \
389 ({ int result, ignore1, ignore2; \
390 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
391 "jnz _L_robust_cond_lock_%=\n\t" \
392 ".subsection 1\n\t" \
393 ".type _L_robust_cond_lock_%=,@function\n" \
394 "_L_robust_cond_lock_%=:\n" \
395 "1:\tleal %2, %%edx\n" \
396 "0:\tmovl %7, %%ecx\n" \
397 "2:\tcall __lll_robust_lock_wait\n" \
399 "4:\t.size _L_robust_cond_lock_%=, 4b-1b\n\t" \
401 LLL_STUB_UNWIND_INFO_4 \
403 : "=a" (result), "=c" (ignore1), "=m" (futex), \
405 : "0" (0), "1" (id | FUTEX_WAITERS), "m" (futex), \
406 "g" ((int) (private)) \
411 #define lll_timedlock(futex, timeout, private) \
412 ({ int result, ignore1, ignore2, ignore3; \
413 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
414 "jnz _L_timedlock_%=\n\t" \
415 ".subsection 1\n\t" \
416 ".type _L_timedlock_%=,@function\n" \
417 "_L_timedlock_%=:\n" \
418 "1:\tleal %3, %%ecx\n" \
419 "0:\tmovl %8, %%edx\n" \
420 "2:\tcall __lll_timedlock_wait\n" \
422 "4:\t.size _L_timedlock_%=, 4b-1b\n\t" \
424 LLL_STUB_UNWIND_INFO_4 \
426 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
427 "=m" (futex), "=S" (ignore3) \
428 : "0" (0), "1" (1), "m" (futex), "m" (timeout), \
429 "4" ((int) (private)) \
433 extern int __lll_timedlock_elision (int *futex
, short *adapt_count
,
434 const struct timespec
*timeout
,
435 int private) attribute_hidden
;
437 #define lll_timedlock_elision(futex, adapt_count, timeout, private) \
438 __lll_timedlock_elision(&(futex), &(adapt_count), timeout, private)
440 #define lll_robust_timedlock(futex, timeout, id, private) \
441 ({ int result, ignore1, ignore2, ignore3; \
442 __asm __volatile (LOCK_INSTR "cmpxchgl %1, %3\n\t" \
443 "jnz _L_robust_timedlock_%=\n\t" \
444 ".subsection 1\n\t" \
445 ".type _L_robust_timedlock_%=,@function\n" \
446 "_L_robust_timedlock_%=:\n" \
447 "1:\tleal %3, %%ecx\n" \
448 "0:\tmovl %8, %%edx\n" \
449 "2:\tcall __lll_robust_timedlock_wait\n" \
451 "4:\t.size _L_robust_timedlock_%=, 4b-1b\n\t" \
453 LLL_STUB_UNWIND_INFO_4 \
455 : "=a" (result), "=c" (ignore1), "=&d" (ignore2), \
456 "=m" (futex), "=S" (ignore3) \
457 : "0" (0), "1" (id), "m" (futex), "m" (timeout), \
458 "4" ((int) (private)) \
462 #if defined NOT_IN_libc || defined UP
463 # define __lll_unlock_asm LOCK_INSTR "subl $1, %0\n\t"
465 # define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t" \
471 #define lll_unlock(futex, private) \
474 if (__builtin_constant_p (private) && (private) == LLL_PRIVATE) \
475 __asm __volatile (__lll_unlock_asm \
476 "jne _L_unlock_%=\n\t" \
477 ".subsection 1\n\t" \
478 ".type _L_unlock_%=,@function\n" \
480 "1:\tleal %0, %%eax\n" \
481 "2:\tcall __lll_unlock_wake_private\n" \
483 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
485 LLL_STUB_UNWIND_INFO_3 \
487 : "=m" (futex), "=&a" (ignore) \
488 : "m" (futex), "i" (MULTIPLE_THREADS_OFFSET) \
493 __asm __volatile (__lll_unlock_asm \
494 "jne _L_unlock_%=\n\t" \
495 ".subsection 1\n\t" \
496 ".type _L_unlock_%=,@function\n" \
498 "1:\tleal %0, %%eax\n" \
499 "0:\tmovl %5, %%ecx\n" \
500 "2:\tcall __lll_unlock_wake\n" \
502 "4:\t.size _L_unlock_%=, 4b-1b\n\t" \
504 LLL_STUB_UNWIND_INFO_4 \
506 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
507 : "i" (MULTIPLE_THREADS_OFFSET), "m" (futex), \
508 "g" ((int) (private)) \
513 #define lll_robust_unlock(futex, private) \
515 ({ int ignore, ignore2; \
516 __asm __volatile (LOCK_INSTR "andl %3, %0\n\t" \
517 "jne _L_robust_unlock_%=\n\t" \
518 ".subsection 1\n\t" \
519 ".type _L_robust_unlock_%=,@function\n" \
520 "_L_robust_unlock_%=:\n\t" \
521 "1:\tleal %0, %%eax\n" \
522 "0:\tmovl %5, %%ecx\n" \
523 "2:\tcall __lll_unlock_wake\n" \
525 "4:\t.size _L_robust_unlock_%=, 4b-1b\n\t" \
527 LLL_STUB_UNWIND_INFO_4 \
529 : "=m" (futex), "=&a" (ignore), "=&c" (ignore2) \
530 : "i" (FUTEX_WAITERS), "m" (futex), \
531 "g" ((int) (private)) \
536 #define lll_robust_dead(futex, private) \
539 register int _nr asm ("edx") = 1; \
540 __asm __volatile (LOCK_INSTR "orl %5, (%2)\n\t" \
545 : "0" (SYS_futex), LLL_EBX_REG (&(futex)), \
546 "c" (__lll_private_flag (FUTEX_WAKE, private)), \
547 "d" (_nr), "i" (FUTEX_OWNER_DIED), \
548 "i" (offsetof (tcbhead_t, sysinfo))); \
551 #define lll_islocked(futex) \
552 (futex != LLL_LOCK_INITIALIZER)
554 /* The kernel notifies a process which uses CLONE_CHILD_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 (LLL_EBX_LOAD \
566 "1:\tmovl %1, %%eax\n\t" \
568 "cmpl $0, (%%ebx)\n\t" \
572 : "i" (SYS_futex), LLL_EBX_REG (&tid), "S" (0), \
573 "c" (FUTEX_WAIT), "d" (_tid), \
574 "i" (offsetof (tcbhead_t, sysinfo)) \
578 extern int __lll_timedwait_tid (int *tid
, const struct timespec
*abstime
)
579 __attribute__ ((regparm (2))) attribute_hidden
;
580 #define lll_timedwait_tid(tid, abstime) \
585 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) \
588 __result = __lll_timedwait_tid (&tid, abstime); \
592 extern int __lll_lock_elision (int *futex
, short *adapt_count
, int private)
595 extern int __lll_unlock_elision(int *lock
, int private)
598 extern int __lll_trylock_elision(int *lock
, short *adapt_count
)
601 #define lll_lock_elision(futex, adapt_count, private) \
602 __lll_lock_elision (&(futex), &(adapt_count), private)
603 #define lll_unlock_elision(futex, private) \
604 __lll_unlock_elision (&(futex), private)
605 #define lll_trylock_elision(futex, adapt_count) \
606 __lll_trylock_elision(&(futex), &(adapt_count))
608 #endif /* !__ASSEMBLER__ */
610 #endif /* lowlevellock.h */