Blame sysdeps/unix/sysv/linux/x86_64/cancellation.S

Packit 6c4009
/* Copyright (C) 2009-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2009.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <tcb-offsets.h>
Packit 6c4009
#include <kernel-features.h>
Packit 6c4009
#include "lowlevellock.h"
Packit 6c4009
Packit 6c4009
#define PTHREAD_UNWIND JUMPTARGET(__pthread_unwind)
Packit 6c4009
#if IS_IN (libpthread)
Packit 6c4009
# if defined SHARED && !defined NO_HIDDEN
Packit 6c4009
#  undef PTHREAD_UNWIND
Packit 6c4009
#  define PTHREAD_UNWIND __GI___pthread_unwind
Packit 6c4009
# endif
Packit 6c4009
#else
Packit 6c4009
# ifndef SHARED
Packit 6c4009
	.weak __pthread_unwind
Packit 6c4009
# endif
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define LOAD_PRIVATE_FUTEX_WAIT(reg) \
Packit 6c4009
	movl	$(FUTEX_WAIT | FUTEX_PRIVATE_FLAG), reg
Packit 6c4009
Packit 6c4009
/* It is crucial that the functions in this file don't modify registers
Packit 6c4009
   other than %rax and %r11.  The syscall wrapper code depends on this
Packit 6c4009
   because it doesn't explicitly save the other registers which hold
Packit 6c4009
   relevant values.  */
Packit 6c4009
	.text
Packit 6c4009
Packit 6c4009
	.hidden __pthread_enable_asynccancel
Packit 6c4009
ENTRY(__pthread_enable_asynccancel)
Packit 6c4009
	movl	%fs:CANCELHANDLING, %eax
Packit 6c4009
2:	movl	%eax, %r11d
Packit 6c4009
	orl	$TCB_CANCELTYPE_BITMASK, %r11d
Packit 6c4009
	cmpl	%eax, %r11d
Packit 6c4009
	je	1f
Packit 6c4009
Packit 6c4009
	lock
Packit 6c4009
	cmpxchgl %r11d, %fs:CANCELHANDLING
Packit 6c4009
	jnz	2b
Packit 6c4009
Packit 6c4009
	andl	$(TCB_CANCELSTATE_BITMASK|TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK|TCB_EXITING_BITMASK|TCB_CANCEL_RESTMASK|TCB_TERMINATED_BITMASK), %r11d
Packit 6c4009
	cmpl	$(TCB_CANCELTYPE_BITMASK|TCB_CANCELED_BITMASK), %r11d
Packit 6c4009
	je	3f
Packit 6c4009
Packit 6c4009
1:	ret
Packit 6c4009
Packit 6c4009
3:	subq	$8, %rsp
Packit 6c4009
	cfi_adjust_cfa_offset(8)
Packit 6c4009
	LP_OP(mov) $TCB_PTHREAD_CANCELED, %fs:RESULT
Packit 6c4009
	lock
Packit 6c4009
	orl	$TCB_EXITING_BITMASK, %fs:CANCELHANDLING
Packit 6c4009
	mov	%fs:CLEANUP_JMP_BUF, %RDI_LP
Packit 6c4009
	call	PTHREAD_UNWIND
Packit 6c4009
	hlt
Packit 6c4009
END(__pthread_enable_asynccancel)
Packit 6c4009
Packit 6c4009
Packit 6c4009
	.hidden __pthread_disable_asynccancel
Packit 6c4009
ENTRY(__pthread_disable_asynccancel)
Packit 6c4009
	testl	$TCB_CANCELTYPE_BITMASK, %edi
Packit 6c4009
	jnz	1f
Packit 6c4009
Packit 6c4009
	movl	%fs:CANCELHANDLING, %eax
Packit 6c4009
2:	movl	%eax, %r11d
Packit 6c4009
	andl	$~TCB_CANCELTYPE_BITMASK, %r11d
Packit 6c4009
	lock
Packit 6c4009
	cmpxchgl %r11d, %fs:CANCELHANDLING
Packit 6c4009
	jnz	2b
Packit 6c4009
Packit 6c4009
	movl	%r11d, %eax
Packit 6c4009
3:	andl	$(TCB_CANCELING_BITMASK|TCB_CANCELED_BITMASK), %eax
Packit 6c4009
	cmpl	$TCB_CANCELING_BITMASK, %eax
Packit 6c4009
	je	4f
Packit 6c4009
1:	ret
Packit 6c4009
Packit 6c4009
	/* Performance doesn't matter in this loop.  We will
Packit 6c4009
	   delay until the thread is canceled.  And we will unlikely
Packit 6c4009
	   enter the loop twice.  */
Packit 6c4009
4:	mov	%fs:0, %RDI_LP
Packit 6c4009
	movl	$__NR_futex, %eax
Packit 6c4009
	xorq	%r10, %r10
Packit 6c4009
	addq	$CANCELHANDLING, %rdi
Packit 6c4009
	LOAD_PRIVATE_FUTEX_WAIT (%esi)
Packit 6c4009
	syscall
Packit 6c4009
	movl	%fs:CANCELHANDLING, %eax
Packit 6c4009
	jmp	3b
Packit 6c4009
END(__pthread_disable_asynccancel)