Received: by 2002:ab2:1347:0:b0:1f4:ac9d:b246 with SMTP id g7csp109105lqg; Wed, 10 Apr 2024 17:52:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWRXQC12U6aXt53Cp9ljbwv5elkKJbo+tFXGPc4IFpjcKb6aHYmz783ifP/uSIY5HGzD5IMXNKZT9OLtGgi2IYfWM/0/0zhgTISitw7aw== X-Google-Smtp-Source: AGHT+IHe7zJxNpA7F4qZluQkR6djyr8SKUsB+RZKhn57hkf66sPF6AJyHhlEfbSk8RU0Ft+wmWh6 X-Received: by 2002:ae9:e204:0:b0:78d:6142:d177 with SMTP id c4-20020ae9e204000000b0078d6142d177mr4132953qkc.73.1712796771881; Wed, 10 Apr 2024 17:52:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712796771; cv=pass; d=google.com; s=arc-20160816; b=ZV9DZ7KgfCPTuc3Oxd4bz1pNjAwdK3yU6YI4dhoA9NygwzE4etBi/hE8E++JZlg9gt BkOx9mkE5xSr/0/jJURvQfj5ArEtYgAnbOcNgh6975EG7TT9zayJiumMWTYwn8LMFvn7 cGuG99XMVor8ko4bn/8O7Nzzymm2rI1W0oJjFNKdpP/i4AVV/x/xkbAwHK4xpgv5gJpH IFTz6tAFBgLWt9wUJi7dzBBHidNvTS9bOObGN8Vpuw06uoZmSbka22BLUliW7natMqFy YeDHyGk6dD63lEjEI9W0BUE0jOzg/mWRrj6Ie06CybUYXEYmv0ULZlcuYOidg+hQahcT PDIg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=616CgmwFgYwzV21QmwiJi32/eELpxgF6Jdi7cJvnEW4=; fh=6JoYChsnVFzsbcTrzEnZbDTSp1QTFnCDgh53qSJeVs4=; b=IHiNkP0IYz4bpDFAnWYJGHVIskg7CU3FBz4LCXCZlzZXoI2TN4jSNHN4fQIN7io1R3 ddWYUbAHOGUyDq66GIQfYE2iezDhemUVxZ+ohIgeBHVyR3J0KMEFfRumUyYprJMYzLxV tmHcUXjU8d7Q/oECrbz6Q57EWUh+gBGWPojgteTmmHBc0UENI364PWd8OGhh8WLaAQev aSSdPQr6jcIazU2YZCwhX9CMUZQ9swEeBi01YM5WUvFPSIng/qfpHo62NjIqaUUuM0I2 JvM1rff/aq7XHxEPXESnZjUVg1OwCrrTNhWIJ3c7uNEg2opSqX6C/LAr/7h9uF9kNDZl lWMw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=hQsQ2KMF; arc=pass (i=1 spf=pass spfdomain=marcan.st dkim=pass dkdomain=marcan.st dmarc=pass fromdomain=marcan.st); spf=pass (google.com: domain of linux-kernel+bounces-139598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139598-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id i9-20020a05620a144900b0078d61468078si467563qkl.582.2024.04.10.17.52.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 17:52:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=hQsQ2KMF; arc=pass (i=1 spf=pass spfdomain=marcan.st dkim=pass dkdomain=marcan.st dmarc=pass fromdomain=marcan.st); spf=pass (google.com: domain of linux-kernel+bounces-139598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139598-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 8C6701C22360 for ; Thu, 11 Apr 2024 00:52:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7BD10134D1; Thu, 11 Apr 2024 00:51:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=marcan.st header.i=@marcan.st header.b="hQsQ2KMF" Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C880513ADC for ; Thu, 11 Apr 2024 00:51:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.63.210.85 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712796713; cv=none; b=UsoojF9mSyg37j50bH2eT0S3KOMmwDVle9TA4KFDXWeBn2z204VwzoWW0yL35r+g2DS2kawUnu7aUVU8mJRixSrBd+vjaUuIloiAnQe1Y3KAx1nO5wu8Rt7Kq6iVTG+opj+8vhAeqBqxxoGy9zBRuMQFWHp+3HAtZ/YwhlHFmfc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712796713; c=relaxed/simple; bh=A6cfBeGeoux68NpHgjY1yK+Y/P3gva5wYhlCSyteIQ8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OEjsKb4ATcT/GFSsvHGwJN5kM+a2zoZRjIDEtWqCo85QcuJ1lDhF/XvQ2W4rj0HdKUue/IMz/LKUP2R9eAZrQbUcNBqTmbKdRSC15kfJFPgsrQeABo4pInH/vHqQdR/o45OxPBgM7+AdmnzqmwPqlOPVgvuiOHgHdRH0pSz1moI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=marcan.st; spf=pass smtp.mailfrom=marcan.st; dkim=pass (2048-bit key) header.d=marcan.st header.i=@marcan.st header.b=hQsQ2KMF; arc=none smtp.client-ip=212.63.210.85 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=marcan.st Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=marcan.st Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id A9CB8425C7; Thu, 11 Apr 2024 00:51:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=marcan.st; s=default; t=1712796710; bh=A6cfBeGeoux68NpHgjY1yK+Y/P3gva5wYhlCSyteIQ8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hQsQ2KMFH+62Ms4wBxA9kOprBD45PuiudDTCI4Jc4FGX9LpD74wyBTzgEPPVjWUnB 953bY8Dqz9SO3OO4aOQL27CegKMagwfpLuV+N2j2IubpZ7mJZRXeV17q646tvLmMdj AQDO6gvir7cpikZZ9v9b5p9KjuO929lju6gRqXmEl6ayhx28tondrxz9ioiCDG4zG/ k8Tkna5i6wcEZy8+4LyPyH56PrRn/PB64V2/cZ1Ga6JK07yFX/7ipVjJ20wK9jbGED /rxmNI9VBoHwwwL0I2ru1B1ACbf8DbqhzbbOC/lshwf4EIjeUqmSxkMii2YRtjO8Uj BZfDAJl3lm9Rg== From: Hector Martin Date: Thu, 11 Apr 2024 09:51:23 +0900 Subject: [PATCH 4/4] arm64: Implement Apple IMPDEF TSO memory model control Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240411-tso-v1-4-754f11abfbff@marcan.st> References: <20240411-tso-v1-0-754f11abfbff@marcan.st> In-Reply-To: <20240411-tso-v1-0-754f11abfbff@marcan.st> To: Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland Cc: Zayd Qumsieh , Justin Lu , Ryan Houdek , Mark Brown , Ard Biesheuvel , Mateusz Guzik , Anshuman Khandual , Oliver Upton , Miguel Luis , Joey Gouly , Christoph Paasch , Kees Cook , Sami Tolvanen , Baoquan He , Joel Granados , Dawei Li , Andrew Morton , Florent Revest , David Hildenbrand , Stefan Roesch , Andy Chiu , Josh Triplett , Oleg Nesterov , Helge Deller , Zev Weiss , Ondrej Mosnacek , Miguel Ojeda , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Asahi Linux , Hector Martin X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5582; i=marcan@marcan.st; h=from:subject:message-id; bh=A6cfBeGeoux68NpHgjY1yK+Y/P3gva5wYhlCSyteIQ8=; b=owGbwMvMwCUm+yP4NEe/cRLjabUkhjRxE94WxUt9IucjBTdtv6v15/UKdQNda43uC39P229ef uim5u6wjlIWBjEuBlkxRZbGE72nuj2nn1NXTZkOM4eVCWQIAxenAEzkhCrDf8/nnCam8mvmbv3j rVJcmMGmb8BzQcSso17uv+gqxuKo9Qz/7O4K+/y0uul+TvFG7uNAH1NVHblvejunNfWVqW+SPqj PBAA= X-Developer-Key: i=marcan@marcan.st; a=openpgp; fpr=FC18F00317968B7BE86201CBE22A629A4C515DD5 Apple CPUs may implement the TSO memory model as an optional configurable mode. This allows x86 emulators to simplify their load/store handling, greatly increasing performance. Expose this via the prctl PR_SET_MEM_MODEL_TSO mechanism. We use the Apple IMPDEF AIDR_EL1 register to check for the availability of TSO mode, and enable this codepath on all CPUs with an Apple implementer. This relies on the ACTLR_EL1 thread state scaffolding introduced earlier. Signed-off-by: Hector Martin --- arch/arm64/Kconfig | 2 ++ arch/arm64/include/asm/apple_cpufeature.h | 15 +++++++++++++++ arch/arm64/include/asm/cpufeature.h | 3 ++- arch/arm64/kernel/cpufeature_impdef.c | 23 +++++++++++++++++++++++ arch/arm64/kernel/process.c | 22 ++++++++++++++++++++++ arch/arm64/tools/cpucaps | 1 + 6 files changed, 65 insertions(+), 1 deletion(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 9b3593b34cce..2f3eedd955c9 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2167,6 +2167,8 @@ endif # ARM64_PSEUDO_NMI config ARM64_MEMORY_MODEL_CONTROL bool "Runtime memory model control" + default ARCH_APPLE + select ARM64_ACTLR_STATE help Some ARM64 CPUs support runtime switching of the CPU memory model, which can be useful to emulate other CPU architectures diff --git a/arch/arm64/include/asm/apple_cpufeature.h b/arch/arm64/include/asm/apple_cpufeature.h new file mode 100644 index 000000000000..4370d91ffa3e --- /dev/null +++ b/arch/arm64/include/asm/apple_cpufeature.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#ifndef __ASM_APPLE_CPUFEATURES_H +#define __ASM_APPLE_CPUFEATURES_H + +#include +#include + +#define AIDR_APPLE_TSO_SHIFT 9 +#define AIDR_APPLE_TSO BIT(9) + +#define ACTLR_APPLE_TSO_SHIFT 1 +#define ACTLR_APPLE_TSO BIT(1) + +#endif diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index 46ab37f8f4d8..a191000d88c2 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -911,7 +911,8 @@ static inline unsigned int get_vmid_bits(u64 mmfr1) static __always_inline bool system_has_actlr_state(void) { - return false; + return IS_ENABLED(CONFIG_ARM64_ACTLR_STATE) && + alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE); } s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur); diff --git a/arch/arm64/kernel/cpufeature_impdef.c b/arch/arm64/kernel/cpufeature_impdef.c index bb04a8e3d79d..9325d1eb12f4 100644 --- a/arch/arm64/kernel/cpufeature_impdef.c +++ b/arch/arm64/kernel/cpufeature_impdef.c @@ -4,8 +4,21 @@ */ #include +#include #ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL +static bool has_apple_feature(const struct arm64_cpu_capabilities *entry, int scope) +{ + u64 val; + WARN_ON(scope != SCOPE_SYSTEM); + + if (read_cpuid_implementor() != ARM_CPU_IMP_APPLE) + return false; + + val = read_sysreg(aidr_el1); + return cpufeature_matches(val, entry); +} + static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope) { /* List of CPUs that always use the TSO memory model */ @@ -22,6 +35,16 @@ static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope) static const struct arm64_cpu_capabilities arm64_impdef_features[] = { #ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL + { + .desc = "TSO memory model (Apple)", + .capability = ARM64_HAS_TSO_APPLE, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .matches = has_apple_feature, + .field_pos = AIDR_APPLE_TSO_SHIFT, + .field_width = 1, + .sign = FTR_UNSIGNED, + .min_field_value = 1, + }, { .desc = "TSO memory model (Fixed)", .capability = ARM64_HAS_TSO_FIXED, diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index 117f80e16aac..34a19ecfb630 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -522,6 +523,10 @@ void update_sctlr_el1(u64 sctlr) #ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL int arch_prctl_mem_model_get(struct task_struct *t) { + if (alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE) && + t->thread.actlr & ACTLR_APPLE_TSO) + return PR_SET_MEM_MODEL_TSO; + return PR_SET_MEM_MODEL_DEFAULT; } @@ -531,6 +536,23 @@ int arch_prctl_mem_model_set(struct task_struct *t, unsigned long val) val == PR_SET_MEM_MODEL_TSO) return 0; + if (alternative_has_cap_unlikely(ARM64_HAS_TSO_APPLE)) { + WARN_ON(!system_has_actlr_state()); + + switch (val) { + case PR_SET_MEM_MODEL_TSO: + t->thread.actlr |= ACTLR_APPLE_TSO; + break; + case PR_SET_MEM_MODEL_DEFAULT: + t->thread.actlr &= ~ACTLR_APPLE_TSO; + break; + default: + return -EINVAL; + } + write_sysreg(t->thread.actlr, actlr_el1); + return 0; + } + if (val == PR_SET_MEM_MODEL_DEFAULT) return 0; diff --git a/arch/arm64/tools/cpucaps b/arch/arm64/tools/cpucaps index daa6b9495402..62f9ca9ce44b 100644 --- a/arch/arm64/tools/cpucaps +++ b/arch/arm64/tools/cpucaps @@ -52,6 +52,7 @@ HAS_STAGE2_FWB HAS_TCR2 HAS_TIDCP1 HAS_TLB_RANGE +HAS_TSO_APPLE HAS_TSO_FIXED HAS_VA52 HAS_VIRT_HOST_EXTN -- 2.44.0