From patchwork Fri Apr 14 15:58:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211768 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 5F137C77B79 for ; Fri, 14 Apr 2023 15:59:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ouudiRHdDqYhRJyMVqjr7OGs7TIswItqnBrrQh+2J7c=; b=qm3bTaSQmTVNy/ Wy/RWSOERC3vQFmB7hj08naXNrfICzHk/5z+dmwcnU80dyByx6MR/Y5HceLIkvDBv9l+yhnnZtgcS bfGDFh1wpAm77F/kr4CpkePl4n2GcHN1LoGxAQquzjabTV+R8lZ/S3mVNWXvcqJgq0gYy+SEXSsD2 KZcJe+mTpTmz96MAQ8dF0s6KMwz3AtwjU96cpR0canXj8l5OC7SjUQiVKzWPRoESsPiOT1zyd86DG G9IiLGncOh10Dbfgv08qAZ1aOmTpSPk493CBRw60w1+IyvKXBoYkM/FLN8IXYS/fXQmQscpcxdiPV vSHbdb59arPcMcU9MZWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpG-00A1Sx-39; Fri, 14 Apr 2023 15:59:02 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpD-00A1RX-2U for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:01 +0000 Received: by mail-pj1-x102d.google.com with SMTP id cm18-20020a17090afa1200b0024713adf69dso6559586pjb.3 for ; Fri, 14 Apr 2023 08:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487939; x=1684079939; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=xZD7AFVtobxh1FGjEKQD2QW8IDIT5bzbSx/S1GQl+8U=; b=SXvhgGoWdY4+v0js6UamSIbUsdNlillnh5BR2/gI3mr5VGZ/aiO0T+5K999+nOdAm1 FV53MybCKnLYnRlS/OUx5MFd1lPXPvysty5xly8BD2hDihMEmSv9d5HhyA2cj42zmrP4 HZs78S+Onq1MBO7YsEZm6NxRdFd2b4CdXnzSwIAs2tfvmTc/DHp9APpNxEkpOQYBw+Xu UVSOcRKjEtMwCau7ZxxA/CvP/NJCTMXlKadL8tIvXYK6togfuSgjaPmDr0KVcf/UzN9u 3fNUWk9rdP3D/0SmwF9P5R5LPJkHfADah2vIlFTDAkCc8JWoQxG/N1boAa9qIPMqunGE KCEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487939; x=1684079939; 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=xZD7AFVtobxh1FGjEKQD2QW8IDIT5bzbSx/S1GQl+8U=; b=OgOKTPaImDnoh568MiOO8IJblYUFXahJU+RBMRzUZX1mc002NG7Mzqqe3faCxFRbNi kKlBVT/r5arPvhQC32sPvmKtd8WyWaGx0GsmyGV2T8VV9+3wL3wOQcikNn8lW4yeMxXC 1cvF94dYuGd0ON5f9Yo1C0waoy71SIOk2rczVJDvlG7VpO5ntgFV2NiZNSupumxzS72V 5wN3FYMkaDO3jumbrtd9Yos351JkNe4BXusVnwYO3XZktSQ0b2czPOdMgjdoQB04hbgB Ug+pmC/XEUg2LuSgPtkmE72LvsyACrfeXQ/F6yud+ZRVCogtANQgzZFe6Lc6lRNz12a0 5k9Q== X-Gm-Message-State: AAQBX9dzF4+7uOKPso8nkIdWc4hyyuqUZLjCWWyAf61opnhtt+cmMkeK +rrxeorg0PpANxvJd4CyqNpfzcKN9zJ+4B93nKVcjQzTM6zM4U17vsS+9CL/2xwSmXHSIVoEvqx tMRbk7HNw2ex37GsVZJQNCT4AkUF75Atbftsg9ep3OZ8wZtO8JiQlKdihLn7FunWCS2qISdH6SB AdFqVvM9rGs5Q/ X-Google-Smtp-Source: AKy350ZxguafjbuTJCtXZ0UwMA3Y+whb6ag2ldUxCKWrmYydILPzUyXVfqAAPNk2N+lqD0cJXe0veA== X-Received: by 2002:a17:90a:bf02:b0:246:57ba:ab25 with SMTP id c2-20020a17090abf0200b0024657baab25mr5991322pjs.11.1681487938804; Fri, 14 Apr 2023 08:58:58 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:58:58 -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, Guo Ren , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Guo Ren , Conor Dooley , Ruinland Tsai , Jisheng Zhang Subject: [PATCH -next v18 01/20] riscv: Rename __switch_to_aux() -> fpu Date: Fri, 14 Apr 2023 15:58:24 +0000 Message-Id: <20230414155843.12963-2-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085859_810294_7C60BDAE X-CRM114-Status: GOOD ( 10.32 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Guo Ren The name of __switch_to_aux() is not clear and rename it with the determine function: __switch_to_fpu(). Next we could add other regs' switch. Signed-off-by: Guo Ren Signed-off-by: Guo Ren Signed-off-by: Greentime Hu Reviewed-by: Anup Patel Reviewed-by: Palmer Dabbelt Signed-off-by: Andy Chiu Tested-by: Heiko Stuebner Reviewed-by: Heiko Stuebner Reviewed-by: Conor Dooley --- arch/riscv/include/asm/switch_to.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 60f8ca01d36e..4b96b13dee27 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -46,7 +46,7 @@ static inline void fstate_restore(struct task_struct *task, } } -static inline void __switch_to_aux(struct task_struct *prev, +static inline void __switch_to_fpu(struct task_struct *prev, struct task_struct *next) { struct pt_regs *regs; @@ -66,7 +66,7 @@ static __always_inline bool has_fpu(void) static __always_inline bool has_fpu(void) { return false; } #define fstate_save(task, regs) do { } while (0) #define fstate_restore(task, regs) do { } while (0) -#define __switch_to_aux(__prev, __next) do { } while (0) +#define __switch_to_fpu(__prev, __next) do { } while (0) #endif extern struct task_struct *__switch_to(struct task_struct *, @@ -77,7 +77,7 @@ do { \ struct task_struct *__prev = (prev); \ struct task_struct *__next = (next); \ if (has_fpu()) \ - __switch_to_aux(__prev, __next); \ + __switch_to_fpu(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) From patchwork Fri Apr 14 15:58:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211767 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id BA669C77B72 for ; Fri, 14 Apr 2023 15:59:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5L6n5HSkzjE1iWdws5f5dMxWt7w9OAR53DVjZVU4q2w=; b=u6Zgr72XJAZpyP 7obfGZMvjiDmYA9fX+YszqUGt9/DbQIQ4cFFtVmhgNX2CFNfGEiL5/FcchYNDeFAOVl7lgq3QTwjd LNhja1hVehQ+V5dj4a1LVa6WIWSeaVp3H0556WI2PG6kb6/qxwEVHEXv23l+bim2UI/wiMnYoLJ5+ v3AZpFBuL1jIaNcjrYKJrYonK03DrhBgNWiRGDqdMUe3Pl25HsFkDPf+A0UolAHyOE+nsFHJpuX2F tFgSKzqXGRw9eQpkKIg0IBF/EtTWPRSIE/S73o88FEQ7PAV6qaLSdd/t4V/ASGgxYB3dDeoKoy69I 8P+IYh5mm5cT3YadCzig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpQ-00A1VW-02; Fri, 14 Apr 2023 15:59:12 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpM-00A1Tn-2T for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:10 +0000 Received: by mail-pj1-x102e.google.com with SMTP id my14-20020a17090b4c8e00b0024708e8e2ddso8167419pjb.4 for ; Fri, 14 Apr 2023 08:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487947; x=1684079947; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=U7ri8ZSvnPvQyAXJ6dQGQtYIAraNypeWm5i0/GIq5FY=; b=YGmvXm/TuZoti9wweuSje/DwLyT5H6AiJaRqLiuK8jADZAJMP4Th25m4WmcRnYQbh3 WC1CY6kS84eID7rzBNa3mpK+uRN9O4nqQKKiOYyxKNc7OiWGMeWkAsi9wQMirwCNcmp8 k15vupj6hAY+hZ1EXRw/8bxxxxbGYtPq7prx9AagWTmKP5iNBHC9Kgc4j0YC76tK76IO OzmKVA6KRzd9ZAexvU9vOVLfiXvwFp5JdiYvi91oTZOHUQB6oLP9ObWHKUpjdTMYar19 Q4x8TLumG60vSH+2HAmEzgNEm5n8uGGctC7JDTWbImNbUWxdSjjT1GS0bGAPCQJ4d6wM mNIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487947; x=1684079947; 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=U7ri8ZSvnPvQyAXJ6dQGQtYIAraNypeWm5i0/GIq5FY=; b=GfsokJTjUU4CF1sFEmwQo2JPbBDgrvCM6ZFjp3GuBwtGt2bNr0Cwv6zV3hVbrMN8yv +PkfUBhLkGRanq/WaIvo4cmhmsEZi0vvlWz0yN68SquKSC7L7ZGFXYit6dn0Wdzm7VHQ g7PcQ+TrsQwJWgO0q1IgE7eOAJ1bvP6A/ARgTAOGGcwpSZ2n1ptOSGFQnMKyEfbqbLSQ H3phSJdiWa0e0qIn3UqfudD4y6OSDLO684gjneQG71V7LzI4b83tEdW10/xW2Xppto48 cCKR6bBQuPPHEhaKuUoEYvhc5uhNTPA+70iCyBZwK7G7Xfo6fomCw0BuO7qiMrEnRnB/ BuRg== X-Gm-Message-State: AAQBX9evBKzgZRDSsiJSUhC3ZU3U9a1UGulmkQBb1txYPJiMxFL97kh3 3pW9xiF7j581Vln9g12UL7yPBXMD9gpb2bTD2w6ppAFAImQBecenRZzVWG523adsPiWt6uNqy3f IXe2L/4GLrRyL02iZTM4w892cggkNVN9aIwhKRcQ57zTcuJghk7NZT7okD5FInocAMFac7RapuS vqfVx4OLsEWenU X-Google-Smtp-Source: AKy350YnbAtdYK25WZBT6vEyXz1kqM58xh87+9tiIROkMCKXWs4cw9ZPIPVhDSDuh+/FG9kqtQfPXw== X-Received: by 2002:a17:903:32cd:b0:1a5:2c2e:e41d with SMTP id i13-20020a17090332cd00b001a52c2ee41dmr4492968plr.51.1681487946919; Fri, 14 Apr 2023 08:59:06 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:06 -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, Guo Ren , Andy Chiu , Paul Walmsley , Albert Ou , Conor Dooley , Heiko Stuebner , Andrew Jones , Guo Ren , Jisheng Zhang , Dao Lu , Vincent Chen Subject: [PATCH -next v18 02/20] riscv: Extending cpufeature.c to detect V-extension Date: Fri, 14 Apr 2023 15:58:25 +0000 Message-Id: <20230414155843.12963-3-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085908_801462_EB2FD9DF X-CRM114-Status: GOOD ( 15.59 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Guo Ren Add V-extension into riscv_isa_ext_keys array and detect it with isa string parsing. Signed-off-by: Guo Ren Signed-off-by: Guo Ren Signed-off-by: Greentime Hu Suggested-by: Vineet Gupta Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/hwcap.h | 1 + arch/riscv/include/asm/vector.h | 26 ++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/hwcap.h | 1 + arch/riscv/kernel/cpufeature.c | 11 +++++++++++ 4 files changed, 39 insertions(+) create mode 100644 arch/riscv/include/asm/vector.h diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 9af793970855..5165588d1e8c 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -22,6 +22,7 @@ #define RISCV_ISA_EXT_m ('m' - 'a') #define RISCV_ISA_EXT_s ('s' - 'a') #define RISCV_ISA_EXT_u ('u' - 'a') +#define RISCV_ISA_EXT_v ('v' - 'a') /* * These macros represent the logical IDs of each multi-letter RISC-V ISA diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h new file mode 100644 index 000000000000..427a3b51df72 --- /dev/null +++ b/arch/riscv/include/asm/vector.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020 SiFive + */ + +#ifndef __ASM_RISCV_VECTOR_H +#define __ASM_RISCV_VECTOR_H + +#include + +#ifdef CONFIG_RISCV_ISA_V + +#include + +static __always_inline bool has_vector(void) +{ + return riscv_has_extension_likely(RISCV_ISA_EXT_v); +} + +#else /* ! CONFIG_RISCV_ISA_V */ + +static __always_inline bool has_vector(void) { return false; } + +#endif /* CONFIG_RISCV_ISA_V */ + +#endif /* ! __ASM_RISCV_VECTOR_H */ diff --git a/arch/riscv/include/uapi/asm/hwcap.h b/arch/riscv/include/uapi/asm/hwcap.h index 46dc3f5ee99f..c52bb7bbbabe 100644 --- a/arch/riscv/include/uapi/asm/hwcap.h +++ b/arch/riscv/include/uapi/asm/hwcap.h @@ -21,5 +21,6 @@ #define COMPAT_HWCAP_ISA_F (1 << ('F' - 'A')) #define COMPAT_HWCAP_ISA_D (1 << ('D' - 'A')) #define COMPAT_HWCAP_ISA_C (1 << ('C' - 'A')) +#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A')) #endif /* _UAPI_ASM_RISCV_HWCAP_H */ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 00d7cd2c9043..923ca75f2192 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -103,6 +103,7 @@ void __init riscv_fill_hwcap(void) isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F; isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D; isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C; + isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V; elf_hwcap = 0; @@ -261,6 +262,16 @@ void __init riscv_fill_hwcap(void) elf_hwcap &= ~COMPAT_HWCAP_ISA_F; } + if (elf_hwcap & COMPAT_HWCAP_ISA_V) { + /* + * ISA string in device tree might have 'v' flag, but + * CONFIG_RISCV_ISA_V is disabled in kernel. + * Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled. + */ + if (!IS_ENABLED(CONFIG_RISCV_ISA_V)) + elf_hwcap &= ~COMPAT_HWCAP_ISA_V; + } + memset(print_str, 0, sizeof(print_str)); for (i = 0, j = 0; i < NUM_ALPHA_EXTS; i++) if (riscv_isa[0] & BIT_MASK(i)) From patchwork Fri Apr 14 15:58:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211769 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 138B2C77B6E for ; Fri, 14 Apr 2023 15:59:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=elXZOFJP8tWFggNBA0Iqfm8ZX6NVNQfyLblxA8T/tDw=; b=EhtyUY/SR0kNy0 xgeX8+d+fi8rFQ1iFBhFLCPxJWN//nKmzUvKemCNwDtV19KoMfRpzY5EFwL0IaF0DcZzQn9EEpn6z eUBAEW3OM6KhsFaiieoPh5Ja4Hi1lm2nvl5PR6eAApnEbsJoFf23Fp86Kfiozub22ehfD4lc8ToSB LaeWaDTQe9lAdJHCe5v+aavuY+PgUKQ0G6IjbNwy8lJRGUOpK7PNUbuXriG2bZs9pOJka4bu0HHd3 ThvORnbBXFT/LS2jAYOlncMiybeYveX0HoR+9e2GosRn8ayPgzedS23z2eInSOn/yDT8FB3XLvvkt 62vzNTsE11S/0KYmqGyQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpT-00A1XN-1Z; Fri, 14 Apr 2023 15:59:15 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpQ-00A1RX-13 for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:14 +0000 Received: by mail-pj1-x102d.google.com with SMTP id cm18-20020a17090afa1200b0024713adf69dso6560212pjb.3 for ; Fri, 14 Apr 2023 08:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487951; x=1684079951; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=Q1Z9F1WWwWuoKVJ9t2cYpsn8ieMzUKF7G5wAxvaa99I=; b=lJSZ+Yo8VhC9W/LYpyoV2wpRLacsn0bF9TUYh/9nkCO4lhjkZByNRGRG1LeA39hSvi 5H5NFhDBuDEqmFhZE4pDGeGmjHCRPfF98HaFcpIVG0osVtXBXGQGy+Ej8jqiez1WROlH tIfqMHPgjJ0754QswUVwTlRNMUn11dBiyXxYj4kWv+/feLZWqoUa/vQt7Xiv9o1i9nQq CyltCcy/feV6x6S8e/+A/58IXp7zImBDmp/8MZwS+4AmX6Vk1PSxd+uibr4KfzLJLrio anXne7kv4KJbFwqUjpxxgDhlu1InzmZLou8i5GIzXGaIMu8JUEFxa80CK0tPgQBuQPLO uDew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487951; x=1684079951; 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=Q1Z9F1WWwWuoKVJ9t2cYpsn8ieMzUKF7G5wAxvaa99I=; b=ZQCdZBWNsFrDAaL5onsrNdMYCmrOx2SXNVDmpCfPeJ7rOUfo5C4SMM1JFVhbYZI2CD UJQ8c3Mm8U8ok0C+ozazv2QtgshgBHy6F4IVYpROcYtGMoUhnqkR2eEGJpTOeXRkxQK6 dBTT0DfR4lCgpqSZBswzS8PjM/VAJtDrQaQMXXWTdA3rUzEI+ICYBkFF04RZQBI6moVb ePoSwOroZlbRmkAJTjwON6k5SRGO9rMeQKTIwAqnftl+i8lgnCDxdcKst9ByZA4EjIx8 Z7p0Sd8HMvEmucZEsHZn6qyEIkWevD5yPtQWvwpQm2wM2qwLBRGmhWJCROOsl6KkTdJT C4Yw== X-Gm-Message-State: AAQBX9eVztbZiY/XVaroxwinOe0H3H2WKp7/cZ9N4139wuGa0tKrWJMI NPKv3RXSvPB+LkfDb5L86T082d4rZXjmxmDJG6OChSJGMGhT7gPuXzTeKTzLtQ3mJ4440WHsMKv OsSuMxthGM/Upn8pGuLa80NcNWAL5TfC4W3j0KL9txsdHx9iZPAXf1P38Q+kqMKNcYQU37vZ3e3 2yhJi1RgMntaCv X-Google-Smtp-Source: AKy350bprE0+W3CBUSvvYObjUvXLMYFHaBAzhudwABw1qf28V38FrSV2m24wt8iLQdpiZ/P8N9+bZg== X-Received: by 2002:a17:90b:70a:b0:240:9ccf:41ff with SMTP id s10-20020a17090b070a00b002409ccf41ffmr5501300pjz.49.1681487951281; Fri, 14 Apr 2023 08:59:11 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:10 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Guo Ren , Anup Patel , Atish Patra , Heiko Stuebner Subject: [PATCH -next v18 03/20] riscv: Add new csr defines related to vector extension Date: Fri, 14 Apr 2023 15:58:26 +0000 Message-Id: <20230414155843.12963-4-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085912_363501_221C6773 X-CRM114-Status: UNSURE ( 8.29 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Follow the riscv vector spec to add new csr numbers. Acked-by: Guo Ren Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Reviewed-by: Palmer Dabbelt Suggested-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/csr.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 7c2b8cdb7b77..39f3fde69ee5 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -24,16 +24,24 @@ #define SR_FS_CLEAN _AC(0x00004000, UL) #define SR_FS_DIRTY _AC(0x00006000, UL) +#define SR_VS _AC(0x00000600, UL) /* Vector Status */ +#define SR_VS_OFF _AC(0x00000000, UL) +#define SR_VS_INITIAL _AC(0x00000200, UL) +#define SR_VS_CLEAN _AC(0x00000400, UL) +#define SR_VS_DIRTY _AC(0x00000600, UL) + #define SR_XS _AC(0x00018000, UL) /* Extension Status */ #define SR_XS_OFF _AC(0x00000000, UL) #define SR_XS_INITIAL _AC(0x00008000, UL) #define SR_XS_CLEAN _AC(0x00010000, UL) #define SR_XS_DIRTY _AC(0x00018000, UL) +#define SR_FS_VS (SR_FS | SR_VS) /* Vector and Floating-Point Unit */ + #ifndef CONFIG_64BIT -#define SR_SD _AC(0x80000000, UL) /* FS/XS dirty */ +#define SR_SD _AC(0x80000000, UL) /* FS/VS/XS dirty */ #else -#define SR_SD _AC(0x8000000000000000, UL) /* FS/XS dirty */ +#define SR_SD _AC(0x8000000000000000, UL) /* FS/VS/XS dirty */ #endif #ifdef CONFIG_64BIT @@ -296,6 +304,12 @@ #define CSR_MIMPID 0xf13 #define CSR_MHARTID 0xf14 +#define CSR_VSTART 0x8 +#define CSR_VCSR 0xf +#define CSR_VL 0xc20 +#define CSR_VTYPE 0xc21 +#define CSR_VLENB 0xc22 + #ifdef CONFIG_RISCV_M_MODE # define CSR_STATUS CSR_MSTATUS # define CSR_IE CSR_MIE From patchwork Fri Apr 14 15:58:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211770 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 37563C77B71 for ; Fri, 14 Apr 2023 15:59:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=rgLbPaJvzDtC7o0IZbLRhlLYg9kI9XhGfkAXIeUeVK0=; b=a8WBQ+Btq/TO3s tT4dATugeTQfHa1P/6z5rbM6el40WRpOMbqBtdFoQcPwnYdPqGBxWKRm3T6juvxJUkIuH4o4Haa1X IP2x9TfwtfnmKW9hXIOsCNfq4wd9BKQNuZYZZOQx5J5YC7PhyvDeMgb2HCebu2WGs3bY+xPmbR7xe aIik/CUBX3kw6KyjQyNJuwE2olGA6DhiwOW7cEj7ZCAYRsUUfyZeZVGOION4jPZknkqAvOR8/aMJR ityQwkPWVTr1CSyymgSqSUX0MEs0iCe8yhgJgUwwJfjg5GLzHx70zmpdPZZd/fG1rC95GigOWbECk T53rd2MZkWU4ncyOHBug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpY-00A1ag-1U; Fri, 14 Apr 2023 15:59:20 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpV-00A1YL-3C for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:19 +0000 Received: by mail-pl1-x631.google.com with SMTP id la3so18549936plb.11 for ; Fri, 14 Apr 2023 08:59:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487956; x=1684079956; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=PG+A+9/fIV/ExGi75FEqrU6haa3ykbx364k88CAqjOs=; b=kdg9Yczwyk4e80R97KxMxEhoaYpdJL7tb7VWV6o89C7M4z3VvTwNyeIybXA8rBzxhW T8tH+KfF9EjEGeaKiZOKjJhDGcDF5/o+NvnTdiejZ31/hDs4nUgbNcT8U8yOLgk8hd70 /ZnDyKiDekpbcSGE8lXcZq7l4qqyV88mzCLCCtPYGvy3gh10tQNZXxu67iyh4ye131lm 9oiIErgg0c6In/JrOmhm0EHN0Di9QVr4GIV3K2uRBMIje+yk4v2+d88nRuOQMmRBfX8n yjvfH1YOJLzNsb0HzhhEatNpsnzy5YJt5lXOYFr1RNS4fgXHa7NlyfEgSX11eeqCI+An fshw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487956; x=1684079956; 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=PG+A+9/fIV/ExGi75FEqrU6haa3ykbx364k88CAqjOs=; b=ifOx5UtoV4GZae7SBjI7zidgRmtjbqMj4pff+hdMAQRCMmoxQject58/2STlQRJtJG 0fZLxtOFNykZrh7qYkrVnSyjefNsKsQNuGqPJiX/PglgaJHbp6/zd3yo3KlLegWIa6Qi h+E25MXhN7OVAX+PjqPBy4T2NJNIXzyfTiqb9AlUJUp5+ujw+U+a7XZKt33mPnV7i5pW yvcYSWTlY8RbtiHWtuDrXf8mRoApZD3aW7c+s+nZCXbSu6w9voQiB+foKtzNq8xKrLZV nbFcDOBLeIxEkMYOD1BYyPMyLwwR5HAOIjtWj8oHAH6hCI5lr/yiFba7Pb5FUAPWNni1 UEcw== X-Gm-Message-State: AAQBX9dYhO54jlEnTBaPgfzhBrr4ZxyfqWqrjHU0vPaHfjnYxrnIz7Rz cl0TXUbEulGUxKT9ox6nUQVx5yLFsW/OJm8MSqctNQ8iH8gTrHcKHVCpsV6nv3mtxeJ81iJOsgj cfoXuBI3qs57kyrYuVNUzHUvtwDjWZbIJGpSHrU6H4EqHAjlv1Ym30tyk57NpgIgV/Mkcw6rIsf LwGmUnHnwwJhqx X-Google-Smtp-Source: AKy350aE0CypeJZKaZSx8L/smNEoI0EeS58SnlbMlAl+92nAEaS9HyCM6GRm4Kom+iZm45DgD9y9eA== X-Received: by 2002:a17:90b:3649:b0:247:4adc:ef4b with SMTP id nh9-20020a17090b364900b002474adcef4bmr1277319pjb.47.1681487955712; Fri, 14 Apr 2023 08:59:15 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:15 -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 , Vincent Chen , Heiko Stuebner , Alexandre Ghiti , Guo Ren , Masahiro Yamada Subject: [PATCH -next v18 04/20] riscv: Clear vector regfile on bootup Date: Fri, 14 Apr 2023 15:58:27 +0000 Message-Id: <20230414155843.12963-5-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085918_028148_69FC0860 X-CRM114-Status: UNSURE ( 9.91 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu clear vector registers on boot if kernel supports V. Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/head.S | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 4bf6c449d78b..3fd6a4bd9c3e 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -392,7 +392,7 @@ ENTRY(reset_regs) #ifdef CONFIG_FPU csrr t0, CSR_MISA andi t0, t0, (COMPAT_HWCAP_ISA_F | COMPAT_HWCAP_ISA_D) - beqz t0, .Lreset_regs_done + beqz t0, .Lreset_regs_done_fpu li t1, SR_FS csrs CSR_STATUS, t1 @@ -430,8 +430,31 @@ ENTRY(reset_regs) fmv.s.x f31, zero csrw fcsr, 0 /* note that the caller must clear SR_FS */ +.Lreset_regs_done_fpu: #endif /* CONFIG_FPU */ -.Lreset_regs_done: + +#ifdef CONFIG_RISCV_ISA_V + csrr t0, CSR_MISA + li t1, COMPAT_HWCAP_ISA_V + and t0, t0, t1 + beqz t0, .Lreset_regs_done_vector + + /* + * Clear vector registers and reset vcsr + * VLMAX has a defined value, VLEN is a constant, + * and this form of vsetvli is defined to set vl to VLMAX. + */ + li t1, SR_VS + csrs CSR_STATUS, t1 + csrs CSR_VCSR, x0 + vsetvli t1, x0, e8, m8, ta, ma + vmv.v.i v0, 0 + vmv.v.i v8, 0 + vmv.v.i v16, 0 + vmv.v.i v24, 0 + /* note that the caller must clear SR_VS */ +.Lreset_regs_done_vector: +#endif /* CONFIG_RISCV_ISA_V */ ret END(reset_regs) #endif /* CONFIG_RISCV_M_MODE */ From patchwork Fri Apr 14 15:58:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211771 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id A0364C77B71 for ; Fri, 14 Apr 2023 15:59:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=h2fyFm07+nn2ahfB60CxlNiPFSokEkf2cHvGIXDYfx4=; b=T1FanA2x1nIbsb /QBb+r/E9mSUI8QDOO05EJRA691dsj3FzLl2VXOfAeeG6zeHnF2QfrZYwkXwFaP8znipwKqtdi3Mi 3KmkvIUS0ykHpZnyl7kuKeYeduks7APlk9Fl634HLufQ6EGnrtepFrhQyP/eHdHxIa3PCgnoJTY9W WnmaNWR9Q5tWfxbNAIR0jOC4nci3ix4mMYXmr/jLdBq3SdSEcNMsA4oXaMhkhBGItMb/+hqlsD4uD /3tS5XcSdIVe4v/QnNpy/oPvD+zvRrEOokTCaR6yHzLBhZeIvEQB9AJr2uf7xx/SkrxJXhMOhn4MP a4tClZ2YyX11tWZ+AhUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpe-00A1dA-0T; Fri, 14 Apr 2023 15:59:26 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpa-00A1bi-29 for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:24 +0000 Received: by mail-pl1-x634.google.com with SMTP id n17so2755796pln.8 for ; Fri, 14 Apr 2023 08:59:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487962; x=1684079962; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=PRENwq6Q5h6bJkuLEU9y2ZlvRnSbbBagGQhAGvyqbY8=; b=eV93yDj+Cvno/AR2Kqbp7T2X+jNoQ0r1Gm5oWicJqdZQBTQoW8zQMKqcIu2ZK8qm57 YoSs3FvjzFcpICELt84PzxQLl7L6iEzZDv2cSCM1UpETOR3hDXewrSMOXeN/pvUS7+vt az6AEvDKtT2we5KD8H70GqYRlIflTmBjlxHUXJzxoAtBAGccGgMeu3RHIPxnJ2mhY2gh sJEyNWIXhgUDw6a5s9dhisff3Oly8G524Sc9mjCiJo/+5D1d0oE1rb4ks4yexgrIPG/h yRf8TxjynIs9s7HbwQiOJKJTCT6LCUkJ160ZyoZhNugeH1DsgnQtMaoRp2StX6kS/Xxc 07bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487962; x=1684079962; 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=PRENwq6Q5h6bJkuLEU9y2ZlvRnSbbBagGQhAGvyqbY8=; b=HaRT/bOl9QCcrlCnzBnlSnPmIHJOdkNemf8qU8SZYyZV4bGO9dzc821MfwPOU07j+V h22jTGbBBX4Q4SznLBLJnHI8SgJz++5UWlXc3KVkZ5aP0fiCr3MFNwC/8yUW20vM+Bvy AzNHWK+Vp4R+egXPzRvXpaLvTKrpXuMoM6Pr6Mftnxa62RenElI5u1jNDv13Al7IlaHZ c3BWbfZTm0wvrNPGSmDEQHY78WdhmQTp5CWNMnpoitH1Q39sJLubyiYHvTXeT4hwlrS/ y18XnGFoge+Di2wjeOYV/AU7dqW/+Zd9SsmBe7L1seDxIZG/ME6Pirn/GOaFyg7Jp+7L 9Xlw== X-Gm-Message-State: AAQBX9caMr6rOdQ34kXRxUJY16sy/iDy5cXRGv08m1PVwHYzqMnsf4dK qpYAbafmobte9FNy8RkVGpkgyc0fmt6+O+y+TFoAb614nvjRbObzwWcWmQdGfp7gPbyJO59ZV0g iEJsiF2u3yBLz/AGyRvbO8245oDnlQmB9o5bx9pMvOVUjDGPpdV7hdRyFnY/1tpT0u6pxrdJXXS Hv2CkGy7Ftzk1y X-Google-Smtp-Source: AKy350YHJaKAhtMi3NPY+rjWgsiPejyppDLmmbC6OaSfsoWJPScFQLerfIgZudmYoZqZM1BdigvDvA== X-Received: by 2002:a17:90b:1e0a:b0:246:ac68:d4e9 with SMTP id pg10-20020a17090b1e0a00b00246ac68d4e9mr6316947pjb.6.1681487961697; Fri, 14 Apr 2023 08:59:21 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:21 -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, Vincent Chen , Han-Kuan Chen , Andy Chiu , Paul Walmsley , Albert Ou , Guo Ren , Jisheng Zhang , Nicolas Saenz Julienne , =?utf-8?b?QmrDtnJuIFTDtnBl?= =?utf-8?b?bA==?= , Frederic Weisbecker , Andrew Bresticker , Heiko Stuebner , Conor Dooley , Masahiro Yamada , Alexandre Ghiti Subject: [PATCH -next v18 05/20] riscv: Disable Vector Instructions for kernel itself Date: Fri, 14 Apr 2023 15:58:28 +0000 Message-Id: <20230414155843.12963-6-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085922_704560_F9E56FCF X-CRM114-Status: GOOD ( 10.20 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Guo Ren Disable vector instructions execution for kernel mode at its entrances. Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Co-developed-by: Han-Kuan Chen Signed-off-by: Han-Kuan Chen Co-developed-by: Greentime Hu Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/entry.S | 6 +++--- arch/riscv/kernel/head.S | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 3fbb100bc9e4..e9ae284a55c1 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -48,10 +48,10 @@ _save_context: * Disable user-mode memory access as it should only be set in the * actual user copy routines. * - * Disable the FPU to detect illegal usage of floating point in kernel - * space. + * Disable the FPU/Vector to detect illegal usage of floating point + * or vector in kernel space. */ - li t0, SR_SUM | SR_FS + li t0, SR_SUM | SR_FS_VS REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 3fd6a4bd9c3e..e16bb2185d55 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -140,10 +140,10 @@ secondary_start_sbi: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 /* Set trap vector to spin forever to help debug */ @@ -234,10 +234,10 @@ pmp_done: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 #ifdef CONFIG_RISCV_BOOT_SPINWAIT From patchwork Fri Apr 14 15:58:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211848 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id B19D2C77B71 for ; Fri, 14 Apr 2023 17:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Rr6x26OTrEap8vNG0D8sKWUDN83KecXm+X4b5z/5kfM=; b=LH2C+dCpx2GX/A /BJCdSRzHkAJzlJbInYygDwxrtosMfsl/O4K6DIOY0PfkEQoWLsR9eNoyg+n/jSryjvfu4IGf6+NY 7dySD6kcdav6EJoUbx6v0pwdyblHnW3/17v22nuorLXveVQYZhW4FXh5HhhXkNrKuKs4Bn2LA0i5n otMbR6FDFzm3Bfsu/qQQTdjWjQ8IBtIJAdJMWBb5iJpTZko/V/ZzoAzLu+CtHXkASYTujkVtI0GOm mmcoybjVwU/tqXXdoTlycw3ZX0fxiuwCb3ue8JUolTaA4QNOoZ2ZzyP+z/IeqKPzLBJt3zJ6QDzox rN/D8I+AXiROqGx5zm8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMnP-00ABLP-2k; Fri, 14 Apr 2023 17:01:11 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpg-00A1dv-23 for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:29 +0000 Received: by mail-pl1-x635.google.com with SMTP id lh8so5739139plb.1 for ; Fri, 14 Apr 2023 08:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487966; x=1684079966; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=6nGKmDwG9+jNVeR75v05JMRVwMFJUAElECktRxuQgsc=; b=mM2vdqpkz2PBDDMrZvLiutf/ppjF1tCkaGP/crzjPrGQC7zlwYPJBix5iA6jmNH8fR LYABnDcmXEMK5KGZbECXDl4ndHVN2C0fLoIlOLGPm7BDAxe7VsWXcTPlVE3ojjKfwS/+ dp7odakVnTOC3JBLRQ1xgAhaDy8CeITNr4D+Hs3XGChG32aocBrilAGAw+HV+is8Kf5G gZTZehgkU4QgO+G8kPL0RzpDo6rLzM7MbfDySeGYYAbt/ory/u5oC+bMs+WdMbPAQBZ0 va77J9q6zwz0bzp9Wb1xVOGjsWQfClGhNn0HMmGLhFJJC5DNWPeu7sFGIyuUs/omzpyB cY7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487966; x=1684079966; 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=6nGKmDwG9+jNVeR75v05JMRVwMFJUAElECktRxuQgsc=; b=HspapT6C/dSn4XalplzYUkm+uAhqXJAyctPzfZswExiZm5Lw185227IGC+ksa7tXVJ FmAing2Jp6S+wZCmh7JNSHGm7ut24Tkmej22tAQwnSkdh6hvjWHNTq9ZV8HWOQMjoDO/ vhJ1rm47WfineTIoZzplOVt8Im2aYdPhBJPT5Gyvqd5+9S4WRnfMiMvq1Z0VIOJ2+IFj Tou44T/HAIDxWFStsqeaFM6wv1HSjVU1Q9i1O5GHfMR4OJa7vEgd8el+71B0pYZ94RyO A753UoH/EL6KX6KMkiw1OqweLN5zzcGqp7bI2pqaAlDRbI1B5BL9OrRa0zboUDOVauk6 W7zw== X-Gm-Message-State: AAQBX9fYuN15YZkr1gmUTqoQL9ycrZWClOKUHn9x59NGaHLcSpecyNgA e+Nm9jgi5zzPAyaCRzYWuyHftfEX6mPCm6Lmi2McRku0EttyxpWPPr4zJS87I2SYeNyu/pDpGs4 wR5Ym+hxxV4xc5o1nNNtWjEYHfCxQV7qh1qi5jKq6+1GUiEiw1Q7QLGsKnmNyBZrh/WmQ7hIkTL TeCmlbqHFEQWsC X-Google-Smtp-Source: AKy350bcQ+ZmyMAff8Obq6yV3BPKqrDn2XpjHAqeL+rg6LKB5pICPuePzioMZgZyYVR/Af5fsEAdrg== X-Received: by 2002:a17:902:e54a:b0:1a2:3b6:8319 with SMTP id n10-20020a170902e54a00b001a203b68319mr3730449plf.54.1681487966260; Fri, 14 Apr 2023 08:59:26 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:25 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Guo Ren , Heiko Stuebner Subject: [PATCH -next v18 06/20] riscv: Introduce Vector enable/disable helpers Date: Fri, 14 Apr 2023 15:58:29 +0000 Message-Id: <20230414155843.12963-7-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085928_676446_F049615E X-CRM114-Status: UNSURE ( 6.37 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu These are small and likely to be frequently called so implement as inline routines (vs. function call). Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/vector.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 427a3b51df72..dfe5a321b2b4 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -11,12 +11,23 @@ #ifdef CONFIG_RISCV_ISA_V #include +#include static __always_inline bool has_vector(void) { return riscv_has_extension_likely(RISCV_ISA_EXT_v); } +static __always_inline void riscv_v_enable(void) +{ + csr_set(CSR_SSTATUS, SR_VS); +} + +static __always_inline void riscv_v_disable(void) +{ + csr_clear(CSR_SSTATUS, SR_VS); +} + #else /* ! CONFIG_RISCV_ISA_V */ static __always_inline bool has_vector(void) { return false; } From patchwork Fri Apr 14 15:58:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211772 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 7B5AAC77B6E for ; Fri, 14 Apr 2023 15:59:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ZK1hGavdOc4hafFH4+8R9RBFXFZMOmQn561qEQYimgE=; b=WTC7FhMhaDUezNEciStYfEcTnn BCFv8wRLPYF8zx5JNr+HYNe685MgFMFXSexd2xO8fz5fdViy9sDvHjD9v1hZ6B9yCm6/7I9IBXBPQ 2Vr2COTtOLelNoCioTDTDJb3lPBggHYd5T2WHpJ57KyR8/pQtnx6mzr2FClTIHDjlTk3MgOPOc5kB kJy20YjHtZQDWP9xHD5rtvsO9DfIq4yykJOL/Z5lBRKCXIrlR/1WZ6a4cTlz8z5avCEmtAqpzPMhQ SvB7mH1gqMcXTcojQGVkh670OZkj4h5h1+gsigFzq/bxYyfQY6ngss4HOjV7PSmZz8pkZAzOwePAv 900EGSgw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpt-00A1kH-2q; Fri, 14 Apr 2023 15:59:41 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpq-00A1iP-1y for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:40 +0000 Received: by mail-pj1-x1032.google.com with SMTP id b2-20020a17090a6e0200b002470b249e59so8043905pjk.4 for ; Fri, 14 Apr 2023 08:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487976; x=1684079976; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=B94BkZ7daghX/inmD9rHvCSaX9DVRA1XEaP0tUQccbw=; b=LGr6tEu6iREyythLaMUfkOHD8BAkTlTDZJ8giXkI8tJt21GrojUizZGrI2hy59fwpj wO0jlf/s0CaWkqSF7QDb7Rl1tZLYhOXuUf/Y01Q2DD7vvmQXoSvRT+2Hdn/pWWnkhQF8 RW1jo+54Pnl0cCKNIHu9VbvqZI2g5MVkieqIrQY4kalhyMuzxm5oqrvkaemp6my8/ADd 7u5FslGK6m6ZXU+DlTmjpF5W6IhBTWJbMA82hUN6bHj8X0AuYrTiJCOo5WcblZ39lg/n I54+3Jwev4NKkR0L+CxitJE1odf5zuDsAjI32yqzhYraD3WGZ4pKxTjl9YQtc5W2fLsx jp5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487976; x=1684079976; 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=B94BkZ7daghX/inmD9rHvCSaX9DVRA1XEaP0tUQccbw=; b=bjh0aZf017X8bujFULCwMxpNOIZDsfRkco4k/pdOoxqykdWCvwS2Tav/4FTCcHZk2p M+UHojRzin4Ym6wLKMeToxqrLXIMg3PfqV87CJD2RNA1BFyVmiukHJXs91CaG4dq7zZc vnIwwHB3pZJkth2KLsibzROIRf4YXmZSSUruYaX5kG7b8gdvXsYPoPzoz7A9Zog619vH 9G20vq6+zbJk5/DKwKJUTSPkzQ/DZt7PIK8K5eVtjMXZEPbw7XwlI9Oh7jwhzMeVbbGJ aNzDJeIXYoA+hCyrHQvlb0D2ACQL4lwwWOIK2EThX1CZgVcVVGQkoyXhRo8IKwXTRoA3 ccFw== X-Gm-Message-State: AAQBX9fIy2XjSbTuOVHRlyYKSayDifWjifkLAKWIkhSxE68ghS7YF7Oc 1Dp7Hw6K4onvma5sNkKPZO78q0ssud1pf0n8GIGn3951TfKtwCEmFabRCGs+3OeolR9HbOewhqH 9m+enFO6dSsim5e42OGKA6Gp8kBWBSMbWBfwZbijm0v+LmJkVV+wLkDNtHKUJ/M/2pSn8WR8J1U xeh6eiSZO9LJl1 X-Google-Smtp-Source: AKy350Z/GDCabIKjwCihnSew4qRlImLymQVij6vd9gfZIWbugGjsZpJbbJRbZrStHp+sqZMowbsY5w== X-Received: by 2002:a17:90b:215:b0:23d:376a:c2bc with SMTP id fy21-20020a17090b021500b0023d376ac2bcmr6023241pjb.5.1681487976338; Fri, 14 Apr 2023 08:59:36 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:35 -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 Subject: [PATCH -next v18 07/20] riscv: Introduce riscv_v_vsize to record size of Vector context Date: Fri, 14 Apr 2023 15:58:30 +0000 Message-Id: <20230414155843.12963-8-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085938_650903_004E75E3 X-CRM114-Status: GOOD ( 19.30 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: guoren@linux.alibaba.com, Jisheng Zhang , Xianting Tian , Liao Chang , Masahiro Yamada , vineetg@rivosinc.com, Philipp Tomsich , Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Andy Chiu , Paul Walmsley , Ley Foon Tan , greentime.hu@sifive.com, Heiko Stuebner , Li Zhengyu , Andrew Jones MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch is used to detect the size of CPU vector registers and use riscv_v_vsize to save the size of all the vector registers. It assumes all harts has the same capabilities in a SMP system. If a core detects VLENB that is different from the boot core, then it warns and turns off V support for user space. Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- Changes in v18: - Detect inconsistent VLEN setup on an SMP system (Heiko). arch/riscv/include/asm/vector.h | 8 ++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ arch/riscv/kernel/smpboot.c | 7 +++++++ arch/riscv/kernel/vector.c | 36 +++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 arch/riscv/kernel/vector.c diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index dfe5a321b2b4..68c9fe831a41 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -7,12 +7,16 @@ #define __ASM_RISCV_VECTOR_H #include +#include #ifdef CONFIG_RISCV_ISA_V #include #include +extern unsigned long riscv_v_vsize; +int riscv_v_setup_vsize(void); + static __always_inline bool has_vector(void) { return riscv_has_extension_likely(RISCV_ISA_EXT_v); @@ -30,7 +34,11 @@ static __always_inline void riscv_v_disable(void) #else /* ! CONFIG_RISCV_ISA_V */ +struct pt_regs; + +static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +#define riscv_v_vsize (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 392fa6e35d4a..be23a021ec32 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -55,6 +55,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o +obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 923ca75f2192..267070f3cc9e 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -17,6 +17,7 @@ #include #include #include +#include #define NUM_ALPHA_EXTS ('z' - 'a' + 1) @@ -263,6 +264,7 @@ void __init riscv_fill_hwcap(void) } if (elf_hwcap & COMPAT_HWCAP_ISA_V) { + riscv_v_setup_vsize(); /* * ISA string in device tree might have 'v' flag, but * CONFIG_RISCV_ISA_V is disabled in kernel. diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index ddb2afba6d25..67ae124db5a4 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include "head.h" @@ -169,6 +171,11 @@ asmlinkage __visible void smp_callin(void) numa_add_cpu(curr_cpuid); set_cpu_online(curr_cpuid, 1); + if (has_vector()) { + if (riscv_v_setup_vsize()) + elf_hwcap &= ~COMPAT_HWCAP_ISA_V; + } + /* * Remote TLB flushes are ignored while the CPU is offline, so emit * a local TLB flush right now just in case. diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c new file mode 100644 index 000000000000..53bb32546248 --- /dev/null +++ b/arch/riscv/kernel/vector.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2023 SiFive + * Author: Andy Chiu + */ +#include + +#include +#include +#include +#include + +unsigned long riscv_v_vsize __read_mostly; +EXPORT_SYMBOL_GPL(riscv_v_vsize); + +int riscv_v_setup_vsize(void) +{ + unsigned long this_vsize; + + /* There are 32 vector registers with vlenb length. */ + riscv_v_enable(); + this_vsize = csr_read(CSR_VLENB) * 32; + riscv_v_disable(); + + if (!riscv_v_vsize) { + riscv_v_vsize = this_vsize; + return 0; + } + + if (riscv_v_vsize != this_vsize) { + WARN(1, "RISCV_ISA_V only supports one vlenb on SMP system"); + return -EOPNOTSUPP; + } + + return 0; +} From patchwork Fri Apr 14 15:58:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211773 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 34C66C77B6E for ; Fri, 14 Apr 2023 15:59:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4YvH7LDuw9eKzo0P1YdgUaffCrIwFjoW3VdP6q6mwjU=; b=HniJjOW3e2jnum jZnmHNe3J0YkYDQ6YyNxPliYcKHrAC3vUKdHqdlyUcUOoshpjNQzAkglayRfbh0cWnx7fo4FjBm8H M/TI94WBgrt0dZ04C5ubvju44AslvvmNDa2mMUMaCMfESq3OJbaZtDDmK+EuQ0olcEKlQf3Hfl0Gc OGlxV8w93B4QqbFMv+3603AK5xieq09Oz5Nv3wbM13NU9gcHG+NRMbHQ5B+JtSDvDQz5EeuNXXCKd CC4h+Gn0sl2A+tfoXOnlJw/inwkIpaLovs/1PDKs9KGIbphmW3kElEruwTh4Mrv4HR/5pGdKOXHGx Ypc9kK/phQOvNClMcCTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpy-00A1n7-30; Fri, 14 Apr 2023 15:59:46 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLpw-00A1l6-1J for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:46 +0000 Received: by mail-pl1-x633.google.com with SMTP id kh6so17138038plb.0 for ; Fri, 14 Apr 2023 08:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487982; x=1684079982; 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=ahp5kQnTghgkZmiSTxR6+eMYSF2yeYW/B9Sl3mLm86I=; b=jWHmU/ESlEHOVioBV8QithJ4pWOgoHH5LTG4cTPCqQwFXhWGAV+CAw9vIPolQzdFKg 1O8hU9YR2eD4QNJ4eslFexI6xAN7K+8XELN9YWB/Q5ikzgY4TKcWePvNli9yKYw2Qf// UGjw4S3bQB0GRC/9puQ/wr8kgEXdBfo5dqG0yEU3DuPl4SDI5vKnvhdG5aWoD97UQuZe qI1AGxRQmg0qa/pJDjOuU/WuRmfA+ua04g027fEB2zwwZiuTlvIzlPC/I4xVtvft3Akc JRcNGAbJ9my8qAf2FFNaXFNEhXyL2mS31Upn6s6ICL1VPqmeM9RzDm2hiP5epyqX6YTu KmMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487982; x=1684079982; 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=ahp5kQnTghgkZmiSTxR6+eMYSF2yeYW/B9Sl3mLm86I=; b=FZbUYEwjr3jdNCLQHUflf/H9dSZFp4+sEc/V06wINDDT7de+yg5eAeEQCBq5v4+MBW 2GlNDC18RcVJfaKsaKv48XRaPAMvqzd2hJKqgvSQOWBR7PnCe1pu4SDlnDJRpD94f+6s bg0F04CYSDvKzXrv7HO2435KG64OsaBIldH+c3u1Wis9k91JmlMAI3ijOoKPtfbJaMvO rGwvrRH1q4rMYozQUr4dxcoPuLQClkfHL9F1JBAFyjZQFL9FCfmtz45MoDoHuFtvuaNn pu0yEKn7H5nFTa8YmuepwEmOFwoYgn2nnuKAa30eoaOKP0XwJwfNCmzNcw3vNxXB6s4m qXnQ== X-Gm-Message-State: AAQBX9fccSQijlK6AXX5kFYBjwfNtUx8gza/LpqsPaCIZ7CmJvZR2/MV JZBHPmfDZnN/7ft0m0ffDK1v2YZxXYtY0Qq7rl6RrEKugWQ1XbK4gIBXRxJttGDuqA6i1oPFHev Sr6WMBzc4LeCyIDv+BZC/FUaqnUXjz0FkUiCgHoF8lopjaRxOFZvNqCAYNwtLvjiO7pyrHHtZX5 yPJxBR1vLu5bYb X-Google-Smtp-Source: AKy350ZKao8CL5faXPhsQHH/k9HmEq8p+sEw8aF/D7/B8/02SnVH9xnwups8GJsXNgURoBC0Xrs70Q== X-Received: by 2002:a17:90a:2e13:b0:23f:3f9c:7878 with SMTP id q19-20020a17090a2e1300b0023f3f9c7878mr5945626pjd.2.1681487981638; Fri, 14 Apr 2023 08:59:41 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:41 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Guo Ren , Heiko Stuebner , Conor Dooley Subject: [PATCH -next v18 08/20] riscv: Introduce struct/helpers to save/restore per-task Vector state Date: Fri, 14 Apr 2023 15:58:31 +0000 Message-Id: <20230414155843.12963-9-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085944_443072_33D46C86 X-CRM114-Status: GOOD ( 12.14 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Add vector state context struct to be added later in thread_struct. And prepare low-level helper functions to save/restore vector contexts. This include Vector Regfile and CSRs holding dynamic configuration state (vstart, vl, vtype, vcsr). The Vec Register width could be implementation defined, but same for all processes, so that is saved separately. This is not yet wired into final thread_struct - will be done when __switch_to actually starts doing this in later patches. Given the variable (and potentially large) size of regfile, they are saved in dynamically allocated memory, pointed to by datap pointer in __riscv_v_ext_state. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Guo Ren Reviewed-by: Björn Töpel Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/vector.h | 95 ++++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/ptrace.h | 17 +++++ 2 files changed, 112 insertions(+) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 68c9fe831a41..7a56bb0769aa 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -11,8 +11,10 @@ #ifdef CONFIG_RISCV_ISA_V +#include #include #include +#include extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); @@ -22,6 +24,26 @@ static __always_inline bool has_vector(void) return riscv_has_extension_likely(RISCV_ISA_EXT_v); } +static inline void __riscv_v_vstate_clean(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN; +} + +static inline void riscv_v_vstate_off(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_OFF; +} + +static inline void riscv_v_vstate_on(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_INITIAL; +} + +static inline bool riscv_v_vstate_query(struct pt_regs *regs) +{ + return (regs->status & SR_VS) != 0; +} + static __always_inline void riscv_v_enable(void) { csr_set(CSR_SSTATUS, SR_VS); @@ -32,13 +54,86 @@ static __always_inline void riscv_v_disable(void) csr_clear(CSR_SSTATUS, SR_VS); } +static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest) +{ + asm volatile ( + "csrr %0, " __stringify(CSR_VSTART) "\n\t" + "csrr %1, " __stringify(CSR_VTYPE) "\n\t" + "csrr %2, " __stringify(CSR_VL) "\n\t" + "csrr %3, " __stringify(CSR_VCSR) "\n\t" + : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl), + "=r" (dest->vcsr) : :); +} + +static __always_inline void __vstate_csr_restore(struct __riscv_v_ext_state *src) +{ + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvl x0, %2, %1\n\t" + ".option pop\n\t" + "csrw " __stringify(CSR_VSTART) ", %0\n\t" + "csrw " __stringify(CSR_VCSR) ", %3\n\t" + : : "r" (src->vstart), "r" (src->vtype), "r" (src->vl), + "r" (src->vcsr) :); +} + +static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to, + void *datap) +{ + unsigned long vl; + + riscv_v_enable(); + __vstate_csr_save(save_to); + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvli %0, x0, e8, m8, ta, ma\n\t" + "vse8.v v0, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v8, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v16, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v24, (%1)\n\t" + ".option pop\n\t" + : "=&r" (vl) : "r" (datap) : "memory"); + riscv_v_disable(); +} + +static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_from, + void *datap) +{ + unsigned long vl; + + riscv_v_enable(); + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvli %0, x0, e8, m8, ta, ma\n\t" + "vle8.v v0, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v8, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v16, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v24, (%1)\n\t" + ".option pop\n\t" + : "=&r" (vl) : "r" (datap) : "memory"); + __vstate_csr_restore(restore_from); + riscv_v_disable(); +} + #else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) +#define riscv_v_vstate_off(regs) do {} while (0) +#define riscv_v_vstate_on(regs) do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 882547f6bd5c..586786d023c4 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -77,6 +77,23 @@ union __riscv_fp_state { struct __riscv_q_ext_state q; }; +struct __riscv_v_ext_state { + unsigned long vstart; + unsigned long vl; + unsigned long vtype; + unsigned long vcsr; + void *datap; + /* + * In signal handler, datap will be set a correct user stack offset + * and vector registers will be copied to the address of datap + * pointer. + * + * In ptrace syscall, datap will be set to zero and the vector + * registers will be copied to the address right after this + * structure. + */ +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ From patchwork Fri Apr 14 15:58:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211774 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 85155C77B6E for ; Fri, 14 Apr 2023 16:00:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IdG5ldH4fiHDLdK45WYa3ztIzjxWUyY0JSanofFcA5c=; b=Xz9xLDS/idzNWz mLHXlUTauzpENcHJ5kKNECwg5e1rofCOUrDH0zQcQkmdtTLPH+t2XQd+gfWtTnOYtFq/yLrTM0s/c XXhwW2NVLA0ztWXpZ83/6GEUnSJJO2IXq6REFu1ehaudleuyodvLUvMJYwNc8cBw4ujI2XF1X0nAA IJRBXya2rouDP6dHwPt3sE8d/UGzvZi/YLAC2L80gJUVJ9WhfBlV6fxBhb3DTNces7GnHdBCXMOjt zI21O37VVsmAl1v5xwFKyjKeYQ9WulZQWiLYbIEkRMwoLkK2meTNvH5HuYlIlD676r9HlL8a1U2BD dUZXKGoQPDOTo3bx5DOA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLq7-00A1r5-0A; Fri, 14 Apr 2023 15:59:55 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLq4-00A1ob-0R for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 15:59:53 +0000 Received: by mail-pl1-x62b.google.com with SMTP id y6so17636658plp.2 for ; Fri, 14 Apr 2023 08:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487990; x=1684079990; 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=W9PBJBzHlcHA8nlVu28xWsGDRnYddiwOeG1IiMG6RqE=; b=Edd4OMMhDqG8RB9EmrkRtFwsLXlZjU86VS859kJ5CmT2cg9JExrOAbjO1DiTXb2WLw cc+8Nj4Kv8VcxeUIx9pwkYNzd/BrJ4v/tYG4G6FBZMa1Qw1xIvYyqsKB3cnRs+x8q8DZ FMhbVR5NP2Up4YoGvBmWdS/+cGnLw7mKF7ENjBoJnughvHks4/Crg7FnoyOl4bBiAHZZ pwEIjGsbkTDP0uBjFRvfe7CpfAf5xuaPhJmPWLCr+SwTO5XLpKu9k3gDB/bgdtfe9JF7 ll3RSYlywOWj6p4Eb3KeaJhSqc3uon+JqkNDi58vqOfF19XTmYbbNokc8NMrhWQ8gOiR SMYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487990; x=1684079990; 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=W9PBJBzHlcHA8nlVu28xWsGDRnYddiwOeG1IiMG6RqE=; b=iuzkGaOSwun7oxZ2UIAOw+wDYQ0Vo9wuc1wFWCS51zkE+TdOhS/UA1iapeQOgZFbng 6iJFnliNB6wMD2NJ1HaDNIsYRHa38xqOnReJKJi7gUoiOAocVCwpO3hHBBWXCKqWGAXj T1dIipVixgSQAlMTIvneLDUioGWcGySULvfWZko/Gy+lPNi+8J73/kOK1EbMueGx4WnE kH4jHfwhOU84SZ5qkqEXGzuUgach6+WPbi6t8+NedjsnExumP/QL+3c3TH92tipJUMH3 TVVkLZQzWp1/wnSjEi58Cqr1KWJqGattFy0A8qpaz8XFsJ1RyQD3GfVOe/tR8Go8bHKy Z8og== X-Gm-Message-State: AAQBX9fFQnZnMVYxzppFr/aUpJ0O/gGqoAKlXhZgcL4SQWgNaM+4QcnT VF6AilsttFt9IZNawYIhjCrvl71C307DAWgpfW9pdh0jru2SHTkJgCe0oWsLk3U94Xkwi/1593v 2JDXEjS03frig3lrL8Gc9/dPVBDcMG59uJ4FZlEcf7/S/9npUWMVePQJ+U8h0TIPqDL3dOaqAaa xBWGC7bzCZMGkp X-Google-Smtp-Source: AKy350aCXuYBAIZyny6piP6ULNC+rZHjP4csuqHeALfTJgSZ0YWbo2iKIFgOERp1f54abTQs87nfpA== X-Received: by 2002:a17:90b:3909:b0:246:fd44:eb6f with SMTP id ob9-20020a17090b390900b00246fd44eb6fmr6113290pjb.39.1681487990329; Fri, 14 Apr 2023 08:59:50 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:49 -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 Subject: [PATCH -next v18 09/20] riscv: Add task switch support for vector Date: Fri, 14 Apr 2023 15:58:32 +0000 Message-Id: <20230414155843.12963-10-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_085952_179025_C25C30F7 X-CRM114-Status: GOOD ( 16.52 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kefeng Wang , guoren@linux.alibaba.com, Jisheng Zhang , Nick Knight , Peter Zijlstra , vineetg@rivosinc.com, =?utf-8?b?Qmo=?= =?utf-8?b?w7ZybiBUw7ZwZWw=?= , Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Ruinland Tsai , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, Heiko Stuebner , "Eric W. Biederman" Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch adds task switch support for vector. It also supports all lengths of vlen. Suggested-by: Andrew Waterman Co-developed-by: Nick Knight Signed-off-by: Nick Knight Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Co-developed-by: Ruinland Tsai Signed-off-by: Ruinland Tsai Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Björn Töpel Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 3 +++ arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/vector.h | 38 ++++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 18 +++++++++++++ 5 files changed, 63 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 94a0590c6971..f0ddf691ac5e 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -39,6 +39,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + struct __riscv_v_ext_state vstate; }; /* Whitelist the fstate from the task_struct for hardened usercopy */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 4b96b13dee27..a727be723c56 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -78,6 +79,8 @@ do { \ struct task_struct *__next = (next); \ if (has_fpu()) \ __switch_to_fpu(__prev, __next); \ + if (has_vector()) \ + __switch_to_vector(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index e0d202134b44..97e6f65ec176 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -81,6 +81,9 @@ struct thread_info { .preempt_count = INIT_PREEMPT_COUNT, \ } +void arch_release_task_struct(struct task_struct *tsk); +int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); + #endif /* !__ASSEMBLY__ */ /* diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 7a56bb0769aa..121d700c6ada 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -12,6 +12,9 @@ #ifdef CONFIG_RISCV_ISA_V #include +#include +#include +#include #include #include #include @@ -124,6 +127,38 @@ static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_ riscv_v_disable(); } +static inline void riscv_v_vstate_save(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) == SR_VS_DIRTY) { + struct __riscv_v_ext_state *vstate = &task->thread.vstate; + + __riscv_v_vstate_save(vstate, vstate->datap); + __riscv_v_vstate_clean(regs); + } +} + +static inline void riscv_v_vstate_restore(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) != SR_VS_OFF) { + struct __riscv_v_ext_state *vstate = &task->thread.vstate; + + __riscv_v_vstate_restore(vstate, vstate->datap); + __riscv_v_vstate_clean(regs); + } +} + +static inline void __switch_to_vector(struct task_struct *prev, + struct task_struct *next) +{ + struct pt_regs *regs; + + regs = task_pt_regs(prev); + riscv_v_vstate_save(prev, regs); + riscv_v_vstate_restore(next, task_pt_regs(next)); +} + #else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs; @@ -132,6 +167,9 @@ static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) +#define riscv_v_vstate_save(task, regs) do {} while (0) +#define riscv_v_vstate_restore(task, regs) do {} while (0) +#define __switch_to_vector(__prev, __next) do {} while (0) #define riscv_v_vstate_off(regs) do {} while (0) #define riscv_v_vstate_on(regs) do {} while (0) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e2a060066730..b7a10361ddc6 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -24,6 +24,7 @@ #include #include #include +#include register unsigned long gp_in_global __asm__("gp"); @@ -146,12 +147,28 @@ void flush_thread(void) fstate_off(current, task_pt_regs(current)); memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); #endif +#ifdef CONFIG_RISCV_ISA_V + /* Reset vector state */ + riscv_v_vstate_off(task_pt_regs(current)); + kfree(current->thread.vstate.datap); + memset(¤t->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); +#endif +} + +void arch_release_task_struct(struct task_struct *tsk) +{ + /* Free the vector context of datap. */ + if (has_vector()) + kfree(tsk->thread.vstate.datap); } int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) { fstate_save(src, task_pt_regs(src)); *dst = *src; + /* clear entire V context, including datap for a new task */ + memset(&dst->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); + return 0; } @@ -184,6 +201,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = 0; } p->thread.ra = (unsigned long)ret_from_fork; + riscv_v_vstate_off(childregs); p->thread.sp = (unsigned long)childregs; /* kernel sp */ return 0; } From patchwork Fri Apr 14 15:58:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211775 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 62A3BC77B6E for ; Fri, 14 Apr 2023 16:00:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Jk8VFgrh1a7uWR20EEii22gMVGlrKzGAckY4wKQjO8I=; b=MIReZS9BAqGoFc AoUVzNbEKLIXinWl0Htw2x1l2hAi3XcsjQIbBljDASCpbKWaJKhIArxbi8B/zSdHNb3NS/yHcYw0w yTXL108hEl3RTJ5xn5wG93KOrIuoXHuglRO12+LMWLAlCgKiDcD732hWO7cs7+VrGidOyZAE8u4nN 3UWhac/+81AOyEdUZqOEYGh6QP2li9Nz5VXyclA25Yx/+HT2uY3Ti0/aeMi1XmA3saGc+evFHYtcq YVxEgkmSzkbn2ps7lzy8TnA6BbMQVymmtWY1UcJCgIzrsvG7flZJUuZ7jlk7BR2sLmxvFodq7gUDO 1qwlUk57dSYVthEkIeuQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqF-00A1vM-2N; Fri, 14 Apr 2023 16:00:03 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqC-00A1tS-2z for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:02 +0000 Received: by mail-pj1-x1035.google.com with SMTP id g6so1847378pjx.4 for ; Fri, 14 Apr 2023 08:59:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681487999; x=1684079999; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=shPX40kPBCrP0GS9blM0MDp2c5Yma0hjlkLm7oM5TaE=; b=ImK+JqHuwNR/IMKcGV4oZbvG43lofGaaTKRLh6pUZxrOfbFFPQXWsfG5MA7b43128M 9fQv6IFXbDxK+6p4qCLJqMBvqEBzJuJGmwSla4FEM+qKxi9cwkANwJEgXvjPp/ZwDEr5 C3UQjl1DZSfaM4vPWSAauAeAYdscEtQDF9TPcsJQBQpQPpDHcwITqgF9WnwQeiDaWMfB AK0NNQQCaq75fA8ujTnTmPrVcpU3LlHi9phoJo2FUeGhZo/DctNecVgwIIZuEFZJADGL vSwB+Ym0ptREOhsf9uFgjYC+mwZff0q9ZIubUINnk9y3j+rchCwGi1/mvJr1c0+L/Wmy W9hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681487999; x=1684079999; 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=shPX40kPBCrP0GS9blM0MDp2c5Yma0hjlkLm7oM5TaE=; b=I9KY3wPM3CUXRu+gPdJgmHv5PjUnF+hjx3gD1PtGNDvc3Ga79fuKe56O6xBbGgIw/7 eaTj2dgFt7Xs3RBDFx4V3uVsH5kKGxZ3+ntQw42rNlR9mY9xUr6RBKy4Pt9TowiqcDJs 1jMcdQU93HxAbFbhfAONEADF4yJtgBvseMek9wE9Ffj6kzxjfDuNjNs6YySC41M4xMnH UkO2Kg27budoDt1ohBl5jLLduS+R0fZNvsJ+9FwYR0hB5rooDq/+F2+2ryTN7tCuSuce NevhwAi65iOfFkdZtMatujd9o064b/yCAxx1rfrNXiBMLok9+XgLk+0vCUyh0Yv+EaW1 lhbw== X-Gm-Message-State: AAQBX9eOMzc1kTk5V7Dqd257PqmsTouwMYhRVargrdEPbWP3IkqLNGLP cZ3BDFW3Njo2e2VdR+H6lLz0lgiCqzSZW3LZp0Fw8w306cbAUtfkKM+FsshFxmVUyE0747fb3gN UcYk0RnSRRYBCrmiI3v9Iyy1cXtfUZTDebho+3fTxIM8ZTOroaZ1+mS4GZpFvGEaKxU8V8t2kau XjJgN5AVcNIAE3 X-Google-Smtp-Source: AKy350YYIXlTgo3whumNWE65A9o9EP5h957lra64VKK98gurCgHN6ECMYPF7liyNqPyU6f0btwUyMg== X-Received: by 2002:a17:90a:ff04:b0:246:5f9e:e4cd with SMTP id ce4-20020a17090aff0400b002465f9ee4cdmr5840607pjb.10.1681487998810; Fri, 14 Apr 2023 08:59:58 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.08.59.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 08:59:58 -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 , Heiko Stuebner , Andrew Jones , Conor Dooley , Lad Prabhakar , Liao Chang , Jisheng Zhang , Guo Ren , Vincent Chen , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Xianting Tian , Mattias Nissler Subject: [PATCH -next v18 10/20] riscv: Allocate user's vector context in the first-use trap Date: Fri, 14 Apr 2023 15:58:33 +0000 Message-Id: <20230414155843.12963-11-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090000_964188_D679ACD8 X-CRM114-Status: GOOD ( 19.90 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Vector unit is disabled by default for all user processes. Thus, a process will take a trap (illegal instruction) into kernel at the first time when it uses Vector. Only after then, the kernel allocates V context and starts take care of the context for that user process. Suggested-by: Richard Henderson Link: https://lore.kernel.org/r/3923eeee-e4dc-0911-40bf-84c34aee962d@linaro.org Signed-off-by: Andy Chiu Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Conor Dooley --- Changes in v18: - Add blank lines (Heiko) - Return immediately in insn_is_vector() if an insn matches (Heiko) arch/riscv/include/asm/insn.h | 29 +++++++++++ arch/riscv/include/asm/vector.h | 2 + arch/riscv/kernel/traps.c | 26 +++++++++- arch/riscv/kernel/vector.c | 91 +++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h index 8d5c84f2d5ef..4e1505cef8aa 100644 --- a/arch/riscv/include/asm/insn.h +++ b/arch/riscv/include/asm/insn.h @@ -137,6 +137,26 @@ #define RVG_OPCODE_JALR 0x67 #define RVG_OPCODE_JAL 0x6f #define RVG_OPCODE_SYSTEM 0x73 +#define RVG_SYSTEM_CSR_OFF 20 +#define RVG_SYSTEM_CSR_MASK GENMASK(12, 0) + +/* parts of opcode for RVF, RVD and RVQ */ +#define RVFDQ_FL_FS_WIDTH_OFF 12 +#define RVFDQ_FL_FS_WIDTH_MASK GENMASK(3, 0) +#define RVFDQ_FL_FS_WIDTH_W 2 +#define RVFDQ_FL_FS_WIDTH_D 3 +#define RVFDQ_LS_FS_WIDTH_Q 4 +#define RVFDQ_OPCODE_FL 0x07 +#define RVFDQ_OPCODE_FS 0x27 + +/* parts of opcode for RVV */ +#define RVV_OPCODE_VECTOR 0x57 +#define RVV_VL_VS_WIDTH_8 0 +#define RVV_VL_VS_WIDTH_16 5 +#define RVV_VL_VS_WIDTH_32 6 +#define RVV_VL_VS_WIDTH_64 7 +#define RVV_OPCODE_VL RVFDQ_OPCODE_FL +#define RVV_OPCODE_VS RVFDQ_OPCODE_FS /* parts of opcode for RVC*/ #define RVC_OPCODE_C0 0x0 @@ -304,6 +324,15 @@ static __always_inline bool riscv_insn_is_branch(u32 code) (RVC_X(x_, RVC_B_IMM_7_6_OPOFF, RVC_B_IMM_7_6_MASK) << RVC_B_IMM_7_6_OFF) | \ (RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); }) +#define RVG_EXTRACT_SYSTEM_CSR(x) \ + ({typeof(x) x_ = (x); RV_X(x_, RVG_SYSTEM_CSR_OFF, RVG_SYSTEM_CSR_MASK); }) + +#define RVFDQ_EXTRACT_FL_FS_WIDTH(x) \ + ({typeof(x) x_ = (x); RV_X(x_, RVFDQ_FL_FS_WIDTH_OFF, \ + RVFDQ_FL_FS_WIDTH_MASK); }) + +#define RVV_EXRACT_VL_VS_WIDTH(x) RVFDQ_EXTRACT_FL_FS_WIDTH(x) + /* * Get the immediate from a J-type instruction. * diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 121d700c6ada..a8881af83ce4 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -21,6 +21,7 @@ extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); +bool riscv_v_first_use_handler(struct pt_regs *regs); static __always_inline bool has_vector(void) { @@ -165,6 +166,7 @@ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) #define riscv_v_vstate_save(task, regs) do {} while (0) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 8c258b78c925..24d309c6ab8d 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -26,6 +26,7 @@ #include #include #include +#include int show_unhandled_signals = 1; @@ -145,8 +146,29 @@ DO_ERROR_INFO(do_trap_insn_misaligned, SIGBUS, BUS_ADRALN, "instruction address misaligned"); DO_ERROR_INFO(do_trap_insn_fault, SIGSEGV, SEGV_ACCERR, "instruction access fault"); -DO_ERROR_INFO(do_trap_insn_illegal, - SIGILL, ILL_ILLOPC, "illegal instruction"); + +asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_user_mode(regs); + + local_irq_enable(); + + if (!has_vector() || !riscv_v_first_use_handler(regs)) + do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc, + "Oops - illegal instruction"); + + irqentry_exit_to_user_mode(regs); + } else { + irqentry_state_t state = irqentry_nmi_enter(regs); + + do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc, + "Oops - illegal instruction"); + + irqentry_nmi_exit(regs, state); + } +} + DO_ERROR_INFO(do_trap_load_fault, SIGSEGV, SEGV_ACCERR, "load access fault"); #ifndef CONFIG_RISCV_M_MODE diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 53bb32546248..1457dc6f6fd4 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -4,10 +4,19 @@ * Author: Andy Chiu */ #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include unsigned long riscv_v_vsize __read_mostly; @@ -34,3 +43,85 @@ int riscv_v_setup_vsize(void) return 0; } + +static bool insn_is_vector(u32 insn_buf) +{ + u32 opcode = insn_buf & __INSN_OPCODE_MASK; + u32 width, csr; + + /* + * All V-related instructions, including CSR operations are 4-Byte. So, + * do not handle if the instruction length is not 4-Byte. + */ + if (unlikely(GET_INSN_LENGTH(insn_buf) != 4)) + return false; + + switch (opcode) { + case RVV_OPCODE_VECTOR: + return true; + case RVV_OPCODE_VL: + case RVV_OPCODE_VS: + width = RVV_EXRACT_VL_VS_WIDTH(insn_buf); + if (width == RVV_VL_VS_WIDTH_8 || width == RVV_VL_VS_WIDTH_16 || + width == RVV_VL_VS_WIDTH_32 || width == RVV_VL_VS_WIDTH_64) + return true; + + break; + case RVG_OPCODE_SYSTEM: + csr = RVG_EXTRACT_SYSTEM_CSR(insn_buf); + if ((csr >= CSR_VSTART && csr <= CSR_VCSR) || + (csr >= CSR_VL && csr <= CSR_VLENB)) + return true; + } + + return false; +} + +static int riscv_v_thread_zalloc(void) +{ + void *datap; + + datap = kzalloc(riscv_v_vsize, GFP_KERNEL); + if (!datap) + return -ENOMEM; + + current->thread.vstate.datap = datap; + memset(¤t->thread.vstate, 0, offsetof(struct __riscv_v_ext_state, + datap)); + return 0; +} + +bool riscv_v_first_use_handler(struct pt_regs *regs) +{ + u32 __user *epc = (u32 __user *)regs->epc; + u32 insn = (u32)regs->badaddr; + + /* If V has been enabled then it is not the first-use trap */ + if (riscv_v_vstate_query(regs)) + return false; + + /* Get the instruction */ + if (!insn) { + if (__get_user(insn, epc)) + return false; + } + + /* Filter out non-V instructions */ + if (!insn_is_vector(insn)) + return false; + + /* Sanity check. datap should be null by the time of the first-use trap */ + WARN_ON(current->thread.vstate.datap); + + /* + * Now we sure that this is a V instruction. And it executes in the + * context where VS has been off. So, try to allocate the user's V + * context and resume execution. + */ + if (riscv_v_thread_zalloc()) { + force_sig(SIGKILL); + return true; + } + riscv_v_vstate_on(regs); + return true; +} From patchwork Fri Apr 14 15:58:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211776 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 376B1C77B6E for ; Fri, 14 Apr 2023 16:00:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CwPSrtVrBk14F3qx7Jc27OgGdUuv6k4O4XIdgZV2BfY=; b=imcpw0QoNeL4DB oMXk/9e1VZGf4Najs6tw+8suPZlzPDeEDZjHu8dCOUUJ59ZG/o9q0/AQF8PhTlZw/FuFQCY8k+EgQ cPBrwkG8CslGM89yQSDnhOKahm63NNscqRHs6i35MuUrUQQOv69/Di63ma7m6cVL0IhlvUw+TVzjy Epu3B7fBHQfXRUpokz02UvPqX7Kd65OfBMmrGedGYqKnivV9QEBTFGlS36OwHQ2nvgF4XyaW6goVP 3CywiutOms73UP5OsjMBRbNZ4CQR1osy12cgKnfbzB3ipzwcImTP029L15CoRaqHMm23ByDeoDf0E xuXHVPyvOwdY1hGRz2RQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqL-00A1yN-1S; Fri, 14 Apr 2023 16:00:09 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqI-00A1wb-0B for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:07 +0000 Received: by mail-pj1-x1032.google.com with SMTP id mq14-20020a17090b380e00b002472a2d9d6aso4422930pjb.5 for ; Fri, 14 Apr 2023 09:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488005; x=1684080005; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=a65b+du8aFNXXTEZ/EQ3wHPYPcrhTwtdey3aCUIWsNk=; b=XbvmyhijWNX+1F3ln8VlPekaqrwI4WWnOocEcmV4hs9e3sS0d1+GTH/js7q92+zeQG U+SDDtlxV18U9F4FtYcZA8LdpOUdM16K1joqjz/O3G4QNIlmJ7bzbWseTJtkxWTwopvT CvX6HM4r1ZQ2JSxoMdkSxr1U1ERS6ANfwvxRL7W2X9BDy57pDx5juUHDJZRmpzDvLsCF C/6aACkNzVJRha1t/uSoLGPuhUPC3nRJe5iZHXXV1NGQm2TEsJXT1dtz6TBiizVG3XWY 2qA1y4YaBXtLYezq/hnSAp++CnLluylrEYuj+T6fiFpPh2onjEc82K7DxJuEbniRXUX6 LuGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488005; x=1684080005; 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=a65b+du8aFNXXTEZ/EQ3wHPYPcrhTwtdey3aCUIWsNk=; b=SdNqHGllKMTJLy0vczecKD2kpD4RS76TM7w3Yco4sg6xvLYsNVVRhlWU6y98oYq6BK 8Rolx9l3OFmeHwIln/u+u1YjVq2pSMnt1JjQEuXfEZoShpiVh4Lk15zFzpYhslgUuzja L0Q4fghf30SNwV0LUZd3UsBIg72o31OOZrpvXNl1mesdzfkqTVfs5tGjse6qZywH32zK RQuZGz9ginCYufdAx+tga2SrUEqZfznOVimQEaj9baZljZAEUfhMw5i9ITdipM1L5vk7 8ucofboitNn4LmDYKHVrtpN4tCsEbObQKiCQ1E+1ytGEAnBnodqLzcv+J4+9Kd16W/Zf kqag== X-Gm-Message-State: AAQBX9c8tivkuQ7cGqxU113D1Syy8zg5wkKHveRz7K1qwJc2DtVuRnq/ M4sJn8Qjb+OuOuXTHBwmL5U1/JRi7A8eV1LuPhPhR4dMnaBl98B6akw4p9L+vP6womMPrA1FX1C oJc4JFK9Vxeq/WmdjiARa0s0y54nXA+mZhmR1rb6z4W9fLSq9mwYSENlyP1Zy3FBvsK45uZMq0C xNco5tWkTOdNNw X-Google-Smtp-Source: AKy350ZmWNEcmvKYQR8MAIFVjXFcQNcRkmcFcA0dLctILDIf6HCnMnKxM+m7tf8yQbqu2zMwWbUfeA== X-Received: by 2002:a17:90a:b783:b0:23d:3fbe:2f7 with SMTP id m3-20020a17090ab78300b0023d3fbe02f7mr6167161pjr.20.1681488004860; Fri, 14 Apr 2023 09:00:04 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:04 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Oleg Nesterov , Eric Biederman , Kees Cook , Heiko Stuebner , Conor Dooley , Catalin Marinas , Mark Brown , Huacai Chen , Qing Zhang , Alexey Dobriyan , Rolf Eike Beer Subject: [PATCH -next v18 11/20] riscv: Add ptrace vector support Date: Fri, 14 Apr 2023 15:58:34 +0000 Message-Id: <20230414155843.12963-12-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090006_092899_128953C6 X-CRM114-Status: GOOD ( 21.06 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch adds ptrace support for riscv vector. The vector registers will be saved in datap pointer of __riscv_v_ext_state. This pointer will be set right after the __riscv_v_ext_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 Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley --- Changes in v18: - Use sizeof(vstate->datap) instead of sizeof(void*) (Eike) arch/riscv/include/uapi/asm/ptrace.h | 7 +++ arch/riscv/kernel/ptrace.c | 70 ++++++++++++++++++++++++++++ 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 586786d023c4..e8d127ec5cf7 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -94,6 +94,13 @@ struct __riscv_v_ext_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 23c48b14a0e7..1d572cf3140f 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -7,6 +7,7 @@ * Copied from arch/tile/kernel/ptrace.c */ +#include #include #include #include @@ -24,6 +25,9 @@ enum riscv_regset { #ifdef CONFIG_FPU REGSET_F, #endif +#ifdef CONFIG_RISCV_ISA_V + REGSET_V, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -80,6 +84,61 @@ static int riscv_fpr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_ISA_V +static int riscv_vr_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct __riscv_v_ext_state *vstate = &target->thread.vstate; + + if (!riscv_v_vstate_query(task_pt_regs(target))) + return -EINVAL; + + /* + * Ensure the vector registers have been saved to the memory before + * copying them to membuf. + */ + if (target == current) + riscv_v_vstate_save(current, task_pt_regs(current)); + + /* Copy vector header from vstate. */ + membuf_write(&to, vstate, offsetof(struct __riscv_v_ext_state, datap)); + membuf_zero(&to, sizeof(vstate->datap)); + + /* Copy all the vector registers from vstate. */ + return membuf_write(&to, vstate->datap, riscv_v_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_ext_state *vstate = &target->thread.vstate; + + if (!riscv_v_vstate_query(task_pt_regs(target))) + return -EINVAL; + + /* Copy rest of the vstate except datap */ + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate, 0, + offsetof(struct __riscv_v_ext_state, datap)); + if (unlikely(ret)) + return ret; + + /* Skip copy datap. */ + size = sizeof(vstate->datap); + count -= size; + ubuf += size; + + /* Copy all the vector registers. */ + pos = 0; + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap, + 0, riscv_v_vsize); + return ret; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -99,6 +158,17 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_fpr_set, }, #endif +#ifdef CONFIG_RISCV_ISA_V + [REGSET_V] = { + .core_note_type = NT_RISCV_VECTOR, + .align = 16, + .n = ((32 * RISCV_MAX_VLENB) + + sizeof(struct __riscv_v_ext_state)) / 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 ac3da855fb19..7d8d9ae36615 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -440,6 +440,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 */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Fri Apr 14 15:58:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211850 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 78B1EC77B71 for ; Fri, 14 Apr 2023 17:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=P73mZPWEJkxJFZdJndOPjq6pZUcDlQJcmuHp15MHroQ=; b=TbPuMWlBSeU9Y7 u9WJCioLAYELSDwyqSh9qAyblCPewDfykbWBDB+ZJ2cyNb89mZsf7PzzhAYEpvWvTUJ1iLSuLsgMb wH0QrOkp6v3xPqFDFppOYRvk2szfGeY7gBRSJz69LIy72zEV3swImjeT8lkHJ1jM5dUf/NRt1tyba 5Oh0gOG9kIvmVmJPUQC7y0E9KQWOR6MJehnsvqUHvvEx4NXo1bHisGPoebUkqwjDyNFIeCntEPj3N WFsmpt+2yVNpMRZDcwpaga2wsDy9WnaSRRQxJqMhhbkb2LJTY7e65a/AyCEnRh91TPDzKp0Tsnd58 WZeTBTfSqPLoNZRpQBSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMnQ-00ABLm-2t; Fri, 14 Apr 2023 17:01:12 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqM-00A1zC-1a for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:11 +0000 Received: by mail-pl1-x635.google.com with SMTP id l21so776505pla.5 for ; Fri, 14 Apr 2023 09:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488009; x=1684080009; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=SfhrBat26HES/W09UQouwTRtDc+oS9f+/Y0OrbpnIB4=; b=Cpc1OLK80fp2Vk+OfTwfWFJ4rIIchbHxVG6VTTA1i4wM2eSwwih69m47Wd70PAWx/U a8zAvqbq2RX1aJfOnf5djGXLd3AzOQLrZT3zb9Lf/3ANEt3kAKEdE9NeDTWBB6nFgafQ c9a2/kJgqJQzdUD/M6JxhRaQcpg5mu6CPLus3DLvoxci6hkBpUDkISuBc/+wc2DStVS3 /hqmC3Bn3fx52+IRJVCY5bMSBElmXP5n15OQbAxKuYtunyLzSi2ALgaXk3KN8h/q1kze 1O/nRtElqeoIqFPrc/slRxWD7zq9HK2wD7avGatrm40cxojJ+C7dlkDZ7xNjj6HdESMu tVWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488009; x=1684080009; 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=SfhrBat26HES/W09UQouwTRtDc+oS9f+/Y0OrbpnIB4=; b=JdyNpiHq1M/fAveYCp8dFAeoQZRNpScM6v3jr0OhvzFnkbGuLuKNxoi1IDUluA4GWc ww9ilpdgWBO7sgQGp6o3xBCYiUsIq1xxcsYVFXIMpP5pxai+Ba4maAwedSeO0/qDJtML mTKUOHCvmolNpdofDxtMjNgZyWRNbhVLkyB0mbwl2Ctu2gI0I886peru630PzXHFlwP+ 6UywPuUKBWzTo+m2N5GgaaoocwsvtOxzVzx0NtxhAZB1mS62h6tY+lpNgTb1PfXN8z8z K5d0qX4IE/I66+lq0Dfgytp6XeZWgl7nxQYgbWu105HHPE8v+odLZC61heEvE78hHcPs q2kQ== X-Gm-Message-State: AAQBX9cZSCOgn23q8idYTtBgen6b9bHvj+qKAySSQCtMkQLftnzGYFZ+ Bl6YY1H61XTixeuTargI84DrjUwD0XnGZ0MWahmdtVJ4DlC+BAsnBs0BySp8ycUBrNf7+SutDqJ b9g9QMgG0UQfOAMLzE4cpx9B8TRMOWcHM3fxmC73U3kI49oOdcXvgbsoWxRy+0ZLw07SWGHTiYV Nio8w8JKD+eFo0 X-Google-Smtp-Source: AKy350Yq5buuwKWHBAdXVXOcSMxtp/KzN9zluNkwjw+6poJKRVXRiimJGmkkVRZRiR95aWSKrJKWow== X-Received: by 2002:a17:90a:7b8c:b0:23a:66:1d3a with SMTP id z12-20020a17090a7b8c00b0023a00661d3amr6095897pjc.45.1681488009342; Fri, 14 Apr 2023 09:00:09 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:08 -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 , Heiko Stuebner , Guo Ren , Conor Dooley , Andrew Bresticker , Vincent Chen Subject: [PATCH -next v18 12/20] riscv: signal: check fp-reserved words unconditionally Date: Fri, 14 Apr 2023 15:58:35 +0000 Message-Id: <20230414155843.12963-13-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090010_525450_CA17D313 X-CRM114-Status: GOOD ( 17.16 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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 Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner --- 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 2e365084417e..4d2f41078f46 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -39,26 +39,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, @@ -66,20 +53,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 @@ -91,11 +67,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; } @@ -146,11 +141,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; } From patchwork Fri Apr 14 15:58:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211777 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 31D84C77B6E for ; Fri, 14 Apr 2023 16:00:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=86FLENfWeyJPrp3kkcepUazJ3qZKWtCn3qbm9L76eu8=; b=ElJt/kkQ4jWTvj q0f0imEc6oNy8McfdRqEL4XLv8rqGIF4qsn0aG2NYC03hrb5/Gd+dr6nic47UIR/pee/MtY83hWWo cFHFgUywjKrtsOOndPj5Mk65GxU/3eKJZ1NSzZDs1d7TgN/3hcAY5oH5aptSxdix0jbUWM56XvYiW mU3QOcFoHuarLR/zH0+3TJIuocXnTbf7iZvv/8NZd4DyYCshmq0MdQ33E+4n/4tK6Vsv9fBabDUwC gwJHr4Wa4fwa3s1s6ZUlozK3MzFVAeRk0oFO6koEDH36q3pwrw+bmOCZc38JHAbQHJT49HTtEkqNb CzgHMPz2bkZCGB2VJgmQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqb-00A25v-2n; Fri, 14 Apr 2023 16:00:25 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqY-00A22j-1d for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:24 +0000 Received: by mail-pj1-x102f.google.com with SMTP id l9-20020a17090a3f0900b0023d32684e7fso8809781pjc.1 for ; Fri, 14 Apr 2023 09:00:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488017; x=1684080017; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=XuQhFMdbEw+nE3leuYHXuNAFKhD6k2VUJmGCQCGQmR4=; b=Tj/gtRricGTjGUQHQDmfO7RWGoXiHNZXQBxvg1WePcES1CZtaiBAJJSUOkl0OkcGxJ CkFJ7D/gZLBQkA0LqCedQurMJkTsyBL8gCpo2rztc4nJOaZ3wf1F/b/vmDd7rzNe7NCr mY38axFCQBe94NCwCxbM7IFfHj+GOBp6VX726fD4dwgachkmkkfxkSXoZjt9KIMoSJYE cCpQ90Zt4/RxTdoyML1qbcQdrwwBW/cj2S8HKsrGfk9rH33fTBdc+XqL9TKixOndFb0E 1H9Zs24IY6bfjuSS4tf08Wzrb/6z833U53//Tj+SJZMFCe+bX4BPRUewV49PZwctcDD8 GwPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488017; x=1684080017; 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=XuQhFMdbEw+nE3leuYHXuNAFKhD6k2VUJmGCQCGQmR4=; b=WqetY4cQQbE8Ydk3CHlJm9IWZao1pu1N2pJwCl2KlJAy1GCMXV+JFKIleTYfazwK2G hlRol5xU5S6cYf88pr8mHeFE7qEqrQS1tdx8ulHFy3MjFNkV4vpJDYGFGz8Adv9c9frj sZSH+BQDn1N4IlxinrmdJs2odsLJkZWXy5DwCx8l9aobTAFnEfDxvWTviBvVdQ93gS2L X3fyNbILRNThlk5XtFQC+RzVSrYtVymYKC0r3OG0mO8+lIDiv+IOtVTj5F4mPn0EiUPF eFLYnzRn2OypWyyHPMRJpTF5qBbUqWPkNa/+YHgT4fOyIya6JaJUc6NUHwANOU16XlGT InzQ== X-Gm-Message-State: AAQBX9fFURRvnot9tK5ZYCDnRLeB9YteBijPxFAOWvz7m2jZ0aXtrkb8 lD4zC9MD1Nd4hKx5E53eyE+zebH8kycobjb+upm2pV4FWJ3+p/4rXR3Q70T3q98tfqNXIwqmVA3 uNrHD9wEWhcCliwU3pTek9jVR1i1t5Kgtd5h/Ch+7GVsSEb+BsBrf1q04WLMqTuengwcIenRPHu gp88rwlVDbQliT X-Google-Smtp-Source: AKy350YzAYFjdqcT4DvjVf4FFnoZ8IPzKDa8Z6No6egMTXy7UsMpiDftnUWaEqRYHgdde9b4dbzogA== X-Received: by 2002:a17:90b:4b48:b0:237:40a5:7acf with SMTP id mi8-20020a17090b4b4800b0023740a57acfmr5926908pjb.33.1681488016912; Fri, 14 Apr 2023 09:00:16 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:16 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Conor Dooley , Richard Henderson , Guo Ren , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Jisheng Zhang , Wenting Zhang , Xianting Tian , Andrew Jones , Al Viro , Andrew Bresticker Subject: [PATCH -next v18 13/20] riscv: signal: Add sigcontext save/restore for vector Date: Fri, 14 Apr 2023 15:58:36 +0000 Message-Id: <20230414155843.12963-14-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090022_548510_9F3E46D9 X-CRM114-Status: GOOD ( 31.56 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch facilitates the existing fp-reserved words for placement of the first extension's context header on the user's sigframe. A context header consists of a distinct magic word and the size, including the header itself, of an extension on the stack. Then, the frame is followed by the context of that extension, and then a header + context body for another extension if exists. If there is no more extension to come, then the frame must be ended with a null context header. A special case is rv64gc, where the kernel support no extensions requiring to expose additional regfile to the user. In such case the kernel would place the null context header right after the first reserved word of __riscv_q_ext_state when saving sigframe. And the kernel would check if all reserved words are zeros when a signal handler returns. __riscv_q_ext_state---->| |<-__riscv_extra_ext_header ~ ~ .reserved[0]--->|0 |<- .reserved <-------|magic |<- .hdr | |size |_______ end of sc_fpregs | |ext-bdy| | ~ ~ +)size ------->|magic |<- another context header |size | |ext-bdy| ~ ~ |magic:0|<- null context header |size:0 | The vector registers will be saved in datap pointer. The datap pointer will be allocated dynamically when the task needs in kernel space. On the other hand, datap pointer on the sigframe will be set right after the __riscv_v_ext_state data structure. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Suggested-by: Vineet Gupta Suggested-by: Richard Henderson Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu Acked-by: Conor Dooley Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/uapi/asm/ptrace.h | 15 ++ arch/riscv/include/uapi/asm/sigcontext.h | 16 ++- arch/riscv/kernel/setup.c | 3 + arch/riscv/kernel/signal.c | 174 +++++++++++++++++++++-- 4 files changed, 193 insertions(+), 15 deletions(-) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index e8d127ec5cf7..e17c550986a6 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -71,6 +71,21 @@ struct __riscv_q_ext_state { __u32 reserved[3]; }; +struct __riscv_ctx_hdr { + __u32 magic; + __u32 size; +}; + +struct __riscv_extra_ext_header { + __u32 __padding[129] __attribute__((aligned(16))); + /* + * Reserved for expansion of sigcontext structure. Currently zeroed + * upon signal, and must be zero upon sigreturn. + */ + __u32 reserved; + struct __riscv_ctx_hdr hdr; +}; + union __riscv_fp_state { struct __riscv_f_ext_state f; struct __riscv_d_ext_state d; diff --git a/arch/riscv/include/uapi/asm/sigcontext.h b/arch/riscv/include/uapi/asm/sigcontext.h index 84f2dfcfdbce..8b8a8541673a 100644 --- a/arch/riscv/include/uapi/asm/sigcontext.h +++ b/arch/riscv/include/uapi/asm/sigcontext.h @@ -8,6 +8,17 @@ #include +/* The Magic number for signal context frame header. */ +#define RISCV_V_MAGIC 0x53465457 +#define END_MAGIC 0x0 + +/* The size of END signal context header. */ +#define END_HDR_SIZE 0x0 + +struct __sc_riscv_v_state { + struct __riscv_v_ext_state v_state; +} __attribute__((aligned(16))); + /* * Signal context structure * @@ -16,7 +27,10 @@ */ struct sigcontext { struct user_regs_struct sc_regs; - union __riscv_fp_state sc_fpregs; + union { + union __riscv_fp_state sc_fpregs; + struct __riscv_extra_ext_header sc_extdesc; + }; }; #endif /* _UAPI_ASM_RISCV_SIGCONTEXT_H */ diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index 5d3184cbf518..bb53b2e22386 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -262,6 +262,8 @@ static void __init parse_dtb(void) #endif } +extern void __init init_rt_signal_env(void); + void __init setup_arch(char **cmdline_p) { parse_dtb(); @@ -299,6 +301,7 @@ void __init setup_arch(char **cmdline_p) riscv_init_cbo_blocksizes(); riscv_fill_hwcap(); + init_rt_signal_env(); apply_boot_alternatives(); if (IS_ENABLED(CONFIG_RISCV_ISA_ZICBOM) && riscv_isa_extension_available(NULL, ZICBOM)) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 4d2f41078f46..5222ed4eb677 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -19,9 +19,11 @@ #include #include #include +#include #include extern u32 __user_rt_sigreturn[2]; +static size_t riscv_v_sc_size __ro_after_init; #define DEBUG_SIG 0 @@ -63,12 +65,87 @@ static long save_fp_state(struct pt_regs *regs, #define restore_fp_state(task, regs) (0) #endif +#ifdef CONFIG_RISCV_ISA_V + +static long save_v_state(struct pt_regs *regs, void __user **sc_vec) +{ + struct __riscv_ctx_hdr __user *hdr; + struct __sc_riscv_v_state __user *state; + void __user *datap; + long err; + + hdr = *sc_vec; + /* Place state to the user's signal context space after the hdr */ + state = (struct __sc_riscv_v_state __user *)(hdr + 1); + /* Point datap right after the end of __sc_riscv_v_state */ + datap = state + 1; + + /* datap is designed to be 16 byte aligned for better performance */ + WARN_ON(unlikely(!IS_ALIGNED((unsigned long)datap, 16))); + + riscv_v_vstate_save(current, regs); + /* Copy everything of vstate but datap. */ + err = __copy_to_user(&state->v_state, ¤t->thread.vstate, + offsetof(struct __riscv_v_ext_state, datap)); + /* Copy the pointer datap itself. */ + err |= __put_user(datap, &state->v_state.datap); + /* Copy the whole vector content to user space datap. */ + err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize); + /* Copy magic to the user space after saving all vector conetext */ + err |= __put_user(RISCV_V_MAGIC, &hdr->magic); + err |= __put_user(riscv_v_sc_size, &hdr->size); + if (unlikely(err)) + return err; + + /* Only progress the sv_vec if everything has done successfully */ + *sc_vec += riscv_v_sc_size; + return 0; +} + +/* + * Restore Vector extension context from the user's signal frame. This function + * assumes a valid extension header. So magic and size checking must be done by + * the caller. + */ +static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) +{ + long err; + struct __sc_riscv_v_state __user *state = sc_vec; + void __user *datap; + + /* Copy everything of __sc_riscv_v_state except datap. */ + err = __copy_from_user(¤t->thread.vstate, &state->v_state, + offsetof(struct __riscv_v_ext_state, datap)); + if (unlikely(err)) + return err; + + /* Copy the pointer datap itself. */ + err = __get_user(datap, &state->v_state.datap); + if (unlikely(err)) + return err; + /* + * Copy the whole vector content from user space datap. Use + * copy_from_user to prevent information leak. + */ + err = copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); + if (unlikely(err)) + return err; + + riscv_v_vstate_restore(current, regs); + + 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) { + void __user *sc_ext_ptr = &sc->sc_extdesc.hdr; + __u32 rsvd; 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)) @@ -81,32 +158,81 @@ static long restore_sigcontext(struct pt_regs *regs, 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; + /* Check the reserved word before extensions parsing */ + err = __get_user(rsvd, &sc->sc_extdesc.reserved); + if (unlikely(err)) + return err; + if (unlikely(rsvd)) + return -EINVAL; + + while (!err) { + __u32 magic, size; + struct __riscv_ctx_hdr __user *head = sc_ext_ptr; - err = __get_user(value, &sc->sc_fpregs.q.reserved[i]); + err |= __get_user(magic, &head->magic); + err |= __get_user(size, &head->size); if (unlikely(err)) + return err; + + sc_ext_ptr += sizeof(*head); + switch (magic) { + case END_MAGIC: + if (size != END_HDR_SIZE) + return -EINVAL; + + return 0; + case RISCV_V_MAGIC: + if (!has_vector() || !riscv_v_vstate_query(regs) || + size != riscv_v_sc_size) + return -EINVAL; + + err = __restore_v_state(regs, sc_ext_ptr); break; - if (value != 0) + default: return -EINVAL; + } + sc_ext_ptr = (void __user *)head + size; } return err; } +static size_t get_rt_frame_size(void) +{ + struct rt_sigframe __user *frame; + size_t frame_size; + size_t total_context_size = 0; + + frame_size = sizeof(*frame); + + if (has_vector() && riscv_v_vstate_query(task_pt_regs(current))) + total_context_size += riscv_v_sc_size; + /* + * Preserved a __riscv_ctx_hdr for END signal context header if an + * extension uses __riscv_extra_ext_header + */ + if (total_context_size) + total_context_size += sizeof(struct __riscv_ctx_hdr); + + frame_size += total_context_size; + + frame_size = round_up(frame_size, 16); + return frame_size; +} + SYSCALL_DEFINE0(rt_sigreturn) { struct pt_regs *regs = current_pt_regs(); struct rt_sigframe __user *frame; struct task_struct *task; sigset_t set; + size_t frame_size = get_rt_frame_size(); /* 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 (!access_ok(frame, frame_size)) goto badframe; if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) @@ -140,17 +266,22 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs) { struct sigcontext __user *sc = &frame->uc.uc_mcontext; + struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; 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]); + /* Save the vector state. */ + if (has_vector() && riscv_v_vstate_query(regs)) + err |= save_v_state(regs, (void __user **)&sc_ext_ptr); + /* Write zero to fp-reserved space and check it on restore_sigcontext */ + err |= __put_user(0, &sc->sc_extdesc.reserved); + /* And put END __riscv_ctx_hdr at the end. */ + err |= __put_user(END_MAGIC, &sc_ext_ptr->magic); + err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size); return err; } @@ -175,6 +306,13 @@ static inline void __user *get_sigframe(struct ksignal *ksig, /* Align the stack frame. */ sp &= ~0xfUL; + /* + * Fail if the size of the altstack is not large enough for the + * sigframe construction. + */ + if (current->sas_ss_size && sp < current->sas_ss_sp) + return (void __user __force *)-1UL; + return (void __user *)sp; } @@ -183,9 +321,10 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, { struct rt_sigframe __user *frame; long err = 0; + size_t frame_size = get_rt_frame_size(); - frame = get_sigframe(ksig, regs, sizeof(*frame)); - if (!access_ok(frame, sizeof(*frame))) + frame = get_sigframe(ksig, regs, frame_size); + if (!access_ok(frame, frame_size)) return -EFAULT; err |= copy_siginfo_to_user(&frame->info, &ksig->info); @@ -313,3 +452,10 @@ void arch_do_signal_or_restart(struct pt_regs *regs) */ restore_saved_sigmask(); } + +void init_rt_signal_env(void); +void __init init_rt_signal_env(void) +{ + riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) + + sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; +} From patchwork Fri Apr 14 15:58:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211852 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 260D8C77B73 for ; Fri, 14 Apr 2023 17:01:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4DYU5nOPEDuvRJZVc6iRoG2e3q0i6oH0/g7DVqkOBnU=; b=Y+5x8dvpBAQFhY OShC9QKnwkwrnTXAiWgdUqOr9PNBzaYA6W2xqgeIzt3euhgkopNH/Bx5Y5HaX4P99XKJ6VJkWJEtw kYYEapwKvN+NcXsq6TqNfRd+0278FZGnnLxK6IIy5c6aWyb/j0Ryx4zz33BaBVI7DreXv5Dd2WIHt 0lc53/qTiqrmG2K+scbzwmFu7cs4eia4zFA/8TVzLN1PII3dRN3S8mNk9Rxy7bpQ7F3FN5SuRs8QQ NjsCtASJIXz+aOCY6rX4w7e64SM+h9CLHG/JFKyvTxk2G7dIs4iY6Vwo+Kppj+uzpI5bX2F0AkvJN KbGfzOS/lx/DiD37tA8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMnR-00ABM6-20; Fri, 14 Apr 2023 17:01:13 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqe-00A25m-05 for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:29 +0000 Received: by mail-pl1-x632.google.com with SMTP id lh8so5741914plb.1 for ; Fri, 14 Apr 2023 09:00:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488024; x=1684080024; 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=AhArLtXmRDwT+xcM9/tIlz+8j5s5RHf4cEPqi0dLUqk=; b=L2OKir3MBV5zkjlZ+m6wtL48Eht6E7K7eezwnTTuw/Lf1+ogugEBXFYd4rSXylnUMB xrxixD2s96bwBPeSkxlJ4VdG8yP4flDIrw7ZQwAwIq1CtqNxJZQCBhJJucGTSo6I9FLm 7mwcdLS2CLx37BTlLaPFYBlHHmgttE+XeBdTSNv3UUoc+bWZ9MMIy7ZYOyvWcA+K2kQb TF3TqH85KUCfnfs5xlRdl1Sjnk1bapBZGFExVr8Gn8kdROxiGQ6nIQ2QUYMbAHrq+ejn kxN5iYJJgnhRfIdjvGUodjpbwdIwnq/XRLxk3vNt91FwKiRJZNtT+6/xjBjWhEnfzGzj CbJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488024; x=1684080024; 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=AhArLtXmRDwT+xcM9/tIlz+8j5s5RHf4cEPqi0dLUqk=; b=TF4/jOubj2kUGhgZ6d9MKGiVWyHhFR+8diaI0jEJlyEBXyX2554t1ETOUDEE5X+Qe9 VCFfN9Iznq2eo25kkYeg0XhzTVe8BCBniRWWMMWx+kXyow64k5O1QPB2kDuJSjJUeNiA TCCBtUzZ4XeZnc2RW8Tz51LR3C66XoN/JFc/udG77dDmUyWCpMX6vi7amLWrWkhf0Krg 97lGAlnIrhk6wFPjs0JykpO/ykaACTRRNOus65A43DOmEaUghQHjvmOlNj6zcsJpJXb2 73fMffFm/F4qGiCUi8vrU5fBAtp4tUG5DCp3EN6FD0CUfXvaIEs0NoyeMEae2/zBv+m9 9jEg== X-Gm-Message-State: AAQBX9cZKoqFCp+x007fOzmXWCr+94I6784wlOb/+Ud2Um3TnzuoTlrU FljHsXFyBdIfcibj1Tepl724MztIl3yTJNUex4zFLkUixwXOL33kbYEskdK0mFSSmQ+/VlogwDL NCOZqMt0p9uN0mAA6Zgv7Z1y/N+94iaYW9pWXOP/ZJmf3MhJc6PvrAZWQNTbHLb0RURF4fQGkwK cM81dbC43hTH3B X-Google-Smtp-Source: AKy350aq13V1WMdOrFwInS9FRcRdyTazI0yTMF/74AshJFtvQPFRPWdwtLeeqvEK4q83qc0Ypr41Zg== X-Received: by 2002:a17:90b:4b05:b0:23d:1f95:de1c with SMTP id lx5-20020a17090b4b0500b0023d1f95de1cmr6257199pjb.28.1681488024338; Fri, 14 Apr 2023 09:00:24 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:23 -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 Subject: [PATCH -next v18 14/20] riscv: signal: Report signal frame size to userspace via auxv Date: Fri, 14 Apr 2023 15:58:37 +0000 Message-Id: <20230414155843.12963-15-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090028_071869_1F7A49C0 X-CRM114-Status: GOOD ( 22.53 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kefeng Wang , guoren@linux.alibaba.com, Kees Cook , Andrew Bresticker , vineetg@rivosinc.com, =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Eric Biederman , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, Zong Li , Heiko Stuebner Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Vincent Chen The vector register belongs to the signal context. They need to be stored and restored as entering and leaving the signal handler. According to the V-extension specification, the maximum length of the vector registers can be 2^16. Hence, if userspace refers to the MINSIGSTKSZ to create a sigframe, it may not be enough. To resolve this problem, this patch refers to the commit 94b07c1f8c39c ("arm64: signal: Report signal frame size to userspace via auxv") to enable userspace to know the minimum required sigframe size through the auxiliary vector and use it to allocate enough memory for signal context. Note that auxv always reports size of the sigframe as if V exists for all starting processes, whenever the kernel has CONFIG_RISCV_ISA_V. The reason is that users usually reference this value to allocate an alternative signal stack, and the user may use V anytime. So the user must reserve a space for V-context in sigframe in case that the signal handler invokes after the kernel allocating V. Signed-off-by: Greentime Hu Signed-off-by: Vincent Chen Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Björn Töpel Reviewed-by: Guo Ren Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/elf.h | 9 +++++++++ arch/riscv/include/asm/processor.h | 2 ++ arch/riscv/include/uapi/asm/auxvec.h | 1 + arch/riscv/kernel/signal.c | 20 +++++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index 30e7d2455960..ca23c4f6c440 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -105,6 +105,15 @@ do { \ get_cache_size(3, CACHE_TYPE_UNIFIED)); \ NEW_AUX_ENT(AT_L3_CACHEGEOMETRY, \ get_cache_geometry(3, CACHE_TYPE_UNIFIED)); \ + /* \ + * Should always be nonzero unless there's a kernel bug. \ + * If we haven't determined a sensible value to give to \ + * userspace, omit the entry: \ + */ \ + if (likely(signal_minsigstksz)) \ + NEW_AUX_ENT(AT_MINSIGSTKSZ, signal_minsigstksz); \ + else \ + NEW_AUX_ENT(AT_IGNORE, 0); \ } while (0) #define ARCH_HAS_SETUP_ADDITIONAL_PAGES struct linux_binprm; diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index f0ddf691ac5e..38ded8c5f207 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -7,6 +7,7 @@ #define _ASM_RISCV_PROCESSOR_H #include +#include #include @@ -81,6 +82,7 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid); extern void riscv_fill_hwcap(void); extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); +extern unsigned long signal_minsigstksz __ro_after_init; #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/include/uapi/asm/auxvec.h b/arch/riscv/include/uapi/asm/auxvec.h index fb187a33ce58..10aaa83db89e 100644 --- a/arch/riscv/include/uapi/asm/auxvec.h +++ b/arch/riscv/include/uapi/asm/auxvec.h @@ -35,5 +35,6 @@ /* entries in ARCH_DLINFO */ #define AT_VECTOR_SIZE_ARCH 9 +#define AT_MINSIGSTKSZ 51 #endif /* _UAPI_ASM_RISCV_AUXVEC_H */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 5222ed4eb677..40f740183768 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -22,6 +22,8 @@ #include #include +unsigned long signal_minsigstksz __ro_after_init; + extern u32 __user_rt_sigreturn[2]; static size_t riscv_v_sc_size __ro_after_init; @@ -196,7 +198,7 @@ static long restore_sigcontext(struct pt_regs *regs, return err; } -static size_t get_rt_frame_size(void) +static size_t get_rt_frame_size(bool cal_all) { struct rt_sigframe __user *frame; size_t frame_size; @@ -204,8 +206,10 @@ static size_t get_rt_frame_size(void) frame_size = sizeof(*frame); - if (has_vector() && riscv_v_vstate_query(task_pt_regs(current))) - total_context_size += riscv_v_sc_size; + if (has_vector()) { + if (cal_all || riscv_v_vstate_query(task_pt_regs(current))) + total_context_size += riscv_v_sc_size; + } /* * Preserved a __riscv_ctx_hdr for END signal context header if an * extension uses __riscv_extra_ext_header @@ -225,7 +229,7 @@ SYSCALL_DEFINE0(rt_sigreturn) struct rt_sigframe __user *frame; struct task_struct *task; sigset_t set; - size_t frame_size = get_rt_frame_size(); + size_t frame_size = get_rt_frame_size(false); /* Always make any pending restarted system calls return -EINTR */ current->restart_block.fn = do_no_restart_syscall; @@ -321,7 +325,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, { struct rt_sigframe __user *frame; long err = 0; - size_t frame_size = get_rt_frame_size(); + size_t frame_size = get_rt_frame_size(false); frame = get_sigframe(ksig, regs, frame_size); if (!access_ok(frame, frame_size)) @@ -458,4 +462,10 @@ void __init init_rt_signal_env(void) { riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) + sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; + /* + * Determine the stack space required for guaranteed signal delivery. + * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry + * in the auxiliary array at process startup. + */ + signal_minsigstksz = get_rt_frame_size(true); } From patchwork Fri Apr 14 15:58:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211778 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 5B07DC77B71 for ; Fri, 14 Apr 2023 16:00:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dRGBZJEnYY+PmGRyeD3enEfmyswaWs2Az+tWXjwHqqM=; b=S9YQh9gLnNHVoH 8GLS1GVR1GjJHY6r3Wk/ocX7+IrUe9pIWTK0h1wpnZdZb5VzHNdadAPhB5egmq6/qeoT0tGYZiWGl S2J40j90ZRzan2SlJySn3pc7xHv7/a7PtKutq8NxHMW3YhH9to13KLdBD8W0aEx5dAowyfQaR4LBa M4bM/HIVwco2gk7KGW+hN5J9eyq0UR9m3Y6T2bOBKsG0bUy4cbl/ktheWmpRJ09cGWa2BwddeKnHO vsPJOzmNsMIK3B5Eq7gjQfgsXUbdaQ7+Uk8WOTHYNzX98mq+F98r79ZHHullMEAkZHoygQstlxvUO r/BMLGIHxyfNXLOjsO5w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqj-00A2AK-0j; Fri, 14 Apr 2023 16:00:33 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqg-00A22U-1D for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:31 +0000 Received: by mail-pj1-x1033.google.com with SMTP id k65-20020a17090a3ec700b00247131783f7so4437912pjc.0 for ; Fri, 14 Apr 2023 09:00:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488029; x=1684080029; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=KUlZU4jbXrvVX0wjIPVLaSJmpFs1THD4w3JnPj0HJFk=; b=Mlgms75yqWeLTkYqHiJjvSel58ySvtkQoXkycao5QCuLMS7lVEiaDjiq/jUSmK5Um5 3DrLR+J8vdOtRDmHsrdXtcyucKdJSduoDf2lxFq8pS6ceLqQ4xqpVXoauqY/x6ktIxiX HSFUtLZc8iEI5ObhFHkkvcMwLCr4hletTwaolJ0SuFZWiSq9cs7t3oc9/S3640t77Vgw yq8um2CLbE1exAnuCTrMjfNE12JtDNuT7kfvM6+CVPA1xovkv27HfXBEyiWPNuKLrMiP maFKu5j02IU4mL8vbTb6Du5BAIyyax+Ita/6KJmnARGhLQn31qWxVWKB3B0HCzijeESv mahQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488029; x=1684080029; 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=KUlZU4jbXrvVX0wjIPVLaSJmpFs1THD4w3JnPj0HJFk=; b=C2fC7xvOcG/s1uiyvaimsOWuYPFZYnXC5E5vKzneIylH+jAhek91aqMZOITmjUgiua tkb+tfqB2b32S0qMulteHBgYFHV1pK1hhdtP1QOcTNvDuvqse01Rdk98lmAIhUjQ98Ih VeykTxtzcBOqgMPt8VheaF3hiKogJyor9Xh0US1cCBovAhEBH0X5VxJfFZge2DT5Rnqi mxIJOGBeabHZliU+dWDXdloKjJ5pRhAB3m5EXwFU3bOXP/cKzlDUN4xYX6BdVMmWoDN4 XlNjucLvw/HpGPo9QEQJPZtwhHWLQ4KEdbONTSUzqyDtOyVgYpxi4j4FH1UQf0nSoj6+ VmoA== X-Gm-Message-State: AAQBX9drTX7jCBR1OOUIzxiYx8/azlArGK7tZDx8VUoc3Mnwcx7C4ujA Ks+0o54p1W8PPUuwmOIFDgh9QN6yjc5tkKHA0FEAk+dZ7+h9SW6xuh+fvQscbtFQSXhUVvAt7A3 WvJsESi74Pitmq5lSePDCOS7QyZ64OwEMwV38foK8vBQUYN+OOiXEpI28UOhy2StY1Hv/KkusWk uwFpxJ+9RhOMQG X-Google-Smtp-Source: AKy350b2yRGqmXSmp577Bu3JFR1YRggH+c5zBk/D9yeWIB98+DOaaoUYfX8QFTu7txBaCsMvgcnr6Q== X-Received: by 2002:a17:90a:4ec2:b0:23c:fef0:d441 with SMTP id v2-20020a17090a4ec200b0023cfef0d441mr5835665pjl.33.1681488028800; Fri, 14 Apr 2023 09:00:28 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:28 -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 , Heiko Stuebner , Conor Dooley , Guo Ren , Andrew Bresticker , Vincent Chen Subject: [PATCH -next v18 15/20] riscv: signal: validate altstack to reflect Vector Date: Fri, 14 Apr 2023 15:58:38 +0000 Message-Id: <20230414155843.12963-16-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090030_418707_4280D9A9 X-CRM114-Status: GOOD ( 10.20 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some extensions, such as Vector, dynamically change footprint on a signal frame, so MINSIGSTKSZ is no longer accurate. For example, an RV64V implementation with vlen = 512 may occupy 2K + 40 + 12 Bytes of a signal frame with the upcoming support. And processes that do not execute any vector instructions do not need to reserve the extra sigframe. So we need a way to guard the allocation size of the sigframe at process runtime according to current status of V. Thus, provide the function sigaltstack_size_valid() to validate its size based on current allocation status of supported extensions. Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/signal.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 40f740183768..11411e113f0e 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -469,3 +469,10 @@ void __init init_rt_signal_env(void) */ signal_minsigstksz = get_rt_frame_size(true); } + +#ifdef CONFIG_DYNAMIC_SIGFRAME +bool sigaltstack_size_valid(size_t ss_size) +{ + return ss_size > get_rt_frame_size(false); +} +#endif /* CONFIG_DYNAMIC_SIGFRAME */ From patchwork Fri Apr 14 15:58:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211779 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id EE275C77B6E for ; Fri, 14 Apr 2023 16:00:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9W40tXwBb/IHTOhUfEvR1aMXU7QRaUyhQIV3Viue5JM=; b=iL0owV4dUTbNGz RCPzg2gAFxTUOKndA7V7PJCIW6rY55yl9Vlxmdynb5o9pdcEsqSuk5v5eOxtjLtr84vxRHMqu+ZXV 8/eh3pGxYNSxTrC+oJ3+5b1vyHk7dUQMWGGY95657tagAnXNcCGxnIsyn9ZFaapWvupuaI1b/NUzM i94sO8N1DB3EIv3Pqg8BHkEFnDEqK0nUZsz30aDE7c6qOVrnJgMeanOageA3kYQWJhe6x5oP7E3Fo HZOPHenuj58a4r5aeP6UdD53o3K8qlxKto7Vh9eNveGl7fT5kActmUdxIhIIBtqidJ0TJMxsuBJIX DT7TkgEhExI4ykwmH/TQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqp-00A2EE-17; Fri, 14 Apr 2023 16:00:39 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqm-00A2BH-34 for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:38 +0000 Received: by mail-pl1-x62d.google.com with SMTP id la3so18553661plb.11 for ; Fri, 14 Apr 2023 09:00:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488034; x=1684080034; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=hWB54ZotS7Y2ZgKJV1SIpYTu+cs8XvC1qHz0MxJpXfU=; b=KmNeQFGssjzK0xQ018T4SlGSmqFwJkBPec76ZOBY4rleHRUhzy2MMot645T/KeOxTy oCUzwlMXTlgUhoQ2v/rnI/1nwPbARVDCBxETuqvmwYWIRGAEKZrfCo/iztYx6JyMhV5g O+uRyS2XqxLUA9A2EqVkmDcKftggvVg/dsNE+MsWdowrLBiDFI9/WYMvk5bD4fSOT/xC FXhbKspwDSOieEFexzugK5oDC5yGNjFwNTmfgG/PP2YFGoMJGq3iCMX/8OPkOd/KD9h8 85Ymzshlsn6z0qRvDtlK0U7vbXaDnK3OjPzWfGqXH1wh0EWCZW+8yhpIAOLa670BS84k KT1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488034; x=1684080034; 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=hWB54ZotS7Y2ZgKJV1SIpYTu+cs8XvC1qHz0MxJpXfU=; b=aRvOqvihwNfcM5SgzWa3xeuJN9H6vS5QAcl3a15XvXxQ3xZQOtjwfXMwI0DTcm/P6c /+OtwQ/kLdZ34rFf9vIlKDrzzjLalMk2y7R4b32Y0aJM+jcMz26D6uSLh+Wl5nLSb+B/ 3xeJ61KX6HbJp+4WSYSzSOIdZYuAxtqXApJV8wxSpYofH8wjj74tdkm9dyVCibOhWsbW jMHLcYHjmlb15WHgpPt9LyTJ+MXGF2hHOIWdSY4+1Mg/23jfnLhADGtwPR12Lax6c0kT koPmApdt6Z4g9PC0bHB2v+coVBc8UqUwSVILdhbbcrCoHbZnQB7jupnyrbSc6Hk1MF4W OZew== X-Gm-Message-State: AAQBX9fihNtEdiHHT47zvPX4Foc/om7c4bMjCrDzhoWVNMQINgq/B2a9 kE6NmFn54wO/TMXLNNr4DlwuaK9s3rAmMgs7t3t0SpQJUO6MnzXynnxvU2OZc934tnwJbF/i1ii cz2YWN4EAO+XzrdOzzxWv3Dcj2DNcCVDIVjxZRC3uSZJBB6G4Wr3pVV/B0VKZGVx+2Ef61w1Qga QLDHvKsLYEYhOw X-Google-Smtp-Source: AKy350bLWel/6yEV1JmbLZwHUbnX6eF24BdOzl+/i67z9xt7yAYwzmd+UIUx2N8+rcDKw8RpGj4wSg== X-Received: by 2002:a17:90a:be08:b0:233:f786:35ca with SMTP id a8-20020a17090abe0800b00233f78635camr5818524pjs.35.1681488033705; Fri, 14 Apr 2023 09:00:33 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:32 -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, ShihPo Hung , Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Conor Dooley , Masahiro Yamada , Alexandre Ghiti , Guo Ren Subject: [PATCH -next v18 16/20] riscv: prevent stack corruption by reserving task_pt_regs(p) early Date: Fri, 14 Apr 2023 15:58:39 +0000 Message-Id: <20230414155843.12963-17-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090036_988694_4F4EC1B0 X-CRM114-Status: UNSURE ( 9.29 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Early function calls, such as setup_vm(), relocate_enable_mmu(), soc_early_init() etc, are free to operate on stack. However, PT_SIZE_ON_STACK bytes at the head of the kernel stack are purposedly reserved for the placement of per-task register context pointed by task_pt_regs(p). Those functions may corrupt task_pt_regs if we overlap the $sp with it. In fact, we had accidentally corrupted sstatus.VS in some tests, treating the kernel to save V context before V was actually allocated, resulting in a kernel panic. Thus, we should skip PT_SIZE_ON_STACK for $sp before making C function calls from the top-level assembly. Co-developed-by: ShihPo Hung Signed-off-by: ShihPo Hung Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/head.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index e16bb2185d55..11c3b94c4534 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -301,6 +301,7 @@ clear_bss_done: la tp, init_task la sp, init_thread_union + THREAD_SIZE XIP_FIXUP_OFFSET sp + addi sp, sp, -PT_SIZE_ON_STACK #ifdef CONFIG_BUILTIN_DTB la a0, __dtb_start XIP_FIXUP_OFFSET a0 @@ -318,6 +319,7 @@ clear_bss_done: /* Restore C environment */ la tp, init_task la sp, init_thread_union + THREAD_SIZE + addi sp, sp, -PT_SIZE_ON_STACK #ifdef CONFIG_KASAN call kasan_early_init From patchwork Fri Apr 14 15:58:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211780 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id D9C2EC77B73 for ; Fri, 14 Apr 2023 16:00:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HPvM0mcFH1GAyzL6R3mrnTrArM/xlT+p//HV49+9KGY=; b=ycLRatV1btH0C+ Scfma/q3JmpvZxR8NfHQLxOTzq0GB3PdmZ132dJ+XyUyaxfSVKWE1kBnnDLVFEH+edPzokaveyMX5 VY5DVw/UlCh2NotUSd3bh+rHlbSVETNwskt0ajbik6qFRJkJMkrrv0+Fu7Afcw7q0soQr2f7XdYcn obzc49+UMfSdd3wvoN45arhZAQFj4rJFzO9TaZXGwu4byuMONAdhfZwtgDvh/z6Eg0wzHqGroO38u zWl0meuI35jN8ylOW99z/DWaYxUh40mszp7JyWmmxY8uy/2EyecP6F5Qis5aagD5htyQNPTl+B9hv nQevz7a1Z5c2DyGo2eSQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqq-00A2Ev-0t; Fri, 14 Apr 2023 16:00:40 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqn-00A25F-2d for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:39 +0000 Received: by mail-pj1-x1033.google.com with SMTP id cm18-20020a17090afa1200b0024713adf69dso6565766pjb.3 for ; Fri, 14 Apr 2023 09:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488037; x=1684080037; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=Odmz0JG+bEL0mEy4+sqiyjpf0XMQ6vVYrEOWb5kBhbI=; b=FOctlegUDtDv3ZHjXnrQJCTB/Q6yyurIgcxUtsUoFzS7dn8jftYmLbD+75EhDZA2Cr zRVATn8/u7h4RXGHOq10e69KRyHc/0+D/hBly1p5VO/Y1tdMTqJaNtb7RYGA/nhmWAf3 08pNFxl+FwPz9dxOalOIoXewTsF8wfk9yyojXsNQOUEFPUe2AB/bSHL2WyxhicJcsa5i 0nU6KQQTrq7QW8967WdAfAJ6sVx/21Z1pTNb/OINlieAIVJVwXtmwsASd+1L+42QsLIg fCWGcV29bYX2+zTqwJe446i9FzP9Dm83skLE4j0C2s1H+FWSddrMeia1ROtHi4sZWwCw 3h4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488037; x=1684080037; 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=Odmz0JG+bEL0mEy4+sqiyjpf0XMQ6vVYrEOWb5kBhbI=; b=b4NWqSUeab+vnVmLES2zj/sUXGkVB569z0IqlNFZhKup2macB//gPweM3aFt9WpqwE 0qM1EzBuLcBT6rHeRARw8SE/p/Rms8QP8rxY6ZoFySmNSDy51B6FBGkshcYogD+Wgakc nOULv8rXI2UgJdwmPl/YwX7EeWYmSW28i2zKEbGAmWr1dGrHdRSW0AcTp74NxhAA2b1E 7/RJP68P2gOMepM5Yshh13wXTpF7+QzeGiYyLHe0FV+xB8/XJ1TmckXmktkAPmDIApIF UKNLaJgOVIUONDU1stEOKSTcD6YRJyG4AOCuIUYZ1RO0Z1nBa3mTp+JyvZf8dUSssyDd FmlQ== X-Gm-Message-State: AAQBX9eAx01PRDFSQzMjx881FCMPO542S20Cvm+qQJO6t6m3uTsdCdL4 0skO/p54egysbv9tbHr+3lcZ6rQK06rUM4ayq6KL3XeWkKqGi2RH1tWrz6BB/16UdY0uXVDTVfu l1ApwocZX850PjDYf3CYW/8bgl6HrHZdloizppeHftVnQe8bCQ7CRsps2A8udUdCISHPd/yriJF 0e2XLFC0V2Wohj X-Google-Smtp-Source: AKy350bD7mze/O0+fYsB9lpeXlbduhlDKv44rIlF3vXS+82oFbA62wV5LVEzKRe2kkBUE5wMisk7Nw== X-Received: by 2002:a17:90a:a091:b0:247:4538:a62e with SMTP id r17-20020a17090aa09100b002474538a62emr1878454pjp.27.1681488036960; Fri, 14 Apr 2023 09:00:36 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:36 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou Subject: [PATCH -next v18 17/20] riscv: kvm: Add V extension to KVM ISA Date: Fri, 14 Apr 2023 15:58:40 +0000 Message-Id: <20230414155843.12963-18-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090037_862909_DE685893 X-CRM114-Status: UNSURE ( 8.70 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Vincent Chen Add V extension to KVM isa extension list to enable supporting of V extension on VCPUs. Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Anup Patel Acked-by: Anup Patel Reviewed-by: Heiko Stuebner --- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu.c | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index e44c1e90eaa7..d562dcb929ea 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -107,6 +107,7 @@ enum KVM_RISCV_ISA_EXT_ID { KVM_RISCV_ISA_EXT_ZIHINTPAUSE, KVM_RISCV_ISA_EXT_ZICBOM, KVM_RISCV_ISA_EXT_ZICBOZ, + KVM_RISCV_ISA_EXT_V, KVM_RISCV_ISA_EXT_MAX, }; diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 6adb1b6112a1..bfdd5b73d462 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -57,6 +57,7 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_H] = RISCV_ISA_EXT_h, [KVM_RISCV_ISA_EXT_I] = RISCV_ISA_EXT_i, [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, + [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v, KVM_ISA_EXT_ARR(SSTC), KVM_ISA_EXT_ARR(SVINVAL), From patchwork Fri Apr 14 15:58:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211851 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id A1C48C77B79 for ; Fri, 14 Apr 2023 17:01:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=A21JtvXivSRL3wnkgs2lRNU3nopaUBmV1TAys/xgZ00=; b=3L223rsDBzOTMV wjqZDrXMK46DYNH9wFmMYx/A34TG/gp+HT7llw3Zl4lkFABNkRk6F0ECAPtUguiLXuEqbKLNzQouM Dxhoqg/pslVMQidRP6okqGuWCKcCNzIjNRhVyH45IKf617Bbjjx3q75qJKFwKtH/Zk6iVIq8t405c kFaq8Eu981wkxJ6OgfpGz7pZKiFMLGTTQem1PRNdO0fq2oiqJzfHO4e/hyLgEzDNLNWGYhwgtmDK6 Ts+SqNMoAiledPAJvFeLzu2pJC0ickWdBersKE5gXhUnpJYlnnDgBcQ1Z7QN7AMoMe46GxDCjtrkz 5gpm8DMlQAEmdhPWLAWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMnT-00ABNk-10; Fri, 14 Apr 2023 17:01:15 +0000 Received: from mail-pj1-f45.google.com ([209.85.216.45]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqt-00A2GZ-1b for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:45 +0000 Received: by mail-pj1-f45.google.com with SMTP id my14-20020a17090b4c8e00b0024708e8e2ddso8173678pjb.4 for ; Fri, 14 Apr 2023 09:00:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488042; x=1684080042; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=cyvbTzJbnOzw1ESvGUlxh39vufBJcYg3/HwtPcMVrvQ=; b=HOLigNDR+483vpT+aRQNa/pQF/KPMjWi4Vk+qT/QDEYy7EXySpx/qDHuTkVRk+fR1a ZO+KYLED3uHbhjC6m6Cx901RU51RhWrjUJ6Ed1KtZXqmLiTmq6puneMBMYMMCGASkpED Wb0kC86dxGQFzGpj5LUz260p6+oVNwL+ylRVxDTWYsSQqwafUqN9Q9979griZA4UgRZL zQ7Jb0NXd+rWyN2iFtMNxqIz4dLFVbt3zmGoSQ4GYYKFmbZ10NTYOxn/e89PqGGuaZpm irayoVgGqUHtC79VIOPOu9r6WUR4Exh8ntYp4sFiNbFxzjXER2xfYTXB+ipU2wbvBAMf SLuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488042; x=1684080042; 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=cyvbTzJbnOzw1ESvGUlxh39vufBJcYg3/HwtPcMVrvQ=; b=iz+40t7Ta4JrLlccQ2ItvM9i4rXMhmQb/XMeabApbuPTLBHTb0a6CxRLHC5Kc8StKa 1MfpKlhgVbdfcbY2StRlv6N5Db2A4OKkby/hRzp9Icx36A9rB8xuDwV+/HM4a6v93yI3 MKd/ZOEfce2IIwptVWZ6Gf9fKtgl0QkbJQPAf7AYepJ/DT2tfoQLZNytx9uGE9ZnsaRJ uVpnKPCZubR2a7pU6EtTbwbL3AM/zxTggzV5LvUDhC/MeVXkqzm9BBjTUkUgzaPWS2aQ AuRyd1bomS/QFuW7dtMyANBpc/2//mWW92J5Zx2sg0Se6QdTq06cOa51BWNJkNiN4tuZ jefQ== X-Gm-Message-State: AAQBX9e+FpX4Sox2ND/4wRiOG/22SDba+cV4wwAWttrmACXcFF7qMq4d mqzI7z+DWw+4FiuWCuYbGibjTWmRI8GZ40vpA0zR6GCYHy3wRzxR49WQCd2G4NAJR1pTiV0FI25 ConLAtuVVseKf70/tztPkmUYgz06ZG0hESlPa/UcnXR0zwzQeU549++yZMurdrDRD6uOyw/weMc YkwGPx2zwyYHG2 X-Google-Smtp-Source: AKy350ZTpegkGXTYtxqIHMV4BgnKD5KsPOeKFaqXWBzCCIjEeMx6EK2b9d4fuD9w0OSOV0whT/xmRw== X-Received: by 2002:a17:90a:ec05:b0:23e:f855:79f2 with SMTP id l5-20020a17090aec0500b0023ef85579f2mr6402562pjy.12.1681488040954; Fri, 14 Apr 2023 09:00:40 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:40 -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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou Subject: [PATCH -next v18 18/20] riscv: KVM: Add vector lazy save/restore support Date: Fri, 14 Apr 2023 15:58:41 +0000 Message-Id: <20230414155843.12963-19-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090043_539148_B14BD4D0 X-CRM114-Status: GOOD ( 24.19 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Vincent Chen This patch adds vector context save/restore for guest VCPUs. To reduce the impact on KVM performance, the implementation imitates the FP context switch mechanism to lazily store and restore the vector context only when the kernel enters/exits the in-kernel run loop and not during the KVM world switch. Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Anup Patel Acked-by: Anup Patel --- arch/riscv/include/asm/kvm_host.h | 2 + arch/riscv/include/asm/kvm_vcpu_vector.h | 82 ++++++++++ arch/riscv/include/uapi/asm/kvm.h | 7 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 22 +++ arch/riscv/kvm/vcpu_vector.c | 186 +++++++++++++++++++++++ 6 files changed, 300 insertions(+) create mode 100644 arch/riscv/include/asm/kvm_vcpu_vector.h create mode 100644 arch/riscv/kvm/vcpu_vector.c diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index cc7da66ee0c0..7e7e23272d32 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,7 @@ struct kvm_cpu_context { unsigned long sstatus; unsigned long hstatus; union __riscv_fp_state fp; + struct __riscv_v_ext_state vector; }; struct kvm_vcpu_csr { diff --git a/arch/riscv/include/asm/kvm_vcpu_vector.h b/arch/riscv/include/asm/kvm_vcpu_vector.h new file mode 100644 index 000000000000..ff994fdd6d0d --- /dev/null +++ b/arch/riscv/include/asm/kvm_vcpu_vector.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2022 SiFive + * + * Authors: + * Vincent Chen + * Greentime Hu + */ + +#ifndef __KVM_VCPU_RISCV_VECTOR_H +#define __KVM_VCPU_RISCV_VECTOR_H + +#include + +#ifdef CONFIG_RISCV_ISA_V +#include +#include + +static __always_inline void __kvm_riscv_vector_save(struct kvm_cpu_context *context) +{ + __riscv_v_vstate_save(&context->vector, context->vector.datap); +} + +static __always_inline void __kvm_riscv_vector_restore(struct kvm_cpu_context *context) +{ + __riscv_v_vstate_restore(&context->vector, context->vector.datap); +} + +void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, + unsigned long *isa); +void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, + unsigned long *isa); +void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx); +void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx); +int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, + struct kvm_cpu_context *cntx); +void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu); +#else + +struct kvm_cpu_context; + +static inline void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) +{ +} + +static inline void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, + unsigned long *isa) +{ +} + +static inline void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, + unsigned long *isa) +{ +} + +static inline void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx) +{ +} + +static inline void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx) +{ +} + +static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, + struct kvm_cpu_context *cntx) +{ + return 0; +} + +static inline void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu) +{ +} +#endif + +int kvm_riscv_vcpu_get_reg_vector(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg, + unsigned long rtype); +int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg, + unsigned long rtype); +#endif diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index d562dcb929ea..0955f9460447 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -155,6 +155,13 @@ enum KVM_RISCV_ISA_EXT_ID { /* ISA Extension registers are mapped as type 7 */ #define KVM_REG_RISCV_ISA_EXT (0x07 << KVM_REG_RISCV_TYPE_SHIFT) +/* V extension registers are mapped as type 8 */ +#define KVM_REG_RISCV_VECTOR (0x08 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_VECTOR_CSR_REG(name) \ + (offsetof(struct __riscv_v_ext_state, name) / sizeof(unsigned long)) +#define KVM_REG_RISCV_VECTOR_REG(n) \ + ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) + #endif #endif /* __LINUX_KVM_RISCV_H */ diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 278e97c06e0a..f29854333cf2 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -17,6 +17,7 @@ kvm-y += mmu.o kvm-y += vcpu.o kvm-y += vcpu_exit.o kvm-y += vcpu_fp.o +kvm-y += vcpu_vector.o kvm-y += vcpu_insn.o kvm-y += vcpu_switch.o kvm-y += vcpu_sbi.o diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index bfdd5b73d462..c495ae1a8091 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { KVM_GENERIC_VCPU_STATS(), @@ -135,6 +137,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) kvm_riscv_vcpu_fp_reset(vcpu); + kvm_riscv_vcpu_vector_reset(vcpu); + kvm_riscv_vcpu_timer_reset(vcpu); WRITE_ONCE(vcpu->arch.irqs_pending, 0); @@ -192,6 +196,9 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) cntx->hstatus |= HSTATUS_SPVP; cntx->hstatus |= HSTATUS_SPV; + if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx)) + return -ENOMEM; + /* By default, make CY, TM, and IR counters accessible in VU mode */ reset_csr->scounteren = 0x7; @@ -227,6 +234,9 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) /* Free unused pages pre-allocated for G-stage page table mappings */ kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); + + /* Free vector context space for host and guest kernel */ + kvm_riscv_vcpu_free_vector_context(vcpu); } int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) @@ -610,6 +620,9 @@ static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D); case KVM_REG_RISCV_ISA_EXT: return kvm_riscv_vcpu_set_reg_isa_ext(vcpu, reg); + case KVM_REG_RISCV_VECTOR: + return kvm_riscv_vcpu_set_reg_vector(vcpu, reg, + KVM_REG_RISCV_VECTOR); default: break; } @@ -637,6 +650,9 @@ static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, KVM_REG_RISCV_FP_D); case KVM_REG_RISCV_ISA_EXT: return kvm_riscv_vcpu_get_reg_isa_ext(vcpu, reg); + case KVM_REG_RISCV_VECTOR: + return kvm_riscv_vcpu_get_reg_vector(vcpu, reg, + KVM_REG_RISCV_VECTOR); default: break; } @@ -906,6 +922,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) kvm_riscv_vcpu_host_fp_save(&vcpu->arch.host_context); kvm_riscv_vcpu_guest_fp_restore(&vcpu->arch.guest_context, vcpu->arch.isa); + kvm_riscv_vcpu_host_vector_save(&vcpu->arch.host_context); + kvm_riscv_vcpu_guest_vector_restore(&vcpu->arch.guest_context, + vcpu->arch.isa); vcpu->cpu = cpu; } @@ -921,6 +940,9 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) kvm_riscv_vcpu_host_fp_restore(&vcpu->arch.host_context); kvm_riscv_vcpu_timer_save(vcpu); + kvm_riscv_vcpu_guest_vector_save(&vcpu->arch.guest_context, + vcpu->arch.isa); + kvm_riscv_vcpu_host_vector_restore(&vcpu->arch.host_context); csr->vsstatus = csr_read(CSR_VSSTATUS); csr->vsie = csr_read(CSR_VSIE); diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c new file mode 100644 index 000000000000..edd2eecbddc2 --- /dev/null +++ b/arch/riscv/kvm/vcpu_vector.c @@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022 SiFive + * + * Authors: + * Vincent Chen + * Greentime Hu + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_RISCV_ISA_V +void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) +{ + unsigned long *isa = vcpu->arch.isa; + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; + + cntx->sstatus &= ~SR_VS; + if (riscv_isa_extension_available(isa, v)) { + cntx->sstatus |= SR_VS_INITIAL; + WARN_ON(!cntx->vector.datap); + memset(cntx->vector.datap, 0, riscv_v_vsize); + } else { + cntx->sstatus |= SR_VS_OFF; + } +} + +static void kvm_riscv_vcpu_vector_clean(struct kvm_cpu_context *cntx) +{ + cntx->sstatus &= ~SR_VS; + cntx->sstatus |= SR_VS_CLEAN; +} + +void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, + unsigned long *isa) +{ + if ((cntx->sstatus & SR_VS) == SR_VS_DIRTY) { + if (riscv_isa_extension_available(isa, v)) + __kvm_riscv_vector_save(cntx); + kvm_riscv_vcpu_vector_clean(cntx); + } +} + +void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, + unsigned long *isa) +{ + if ((cntx->sstatus & SR_VS) != SR_VS_OFF) { + if (riscv_isa_extension_available(isa, v)) + __kvm_riscv_vector_restore(cntx); + kvm_riscv_vcpu_vector_clean(cntx); + } +} + +void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx) +{ + /* No need to check host sstatus as it can be modified outside */ + if (riscv_isa_extension_available(NULL, v)) + __kvm_riscv_vector_save(cntx); +} + +void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx) +{ + if (riscv_isa_extension_available(NULL, v)) + __kvm_riscv_vector_restore(cntx); +} + +int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu, + struct kvm_cpu_context *cntx) +{ + cntx->vector.datap = kmalloc(riscv_v_vsize, GFP_KERNEL); + if (!cntx->vector.datap) + return -ENOMEM; + + vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL); + if (!vcpu->arch.host_context.vector.datap) + return -ENOMEM; + + return 0; +} + +void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu) +{ + kfree(vcpu->arch.guest_reset_context.vector.datap); + kfree(vcpu->arch.host_context.vector.datap); +} +#endif + +static void *kvm_riscv_vcpu_vreg_addr(struct kvm_vcpu *vcpu, + unsigned long reg_num, + size_t reg_size) +{ + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; + void *reg_val; + size_t vlenb = riscv_v_vsize / 32; + + if (reg_num < KVM_REG_RISCV_VECTOR_REG(0)) { + if (reg_size != sizeof(unsigned long)) + return NULL; + switch (reg_num) { + case KVM_REG_RISCV_VECTOR_CSR_REG(vstart): + reg_val = &cntx->vector.vstart; + break; + case KVM_REG_RISCV_VECTOR_CSR_REG(vl): + reg_val = &cntx->vector.vl; + break; + case KVM_REG_RISCV_VECTOR_CSR_REG(vtype): + reg_val = &cntx->vector.vtype; + break; + case KVM_REG_RISCV_VECTOR_CSR_REG(vcsr): + reg_val = &cntx->vector.vcsr; + break; + case KVM_REG_RISCV_VECTOR_CSR_REG(datap): + default: + return NULL; + } + } else if (reg_num <= KVM_REG_RISCV_VECTOR_REG(31)) { + if (reg_size != vlenb) + return NULL; + reg_val = cntx->vector.datap + + (reg_num - KVM_REG_RISCV_VECTOR_REG(0)) * vlenb; + } else { + return NULL; + } + + return reg_val; +} + +int kvm_riscv_vcpu_get_reg_vector(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg, + unsigned long rtype) +{ + unsigned long *isa = vcpu->arch.isa; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + rtype); + void *reg_val = NULL; + size_t reg_size = KVM_REG_SIZE(reg->id); + + if (rtype == KVM_REG_RISCV_VECTOR && + riscv_isa_extension_available(isa, v)) { + reg_val = kvm_riscv_vcpu_vreg_addr(vcpu, reg_num, reg_size); + } + + if (!reg_val) + return -EINVAL; + + if (copy_to_user(uaddr, reg_val, reg_size)) + return -EFAULT; + + return 0; +} + +int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg, + unsigned long rtype) +{ + unsigned long *isa = vcpu->arch.isa; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + rtype); + void *reg_val = NULL; + size_t reg_size = KVM_REG_SIZE(reg->id); + + if (rtype == KVM_REG_RISCV_VECTOR && + riscv_isa_extension_available(isa, v)) { + reg_val = kvm_riscv_vcpu_vreg_addr(vcpu, reg_num, reg_size); + } + + if (!reg_val) + return -EINVAL; + + if (copy_from_user(reg_val, uaddr, reg_size)) + return -EFAULT; + + return 0; +} From patchwork Fri Apr 14 15:58:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211833 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id BDCB4C77B72 for ; Fri, 14 Apr 2023 16:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1M63AQpbnjkxEqqgLfZg5isZQOOIP8cgCkHLFVSP4Ts=; b=G13Z14Sa/eYqe7 DCedn7HNl9slVH62FsKxlHPA69I+YQW4AoC7x2kXo/tLSmp9mI3g9uUe2r3kTGAtrVRFxTDwDFkjC df+ynE9aZNDZYhf/G1TYUBpCokegEyYco7qkKxASKEyl+PjyV1yfJoHqWXlTHvZkM2G1RGJjKx/r1 kVsQYsreyjuqfJ/rIaIDTXj6FgTfIFVnm8gMQJRkTri2CdKxrvYt7f2EiptGH765xszZXQe3HTeTG B7Oyd19A8ykRHrzX3a9Qpet0LoFIxOyERdSzSR66aVxw8rnCwDNhCyA1srHlwa9vwcYYThfFLn/u2 kLPC7fi06DMsrrpkEElg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMYB-00A8yk-0p; Fri, 14 Apr 2023 16:45:27 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnMYA-00A8yJ-0t for linux-riscv@bombadil.infradead.org; Fri, 14 Apr 2023 16:45:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=References:In-Reply-To:Message-Id:Date :Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description; bh=zmWeoSmrBMtxR3vKNVKojBcvrcQrIEBGMqJAoaelw98=; b=cjDPrrqoSOLlCppQMQXvGQdqAM 68YzF1wrYM0Jkkwk6eXktr6Scyt7CPlJbvLZrU00Q/Uvi7MB6TfnDvVoFSFfQ/Z89kSh2jcWwqdy1 54xYsLw9hZUEY7Q3MjEl3ar73eNbO13LhVUu+stlf2yoSNT/cZp0DdG8qCsh8+Oc4ru1vka9Yxd90 8ObuocbC6DTuBH69tvCAg/Dm/KG9WP/3GSWwPPzTnJcJz259D/vmyCjUeeoIJNisoGP+/F5NVVe1p 4l3sImbLrz9iVFyiBfFsVBNQdNWtg3dDb8XG1Uy8whZkn6rNISyCOMVJKRHTd0h9RtlsxGaWF1fQA aZgtozXw==; Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqx-00FcLK-1q for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:50 +0000 Received: by mail-pj1-x102a.google.com with SMTP id y11-20020a17090a600b00b0024693e96b58so17996931pji.1 for ; Fri, 14 Apr 2023 09:00:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488045; x=1684080045; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=zmWeoSmrBMtxR3vKNVKojBcvrcQrIEBGMqJAoaelw98=; b=G2pr/OVLqAjlZTUCowmQ8edFsVI/ipTpPtlA8Cefa/YtKrlp3dSIe1ucbn2mJkpITG kaI06/iiyzCch4xRRhUP3D6Vx0YcXDUKTNP+uM5XmOihjUxyuq3+Bu2SHAwoWy8DT/r3 VfYEWh5W8jec6s26Gxzod+MN6Nb8Ua2X374Dq+UuteqPg3wCyKDMxQWvzgPfEPYYGv89 9kL0zZV13R14WLiJN0V82KZGMhdarOvbHYu9pZouoqrqdmdtE35r/Z3Q+nd0F0XxbCFc 1Pi0RXacB/bfcqnunIavT1H4gDROfR8Teu06QbF9UGVwqOH8v7JZhVVMaUUAcBWMjeLl cXQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488045; x=1684080045; 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=zmWeoSmrBMtxR3vKNVKojBcvrcQrIEBGMqJAoaelw98=; b=j4xqm3cTWEZXjZzczU//tYZakvexEF6dXUc6TrnC5ljtq5Z6SL2/2xKrKqDAAR5em5 1C62tt0RWH5WH77QC4hJ3HBn8xg/nuvj4BrjbUNeJVyMj2HXl4/sV6ovbhxoqVFiwnZB TvKbH+OpXCQWzmDpKeC2AIoiiJyQRvIaRl4rpXnMmJNacjhTggcT40KXTlrHoS86a8rx qIhnTp6YmyvqZ5Nq4jLWalzLg0MsIScLADDhSqrv+RRb2UkRFsKTsna1U3g+yfGYyjbE dpn//OWzG4zY+W0pcZ+DAj0x6l5VsPfYloScwBTN4JN1WiBT5WcdNZDz56jHe/F0t/sJ NHZA== X-Gm-Message-State: AAQBX9eKjZ2WyYNNtt0CTgZjkqif0Rjn6Xey7KkeH2LDO9agUZIlWxjL fbHlQb1AaUTYrO0IFkQGRI+YQZ5peEAs5YwE6Q4nXSyFeJRps6J3RHNTZa90+oFm57fLohqrZuo KsO59yalNudivDaOMuOsxHlRHl0+Mdhj20SUQgNnbMmYcloX11vR3imJAp3ideUgyV7sLzqCU/U BH7GSA5DHqL4fG X-Google-Smtp-Source: AKy350ZZ8n236tZyzzCLneynVwAEUb3PwX3tobYlXJckVQxkA1mIcx2zmM9lIQADhKB5QaKuGssmRg== X-Received: by 2002:a17:902:ceca:b0:1a2:a904:c438 with SMTP id d10-20020a170902ceca00b001a2a904c438mr3604960plg.58.1681488044928; Fri, 14 Apr 2023 09:00:44 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:44 -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 , Nathan Chancellor , Nick Desaulniers , Tom Rix Subject: [PATCH -next v18 19/20] riscv: detect assembler support for .option arch Date: Fri, 14 Apr 2023 15:58:42 +0000 Message-Id: <20230414155843.12963-20-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_170047_828533_7726B9B1 X-CRM114-Status: GOOD ( 10.23 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some extensions use .option arch directive to selectively enable certain extensions in parts of its assembly code. For example, Zbb uses it to inform assmebler to emit bit manipulation instructions. However, supporting of this directive only exist on GNU assembler and has not landed on clang at the moment, making TOOLCHAIN_HAS_ZBB depend on AS_IS_GNU. While it is still under review at https://reviews.llvm.org/D123515, the upcoming Vector patch also requires this feature in assembler. Thus, provide Kconfig AS_HAS_OPTION_ARCH to detect such feature. Then TOOLCHAIN_HAS_XXX will be turned on automatically when the feature land. Suggested-by: Nathan Chancellor Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Nathan Chancellor Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/Kconfig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index cc02eb9eee1f..205ce6e009a2 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -246,6 +246,12 @@ config RISCV_DMA_NONCOHERENT config AS_HAS_INSN def_bool $(as-instr,.insn r 51$(comma) 0$(comma) 0$(comma) t0$(comma) t0$(comma) zero) +config AS_HAS_OPTION_ARCH + # https://reviews.llvm.org/D123515 + def_bool y + depends on $(as-instr, .option arch$(comma) +m) + depends on !$(as-instr, .option arch$(comma) -i) + source "arch/riscv/Kconfig.socs" source "arch/riscv/Kconfig.errata" @@ -442,7 +448,7 @@ config TOOLCHAIN_HAS_ZBB depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zbb) depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zbb) depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900 - depends on AS_IS_GNU + depends on AS_HAS_OPTION_ARCH config RISCV_ISA_ZBB bool "Zbb extension support for bit manipulation instructions" From patchwork Fri Apr 14 15:58:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13211849 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id A7816C77B6E for ; Fri, 14 Apr 2023 17:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=lwcx7N9I5Z5lkqCbbrYzKnWLE26Ub7oM0i02O1kz0Fg=; b=nYdlqJZaCXg39j JB/1f1ddVSmVsojLGQ15qbFQtJ/XoRgQFZYyZn59uoHKSgyYYEMIIjrxsbKz+wa0sSmv6mYD94L2z j3TlLWl/7K1VdQJovNF1nGy3gloaSqdneCsoabAg+mqRrWc+L2ikvmcA9Le411yyQ8/gXB3Pr/5j7 SRl0YjNvdjCUG07BrI8B3t7akAgi1LS96DOvSfCK1ZsNXO9TNaHVl7Jja1Yoa7lOlDCyr4F4A0xza 1tiVn0ZV7BYNrlAFcXc+T9RIgvlg5fFjvhy4McknPKvotjbMbrc/Ji7zDAm71mw8XhITt/IdKTsoe TNRhnGw1dAiCqhKpebow==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnMnU-00ABOt-14; Fri, 14 Apr 2023 17:01:16 +0000 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnLqz-00A28I-0J for linux-riscv@lists.infradead.org; Fri, 14 Apr 2023 16:00:50 +0000 Received: by mail-pj1-x1031.google.com with SMTP id hg14-20020a17090b300e00b002471efa7a8fso5413114pjb.0 for ; Fri, 14 Apr 2023 09:00:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1681488048; x=1684080048; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=E9XECObCyZpUedK9knyGwZy3BMqflVOS6gxHGxGLKTE=; b=gkc5s6HPzym+RHUm++bVdfv9jb1/AkXmpyekQHGNyDGRRDRYGjsm+T5pPdbkIedgdV ZFQ6LrmUfO+1WkSx2IgoF4ZBZCNi62pkkDd2LecWTiR0dGThihYtuuKD3qAYYgyp4scN 6dSaca2jMax/dpE1eRXkip1mPWJVjhnFxU4OsG9dCgtR/38nce7UO6rc31M/0niyugTY OGZBDUSrCItNQk5ZhcxqJNi+vl4rQPEd4dLts6y+fkwyPmztt2QGc3F1zJKebU4pVnFG 0z5VwwXgPyU4CL8P1dX+GDezKB2ciU51Yp3dyrXKzingm/2BiEBojiIbxgZVrF+8FpuD 77VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681488048; x=1684080048; 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=E9XECObCyZpUedK9knyGwZy3BMqflVOS6gxHGxGLKTE=; b=TGq75Y8Fp9d4Mv3lDQJSB4k1/gMk4ulpEPYPeMdltQyZTlMdYriaqylvxyZHPPgPOi C02zejklTEX4zz6uE2QDMu128HwKZY7TyTbICAlKYaszUU+xO+t8TcEtAW78lL+uYwTr GwBbtKoCgleaVhiQ+r+Y8je01UBwZU7VJnxLJngpQkBoSJBgOXA4u3QbWwVgreF+f/Uh lnjH7CuOP618YBv89EUorhuL4nov4czhxthQPONWlCzBoW15jcyb+t8CW2iItmhNxEB4 AVddEUEtj7nSVeC6ZqkOkvFoDldoJcWbJ1v++NNLK0EzX3mhOqulDD5jUtDedGcnnulk krXg== X-Gm-Message-State: AAQBX9dkQpg8n9Qw8tCDtgl7l62zC+9VCdVzH8SSfMpA2jD7/nszzIA4 7LGJp6Mt3I/EB6mPFnUtpxMsYI3J9luprap+2JhkOZVR3LWu/dRpunA1CvYqSwkdfxWnbxmlR0z 01NLwQSmVmF9OqmG5yErXTWJ6RXqS4HQFTqPmIG9/y0gZl9PVRxJB8o+ebRMpzYI3IjapNzFRLx l3lbxl4XV8+sDr X-Google-Smtp-Source: AKy350bAtaGvhnaI4rhSGJ+v1XE5PL7wflyiii5/XGq5ELK2D7RWeVxXUPMbdb1aQIpE7CzaET8gZQ== X-Received: by 2002:a17:90b:f0a:b0:23f:582d:f45f with SMTP id br10-20020a17090b0f0a00b0023f582df45fmr5745324pjb.1.1681488047968; Fri, 14 Apr 2023 09:00:47 -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 br8-20020a17090b0f0800b00240d4521958sm3083584pjb.18.2023.04.14.09.00.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 09:00:47 -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 Subject: [PATCH -next v18 20/20] riscv: Enable Vector code to be built Date: Fri, 14 Apr 2023 15:58:43 +0000 Message-Id: <20230414155843.12963-21-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230414155843.12963-1-andy.chiu@sifive.com> References: <20230414155843.12963-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_090049_131330_9510DC6E X-CRM114-Status: GOOD ( 10.64 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Guo Ren This patch adds a config which enables vector feature from the kernel space. Signed-off-by: Guo Ren Co-developed-by: Greentime Hu Signed-off-by: Greentime Hu Suggested-by: Vineet Gupta Suggested-by: Atish Patra Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/Kconfig | 20 ++++++++++++++++++++ arch/riscv/Makefile | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 205ce6e009a2..5edfc545aafd 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -442,6 +442,26 @@ config RISCV_ISA_SVPBMT If you don't know what to do here, say Y. +config TOOLCHAIN_HAS_V + bool + default y + depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64iv) + depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32iv) + depends on LLD_VERSION >= 140000 || LD_VERSION >= 23800 + depends on AS_HAS_OPTION_ARCH + +config RISCV_ISA_V + bool "VECTOR extension support" + depends on TOOLCHAIN_HAS_V + depends on FPU + select DYNAMIC_SIGFRAME + default y + help + Say N here if you want to disable all vector related procedure + in the kernel. + + If you don't know what to do here, say Y. + config TOOLCHAIN_HAS_ZBB bool default y diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 1b276f62f22b..94684dbe3b36 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -56,6 +56,7 @@ riscv-march-$(CONFIG_ARCH_RV32I) := rv32ima riscv-march-$(CONFIG_ARCH_RV64I) := rv64ima riscv-march-$(CONFIG_FPU) := $(riscv-march-y)fd riscv-march-$(CONFIG_RISCV_ISA_C) := $(riscv-march-y)c +riscv-march-$(CONFIG_RISCV_ISA_V) := $(riscv-march-y)v # Newer binutils versions default to ISA spec version 20191213 which moves some # instructions from the I extension to the Zicsr and Zifencei extensions. @@ -65,7 +66,10 @@ riscv-march-$(toolchain-need-zicsr-zifencei) := $(riscv-march-y)_zicsr_zifencei # Check if the toolchain supports Zihintpause extension riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) := $(riscv-march-y)_zihintpause -KBUILD_CFLAGS += -march=$(subst fd,,$(riscv-march-y)) +# Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by +# matching non-v and non-multi-letter extensions out with the filter ([^v_]*) +KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/') + KBUILD_AFLAGS += -march=$(riscv-march-y) KBUILD_CFLAGS += -mno-save-restore