From patchwork Sun Mar 19 15:10:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 66611 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 58BF6394D81C for ; Sun, 19 Mar 2023 15:16:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 58BF6394D81C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679239009; bh=nyZfBYc80miKdU/5Y0+oDe6GOTMX6L7uaq94omlCN7M=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=AhgpiqDzl5tUOZnyGgQ4DZ2PS6VRP67Kzx2vpzbg7/XHFzo7DO4UWYcY75W7HHSfP TRYywFKrVyQRS9aPJJbyl2rVVwu/Vq1a0cDUzz8VkeXWGXaAvznm361jYwAwRQPmPG fqrpuG25WNIngfp/xZgVhtynPe1P6l9LBF6ddfjs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id D011C3850212 for ; Sun, 19 Mar 2023 15:11:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D011C3850212 Received: by mail-lf1-x130.google.com with SMTP id t11so12043959lfr.1 for ; Sun, 19 Mar 2023 08:11:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679238687; 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=nyZfBYc80miKdU/5Y0+oDe6GOTMX6L7uaq94omlCN7M=; b=nezgmjTb3PWLW1udOiqKAirqlou50C3eY5nrC6uukQ0hZ2AYniY8upgryF4h5Kro7b gxaIoWoFqN59SrFl5M+LWKqi3DzTdvJx1Lc+KZK8DPJEAyefC4wK3lnQO8yncVbDlS27 n+WOIe6jlXbYj+i6IRGMl0REVdiedU1I7aeZhrhapsXOZeWHq6ofhESgQHGm6DQQEcih m6bGzSWdW8FfZLJ62H/k+dbb9a3uQwd0towWl6olPjM7TMj578J929TiRPq82lAkNaPf O+ty97Y3gwbEotgpWwaDnD1RRW2PxL4SdfsXnZXKUm71Rc1+vxkrzGiZbWilaqYvxbxq elKg== X-Gm-Message-State: AO0yUKVzf1ESIeXb17vBZiliWvXmyu4O4ALyO6C+hWUUXUwY3TrjZ51r UvRL5B3QaVEIKkCmd9J6wmb9JwAAfsNSyw== X-Google-Smtp-Source: AK7set8NN/2FeKVlmlTGVNvwLKAtbg66wZ4h1kF2Fpxg+5nO1Igrcocq6mESWjllqmOa1HSMIfdrBw== X-Received: by 2002:ac2:57db:0:b0:4de:3f1e:cdc4 with SMTP id k27-20020ac257db000000b004de3f1ecdc4mr5752958lfo.13.1679238687113; Sun, 19 Mar 2023 08:11:27 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:577a:76f4:df43:5e66]) by smtp.gmail.com with ESMTPSA id m19-20020ac24253000000b004e90dee5469sm1274089lfl.157.2023.03.19.08.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 08:11:26 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Sergey Bugaev Subject: [RFC PATCH glibc 32/34] hurd: Implement sigreturn for x86_64 Date: Sun, 19 Mar 2023 18:10:15 +0300 Message-Id: <20230319151017.531737-33-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230319151017.531737-1-bugaevc@gmail.com> References: <20230319151017.531737-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/x86_64/sigreturn.c | 155 +++++++++++++++++++++++++++ 1 file changed, 155 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..b8c62e53 --- /dev/null +++ b/sysdeps/mach/hurd/x86_64/sigreturn.c @@ -0,0 +1,155 @@ +/* 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) +{ + _hurd_sigstate_unlock (ss); + + /* We get jumped into, not called normally. The register dump starts where + our return address would normally be. */ + uintptr_t *usp = (uintptr_t *) __builtin_frame_address (0) + 1; + + 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 jump off to + __sigreturn2. Clobber "memory" to make sure GCC flushes the stack + setup to actual memory. XXX: This ignores stack alignment requirements; + hopefully it's not going to matter since __sigreturn2 must not use the + FPU. */ + asm volatile ("movq %0, %%rsp\n" + "jmp __sigreturn2" : + : "rm" (usp), "D" (ss) + : "memory"); + __builtin_unreachable (); + } +} + +weak_alias (__sigreturn, sigreturn)