| 1 | /* Save current context. |
| 2 | Copyright (C) 2002-2020 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Andreas Jaeger <aj@suse.de>, 2002. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <sysdep.h> |
| 21 | #include <asm/prctl.h> |
| 22 | |
| 23 | #include "ucontext_i.h" |
| 24 | |
| 25 | /* int __getcontext (ucontext_t *ucp) |
| 26 | |
| 27 | Saves the machine context in UCP such that when it is activated, |
| 28 | it appears as if __getcontext() returned again. |
| 29 | |
| 30 | This implementation is intended to be used for *synchronous* context |
| 31 | switches only. Therefore, it does not have to save anything |
| 32 | other than the PRESERVED state. */ |
| 33 | |
| 34 | |
| 35 | ENTRY(__getcontext) |
| 36 | /* Save the preserved registers, the registers used for passing |
| 37 | args, and the return address. */ |
| 38 | movq %rbx, oRBX(%rdi) |
| 39 | movq %rbp, oRBP(%rdi) |
| 40 | movq %r12, oR12(%rdi) |
| 41 | movq %r13, oR13(%rdi) |
| 42 | movq %r14, oR14(%rdi) |
| 43 | movq %r15, oR15(%rdi) |
| 44 | |
| 45 | movq %rdi, oRDI(%rdi) |
| 46 | movq %rsi, oRSI(%rdi) |
| 47 | movq %rdx, oRDX(%rdi) |
| 48 | movq %rcx, oRCX(%rdi) |
| 49 | movq %r8, oR8(%rdi) |
| 50 | movq %r9, oR9(%rdi) |
| 51 | |
| 52 | movq (%rsp), %rcx |
| 53 | movq %rcx, oRIP(%rdi) |
| 54 | leaq 8(%rsp), %rcx /* Exclude the return address. */ |
| 55 | movq %rcx, oRSP(%rdi) |
| 56 | |
| 57 | #if SHSTK_ENABLED |
| 58 | /* Check if shadow stack is enabled. */ |
| 59 | testl $X86_FEATURE_1_SHSTK, %fs:FEATURE_1_OFFSET |
| 60 | jz L(no_shstk) |
| 61 | |
| 62 | /* Save RDI in RDX which won't be clobbered by syscall. */ |
| 63 | movq %rdi, %rdx |
| 64 | |
| 65 | xorl %eax, %eax |
| 66 | cmpq %fs:SSP_BASE_OFFSET, %rax |
| 67 | jnz L(shadow_stack_bound_recorded) |
| 68 | |
| 69 | /* Get the base address and size of the default shadow stack |
| 70 | which must be the current shadow stack since nothing has |
| 71 | been recorded yet. */ |
| 72 | sub $24, %RSP_LP |
| 73 | mov %RSP_LP, %RSI_LP |
| 74 | movl $ARCH_CET_STATUS, %edi |
| 75 | movl $__NR_arch_prctl, %eax |
| 76 | syscall |
| 77 | testq %rax, %rax |
| 78 | jz L(continue_no_err) |
| 79 | |
| 80 | /* This should never happen. */ |
| 81 | hlt |
| 82 | |
| 83 | L(continue_no_err): |
| 84 | /* Record the base of the current shadow stack. */ |
| 85 | movq 8(%rsp), %rax |
| 86 | movq %rax, %fs:SSP_BASE_OFFSET |
| 87 | add $24, %RSP_LP |
| 88 | |
| 89 | /* Restore RDI. */ |
| 90 | movq %rdx, %rdi |
| 91 | |
| 92 | L(shadow_stack_bound_recorded): |
| 93 | /* Get the current shadow stack pointer. */ |
| 94 | rdsspq %rax |
| 95 | /* NB: Save the caller's shadow stack so that we can jump back |
| 96 | to the caller directly. */ |
| 97 | addq $8, %rax |
| 98 | movq %rax, oSSP(%rdx) |
| 99 | |
| 100 | /* Save the current shadow stack base in ucontext. */ |
| 101 | movq %fs:SSP_BASE_OFFSET, %rax |
| 102 | movq %rax, (oSSP + 8)(%rdi) |
| 103 | |
| 104 | L(no_shstk): |
| 105 | #endif |
| 106 | /* We have separate floating-point register content memory on the |
| 107 | stack. We use the __fpregs_mem block in the context. Set the |
| 108 | links up correctly. */ |
| 109 | |
| 110 | leaq oFPREGSMEM(%rdi), %rcx |
| 111 | movq %rcx, oFPREGS(%rdi) |
| 112 | /* Save the floating-point environment. */ |
| 113 | fnstenv (%rcx) |
| 114 | fldenv (%rcx) |
| 115 | stmxcsr oMXCSR(%rdi) |
| 116 | |
| 117 | /* Save the current signal mask with |
| 118 | rt_sigprocmask (SIG_BLOCK, NULL, set,_NSIG/8). */ |
| 119 | leaq oSIGMASK(%rdi), %rdx |
| 120 | xorl %esi,%esi |
| 121 | #if SIG_BLOCK == 0 |
| 122 | xorl %edi, %edi |
| 123 | #else |
| 124 | movl $SIG_BLOCK, %edi |
| 125 | #endif |
| 126 | movl $_NSIG8,%r10d |
| 127 | movl $__NR_rt_sigprocmask, %eax |
| 128 | syscall |
| 129 | cmpq $-4095, %rax /* Check %rax for error. */ |
| 130 | jae SYSCALL_ERROR_LABEL /* Jump to error handler if error. */ |
| 131 | |
| 132 | /* All done, return 0 for success. */ |
| 133 | xorl %eax, %eax |
| 134 | ret |
| 135 | PSEUDO_END(__getcontext) |
| 136 | |
| 137 | weak_alias (__getcontext, getcontext) |
| 138 | |