From patchwork Tue May 9 10:30: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: 13235592 X-Patchwork-Delegate: palmer@dabbelt.com 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 19983C7EE22 for ; Tue, 9 May 2023 10:31:26 +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=l7CymzciyCyGUd Aq0IFelYb/e022e1CdJM9qGcSLciqLx+YLq+RQEnVxzoBLuS/J0ui4lYMIzT1X74+wL7kuSYrsdhS S4C7kei5O2o03htJDVD7KOJ7Vpq6/HzLXiAXco+fhndQap+nT78Y50gmLOpY/09rdUrOJw3WvfQn0 7+SnpPkdHXJn3gcV+/oP3+LZY0XapxF8+mqeiOwAYN21UrzO+ZoF8DsbsezVbsQjK/OZQKlAB93Lx nPI39Cg6Tx9t+V9IT3kUrAry60+y/fMbD23rkzCm4iJGBZVJhKoEezFrJTOESAx9etGzX7t4o1Pgn Rz/XLPq9LpoePu/OzgoA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKco-002seY-02; Tue, 09 May 2023 10:31:18 +0000 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKcl-002sbo-0z for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:16 +0000 Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-52cbd7e73d2so3267673a12.3 for ; Tue, 09 May 2023 03:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628273; x=1686220273; 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=du0Io6LpcTYopchyJqH+rRocOYaZGCdablSccjAYQc49Ba6S0Rrmyl+ci/Z+nxxPQw YaGBoKgHS8g3Qaggx+BYSu+hG+hAq80EgnZUR8Ct3LhmNYq4RlRrKbL4AwRUdiHmIlkD OcFpHCYfFBsM0b1GAsqbvXLC/uMZqsaurfdT4Zi4oZieblml1tYf+lAmm06k2j/ghmRA nZJfoSzceS6dk8Uj9dFuPVrcmHIow3ct78SnSgotzluof0dvxdUaahq1BZuj286W1pZO XxyuRuSDnvPxZj5Nc+Egn1JuGzOsL+9sOHwXxobILy/DT2BmJPKbef9R6okXZQ478eGF TGmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628273; x=1686220273; 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=MyyJ1bngJMvDrbnbIFV9tUu9qNmoauwtTiXf2jUnHkcRM4PoWgmVZ06Y+Jy+Yp4zPh UxA5r639bLjB74K1FUujaybM9W4jjYSE/oW+PpXnMBnscvDlMarMW0Mlmwh4O2LkWVQx AJT5+9RykP/KSPNaJ0ugHyMiHsCDYNntAPnl+3Fn2LvAgZjSfpOpBBUcI1FAGOI9iKmb UUHo8kNQLIk/LH01tev1G0U2EihwiHvA+2MHZX6Ruk3R2i6ghRcX0QHL3CotOHUWzEKt V0MhHFnldQPHSZ2aUDBrFvw8+DmEqZwTDhrtr+vAkSKgevsGE9fztPO+nFZklRHNoX53 GFbQ== X-Gm-Message-State: AC+VfDztPbILQoZMZZjHb2kzXM5ZeTGfqw7s9MvtR9QVGIpSgWX9B13x bKHFciWFxIn3X+TYeCj8A6KGNNKj1bpTGWlxKW+9kqGPOAL+1pg+kF3CD4P48jUAManOcVanEko 67LucHVJKNGD55eAyB4KTSFOE3ncux6Yk0XkFiusMPeO0B4RgvJ0ipDnR81FHlAFzqyMiNilgXO RhVEeMQhGSEzOC X-Google-Smtp-Source: ACHHUZ7IV6OdLi4xQaDu/P5gI9gcveGZ+FpKBQDsjRAPmDvKXG6DubfHPMgSkhsF+HAPAzAZIy4G8A== X-Received: by 2002:a17:903:18f:b0:1ac:8717:d436 with SMTP id z15-20020a170903018f00b001ac8717d436mr5304921plg.60.1683628272702; Tue, 09 May 2023 03:31:12 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31:12 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Guo Ren , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Guo Ren , Conor Dooley , Jisheng Zhang Subject: [PATCH -next v19 01/24] riscv: Rename __switch_to_aux() -> fpu Date: Tue, 9 May 2023 10:30:10 +0000 Message-Id: <20230509103033.11285-2-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033115_344243_B0AADFC4 X-CRM114-Status: GOOD ( 11.21 ) 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 Reviewed-by: Palmer Dabbelt --- 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 Tue May 9 10:30: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: 13235594 X-Patchwork-Delegate: palmer@dabbelt.com 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 7F3DBC7EE22 for ; Tue, 9 May 2023 10:31:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jqKysOAPGPvsl1jNkxSPTw98cBZe8MAjj02if7vISwI=; b=hqX4oxa8oFYZWL mexlQ7tIuGlXIdbARxiJ/QvNFTxKNvwcVZXI3Hg3kdF7qvKCI6OF/cmgBf73/57x2sGtY/g1YJIPT vhMC2q/i9OrGFsacZ17cG7JYgerzJS7MzvDBHu3NvoSkdAJH0LnDcwL0/bhFQn+1IoprW/JpfshiT LKYgQUHzRrIdFxmcmY/Q2QsJKLvt2Q93bJbUFJABmwCUey8qE6EX2DIxtZCYlSOqiKB1ESAuTvRfh DydALVzxbsdmnAcxIoEy14yjYE+mEz4kddgp6So20d3+Z8g5+SM4XTvh7C1GEzGQOZdzQBXboskrc 2yGn4SJtX/rLXH9dMvbg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKcx-002sk8-2g; Tue, 09 May 2023 10:31:27 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKcu-002sga-2e for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:26 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-64115e652eeso41439800b3a.0 for ; Tue, 09 May 2023 03:31:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628281; x=1686220281; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=4wVUwbllopTYZd/z7ThVFHG6Y+HYlXfckIEwVQA59q4=; b=YFflHspUoG4IQZzhTqmkxe4jAME+zJXqS9gGObv2f/9PFDQX/NYcr92ec8WgV28bHQ rNQKUwbFypLF6UkQqN4f8NGQpgHEH/SqQPbY9euSlQ1ybdvw6irMxYNLMdJ7plsU2Frl rPEVpbi8yylEEMZUH6szSeGPYyK+I3qkfSuxMsKUgefWBbpr+JUjIKfTcO5lR7N9PLNs 4dLYdUWpP9L2Mx8TWXUr72S5iOO6PHnhF/H0xhtKBA0vH3sFANyFMXFUsJ2pzZFRIsKn qspvyIv33X+EvVcwVkj2EBZ39Hcu0geeAsgMOjSuXF8Dug/7ECYIg4dACSCFCAQnnSL5 khEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628281; x=1686220281; 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=4wVUwbllopTYZd/z7ThVFHG6Y+HYlXfckIEwVQA59q4=; b=T6AclFLU7M6fgDUEQ+MaE3wDAMfHBgaOi8+bcW4fboEpnY0APvewcp7dWhoG3T/yMR YgjH95q4rMTVHepaGM+2gGxOV4Wd3a8iwZWhAUzZwtAJ3Z+PjJQchneRwiGaPgtVZqNI f9s9Dpz9b+36kLIVC36nhEim3Zs1sOBtLo5eLjylm/2FrflCD1N6XTZ06DLyrw4HV4+f SQLDp18jcxPd6oB9nyea6GO0KeZwQxRszPNYz9p8bkIRC3axUnPOQVp/EWfb6fG35ngz OB/VD9//0DvYiMz4Tjp2m6KaVtIAB3wrUpaSbMqQcsMkv8nZPilBSi2/mzbojPMIATaM O5Zg== X-Gm-Message-State: AC+VfDxUAnn9hJ6cwPxU3J0xyIWxzpbS++kO0hKpnlWEz5utaXcwZmyG 8IDPpWmg0p+2b/b/0+xvMQdK7qvBF5WF8nJdcHsfiJIQ0LPqm6QDY84VyIu3Z/a5HzzzpHjJBqZ 4pjBUbWHXSzQulReEqlEJasaLNncncjIJzfT6duy4ngua6O7mnBX5UlQIkFkhp80BImosonaDam CeDOH/orO2nDBB X-Google-Smtp-Source: ACHHUZ4JxML66lwMZryjBkK8ksZcrYdSxtkYiYRybmZPkELVtHptGkcR2ARRq1p9JuDBnBhlRU+O/A== X-Received: by 2002:a17:903:338e:b0:1a1:cb18:7f99 with SMTP id kb14-20020a170903338e00b001a1cb187f99mr11478595plb.30.1683628281230; Tue, 09 May 2023 03:31:21 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31: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, Guo Ren , Andy Chiu , Paul Walmsley , Albert Ou , Andrew Jones , Conor Dooley , Heiko Stuebner , Anup Patel , Jisheng Zhang , Guo Ren , Vincent Chen Subject: [PATCH -next v19 02/24] riscv: Extending cpufeature.c to detect V-extension Date: Tue, 9 May 2023 10:30:11 +0000 Message-Id: <20230509103033.11285-3-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033124_861433_29241F62 X-CRM114-Status: GOOD ( 16.36 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Guo Ren Add V-extension into riscv_isa_ext_keys array and detect it with isa string parsing. Signed-off-by: Guo Ren Signed-off-by: Guo Ren Signed-off-by: Greentime Hu Suggested-by: Vineet Gupta Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- 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 e0c40a4c63d5..574385930ba7 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 b1d6b7e4b829..7aaf92fff64e 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -107,6 +107,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; @@ -267,6 +268,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 Tue May 9 10:30:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235595 X-Patchwork-Delegate: palmer@dabbelt.com 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 9ADAEC7EE22 for ; Tue, 9 May 2023 10:31:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zYWUN742+jrBKv+d7Ti8s1n8JNJmG0+9q2iWV9icUKI=; b=EGaQIN/lx+/E4k IS5CHUCI35mMOXY3RfmxC0tw1wiSSDQfJOf+rOUGMtg0Vc94KRUDEBRxMmZwAv2VbkROe/Sg00Lkk uhdJAyQwRBmjmAIJx62/sf0dXWyvK6tx70VBQi1qkuZGmmW4uRsTPW1rZLu3acAHhOxEy1UPUt5X1 GOTjsoVuFJ8PWhGOptTCHdBAqybjTbOYOONuMdqeWLp63EoUVGmvexPwwaLjXAn78ioAS5DZ2ZAqA zSN3YO7JTDPrlZ13wVdovU+xu+FGR/odNaUVUCLv4PMXG4JsXfRyXLqXhBK1VEnxGuOAZ+FO84xx3 oJzY6yZqfZBf+nhpAYGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKd6-002sqG-0Y; Tue, 09 May 2023 10:31:36 +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 1pwKd3-002smc-1z for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:35 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1aaef97652fso39103005ad.0 for ; Tue, 09 May 2023 03:31:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628290; x=1686220290; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=HI4TRg2cuwYEsvBzmibkE9/BpJFs4hKp38ctSqfVtcI=; b=V+hcuVqSf3DOnNLL9zUwE4nn72OzWJhKaI5ymGHd8DnVmthAG5WaVmjgu/966048s6 VUvhUfxiyeQdOO7ElI3t1hidGZaI19x7wWqA4wGHL1ZNfnBqv/p3iSErW3fxn0zugI1P 0FoVXf95WCjaZJZZEv/HkhPxCPj+arzTKg5mKBodJM4IfrIjqnYSwKNosXDIFZGmMAO/ /5TrHdjpjp+IUXiCGW6Pf43nmfCAhMIa2PHU9qNX4eTQGKILlUQLIpMG5ZhjPXtHO9lX hBk7xK9LhpN+oG5TgJzNebaHDCQIQ41mM1b4lZ/o0P7W+1PiNynjtZu06GQ4t28jLRkA zgZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628290; x=1686220290; 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=HI4TRg2cuwYEsvBzmibkE9/BpJFs4hKp38ctSqfVtcI=; b=MOPTEly5Eu6U0XTLoW4L8KI9G+wwPOVlA6b7O4XnLirZCXZDTaCZ6pEhEVCdYoz9Ol HtKyFNiNgkcJr/CdBnG6sqxhw3VKci7AZ1JrjXcc2ig3UBiejks/8MY/5U/8eqInkErN EXeLJ3lXbN4M8vVm8VEyfEKunOKX3h+Mo0gI8F4bjv6KAq71RH2oShzmTHALZC7U+7Gp Z4WTnfm7/Ue5pjl+H/kji0Zo1tPsyEu9PczMyz5AcuwFm4Z2F5tb7WnKmpB8Uzyfwbyi WLRpY0F1SlKDZMa8W3HtWfXRtymVFk9H02cTcCEmJfvfH//4V0xeo56PDZau8NEdhkvS VdlA== X-Gm-Message-State: AC+VfDxMqmzCtVoprXxuNe850iTH5huQy1O5GF7zB8ceM6uC7dfuGB5Z ju5W0oZ8J6rKOUPZkJsGXhIFFajRgzUs7JbWALQORm8Eu4Xo7XyVp3iq+TqbrO/gTrbTT2/I7Tm 9zNZFrkFF0/ZJIjzy7yvRJkrCms+XDgkhCV5+Xb0pnGlPXRm5LRcMO53tg3se1Ik1VFT8mKSbWF Ir1ejAOo74vBWK X-Google-Smtp-Source: ACHHUZ4yDs51/jrzt305XFV64RxEA67yxjmxZFxFkfsTsm5LIVITSI6vKK9EGNi++sY/Px+1AIGeog== X-Received: by 2002:a17:902:b78a:b0:1aa:d9c5:9cd5 with SMTP id e10-20020a170902b78a00b001aad9c59cd5mr13572335pls.11.1683628290049; Tue, 09 May 2023 03:31:30 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31: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 Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Jonathan Corbet , Paul Walmsley , Albert Ou , Heiko Stuebner , Evan Green , Conor Dooley , Andrew Jones , Celeste Liu , Andrew Bresticker Subject: [PATCH -next v19 03/24] riscv: hwprobe: Add support for RISCV_HWPROBE_BASE_BEHAVIOR_V Date: Tue, 9 May 2023 10:30:12 +0000 Message-Id: <20230509103033.11285-4-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033133_664555_4DD4817C X-CRM114-Status: GOOD ( 14.89 ) 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 Probing kernel support for Vector extension is available now. Signed-off-by: Andy Chiu --- Documentation/riscv/hwprobe.rst | 10 ++++++++++ arch/riscv/include/asm/hwprobe.h | 2 +- arch/riscv/include/uapi/asm/hwprobe.h | 3 +++ arch/riscv/kernel/sys_riscv.c | 9 +++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Documentation/riscv/hwprobe.rst b/Documentation/riscv/hwprobe.rst index 9f0dd62dcb5d..b8755e180fbf 100644 --- a/Documentation/riscv/hwprobe.rst +++ b/Documentation/riscv/hwprobe.rst @@ -53,6 +53,9 @@ The following keys are defined: programs (it may still be executed in userspace via a kernel-controlled mechanism such as the vDSO). + * :c:macro:`RISCV_HWPROBE_BASE_BEHAVIOR_V`: Support for Vector extension, as + defined by verion 1.0 of the RISC-V Vector extension. + * :c:macro:`RISCV_HWPROBE_KEY_IMA_EXT_0`: A bitmask containing the extensions that are compatible with the :c:macro:`RISCV_HWPROBE_BASE_BEHAVIOR_IMA`: base system behavior. @@ -64,6 +67,13 @@ The following keys are defined: * :c:macro:`RISCV_HWPROBE_IMA_C`: The C extension is supported, as defined by version 2.2 of the RISC-V ISA manual. +* :c:macro:`RISCV_HWPROBE_KEY_V_EXT_0`: A bitmask containing the extensions + that are compatible with the :c:macro:`RISCV_HWPROBE_BASE_BEHAVIOR_V`: base + system behavior. + + * :c:macro:`RISCV_HWPROBE_V`: The V extension is supported, as defined by + version 1.0 of the RISC-V Vector extension manual. + * :c:macro:`RISCV_HWPROBE_KEY_CPUPERF_0`: A bitmask that contains performance information about the selected set of processors. diff --git a/arch/riscv/include/asm/hwprobe.h b/arch/riscv/include/asm/hwprobe.h index 78936f4ff513..39df8604fea1 100644 --- a/arch/riscv/include/asm/hwprobe.h +++ b/arch/riscv/include/asm/hwprobe.h @@ -8,6 +8,6 @@ #include -#define RISCV_HWPROBE_MAX_KEY 5 +#define RISCV_HWPROBE_MAX_KEY 6 #endif diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 8d745a4ad8a2..93a7fd3fd341 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -22,6 +22,7 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_KEY_MIMPID 2 #define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3 #define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0) +#define RISCV_HWPROBE_BASE_BEHAVIOR_V (1 << 1) #define RISCV_HWPROBE_KEY_IMA_EXT_0 4 #define RISCV_HWPROBE_IMA_FD (1 << 0) #define RISCV_HWPROBE_IMA_C (1 << 1) @@ -32,6 +33,8 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0) #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0) #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0) +#define RISCV_HWPROBE_KEY_V_EXT_0 6 +#define RISCV_HWPROBE_V (1 << 0) /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */ #endif diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index 5db29683ebee..6280a7f778b3 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -161,6 +162,7 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair, */ case RISCV_HWPROBE_KEY_BASE_BEHAVIOR: pair->value = RISCV_HWPROBE_BASE_BEHAVIOR_IMA; + pair->value |= RISCV_HWPROBE_BASE_BEHAVIOR_V; break; case RISCV_HWPROBE_KEY_IMA_EXT_0: @@ -173,6 +175,13 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair, break; + case RISCV_HWPROBE_KEY_V_EXT_0: + pair->value = 0; + if (has_vector()) + pair->value |= RISCV_HWPROBE_V; + + break; + case RISCV_HWPROBE_KEY_CPUPERF_0: pair->value = hwprobe_misaligned(cpus); break; From patchwork Tue May 9 10:30:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235596 X-Patchwork-Delegate: palmer@dabbelt.com 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 A5FCCC7EE22 for ; Tue, 9 May 2023 10:31:47 +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=cWE0WNTDcIJGT/EIxnpE8HahZhonS2BjUM7ROH4ynOU=; b=GPOLeuClbYwNVZ XdcBI8Z4RtBGFkzbKdI4qjgUW3ZRzye84zunzAHusnAgMKIfSz1DNhEiYrQNdDB4YtBRqRK+oTeEh R3VzGs5jlAv8NuBBnX2ZSmZT7a7c0k0E4Z2vzGvyEHg8aA3mZEdjYt0C46MdtNFDxCEyT++jfcfrU sf/v8tcN6Im/WtXLCY9x/MYghEo/NETGgbeaQfnPSSrhVnTefesthylGT4ANyEEmfztLnBcQoML9q tr64iKTsBVOATGrm2k95DvBRbkppZ6dgFDRtnwELFFd3U4Zt1PJcfU2zU2wQpKM6LVmg0Z8KbUxl5 1mvy/jA73EvpkQvRFJqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdB-002sud-0M; Tue, 09 May 2023 10:31:41 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKd7-002spN-2N for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:39 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1ab01bf474aso43123545ad.1 for ; Tue, 09 May 2023 03:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628294; x=1686220294; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=d1t0mcJTBhtsZBYqWTjUk/5U+5ffoYMHXYjoCjqrD1Y=; b=h5dBiJ8UgfnD5JORoVfPqn6nTI4sms8Kr2jL1vgMN3Q9h3oPFPPmL2ab9qZATEL1Si 0Ay/aPr9YKsXB9WRz0LeqDqQbfCc6Q8ToZENURfwIMqercpcHeJ6aq4QVIVjEc89B3ns 9aHcEYW3QGl4sHJrf3XZlSIkRq8mB6/AiYldXdL4Bd1fatPmM8M3y/GoqFhGPhgpWNOb j5d42CFUr2e0AenKtIz0rdXpA9uGJ/8U9xgbGSzZdNcQWuyaQDZGBx6Xw3i4+dF9bE7T Qy1+pcDmMIQ4lIdLrOFCA/xZOvLvpCPvTl1b8Ijv/hU+kbmAgi23RJbkL49I+TlYGWE8 3AQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628294; x=1686220294; 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=d1t0mcJTBhtsZBYqWTjUk/5U+5ffoYMHXYjoCjqrD1Y=; b=dZsg9LiVrFT16ehvJwa7/89QWPIUUplI4bto/4YE2bNt12S+U3mC3RnufGfCAvF80d EwFB0p3oM79IPDY3Pa0CzSLB5mmSJAogfjyHLh/5kzIA8V0qoRt/QvSAotDYQCQEupDc FGwn4YoVohtduV29qwfqiAv/Tvd5jSLIixmXqT6a54FyaEUhKb5DyPiS3wQsVv+JmZUO aMOUEK+w8eMKGpBFJOUp+WNu4JpLUbaNDWvc2Edl5b3dZZzZsT2CV9eTEtKQGjUeMtX+ U8AOBozSvm0weNo1Ho3+WeZ13XYSdWwsLGv3KULCSb6YR9jr5fMRBLoex7Qn51N3NT7a z4pw== X-Gm-Message-State: AC+VfDzK7OKHYdWlj9+ioOG1nnjn7MBd6/U0pL7GfwDMhjdcX/mU7MTD Ihhky7wtojmRg5AUjjhZu5RqVV9/lbA/U1xzmqiSwPMH59nv9wlbHEFP0OO+7kZDHC8iAvwLwNP 8SJxsIfQfrb1dfm4aNl+lXYMj1hCBPjTWxXjq+LVwPIv3SZUeRHLwoI3s14AJYxZqzZ+i2Ao3Xa mz+3aPRHGBj3kO X-Google-Smtp-Source: ACHHUZ4qMeeZ2GYD3WyxVZJAhNSVOzTJFNxDda0VTV7c0dzyVvQQU0d7TOmaiGsDSfh2vGTJ6Aga6Q== X-Received: by 2002:a17:902:ce8d:b0:1ac:2c8b:a0da with SMTP id f13-20020a170902ce8d00b001ac2c8ba0damr16876004plg.51.1683628294269; Tue, 09 May 2023 03:31:34 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31:33 -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 , Atish Patra , Guo Ren Subject: [PATCH -next v19 04/24] riscv: Add new csr defines related to vector extension Date: Tue, 9 May 2023 10:30:13 +0000 Message-Id: <20230509103033.11285-5-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033137_776014_1D9596D1 X-CRM114-Status: UNSURE ( 8.88 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Follow the riscv vector spec to add new csr numbers. Acked-by: Guo Ren Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Reviewed-by: Palmer Dabbelt Suggested-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- 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 b6acb7ed115f..b98b3b6c9da2 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 @@ -375,6 +383,12 @@ #define CSR_MVIPH 0x319 #define CSR_MIPH 0x354 +#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 Tue May 9 10:30:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235598 X-Patchwork-Delegate: palmer@dabbelt.com 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 C5B6CC7EE22 for ; Tue, 9 May 2023 10:31:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=rgLbPaJvzDtC7o0IZbLRhlLYg9kI9XhGfkAXIeUeVK0=; b=UHOqgV/spI9P1v yAEQB7DQ0j+MlHHSMrGbRA/TV42qTGq+q2JgInrVO82b8md4k9LufYi/nZ7BD59q9krBI+D7sdHLH T8CIdqYafKLTS5JC2b6+r+T678hylmnPD+xrIaTzHmyQiEC17OF0vCng7Dwddnetoq3/DCZuEEppn 588Jz5OeoMjPknFzMcBefhYRCyLQfBbGTc4zexCDLDf0yHd0A+CuJPUEPmYukgLHWPPUhUFRvBrHe 51ypvX8X1bZh/edQGGJdxA9TrJ9+gkqCaxQuK6ehFr1RDjSrCsB7kfccWDFMHlpRIhpFhIh5UvVUI 9vlukOA8YMuHQBOKmTIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdK-002t2L-1K; Tue, 09 May 2023 10:31:50 +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 1pwKdI-002szy-1a for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 10:31:48 +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=PG+A+9/fIV/ExGi75FEqrU6haa3ykbx364k88CAqjOs=; b=QZ7vCA88+ff2fWwK9wmwr7aKlp unXBae42Si7FQ8oXm8XlfPL37TmSvh5PuHLUUHeq68xiOBFC0H3QxFe/6BTePZSOfBhkrCfXXo2lP JUPguP6AKG/l8KLQsiU4T1caVw6Urk1dxAFbNtXb6LtEphh1Bh3EuQT03eEMICpun2jmgZZ/YfQyI as4CIV1kTR1/ZYgiYg1ROpF8vbG4X/oO3o2tYoolo0IO/SHD7TBY1BifnLW1B1JZItUvuP5wwT9wA Pnl3qvRdRqpGh0qU2kqwYq/0eTC2JVKB7RWTbsuvV3ObUXDjQxXCjnIaDEzR8B9IjS4BINFA/AlO1 xs/OUyIQ==; Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdE-00666C-2Q for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:47 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1ab1b79d3a7so38938975ad.3 for ; Tue, 09 May 2023 03:31:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628299; x=1686220299; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=PG+A+9/fIV/ExGi75FEqrU6haa3ykbx364k88CAqjOs=; b=B9+GWY5AxyRstg0w2rDdp310x+FpVxN/5CyJUkdglxX17elf8YdK5332Ef/a8by+tt RYIViO4Uq7YrL3M8ZuFC8G/dtupwplGp2Kj1+GsdWWsduEQgfMVPiaUs4T4aI/6EpQmP A7zGjGB8fjBmU7ldGuZqLmIKodSLyzdnZYjE7cv8xyhQQigXE11lJQB3ErstpwC9JdgR 0Sb/LgTEWCNLBOm3ypPPMhonQ12RL6arBExtZyPdEwehNjh3nKgJcnEEQTssHUrGDi/y 4ofwA48T29MiRMt5WyZwXfzIFJpc/DlBGA/9qMQhnAduvI5P06wM8xCAt/i6iHhxVD4I Zkzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628299; x=1686220299; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PG+A+9/fIV/ExGi75FEqrU6haa3ykbx364k88CAqjOs=; b=aOrlBrLgsmIVoqewOURWtmUZGrzIZbHLzM+4/zXGcuHWHbpsM8jMZaxnIVBwA8e0wV XjseCYE6tSIOEQ29k1Yr+lyoHHOgjZtrottzgGlsGt+nw1+DxgNHbp5h/w5b7PdZp4n7 UeitVt6xOPVx4nGHrybNceNQLs8M9qpSVlZdlkAQuQGTwWgXk5uJYjKAZXL4Yn/sOQMP pnK1C1E/0DjlhKNlAivsNT1zSuaGDZBwajs33DB+OSL6xaqzMp/p/Zn6GonjVq8oCDO2 vPo9XTe6u1HhxM64qd2J2nGGgDWZsSjnr1NP5DNzaSNh6aNH7K/yn7kk6wtwM/S+pyMq 9RWw== X-Gm-Message-State: AC+VfDwjK0XsdyuWiSH/2wD6uf56GaH+AN3Jb6NqE6nvq9e42odd264n E7CKo0r8hntGcdJ1At9KTUIaw4l4Ae51MzZlDWF1veDyOR3p620CchkV3GmBdagrvMQvgCVMrxf IGWnZQALf9Bh4sbuCE3MAbI/QLxp4AMCDOsz3TXa1WbK2e7wSrpUvXPxKQLVSdVMirrVCR7nvhs eEzVsn27u+D4tW X-Google-Smtp-Source: ACHHUZ5CLHLIAeI1mcgv08ZYoV9tvksZf4THmfiw41Juia/dv6ItXzsNsWolGM5RhjaOcyfX1QYZAw== X-Received: by 2002:a17:902:ceca:b0:1a6:5fa2:3293 with SMTP id d10-20020a170902ceca00b001a65fa23293mr16306391plg.56.1683628298789; Tue, 09 May 2023 03:31: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31: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 , Heiko Stuebner , Vincent Chen , Conor Dooley , Guo Ren , Alexandre Ghiti , Masahiro Yamada Subject: [PATCH -next v19 05/24] riscv: Clear vector regfile on bootup Date: Tue, 9 May 2023 10:30:14 +0000 Message-Id: <20230509103033.11285-6-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113145_044717_7F129015 X-CRM114-Status: GOOD ( 10.56 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu clear vector registers on boot if kernel supports V. Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- 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 Tue May 9 10:30: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: 13235597 X-Patchwork-Delegate: palmer@dabbelt.com 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 4E17AC77B7C for ; Tue, 9 May 2023 10:31:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=FMOh8ZvyZjKggCaz9pf+1kf5mol2f4XaJzE7rugkxCA=; b=Exnf6MW8DSBa1s MGRorGdKVWVHjB90YStPN2CqFqGlme+93RTD6+hNxWW43cHAQsvfeBnFyAK+S1XRb4sA7HOdEiQTz wQgTP+5NP4ghJ4ohxBEem3M9alzc+928kjm3eq//L3NBwdDcazwK1MLm8/kej0cCFTzjiPQMIO6Hs yIIfKuq8lCqs4vs168tFyg3Fnn2wk7K7Hn2hMYbr/Jh5peYOhqA+s9HsmKntFSoEMAN9PzsSbds1i dX5JLKTGexnruLLKXSMDxshOmtYyizvqUlu+gwsDCUwQjFGjcdbt6GJ/1Dx1805rRVkLsIAQvlmXN USBztU1aba6Ay8n9sEzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdI-002t0h-1m; Tue, 09 May 2023 10:31:48 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdF-002sgO-2G for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:47 +0000 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-64115e652eeso41442108b3a.0 for ; Tue, 09 May 2023 03:31:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628305; x=1686220305; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=OMViiC6TRgUBy+1t9x/tkF3qbB8zyqTk5+4zqct9RAc=; b=H2H87NzV+z4a95p/WDxeVW9lVOiGG3MSpJcPfFkIIbEFcdgH1iXqaTbeCOJcldkmgZ QHzCfmsREI1BjPcHxgpky6/suqYvVCrBU2VKm8ciawCSYnisvkIn4xLZEE6dcskpACU/ 8Z5gKCJoAjDk1pf5n+3cyj2NYeY80YiqmRFiKBumv78pSA5HpqnCi4cuhxXHZvEasMeI HvStYT06za8kCu6usOo4fFwHI/HF8U0+oaCx74xK8NeKVqLickCbRYZ24zOkqx9x+zLc TLvD5yKb5/qul3mGcfNl//kzaRDsysx/EZ2zWQ8z4TFqxt1K85yWgR5K80C9mdDxED35 5AAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628305; x=1686220305; 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=OMViiC6TRgUBy+1t9x/tkF3qbB8zyqTk5+4zqct9RAc=; b=Bb4UHBRWBXaDUkA5chHulVpPxVw6sOD/5ilrC4/qsHNhhxky2uuUW5QGMNXw2VteSV k0FiW0YqiAiZxCqtZ+qq7yUYtDDPzh6+HmcRtO1toRdvM0TVT1h13+9ID/8wMCeehl5D lG6vnlR9RBDdi3DrJGXZmnbGIJ6BPzzYXCR4mIMwM4XLFHoG4rNHYdcXrO8gFDBizWcS o2wXaFGERCkiYLboFVxH+7Oz/3WSEgjfiLUMwQGhiBJfRg3ieiBmsAJ9xvV3FS0UkVhE 34dcQ7hHrL1Vsr+DLojxn22wjFukFcaAv7wmB2bQ+pX2x6GpvWFMwWI2K9uO+aVbmVDo uy3A== X-Gm-Message-State: AC+VfDx+qc8DbSe/WddbkyxuEtSws3swiABuAsn8/tZf22pX0QBGoGWF 2+GQaHhbOagXlpxArsvKFmINHrpdrNdCigNZ0yZ+HexpWqD/3dLvxPoJEOuh3rkYvyEU4YVF0na GeYfYSf7BuE45SP57HwjNqD0mKDvHBUX9QIhcgJqQAuCGmy3efs/ikGOT9ahgZ6JriWc2/SC2WK +J6PSYNx2EoDcZ X-Google-Smtp-Source: ACHHUZ77LjVJMMtiEt2KLQzAUBVsb9v7+qxLpSGV6oaLIZ1n//HV6AEtIF0juAPI98sE1sXMla/j7A== X-Received: by 2002:a17:902:da8b:b0:1ac:7e95:74bf with SMTP id j11-20020a170902da8b00b001ac7e9574bfmr6630959plx.6.1683628304718; Tue, 09 May 2023 03:31: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31: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 , Han-Kuan Chen , Andy Chiu , Paul Walmsley , Albert Ou , Guo Ren , Nicolas Saenz Julienne , Jisheng Zhang , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Frederic Weisbecker , Andrew Bresticker , Heiko Stuebner , Alexandre Ghiti , Masahiro Yamada Subject: [PATCH -next v19 06/24] riscv: Disable Vector Instructions for kernel itself Date: Tue, 9 May 2023 10:30:15 +0000 Message-Id: <20230509103033.11285-7-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033145_747317_9A2EC9A9 X-CRM114-Status: GOOD ( 11.42 ) 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. This helps find illegal uses of vector in the kernel space, which is similar to the fpu. Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Co-developed-by: Han-Kuan Chen Signed-off-by: Han-Kuan Chen Co-developed-by: Greentime Hu Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- Changelog V19: - Add description in commit msg (Heiko's suggestion on v17) arch/riscv/kernel/entry.S | 6 +++--- arch/riscv/kernel/head.S | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 3fbb100bc9e4..e9ae284a55c1 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -48,10 +48,10 @@ _save_context: * Disable user-mode memory access as it should only be set in the * actual user copy routines. * - * Disable the FPU to detect illegal usage of floating point in kernel - * space. + * Disable the FPU/Vector to detect illegal usage of floating point + * or vector in kernel space. */ - li t0, SR_SUM | SR_FS + li t0, SR_SUM | SR_FS_VS REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 3fd6a4bd9c3e..e16bb2185d55 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -140,10 +140,10 @@ secondary_start_sbi: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 /* Set trap vector to spin forever to help debug */ @@ -234,10 +234,10 @@ pmp_done: .option pop /* - * Disable FPU to detect illegal usage of - * floating point in kernel space + * Disable FPU & VECTOR to detect illegal usage of + * floating point or vector in kernel space */ - li t0, SR_FS + li t0, SR_FS_VS csrc CSR_STATUS, t0 #ifdef CONFIG_RISCV_BOOT_SPINWAIT From patchwork Tue May 9 10:30: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: 13235599 X-Patchwork-Delegate: palmer@dabbelt.com 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 D5FA7C7EE22 for ; Tue, 9 May 2023 10:32:02 +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=+3RAm3jcMJvXdOWhlbVKnwOwtohfJ41042ZFRKv/cgs=; b=f3c5mKG+npqaLS 3jPawRSxyp7Zcw80LJ5TRXztUKOYI3618bB1SPYHoDTbrTLGdHqdhk5I2xD0YdXVr9cMc6ohlOJyg 6+nOE8kJq2C7qNf5/7PWua0kzLvg1eAasM+eJrCrY6efm1wi82Md+0Y2HBPtsbMe+NGeb3pEyjmbq 6HxEJRj4B0BCHTnn2tFZXoa/nZmDZmXo/eM408UcYPdJZdFexpjH2l8ibgHEohtFuSq3+33AbeSmb f/I4FI4QC1M8lzVO/G/fAIv+MaDdct3HjrjJ8uuKQm3N0Nh5zlqwanUYZmHL8V4wcFLYP4n+VLDDO wb9g2bZ1ssfoH1v3wXng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdQ-002t8I-1f; Tue, 09 May 2023 10:31:56 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdN-002t2E-2V for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:31:55 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1ab032d9266so53526205ad.0 for ; Tue, 09 May 2023 03:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628309; x=1686220309; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=wdT8PJ8oH1xp8pRaW3V56IfqtFcYTK0lyTpuGxj9OGI=; b=dEyjenlH8KNtGGE0LzwpQCnXQrGwiZonY0VzG1DZI3BoDMmCE1APNK7EpqOvJnSM9U SHiB3BDqnX44K0xYiiKVKDsc2bxbtQmLgy/XcMc3HjYVKtLPy6HNJXR7xM0qsrfUn56P ez3vP11D5ZC/goMxSu0BNgkFU4ljoa1wUnglHvZxzy7ec2BqYzogPRD/CXs+HgCgZTLy cc6vy7SSoIO+Gl2MAEtui2mWFGoERTgdZLrhfGMKIUcbHQPBM197+2ABmRrIUw/vTvRP I+h0y6pMhH7e/5zqnvNe9GOPuQVe3Zmq+QXl1p1q9FhXMUG3WiD3sP3G2+cQH8bmqZDh xrYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628309; x=1686220309; 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=wdT8PJ8oH1xp8pRaW3V56IfqtFcYTK0lyTpuGxj9OGI=; b=VFo6wTaP7pySLXScOQTEY0Z4dVQvv6KV3Ozpm8zaEcTUZqtMRDkR45o37WK3ZPteog Tfy7VAcEk/0y1ccC1bfPVSmFKQE41yCgqfDcuERVDGGuXILGh8X4bLvj36fTekpJiAdz PBW55jmDuAB/RBxO/Sc2lyTlxcskSCRYg9zC35w8JA1RYI2f2AlTljhFnOodONKWKo8q WIFgEJU6r+eWMhNxerCqYWi7kxUkRKdSTnKdJU1Ods9lFSxa7VUHOXhYJUJQ1OHDpOX3 FUifqC6fqXp4etrbaOmrrvpEp8Xuv1l7RxY1OIgopwC23IBJ/8N5TGdFjdp1U/x9F674 pdEg== X-Gm-Message-State: AC+VfDzuNbJI7whTDWfIisXylM2zQbeBEVONvPwgtFcor0w6byaK52u5 xQfcdeJqCNDcsGIfXC5c9sVXEcswPcUk/mhdMMF1OfJ+z/gELiP2Gu03tUw1jC1EYPwAWz+1fNY sSaBI9K5uosMZGDYovNLWHl3HwI74kL/et8mbhBaNlrIGpa3yS2YCs5yk8Hm28upk81Z3+HpGmL HJ8UDj3ZrOirhR X-Google-Smtp-Source: ACHHUZ53xZYSNwoONmUG4WIDAbzuioKTIVZMq4k8YL6PVVqG3QazEaniinqBNr8eY8d+la89wqpTbw== X-Received: by 2002:a17:902:9897:b0:1ab:253e:6906 with SMTP id s23-20020a170902989700b001ab253e6906mr12331938plp.67.1683628309309; Tue, 09 May 2023 03:31: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31: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, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Guo Ren Subject: [PATCH -next v19 07/24] riscv: Introduce Vector enable/disable helpers Date: Tue, 9 May 2023 10:30:16 +0000 Message-Id: <20230509103033.11285-8-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033153_838543_5A588AD7 X-CRM114-Status: UNSURE ( 7.16 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu These are small and likely to be frequently called so implement as inline routines (vs. function call). Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- 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 Tue May 9 10:30: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: 13235600 X-Patchwork-Delegate: palmer@dabbelt.com 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 EEB2FC7EE22 for ; Tue, 9 May 2023 10:32:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=lNFLYQJw935//vZ+hE6jTyMVdKdlDimOhLg8gWaD+MQ=; b=smD1QETjvWyKCjnP+iz6ADT9it 2QmLPNzgHHwrE7pK4u4z/rLcim2HTYaGBNuvPm6ivM7Kftm4UB86Sq8fOfQqCjuA5c7KicX0rhdW9 LM7CBwGzFhVm170/sumL8AfnD4seONPYf+Jy6h6uq/kJdbsKeFVJCp3USIQcpHPuT2uoLYroRnj0z 7NWWNKSVJDFre+hVlqauaA1ZYeE+fNulrvCA0giayYMRH2atuJBsYayWhucUVO1Tk788hu6kK52uw k/GYfX7kv3OVvhQkeqFv9eXLTHSnsdSKaY10LFGVUtP/raoVJ6Nlngiq9UcJlCqKgFeB7Yq+SGA9o mwDV3l7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdY-002tFY-1Y; Tue, 09 May 2023 10:32:04 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdV-002tCR-0W for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:02 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1aae5c2423dso55636115ad.3 for ; Tue, 09 May 2023 03:32:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628320; x=1686220320; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=9dNmDeCKcESyJpSC9hlvCfiuj/5kxC6whnLBDBKP6HY=; b=WPy8wXShGoaX1y9t3jhDp3KE75uw/Rf+jqMcDW1a0G7RBSV39XZtuKiDv0BkdDH2Zv ZIPGHz8CmwuonydaL4Wz9IQ5f1fMoGaeA6PbPldu9bQJTGduODS+O3vjpcPeFgxNK82i ucw2ysoQLF321No2wuD6+evKHLcwCkn+qi75XcSJUJg3Y5OAJ6UYJ1ORtB8tiR4wPItC /D3sy3sZfOiG316IPz9tVGvrtqd9oG2je4wy/EeAoFYubeJbgm5V97GevW6qXTLRD5E/ +u4dIHurBoXz91WbHy4WPSIZwCCnJVWuNL23rf/xV58eb3my1sdHmR4A289+nh/Iv+U9 mEhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628320; x=1686220320; 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=9dNmDeCKcESyJpSC9hlvCfiuj/5kxC6whnLBDBKP6HY=; b=iJPohrXglokCHjrkt3I1Jl4tKzzoNwvNNQ89lYZKSk4IXO4AoqTrK8kzUtWgNCN1LX e09myVBZIbQ2Qfhch1VdIz7eeoYD+k6nMBL3UtmibLDZ5p9O+Kv9Zd+FQmKEFVEExOXB 09aH+C5CcawszqHNg64gNVcCgyMDQB/3pC4k8jLTK5+vkIbsujsPL5JQJHFZ6qX0uuix ROeuLddYyERmbepXmQeYqc05HkK9hPERmxQSONCQ5i3uTEaU41jcGnbRJxObFb3nL+hb PJ5vY6TGMdNaUlbCQLI5/SiTXL3LTdGkkuaYqIvv4zjUYmsErnJJ1+LNmpYhUiCtA5dZ 7lsA== X-Gm-Message-State: AC+VfDx5nk7jPX6HAtmvRneFHsKD08bpgYDgIyFypn4ciEytIGa0TIZr I+yWL4e10mAeRgtswod8uemKdKVT6VEZ1YnO2xQkkHdRKmkP6ln1Ye13nDk/mn4Sg6c+isPxkcY ZverZPITgzSiRucwpplkb1C17aypILkU9W7evq1l9UJmBovyh5rRkLvDHgTCgPAi4azI9QmBhSt 3peHsqcFGXuEj9 X-Google-Smtp-Source: ACHHUZ6ntCkVRmqN5Oub1N6jZdS/zYP/THEb50rZ/rfC8D5t1e/cxCTvy7NyhdvwRfvBYuvH7NRaSQ== X-Received: by 2002:a17:902:d304:b0:1a9:21bc:65f8 with SMTP id b4-20020a170902d30400b001a921bc65f8mr14262189plc.11.1683628320059; Tue, 09 May 2023 03:32:00 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:31:59 -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 v19 08/24] riscv: Introduce riscv_v_vsize to record size of Vector context Date: Tue, 9 May 2023 10:30:17 +0000 Message-Id: <20230509103033.11285-9-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033201_201976_1D6B2CF7 X-CRM114-Status: GOOD ( 19.84 ) 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: Anup Patel , guoren@linux.alibaba.com, Jisheng Zhang , Ley Foon Tan , Atish Patra , Masahiro Yamada , vineetg@rivosinc.com, =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, Andrew Jones , Heiko Stuebner , Li Zhengyu , Alexandre Ghiti MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch is used to detect the size of CPU vector registers and use riscv_v_vsize to save the size of all the vector registers. It assumes all harts has the same capabilities in a SMP system. If a core detects VLENB that is different from the boot core, then it warns and turns off V support for user space. Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Palmer Dabbelt --- Changelog V19: - Fix grammar in WARN() (Conor) Changelog V18: - Detect inconsistent VLEN setup on an SMP system (Heiko). arch/riscv/include/asm/vector.h | 8 ++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/cpufeature.c | 2 ++ arch/riscv/kernel/smpboot.c | 7 +++++++ arch/riscv/kernel/vector.c | 36 +++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 arch/riscv/kernel/vector.c diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index dfe5a321b2b4..68c9fe831a41 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -7,12 +7,16 @@ #define __ASM_RISCV_VECTOR_H #include +#include #ifdef CONFIG_RISCV_ISA_V #include #include +extern unsigned long riscv_v_vsize; +int riscv_v_setup_vsize(void); + static __always_inline bool has_vector(void) { return riscv_has_extension_likely(RISCV_ISA_EXT_v); @@ -30,7 +34,11 @@ static __always_inline void riscv_v_disable(void) #else /* ! CONFIG_RISCV_ISA_V */ +struct pt_regs; + +static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +#define riscv_v_vsize (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index fbdccc21418a..c51f34c2756a 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -56,6 +56,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 7aaf92fff64e..28032b083463 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -18,6 +18,7 @@ #include #include #include +#include #define NUM_ALPHA_EXTS ('z' - 'a' + 1) @@ -269,6 +270,7 @@ void __init riscv_fill_hwcap(void) } if (elf_hwcap & COMPAT_HWCAP_ISA_V) { + riscv_v_setup_vsize(); /* * ISA string in device tree might have 'v' flag, but * CONFIG_RISCV_ISA_V is disabled in kernel. diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 445a4efee267..66011bf2b36e 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include "head.h" @@ -169,6 +171,11 @@ asmlinkage __visible void smp_callin(void) set_cpu_online(curr_cpuid, 1); probe_vendor_features(curr_cpuid); + if (has_vector()) { + if (riscv_v_setup_vsize()) + elf_hwcap &= ~COMPAT_HWCAP_ISA_V; + } + /* * Remote TLB flushes are ignored while the CPU is offline, so emit * a local TLB flush right now just in case. diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c new file mode 100644 index 000000000000..120f1ce9abf9 --- /dev/null +++ b/arch/riscv/kernel/vector.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2023 SiFive + * Author: Andy Chiu + */ +#include + +#include +#include +#include +#include + +unsigned long riscv_v_vsize __read_mostly; +EXPORT_SYMBOL_GPL(riscv_v_vsize); + +int riscv_v_setup_vsize(void) +{ + unsigned long this_vsize; + + /* There are 32 vector registers with vlenb length. */ + riscv_v_enable(); + this_vsize = csr_read(CSR_VLENB) * 32; + riscv_v_disable(); + + if (!riscv_v_vsize) { + riscv_v_vsize = this_vsize; + return 0; + } + + if (riscv_v_vsize != this_vsize) { + WARN(1, "RISCV_ISA_V only supports one vlenb on SMP systems"); + return -EOPNOTSUPP; + } + + return 0; +} From patchwork Tue May 9 10:30: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: 13235601 X-Patchwork-Delegate: palmer@dabbelt.com 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 2AF39C7EE24 for ; Tue, 9 May 2023 10:32: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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4YvH7LDuw9eKzo0P1YdgUaffCrIwFjoW3VdP6q6mwjU=; b=kosh1tNwBQcIky fjsO2MGXYmjx6td7FtIuED6u9qk/VNzRPQNWGxqhM56mZkD8565Gs+RTN2+2qiuuukM8r0KZurd/+ eVQS7ciHccoJWFKAas3dtSO8SDIFlRruzbF9pDZzP9HSWSv29hdeeZQztpOi1hH/+GDeqB+KOkPBm /SS6F7FbAP6UMZfjqXMtjgxCNSaHZSZbZEV/3QwbIDctQBp4cBV1+fUC2VcTbHrgVygWKmbjZOYo1 D0cbjIWnhAfefJtuLkXSoSCKWk0CC85sEllKQeQZByrH1al3VGPyGzRb4bgSiX52ksNdBnyn03wFy 0P5/7MibWsEOYasWIsUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdk-002tRO-1u; Tue, 09 May 2023 10:32:16 +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 1pwKdi-002tOe-2e for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 10:32:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=ahp5kQnTghgkZmiSTxR6+eMYSF2yeYW/B9Sl3mLm86I=; b=MmEJ9TigkUFzSjKCnUbalkNsGy /uyh10/nNOivDb6Exj7IJNypuwpPPKKUbsarEPFo9DrGQgbCmBKozvn6bW8wK8mu/jf00LvgQigsm 032PoBmxl8aGFcTCj9UX/Q0IrvQDVjLyHJztcdL//vRGVIL1DuJEXX6Q0ghYOCz6YSxWAaj1cjPGz 5FpIkNQ50TJaL65J4kVpvc55hRtn2OxIWApbjf75EwTcyAOoSqx4dj6RmejMxvPEqU9S/U1IxOijb eScADJpIxzLbI59bwSOJqI3SndGgQv4srRAxX46NTmXNNG1C5w4NnF+Q+S9izcMBXq8F1jLeSsRJP Hq+3bzUg==; Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdc-00666h-0o for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:13 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1aad55244b7so43143045ad.2 for ; Tue, 09 May 2023 03:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628326; x=1686220326; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ahp5kQnTghgkZmiSTxR6+eMYSF2yeYW/B9Sl3mLm86I=; b=KfhD6w8KZHsd6v9aUu137matM7Ud86Ork+O59oxBKe2TV9bPYAcytyN3pit3i26NkU jvDo7ysnSvVOCb+SGiVkyo6K06HsFVbmGtHgwxwb+w7SBMCWCVavWo+Cbh/7swt8C7R2 5aZaYguuEa1PBX6GsklEyZ9vPZoy7N3Emm+rAxildNuVMy6bxIKzPq/NjqgnWjnL9rsF CXU8M2vdMyGRJAKQ8wh6/M+55fIX8AEQJovM8gkN+LlyB6Y6QZJDgRElrWivDSODu3QG CsH7Hq/bzPXYgyKt12QSjYXkhdJj6XwJsHhfggpK5ISp3zh9sZYTGjRx+IRndd1jd0kW 2dmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628326; x=1686220326; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ahp5kQnTghgkZmiSTxR6+eMYSF2yeYW/B9Sl3mLm86I=; b=aI5d/9m3oeRboF0oxOFsHMImDT70/4o9wDzeyZLyHYESDA73SsiY+csC1GwBJyIE5Z FwERZQyixlbaA0bil9dYnDxt/uR1TNGXLUhMPmFCxGFl9WbyZntopTFUA6F6vyHItTDA KReHg644NV3PXc5OloV6kOnBjZW6l+qqTGe7jhseVA2e24L2wrYq/8HVKaC736MDV9Wn TMAgTPa+PLfukjVb3Ttn6zWS8XCVZbtLz3oaoYVH8o0QrANnoCWMwvqnR+u+HslPlt/y mHhQQP4l5pcCTr+jkrxf1C8Hb1K7lSzf1s9UzCtPmu292+Pj0tyElxfjeSx9iZX3ExSq 2FMA== X-Gm-Message-State: AC+VfDyNesEmbkAjvbes0SjCwCqisBhKSdVOslOhARBkVGLZX90MI2mX Tj0Gggvj/pomSUwaszlwTh3kMyXqCyD54oBzl/OXi9BvqGPk3FXu0qYxj40qOfEU0o4DAQYRqUs ek/irGTKSw2D56Yj0ydPdblN2HvnbVCeU0PnFEfQ7z5989MlMPU2IY+/0qBsfwd2EtJggiLt9aY zbewuhcOnU3HfX X-Google-Smtp-Source: ACHHUZ6KFsz81viA3s3MvRxm/AxDuAtt4z22AwlOnW2jTQrrGqzIl05BEki990/hiZjbPe1AoMhs/g== X-Received: by 2002:a17:902:ba86:b0:1a6:bd5c:649d with SMTP id k6-20020a170902ba8600b001a6bd5c649dmr13435174pls.56.1683628325622; Tue, 09 May 2023 03:32: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32: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 , Heiko Stuebner , Guo Ren , Conor Dooley Subject: [PATCH -next v19 09/24] riscv: Introduce struct/helpers to save/restore per-task Vector state Date: Tue, 9 May 2023 10:30:18 +0000 Message-Id: <20230509103033.11285-10-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-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-20230509_113210_204421_306676AC X-CRM114-Status: GOOD ( 12.98 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Add vector state context struct to be added later in thread_struct. And prepare low-level helper functions to save/restore vector contexts. This include Vector Regfile and CSRs holding dynamic configuration state (vstart, vl, vtype, vcsr). The Vec Register width could be implementation defined, but same for all processes, so that is saved separately. This is not yet wired into final thread_struct - will be done when __switch_to actually starts doing this in later patches. Given the variable (and potentially large) size of regfile, they are saved in dynamically allocated memory, pointed to by datap pointer in __riscv_v_ext_state. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Vineet Gupta Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Guo Ren Reviewed-by: Björn Töpel Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/vector.h | 95 ++++++++++++++++++++++++++++ arch/riscv/include/uapi/asm/ptrace.h | 17 +++++ 2 files changed, 112 insertions(+) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 68c9fe831a41..7a56bb0769aa 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -11,8 +11,10 @@ #ifdef CONFIG_RISCV_ISA_V +#include #include #include +#include extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); @@ -22,6 +24,26 @@ static __always_inline bool has_vector(void) return riscv_has_extension_likely(RISCV_ISA_EXT_v); } +static inline void __riscv_v_vstate_clean(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN; +} + +static inline void riscv_v_vstate_off(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_OFF; +} + +static inline void riscv_v_vstate_on(struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_INITIAL; +} + +static inline bool riscv_v_vstate_query(struct pt_regs *regs) +{ + return (regs->status & SR_VS) != 0; +} + static __always_inline void riscv_v_enable(void) { csr_set(CSR_SSTATUS, SR_VS); @@ -32,13 +54,86 @@ static __always_inline void riscv_v_disable(void) csr_clear(CSR_SSTATUS, SR_VS); } +static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest) +{ + asm volatile ( + "csrr %0, " __stringify(CSR_VSTART) "\n\t" + "csrr %1, " __stringify(CSR_VTYPE) "\n\t" + "csrr %2, " __stringify(CSR_VL) "\n\t" + "csrr %3, " __stringify(CSR_VCSR) "\n\t" + : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl), + "=r" (dest->vcsr) : :); +} + +static __always_inline void __vstate_csr_restore(struct __riscv_v_ext_state *src) +{ + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvl x0, %2, %1\n\t" + ".option pop\n\t" + "csrw " __stringify(CSR_VSTART) ", %0\n\t" + "csrw " __stringify(CSR_VCSR) ", %3\n\t" + : : "r" (src->vstart), "r" (src->vtype), "r" (src->vl), + "r" (src->vcsr) :); +} + +static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to, + void *datap) +{ + unsigned long vl; + + riscv_v_enable(); + __vstate_csr_save(save_to); + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvli %0, x0, e8, m8, ta, ma\n\t" + "vse8.v v0, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v8, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v16, (%1)\n\t" + "add %1, %1, %0\n\t" + "vse8.v v24, (%1)\n\t" + ".option pop\n\t" + : "=&r" (vl) : "r" (datap) : "memory"); + riscv_v_disable(); +} + +static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_from, + void *datap) +{ + unsigned long vl; + + riscv_v_enable(); + asm volatile ( + ".option push\n\t" + ".option arch, +v\n\t" + "vsetvli %0, x0, e8, m8, ta, ma\n\t" + "vle8.v v0, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v8, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v16, (%1)\n\t" + "add %1, %1, %0\n\t" + "vle8.v v24, (%1)\n\t" + ".option pop\n\t" + : "=&r" (vl) : "r" (datap) : "memory"); + __vstate_csr_restore(restore_from); + riscv_v_disable(); +} + #else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) +#define riscv_v_vstate_off(regs) do {} while (0) +#define riscv_v_vstate_on(regs) do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 882547f6bd5c..586786d023c4 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -77,6 +77,23 @@ union __riscv_fp_state { struct __riscv_q_ext_state q; }; +struct __riscv_v_ext_state { + unsigned long vstart; + unsigned long vl; + unsigned long vtype; + unsigned long vcsr; + void *datap; + /* + * In signal handler, datap will be set a correct user stack offset + * and vector registers will be copied to the address of datap + * pointer. + * + * In ptrace syscall, datap will be set to zero and the vector + * registers will be copied to the address right after this + * structure. + */ +}; + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ From patchwork Tue May 9 10:30:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235602 X-Patchwork-Delegate: palmer@dabbelt.com 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 777D8C7EE22 for ; Tue, 9 May 2023 10:32:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IdG5ldH4fiHDLdK45WYa3ztIzjxWUyY0JSanofFcA5c=; b=x2CkDEbdyRs68o 5IJynssavWaC/m4vCdxEn0qDNnOqQ43IeYNlAOULHqxOmkL8svn6K4uvdgrH6DCcOD3R9N5BHgZyC +O3heDOrResbNknZBLLPKmVWdtm2UwJmZ+9R4Ouoz9tBS7vtOYTlBaMU2Wexz/y/Qe6E6PSB4jKPu 7gPr+bX5A/2ymQkjBTHaRK/4qaBQZrqKb3GOx8k6+r33oHnuwqCniC06jtF4PdgZxhGNrHWeea4H+ OdkyBORdO0Gyjp2+ES8sLA3rP1poDYpHoWqYHuc5Jqr8qT2bcOTi21Y5ds98RVSAZlFrQ6ZC+Jc84 bJczF69RrKmX+pl7V1xQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdq-002tWe-10; Tue, 09 May 2023 10:32:22 +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 1pwKdo-002tUA-1F for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 10:32:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=W9PBJBzHlcHA8nlVu28xWsGDRnYddiwOeG1IiMG6RqE=; b=ENFd18DFpzIUiLnrqps+UY3zRx vLR2c8gu7HEWe/lXgLP6UChOuxnJlBe9UA6sM2ZDhck8W1oAd6dU5p5zPlJEImGHOj+3FNm1SOXJk RYhkloGemHpG1wb0cQmQxLUQOVJbGPa3TbXaUl0RF3x92Em/LRD1E5yMiDtQEfAbZRZIhvmIxb1gk MThCsSnNpmBcSidvILQej5kE89si7Nu5nmXY1zOShWwYkNEkKvjC6BF3qjmE1WfUY9szdhmahH9gb udC1LZphmfd2kjgcQoY3/edTqf0+WNSKby5LOB9bAw6tA1e0Rl0WRiqX1LqSUMqqRcsmVGYyE5Nun O8Qhoeog==; Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdj-00666C-27 for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:19 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1ab1b79d3a7so38944305ad.3 for ; Tue, 09 May 2023 03:32:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628334; x=1686220334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=W9PBJBzHlcHA8nlVu28xWsGDRnYddiwOeG1IiMG6RqE=; b=fgFYU4GjyB45BopdN6/mp7jhxbWo36eigX6K9WDGkmRzoA52E8rBme8tzQx0e3fzxC 8JcR91zK7V0T63GX9JfP7YfNtEn3QzTB8PDscEI8aLVhVm7fpmPy56LNnX9O9ejUjwk6 9tmNrXp/McmNAaTMvGAOUNE9xzqcywpwGWtjXRKPvR4vOD8hNh37QBdB6czEFqF/xEpA c8EGd61ObNqMc6hiEkSRy0V83SZjH8GeKYSkadZU8RRiBvtmmtiGVeGK5qM8hxgWzqvG tFYLsTidgbjnHLYVoQ4If5pJU0i6OVkMNCNd8a6IENap7Jp3I3tIe6UqKBtfmt5NzgOZ R/BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628334; x=1686220334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=W9PBJBzHlcHA8nlVu28xWsGDRnYddiwOeG1IiMG6RqE=; b=aRG+d/uwxP+y3dv/xxF5Tt4qNhgDtgrjB8pXnH0a51eXyVvwaGGYWkU2p5L4QMjmgn pdtTU6uNCOOQiP90EirgSHNax8pUhmRIowxasQI+0q36EorYM/rklvNUkpeMSuXqCPGu qzHpgPJUb6eSm54Yl1h3TRwVqr6vIppW3MXuyXsJ7qnAF7IbLQ3C3+ossNdJnkaEMOfe J7DGb21QV6MwbnOczfwRzEg1PS+4guEegPxXymSOA96LQw8+TkiObIj2jaH98x9iQ6dv sGxLuHKOUnWfP5hyKJe0WQLAAUFUHcCWzfcKPcNnMsUKKkLiLz9w4iSOwnkAcxJv+Zzr BDrg== X-Gm-Message-State: AC+VfDx9MD/+jBxu4FJODNfssu31ST1mXeOvu0S3MNwLSX8FHyU53k++ JPOHqDjoJHhsUfJD5T9jpYHC1wzWH3iZpoHdvy+QgRIi1LoBuJNdQtZxJky6Pi/lAvJAKQllZJu nVD2u1zTGvX8/LJpgXDHoD8mvLF/bJMER0pkdDqdgLrKmI14M3wJ9wgiBZqN3ZxyJMIKkTJvzf4 IX34/fFA1axmFX X-Google-Smtp-Source: ACHHUZ7yfmEM4E6vWuEzYwtxvd6PrSPoyafIOeEJxl/CBUSDWn2J/WhYKrhqHToYgslPH9nfweruRw== X-Received: by 2002:a17:902:ab5e:b0:1ac:2d81:3cf9 with SMTP id ij30-20020a170902ab5e00b001ac2d813cf9mr11588040plb.13.1683628334488; Tue, 09 May 2023 03:32:14 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32: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 Subject: [PATCH -next v19 10/24] riscv: Add task switch support for vector Date: Tue, 9 May 2023 10:30:19 +0000 Message-Id: <20230509103033.11285-11-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-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-20230509_113217_216862_01262934 X-CRM114-Status: GOOD ( 16.99 ) 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, Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Ruinland Tsai , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, 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 Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 3 +++ arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/vector.h | 38 ++++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 18 +++++++++++++ 5 files changed, 63 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 94a0590c6971..f0ddf691ac5e 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -39,6 +39,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + struct __riscv_v_ext_state vstate; }; /* Whitelist the fstate from the task_struct for hardened usercopy */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 4b96b13dee27..a727be723c56 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -78,6 +79,8 @@ do { \ struct task_struct *__next = (next); \ if (has_fpu()) \ __switch_to_fpu(__prev, __next); \ + if (has_vector()) \ + __switch_to_vector(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index e0d202134b44..97e6f65ec176 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -81,6 +81,9 @@ struct thread_info { .preempt_count = INIT_PREEMPT_COUNT, \ } +void arch_release_task_struct(struct task_struct *tsk); +int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); + #endif /* !__ASSEMBLY__ */ /* diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 7a56bb0769aa..121d700c6ada 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -12,6 +12,9 @@ #ifdef CONFIG_RISCV_ISA_V #include +#include +#include +#include #include #include #include @@ -124,6 +127,38 @@ static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_ riscv_v_disable(); } +static inline void riscv_v_vstate_save(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) == SR_VS_DIRTY) { + struct __riscv_v_ext_state *vstate = &task->thread.vstate; + + __riscv_v_vstate_save(vstate, vstate->datap); + __riscv_v_vstate_clean(regs); + } +} + +static inline void riscv_v_vstate_restore(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) != SR_VS_OFF) { + struct __riscv_v_ext_state *vstate = &task->thread.vstate; + + __riscv_v_vstate_restore(vstate, vstate->datap); + __riscv_v_vstate_clean(regs); + } +} + +static inline void __switch_to_vector(struct task_struct *prev, + struct task_struct *next) +{ + struct pt_regs *regs; + + regs = task_pt_regs(prev); + riscv_v_vstate_save(prev, regs); + riscv_v_vstate_restore(next, task_pt_regs(next)); +} + #else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs; @@ -132,6 +167,9 @@ static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) +#define riscv_v_vstate_save(task, regs) do {} while (0) +#define riscv_v_vstate_restore(task, regs) do {} while (0) +#define __switch_to_vector(__prev, __next) do {} while (0) #define riscv_v_vstate_off(regs) do {} while (0) #define riscv_v_vstate_on(regs) do {} while (0) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e2a060066730..b7a10361ddc6 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -24,6 +24,7 @@ #include #include #include +#include register unsigned long gp_in_global __asm__("gp"); @@ -146,12 +147,28 @@ void flush_thread(void) fstate_off(current, task_pt_regs(current)); memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); #endif +#ifdef CONFIG_RISCV_ISA_V + /* Reset vector state */ + riscv_v_vstate_off(task_pt_regs(current)); + kfree(current->thread.vstate.datap); + memset(¤t->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); +#endif +} + +void arch_release_task_struct(struct task_struct *tsk) +{ + /* Free the vector context of datap. */ + if (has_vector()) + kfree(tsk->thread.vstate.datap); } int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) { fstate_save(src, task_pt_regs(src)); *dst = *src; + /* clear entire V context, including datap for a new task */ + memset(&dst->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); + return 0; } @@ -184,6 +201,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.s[0] = 0; } p->thread.ra = (unsigned long)ret_from_fork; + riscv_v_vstate_off(childregs); p->thread.sp = (unsigned long)childregs; /* kernel sp */ return 0; } From patchwork Tue May 9 10:30: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: 13235603 X-Patchwork-Delegate: palmer@dabbelt.com 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 2BBC8C7EE24 for ; Tue, 9 May 2023 10:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9UKCKxi4Fs8bliLgQRxy2FCfVuDS36d7AfY/yf5u4lI=; b=X2P5NOG/qZ2hE0 iB6p4Ykk1mjdRSwAJ0mx+SBzP3Xrtl130IftVIv9MsMu0cFywmfxktK//qSuS0nz80JrgJJMhbEI3 D9pE7hoEQJbW4MSY5hOkKPVTe88Tgj+mpA3Sdc8Cc+o4c4TOcX2ixSdxtH5pVUF/i241tyljpSSs2 DRyuacXWaIeayCeFzQrU8DcgbF3yi+qbaaQqqy0JGqcd+SuethraWuIBtKFXrnfm1imAgq3sWcytq WnVfWwbCuoITRXMLr15A1bs6onL/BRSWN+x5ZuwE9JFUBaBXvfeBzhPAzjK5LWySTwuhSDrImyiR2 J5yI9ltTfWOkKEil2TpA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdv-002tbV-1H; Tue, 09 May 2023 10:32:27 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKds-002tYG-1B for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:26 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-64115eef620so41331285b3a.1 for ; Tue, 09 May 2023 03:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628343; x=1686220343; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=5EhZPsG4fUMbyzD4ZH3jWmG6kJzEmCE9DjIQkN59Cig=; b=iU8/xeO4UBiYFA4jRzrLnD1X0x/XfmL3CLbRO6hlBAjCPzh/Y/dANVDNgbMN6umhOe Jq3AR8Ump/y7zXnZF0gFGpZ6eGZ2epC2OZIiNhlHRToa20qCUOk4V/9kTUfdg1kR9h5L rNiMNLeDY2fS7eGReto6ek8gDlSDsa1eTC/2d4Fo+Uf5WLedvs2xVcezeR4LRDdMp4gA SPov0R9qmhElXANHyEC9x8SDkPyecskLsruB1YiBGzYH4coNKQKUPTebgkG6zTmtvp7r cUAMVNRq6hzTzAZ/sm9BanMXXh8mAyu4pkFSHV9C9toAgIAf2hS+19xerjuZ9CnxUFvp Omcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628343; x=1686220343; 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=5EhZPsG4fUMbyzD4ZH3jWmG6kJzEmCE9DjIQkN59Cig=; b=gTw+R1Ct+If85GZvkQedCi/HEczValuYIPbq1p8yUsP9sTUzLL0gTAfpoWh+ZU3ggo P09XrBWd4aFihYXRE0iNxaMqJrYjHm7z1xi81wfDg9fgROIUfQLJv/3LqUYNDC5GE+hI V02ir/zBHXk50jbh1FGjZhv2VhzVJQ2DTk+4Oygz6QVxRBZ90paMNHw1WxETzgy+huOV 7rwsS5pR2HGJKd1bhZA53kgyVTe+47x35o0iCdiuB/VDzIqud9X9rbMDRghN1HG1WOou AsJJJtb5eOuD5OqWYSX1Z7/Oa+snGf31kMdwyEBsu6nUD7NbelaI5hAMVwjK0TgThYtr 7Aew== X-Gm-Message-State: AC+VfDxJaChWZVCosJtRYw3sdTz4hAt58x7yAoudwYPLHN1zRnludJA1 JPQ2zdlhLNxAKKQmqnEDPB0mUEsZ24GbzjNdUQN1ZOc2WRYo2bBBXtirF8AmqxVCttoB7Zgajnl a2O3BcXcNNitcK/tK+FMoMmrYztcCHpAGyv1oYPaMk6jnduWJXXohDavba4uUX5ofyyvf/cJbFv kCKQa0tsM2XGLu X-Google-Smtp-Source: ACHHUZ5qU9+zuapc71KpOuENn7lVNsrngsp86g4fPmCcN3LKYVPuVF2fNbM7L9T6PPNt4nnvXmuZkg== X-Received: by 2002:a17:902:f54c:b0:1ac:3b69:bb9c with SMTP id h12-20020a170902f54c00b001ac3b69bb9cmr17918311plf.28.1683628343159; Tue, 09 May 2023 03:32:23 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:22 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Andrew Jones , Conor Dooley , Lad Prabhakar , Liao Chang , Jisheng Zhang , Vincent Chen , Guo Ren , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Mattias Nissler Subject: [PATCH -next v19 11/24] riscv: Allocate user's vector context in the first-use trap Date: Tue, 9 May 2023 10:30:20 +0000 Message-Id: <20230509103033.11285-12-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033224_428344_31FCAFAF X-CRM114-Status: GOOD ( 20.61 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Vector unit is disabled by default for all user processes. Thus, a process will take a trap (illegal instruction) into kernel at the first time when it uses Vector. Only after then, the kernel allocates V context and starts take care of the context for that user process. Suggested-by: Richard Henderson Link: https://lore.kernel.org/r/3923eeee-e4dc-0911-40bf-84c34aee962d@linaro.org Signed-off-by: Andy Chiu Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner Reviewed-by: Conor Dooley --- Changelog V18: - Add blank lines (Heiko) - Return immediately in insn_is_vector() if an insn matches (Heiko) arch/riscv/include/asm/insn.h | 29 +++++++++++ arch/riscv/include/asm/vector.h | 2 + arch/riscv/kernel/traps.c | 26 +++++++++- arch/riscv/kernel/vector.c | 91 +++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h index 8d5c84f2d5ef..4e1505cef8aa 100644 --- a/arch/riscv/include/asm/insn.h +++ b/arch/riscv/include/asm/insn.h @@ -137,6 +137,26 @@ #define RVG_OPCODE_JALR 0x67 #define RVG_OPCODE_JAL 0x6f #define RVG_OPCODE_SYSTEM 0x73 +#define RVG_SYSTEM_CSR_OFF 20 +#define RVG_SYSTEM_CSR_MASK GENMASK(12, 0) + +/* parts of opcode for RVF, RVD and RVQ */ +#define RVFDQ_FL_FS_WIDTH_OFF 12 +#define RVFDQ_FL_FS_WIDTH_MASK GENMASK(3, 0) +#define RVFDQ_FL_FS_WIDTH_W 2 +#define RVFDQ_FL_FS_WIDTH_D 3 +#define RVFDQ_LS_FS_WIDTH_Q 4 +#define RVFDQ_OPCODE_FL 0x07 +#define RVFDQ_OPCODE_FS 0x27 + +/* parts of opcode for RVV */ +#define RVV_OPCODE_VECTOR 0x57 +#define RVV_VL_VS_WIDTH_8 0 +#define RVV_VL_VS_WIDTH_16 5 +#define RVV_VL_VS_WIDTH_32 6 +#define RVV_VL_VS_WIDTH_64 7 +#define RVV_OPCODE_VL RVFDQ_OPCODE_FL +#define RVV_OPCODE_VS RVFDQ_OPCODE_FS /* parts of opcode for RVC*/ #define RVC_OPCODE_C0 0x0 @@ -304,6 +324,15 @@ static __always_inline bool riscv_insn_is_branch(u32 code) (RVC_X(x_, RVC_B_IMM_7_6_OPOFF, RVC_B_IMM_7_6_MASK) << RVC_B_IMM_7_6_OFF) | \ (RVC_IMM_SIGN(x_) << RVC_B_IMM_SIGN_OFF); }) +#define RVG_EXTRACT_SYSTEM_CSR(x) \ + ({typeof(x) x_ = (x); RV_X(x_, RVG_SYSTEM_CSR_OFF, RVG_SYSTEM_CSR_MASK); }) + +#define RVFDQ_EXTRACT_FL_FS_WIDTH(x) \ + ({typeof(x) x_ = (x); RV_X(x_, RVFDQ_FL_FS_WIDTH_OFF, \ + RVFDQ_FL_FS_WIDTH_MASK); }) + +#define RVV_EXRACT_VL_VS_WIDTH(x) RVFDQ_EXTRACT_FL_FS_WIDTH(x) + /* * Get the immediate from a J-type instruction. * diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 121d700c6ada..a8881af83ce4 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -21,6 +21,7 @@ extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); +bool riscv_v_first_use_handler(struct pt_regs *regs); static __always_inline bool has_vector(void) { @@ -165,6 +166,7 @@ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } +static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } #define riscv_v_vsize (0) #define riscv_v_vstate_save(task, regs) do {} while (0) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 8c258b78c925..24d309c6ab8d 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -26,6 +26,7 @@ #include #include #include +#include int show_unhandled_signals = 1; @@ -145,8 +146,29 @@ DO_ERROR_INFO(do_trap_insn_misaligned, SIGBUS, BUS_ADRALN, "instruction address misaligned"); DO_ERROR_INFO(do_trap_insn_fault, SIGSEGV, SEGV_ACCERR, "instruction access fault"); -DO_ERROR_INFO(do_trap_insn_illegal, - SIGILL, ILL_ILLOPC, "illegal instruction"); + +asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs) +{ + if (user_mode(regs)) { + irqentry_enter_from_user_mode(regs); + + local_irq_enable(); + + if (!has_vector() || !riscv_v_first_use_handler(regs)) + do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc, + "Oops - illegal instruction"); + + irqentry_exit_to_user_mode(regs); + } else { + irqentry_state_t state = irqentry_nmi_enter(regs); + + do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc, + "Oops - illegal instruction"); + + irqentry_nmi_exit(regs, state); + } +} + DO_ERROR_INFO(do_trap_load_fault, SIGSEGV, SEGV_ACCERR, "load access fault"); #ifndef CONFIG_RISCV_M_MODE diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 120f1ce9abf9..960a343799c6 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -4,10 +4,19 @@ * Author: Andy Chiu */ #include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include unsigned long riscv_v_vsize __read_mostly; @@ -34,3 +43,85 @@ int riscv_v_setup_vsize(void) return 0; } + +static bool insn_is_vector(u32 insn_buf) +{ + u32 opcode = insn_buf & __INSN_OPCODE_MASK; + u32 width, csr; + + /* + * All V-related instructions, including CSR operations are 4-Byte. So, + * do not handle if the instruction length is not 4-Byte. + */ + if (unlikely(GET_INSN_LENGTH(insn_buf) != 4)) + return false; + + switch (opcode) { + case RVV_OPCODE_VECTOR: + return true; + case RVV_OPCODE_VL: + case RVV_OPCODE_VS: + width = RVV_EXRACT_VL_VS_WIDTH(insn_buf); + if (width == RVV_VL_VS_WIDTH_8 || width == RVV_VL_VS_WIDTH_16 || + width == RVV_VL_VS_WIDTH_32 || width == RVV_VL_VS_WIDTH_64) + return true; + + break; + case RVG_OPCODE_SYSTEM: + csr = RVG_EXTRACT_SYSTEM_CSR(insn_buf); + if ((csr >= CSR_VSTART && csr <= CSR_VCSR) || + (csr >= CSR_VL && csr <= CSR_VLENB)) + return true; + } + + return false; +} + +static int riscv_v_thread_zalloc(void) +{ + void *datap; + + datap = kzalloc(riscv_v_vsize, GFP_KERNEL); + if (!datap) + return -ENOMEM; + + current->thread.vstate.datap = datap; + memset(¤t->thread.vstate, 0, offsetof(struct __riscv_v_ext_state, + datap)); + return 0; +} + +bool riscv_v_first_use_handler(struct pt_regs *regs) +{ + u32 __user *epc = (u32 __user *)regs->epc; + u32 insn = (u32)regs->badaddr; + + /* If V has been enabled then it is not the first-use trap */ + if (riscv_v_vstate_query(regs)) + return false; + + /* Get the instruction */ + if (!insn) { + if (__get_user(insn, epc)) + return false; + } + + /* Filter out non-V instructions */ + if (!insn_is_vector(insn)) + return false; + + /* Sanity check. datap should be null by the time of the first-use trap */ + WARN_ON(current->thread.vstate.datap); + + /* + * Now we sure that this is a V instruction. And it executes in the + * context where VS has been off. So, try to allocate the user's V + * context and resume execution. + */ + if (riscv_v_thread_zalloc()) { + force_sig(SIGKILL); + return true; + } + riscv_v_vstate_on(regs); + return true; +} From patchwork Tue May 9 10:30: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: 13235604 X-Patchwork-Delegate: palmer@dabbelt.com 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 24FE3C77B7C for ; Tue, 9 May 2023 10:32:40 +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=6ZlUrbW5AQglCmV+1d7Vwjju4gPR3KPlglYwhZeG2dA=; b=dUmVRQu8zkff2k 1Vy1bllEnd2moxawYer8FjdiX4i3LLK2EIZF+DqBM2ORfCknGyqXcl8K4c+MndLgC34lc2plL8tVE 1u/8SmTwVCR2TpqrKSDN6QoaTMUzvnuW0H9hHPsBmWMPj/Gdx5reCil6as++oADaogiex+pPDiLTB SsDZyvVqB9NsknAOOgDpRY9J1sWKZLu0So7xUWBS1sA+XLmfh5lrIGMUnfI739FBYdZCvawIW+dfa NXjJyK2sKqzm/is/qRyakncj6BxOzKpHeiDHpx/7EZULav4Yg3wO4H1+Nnfsx8U8BaXBr71yy7bOK PJPu34MsgZiHacUxSlig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKe1-002thE-1P; Tue, 09 May 2023 10:32:33 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKdy-002tCR-1p for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:32 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1aae5c2423dso55641725ad.3 for ; Tue, 09 May 2023 03:32:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628350; x=1686220350; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=I2ymYvWixmKAaJBFtVG3aqYYVLOHwMW6FuDRLqU5/PQ=; b=beQQS1OQeQU71bCN9jiZ9iBRB2uTEU53TcPdERLIRssvLxXiQg50aYxXWPog0ZvH/f 22bxr92y68YwXCTr2wiXvdF3RfUBA8cyVeL+VDoV5jYc45gKkvygwjekMHPvRgiWyncq TspC5iMz15yKlQPRbLXMk4Hem1NCyhRTJjcc+qJanygFrXitDBmDDO5upuHwfSwnu32g K4v/KXaZrcjPW5V/Yv2XPiIxsqjFxANw0DmJ8BO/VIJpickWPR1vfXQlb6lFIx0+eMZJ FXKQd/iBB4HPna/KSeGqvF14lY5kz4PstNgVMdJa/4Ynu700NnzFEP0ImbQW433avo4/ 1Rmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628350; x=1686220350; 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=I2ymYvWixmKAaJBFtVG3aqYYVLOHwMW6FuDRLqU5/PQ=; b=SsgXEYlx1tinSNo5GcUB99L2S8f+3Dfv/KXnHocZl66m632cByhFTsFstlutJwPTZx FKL2cGScyyFRhbAGtWwOPEAgOzIzatn0eY1wKKB8BwEfDxfScU8LACJCci21M7rRFgg2 u0BFVhcZaJyx79SZUj4KgCeYRuR/E7sebzKPh4gaQ0StRcUloM4wfoSSW5NnpIsAX4+R brp52jjeirzH76p2dCnmt60X5d5HiFmnJUTJU+BCV1bJP9+TQoAokGf6ndEULKbiTve4 SIGl83bGlVSnA8uIl9ZHVO+iS0CX8N6t1nRmyczxowZjONUabUWdL3HFcVi365ZdO05r ri8g== X-Gm-Message-State: AC+VfDxDyNC8rGycl8Rww+DNSEXa72X/uYsjP2mtpeCvsW1EClFlIsK1 HSb5LdUg5rQCTN/3Dvez4B/lsB1cGs9LAv0NLfTqZqapKBWI52wpZXNZebMrQbXND6QDxcS8IRb hbGvW1sGnoVoZUIKwAulgNo8Io3cAhX1EbPJ6mhqw8MUB4Jc9zx8EkSBQpcexbQ3v2LkTv0lD7O Mev9JLVRgBYaKJ X-Google-Smtp-Source: ACHHUZ5566jRAm+hfgXJLY8Rr+26toy5pjJB3WHRNTFIuLzCJ1lhOBC1GiGvBr7QzDiydEEydk9nQg== X-Received: by 2002:a17:902:d4c7:b0:1a6:7510:3341 with SMTP id o7-20020a170902d4c700b001a675103341mr17003259plg.69.1683628349487; Tue, 09 May 2023 03:32: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:28 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Oleg Nesterov , Eric Biederman , Kees Cook , Heiko Stuebner , Conor Dooley , Huacai Chen , Andrew Morton , Qing Zhang , Alexey Dobriyan , Rolf Eike Beer , Janosch Frank , Mark Brown Subject: [PATCH -next v19 12/24] riscv: Add ptrace vector support Date: Tue, 9 May 2023 10:30:21 +0000 Message-Id: <20230509103033.11285-13-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033230_605428_22B7E15D X-CRM114-Status: GOOD ( 21.95 ) 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 --- Changelog V18: - Use sizeof(vstate->datap) instead of sizeof(void*) (Eike) arch/riscv/include/uapi/asm/ptrace.h | 7 +++ arch/riscv/kernel/ptrace.c | 70 ++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 3 files changed, 78 insertions(+) diff --git a/arch/riscv/include/uapi/asm/ptrace.h b/arch/riscv/include/uapi/asm/ptrace.h index 586786d023c4..e8d127ec5cf7 100644 --- a/arch/riscv/include/uapi/asm/ptrace.h +++ b/arch/riscv/include/uapi/asm/ptrace.h @@ -94,6 +94,13 @@ struct __riscv_v_ext_state { */ }; +/* + * According to spec: The number of bits in a single vector register, + * VLEN >= ELEN, which must be a power of 2, and must be no greater than + * 2^16 = 65536bits = 8192bytes + */ +#define RISCV_MAX_VLENB (8192) + #endif /* __ASSEMBLY__ */ #endif /* _UAPI_ASM_RISCV_PTRACE_H */ diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 23c48b14a0e7..1d572cf3140f 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -7,6 +7,7 @@ * Copied from arch/tile/kernel/ptrace.c */ +#include #include #include #include @@ -24,6 +25,9 @@ enum riscv_regset { #ifdef CONFIG_FPU REGSET_F, #endif +#ifdef CONFIG_RISCV_ISA_V + REGSET_V, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -80,6 +84,61 @@ static int riscv_fpr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_ISA_V +static int riscv_vr_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + struct __riscv_v_ext_state *vstate = &target->thread.vstate; + + if (!riscv_v_vstate_query(task_pt_regs(target))) + return -EINVAL; + + /* + * Ensure the vector registers have been saved to the memory before + * copying them to membuf. + */ + if (target == current) + riscv_v_vstate_save(current, task_pt_regs(current)); + + /* Copy vector header from vstate. */ + membuf_write(&to, vstate, offsetof(struct __riscv_v_ext_state, datap)); + membuf_zero(&to, sizeof(vstate->datap)); + + /* Copy all the vector registers from vstate. */ + return membuf_write(&to, vstate->datap, riscv_v_vsize); +} + +static int riscv_vr_set(struct task_struct *target, + const struct user_regset *regset, + unsigned int pos, unsigned int count, + const void *kbuf, const void __user *ubuf) +{ + int ret, size; + struct __riscv_v_ext_state *vstate = &target->thread.vstate; + + if (!riscv_v_vstate_query(task_pt_regs(target))) + return -EINVAL; + + /* Copy rest of the vstate except datap */ + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate, 0, + offsetof(struct __riscv_v_ext_state, datap)); + if (unlikely(ret)) + return ret; + + /* Skip copy datap. */ + size = sizeof(vstate->datap); + count -= size; + ubuf += size; + + /* Copy all the vector registers. */ + pos = 0; + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap, + 0, riscv_v_vsize); + return ret; +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -99,6 +158,17 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_fpr_set, }, #endif +#ifdef CONFIG_RISCV_ISA_V + [REGSET_V] = { + .core_note_type = NT_RISCV_VECTOR, + .align = 16, + .n = ((32 * RISCV_MAX_VLENB) + + sizeof(struct __riscv_v_ext_state)) / sizeof(__u32), + .size = sizeof(__u32), + .regset_get = riscv_vr_get, + .set = riscv_vr_set, + }, +#endif }; static const struct user_regset_view riscv_user_native_view = { diff --git a/include/uapi/linux/elf.h b/include/uapi/linux/elf.h index ac3da855fb19..7d8d9ae36615 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -440,6 +440,7 @@ typedef struct elf64_shdr { #define NT_MIPS_DSP 0x800 /* MIPS DSP ASE registers */ #define NT_MIPS_FP_MODE 0x801 /* MIPS floating-point mode */ #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ +#define NT_RISCV_VECTOR 0x900 /* RISC-V vector registers */ #define NT_LOONGARCH_CPUCFG 0xa00 /* LoongArch CPU config registers */ #define NT_LOONGARCH_CSR 0xa01 /* LoongArch control and status registers */ #define NT_LOONGARCH_LSX 0xa02 /* LoongArch Loongson SIMD Extension registers */ From patchwork Tue May 9 10:30: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: 13235605 X-Patchwork-Delegate: palmer@dabbelt.com 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 91C26C7EE22 for ; Tue, 9 May 2023 10:32:44 +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=TZKjtlwj+2E27RE9ws1BdnHe2nxEy130Ys/PBRrEPaY=; b=xuh4JIaDj76tTd 48/bW2yeepy69o6KUO6M7BTCF1L3eReQ83CnvLi3062fUBZcj8C1mrdMHQFZG3e2Cml5FKYIE35PV UtNpkMk7owicimBFhimWSGi4SUId7UZO6o/hwSm2zKY/QsDFdKTOY2TRpkjAQGIYJKhJhxJP2ahmr pAl3c2oRoJzkkVfE4CFPqAomgvjTWOKnsQe+19Ch8j+SNFW7KFRc0jRbTJAQNg1eJsJBbaix2pHpy rZgTrq1Cqbu+nB5VrzvkE3wrVpnbX0stnjcwF6/sFSIV1Zf5KzcztXu9hz67gA56MXcc78Iwlka/r nWlKII7oRxPv/Zz02Isw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKe5-002tlc-2l; Tue, 09 May 2023 10:32:37 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKe3-002tiX-0m for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:37 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1aaec6f189cso38977325ad.3 for ; Tue, 09 May 2023 03:32:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628354; x=1686220354; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=wN7ZHBcxzp7ZsRkqqH3xjhadPlnuKqjQR/mkhzwbhmg=; b=ccuT5IOXOWZXEkVCczhFZQg1DR5ApUiwiB8G4HWAXcLvcWjy0n/7uX/tX0xWcCeAIp ygpMvtU3DicpYIZ1v3/Mz7ceykNagPLEo6d5v4G3lPVpmtLGwHsTIouFPuaRoUwKVydc 3M4+QtgPyg+Typ3Jkoyl11WGaeV8vPEx7miGPoBIK2jKe3FQ7r9oz6rxVuyNG2tS9gpH zsAf/LzqkyE5ImJOqBkabJ4JW0Jc+Txd4saDBjw6QuwVgyJlHKwsHlnzNWlfMaunK/ZM lEkb45jlwXwoOn4oPAjO1uQ9HGVE296cqjsu4NzPq2Yiyb9kQVmq5zCgQ+m7R+RGVbGG BIYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628354; x=1686220354; 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=wN7ZHBcxzp7ZsRkqqH3xjhadPlnuKqjQR/mkhzwbhmg=; b=hhSfcBN7lnAz6LJzkOYSIErUDbi2llBU3pbE5NjBDfjV8eOPHEe4GvpJQXhCbYKBH7 oXKCsa/Y0PtL/bCcuyhqoLRkzLpf094Uls6auHxKi25da8xd1OwCGAf2GcRm9S/nXPkk fv/v+dmA7ap3DS86f9Gpi3IkXNNdpxDFja5SXGbtGsJUd+aoWWk3YVCLh2KdurqH8Bys WMUkw6C6ZSg1IFFDqwaMII1jkKL47/aRg3XcmhZxU+ADeY9omgopIOSJGkFxomCNTMTs tmy5Y6CCZNLT/7/4CWpCN8SNlaccLoU6QYWneWNbWGdgVRVncmf5iaMDXlt5Bbt0V1Sy CC2Q== X-Gm-Message-State: AC+VfDwtgS1zc1PS5KUj9LnBDWXq1KW1qVDuwbP3mCzShWNH9Hwy1I3U L7lH0tLnQ6E2uUyK7UNwUsV71XgA9YcCtwzLIXrq4QH7ZY7vudQQ3Q9uINwQWl84ciJ7jMyuagV xTjvf0je5aaSZl60NiWZ5i6eLnf5TB7iY4H54s2NNRY0JkR4RjKz6VmXhwwRYeioObqSt9qrk6X psvQadGRKlpHmf X-Google-Smtp-Source: ACHHUZ5JVUtGHm+RvvF2AiQVn+ndz7H6SRxoKr9k5Gp1HsvibL5yeUcf5X09oziZZrqPYY/Z2Ayoaw== X-Received: by 2002:a17:902:f54b:b0:1aa:fe52:a827 with SMTP id h11-20020a170902f54b00b001aafe52a827mr18554926plf.13.1683628354237; Tue, 09 May 2023 03:32:34 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:33 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Guo Ren , Conor Dooley , Al Viro , Mathis Salmen , Vincent Chen , Andrew Bresticker Subject: [PATCH -next v19 13/24] riscv: signal: check fp-reserved words unconditionally Date: Tue, 9 May 2023 10:30:22 +0000 Message-Id: <20230509103033.11285-14-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033235_391067_401973D9 X-CRM114-Status: GOOD ( 18.36 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org In order to let kernel/user locate and identify an extension context on the existing sigframe, we are going to utilize reserved space of fp and encode the information there. And since the sigcontext has already preserved a space for fp context w or w/o CONFIG_FPU, we move those reserved words checking/setting routine back into generic code. This commit also undone an additional logical change carried by the refactor commit 007f5c3589578 ("Refactor FPU code in signal setup/return procedures"). Originally we did not restore fp context if restoring of gpr have failed. And it was fine on the other side. In such way the kernel could keep the regfiles intact, and potentially react at the failing point of restore. Signed-off-by: Andy Chiu Acked-by: Conor Dooley Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/signal.c | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 9aff9d720590..6b4a5c90bd87 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -40,26 +40,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, @@ -67,20 +54,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 @@ -92,11 +68,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; } @@ -147,11 +142,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 Tue May 9 10:30: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: 13235812 X-Patchwork-Delegate: palmer@dabbelt.com 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 BC379C77B7C for ; Tue, 9 May 2023 13:45:58 +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=OOTAEaEA0s9K+OV0vnJyUkZfaqquyna7TmH7m+m+42U=; b=J2C55aaJuDb8Ao Sfte7/2qMekduvTyPgNr84n7XWxqjmrLuluOVhQtYYZYOKdtIEAS5NwL1bbUr7qMJ6b/4hw1YPpBG xs/rwyL9pKm/egv7gmxyScXIvxcHyS8Yva0Vsa38wMbDkvQFN2Fpo7Xhmtg3rJC5nWI4kTMpX92B7 Wo9E+919/C3d0H3tOVWGzJ1mUTUaGB7PUYt7zYZ8HSfCnHVzsrw/w4ExVnvtwFbFu7f+kwccb7ZnT zev15mze64fjsRlag/ZcgK3gmBKxTrbBl5bW92YzSf+L53uwMgxAfn8dsVpmyifCARIqaTrqpbdSL QbonMYYlEwpE3ba9XnOw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwNf4-003RZW-1G; Tue, 09 May 2023 13:45:50 +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 1pwKeB-002tri-1B for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:45 +0000 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1aae46e62e9so40687545ad.2 for ; Tue, 09 May 2023 03:32:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628362; x=1686220362; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=ZUzGFhvm0UcaxflcgjWv0Vpwl9g6jcVWZE3ozpjtnAU=; b=c8ZvwnFutAMPiL2BcXAbByanEozsdEBBDlEKAcJ34h2/kCW+0NIuy0Mu87Dwe7pWlH g0gd7Fk49WRPGiuoyqzHsdPkFkHo1VSn3sRXtpvQiycV/4CGfUMzh7kgvX4Giv2/Mhzs wnIa37gTv7OB0AXr5EEDgKjuqCwjB7qB9bKyj/cHx0lD7Hjva8kUpFkFnKeigttRCYdL J8JPXgv3P0aRtYotkccQt+KXPMgcOXVrEKZ/smm6ysx/rzaActMB6UbYtCZA7DZV7LkV UWyPEQwMLMLN4gmmzZ6BAy6zA8KEkpWSkajI6QbqXZwpiwq+yg7hUi3zVeAsSvvHCnc3 /bug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628362; x=1686220362; 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=ZUzGFhvm0UcaxflcgjWv0Vpwl9g6jcVWZE3ozpjtnAU=; b=a546mqlOqMYUMfWJ1m4rsonFC4C+ZLo94r5LkfR1eGXNimAAGEw9Any1qqNVfNmEir fAKZwBFOJqY1TAZ/hGkUsHAur7PXXtN9xaEiNIQE5ds4bLJAe299M7iopUmbnmdtHHbO NcFLoSTh1285BjzQlgNvA4YW60jBCwVVTLgOZSFJ+XM4uz1R59bcXksOXTw2fHrY6jSi e4BgNZMGRGGKyg0/oNUbBTkE0tu0M0N98H8d6s/oZg7y5JzBncvukPcAl8AWaSi9Idea KRsj0IkJWZau+Dyet4DzvUUYFyZ0o0o/qhRwzpycorsV0JYahuc7ZKh5PlApuDqG90eO XcPQ== X-Gm-Message-State: AC+VfDze4uZRoPCiviX/dSR8VrHv2HOue4TRamf3PMbYx/1ehWlFmDkz M8VaZSWtdOJCwbsCQSRF81BLT4HNU9sRHrmq5mHxFwfPOhrJ08MaQGiojkDxrwNJh8VUmY6eiLH DqTD8IGCC6fhQJFw3VVU+PBlMrb0IhAFb88H37SC6WNrMmei/GXM9/ut8xD+tnhLlvmHpdWnoHF gHogpLHRs+JU4n X-Google-Smtp-Source: ACHHUZ4dNzp0t7MjP9ivN9eojd53i39ZqjwRGOndiJGutyUOm1AspaCS+Hs5ONBXcWuz9cKt3YavUw== X-Received: by 2002:a17:903:294d:b0:19d:1834:92b9 with SMTP id li13-20020a170903294d00b0019d183492b9mr12777266plb.56.1683628362159; Tue, 09 May 2023 03:32:42 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:41 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Conor Dooley , Richard Henderson , Alexandre Ghiti , Guo Ren , Wenting Zhang , Andrew Jones , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Jisheng Zhang , Xianting Tian , Mathis Salmen , Andrew Bresticker Subject: [PATCH -next v19 14/24] riscv: signal: Add sigcontext save/restore for vector Date: Tue, 9 May 2023 10:30:23 +0000 Message-Id: <20230509103033.11285-15-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033243_410698_45807FFB X-CRM114-Status: GOOD ( 32.32 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu This patch facilitates the existing fp-reserved words for placement of the first extension's context header on the user's sigframe. A context header consists of a distinct magic word and the size, including the header itself, of an extension on the stack. Then, the frame is followed by the context of that extension, and then a header + context body for another extension if exists. If there is no more extension to come, then the frame must be ended with a null context header. A special case is rv64gc, where the kernel support no extensions requiring to expose additional regfile to the user. In such case the kernel would place the null context header right after the first reserved word of __riscv_q_ext_state when saving sigframe. And the kernel would check if all reserved words are zeros when a signal handler returns. __riscv_q_ext_state---->| |<-__riscv_extra_ext_header ~ ~ .reserved[0]--->|0 |<- .reserved <-------|magic |<- .hdr | |size |_______ end of sc_fpregs | |ext-bdy| | ~ ~ +)size ------->|magic |<- another context header |size | |ext-bdy| ~ ~ |magic:0|<- null context header |size:0 | The vector registers will be saved in datap pointer. The datap pointer will be allocated dynamically when the task needs in kernel space. On the other hand, datap pointer on the sigframe will be set right after the __riscv_v_ext_state data structure. Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Suggested-by: Vineet Gupta Suggested-by: Richard Henderson Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu Acked-by: Conor Dooley Acked-by: Heiko Stuebner Tested-by: Heiko Stuebner --- Changelog V19: - Fix a conflict in signal.c due to commit 8d736482749f ("riscv: add icache flush for nommu sigreturn trampoline") 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 36b026057503..60ebe757ef20 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(); @@ -295,6 +297,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 6b4a5c90bd87..c46f3dc039bb 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -19,10 +19,12 @@ #include #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 @@ -64,12 +66,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)) @@ -82,32 +159,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))) @@ -141,17 +267,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; } @@ -176,6 +307,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; } @@ -185,9 +323,10 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct rt_sigframe __user *frame; long err = 0; unsigned long __maybe_unused addr; + 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); @@ -320,3 +459,10 @@ void arch_do_signal_or_restart(struct pt_regs *regs) */ restore_saved_sigmask(); } + +void init_rt_signal_env(void); +void __init init_rt_signal_env(void) +{ + riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) + + sizeof(struct __sc_riscv_v_state) + riscv_v_vsize; +} From patchwork Tue May 9 10:30:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235606 X-Patchwork-Delegate: palmer@dabbelt.com 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 10609C7EE22 for ; Tue, 9 May 2023 10:33: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: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=kqY9nfYBi5XI436Q90tOsRgodoA2r8B6wUFVOWMM+as=; b=BSXJZx9L/+nxEI uspK0SzFMXoqjH8Ljxb95ejL5Oo49AbzsDdZzVMGfAqtPnbqcnhxh0yfmylBOdR1iuHMtQq3JbT++ 4+WdpbVgFR0v/W50F3Cil9lnmjo7phl8XfR6ZdDQ8BA+INjS9tRkVHwaDAJ1J6QtfARW/dGnnex+s +owMrctZcBFnaBkLzQXuOXmuG8mkKCGb297ke8zUfgSSng+lm8KvGvIi0TnkaLIYr25Yos2iig1rg Mfp3s9RTt/jZ0n0DTV+O2rqgwOwFqje0VoMbDsOGjgPfnzExRyZy12it1G9BLxrxNgJkcCDORrctu leXQg472L0/d9FVI6xqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeP-002u5u-2E; Tue, 09 May 2023 10:32:57 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeL-002tz2-19 for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:55 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-64384274895so4024823b3a.2 for ; Tue, 09 May 2023 03:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628370; x=1686220370; 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=aKBdM10p42yS0Ug5E/KUJYm6/Rr1D5Li03q285TdFAw=; b=U9mKzV9TYD6a92nx2P+DpxRK+boTSXwr/0MaET345DZzdQxZN62yb7AwLXQ6zfCgg8 WilRuwECpUCthI6jZoS8Y4KZfdW19bq1RuN3nSyIdzkdaQmr3xAWOLLaEhB6Z7VBAdth HtYqIKngbY0Y9m4yrbvYHKuZRg5iB1YGjQM+8qisWg6u/3JWn2IpFcLCoFabDy2es8cZ lN5RYf/AWJMEwArnmpVfOErcTOBxf+pcMdrgPvl+So1BXViBVy0r8hVR4iQbs9GltGml q4Q2s92ELeJsC2AIPP8m9o5kGXaK8zPC/mzUr40mQgvk3AQf0q24DBfqgtJHqYRRf1DL DGNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628370; x=1686220370; 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=aKBdM10p42yS0Ug5E/KUJYm6/Rr1D5Li03q285TdFAw=; b=R+x+11WEf8CCdkGVY2RYo+nfk+xOKd9svnhq16YgTJDVhHkDlcrJ54Jc4nRjkGxBjI gOwl3miXSbDZKMAP5L5oHS/rs1pWoEKV2kGtT7UVOUbfagieuL4p2+XuzxTfPutVDE1j Z4x5Jq2vmYOxGVOF4seD3Uqv/aUHNoPoEEZU7CjFxkMkkc8zSw1on3sbrDrobNoopYbh l6Ot4GM3FsiWcw1Pf/US0PPSrDtRMysdIgXI1GTEto456XYahn1gQfxb9E9NWEHMp9Iw Ou7thI/1FC6LJ7+0fOdggA2m7MCRW9e3PMMUfRyhHCs7/CHYIwmZ1qQakwqLffuO5CWf Pg8A== X-Gm-Message-State: AC+VfDxewWuEdkEb+mWxI6eJg9oHChj3UZr5Eex+TD4+9of0b9ecK1hh D1/O6e3dv9ZTNs5lJ9WW3tcoKgZszYLylAGqO33nF8V9jni+WAx8hnKrieDrJWKQuIU05Xh7web MMlu4dxoOIzgdzRjTbrGt6Jl8ciEF1+4TnRLn5tiBeraRVBPuGe2j9ZudJXWdWZ/xpUQa0nFP2G WQNAzkNmoOcyme X-Google-Smtp-Source: ACHHUZ7ZTQOGT9/asxwGfiP68CsmX5wXxCNr+2oTYdT3b2w+YUC8AcbwGiz87KUJEvICWJWz2Gp4GA== X-Received: by 2002:a17:903:2292:b0:1a6:ebc1:c54c with SMTP id b18-20020a170903229200b001a6ebc1c54cmr17085754plh.1.1683628369884; Tue, 09 May 2023 03:32: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:49 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Subject: [PATCH -next v19 15/24] riscv: signal: Report signal frame size to userspace via auxv Date: Tue, 9 May 2023 10:30:24 +0000 Message-Id: <20230509103033.11285-16-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-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-20230509_033253_406016_56AEA9F8 X-CRM114-Status: GOOD ( 23.59 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kefeng Wang , guoren@linux.alibaba.com, Mathis Salmen , Kees Cook , Andrew Bresticker , vineetg@rivosinc.com, Vincent Chen , Conor Dooley , Albert Ou , Guo Ren , Eric Biederman , Andy Chiu , Paul Walmsley , greentime.hu@sifive.com, Zong Li , Heiko Stuebner Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Vincent Chen The vector register belongs to the signal context. They need to be stored and restored as entering and leaving the signal handler. According to the V-extension specification, the maximum length of the vector registers can be 2^16. Hence, if userspace refers to the MINSIGSTKSZ to create a sigframe, it may not be enough. To resolve this problem, this patch refers to the commit 94b07c1f8c39c ("arm64: signal: Report signal frame size to userspace via auxv") to enable userspace to know the minimum required sigframe size through the auxiliary vector and use it to allocate enough memory for signal context. Note that auxv always reports size of the sigframe as if V exists for all starting processes, whenever the kernel has CONFIG_RISCV_ISA_V. The reason is that users usually reference this value to allocate an alternative signal stack, and the user may use V anytime. So the user must reserve a space for V-context in sigframe in case that the signal handler invokes after the kernel allocating V. Signed-off-by: Greentime Hu Signed-off-by: Vincent Chen Signed-off-by: Andy Chiu Acked-by: Conor Dooley Reviewed-by: Björn Töpel Reviewed-by: Guo Ren Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- Changelog V19: - Fix a conflict in signal.c due to commit 8d736482749f ("riscv: add icache flush for nommu sigreturn trampoline") 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 c46f3dc039bb..f117641c1c49 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -23,6 +23,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; @@ -197,7 +199,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; @@ -205,8 +207,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 @@ -226,7 +230,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; @@ -323,7 +327,7 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct rt_sigframe __user *frame; long err = 0; unsigned long __maybe_unused addr; - 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)) @@ -465,4 +469,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 Tue May 9 10:30:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235803 X-Patchwork-Delegate: palmer@dabbelt.com 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 DABC6C7EE24 for ; Tue, 9 May 2023 13:00: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=s5LqxxpFVCjPfNdoxB3TWHJj9h54K0yGxDCMBztb/JQ=; b=ftRCZHF8DGCT9K UZZHhiCM5G2YA/rSmZWBnlwy3ASvi7lx2Ibf0rE3Q+BPFXlr+aqX0w/uQrWGIJgcWnDhy6wkEa4mA hx/rix4n/hghtWJudy+fJky3Pd5Sm/ZcDftEJ3WGENyXWn8q1MkX3ZrHzX5FpdfCPRX5FKYwofO7e YAFedIG+s/6mnPJadoqxZPM/D+jghgZBLQs70cmY4GQnYPqHn8AEiIrK5a468C6VMhsJAS4oR4bte TWVz/YTr9h11MTGhyb07w96qdAIrGaMbIvf2FFxXf1U1IJwA5pclXJP8XlSHldJI2JbZluo5hwITl 74w1RhUm0q98lEje6h3w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMxU-003Lsl-0K; Tue, 09 May 2023 13:00:48 +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 1pwKeR-002u6z-1i for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 10:32:59 +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=le6WsiFFpntTNhwR6ZdThyFuzfVMTXPYkTAyRr8nwRI=; b=p3A2jjPtcJgh+fFvceNXxJaCnv +0NX0cnrCZJ01Nz6UqxyxonL57m7QDdsL/qi6Uc2euWPPKRDv4brn8R4QQtNHA9z4D3ba2BvkwRmR pBSZwU1x+nOZ+KwAuxWqg908SRgtaawBmRGoNd0iVIWjawooBSoYSYfRI12inGilE+vqav3OgFJcR z3UWkj3tIhEcrhDRPbkiuw2mb14ky5mZfK+2B2GQtMDmvU2n8kL6cH6NvlRg1qW3weHTn0IcuKgGW Po4o5c7RxnHaBUlg+AXlXQ1hZYotfsNSs+SA4eciclNMdCBGpNZ0AZOlAqyQqEUYcOCCy08ELO2kI o5yesJxQ==; Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeO-00667t-0q for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:32:58 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1a5197f00e9so40114565ad.1 for ; Tue, 09 May 2023 03:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628374; x=1686220374; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=le6WsiFFpntTNhwR6ZdThyFuzfVMTXPYkTAyRr8nwRI=; b=dkcYXwuoWC+sWSXkRa1UkLXyr+4XeX3wyrwCnJW1m4VUC31Sc/0XGayn6JJ/j/cD00 9NAkxSPLSJk8NztvhxgyuIEcv6aGgxydyzXNVJqsi4T9U720OmiUDVYJYJKJZZm/J28Q ccwMzjTHVkzXDsF6ftKJiOuAIDQ9YAaJPwkwricp2/BqNbsYPzJEeYqo1PgLdkkB526W +32RH0S3cb7fUOpmYREceNRXiOsChWS7gHaCG2oNdLGikKclVjo8rLiVhPI/cvdloWvF 8KJx8n0bHIpvthsrQUCP3ToQIiDIkwiI+jpMJ351x8X8DkDyWa1i9VnYlHnBdFAYfSOY io/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628374; x=1686220374; 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=le6WsiFFpntTNhwR6ZdThyFuzfVMTXPYkTAyRr8nwRI=; b=ejOkUbxerHwfeviGD0oi3Phi8kDH2kjLLBEsh8lVLay1QK8pGNLKtMfDUyeoLgwXz1 p9BSTJUmBzAZ8UysGevjcAegIk/p8v0BCAPYs7vly72X0I0B1Wj0hGlX/7xV+j7dbqMd swYth+w/c26u2NCYcqna9QgFctf2CpoKeWHLhx71FT542xrOyyZ6zjAyIO2KjZurD7YD rNh0zeOujpCpUr6dsC7nSdTGiln+vog4yQ3v+7u9k7mEqW0gNaOeQmA/OMdCk/9DWPmU OcZAgZmdb4wUBCslFZt/tFXi3o7gcq9MgpOXAB6qZvaa0tsyow6q52Lp5PlOhpfjeQoJ tW+w== X-Gm-Message-State: AC+VfDymoGAKDuQKn6f/zHQXpUg7FI3JburQHUuoC/JxXL29Bwx+Tm1G w++BE2F6oJdKeEQszo4bdEynkjTa6sbeLI9ohgjfxkqkxW5ulhIEsjedaSBVFtCuu8PrJKanm6K GoIf9l1WiMl95gTOjnx881mO27atT/BdQfhvPE4wPha903Az7vSsnpz+48EE7dKI5Bv+bnYB/sE bWbeaCOm3Y4PDx X-Google-Smtp-Source: ACHHUZ4foWCrYcy+o18/SjgL1rsvMsKKMsfLDxh/nHILJosxokD4padIvzV9kYiYkeuD+KnqM3ICaQ== X-Received: by 2002:a17:902:bb8c:b0:1a1:ee8c:eef5 with SMTP id m12-20020a170902bb8c00b001a1ee8ceef5mr11531654pls.7.1683628374212; Tue, 09 May 2023 03:32:54 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:53 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Guo Ren , Conor Dooley , Andrew Bresticker , Vincent Chen Subject: [PATCH -next v19 16/24] riscv: signal: validate altstack to reflect Vector Date: Tue, 9 May 2023 10:30:25 +0000 Message-Id: <20230509103033.11285-17-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113256_549354_84A707DC X-CRM114-Status: GOOD ( 10.73 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some extensions, such as Vector, dynamically change footprint on a signal frame, so MINSIGSTKSZ is no longer accurate. For example, an RV64V implementation with vlen = 512 may occupy 2K + 40 + 12 Bytes of a signal frame with the upcoming support. And processes that do not execute any vector instructions do not need to reserve the extra sigframe. So we need a way to guard the allocation size of the sigframe at process runtime according to current status of V. Thus, provide the function sigaltstack_size_valid() to validate its size based on current allocation status of supported extensions. Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/signal.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index f117641c1c49..180d951d3624 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -476,3 +476,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 Tue May 9 10:30:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235725 X-Patchwork-Delegate: palmer@dabbelt.com 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 3BBEEC7EE24 for ; Tue, 9 May 2023 12:12: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=9W40tXwBb/IHTOhUfEvR1aMXU7QRaUyhQIV3Viue5JM=; b=FnrRQKsU/bWJ9r GbLzQidu/qbOtKBnMupfHpPwyJbbRIWHrcWJ9iZ9BKt3yymY34mhHhnpAG2X6ZzLDl7q5nsl4LAzX u+79ImwKCHUAzMrTPAQk+yslZy4NItXSCry781Wq90uLAk4VI/KqRT74T//72k2DiAdrneZKdxh+8 ZxndyVJw8RgTs0C4EKU73tLptKi8jBtiVPaBA5njXCIUA8qj3t+dOaHib+3Dgm+GZIHIhv+iFoeCn BRKQuA1mhCDU9VNW+3PF097csXkfV8RLDWe38J+IZuFQvhvfOwAAhNl0lZObTZ8iIJdRSkpFNmST6 34TGRvOsX7T6fh/iqltA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMCb-003FA6-0P; Tue, 09 May 2023 12:12:21 +0000 Received: from desiato.infradead.org ([90.155.92.199]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwMCY-003F8n-1d for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 12:12:18 +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=hWB54ZotS7Y2ZgKJV1SIpYTu+cs8XvC1qHz0MxJpXfU=; b=g3KoptGDxH2EM0wh2dAGJipP7L FvlNJURmL9yD6lUBFk12STP5cr+t3T2rUwlP1vEDSRqnc8XVOqOG6N3yznjZRpzuHFRhXlBep2KpB mBkGYanqGqc1Xf/GrK/6hhYW0bpWCNm/VyrUWRQ9a9IlcRMZtHE02JXduppRIBunojc6qqJoy7qXg NpMQ1sMEHhIWf7f0BYz3pBn8L3+D784elI003N4OUsH3nOX+AHunz3TSeJ09cM9jYajECEMjluIlS zIuCRAOi2P/77xN4rh70kIPzB7QfYq45bFyjMh7Phxx5hms5RzSJRy8tFJXDsQEWAZ0WoUtmibftH Jk/YceuA==; Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeU-00668C-0c for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:04 +0000 Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-5144a9c11c7so5344573a12.2 for ; Tue, 09 May 2023 03:33:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628379; x=1686220379; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=hWB54ZotS7Y2ZgKJV1SIpYTu+cs8XvC1qHz0MxJpXfU=; b=QkSGQgEeLO0u2CM+0ENpyxSPJrVs5RDZxlAodKF2CMos24WptSccSet4gExIwDxgKY J53EVXnz92lB7aO2d6gQHFtDbzXVUCM5MbFmhVWexLhy5xFqQhTKL1HCd6G6OrA62ayQ XSxva6pG1WoKVxi15tFy8H4EAjDnV+L4mxEjpCx43pdqaxP+/JYCPRb2nJmIzOHPJgf8 vdo1u82T8B/b1ar1wFoUR43j0kvuKZHIslOperfivstB7OWv7yhuVMYdfXB/yHRo+GuN YBOa11pFpVlI7ZgSjhHcjzFku4nq6xcooejKrOPSRTmQgYpXps+BjhUzW5FOoCmDRwiV uTjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628379; x=1686220379; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hWB54ZotS7Y2ZgKJV1SIpYTu+cs8XvC1qHz0MxJpXfU=; b=XW/6Hxawrlw60gB1ZcShCYCgTjS3mPE+F8W1DeKJMD81poO0z5vRQrwaa8CZ1FqwfQ yGEjRksRmra+ia2bJj94H6b92KoFHNeQuxsWyTUtCs/VZcqlNOHfLxxNj/GE9YodknBS AnBfvW9BlQQvTwLDIqcxH2x0Oqr99O+OC9pBc+7lSprDUISPsHmsmy/IeTG7l91JHT/n Ij9xGuqIRD7PF8oKtl82Iam6ALgGRBBWQO0SRhq9FVT/GqbqNcqnhuFlnbTRXnAV0sdg eZEzZnsbDEPrT848O6zJqEvXDLE3pHutfM+Ai7AkNUKSfcJG6IVHdi4X1cC0hJpU2n19 0GPA== X-Gm-Message-State: AC+VfDzkoVy1UJZ0E9+u0fDkwkFCJbvdXxQMOfSdtPLp9JC+RvKcd3nB gddF0gv3E93pqw1rTY3TPNzWO8n1UUGsF/pIVcT0l7IUGEILNgIxcYvzQGIyuPBizqly2dbI0Yg gaA11ayJ8vjNcnPAFjMK+wG7yVvzb+w8NKAiuNjgi+riy/1wHbAL+a7xqJCgI8PjmgQqK9NVo2h dV0g7DD968TbPv X-Google-Smtp-Source: ACHHUZ4XvQ8y8AUL5cpt7HXQnZP9qpclGxYr2ttP7vra/2g+xq2YwP3+ZiHmOQjumisSjZU4HbyLNA== X-Received: by 2002:a17:90a:e60e:b0:24e:1ca3:7279 with SMTP id j14-20020a17090ae60e00b0024e1ca37279mr13290490pjy.38.1683628378813; Tue, 09 May 2023 03:32:58 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:32:58 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, ShihPo Hung , Vincent Chen , Andy Chiu , Paul Walmsley , Albert Ou , Heiko Stuebner , Conor Dooley , Masahiro Yamada , Alexandre Ghiti , Guo Ren Subject: [PATCH -next v19 17/24] riscv: prevent stack corruption by reserving task_pt_regs(p) early Date: Tue, 9 May 2023 10:30:26 +0000 Message-Id: <20230509103033.11285-18-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113302_426694_94E7D7FC X-CRM114-Status: GOOD ( 10.13 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Greentime Hu Early function calls, such as setup_vm(), relocate_enable_mmu(), soc_early_init() etc, are free to operate on stack. However, PT_SIZE_ON_STACK bytes at the head of the kernel stack are purposedly reserved for the placement of per-task register context pointed by task_pt_regs(p). Those functions may corrupt task_pt_regs if we overlap the $sp with it. In fact, we had accidentally corrupted sstatus.VS in some tests, treating the kernel to save V context before V was actually allocated, resulting in a kernel panic. Thus, we should skip PT_SIZE_ON_STACK for $sp before making C function calls from the top-level assembly. Co-developed-by: ShihPo Hung Signed-off-by: ShihPo Hung Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/kernel/head.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index e16bb2185d55..11c3b94c4534 100644 --- a/arch/riscv/kernel/head.S +++ b/arch/riscv/kernel/head.S @@ -301,6 +301,7 @@ clear_bss_done: la tp, init_task la sp, init_thread_union + THREAD_SIZE XIP_FIXUP_OFFSET sp + addi sp, sp, -PT_SIZE_ON_STACK #ifdef CONFIG_BUILTIN_DTB la a0, __dtb_start XIP_FIXUP_OFFSET a0 @@ -318,6 +319,7 @@ clear_bss_done: /* Restore C environment */ la tp, init_task la sp, init_thread_union + THREAD_SIZE + addi sp, sp, -PT_SIZE_ON_STACK #ifdef CONFIG_KASAN call kasan_early_init From patchwork Tue May 9 10:30:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235801 X-Patchwork-Delegate: palmer@dabbelt.com 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 B8718C7EE22 for ; Tue, 9 May 2023 13: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=J7Zpk/i0vy9xvBHfYOIufpEZ46C+ZgqW+byMyoUfrIg=; b=aXeaWyydM21R+J jwJ33fAg7uaK3Y+byPSlGbXKCb/5I+F2m3Ft20DDZGSLnSfYea2B8VByr5uk0Xi0MSDj/gd9QRyCz Et4NhuHQvk71CVk/WbDqOrN63gHPWevzY0sNRuBItBKavSrBm8LfYrIxOqjnWzT/L+YjtSo+DGBt3 0RcOEkeAtR+wk/Htzi8XjvNvRoR6FIIsnsJEjtlwoWatfipon4l71NKJWY/eWjzhNdH5btdJrKRiS ktSMfWX/8kYMYyEPCcXWX877R41CSdZThbmh2DbPNafLqq5jVGZ6tRRTaSrkfdxkX6K6mRx3zX0yB 5mo3vQMOwBiUUHj1rdgw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMxU-003Lt1-1u; Tue, 09 May 2023 13:00:48 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeU-002tCR-2H for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:03 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1aae5c2423dso55649445ad.3 for ; Tue, 09 May 2023 03:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628382; x=1686220382; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=2CmyEkggETxM6j+XfIPwTDc2gU78hVyVqaTRQMELVN4=; b=D/421YHEpIqNI2B2tC62nRVwKRwWISvXzmsVHqTnpPS7sWEA5oV1gg2yv1CEkeGvOm bu1OOQ6Yf89bbybbbU0wwjnQwiQVjiB/EhJ03s/W50aVstzO9WgbfPOMzwwsnKGKI0to MZQ2nQbm8+QMiWVOZnDBjVZcsGMvGpMmQnb1HigNlJb39UODk9qWFugpBSB2zx4VIkny sBXzjT4kXsh3egVScNVaKPzn/3b8PGUP2CoQ7prUfczz+/WdJZ1tjNK8fzTX0PNwwj78 xL/z1eYT+/J+H40phb43402KErFEDLk32KMdSisouu6uFPJiGp8NE8k3YQV359BRYaP0 u77w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628382; x=1686220382; 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=2CmyEkggETxM6j+XfIPwTDc2gU78hVyVqaTRQMELVN4=; b=ONBEGxjQ3iBB931aelw6Y5NgV/owTV6nP25o+CNbr90aZxqcIyLpbgipVGT6LZ4JQs GV8ukkxvwAVrtuojO/MUHlRvYL1yP1gWHIHnGjIeYjF57Rluly3lHCDjkRDkSFJlEuv6 HYstjRixMMCwBMBIHnboW7IiwYipJfIkTY4RadAhb2WLJqer9uTk3yRWnEh5htY8l6Ni 5+tFC34jIK8F9yHpikdb4sGW+H8sBk6jXdcyeD42nAl2mAGHwlsvI9mWEXb7RpDbcokb CunuIQbCHP7gZg8Y9JZYt65C4pchxwm+2dkIIjCHmGLYtdN+d8VcE2o7u3cUSWhfWf+N Yxfw== X-Gm-Message-State: AC+VfDzpQ7jyK9D9pL5IzkPKisOQgUYwQ9VcitF5ROJ400YCilQF4p+l H+PBxyAF21Mh0Ph5GJ99ENa+VgIm+kYgD89Lpq7NttkQAElCPBJ4pnDSjepASW+E2I2RxoQDDIp huM/ncL5VGwy+px8+smHqRjz0DpwKh6yrZoOZUk2BPBT9iXpmoAGYMzv200ZWU4CkaEaxGwlv5P icq5qG+P/YZvyw X-Google-Smtp-Source: ACHHUZ4Ly10PFLn+5gXRgXrwx2fNdl0W+ClxbNID56SqwDXqCRw4c0/ArwzFpS0/+IbhyeeqFQd3tg== X-Received: by 2002:a17:90a:2b4a:b0:24e:49d4:bc42 with SMTP id y10-20020a17090a2b4a00b0024e49d4bc42mr13483367pjc.1.1683628381876; Tue, 09 May 2023 03:33: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33:01 -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 v19 18/24] riscv: kvm: Add V extension to KVM ISA Date: Tue, 9 May 2023 10:30:27 +0000 Message-Id: <20230509103033.11285-19-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033302_753956_EBF2A3F6 X-CRM114-Status: UNSURE ( 9.61 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Vincent Chen Add V extension to KVM isa extension list to enable supporting of V extension on VCPUs. Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Anup Patel Acked-by: Anup Patel Reviewed-by: Heiko Stuebner --- arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/vcpu.c | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index f92790c9481a..8feb57c4c2e8 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -121,6 +121,7 @@ enum KVM_RISCV_ISA_EXT_ID { KVM_RISCV_ISA_EXT_ZICBOZ, KVM_RISCV_ISA_EXT_ZBB, KVM_RISCV_ISA_EXT_SSAIA, + 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 8bd9f2a8a0b9..f3282ff371ca 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(SSAIA), KVM_ISA_EXT_ARR(SSTC), From patchwork Tue May 9 10:30:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235607 X-Patchwork-Delegate: palmer@dabbelt.com 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 255B2C77B7C for ; Tue, 9 May 2023 10:33:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Dkc8KSRhg646lBMbz6yk1jvFAOxNIF2YaDd6s3szDjw=; b=2Ugj0UiXNRCB9S BeJiibWczy3WaVfsHdm2M3PVAlyMB7HyRgnjlHAeFVZJ0mPqy2kPOZGahtl+RXmBDZVi7DqsXJxdD ONm3V6p9L09v+STb+n0cJ508b35AstlUdxJAHJEE3IdFztIDTC4S3H5qb32azAOJZPwzquwgil/yR 9quC4JlOONpJa3YQVTs2b8AmVrcFH4aKhIQz614zI5zdPzODngo3s/WIyDPscUdySZZMzkLoy6aoB 2E+LY+XtzIK5J05qZlK1uQxAPp70AHNswRcF/SkZ9RM0XkCzaf2ttqHElevypVrq4LJmCDW4AAip9 Lbv20f8L03tmKqyGRxOQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKec-002uJu-30; Tue, 09 May 2023 10:33:10 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeZ-002uFw-0X for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:09 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1aad55244b7so43152795ad.2 for ; Tue, 09 May 2023 03:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628386; x=1686220386; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=8YDLhw4J1iNBGhnqoBYiELHXPjXZy0Vp5vGq+fPQkxk=; b=UybZZiim5DlvChV/iuS2ZwmvLrP+jmQ/9211EVD9XIbW+X0UbnfeH9udj2wxYDE+SF I5/t+ejhRWpMRzFHlEp1JuEbGvqrTJdRbD+ems3IV9mofTsPbRV78KRdTjZncD/W4TZp lpE/cu1zgMHtVwyDfqPaAz+JKZihi0EwcfpmSO2pA+uZYv6ftxEVqMPtKbZCek5oPwOd 1fC5Q0j2tHX4o5qlo/ij+IIeyx12L108UMAGnLl9pExeaoPANsx9IMIJHReeV5ftitBS s8ZzWTRttKAUVs6XtBSBj0nKHbg5j/+uA8b8JwM70LgQtpBLUX/M2Quy6islaVvvKbSs pW9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628386; x=1686220386; 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=8YDLhw4J1iNBGhnqoBYiELHXPjXZy0Vp5vGq+fPQkxk=; b=aRjnYfbTuErmp+B/4B4j1xpTeodxtRI8dkk+8Z8xhq3cMdQvM1bvhqqh+ftvz6ZryA jxEB66k4ocwix0xIx7Tshj/wv3EH+F74tb1G5/yMKcW5xx3yDgb3HQ+YVvYLNbLItXk3 k8XYkhgP366/B+ovpgeMMqd2c/ORwmeaSL2WQfZEwgVv1QwQXWWMeeqUCj06z6+RNbbj f/803qOK8QNJrk2V7wtkuC5TT+9mzCCyUThx3DOboDdQl9MjTRGNOy+EgiUaHvba659F 1uXFSkw/+5optU8VX1s88pgz2qQaVJDMPY9Y+thA1LM+cFd0nhkyT4Q1HC9YgKIBxyyK uwRg== X-Gm-Message-State: AC+VfDxWaomJE9xQXMFizrcKltZfC7AsCNQMJcgToRarZ+cMsjAkAzuZ d1SzbMW0/SUJBok0+fYb3QbVzmTRQCYClaYPTXprZA4gSq5NY170Nhuo4GCsOk+VqphFWIsKNm2 frAcy8AXgiKvHcymggpoTzwwOBoLJYDeznmMfTLL4Bx8YWKosnylLXWx5ICODXwakxuvIMa2UhZ 3A+9NNe0bbFDq2 X-Google-Smtp-Source: ACHHUZ5P5SbyJuyRHjADeAzfw/bKqspYJW68lQuwmyDPg6QKkT2bprCA9dvmv3xJJDj0u8uZjtCtUA== X-Received: by 2002:a17:902:7d98:b0:1ac:7b13:de71 with SMTP id a24-20020a1709027d9800b001ac7b13de71mr6809892plm.37.1683628385968; Tue, 09 May 2023 03:33: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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33: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 Subject: [PATCH -next v19 19/24] riscv: KVM: Add vector lazy save/restore support Date: Tue, 9 May 2023 10:30:28 +0000 Message-Id: <20230509103033.11285-20-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033307_222138_C92D978C X-CRM114-Status: GOOD ( 24.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: Vincent Chen This patch adds vector context save/restore for guest VCPUs. To reduce the impact on KVM performance, the implementation imitates the FP context switch mechanism to lazily store and restore the vector context only when the kernel enters/exits the in-kernel run loop and not during the KVM world switch. Signed-off-by: Vincent Chen Signed-off-by: Greentime Hu Signed-off-by: Andy Chiu Reviewed-by: Anup Patel Acked-by: Anup Patel --- Changelog V19: - remap V extension registers as type 9 in uapi/asm/kvm.h 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 ee0acccb1d3b..bd47a1dc2ff8 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -145,6 +146,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 8feb57c4c2e8..855c047e86d4 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -204,6 +204,13 @@ enum KVM_RISCV_SBI_EXT_ID { #define KVM_REG_RISCV_SBI_MULTI_REG_LAST \ KVM_REG_RISCV_SBI_MULTI_REG(KVM_RISCV_SBI_EXT_MAX - 1) +/* V extension registers are mapped as type 9 */ +#define KVM_REG_RISCV_VECTOR (0x09 << 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 8031b8912a0d..7b4c21f9aa6a 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 f3282ff371ca..e5e045852e6a 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(), @@ -139,6 +141,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); kvm_riscv_vcpu_aia_reset(vcpu); @@ -199,6 +203,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; @@ -242,6 +249,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) @@ -680,6 +690,9 @@ static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, return kvm_riscv_vcpu_set_reg_isa_ext(vcpu, reg); case KVM_REG_RISCV_SBI_EXT: return kvm_riscv_vcpu_set_reg_sbi_ext(vcpu, reg); + case KVM_REG_RISCV_VECTOR: + return kvm_riscv_vcpu_set_reg_vector(vcpu, reg, + KVM_REG_RISCV_VECTOR); default: break; } @@ -709,6 +722,9 @@ static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, return kvm_riscv_vcpu_get_reg_isa_ext(vcpu, reg); case KVM_REG_RISCV_SBI_EXT: return kvm_riscv_vcpu_get_reg_sbi_ext(vcpu, reg); + case KVM_REG_RISCV_VECTOR: + return kvm_riscv_vcpu_get_reg_vector(vcpu, reg, + KVM_REG_RISCV_VECTOR); default: break; } @@ -1003,6 +1019,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); kvm_riscv_vcpu_aia_load(vcpu, cpu); @@ -1022,6 +1041,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 Tue May 9 10:30:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235846 X-Patchwork-Delegate: palmer@dabbelt.com 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 8FCDFC7EE24 for ; Tue, 9 May 2023 14:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Q6jGJpXeMSG+s/ebSIPpW1HGwTUmm6n4GgJTWx5dtYI=; b=YBLlIfcGzIOn5Q9bm/E0qQ16r7 B7Q19w8CIgtqGf5B7tEmSnpygFSBuaDyYN7ZeO1B8o0OGWLEQlrvKpdkO4a/RCVU4PRHQnCtRbSw+ POJ60cjOtvskADJeUbFCLYVrsHvjryEs7Vs1RG0lDHsiNPdw4nFJG/Pi18Cc0bMl+u7sa4Z4uygTr y8zm6pfDi14ibbdcWbiivIMPabLASr8E2KKLo/4L91iEUXnHJoO7L7cKynJTzQB7K574avWCSk6qK 7bJGd6+nCkgTLCEyMA5JjA+l2cI/EvAsf35CvQlA9rLuAsElBhjM/JWTj4W9ERRNJFSQ7ySUKkLPS y5gkcdfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwOX1-003aoH-04; Tue, 09 May 2023 14:41:35 +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 1pwOWz-003ams-1v for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 14:41:33 +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=Wvw2X3Tokh9Yvndr5oaHktaTH/5I/tzZU3ea/umrEpk=; b=hDpL3YG4VtnN2OmSHmOst6Yru4 SonpzvSlLjOhSVDSpOa+iv1U4B1Ukblur3U3siZdbwVKLHtnoF50G3AEccFoDMzInSQ/5kByT+8Jg aumoOpMHmvGxmu9TDskB5cVrkftbgH1ep0nzqulcykjFDCuBD3okkMKds27s+iUAFVIUCMT/P3zXE /0XePKq8LhP7oT3hMkQl8QdweRLLP0LOPGkqGa5z0vhKll/t56H7OsDD1jjkmRiAWWqoEs3LWx7cc SBFMKgxSY2TWHy9LDgLCv99cmRC3q9SnKvSvT5vRYGTwv72SXHNp5IqfHvQvU3PHE9GzjIOxpaDTt SgrZd0ug==; Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKem-00669J-02 for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:22 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1aaebed5bd6so40014195ad.1 for ; Tue, 09 May 2023 03:33:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628397; x=1686220397; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=Wvw2X3Tokh9Yvndr5oaHktaTH/5I/tzZU3ea/umrEpk=; b=QRTL+hWhxW4k8g47KY4jhuizJpE4f6MzqJaBVF5nTX/w0uOtimQpy5JXhZd2nkq3l3 a239pNjtOqrNUy31ccEniSf+9uTF95z8Vy5mZWZqElzwQMrjGJgrUkV65+mjaWUtDdUB f/h2tlt4VORNhhJKdqPt+CZLpzQB4nC2Cfo2ziubk8rqSfq0wheKIg7hL8/pH4ZQPDG8 DLB1jFW149Mm9N2ZIEVT6qXxWORYHIyesKiUWCAOcj3B5KOw1VFZmxH5O8ZfID/NXmDb jM6HGo2VjD3tF/a3xTkXwGq6as0+17RujTWHjPK8GindrY1qhOuFias7+4j2dkePD4Hn 3+YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628397; x=1686220397; 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=Wvw2X3Tokh9Yvndr5oaHktaTH/5I/tzZU3ea/umrEpk=; b=Wi1tVY+8LrW5iNWfsZ4UeBNu/cs8eUNLMzIkXfznvMZhaEheBgcu91VQPeomFnpXD+ TXPSUNGLdflqcM+5YLidm1mNv/RuRhVM31liWj2xv3kg70fKrGMh35uZGeNmb4Ys2PYn Uy24NnOSCXLS66/b2EYfqajUoK25Md+wpTaCaTqQkY9rCIxMzp/t49SemCjRRryzBKWL 3P8AjPHEeC9gOwAIDTMDU7DSdnUx4OiyPQaxsg5ehgKPFIP51dBLU0hK25iDWpQsYFWa NfZElsEbxKlPHe14tHjt22mlZ0db6PuMxy/p5qwHwxtWYauYLnp22Wrl00SXlAQqGlBP Zo5Q== X-Gm-Message-State: AC+VfDxDm4UgELZtBHzXD5WKzvkLgdUfzGbihtQwUg6RX/ydCBqOCOgh OGQUW60nlQXVLfR+yXttEQnmZ3Sirzr6EyKluEpMfaLPMTf/KhBq3Ty9brgPxGrqKFUg3UeLf2b Bn2ETeQuBMRWJI/pGjDuKoi1saEG3ZW9QVUdO94J1An5TXr2NPgulouD/oyAqYO3nRuZhhMcBub MjxBsm2bF2n2eS X-Google-Smtp-Source: ACHHUZ4f9SOwyh878xI6SJQQvLHb3oaXlmqoP4mKlWBBXS2d+KTOP7wOVmrQ36zCozyw75eag3fDLw== X-Received: by 2002:a17:903:2304:b0:1a6:a7d4:7474 with SMTP id d4-20020a170903230400b001a6a7d47474mr14484782plh.44.1683628397316; Tue, 09 May 2023 03:33:17 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33: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 Subject: [PATCH -next v19 20/24] riscv: Add prctl controls for userspace vector management Date: Tue, 9 May 2023 10:30:29 +0000 Message-Id: <20230509103033.11285-21-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113320_194304_A561E3C0 X-CRM114-Status: GOOD ( 22.71 ) 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, David Hildenbrand , Peter Zijlstra , Catalin Marinas , "Jason A. Donenfeld" , Joey Gouly , Conor Dooley , Guo Ren , Jisheng Zhang , greentime.hu@sifive.com, Albert Ou , Stefan Roesch , vineetg@rivosinc.com, Josh Triplett , Paul Walmsley , Heiko Stuebner , Jordy Zomer , Ondrej Mosnacek , Vincent Chen , "Eric W. Biederman" , Andy Chiu , Andrew Morton MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This patch add two riscv-specific prctls, to allow usespace control the use of vector unit: * PR_RISCV_V_SET_CONTROL: control the permission to use Vector at next, or all following execve for a thread. Turning off a thread's Vector live is not possible since libraries may have registered ifunc that may execute Vector instructions. * PR_RISCV_V_GET_CONTROL: get the same permission setting for the current thread, and the setting for following execve(s). Signed-off-by: Andy Chiu Reviewed-by: Greentime Hu Reviewed-by: Vincent Chen --- arch/riscv/include/asm/processor.h | 13 ++++ arch/riscv/include/asm/vector.h | 4 ++ arch/riscv/kernel/process.c | 1 + arch/riscv/kernel/vector.c | 108 +++++++++++++++++++++++++++++ arch/riscv/kvm/vcpu.c | 2 + include/uapi/linux/prctl.h | 11 +++ kernel/sys.c | 12 ++++ 7 files changed, 151 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 38ded8c5f207..79261da74cfd 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -40,6 +40,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long vstate_ctrl; struct __riscv_v_ext_state vstate; }; @@ -83,6 +84,18 @@ 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; + +#ifdef CONFIG_RISCV_ISA_V +/* Userspace interface for PR_RISCV_V_{SET,GET}_VS prctl()s: */ +#define RISCV_V_SET_CONTROL(arg) riscv_v_vstate_ctrl_set_current(arg) +#define RISCV_V_GET_CONTROL() riscv_v_vstate_ctrl_get_current() +extern unsigned int riscv_v_vstate_ctrl_set_current(unsigned long arg); +extern unsigned int riscv_v_vstate_ctrl_get_current(void); +#else /* !CONFIG_RISCV_ISA_V */ +#define RISCV_V_SET_CONTROL(arg) (-EINVAL) +#define RISCV_V_GET_CONTROL() (-EINVAL) +#endif /* CONFIG_RISCV_ISA_V */ + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index a8881af83ce4..e7db2d373044 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -160,6 +160,9 @@ static inline void __switch_to_vector(struct task_struct *prev, riscv_v_vstate_restore(next, task_pt_regs(next)); } +void riscv_v_vstate_ctrl_init(struct task_struct *tsk); +bool riscv_v_user_allowed(void); + #else /* ! CONFIG_RISCV_ISA_V */ struct pt_regs; @@ -168,6 +171,7 @@ static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } +static inline bool riscv_v_user_allowed(void) { return false; } #define riscv_v_vsize (0) #define riscv_v_vstate_save(task, regs) do {} while (0) #define riscv_v_vstate_restore(task, regs) do {} while (0) diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index b7a10361ddc6..60278233926c 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -149,6 +149,7 @@ void flush_thread(void) #endif #ifdef CONFIG_RISCV_ISA_V /* Reset vector state */ + riscv_v_vstate_ctrl_init(current); riscv_v_vstate_off(task_pt_regs(current)); kfree(current->thread.vstate.datap); memset(¤t->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 960a343799c6..16ccb35625a9 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -19,6 +20,8 @@ #include #include +static bool riscv_v_implicit_uacc = !IS_ENABLED(CONFIG_RISCV_V_DISABLE); + unsigned long riscv_v_vsize __read_mostly; EXPORT_SYMBOL_GPL(riscv_v_vsize); @@ -91,11 +94,51 @@ static int riscv_v_thread_zalloc(void) return 0; } +#define VSTATE_CTRL_GET_CUR(x) ((x) & PR_RISCV_V_VSTATE_CTRL_CUR_MASK) +#define VSTATE_CTRL_GET_NEXT(x) (((x) & PR_RISCV_V_VSTATE_CTRL_NEXT_MASK) >> 2) +#define VSTATE_CTRL_MAKE_NEXT(x) (((x) << 2) & PR_RISCV_V_VSTATE_CTRL_NEXT_MASK) +#define VSTATE_CTRL_GET_INHERIT(x) (!!((x) & PR_RISCV_V_VSTATE_CTRL_INHERIT)) +static inline int riscv_v_get_cur_ctrl(struct task_struct *tsk) +{ + return VSTATE_CTRL_GET_CUR(tsk->thread.vstate_ctrl); +} + +static inline int riscv_v_get_next_ctrl(struct task_struct *tsk) +{ + return VSTATE_CTRL_GET_NEXT(tsk->thread.vstate_ctrl); +} + +static inline bool riscv_v_test_ctrl_inherit(struct task_struct *tsk) +{ + return VSTATE_CTRL_GET_INHERIT(tsk->thread.vstate_ctrl); +} + +static inline void riscv_v_set_ctrl(struct task_struct *tsk, int cur, int nxt, + bool inherit) +{ + unsigned long ctrl; + + ctrl = cur & PR_RISCV_V_VSTATE_CTRL_CUR_MASK; + ctrl |= VSTATE_CTRL_MAKE_NEXT(nxt); + if (inherit) + ctrl |= PR_RISCV_V_VSTATE_CTRL_INHERIT; + tsk->thread.vstate_ctrl = ctrl; +} + +bool riscv_v_user_allowed(void) +{ + return riscv_v_get_cur_ctrl(current) == PR_RISCV_V_VSTATE_CTRL_ON; +} + bool riscv_v_first_use_handler(struct pt_regs *regs) { u32 __user *epc = (u32 __user *)regs->epc; u32 insn = (u32)regs->badaddr; + /* Do not handle the trap if V is not allowed for this process*/ + if (!riscv_v_user_allowed()) + return false; + /* If V has been enabled then it is not the first-use trap */ if (riscv_v_vstate_query(regs)) return false; @@ -125,3 +168,68 @@ bool riscv_v_first_use_handler(struct pt_regs *regs) riscv_v_vstate_on(regs); return true; } + +void riscv_v_vstate_ctrl_init(struct task_struct *tsk) +{ + bool inherit; + int cur, next; + + next = riscv_v_get_next_ctrl(tsk); + if (!next) { + if (riscv_v_implicit_uacc) + cur = PR_RISCV_V_VSTATE_CTRL_ON; + else + cur = PR_RISCV_V_VSTATE_CTRL_OFF; + } else { + cur = next; + } + /* Clear next mask if inherit-bit is not set */ + inherit = riscv_v_test_ctrl_inherit(tsk); + if (!inherit) + next = PR_RISCV_V_VSTATE_CTRL_DEFAULT; + + riscv_v_set_ctrl(tsk, cur, next, inherit); +} + +unsigned int riscv_v_vstate_ctrl_get_current(void) +{ + return current->thread.vstate_ctrl & PR_RISCV_V_VSTATE_CTRL_MASK; +} + +unsigned int riscv_v_vstate_ctrl_set_current(unsigned long arg) +{ + bool inherit; + int cur, next; + + if (arg & ~PR_RISCV_V_VSTATE_CTRL_MASK) + return -EINVAL; + + cur = VSTATE_CTRL_GET_CUR(arg); + switch (cur) { + case PR_RISCV_V_VSTATE_CTRL_OFF: + /* Do not allow user to turn off V if current is not off */ + if (riscv_v_get_cur_ctrl(current) != PR_RISCV_V_VSTATE_CTRL_OFF) + return -EPERM; + + break; + case PR_RISCV_V_VSTATE_CTRL_ON: + break; + case PR_RISCV_V_VSTATE_CTRL_DEFAULT: + cur = riscv_v_get_cur_ctrl(current); + break; + default: + return -EINVAL; + } + + next = VSTATE_CTRL_GET_NEXT(arg); + inherit = VSTATE_CTRL_GET_INHERIT(arg); + switch (next) { + case PR_RISCV_V_VSTATE_CTRL_DEFAULT: + case PR_RISCV_V_VSTATE_CTRL_OFF: + case PR_RISCV_V_VSTATE_CTRL_ON: + riscv_v_set_ctrl(current, cur, next, inherit); + return 0; + } + + return -EINVAL; +} diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e5e045852e6a..704968b71272 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -88,6 +88,8 @@ static bool kvm_riscv_vcpu_isa_enable_allowed(unsigned long ext) switch (ext) { case KVM_RISCV_ISA_EXT_H: return false; + case KVM_RISCV_ISA_EXT_V: + return riscv_v_user_allowed(); default: break; } diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index f23d9a16507f..3c36aeade991 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -294,4 +294,15 @@ struct prctl_mm_map { #define PR_SET_MEMORY_MERGE 67 #define PR_GET_MEMORY_MERGE 68 + +#define PR_RISCV_V_SET_CONTROL 69 +#define PR_RISCV_V_GET_CONTROL 70 +# define PR_RISCV_V_VSTATE_CTRL_DEFAULT 0 +# define PR_RISCV_V_VSTATE_CTRL_OFF 1 +# define PR_RISCV_V_VSTATE_CTRL_ON 2 +# define PR_RISCV_V_VSTATE_CTRL_INHERIT (1 << 4) +# define PR_RISCV_V_VSTATE_CTRL_CUR_MASK 0x3 +# define PR_RISCV_V_VSTATE_CTRL_NEXT_MASK 0xc +# define PR_RISCV_V_VSTATE_CTRL_MASK 0x1f + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 339fee3eff6a..412d2c126060 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -140,6 +140,12 @@ #ifndef GET_TAGGED_ADDR_CTRL # define GET_TAGGED_ADDR_CTRL() (-EINVAL) #endif +#ifndef PR_RISCV_V_SET_CONTROL +# define PR_RISCV_V_SET_CONTROL(a) (-EINVAL) +#endif +#ifndef PR_RISCV_V_GET_CONTROL +# define PR_RISCV_V_GET_CONTROL() (-EINVAL) +#endif /* * this is where the system-wide overflow UID and GID are defined, for @@ -2708,6 +2714,12 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags); break; #endif + case PR_RISCV_V_SET_CONTROL: + error = RISCV_V_SET_CONTROL(arg2); + break; + case PR_RISCV_V_GET_CONTROL: + error = RISCV_V_GET_CONTROL(); + break; default: error = -EINVAL; break; From patchwork Tue May 9 10:30:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235789 X-Patchwork-Delegate: palmer@dabbelt.com 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 1D935C77B7C for ; Tue, 9 May 2023 12:48:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=iBl0cZ1ByhGq29YikE8wkkGDG7jh0HGt7LtphlHMzMM=; b=jJ/t4Xm7EBgpLP 5O8R1Ogul8SknHVYDvEvVPZM9SVwK8zGlWjhnvoZiUUnotoRZ51kBn5cBEspDutEgsEZ0lJBrtBtF 5CNGRyoKHn1tGcP6SRbynE/3QTc/HWJVx4iOgKalKXgkJPbTbYxO9XqD1Ur9kC5n59Lsf4XjLX/Rb a2T4QHV/3J8jZDVUI5cW6AYPdba17DdzkkgcnzPxDaMpJb2k+tMszEG3gKMSw9UeECrzrtp2h7og0 zgFkNsMyWa7M2mDBxnkcxwIWBAE5taLRjMzjmBGDx5iC8ZzVhZgodxt20csSSouPL2IV0c4zYufd2 iLkAVxt2/GiWc4ILYQqQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMl0-003KFQ-2W; Tue, 09 May 2023 12:47:54 +0000 Received: from desiato.infradead.org ([90.155.92.199]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwMkv-003KCb-2k for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 12:47:50 +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=941FZ9JR8q5CEhK2jSJwDse+5gwyYf1wDxuvPa9cwEY=; b=ae+AKqtk+Mp35xM7biefREYSMe dLDRmxwFrPwuaMwGkG1p0Pd8JKOJ1iTU5MvR5GOFqPy3RM8q5NRAcqG8XIjqar/aVJ9X8DEOVO1Yy 8IeMXvhJ7Mtb6tZcWv5zuSURQdt1AEERTbVR7iefbpXTYSQiv8ecS+9ct3th/DXbk4I67umLGtSk/ hl6VbA0Vex7GHO3ENiMH8JDUcqvGhKzn/p7NX8ow/dsMSDgF5xP8XoLU2YrhoDRNJXBmghh92V+73 BjYGnBN5msWMpCMK0SHCbispIUMkB0Srv9zq43912mdRFFMjwyYNAks1dfYb27iwGIlf9rbDUW/oz o5Dc1j5g==; Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKeq-00669x-2N for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:28 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1aad5245632so40655875ad.3 for ; Tue, 09 May 2023 03:33:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628402; x=1686220402; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=941FZ9JR8q5CEhK2jSJwDse+5gwyYf1wDxuvPa9cwEY=; b=An7cnHT+loomL/g7RPJmFt75o6e0J+e4OnOK/7+TXoVdE0nczQr9FSgNtsTA8dGa6l oXlXl7mwBZOFnD6EQIrBdlqArIc9rVndl/oGZPf/MWlv6mO2tjsTT03YHNw1ywBFECAW FKU9PP5P50HYEcMnrxwzcAj03n5dxcznQO6tM3Au55xmfaUb0CY4IG8AQ4H30aNtKOZn yVVKy+VO6yNMuIU2hydnKL9Na7YRePysTlTdtSn1z0BqqWy2aSgG927vfJg7IpJHaAgL ni330YvIhU3/uX84oahdFv/wkb3JsgKE01aKtRdEcWK+pM3q/i3jvEjy0++kjkvrUkfN yD/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628402; x=1686220402; 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=941FZ9JR8q5CEhK2jSJwDse+5gwyYf1wDxuvPa9cwEY=; b=TDJQCUnVt+nqPeJ3PCMwAScuLln4S9HNFGB5n0pSNYp/nUW3z9V8BxciAnDRlZQWIV MfbDSEvdPAMJJ9zWF5jRfoKkS2hI8aVfq8KCJSyGM+o3tWLspHAER68EhasqZEfgp45a J2dFt8NIQLGqHZMe86oMM56hGGTTo21wgu0WcekVlPrkg3HYDzXxE9ivh4d2dvJHtni6 OCRcIxoY5oOANlGxBHEcS3VygKD3rJew88TsGtAoYYdpwDoctZIJnlLcYVc4iqQHz05b BFbp9eoccT878o7vFVcvnIpPhhalhARktQG0PJRJJJYSjdaY4R2LofbBadoJEuAmszLO oGhw== X-Gm-Message-State: AC+VfDwh4uK6uSBjhUZ4BuKo9J2K8qXSNZrtEW6+IHM9Vk7OA8ekXyy9 M+9gb/TG0Q4rKC/kMOIYsblKk2IgiDBCy6lRutAXb6swD8hdD2bv+xaGvoLBB3vGbTb9wa9ruDP Bk1YwI66RnvIx0oDPDzYtRo/y3wTAnpEh+ecOUlzRNWTDEPvL1almrlWTT/yqweuZ4JIZ7EMDTE C6YDMKp63xHSp+ X-Google-Smtp-Source: ACHHUZ4L8N85doaQ33hgQswXAJ2/iV0cFZ2Wf2RzXa8ycldgql4vMle8s5Eb4TVPCsn5L6rx7lVvJA== X-Received: by 2002:a17:902:b583:b0:1a9:20ea:f49b with SMTP id a3-20020a170902b58300b001a920eaf49bmr14245517pls.24.1683628402118; Tue, 09 May 2023 03:33:22 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33:21 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Paul Walmsley , Albert Ou , Vincent Chen , Heiko Stuebner , Guo Ren Subject: [PATCH -next v19 21/24] riscv: Add sysctl to set the default vector rule for new processes Date: Tue, 9 May 2023 10:30:30 +0000 Message-Id: <20230509103033.11285-22-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113324_862813_3ADA845D X-CRM114-Status: GOOD ( 13.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 To support Vector extension, the series exports variable-length vector registers on the signal frame. However, this potentially breaks abi if processing vector registers is required in the signal handler for old binaries. For example, there is such need if user-level context switch is triggerred via signals[1]. For this reason, it is best to leave a decision to distro maintainers, where the enablement of userspace Vector for new launching programs can be controlled. Developers may also need the switch to experiment with. The parameter is configurable through sysctl interface so a distro may turn off Vector early at init script if the break really happens in the wild. The switch will only take effects on new execve() calls once set. This will not effect existing processes that do not call execve(), nor processes which has been set with a non-default vstate_ctrl by making explicit PR_RISCV_V_SET_CONTROL prctl() calls. Link: https://lore.kernel.org/all/87cz4048rp.fsf@all.your.base.are.belong.to.us/ Signed-off-by: Andy Chiu Reviewed-by: Greentime Hu Reviewed-by: Vincent Chen --- arch/riscv/kernel/vector.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 16ccb35625a9..1c4ac821e008 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -233,3 +233,34 @@ unsigned int riscv_v_vstate_ctrl_set_current(unsigned long arg) return -EINVAL; } + +#ifdef CONFIG_SYSCTL + +static struct ctl_table riscv_v_default_vstate_table[] = { + { + .procname = "riscv_v_default_allow", + .data = &riscv_v_implicit_uacc, + .maxlen = sizeof(riscv_v_implicit_uacc), + .mode = 0644, + .proc_handler = proc_dobool, + }, + { } +}; + +static int __init riscv_v_sysctl_init(void) +{ + if (has_vector()) + if (!register_sysctl("abi", riscv_v_default_vstate_table)) + return -EINVAL; + return 0; +} + +#else /* ! CONFIG_SYSCTL */ +static int __init riscv_v_sysctl_init(void) { return 0; } +#endif /* ! CONFIG_SYSCTL */ + +static int riscv_v_init(void) +{ + return riscv_v_sysctl_init(); +} +core_initcall(riscv_v_init); From patchwork Tue May 9 10:30:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235802 X-Patchwork-Delegate: palmer@dabbelt.com 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 484DBC77B7C for ; Tue, 9 May 2023 13:00:58 +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=1GPLtW/TuHg6KoOshQ6ax3kZwDybZyRr1BYPdqVgRwY=; b=Rv6bTJL3JHBEdl qlvXofJxf4Mg5P8h9b5SGgbdyTD2YaUB88AYfjHb9QPSOQk8Yst4gbWp9WUFZ5fdBbrx2PIkO55lW ilh1QKYt4Y8OoGFmZD/MHqesUS0ag7iqOV1yFRV9D+MAp08CYpVGBh1Nkn7ThQ17tFdN8eIBTzJNm WdvPDcG+BZwoP1ngDYba4EPmkta9cOFZn3QYJ8E0YTrgyaZtvIOo9jNVr/P259CYbWqSTpdvX/KsY EVvXGDZucPRc1Me9LDqr9wefA2R4qfQlfbHy0xwltdSxl8/uQnLwdEOtkELfy/j4buU3Cy7yloQav D3sHwj2ASusPEVnxO41g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMxV-003LtG-0J; Tue, 09 May 2023 13:00:49 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKes-002uaE-1k for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:28 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-64115e652eeso41451266b3a.0 for ; Tue, 09 May 2023 03:33:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628405; x=1686220405; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=otAeXCQ9wHQgYb9XVLjxDELtJAEuURMD8rtVKOhLCTY=; b=a9gJeRbmISiXvK9+ul3SdWxI8z70Xub/NyicER2OxvyXBKL8WAsFJ+NZKPU4fljCMg jEWPgHcpV+zQtBxAvfFc0rsrG0sq4TIjaUlrodvh0dfOo4QGlXqDcFGClwooFLaRg+iW f5Sea6jREl7x6Zk+llerqT3zNu0RJbFkfJow2/bAZ71mKI+rtbJWHdU+9dBVtKPfzhLH RfUQluZd74bWBzHBmIui7882fXj9GbBfCAuPdJ5MTXw4gTFPoDw0DaXiRulGAU9C4b6H fl9t9ICOqSmqf94z8fPDKhXmhLYrgX+d/g+8kGAd2ICbtYdaP/Y8K6/uZRPkW4UChHW3 jTsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628405; x=1686220405; 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=otAeXCQ9wHQgYb9XVLjxDELtJAEuURMD8rtVKOhLCTY=; b=XM1gz/N/dJN7mSllcCDqaJq3KRPKJZ3+sMbC7WEREgCEopz1X48+vBW5ZRxwRTE1iv 48KnxQmAKs1v/m3a+wU+MoBCG2NQ/F9VgWCAT2jsG5rDRpPgXNx+pwG+wBeQbn9Y+zud WCuNvuIcZWsD1chd2PJgjz1DHEy+FT54YsnPGuUCwiLtbjw/NbeMwuBcHbexe2v3aLZo tfRGkfYqnpuwvJWUXVVEAdygLGvhNIWKXRXHUQKu2RGZWG0zub77cgSUOLhgW0Ck5Kc/ 65O09pzuUQupTlwQPzChIE3m3PhkGpCr0aE0edUgpZU3WZTryqvA4RBl4hxYPem9jVcH /Bng== X-Gm-Message-State: AC+VfDxSWZtOZxZjAwdErb3lk3wCTkxSoZDODRVjDhv0S6pYjVSXBcd8 rR7egRRT2TdNgmfXu/h/8WlARM4aBpAGqWHw2Dag1hEiyjqmYl/QxDG+wz/niaR3772dIPTuSmk 2sU4K2gsJAnqZMEHGYS2r9RwOZe87DLHfPkcypQmUPWyTg3BsnhEYc3GBZwEL413WcJOHnL+d+X 2+Hu6tP6OFTZF9 X-Google-Smtp-Source: ACHHUZ7GzqAZDpDxZD2SUwiWoansXVS+z1qNjyguAay5WAkNMAyCcaNR9ja+6NBQli6ZWMZZmnBJcg== X-Received: by 2002:a17:902:da92:b0:1ac:94b3:3ab4 with SMTP id j18-20020a170902da9200b001ac94b33ab4mr2357313plx.27.1683628405463; Tue, 09 May 2023 03:33:25 -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 o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33:24 -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 v19 22/24] riscv: detect assembler support for .option arch Date: Tue, 9 May 2023 10:30:31 +0000 Message-Id: <20230509103033.11285-23-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033326_581425_51D4BE6D X-CRM114-Status: GOOD ( 11.23 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some extensions use .option arch directive to selectively enable certain extensions in parts of its assembly code. For example, Zbb uses it to inform assmebler to emit bit manipulation instructions. However, supporting of this directive only exist on GNU assembler and has not landed on clang at the moment, making TOOLCHAIN_HAS_ZBB depend on AS_IS_GNU. While it is still under review at https://reviews.llvm.org/D123515, the upcoming Vector patch also requires this feature in assembler. Thus, provide Kconfig AS_HAS_OPTION_ARCH to detect such feature. Then TOOLCHAIN_HAS_XXX will be turned on automatically when the feature land. Suggested-by: Nathan Chancellor Signed-off-by: Andy Chiu Reviewed-by: Conor Dooley Reviewed-by: Nathan Chancellor Reviewed-by: Heiko Stuebner Tested-by: Heiko Stuebner --- arch/riscv/Kconfig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 348c0fa1fc8c..1019b519d590 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -262,6 +262,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" @@ -466,7 +472,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 Tue May 9 10:30:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235608 X-Patchwork-Delegate: palmer@dabbelt.com 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 B1E70C77B7C for ; Tue, 9 May 2023 10:33:38 +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=KyxE7Oopv7jdp3aCgZoOj7n5J0/fRo5l8chvSua7cUc=; b=cg3qBhQW6L46mp ttlXHiY7ZkTxeD8MuDQE6j4cLjkAzByxBXouibYStVoZJgi+OyIenwhUImK3rABPgccxIoVzzhrN/ 2hK6EnZ4uLihYIbFzOpBwt7/yW1Ym3jVjJh2throdO7wx+MOpgOYiIOfsqEOi/XbL6S4JxANbn71q aRwHQ0s6879DCASc3ZNkZMXjnV4O1JiOWp2ujJn0miN3Nj2YW958hKqBs1XZUxctCAyDiRFJdmOaa x5fgZ+xhyPBLMwIY20LvhpeAlFSD5ZmeHF0GHvmBRtcnn/yulIc6dzCrpxaRZKPen61WzjxPKwIFw uGHmdWtu2R4UJD9f2Tpg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwKey-002uhB-1P; Tue, 09 May 2023 10:33:32 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKev-002uaE-10 for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:30 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-64115e652eeso41451502b3a.0 for ; Tue, 09 May 2023 03:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628408; x=1686220408; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=tnXXiygjZrZ2nYQZcu1XSE2OAbXCB6jkIWrGS8Lz/3Q=; b=IR3iXP3SDmxJ/rPrizGRos8/ObgSjWdTQdkW3fRxoUTzbLKnjqc6Oxcb4DF3Izln/H 8AZCqVbmImpoC/l8GrCzcLTxwa9Yj0xuj7D7Mz92ec7BjELInCJJy4gTVvUyxRezuR9M zECoLoEU7mNOq/xHSu1cs2GlytbDOJ99vYIq6fKDvMMPITHlthy/2Etb1HKrvQbZKfH7 zNqNIAuNfFLRuuOdHiRmq5X7sKkq7aQVhUSPztHNLZzAyXBFv/pLq5Ti0KZzSlajA2x8 +a1PoFgQDzIMM2X1ZE5rNn3sm4MsTMxGs4F+tCatjRS79wZfXqrM4xL0Q3Havcq/b5nO oPGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628408; x=1686220408; 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=tnXXiygjZrZ2nYQZcu1XSE2OAbXCB6jkIWrGS8Lz/3Q=; b=CdIg9iedAN9p3isps01QBvFIiF6R9Ny6mIR/w3rjX0jKiTVmS4obtYv5MZQJ0FvXrt cfTmvgdo8pgxDKNDWVvUBtHtfpkebXHfhfBbosf4nfgZUe6QGP+WjwL9gPZ25Zrsc6LL 5RIvJ/o+bK4ghKoYrMaRR6AEoHUBy2bSeNTG7BeIqiwK51PYaeZ1WHySmkYaeFh8V3bp cXOLXvDd5Ox0UICWQ+LOUYSfhF7sqR9U43FzAeHqGVhdTQm0jQ7WoCWILo29rB9DQYQT HT84zq1DxEBm9YXauDIk8/GuC2PXPUQ7El6Zs5HX0GsrkLACLVzD1se6XJVUV0A5Kt10 pY6g== X-Gm-Message-State: AC+VfDwJmkdzycWZfyasNHvFoAwyzgulMiqvrj8MA9dZ6aRRsv4eqceJ yVHw9ZmbVGNqC+VjUuIRI3CBu8ZziQvixnkWRq968W2e8R5PLMs4AgaEd2d5IS1tnjnHQmRAhDq Qqp3uEQCbZmrreVzfCb8UKG1izTJapovNmtx25YIo98G21vei8H8cG6FpWYz85/FpS/bpGFS8Y/ UKQyc6C4GwjxWg X-Google-Smtp-Source: ACHHUZ5H9al3o7R9DVJ8noKHqXN/Z6PK0n9zsnGfLVf4cZOf/4z90B0NyME5n2HJdo21x+D4YU0/wg== X-Received: by 2002:a17:902:d505:b0:1a6:f93a:a136 with SMTP id b5-20020a170902d50500b001a6f93aa136mr22651724plg.22.1683628408433; Tue, 09 May 2023 03:33:28 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33:27 -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 v19 23/24] riscv: Enable Vector code to be built Date: Tue, 9 May 2023 10:30:32 +0000 Message-Id: <20230509103033.11285-24-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_033329_356356_97F8A939 X-CRM114-Status: GOOD ( 12.18 ) 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 --- Changelog V19: - Add RISCV_V_DISABLE to set compile-time default. arch/riscv/Kconfig | 31 +++++++++++++++++++++++++++++++ arch/riscv/Makefile | 6 +++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 1019b519d590..fa256f2e23c1 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -466,6 +466,37 @@ 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 RISCV_V_DISABLE + bool "Disable userspace Vector by default" + depends on RISCV_ISA_V + default n + help + Say Y here if you want to disable default enablement state of Vector + in u-mode. This way userspace has to make explicit prctl() call to + enable Vector, or enable it via sysctl interface. + + If you don't know what to do here, say N. + config TOOLCHAIN_HAS_ZBB bool default y diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 0fb256bf8270..6ec6d52a4180 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -60,6 +60,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 ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC KBUILD_CFLAGS += -Wa,-misa-spec=2.2 @@ -71,7 +72,10 @@ endif # 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 From patchwork Tue May 9 10:30:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Chiu X-Patchwork-Id: 13235731 X-Patchwork-Delegate: palmer@dabbelt.com 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 0773FC77B7C for ; Tue, 9 May 2023 12:30:44 +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=c0RIERVpZzobVKTjC2odXvZbuxjOcKhl+VfW6RoCQh8=; b=Kcqn0uuyry2AvV lGeLnWInrhmeWVndR9iSwwfPV/oa0RShK3Rgf8pYC3SSKuZZoHoGJDvnxXTMeoFwUGMyCYZ5+wbrm F08wnCyFN3ew5ihTQYExy50YlqUP3lDaLgPHlyBTAtZtK2zvWec/6Nk63Yx1Vpw8AS9i1yp5z/3Vp 7yz3AiZsa4/YtaUvWH6SNI20v35PTLu/eKdJoP5MBkJRN9jvnpSFY26KzwDLWETbXPr1n/7xf9NnE qh87xaQN4DghA4CW2o47VqAhoQWAXNRSHxVuHMVKB3nxpi21gcHehTkCNNaQKXkR6q/vvsJybHCYG wYuADHutbTeBXeH8QUgA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pwMUD-003I2Q-3A; Tue, 09 May 2023 12:30:33 +0000 Received: from desiato.infradead.org ([90.155.92.199]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwMUC-003Hzf-1E for linux-riscv@bombadil.infradead.org; Tue, 09 May 2023 12:30:32 +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=qucKeRyh/Imh/QucdmZht4NliS4NWL7FZ9KuokUyTQ4=; b=YHr1BJcZ/iONWarlxk0OG3RMbI fdeF94DRomh5mGxn4uQ/svmafIfrdzq56IGGSP5fq0mTb5rT7vyGAysXGlWkQeYRD+wrlWQmid9pD /7M3LlZ6PWYBjyUA9bG/IZv35RbODN6VO+yY3Ow1JE5WX0GfCxp5cEr15WCI5iuHR8U/4ChJdpz3A vOI/0rtlUzECJ1WJsGJnPjPuZ6MSXFtKotY/QL74c9G8Ooz5++rpWABTPo9b0gaoWkh69niGA0i5z l+qOYFu2gVdBhEGBRIH7WITqAJwaVR2FwUJjsbekF+DLgY2aYcCAgP+2cmYhID3UvtGuT9ULUwKHv Iy0FzJ9A==; Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pwKf5-0066Ch-2C for linux-riscv@lists.infradead.org; Tue, 09 May 2023 10:33:43 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1ab032d9266so53544995ad.0 for ; Tue, 09 May 2023 03:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1683628416; x=1686220416; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=qucKeRyh/Imh/QucdmZht4NliS4NWL7FZ9KuokUyTQ4=; b=RPHrkafqXS52Wg7m6OhvO5aJv73Qq0CPcXlWNAChl9UFVDlc7obLTUAb1mX7TO3eBk /VEs6rJ9PxTZKSG7MnxNdDpg10fxFHmy/21n9Ghtl9CUhqrRdB73OuSsrlxz69txwk+T m/hNCvQo4ID72yyqc940tYoMMfNGaL9+UpFECLx6Ut9AL+uGXrgpnYRycGke34mYFDY6 16Zb2/Ub6DDT4VYWEcLgSXk/WRgzdJbySCGNEFwcQ6dW0bkh0ORQKZmZ9d1WhdKXvbWA agvf9ixKTq+GMCf2KESyQ3DyOXMs357vkPNWyGm4/cPCeNKJS7qBc9Uh6tP5caFAb5sx vi8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683628416; x=1686220416; 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=qucKeRyh/Imh/QucdmZht4NliS4NWL7FZ9KuokUyTQ4=; b=OycoBYSix1Q+Hld750Z8iG3t7XBSqRcGZkKhogMIY+0s3nj2LbMzoPcqrBEU0jHsa4 h2Avsal1SamasEOW3exDfNKaBbMa+96ktZHLR897KlidKqysLAvjeMFOlwElVnEYe3YV EtH0a66DUXb53VT5a+pKy5rLTKyR+HubYG0QXSxEC8y2Nkj8wYap7+/2dJk4GnfVZwCR vbz7MSASqo0zu6gt0wlPoFh6rHX5AqR2J/8thGs/kCdxdJM18zoWZLXLqM6Z0nUyZdGQ mnyas4zVkKpaMtheptD5JVEQOldXzarwicuzDlQLwYXz6HJi5xpnd4+xZk28Tu140dBI XD0Q== X-Gm-Message-State: AC+VfDwQ7XI36smEg3ekV2VX4DO9DnahLUpRWHtnL0XYCVDzLzePSYbh nYxXjdjLbrDGNGWbb1lt/raCIbHx9PYA5f1xo4dG8/yWvrRdXKt2/G6x95CHLUaP8gjsDbtV+BW q3WzAJgMwiBtrbxf/cApTcsB9isma8C36y4oJVzSl3LzEgemvobYk/Z/lTYEViW07q467f3zbHO 3zsb2+hPYwY+Bz X-Google-Smtp-Source: ACHHUZ69hHFFrmlH7WzhZrLSkuYBwmBwSmfhQkhRM9vbdt9geJFktynx5l8H14IWczWj026EcHV8Fw== X-Received: by 2002:a17:902:ecd0:b0:1ac:7ae7:3fe1 with SMTP id a16-20020a170902ecd000b001ac7ae73fe1mr7424380plh.20.1683628416235; Tue, 09 May 2023 03:33:36 -0700 (PDT) Received: from hsinchu25.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id o11-20020a170902d4cb00b001a076025715sm1195191plg.117.2023.05.09.03.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 May 2023 03:33:35 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com, anup@brainfault.org, atishp@atishpatra.org, kvm-riscv@lists.infradead.org, kvm@vger.kernel.org Cc: vineetg@rivosinc.com, greentime.hu@sifive.com, guoren@linux.alibaba.com, Andy Chiu , Jonathan Corbet , Paul Walmsley , Albert Ou , Heiko Stuebner , Conor Dooley , Vincent Chen , Evan Green Subject: [PATCH -next v19 24/24] riscv: Add documentation for Vector Date: Tue, 9 May 2023 10:30:33 +0000 Message-Id: <20230509103033.11285-25-andy.chiu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230509103033.11285-1-andy.chiu@sifive.com> References: <20230509103033.11285-1-andy.chiu@sifive.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230509_113341_584007_45AAF9E0 X-CRM114-Status: GOOD ( 26.04 ) 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 This patch add a brief documentation of the userspace interface in regard to the RISC-V Vector extension. Signed-off-by: Andy Chiu Reviewed-by: Greentime Hu Reviewed-by: Vincent Chen --- Documentation/riscv/index.rst | 1 + Documentation/riscv/vector.rst | 128 +++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 Documentation/riscv/vector.rst diff --git a/Documentation/riscv/index.rst b/Documentation/riscv/index.rst index 175a91db0200..95cf9c1e1da1 100644 --- a/Documentation/riscv/index.rst +++ b/Documentation/riscv/index.rst @@ -10,6 +10,7 @@ RISC-V architecture hwprobe patch-acceptance uabi + vector features diff --git a/Documentation/riscv/vector.rst b/Documentation/riscv/vector.rst new file mode 100644 index 000000000000..d4d626721921 --- /dev/null +++ b/Documentation/riscv/vector.rst @@ -0,0 +1,128 @@ +.. SPDX-License-Identifier: GPL-2.0 +========================================= +Vector Extension Support for RISC-V Linux +========================================= + +This document briefly outlines the interface provided to userspace by Linux in +order to support the use of the RISC-V Vector Extension. + +1. prctl() Interface +--------------------- + +Two new prctl() calls are added to allow programs to manage the enablement +status for the use of Vector in userspace: + +prctl(PR_RISCV_V_SET_CONTROL, unsigned long arg) + + Sets the Vector enablement status of the calling thread, where the control + argument consists of two 2-bit enablement statuses and a bit for inheritance + model. Other threads of the calling process are unaffected. + + Enablement status is a tri-state value each occupying 2-bit of space in + the control argument: + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_DEFAULT`: Use the system-wide default + enablement status on execve(). The system-wide default setting can be + controlled via sysctl interface (see sysctl section below). + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_ON`: Allow Vector to be run for the + thread. + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_OFF`: Disallow Vector. Executing Vector + instructions under such condition will trap and casuse the termination of the thread. + + arg: The control argument is a 5-bit value consisting of 3 parts, which can + be interpreted as the following structure, and accessed by 3 masks + respectively. + + struct control_argument { + // Located by PR_RISCV_V_VSTATE_CTRL_CUR_MASK + int current_enablement_status : 2; + // Located by PR_RISCV_V_VSTATE_CTRL_NEXT_MASK + int next_enablement_status : 2; + // Located by PR_RISCV_V_VSTATE_CTRL_INHERIT + bool inherit_mode : 1; + } + + The 3 masks, PR_RISCV_V_VSTATE_CTRL_CUR_MASK, + PR_RISCV_V_VSTATE_CTRL_NEXT_MASK, and PR_RISCV_V_VSTATE_CTRL_INHERIT + represents bit[1:0], bit[3:2], and bit[4] respectively. bit[1:0] and + accounts for the enablement status of current thread, and bit[3:2] the + setting for when next execve() happens. bit[4] defines the inheritance model + of the setting in bit[3:2] + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_CUR_MASK`: bit[1:0]: Account for the + Vector enablement status for the calling thread. The calling thread is + not able to turn off Vector once it has been enabled. The prctl() call + fails with EPERM if the value in this mask is PR_RISCV_V_VSTATE_CTRL_OFF + but the current enablement status is not off. Setting + PR_RISCV_V_VSTATE_CTRL_DEFAULT here takes no effect but to set back + the original enablement status. + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_NEXT_MASK`: bit[3:2]: Account for the + Vector enablement setting for the calling thread at the next execve() + system call. If PR_RISCV_V_VSTATE_CTRL_DEFAULT is used in this mask, + then the enablement status will be decided by the system-wide + enablement status when execve() happen. + + * :c:macro:`PR_RISCV_V_VSTATE_CTRL_INHERIT`: bit[4]: the inheritance + model for the setting at PR_RISCV_V_VSTATE_CTRL_NEXT_MASK. If the bit + is set then the following execve() will not clear the setting in both + PR_RISCV_V_VSTATE_CTRL_NEXT_MASK and PR_RISCV_V_VSTATE_CTRL_INHERIT. + This setting persists across changes in the system-wide default value. + + Return value: return 0 on success, or a negative error value: + EINVAL: Vector not supported, invalid enablement status for current or + next mask + EPERM: Turning off Vector in PR_RISCV_V_VSTATE_CTRL_CUR_MASK if Vector + was enabled for the calling thread. + + On success: + * A valid setting for PR_RISCV_V_VSTATE_CTRL_CUR_MASK takes place + immediately. The enablement status specified in + PR_RISCV_V_VSTATE_CTRL_NEXT_MASK happens at the next execve() call, or + all following execve() calls if PR_RISCV_V_VSTATE_CTRL_INHERIT bit is + set. + * Every successful call overwrites a previous setting for the calling + thread. + +prctl(PR_RISCV_V_SET_CONTROL) + + Gets the same Vector enablement status for the calling thread. Setting for + next execve() call and the inheritance bit are all OR-ed together. + + Return value: a nonnegative value on success, or a negative error value: + EINVAL: Vector not supported. + +2. System runtime configuration (sysctl) +----------------------------------------- + + * To mitigate the ABI impact of expansion of the signal stack, a + policy mechanism is provided to the administrators, distro maintainers, and + developers to control the default Vector enablement status for userspace + processes: + +/proc/sys/abi/riscv_v_default_allow + + Writing the text representation of 0 or 1 to this file sets the default + system enablement status for new starting userspace programs. A valid value + should be: + + 0: Do not allow Vector code to be executed as the default for new processes. + + 1: Allow Vector code to be executed as the default for new processes. + + Reading this file returns the current system default enablement status. + +* At every execve() call, a new enablement status of the new process is set to + the system default, unless: + + * PR_RISCV_V_VSTATE_CTRL_INHERIT is set for the calling process, and the + setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not + PR_RISCV_V_VSTATE_CTRL_DEFAULT. Or, + + * The setting in PR_RISCV_V_VSTATE_CTRL_NEXT_MASK is not + PR_RISCV_V_VSTATE_CTRL_DEFAULT. + +* Modifying the system default enablement status does not affect the enablement + status of any existing process of thread that do not make an execve() call.