From patchwork Mon Feb 10 20:26:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969342 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 37902C021A1 for ; Mon, 10 Feb 2025 20:26: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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pTSA1S2Hfv043WtYxe5yIlGs5CoYWlSZBQAAb1H98Cs=; b=MD+4r0s58YCqq0 34PPJf5YVRNHNiXniGxsIYqMLGtGGto46MJRPDZKFu7ArhfvAVEqi/mUc7p/wWrChIIGpP6jjRscr BuhDGzP1BljQLErC1DlE+1frjrfZcxZ1XgOlGPPiPaRucCNJs9EF6E9q5ceqcJ1fFQ8m/E9Kjs5lR mQo0mo/kvzeagITd+K49OXZps6vdokSGVowPcD7d3hL1uxL2UM3uot5pD1ExtldZr4JiB+90yMfiO 0JIWs1ia3fbv4VuLYByA84nTVAqjyuFm+GEVFhAV6h1cW1XVu4LOzTcvzOA/BScnfxogcoRAHUJIc 9dbq9Jwm4Yq5Z9rcsvfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaMm-00000001JbZ-2jny; Mon, 10 Feb 2025 20:26:52 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaMk-00000001JZN-11AZ for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:26:51 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-21f3e2b4eceso90250675ad.2 for ; Mon, 10 Feb 2025 12:26:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219209; x=1739824009; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=e4Q5cQ45XPtn6y+RD8Sdq5HQMM0KMIYwSdtAswCfSLwmNUw8GoCsWaxNzb8NEwGwXz E0ZTQ19d+pNagn/jPur26Pmho6Q6Pip1E3XFEwCZ0opFSiJ7gDljo4kSJgz4hxSQITzK Ho4xukNMRoZERHAWHOG/qs/AskR6Pyg5BwgnUiCGyihrthrPCgYxUaaALJ3j+3/pf5/e YQ+44IBFTpyhE71pok66lL8fcHHk60nTKyOD5nWEDg28ZJExK5Y0pw2NzgZm9i/yjhGZ a4G8s4vSPXh61hyPn8M9M8r7IVuWECrPW2Rvx6YR01m0GPDEgGWEQcsm5ag9R+HUQnFX iT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219209; x=1739824009; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dbqILCM4sqAsqnZWR3n0haWRm4rdBukz3/+QEOiXjzw=; b=QYi48kjQyJr689+jSwcGWufzfExGcFnN3dPBDYJOy8Cv28zzxEBe82+utDlrfzMPEw 4svYZk8DfSl8Mf6SAAdJ/NrNX7H+7vQbDf57bbuIjsn3+K5ZLaZByfqtRQybf9ErCWf7 y/Nsv1mWfSPmuTmtlnei1rDnTnUxonguHkGE+8r06St+eS/ri5HvDl1py7+9flGBzQK3 +KRKD/XMbbipmX0OjqVV7yeE08KR8ubTAyqQDX2PDfxmYb8cfwThK5cFIqQImptdXAN+ Z4loHlC2M5b7LJuPF2ToIL2Go9HxGbidJId2bCI1C1v0X6BKFbL3xttDf4c/LZrIVSN6 qJgA== X-Forwarded-Encrypted: i=1; AJvYcCVlJdz2kGj0L4qkAXEVW8KlmdKJq/TT9WcfJgjXpyG9+jS6X44PxQ4DdqbW6purlfoJ54UK4T5bdBoMsQ==@lists.infradead.org X-Gm-Message-State: AOJu0YywPeNVfxKhy8EtnCZepobK8D9JVqtm6mTsboGrkgaY6Xbv40X+ yYSTgJp4RBMWwYdyKwMfIQGRa8Jw/+iJb5abYilbLRyik54WO2J/42+4HX5ur4o= X-Gm-Gg: ASbGncs712gtso85Oeum29czXMpe/zrrK9ucMTkT3K6WLqwrzF2AvTqQbSlXn9VN6dI YKiGTXHef/JfbqeqpCKUDSttq39leERFZ9QV9g2ZBXbHWF7HmoArzaH0NMzrVCLty5pRqxxjPoA 34o4tNs4cT4GPAKD0+MuNRxNjF690EAFvl6fLB62L4h2BU4iHY+vkL2tHmlRGk8VCJwLlhHoUcx ZDo7K+pHDw44GFrtLExa72ke15+jMxx5neRPxYY+YGLj6INaG6cvb7NSz0YAqMmzft9Moo5Fkw4 PNDe7QM0vNACG9aGKKQTYhI8Iw== X-Google-Smtp-Source: AGHT+IEhZ3TYH/tcMpvG04g1Gw5nBcJh1Es1LXU8QZEq1VPJSIGDwObL506BPg7EHhAIxgdRzRFlCQ== X-Received: by 2002:a17:902:c40e:b0:21f:1549:a55f with SMTP id d9443c01a7336-21f4e6c0f49mr269385645ad.19.1739219209495; Mon, 10 Feb 2025 12:26:49 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:26:49 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:36 -0800 Subject: [PATCH v10 03/27] riscv: zicfiss / zicfilp enumeration MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-3-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_122650_285208_EFA8C1F6 X-CRM114-Status: GOOD ( 16.29 ) 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 This patch adds support for detecting zicfiss and zicfilp. zicfiss and zicfilp stands for unprivleged integer spec extension for shadow stack and branch tracking on indirect branches, respectively. This patch looks for zicfiss and zicfilp in device tree and accordinlgy lights up bit in cpu feature bitmap. Furthermore this patch adds detection utility functions to return whether shadow stack or landing pads are supported by cpu. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/cpufeature.h | 13 +++++++++++++ arch/riscv/include/asm/hwcap.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/kernel/cpufeature.c | 13 +++++++++++++ 4 files changed, 29 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 569140d6e639..69007b8100ca 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -137,4 +138,16 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); } +static inline bool cpu_supports_shadow_stack(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFISS)); +} + +static inline bool cpu_supports_indirect_br_lp_instr(void) +{ + return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && + riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICFILP)); +} + #endif diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 869da082252a..2dc4232bdb3e 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -100,6 +100,8 @@ #define RISCV_ISA_EXT_ZICCRSE 91 #define RISCV_ISA_EXT_SVADE 92 #define RISCV_ISA_EXT_SVADU 93 +#define RISCV_ISA_EXT_ZICFILP 94 +#define RISCV_ISA_EXT_ZICFISS 95 #define RISCV_ISA_EXT_XLINUXENVCFG 127 diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 5f56eb9d114a..e3aba3336e63 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,6 +13,7 @@ #include #include +#include #define arch_get_mmap_end(addr, len, flags) \ ({ \ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index c6ba750536c3..82065cc55822 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -150,6 +150,15 @@ static int riscv_ext_svadu_validate(const struct riscv_isa_ext_data *data, return 0; } +static int riscv_cfi_validate(const struct riscv_isa_ext_data *data, + const unsigned long *isa_bitmap) +{ + if (!IS_ENABLED(CONFIG_RISCV_USER_CFI)) + return -EINVAL; + + return 0; +} + static const unsigned int riscv_zk_bundled_exts[] = { RISCV_ISA_EXT_ZBKB, RISCV_ISA_EXT_ZBKC, @@ -333,6 +342,10 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xlinuxenvcfg_exts, riscv_ext_zicboz_validate), __RISCV_ISA_EXT_DATA(ziccrse, RISCV_ISA_EXT_ZICCRSE), + __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicfilp, RISCV_ISA_EXT_ZICFILP, riscv_xlinuxenvcfg_exts, + riscv_cfi_validate), + __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicfiss, RISCV_ISA_EXT_ZICFISS, riscv_xlinuxenvcfg_exts, + riscv_cfi_validate), __RISCV_ISA_EXT_DATA(zicntr, RISCV_ISA_EXT_ZICNTR), __RISCV_ISA_EXT_DATA(zicond, RISCV_ISA_EXT_ZICOND), __RISCV_ISA_EXT_DATA(zicsr, RISCV_ISA_EXT_ZICSR),