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

Packit 6c4009
/* Save current context.
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Andreas Jaeger <aj@suse.de>, 2002.
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 <asm/prctl.h>
Packit 6c4009
Packit 6c4009
#include "ucontext_i.h"
Packit 6c4009
Packit 6c4009
/*  int __getcontext (ucontext_t *ucp)
Packit 6c4009
Packit 6c4009
  Saves the machine context in UCP such that when it is activated,
Packit 6c4009
  it appears as if __getcontext() returned again.
Packit 6c4009
Packit 6c4009
  This implementation is intended to be used for *synchronous* context
Packit 6c4009
  switches only.  Therefore, it does not have to save anything
Packit 6c4009
  other than the PRESERVED state.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
ENTRY(__getcontext)
Packit 6c4009
	/* Save the preserved registers, the registers used for passing
Packit 6c4009
	   args, and the return address.  */
Packit 6c4009
	movq	%rbx, oRBX(%rdi)
Packit 6c4009
	movq	%rbp, oRBP(%rdi)
Packit 6c4009
	movq	%r12, oR12(%rdi)
Packit 6c4009
	movq	%r13, oR13(%rdi)
Packit 6c4009
	movq	%r14, oR14(%rdi)
Packit 6c4009
	movq	%r15, oR15(%rdi)
Packit 6c4009
Packit 6c4009
	movq	%rdi, oRDI(%rdi)
Packit 6c4009
	movq	%rsi, oRSI(%rdi)
Packit 6c4009
	movq	%rdx, oRDX(%rdi)
Packit 6c4009
	movq	%rcx, oRCX(%rdi)
Packit 6c4009
	movq	%r8, oR8(%rdi)
Packit 6c4009
	movq	%r9, oR9(%rdi)
Packit 6c4009
Packit 6c4009
	movq	(%rsp), %rcx
Packit 6c4009
	movq	%rcx, oRIP(%rdi)
Packit 6c4009
	leaq	8(%rsp), %rcx		/* Exclude the return address.  */
Packit 6c4009
	movq	%rcx, oRSP(%rdi)
Packit 6c4009
Packit 6c4009
#if SHSTK_ENABLED
Packit 6c4009
	/* Check if shadow stack is enabled.  */
Packit 6c4009
	testl	$X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
Packit 6c4009
	jz	L(no_shstk)
Packit 6c4009
Packit 6c4009
	/* Save RDI in RDX which won't be clobbered by syscall.  */
Packit 6c4009
	movq	%rdi, %rdx
Packit 6c4009
Packit 6c4009
	xorl	%eax, %eax
Packit 6c4009
	cmpq	%fs:SSP_BASE_OFFSET, %rax
Packit 6c4009
	jnz	L(shadow_stack_bound_recorded)
Packit 6c4009
Packit 6c4009
	/* Get the base address and size of the default shadow stack
Packit 6c4009
	   which must be the current shadow stack since nothing has
Packit 6c4009
	   been recorded yet.  */
Packit 6c4009
	sub	$24, %RSP_LP
Packit 6c4009
	mov	%RSP_LP, %RSI_LP
Packit 6c4009
	movl	$ARCH_CET_STATUS, %edi
Packit 6c4009
	movl	$__NR_arch_prctl, %eax
Packit 6c4009
	syscall
Packit 6c4009
	testq	%rax, %rax
Packit 6c4009
	jz	L(continue_no_err)
Packit 6c4009
Packit 6c4009
	/* This should never happen.  */
Packit 6c4009
	hlt
Packit 6c4009
Packit 6c4009
L(continue_no_err):
Packit 6c4009
	/* Record the base of the current shadow stack.  */
Packit 6c4009
	movq	8(%rsp), %rax
Packit 6c4009
	movq	%rax, %fs:SSP_BASE_OFFSET
Packit 6c4009
	add	$24, %RSP_LP
Packit 6c4009
Packit 6c4009
	/* Restore RDI.  */
Packit 6c4009
	movq	%rdx, %rdi
Packit 6c4009
Packit 6c4009
L(shadow_stack_bound_recorded):
Packit 6c4009
	/* Get the current shadow stack pointer.  */
Packit 6c4009
	rdsspq	%rax
Packit 6c4009
	/* NB: Save the caller's shadow stack so that we can jump back
Packit 6c4009
	   to the caller directly.  */
Packit 6c4009
	addq	$8, %rax
Packit 6c4009
	movq	%rax, oSSP(%rdx)
Packit 6c4009
Packit 6c4009
	/* Save the current shadow stack base in ucontext.  */
Packit 6c4009
	movq	%fs:SSP_BASE_OFFSET, %rax
Packit 6c4009
	movq	%rax, (oSSP + 8)(%rdi)
Packit 6c4009
Packit 6c4009
L(no_shstk):
Packit 6c4009
#endif
Packit 6c4009
	/* We have separate floating-point register content memory on the
Packit 6c4009
	   stack.  We use the __fpregs_mem block in the context.  Set the
Packit 6c4009
	   links up correctly.  */
Packit 6c4009
Packit 6c4009
	leaq	oFPREGSMEM(%rdi), %rcx
Packit 6c4009
	movq	%rcx, oFPREGS(%rdi)
Packit 6c4009
	/* Save the floating-point environment.  */
Packit 6c4009
	fnstenv	(%rcx)
Packit 6c4009
	fldenv	(%rcx)
Packit 6c4009
	stmxcsr oMXCSR(%rdi)
Packit 6c4009
Packit 6c4009
	/* Save the current signal mask with
Packit 6c4009
	   rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8).  */
Packit 6c4009
	leaq	oSIGMASK(%rdi), %rdx
Packit 6c4009
	xorl	%esi,%esi
Packit 6c4009
#if SIG_BLOCK == 0
Packit 6c4009
	xorl	%edi, %edi
Packit 6c4009
#else
Packit 6c4009
	movl	$SIG_BLOCK, %edi
Packit 6c4009
#endif
Packit 6c4009
	movl	$_NSIG8,%r10d
Packit 6c4009
	movl	$__NR_rt_sigprocmask, %eax
Packit 6c4009
	syscall
Packit 6c4009
	cmpq	$-4095, %rax		/* Check %rax for error.  */
Packit 6c4009
	jae	SYSCALL_ERROR_LABEL	/* Jump to error handler if error.  */
Packit 6c4009
Packit 6c4009
	/* All done, return 0 for success.  */
Packit 6c4009
	xorl	%eax, %eax
Packit 6c4009
	ret
Packit 6c4009
PSEUDO_END(__getcontext)
Packit 6c4009
Packit 6c4009
weak_alias (__getcontext, getcontext)