1/* setjmp for x86-64.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <sysdep.h>
20#include <pointer_guard.h>
21#include <jmpbuf-offsets.h>
22#include <jmp_buf-ssp.h>
23#include <asm-syntax.h>
24#include <stap-probe.h>
25
26/* Don't save shadow stack register if shadow stack isn't enabled. */
27#if !SHSTK_ENABLED
28# undef SHADOW_STACK_POINTER_OFFSET
29#endif
30
31ENTRY (__sigsetjmp)
32 /* Save registers. */
33 movq %rbx, (JB_RBX*8)(%rdi)
34#ifdef PTR_MANGLE
35# ifdef __ILP32__
36 /* Save the high bits of %rbp first, since PTR_MANGLE will
37 only handle the low bits but we cannot presume %rbp is
38 being used as a pointer and truncate it. Here we write all
39 of %rbp, but the low bits will be overwritten below. */
40 movq %rbp, (JB_RBP*8)(%rdi)
41# endif
42 mov %RBP_LP, %RAX_LP
43 PTR_MANGLE (%RAX_LP)
44 mov %RAX_LP, (JB_RBP*8)(%rdi)
45#else
46 movq %rbp, (JB_RBP*8)(%rdi)
47#endif
48 movq %r12, (JB_R12*8)(%rdi)
49 movq %r13, (JB_R13*8)(%rdi)
50 movq %r14, (JB_R14*8)(%rdi)
51 movq %r15, (JB_R15*8)(%rdi)
52 lea 8(%rsp), %RDX_LP /* Save SP as it will be after we return. */
53#ifdef PTR_MANGLE
54 PTR_MANGLE (%RDX_LP)
55#endif
56 movq %rdx, (JB_RSP*8)(%rdi)
57 mov (%rsp), %RAX_LP /* Save PC we are returning to now. */
58 LIBC_PROBE (setjmp, 3, LP_SIZE@%RDI_LP, -4@%esi, LP_SIZE@%RAX_LP)
59#ifdef PTR_MANGLE
60 PTR_MANGLE (%RAX_LP)
61#endif
62 movq %rax, (JB_PC*8)(%rdi)
63
64#ifdef SHADOW_STACK_POINTER_OFFSET
65# if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
66 /* Check if Shadow Stack is enabled. */
67 testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
68 jz L(skip_ssp)
69# else
70 xorl %eax, %eax
71# endif
72 /* Get the current Shadow-Stack-Pointer and save it. */
73 rdsspq %rax
74 movq %rax, SHADOW_STACK_POINTER_OFFSET(%rdi)
75# if IS_IN (libc) && defined SHARED && defined FEATURE_1_OFFSET
76L(skip_ssp):
77# endif
78#endif
79#if IS_IN (rtld)
80 /* In ld.so we never save the signal mask. */
81 xorl %eax, %eax
82 retq
83#else
84 /* Make a tail call to __sigjmp_save; it takes the same args. */
85 jmp __sigjmp_save
86#endif
87END (__sigsetjmp)
88hidden_def (__sigsetjmp)
89

source code of glibc/sysdeps/x86_64/setjmp.S