From patchwork Tue Mar 19 21:58:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597088 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 B67E6C54E68 for ; Tue, 19 Mar 2024 21:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=1u7p3hvknd7vu0mye4XdTWOSTQoC+dXEGS4jlXMhC7A=; b=JWEPaXfiJyhEE2 K8Z2g/UGgdDmAjylXVUqwgxEwtVuW2iaewOH7EFbSjizWDMVYQa9opjtVqIjnjIb81WZOM8r/dW9K oYfi0tGOnOSZm1KI4an6zcH8SGJvNQUhPM0q578tU512qfsX1HMykyb85Tf2bbhywjz5S2S99bmUD xI4lhb5hsdXEy6vFxmqW6MC9zD1iPV1Em4X2yFknuyQLkYWklt1/XTgDbqBCZ54GPnYmN7vaxUxJq VqjqXvNhUvcx58C2/FbioSnVxugdNligtEa7KdFoDZIYigTFRKNkqyXslP/PpCnYOLzOGWbWzC5F8 fp/OEL8djVrKWQ/8Mf5g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUZ-0000000EMju-475y; Tue, 19 Mar 2024 21:59:31 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUT-0000000EMeZ-2ZRe for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:27 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6e73e8bdea2so1554163b3a.0 for ; Tue, 19 Mar 2024 14:59:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885558; x=1711490358; darn=lists.infradead.org; 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=mf8TbChjgkUHpdp7I/ykwc/bJc9eIQRGH4wGpVTxw4U=; b=KlpdTY9nSZaLnIRq/hmVdiDKj2HcCj9bZ6NQKbH1xhyRjOil6ivLSbMuI2jgg/2tzP aHCLKY+QGhN606Iflcb0q1FvkCzQkqFnhvDFKc81Qe4pBqANa+Emu7yIh8XEv8KlUKdG uVAyzScgz7CM3nbiv7Ch+5hnqg9Yl+Oj2g0suAJY/5HfuWm8fi8b6Vg6zRaaQXuZfcax 6/U+A38+pNlV6i9RevEHwCgcsbexOd3s40VBKB4iXfunaEGfGEWmftgZ5kPltiw3E6R1 fNrLYS7Z30KdiTublkQpSzB/oKgGYpUAAEDQavGw1iMUUTgcswjk9hKMqdmuasEphK5M 5w+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885558; x=1711490358; 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=mf8TbChjgkUHpdp7I/ykwc/bJc9eIQRGH4wGpVTxw4U=; b=RuY35+D1knvbF1UjbLI4AFo+6mitL3XjDoxXrppWrn3M8CneJ0e7FWGByQ0o9w30ml tkhgxjT/ngxp2vk0guiWOzs0/AwOEuh4tCSXOLodFbsgtIbGPeKGGu6jYcMujFUgSOoI t33wcIWMLadoAXMJckRGgnNqNv7Y923ZuX91t95CKS0ay1bv+xKJTu4b/fSMVJSQ+4WQ npiSSQwna1WK4StokDUEGY2nBDVb7zOJAUunS34GfQUC4F1d8b63uHx0Cqykr8NsvJdX 26FZYaPjLPXLpnyr1pip4OS73Yq4qOOSultZcWPoBNweZzGfFdsrvaBf9Cd0/WJ4VeUb 5dAA== X-Forwarded-Encrypted: i=1; AJvYcCV9/LM2g2xIEAWCd1tyt5Pb58GdtX47dPxP9o8DRR8MoGUBEtcfbJNHqluavieYwHbzXMgmIMVGaFh7lNs9jEdwPjt/8LLGzyqL8c5Y+meW X-Gm-Message-State: AOJu0YzTVe5E0Gvw7itlwx9cUu3SFOpmah0Gc9OHOvzfAP/yQH5/+e/c KW15vAavuQwv6h6vp9xxtqQQoGQG3ycZM3xS2ztd7OHmM4h1Rw3Q+dNXJ+n38n8= X-Google-Smtp-Source: AGHT+IHxqp3vSJIj8u1FQnLhGGhgkkZd9sPyRN/6PCbIT9hhDxZABaPjD8SNbLUKLifMerMmPS4+xQ== X-Received: by 2002:a05:6a00:cd5:b0:6e6:9ac4:d501 with SMTP id b21-20020a056a000cd500b006e69ac4d501mr443835pfv.25.1710885558184; Tue, 19 Mar 2024 14:59:18 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:17 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Paul Walmsley Subject: [RFC PATCH 1/9] dt-bindings: riscv: Add pointer masking ISA extensions Date: Tue, 19 Mar 2024 14:58:27 -0700 Message-ID: <20240319215915.832127-2-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145925_714889_B4D21173 X-CRM114-Status: UNSURE ( 8.62 ) 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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The RISC-V Pointer Masking specification defines three extensions: Smmpm, Smnpm, and Ssnpm. Document the behavior as of the current draft of the specification, which is version 0.8.4. Signed-off-by: Samuel Holland --- .../devicetree/bindings/riscv/extensions.yaml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Documentation/devicetree/bindings/riscv/extensions.yaml b/Documentation/devicetree/bindings/riscv/extensions.yaml index 63d81dc895e5..bb7d5d84f31f 100644 --- a/Documentation/devicetree/bindings/riscv/extensions.yaml +++ b/Documentation/devicetree/bindings/riscv/extensions.yaml @@ -128,6 +128,18 @@ properties: changes to interrupts as frozen at commit ccbddab ("Merge pull request #42 from riscv/jhauser-2023-RC4") of riscv-aia. + - const: smmpm + description: | + The standard Smmpm extension for M-mode pointer masking as defined + at commit a1e68469c60 ("Minor correction to pointer masking spec.") + of riscv-j-extension. + + - const: smnpm + description: | + The standard Smnpm extension for next-mode pointer masking as defined + at commit a1e68469c60 ("Minor correction to pointer masking spec.") + of riscv-j-extension. + - const: smstateen description: | The standard Smstateen extension for controlling access to CSRs @@ -147,6 +159,12 @@ properties: and mode-based filtering as ratified at commit 01d1df0 ("Add ability to manually trigger workflow. (#2)") of riscv-count-overflow. + - const: ssnpm + description: | + The standard Ssnpm extension for next-mode pointer masking as defined + at commit a1e68469c60 ("Minor correction to pointer masking spec.") + of riscv-j-extension. + - const: sstc description: | The standard Sstc supervisor-level extension for time compare as From patchwork Tue Mar 19 21:58:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597084 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 ABEB8CD11DE for ; Tue, 19 Mar 2024 21:59: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: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=+JBNM2jeVYpBmVTHvObKFE0tI+dJ0x/+Pqp0LUQ8NTA=; b=X7AaLNkPHI9zI7 hb5AITtrY2PUYuCdrmBcLXVzG6k9dBg1xM/t56su6Vc0FM9DO6c98b6MlclixAuh0iF/ElwaRSbB+ 9fY7n2ZS2ZSj94cFelm7bibRAzsr5cZwJMcDzV8wPLB44tawyfjHo0W2oRdGx5tm8GE+urhRNp1vp Qq5P7S9ZURG7L4jAAuElUZbgoyZqT13ESdiWxFE28Hk6UDnAb4Pg4Gfm3/tYhmQmCNDfE7KTL8PaJ f7hEsHrS+K3pzS1EML4Q5SeZK5E4QBr4cVqIid2g4ZblJUkZyEJFewGvv6WmNFRM2E5iePOigNXkI xtRY8wbO90J0n9WPuv1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUe-0000000EMn4-24uE; Tue, 19 Mar 2024 21:59:36 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUV-0000000EMec-3wIy for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:30 +0000 Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5e4613f2b56so4648796a12.1 for ; Tue, 19 Mar 2024 14:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885559; x=1711490359; darn=lists.infradead.org; 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=egqW9CgCHR/h+XnNBIYYSLcc7kf3PBZY4lOdjowA6vA=; b=nNhhOCp8AjFlipBmaXhF+li3kSfQ4kj5fMpOiYck8EYRuliO1u6XpqDRgj3cHi/Kj+ Y/yUocCRPYgUH6yQbBWtIgzIoRvNy4kfyL9ZC4w+tIkZ9L1cjC7nGrh9SbjC5fysB3Ax 0GL2jheRa2J/N2/hYlmqabTn5hTSbgYNHkwbGTT3LsZpjdqEyBgBQOEVP9/9rcOtZ2b8 wDRzq5A78mh/tyOXduH42SOb2ZVwLoYMIQPeedMsLaxnS1qTumfw/Gc0gg0pJz1oJYl+ ExMKVRoUfBTUTQN+QOD4qL4yErxu8uRi3/lnzRk7gv2SJ4c5ys5qet+8HhijGKbE1DJF TbOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885559; x=1711490359; 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=egqW9CgCHR/h+XnNBIYYSLcc7kf3PBZY4lOdjowA6vA=; b=T2m/U5DV3Z4l6mHgsuf07EJYkKxhHNqLisX3+2ODDRHMKR+m10AOOYuwOv/YFBNBUT OrS00XZBS9HMgpNN5ddR7c9+4UodPxXVi1AIuUeGbsVu/wb/nrCqfsO3pYEe9ptzmXY3 JOUtWtEMIaEGBMBbJqM+Zz/YDU0r6Qfimnk3uDysO9mbKZ5Q1pzKXxFQtuqv84RzjBne eONfMA7ubs1bO6Tdg9KI3YEb92auioZ2Gqp56dWhuAhoqVFlfhjkCKzc2PbW6WMPndEz FhbxQ/+mlQepAyk/BBSGDVp54X1Hp2OIFSwgdmF+tohRTf9nsbzUJoabNZ45C8AIuAlQ aVeQ== X-Forwarded-Encrypted: i=1; AJvYcCWyGMl7p5Y0XIjVB++0qEKdfkvhUovDfhSnjZfNcIQxnPEggOgSjkQfVdTMxjXmpFasB7oQomimEo/q2DmrMkN/M2JS1ucD7+PkK+mqSHgY X-Gm-Message-State: AOJu0YzOzVV3ELOIftLkwGZWMbyArE0ZCDfBUmllcP2pbwazqiJAGuKX UDz+sNBj3g6rYl8UFT4AELS4nIe5JAHeLNx10vZzWOYuPHMyJR/IgAk1UVvLuPM= X-Google-Smtp-Source: AGHT+IHiAdEaZGvoCjNSk/dkwDMZl0Qwbws/pc2s32fXcllKuOrTTy4J+408sRjDB0ufVB39xHFOUg== X-Received: by 2002:a05:6a20:9f8f:b0:1a3:60cb:8172 with SMTP id mm15-20020a056a209f8f00b001a360cb8172mr9089470pzb.39.1710885559375; Tue, 19 Mar 2024 14:59:19 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:19 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Andrew Jones Subject: [RFC PATCH 2/9] riscv: Add ISA extension parsing for pointer masking Date: Tue, 19 Mar 2024 14:58:28 -0700 Message-ID: <20240319215915.832127-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145928_181590_6E39773A X-CRM114-Status: GOOD ( 10.88 ) 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 The RISC-V Pointer Masking specification defines three extensions: Smmpm, Smnpm, and Ssnpm. Add support for parsing each of them. Smmpm implies the existence of the mseccfg CSR. As it is the only user of this CSR so far, there is no need for an Xlinuxmseccfg extension. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/hwcap.h | 5 +++++ arch/riscv/kernel/cpufeature.c | 3 +++ 2 files changed, 8 insertions(+) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index 1f2d2599c655..1a21dfc47f08 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -80,6 +80,9 @@ #define RISCV_ISA_EXT_ZFA 71 #define RISCV_ISA_EXT_ZTSO 72 #define RISCV_ISA_EXT_ZACAS 73 +#define RISCV_ISA_EXT_SMMPM 74 +#define RISCV_ISA_EXT_SMNPM 75 +#define RISCV_ISA_EXT_SSNPM 76 #define RISCV_ISA_EXT_XLINUXENVCFG 127 @@ -88,8 +91,10 @@ #ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA +#define RISCV_ISA_EXT_SxNPM RISCV_ISA_EXT_SMNPM #else #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SSAIA +#define RISCV_ISA_EXT_SxNPM RISCV_ISA_EXT_SSNPM #endif #endif /* _ASM_RISCV_HWCAP_H */ diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 79a5a35fab96..d1846aab1f78 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -311,9 +311,12 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = { __RISCV_ISA_EXT_BUNDLE(zvksg, riscv_zvksg_bundled_exts), __RISCV_ISA_EXT_DATA(zvkt, RISCV_ISA_EXT_ZVKT), __RISCV_ISA_EXT_DATA(smaia, RISCV_ISA_EXT_SMAIA), + __RISCV_ISA_EXT_DATA(smmpm, RISCV_ISA_EXT_SMMPM), + __RISCV_ISA_EXT_SUPERSET(smnpm, RISCV_ISA_EXT_SMNPM, riscv_xlinuxenvcfg_exts), __RISCV_ISA_EXT_DATA(smstateen, RISCV_ISA_EXT_SMSTATEEN), __RISCV_ISA_EXT_DATA(ssaia, RISCV_ISA_EXT_SSAIA), __RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF), + __RISCV_ISA_EXT_SUPERSET(ssnpm, RISCV_ISA_EXT_SSNPM, riscv_xlinuxenvcfg_exts), __RISCV_ISA_EXT_DATA(sstc, RISCV_ISA_EXT_SSTC), __RISCV_ISA_EXT_DATA(svinval, RISCV_ISA_EXT_SVINVAL), __RISCV_ISA_EXT_DATA(svnapot, RISCV_ISA_EXT_SVNAPOT), From patchwork Tue Mar 19 21:58:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597086 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 827A9C54E71 for ; Tue, 19 Mar 2024 21:59: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: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=Dstrh3lzlVGLVSABDCHsoac3fbOlftN3PKYAFygIrK4=; b=rgHaLZWLrVyulu d+x7xrh/0rDEEe1bi7u/N4H3IwltGVamuBhjuFotVx//UKUADQhu1yjRQsV1M0/536jIOAbpjednZ OY8BXgM5Mtcs136hQJDBvVHDLN13CJM3x80BsJbEi6wJ6yDyn7dHpV9rQUeQ1cLFRjOBG//w4800Y x3qIYs6pgnFBBxmzDgFN6GGM4EeK3FCWJYpyoN0SEf9RD0s6NCIiAdp1EL3fp7RvXb//Ej0Hx/UPT Oghpta9+G8VH2qXh1LJePuUKhZuTltdDij0QY3hQpG7ltgHzQYRQvgu4StTu9psXWPU+zAnDTlLVX BNEjTCHd1JE5u8/dfhyA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUg-0000000EMoB-0jo0; Tue, 19 Mar 2024 21:59:38 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUV-0000000EMee-3xV0 for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:30 +0000 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-6e6b54a28ebso5898712b3a.2 for ; Tue, 19 Mar 2024 14:59:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885560; x=1711490360; darn=lists.infradead.org; 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=hw7AaJXdckjCcYy8ru8KTiHIANuUIN3Ni5CiYoljsMY=; b=RI9LjRLE5al2SBuDYBQksj+czIe+q8fo1gbMl28JpoDeT0DxxR0SFntih9BANEsyVU clIcF9kjeCE8VndaUNPJiPYDeH6N4C4hL0780etzCtYXi4Df+tp5Jft+RMpIgZbX6Nsh 2NPa6DCKij0ckznJ+hGO1mWHv8y2NLcqKwdv1/1mT6/X4hNwwSL4vazU/JAilE0XWe73 Cnj3J9GImfDonllzFmH1xmO29EvMo2ARbsFJ5zpruR1oYlXWaH5EMhyEmhaP2ywcAU91 nUKG4f94GMjtHQXunHYNRZKDVyKxul2eX04BWY9h6J3aFO+7WH2wfDLnkImJwWkvpIGr fOmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885560; x=1711490360; 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=hw7AaJXdckjCcYy8ru8KTiHIANuUIN3Ni5CiYoljsMY=; b=VwNKyJh0Uyp9clceU5PozSaOcG1K0EuHlQaqzNcqILrb/FgkUwLbbflMAX8hXTjBMp uVImFGeB7NcT2JknibiyffxDFPx5gtwy8K4nD0SNdrInQjFgXr/hmmpkuzuo2/mj8sSQ EfGfNUrrCq7q7qJpg238H955eeglGlVZVlj/Ti9D8cAKCCAMPAajaeUsPojJg6T1aHu5 4XFhUjO6Jzc+a4V8MXEcPPp04HLAnDNUr5wa32WpZPMM7R/ekHTHCfa0cRsXU3OoQKri BuqciAjctbsMFq5irESfi33umOBCYkRpmgXuriz/ATLD1rqgGoGtGWEXsmP754J3BIUz cgnA== X-Forwarded-Encrypted: i=1; AJvYcCV5WAS47u8rFgWs8Fx/t2QADpVLE+9ctmCEzUXi4i4+T+8Ma7F5UQf+v5QUdW89EKH5HlNN3haLrbNHiYrO8NHy00K7kEoONKFXfHyeKvg+ X-Gm-Message-State: AOJu0YzSDqfsUgzdG3l6Suk2vHHQP9Q7nFsYMCv7M9iIhRLVKmfu3Wpw je86MI98DqUJgwD2N0doYWWzDGXgT7S+UKimZyr8gnaSI/MMaJP2EViNDsi998A= X-Google-Smtp-Source: AGHT+IGDkK4VMEEzpfRCSNThBGjrgCcpdqf0s10wRrEBrgNVgbv2+4UnRTUBfVw5XKDLPYKeaCEdMQ== X-Received: by 2002:a05:6a00:1701:b0:6e7:8047:96f0 with SMTP id h1-20020a056a00170100b006e7804796f0mr796041pfc.28.1710885560589; Tue, 19 Mar 2024 14:59:20 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:20 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Andrew Jones , Greentime Hu Subject: [RFC PATCH 3/9] riscv: Add CSR definitions for pointer masking Date: Tue, 19 Mar 2024 14:58:29 -0700 Message-ID: <20240319215915.832127-4-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145928_186200_93E7747B X-CRM114-Status: UNSURE ( 8.59 ) 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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Pointer masking is controlled via a two-bit PMM field, which appears in various CSRs depending on which extensions are implemented. Smmpm defines the field in mseccfg; Smnpm defines the field in menvcfg; Ssnpm defines the field in senvcfg and (if present) henvcfg and hstatus. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/csr.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h index 2468c55933cd..1d5a6d73482c 100644 --- a/arch/riscv/include/asm/csr.h +++ b/arch/riscv/include/asm/csr.h @@ -119,6 +119,10 @@ /* HSTATUS flags */ #ifdef CONFIG_64BIT +#define HSTATUS_PMM _AC(0x3000000000000, UL) +#define HSTATUS_PMM_PMLEN_0 _AC(0x0000000000000, UL) +#define HSTATUS_PMM_PMLEN_7 _AC(0x2000000000000, UL) +#define HSTATUS_PMM_PMLEN_16 _AC(0x3000000000000, UL) #define HSTATUS_VSXL _AC(0x300000000, UL) #define HSTATUS_VSXL_SHIFT 32 #endif @@ -194,6 +198,10 @@ /* xENVCFG flags */ #define ENVCFG_STCE (_AC(1, ULL) << 63) #define ENVCFG_PBMTE (_AC(1, ULL) << 62) +#define ENVCFG_PMM _AC(0x300000000, ULL) +#define ENVCFG_PMM_PMLEN_0 _AC(0x000000000, ULL) +#define ENVCFG_PMM_PMLEN_7 _AC(0x200000000, ULL) +#define ENVCFG_PMM_PMLEN_16 _AC(0x300000000, ULL) #define ENVCFG_CBZE (_AC(1, UL) << 7) #define ENVCFG_CBCFE (_AC(1, UL) << 6) #define ENVCFG_CBIE_SHIFT 4 @@ -215,6 +223,12 @@ #define SMSTATEEN0_SSTATEEN0_SHIFT 63 #define SMSTATEEN0_SSTATEEN0 (_ULL(1) << SMSTATEEN0_SSTATEEN0_SHIFT) +/* mseccfg bits */ +#define MSECCFG_PMM ENVCFG_PMM +#define MSECCFG_PMM_PMLEN_0 ENVCFG_PMM_PMLEN_0 +#define MSECCFG_PMM_PMLEN_7 ENVCFG_PMM_PMLEN_7 +#define MSECCFG_PMM_PMLEN_16 ENVCFG_PMM_PMLEN_16 + /* symbolic CSR names: */ #define CSR_CYCLE 0xc00 #define CSR_TIME 0xc01 @@ -381,6 +395,8 @@ #define CSR_MIP 0x344 #define CSR_PMPCFG0 0x3a0 #define CSR_PMPADDR0 0x3b0 +#define CSR_MSECCFG 0x747 +#define CSR_MSECCFGH 0x757 #define CSR_MVENDORID 0xf11 #define CSR_MARCHID 0xf12 #define CSR_MIMPID 0xf13 From patchwork Tue Mar 19 21:58:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597083 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 BA240C6FD1F for ; Tue, 19 Mar 2024 21:59:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CoX5bqjstk0sdAD5+6gSm+uGVxJS5mGu5tjYL5LyQEY=; b=iJ7stJfP16hLzc ruNmt6LJ7Y85qbCjF6uPVbXErgFDwENxvyDm53wMFINCbeEje2D2nfa6vBb6VFNvJ7axL5b7SJTjO hLlVnL+hUMDlb2qc5q82pa0+R/RfJ/OYmak4fDO0JEATz/gOeIAbySjG+POZ4bgdpN0rp6Qn6TGPD I2Z5EpuDmdz5/8CmrxtJXkcyyycWeUT3t4hgFQOyoOdnjf6O+gqSsklKWpwxv1gOLNXLsFRAM4vmV EjrjscF9PkdWYNCPzJceh3o73+SjRuAEHJ3V7W86Y5UNIBVXkY7J3C90gTInvH5sUAzgzd7Y8qjzq hhki/q23vI/hxGzFAYgQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUa-0000000EMkE-3Hxy; Tue, 19 Mar 2024 21:59:32 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUT-0000000EMew-2Zyq for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:27 +0000 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-6e6f6d782e4so4000995b3a.0 for ; Tue, 19 Mar 2024 14:59:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885562; x=1711490362; darn=lists.infradead.org; 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=ciVO7+10eZV72+E/ORaPRZFjospVEi/8CuyB7rGS1wk=; b=HFcEpbhTDIzPhffKUK2gFsLkt//X+eo5dTnQ1bDy7WkFpKXT4f6wLVbp1NTUcr+xd7 kW3Le5ViqEq0pMguzDqmm8plzHLxMZ4FLxeld85/ig8W2MznT/nJlDpKoOnr8mCuSVaB LU1//TGFLFIc0tYyRn9XATwS7oNlipH3APtLaRDSZF/5Mk5kZ7WsriX2g82m++sOvpXX Jk4902vHJI6Fu1eElUvGJp1Pl0L0n78LGCfZRT8moBXbcf8cKLyosgp0A413TSf/NMgz pr4c7f1B7UPb5RVUxRNinpbz0MkilZntqtveukzveOxM/T5ZrJNmE20Y5WyXwxSENjlX h+5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885562; x=1711490362; 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=ciVO7+10eZV72+E/ORaPRZFjospVEi/8CuyB7rGS1wk=; b=NnHBy2q8rDXAM/7MZZIfrL067bPDdoNoNAySINMfbiaP2JCwh7g7YCW9U+DWRBIJxt qsB/uNzanzyTtDsSDg/PLUFjShz8SWr2PuJNK8+L6fGk5ToWvRB22PTfVsaHDJx0Vp/K 44+4t4BLjhf0gI4VL8u6jx/ZXvoCkdgA9STEVqrIUYRvVdkMaZBvvbAKGf+XSOdcQGt+ Fuh2V6/gYLyl+MEYphElDPghY88MaUy6rr9PM9YDL4xVJVCkgVIDKjUJtvg8LEaAca5j GiSFIfCC0GS9akU5mqPD2A5N5DYRSFAsZgy4KXODiUX1jvAh/nXjo3zZGXOdDSaWxBTT khzQ== X-Forwarded-Encrypted: i=1; AJvYcCUODhjsXOsyYufibSW/ciZy06n5+G9as5/WBIbiAAbOj3pAJXoc5SfBldIf2FK6v1CHWnOb7o3yuB0b7/DuWyq9YjkcGsPxtPqLOGe6bTuk X-Gm-Message-State: AOJu0YxKDnhfEBTM0djDFOZ5m//2x2c+Gt6Ek46TjhPVvU7TEN07nOzS 1QJ5ybhG45nYxezKVfUnexMz7U4GkL6lsWoS90rPhPUiD5/cu/L4Q5lTRYE2zm8= X-Google-Smtp-Source: AGHT+IFywIvk95HmWYC1ps8ZiAyORSpJBD5fZsHjoqcZoqHYlw5bVo0KamFA/Wgvuncr3jxrfpyfUQ== X-Received: by 2002:a05:6a00:721c:b0:6e6:fcd4:4f44 with SMTP id lk28-20020a056a00721c00b006e6fcd44f44mr11479878pfb.16.1710885561953; Tue, 19 Mar 2024 14:59:21 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:21 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Paul Walmsley Subject: [RFC PATCH 4/9] riscv: Define is_compat_thread() Date: Tue, 19 Mar 2024 14:58:30 -0700 Message-ID: <20240319215915.832127-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145925_726838_88965E7D X-CRM114-Status: UNSURE ( 9.51 ) 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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This allows checking if some thread other than current is 32-bit. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/compat.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h index 2ac955b51148..233c439c12d7 100644 --- a/arch/riscv/include/asm/compat.h +++ b/arch/riscv/include/asm/compat.h @@ -12,11 +12,18 @@ #include #include +#ifdef CONFIG_COMPAT + static inline int is_compat_task(void) { return test_thread_flag(TIF_32BIT); } +static inline int is_compat_thread(struct thread_info *thread) +{ + return test_ti_thread_flag(thread, TIF_32BIT); +} + struct compat_user_regs_struct { compat_ulong_t pc; compat_ulong_t ra; @@ -126,4 +133,13 @@ static inline void cregs_to_regs(struct compat_user_regs_struct *cregs, regs->t6 = (unsigned long) cregs->t6; }; +#else + +static inline int is_compat_thread(struct thread_info *thread) +{ + return 0; +} + +#endif + #endif /* __ASM_COMPAT_H */ From patchwork Tue Mar 19 21:58:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597081 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 70B86C54E68 for ; Tue, 19 Mar 2024 21:59:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6SDsFTeZ+85HRjjLVFHS5RbhBW2k1PPaNU1l+euh0Nw=; b=o73V1ScBiLS9WE DifH+y9OkFXBnaTGEuAQQ02jCWke04ahD4q+ZuT3tzLv+YSHPXpgMNn1dcYiBsvg7dj+0aad+m/2M UlGpD3A8B1DF1O3we5hYBlOkGdh4jD2qKwKUkx+flRxEdTnzkXSKI1kjOdNFkXons9Va5EP0M/eg9 uXjNH10wWiopc/VyJkJSc69Nxs170mA4+pB7GTRSHXla+QzUvjGVFXF7/AcXPDnFCfNlClLHB2FV3 agIt8Xl/x7BVZUC9VzxFZuMza2DC56xb9POPZEzc0Nld8ulPYuRnmyBygJnR+LR+TjjuOhUaX6R1L Kv8gL4D71N5SZ0scYtCQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUY-0000000EMjK-3X2j; Tue, 19 Mar 2024 21:59:30 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUT-0000000EMey-2XuU for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:27 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6e74aa08d15so725499b3a.1 for ; Tue, 19 Mar 2024 14:59:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885563; x=1711490363; darn=lists.infradead.org; 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=dz0gWVnpXPU7ElrUaImwHtbiCroEAM5Xe/I2DTavV7g=; b=ZHfSzQ9HSXn7L5lzsuL3DgweHkzOcUjkq+w4sMMBjUHCSwluihaT0W/RVqJhuI5aIe yiK1qah+cpd1W23luuNHUI4+NXmNj2tBU38UDm3WbVdVPjlsM6F+d/PxAifpnwDC5ifU b9nodf7qmDP7UeskUIYIJ5LTlPaB3le7sAX4FF3JDWzRXeTzgK48yT8/rTuec/ghHuWY mJEUn69twBL64x7Rt1WQwOHfuBGF3GjuDcYNoivn/8K0HpXwCorCQj74HT6h5nsOx7cj CMfMUpdhAhlmjWtsPqTyGcKXkXhc9sSepCNnfvkdP0SZNMXZOxsJJNxEruuuRARUbxpC Zblw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885563; x=1711490363; 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=dz0gWVnpXPU7ElrUaImwHtbiCroEAM5Xe/I2DTavV7g=; b=JAvoIilgjfiX5LWBdxvLtWwkHc+JdeGD5tr40wwoRcdSmacoj2jxMpmUT21t5fzR05 ka8GCh8vB54Dke5TqQZMyU9L7Bs13Z3e6xbuTTiPkQbvEBfhkA3rJzNHAQE5lKu6bFUt c9EKaD1zx1cK6gqawm7ZVdLmwcnP0YR5VMXIhZP8pl/nssh5FqqPRhSEqrCHQ0Ss3aSM 4e5WLxeIW0aCbhvyheyFkpsOIGSrGyuYzhO7Zm0XaL33esVdVJckNfz+IR0rhmR+7khq 6Md79oHvcprO+xCZi4eVy51x4VntZTo50+ZxtentGuBKdzz7DyxA+jZ+2ZamtJCw+UBt Yh7A== X-Forwarded-Encrypted: i=1; AJvYcCUr7P8/zYB6RoyFZwS40IjPIW0BX0aVe0hetYCB1WZQ08YDhz8Y82zp+zBAH3yXvE7aVBWaZHAeuC1DBEfSrd4hEa7DJsmg61FNVwGoZ+Xm X-Gm-Message-State: AOJu0YxQlLf5eGwPdnANKUw73OJ14FDgYY54xDyfGd7LkbHzTUIwsKdB hetPaEhF+ATRS0e9zJbk+7MIKDbMdVRKW0mWYqYCP2TTHQTaQBCT4+OkhCe0uU4= X-Google-Smtp-Source: AGHT+IEXM30Lrne9ss/C/7W/K1gxYQD71hKRIdntgbmHeEfv/1G3LVtA8crJjeRdhBQq5PV/uA1LDA== X-Received: by 2002:a05:6a00:1482:b0:6e7:2379:dd18 with SMTP id v2-20020a056a00148200b006e72379dd18mr4524923pfu.0.1710885563216; Tue, 19 Mar 2024 14:59:23 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:22 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Andrew Jones , Guo Ren , Heiko Stuebner , Paul Walmsley Subject: [RFC PATCH 5/9] riscv: Split per-CPU and per-thread envcfg bits Date: Tue, 19 Mar 2024 14:58:31 -0700 Message-ID: <20240319215915.832127-6-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145925_683126_9ACB1961 X-CRM114-Status: GOOD ( 15.51 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some envcfg bits need to be controlled on a per-thread basis, such as the pointer masking mode. However, the envcfg CSR value cannot simply be stored in struct thread_struct, because some hardware may implement a different subset of envcfg CSR bits is across CPUs. As a result, we need to combine the per-CPU and per-thread bits whenever we switch threads. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/cpufeature.h | 2 ++ arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/switch_to.h | 12 ++++++++++++ arch/riscv/kernel/cpufeature.c | 4 +++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 0bd11862b760..b1ad8d0b4599 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -33,6 +33,8 @@ DECLARE_PER_CPU(long, misaligned_access_speed); /* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS]; +DECLARE_PER_CPU(unsigned long, riscv_cpu_envcfg); + void riscv_user_isa_enable(void); #ifdef CONFIG_RISCV_MISALIGNED diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a8509cc31ab2..06b87402a4d8 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -118,6 +118,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long envcfg; u32 riscv_v_flags; u32 vstate_ctrl; struct __riscv_v_ext_state vstate; diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 7efdb0584d47..256a354a5c4a 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -69,6 +69,17 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void sync_envcfg(struct task_struct *task) +{ + csr_write(CSR_ENVCFG, this_cpu_read(riscv_cpu_envcfg) | task->thread.envcfg); +} + +static inline void __switch_to_envcfg(struct task_struct *next) +{ + if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG)) + sync_envcfg(next); +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -80,6 +91,7 @@ do { \ __switch_to_fpu(__prev, __next); \ if (has_vector()) \ __switch_to_vector(__prev, __next); \ + __switch_to_envcfg(__next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index d1846aab1f78..32aaaf41f8a8 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -44,6 +44,8 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; /* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS]; +DEFINE_PER_CPU(unsigned long, riscv_cpu_envcfg); + /* Performance information */ DEFINE_PER_CPU(long, misaligned_access_speed); @@ -978,7 +980,7 @@ arch_initcall(check_unaligned_access_all_cpus); void riscv_user_isa_enable(void) { if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ)) - csr_set(CSR_ENVCFG, ENVCFG_CBZE); + this_cpu_or(riscv_cpu_envcfg, ENVCFG_CBZE); } #ifdef CONFIG_RISCV_ALTERNATIVE From patchwork Tue Mar 19 21:58:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597085 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 49248C54E68 for ; Tue, 19 Mar 2024 21:59: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: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=h6H1VEo432GhcW1Csa25qR2loRvUT5T8LoHcF08xUf8=; b=ht/e0SoVt+p9vS YcytY8NfWP2slS1zcBznIui3FfTusP+rVeU22OhGbJP9eofca/aIHVV61PUNPkD9mnqHiwP7cln13 uC1/JzKESL9w4/InkDhqTcEgXbM/BA2+BiX39+Sq3PRlFSoGtFo96a3yX7038yxxWhaYTSLDyrIb9 ShJlmIAZTyF+iYQFCrkRA7jKULT0fakjipc1wjX+mz6528iUQDwHXeHqPlxFemjL39bWrGq29jeNj 6W/SCUyFImeBVggQKfsh0g8AiLQmNH92ev1Vbfs2ZPhAFiJB+iRZu1k6Lx0rTqUqX53/hDi5BJ/iO 6GwzkAkZIROoas2oSrEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUd-0000000EMmJ-2WBA; Tue, 19 Mar 2024 21:59:35 +0000 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUV-0000000EMfM-3wbW for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:29 +0000 Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-5d8b887bb0cso4651426a12.2 for ; Tue, 19 Mar 2024 14:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885564; x=1711490364; darn=lists.infradead.org; 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=L9YnfDFlIzM6IgPNouej5vSECO3AYIq38694OD5b58U=; b=RQYBd2ibWTBCp4EAQntVubKy2BcjgC2JKzvpM30BIDEZouJErAigJt+e8485OswShg kjd4Vii72PiYW0Kj3OS6G1Pr+wb5wbZUg0NzRRAGLs3DazAZ8SyG/KJNg6x0m3B5wI3v SgMcctMUweJZyPEFtH1ANx9FAon4A/rKJPBtqwhxwIr3rdPsK2lU8bfrcK/Wn01uvEwO aZakJJ/QgkLelqnOq1qkM4KfKsA6SKkdHN5O/tqBhWEI2yI+6bcedYjBCKpPw36Mdytr Yel+WdANyW9CknXdzRIKIcXl4kSCJcRW83mL8K2SUE5VgNPJ3+jSDXmUE5dAkYu2Nerr fEEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885564; x=1711490364; 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=L9YnfDFlIzM6IgPNouej5vSECO3AYIq38694OD5b58U=; b=QbUL3a1HtqZRhRefIaxgyaOfJ3ParvaccIJEidS32tz1giOMuTU7vkup+8e8h3CrLF Fhiu0xZGB5+pTnwNBVuJeOank9mndoFzJ1ljXRgzAk2ZPFH7fKBWVVVIJcihDRGmEtBc s0dT8helhRfFgGMmsmuVFthWzNQ9LBW12EW5iNk2n71g3KjA4AV1rJFuN1njvQ2PTRgi Mqr53292DLd6sNvAw+fcJAyx6FQfjJbu/Ny6xup3NXa6dP8ijqaZ14Zh+oxXGNTO7zGT sjBtrUoVMb4T6cAzaj/IibeICZbR8avRo5WrerQ+XUf80tuJ3iCVTLTVCfhhrSOWz6H6 0Icg== X-Forwarded-Encrypted: i=1; AJvYcCWjhT7P01XOA1fVXZNyOeC/zyL1rSOSvDxs/KMMxbJXu4vXficVg/nc359DLUeTwHZ9m2qUW77VE+cXr+5Cv6Hu1a3TcF/BcPPTaGTaPib6 X-Gm-Message-State: AOJu0YxtJKGwPVND/fI8HeDs9XHafYKIEUlZgYbPQ9ZuWbTmLGZESRcF 1Z2KOp95cHmfkqK+pnbc1UwY1yKG37RvzZq4xxmLUHAwh0NiFiZ9RRhEsbOnFtY= X-Google-Smtp-Source: AGHT+IHrqGWm4HKmgxz4QikODSsEmQwi2ndyv1CO9uxCpMgdQukkIvJsZM3/zryU+sLiBqD988H0gQ== X-Received: by 2002:a05:6a21:350d:b0:1a3:7efc:81f4 with SMTP id zc13-20020a056a21350d00b001a37efc81f4mr1954693pzb.16.1710885564420; Tue, 19 Mar 2024 14:59:24 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:24 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Guo Ren , Paul Walmsley , Stefan Roesch Subject: [RFC PATCH 6/9] riscv: Add support for userspace pointer masking Date: Tue, 19 Mar 2024 14:58:32 -0700 Message-ID: <20240319215915.832127-7-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145928_172875_0FD0A960 X-CRM114-Status: GOOD ( 21.33 ) 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 RISC-V supports pointer masking with a variable number of tag bits ("PMLEN") and which is configured at the next higher privilege level. Wire up the PR_SET_TAGGED_ADDR_CTRL and PR_GET_TAGGED_ADDR_CTRL prctls so userspace can request a minimum number of tag bits and determine the actual number of tag bits. As with PR_TAGGED_ADDR_ENABLE, the pointer masking configuration is thread-scoped, inherited on clone() and fork() and cleared on exec(). Signed-off-by: Samuel Holland --- arch/riscv/Kconfig | 8 +++ arch/riscv/include/asm/processor.h | 8 +++ arch/riscv/kernel/process.c | 107 +++++++++++++++++++++++++++++ include/uapi/linux/prctl.h | 3 + 4 files changed, 126 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index e3142ce531a0..a1a1585120f0 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -479,6 +479,14 @@ config RISCV_ISA_C If you don't know what to do here, say Y. +config RISCV_ISA_POINTER_MASKING + bool "Smmpm, Smnpm, and Ssnpm extensions for pointer masking" + depends on 64BIT + default y + help + Add support to dynamically detect the presence of the Smmpm, Smnpm, + and Ssnpm extensions (pointer masking) and enable their usage. + config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 06b87402a4d8..64b34e839802 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -185,6 +185,14 @@ extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val); #define GET_UNALIGN_CTL(tsk, addr) get_unalign_ctl((tsk), (addr)) #define SET_UNALIGN_CTL(tsk, val) set_unalign_ctl((tsk), (val)) +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING +/* PR_{SET,GET}_TAGGED_ADDR_CTRL prctl */ +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg); +long get_tagged_addr_ctrl(struct task_struct *task); +#define SET_TAGGED_ADDR_CTRL(arg) set_tagged_addr_ctrl(current, arg) +#define GET_TAGGED_ADDR_CTRL() get_tagged_addr_ctrl(current) +#endif + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 92922dbd5b5c..3578e75f4aa4 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -7,6 +7,7 @@ * Copyright (C) 2017 SiFive */ +#include #include #include #include @@ -154,6 +155,18 @@ void start_thread(struct pt_regs *regs, unsigned long pc, #endif } +static void flush_tagged_addr_state(void) +{ +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SxNPM)) + return; + + current->thread.envcfg &= ~ENVCFG_PMM; + + sync_envcfg(current); +#endif +} + void flush_thread(void) { #ifdef CONFIG_FPU @@ -173,6 +186,7 @@ void flush_thread(void) memset(¤t->thread.vstate, 0, sizeof(struct __riscv_v_ext_state)); clear_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE); #endif + flush_tagged_addr_state(); } void arch_release_task_struct(struct task_struct *tsk) @@ -236,3 +250,96 @@ void __init arch_task_cache_init(void) { riscv_v_setup_ctx_cache(); } + +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING +static bool have_user_pmlen_7; +static bool have_user_pmlen_16; + +long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) +{ + unsigned long valid_mask = PR_PMLEN_MASK; + struct thread_info *ti = task_thread_info(task); + u8 pmlen; + + if (is_compat_thread(ti)) + return -EINVAL; + + if (arg & ~valid_mask) + return -EINVAL; + + pmlen = FIELD_GET(PR_PMLEN_MASK, arg); + if (pmlen > 16) { + return -EINVAL; + } else if (pmlen > 7) { + if (have_user_pmlen_16) + pmlen = 16; + else + return -EINVAL; + } else if (pmlen > 0) { + /* + * Prefer the smallest PMLEN that satisfies the user's request, + * in case choosing a larger PMLEN has a performance impact. + */ + if (have_user_pmlen_7) + pmlen = 7; + else if (have_user_pmlen_16) + pmlen = 16; + else + return -EINVAL; + } + + task->thread.envcfg &= ~ENVCFG_PMM; + if (pmlen == 7) + task->thread.envcfg |= ENVCFG_PMM_PMLEN_7; + else if (pmlen == 16) + task->thread.envcfg |= ENVCFG_PMM_PMLEN_16; + + if (task == current) + sync_envcfg(current); + + return 0; +} + +long get_tagged_addr_ctrl(struct task_struct *task) +{ + struct thread_info *ti = task_thread_info(task); + long ret = 0; + + if (is_compat_thread(ti)) + return -EINVAL; + + switch (task->thread.envcfg & ENVCFG_PMM) { + case ENVCFG_PMM_PMLEN_7: + ret |= FIELD_PREP(PR_PMLEN_MASK, 7); + break; + case ENVCFG_PMM_PMLEN_16: + ret |= FIELD_PREP(PR_PMLEN_MASK, 16); + break; + } + + return ret; +} + +static bool try_to_set_pmm(unsigned long value) +{ + csr_set(CSR_ENVCFG, value); + return (csr_read_clear(CSR_ENVCFG, ENVCFG_PMM) & ENVCFG_PMM) == value; +} + +static int __init tagged_addr_init(void) +{ + if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SxNPM)) + return 0; + + /* + * envcfg.PMM is a WARL field. Detect which values are supported. + * Assume the supported PMLEN values are the same on all harts. + */ + csr_clear(CSR_ENVCFG, ENVCFG_PMM); + have_user_pmlen_7 = try_to_set_pmm(ENVCFG_PMM_PMLEN_7); + have_user_pmlen_16 = try_to_set_pmm(ENVCFG_PMM_PMLEN_16); + + return 0; +} +core_initcall(tagged_addr_init); +#endif /* CONFIG_RISCV_ISA_POINTER_MASKING */ diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 370ed14b1ae0..488b0d8e8495 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -244,6 +244,9 @@ struct prctl_mm_map { # define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) /* Unused; kept only for source compatibility */ # define PR_MTE_TCF_SHIFT 1 +/* RISC-V pointer masking tag length */ +# define PR_PMLEN_SHIFT 24 +# define PR_PMLEN_MASK (0x7fUL << PR_PMLEN_SHIFT) /* Control reclaim behavior when allocating memory */ #define PR_SET_IO_FLUSHER 57 From patchwork Tue Mar 19 21:58:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597087 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 B652BCD11DC for ; Tue, 19 Mar 2024 21:59: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: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=9v3Bt0Fzls8swId5mt3M+G2tw3nIlSxxhL6YENVRjZ4=; b=fUOKn5CNneYUqO p1gy8WmSAPPVcNywdcs8yTP1D6tpqbq1LtagRaF1gUYZs9xl8bOpZ8ppzD6Wol8TRn4s+zRqR8Sw7 1j/OFkzbM1eMXNdIZ7Fn5AXdL1RAkzmxZE6g8KVddz89Bd+CNxEeY2xoWV/x9XiMpd2oiHRfLXnHf Iv9OLfMJJOyVnFJAZZ/R8ddsNTsHKh5jmnJDtTDzyppgH+tc+iZyDGkDGJxV53H1ctJcsaFbJ60x7 FwJl3sYKdN7C713X9ZJVcA/zNO8hHm6kivkHMcmjlMzCxJcHFqZeIcAdq79+xdTQBST5hT0Btr2E0 LUQxq8yF92CN2LAvGYLw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUh-0000000EMpQ-3TVB; Tue, 19 Mar 2024 21:59:39 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUW-0000000EMh1-3jwE for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:31 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6e782e955adso75013b3a.3 for ; Tue, 19 Mar 2024 14:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885567; x=1711490367; darn=lists.infradead.org; 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=//J7AYtv61iHxtKJQn3MiSxiIHpG2s0TTUnDmfuJahA=; b=Q96xe04TSIkUCqTY5Tl684ziyNkoAV/4lkGkFwj/qbQFttHvcODkYu7bg3EajeOQHo 1WO/BkdEmAIxJFGNTjj2hYPzYVSACOC8vFToRoLu5CsdT7h8XK7BZLk1gAu6D+YrUS8l tuFMsqUyp5gwJQvjaw0am5ZBoN+9eAzWqfldyY5DmHQCZ9Ipgqg3EN24UH2cFhAbfzMT vWmKpKNPFGyLeLHJvyLg/Ar/Yql6XdHkkI5QXtXi9PsCB2wUFAG3zvIg75sNYnjVQTiK Ig7EbiLqtfespaIiv4LCx21hCHUOYFv7NRQLFHqeMVDMWKi3raGNd/wofEsfPO7dsIWG FusQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885567; x=1711490367; 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=//J7AYtv61iHxtKJQn3MiSxiIHpG2s0TTUnDmfuJahA=; b=EYzhGlCIfpzPuzsxforG9YzVqqMFD88p16Dsx9ntTsMMJvAFFOiNSQwLiQfJvhnkqQ V5Wm8LTlo6FBHhvZmab+W7wzC3HD7404cMX1+M+cwbkEVFiJ4tBt3o3V8HYlWh+YvEAV DPnzZykNxyilMIOret93pW7eCGJkvBhSbjqEIoV6C6Dhk2eCx6e6G3MtNmcQ0OREwKxm zYVaTooE1KgszJ1PyXBNdwy3cEyhEqDFU5rQ0DAYWPHeTppPUZ+cxZIsV8WbM+NsTEa5 1pTtu7fmpSv1pwZQTEVheEXkv5bvUMVw3tYmnE5JfuS3gD5eL+MwIj7fVOvOIGczXPiQ hH4w== X-Forwarded-Encrypted: i=1; AJvYcCVrR0DAh/AB7EjS+nMvZ9wF1dc42JYB3kBY2NuvWhwsHcn4aHytIPPT64Dbtwe9XoTq1p5+ffPxG2Ei5VOejJVS6PWvQeuLlUaGTEHKaZHq X-Gm-Message-State: AOJu0YzKVjO88mmZTmaZ7SX8c1MjpwwT+/h3MPVNpN+LlQdi+JSXhVwU XD+VkSomZIkO8XPx9ergnMwVw9t3g4S/DZbaXRcdLu5MzM7x3JJEk9kUFGCjoY4= X-Google-Smtp-Source: AGHT+IFBtKPHZqAeY1IySQ3pzoH0nMl2mX+/T7GPlQacqCenBzo0xWbZizuV66Z+QSEH8NLBCD7ebQ== X-Received: by 2002:a05:6a00:2447:b0:6e6:aae6:acfe with SMTP id d7-20020a056a00244700b006e6aae6acfemr19345714pfj.23.1710885565732; Tue, 19 Mar 2024 14:59:25 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:25 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Greentime Hu Subject: [RFC PATCH 7/9] riscv: Add support for the tagged address ABI Date: Tue, 19 Mar 2024 14:58:33 -0700 Message-ID: <20240319215915.832127-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145929_392748_67D3F959 X-CRM114-Status: GOOD ( 24.31 ) 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 When pointer masking is enabled for userspace, the kernel can accept tagged pointers as arguments to some system calls. Allow this by untagging the pointers in access_ok() and the uaccess routines. The software untagging in the uaccess routines is required because U-mode and S-mode have entirely separate pointer masking configurations. Signed-off-by: Samuel Holland --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/uaccess.h | 40 +++++++++++++++++++++--- arch/riscv/kernel/process.c | 49 +++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 64b34e839802..cdc8569b2118 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -124,6 +124,7 @@ struct thread_struct { struct __riscv_v_ext_state vstate; unsigned long align_ctl; struct __riscv_v_ext_state kernel_vstate; + u8 pmlen; }; /* Whitelist the fstate from the task_struct for hardened usercopy */ diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uaccess.h index ec0cab9fbddd..ed282dcf9a6d 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -9,8 +9,38 @@ #define _ASM_RISCV_UACCESS_H #include +#include #include /* for TASK_SIZE */ +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING +static inline unsigned long __untagged_addr(unsigned long addr) +{ + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SxNPM)) { + u8 shift = current->thread.pmlen; + + /* + * Virtual addresses are sign-extended, while + * physical addresses are zero-extended. + */ + if (IS_ENABLED(CONFIG_MMU)) + return (long)(addr << shift) >> shift; + else + return (addr << shift) >> shift; + } + + return addr; +} + +#define untagged_addr(addr) ({ \ + unsigned long __addr = (__force unsigned long)(addr); \ + (__force __typeof__(addr))__untagged_addr(__addr); \ +}) + +#define access_ok(addr, size) likely(__access_ok(untagged_addr(addr), size)) +#else +#define untagged_addr(addr) addr +#endif + /* * User space memory access functions */ @@ -130,7 +160,7 @@ do { \ */ #define __get_user(x, ptr) \ ({ \ - const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + const __typeof__(*(ptr)) __user *__gu_ptr = untagged_addr(ptr); \ long __gu_err = 0; \ \ __chk_user_ptr(__gu_ptr); \ @@ -246,7 +276,7 @@ do { \ */ #define __put_user(x, ptr) \ ({ \ - __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ + __typeof__(*(ptr)) __user *__gu_ptr = untagged_addr(ptr); \ __typeof__(*__gu_ptr) __val = (x); \ long __pu_err = 0; \ \ @@ -293,13 +323,13 @@ unsigned long __must_check __asm_copy_from_user(void *to, static inline unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n) { - return __asm_copy_from_user(to, from, n); + return __asm_copy_from_user(to, untagged_addr(from), n); } static inline unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n) { - return __asm_copy_to_user(to, from, n); + return __asm_copy_to_user(untagged_addr(to), from, n); } extern long strncpy_from_user(char *dest, const char __user *src, long count); @@ -314,7 +344,7 @@ unsigned long __must_check clear_user(void __user *to, unsigned long n) { might_fault(); return access_ok(to, n) ? - __clear_user(to, n) : n; + __clear_user(untagged_addr(to), n) : n; } #define __get_kernel_nofault(dst, src, type, err_label) \ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 3578e75f4aa4..36129040b7bd 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -162,6 +162,7 @@ static void flush_tagged_addr_state(void) return; current->thread.envcfg &= ~ENVCFG_PMM; + current->thread.pmlen = 0; sync_envcfg(current); #endif @@ -255,9 +256,14 @@ void __init arch_task_cache_init(void) static bool have_user_pmlen_7; static bool have_user_pmlen_16; +/* + * Control the relaxed ABI allowing tagged user addresses into the kernel. + */ +static unsigned int tagged_addr_disabled; + long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) { - unsigned long valid_mask = PR_PMLEN_MASK; + unsigned long valid_mask = PR_PMLEN_MASK | PR_TAGGED_ADDR_ENABLE; struct thread_info *ti = task_thread_info(task); u8 pmlen; @@ -288,12 +294,25 @@ long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) return -EINVAL; } + /* + * Do not allow the enabling of the tagged address ABI if globally + * disabled via sysctl abi.tagged_addr_disabled, if pointer masking + * is disabled for userspace. + */ + if (arg & PR_TAGGED_ADDR_ENABLE && (tagged_addr_disabled || !pmlen)) + return -EINVAL; + task->thread.envcfg &= ~ENVCFG_PMM; if (pmlen == 7) task->thread.envcfg |= ENVCFG_PMM_PMLEN_7; else if (pmlen == 16) task->thread.envcfg |= ENVCFG_PMM_PMLEN_16; + if (arg & PR_TAGGED_ADDR_ENABLE) + task->thread.pmlen = pmlen; + else + task->thread.pmlen = 0; + if (task == current) sync_envcfg(current); @@ -308,6 +327,13 @@ long get_tagged_addr_ctrl(struct task_struct *task) if (is_compat_thread(ti)) return -EINVAL; + if (task->thread.pmlen) + ret = PR_TAGGED_ADDR_ENABLE; + + /* + * The task's pmlen is only set if the tagged address ABI is enabled, + * so the effective PMLEN must be extracted from envcfg.PMM. + */ switch (task->thread.envcfg & ENVCFG_PMM) { case ENVCFG_PMM_PMLEN_7: ret |= FIELD_PREP(PR_PMLEN_MASK, 7); @@ -326,6 +352,24 @@ static bool try_to_set_pmm(unsigned long value) return (csr_read_clear(CSR_ENVCFG, ENVCFG_PMM) & ENVCFG_PMM) == value; } +/* + * Global sysctl to disable the tagged user addresses support. This control + * only prevents the tagged address ABI enabling via prctl() and does not + * disable it for tasks that already opted in to the relaxed ABI. + */ + +static struct ctl_table tagged_addr_sysctl_table[] = { + { + .procname = "tagged_addr_disabled", + .mode = 0644, + .data = &tagged_addr_disabled, + .maxlen = sizeof(int), + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, +}; + static int __init tagged_addr_init(void) { if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SxNPM)) @@ -339,6 +383,9 @@ static int __init tagged_addr_init(void) have_user_pmlen_7 = try_to_set_pmm(ENVCFG_PMM_PMLEN_7); have_user_pmlen_16 = try_to_set_pmm(ENVCFG_PMM_PMLEN_16); + if (!register_sysctl("abi", tagged_addr_sysctl_table)) + return -EINVAL; + return 0; } core_initcall(tagged_addr_init); From patchwork Tue Mar 19 21:58:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597082 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 BA27CCD11DD for ; Tue, 19 Mar 2024 21:59:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ilMoEC+uRHnhokm+vGp4faT+VMpuS66v+tw/+y045Ys=; b=NqCjY+LQ+cB7zX /tmPz+gjRrcKOeYDnxbrjPYkm2WMaKg3qjUPNIL5QPQG67/8Jg5z4bmPVAReUlG5pSKSGj9U4C0rm iB+JZFUh5kl8Q4v4cqupTkVKFmkqr1sFnErM3uMPsyjT+huSBsPyU2736hfPi/hiBeEiSX/InHFMU cOTgUX3OSHYW79fCL7EdoBMcV5roYOOoF2cx2z2kUd1ljjEmcvCD+nIkewJTZsElP6ykk8CnaT4qW //WDrp8mOwIAaWwzmlRiW2/ZokbeWB/cL6lK+4VQv1QSqzPWdEMIuuVU5Zvr1a/r8j1sATZHXSCf7 4OZkAyZW1cWX6K/cO3uQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUc-0000000EMlP-37PS; Tue, 19 Mar 2024 21:59:34 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUV-0000000EMh0-2LNY for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:28 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-6e6f6d782e4so4001062b3a.0 for ; Tue, 19 Mar 2024 14:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885567; x=1711490367; darn=lists.infradead.org; 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=5y8ITBc9KMnXCgtx77FHcE1LkP6cPCkee9IZD/0lAIU=; b=lHAyxCKEMomcOIHhpEQ5jzppiCLOx3dPcKOIYH3Is90u3z3TxTYlf3zNCuZqUmCOgu 6Zplox9h6+eZW51+PsD5U1Hb2tFbi6tEb3UUKz2J15G2xlReOHXSBbJePQ4UeQsN/NWU xRoc70ic2ioYG7oNe0JfmrpyB9hx8esqYe23fTUZ/gv1SLIbTct570GRp1aRWvxv9ohE MbyaZYrDgUZBMBcgDtSZ5biD2M/8o0XR/uwEmkPbtTsMQ/B4jWTSll51d5XESUFz4cjZ GsBJsQ+Uctgp79cKSKVODm7t3LrKg6ROvOHkV90o1rKkvWcQaJwIhTgzO/OWHmzrlKgV lbyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885567; x=1711490367; 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=5y8ITBc9KMnXCgtx77FHcE1LkP6cPCkee9IZD/0lAIU=; b=N0JMf436pCObpc7IV9VRaFcOJZXglnvK+vwROf1z4ULO1idU1A8fFtj6HYdTwgnZZ/ a9YeEVeePm74XZI9fK3SZTfQPCGndXLMlnuc+CFGUpg+f35oXSDIxg1XrcMKv3rujA9J 84/I2USgj/oygO5tNGctSLycf4/3P/nCH8n29HuHfe0DfOAxCgLaTVYrXe44DSCPvLDA 8kuIlzbE4iKLHBSz8iTq0Wu3SLO7cUGnnOULc1OOEko3QdD8tEcMoDeR1YipW0hswAmj 0nT9+DQHTCn/rph+c6mIIyZgn3WMmsHEJn9GtVOIR2I5PwYk0WXPqvQTSJauSRXdlTMK uaiw== X-Forwarded-Encrypted: i=1; AJvYcCV7w7BLb+JgNpMLzKoGcUM9d3deQMzlxoTpJ25TYEdf1mCCjCUePjETTT7fUuVp3wAQPYVH0aYlZhkd3ToqzxcOWkK2Tw69ehKTbdrNjIhB X-Gm-Message-State: AOJu0YycQDVF3mPU8QKTy9i1lHIBh+uhEVW86/OGCsAXlFI3jYpuvAkm 2Xu7bEHDeRHOI8FHmteVzuJTL8jBdsYNCPa/haFAD2GavKOyyWz7LcPcQAdsNtI= X-Google-Smtp-Source: AGHT+IHUdXawCkbimlLWn+n2NYwUpCHwdCxYmf1G6S225P30rVgH1guDbncjMramMLIp2vM31iLyBg== X-Received: by 2002:a05:6a20:3942:b0:1a3:2f9e:b0da with SMTP id r2-20020a056a20394200b001a32f9eb0damr14091704pzg.23.1710885567124; Tue, 19 Mar 2024 14:59:27 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:26 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Alejandro Colomar , Oleg Nesterov , Paul Walmsley Subject: [RFC PATCH 8/9] riscv: Allow ptrace control of the tagged address ABI Date: Tue, 19 Mar 2024 14:58:34 -0700 Message-ID: <20240319215915.832127-9-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145927_633247_349470A3 X-CRM114-Status: GOOD ( 13.27 ) 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 allows a tracer to control the ABI of the tracee, as on arm64. Signed-off-by: Samuel Holland --- arch/riscv/kernel/ptrace.c | 42 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/elf.h | 1 + 2 files changed, 43 insertions(+) diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index e8515aa9d80b..3d414db2118b 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -28,6 +28,9 @@ enum riscv_regset { #ifdef CONFIG_RISCV_ISA_V REGSET_V, #endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + REGSET_TAGGED_ADDR_CTRL, +#endif }; static int riscv_gpr_get(struct task_struct *target, @@ -152,6 +155,35 @@ static int riscv_vr_set(struct task_struct *target, } #endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING +static int tagged_addr_ctrl_get(struct task_struct *target, + const struct user_regset *regset, + struct membuf to) +{ + long ctrl = get_tagged_addr_ctrl(target); + + if (IS_ERR_VALUE(ctrl)) + return ctrl; + + return membuf_write(&to, &ctrl, sizeof(ctrl)); +} + +static int tagged_addr_ctrl_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; + long ctrl; + + ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1); + if (ret) + return ret; + + return set_tagged_addr_ctrl(target, ctrl); +} +#endif + static const struct user_regset riscv_user_regset[] = { [REGSET_X] = { .core_note_type = NT_PRSTATUS, @@ -182,6 +214,16 @@ static const struct user_regset riscv_user_regset[] = { .set = riscv_vr_set, }, #endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + [REGSET_TAGGED_ADDR_CTRL] = { + .core_note_type = NT_RISCV_TAGGED_ADDR_CTRL, + .n = 1, + .size = sizeof(long), + .align = sizeof(long), + .regset_get = tagged_addr_ctrl_get, + .set = tagged_addr_ctrl_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 9417309b7230..90806024fed6 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -447,6 +447,7 @@ typedef struct elf64_shdr { #define NT_MIPS_MSA 0x802 /* MIPS SIMD registers */ #define NT_RISCV_CSR 0x900 /* RISC-V Control and Status Registers */ #define NT_RISCV_VECTOR 0x901 /* RISC-V vector registers */ +#define NT_RISCV_TAGGED_ADDR_CTRL 0x902 /* RISC-V tagged address control (prctl()) */ #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 Mar 19 21:58:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13597089 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 D0433C6FD1F for ; Tue, 19 Mar 2024 21:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=v3WUl/9g5EVKvbDO5h0No6YzSZ82CtZzUaKItGHytiE=; b=sl79CZifN3UgQZ aStO6oKnJ20X0njogipeVK5dl7nvrhZRf4WYM1U6QvxQHEotrrGo9EWK2ePb3tzUaynXBGfdFf7dd C1rLx3jUKPpgqwxnoeNqTRfQ+v7++8ktyL2lV3JW68yCvOmqfYE4e5HCWGh7wJLFHkiv5F77FmyU+ BjlusoZQaX64MJjY0qRHUSK5j5zULfHAqMViszjCYWYqZnTJWlomjX9hz9ysZJekj/5ItRVsBxqff VRfQICj6Z9pz+fiSSzSZxMOt+3HGt0Un893sLtCfoeFzA1pb5aRrN5u93snNmYqHvoPXYrKkM/DHl cq3IL7gJ8ZMz54pRZMtQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUj-0000000EMqg-0eVj; Tue, 19 Mar 2024 21:59:41 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rmhUY-0000000EMi6-0hm2 for linux-riscv@lists.infradead.org; Tue, 19 Mar 2024 21:59:32 +0000 Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-5dbf7b74402so4293664a12.0 for ; Tue, 19 Mar 2024 14:59:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710885568; x=1711490368; darn=lists.infradead.org; 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=Pby1JczWDGJraZTWE5gkOPCKAXwxysMeCn/eal1+wiY=; b=hvkNNudOKwUf2kJziRwOl7Hp8QUlVj1Iv7IVIS0UU1/OTEWhifxYHosyY95032UoCc HUszsOBhYbKDrQW8bPGVDHg/efEDbz2tLsciiEMc2sVk9p1yLhe6IjxrFW77gndpb58c WBbRyLu29AT9UwKbTeiMgSCoklifOszqAELU7Dzn/C8tywT5CcxOAKjUjboQB9XkcvZf y7nySR3x9Fj9gnsyC23VzcKeU++VX8rbNY5cvn5U7a5efDa5XcWgj9YsY+vsYttst0M+ zwYNA7Ty4b8Sh/fRzilM+6IpWN4BWdMCy4NNOAu8BpW2l7bBZUGDGI7TjLUc2HV66iHu gZmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710885568; x=1711490368; 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=Pby1JczWDGJraZTWE5gkOPCKAXwxysMeCn/eal1+wiY=; b=jbiJiDetJeBtKqzn/aJRnSoY8MM+KrzrbacGSukW7PeZCDbjIZtpKBIAa0Oe2cOPNe vNcFJeq0eTpqYHH1DQtaPv02++T+u2HcTeVPKDO03g6alQq/ORcmBMFQC012k94148Dj ZdUsLHK1kzylgssZH/HOtV1ex9sHvxOq3jXInCK5qyJQ9M8qmxbB0Gckst8eKnUZ4jEJ ne2X48Wn/Ox8zTsvfUn1zMh4PDj7+k8l0TG24YjamGiEYmpsvrMH4zBInbmn6w9cbVy3 ujq8dMqNZoILOKx+UAxLhuPBOKksBIG0Atv53AlgrrJpAfUloHULnAuyMtR2sHWo2xdc fhgA== X-Forwarded-Encrypted: i=1; AJvYcCXwcpz9fToLdpzJPhQxEY4ZfzVkBXwLusxD/qivP0o6Tep7ohh7QcIxRYe429kKcOyCavApVclNz39eUvzBhW6rwOf2I8ExC8J2GjOIatgr X-Gm-Message-State: AOJu0Yw8xb3Rb1MyrULS2Wmr43bsaJ1FKsWhoeaVMiMiuvBs2LAnGt63 ICq+7Th2C45vK8t5uc/XxpexR3IRzWwe3cJgHujdq9CxMSAN5QkIVtgpV8XT9Js= X-Google-Smtp-Source: AGHT+IEA586m37jAo+36FgUE63sLe4XTBKY0jo4h+XaiaT2qw9bA66mDdOVeEhZbRIOGXg5o3vgG/Q== X-Received: by 2002:a05:6a20:c91b:b0:1a1:15ff:43b with SMTP id gx27-20020a056a20c91b00b001a115ff043bmr15335731pzb.23.1710885568321; Tue, 19 Mar 2024 14:59:28 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id z25-20020aa785d9000000b006e6c61b264bsm10273892pfn.32.2024.03.19.14.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 14:59:28 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, tech-j-ext@lists.risc-v.org, Conor Dooley , kasan-dev@googlegroups.com, Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , Samuel Holland , Albert Ou , Shuah Khan Subject: [RFC PATCH 9/9] selftests: riscv: Add a pointer masking test Date: Tue, 19 Mar 2024 14:58:35 -0700 Message-ID: <20240319215915.832127-10-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240319215915.832127-1-samuel.holland@sifive.com> References: <20240319215915.832127-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240319_145930_355016_82956818 X-CRM114-Status: GOOD ( 21.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: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This test covers the behavior of the PR_SET_TAGGED_ADDR_CTRL and PR_GET_TAGGED_ADDR_CTRL prctl() operations, their effects on the userspace ABI, and their effects on the system call ABI. Signed-off-by: Samuel Holland --- tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/tags/Makefile | 10 + .../selftests/riscv/tags/pointer_masking.c | 307 ++++++++++++++++++ 3 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/tags/Makefile create mode 100644 tools/testing/selftests/riscv/tags/pointer_masking.c diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 4a9ff515a3a0..6e7e6621a71a 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell uname -m 2>/dev/null || echo not) ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?= hwprobe vector mm +RISCV_SUBTARGETS ?= hwprobe mm tags vector else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/tags/Makefile b/tools/testing/selftests/riscv/tags/Makefile new file mode 100644 index 000000000000..ed82ff9c664e --- /dev/null +++ b/tools/testing/selftests/riscv/tags/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 + +CFLAGS += -I$(top_srcdir)/tools/include + +TEST_GEN_PROGS := pointer_masking + +include ../../lib.mk + +$(OUTPUT)/pointer_masking: pointer_masking.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/tags/pointer_masking.c b/tools/testing/selftests/riscv/tags/pointer_masking.c new file mode 100644 index 000000000000..c9f66e8436ab --- /dev/null +++ b/tools/testing/selftests/riscv/tags/pointer_masking.c @@ -0,0 +1,307 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +#ifndef PR_PMLEN_SHIFT +#define PR_PMLEN_SHIFT 24 +#endif +#ifndef PR_PMLEN_MASK +#define PR_PMLEN_MASK (0x7fUL << PR_PMLEN_SHIFT) +#endif + +static int dev_zero; + +static sigjmp_buf jmpbuf; + +static void sigsegv_handler(int sig) +{ + siglongjmp(jmpbuf, 1); +} + +static int min_pmlen; +static int max_pmlen; + +static inline bool valid_pmlen(int pmlen) +{ + return pmlen == 0 || pmlen == 7 || pmlen == 16; +} + +static void test_pmlen(void) +{ + ksft_print_msg("Testing available PMLEN values\n"); + + for (int request = 0; request <= 16; request++) { + int pmlen, ret; + + ret = prctl(PR_SET_TAGGED_ADDR_CTRL, request << PR_PMLEN_SHIFT, 0, 0, 0); + if (ret) { + ksft_test_result_skip("PMLEN=%d PR_GET_TAGGED_ADDR_CTRL\n", request); + ksft_test_result_skip("PMLEN=%d constraint\n", request); + ksft_test_result_skip("PMLEN=%d validity\n", request); + continue; + } + + ret = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + ksft_test_result(ret >= 0, "PMLEN=%d PR_GET_TAGGED_ADDR_CTRL\n", request); + if (ret < 0) { + ksft_test_result_skip("PMLEN=%d constraint\n", request); + ksft_test_result_skip("PMLEN=%d validity\n", request); + continue; + } + + pmlen = (ret & PR_PMLEN_MASK) >> PR_PMLEN_SHIFT; + ksft_test_result(pmlen >= request, "PMLEN=%d constraint\n", request); + ksft_test_result(valid_pmlen(pmlen), "PMLEN=%d validity\n", request); + + if (min_pmlen == 0) + min_pmlen = pmlen; + if (max_pmlen < pmlen) + max_pmlen = pmlen; + } + + if (max_pmlen == 0) + ksft_exit_fail_msg("Failed to enable pointer masking\n"); +} + +static int set_tagged_addr_ctrl(int pmlen, bool tagged_addr_abi) +{ + int arg, ret; + + arg = pmlen << PR_PMLEN_SHIFT | tagged_addr_abi; + ret = prctl(PR_SET_TAGGED_ADDR_CTRL, arg, 0, 0, 0); + if (!ret) { + ret = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + if (ret == arg) + return 0; + } + + return ret < 0 ? -errno : -ENODATA; +} + +static void test_dereference_pmlen(int pmlen) +{ + static volatile int i; + volatile int *p; + int ret; + + ret = set_tagged_addr_ctrl(pmlen, false); + if (ret) + return ksft_test_result_error("PMLEN=%d setup (%d)\n", pmlen, ret); + + i = pmlen; + + if (pmlen) { + p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen); + + /* These dereferences should succeed. */ + if (sigsetjmp(jmpbuf, 1)) + return ksft_test_result_fail("PMLEN=%d valid tag\n", pmlen); + if (*p != pmlen) + return ksft_test_result_fail("PMLEN=%d bad value\n", pmlen); + *p++; + } + + p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen - 1); + + /* These dereferences should raise SIGSEGV. */ + if (sigsetjmp(jmpbuf, 1)) + return ksft_test_result_pass("PMLEN=%d dereference\n", pmlen); + *p++; + ksft_test_result_fail("PMLEN=%d invalid tag\n", pmlen); +} + +static void test_dereference(void) +{ + ksft_print_msg("Testing userspace pointer dereference\n"); + + signal(SIGSEGV, sigsegv_handler); + + test_dereference_pmlen(0); + test_dereference_pmlen(min_pmlen); + test_dereference_pmlen(max_pmlen); + + signal(SIGSEGV, SIG_DFL); +} + +static void test_fork_exec(void) +{ + int ret, status; + + ksft_print_msg("Testing fork/exec behavior\n"); + + ret = set_tagged_addr_ctrl(min_pmlen, false); + if (ret) + return ksft_test_result_error("setup (%d)\n", ret); + + if (fork()) { + wait(&status); + ksft_test_result(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "dereference after fork\n"); + } else { + static volatile int i; + volatile int *p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - min_pmlen); + + exit(*p); + } + + if (fork()) { + wait(&status); + ksft_test_result(WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV, + "dereference after fork+exec\n"); + } else { + execl("/proc/self/exe", "", NULL); + } +} + +static void test_tagged_addr_abi_sysctl(void) +{ + char value; + int fd; + + ksft_print_msg("Testing tagged address ABI sysctl\n"); + + fd = open("/proc/sys/abi/tagged_addr_disabled", O_WRONLY); + if (fd < 0) { + ksft_test_result_skip("failed to open sysctl file\n"); + ksft_test_result_skip("failed to open sysctl file\n"); + return; + } + + value = '1'; + pwrite(fd, &value, 1, 0); + ksft_test_result(set_tagged_addr_ctrl(min_pmlen, true) == -EINVAL, + "sysctl disabled\n"); + + value = '0'; + pwrite(fd, &value, 1, 0); + ksft_test_result(set_tagged_addr_ctrl(min_pmlen, true) == 0, + "sysctl enabled\n"); + + set_tagged_addr_ctrl(0, false); + + close(fd); +} + +static void test_tagged_addr_abi_pmlen(int pmlen) +{ + int i, *p, ret; + + i = ~pmlen; + + if (pmlen) { + p = (int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen); + + ret = set_tagged_addr_ctrl(pmlen, false); + if (ret) + return ksft_test_result_error("PMLEN=%d ABI disabled setup (%d)\n", + pmlen, ret); + + ret = write(dev_zero, p, sizeof(*p)); + if (ret >= 0 || errno != EFAULT) + return ksft_test_result_fail("PMLEN=%d ABI disabled write\n", pmlen); + + ret = read(dev_zero, p, sizeof(*p)); + if (ret >= 0 || errno != EFAULT) + return ksft_test_result_fail("PMLEN=%d ABI disabled read\n", pmlen); + + if (i != ~pmlen) + return ksft_test_result_fail("PMLEN=%d ABI disabled value\n", pmlen); + + ret = set_tagged_addr_ctrl(pmlen, true); + if (ret) + return ksft_test_result_error("PMLEN=%d ABI enabled setup (%d)\n", + pmlen, ret); + + ret = write(dev_zero, p, sizeof(*p)); + if (ret != sizeof(*p)) + return ksft_test_result_fail("PMLEN=%d ABI enabled write\n", pmlen); + + ret = read(dev_zero, p, sizeof(*p)); + if (ret != sizeof(*p)) + return ksft_test_result_fail("PMLEN=%d ABI enabled read\n", pmlen); + + if (i) + return ksft_test_result_fail("PMLEN=%d ABI enabled value\n", pmlen); + + i = ~pmlen; + } else { + /* The tagged address ABI cannot be enabled when PMLEN == 0. */ + ret = set_tagged_addr_ctrl(pmlen, true); + if (ret != -EINVAL) + return ksft_test_result_error("PMLEN=%d ABI setup (%d)\n", + pmlen, ret); + } + + p = (int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen - 1); + + ret = write(dev_zero, p, sizeof(*p)); + if (ret >= 0 || errno != EFAULT) + return ksft_test_result_fail("PMLEN=%d invalid tag write (%d)\n", pmlen, errno); + + ret = read(dev_zero, p, sizeof(*p)); + if (ret >= 0 || errno != EFAULT) + return ksft_test_result_fail("PMLEN=%d invalid tag read\n", pmlen); + + if (i != ~pmlen) + return ksft_test_result_fail("PMLEN=%d invalid tag value\n", pmlen); + + ksft_test_result_pass("PMLEN=%d tagged address ABI\n", pmlen); +} + +static void test_tagged_addr_abi(void) +{ + ksft_print_msg("Testing tagged address ABI\n"); + + test_tagged_addr_abi_pmlen(0); + test_tagged_addr_abi_pmlen(min_pmlen); + test_tagged_addr_abi_pmlen(max_pmlen); +} + +static struct test_info { + unsigned int nr_tests; + void (*test_fn)(void); +} tests[] = { + { .nr_tests = 17 * 3, test_pmlen }, + { .nr_tests = 3, test_dereference }, + { .nr_tests = 2, test_fork_exec }, + { .nr_tests = 2, test_tagged_addr_abi_sysctl }, + { .nr_tests = 3, test_tagged_addr_abi }, +}; + +int main(int argc, char **argv) +{ + unsigned int plan = 0; + + /* Check if this is the child process after execl(). */ + if (!argv[0][0]) { + static volatile int i; + volatile int *p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - 7); + + return *p; + } + + dev_zero = open("/dev/zero", O_RDWR); + if (dev_zero < 0) + return 1; + + ksft_print_header(); + + for (int i = 0; i < ARRAY_SIZE(tests); ++i) + plan += tests[i].nr_tests; + + ksft_set_plan(plan); + + for (int i = 0; i < ARRAY_SIZE(tests); ++i) + tests[i].test_fn(); + + ksft_finished(); +}