From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id AB96D3858C31 for ; Mon, 3 Apr 2023 11:47:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB96D3858C31 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lj1-x22a.google.com with SMTP id bx10so11703913ljb.8 for ; Mon, 03 Apr 2023 04:47:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680522454; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JpCjzZ9Kbw+C6eNtcLPrifZ79um9iSNahQ4h4jkSBvo=; b=X3t3M2ABE2veoFXsc5RFIilnEAUP452XGCqTTatZbCsdm8jiqRaEMEM6kRtogh+Iy8 L+aSBmOElTLgjAl2M2eJeGhR8Ear9QXNR9CM6ADDR4fuaAC/VjhNuF8v/2ppchFE5Do9 D8dn+8ToiUv0obkwkn1mIdyujWwJHwWdWE8mH0by6ELFthXON3e0VDPmk1i1S77Kh5B/ n+LHMStZiNfQXCD89ZHc8qfE0cOuwVuMTpZECipNP+bWW1PtkEkCcue+zfTx/+JUzk3U Pe4m2rFBaGAohsivDLytNfZ9dIM4S1skVlHzAKFN8X4fGVf7hkNoQpPvC/7+1FJJCJJE GX3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680522454; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JpCjzZ9Kbw+C6eNtcLPrifZ79um9iSNahQ4h4jkSBvo=; b=eSvQUU17rnYIHg300oYRB3WAcyseMNYjt37x0JLTQ4+4wnHORdqUsxuoVxsX+I8lQD 7Vl6yYFartIwVpcdyuan19uHoNgXgwpQWTfOMkrKlBmAUb9t93JMCYWo1huaZwl3Yi5X 1Dzt2w9M5HNRt2OrpOH12AIsAwvK5XsopqrCUak4r6UmMEwUcmCab6gNzS+i6PN6/Qxp O2wBHOYPrPD8I9dGD/Q2THnf9cq0lJDJDf7ql10xLcwaPcqchJonIx797iRJwVQxbzEk duVeZiR9blnRAw+mdp4UxKYIXddvdpVj9VyCcnH6hQairt5A9IkGcew40Uh97trjNPpt qV+Q== X-Gm-Message-State: AAQBX9dPnPqQy3wKci/H59cnD5G7CHa/wps9rL3JH2EbAX1K1avk8VxP vzo5CNsiJ8qsfNDfZq5cd9c= X-Google-Smtp-Source: AKy350a5JS0OjNW7geStm1S8Sk1OEvkcuB3UptUHAOtEdSvkvsSiSZI7PLYeVZGdXYMc04Ct0t6wBQ== X-Received: by 2002:a05:651c:22e:b0:2a1:2cb4:5f81 with SMTP id z14-20020a05651c022e00b002a12cb45f81mr9952804ljn.36.1680522453656; Mon, 03 Apr 2023 04:47:33 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id e18-20020a2e8ed2000000b002987088bda4sm1654118ljl.69.2023.04.03.04.47.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Apr 2023 04:47:33 -0700 (PDT) From: Sergey Bugaev To: bug-hurd@gnu.org, libc-alpha@sourceware.org Cc: Samuel Thibault , Sergey Bugaev Subject: [PATCH v2] hurd: Implement sigreturn for x86_64 Date: Mon, 3 Apr 2023 14:47:30 +0300 Message-Id: <20230403114730.257595-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230319151017.531737-33-bugaevc@gmail.com> References: <20230319151017.531737-33-bugaevc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_NUMSUBJECT,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Here's a better version that does not do the stupid __builtin_frame_address thing (instead, we call the function for real, and pass usp as an arg), and should actually ensure the 16-byte alignment. -- >8 -- Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/x86_64/sigreturn.c | 151 +++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 sysdeps/mach/hurd/x86_64/sigreturn.c diff --git a/sysdeps/mach/hurd/x86_64/sigreturn.c b/sysdeps/mach/hurd/x86_64/sigreturn.c new file mode 100644 index 00000000..429cee30 --- /dev/null +++ b/sysdeps/mach/hurd/x86_64/sigreturn.c @@ -0,0 +1,151 @@ +/* Copyright (C) 1991-2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +/* This is run on the thread stack after restoring it, to be able to + unlock SS off sigstack. */ +void +__sigreturn2 (struct hurd_sigstate *ss, uintptr_t *usp) +{ + _hurd_sigstate_unlock (ss); + + asm volatile ( + /* Point the stack to the register dump. */ + "movq %0, %%rsp\n" + /* Pop off the registers. */ + "popq %%r8\n" + "popq %%r9\n" + "popq %%r10\n" + "popq %%r11\n" + "popq %%r12\n" + "popq %%r13\n" + "popq %%r14\n" + "popq %%r15\n" + "popq %%rdi\n" + "popq %%rsi\n" + "popq %%rbp\n" + "popq %%rbx\n" + "popq %%rdx\n" + "popq %%rcx\n" + "popq %%rax\n" + "popfq\n" + /* Restore %rip and %rsp with a single instruction. */ + "retq $128" : + : "rm" (usp)); + __builtin_unreachable (); +} + +int +__sigreturn (struct sigcontext *scp) +{ + struct hurd_sigstate *ss; + struct hurd_userlink *link = (void *) &scp[1]; + mach_port_t reply_port; + + if (scp == NULL || (scp->sc_mask & _SIG_CANT_MASK)) + { + errno = EINVAL; + return -1; + } + + ss = _hurd_self_sigstate (); + _hurd_sigstate_lock (ss); + + /* Remove the link on the `active resources' chain added by + _hurd_setup_sighandler. Its purpose was to make sure + that we got called; now we have, it is done. */ + _hurd_userlink_unlink (link); + + /* Restore the set of blocked signals, and the intr_port slot. */ + ss->blocked = scp->sc_mask; + ss->intr_port = scp->sc_intr_port; + + /* Check for pending signals that were blocked by the old set. */ + if (_hurd_sigstate_pending (ss) & ~ss->blocked) + { + /* There are pending signals that just became unblocked. Wake up the + signal thread to deliver them. But first, squirrel away SCP where + the signal thread will notice it if it runs another handler, and + arrange to have us called over again in the new reality. */ + ss->context = scp; + _hurd_sigstate_unlock (ss); + __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ()); + /* If a pending signal was handled, sig_post never returned. + If it did return, the pending signal didn't run a handler; + proceed as usual. */ + _hurd_sigstate_lock (ss); + ss->context = NULL; + } + + if (scp->sc_onstack) + ss->sigaltstack.ss_flags &= ~SS_ONSTACK; + + /* Destroy the MiG reply port used by the signal handler, and restore the + reply port in use by the thread when interrupted. */ + reply_port = THREAD_GETMEM (THREAD_SELF, reply_port); + THREAD_SETMEM (THREAD_SELF, reply_port, scp->sc_reply_port); + __mach_port_mod_refs (__mach_task_self (), reply_port, + MACH_PORT_RIGHT_RECEIVE, -1); + + if (scp->sc_fpused) + /* Restore the FPU state. Mach conveniently stores the state + in the format the i387 `frstor' instruction uses to restore it. */ + asm volatile ("frstor %0" : : "m" (scp->sc_fpsave)); + + { + /* There are convenient instructions to pop state off the stack, so we + copy the registers onto the user's stack, switch there, pop and + return. */ + + uintptr_t *usp = (uintptr_t *) scp->sc_ursp - 128; + + *--usp = scp->sc_rip; + *--usp = scp->sc_rfl; + *--usp = scp->sc_rax; + *--usp = scp->sc_rcx; + *--usp = scp->sc_rdx; + *--usp = scp->sc_rbx; + *--usp = scp->sc_rbp; + *--usp = scp->sc_rsi; + *--usp = scp->sc_rdi; + *--usp = scp->sc_r15; + *--usp = scp->sc_r14; + *--usp = scp->sc_r13; + *--usp = scp->sc_r12; + *--usp = scp->sc_r11; + *--usp = scp->sc_r10; + *--usp = scp->sc_r9; + *--usp = scp->sc_r8; + + /* Switch to the user's stack that we have just prepared, and call + __sigreturn2. Clobber "memory" to make sure GCC flushes the stack + setup to actual memory. We align the stack as per the ABI, but pass + the original usp to __sigreturn2 as an argument. */ + asm volatile ("movq %1, %%rsp\n" + "andq $-16, %%rsp\n" + "call __sigreturn2" : + : "D" (ss), "S" (usp) + : "memory"); + __builtin_unreachable (); + } +} + +weak_alias (__sigreturn, sigreturn) -- 2.39.2