1 /* Save current context.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
20 #include <asm/prctl.h>
22 #include "ucontext_i.h"
24 /* int __getcontext (ucontext_t *ucp)
26 Saves the machine context in UCP such that when it is activated,
27 it appears as if __getcontext() returned again.
29 This implementation is intended to be used for *synchronous* context
30 switches only. Therefore, it does not have to save anything
31 other than the PRESERVED state. */
35 /* Save the preserved registers, the registers used for passing
36 args, and the return address. */
53 leaq 8(%rsp), %rcx /* Exclude the return address. */
57 /* Check if shadow stack is enabled. */
58 testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET
61 /* Save RDI in RDX which won't be clobbered by syscall. */
65 cmpq %fs:SSP_BASE_OFFSET, %rax
66 jnz L(shadow_stack_bound_recorded)
68 /* Get the base address and size of the default shadow stack
69 which must be the current shadow stack since nothing has
73 movl $ARCH_CET_STATUS, %edi
74 movl $__NR_arch_prctl, %eax
79 /* This should never happen. */
83 /* Record the base of the current shadow stack. */
85 movq %rax, %fs:SSP_BASE_OFFSET
91 L(shadow_stack_bound_recorded):
92 /* Get the current shadow stack pointer. */
94 /* NB: Save the caller's shadow stack so that we can jump back
95 to the caller directly. */
99 /* Save the current shadow stack base in ucontext. */
100 movq %fs:SSP_BASE_OFFSET, %rax
101 movq %rax, (oSSP + 8)(%rdi)
105 /* We have separate floating-point register content memory on the
106 stack. We use the __fpregs_mem block in the context. Set the
107 links up correctly. */
109 leaq oFPREGSMEM(%rdi), %rcx
110 movq %rcx, oFPREGS(%rdi)
111 /* Save the floating-point environment. */
116 /* Save the current signal mask with
117 rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8). */
118 leaq oSIGMASK(%rdi), %rdx
123 movl $SIG_BLOCK, %edi
126 movl $__NR_rt_sigprocmask, %eax
128 cmpq $-4095, %rax /* Check %rax for error. */
129 jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */
131 /* All done, return 0 for success. */
134 PSEUDO_END(__getcontext)
136 weak_alias (__getcontext, getcontext)