| 1 | /* Copyright (C) 1991-2024 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <hurd.h> |
| 19 | #include <hurd/signal.h> |
| 20 | #include <hurd/msg.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | /* This is run on the thread stack after restoring it, to be able to |
| 24 | unlock SS off sigstack. */ |
| 25 | void |
| 26 | __sigreturn2 (struct hurd_sigstate *ss, uintptr_t *usp, |
| 27 | mach_port_t sc_reply_port) |
| 28 | { |
| 29 | mach_port_t reply_port; |
| 30 | _hurd_sigstate_unlock (ss); |
| 31 | |
| 32 | /* Destroy the MiG reply port used by the signal handler, and restore the |
| 33 | reply port in use by the thread when interrupted. |
| 34 | |
| 35 | We cannot use the original reply port for our RPCs that we do here, since |
| 36 | we could unexpectedly receive/consume a reply message meant for the user |
| 37 | (in particular, msg_sig_post_reply), and also since we would deallocate |
| 38 | the port if *our* RPC fails, which we don't want to do since the user |
| 39 | still has the old name. And so, temporarily set MACH_PORT_DEAD as our |
| 40 | reply name, and make sure destroying the port is the very last RPC we |
| 41 | do. */ |
| 42 | reply_port = THREAD_GETMEM (THREAD_SELF, reply_port); |
| 43 | THREAD_SETMEM (THREAD_SELF, reply_port, MACH_PORT_DEAD); |
| 44 | if (__glibc_likely (MACH_PORT_VALID (reply_port))) |
| 45 | (void) __mach_port_mod_refs (__mach_task_self (), reply_port, |
| 46 | MACH_PORT_RIGHT_RECEIVE, -1); |
| 47 | THREAD_SETMEM (THREAD_SELF, reply_port, sc_reply_port); |
| 48 | |
| 49 | asm volatile ( |
| 50 | /* Point the stack to the register dump. */ |
| 51 | "movq %0, %%rsp\n" |
| 52 | /* Pop off the registers. */ |
| 53 | "popq %%r8\n" |
| 54 | "popq %%r9\n" |
| 55 | "popq %%r10\n" |
| 56 | "popq %%r11\n" |
| 57 | "popq %%r12\n" |
| 58 | "popq %%r13\n" |
| 59 | "popq %%r14\n" |
| 60 | "popq %%r15\n" |
| 61 | "popq %%rdi\n" |
| 62 | "popq %%rsi\n" |
| 63 | "popq %%rbp\n" |
| 64 | "popq %%rbx\n" |
| 65 | "popq %%rdx\n" |
| 66 | "popq %%rcx\n" |
| 67 | "popq %%rax\n" |
| 68 | "popfq\n" |
| 69 | /* Restore %rip and %rsp with a single instruction. */ |
| 70 | "retq $128" : |
| 71 | : "rm" (usp)); |
| 72 | __builtin_unreachable (); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | __sigreturn (struct sigcontext *scp) |
| 77 | { |
| 78 | struct hurd_sigstate *ss; |
| 79 | struct hurd_userlink *link = (void *) &scp[1]; |
| 80 | uintptr_t *usp; |
| 81 | mach_port_t sc_reply_port; |
| 82 | |
| 83 | if (__glibc_unlikely (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK))) |
| 84 | return __hurd_fail (EINVAL); |
| 85 | |
| 86 | ss = _hurd_self_sigstate (); |
| 87 | _hurd_sigstate_lock (ss); |
| 88 | |
| 89 | /* Remove the link on the `active resources' chain added by |
| 90 | _hurd_setup_sighandler. Its purpose was to make sure |
| 91 | that we got called; now we have, it is done. */ |
| 92 | _hurd_userlink_unlink (link); |
| 93 | |
| 94 | /* Restore the set of blocked signals, and the intr_port slot. */ |
| 95 | ss->blocked = scp->sc_mask; |
| 96 | ss->intr_port = scp->sc_intr_port; |
| 97 | |
| 98 | /* Check for pending signals that were blocked by the old set. */ |
| 99 | if (_hurd_sigstate_pending (ss) & ~ss->blocked) |
| 100 | { |
| 101 | /* There are pending signals that just became unblocked. Wake up the |
| 102 | signal thread to deliver them. But first, squirrel away SCP where |
| 103 | the signal thread will notice it if it runs another handler, and |
| 104 | arrange to have us called over again in the new reality. */ |
| 105 | ss->context = scp; |
| 106 | _hurd_sigstate_unlock (ss); |
| 107 | __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ()); |
| 108 | /* If a pending signal was handled, sig_post never returned. |
| 109 | If it did return, the pending signal didn't run a handler; |
| 110 | proceed as usual. */ |
| 111 | _hurd_sigstate_lock (ss); |
| 112 | ss->context = NULL; |
| 113 | } |
| 114 | |
| 115 | if (scp->sc_onstack) |
| 116 | ss->sigaltstack.ss_flags &= ~SS_ONSTACK; |
| 117 | |
| 118 | if (scp->sc_fpused) |
| 119 | /* Restore the FPU state. Mach conveniently stores the state |
| 120 | in the format the i387 `frstor' instruction uses to restore it. */ |
| 121 | asm volatile ("frstor %0" : : "m" (scp->sc_fpsave)); |
| 122 | |
| 123 | /* Copy the registers onto the user's stack, to be able to release the |
| 124 | altstack (by unlocking sigstate). Note that unless an altstack is used, |
| 125 | the sigcontext will itself be located on the user's stack, so we may well |
| 126 | be overwriting it here (or later in __sigreturn2). |
| 127 | |
| 128 | So: do this very carefully. First, load sc_reply_port, which is the only |
| 129 | other bit of sigcontext that __sigreturn2 needs. Then copy the registers |
| 130 | without reordering them, but skipping the ones we won't need. We have to |
| 131 | copy starting from the larger addresses down, since our register dump is |
| 132 | located at a larger address than the sigcontext. */ |
| 133 | |
| 134 | sc_reply_port = scp->sc_reply_port; |
| 135 | usp = (uintptr_t *) (scp->sc_ursp - 128); |
| 136 | |
| 137 | *--usp = scp->sc_rip; |
| 138 | *--usp = scp->sc_rfl; |
| 139 | *--usp = scp->sc_rax; |
| 140 | *--usp = scp->sc_rcx; |
| 141 | *--usp = scp->sc_rdx; |
| 142 | *--usp = scp->sc_rbx; |
| 143 | *--usp = scp->sc_rbp; |
| 144 | *--usp = scp->sc_rsi; |
| 145 | *--usp = scp->sc_rdi; |
| 146 | *--usp = scp->sc_r15; |
| 147 | *--usp = scp->sc_r14; |
| 148 | *--usp = scp->sc_r13; |
| 149 | *--usp = scp->sc_r12; |
| 150 | *--usp = scp->sc_r11; |
| 151 | *--usp = scp->sc_r10; |
| 152 | *--usp = scp->sc_r9; |
| 153 | *--usp = scp->sc_r8; |
| 154 | |
| 155 | /* Switch to the user's stack that we have just prepared, and call |
| 156 | __sigreturn2. Clobber "memory" to make sure GCC flushes the stack |
| 157 | setup to actual memory. We align the stack as per the ABI, but pass |
| 158 | the original usp to __sigreturn2 as an argument. */ |
| 159 | asm volatile ("movq %1, %%rsp\n" |
| 160 | "andq $-16, %%rsp\n" |
| 161 | "call __sigreturn2" : |
| 162 | : "D" (ss), "S" (usp), "d" (sc_reply_port) |
| 163 | : "memory" ); |
| 164 | __builtin_unreachable (); |
| 165 | } |
| 166 | |
| 167 | weak_alias (__sigreturn, sigreturn) |
| 168 | |