|
Packit |
6c4009 |
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
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 <jmpbuf-offsets.h>
|
|
Packit |
6c4009 |
#include <jmp_buf-ssp.h>
|
|
Packit |
6c4009 |
#include <asm-syntax.h>
|
|
Packit |
6c4009 |
#include <stap-probe.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Don't restore shadow stack register if
|
|
Packit |
6c4009 |
1. Shadow stack isn't enabled. Or
|
|
Packit |
6c4009 |
2. __longjmp is defined for __longjmp_cancel.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
#if !SHSTK_ENABLED || defined __longjmp
|
|
Packit |
6c4009 |
# undef SHADOW_STACK_POINTER_OFFSET
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Jump to the position specified by ENV, causing the
|
|
Packit |
6c4009 |
setjmp call there to return VAL, or 1 if VAL is 0.
|
|
Packit |
6c4009 |
void __longjmp (__jmp_buf env, int val). */
|
|
Packit |
6c4009 |
.text
|
|
Packit |
6c4009 |
ENTRY(__longjmp)
|
|
Packit |
6c4009 |
/* Restore registers. */
|
|
Packit |
6c4009 |
mov (JB_RSP*8)(%rdi),%R8_LP
|
|
Packit |
6c4009 |
mov (JB_RBP*8)(%rdi),%R9_LP
|
|
Packit |
6c4009 |
mov (JB_PC*8)(%rdi),%RDX_LP
|
|
Packit |
6c4009 |
#ifdef PTR_DEMANGLE
|
|
Packit |
6c4009 |
PTR_DEMANGLE (%R8_LP)
|
|
Packit |
6c4009 |
PTR_DEMANGLE (%R9_LP)
|
|
Packit |
6c4009 |
PTR_DEMANGLE (%RDX_LP)
|
|
Packit |
6c4009 |
# ifdef __ILP32__
|
|
Packit |
6c4009 |
/* We ignored the high bits of the %rbp value because only the low
|
|
Packit |
6c4009 |
bits are mangled. But we cannot presume that %rbp is being used
|
|
Packit |
6c4009 |
as a pointer and truncate it, so recover the high bits. */
|
|
Packit |
6c4009 |
movl (JB_RBP*8 + 4)(%rdi), %eax
|
|
Packit |
6c4009 |
shlq $32, %rax
|
|
Packit |
6c4009 |
orq %rax, %r9
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
#ifdef SHADOW_STACK_POINTER_OFFSET
|
|
Packit |
6c4009 |
# if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
|
|
Packit |
6c4009 |
/* Check if Shadow Stack is enabled. */
|
|
Packit |
6c4009 |
testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
|
|
Packit |
6c4009 |
jz L(skip_ssp)
|
|
Packit |
6c4009 |
# else
|
|
Packit |
6c4009 |
xorl %eax, %eax
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
/* Check and adjust the Shadow-Stack-Pointer. */
|
|
Packit |
6c4009 |
/* Get the current ssp. */
|
|
Packit |
6c4009 |
rdsspq %rax
|
|
Packit |
6c4009 |
/* And compare it with the saved ssp value. */
|
|
Packit |
6c4009 |
subq SHADOW_STACK_POINTER_OFFSET(%rdi), %rax
|
|
Packit |
6c4009 |
je L(skip_ssp)
|
|
Packit |
6c4009 |
/* Count the number of frames to adjust and adjust it
|
|
Packit |
6c4009 |
with incssp instruction. The instruction can adjust
|
|
Packit |
6c4009 |
the ssp by [0..255] value only thus use a loop if
|
|
Packit |
6c4009 |
the number of frames is bigger than 255. */
|
|
Packit |
6c4009 |
negq %rax
|
|
Packit |
6c4009 |
shrq $3, %rax
|
|
Packit |
6c4009 |
/* NB: We saved Shadow-Stack-Pointer of setjmp. Since we are
|
|
Packit |
6c4009 |
restoring Shadow-Stack-Pointer of setjmp's caller, we
|
|
Packit |
6c4009 |
need to unwind shadow stack by one more frame. */
|
|
Packit |
6c4009 |
addq $1, %rax
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
movl $255, %ebx
|
|
Packit |
6c4009 |
L(loop):
|
|
Packit |
6c4009 |
cmpq %rbx, %rax
|
|
Packit |
6c4009 |
cmovb %rax, %rbx
|
|
Packit |
6c4009 |
incsspq %rbx
|
|
Packit |
6c4009 |
subq %rbx, %rax
|
|
Packit |
6c4009 |
ja L(loop)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
L(skip_ssp):
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
LIBC_PROBE (longjmp, 3, LP_SIZE@%RDI_LP, -4@%esi, LP_SIZE@%RDX_LP)
|
|
Packit |
6c4009 |
/* We add unwind information for the target here. */
|
|
Packit |
6c4009 |
cfi_def_cfa(%rdi, 0)
|
|
Packit |
6c4009 |
cfi_register(%rsp,%r8)
|
|
Packit |
6c4009 |
cfi_register(%rbp,%r9)
|
|
Packit |
6c4009 |
cfi_register(%rip,%rdx)
|
|
Packit |
6c4009 |
cfi_offset(%rbx,JB_RBX*8)
|
|
Packit |
6c4009 |
cfi_offset(%r12,JB_R12*8)
|
|
Packit |
6c4009 |
cfi_offset(%r13,JB_R13*8)
|
|
Packit |
6c4009 |
cfi_offset(%r14,JB_R14*8)
|
|
Packit |
6c4009 |
cfi_offset(%r15,JB_R15*8)
|
|
Packit |
6c4009 |
movq (JB_RBX*8)(%rdi),%rbx
|
|
Packit |
6c4009 |
movq (JB_R12*8)(%rdi),%r12
|
|
Packit |
6c4009 |
movq (JB_R13*8)(%rdi),%r13
|
|
Packit |
6c4009 |
movq (JB_R14*8)(%rdi),%r14
|
|
Packit |
6c4009 |
movq (JB_R15*8)(%rdi),%r15
|
|
Packit |
6c4009 |
/* Set return value for setjmp. */
|
|
Packit |
6c4009 |
mov %esi, %eax
|
|
Packit |
6c4009 |
mov %R8_LP,%RSP_LP
|
|
Packit |
6c4009 |
movq %r9,%rbp
|
|
Packit |
6c4009 |
LIBC_PROBE (longjmp_target, 3,
|
|
Packit |
6c4009 |
LP_SIZE@%RDI_LP, -4@%eax, LP_SIZE@%RDX_LP)
|
|
Packit |
6c4009 |
jmpq *%rdx
|
|
Packit |
6c4009 |
END (__longjmp)
|