Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6668955iob; Wed, 11 May 2022 02:45:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3+1NfS0wQ2m3hhGTqNe6jmOdvbqsHyUTwuig0dOZqc+LUK2lCXQu0iFTJR5mv1CsFSIpR X-Received: by 2002:a17:902:db12:b0:15e:acc0:ea84 with SMTP id m18-20020a170902db1200b0015eacc0ea84mr24474243plx.127.1652262307794; Wed, 11 May 2022 02:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652262307; cv=none; d=google.com; s=arc-20160816; b=A+vM+KthofDuyDv5hwo9dLqKl0w0OGy1Xt3nv3+gpHpeQT2U4HLJsvBoq6kHgseGut i44voZgedzIvVzv4/z7lG5z1Je8wuWyj34Ygd1ElZ79B4vtZn+shheJHZ1O4P5v7M98H e76q7DQmNIjC2rDscDkJpVSBM4T1pjZWRCNiGx3TniC3p6JcnguJQ1w2WKIE9UJHW9eJ R7uDPSwG33v8ZEF8YhsKAtwhwVKaoMrntu7vSJEgrxBuJQpFPllutW8KbVdyrEHIw26f ENt2eP3Eha6la0v9fASzOBvxik3lPdjbkS1HSXNATdQy/m6Tx6EQZyl3PMBTQXZv0IKg lhEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=hn9a0/YIZzR3pf/XVlEiVrwG/Xe5Ymt+E9Xn6o6kKJM=; b=E5IgzFYBv9tWCLzQefknvWQZHJ0gXIJ/rfZcDIxfnbm3JIrLjCwqXwFukK1HOAqBJA +i+tRpn/r/PWUO5oWaVHzzJjn88XwGpZH154/jx85ojEtIoIcA26b8iU+4KxEjaiaYl5 osDBaGbgCDwLKn070S4gV3+mZaJhxjQNs+idL/NzNk1Y0NLmZceDehNahfuo72T7Rt65 CBMf4J5KZ6qb+Zsmwr5giFayk8ghHGnZ6ROlBxxmIfe5puRG5hY9BHgJtav7NwRxhgWl gbyL8XEIGEf65Wq2TAnLZ3pKOvzms6P2BJu1glJroRrYbosalJK/m/XO7IFq+CdRcgYa eB1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=KIPMNaSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 207-20020a6306d8000000b003c253889c13si2285432pgg.40.2022.05.11.02.44.52; Wed, 11 May 2022 02:45:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=KIPMNaSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241631AbiEKIc7 (ORCPT + 99 others); Wed, 11 May 2022 04:32:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243758AbiEKIbt (ORCPT ); Wed, 11 May 2022 04:31:49 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 791AF2AC7F for ; Wed, 11 May 2022 01:31:44 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id a191so1195818pge.2 for ; Wed, 11 May 2022 01:31:44 -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:in-reply-to :references; bh=hn9a0/YIZzR3pf/XVlEiVrwG/Xe5Ymt+E9Xn6o6kKJM=; b=KIPMNaSvgyCbpsbGTq8d2mWXUto29HB+ZCEjfgcJ5KcgD2LiZxHLHCkSel9nGFEsyx eZ2jkyCn/Z2XTf1Y8TxPRhlAZvyxtpLW//MXf4FtyuinEWaVckfOp08GzRcMRdGYt8O0 ZXzDROyJEnE4pBy3QQdrCkCF6mWdVNNah9ICken2EGT5rGV0B7Nn1v1ZMx2TapOq7hCK kUBeCKzLSC2VI5s/s6WIsPX0rlbeJIwyjiCjJjI+Ia8mFDpjZ0Cd2H+FaluqLmVdfzjE F84cvF3uM7gN9/pEWvggRVqkXuGPt6chyKZ7YngU3hlSFmG9ch35Q0aRloeECr+Zlz1d W9ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=hn9a0/YIZzR3pf/XVlEiVrwG/Xe5Ymt+E9Xn6o6kKJM=; b=mE/pHVI7OFdg2e2XHHLBI565q1GRu2sNn+kx12JIsUqURslnsnKeUJAyCLEGqto9uH q8+45mYFJcDbBttPDlN0NeUzslEx387aPgPWwiDydQUIlJkVpdfDcWpUW13ZfMCC71bT I9frSHAlY0FBeKPW5sklJ0sFN7Lew818ALc1SMhczT80OzFvwPSrjGq0AWIUXuAxMB3B GGAbkWgtW8xdQ1ibwX+75drVXFuXHf6pW8ani+QpQKRT/x9pKmCDE4Sy9aK6DlGa1mkt FgzMLu0Y25bLvg1geAYeGVdSeet3zkHaS7yLzP2stJ6YdPwnHz9iVxGznmvZ6pj0/cHc EXXA== X-Gm-Message-State: AOAM533bncScreiZTTdy/D7YjLl7YBDJyEQBMXgfHELudtAFa/S81bzs o9FkxlfbIVntamEI50wAu74pFQ== X-Received: by 2002:a05:6a00:2310:b0:505:a8ac:40e7 with SMTP id h16-20020a056a00231000b00505a8ac40e7mr24183433pfh.11.1652257903964; Wed, 11 May 2022 01:31:43 -0700 (PDT) Received: from localhost.localdomain (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id m10-20020aa7900a000000b0050dc7628170sm1020202pfo.74.2022.05.11.01.31.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 01:31:43 -0700 (PDT) From: Greentime Hu To: palmer@dabbelt.com, paul.walmsley@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, aou@eecs.berkeley.edu Subject: [PATCH v10 09/16] riscv: Add ptrace vector support Date: Wed, 11 May 2022 08:31:19 +0000 Message-Id: <33d5a0f0e3a689831be52d3cfcf4720115edb868.1652257230.git.greentime.hu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds ptrace support for riscv vector. The vector registers will be saved in datap pointer of __riscv_v_state. This pointer will be set right after the __riscv_v_state data structure then it will be put in ubuf for ptrace system call to get or set. It will check if the datap got from ubuf is set to the correct address or not when the ptrace system call is trying to set the vector registers. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu --- arch/riscv/include/uapi/asm/ptrace.h | 6 +++ arch/riscv/kernel/ptrace.c | 71 ++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 78 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 6ee1ca2edfa7..2491875be80d 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -94,6 +94,12 @@ struct __riscv_v_state { */ }; +/* + * According to spec: The number of bits in a single vector register, + * VLEN >= ELEN, which must be a power of 2, and must be no greater than + * 2^16 = 65536bits = 8192bytes + */ +#define RISCV_MAX_VLENB (8192) #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 793c7da0554b..6f5d88220c84 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -26,6 +26,9 @@ enum riscv_regset { #ifdef CONFIG_FPU REGSET_F, #endif +#ifdef CONFIG_VECTOR + REGSET_V, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -82,6 +85,64 @@ static int riscv_fpr_set(struct task_struct *target, } #endif +#ifdef CONFIG_VECTOR +static int riscv_vr_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct __riscv_v_state *vstate = &target->thread.vstate; + + /* + * Ensure the vector registers have been saved to the memory before + * copying them to membuf. + */ + if (target == current) + vstate_save(current, task_pt_regs(current)); + + /* Copy vector header from vstate. */ + membuf_write(&to, vstate, RISCV_V_STATE_DATAP); + membuf_zero(&to, sizeof(void *)); +#if __riscv_xlen == 32 + membuf_zero(&to, sizeof(__u32)); +#endif + + /* Copy all the vector registers from vstate. */ + return membuf_write(&to, vstate->datap, riscv_vsize); +} + +static int riscv_vr_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret, size; + struct __riscv_v_state *vstate = &target->thread.vstate; + + /* Copy rest of the vstate except datap and __padding. */ + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate, 0, + RISCV_V_STATE_DATAP); + if (unlikely(ret)) + return ret; + + /* Skip copy datap. */ + size = sizeof(vstate->datap); + count -= size; + ubuf += size; +#if __riscv_xlen == 32 + /* Skip copy _padding. */ + size = sizeof(vstate->__padding); + count -= size; + ubuf += size; +#endif + + /* Copy all the vector registers. */ + pos = 0; + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap, + 0, riscv_vsize); + return ret; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -101,6 +162,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_fpr_set, }, #endif +#ifdef CONFIG_VECTOR + [REGSET_V] = { + .core_note_type = NT_RISCV_VECTOR, + .align = 16, + .n = (32 * RISCV_MAX_VLENB)/sizeof(__u32), + .size = sizeof(__u32), + .regset_get = riscv_vr_get, + .set = riscv_vr_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index 7ce993e6786c..b4e8d7dde15b 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -436,6 +436,7 @@ typedef struct elf64_shdr { #define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */ #define NT_MIPS_FP_MODE 0x801 /* MIPS floating-point mode */ #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ +#define NT_RISCV_VECTOR 0x900 /* RISC-V vector registers */ /* Note types with note name "GNU" */ #define NT_GNU_PROPERTY_TYPE_0 5 -- 2.17.1