|
Packit Service |
82fcde |
/* Save current context.
|
|
Packit Service |
82fcde |
Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Andreas Jaeger <aj@suse.de>, 2002.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <sysdep.h>
|
|
Packit Service |
82fcde |
#include <asm/prctl.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include "ucontext_i.h"
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* int __getcontext (ucontext_t *ucp)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
Saves the machine context in UCP such that when it is activated,
|
|
Packit Service |
82fcde |
it appears as if __getcontext() returned again.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
This implementation is intended to be used for *synchronous* context
|
|
Packit Service |
82fcde |
switches only. Therefore, it does not have to save anything
|
|
Packit Service |
82fcde |
other than the PRESERVED state. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
ENTRY(__getcontext)
|
|
Packit Service |
82fcde |
/* Save the preserved registers, the registers used for passing
|
|
Packit Service |
82fcde |
args, and the return address. */
|
|
Packit Service |
82fcde |
movq %rbx, oRBX(%rdi)
|
|
Packit Service |
82fcde |
movq %rbp, oRBP(%rdi)
|
|
Packit Service |
82fcde |
movq %r12, oR12(%rdi)
|
|
Packit Service |
82fcde |
movq %r13, oR13(%rdi)
|
|
Packit Service |
82fcde |
movq %r14, oR14(%rdi)
|
|
Packit Service |
82fcde |
movq %r15, oR15(%rdi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movq %rdi, oRDI(%rdi)
|
|
Packit Service |
82fcde |
movq %rsi, oRSI(%rdi)
|
|
Packit Service |
82fcde |
movq %rdx, oRDX(%rdi)
|
|
Packit Service |
82fcde |
movq %rcx, oRCX(%rdi)
|
|
Packit Service |
82fcde |
movq %r8, oR8(%rdi)
|
|
Packit Service |
82fcde |
movq %r9, oR9(%rdi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movq (%rsp), %rcx
|
|
Packit Service |
82fcde |
movq %rcx, oRIP(%rdi)
|
|
Packit Service |
82fcde |
leaq 8(%rsp), %rcx /* Exclude the return address. */
|
|
Packit Service |
82fcde |
movq %rcx, oRSP(%rdi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#if SHSTK_ENABLED
|
|
Packit Service |
82fcde |
/* Check if shadow stack is enabled. */
|
|
Packit Service |
82fcde |
testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
|
|
Packit Service |
82fcde |
jz L(no_shstk)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Save RDI in RDX which won't be clobbered by syscall. */
|
|
Packit Service |
82fcde |
movq %rdi, %rdx
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
xorl %eax, %eax
|
|
Packit Service |
82fcde |
cmpq %fs:SSP_BASE_OFFSET, %rax
|
|
Packit Service |
82fcde |
jnz L(shadow_stack_bound_recorded)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Get the base address and size of the default shadow stack
|
|
Packit Service |
82fcde |
which must be the current shadow stack since nothing has
|
|
Packit Service |
82fcde |
been recorded yet. */
|
|
Packit Service |
82fcde |
sub $24, %RSP_LP
|
|
Packit Service |
82fcde |
mov %RSP_LP, %RSI_LP
|
|
Packit Service |
82fcde |
movl $ARCH_CET_STATUS, %edi
|
|
Packit Service |
82fcde |
movl $__NR_arch_prctl, %eax
|
|
Packit Service |
82fcde |
syscall
|
|
Packit Service |
82fcde |
testq %rax, %rax
|
|
Packit Service |
82fcde |
jz L(continue_no_err)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* This should never happen. */
|
|
Packit Service |
82fcde |
hlt
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(continue_no_err):
|
|
Packit Service |
82fcde |
/* Record the base of the current shadow stack. */
|
|
Packit Service |
82fcde |
movq 8(%rsp), %rax
|
|
Packit Service |
82fcde |
movq %rax, %fs:SSP_BASE_OFFSET
|
|
Packit Service |
82fcde |
add $24, %RSP_LP
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Restore RDI. */
|
|
Packit Service |
82fcde |
movq %rdx, %rdi
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(shadow_stack_bound_recorded):
|
|
Packit Service |
82fcde |
/* Get the current shadow stack pointer. */
|
|
Packit Service |
82fcde |
rdsspq %rax
|
|
Packit Service |
82fcde |
/* NB: Save the caller's shadow stack so that we can jump back
|
|
Packit Service |
82fcde |
to the caller directly. */
|
|
Packit Service |
82fcde |
addq $8, %rax
|
|
Packit Service |
82fcde |
movq %rax, oSSP(%rdx)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Save the current shadow stack base in ucontext. */
|
|
Packit Service |
82fcde |
movq %fs:SSP_BASE_OFFSET, %rax
|
|
Packit Service |
82fcde |
movq %rax, (oSSP + 8)(%rdi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(no_shstk):
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
/* We have separate floating-point register content memory on the
|
|
Packit Service |
82fcde |
stack. We use the __fpregs_mem block in the context. Set the
|
|
Packit Service |
82fcde |
links up correctly. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
leaq oFPREGSMEM(%rdi), %rcx
|
|
Packit Service |
82fcde |
movq %rcx, oFPREGS(%rdi)
|
|
Packit Service |
82fcde |
/* Save the floating-point environment. */
|
|
Packit Service |
82fcde |
fnstenv (%rcx)
|
|
Packit Service |
82fcde |
fldenv (%rcx)
|
|
Packit Service |
82fcde |
stmxcsr oMXCSR(%rdi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Save the current signal mask with
|
|
Packit Service |
82fcde |
rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8). */
|
|
Packit Service |
82fcde |
leaq oSIGMASK(%rdi), %rdx
|
|
Packit Service |
82fcde |
xorl %esi,%esi
|
|
Packit Service |
82fcde |
#if SIG_BLOCK == 0
|
|
Packit Service |
82fcde |
xorl %edi, %edi
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
movl $SIG_BLOCK, %edi
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
movl $_NSIG8,%r10d
|
|
Packit Service |
82fcde |
movl $__NR_rt_sigprocmask, %eax
|
|
Packit Service |
82fcde |
syscall
|
|
Packit Service |
82fcde |
cmpq $-4095, %rax /* Check %rax for error. */
|
|
Packit Service |
82fcde |
jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* All done, return 0 for success. */
|
|
Packit Service |
82fcde |
xorl %eax, %eax
|
|
Packit Service |
82fcde |
ret
|
|
Packit Service |
82fcde |
PSEUDO_END(__getcontext)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
weak_alias (__getcontext, getcontext)
|