qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC 0/5] target/arm: add support for MTE4
@ 2025-11-12  0:50 Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 1/5] target/arm: explicitly disable MTE4 for max Gabriel Brookman
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

This patch, when complete, will implement ARM's Enhanced Memory
Tagging Extension (MTE4). MTE4 guarantees the presence of several
subfeatures: FEAT_MTE_CANONICAL_TAGS, FEAT_MTE_TAGGED_FAR,
FEAT_MTE_STORE_ONLY, FEAT_MTE_NO_ADDRESS_TAGS, and FEAT_MTE_PERM,
none of which are currently implemented in QEMU.

According to the ARM ARM, the presence of any of these features (except
FEAT_MTE_PERM) implies the presence of all the others. For simplicity  
and ease of review, I plan to introduce them one at a time. This patch
handles FEAT_MTE_TAGGED_FAR and FEAT_MTE_STORE_ONLY, with the plan to
introduce FEAT_MTE_CANONICAL_TAGS, FEAT_MTE_NO_ADDRESS_TAGS, and
FEAT_MTE_PERM in later versions of the patch, although I'm submitting
in pieces for ease of review.

FEAT_MTE_TAGGED_FAR guarantees that the full fault address (including
tag bits) is reported after a SEGV_MTESERR, and exposes itself in the
ID_AA64PFR2_EL1 register. QEMU already reports the full address in this
case, so this change only advertises the feature by setting the
appropriate field in ID_AA64PFR2_EL1. My previous version also unset
the feature when MTE was disabled, but looking at the way that similar
features (e.g. MTE_ASYNC) are not unset in this case, I changed my
design to be consistent with them.

FEAT_MTE_STORE_ONLY, when enabled, skips tag checks for memory reads (it
keeps them for writes). I implemented this by creating a helper that
reads the appropriate control registers to determine whether or not this
feature is enabled, and introduced a conditional at the start of
mte_check that skips the check if it's a read and if the feature is
enabled. The conditional only hinges on the contents of the control
register for this feature, not whether or not the feature is actually
implemented on the chosen cpu. I would appreciate feedback about whether
or not I should explicitly check for cpu implementation of this feature.

Testing:
- For FEAT_MTE_TAGGED_FAR, I wrote a test for this functionality that is
  now included as mte-9.c, as per Gustavo's suggestion.
- For FEAT_MTE_STORE_ONLY, I wrote a test for this functionality, but
  since the feature requires setting a SCTLR_EL1 bit to be enabled, I
  was only able to pass this test successfully after I manually set the
  bit in question. It's not clear to me how I should go about submitting
  this test, since I don't see a way to set this SCTLR_EL1 bit from
  within a user-mode test. Some guidance here would be useful.

Follow-up patches will implement the remaining MTE4 subfeatures listed
above.

Thanks,
Gabriel Brookman

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
Gabriel Brookman (5):
      target/arm: explicitly disable MTE4 for max
      tests/tcg: added test for MTE FAR
      target/arm: add TCSO bitmasks to SCTLR
      target/arm: add FEAT_MTE_STORE_ONLY logic
      docs: added MTE4 features to docs

 docs/system/arm/emulation.rst     |  2 ++
 target/arm/cpu.h                  |  2 ++
 target/arm/helper.c               |  4 ++--
 target/arm/tcg/cpu64.c            |  8 +++++++
 target/arm/tcg/mte_helper.c       | 22 ++++++++++++++++++
 tests/tcg/aarch64/Makefile.target |  2 +-
 tests/tcg/aarch64/mte-9.c         | 48 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 85 insertions(+), 3 deletions(-)
---
base-commit: 593aee5df98b4a862ff8841a57ea3dbf22131a5f
change-id: 20251109-feat-mte4-6740a6202e83

Best regards,
-- 
Gabriel Brookman <brookmangabriel@gmail.com>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH RFC 1/5] target/arm: explicitly disable MTE4 for max
  2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
@ 2025-11-12  0:50 ` Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 2/5] tests/tcg: added test for MTE FAR Gabriel Brookman
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

Previously, the bits used to advertise the various MTE4 features were
not explicitly set for -cpu max. This commit calls out these bits and
explicitly unsets them. At the end of the patch series, a second commit
will explicitly set all of them.

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
 target/arm/tcg/cpu64.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/target/arm/tcg/cpu64.c b/target/arm/tcg/cpu64.c
index 6871956382..6688b78bb8 100644
--- a/target/arm/tcg/cpu64.c
+++ b/target/arm/tcg/cpu64.c
@@ -1281,8 +1281,16 @@ void aarch64_max_tcg_initfn(Object *obj)
     t = FIELD_DP64(t, ID_AA64PFR1, CSV2_FRAC, 0); /* FEAT_CSV2_3 */
     t = FIELD_DP64(t, ID_AA64PFR1, NMI, 1);       /* FEAT_NMI */
     t = FIELD_DP64(t, ID_AA64PFR1, GCS, 1);       /* FEAT_GCS */
+    t = FIELD_DP64(t, ID_AA64PFR1,
+            MTEX, 0);      /* FEAT_MTE_NO_ADDRESS_TAGS + FEAT_MTE_CANONICAL_TAGS */
     SET_IDREG(isar, ID_AA64PFR1, t);
 
+    t = GET_IDREG(isar, ID_AA64PFR2);
+    t = FIELD_DP64(t, ID_AA64PFR2, MTEFAR, 0);    /* FEAT_MTE_TAGGED_FAR */
+    t = FIELD_DP64(t, ID_AA64PFR2, MTESTOREONLY, 0);    /* FEAT_MTE_STORE_ONLY */
+    t = FIELD_DP64(t, ID_AA64PFR2, MTEPERM, 0);    /* FEAT_MTE_PERM */
+    SET_IDREG(isar, ID_AA64PFR2, t);
+
     t = GET_IDREG(isar, ID_AA64MMFR0);
     t = FIELD_DP64(t, ID_AA64MMFR0, PARANGE, 6); /* FEAT_LPA: 52 bits */
     t = FIELD_DP64(t, ID_AA64MMFR0, TGRAN16, 1);   /* 16k pages supported */

-- 
2.51.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH RFC 2/5] tests/tcg: added test for MTE FAR
  2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 1/5] target/arm: explicitly disable MTE4 for max Gabriel Brookman
@ 2025-11-12  0:50 ` Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 3/5] target/arm: add TCSO bitmasks to SCTLR Gabriel Brookman
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

This functionality was previously enabled but not advertised or tested.
This commit adds a new test, mte-9, that tests the code for proper
full-address reporting.

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
 tests/tcg/aarch64/Makefile.target |  2 +-
 tests/tcg/aarch64/mte-9.c         | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target
index 9fa8687453..b491cfb5e1 100644
--- a/tests/tcg/aarch64/Makefile.target
+++ b/tests/tcg/aarch64/Makefile.target
@@ -64,7 +64,7 @@ AARCH64_TESTS += bti-2
 
 # MTE Tests
 ifneq ($(CROSS_CC_HAS_ARMV8_MTE),)
-AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-5 mte-6 mte-7 mte-8
+AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-5 mte-6 mte-7 mte-8 mte-9
 mte-%: CFLAGS += $(CROSS_CC_HAS_ARMV8_MTE)
 endif
 
diff --git a/tests/tcg/aarch64/mte-9.c b/tests/tcg/aarch64/mte-9.c
new file mode 100644
index 0000000000..9626a90c13
--- /dev/null
+++ b/tests/tcg/aarch64/mte-9.c
@@ -0,0 +1,48 @@
+/*
+ * Memory tagging, full-address reporting.
+ *
+ * Copyright (c) 2021 Linaro Ltd
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "mte.h"
+
+static void *faulting_ptr;
+
+void pass(int sig, siginfo_t *info, void *uc)
+{
+    assert(faulting_ptr == info->si_addr);
+    exit(0);
+}
+
+int main(int ac, char **av)
+{
+    struct sigaction sa;
+    int *p0, *p1, *p2;
+    long excl = 1;
+
+    enable_mte(PR_MTE_TCF_SYNC);
+    p0 = alloc_mte_mem(sizeof(*p0));
+
+    /* Create two differently tagged pointers. */
+    asm("irg %0,%1,%2" : "=r"(p1) : "r"(p0), "r"(excl));
+    asm("gmi %0,%1,%0" : "+r"(excl) : "r" (p1));
+    assert(excl != 1);
+    asm("irg %0,%1,%2" : "=r"(p2) : "r"(p0), "r"(excl));
+    assert(p1 != p2);
+
+    /* Store the tag from the first pointer.  */
+    asm("stg %0, [%0]" : : "r"(p1));
+
+    *p1 = 0;
+
+    memset(&sa, 0, sizeof(sa));
+    sa.sa_sigaction = pass;
+    sa.sa_flags = SA_SIGINFO;
+    sigaction(SIGSEGV, &sa, NULL);
+
+    faulting_ptr = p2;
+    *p2 = 0;
+
+    abort();
+}

-- 
2.51.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH RFC 3/5] target/arm: add TCSO bitmasks to SCTLR
  2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 1/5] target/arm: explicitly disable MTE4 for max Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 2/5] tests/tcg: added test for MTE FAR Gabriel Brookman
@ 2025-11-12  0:50 ` Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 4/5] target/arm: add FEAT_MTE_STORE_ONLY logic Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 5/5] docs: added MTE4 features to docs Gabriel Brookman
  4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

These are the bitmasks used to control the FEAT_MTE_STORE_ONLY feature.
They are now named and setting these fields of SCTLR is ignored if MTE
is disabled, as per convention.

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
 target/arm/cpu.h    | 2 ++
 target/arm/helper.c | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/target/arm/cpu.h b/target/arm/cpu.h
index 39f2b2e54d..2c7c76777f 100644
--- a/target/arm/cpu.h
+++ b/target/arm/cpu.h
@@ -1424,6 +1424,8 @@ void pmu_init(ARMCPU *cpu);
 #define SCTLR_EnAS0   (1ULL << 55) /* FEAT_LS64_ACCDATA */
 #define SCTLR_EnALS   (1ULL << 56) /* FEAT_LS64 */
 #define SCTLR_EPAN    (1ULL << 57) /* FEAT_PAN3 */
+#define SCTLR_TSCO0    (1ULL << 58) /* FEAT_MTE_STORE_ONLY */
+#define SCTLR_TSCO    (1ULL << 59) /* FEAT_MTE_STORE_ONLY */
 #define SCTLR_EnTP2   (1ULL << 60) /* FEAT_SME */
 #define SCTLR_NMI     (1ULL << 61) /* FEAT_NMI */
 #define SCTLR_SPINTMASK (1ULL << 62) /* FEAT_NMI */
diff --git a/target/arm/helper.c b/target/arm/helper.c
index 27ebc6f29b..32fbb2e25d 100644
--- a/target/arm/helper.c
+++ b/target/arm/helper.c
@@ -3364,10 +3364,10 @@ static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 
     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
         if (ri->opc1 == 6) { /* SCTLR_EL3 */
-            value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
+            value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA | SCTLR_TSCO);
         } else {
             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
-                       SCTLR_ATA0 | SCTLR_ATA);
+                       SCTLR_ATA0 | SCTLR_ATA | SCTLR_TSCO | SCTLR_TSCO0);
         }
     }
 

-- 
2.51.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH RFC 4/5] target/arm: add FEAT_MTE_STORE_ONLY logic
  2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
                   ` (2 preceding siblings ...)
  2025-11-12  0:50 ` [PATCH RFC 3/5] target/arm: add TCSO bitmasks to SCTLR Gabriel Brookman
@ 2025-11-12  0:50 ` Gabriel Brookman
  2025-11-12  0:50 ` [PATCH RFC 5/5] docs: added MTE4 features to docs Gabriel Brookman
  4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

This feature automatically succeeds tag checks on load instructions when
the appropriate SCTLR_TCSO register for the current exception level is
set.

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
 target/arm/tcg/mte_helper.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/target/arm/tcg/mte_helper.c b/target/arm/tcg/mte_helper.c
index bb48fe359b..3f7e89f436 100644
--- a/target/arm/tcg/mte_helper.c
+++ b/target/arm/tcg/mte_helper.c
@@ -865,8 +865,30 @@ static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
     return 0;
 }
 
+static bool mte_store_only_active(CPUARMState *env)
+{
+    int el = arm_current_el(env);
+    if (el) {
+        if (SCTLR_TSCO & env->cp15.sctlr_el[el]) {
+            return true;
+        }
+    } else {
+        if ((HCR_E2H & env->cp15.hcr_el2) &&
+            (SCTLR_TSCO0 & env->cp15.sctlr_el[2])) {
+            return true;
+        } else if (SCTLR_TSCO0 & env->cp15.sctlr_el[1]) {
+            return true;
+        }
+    }
+    return false;
+}
+
 uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra)
 {
+    if (!FIELD_EX32(desc, MTEDESC, WRITE) && mte_store_only_active(env)) {
+        return useronly_clean_ptr(ptr);
+    }
+
     uint64_t fault;
     int ret = mte_probe_int(env, desc, ptr, ra, &fault);
 

-- 
2.51.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH RFC 5/5] docs: added MTE4 features to docs
  2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
                   ` (3 preceding siblings ...)
  2025-11-12  0:50 ` [PATCH RFC 4/5] target/arm: add FEAT_MTE_STORE_ONLY logic Gabriel Brookman
@ 2025-11-12  0:50 ` Gabriel Brookman
  4 siblings, 0 replies; 6+ messages in thread
From: Gabriel Brookman @ 2025-11-12  0:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Gustavo Romero, qemu-arm, Gabriel Brookman

The implemented MTE4 features are now present in
docs/system/arm/emulation.rst

Signed-off-by: Gabriel Brookman <brookmangabriel@gmail.com>
---
 docs/system/arm/emulation.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/docs/system/arm/emulation.rst b/docs/system/arm/emulation.rst
index 31a5878a8f..1bf99c77bb 100644
--- a/docs/system/arm/emulation.rst
+++ b/docs/system/arm/emulation.rst
@@ -106,6 +106,8 @@ the following architecture extensions:
 - FEAT_MTE3 (MTE Asymmetric Fault Handling)
 - FEAT_MTE_ASYM_FAULT (Memory tagging asymmetric faults)
 - FEAT_MTE_ASYNC (Asynchronous reporting of Tag Check Fault)
+- FEAT_MTE_TAGGED_FAR (Full address reporting of Tag Check Fault)
+- FEAT_MTE_STORE_ONLY (Store-only tag checking)
 - FEAT_NMI (Non-maskable Interrupt)
 - FEAT_NV (Nested Virtualization)
 - FEAT_NV2 (Enhanced nested virtualization support)

-- 
2.51.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-11-12  0:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-12  0:50 [PATCH RFC 0/5] target/arm: add support for MTE4 Gabriel Brookman
2025-11-12  0:50 ` [PATCH RFC 1/5] target/arm: explicitly disable MTE4 for max Gabriel Brookman
2025-11-12  0:50 ` [PATCH RFC 2/5] tests/tcg: added test for MTE FAR Gabriel Brookman
2025-11-12  0:50 ` [PATCH RFC 3/5] target/arm: add TCSO bitmasks to SCTLR Gabriel Brookman
2025-11-12  0:50 ` [PATCH RFC 4/5] target/arm: add FEAT_MTE_STORE_ONLY logic Gabriel Brookman
2025-11-12  0:50 ` [PATCH RFC 5/5] docs: added MTE4 features to docs Gabriel Brookman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).