From patchwork Thu Aug 29 01:01:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13782305 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 C7DE9C71156 for ; Thu, 29 Aug 2024 01:02: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: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:In-Reply-To:References: List-Owner; bh=NNT/pFK61JaQ3knjtr/fBom0vGwXSbsaOwhmF4RH8vk=; b=xpUiCt7sehQKiF zU69hg0Wy2uPtznHcRzKaLHmY3cqfCd13HQ2OP0GwQoa4tjs9w1kzgIu14gX+owZ2bCbIHYnp+gMk nW3ZJbtYW7rNYiCsTlc94qqCmZnxbSNjbk0TM4rktfZKO14bcHW/mFVd2S/4NdMVIiNSjUg0KJ2nc acPb8hBMrZCMDaU+ew050h+FyISWF0bj623R8YDctbGg+GFi3XsU8MXD4ZnE7iDouGA4mEbaYM2xE VfQGFN+c2y2kKp5uurcF/E0bkc7xdtb7JKIbsk35aB3lufdSAgGjqcAH0CDr0c/OlTMQGiCndH55F aqck1usKq3tAOda5t4dw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjTXz-000000003by-1snI; Thu, 29 Aug 2024 01:01:59 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjTXv-000000003aW-0Ov8 for linux-riscv@lists.infradead.org; Thu, 29 Aug 2024 01:01:57 +0000 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-7141d7b270dso109518b3a.2 for ; Wed, 28 Aug 2024 18:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1724893314; x=1725498114; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6QwsbhYPJkRIIKdEOQBCTBjh/yHwiNO4KwyjIKyWe5c=; b=cFHtvUJuImj9lYLntx6CHTSD2fQsN2kdZveBzKVKKI962lLGqTomvJ8pPk8Vc9bv+9 C3qPpbxVU9cCJe+dvKbkj2oM4HiIKUuGSHcSDd4it/QebUvPwsjnXMnSg2Y4fVqgQbA3 wKXH9TA4mIQBcbNeCiyhTLAIq5WgI6q+j3dZDCbW0QGlc2Tt/c7UCnmiyV7yOiNxBoGA Hp7MwDAkLfbUzEvlZrf+7OWU0Q0csDxnoHQedTZXjP3lpXNkdLk3tZPbynhJanrssGJ3 y8I6PNL2yOl2zDuVjC5wtMzB2It0JvCxoaye19Z114Q0p3vfr19CUDpNZnqLrkkdSZjo 2wvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724893314; x=1725498114; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6QwsbhYPJkRIIKdEOQBCTBjh/yHwiNO4KwyjIKyWe5c=; b=YrJq056cpbKIq4yBQLTHshkShnVbwJwVc2L9x1HUYq6xQFnmCwz0hJDLEIg/EBKaFl tTOWx++85TjZt2ACgKk+X7BaiK2qJXVkOATLktN16orCGNh3GUPpeCFXC31WX9ytau2G xQTSsd9wAAYKZCcCl27ail4sjsnNK8QeOwtUgD+w+1JOQyN1yvR3KBamNKT0J3R+dovu SsO00nlyoVbpbOTmkcFAOXWlgGGyGhxvYIuhKyH1S049UwHRpCkoHGGCwwR7sW8khCVG hnUnyvigNeh3BhX/Ma249sUuK8CWn0wfyJ5TAxZIx1ti3SrKKuItNKjoiC1u///xrWUJ zGwQ== X-Forwarded-Encrypted: i=1; AJvYcCW2psxvtcI4jHkU9/UxGXDjoc4ogI/bdkj3peGqhI9OfSsaisMXhARU8ZKUT6MXy5FvvayaU67e+Owbig==@lists.infradead.org X-Gm-Message-State: AOJu0Yyiq/1ZUWAkVsVy/UJ8HbIjjKgQGGxHVjq3FNZmQ439gM97JMn4 lSvr9yBqNx9TxR9+tpVqoDXXcMtT9G1u6tqG3GS5JJMG3fFFNkIpkhGRhvlJMdA= X-Google-Smtp-Source: AGHT+IG1GSeRH5Ks0VS3MWIlvyXPXQ98om2zedyIsxVmsBu+8+cd1nlfukxYqhWeYJz+YalTi4s/bQ== X-Received: by 2002:a05:6a00:17a8:b0:70a:fb91:66d7 with SMTP id d2e1a72fcca58-715dfca3b68mr1609341b3a.20.1724893313850; Wed, 28 Aug 2024 18:01:53 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-715e5576a4dsm89670b3a.17.2024.08.28.18.01.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 18:01:53 -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 v4 00/10] riscv: Userspace pointer masking and tagged address ABI Date: Wed, 28 Aug 2024 18:01:22 -0700 Message-ID: <20240829010151.2813377-1-samuel.holland@sifive.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240828_180155_193523_8C7D845C X-CRM114-Status: GOOD ( 23.99 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org RISC-V defines three extensions for pointer masking[1]: - Smmpm: configured in M-mode, affects M-mode - Smnpm: configured in M-mode, affects the next lower mode (S or U-mode) - Ssnpm: configured in S-mode, affects the next lower mode (VS, VU, or U-mode) This series adds support for configuring Smnpm or Ssnpm (depending on which privilege mode the kernel is running in) to allow pointer masking in userspace (VU or U-mode), extending the PR_SET_TAGGED_ADDR_CTRL API from arm64. Unlike arm64 TBI, userspace pointer masking is not enabled by default on RISC-V. Additionally, the tag width (referred to as PMLEN) is variable, so userspace needs to ask the kernel for a specific tag width, which is interpreted as a lower bound on the number of tag bits. This series also adds support for a tagged address ABI similar to arm64 and x86. Since accesses from the kernel to user memory use the kernel's pointer masking configuration, not the user's, the kernel must untag user pointers in software before dereferencing them. And since the tag width is variable, as with LAM on x86, it must be kept the same across all threads in a process so untagged_addr_remote() can work. This series depends on my per-thread envcfg series[3]. This series can be tested in QEMU by applying a patch set[2]. KASAN support will be added in a separate patch series. [1]: https://github.com/riscv/riscv-j-extension/releases/download/pointer-masking-v1.0.0-rc2/pointer-masking-v1.0.0-rc2.pdf [2]: https://lore.kernel.org/qemu-devel/20240511101053.1875596-1-me@deliversmonkey.space/ [3]: https://lore.kernel.org/linux-riscv/20240814081126.956287-1-samuel.holland@sifive.com/ Changes in v4: - Switch IS_ENABLED back to #ifdef to fix riscv32 build - Combine __untagged_addr() and __untagged_addr_remote() Changes in v3: - Note in the commit message that the ISA extension spec is frozen - Rebase on riscv/for-next (ISA extension list conflicts) - Remove RISCV_ISA_EXT_SxPM, which was not used anywhere - Use shifts instead of large numbers in ENVCFG_PMM* macro definitions - Rename CONFIG_RISCV_ISA_POINTER_MASKING to CONFIG_RISCV_ISA_SUPM, since it only controls the userspace part of pointer masking - Use IS_ENABLED instead of #ifdef when possible - Use an enum for the supported PMLEN values - Simplify the logic in set_tagged_addr_ctrl() - Use IS_ENABLED instead of #ifdef when possible - Implement mm_untag_mask() - Remove pmlen from struct thread_info (now only in mm_context_t) Changes in v2: - Drop patch 4 ("riscv: Define is_compat_thread()"), as an equivalent patch was already applied - Move patch 5 ("riscv: Split per-CPU and per-thread envcfg bits") to a different series[3] - Update pointer masking specification version reference - Provide macros for the extension affecting the kernel and userspace - Use the correct name for the hstatus.HUPMM field - Rebase on riscv/linux.git for-next - Add and use the envcfg_update_bits() helper function - Inline flush_tagged_addr_state() - Implement untagged_addr_remote() - Restrict PMLEN changes once a process is multithreaded - 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 - Export Supm via hwprobe - Export Smnpm and Ssnpm to KVM guests Samuel Holland (10): dt-bindings: riscv: Add pointer masking ISA extensions riscv: Add ISA extension parsing for pointer masking riscv: Add CSR definitions for pointer masking riscv: Add support for userspace pointer masking riscv: Add support for the tagged address ABI riscv: Allow ptrace control of the tagged address ABI selftests: riscv: Add a pointer masking test riscv: hwprobe: Export the Supm ISA extension RISC-V: KVM: Allow Smnpm and Ssnpm extensions for guests KVM: riscv: selftests: Add Smnpm and Ssnpm to get-reg-list test Documentation/arch/riscv/hwprobe.rst | 3 + .../devicetree/bindings/riscv/extensions.yaml | 18 + arch/riscv/Kconfig | 11 + arch/riscv/include/asm/csr.h | 16 + arch/riscv/include/asm/hwcap.h | 5 + arch/riscv/include/asm/mmu.h | 7 + arch/riscv/include/asm/mmu_context.h | 13 + arch/riscv/include/asm/processor.h | 8 + arch/riscv/include/asm/switch_to.h | 11 + arch/riscv/include/asm/uaccess.h | 43 ++- arch/riscv/include/uapi/asm/hwprobe.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 2 + arch/riscv/kernel/cpufeature.c | 3 + arch/riscv/kernel/process.c | 154 ++++++++ arch/riscv/kernel/ptrace.c | 42 +++ arch/riscv/kernel/sys_hwprobe.c | 3 + arch/riscv/kvm/vcpu_onereg.c | 3 + include/uapi/linux/elf.h | 1 + include/uapi/linux/prctl.h | 3 + .../selftests/kvm/riscv/get-reg-list.c | 8 + 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 ++++++++++++++++++ 24 files changed, 692 insertions(+), 6 deletions(-) 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