From patchwork Thu Mar 23 14:59:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185747 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CC4DC6FD1C for ; Thu, 23 Mar 2023 15:01:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231966AbjCWPBV (ORCPT ); Thu, 23 Mar 2023 11:01:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231982AbjCWPBH (ORCPT ); Thu, 23 Mar 2023 11:01:07 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF7F535EE1 for ; Thu, 23 Mar 2023 08:00:49 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id o11so22575860ple.1 for ; Thu, 23 Mar 2023 08:00:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583649; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=EnMly8Z4ZP6m5MefJgirtZn1/V9pgmnzrBMhkpUZtfA=; b=NVwTzlEK2Gds43FwU0cLpEZhn54YcxwlTCLUsSTxDTqUEUYYt5DIgDXlc9//y81m7s fQjoljnJpSYqIs3Z6oFR2D9HfihLmaaILuk0P+p1T+5QWedR8keaszunQbuE6HPpqRnp B4ZvAY9d76vZrs2fC27teiTCScazZ9RWCV0r/117ROvCL+3vXy4ntWPqkH33ivz9+GXa XjC8x7PzvADpGxpwOEvnuHjq9vHIXh1b231QpLJmhScReGM+rC6+YM2h2SN4rZLlUXpI QkWBB77kTUEK3znFg/NCe4+zgtmhgWzjnGkGHNrAVdZeJvF521DwNqr/C+veMAUixG4b NxAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583649; h=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=EnMly8Z4ZP6m5MefJgirtZn1/V9pgmnzrBMhkpUZtfA=; b=AZl8QrnUk/RELINcs5fsKjfkLVfMTbN1MJwSGUwdL2t7PSseKzCI+Aw6DrwpXcxOJZ UNkOpdmHrHqc6EGN4GNUWrHPTmaODq4CRYxy5+xbH7wbYtu9tv4q1aEmPb6cb5+3K8ws hMlID9NADoKa/96GsrSss67i9i30MN4sAddncW5MOp6rHqRnLpiiYMFkMGn27O4p29yL 8/ojD+Hg1Vm2UpVCI8LDnk79fBqAgBTel4UmqK7nl8129EG0L8+MuZdm6Q12Uj/luVCY VhilARXhykhIu5QjaPAITjuo5onJtvuv9KuUVs4ZIfPaxldrnEvSg6Q1Lw4ZWJ++7rxc jFPQ== X-Gm-Message-State: AO0yUKVct6EsG5+LAG9OImK+DY+V5wJRIQZuJflRdm5OTtM8mVQ6iudZ DSBrYPNEFnb1pqWo/ORpUYcYyg== X-Google-Smtp-Source: AK7set+DABhERXjaISFRv1JqomxsZIjenBSeZXajHTFO9RP7x7K96i1XQUT6LqKW+/vqMeX80/LJMg== X-Received: by 2002:a17:903:1389:b0:1a0:485c:a6c with SMTP id jx9-20020a170903138900b001a0485c0a6cmr5821899plb.8.1679583649117; Thu, 23 Mar 2023 08:00:49 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:48 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Conor Dooley , Guo Ren , Vincent Chen , Al Viro , Andrew Bresticker Subject: [PATCH -next v16 12/20] riscv: signal: check fp-reserved words unconditionally Date: Thu, 23 Mar 2023 14:59:16 +0000 Message-Id: <20230323145924.4194-13-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org In order to let kernel/user locate and identify an extension context on the existing sigframe, we are going to utilize reserved space of fp and encode the information there. And since the sigcontext has already preserved a space for fp context w or w/o CONFIG_FPU, we move those reserved words checking/setting routine back into generic code. This commit also undone an additional logical change carried by the refactor commit 007f5c3589578 ("Refactor FPU code in signal setup/return procedures"). Originally we did not restore fp context if restoring of gpr have failed. And it was fine on the other side. In such way the kernel could keep the regfiles intact, and potentially react at the failing point of restore. Signed-off-by: Andy Chiu Acked-by: Conor Dooley --- arch/riscv/kernel/signal.c | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index bfb2afa4135f..eefc78d74055 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -38,26 +38,13 @@ static long restore_fp_state(struct pt_regs *regs, { long err; struct __riscv_d_ext_state __user *state = &sc_fpregs->d; - size_t i; err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); if (unlikely(err)) return err; fstate_restore(current, regs); - - /* We support no other extension state at this time. */ - for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { - u32 value; - - err = __get_user(value, &sc_fpregs->q.reserved[i]); - if (unlikely(err)) - break; - if (value != 0) - return -EINVAL; - } - - return err; + return 0; } static long save_fp_state(struct pt_regs *regs, @@ -65,20 +52,9 @@ static long save_fp_state(struct pt_regs *regs, { long err; struct __riscv_d_ext_state __user *state = &sc_fpregs->d; - size_t i; fstate_save(current, regs); err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); - if (unlikely(err)) - return err; - - /* We support no other extension state at this time. */ - for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { - err = __put_user(0, &sc_fpregs->q.reserved[i]); - if (unlikely(err)) - break; - } - return err; } #else @@ -90,11 +66,30 @@ static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) { long err; + size_t i; + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); + if (unlikely(err)) + return err; + /* Restore the floating-point state. */ - if (has_fpu()) - err |= restore_fp_state(regs, &sc->sc_fpregs); + if (has_fpu()) { + err = restore_fp_state(regs, &sc->sc_fpregs); + if (unlikely(err)) + return err; + } + + /* We support no other extension state at this time. */ + for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) { + u32 value; + + err = __get_user(value, &sc->sc_fpregs.q.reserved[i]); + if (unlikely(err)) + break; + if (value != 0) + return -EINVAL; + } return err; } @@ -145,11 +140,17 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, { struct sigcontext __user *sc = &frame->uc.uc_mcontext; long err; + size_t i; + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); /* Save the floating-point state. */ if (has_fpu()) err |= save_fp_state(regs, &sc->sc_fpregs); + /* We support no other extension state at this time. */ + for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) + err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]); + return err; }