1/* Copyright (C) 2009-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2009.
4
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.
9
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.
14
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/>. */
18
19#include <sysdep.h>
20#include <tcb-offsets.h>
21#include <kernel-features.h>
22#include "lowlevellock.h"
23
24#define PTHREAD_UNWIND JUMPTARGET(__pthread_unwind)
25#if IS_IN (libpthread)
26# if defined SHARED && !defined NO_HIDDEN
27# undef PTHREAD_UNWIND
28# define PTHREAD_UNWIND __GI___pthread_unwind
29# endif
30#else
31# ifndef SHARED
32 .weak __pthread_unwind
33# endif
34#endif
35
36
37#ifdef __ASSUME_PRIVATE_FUTEX
38# define LOAD_PRIVATE_FUTEX_WAIT(reg) \
39 movl $(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), reg
40#else
41# if FUTEX_WAIT == 0
42# define LOAD_PRIVATE_FUTEX_WAIT(reg) \
43 movl %fs:PRIVATE_FUTEX, reg
44# else
45# define LOAD_PRIVATE_FUTEX_WAIT(reg) \
46 movl %fs:PRIVATE_FUTEX, reg ; \
47 orl $FUTEX_WAIT, reg
48# endif
49#endif
50
51/* It is crucial that the functions in this file don't modify registers
52 other than %rax and %r11. The syscall wrapper code depends on this
53 because it doesn't explicitly save the other registers which hold
54 relevant values. */
55 .text
56
57 .hidden __pthread_enable_asynccancel
58ENTRY(__pthread_enable_asynccancel)
59 movl %fs:CANCELHANDLING, %eax
602: movl %eax, %r11d
61 orl $TCB_CANCELTYPE_BITMASK, %r11d
62 cmpl %eax, %r11d
63 je 1f
64
65 lock
66 cmpxchgl %r11d, %fs:CANCELHANDLING
67 jnz 2b
68
69 andl $(TCB_CANCELSTATE_BITMASK|TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK|TCB_EXITING_BITMASK|TCB_CANCEL_RESTMASK|TCB_TERMINATED_BITMASK), %r11d
70 cmpl $(TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK), %r11d
71 je 3f
72
731: ret
74
753: subq $8, %rsp
76 cfi_adjust_cfa_offset(8)
77 LP_OP(mov) $TCB_PTHREAD_CANCELED, %fs:RESULT
78 lock
79 orl $TCB_EXITING_BITMASK, %fs:CANCELHANDLING
80 mov %fs:CLEANUP_JMP_BUF, %RDI_LP
81 call PTHREAD_UNWIND
82 hlt
83END(__pthread_enable_asynccancel)
84
85
86 .hidden __pthread_disable_asynccancel
87ENTRY(__pthread_disable_asynccancel)
88 testl $TCB_CANCELTYPE_BITMASK, %edi
89 jnz 1f
90
91 movl %fs:CANCELHANDLING, %eax
922: movl %eax, %r11d
93 andl $~TCB_CANCELTYPE_BITMASK, %r11d
94 lock
95 cmpxchgl %r11d, %fs:CANCELHANDLING
96 jnz 2b
97
98 movl %r11d, %eax
993: andl $(TCB_CANCELING_BITMASK|TCB_CANCELED_BITMASK), %eax
100 cmpl $TCB_CANCELING_BITMASK, %eax
101 je 4f
1021: ret
103
104 /* Performance doesn't matter in this loop. We will
105 delay until the thread is canceled. And we will unlikely
106 enter the loop twice. */
1074: mov %fs:0, %RDI_LP
108 movl $__NR_futex, %eax
109 xorq %r10, %r10
110 addq $CANCELHANDLING, %rdi
111 LOAD_PRIVATE_FUTEX_WAIT (%esi)
112 syscall
113 movl %fs:CANCELHANDLING, %eax
114 jmp 3b
115END(__pthread_disable_asynccancel)
116