From patchwork Tue Jun 25 21:09:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712054 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 16694C2BBCA for ; Tue, 25 Jun 2024 21:10:06 +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=CcHTUmNzz7Yz5Z8Asc8QoQONZF+lHDJZizrE+nhMo/s=; b=HLPOFUqifyGKlt l57bNRbp3Tdzt8VfxIIsiWQcloWk7iD1HunPav6TBcwjXc9ZxTtkx0kybBvySuX0bRK2Y2nOTou28 a2BBSlSpLpGfDCFFnWwRnT9OoxX5R/pbkjU6S59sCeCUjGR5fObggGUzoddwbwsuIYc3V9TsplD4L nW0ZyG397i+7Bs85Xy2Ruz3m50uq2t4wEPVlScWWOeUgHCJ35OwBLmHje2qyRtETuvtHKXTNJbP6I +6ltSFfE20f5dtPpQsh8HJsyr8qhTZ7MlyUd/HJAu42dwoK/STaLqxOjb3Dta/O23RuzfiqCrFoGa W4SygxjQjPjX7N94MiCA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ6-00000004V4W-2AWH; Tue, 25 Jun 2024 21:09:42 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ3-00000004V2J-0h1l for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:40 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1f9b52ef481so48999045ad.1 for ; Tue, 25 Jun 2024 14:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349777; x=1719954577; 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=RApDUBWnH8dOo11vUms6fSIyGurtiCmTLAn99yajCOk=; b=DjzE/LijlkPXd2oa6vuSXpi5Yaf+sBAKuiRLWHIk8lZRAEIF8/J0KuGwcrHfjEts0o qXHBPPOtV0Rpfix8GjiQ2F1AbWzNhv1j0w4h4VGr2GIcbc9EPfEjkzR1DNKmuuL2I/Nc M7vihOfS4lY6Ip9NGt6P9LOdtEfpQbGmBIhaGFKIZEMIw+729PtjfDmXEYY8BqDiXXIh 5ZtzMTg48Fi6/tXxcQa1d6gWcZnGMBzUN1Ko7fvKVpr+ptAPeZeg/2Q65GLY0dzrHZkA uhK5Ur/wDebKnh2NukNorazKzbrgeECYkGI4lTMrGbUBWl/1T1hi9QdTSU6MJsiX7Lub LKlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349777; x=1719954577; 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=RApDUBWnH8dOo11vUms6fSIyGurtiCmTLAn99yajCOk=; b=a7zqELew2to5rn7ucKu7ZTGJnXZiGncbTa8Ei0nk5pZQA+5P9PuyuGFHgYdslQW/Br A+d1AOHqJqM+ah4UqBssBuq5ALycaOl3MYlIUaIcaFl3jWpHEia7fuGUmQQ23i8UXZdT lEfkgKCroyNQFPqda8uWd5keo37OQee+gEIZeLud458fcHrnQJMjRFvB5dKz/7Ql8yd1 ApnvGMkPMOJXscDHPUok6hWNPmYXI1EeGqWeWglT+6V6rt2NV9aonKKllhyG5XyK4h6j 70qGrr54H7bGcPON4egEc9PmCVYI5Hmh8ChptFppY7Z5IzM7Mu7vnckLYapLvMFjDmQw f+jQ== X-Forwarded-Encrypted: i=1; AJvYcCUwyGAc1dXCrw5n3V7CNrjTYwGcexhRQAJAY1Fx5z7n+6DeET3AXstq4siioAex3tsHKwzhrqyBDCLVGd8XdkLmIWi+qN4MvgH3pttAg0WJ X-Gm-Message-State: AOJu0YzksJj26xdxtYkBcTJyjNYpCwTSdGywE2sXClP7cM35U2OJWJS/ 9EYVQhIGdpuV40BtUcTz040aT7bBXyRWH7+wzmnrmLYIJD8ppGtQumetZofjBUg= X-Google-Smtp-Source: AGHT+IEeTn6riobaHMOyEcGvzSEs8JEyz1MxFY6peHqzdzbBPKE7rdm0nA1jgpjPUCoTtFM7TxnQ4w== X-Received: by 2002:a17:903:230e:b0:1f9:c3a1:4b65 with SMTP id d9443c01a7336-1fa23f1d44bmr113141665ad.47.1719349777431; Tue, 25 Jun 2024 14:09:37 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:37 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 01/10] dt-bindings: riscv: Add pointer masking ISA extensions Date: Tue, 25 Jun 2024 14:09:12 -0700 Message-ID: <20240625210933.1620802-2-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140939_436922_B0419D2D X-CRM114-Status: UNSURE ( 8.78 ) 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 of these extensions as following the current draft of the specification, which is 1.0.0-rc2. Signed-off-by: Samuel Holland Acked-by: Conor Dooley --- Changes in v2: - Update pointer masking specification version reference .../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 cfed80ad5540..b6aeedc53676 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 654a5c4a7725 ("Update PDF and version number.") of + riscv-j-extension. + + - const: smnpm + description: | + The standard Smnpm extension for next-mode pointer masking as defined + at commit 654a5c4a7725 ("Update PDF and version number.") 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 654a5c4a7725 ("Update PDF and version number.") of + riscv-j-extension. + - const: sstc description: | The standard Sstc supervisor-level extension for time compare as From patchwork Tue Jun 25 21:09:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712049 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 2256DC30653 for ; Tue, 25 Jun 2024 21:09: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: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=srw/SyY0t7xbbUG3TP3wEic1YDcGOTmYqFnqEGC+ng8=; b=PShzNTQWMsFIv+ OBXwdE712LPk0oy6gp2L66cIW+YIp79NtO78jeL8mukx0FmQtP4KThqskJHADQogpG77uiBESfsZE BHPQaWgNh2OzAqphL9u7s9fwzQADLJpCNjp+67xDmtXBUZG1OCxIbdTrOrTM5KBrHnNkG6jmRs5f5 K8Ux2gWWibSSpHiccPzS9hAsmq5jN62AvCA8xk6jjS3m0kbjSXDkXoRaw2XfO/dIjFBDPUAEfNUhw zF5e9ebZ5+orowj+qSgtk6Tjlbm+VMrxHbtPeShy5K1o2l74zhnqb2m1dXAyIvZp7O9FPmgJwdIST 03gu6TTvIBQ6LvPEIpXw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ9-00000004V5e-0PK3; Tue, 25 Jun 2024 21:09:45 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ4-00000004V39-0oUj for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:41 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fa244db0b2so24671855ad.3 for ; Tue, 25 Jun 2024 14:09:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349779; x=1719954579; 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=/UOSPToUyaRaOvApUAfkaCWvJhdcwWzca8vHMvSzGJQ=; b=a3mAVpotbbb1PzlJMT5D31mjy3tU4ADndm2GvyA7EgC3WYBxE2psGqXzd3aLnsBozG 96DirKVA3v4QrH1WQLGb2FAVwssI8I2V8o9Gsa07q2WpEpq3rw8FPxKG2/ge0Nrixbd3 Qiw/LJKUY2dsSCY4AoNkgYtdEehcmZzF0eCLpPa2j13yx2hlxD5HKQD7u0xfCpcovAdM srTfRuCYWcOCoImPCZcvwYAYMedoIzWYP5f+P133icnUdEZAKUQRT61Fbw8gkxn/psRZ wAWggq5UjaJs4qsSrKznVakEPW+y2q7YtSFePNd1tBJwqSYxgeAVVjs87suhoF/716wK JPZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349779; x=1719954579; 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=/UOSPToUyaRaOvApUAfkaCWvJhdcwWzca8vHMvSzGJQ=; b=xNVPipvj6vbvpA+qVunH45mRiYVhmhttVdW7HHGUa/9729Ssjg3t7BCfbns5Iwe4ce kTwsyavhiQuQ3S2TVaRzVvH6dphDt5dSmbxdtac0BiolDbMYe+r0p7oUKSMenNxWlyKR Ko+YmdBR6ok9iEiB20M2pCUBFAyWZSl8vC/nQfTCJscpOYg4aGKKValpnO1lrAgeyfbz tiM1i6uJmT2BocuNgkxmrPgJAdEWSROaLYHIt5RvjzEi2WBuyAlYAPI8P5wh0ir2xcMV q7tj12zzU/vQW2Z7+SxmT5atuqu5U4ehu9ZLl4Fc0V5LnYFr2BO7H01Qqajwddc7IVm3 pBcg== X-Forwarded-Encrypted: i=1; AJvYcCWdPNpsAzmqtUYI+KRCug+YMaaO9aBSsyU1VZxYmV9bYLxg6pxDFnumu6bOfddMEiBmrqSLVddVTTzpJRkhm4qR6WQiwi4IRfw8uIiv+57e X-Gm-Message-State: AOJu0Yz7SEmH+pAgxyDjdwjPNtuAravsaAux235/yPmjuSpVT8hLTMIm CJFXLkiTA/1HIp1d6wrUnDk1lKkh+VdNxaHBa0WcMKVsFDTbTDn/aSp24QHW8Ck= X-Google-Smtp-Source: AGHT+IHAyd2tBEC0veEAkEFMqZZW11Z0zZSakCryxlScbkhfTIHyKIo9oYRvo1zUng9rMVK0IoFeJw== X-Received: by 2002:a17:902:ecc8:b0:1f9:fc92:1b65 with SMTP id d9443c01a7336-1fa158d0cc8mr98348615ad.9.1719349779134; Tue, 25 Jun 2024 14:09:39 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:38 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 02/10] riscv: Add ISA extension parsing for pointer masking Date: Tue, 25 Jun 2024 14:09:13 -0700 Message-ID: <20240625210933.1620802-3-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140940_256529_EC1B6837 X-CRM114-Status: GOOD ( 11.53 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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. Which of these three extensions provide pointer masking support in the kernel (SxPM) and in userspace (SUPM) depends on the kernel's privilege mode, so provide macros to abstract this selection. 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 --- Changes in v2: - Provide macros for the extension affecting the kernel and userspace arch/riscv/include/asm/hwcap.h | 7 +++++++ arch/riscv/kernel/cpufeature.c | 3 +++ 2 files changed, 10 insertions(+) diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h index f64d4e98e67c..5291e08fe026 100644 --- a/arch/riscv/include/asm/hwcap.h +++ b/arch/riscv/include/asm/hwcap.h @@ -86,6 +86,9 @@ #define RISCV_ISA_EXT_ZVE64X 77 #define RISCV_ISA_EXT_ZVE64F 78 #define RISCV_ISA_EXT_ZVE64D 79 +#define RISCV_ISA_EXT_SMMPM 80 +#define RISCV_ISA_EXT_SMNPM 81 +#define RISCV_ISA_EXT_SSNPM 82 #define RISCV_ISA_EXT_XLINUXENVCFG 127 @@ -94,8 +97,12 @@ #ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA +#define RISCV_ISA_EXT_SxPM RISCV_ISA_EXT_SMMPM +#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPM #else #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SSAIA +#define RISCV_ISA_EXT_SxPM RISCV_ISA_EXT_SMNPM +#define RISCV_ISA_EXT_SUPM 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 d3e3a865b874..b22087244856 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -339,9 +339,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 Jun 25 21:09:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712046 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 6AEA3C2BBCA for ; Tue, 25 Jun 2024 21:09:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=ttJ2njPuYs510nkyDKj/HerGZygFyZvG55DySvbDvbg=; b=BoETaQxjnbTAli FnGFO3OYQtt6c8/C+lJnoamPmIQ2mekdfpsc73B6A0gLl1cLmo/ak3gfpTBfkPnOs+TwmZp4VPan1 7aLsbSJCRrhV9XRRkEZs75W4fK28kdftSR4s4hcCdD533JNVWuqnaBE7705mh3mjSHiA3eaYm7yM0 Qz4+G0c7G1P+k8Cm6kncAbxoZeOyUi9dhbwOr3zbLPSqllHCVmvyEVWcoc2rNcR34uHqNkUDDNjVm Nd/u2QR7221GnTfUoZ+oaGZBPGj4Nz8v9AVVmqA/13dpgedVXX5sKBe6Sqn14SxmKEaR/EbDz0DI9 ObSuumDM/d8hPJcCoDbw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQA-00000004V6b-05pk; Tue, 25 Jun 2024 21:09:46 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ6-00000004V3y-2N3R for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:44 +0000 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1f4c7b022f8so51409565ad.1 for ; Tue, 25 Jun 2024 14:09:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349781; x=1719954581; 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=FrGWvMN3Ht8MgkCDub4Kzj37NN41tMPLHr9Zd0plWWo=; b=DcPnnYuvEDyLblWsFHUcuvvUfzobI22f1CIrXVTXPaJH7p+8SvJ+mpZx5jzqpqMKEz DNzfV0VOOP7mCCeczhl9FiR17tzwjTyeGKmhkSn2gNNwiPOTp2B0akCuGN8UDlS2wbSL uNa9W+i8ulUqZeybAY3N+mOhvNs81T61hIRgUiA+Xn9QoUzxYgkRK9HY988qgg9FXeoX 9lodOmxBJUwuWeNlzi8Bpqc9SuVr6IZIPM8N0St0vWvNJ/WTPSVPwpeB7pQaNlGGrfoq vqsTKY9sH8VF+0bG25bchD9TC1/XxZMUk4g1vD9YRdhC/OW3r5nfMmEIanzpEfBpkUfT 1Zzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349781; x=1719954581; 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=FrGWvMN3Ht8MgkCDub4Kzj37NN41tMPLHr9Zd0plWWo=; b=mvMX1uCCZ1Q7SyhIn2kYNu52f0nTBaUSWbLDdADy36m1JP9nvCw+bvXCmS3qagjRHW eIO8Lw+YflN9fAZnJMEQ06NoSZOCjSuYTyPaM0glZLBaBiEoXhsPOS4p6pHCsSAhlOn8 LDH8w3bIY9L7X7RTMxX/GCSv0b4SzUtLQbNwdSa9eZ4wF7boazYyAH8728wMjVyPe5Kn ix0qR2DK2+CWGcZxi2yh3kJgtIPtXyTUVAFN+nM1c0GJMXSHdgqkC3eKZ8YZPdDdKKhM H89RuFUcG1rP1zIf/M5FBIPx2T0Fot7ZbiX/+unPl4+9nKD4O5O5EMrM8n1ZRvdXlfP/ Ub0A== X-Forwarded-Encrypted: i=1; AJvYcCUXrVMNHckaxKyikoQ+kmMk1p64EembffAjSGl/9sb5GquzfsKiSukn7JwUijJqGg+n4SlDBA35pJW/rYvZ3SOCF7eo/AXOdeuANNHSB027 X-Gm-Message-State: AOJu0YyYm9MjZYp/7g145EUxm2tgTOtLklMWSeJfq0CA5FuKSQbXAjwc KSZvVVyjKv4B/bPPUBHDaZcQuV5HUL+SZ30RLd9DjZA8jNbHACiYtBu9t1rhf4eCm8i+/o/v1Vb q X-Google-Smtp-Source: AGHT+IHVaMXVM6zxBwAZCg/cGqYarn4awLSQvEqo6T1mYUpeIGRyF6rap0HbZ7xb98QAIxlepqf95A== X-Received: by 2002:a17:903:41c6:b0:1f7:35e0:5af4 with SMTP id d9443c01a7336-1fa23dce235mr106984915ad.30.1719349781063; Tue, 25 Jun 2024 14:09:41 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:40 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 03/10] riscv: Add CSR definitions for pointer masking Date: Tue, 25 Jun 2024 14:09:14 -0700 Message-ID: <20240625210933.1620802-4-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140942_725059_D0EE22E8 X-CRM114-Status: UNSURE ( 9.84 ) 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 adds the field to mseccfg; Smnpm adds the field to menvcfg; Ssnpm adds the field to senvcfg. If the H extension is implemented, Ssnpm also defines henvcfg.PMM and hstatus.HUPMM. Signed-off-by: Samuel Holland --- Changes in v2: - Use the correct name for the hstatus.HUPMM field 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 25966995da04..5c0c0d574f63 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_HUPMM _AC(0x3000000000000, UL) +#define HSTATUS_HUPMM_PMLEN_0 _AC(0x0000000000000, UL) +#define HSTATUS_HUPMM_PMLEN_7 _AC(0x2000000000000, UL) +#define HSTATUS_HUPMM_PMLEN_16 _AC(0x3000000000000, UL) #define HSTATUS_VSXL _AC(0x300000000, UL) #define HSTATUS_VSXL_SHIFT 32 #endif @@ -195,6 +199,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 @@ -216,6 +224,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 @@ -382,6 +396,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 Jun 25 21:09:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712048 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 865B6C30653 for ; Tue, 25 Jun 2024 21:09:54 +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=Q9k07uZKn0saRaAqE49+peCfRoBLr9hxxr+CrpcBlw4=; b=y1RqmLvHkHrPqv /PAs+0Jf/5basZc0SgeUjAiyybStpVfTMFvQCrGJKz+EdkOVjB9fdxcjTc4eVlQ+CflkQ8aG4qmly UuVuPYABKqPg0lgBEGPGyFLMerTP1/fyXVLP05VYVVV78MtO+Yt3gF/MkVChm2kVBfgkmsa3N5bOs qNFAAFzqq+ubUgzZ5y8I29Jy2tv8De4bu/ltJOauJxjc+yckVnPsHapnTB8kAnK9n/berj9RnM6Tr 0AvTu2Yfz/3CJ99TNqij9eRtV1tGUje1cC1oSqCfyqXQlkLJm/wve5yeBWmqUp9wDyXaLnKJ/YHaS gp7dAXcKvFWfQTO85ckg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQF-00000004VA9-1YTp; Tue, 25 Jun 2024 21:09:51 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQ7-00000004V4q-40jh for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:45 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1f9de13d6baso42306045ad.2 for ; Tue, 25 Jun 2024 14:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349783; x=1719954583; 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=9RHKmejM9a0vevYfMOXCLHoYWj48ZdPOlbThmmBhK0Q=; b=DboAAj7o9855qstQWxAB391zou38al+VxTgVdclL+PB4nbXuWxL4ssx9banvh7ecpW r02fZdcztpK062AtQrwI16EL2SW8ndr57RU1TSqjWdz+MZqg3y+v/DwQNmhLgZOJEDFw 3KHaGHkoSFTecaby25QAAllW97QS45D3c0mPNvQFS+TNtIX0jtbRzFCxd2AUriiE6esF 976pNncXqy8LDHXLQ+DfB/1w3WxPPt6OEi2XXvsVI6o4ttf/Gq/oQBN+XQfWCsd/3HWd CS0S52mVOzRHFmSxKzoD21dBlsGSKIHEZqINTxAWtjRWoOz7ruE1EBQKUIAxMJupdjKC 9LPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349783; x=1719954583; 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=9RHKmejM9a0vevYfMOXCLHoYWj48ZdPOlbThmmBhK0Q=; b=O6KFbZYHIHw9tAp3mK7+QFjMfrSaAFIpAjKjpTd+E+BjPUySIq+UEv/wiAlRn20Pcq 7mhOAL/iYYUqNaMXQAi3cs5WXmi+31vCpgibtZIvYOgqi+IWyTEbN1hnjDOQmFRiEztY gMh03V7JUsLymDJpFk9Il6Y9sAM6U3NACo9UaAjPmRINDyTZVd964MsFzJGpfnE5aq5t +kEDMWLmmBpGYYZjn/88OGXwOxs9ZzoWYL01QuSFHrd09FMLIigYOTe9qwl+aoaGYDsd DlqSwZ0PX3fRSYqHgU7/Il9C9VAS/3SAHLI5XLgBNimEo8cXXKyNrSMxcRzLnr9YFgVo WoeA== X-Forwarded-Encrypted: i=1; AJvYcCXzn77gg8eD1gaR3k57udAEC3CJSEnuA0gGwWtA24bT6kcnA9lIOB/0CVPUy2Zgvko+ZyPHUGEUZgffR+YH3hv+WYAu6Fdd1aKDFDcSmHvb X-Gm-Message-State: AOJu0YzZYiau5+qnYNnYPrPY+XkwtU9LYCYRcSy8Kb+USQm77wsts9yt XRvLoGzibgceLDGrdWP9ii0DMl1CipHjh4yADMHB3wuA67ecr5gflBmgRfxCdYnZn4/sTKOvsGP k X-Google-Smtp-Source: AGHT+IH/DMRd777ZSRsWNFii7RZFNgZupCFLnI/6nswVeiRDaQZIaz+Y8uQB+pMOl2GL7EuQdKoZrw== X-Received: by 2002:a17:902:c943:b0:1fa:2210:4562 with SMTP id d9443c01a7336-1fa23fd8a00mr103679455ad.29.1719349782852; Tue, 25 Jun 2024 14:09:42 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:42 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 04/10] riscv: Add support for userspace pointer masking Date: Tue, 25 Jun 2024 14:09:15 -0700 Message-ID: <20240625210933.1620802-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140944_021217_A0AB44ED X-CRM114-Status: GOOD ( 23.58 ) 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 (which is called "PMLEN" in the specification) 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 lower bound on the number of tag bits and determine the actual number of tag bits. As with arm64's PR_TAGGED_ADDR_ENABLE, the pointer masking configuration is thread-scoped, inherited on clone() and fork() and cleared on execve(). Signed-off-by: Samuel Holland --- Changes in v2: - Rebase on riscv/linux.git for-next - Add and use the envcfg_update_bits() helper function - Inline flush_tagged_addr_state() arch/riscv/Kconfig | 11 ++++ arch/riscv/include/asm/processor.h | 8 +++ arch/riscv/include/asm/switch_to.h | 11 ++++ arch/riscv/kernel/process.c | 99 ++++++++++++++++++++++++++++++ include/uapi/linux/prctl.h | 3 + 5 files changed, 132 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index b94176e25be1..8f9980f81ea5 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -505,6 +505,17 @@ 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 for the pointer masking extensions (Smmpm, Smnpm, + and Ssnpm) when they are detected at boot. + + If this option is disabled, userspace will be unable to use + the prctl(PR_{SET,GET}_TAGGED_ADDR_CTRL) API. + 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 0838922bd1c8..4f99c85d29ae 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -194,6 +194,14 @@ extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val); #define RISCV_SET_ICACHE_FLUSH_CTX(arg1, arg2) riscv_set_icache_flush_ctx(arg1, arg2) extern int riscv_set_icache_flush_ctx(unsigned long ctx, unsigned long per_thread); +#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/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 9685cd85e57c..94e33216b2d9 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -70,6 +70,17 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +static inline void envcfg_update_bits(struct task_struct *task, + unsigned long mask, unsigned long val) +{ + unsigned long envcfg; + + envcfg = (task->thread.envcfg & ~mask) | val; + task->thread.envcfg = envcfg; + if (task == current) + csr_write(CSR_ENVCFG, envcfg); +} + static inline void __switch_to_envcfg(struct task_struct *next) { asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0", diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e4bc61c4e58a..dec5ccc44697 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 @@ -171,6 +172,10 @@ 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 +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) + envcfg_update_bits(current, ENVCFG_PMM, ENVCFG_PMM_PMLEN_0); +#endif } void arch_release_task_struct(struct task_struct *tsk) @@ -233,3 +238,97 @@ 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); + unsigned long pmm; + 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; + } + + if (pmlen == 7) + pmm = ENVCFG_PMM_PMLEN_7; + else if (pmlen == 16) + pmm = ENVCFG_PMM_PMLEN_16; + else + pmm = ENVCFG_PMM_PMLEN_0; + + envcfg_update_bits(task, ENVCFG_PMM, pmm); + + 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_SUPM)) + 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 35791791a879..6e84c827869b 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 Jun 25 21:09:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712050 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 2BFE7C2BBCA for ; Tue, 25 Jun 2024 21:09: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: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=vqlO6jknj/srd8yX1otAM0bQXCz0V2tFVztA0Oujo3E=; b=pSlyH8YY+KX2EH 9rxcU58RvXlpbR9ddzolj0tKkPs8lG6f1Ct/zeY2YbBYZTB5eKpu6xOwWYe+TF8iAuyGpgp1+kw6m 4z5B5zJ0h4HZwgD0FnIAGJfQvSFUiIJlzEqB+WgdNdntlTnIUXWXHtw9xq5r3DGO2ilsMEDB7dqKS eCQLHh8B9PUzFq3KG1myGSlj4VEF/h7SYmb0dJFI8Dr9AZPMBE6RkUOkDhm15qNuVH4OairtFeIoy dmQkZKKqtk89V2aU6fSdx8ZqWotUaPqwKFmkNqEhnXrxZaNvqqaQ+fgVbMikPqdi7+13KY5l7BvzC 0qM8OKF69K492Fr/EvZw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQI-00000004VBs-0HDX; Tue, 25 Jun 2024 21:09:54 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQA-00000004V5k-056G for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:47 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1fa2ea1c443so24278775ad.0 for ; Tue, 25 Jun 2024 14:09:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349785; x=1719954585; 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=Icm6uA8wnqdys37A8Kzf4AV1ey+zBIexi2g5XJMtDfE=; b=BugkdMD37lNKM40LR6LISp5Gmqxp8h052PR1l+w2pjE4M1jPAdx/zaSo19eiR1OxB0 3Q1Cwx1CPKWpUi0bUytjiEjXBDxTI1rKCTlHpcAhDdZHw9ryCc4COJAzYYq4yNGeWkM7 CbIREqybD3EwQKlOyf1oCdSsFPMlz5GpyPIqPdalSq4f+jsM35+2ekMf8M56w3io4EES 9woFolr456RSTPBoDHeVoqfYI5I1nqOHYq0xEotu344dB6CrqDO4/3VzekWqHJ3mgXGM 2t0JMnL5YeMOAeXXV/S1+azsn/Kb5pANB6ws9uI7NKQUIwJyDIyl6Xxf+F9gYy4QPk9W aguA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349785; x=1719954585; 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=Icm6uA8wnqdys37A8Kzf4AV1ey+zBIexi2g5XJMtDfE=; b=nqjhpO8OFmJdkX4LXX3PA9TNrzDaRnoH4ovwXcatpLKgA9TVenLCIPogPht9m9ltgl GePqTmIUPhVYilYkDY2AkRrooSIj1iPYx5yhAyisHoD+zBLwxNu5vTbxY1h+8nmQPCXu HHipEEDAiwblu1l8eWGD/dGZyq32Dnqt0+p7glKHZ80NHuwvYERmCsZ8khOpkovbKapE w35TI2NiWb/3Oz90dMvkG2+q7acLea01fZjekdbd8lDlI3ETtp5j9Rd3WxJKnAE9CHaU a/zX3YMY5SIyXUQzhaft0jFsikEVSwE7opO+WtBj2S8MBSYvltfMPTGGAxffUbbuxgO4 Yu0Q== X-Forwarded-Encrypted: i=1; AJvYcCXFaElLr4zYQu3EktWlDzV0JnaHZW2Bg1X9Ev1kM74cpVYW2t5jOBz442N6sTfjVF5kEAtmBO1QcV9Me/so0zhGqBQYUOY+ubFt708Srizd X-Gm-Message-State: AOJu0YxKfTeA8ZUyzE/wQbROMiDB7qR9iZ4jfawbZVD0TD8xDWNqbDYJ JtQZQbSmzfQuccHDBnouZFD3vqp+Jw56rtf9kF1s0AYL4ugxxqfjfDR4/GOyKX4= X-Google-Smtp-Source: AGHT+IHthtdLPOXBG6B10DrZ39vkgS4mJz07lhPLTxOAb1nTigmUhpXbFgrxYa8MQ6WsjtQ9WbY67w== X-Received: by 2002:a17:903:32ce:b0:1fa:643:f424 with SMTP id d9443c01a7336-1fa238e46ddmr113134855ad.14.1719349784670; Tue, 25 Jun 2024 14:09:44 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:44 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 05/10] riscv: Add support for the tagged address ABI Date: Tue, 25 Jun 2024 14:09:16 -0700 Message-ID: <20240625210933.1620802-6-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140946_148956_3A1A8FBE X-CRM114-Status: GOOD ( 30.10 ) 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 uaccess routines must peform untagging in software because U-mode and S-mode have entirely separate pointer masking configurations. In fact, hardware may not even implement pointer masking for S-mode. Since the number of tag bits is variable, untagged_addr_remote() needs to know what PMLEN to use for the remote mm. Therefore, the pointer masking mode must be the same for all threads sharing an mm. Enforce this with a lock flag in the mm context, as x86 does for LAM. The flag gets reset in init_new_context() during fork(), as the new mm is no longer multithreaded. Unlike x86, untagged_addr() gets pmlen from struct thread_info instead of a percpu variable, as this both avoids context switch overhead and loads the value more efficiently. Signed-off-by: Samuel Holland --- Changes in v2: - Implement untagged_addr_remote() - Restrict PMLEN changes once a process is multithreaded arch/riscv/include/asm/mmu.h | 7 +++ arch/riscv/include/asm/mmu_context.h | 6 +++ arch/riscv/include/asm/thread_info.h | 3 ++ arch/riscv/include/asm/uaccess.h | 58 +++++++++++++++++++++-- arch/riscv/kernel/process.c | 69 +++++++++++++++++++++++++++- 5 files changed, 136 insertions(+), 7 deletions(-) diff --git a/arch/riscv/include/asm/mmu.h b/arch/riscv/include/asm/mmu.h index 947fd60f9051..361a9623f8c8 100644 --- a/arch/riscv/include/asm/mmu.h +++ b/arch/riscv/include/asm/mmu.h @@ -26,8 +26,15 @@ typedef struct { unsigned long exec_fdpic_loadmap; unsigned long interp_fdpic_loadmap; #endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + unsigned long flags; + u8 pmlen; +#endif } mm_context_t; +/* Lock the pointer masking mode because this mm is multithreaded */ +#define MM_CONTEXT_LOCK_PMLEN 0 + #define cntx2asid(cntx) ((cntx) & SATP_ASID_MASK) #define cntx2version(cntx) ((cntx) & ~SATP_ASID_MASK) diff --git a/arch/riscv/include/asm/mmu_context.h b/arch/riscv/include/asm/mmu_context.h index 7030837adc1a..62a9f76cf257 100644 --- a/arch/riscv/include/asm/mmu_context.h +++ b/arch/riscv/include/asm/mmu_context.h @@ -20,6 +20,9 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next, static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next) { +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + next->context.pmlen = 0; +#endif switch_mm(prev, next, NULL); } @@ -29,6 +32,9 @@ static inline int init_new_context(struct task_struct *tsk, { #ifdef CONFIG_MMU atomic_long_set(&mm->context.id, 0); +#endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + clear_bit(MM_CONTEXT_LOCK_PMLEN, &mm->context.flags); #endif return 0; } diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 5d473343634b..cd355f8a550f 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -60,6 +60,9 @@ struct thread_info { void *scs_base; void *scs_sp; #endif +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + u8 pmlen; +#endif }; #ifdef CONFIG_SHADOW_CALL_STACK diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uaccess.h index 72ec1d9bd3f3..153495997bc1 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -9,8 +9,56 @@ #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_SUPM)) { + u8 pmlen = current->thread_info.pmlen; + + /* Virtual addresses are sign-extended; physical addresses are zero-extended. */ + if (IS_ENABLED(CONFIG_MMU)) + return (long)(addr << pmlen) >> pmlen; + else + return (addr << pmlen) >> pmlen; + } + + return addr; +} + +#define untagged_addr(addr) ({ \ + unsigned long __addr = (__force unsigned long)(addr); \ + (__force __typeof__(addr))__untagged_addr(__addr); \ +}) + +static inline unsigned long __untagged_addr_remote(struct mm_struct *mm, unsigned long addr) +{ + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) { + u8 pmlen = mm->context.pmlen; + + /* Virtual addresses are sign-extended; physical addresses are zero-extended. */ + if (IS_ENABLED(CONFIG_MMU)) + return (long)(addr << pmlen) >> pmlen; + else + return (addr << pmlen) >> pmlen; + } + + return addr; +} + +#define untagged_addr_remote(mm, addr) ({ \ + unsigned long __addr = (__force unsigned long)(addr); \ + mmap_assert_locked(mm); \ + (__force __typeof__(addr))__untagged_addr_remote(mm, __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 +178,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 +294,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 +341,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 +362,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 dec5ccc44697..7bd445dade92 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -173,8 +173,10 @@ void flush_thread(void) clear_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE); #endif #ifdef CONFIG_RISCV_ISA_POINTER_MASKING - if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SUPM)) { envcfg_update_bits(current, ENVCFG_PMM, ENVCFG_PMM_PMLEN_0); + current->thread_info.pmlen = 0; + } #endif } @@ -204,6 +206,12 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) unsigned long tls = args->tls; struct pt_regs *childregs = task_pt_regs(p); +#ifdef CONFIG_RISCV_ISA_POINTER_MASKING + /* Ensure all threads in this mm have the same pointer masking mode. */ + if (p->mm && (clone_flags & CLONE_VM)) + set_bit(MM_CONTEXT_LOCK_PMLEN, &p->mm->context.flags); +#endif + memset(&p->thread.s, 0, sizeof(p->thread.s)); /* p->thread holds context to be restored by __switch_to() */ @@ -243,10 +251,16 @@ 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); + struct mm_struct *mm = task->mm; unsigned long pmm; u8 pmlen; @@ -277,6 +291,14 @@ 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; + if (pmlen == 7) pmm = ENVCFG_PMM_PMLEN_7; else if (pmlen == 16) @@ -284,7 +306,22 @@ long set_tagged_addr_ctrl(struct task_struct *task, unsigned long arg) else pmm = ENVCFG_PMM_PMLEN_0; + if (!(arg & PR_TAGGED_ADDR_ENABLE)) + pmlen = 0; + + if (mmap_write_lock_killable(mm)) + return -EINTR; + + if (test_bit(MM_CONTEXT_LOCK_PMLEN, &mm->context.flags) && mm->context.pmlen != pmlen) { + mmap_write_unlock(mm); + return -EBUSY; + } + envcfg_update_bits(task, ENVCFG_PMM, pmm); + task->mm->context.pmlen = pmlen; + task->thread_info.pmlen = pmlen; + + mmap_write_unlock(mm); return 0; } @@ -297,6 +334,13 @@ long get_tagged_addr_ctrl(struct task_struct *task) if (is_compat_thread(ti)) return -EINVAL; + if (task->thread_info.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); @@ -315,6 +359,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_SUPM)) @@ -328,6 +390,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 Jun 25 21:09:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712051 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 6535EC30659 for ; Tue, 25 Jun 2024 21:09: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: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=Cu9y+BrjxJhHJq8Y1qXY8RWIHrqKzhD536OpyowVgmE=; b=OcLuo8D1bwh7w9 za6QRmwIFWzPhxYIkacUuRMZBeLgF1e146O1Bax4M+3RZhmJCRjeDc2VoK2WEEsYxarj0tqlMMYGP oSW4u6LKP/kheVxb4RvAeI3SDDP+mhBWYVjbVrjHHX0+q2RGvVBuhGcdJgl4bwkXvglLOTHibcpLr hSZ3X/drIfgQxwSpYppRXkQUTw+w+3cvfsKXwwK6BycNZPeS2v3w2HAyWjqqs9VBdkhQpw9oJ8/ex 5Uh15kvTqRv2QGjMKQnJwAnKMi/9tBoJbZB2+HvhfhCI2N4Bf2ycIkMLt+uHX1ZxMK3f8guLedSux hTPmZxUiygLWF1kL5/uw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQJ-00000004VD5-24ZI; Tue, 25 Jun 2024 21:09:55 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQB-00000004V79-2Clo for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:49 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1f9e2affc8cso36398835ad.2 for ; Tue, 25 Jun 2024 14:09:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349786; x=1719954586; 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=TcVNJ1c0k9nGflpMj97RQ79v8JL+NQUn2VhiWagHpuA=; b=dNQUXlyPFZS5jAAVQn/uem1D12BNqcuWKDAPSxKC4oL7hFppGbO90ybb7fIIfPnpHN 6Lchoh1z7dokrUw8CekxZGRybM6wXpPeYEDAnpBpaYxXoScbB9o2mxoYWEU0RoCmpD8Y D6uk5vUUAyaxUBlcC89EYpa3RNTEqAbMJYTTSgK27RGn/f6HrfrVNWOszja2DoBga3RW QUVr/3dtgHB7QpscvTWB7y0LlZAkBcOUEX0MyUG/Ito3kYx53JpTsFN/2SKJd5wuZyax Ph95fBqqupYTO7mWbTi41gwUs9rgyOGVIQH+iqUpJvDGQQUGsddOTP2UFy+ibg9zXeGt 77JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349786; x=1719954586; 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=TcVNJ1c0k9nGflpMj97RQ79v8JL+NQUn2VhiWagHpuA=; b=rfGLvbNXu8k9N0J4JhpE8XHKmZkJjJ5/A9v5qTeKNFlfKczAHTjnQhuchez1zXCsaP pzZxppo6xUgSnXLXfrCIqBTR1XfDIZVkFyfyvGh0TeSi54GDj0gALytvsptFn7nYwTWS Kb5zSX9cXqjQ9K7iGRaAIeaPYE5ds2IZS+TgzpPpvcIlHcfDdaPBdamLkuIiHwm/5oh/ Z0xiiYbgvBG4bx7Y/wVXyTCa2RVA9Yb8bgo/LW7uHZ223/ju11vs7WUzGhcI/3gPmcHC rRm+goj4rK4SOmZQgdaER2caPdaQayCr+E8Toyoh3hY0J6vCS0/VY8xEKQUO7FtCQeu/ BSYw== X-Forwarded-Encrypted: i=1; AJvYcCUX7Y0YZ2qbeK2inLqpW6HRKM13vQQfZ9rpjmrDIJAZ0VQv3h47VBvJcVORF+LNjcL5Qk/ir6O461WV0JWfVbj7Gv4vQyYAh8wV5iiNYgfE X-Gm-Message-State: AOJu0Yw5iaNTVpjQhUpv5c3ywC342jI5NDHttEEV2oy/lZrtuh6kRKrn jKbE124JhSJW7NMAufT4D692sEL6JBTTVx7QJxwLWBSRZEwXoj7qRNuygG9qoZg= X-Google-Smtp-Source: AGHT+IEkxpAQRQEVDc6TW5GfsE+EzweemTWIRBHDNXOU7XbbycmCakpPne7BxVO3PsXnE1O0ndqnMA== X-Received: by 2002:a17:903:2291:b0:1f8:5a64:b466 with SMTP id d9443c01a7336-1fa23eceaedmr107096065ad.21.1719349786391; Tue, 25 Jun 2024 14:09:46 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:46 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 06/10] riscv: Allow ptrace control of the tagged address ABI Date: Tue, 25 Jun 2024 14:09:17 -0700 Message-ID: <20240625210933.1620802-7-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140947_660926_11315391 X-CRM114-Status: GOOD ( 13.41 ) 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 --- (no changes since v1) 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 92731ff8c79a..f8ceecc562fe 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 b54b313bcf07..9a32532d7264 100644 --- a/include/uapi/linux/elf.h +++ b/include/uapi/linux/elf.h @@ -448,6 +448,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 Jun 25 21:09:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712052 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 BE223C3065A for ; Tue, 25 Jun 2024 21:10:01 +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=GUBybJNYh8/w/yvguwKaLi/DVgmyzQwLxrsNT4BaRKQ=; b=5FEkQfg3ZeXL0e wo9Pw7b2duCYVy39vSskn9OjgLQFIzI6vfb+8fF3s0hQeEfZXRSMtjuGa3/7YG9SPcN60kmHiTe/l 8Kyj3hEAgAYTQz+XbtBQE01Pbg95QHyT2vfS3cREEjPz/cxeljcC2cam1vAtHKAE85Uy0dIh5FBqs NvSOKGyENbubZhkyjg3xuAblDT+wgpqgBWWEo0/HzlEsdK/enN8+u3keuGBV9J4J7R3QUPlX6btMe W16swAiVUO3zFBAwx6LR0taivybYXa+5TAWnWJ9ChOmA/NUb3EqZ40s23WWnkAjlrL/e4sRQbE02r vNM4sEeDHj5I9w8PJGRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQK-00000004VE5-3Ksy; Tue, 25 Jun 2024 21:09:56 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQC-00000004V7n-2f9t for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:50 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1f47f07aceaso47068465ad.0 for ; Tue, 25 Jun 2024 14:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349788; x=1719954588; 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=TPuofY2kiE0LkR878/Zu2IFTHH09jSBTsb0Rtm6Nb1g=; b=hya6uhPTlI8fEqf1frKYIkrqGNq8yUGELSjCuVbeUfV2h78/d7kDso0foKTObLtP0j VsC/Pcoc0eIz5TQJ2LaJopOA3VqBhG1PEQjoAVynVMIFfN9S/HzAAZLXgHRV9a0EB/Eh YFZe/zSr0KPyHVWxwA6Sa5pJ+67YTkbRCiikK6U1Bm3xi4DI/cDZn2eJhbxDXr6cp8rp vQPMN03SLSGBkFilEkb+Csib7qq76l004uvZy7bzSoNrHu6UmxjsIdS3bvsShCbT0mvS +x7zIIVmnIqAPhctiLSHfwz8h/Bod0orTFKcLh575TZXMmP/BObpoAzfcl0G+6C1g53d eWDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349788; x=1719954588; 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=TPuofY2kiE0LkR878/Zu2IFTHH09jSBTsb0Rtm6Nb1g=; b=vsUsNzWIuRv9zSwYQVJC3nNdmN32VeVhUgTRIPDHhl/aIe+toFFAu2uOZRCzGn+KKr VZaK/b3mJN61DXrrjubvzCCn93o3GEL7QfB7rAZSQvlM22OLTG4/ZYIdQCqXQbSDDc2b f/qGVAcS8Ng+j4Yi2ezMJUV2czuIPX+JVwPByFq8Z+WVMv2oXhlgkNDCKLzuBkqvn/kp 7pszuqIo9nVLGk3BV+rn8o72Xg9uDU/f0NR7gJHSWOFNKYWOync7aKKibqy9IOaMXg90 S8bTHxY/A/RhlamSoRFLFmdAXz2p5j3b/tbnPX294cXQrieaACLxlDL6Uv3EWCimKRRY eXJQ== X-Forwarded-Encrypted: i=1; AJvYcCXquvENBMfJQm5zvfAaz8+T95h/R1ZqdVXoXSRsq/yZlGtmZV+zaqpwzivCaINTAlqLhZURW0RxG2VU/WnqCA3fL07vdwj4qyP0umL4f+0Y X-Gm-Message-State: AOJu0YzVi55cAxAMvOS6w+GM66GDmJK3NhwU5KFGbZOf1KtxO0HRLBOD b1vY0KwzN6KMPorvkoBKwxOML3R9d29OEoWxhAo65e+yM31xpgv2pugChGA+e8Q= X-Google-Smtp-Source: AGHT+IHBzWveHbkEb4O4bsOdc9hCbynxG7YU0zM8VzUNtP3uAB5HdQx6amHwmHSg9LkKRKIz3BejeA== X-Received: by 2002:a17:902:c950:b0:1f9:e7b4:5df6 with SMTP id d9443c01a7336-1fa158d0d2emr100281835ad.3.1719349788079; Tue, 25 Jun 2024 14:09:48 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:47 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 07/10] selftests: riscv: Add a pointer masking test Date: Tue, 25 Jun 2024 14:09:18 -0700 Message-ID: <20240625210933.1620802-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140948_727078_EFD5DF22 X-CRM114-Status: GOOD ( 24.11 ) 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 --- Changes in v2: - Rename "tags" directory to "pm" to avoid .gitignore rules - Add .gitignore file to ignore the compiled selftest binary - Write to a pipe to force dereferencing the user pointer - Handle SIGSEGV in the child process to reduce dmesg noise tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/pm/.gitignore | 1 + tools/testing/selftests/riscv/pm/Makefile | 10 + .../selftests/riscv/pm/pointer_masking.c | 330 ++++++++++++++++++ 4 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/pm/.gitignore create mode 100644 tools/testing/selftests/riscv/pm/Makefile create mode 100644 tools/testing/selftests/riscv/pm/pointer_masking.c diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile index 7ce03d832b64..2ee1d1548c5f 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 sigreturn +RISCV_SUBTARGETS ?= hwprobe mm pm sigreturn vector else RISCV_SUBTARGETS := endif diff --git a/tools/testing/selftests/riscv/pm/.gitignore b/tools/testing/selftests/riscv/pm/.gitignore new file mode 100644 index 000000000000..b38358f91c4d --- /dev/null +++ b/tools/testing/selftests/riscv/pm/.gitignore @@ -0,0 +1 @@ +pointer_masking diff --git a/tools/testing/selftests/riscv/pm/Makefile b/tools/testing/selftests/riscv/pm/Makefile new file mode 100644 index 000000000000..ed82ff9c664e --- /dev/null +++ b/tools/testing/selftests/riscv/pm/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/pm/pointer_masking.c b/tools/testing/selftests/riscv/pm/pointer_masking.c new file mode 100644 index 000000000000..0fe80f963ace --- /dev/null +++ b/tools/testing/selftests/riscv/pm/pointer_masking.c @@ -0,0 +1,330 @@ +// 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 int pipefd[2]; + +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) + goto pr_set_error; + + 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) + goto pr_get_error; + + 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; + + continue; + +pr_set_error: + ksft_test_result_skip("PMLEN=%d PR_GET_TAGGED_ADDR_CTRL\n", request); +pr_get_error: + ksft_test_result_skip("PMLEN=%d constraint\n", request); + ksft_test_result_skip("PMLEN=%d validity\n", request); + } + + 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 execve_child_sigsegv_handler(int sig) +{ + exit(42); +} + +static int execve_child(void) +{ + static volatile int i; + volatile int *p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - 7); + + signal(SIGSEGV, execve_child_sigsegv_handler); + + /* This dereference should raise SIGSEGV. */ + return *p; +} + +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) == 42, + "dereference after fork\n"); + } else { + static volatile int i = 42; + volatile int *p = (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - min_pmlen); + + /* This dereference should succeed. */ + exit(*p); + } + + if (fork()) { + wait(&status); + ksft_test_result(WIFEXITED(status) && WEXITSTATUS(status) == 42, + "dereference after fork+exec\n"); + } else { + /* Will call execve_child(). */ + execve("/proc/self/exe", (char *const []) { "", NULL }, 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(pipefd[1], 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(pipefd[1], 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(pipefd[1], 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; + int ret; + + /* Check if this is the child process after execve(). */ + if (!argv[0][0]) + return execve_child(); + + dev_zero = open("/dev/zero", O_RDWR); + if (dev_zero < 0) + return 1; + + /* Write to a pipe so the kernel must dereference the buffer pointer. */ + ret = pipe(pipefd); + if (ret) + 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(); +} From patchwork Tue Jun 25 21:09:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712053 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 AEFC2C30653 for ; Tue, 25 Jun 2024 21:10:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type: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=72rArpNILmPkUEeerv8i3A8Nve4tdmH4nDRFa6e8pbM=; b=KeQU5uiAbsZN2y eV9Ls69PDyZLVIiky74b1fhGHJwwdL/fSEBR70/4L5ERIXbDqFK386Pr8fdmnM78kDrLg8m4V4ctJ boUhImMuloKLWVcZ46O+8okiGo0Zd9bYlb7XcWBGWlt4z1c4x8lma8CtpuAMmlBE1sLtpS4CXDwRs +9NQxY+YRd384FSTHS/tCpWLM8EFNS3YqezT7Q/3e1NfLHkS38OYk/0CUo3HKRROhf/TDA95u+7of 1O1YQR4bdoLKm58v8k73wbIOuL3eyHKaxT6O28Ex7oTokW5GRrC+aA8wFAvLGwPtJMA8uXNHK30XP p9x17NiZoUqO/td5ewHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQS-00000004VJF-0FMH; Tue, 25 Jun 2024 21:10:04 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQE-00000004V8i-26zE for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:53 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1fa07e4f44eso31658855ad.2 for ; Tue, 25 Jun 2024 14:09:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349790; x=1719954590; 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=XfPM04Pyb/lGY9Q7saAL52HDlLyxzOizf7aq1c4K5eE=; b=c+mA4me4o5o0cjBcEem5AssuZcD13UgspKIIS4ZEZl3vHPZIz+wLD0xc1HdOSX6Hrv O1sWA31vSycUREZHNaB8d82UMnKKOi+eeJ4IlxbKQgkiyx+nQJxuHnRkI8qIkI9eZHZ+ gZSnnkn+Y84lFysM1FS68sctI0aruBQ4khw7/XDmtOLPXUwnTrKpX36E5gLjb7QmIY2r fgXPck1sOxUNlTnbF7PmdY6vws94tCjKG4bUR9hf2EgeyV0PqaG825CTc4Uc8iX79yYe olBJKxATodGF9URFR68cZgZr9xiyumr/SA1z5zB+gHcgM+E3JZCKprGeQ7pOLniwLkt6 f9Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349790; x=1719954590; 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=XfPM04Pyb/lGY9Q7saAL52HDlLyxzOizf7aq1c4K5eE=; b=VkQ3cT4v4Xotj0GSx3PYmYFS+dYeRzg4VmfhzLU10eaaDoMo/sXrwnqDJvyXAejHCp v5s9RSRw+Aqr10XDpPM50kOMcwBsm2rUYJAjn/BxBcxGTZBxLkwf1uuaBUNKItJ3KQF+ zUOx3E6vEMvci4ep0MZu0DDatNBH77OTNgzYwIAdU/oFIDOnsWroMreOVVvcfQcaeuIv fqbbu5P9pAsqsMHm8eVTuNo3uPERXrHpuLY4CH86EQLLLiGSsf8oXNqycXQFbrdZZvt5 +PDsBsPqPn14iHuwgDUjhcLfMUI/SdRC1ED2cFdFfqCJjHr7OUR8BIOVWLcDDPMBgr3Y Q8qw== X-Forwarded-Encrypted: i=1; AJvYcCUyxKnjinjC4NNaHoa+/ythqtEy5WcKvQi0AzXinFEZXoBmIDdmeOQb/u+O333ehKdwbUGt4LphsRY6XsCt3/r9zNzszlsiuv0iXIChoF7x X-Gm-Message-State: AOJu0Yw6hCTMNaihOrjJlX1sy5PCRIqEOJdRruWBG5qyr9tatONDuv5e JAKjyXeFK+9+NFyfP7IAAGQVdIIMx6UvnfYT8xJvxGFkrdB8d4N/Ln0ADU3pGA4= X-Google-Smtp-Source: AGHT+IGeWKqd97hO0c2XIDZhD2hlaWhB/xhaoVBtqBOKtiSu9iE5NhFqVkg/azBhVzCaS3o6lABNTw== X-Received: by 2002:a17:902:e5d2:b0:1f9:c8cc:9df4 with SMTP id d9443c01a7336-1fa23ef7f7emr98497255ad.45.1719349789879; Tue, 25 Jun 2024 14:09:49 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:49 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 08/10] riscv: hwprobe: Export the Supm ISA extension Date: Tue, 25 Jun 2024 14:09:19 -0700 Message-ID: <20240625210933.1620802-9-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140950_656992_023CAC59 X-CRM114-Status: GOOD ( 13.07 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Supm is a virtual ISA extension defined in the RISC-V Pointer Masking specification, which indicates that pointer masking is available in U-mode. It can be provided by either Smnpm or Ssnpm, depending on which mode the kernel runs in. Userspace should not care about this distinction, so export Supm instead of either underlying extension. Hide the extension if the kernel was compiled without support for pointer masking. Signed-off-by: Samuel Holland --- Changes in v2: - New patch for v2 Documentation/arch/riscv/hwprobe.rst | 3 +++ arch/riscv/include/uapi/asm/hwprobe.h | 1 + arch/riscv/kernel/sys_hwprobe.c | 3 +++ 3 files changed, 7 insertions(+) diff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst index fc015b452ebf..75fbefa0af26 100644 --- a/Documentation/arch/riscv/hwprobe.rst +++ b/Documentation/arch/riscv/hwprobe.rst @@ -207,6 +207,9 @@ The following keys are defined: * :c:macro:`RISCV_HWPROBE_EXT_ZVE64D`: The Vector sub-extension Zve64d is supported, as defined by version 1.0 of the RISC-V Vector extension manual. + * :c:macro:`RISCV_HWPROBE_EXT_SUPM`: The Supm extension is supported as + defined in version 1.0.0-rc2 of the RISC-V Pointer Masking 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/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 7b95fadbea2a..abb7725fd71b 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -65,6 +65,7 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_EXT_ZVE64X (1ULL << 39) #define RISCV_HWPROBE_EXT_ZVE64F (1ULL << 40) #define RISCV_HWPROBE_EXT_ZVE64D (1ULL << 41) +#define RISCV_HWPROBE_EXT_SUPM (1ULL << 42) #define RISCV_HWPROBE_KEY_CPUPERF_0 5 #define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0) #define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0) diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index 83fcc939df67..b4f4b6d93c00 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -142,6 +142,9 @@ static void hwprobe_isa_ext0(struct riscv_hwprobe *pair, EXT_KEY(ZFHMIN); EXT_KEY(ZFA); } + + if (IS_ENABLED(CONFIG_RISCV_ISA_POINTER_MASKING)) + EXT_KEY(SUPM); #undef EXT_KEY } From patchwork Tue Jun 25 21:09:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712055 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 41235C2BBCA for ; Tue, 25 Jun 2024 21:10:12 +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=gaqQoWdKLsrEoLDpyNJ8HmBzEBChs2FZ7DejVoVyCQ8=; b=lwvL7WtDCX7sSD hpwqWeHawne6d9HCDcvfvkQqC2aqPpMI+HzWQcQjUZLoWp/AMxcWR87QHLaOIiBkQ0jM7pzol72eS ptfqRyMv2WVgB734mVqva3MNZLpy+xhZM8rkmWc7WszIx77ykxe/yh/teUsD9GnShRQ/1y1gJfMFM aWOICGYv/rWMDtj1qXyk/OnWY9BqNEsZj9G5vzOX5fDpXbwapuRA3Cl9ophDmAJ6gheG4CBmBaiZG uGIUI23cHxrse+kotV0IdsBf5hLaTsmoWelz4SSzwkojlty/55IbQFjmr5ysbPTU38qMxOT31/v27 8fdUj6nkGyvJnz7xtUGw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQU-00000004VLD-2CVi; Tue, 25 Jun 2024 21:10:06 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQG-00000004VAk-0d6W for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:54 +0000 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1f9b364faddso49242675ad.3 for ; Tue, 25 Jun 2024 14:09:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349791; x=1719954591; 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=WmS6vWk00EfafKWbKkl85gc2oDZFAwwt7tuzVTWTGwo=; b=nX+ohpKFPLENKSirvNKqu+zoh/UvCgiql1fP4shoKiIO2pr/YmjApwx/2S9uJ/x7m+ LSkym/lS998RWrxq4XwBKkpQPZukza4121MmhsBOk7rMmIGrlk4EaYNBx/Tw6F0IH979 dBoi+69YNaKG2/E6BI9ubsKM5DInNTR4xYIhvteNKT/SoAvHPiZF7xgc8uMooddTSzWh hZzCpo+WS7Yie7qQdvLRVfGfnncnOuX2sHBP1hbXGNbOYAjwJg3AWTL5Tv2T5daWUguu vSnZJiCOEOAGTMS2WqUQM32yQovqKT9V5yvg0JgSVcNNuKLcKA9vaGWGtSxbIxXGSzD4 numw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349791; x=1719954591; 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=WmS6vWk00EfafKWbKkl85gc2oDZFAwwt7tuzVTWTGwo=; b=VBjtVwQJHg/i1v27NKSEjsQ9pT4gSra93P2dquz6eUiOOLvQbZAoBy8cIi/pxcd+hS 8YGA4qrgLRuZp/rt4L1WdiguMPxqTbpJtBDX7yyWmI2C/nAjdco4pdOrzorGN94Csr9v qNTUcs5oPb0m6JLrtyFGN03gBgnQK9Zf0ZnbfTkhfvuusXp7iYA9VaeEK54agHv/Ysik yZHqtIeUvyx+xVwNDY5IHVZidrt9ZUrELCEee7mvGnIXVGYLbtuJ0vI4lqv/MycaVaGI 036/6lkbO7ZLyINyk1iOnXaiuURXlfyIt53qnZyJcqSu/+63lFi85iiP5upSnP2NIdaV Z/UA== X-Forwarded-Encrypted: i=1; AJvYcCUpJg3SX5c7n5tQS0EpvPhwjT/PnzSKuiorGOB2icblTeAwedI3aEP4xeO25WJF4m8s51nXk1gPxGG3ucntXEW8WFwx5xRV1+nObThFa1xG X-Gm-Message-State: AOJu0Yz7P6eDm4vaOCDhqqiK3esV3SZQttBVREVxxApm29Js7Ls5xWUS TCG8C48Ws0trjxA9w3YO0phYZbQY66eZb8tg8nR2pGYbaq9HayLNdDpkCeAn1ZI= X-Google-Smtp-Source: AGHT+IHTw5qN2SpgPrcqrPEXyzhXThDZldXGwPxQ/Q1c/gHxKEx75NLlZ00CvfUvOupr2aJXM+dfMw== X-Received: by 2002:a17:903:32ce:b0:1f9:e95b:5810 with SMTP id d9443c01a7336-1fa24082313mr90605365ad.53.1719349791487; Tue, 25 Jun 2024 14:09:51 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:51 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 09/10] RISC-V: KVM: Allow Smnpm and Ssnpm extensions for guests Date: Tue, 25 Jun 2024 14:09:20 -0700 Message-ID: <20240625210933.1620802-10-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140952_755955_DE1EF0ED X-CRM114-Status: GOOD ( 11.10 ) 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 interface for controlling pointer masking in VS-mode is henvcfg.PMM, which is part of the Ssnpm extension, even though pointer masking in HS-mode is provided by the Smnpm extension. As a result, emulating Smnpm in the guest requires (only) Ssnpm on the host. Since the guest configures Smnpm through the SBI Firmware Features interface, the extension can be disabled by failing the SBI call. Ssnpm cannot be disabled without intercepting writes to the senvcfg CSR. Signed-off-by: Samuel Holland --- Changes in v2: - New patch for v2 arch/riscv/include/uapi/asm/kvm.h | 2 ++ arch/riscv/kvm/vcpu_onereg.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index e878e7cc3978..eda2a54c93e3 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -168,6 +168,8 @@ enum KVM_RISCV_ISA_EXT_ID { KVM_RISCV_ISA_EXT_ZTSO, KVM_RISCV_ISA_EXT_ZACAS, KVM_RISCV_ISA_EXT_SSCOFPMF, + KVM_RISCV_ISA_EXT_SMNPM, + KVM_RISCV_ISA_EXT_SSNPM, KVM_RISCV_ISA_EXT_MAX, }; diff --git a/arch/riscv/kvm/vcpu_onereg.c b/arch/riscv/kvm/vcpu_onereg.c index c676275ea0a0..71c6541d7070 100644 --- a/arch/riscv/kvm/vcpu_onereg.c +++ b/arch/riscv/kvm/vcpu_onereg.c @@ -34,9 +34,11 @@ static const unsigned long kvm_isa_ext_arr[] = { [KVM_RISCV_ISA_EXT_M] = RISCV_ISA_EXT_m, [KVM_RISCV_ISA_EXT_V] = RISCV_ISA_EXT_v, /* Multi letter extensions (alphabetically sorted) */ + [KVM_RISCV_ISA_EXT_SMNPM] = RISCV_ISA_EXT_SSNPM, KVM_ISA_EXT_ARR(SMSTATEEN), KVM_ISA_EXT_ARR(SSAIA), KVM_ISA_EXT_ARR(SSCOFPMF), + KVM_ISA_EXT_ARR(SSNPM), KVM_ISA_EXT_ARR(SSTC), KVM_ISA_EXT_ARR(SVINVAL), KVM_ISA_EXT_ARR(SVNAPOT), @@ -122,6 +124,7 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsigned long ext) case KVM_RISCV_ISA_EXT_M: /* There is not architectural config bit to disable sscofpmf completely */ case KVM_RISCV_ISA_EXT_SSCOFPMF: + case KVM_RISCV_ISA_EXT_SSNPM: case KVM_RISCV_ISA_EXT_SSTC: case KVM_RISCV_ISA_EXT_SVINVAL: case KVM_RISCV_ISA_EXT_SVNAPOT: From patchwork Tue Jun 25 21:09:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13712056 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 88122C2BBCA for ; Tue, 25 Jun 2024 21:10:15 +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=7ql3fxAm0hZlscxB2cxGeE8yfAu3LdbtbMOw1PgmBDw=; b=3de7fMzt+qRMBD Jc0AYUrg1kxdHe3wkczkOFf3f6j6eCZ+DbuSa6Q/Z6Z/XrTUfxwHZwPiAa23j4s/oK40AMKWRJkRi lZoD5RM5GzC5Qt5xnN/aIDRxf1iZamtvxFxBDHR/cCIB+D/qM48LmEbSrV7bL3iBBN6PwjCiMFg9P mJz8mJjn2VacL66CmCB54MeFG1cDkMbDlRhi5cKr3lX0TVpUglPdQ7gZl/vnMkzKg5ZtCQLuZyze6 a+nRPiemV6uGPy+Rgv8E7nPWl6PDm379ieKALSb1T5y9oAjjpcuYiCGSvLnIWmV2oduSCZ4/Xdx4j QTwxZDCpbolK4MIaB5/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQX-00000004VNH-0VMe; Tue, 25 Jun 2024 21:10:09 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMDQH-00000004VBi-3ivE for linux-riscv@lists.infradead.org; Tue, 25 Jun 2024 21:09:55 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1f9a78c6c5dso47321035ad.1 for ; Tue, 25 Jun 2024 14:09:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349793; x=1719954593; 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=ZsHcmVVCPLrcBxzvj/XWrk8rIm5ozAhc40jGfT9csjA=; b=EBJ0glFYOI+C/p4n9Vu7gOMw3iQl4dNRSaDoArdG08flaaA9VZ0fP28QKEk+Il2mKH qfvldUIFxyRsEmOyWhO8ScvAEuiFhgugiRqTPiampBa7eLfr6YgcC3hCvrJcGnMO8or1 sMlAjO89J89iFK57GkIlb/oSJdv9JiIdEfs3eKk08UYEocXGC00aJGJMefPyOZVZH1ZK FRL/rQluaUxC/qluqWh0MOCjs4S6MMV2V++MR9PJEzf1QVjD7BKnHaBOlshFKk6lIdVm IBJLpA+vLReaB9oCMYyz0PXdDgwLDSYVmp5xiKnBnlNGVP+maLsa/WjMKuS20lDEaDf9 ROog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349793; x=1719954593; 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=ZsHcmVVCPLrcBxzvj/XWrk8rIm5ozAhc40jGfT9csjA=; b=kH7oV9+p/iGie/r+ExJBuauHYk1uGSJblE4+pXRvJBhSNzQ6pO9nXVXsDGaYEA38f9 EB/2xdve2DSgr8VLk1ITJxUICOmR548EVP0oj+uRPRV6LlzwDeYq6kHxkS9SRBXij279 9KnNY7Ak0EA1wrNyY1r/pcq2RN5cBPp2HXeubfbUduHYMc+MNwYVnzj0fnWPleQ87Rqa 6oQpR9YHutmZEmrbQCDcq/3KZuw6dod+RfzSuciIsjHEdS3xz5Gv5MkxjnNfJ5wprslT jH7B+5S7xewNO9fd4Ipqv77ee+B5c1KujdaVFuj4SLaKrlvwgS9bexUavm+qflN69Lik 4+og== X-Forwarded-Encrypted: i=1; AJvYcCULBZFKQpJggJRi/VIbulihhNpRaO0EPI7pjb6mN31LF0L1UrWzJRlcICP3PTW6lnnycbm5btMK5r/pJRO/J2Hskb32gLpjlQHXS6bP5488 X-Gm-Message-State: AOJu0YzTvH+8eXnHTSnJTRA+UvpKrhHIlgciYCpFRYiThPNJD+PeOBBw SlCVWaLPrOTBbit00rGQP/afkA4cPExd4G+CxfqEwLylweTTQScZonDuZ0Z4ECo= X-Google-Smtp-Source: AGHT+IF7960Z3y3y/X3zynf0lJ1wO8G/3jJV2h9jR5m8BoGcdIwQfMMMznl9rO+PXdYb3q90GGzu1Q== X-Received: by 2002:a17:902:e5c1:b0:1f9:c6df:a84e with SMTP id d9443c01a7336-1fa23f3638fmr104356595ad.64.1719349793095; Tue, 25 Jun 2024 14:09:53 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:52 -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, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 10/10] KVM: riscv: selftests: Add Smnpm and Ssnpm to get-reg-list test Date: Tue, 25 Jun 2024 14:09:21 -0700 Message-ID: <20240625210933.1620802-11-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-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-20240625_140954_086247_E392F695 X-CRM114-Status: UNSURE ( 9.54 ) 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 Add testing for the pointer masking extensions exposed to KVM guests. Signed-off-by: Samuel Holland --- Changes in v2: - New patch for v2 tools/testing/selftests/kvm/riscv/get-reg-list.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/selftests/kvm/riscv/get-reg-list.c b/tools/testing/selftests/kvm/riscv/get-reg-list.c index 222198dd6d04..301761a5364d 100644 --- a/tools/testing/selftests/kvm/riscv/get-reg-list.c +++ b/tools/testing/selftests/kvm/riscv/get-reg-list.c @@ -41,9 +41,11 @@ bool filter_reg(__u64 reg) case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_I: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_M: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_V: + case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SMNPM: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SMSTATEEN: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SSAIA: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SSCOFPMF: + case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SSNPM: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SSTC: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SVINVAL: case KVM_REG_RISCV_ISA_EXT | KVM_REG_RISCV_ISA_SINGLE | KVM_RISCV_ISA_EXT_SVNAPOT: @@ -407,9 +409,11 @@ static const char *isa_ext_single_id_to_str(__u64 reg_off) KVM_ISA_EXT_ARR(I), KVM_ISA_EXT_ARR(M), KVM_ISA_EXT_ARR(V), + KVM_ISA_EXT_ARR(SMNPM), KVM_ISA_EXT_ARR(SMSTATEEN), KVM_ISA_EXT_ARR(SSAIA), KVM_ISA_EXT_ARR(SSCOFPMF), + KVM_ISA_EXT_ARR(SSNPM), KVM_ISA_EXT_ARR(SSTC), KVM_ISA_EXT_ARR(SVINVAL), KVM_ISA_EXT_ARR(SVNAPOT), @@ -932,8 +936,10 @@ KVM_ISA_EXT_SUBLIST_CONFIG(aia, AIA); KVM_ISA_EXT_SUBLIST_CONFIG(fp_f, FP_F); KVM_ISA_EXT_SUBLIST_CONFIG(fp_d, FP_D); KVM_ISA_EXT_SIMPLE_CONFIG(h, H); +KVM_ISA_EXT_SIMPLE_CONFIG(smnpm, SMNPM); KVM_ISA_EXT_SUBLIST_CONFIG(smstateen, SMSTATEEN); KVM_ISA_EXT_SIMPLE_CONFIG(sscofpmf, SSCOFPMF); +KVM_ISA_EXT_SIMPLE_CONFIG(ssnpm, SSNPM); KVM_ISA_EXT_SIMPLE_CONFIG(sstc, SSTC); KVM_ISA_EXT_SIMPLE_CONFIG(svinval, SVINVAL); KVM_ISA_EXT_SIMPLE_CONFIG(svnapot, SVNAPOT); @@ -988,8 +994,10 @@ struct vcpu_reg_list *vcpu_configs[] = { &config_fp_f, &config_fp_d, &config_h, + &config_smnpm, &config_smstateen, &config_sscofpmf, + &config_ssnpm, &config_sstc, &config_svinval, &config_svnapot,