From patchwork Tue May 26 07:02:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greentime Hu X-Patchwork-Id: 11570019 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8B0EF14F6 for ; Tue, 26 May 2020 07:03:40 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 68C182087D for ; Tue, 26 May 2020 07:03:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="e7ui7MOh"; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="mvVjA5mk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 68C182087D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=f85Vn4OuGxjQe/WprhGo5P1pwBgJ5q0VeEPd5gC4HYw=; b=e7ui7MOhnHldW0HSyBl7KQ4XQX GoqIdKUFIPYrCwuk7pcuBw87MMzLI1I1wKA2ENk4x6z41bFZpPj7IyW9SjOnHMHQc0EfxAmCv81eF yKQt4MAHeMo9RedWRxoIKel9CAKPHujuNa5C3bruZEov7O0Zi4gwUeLG+k4E4WEPSC0HBR2ENm8rW hC46sFNeBnChV51ylhVp3GzM3eRv2sN5WrzIg8cJR5iRVZVTbOXVcuSJMbZLhtUCf3AFYLWsRw+0T zNKA2RvWQXVnl+U178LJvBpHFjj7ckiUYSGF6Z8lls0q/0KiHrRpOlWVDVGF74yKAzIHrohUgYGzZ kuUqC7Sg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jdTce-00088K-Rp; Tue, 26 May 2020 07:03:36 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jdTcJ-0007i3-UH for linux-riscv@lists.infradead.org; Tue, 26 May 2020 07:03:17 +0000 Received: by mail-pf1-x441.google.com with SMTP id 131so1333821pfv.13 for ; Tue, 26 May 2020 00:03:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=f85Vn4OuGxjQe/WprhGo5P1pwBgJ5q0VeEPd5gC4HYw=; b=mvVjA5mkqlSCRsV0hWJXQfgQW2EKXKVzl9KCXz0dVWjoM7MNkN/UwWJvoT68TiixQZ pOgMpdfKhcG3KKooS4e7Lb687eSsfVtruJJoZPgc/qPvWkaxccEEErkfNDgcnkVZruTd PVcyh5PyK24WUzGUCHEoe9BsnF7AJZjBZn/ktvYEAnuYlL+MqnJS8///agNGaX0M1MeJ jutdO/RjRfK3B0PM8C0K5glqPOpYqhOvsdh5YdPos9IAwYpXN9tGD3d5+bSRglTsLvpf +l1NBti/l6MTxRhJnUg/z/QuYmbzPsxgsWNLfqLI8T0GFy2VH1u4TieQkbkWXrnsrdeD hSbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=f85Vn4OuGxjQe/WprhGo5P1pwBgJ5q0VeEPd5gC4HYw=; b=b62gfohWCSwUkfQh8pRhCS+wcR2hOJbbfxkPbaOI8Oe+GR+eGmDIXGlFuKDigjxRC9 XgZNxFeL3WYtjChBIwkzJAd3J/bt40LVmsh28bDepduHFOQwzWHiaG1fgs3rLUsPbqRD +kgQxl4vYJfKYfqN8TcEXzZ/Gm9QaecgG0W0EHz2X2QetvuyBxBo6r9eqo//kji0r7SL hk8zrzWiZ9k1jd7RIfB+ApZ5hqEHRTEBUg57rciEOmfBLZtibLH8JfL8vUaJhBdjsT2G ci3gQH8HLcrYcu/Bo7rd2+mICru1OeV9p8drpp5cdrvjQ90SVNy/eAwkFCybULTLnyup 0eyg== X-Gm-Message-State: AOAM5318XgexPO4+2o2RpG8q8e9DsWCwp5/fiGY43C4duBPMNkVHypDz 5OlfgN+thRbbMG3jFYoLHI29pA== X-Google-Smtp-Source: ABdhPJyA8DWABjY15lGbo3kBA5t3k19gSIsjCy5JXETqptaEzgVZp8aO2jxq7pDI7+wl9J0+PPPIMw== X-Received: by 2002:a62:9244:: with SMTP id o65mr14222963pfd.138.1590476594852; Tue, 26 May 2020 00:03:14 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (114-34-229-221.HINET-IP.hinet.net. [114.34.229.221]) by smtp.gmail.com with ESMTPSA id q34sm15167431pja.22.2020.05.26.00.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 00:03:14 -0700 (PDT) From: Greentime Hu To: greentime.hu@sifive.com, guoren@linux.alibaba.com, vincent.chen@sifive.com, paul.walmsley@sifive.com, palmerdabbelt@google.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, oleg@redhat.com Subject: [RFC PATCH v4 12/13] riscv: Add sigcontext save/restore for vector Date: Tue, 26 May 2020 15:02:41 +0800 Message-Id: <8dd3e441842d2b0fb63da82ecc04b08f99930e6a.1590474856.git.greentime.hu@sifive.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200526_000315_993059_82853483 X-CRM114-Status: GOOD ( 11.95 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:441 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+patchwork-linux-riscv=patchwork.kernel.org@lists.infradead.org From: Guo Ren This patch adds sigcontext save/restore for vector. The vector registers will be saved in datap pointer. The datap pointer will be allocaed dynamically when the task needs in kernel space. The datap pointer will be set right after the __riscv_v_state data structure to save all the vector registers in the signal handler stack. [greentime.hu@sifive.com: add support for dynamic vlen] Signed-off-by: Greentime Hu Signed-off-by: Guo Ren --- arch/riscv/include/uapi/asm/sigcontext.h | 2 + arch/riscv/kernel/signal.c | 92 +++++++++++++++++++++++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/uapi/asm/sigcontext.h b/arch/riscv/include/uapi/asm/sigcontext.h index 84f2dfcfdbce..4217f3f1c8ba 100644 --- a/arch/riscv/include/uapi/asm/sigcontext.h +++ b/arch/riscv/include/uapi/asm/sigcontext.h @@ -8,6 +8,7 @@ #include +#define RVV_MAGIC 0x53465457 /* * Signal context structure * @@ -17,6 +18,7 @@ struct sigcontext { struct user_regs_struct sc_regs; union __riscv_fp_state sc_fpregs; + struct __riscv_v_state sc_vregs; }; #endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 17ba190e84a5..9ada6f74bb95 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -83,6 +83,80 @@ static long save_fp_state(struct pt_regs *regs, #define restore_fp_state(task, regs) (0) #endif +#ifdef CONFIG_VECTOR +static long restore_v_state(struct pt_regs *regs, struct sigcontext *sc) +{ + long err; + struct __riscv_v_state __user *state = &sc->sc_vregs; + void *datap; + __u32 magic; + + /* Get magic number and check it. */ + err = __get_user(magic, &state->magic); + if (unlikely(err)) + return err; + + if (magic != RVV_MAGIC) + return -EINVAL; + + /* Copy everything of __riscv_v_state except datap. */ + err = __copy_from_user(¤t->thread.vstate, state, + RISCV_V_STATE_DATAP); + if (unlikely(err)) + return err; + + /* Copy the pointer datap itself. */ + err = __get_user(datap, &state->datap); + if (unlikely(err)) + return err; + + + /* Copy the whole vector content from user space datap. */ + err = __copy_from_user(current->thread.vstate.datap, datap, + current->thread.vstate.size); + if (unlikely(err)) + return err; + + vstate_restore(current, regs); + + return err; +} + +static long save_v_state(struct pt_regs *regs, struct sigcontext *sc) +{ + long err; + struct __riscv_v_state __user *state = &sc->sc_vregs; + /* Set the datap right after the sigcntext structure. */ + void *datap = sc + 1; + + vstate_save(current, regs); + /* Copy everything of vstate but datap. */ + err = __copy_to_user(state, ¤t->thread.vstate, + RISCV_V_STATE_DATAP); + if (unlikely(err)) + return err; + + /* Copy the magic number. */ + err = __put_user(RVV_MAGIC, &state->magic); + if (unlikely(err)) + return err; + + /* Copy the pointer datap itself. */ + err = __put_user(datap, &state->datap); + if (unlikely(err)) + return err; + + /* Copy the whole vector content to user space datap. */ + err = __copy_to_user(datap, current->thread.vstate.datap, + current->thread.vstate.size); + + return err; +} +#else +#define save_v_state(task, regs) (0) +#define restore_v_state(task, regs) (0) +#endif + static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) { @@ -92,6 +166,9 @@ static long restore_sigcontext(struct pt_regs *regs, /* Restore the floating-point state. */ if (has_fpu) err |= restore_fp_state(regs, &sc->sc_fpregs); + /* Restore the vector state. */ + if (has_vector) + err |= restore_v_state(regs, sc); return err; } @@ -101,13 +178,16 @@ SYSCALL_DEFINE0(rt_sigreturn) struct rt_sigframe __user *frame; struct task_struct *task; sigset_t set; + size_t frame_size = sizeof(*frame); /* Always make any pending restarted system calls return -EINTR */ current->restart_block.fn = do_no_restart_syscall; frame = (struct rt_sigframe __user *)regs->sp; - if (!access_ok(frame, sizeof(*frame))) + if (has_vector) + frame_size += current->thread.vstate.size; + if (!access_ok(frame, frame_size)) goto badframe; if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) @@ -145,6 +225,9 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, /* Save the floating-point state. */ if (has_fpu) err |= save_fp_state(regs, &sc->sc_fpregs); + /* Save the vector state. */ + if (has_vector) + err |= save_v_state(regs, sc); return err; } @@ -176,9 +259,12 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, { struct rt_sigframe __user *frame; long err = 0; + size_t frame_size = sizeof(*frame); - frame = get_sigframe(ksig, regs, sizeof(*frame)); - if (!access_ok(frame, sizeof(*frame))) + if (has_vector) + frame_size += current->thread.vstate.size; + frame = get_sigframe(ksig, regs, frame_size); + if (!access_ok(frame, frame_size)) return -EFAULT; err |= copy_siginfo_to_user(&frame->info, &ksig->info);