From patchwork Thu Mar 23 14:59:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185758 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 C1E73C76196 for ; Thu, 23 Mar 2023 14: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: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=06GFRca52two65 Hozx9fn72Ii3FwCuKKTb1ghQ+EwJwnHTw0OX/wuwzk0lzLwS90+FjS2Tpu61jf/gTS33AqRhhMOMa bHlfB2fmcoBTlamdOknB1g41UY8Pg9Xkgk9KGjdTaYrmHgkeMAh+joVV5DN/8K6Wn0dMhce/pZTh5 I1vR199hJzHuZbfNHFr2CL6MxVmcxcc0Fim/8JRIpZcI365G2KsWRMz/i7pfqYX5vrfnTjkMy+eg4 LeKBIfbmMIx6ccsM0sRu9/Cm7eRVFfYaMMpsrz5cK7LanjGNC4CWHUp01OtdjdzMJv2dI/hEAAJMe eHcSojJC+iWkAkZX4qvA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMPm-002GYP-2I; Thu, 23 Mar 2023 14:59:42 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMPk-002GWH-1J for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 14:59:41 +0000 Received: by mail-pl1-x62a.google.com with SMTP id o11so22571786ple.1 for ; Thu, 23 Mar 2023 07:59:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583578; 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=EE4iyZ9ZIYFKB5FMvRNeYHxXk8u+i1yhDt4omFH12gjYyHwTnUUiC13hv+Il1HyF6s rDXdUWb0h4Ie637VWRym/g/GXTm3eWY062jeeytZnqOgRxRu5dAhzahW6W4wRgoh0Jjh ZEfTkXrLECW3ihGU8STm1NE6hOWmG7EzPpqt1Pyd3QbkBLvFgZ41nH7kZ2mEcb91ebJH GEDqMHY53zS6Czn5N5+Bfr+Fx5vAlzIMcgn1Sv9Gq08CJJrPQ8I9p8F+7MScW4hMcSqE 1I7bS048M166lupwCQ8xyRVPoWr8gV/rb1IH9lvyAJTs2q+B4Mqgfemdugx3j7vzqbvE paOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583578; 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=WbC9dku/MCkkNuxsei9Bs9Cr0zpiG7xBfkwIVfc0hZFDFufj/KJgTtrxke3MRU48xe oCmgmaiIzZXNnDFTrwTa9k5Epqna48I0ObF23i/yKrJHHLPWqgVjqCJ3EYZinulWsUlU fB1npQTL+kD6yAuL4eDocXRKRaqI95b5cFEeKdhbmDkAWIGkIVVtSu8uS6AntFBinNyB 0mxGSsPkqScrSlpA/LFbqdARRF1klnRRR/+mXrWujppyEq+jsakgwkexxSua1x2odoZi U9a4YEznMkHxazUextJwgyNcJjnrldBDEYQz1F+Uon6UwH/LsNZqtiOajZCn8JtaiNpZ GrYg== X-Gm-Message-State: AO0yUKW5vO5/eRPD09NmUUdS24gDm3X60FvmWHBEvKLU+Y4+Fxwoxsdj l4FoL3hPAzOY5sBiS8BFU33iWoJWQ9rQb7whtzej72p0tV1uM1Jg+0Ss5IJaspzgWCAn0xDU2PV N28Kca97u7wnw63x2BaZtIza5p6O4c4CznJ9iiyVdkYSrvy4PRzadcJNyOIbOxL0xvU3dYvX/h6 /9hUUfdLdz8XB7 X-Google-Smtp-Source: AK7set+/B5nEPkQgpgS7TZzUH0y5Y+FUfRpzPltL0G5GjJIHTjTPQSYBW3inr/Q72qMdyce5IYxZZQ== X-Received: by 2002:a17:903:24f:b0:1a0:7508:daf2 with SMTP id j15-20020a170903024f00b001a07508daf2mr7393444plh.2.1679583578342; Thu, 23 Mar 2023 07:59:38 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.07.59.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 07:59:37 -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 , Guo Ren , Conor Dooley , Heiko Stuebner , Jisheng Zhang Subject: [PATCH -next v16 01/20] riscv: Rename __switch_to_aux() -> fpu Date: Thu, 23 Mar 2023 14:59:05 +0000 Message-Id: <20230323145924.4194-2-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_075940_446516_36239F10 X-CRM114-Status: GOOD ( 10.46 ) 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 Thu Mar 23 14:59:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185759 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 417E5C76196 for ; Thu, 23 Mar 2023 14:59:59 +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=YNiQ3+JWLk5uWsiIJOFGVe5qrfp/yJkVgaSjcKlVgug=; b=Gzsp+p14tfJDEE S+S1X4cipBDAEovxPBpSc1b+q4fVZ1l+zTsyrG1i2rJLpW/AOA3pxu/lbrowMJ51kZsR1sb4RI1jJ rRr43pLG3zWzh6F42x9SN39Yh7Ku0mEpCr+kX2Mtp2bTxmQQL4wCmCxW0tAYrlcFGxXRGnj+S7LVL Io3+xxQY9uuOsn5Kdfw+ppfVH0EdnLL+4EvJCjDL9HEmQzhOUvKy762+7XIflwW0nviv2/ouxqXYM HJE78z4nSpEsFiYHYqpM4EamzbG5wNMF8d0IvP4i8XaZPNWVNqIQzzp/4Fw5opq+C0SsQM4dVPJFa q5QwyW0dy6Sh3Z21kUFQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMPw-002Gcp-2b; Thu, 23 Mar 2023 14:59:52 +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 1pfMPt-002Gag-31 for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 14:59:51 +0000 Received: by mail-pj1-x102f.google.com with SMTP id a16so17365234pjs.4 for ; Thu, 23 Mar 2023 07:59:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583587; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=8ysjVmLSFjJmySy1HzqCUzkjl7bv/RwwIsrQKPYD1lk=; b=mOQ06MlFhAwl4tdwszxSxr1cfY5V78ViEsk9APJqofD0UlU/77e/kNfcI/QRqczkAr V5BzJS8O5fqGR71Pk5cAU1c1viCQEztnwNbjf8SQhGVq7+7Tsnn7OFeZglBYjlOK6EW1 X9dyItoOJVC8q0lx6gtjXOv3A/sG2hEKvGAw6WafQHYz3xFz/xvhVdzcMrXp7sAKAW1e EdJhyUj6goBP5Ym5ya7IQNX8RxHuFMheSs3bo2MxIIG/dndSd/QxuCZCwkGOdGW+7F1h 12cxJNxhFpxjvPPVsOKZ0DUNZHi/JAymogfQs/iVqNIYXuR9D29vbZ2y4cocvy6GIc6U 8+WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583587; 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=8ysjVmLSFjJmySy1HzqCUzkjl7bv/RwwIsrQKPYD1lk=; b=2gjGvGcAeEzBLAR2x9n33pZPCdaLEl1rwEd1DVcwqgVds48h2gW9RgPWl5Ten3S/kD RHb5ciVMCxU29aGH1071+tzTewh1edkeazWNnH99qx/EhQapVgVSw0SlMQLwNdVkA1EX DfP4I9yI0lyn2IDMn7QmVe688HlNFjyhQ87iovfhJb2WdSbjqxXC2AfIN3PbdJS5KUNt d6WTM5P5RfWCaYFGuySE/ayoHW5VLN6MZ9sgrmLKDzQT+Q03go3NEWvglJXgNgmaGBcf Z+r4ZcYKaPV7MisL5l01F4jF99SZe/7b05kxy4+f7TtixX+ymceAu93e2DNPADGoiHVm 2eUw== X-Gm-Message-State: AO0yUKUR+iPNL3+ogxRqYjMKUC8vWYtOiIHnH2z5LdmmcenA659OXVFC xj0nVXCm1K+SdedY4+bS1EEBIov0FH16cV2YVu9SgvHu+xuRRQtfcy8HA/aNMieCGlyWM+aZOEm 2zqPR2nLuZphg5dJuAi23dNwX+IBB6b8SQufIuONlZUIoraAdXv98y5gmYo/wy3plC3MrC2Yvs6 h9ib2019lTFiHN X-Google-Smtp-Source: AK7set86fbWkiv9YiIHGj3faRY7lEf+H+q24ZHOdFTkQ2PKKWiqta70OHUd/x0D9fUsMvnjP04qxrg== X-Received: by 2002:a17:902:e543:b0:1a0:7422:939a with SMTP id n3-20020a170902e54300b001a07422939amr8624796plf.4.1679583586964; Thu, 23 Mar 2023 07:59:46 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.07.59.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 07:59:46 -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 , Andrew Jones , Heiko Stuebner , Guo Ren , Jisheng Zhang , Dao Lu , Qinglin Pan , Vincent Chen Subject: [PATCH -next v16 02/20] riscv: Extending cpufeature.c to detect V-extension Date: Thu, 23 Mar 2023 14:59:06 +0000 Message-Id: <20230323145924.4194-3-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_075949_976655_93EE55F2 X-CRM114-Status: GOOD ( 15.72 ) 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 --- 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 bbde5aafa957..7df8db320934 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 Thu Mar 23 14:59:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185760 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 25CC2C76196 for ; Thu, 23 Mar 2023 15:00:04 +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=e3y/UC4tZuPU1Kvpx1IVBzU9ySmEjPyAXMO7VSu+qNU=; b=F+wivSw49fzoyj KId7eK0igu3S9S3VgolWmuHe7FQ2gHpl34yX0GQR55JX3BVFPUbrSNw4NbIFukCZuU13UMFeOfz0W M9JIrohA3Nj/1TiPeqPBvmqplMPtvQmQH1JdzDxAXM6Ga2Mv1YdWALuQEm/yGn8G3AonYQpYeaGrA Fdg9vRPfG2lJnPVx2HS7cYUSxYhx8elP0P/9/icCtPMdcEoAy0FW3m0Yx4YT0fPNIREIA1Rfpl1/o vlicJpVmviXAGY/Mmcz9QSfwxtkC9+P9nIrEpxH7v4Aze1r1lxevKUX/P0aQPIHSiAegm0I5tyxw5 gvjAp535p90ZP3Ig7VMg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQ1-002Gg2-0O; Thu, 23 Mar 2023 14:59:57 +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 1pfMPy-002GcT-29 for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 14:59:56 +0000 Received: by mail-pj1-x1032.google.com with SMTP id l9-20020a17090a3f0900b0023d32684e7fso3496819pjc.1 for ; Thu, 23 Mar 2023 07:59:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583591; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=PnQ1gM/aEBoZbxhfb57ZLXc6ZOM+cLTwn6NQvrPI9A8=; b=in9L72Bq1wyyyCu5Us/d2fWoyH5i9lIDbJZuj0vsYeySOsINiL7P0D/RCfG5TfBXmj S1OLjKNr+cT37kDSBYmYW4HtRGIr123Fm6izH4Cl6/LbPiuvZIDdoHAkhscgqRy6SIv4 FmEb3HsM5N2OVUfSrnPgRTsG3eE/JmLmgHehgDKxIT81xZc3eHOTLcYXzhCs8LLT833U /YxuN07sT+10IrOGVr8Vchp9kmYDgXNIfTOeXmIfqoSdpSJUOky270eCw8KvkcN9Wf9Y 1kz8IDzvOmksd7cwqYAEzXcPwuld+70QohDxInOBcVHCv0LI5hxHaBw2SJnaOSeGTo0a jQJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583591; 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=PnQ1gM/aEBoZbxhfb57ZLXc6ZOM+cLTwn6NQvrPI9A8=; b=3GjM0k3b5dWXz6qaTEmGXkfZevw6AIFMVx4K/4nIcAIQoWq42CsY5QZCoVdLRkhHde agCa19J3Quha70JNe6J88lZQsgYd8HTrZwIBeor56Qrsg5rVBZNAB3K3IzzCWq0NDgOi JD6DazZdS2XxAgl4G50E4aJBm4XRNXXo+mdOYhgTNakNL7FqU0tT0q23nEalb6nRZokC j714ZjjkHKIPM4+Rdr4nDGebH5gC6svbJkYr1s2Caoa9Sz0WW2cntzfDDFbUNqcfohci 0xPnv41YrA++zLELbmNObFiUxLgzOvdvHP17764k35se3Fec9SDw/2TEcUJEhnepu44V rNnA== X-Gm-Message-State: AO0yUKW65ggMKWmJScKNlX0DJvfN48V49ujCWLPQ1r4ZYO8dzc+mDZud u/3mbtbo6DilyvFaF94I/fvpP/l9HttqTW0PwjTIHgnq1caivB0YjbsuvHTJwsq6JjS6CY74STl ZtMBTjcGyC7wQ8o62Az9WzQnXpwU92b4Vn1HEgBBBTk0P1aZSqGdkYKg9rFe+4/RekTP9tMofzZ 4fVq8eiU0BXVpC X-Google-Smtp-Source: AK7set/F8IoD8xsNa6gXIltPvxqTiGdqc94EgYvU4CAtCYhWyJCGJeZQfhZAwa4DW9aCaz6SjLNrCQ== X-Received: by 2002:a17:902:d1d1:b0:1a0:48c6:3b43 with SMTP id g17-20020a170902d1d100b001a048c63b43mr5334195plb.37.1679583591224; Thu, 23 Mar 2023 07:59:51 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.07.59.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 07:59:50 -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 , Anup Patel , Guo Ren , Atish Patra , Heiko Stuebner Subject: [PATCH -next v16 03/20] riscv: Add new csr defines related to vector extension Date: Thu, 23 Mar 2023 14:59:07 +0000 Message-Id: <20230323145924.4194-4-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_075954_706924_56091987 X-CRM114-Status: UNSURE ( 8.31 ) 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 --- 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 0e571f6483d9..c3b87a7d1241 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 @@ -297,6 +305,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 Thu Mar 23 14:59:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185761 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 C3E65C76196 for ; Thu, 23 Mar 2023 15:00:07 +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=gW3Ff5/9GiJaKmYCQl7s6SpsJEYyJ7eFXZtr7ONWcRg=; b=Ps5paYLAHVijMN T9IFSepdsIYK3o2zaBCC62DPYM1UcB5uGzBfyaRVzVVDdcfXCYeQdzA5EUQh90mKHtkKa6pL5qwH4 OLqOJSnqohc9XVAxAK18AkW68e4gZPBA5NX6AREBofc/oLHI4sw2lVBYabyUW1hacudNvWy0pNTsd gjTMZJgu6eUSbbL6jORnG1eaeQQWo+OBIzYMMjOh2F/9r6bQNoQ9FhzYcFkCVmgZcfk7H/L9aGuZY mdejTZTjs+V7W+W8Gzr4LJSx85SzfxBLGjqJuGEwxWQnWOSOfsDeVLeQTxSebU6KqzwdrZkCgfsm5 kzTKb/HjSfD7k4BfU1WQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQ4-002GiM-1E; Thu, 23 Mar 2023 15:00:00 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQ0-002GfJ-2m for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 14:59:58 +0000 Received: by mail-pj1-x1029.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so2367660pjb.0 for ; Thu, 23 Mar 2023 07:59:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583595; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=CteMDAzkrOKUdcbIvPON419eHbnDZdFOKMS6mt0sWZE=; b=YZmF4D5+ASu+0FbybKdp/QQW9kNTxyCx5YsT6ZvM2DG/OesJXHQ+aDXBJlPpA/hNuc 6Ok2Cqe/0ncNLp8Mo6vJqHdPzwEjZZW2E/qTUILb1ds/11rWsZLPg1ULU9sRZNlJtX0e RLywUoQ2+Gk3ZsR0AtYjUYnMExlwFt7XFe5sx0VlC7XSB2iQuY7iwCXW4DcTDzLUXj8H 4VHAMluEY0zmLjuaXhhHMQqnsGuRw6fV7o5U6rpZZj8nBGQB7ctBlVTA5KmDh6fVFxec rMpDpvayB6Y69AQ542RP4xGTxSXcMT1SghG8eEJIBMo5uCkewwP1Dk8B8xUg3VgHwOnc L5mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583595; 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=CteMDAzkrOKUdcbIvPON419eHbnDZdFOKMS6mt0sWZE=; b=W/UT6+NQ1s+pM180tVSVqoWgMk+a2NApDYqCb+ItQqvIMX2VrBC7Vk6OaQVgpbqNzS 3JA9/A6Q/wp10GOilMBgqGknEgPfVrrWg7J4Tf4ZhhOEdIVPgFZs93LP65J0f2knn/f7 iwRdVcUlAKjp8su5GkSG7DCBN3+0f+69ayoNk9jIKlrOUkQB+5bEJXashMvoyRjZ2hWL agcAxdxm9KMVueyhqsFrsbyHEkPI3VqMwnDHnjajF7QsN+BXxtt0ghCD5L37xy8xFufy aJ+SegXxqDeIztasTOY72CAjh1UL2rQQqbsNI6ny5Rq6UFEMeDMgSy/9Z13ifvoc/pdg P06g== X-Gm-Message-State: AO0yUKVk9ONtKYAyBAbGqnmVPO2+gn85gNj8f7bvUp9wPSouY5iI12Mg 0EdMmEIvy2g9gf7p/luCYbr13Pgb/t9V82fSv3f49t6LqFIzDMPwRQTtRhGwubLH43v9zlhU9IP wCpAq1RbnHHj6kfsV8LR3LLnQSauSfkkqPPmvH9dWdn3evs9Rc4hOL1jlhHVMDXkNmm7itAIcIV twHjxK6cPma8Ip X-Google-Smtp-Source: AK7set8A+aZ2M7mxzfToXXBNFwaoRnGD4RaT2KdE16M9huLbT1x0LNbyAUn8zoOUaP8wkbeGiMTSjw== X-Received: by 2002:a17:90b:38c4:b0:234:67ef:304b with SMTP id nn4-20020a17090b38c400b0023467ef304bmr8717993pjb.37.1679583595508; Thu, 23 Mar 2023 07:59:55 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.07.59.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 07:59:54 -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 , Conor Dooley , Guo Ren , Masahiro Yamada , Alexandre Ghiti Subject: [PATCH -next v16 04/20] riscv: Clear vector regfile on bootup Date: Thu, 23 Mar 2023 14:59:08 +0000 Message-Id: <20230323145924.4194-5-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_075956_908933_9D779C02 X-CRM114-Status: UNSURE ( 9.94 ) 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 --- 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 Thu Mar 23 14:59:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185763 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 AEDA8C6FD1C for ; Thu, 23 Mar 2023 15:00: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=UcH3bmgZGNNpa4jcd8gdAsi6+3KUxK8/u2Xh3autlVw=; b=TsNcRZhrB9xbtL dvTw7VB3TUCS8eFvD5Y8cRIJttRcPHhwShiOHZhUn0WN8ao9Y7klDcQt7pNGzPNqNvDWYerRrSkVE P/7HYckYl28bWhlsZyq+SuKD5HdOaEM7H0uc0O/JqzQkqOJFxFgFcKAlrTv1f2rSzW/BBZFtTK2Uu vdH6YVeZdyYUm7afBXRxxATf3Byn3ncN+M8ugUdOKOVk45zWhXb649O1WiReloDU/qK83FL7xDCvz JwufxG3caYfV9cbVtOJH4I/QMCEzEuoBo0VF6MeTJUx+VD9TL5lviJ/R1yvp5UTEZOWSmLfhtxLwy CYr4umVoR1xr+76WrVtA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQL-002GvM-08; Thu, 23 Mar 2023 15:00:17 +0000 Received: from mail-pl1-f170.google.com ([209.85.214.170]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQA-002Gjm-2q for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:09 +0000 Received: by mail-pl1-f170.google.com with SMTP id o2so15147466plg.4 for ; Thu, 23 Mar 2023 08:00:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583601; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=q2CeCkoiCmH3/ws0KZkp+HQfm92M4kXVwNFV93ZjvNY=; b=lLHa8zxeOUxzOBWvUjn7GcGuisfOvitnbjturVsZByJRIeqv9N8MIxb7OnvTwqVShG wo9qCRlhw9kGNSJLIjTl7MvXQ2+hUf8eSnx7A2q/Qo5Qz4fJooE3k/Jy4EdERcWzawJJ VyBl3bkA5Xfd6cjaWeUWGoB5AAA42bLqLvJHZp7uUsflwO4BLeyBaYIdIs0j3lURmsoD zrXgpdMOBKJcWpsTi5KXJyVGXkiLKpEYV7rCREQwFT/B3fv2qIjdys5PkiBmagpov9Nx sHdlKpQXE9U21Kn4WarsBlM6edRYD+aFFotkOANTGbIRJg2+ZVub/aCfPR51cxiqc9RO V98Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583601; 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=q2CeCkoiCmH3/ws0KZkp+HQfm92M4kXVwNFV93ZjvNY=; b=EU4yf0kcQXcuXfujvUVLp34AC45GF0MiJb9Bjr/9iE0LqMT8BxUSsouxrr/pP2jN91 JdN3Rwi1NzPsDGxcNUo8SLfUNeC2lKjECXXZSC+ruC9kRYuLfXuM0eLiK7HjyEKaZpsw Rv/IljYJy+6G1KcFY93tWWVwwjZ0NBtFLu8n5ia3QeS7mr2jXCTKGDSsZuOepkoa2rWy FQQIagXhoLzUbt/sTnMMDCoraaISB2wUce64DehgHyr/zgadQCiexx1wc/tbjWpLUNgW XPxfHznkHzATt+5M399oFa0nOMDB8H0ZYIoX41Ic6uJBSvEaqDFo5JK9LtPIIqETrgvS M9Vw== X-Gm-Message-State: AO0yUKXw08KcGPyKMXMatbLudZsj7Am7Z1TKBqLmI8qQ1KY3YY3GvmG4 ecwHChYtA6ffDrmGm8GW5kYUIlwOMiFYu/5bfd9/gR1mU7WUcXAx6EAZrzzLGqRoG6FKPOwhxHU NRwCcO48Fazmsy5prAcK4ynLGOz7D7qYFX66k7X84QjOlR0AaB9Gmo5UW+R8vEG4jCYoWUSxje8 Xs9oxNZ7ts3rJK X-Google-Smtp-Source: AK7set92WvDWc4ERqOKa4APMJXuFZedooI+dsnimgnByggGX+kJn+VyeIwql1XMYvpcM95bSWPxoRA== X-Received: by 2002:a17:903:64e:b0:1a1:c0e6:d8d6 with SMTP id kh14-20020a170903064e00b001a1c0e6d8d6mr5930147plb.54.1679583601222; Thu, 23 Mar 2023 08:00:01 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.07.59.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:00 -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 , Nicolas Saenz Julienne , Frederic Weisbecker , Andrew Bresticker , Jisheng Zhang , Conor Dooley , Masahiro Yamada , Alexandre Ghiti Subject: [PATCH -next v16 05/20] riscv: Disable Vector Instructions for kernel itself Date: Thu, 23 Mar 2023 14:59:09 +0000 Message-Id: <20230323145924.4194-6-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080006_968585_89F4288E X-CRM114-Status: GOOD ( 10.08 ) 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 --- 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 99d38fdf8b18..e38676d9a0d6 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -77,10 +77,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 Thu Mar 23 14:59:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185762 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 3349CC76196 for ; Thu, 23 Mar 2023 15:00:23 +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=Y6piRx0lDD0Kvl Y59IRGJP4JYi2JZNC53ha4IVzVlt6/OkB9I/vZCx0XzHdH0Jsmc60nfzWZtoB/Tifm9fpudWuHeug DfPJRKWnQngB4jnTpXWoqD1qnX++fnYJHf2mcH8v/itxjU2QXvy0GSfOIW2M6MFKQLfG/0HcVkO4v Gd1XWTiLbOF7+6i5Y55XsaowbeXvBvSZTsLRCK8gOdZ2Dug9YMQtaEObPXeTHyMgcwRm5C1ndlHBW xn7BLaZ0UbNNrwKycJEMtTKbTKrLmoUm/HVu+jSzezM6zo1kanoMHSSPVK+5UhSwb0+ZDKjW0HCsc cKLQ4QgysQy5cDXre0yw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQK-002Gur-13; Thu, 23 Mar 2023 15:00:16 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQB-002GjD-0X for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:09 +0000 Received: by mail-pj1-x1029.google.com with SMTP id h12-20020a17090aea8c00b0023d1311fab3so2340795pjz.1 for ; Thu, 23 Mar 2023 08:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583606; 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=Rz41bOqdKfl5FfAf65R7fUQGQX1CzCAON8GMDbEPTjJc+Ea0PVKsuf2+GpTQEJL9Bp bY+tkHzGfmFJVhiAzf3bM6MPdE01bkF6uNUUoFKy2sUODXQTlA2Iz05JtvpTfQ5kIln3 TFoxrqWFkMJU1glN8/fRwTK7D09J7kW7WszfdkGuH2HdjYSEUXQcrrO7johdGVfbIaCn PWU/T75XvVvO+g71UJilt/RdOYV8w4mVxWkFhB0kbQSf2gtD5qigJT0VniGcFhXRv5zC Aoo7PpJIfkhnbD4MLff+iLmSP16tTABjSff+vkRsm2LFEdEs+KL8ovv2NJoA5VYjta6M yExA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583606; 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=yYQ4AMYRB/soz6NBKR8hUblAczzYnTiTAX2tW3+0myAlZK2Z82el2pv5kqSTTZfOqS FpjWSoSCHtcWEmx/oE38H6A2wOg0G15GCoVJJHbK7jfa2k27Kn0tEe4rzq2eQ/T2eE6n K806JGU6zQLA5a6twmmIwLt9v6RROSl4uiiNRmIkHhYTWrRXl3w8sxZ72ULcWmjsT35m ZjmjHgxnSSXH5U/3IlEopLBLz4vfyONOuSLzVzWhkLFV4jGLbBnsaOdYBQQft7Li9aXX +deuA8JdA3DowK1u3cC2515blZEZ16kaz+HNQASRJPH3zjF03U3H68gliEWzJaktJIKj ctFw== X-Gm-Message-State: AO0yUKVOKMQHCg9f1Un1e+ekSVcQWQhxMt01kEG3CQQz7tBg4mdmO66t 417ljTrp5mkQ91PN8UwTLidvgc2z8Xp8UVvPfCeoOlG/cjEjInJz89H/WBFP1/lqQ9TlNVk2cOY 2HZz4CvxXOl3uqmlaCMNd1z+nok0ACVq8X6+bTsGegCEZUQAmy9bIoorw2RJTFJeyNVUX4vphJY qZMc8JWzSZuoB3 X-Google-Smtp-Source: AK7set+uoxNy2JVwtQ/ySsF5piETGWumQRej0C/cqwQforKo3fh/cYPJk1holgP2j/v4Dycx8bWqOw== X-Received: by 2002:a17:90b:4a43:b0:22c:6d7c:c521 with SMTP id lb3-20020a17090b4a4300b0022c6d7cc521mr7293331pjb.45.1679583605730; Thu, 23 Mar 2023 08:00:05 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:05 -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 , Conor Dooley Subject: [PATCH -next v16 06/20] riscv: Introduce Vector enable/disable helpers Date: Thu, 23 Mar 2023 14:59:10 +0000 Message-Id: <20230323145924.4194-7-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080007_230072_68718BB5 X-CRM114-Status: UNSURE ( 6.31 ) 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 --- 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 Thu Mar 23 14:59:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185764 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 4FA1BC76196 for ; Thu, 23 Mar 2023 15:00:35 +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=eC4rV5WrVacCrG4VZPS8qLjlLyrCGLV+uBJefAKSTyU=; b=WFx7NXjhJrNchZ OwfikjYN1VcrRW/5fb1mv6cKpYVz5ZONMZv7g1YigLCFMeuPlYpSd9288xCwRNs7m+kOM27quJYR4 aMmhU1MAeLpFRIfjGRzxqQbX+pp2O43ol2J3wBWilX9H2ejgGWpRG2Jvz2YtQ48JVv8kSNnMol63D 7HeP9Qx/rR6hpXz7fGmb9GOSvr/zh0diX9iPCOfIZQ2S+qXM3duqRPZpytJpsg9NYG2ahFnDx/1N9 TjO4Uwt7OCW7Kiygy5z7HACkx6j8euEY1QfIcVGM+1B/GllNTzo5IkVVpXHWjMN7p5Vr3EhttrgaR Ciq9UF2yaFl7KlsgA39w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQW-002H4A-0X; Thu, 23 Mar 2023 15:00:28 +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 1pfMQO-002Gv1-0L for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:21 +0000 Received: by mail-pj1-x1033.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso2338968pjb.2 for ; Thu, 23 Mar 2023 08:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583616; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vEs6CRaMiSaSyMADyPFyhJnxav5yWBpTn1OIiNt7Cro=; b=dYj5R9/RWEAxx/UepPnaJQ/552VnDdZSDIMjHCAF49+RqgNbtTzf4I9CFPZUBOSY5U NZ+SvXZDKrmEYrAIJrVcvyWEi9sfGMIx2YRm8dMI8mJJg2X60LjLcEKadtXwhh09qVju CrdkRiFxYjzrkWN0ho986sHGszMHAujla+NjDaO0o2TieMZ0Go7LWw2/oynZwdXgWT3M m2NVy0DjyI6Mb17WLGMcTYfiSXKrUpOIghZNiMAdDAdTXB5cd82Tt/kZKTWBbBtMokdM AUrGx9DsrJWgU7rv5b6urrX2x4GyYoECvCQjzCnFYhF6/JXHDSzfC8IEFvXTh/3ykX1l 2gbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583616; 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=vEs6CRaMiSaSyMADyPFyhJnxav5yWBpTn1OIiNt7Cro=; b=627ap1DEuqEO7UjL+s3+IeIi6ozOFXsce3PWvEw8r0De+NCtUybf6VW2K/tQ0jcL/t GPBGuck03jKE5DrRTRnwim7VLCiVOwkEm7FOQgqKSARhp+rbUDe0VwDc1Dla8kZUK3lR AfU3hCu9OMOFBQ3fuvSiKiqebzZtoP4kkL8A8D8IH99+eT6heMIqngiJY/zKZnty2+tj bUG/z78q8ItHn+L8YbyWVPBT62Kdv52YaJJPWr+WGfaGtDblgRKdWJm+5x4ukyisiN5c et+MBktS0Cb1cGxd4tcTKbt0dIEP6DM1ZDniGt1lkGdmhPBXGyZi25V8b8QEC+2nNC/H FlHA== X-Gm-Message-State: AO0yUKUNDvf0+XRZG2ZoHFJUcCupAVQm6zPjsBoEjhNTYVlI37oOoM+C fa6UpwyY+pdMCn9pCLvGEB2q79GorEVs0raQfR2c6k8KmftsEgKmnhd31S/j6ijMjqMVGdd5jbz TqvQLtB7pwfdyWAELdLBrP7zPj0VhiDeFDvqd6BOPaAKCCgXqL/1PiHGVx5M/rIPLyAov2FGmqn EhERH+QLDTapyt X-Google-Smtp-Source: AK7set+PtVegPK51VFFk78fLlqp5HSdE85hJsVjyxbQGFMw0HWCd68xqflXn7JKfh7vFpjwOj7Qeng== X-Received: by 2002:a17:903:64e:b0:1a1:c0e6:d8d6 with SMTP id kh14-20020a170903064e00b001a1c0e6d8d6mr5931238plb.54.1679583615268; Thu, 23 Mar 2023 08:00: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 d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:14 -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 , Conor Dooley , Heiko Stuebner , Masahiro Yamada , Li Zhengyu , Xianting Tian , Andrew Jones , Jisheng Zhang , Richard Henderson Subject: [PATCH -next v16 07/20] riscv: Introduce riscv_v_vsize to record size of Vector context Date: Thu, 23 Mar 2023 14:59:11 +0000 Message-Id: <20230323145924.4194-8-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080020_156076_261FF784 X-CRM114-Status: GOOD ( 16.07 ) 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 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. 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 --- arch/riscv/include/asm/vector.h | 5 +++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ arch/riscv/kernel/vector.c | 20 ++++++++++++++++++++ 4 files changed, 28 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..e433ba3cd4da 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -13,6 +13,9 @@ #include #include +extern unsigned long riscv_v_vsize; +void riscv_v_setup_vsize(void); + static __always_inline bool has_vector(void) { return riscv_has_extension_likely(RISCV_ISA_EXT_v); @@ -31,6 +34,8 @@ static __always_inline void riscv_v_disable(void) #else /* ! CONFIG_RISCV_ISA_V */ static __always_inline bool has_vector(void) { return false; } +#define riscv_v_vsize (0) +#define riscv_v_setup_vsize() do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 4cf303a779ab..48d345a5f326 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/vector.c b/arch/riscv/kernel/vector.c new file mode 100644 index 000000000000..03582e2ade83 --- /dev/null +++ b/arch/riscv/kernel/vector.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2023 SiFive + * Author: Andy Chiu + */ +#include + +#include +#include + +unsigned long riscv_v_vsize __read_mostly; +EXPORT_SYMBOL_GPL(riscv_v_vsize); + +void riscv_v_setup_vsize(void) +{ + /* There are 32 vector registers with vlenb length. */ + riscv_v_enable(); + riscv_v_vsize = csr_read(CSR_VLENB) * 32; + riscv_v_disable(); +} From patchwork Thu Mar 23 14:59:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185765 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 E42B4C761AF for ; Thu, 23 Mar 2023 15:00: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: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=AriwCIWQl5GVrfY0DI9ZbhPhDlsPTDguCQtlaORkSNs=; b=uwbiEUz26YLaQ3 a+TSvkeDv0t10uO1VbgX1FSg7g2EzDlDp3pOrZhlAUaABJA7VsGqd6xoF4ECCGIgZGFpbZK+F6q+y foAPucX2F4PIHw3v7ZjXuqe3BVaWSlAhIey1UQs0futyGxbfKxvfizJhnicJdLDuvpRxwxIv9P7pz v5epvtMBeNF91/HXVHbUnEIMOrIAUwvwK/EONdd2Eh43uakXvfxNOO+EpX1tpk+Dij1CD3zbwsVWH 74xtC9Prh/hp4q2WBw9rQoTTntZlmyWLRfqKOHTW/KfJb2w08rolZ1eLywj0sf99E3FQmvAsgpqvk 40THVHGknjjIbPUvz7iQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQX-002H5T-1B; Thu, 23 Mar 2023 15:00:29 +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 1pfMQP-002Gzb-2k for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:23 +0000 Received: by mail-pj1-x102f.google.com with SMTP id p3-20020a17090a74c300b0023f69bc7a68so2331796pjl.4 for ; Thu, 23 Mar 2023 08:00:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583621; 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=Udr950R2Z02WuBhnFPF0mNQ6jR2x6UMx+TQ6xeOkBXQ=; b=bSsEQwQ4XPhZSMXAXUQujNFGLk7NDaeeq+gVyb1BYBY7ee/7iDet3nfe3ZiVq6/Pal BUNnz1HYK8poqIlaP3vonnN2jXi4EXQ66wCGbaMC6J5egiZSoj6XcmOpp/9IcjgcP2rW 9oDkF6It6dUKhW9CrH0jQNRtI3NyCTMKqsBAIATLzKbL+WcEk31EuLEYhR+4SXtq1eJH ebDPFS9xdHF6rKcxWZixzrvR/BMnu5trvK6W59yahFT9w7xybXYctFlTEA6LfHVm2AMh zgsN2wImYreYNGRrhfvi5gy8KUU69gM7FMzJnWoXkfSAspgDqmc/yITTSNdPVCJu6gLF gEhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583621; 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=Udr950R2Z02WuBhnFPF0mNQ6jR2x6UMx+TQ6xeOkBXQ=; b=uxaY9dAFZpyBDVTDzS8Q8NhDFc6gMoGFtkeqKyWL6PnfEQ54hceY2oJrgzdiXXjRpi 67qw41QuYZSL6xd8ma6oj7yZPwIHqTP9BDglpYDMXHTJeEXAbZbrTcWo+v7qAaaPtwPc 5IzigPqt0yd1dUR160KfT3YE1oNUIAP6M3vY+7BFLYVqRLTKumPVMh2R9SH18p4S3hTO SjksRZ0nhdftyEFaZqd3jeP7GMB2sHbT5GZdQLuu2oADO+imX9j/YMj3qRyDe4VbW3Jc VoQIzyth5ZaaiDc+HvXUTgzV+1aBUMbnUN9TDxCbqeMICCUVNm7ysHDJxRhkLnHmPPNI JZPQ== X-Gm-Message-State: AO0yUKVSxcVZsMLojzIPNaeeXWgSPOmiGqml0279AO/482qgnVCNeAjM U/Nsk+7Lxhw00Tcd0k9Xy6HY+xAkhMplEovsd4dRaW4CULr7iE/cn4d60RPdVJcJe6iVGdOgzV/ 9WUVXmokshZj9zZuvYrAK5EprXmAHoevQMwAL2KW/azgRvzEUVjKRT1gV1SgNmCxNlcKm028MDS mF9M2ckemiFd7B X-Google-Smtp-Source: AK7set9bj7jAM9hvZwKVYTRegG6kcX9LkdIIlXSH0wRRcsFhsLrRf7DJroNzGw6IIjucOKkptGl3bg== X-Received: by 2002:a17:902:e543:b0:1a0:7422:939a with SMTP id n3-20020a170902e54300b001a07422939amr8628242plf.4.1679583620849; Thu, 23 Mar 2023 08:00:20 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:20 -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 , Conor Dooley Subject: [PATCH -next v16 08/20] riscv: Introduce struct/helpers to save/restore per-task Vector state Date: Thu, 23 Mar 2023 14:59:12 +0000 Message-Id: <20230323145924.4194-9-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080021_894230_21D745DE X-CRM114-Status: GOOD ( 12.09 ) 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 --- arch/riscv/include/asm/vector.h | 97 ++++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/ptrace.h | 17 +++++ 2 files changed, 114 insertions(+) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index e433ba3cd4da..cb60637443be 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -10,8 +10,10 @@ #ifdef CONFIG_RISCV_ISA_V +#include #include #include +#include extern unsigned long riscv_v_vsize; void riscv_v_setup_vsize(void); @@ -21,6 +23,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); @@ -31,11 +53,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 __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_setup_vsize() do {} while (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 Thu Mar 23 14:59:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185766 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 1614FC6FD1C for ; Thu, 23 Mar 2023 15:00:50 +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=0U4odgkvBel7qoS1q5GKpEstHdHW39YLgL1BGELnSAE=; b=OM9NIntqdZgrIq lILmPZbT+dX/Ad3B6AOX1UO2UwzQyc4plIhVJy5AK6oFhHFI42SaptCSCFgmMUEyAlkmdlR3Cvshe +izz7E9iGGDOTszsuXMAkTLO5o7cZl1Sxgh1j9Hsg63b8MircVBQ+SEiKcqUWJxMcckuMWhhAe4EH Qp4AjogM54ISwKvzQI7tfOnJFPVCSFFB7WOTBE01g0BCMk5l1LCAw14qAcMpP+qStoGj01HRjlg5B w6ZQprtsPbh2BPT+InKl//bh6Ks7Qm28QIa1kbfmaVXivZtYtIZw2RnGGY0qPfJ1N4DhIlHfcfcPs 20BEo2VpyffYRyJzP1uQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQk-002HCK-2l; Thu, 23 Mar 2023 15:00:42 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQY-002GfJ-2t for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:32 +0000 Received: by mail-pj1-x1029.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so2370074pjb.0 for ; Thu, 23 Mar 2023 08:00:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583630; 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=Xf2AwGCX9J1lxfTrmsqASjkcgXs4Ao54RIi60tUfXgU=; b=HkJg6g1cs3fOqBNy0EF5TsFPlRGl4NT/5Wjs1oUSrkfP6ZXUPYM1f/2koTwjGKRVBy ebazUW27dlCO4h24Q2seOmYbopzE2brTHoaJG1T5GyQbImzbD+VXltsoj4Q77zz++D/3 YMulKolT+kmzvtqCyOUK5gIZKbcLg2Vcs94FKHYbTxw4rv77NoiREeMLUGFrDpHDDluC 3JanPY8XGFNyTy7mIEj/zQWVGrsV9VlgB1WnlMsFd9UgbcoYoocz8JsXrIooe7sdsXyA ds4mi3e5HwdKsw48U0+h2NEMzXlyh68EAZkyWjHxIvJ026boKLj2kzz9yKKxaD/eErmE fhYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583630; 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=Xf2AwGCX9J1lxfTrmsqASjkcgXs4Ao54RIi60tUfXgU=; b=JeOK6aj+/OLBOKdl/dlhGwDrjuoaDBHpJl3IsznAtenVmkIN/1STYiZooEecu/dIXC 8lF6ufCW66Ila7XBSEP6Y5OFxYbkId6AYnOYQixf7CDztF2GKpp0vs0d8cqC34M6DZz1 qpZOeo38AxKjO+KzgIn2iJP9nkTZlwUn71Wjtd4EvaOm1Yj3E42g2GpOB4QDe9rStgVQ SmkyOePlUDswGVGHS6gUuf34ryPwcfskVh1bJ7lrMryvGuf9ek1cDXMMJBnlldMRurap mGO3NGNoPl8ZIDcdglk41L5UrhA1gAreFHeZwboSgk5ZthgZrLP8BpDMo2RZVvdFPULL BYbA== X-Gm-Message-State: AO0yUKWnH2PX7xUJLzD7H+0wIr3wj2d5zLJFhb5GDPKeef9ysXNRgDCV kfZvO6GRGiL6MFs4j6sb5wDGRlImcDeyGBFcUphKb0P5s87tbvq7lcMCMj2ve/w1/6um5hjqNzW Rzg5L1pwsSgC9h4U7sec2lx5C1Gh4TQF0I5bM3mCrU8kk7ZtfvrlUqEcADt+BYniqBgsxyCJVSi /xyHkJtankZ21U X-Google-Smtp-Source: AK7set8n7qvLksH/zDfLkF9Mucv+9QxnGONRC2o+nYgKS2SGbOLOMqFWsJzJrGMATbPhF42bQx2UoA== X-Received: by 2002:a17:903:74d:b0:1a0:69ba:832e with SMTP id kl13-20020a170903074d00b001a069ba832emr5885983plb.0.1679583629945; Thu, 23 Mar 2023 08:00:29 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:29 -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 v16 09/20] riscv: Add task switch support for vector Date: Thu, 23 Mar 2023 14:59:13 +0000 Message-Id: <20230323145924.4194-10-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080030_943855_3889A69A X-CRM114-Status: GOOD ( 18.12 ) 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, "Eric W. Biederman" , Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Ruinland Tsai , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, Dmitry Vyukov , Heiko Stuebner 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 --- 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 f704c8dd57e0..9e28c0199030 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -80,6 +80,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 cb60637443be..4161352d6ea8 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -11,6 +11,9 @@ #ifdef CONFIG_RISCV_ISA_V #include +#include +#include +#include #include #include #include @@ -123,6 +126,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; @@ -131,6 +166,9 @@ 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_setup_vsize() do {} while (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 774ffde386ab..44ca0be58ce7 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"); @@ -147,12 +148,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; } @@ -185,6 +202,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->a0 = 0; /* Return value of fork() */ 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 Thu Mar 23 14:59:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185767 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 B80BDC76196 for ; Thu, 23 Mar 2023 15:00:53 +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=OKkCKXveSevpkwzbni7+mZq/0LHC1A+LohrvlbS1Qe0=; b=k9ipcFf/Jg+6zP dQkulpvMlpoUKGIYDSpMaug840sSAJOYpuxJnyt1cs72YFReF1jAHcqdOLfLnl4VaZzQHXeFT8Pqw 8jixbKPPkdxCFYRInHAqEZLMXLGuep+42kSoGAU8eW9LMPQoeFy43LzrqOD0qq1C1OcfffEJIhyqJ 9SnqAa7SB9bi8Y9f8eCtCxM14QK7CSxdn9KSmcG3Tf1W5IL4otWHDQu1oNGd6bzYqhoOWMj2G6lc5 Y61/1Pjmp8dI9hqopnn4YfEatwfAV33XGx7I0hQCkcZO/2iDibY70oMM2xt8dKbGc8knJntPKba/N aUD1J2oXc2B21XdCBPLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQo-002HFK-0S; Thu, 23 Mar 2023 15:00:46 +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 1pfMQi-002HAi-02 for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:44 +0000 Received: by mail-pj1-x1032.google.com with SMTP id f6-20020a17090ac28600b0023b9bf9eb63so2335020pjt.5 for ; Thu, 23 Mar 2023 08:00:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583639; 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=vWoeZKMFdcr/0hYCGSv8VJMlx+MIeRRmUBq7O45jdjw=; b=Vk58G51E5P8eWr/WC6am4Yqc+8GArjpHPhwqenf0gZwj1HGbHFXjYmOctx6E8PzudD Gggr3w6DvIbhw78V4tVOgyn+eX1Ob22ODUleBP2ywI/DQFptrZdpCHU0y8Pq/C8GE7VM qe98S2oLTD6AmkxtZUoztwh+Grw94ALts62gfp0tHMO7f+EL4cKaqsCt74x9r6XJncIh rQfbwzk8Zjh9e8wdO8OAFKzfpSKK9Tk0RJtlur8q3JU9NFh7dWV5XKJUPjMBj+Uct1Ma TQV661eemYmIm5z1ZVH8xM9KcqSF1VZRdmMtHzuN4HiYAQ3IiDWOmhqlHBwaHI+BzwT2 +AZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583639; 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=vWoeZKMFdcr/0hYCGSv8VJMlx+MIeRRmUBq7O45jdjw=; b=4OKpZ8h1C0Dg7/umYOc41ATfKSZhWvXCGBHVyEiMYTi1dhuW2YfDwO9Klp4jfFz9+k uJRACxyi1Y/ODLeQVepIHmJzTwKf3UKynKVpobYk0JVfpGHGFz+U/LtuEGJgCUF4/Fah UlB0XA+KjMYP3PGiD4LrIUyGZqPpj1V4xqqT14i9rhzpjVZDaJROsNrnMS3ThLbVvsky eKzBtnyhFOujaMjfb5QKT1JYYza4jyXKpQl8QWk4k6KNqIZjqv24D6gVFOSWDvQWfdnh j3E30WPKvc+XrulDWnlXzyHcE8CpNlycW+d6+GkqOHV5PAMgSgn7O3fJpM8EumYo5+ot OY7A== X-Gm-Message-State: AO0yUKWDzUpZABtVFxXCZdw6ok6sH0MWCuTHNQ0yKXf2y9PmIOTdSNKA jlwVis0UMVOjHeWqsjUMQmn5eqce7RZQgNPT8R4L9Qh4XuKnoYkqcNY32u5mNOxjdMivp9tqz1Z 7lsf2xscTBmazjtthywX3QPzwErdX9D+pUQawleVJd+OEhKmzfWhGXhDNCGECMidQ2vzZMkPeg8 0MpIthjU5wZEHY X-Google-Smtp-Source: AK7set8XaAE8pcE6mAq1eOufgmmblzYkbe6JPdGia22/gwghI7o+FFi/UVvSCiWl0mW/TYIp1cx0AA== X-Received: by 2002:a17:902:d2cf:b0:1a1:918e:4129 with SMTP id n15-20020a170902d2cf00b001a1918e4129mr8041443plc.30.1679583638645; Thu, 23 Mar 2023 08:00:38 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:38 -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 , Andrew Jones , Heiko Stuebner , Conor Dooley , Lad Prabhakar , Jisheng Zhang , Liao Chang , Guo Ren , Vincent Chen , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Mattias Nissler , Richard Henderson Subject: [PATCH -next v16 10/20] riscv: Allocate user's vector context in the first-use trap Date: Thu, 23 Mar 2023 14:59:14 +0000 Message-Id: <20230323145924.4194-11-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080042_416456_8DD5BD64 X-CRM114-Status: GOOD ( 19.75 ) 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 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 Reviewed-by: Conor Dooley Reviewed-by: Björn Töpel --- arch/riscv/include/asm/insn.h | 29 +++++++++++ arch/riscv/include/asm/vector.h | 2 + arch/riscv/kernel/traps.c | 14 ++++- arch/riscv/kernel/vector.c | 90 +++++++++++++++++++++++++++++++++ 4 files changed, 133 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 4161352d6ea8..70a5e696c1de 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -20,6 +20,7 @@ extern unsigned long riscv_v_vsize; void riscv_v_setup_vsize(void); +bool riscv_v_first_use_handler(struct pt_regs *regs); static __always_inline bool has_vector(void) { @@ -163,6 +164,7 @@ static inline void __switch_to_vector(struct task_struct *prev, struct pt_regs; 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_setup_vsize() do {} while (0) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index f6fda94e8e59..2a98fe74274e 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -24,6 +24,7 @@ #include #include #include +#include int show_unhandled_signals = 1; @@ -135,8 +136,17 @@ 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 (has_vector() && user_mode(regs)) { + if (riscv_v_first_use_handler(regs)) + return; + } + do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc, + "Oops - illegal instruction"); +} + 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 03582e2ade83..ea59f32adf46 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -4,9 +4,19 @@ * Author: Andy Chiu */ #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include +#include +#include unsigned long riscv_v_vsize __read_mostly; EXPORT_SYMBOL_GPL(riscv_v_vsize); @@ -18,3 +28,83 @@ void riscv_v_setup_vsize(void) riscv_v_vsize = csr_read(CSR_VLENB) * 32; riscv_v_disable(); } + +static bool insn_is_vector(u32 insn_buf) +{ + u32 opcode = insn_buf & __INSN_OPCODE_MASK; + bool is_vector = false; + 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: + is_vector = true; + break; + 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) + is_vector = 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)) + is_vector = true; + break; + } + return is_vector; +} + +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 Thu Mar 23 14:59:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185768 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 577D9C6FD1C for ; Thu, 23 Mar 2023 15:00:57 +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=MS+y9vexMeqnbpj7V1EUhfVD0gPOe12MhcgPSw0Eq8A=; b=NrD08VpNOJxjte AiiPm9gL08ec47jCjuo+QbXls6l3zlB0lx3t+7wyw0vbGqV89vBtmWByw9OemIpD+Qsm4J17bYPiF omAksWKB6RAF0LlfrNXDNjKFocj3rhRpuZDLflgTN1D4IDlweBPKoYFU44SKXyh5hEGggDu0RyD6F 5PVNMqOweTZtfGLAr0NcePexG0I8larXUEgTXjNZvvOrOLj7+HN0w8UYtZ+pkfJsDEmC7pQJP1z22 ux0tZDFHXHe2MitP5dYT5oWe6sJkky3OqXwAP1j7+HKrcvY4lKemYi66PJhNuGblFzsqEO2C0wsZ9 ovb44O7rONLhxvAmpaNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQr-002HHm-1d; Thu, 23 Mar 2023 15:00:49 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQn-002HEw-37 for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:47 +0000 Received: by mail-pl1-x62c.google.com with SMTP id kq3so10158106plb.13 for ; Thu, 23 Mar 2023 08:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583645; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=iCcObYH91oMWZ6bdmVu48SZE269reFVecBrOrqoWxGA=; b=EedgoYClEcxGQrd7CKPRbbltCB2tJHu+Fmlpu8Pyj2bwl7Gnf6pMQCHledQLM5pNGt eu/R4VHE3TUdT1hoMGOil5MG8hLG3yOvHuzb2/80pADS2gD8Ca8mYIRB6lydKe+RGB+4 5SkucqXXrvupapqLr7hGhptUAscK0M1iAg4gVQE8+Tfqv0cSu3I4WuzavtEIr06oClKn J4skEaVk7mxy/P1vj1hNohtrzMGtmUYqauqo7vdv7WGBtHAYtXgc1kY4wckCqbG3vL5J qVDEIQSdKBAtprO/vamwbfCszmuYY5ClsdJXXYAsnEdVQ0gut1Wkp7qirKDz3y0PckcO ePjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583645; 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=iCcObYH91oMWZ6bdmVu48SZE269reFVecBrOrqoWxGA=; b=i2vMmLPVgBqIVSNnN6x5Ai8plo4iQ8EnOiqAD3a7o+83+kgR+OCslm/8xHzaGFeNEN zSiJhbSZCMfO3/CJtGlTRF/TgsptYM6CXbXtXRt+KU6DrQM9HNIpG+ayumZJX/y6PpbY CPQvqOjWza66rNtVvTp6oC0m0vSIyWvwXh4QR1thfVXKxIfOztcmogT/MCx7fc8IfOkB 7PFtDI9+GaR9lLngvos5B+Net1J15Um33Ws/MfDPCwp+OsevoFcFNfYO9novWFZ1pRtG B1pnBU44ITvPQhuixRLtvK2coCRJ09EC6zi0DhxniS2qT59fSt9Sv8jqf3gmSpyGjpbJ nUtw== X-Gm-Message-State: AO0yUKVHbZJWbPw1a0/XorjENmf5TLzXM4hOSpFnqpISebkCUilB6sbN H9qTRXpGiSQRSZbboOSnAz2fiiG9TLXJDLIIV45Y8gyVra0i0o1yOQERKL/jGCQB1Vh/n5FGiJL AqDJtqsYQ5590xiONKyo44q4x9cVjA0DcUm75FLOXYg9n8ok3EigmQVUbX4VeRatOrNjqxIq9/b 1MkImW1SQWD6rh X-Google-Smtp-Source: AK7set+KLpJHA+/Z+2zEQnRhf/oukFKUa4LDaaL+p8lySAkLSMylITH3L3Yp2iGzhZB2T4tJ79F5NQ== X-Received: by 2002:a17:903:41c1:b0:1a1:b7fc:eeba with SMTP id u1-20020a17090341c100b001a1b7fceebamr9008680ple.19.1679583644813; Thu, 23 Mar 2023 08: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 d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08: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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Oleg Nesterov , Eric Biederman , Kees Cook , Conor Dooley , Catalin Marinas , Mark Brown , Huacai Chen , Qing Zhang , Rolf Eike Beer , Alexey Dobriyan Subject: [PATCH -next v16 11/20] riscv: Add ptrace vector support Date: Thu, 23 Mar 2023 14:59:15 +0000 Message-Id: <20230323145924.4194-12-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080046_005701_2532F80D X-CRM114-Status: GOOD ( 20.66 ) 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 --- 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 2ae8280ae475..84df5be90742 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 @@ -27,6 +28,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, @@ -83,6 +87,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(void *)); + + /* 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, @@ -102,6 +161,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 Thu Mar 23 14:59:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185769 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 70991C76196 for ; Thu, 23 Mar 2023 15:01:03 +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=Yih9YS3CawQpDTK8R6f4yC9tg6V/3TBA83gD4gH3Wog=; b=B6UM/BGWthMyNe HNfsAh+91cSC/MC/PZ5q6u/XFOR5nwIAiRi5sqg3vHgvJYiZ8xCr2+D9/UW5TeW/EM9yIiRMZ0unX MttRfbCx1KxciEyX88baF3zEy0J8Jy/7OS3AJa6UTCvrlkFevyuaKmGs8uUuSix21/4TXOa+TkCZF iIt8OlYI0wlyQSMtK5K6W5KTRzM76oL5sNkwzD+vwVA+DruduTasCzmstC/qZ6RggjToe4jSU269Q 5Pgm7fe7Hdf4fYw/ShChppzScSO5FtuJqCM4vYbWAebHCV72TOM9t2Q9Z1CtvOUs++XBWzCFA1Pv5 yGWuZ+CoYxNMqmolUnRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQv-002HLz-2j; Thu, 23 Mar 2023 15:00:53 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMQs-002HIY-0n for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:00:52 +0000 Received: by mail-pl1-x62c.google.com with SMTP id z19so12142573plo.2 for ; Thu, 23 Mar 2023 08:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583649; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=EnMly8Z4ZP6m5MefJgirtZn1/V9pgmnzrBMhkpUZtfA=; b=NVwTzlEK2Gds43FwU0cLpEZhn54YcxwlTCLUsSTxDTqUEUYYt5DIgDXlc9//y81m7s fQjoljnJpSYqIs3Z6oFR2D9HfihLmaaILuk0P+p1T+5QWedR8keaszunQbuE6HPpqRnp B4ZvAY9d76vZrs2fC27teiTCScazZ9RWCV0r/117ROvCL+3vXy4ntWPqkH33ivz9+GXa XjC8x7PzvADpGxpwOEvnuHjq9vHIXh1b231QpLJmhScReGM+rC6+YM2h2SN4rZLlUXpI QkWBB77kTUEK3znFg/NCe4+zgtmhgWzjnGkGHNrAVdZeJvF521DwNqr/C+veMAUixG4b NxAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583649; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EnMly8Z4ZP6m5MefJgirtZn1/V9pgmnzrBMhkpUZtfA=; b=U2OOfiYOr32lFPEbWy0SIp0MtW9B+m6xkmIcDpW+3nSXht7uwajOC+EnEq3auvWIOh a8pKjY69KsSt6jY8Jz33aK/Szka1/iT+kx6NincTSLhvVYPo4nxl8hxx190AhosyRHVw YZmbeusV2HgANh45ALQaJM4YtqnOV5304h07krJA59EEABuNS4fGn4F8gwGf2g2ZMY0P gwOba/7kU7n770q4Edo/xjNeYMwzU+8wOOeVxqM69m9cTHuvtSAhVsYWhr4z0y4740ha pqJWcFd/Fkn+RDu0RnblksxYIdwhzhypdPe6V3SIBoJxUzUQ1Gca6ckOJHcYdztF+hb+ HgSQ== X-Gm-Message-State: AO0yUKUAk2cclptpXttYDTQ6yFnR31uGMgTFDZK7iIneDbefYmn3jGID Mm3/ykIJfjOTrh5A1IlC0HT7VWEMbbG6AhznE4hi8Xl2B85K/qrmMvEqiwSnznfNRWsopNxiD6E lVjMaK0cTv0/19G0wDfblk2OPO8s4mvlwEd5pS5iZb+fdSyz4pjJLRCL2nVP+SRBPjKb4Wpg3lk UtFYhdZwisPUKO X-Google-Smtp-Source: AK7set+DABhERXjaISFRv1JqomxsZIjenBSeZXajHTFO9RP7x7K96i1XQUT6LqKW+/vqMeX80/LJMg== X-Received: by 2002:a17:903:1389:b0:1a0:485c:a6c with SMTP id jx9-20020a170903138900b001a0485c0a6cmr5821899plb.8.1679583649117; Thu, 23 Mar 2023 08:00:49 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:48 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Conor Dooley , Guo Ren , Vincent Chen , Al Viro , Andrew Bresticker Subject: [PATCH -next v16 12/20] riscv: signal: check fp-reserved words unconditionally Date: Thu, 23 Mar 2023 14:59:16 +0000 Message-Id: <20230323145924.4194-13-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080050_290279_0D4630B8 X-CRM114-Status: GOOD ( 17.51 ) 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 --- arch/riscv/kernel/signal.c | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index bfb2afa4135f..eefc78d74055 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -38,26 +38,13 @@ static long restore_fp_state(struct pt_regs *regs, { long err; struct __riscv_d_ext_state __user *state = &sc_fpregs->d; - size_t i; err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state)); if (unlikely(err)) return err; fstate_restore(current, regs); - - /* We support no other extension state at this time. */ - for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { - u32 value; - - err = __get_user(value, &sc_fpregs->q.reserved[i]); - if (unlikely(err)) - break; - if (value != 0) - return -EINVAL; - } - - return err; + return 0; } static long save_fp_state(struct pt_regs *regs, @@ -65,20 +52,9 @@ static long save_fp_state(struct pt_regs *regs, { long err; struct __riscv_d_ext_state __user *state = &sc_fpregs->d; - size_t i; fstate_save(current, regs); err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state)); - if (unlikely(err)) - return err; - - /* We support no other extension state at this time. */ - for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) { - err = __put_user(0, &sc_fpregs->q.reserved[i]); - if (unlikely(err)) - break; - } - return err; } #else @@ -90,11 +66,30 @@ static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) { long err; + size_t i; + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs)); + if (unlikely(err)) + return err; + /* Restore the floating-point state. */ - if (has_fpu()) - err |= restore_fp_state(regs, &sc->sc_fpregs); + if (has_fpu()) { + err = restore_fp_state(regs, &sc->sc_fpregs); + if (unlikely(err)) + return err; + } + + /* We support no other extension state at this time. */ + for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) { + u32 value; + + err = __get_user(value, &sc->sc_fpregs.q.reserved[i]); + if (unlikely(err)) + break; + if (value != 0) + return -EINVAL; + } return err; } @@ -145,11 +140,17 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, { struct sigcontext __user *sc = &frame->uc.uc_mcontext; long err; + size_t i; + /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); /* Save the floating-point state. */ if (has_fpu()) err |= save_fp_state(regs, &sc->sc_fpregs); + /* We support no other extension state at this time. */ + for (i = 0; i < ARRAY_SIZE(sc->sc_fpregs.q.reserved); i++) + err |= __put_user(0, &sc->sc_fpregs.q.reserved[i]); + return err; } From patchwork Thu Mar 23 14:59:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185770 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 79E1CC761AF for ; Thu, 23 Mar 2023 15:01: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=676p0Hp4oe4zqqs8aBrdQZ07U5DEPPZk2XysnUjsPUQ=; b=Ja5QVo+fEszsli 9p8I1iBIefqdc3r5k+ZH7Z8NaIBb/dzom++jraJYw7dNDcTIZPk5x1CGbRvbZh3C1Utjypdjk/AIr 2UEn7zhneJQHjbY7YjMl+RoBs0uak5b31HoVfzUwreTE7I2vT6FlitdP0ry/svzUGGckK/bFwzaq5 9iPBgt/1+lJDY6bgKnqMxRuehiYk/i9vpvDlHsETCLEAxaSrfzGebpwPJsAR90ytRsPny4et0iLPx ifj5kQWKpm4Z6LyQPJSAIGOsjHsmVRLp8rqIztN2VUUinVLwsrxDpgRdeSjs2JnWGZDCxHhzkNt9h GtNCvyuf0cOsz9LeuDUA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMR8-002HTl-12; Thu, 23 Mar 2023 15:01:06 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMR3-002HOY-0E for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:05 +0000 Received: by mail-pj1-x1030.google.com with SMTP id x15so11371877pjk.2 for ; Thu, 23 Mar 2023 08:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583657; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=U1Hhma2wTQlRx1y4u87lZR03ZphPL/9rJZHy+bfOkMc=; b=WA2RAAQ0NQcfjfF5KyMEAORoxc6xw8LuEmn7YYtQ9Mynp0KGq7irnRa2cvlI9s60eI YG/aqH4mYPOCOJ/WFjeL2GcizVuqF5Fn55TNLvnXnDEYy1KFKx2RHInv+sVIMm0FnqM1 +rB85uGGSmq3h0U/BOY69KHTcoStx0oK/BNR6TyTI8XLaqnAIEaogqaPkItBO2lbrFSa 24Ezw1UkCzFnBdwvIMu1pwxbRSxSLtp3KTQZndLidq1nsbN50JAWnx0J4SzlAqW8BWy1 hycGsYIP2Is8i58O1ErFsqPFSuiwJVwjY7WH4SUWnuEIoEOrTqK78hoPq/h4LaWPR974 713w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583657; 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=U1Hhma2wTQlRx1y4u87lZR03ZphPL/9rJZHy+bfOkMc=; b=LhW1b/Y+WkoBduUfcwwlMU8MoVCw21wUi19zXY9M0xYrjsCAz1gwF/xs+i0HO1Rv6W KQiE6n9cxzThNhkDVFd3upKx70VkcYuR5YpagG9MJT0O+7v+FLIYUQiEZDOPmuhsQvbT /BHvAahvfJcf+2T5cbAo7DwP27hhaAbLTeX/zfojcyR8/ZFpkrlluA9b55nxLNocPefo 8qKx/B1syXvTRSXRMSdzmOqz5XhxDbxjsAq3/Tcmt2NP5UJcQUHASDOgtBHOYh9lPWdv gxBvRK7yFlayRlI/WDbWI/GLIf+LPUtNd++GyCTudejROedIBGU4lHVyZJg84K4QAyhI QuRg== X-Gm-Message-State: AO0yUKV+eceLhWu6IIcSL6n8hXYnKzskCWkYj5kOHjG/kRbHXIAWSI6Y FzLFJver5rwzRiOKfHZ4NNLOsOnC6IuDkem+m1LeAKUNX0cfQeL2gP14crHBnFzPomAU+geD2dX SdW4Ru/nXzWGxXgmOt8sg5bOPRNdAVFcWeS90y8Nwgq4dGGc8XhBO2jRiJ7zKkA9oFubbjH9ga6 KEEyMwiSb/Mg6c X-Google-Smtp-Source: AK7set/ot9fcFG6VsWu3NmpYERYzs176uUJflk4kOMGQ6vcn1ddJP/G2V6s9w3jSm+jmhwz7e4HUBw== X-Received: by 2002:a17:902:e1c3:b0:1a1:a800:96a7 with SMTP id t3-20020a170902e1c300b001a1a80096a7mr5500670pla.8.1679583656975; Thu, 23 Mar 2023 08:00:56 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.00.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:56 -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 , Conor Dooley , Heiko Stuebner , Guo Ren , Alexandre Ghiti , Andrew Jones , Wenting Zhang , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Jisheng Zhang , Xianting Tian , Andrew Bresticker , Al Viro Subject: [PATCH -next v16 13/20] riscv: signal: Add sigcontext save/restore for vector Date: Thu, 23 Mar 2023 14:59:17 +0000 Message-Id: <20230323145924.4194-14-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080101_113498_B48771C6 X-CRM114-Status: GOOD ( 31.60 ) 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 --- 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 eefc78d74055..53b17839d606 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -18,9 +18,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 @@ -62,12 +64,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)) @@ -80,32 +157,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))) @@ -139,17 +265,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; } @@ -174,6 +305,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; } @@ -182,9 +320,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); @@ -338,3 +477,10 @@ asmlinkage __visible void do_work_pending(struct pt_regs *regs, thread_info_flags = read_thread_flags(); } while (thread_info_flags & _TIF_WORK_MASK); } + +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 Thu Mar 23 14:59:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185773 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 EDAAEC76196 for ; Thu, 23 Mar 2023 15:01:33 +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=mHV0Et1Lof9Yhb8+dJw02tlFt07gD4/0Jg69PnmIUdY=; b=eWKIIoD3FFyAOz Gigtx39Ta5FpYOlUVIc+XuAqH9+7A5XZiWR40TIW38yZqaWQVLKywwCB/An4caPzACmY48SaCn6LO jtXt4vEXN4rKQNhbwISWkmruWfVWREsvDDK5Dbw/JywiYs4dS0w2ufN/fBzMossM2xW0ykNXLLHsD 8+t9feRE9fCJzQEAV8cpVGnd2cE/bvB72EoZ9yfR2z4rvMj6+VcKGoGzwwKmBn5xPQpk/ihuCGkPS Vqctn8V8pwesUnu0bQ9WJ58M718AWn0K7tY/rgMOTVWCbMhK98itEILFw3tLA7KR+0A8ZrFj2HNWq Lhy/DxmzQ9p3dpO0/+tg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRE-002HXV-0B; Thu, 23 Mar 2023 15:01:12 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMR8-002HEw-0J for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:10 +0000 Received: by mail-pl1-x62c.google.com with SMTP id kq3so10159384plb.13 for ; Thu, 23 Mar 2023 08:01:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583665; 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=tw8ZKuKEPT8AsBgdKtvPSORjjdmidJuqaPi4DZ1mAhU=; b=bvy7I8TUNU8naMd+Sfj2fslrCHGLNUMYZmK4vqGpgAllweyGDZG/ntUpunVR7XmcDy cFmMoL64R0BJzlawqUpuiG4fk2qVBThebJPGwUwLXmG6EE0AeID7RulZTQJJ6PebQbXd PSCg5hBp4Ic9MUA1hIjJ4uyxUbiAEYPqAxAQIpDuTJ5Q9tUdN4sbAH/K+WDJk4gNk7dI ZpfZfEAwxOJziT9CUh3RBQMQ54UyRKMS1MRmVqI4lk43cMMboLlPWNyV0nT6NcE6nKOF JjOjjckmn40wuL3NkIXiN0jC8NF+gTrC5XyKCB+ByHhOudHv1GN6+nNsJqEVbpR38Cby g/jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583665; 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=tw8ZKuKEPT8AsBgdKtvPSORjjdmidJuqaPi4DZ1mAhU=; b=dBMnK8cozMptzThmkIUvP6Nz5w/bhcWmPEKii4rKzOULxgryjJwNFC8JX5bJuxvkdn Ls3vYeetK3ME9AshZIqKKoI+3LtUzNK2vVAcufE7iLRWa9PR4rTBWVttqdthsvvWJlTa lxhsCuLVHvNVFVOehwJjGVRkuAw1EfC+6iXBNofhJDQNLwjmCrd7IwdvLGwmecEiTg93 pHqHfq+Vk+z4rx0Hl3eft+N41GydR6utZLjN3PfwktK44KRLN5XhYMhnnsryn8To0LJt 0RI54+Wy8s69B2rzQa+FyVAi9BpoywwVHVw/ReI5BjBfHSGXQdRQDprlnGUZU9o1iQoJ nlnA== X-Gm-Message-State: AAQBX9eLYAOPjRIqo23XCC1DhEB3MzPejHIKIMYoppxH2nP3RaWsd0V2 gWhFzMc5i3yYa+2kWNJfn75Ci+FwR4LsPuzSYV+f9bFYD9XLIovsI2fH9Q6qy7s7PgvHv5JDaBy G4MkxF5l5ec/3CtrhRcF1YHLYHrRWu0FL3e1qJTfyUeHULH7GMyeQk89em1c8eS5WdKOmHGerfR CHsnNmgbMlrnWX X-Google-Smtp-Source: AKy350aaFJ1127aBTrhi4lqsUpIZSap+STJ0lRVnnhWekHIcI2E2Ot/a8mgbJ7WsyqB4nmBzy1fPEA== X-Received: by 2002:a17:902:d489:b0:19a:9dab:3438 with SMTP id c9-20020a170902d48900b0019a9dab3438mr7001571plg.2.1679583665088; Thu, 23 Mar 2023 08:01:05 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01: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 Subject: [PATCH -next v16 14/20] riscv: signal: Report signal frame size to userspace via auxv Date: Thu, 23 Mar 2023 14:59:18 +0000 Message-Id: <20230323145924.4194-15-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080106_141538_9BF4A639 X-CRM114-Status: GOOD ( 23.15 ) 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 , Nick Knight , Andrew Bresticker , vineetg@rivosinc.com, =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Al Viro , 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 --- 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 53b17839d606..6b8bf935b33a 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -21,6 +21,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; @@ -195,7 +197,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; @@ -203,8 +205,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 @@ -224,7 +228,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; @@ -320,7 +324,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)) @@ -483,4 +487,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 Thu Mar 23 14:59:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185771 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 554A7C76196 for ; Thu, 23 Mar 2023 15:01:28 +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=5kwd2C5keTYDJTgrHTfmDT1KF6JHreRTg5Lg/ZMX2TM=; b=wnfJnqTTTClBdf MPvN6jKpueMc52atyuTqdqvbLydNovdFjzhQpBffvsZAWRxlkA+k9i39lQ4EHPHTgozyxv0nc3Ffb WNgiKyyJveHxm82kr6BlRzbpLxmhGlUD5LiX0YzBAVWTBBKCihJnHhQ67fFH4UlAjbohcyKTEdM6K ZEPm4hUhf3pP71O9QXA87Z/A5F54ZslCLtbFII4F6gac6KVJvCILd5xgQ/HxBFakUhy2HMP8bqfD1 XtAD9V75Si956g/wE9wMKNDrOJlGsfFd3RBPyDY8W0JxFd0qO66bVAnCByVC14Tc2qWmThgOFemso CVqgcQC9xhRh/FL/mbjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRH-002Ha9-23; Thu, 23 Mar 2023 15:01:15 +0000 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRC-002GfJ-0y for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:14 +0000 Received: by mail-pj1-x1029.google.com with SMTP id fy10-20020a17090b020a00b0023b4bcf0727so2372812pjb.0 for ; Thu, 23 Mar 2023 08:01:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583669; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=U+ouetyhXDlSjJJrs7uB9nNBREilJX6aCD2bcZWQVec=; b=CTVAyxXuZQajxAGCxDiblOwdF6oTSft+FYeysyuhJ/h7OntJ0akZBDT1uUC6xXIhGt V59bRkFFoNHXVo1L5auDb1xzyzkowkZQ1JfG0iEYg9lMZiixduQPEZ57qSeNxIJ140HG FOwCekVIUcluoDRvWd6kaOL+d69HCMWpkGQfsJCfWAdferXxduhKsAvCSQmvrnDn63o4 wjGNdEX0uVwWV8BOZiX2plGjf7e+b19Vz/4eYr4rm7eOP0+KgH74Xiv8OiHafxzZr0lP eaurdK4tMrqmMLxi0Peq8riIOHb185yFZY6Pyd3qIELT5ivmv4j5X8AnOzZMdsl3DORE 8HZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583669; 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=U+ouetyhXDlSjJJrs7uB9nNBREilJX6aCD2bcZWQVec=; b=GbBETyhGw58ELfh8Q8OVjn/ekpmBa8vQSFUeWC43pVQTI5FUk/iJqiz0OaKOkMGvNT haNWMWo7ly1Ui3nqLLqWL74oY0VRPQKJgM13JVvh5t3SS3cNeamJLMSLm8EGjKHhCfQN d9IlNNM4CSXr3nmopKlt9TKNMtHZnFDx5DD1Qb7t+iWY/zEMX2kkdx20Gq+wTqUr/AqI E/2UcU53WA7UeAyA5krPOzdKljNlNeXuCHN1DPPE+ng3PRUHj6Uh4SjAv8UpoInqG986 13lck7GzGy3cXoFUPDLyMMgKxC8fgIQSZkkiKhjgVnoAOdP5b+nBe9FFYEpCLMPT4l1i WPJQ== X-Gm-Message-State: AO0yUKV9Q75NiBOyXcDQld/rgeRvBABQos4drjgMp8ySnnUv31+qyLvE n3pDuScyAtBHlhh2s2et9IjN5GJBtrGR+5woLrOB9GdmQ5nge6Hq4laeabTSBtyDe3ehoSjzg/g Z/z/YY/YUjq++JUpMyaJMeL3ig0jFF384zKXTrvN3K9iqlT7Rw0ZAilpeCr3Ag8Dkm/Hli9YVV/ 2Vzk7M3Ok2QFA5 X-Google-Smtp-Source: AK7set8rM63JHpwDOS1Hl4On1gtG8fPUiROZmSOfWhxCHPK1Cemsm4kUqDHJHPMQawySJxqBigleLQ== X-Received: by 2002:a17:90a:d583:b0:237:c5cc:15bf with SMTP id v3-20020a17090ad58300b00237c5cc15bfmr8344876pju.13.1679583669355; Thu, 23 Mar 2023 08:01: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 d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01: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 , Conor Dooley , Vincent Chen , Guo Ren , Andrew Bresticker , Al Viro Subject: [PATCH -next v16 15/20] riscv: signal: validate altstack to reflect Vector Date: Thu, 23 Mar 2023 14:59:19 +0000 Message-Id: <20230323145924.4194-16-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080110_335119_95412A28 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 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 --- 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 6b8bf935b33a..ffde81cfadb7 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -494,3 +494,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 Thu Mar 23 14:59:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185772 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 41A95C6FD1C for ; Thu, 23 Mar 2023 15: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=TB6FxQQEC+5cFnAijbyIjBOW3veR4+3OU8Yx5nzt8Ok=; b=MgUv0MLdo1pnDG xBSdzuXlfYPpyX8FgmzSvh7qfvWdVHe660jYfi7EvB+81GaqNKfN5QownbrtHpLZ5nU1rAiY0ExP9 bWc+L+pqGeqxlIs1kn1sSIlvlWSh5NP76vKdOUJIqbWfTjMGCLmn/0EBtQWPgv0Mj5C6LhMj45TGc IRVrvwAnZmzmhs0ebzHUVocCTyqWA8cUBE3x8ewDIX6CMHtPgZHJSr4xW2KPK92cNINXlVuUvjmUC R12TNmyZjBkGJke0vjyTkD+88LfhvMfWAqZblE6zAMicffKfn3oMiMQAgW9hYbXOKpl5I2AWWYi2P EqEHINnLzUyUar4wZQIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRN-002Hdy-1D; Thu, 23 Mar 2023 15:01:21 +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 1pfMRI-002HZP-1H for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:20 +0000 Received: by mail-pj1-x102e.google.com with SMTP id qe8-20020a17090b4f8800b0023f07253a2cso2342065pjb.3 for ; Thu, 23 Mar 2023 08:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583674; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=2/srIeqtOeL8WlVYBrDSAnb/WyWsrKaiEJtMn58iONk=; b=WVySlEfxqDWV/IQZE+oLOQ0wO+duX+c8vFyXLiyJTO/x6njrE+NXSvUp4U7SbhlmAa e3vIJwBitYU7bQFELWAorOZWaW5Ygi61aiL6LAKJx3TKW7xvpiA3+or3+K+TiDyz09hm g+QyZzE/7LB+9xXQk7NZunq8TuIPP2iKcHm2RiHeMiAEKP7LL1QIEYhsg45Yp77co4uP YcRTK1ZEsYUfMs4g9mAyToSQLHgadRVMtO62rVhtzPwtgp/9qYO++Wv1sAOUBPh9RODK uCM5Y26DzOX7s3YDNZloU38f2UWvbFmt9NrBayRYHsiMxHTG/fCCWPbiXW2fy2H3mD3P SXlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583674; 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=2/srIeqtOeL8WlVYBrDSAnb/WyWsrKaiEJtMn58iONk=; b=GgL8X6WHBS6ZqjRRpuYhoV2kp2OODNZdx1eVIvwFdeOG0hkJlcXZSdz1cpDWCfiM+u tnH0DlN7Du+9UPhB50wbQHOvgG9gFgL3W50v6Csoe9FKkb6rURgXKdkYPXuOzFRwFFh5 IkBY4LyNYmVSnBKP9x2/3aPLyg8nQ//37Z87mFAL3uvTYY1cQRGp0GJw9OTeySjuuHqB DIAFreapTuxP/0Nu9ZqWbQVTJH3i3yzV7jM9JLmCTQ0wEBj0wwcBB+nSLA0+GiuQwzFH aH+IC1olQsqyOcnDztJCoBe6bmt5wo84eZfIN0bH+lWa+JRHNes2S5qUcGqkkyzngfUx tSqA== X-Gm-Message-State: AO0yUKUofLUbdy6d3OtTJdIY/wr+RlYw78fJBx0KFS5h8GSlHj/sYyxa lNBswsjgyFb7Y34bRbBy1j7JDN5dtq7d3jAnK+ERdmEODHDylh+nrnP2GnUc4OZHv+f7XFQ0nQb 72IE7RFCFc+qoI3Vl3nK8qq7s4DHaXsDsA99hZ6ofdpd1GstAfTW8sX2wHVciZ+gJtlTofhYL5Y HYVtun6LLY9MvY X-Google-Smtp-Source: AK7set/L3cVGLRza2ds3tB3eAlc8YjsgRg8DqSZzrpSTVdPBTBMhw0vBkaTe8IhXbUtRRWsR0LQz6Q== X-Received: by 2002:a17:902:ecc2:b0:1a1:e01e:7279 with SMTP id a2-20020a170902ecc200b001a1e01e7279mr9171143plh.4.1679583673757; Thu, 23 Mar 2023 08:01:13 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01:13 -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 , Conor Dooley , Alexandre Ghiti , Masahiro Yamada , Guo Ren Subject: [PATCH -next v16 16/20] riscv: prevent stack corruption by reserving task_pt_regs(p) early Date: Thu, 23 Mar 2023 14:59:20 +0000 Message-Id: <20230323145924.4194-17-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080116_447848_D4F4CBF3 X-CRM114-Status: UNSURE ( 9.32 ) 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 --- 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 Thu Mar 23 14:59:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185811 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 6FDC2C6FD1C for ; Thu, 23 Mar 2023 16:01:52 +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=WlKkYSW6y1jJ7BibbTu7JX/OdkGuFFeZMYgvEL01RA0=; b=UF6E7fHrxCsb4A 8wQh9aomWoC8je0b5eICaXFJb74VU1YxmsDj2Tp1aWzyEHpOa58E/ZQszKTBs1NAzZ7iXdNwWCY8l mDIac6CKiLgvRHl+96KQdg2Gu/fnGUOWS8e6KNKjmIKmeY9EQyjDiohAmQC90B8cH9nnEa0HVLTB4 8ZiDMto0J/+W6kNSPAvODek4vpt5xmX5DdD2r7pOGUgCv2PSo3t4GyH9JX+jMDGJ/mbm6rJqOQGt+ fVWIKj5LUUle2Osz7l9ktizxHkCNueulYIx4Ud4SDO6VLNGdT+69sBiS2K3Pe/g3ePm0RG2ocfk84 bm1REj6NNQTj+9YhhLqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfNNe-002Q5Q-2G; Thu, 23 Mar 2023 16:01:34 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRJ-002HIY-1i for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:23 +0000 Received: by mail-pl1-x62c.google.com with SMTP id z19so12144210plo.2 for ; Thu, 23 Mar 2023 08:01:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583677; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=JozESal/Fbpp9RViTDB8Qd5//8qhpKIjNlNXSseO97c=; b=GLZYv/aG+Ac/8djv6J4vsr0ttPd9TwjukVUqCjcfy7McPcuKFVHuqGy5w0iYCgH0ve YRQhpVGOmVc4RurKC+5esLk2PdcLbPPFidaAN9Hp9u7ozr0XoOlEO5YKecamglCLfr+Y 0p75r2grShf5CGdEDVy65zHfCz86AqYD4ygkSKKuiUDhhfLFNWWY084z8ZwLFhwK7ids R4w6yIbHpa2CiACplfWPfzv1nDfk4gnJO6CPCZLH+NyTylJ4Hp0GCsWVP4mbvnI6d1DI CUTL7UkrdUSPWSfpdf1sI1Pw9RDsFBUuHaQhsOWnoxF7ZuJn3wxmIYay6txUTs3gwkO5 hNyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583677; 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=JozESal/Fbpp9RViTDB8Qd5//8qhpKIjNlNXSseO97c=; b=QZkAtjKEvT3846jfhtS6KxcMdIUPO7rEojJDdCfL8HlN5DTYSo3zJB8Zmu4oI+3k6d Gq9gKKl3CtOj3b/E+GJLpxrcjguzz9VSamVjeTIh54doLZ+7kRUHb6sW+1uLTeCZ3KcZ +qYYc8tdDrq8aP+QfEuzUhuSGc7R4o/sJycydUUoJm4lCJuuO8QqNfY3XkUtoxB3xD2V +gUQYvCLgMZVNNz2qKqXzSYTt0twVyBnoKUWtZxBMh2gDK07MfFt8b18mZTcZw4GC21R YmT9O5lsjLObuYsqHMC4XyyE5O0SJYFx2HjC4+1S3nBQj/GsXJo4YrjyGcprCFo87k9Y zUoA== X-Gm-Message-State: AAQBX9cBpCVqM4FX4sHnlA/Nzmxs+enW2M1u8gr9gOvW/kHbj3pjG8Jd jk95gXBXn7FUPSEX/VvRZFl/gS+QwN03j3lvYq9gHaiD8YsPN8IcPJJJKY7Q46xrMGdgQyqRQGt 6MsP+zVkrOs8fEjosvlBjj4IXdK7OLDXdH8Rj+OEdnxcmihZWTiT0SWetiKvq3kgJJUsAuAZFsX N+hlMY68ZQo5Mw X-Google-Smtp-Source: AKy350a6Nkugs3BdGgoNETgzYze7cy/cXizByVIZO8sc3UhB/5O/9VTlWpWOJUEXeJgtEB1dDswOuQ== X-Received: by 2002:a17:902:e5c9:b0:19a:7217:32a9 with SMTP id u9-20020a170902e5c900b0019a721732a9mr6934775plf.26.1679583676752; Thu, 23 Mar 2023 08:01: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 d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01: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 Subject: [PATCH -next v16 17/20] riscv: kvm: Add V extension to KVM ISA Date: Thu, 23 Mar 2023 14:59:21 +0000 Message-Id: <20230323145924.4194-18-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080117_567008_81C754EA X-CRM114-Status: UNSURE ( 8.72 ) 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 --- 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 Thu Mar 23 14:59:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185776 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 95B5EC76196 for ; Thu, 23 Mar 2023 15:01: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=UGM4qn1eNzK9Sqs7XyMXvVzMQTwo8IWcwbo3XcWxfVc=; b=lEFDNSbX9w7v5E 1m3q1zv8MppxV8konMuSYHBwmDPKMgtYHyyzZClRSgyZRrAir4nUOPwpehz44UKBsg9sXa9HxEp8C bS3FB3PxZL13DYMQi8gtFth8Eia8b722+BQ1SOJ0X4SFv3XCvQQyvF3BISryfFv2fYCC9fx8dhHA9 Kk3ytfGQJB/fnFJ0GgTQLgeNrWqCvVhQiIMQm33iE+jhtIL7+ZsIhv8XGM0yLz3v0/xoqD9UKevJy t0nISpFdV4EMtzcJNTe92vfiaWthSSykkF2vJlQ8lfA37dZTEp/Ndzbyd9qH8W/kUtl+6RDPQxtWa S9lFa/sK4fSbrdH5oXHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRT-002HiS-2A; Thu, 23 Mar 2023 15:01:27 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRN-002HVj-2I for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:26 +0000 Received: by mail-pl1-x62c.google.com with SMTP id c18so22540655ple.11 for ; Thu, 23 Mar 2023 08:01:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583681; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vAZY+/XOqMaX1ExO9L6qEYsxav+5DNgQkYhkRbyffRQ=; b=Lg9vp/Cvikdo3jVtMbkTK94zZMtaV1eH+pA8ukZ+1h3e5WdYwvgsv4u2gH1DI11w4W /v0MM4xf0awzlw1Rg7UGU0fVAtAtt1EIv8Cg5sDZA3OQZ9ylGkeo3nQLAUZXeC1HRhzU YLXVlR5mcCNxGPeLPixG3eogTj23pXnOC6SLO54RH8f2RVoT8kOM9KuQ1rSPmysCEv4h xYwBNO77F7SyBm5shSjJV8fAdoL2CQ+YdrSs1GPDZt3o977ODiKSEN3brVUFEhraGtYX WdQoc9WEgigSyyjTTqQqfrKDevy6kd6N12F2KJ1xNlD+o21dmBvvrC+bm3SV/1aIkrTa LVbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583681; 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=vAZY+/XOqMaX1ExO9L6qEYsxav+5DNgQkYhkRbyffRQ=; b=uxVU8Tl5C5QpuRWxEGXO0CpSIJKylKql+wLapu7NSO6r0iyq41hpiz2Iy/Xg2aUVn7 J8WspjBwZ0qrUPLGmdOX1+IjU8u/CffN+n4/kSlrNgLCMbkDTdQq5CjzA9qXpYZ1qthK OrrOkif6nn37PNsciqws2ccr3FFolcQdgajtLHYcbCbsDMQo19XRHCrFGu7nHiWaEAIV X4KW45esmKaKyUotSOVg3YpZljVufW0jG0Lnli6Vq8A8g2pwntbUNo/nA5O7i2dGPetz cJoT1G9yArWb8gcKeHwhF5bmUo/cZ13n6vmlSq0jAPlgX0t4sle6bO/zitD4iX6KH9mL Jadw== X-Gm-Message-State: AO0yUKU4NRMxo2LlZrf3OHoNG2PX42es2dSPJ8NPSRyL+9w/aMuniNLa nnec1yOBXKuVKo9riT016twJWribJefqj6awSqaH90I9h73uN3F3aRSmuv4BEI3wwIi9Tc6yVOp N/7+maTLsWwctmrEVtgPB4eFxn2LA8AEKWT3H1rylPE682XUEJhHmbTkqFV2PxkxEFtlude02v3 Ju+fidD8wxDIRW X-Google-Smtp-Source: AK7set8aCy9KnzdYMwX6JG5zRYT5REVZHp9YHk6yaXnQzlkaLDQ4PnTQ/nNUgNLAbq9pbijZ2Ydygg== X-Received: by 2002:a17:90b:4d0d:b0:23a:87d1:9586 with SMTP id mw13-20020a17090b4d0d00b0023a87d19586mr7787683pjb.23.1679583680726; Thu, 23 Mar 2023 08:01:20 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01:20 -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 v16 18/20] riscv: KVM: Add vector lazy save/restore support Date: Thu, 23 Mar 2023 14:59:22 +0000 Message-Id: <20230323145924.4194-19-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080121_758008_6907C74B X-CRM114-Status: GOOD ( 24.46 ) 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 --- 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 Thu Mar 23 14:59:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13185775 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 A15BBC6FD1C for ; Thu, 23 Mar 2023 15:01:49 +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=F3n0ZckaFobdrkcZaCtJgMDvlM8OIV7ocDT5IEXfAgc=; b=DPQEVFBq5kwAFk p1RDaZ8oQBATPFZJJ7x/NC7i6bRGQRN5ZuXG2t2OZ/4pr/JF6EluFvSoQelzYJQWJvUBi0Qdru45Y Zj9B97QK+E+zGnqwuLLZb+sqvGj5WXVRztsKooi7xPhFBgHp09iPuiWBtZNEaYzyfm0dfOEXmphGY XRfs5mPLTqksP5dtYjVoKe016OwSbo4t3r9KQGYa/lRXPiEc3yUVHXgg7s7ISOemzY9Yw+MZBwWEH Ft+JrBNE6+k5AGwbEg0M0R+umJ/HSTbpJbWBDF/UQmjm4JUdTDkzueF/ks7mSzujjR2DRYOGEXKI/ f1unERyJTEdhg/mx78AA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRi-002Hrb-0R; Thu, 23 Mar 2023 15:01:42 +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 1pfMRg-002HqJ-2p for linux-riscv@bombadil.infradead.org; Thu, 23 Mar 2023 15:01:40 +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=BYoTKoh2oaNxCYlsCt/bFwPgHo97od34r6h8Ka1Tses=; b=YbYwpD6rQ2Tt+IvJnZyAUAQeru TMdEbbNTwCX15jyM4M+Fw6x6LPERowz8F2tR/TeqH/brr6OWUNfYl6BBD4KGU1lA5WDr/Bi3XkPMJ 82Cj8Q41jIxvXVrIMSXz5SpNYBGmHTEX+sCajAyXfw0a7h7siQQRXzplP6iyxwtYelMxytJIVRIod t+zmxRdZsr/7f3YzMduEARzMT93EcQUGd4o8wl0531UNPLP8KsLHDzRjwMDoEfbe38M70FMtzMcYc rMtEQxxPB2pyW6nFUOzev+eJn05V44Vm/yvq4NSOCGpTCVuG3+2WPhcD3YPVQanA8PoGDmtOuhnKz 3XWCO7SA==; Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRW-004t0b-2Q for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:38 +0000 Received: by mail-pj1-x1035.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso2246136pjb.0 for ; Thu, 23 Mar 2023 08:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583684; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=BYoTKoh2oaNxCYlsCt/bFwPgHo97od34r6h8Ka1Tses=; b=DqXgOrNCmgxCkO3fN35n3mAne+CXZvia4w/MJIDLeN/j4ng68vuqyGlut4wg/LgD65 FMoBupViafdgAxEwWtpxM1jGxDs8Eq5TfJH39bZPjjy4XjPw6oJYMPdWqcNAI+Lr72a+ z+vlDU1MLIVbrE25YIrwwYOY4qyhqpSTOjqrvVVUUjpmD5khBvkAhYAEQIq57s9yRQbS 96R+DnB4++T9fg/aa+CjPJj+RhwLcORGIxw///qvNgt6pZ/tXodW9FicF4J3RR2Htfhk l+VMx+IsicS05FfKuYe/EDLu6mjD0IsUdCUPUuUOGxXWD0atth/9Uj3ugHrtUxbJOJ1S Ja4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583684; 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=BYoTKoh2oaNxCYlsCt/bFwPgHo97od34r6h8Ka1Tses=; b=W3+9juq95fgwnsBJgEwJs5N+P1Z9dOm5L639J8QxWC5COUeRQidcWh1AC2qI0elvy2 eLhiCIGoR6YcWohky79mu51rRARMNEgHcUN6IDNy8uuP+k/7v0FEe/sAMYen1kCySDwu xznXuVkA1K7n95z+1ekY6eCKK3dpbyP7b225DKS/zJRH8Si42KEutRa4dJ+1e/f/CSsv YR2K1EbMk6F2bvq5GAMaeqk3rf20h+AQZ/whFTYDqNk/CCXv2svvcEJJm8vbW8tIlWFb sAKzIZuciUKQiHznHKmJCESv0i51m/haAOPhDrQcvwkvffgVtzia+3ntXFHNhdQ3uVcC lXvQ== X-Gm-Message-State: AO0yUKXwMWpnSWjpCZycRGgOVn7gotT4X5pveirUO3HbdnAlfZQ0zZif 9629C8uu9BJgT6TqaHK6wmShbmK4OtjRifXaOxwRMA3edXdfoa2cfXp/NfxwdYgDEUg8SROM0Jx XDr4+5TSJkQBc+yepIbZupxJcEo5TiYF+cTMXnrginZ3G1tS7GWpULDTRZ5pP/uyi2cB4jDXW4E exzogKQ482mlp+ X-Google-Smtp-Source: AK7set/RqhPy57RVMl+b0k//9zMeEBHGG9QTpL++qz91v0jRmKZ3Kkxa9Cz2bI1AWk/Tldw55GeoLQ== X-Received: by 2002:a17:903:288c:b0:1a1:9842:2020 with SMTP id ku12-20020a170903288c00b001a198422020mr5872275plb.43.1679583684117; Thu, 23 Mar 2023 08:01: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 d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01: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 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 v16 19/20] riscv: detect assembler support for .option arch Date: Thu, 23 Mar 2023 14:59:23 +0000 Message-Id: <20230323145924.4194-20-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_150131_368586_353F65DE X-CRM114-Status: GOOD ( 10.43 ) 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 --- arch/riscv/Kconfig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 36a5b6fed0d3..4f8fd4002f1d 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -244,6 +244,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 Thu Mar 23 14:59: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: 13185774 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 AE112C76196 for ; Thu, 23 Mar 2023 15:01: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=mKS71QHu+BVDlT10/XKukzXelczkg107SD3eEsVZ77E=; b=ibxKiWac3Q6xL1 9NDcUUKHkyCi2XXPndHUV+qvDtxJGZfuTcdHV5bZo2i54v6CndhwSRhov4vMnqKYXEGLixWwp0c0A 6/UIUS8983+twzPt08nHItv/keeTizNBTHULOGs/eicVutPo2+UybFwmuoTtZdutKVikQyWcyilUf fWStFJDV0PpHLAOeMb9CWDV997rKHo+C0eUSPf+HeCOknEKqzvRfjdMK6y44t7EnLk88U8LS5hnj0 Z8339N4A7VzWjAx7o48+XndLXC+aDsnRnbgnErnJ46PDVrWYgIh9RuMDTbGg86gL8qM9TvgUHa5lr OYCFFW1XKrCRgmpDt84Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pfMRZ-002HmP-2i; Thu, 23 Mar 2023 15:01:33 +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 1pfMRU-002Hir-0m for linux-riscv@lists.infradead.org; Thu, 23 Mar 2023 15:01:32 +0000 Received: by mail-pj1-x102e.google.com with SMTP id o6-20020a17090a9f8600b0023f32869993so2220758pjp.1 for ; Thu, 23 Mar 2023 08:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1679583687; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vtoGGjKeMirMGWzivkA2ddkX532xxY9JlIIZ7C9ExVA=; b=LP/ip08TU4mj5NiGXR+IDitS9xcZ1lrJxRBqmYgILXm/2WMj6AuS7ovyakecDS9cah l0rkl15XxJiJfG1UqvnRw/+BAWAnppVdkzjFU8Zu8FVB+lWTZHWgY6SDKFGSsxUUOOMB 0VHGRGiOwcQgNzEJaa9LaNYMT8BxDNaZ9rKXpzIP1asHGkFMWOEAqk8waZgrOnvDa7Sx vkmJVjA/9CRO6gv0aj4oTv6lTWMaE2psbeTBGXEZwk84BAGsOg+MWHoKzwX7aGv+NyAj RM+gRv7r8HOGdqyjLhWxOFhB1OmPgkdg++IDwfqJH9h03f7By9rIQUiCclMhLICHBFiS iAhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583687; 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=vtoGGjKeMirMGWzivkA2ddkX532xxY9JlIIZ7C9ExVA=; b=pydUankF2r4yva0taXbCALkXzRTxYybe48I8kVPD6wFTvtozRoayxWoP11983YSpDx LK66q0xNyncINwXArn1lIg0Yf+tvk/58mqKOJx389s6ZEo3cpjlcopYPGSyfwHEhoG5L aXD95NZvNa3BE9HxKYR75skh+NOdN4U77PQSuluX8Nuvhxe8vMbIuudgMd2QpwsKsHOq N+4aRVG3RTuMjODkycd04cOOQvmpteZ6NBEgRmo+j4AbPq5vBFF2G4MacXZyil1IHJRu Mnxy8DWtEDmdJs20wy1NZ60nQVCIri29Dvpsi7A9yrlPqmHGlj9kK8PCb3zgbG8Wg/ud lAjw== X-Gm-Message-State: AO0yUKWpQDQ68QOEqYMSV5yfW7pK/8YjO+exN1GD4dSvNHPSN1yuZC9z XcqEVucpl8lpoUn7DGyF4VGTSvqQykSFcJystWjZfYMoePUfPo3E1oYB+zp31GelfAKvZkaYFsG Jl9vLU1DBg4IsqiMv0g3wuNeGJSrZvYByYG1/oj5RgczvLEnrBEFvAQDumTF0wDRAsdbadMfLJb WLrQFNQZCfQEE6 X-Google-Smtp-Source: AK7set8RGVbK2oSYU+p7FyEM2ztkMqRmYkTbXOwkaqPpCfw5FseydH8YU6eaCSf0Vcc60jPrXr193A== X-Received: by 2002:a17:903:2846:b0:19a:a9d8:e48a with SMTP id kq6-20020a170903284600b0019aa9d8e48amr5745288plb.22.1679583687163; Thu, 23 Mar 2023 08:01:27 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d9-20020a170902854900b0019f53e0f136sm12503965plo.232.2023.03.23.08.01.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01:26 -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 v16 20/20] riscv: Enable Vector code to be built Date: Thu, 23 Mar 2023 14:59:24 +0000 Message-Id: <20230323145924.4194-21-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230323145924.4194-1-andy.chiu@sifive.com> References: <20230323145924.4194-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230323_080128_284476_19C91850 X-CRM114-Status: GOOD ( 10.66 ) 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 --- 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 4f8fd4002f1d..0e0377b5319c 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 6203c3378922..6f32c0ab32e3 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