|
Packit |
6c4009 |
/* Install given context.
|
|
Packit |
6c4009 |
Copyright (C) 2001-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>, 2001.
|
|
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>
|
|
|
9498ea |
#include <asm/prctl.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include "ucontext_i.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY(__setcontext)
|
|
Packit |
6c4009 |
/* Load address of the context data structure. */
|
|
Packit |
6c4009 |
movl 4(%esp), %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the current signal mask. Note that we preserve EBX in case
|
|
Packit |
6c4009 |
the system call fails and we return from the function with an
|
|
Packit |
6c4009 |
error. */
|
|
Packit |
6c4009 |
pushl %ebx
|
|
Packit |
6c4009 |
cfi_adjust_cfa_offset (4)
|
|
Packit |
6c4009 |
xorl %edx, %edx
|
|
Packit |
6c4009 |
leal oSIGMASK(%eax), %ecx
|
|
Packit |
6c4009 |
movl $SIG_SETMASK, %ebx
|
|
Packit |
6c4009 |
cfi_rel_offset (ebx, 0)
|
|
Packit |
6c4009 |
movl $__NR_sigprocmask, %eax
|
|
Packit |
6c4009 |
ENTER_KERNEL
|
|
Packit |
6c4009 |
popl %ebx
|
|
Packit |
6c4009 |
cfi_adjust_cfa_offset (-4)
|
|
Packit |
6c4009 |
cfi_restore (ebx)
|
|
Packit |
6c4009 |
cmpl $-4095, %eax /* Check %eax for error. */
|
|
Packit |
6c4009 |
jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* EAX was modified, reload it. */
|
|
Packit |
6c4009 |
movl 4(%esp), %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Restore the floating-point context. Not the registers, only the
|
|
Packit |
6c4009 |
rest. */
|
|
Packit |
6c4009 |
movl oFPREGS(%eax), %ecx
|
|
Packit |
6c4009 |
fldenv (%ecx)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Restore the FS segment register. We don't touch the GS register
|
|
Packit |
6c4009 |
since it is used for threads. */
|
|
Packit |
6c4009 |
movl oFS(%eax), %ecx
|
|
Packit |
6c4009 |
movw %cx, %fs
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Load the new stack pointer. */
|
|
Packit |
6c4009 |
cfi_def_cfa (eax, 0)
|
|
Packit |
6c4009 |
cfi_offset (edi, oEDI)
|
|
Packit |
6c4009 |
cfi_offset (esi, oESI)
|
|
Packit |
6c4009 |
cfi_offset (ebp, oEBP)
|
|
Packit |
6c4009 |
cfi_offset (ebx, oEBX)
|
|
Packit |
6c4009 |
movl oESP(%eax), %esp
|
|
Packit |
6c4009 |
|
|
|
9498ea |
#if SHSTK_ENABLED
|
|
|
9498ea |
/* Check if Shadow Stack is enabled. */
|
|
|
9498ea |
testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
|
|
|
9498ea |
jz L(no_shstk)
|
|
|
9498ea |
|
|
|
9498ea |
/* If the base of the target shadow stack is the same as the
|
|
|
9498ea |
base of the current shadow stack, we unwind the shadow
|
|
|
9498ea |
stack. Otherwise it is a stack switch and we look for a
|
|
|
9498ea |
restore token. */
|
|
|
9498ea |
movl oSSP(%eax), %esi
|
|
|
9498ea |
movl %esi, %edi
|
|
|
9498ea |
|
|
|
9498ea |
/* Get the base of the target shadow stack. */
|
|
|
9498ea |
movl (oSSP + 4)(%eax), %ecx
|
|
|
9498ea |
cmpl %gs:SSP_BASE_OFFSET, %ecx
|
|
|
9498ea |
je L(unwind_shadow_stack)
|
|
|
9498ea |
|
|
|
9498ea |
/* Align the saved original shadow stack pointer to the next
|
|
|
9498ea |
8 byte aligned boundary. */
|
|
|
9498ea |
andl $-8, %esi
|
|
|
9498ea |
|
|
|
9498ea |
L(find_restore_token_loop):
|
|
|
9498ea |
/* Look for a restore token. */
|
|
|
9498ea |
movl -8(%esi), %ebx
|
|
|
9498ea |
andl $-8, %ebx
|
|
|
9498ea |
cmpl %esi, %ebx
|
|
|
9498ea |
je L(restore_shadow_stack)
|
|
|
9498ea |
|
|
|
9498ea |
/* Try the next slot. */
|
|
|
9498ea |
subl $8, %esi
|
|
|
9498ea |
jmp L(find_restore_token_loop)
|
|
|
9498ea |
|
|
|
9498ea |
L(restore_shadow_stack):
|
|
|
9498ea |
/* Pop return address from the shadow stack since setcontext
|
|
|
9498ea |
will not return. */
|
|
|
9498ea |
movl $1, %ebx
|
|
|
9498ea |
incsspd %ebx
|
|
|
9498ea |
|
|
|
9498ea |
/* Use the restore stoken to restore the target shadow stack. */
|
|
|
9498ea |
rstorssp -8(%esi)
|
|
|
9498ea |
|
|
|
9498ea |
/* Save the restore token on the old shadow stack. NB: This
|
|
|
9498ea |
restore token may be checked by setcontext or swapcontext
|
|
|
9498ea |
later. */
|
|
|
9498ea |
saveprevssp
|
|
|
9498ea |
|
|
|
9498ea |
/* Record the new shadow stack base that was switched to. */
|
|
|
9498ea |
movl (oSSP + 4)(%eax), %ebx
|
|
|
9498ea |
movl %ebx, %gs:SSP_BASE_OFFSET
|
|
|
9498ea |
|
|
|
9498ea |
L(unwind_shadow_stack):
|
|
|
9498ea |
rdsspd %ebx
|
|
|
9498ea |
subl %edi, %ebx
|
|
|
9498ea |
je L(skip_unwind_shadow_stack)
|
|
|
9498ea |
negl %ebx
|
|
|
9498ea |
shrl $2, %ebx
|
|
|
9498ea |
movl $255, %esi
|
|
|
9498ea |
L(loop):
|
|
|
9498ea |
cmpl %esi, %ebx
|
|
|
9498ea |
cmovb %ebx, %esi
|
|
|
9498ea |
incsspd %esi
|
|
|
9498ea |
subl %esi, %ebx
|
|
|
9498ea |
ja L(loop)
|
|
|
9498ea |
|
|
|
9498ea |
L(skip_unwind_shadow_stack):
|
|
|
9498ea |
|
|
|
9498ea |
/* Load the values of all the preserved registers (except ESP). */
|
|
|
9498ea |
movl oEDI(%eax), %edi
|
|
|
9498ea |
movl oESI(%eax), %esi
|
|
|
9498ea |
movl oEBP(%eax), %ebp
|
|
|
9498ea |
movl oEBX(%eax), %ebx
|
|
|
9498ea |
|
|
|
9498ea |
/* Get the return address set with getcontext. */
|
|
|
9498ea |
movl oEIP(%eax), %ecx
|
|
|
9498ea |
|
|
|
9498ea |
/* Check if return address is valid for the case when setcontext
|
|
|
9498ea |
is invoked from L(exitcode) with linked context. */
|
|
|
9498ea |
rdsspd %eax
|
|
|
9498ea |
cmpl (%eax), %ecx
|
|
|
9498ea |
/* Clear EAX to indicate success. NB: Don't use xorl to keep
|
|
|
9498ea |
EFLAGS for jne. */
|
|
|
9498ea |
movl $0, %eax
|
|
|
9498ea |
jne L(jmp)
|
|
|
9498ea |
/* Return to the new context if return address valid. */
|
|
|
9498ea |
pushl %ecx
|
|
|
9498ea |
ret
|
|
|
9498ea |
|
|
|
9498ea |
L(jmp):
|
|
|
9498ea |
/* Jump to the new context directly. */
|
|
|
9498ea |
jmp *%ecx
|
|
|
9498ea |
|
|
|
9498ea |
L(no_shstk):
|
|
|
9498ea |
#endif
|
|
|
9498ea |
|
|
|
9498ea |
/* Fetch the address to return to. */
|
|
|
9498ea |
movl oEIP(%eax), %ecx
|
|
|
9498ea |
|
|
Packit |
6c4009 |
/* Push the return address on the new stack so we can return there. */
|
|
Packit |
6c4009 |
pushl %ecx
|
|
Packit |
6c4009 |
|
|
|
28076c |
/* Load the values of all the preserved registers (except ESP). */
|
|
Packit |
6c4009 |
movl oEDI(%eax), %edi
|
|
Packit |
6c4009 |
movl oESI(%eax), %esi
|
|
Packit |
6c4009 |
movl oEBP(%eax), %ebp
|
|
Packit |
6c4009 |
movl oEBX(%eax), %ebx
|
|
|
28076c |
|
|
|
28076c |
/* All done, return 0 for success. */
|
|
|
28076c |
xorl %eax, %eax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* End FDE here, we fall into another context. */
|
|
Packit |
6c4009 |
cfi_endproc
|
|
Packit |
6c4009 |
cfi_startproc
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The following 'ret' will pop the address of the code and jump
|
|
Packit |
6c4009 |
to it. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
PSEUDO_END(__setcontext)
|
|
Packit |
6c4009 |
libc_hidden_def (__setcontext)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
weak_alias (__setcontext, setcontext)
|