1 | /* Save current context. |
2 | Copyright (C) 2002-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 <asm/prctl.h> |
21 | |
22 | #include "ucontext_i.h" |
23 | |
24 | /* int __getcontext (ucontext_t *ucp) |
25 | |
26 | Saves the machine context in UCP such that when it is activated, |
27 | it appears as if __getcontext() returned again. |
28 | |
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. */ |
32 | |
33 | |
34 | ENTRY(__getcontext) |
35 | /* Save the preserved registers, the registers used for passing |
36 | args, and the return address. */ |
37 | movq %rbx, oRBX(%rdi) |
38 | movq %rbp, oRBP(%rdi) |
39 | movq %r12, oR12(%rdi) |
40 | movq %r13, oR13(%rdi) |
41 | movq %r14, oR14(%rdi) |
42 | movq %r15, oR15(%rdi) |
43 | |
44 | movq %rdi, oRDI(%rdi) |
45 | movq %rsi, oRSI(%rdi) |
46 | movq %rdx, oRDX(%rdi) |
47 | movq %rcx, oRCX(%rdi) |
48 | movq %r8, oR8(%rdi) |
49 | movq %r9, oR9(%rdi) |
50 | |
51 | movq (%rsp), %rcx |
52 | movq %rcx, oRIP(%rdi) |
53 | leaq 8(%rsp), %rcx /* Exclude the return address. */ |
54 | movq %rcx, oRSP(%rdi) |
55 | |
56 | #if SHSTK_ENABLED |
57 | /* Check if shadow stack is enabled. */ |
58 | testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET |
59 | jz L(no_shstk) |
60 | |
61 | xorl %eax, %eax |
62 | cmpq %fs:SSP_BASE_OFFSET, %rax |
63 | jnz L(shadow_stack_bound_recorded) |
64 | |
65 | /* When the shadow stack base is unset, the default shadow |
66 | stack is in use. Use the current shadow stack pointer |
67 | as the marker for the default shadow stack. */ |
68 | rdsspq %rax |
69 | movq %rax, %fs:SSP_BASE_OFFSET |
70 | |
71 | L(shadow_stack_bound_recorded): |
72 | /* Get the current shadow stack pointer. */ |
73 | rdsspq %rax |
74 | /* NB: Save the caller's shadow stack so that we can jump back |
75 | to the caller directly. */ |
76 | addq $8, %rax |
77 | movq %rax, oSSP(%rdi) |
78 | |
79 | /* Save the current shadow stack base in ucontext. */ |
80 | movq %fs:SSP_BASE_OFFSET, %rax |
81 | movq %rax, (oSSP + 8)(%rdi) |
82 | |
83 | L(no_shstk): |
84 | #endif |
85 | /* We have separate floating-point register content memory on the |
86 | stack. We use the __fpregs_mem block in the context. Set the |
87 | links up correctly. */ |
88 | |
89 | leaq oFPREGSMEM(%rdi), %rcx |
90 | movq %rcx, oFPREGS(%rdi) |
91 | /* Save the floating-point environment. */ |
92 | fnstenv (%rcx) |
93 | fldenv (%rcx) |
94 | stmxcsr oMXCSR(%rdi) |
95 | |
96 | /* Save the current signal mask with |
97 | rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8). */ |
98 | leaq oSIGMASK(%rdi), %rdx |
99 | xorl %esi,%esi |
100 | #if SIG_BLOCK == 0 |
101 | xorl %edi, %edi |
102 | #else |
103 | movl $SIG_BLOCK, %edi |
104 | #endif |
105 | movl $_NSIG8,%r10d |
106 | movl $__NR_rt_sigprocmask, %eax |
107 | syscall |
108 | cmpq $-4095, %rax /* Check %rax for error. */ |
109 | jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ |
110 | |
111 | /* All done, return 0 for success. */ |
112 | xorl %eax, %eax |
113 | ret |
114 | PSEUDO_END(__getcontext) |
115 | |
116 | weak_alias (__getcontext, getcontext) |
117 | |