Skip to contentSkip to conversation

target/arm: add support for MTE4

RFC PATCH Superseded Gabriel Brookman 2 participants 20 messages Jan 5 – Jan 6

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover target/arm: add support for MTE4

This patch implements ARM's Enhanced Memory Tagging Extension (MTE4). MTE4 implies 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. This patch implements all five.

Testing:
- Included for FAR and STORE_ONLY.
- The MTE_CANONICAL/NAT test from the previous email, modified so MTE_CANONICAL is enabled in user mode.
- A bare-metal testsuite that sets up page tables for S1 and S2 translation, to test the features that rely on those. Didn't include it because it's unwieldy.
- The bare-metal testsuite also was used to test LDGM and similar instructions not permitted in user-mode.

Thanks,
Gabriel Brookman

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3116

---
Changes in v3:
- Added prctl for MTE_STORE_ONLY to linux-user
- mte_check is no longer generated on read when STORE_ONLY enabled
- Implemented LDGM instruction
- Removed "long" datatype as per Richard's suggestion
- Implemented masking for VA range checks when MTX bit enabled
- Implemented MTE_PERM, with NoTagAccess attribute
- Removed user-mode test for MTE_CANONICAL, since can't enable in
user-mode.
- Removed TBI from mte_check generation logic
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- Added tests for STORE_ONLY.
- Refined commit messages.
- Added FEAT_MTE_CANONICAL_TAGS and FEAT_MTE_NO_ADDRESS_TAGS + tests.
- fixed TCSO bit macro names.
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Gabriel Brookman (12):
target/arm: implement MTE_PERM target/arm: add TCSO bitmasks to SCTLR target/arm: mte_check unemitted on STORE_ONLY load
linux-user: add MTE_STORE_ONLY to prctl target/arm: removed TBI bits from MTE check logic target/arm: add canonical and no-address tag logic target/arm: ldg on canonical tag loads the tag target/arm: storing to canonical tags faults target/arm: added mtx to translation logic
docs: add MTE4 features to docs tests/tcg: add test for MTE FAR tests/tcg: add test for MTE_STORE_ONLY

 docs/system/arm/emulation.rst        |   5 ++
 linux-user/aarch64/mte_user_helper.h |   3 +
 linux-user/aarch64/target_prctl.h    |  11 +++
 target/arm/cpu-features.h            |  10 +++
 target/arm/cpu.h                     |   4 +
 target/arm/helper.c                  |  27 ++++++-
 target/arm/internals.h               |  31 ++++++++
 target/arm/ptw.c                     |  45 ++++++++++-
 target/arm/tcg/cpu64.c               |   8 ++
 target/arm/tcg/helper-a64.c          |   9 +--
 target/arm/tcg/hflags.c              |  25 +++++--
 target/arm/tcg/mte_helper.c          | 140 +++++++++++++++++++++++++++++++----
 target/arm/tcg/translate-a64.c       |   8 +-
 target/arm/tcg/translate.h           |   2 +
 tests/tcg/aarch64/Makefile.target    |   2 +-
 tests/tcg/aarch64/mte-10.c           |  49 ++++++++++++
 tests/tcg/aarch64/mte-9.c            |  48 ++++++++++++
 tests/tcg/aarch64/mte.h              |  15 ++++
 18 files changed, 401 insertions(+), 41 deletions(-)

---
base-commit: 159107e390609f71b78268a4888563dcdce6ac65
change-id: 20251109-feat-mte4-6740a6202e83

Best regards,

Signature · 1 line
Gabriel Brookman <[email protected]>
#17 lore raw

RFC PATCH v3 11/12 tests/tcg: add test for MTE FAR

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. FEAT_MTE_TAGGED_FAR requires that FAR_ELx report the full logical address, including tag bits.

---
 tests/tcg/aarch64/Makefile.target |  2 +-
 tests/tcg/aarch64/mte-9.c         | 48 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
tests/tcg/aarch64/Makefile.target +1 -1
index 9fa8687453..b491cfb5e1 100644
@@ -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
 
tests/tcg/aarch64/mte-9.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.52.0
#3 lore raw

RFC PATCH v3 2/12 target/arm: add TCSO bitmasks to SCTLR

In reply to 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 or MTE4 is disabled, as per convention.

---
 target/arm/cpu-features.h |  5 +++++
 target/arm/cpu.h          |  2 ++
 target/arm/helper.c       | 11 +++++++++--
 3 files changed, 16 insertions(+), 2 deletions(-)
target/arm/cpu-features.h +5 -0
index 48009b5a66..3473787ab8 100644
@@ -1144,6 +1144,11 @@ static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) == 1;
 }
 
+static inline bool isar_feature_aa64_mte4(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEFAR) == 1;
+}
+
 static inline bool isar_feature_aa64_sme(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, SME) != 0;
target/arm/cpu.h +2 -0
index 9579d43ba3..393bfc0dc9 100644
@@ -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_TCSO0   (1ULL << 58) /* FEAT_MTE_STORE_ONLY */
+#define SCTLR_TCSO    (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 */
target/arm/helper.c +9 -2
index 263ca29d92..4086423b6f 100644
@@ -3364,10 +3364,17 @@ 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_TCSO);
         } else {
             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
-                       SCTLR_ATA0 | SCTLR_ATA);
+                       SCTLR_ATA0 | SCTLR_ATA | SCTLR_TCSO | SCTLR_TCSO0);
+        }
+    } else if (ri->state == ARM_CP_STATE_AA64
+            && !cpu_isar_feature(aa64_mte4, cpu)) { /* mte but not mte4 */
+        if (ri->opc1 == 6) { /* SCTLR_EL3 */
+            value &= ~SCTLR_TCSO;
+        } else {
+            value &= ~(SCTLR_TCSO | SCTLR_TCSO0);
         }
     }
 
Signature · 1 line
2.52.0
#5 lore raw

RFC PATCH v3 3/12 target/arm: mte_check unemitted on STORE_ONLY load

In reply to Gabriel Brookman

This feature disables generation of the mte check helper on loads when STORE_ONLY tag checking mode is enabled.

---
 target/arm/cpu.h               |  2 ++
 target/arm/tcg/hflags.c        | 14 ++++++++++++++
 target/arm/tcg/translate-a64.c |  8 ++++++--
 target/arm/tcg/translate.h     |  2 ++
 4 files changed, 24 insertions(+), 2 deletions(-)
target/arm/cpu.h +2 -0
index 393bfc0dc9..4087484faf 100644
@@ -2496,6 +2496,8 @@ FIELD(TBFLAG_A64, ZT0EXC_EL, 39, 2)
 FIELD(TBFLAG_A64, GCS_EN, 41, 1)
 FIELD(TBFLAG_A64, GCS_RVCEN, 42, 1)
 FIELD(TBFLAG_A64, GCSSTR_EL, 43, 2)
+FIELD(TBFLAG_A64, MTE_STORE_ONLY, 45, 1)
+FIELD(TBFLAG_A64, MTE0_STORE_ONLY, 46, 1)
 
 /*
  * Helpers for using the above. Note that only the A64 accessors use
target/arm/tcg/hflags.c +14 -0
index 5c9b9bec3b..c4696af5d8 100644
@@ -425,6 +425,16 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
                      */
                     DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
                 }
+                /*
+                 * Repeat for MTE_STORE_ONLY
+                 */
+                if (cpu_isar_feature(aa64_mte4, env_archcpu(env)) &&
+                    ((el == 0 ? SCTLR_TCSO0 : SCTLR_TCSO) & sctlr)) {
+                    DP_TBFLAG_A64(flags, MTE_STORE_ONLY, 1);
+                    if (!EX_TBFLAG_A64(flags, UNPRIV)) {
+                        DP_TBFLAG_A64(flags, MTE0_STORE_ONLY, 1);
+                    }
+                }
             }
         }
         /* And again for unprivileged accesses, if required.  */
@@ -434,6 +444,10 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
             && (sctlr & SCTLR_TCF0)
             && allocation_tag_access_enabled(env, 0, sctlr)) {
             DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
+            if (cpu_isar_feature(aa64_mte4, env_archcpu(env)) &&
+                (SCTLR_TCSO0 & sctlr)) {
+                DP_TBFLAG_A64(flags, MTE0_STORE_ONLY, 1);
+            }
         }
         /*
          * For unpriv tag-setting accesses we also need ATA0. Again, in
target/arm/tcg/translate-a64.c +6 -2
index cde22a5cca..8b39d5357a 100644
@@ -298,7 +298,8 @@ static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
                                       MemOp memop, bool is_unpriv,
                                       int core_idx)
 {
-    if (tag_checked && s->mte_active[is_unpriv]) {
+    if (tag_checked && s->mte_active[is_unpriv] &&
+        (is_write || !s->mte_store_only[is_unpriv])) {
         TCGv_i64 ret;
         int desc = 0;
 
@@ -330,7 +331,8 @@ TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
                         bool tag_checked, int total_size, MemOp single_mop)
 {
-    if (tag_checked && s->mte_active[0]) {
+    if (tag_checked && s->mte_active[0] &&
+        (is_write || !s->mte_store_only[0])) {
         TCGv_i64 ret;
         int desc = 0;
 
@@ -10693,6 +10695,8 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
     dc->ata[1] = EX_TBFLAG_A64(tb_flags, ATA0);
     dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
     dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
+    dc->mte_store_only[0] = EX_TBFLAG_A64(tb_flags, MTE_STORE_ONLY);
+    dc->mte_store_only[1] = EX_TBFLAG_A64(tb_flags, MTE0_STORE_ONLY);
     dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
     dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
     dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
target/arm/tcg/translate.h +2 -0
index b62104b4ae..206aadcb17 100644
@@ -140,6 +140,8 @@ typedef struct DisasContext {
     bool ata[2];
     /* True if v8.5-MTE tag checks affect the PE; index with is_unpriv.  */
     bool mte_active[2];
+    /* True if v8.5-MTE tag checks disabled for reads; index with is_unpriv. */
+    bool mte_store_only[2];
     /* True with v8.5-BTI and SCTLR_ELx.BT* set.  */
     bool bt;
     /* True if any CP15 access is trapped by HSTR_EL2 */
Signature · 1 line
2.52.0
#15 lore raw

RFC PATCH v3 9/12 target/arm: added mtx to translation logic

In reply to Gabriel Brookman

Added translation logic to ignore canonicity mismatch in tag bits during translation step if canonical tag checking is active.

---
 target/arm/helper.c    | 16 +++++++++++++++-
 target/arm/internals.h |  2 ++
 target/arm/ptw.c       | 28 +++++++++++++++++++++++++---
 3 files changed, 42 insertions(+), 4 deletions(-)
target/arm/helper.c +15 -1
index 4086423b6f..5e8b5b1bc5 100644
@@ -9579,6 +9579,16 @@ uint64_t arm_sctlr(CPUARMState *env, int el)
     return env->cp15.sctlr_el[el];
 }
 
+int aa64_va_parameter_mtx(uint64_t tcr, ARMMMUIdx mmu_idx)
+{
+    if (regime_has_2_ranges(mmu_idx)) {
+        return extract64(tcr, 60, 2);
+    } else {
+        /* Replicate the single MTX bit so we always have 2 bits.  */
+        return extract64(tcr, 33, 1) * 3;
+    }
+}
+
 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
 {
     if (regime_has_2_ranges(mmu_idx)) {
@@ -9703,7 +9713,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
 {
     uint64_t tcr = regime_tcr(env, mmu_idx);
     bool epd, hpd, tsz_oob, ds, ha, hd, pie = false;
-    bool aie = false;
+    bool aie, mtx = false;
     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
     ARMGranuleSize gran;
     ARMCPU *cpu = env_archcpu(env);
@@ -9740,6 +9750,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
         ds = extract64(tcr, 32, 1);
+        mtx = extract64(tcr, 33, 1) && cpu_isar_feature(aa64_mte4, cpu);
     } else {
         bool e0pd;
 
@@ -9755,6 +9766,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
             sh = extract32(tcr, 12, 2);
             hpd = extract64(tcr, 41, 1);
             e0pd = extract64(tcr, 55, 1);
+            mtx = extract64(tcr, 60, 1) && cpu_isar_feature(aa64_mte4, cpu);
         } else {
             tsz = extract32(tcr, 16, 6);
             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
@@ -9762,6 +9774,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
             sh = extract32(tcr, 28, 2);
             hpd = extract64(tcr, 42, 1);
             e0pd = extract64(tcr, 56, 1);
+            mtx = extract64(tcr, 61, 1) && cpu_isar_feature(aa64_mte4, cpu);
         }
         ps = extract64(tcr, 32, 3);
         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
@@ -9861,6 +9874,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
         .gran = gran,
         .pie = pie,
         .aie = aie,
+        .mtx = mtx,
     };
 }
 
target/arm/internals.h +2 -0
index 31d37b80fb..e3e36300f8 100644
@@ -1395,6 +1395,7 @@ typedef struct ARMVAParameters {
     ARMGranuleSize gran : 2;
     bool pie        : 1;
     bool aie        : 1;
+    bool mtx:1;
 } ARMVAParameters;
 
 /**
@@ -1410,6 +1411,7 @@ ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
                                    ARMMMUIdx mmu_idx, bool data,
                                    bool el1_is_aa32);
 
+int aa64_va_parameter_mtx(uint64_t tcr, ARMMMUIdx mmu_idx);
 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx);
 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx);
target/arm/ptw.c +25 -3
index 9f864fe837..f95034a2a8 100644
@@ -1939,7 +1939,16 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,
      * validation to do here.
      */
     if (inputsize < addrsize) {
-        uint64_t top_bits = sextract64(address, inputsize,
+        /*
+         * If MTX is enabled, bits 56-59 aren't checked for canonicity
+         * during translation, since they will later be checked during
+         * the tag check step.
+         */
+        uint64_t masked_address = address;
+        if (param.mtx) {
+            masked_address = deposit64(address, 56, 4, param.select * 0xf);
+        }
+        uint64_t top_bits = sextract64(masked_address, inputsize,
                                            addrsize - inputsize);
         if (-top_bits != param.select) {
             /* The gap between the two regions is a Translation fault */
@@ -3487,15 +3496,28 @@ static bool get_phys_addr_disabled(CPUARMState *env,
         if (arm_el_is_aa64(env, r_el)) {
             int pamax = arm_pamax(env_archcpu(env));
             uint64_t tcr = env->cp15.tcr_el[r_el];
-            int addrtop, tbi;
+            int addrtop, tbi, mtx;
+            bool bit55;
 
             tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
+            mtx = aa64_va_parameter_mtx(tcr, mmu_idx);
             if (access_type == MMU_INST_FETCH) {
                 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
             }
-            tbi = (tbi >> extract64(address, 55, 1)) & 1;
+            bit55 = extract64(address, 55, 1);
+            tbi = (tbi >> bit55) & 1;
+            mtx = (mtx >> bit55) & 1;
             addrtop = (tbi ? 55 : 63);
 
+            /*
+             * With MTX enabled, bits 56-59 are not checked according to
+             * AArch64.S1DisabledOutput.
+             */
+            if (cpu_isar_feature(aa64_mte4, env_archcpu(env)) && mtx &&
+                access_type != MMU_INST_FETCH) {
+                address = deposit64(address, 56, 4, ((mmu_idx) && bit55) * 0xF);
+            }
+
             if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
                 fi->type = ARMFault_AddressSize;
                 fi->level = 0;
Signature · 1 line
2.52.0
#18 lore raw

RFC PATCH v3 12/12 tests/tcg: add test for MTE_STORE_ONLY

In reply to Gabriel Brookman

Added a test that checks that MTE checks are not performed on loads when MTE_STORE_ONLY is enabled.

---
 tests/tcg/aarch64/Makefile.target |  2 +-
 tests/tcg/aarch64/mte-10.c        | 49 +++++++++++++++++++++++++++++++++++++++
 tests/tcg/aarch64/mte.h           | 12 ++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
tests/tcg/aarch64/Makefile.target +1 -1
index b491cfb5e1..6203ac9b51 100644
@@ -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 mte-9
+AARCH64_TESTS += mte-1 mte-2 mte-3 mte-4 mte-5 mte-6 mte-7 mte-8 mte-9 mte-10
 mte-%: CFLAGS += $(CROSS_CC_HAS_ARMV8_MTE)
 endif
 
tests/tcg/aarch64/mte-10.c new file +49 -0

49 lines not on this page · raw message

tests/tcg/aarch64/mte.h +12 -0
index 17b932f3f1..1d3ebf8696 100644
@@ -51,6 +51,18 @@ static void enable_mte(int tcf)
     }
 }
 
+static void enable_mte_store_only(int tcf)
+{
+    int r = prctl(PR_SET_TAGGED_ADDR_CTRL,
+                  PR_TAGGED_ADDR_ENABLE | PR_MTE_STORE_ONLY | tcf |
+                  (0xfffe << PR_MTE_TAG_SHIFT),
+                  0, 0, 0);
+    if (r < 0) {
+        perror("PR_SET_TAGGED_ADDR_CTRL");
+        exit(2);
+    }
+}
+
 static void * alloc_mte_mem(size_t size) __attribute__((unused));
 static void * alloc_mte_mem(size_t size)
 {
Signature · 1 line
2.52.0
#7 lore raw

RFC PATCH v3 4/12 linux-user: add MTE_STORE_ONLY to prctl

In reply to Gabriel Brookman

Linux-user processes can now control whether MTE_STORE_ONLY is enabled using the prctl syscall.

---
 linux-user/aarch64/mte_user_helper.h |  3 +++
 linux-user/aarch64/target_prctl.h    | 11 +++++++++++
 tests/tcg/aarch64/mte.h              |  3 +++
 3 files changed, 17 insertions(+)
linux-user/aarch64/mte_user_helper.h +3 -0
index 0c53abda22..afd2d6dbda 100644
@@ -20,6 +20,9 @@
 # define PR_MTE_TAG_SHIFT       3
 # define PR_MTE_TAG_MASK        (0xffffUL << PR_MTE_TAG_SHIFT)
 #endif
+#ifndef PR_MTE_STORE_ONLY
+# define PR_MTE_STORE_ONLY      (1UL << 19)
+#endif
 
 /**
  * arm_set_mte_tcf0 - Set TCF0 field in SCTLR_EL1 register
linux-user/aarch64/target_prctl.h +11 -0
index 621be5727f..5207580c1a 100644
@@ -168,6 +168,9 @@ static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2)
     if (cpu_isar_feature(aa64_mte, cpu)) {
         valid_mask |= PR_MTE_TCF_MASK;
         valid_mask |= PR_MTE_TAG_MASK;
+        if (cpu_isar_feature(aa64_mte4, cpu)) {
+            valid_mask |= PR_MTE_STORE_ONLY;
+        }
     }
 
     if (arg2 & ~valid_mask) {
@@ -185,6 +188,14 @@ static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2)
          */
         env->cp15.gcr_el1 =
             deposit64(env->cp15.gcr_el1, 0, 16, ~arg2 >> PR_MTE_TAG_SHIFT);
+
+        /*
+         * If MTE_STORE_ONLY is enabled, set the corresponding sctlr_el1 bit
+         */
+        if (cpu_isar_feature(aa64_mte4, cpu)) {
+            env->cp15.sctlr_el[1] =
+                deposit64(env->cp15.sctlr_el[1], 58, 1, extract64(arg2, 19, 1));
+        }
         arm_rebuild_hflags(env);
     }
     return 0;
tests/tcg/aarch64/mte.h +3 -0
index 0805676b11..17b932f3f1 100644
@@ -20,6 +20,9 @@
 #ifndef PR_TAGGED_ADDR_ENABLE
 # define PR_TAGGED_ADDR_ENABLE    (1UL << 0)
 #endif
+#ifndef PR_MTE_STORE_ONLY
+# define PR_MTE_STORE_ONLY        (1UL << 19)
+#endif
 #ifndef PR_MTE_TCF_SHIFT
 # define PR_MTE_TCF_SHIFT         1
 # define PR_MTE_TCF_NONE          (0UL << PR_MTE_TCF_SHIFT)
Signature · 1 line
2.52.0
#11 lore raw

RFC PATCH v3 6/12 target/arm: add canonical and no-address tag logic

In reply to Gabriel Brookman

This feature causes tag checks to compare logical address tags against their canonical form rather than against allocation tags. Described in the ARM ARM section "Logical Address Tagging".

---
 target/arm/internals.h      | 29 +++++++++++++++++++++++++++++
 target/arm/tcg/mte_helper.c | 20 ++++++++++++++++++++
 2 files changed, 49 insertions(+)
target/arm/internals.h +29 -0
index 9cd4bf74ef..31d37b80fb 100644
@@ -1633,6 +1633,35 @@ static inline bool tcma_check(uint32_t desc, int bit55, int ptr_tag)
     return tcma && match;
 }
 
+/* Return whether or not the second nibble of a VA matches bit 55.  */
+static inline bool tag_is_canonical(int ptr_tag, int bit55)
+{
+    return ((ptr_tag + bit55) & 0xf) == 0;
+}
+
+/* Return true if mtx bits mean that the access is canonically checked.  */
+static inline bool mtx_check(CPUARMState *env, bool bit55)
+{
+    int mmu_idx;
+    uint64_t tcr, mtx_bit;
+
+    /* If mte4 is not implemented, then mtx is by definition not enabled */
+    if (!cpu_isar_feature(aa64_mte4, env_archcpu(env))) {
+        return false;
+    }
+
+    mmu_idx = arm_mmu_idx_el(env, arm_current_el(env));
+    tcr = regime_tcr(env, mmu_idx);
+
+    /*
+     * In two-range regimes, mtx is governed by bit 60 or 61 of TCR, and in
+     * one-range regimes, bit 33 is used.
+     */
+    mtx_bit = regime_has_2_ranges(mmu_idx) ? 60 + bit55 : 33;
+
+    return extract64(tcr, mtx_bit, 1);
+}
+
 /*
  * For TBI, ideally, we would do nothing.  Proper behaviour on fault is
  * for the tag to be present in the FAR_ELx register.  But for user-only
target/arm/tcg/mte_helper.c +20 -0
index f0880991b6..6827d030dd 100644
@@ -825,6 +825,14 @@ static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
         return 1;
     }
 
+    /*
+     * If mtx is enabled, then the access is MemTag_CanonicallyTagged,
+     * otherwise it is MemTag_AllocationTagged. See AArch64.CheckTag.
+     */
+    if (mtx_check(env, bit55)) {
+        return tag_is_canonical(ptr_tag, bit55);
+    }
+
     mmu_idx = FIELD_EX32(desc, MTEDESC, MIDX);
     type = FIELD_EX32(desc, MTEDESC, WRITE) ? MMU_DATA_STORE : MMU_DATA_LOAD;
     sizem1 = FIELD_EX32(desc, MTEDESC, SIZEM1);
@@ -961,6 +969,18 @@ uint64_t HELPER(mte_check_zva)(CPUARMState *env, uint32_t desc, uint64_t ptr)
         goto done;
     }
 
+    /*
+     * If mtx is enabled, then the access is MemTag_CanonicallyTagged,
+     * otherwise it is MemTag_AllocationTagged. See AArch64.CheckTag.
+     */
+    if (mtx_check(env, bit55)) {
+        if (tag_is_canonical(ptr_tag, bit55)) {
+            goto done;
+        }
+        mte_check_fail(env, desc, ptr, ra);
+    }
+
+
     /*
      * In arm_cpu_realizefn, we asserted that dcz > LOG2_TAG_GRANULE+1,
      * i.e. 32 bytes, which is an unreasonably small dcz anyway, to make
Signature · 1 line
2.52.0
#1 lore raw

RFC PATCH v3 1/12 target/arm: implement MTE_PERM

In reply to Gabriel Brookman

Introduces a new stage 2 memory attribute, NoTagAccess, that raises a stage 2 data abort on a tag check, tag read, or tag write.

---
 target/arm/cpu-features.h   |  5 +++++
 target/arm/ptw.c            | 17 ++++++++++++++++-
 target/arm/tcg/mte_helper.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)
target/arm/cpu-features.h +5 -0
index c86a4e667d..48009b5a66 100644
@@ -1139,6 +1139,11 @@ static inline bool isar_feature_aa64_mte3(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, MTE) >= 3;
 }
 
+static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) == 1;
+}
+
 static inline bool isar_feature_aa64_sme(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, SME) != 0;
target/arm/ptw.c +16 -1
index 2e6b149b2d..9f864fe837 100644
@@ -3393,7 +3393,7 @@ static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
                                         ARMCacheAttrs s1, ARMCacheAttrs s2)
 {
     ARMCacheAttrs ret;
-    bool tagged = false;
+    bool tagged, notagaccess = false;
 
     assert(!s1.is_s2_format);
     ret.is_s2_format = false;
@@ -3403,6 +3403,18 @@ static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
         s1.attrs = 0xff;
     }
 
+    if (hcr & HCR_FWB) {
+        if (s2.attrs >= 0xe) {
+            notagaccess = true;
+            s2.attrs = 0x7;
+        }
+    } else {
+        if (s2.attrs == 0x4) {
+            notagaccess = true;
+            s2.attrs = 0xf;
+        }
+    }
+
     /* Combine shareability attributes (table D4-43) */
     if (s1.shareability == 2 || s2.shareability == 2) {
         /* if either are outer-shareable, the result is outer-shareable */
@@ -3437,6 +3449,9 @@ static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
     /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
     if (tagged && ret.attrs == 0xff) {
         ret.attrs = 0xf0;
+        if (notagaccess) {
+            ret.attrs = 0xe0;
+        }
     }
 
     return ret;
target/arm/tcg/mte_helper.c +31 -0
index bb48fe359b..942bd4103d 100644
@@ -57,6 +57,28 @@ static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude)
     return tag;
 }
 
+#ifdef CONFIG_USER_ONLY
+#else
+/*
+ * Constructs S2 Permission Fault as described in ARM ARM "Stage 2 Memory
+ * Tagging Attributes".
+ */
+static void mte_perm_check_fail(CPUARMState *env, uint64_t dirty_ptr,
+                                uintptr_t ra, bool is_write)
+{
+    uint64_t syn;
+
+    env->exception.vaddress = dirty_ptr;
+
+    syn = syn_data_abort_no_iss(0, 0, 0, 0, 0, is_write, 0);
+
+    syn |= BIT_ULL(42); /* TnD is bit 42 */
+
+    raise_exception_ra(env, EXCP_DATA_ABORT, syn, 2, ra);
+    g_assert_not_reached();
+}
+#endif
+
 uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
                                   uint64_t ptr, MMUAccessType ptr_access,
                                   int ptr_size, MMUAccessType tag_access,
@@ -116,6 +138,15 @@ uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
     }
     assert(!(flags & TLB_INVALID_MASK));
 
+    /*
+     * If the virtual page MemAttr == Tagged NoTagAccess, throw S2 permission
+     * fault (conditional on mteperm being implemented and RA != 0).
+     */
+    if (ra && cpu_isar_feature(aa64_mteperm, env_archcpu(env))
+        && full->extra.arm.pte_attrs == 0xe0) {
+        mte_perm_check_fail(env, ptr, ra, tag_access == 1);
+    }
+
     /* If the virtual page MemAttr != Tagged, access unchecked. */
     if (full->extra.arm.pte_attrs != 0xf0) {
         return NULL;
Signature · 1 line
2.52.0
#13 lore raw

RFC PATCH v3 8/12 target/arm: storing to canonical tags faults

In reply to Gabriel Brookman

According to ARM ARM, section "Memory region tagging types", tag-store instructions targeting canonically tagged regions cause a stage 1 permission fault.

---
 target/arm/tcg/mte_helper.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
target/arm/tcg/mte_helper.c +44 -0
index 795a5ad20b..8f06ed3162 100644
@@ -227,6 +227,20 @@ uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
 #endif
 }
 
+static void canonical_tag_write_fail(CPUARMState *env,
+                                uint64_t dirty_ptr, uintptr_t ra)
+{
+    uint64_t syn;
+
+    env->exception.vaddress = dirty_ptr;
+
+    syn = syn_data_abort_no_iss(arm_current_el(env) != 0, 0, 0, 0, 0, 1, 0);
+    syn |= BIT_ULL(42); /* TnD is bit 42 */
+
+    raise_exception_ra(env, EXCP_DATA_ABORT, syn, exception_target_el(env), ra);
+    g_assert_not_reached();
+}
+
 static uint8_t *allocation_tag_mem(CPUARMState *env, int ptr_mmu_idx,
                                    uint64_t ptr, MMUAccessType ptr_access,
                                    int ptr_size, MMUAccessType tag_access,
@@ -363,6 +377,11 @@ static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt,
     int mmu_idx = arm_env_mmu_index(env);
     uint8_t *mem;
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     check_tag_aligned(env, ptr, ra);
 
     /* Trap if accessing an invalid page.  */
@@ -390,6 +409,11 @@ void HELPER(stg_stub)(CPUARMState *env, uint64_t ptr)
     int mmu_idx = arm_env_mmu_index(env);
     uintptr_t ra = GETPC();
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     check_tag_aligned(env, ptr, ra);
     probe_write(env, ptr, TAG_GRANULE, mmu_idx, ra);
 }
@@ -401,6 +425,11 @@ static inline void do_st2g(CPUARMState *env, uint64_t ptr, uint64_t xt,
     int tag = allocation_tag_from_addr(xt);
     uint8_t *mem1, *mem2;
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     check_tag_aligned(env, ptr, ra);
 
     /*
@@ -449,6 +478,11 @@ void HELPER(st2g_stub)(CPUARMState *env, uint64_t ptr)
     uintptr_t ra = GETPC();
     int in_page = -(ptr | TARGET_PAGE_MASK);
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     check_tag_aligned(env, ptr, ra);
 
     if (likely(in_page >= 2 * TAG_GRANULE)) {
@@ -548,6 +582,11 @@ void HELPER(stgm)(CPUARMState *env, uint64_t ptr, uint64_t val)
 
     ptr = QEMU_ALIGN_DOWN(ptr, gm_bs_bytes);
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     /* Trap if accessing an invalid page.  */
     tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_STORE,
                                  gm_bs_bytes, MMU_DATA_LOAD, ra);
@@ -594,6 +633,11 @@ void HELPER(stzgm_tags)(CPUARMState *env, uint64_t ptr, uint64_t val)
     intptr_t dcz_bytes, tag_bytes;
     uint8_t *mem;
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
     /*
      * In arm_cpu_realizefn, we assert that dcz > LOG2_TAG_GRANULE+1,
      * i.e. 32 bytes, which is an unreasonably small dcz anyway,
Signature · 1 line
2.52.0
#9 lore raw

RFC PATCH v3 5/12 target/arm: removed TBI bits from MTE check logic

In reply to Gabriel Brookman

Previously, the TBI bit was used to mediate whether MTE checks happened. This dependency isn't described in the ARM ARM. See D10.4.1, Tag Checked Memory Accesses, specifically.

Decoupling tag checking from TBI is required to correctly implement canonical tag checking, which must function even when TBI is disabled.

---
 target/arm/tcg/helper-a64.c |  9 +--------
 target/arm/tcg/hflags.c     | 11 ++++-------
 target/arm/tcg/mte_helper.c | 10 ----------
 3 files changed, 5 insertions(+), 25 deletions(-)
target/arm/tcg/helper-a64.c +1 -8
index ba1d775d81..f64025925d 100644
@@ -1050,20 +1050,13 @@ static int mops_sizereg(uint32_t syndrome)
 }
 
 /*
- * Return true if TCMA and TBI bits mean we need to do MTE checks.
+ * Return true if the TCMA bit means we need to do MTE checks.
  * We only need to do this once per MOPS insn, not for every page.
  */
 static bool mte_checks_needed(uint64_t ptr, uint32_t desc)
 {
     int bit55 = extract64(ptr, 55, 1);
 
-    /*
-     * Note that tbi_check() returns true for "access checked" but
-     * tcma_check() returns true for "access unchecked".
-     */
-    if (!tbi_check(desc, bit55)) {
-        return false;
-    }
     return !tcma_check(desc, bit55, allocation_tag_from_addr(ptr));
 }
 
target/arm/tcg/hflags.c +4 -7
index c4696af5d8..80d7ea9349 100644
@@ -405,15 +405,13 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
         /*
          * Set MTE_ACTIVE if any access may be Checked, and leave clear
          * if all accesses must be Unchecked:
-         * 1) If no TBI, then there are no tags in the address to check,
-         * 2) If Tag Check Override, then all accesses are Unchecked,
-         * 3) If Tag Check Fail == 0, then Checked access have no effect,
-         * 4) If no Allocation Tag Access, then all accesses are Unchecked.
+         * 1) If Tag Check Override, then all accesses are Unchecked,
+         * 2) If Tag Check Fail == 0, then Checked access have no effect,
+         * 3) If no Allocation Tag Access, then all accesses are Unchecked.
          */
         if (allocation_tag_access_enabled(env, el, sctlr)) {
             DP_TBFLAG_A64(flags, ATA, 1);
-            if (tbid
-                && !(env->pstate & PSTATE_TCO)
+            if (!(env->pstate & PSTATE_TCO)
                 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
                 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
                 if (!EX_TBFLAG_A64(flags, UNPRIV)) {
@@ -439,7 +437,6 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
         }
         /* And again for unprivileged accesses, if required.  */
         if (EX_TBFLAG_A64(flags, UNPRIV)
-            && tbid
             && !(env->pstate & PSTATE_TCO)
             && (sctlr & SCTLR_TCF0)
             && allocation_tag_access_enabled(env, 0, sctlr)) {
target/arm/tcg/mte_helper.c +0 -10
index 942bd4103d..f0880991b6 100644
@@ -819,11 +819,6 @@ static int mte_probe_int(CPUARMState *env, uint32_t desc, uint64_t ptr,
     bit55 = extract64(ptr, 55, 1);
     *fault = ptr;
 
-    /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
-    if (unlikely(!tbi_check(desc, bit55))) {
-        return -1;
-    }
-
     ptr_tag = allocation_tag_from_addr(ptr);
 
     if (tcma_check(desc, bit55, ptr_tag)) {
@@ -960,11 +955,6 @@ uint64_t HELPER(mte_check_zva)(CPUARMState *env, uint32_t desc, uint64_t ptr)
 
     bit55 = extract64(ptr, 55, 1);
 
-    /* If TBI is disabled, the access is unchecked, and ptr is not dirty. */
-    if (unlikely(!tbi_check(desc, bit55))) {
-        return ptr;
-    }
-
     ptr_tag = allocation_tag_from_addr(ptr);
 
     if (tcma_check(desc, bit55, ptr_tag)) {
Signature · 1 line
2.52.0
#12 lore raw

RFC PATCH v3 7/12 target/arm: ldg on canonical tag loads the tag

In reply to Gabriel Brookman

According to ARM ARM, section "Memory Tagging Region Types", loading tags from canonically tagged regions should use the canonical tags, not allocation tags.

---
 target/arm/tcg/mte_helper.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
target/arm/tcg/mte_helper.c +29 -6
index 6827d030dd..795a5ad20b 100644
@@ -310,9 +310,14 @@ uint64_t HELPER(ldg)(CPUARMState *env, uint64_t ptr, uint64_t xt)
     mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD, 1,
                              MMU_DATA_LOAD, GETPC());
 
-    /* Load if page supports tags. */
+    /* Load if page supports tags. Set to canonical value if MTX is set. */
     if (mem) {
-        rtag = load_tag1(ptr, mem);
+        uint64_t bit55 = extract64(ptr, 55, 1);
+        if (mtx_check(env, bit55)) {
+            rtag = 0xF * bit55;
+        } else {
+            rtag = load_tag1(ptr, mem);
+        }
     }
 
     return address_with_allocation_tag(xt, rtag);
@@ -463,8 +468,10 @@ uint64_t HELPER(ldgm)(CPUARMState *env, uint64_t ptr)
     void *tag_mem;
     uint64_t ret;
     int shift;
+    bool bit55;
 
     ptr = QEMU_ALIGN_DOWN(ptr, gm_bs_bytes);
+    bit55 = extract64(ptr, 55, 1);
 
     /* Trap if accessing an invalid page.  */
     tag_mem = allocation_tag_mem(env, mmu_idx, ptr, MMU_DATA_LOAD,
@@ -490,19 +497,35 @@ uint64_t HELPER(ldgm)(CPUARMState *env, uint64_t ptr)
     switch (gm_bs) {
     case 3:
         /* 32 bytes -> 2 tags -> 8 result bits */
-        ret = *(uint8_t *)tag_mem;
+        if (mtx_check(env, bit55)) {
+            ret = -(uint8_t)bit55;
+        } else {
+            ret = *(uint8_t *)tag_mem;
+        }
         break;
     case 4:
         /* 64 bytes -> 4 tags -> 16 result bits */
-        ret = cpu_to_le16(*(uint16_t *)tag_mem);
+        if (mtx_check(env, bit55)) {
+            ret = -(uint16_t)bit55;
+        } else {
+            ret = cpu_to_le16(*(uint16_t *)tag_mem);
+        }
         break;
     case 5:
         /* 128 bytes -> 8 tags -> 32 result bits */
-        ret = cpu_to_le32(*(uint32_t *)tag_mem);
+        if (mtx_check(env, bit55)) {
+            ret = -(uint32_t)bit55;
+        } else {
+            ret = cpu_to_le32(*(uint32_t *)tag_mem);
+        }
         break;
     case 6:
         /* 256 bytes -> 16 tags -> 64 result bits */
-        return cpu_to_le64(*(uint64_t *)tag_mem);
+        if (mtx_check(env, bit55)) {
+            return -(uint64_t)bit55;
+        } else {
+            return cpu_to_le64(*(uint64_t *)tag_mem);
+        }
     default:
         /*
          * CPU configured with unsupported/invalid gm blocksize.
Signature · 1 line
2.52.0
#16 lore raw

RFC PATCH v3 10/12 docs: add MTE4 features to docs

In reply to Gabriel Brookman

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

---
 docs/system/arm/emulation.rst | 5 +++++
 target/arm/tcg/cpu64.c        | 8 ++++++++
 2 files changed, 13 insertions(+)
docs/system/arm/emulation.rst +5 -0
index 31a5878a8f..12662189fc 100644
@@ -106,6 +106,11 @@ 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_PERM (NoTagAccess memory attribute)
+- FEAT_MTE_TAGGED_FAR (Full address reporting of Tag Check Fault)
+- FEAT_MTE_STORE_ONLY (Store-only tag checking)
+- FEAT_MTE_CANONICAL_TAGS (Canonical tag checking)
+- FEAT_MTE_NO_ADDRESS_TAGS (Address tagging disabled)
 - FEAT_NMI (Non-maskable Interrupt)
 - FEAT_NV (Nested Virtualization)
 - FEAT_NV2 (Enhanced nested virtualization support)
target/arm/tcg/cpu64.c +8 -0
index 917db5bb09..4ba126f4f8 100644
@@ -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, 1);   /* 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, 1);    /* FEAT_MTE_TAGGED_FAR */
+    t = FIELD_DP64(t, ID_AA64PFR2, MTESTOREONLY, 1);   /* FEAT_MTE_STORE_ONLY */
+    t = FIELD_DP64(t, ID_AA64PFR2, MTEPERM, 1);    /* 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 */
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH RFC v3 01/12] target/arm: implement MTE_PERM

In reply to Gabriel Brookman
Quoted 15 lines · Gabriel Brookman

Introduces a new stage 2 memory attribute, NoTagAccess, that raises a stage 2 data abort on a tag check, tag read, or tag write.

---
  target/arm/cpu-features.h   |  5 +++++
  target/arm/ptw.c            | 17 ++++++++++++++++-
  target/arm/tcg/mte_helper.c | 31 +++++++++++++++++++++++++++++++
  3 files changed, 52 insertions(+), 1 deletion(-)
target/arm/cpu-features.h +0 -0
index c86a4e667d..48009b5a66 100644

target/arm/cpu-features.h static inline bool isar_feature_aa64_mte3(const ARMISARegisters *id) · line 1145

Quoted 7 lines
target/arm/cpu-features.h +4 -0
@@ -1139,6 +1139,11 @@ static inline bool isar_feature_aa64_mte3(const ARMISARegisters *id)
      return FIELD_EX64_IDREG(id, ID_AA64PFR1, MTE) >= 3;
  }
 
+static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) == 1;
+}

Per the Arm ARM re future expansion, this should be true for any non-zero value. See "D24.1.3 Principles of the ID scheme for fields in ID registers".

Quoted 20 lines
target/arm/cpu-features.h +12 -0
@@ -3403,6 +3403,18 @@ static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
          s1.attrs = 0xff;
      }
 
+    if (hcr & HCR_FWB) {
+        if (s2.attrs >= 0xe) {
+            notagaccess = true;
+            s2.attrs = 0x7;
+        }
+    } else {
+        if (s2.attrs == 0x4) {
+            notagaccess = true;
+            s2.attrs = 0xf;
+        }
+    }
+
      /* Combine shareability attributes (table D4-43) */
      if (s1.shareability == 2 || s2.shareability == 2) {
          /* if either are outer-shareable, the result is outer-shareable */

target/arm/cpu-features.h static ARMCacheAttrs combine_cacheattrs(uint64_t hcr, · line 3455

Quoted 7 lines
target/arm/cpu-features.h +3 -0
@@ -3437,6 +3449,9 @@ static ARMCacheAttrs combine_cacheattrs(uint64_t hcr,
      /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
      if (tagged && ret.attrs == 0xff) {
          ret.attrs = 0xf0;
+        if (notagaccess) {
+            ret.attrs = 0xe0;
+        }
      }

Better as

    ret.attrs = notagaccess ? 0xe0 : 0xf0;

Given that this hunk corresponds to J1.3.5.2 EncodePARAttrs, I think it would be worth a comment that FEAT_MTE_PERM is checked in allocation_tag_mem_probe and that the attr encoding is otherwise RESERVED.

target/arm/tcg/mte_helper.c static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude) · line 61

Quoted 10 lines
target/arm/tcg/mte_helper.c +2 -0
index bb48fe359b..942bd4103d 100644
@@ -57,6 +57,28 @@ static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude)
      return tag;
  }
 
+#ifdef CONFIG_USER_ONLY
+#else

#ifndef

Quoted 4 lines
target/arm/tcg/mte_helper.c +4 -0
+/*
+ * Constructs S2 Permission Fault as described in ARM ARM "Stage 2 Memory
+ * Tagging Attributes".
+ */

target/arm/tcg/mte_helper.c static int choose_nonexcluded_tag(int tag, int offset, uint16_t exclude) · line 75

Quoted 10 lines
target/arm/tcg/mte_helper.c +10 -0
+static void mte_perm_check_fail(CPUARMState *env, uint64_t dirty_ptr,
+                                uintptr_t ra, bool is_write)
+{
+    uint64_t syn;
+
+    env->exception.vaddress = dirty_ptr;
+
+    syn = syn_data_abort_no_iss(0, 0, 0, 0, 0, is_write, 0);
+
+    syn |= BIT_ULL(42); /* TnD is bit 42 */

Surely (32 + 9) == 41 for the TagAccess bit.

Quoted 16 lines
target/arm/tcg/mte_helper.c +8 -0
+
+    raise_exception_ra(env, EXCP_DATA_ABORT, syn, 2, ra);
+    g_assert_not_reached();
+}
+#endif
+
  uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
                                    uint64_t ptr, MMUAccessType ptr_access,
                                    int ptr_size, MMUAccessType tag_access,
@@ -116,6 +138,15 @@ uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
      }
      assert(!(flags & TLB_INVALID_MASK));
 
+    /*
+     * If the virtual page MemAttr == Tagged NoTagAccess, throw S2 permission

target/arm/tcg/mte_helper.c uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx, · line 152

Quoted 10 lines
target/arm/tcg/mte_helper.c +7 -0
+     * fault (conditional on mteperm being implemented and RA != 0).
+     */
+    if (ra && cpu_isar_feature(aa64_mteperm, env_archcpu(env))
+        && full->extra.arm.pte_attrs == 0xe0) {
+        mte_perm_check_fail(env, ptr, ra, tag_access == 1);
+    }
+
      /* If the virtual page MemAttr != Tagged, access unchecked. */
      if (full->extra.arm.pte_attrs != 0xf0) {
          return NULL;

r~

#4 lore raw

Re: [PATCH RFC v3 02/12] target/arm: add TCSO bitmasks to SCTLR

In reply to Gabriel Brookman
Quoted 16 lines · 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 or MTE4 is disabled, as per convention.

---
  target/arm/cpu-features.h |  5 +++++
  target/arm/cpu.h          |  2 ++
  target/arm/helper.c       | 11 +++++++++--
  3 files changed, 16 insertions(+), 2 deletions(-)
target/arm/cpu-features.h +0 -0
index 48009b5a66..3473787ab8 100644

target/arm/cpu-features.h static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id) · line 1150

Quoted 7 lines
target/arm/cpu-features.h +4 -0
@@ -1144,6 +1144,11 @@ static inline bool isar_feature_aa64_mteperm(const ARMISARegisters *id)
      return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEPERM) == 1;
  }
 
+static inline bool isar_feature_aa64_mte4(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR2, MTEFAR) == 1;
+}

There is no ID register field for FEAT_MTE4.

Quoted 12 lines
target/arm/helper.c +1 -1
index 263ca29d92..4086423b6f 100644
@@ -3364,10 +3364,17 @@ 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_TCSO);
          } else {
              value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |

target/arm/helper.c static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 3378

Quoted 10 lines
target/arm/helper.c +8 -1
-                       SCTLR_ATA0 | SCTLR_ATA);
+                       SCTLR_ATA0 | SCTLR_ATA | SCTLR_TCSO | SCTLR_TCSO0);
+        }
+    } else if (ri->state == ARM_CP_STATE_AA64
+            && !cpu_isar_feature(aa64_mte4, cpu)) { /* mte but not mte4 */
+        if (ri->opc1 == 6) { /* SCTLR_EL3 */
+            value &= ~SCTLR_TCSO;
+        } else {
+            value &= ~(SCTLR_TCSO | SCTLR_TCSO0);
          }

(1) Better to refactor to eliminate duplicate checks.
(2) These bits are explicitly vs FEAT_MTE_STORE_ONLY not FEAT_MTE4.

Thus

    if (ri->state == ARM_CP_STATE_AA64) {
        if (!cpu_isar_feature(aa64_mte, cpu)) {
            ...
        } else if (!cpu_isar_feature(aa64_mte_store_only, cpu)) {
            ...
        }
    }

r~

#6 lore raw

Re: [PATCH RFC v3 03/12] target/arm: mte_check unemitted on STORE_ONLY load

In reply to Gabriel Brookman
Quoted 16 lines · Gabriel Brookman

This feature disables generation of the mte check helper on loads when STORE_ONLY tag checking mode is enabled.

---
  target/arm/cpu.h               |  2 ++
  target/arm/tcg/hflags.c        | 14 ++++++++++++++
  target/arm/tcg/translate-a64.c |  8 ++++++--
  target/arm/tcg/translate.h     |  2 ++
  4 files changed, 24 insertions(+), 2 deletions(-)
target/arm/cpu.h +0 -0
index 393bfc0dc9..4087484faf 100644

target/arm/cpu.h FIELD(TBFLAG_A64, ZT0EXC_EL, 39, 2) · line 2500

Quoted 5 lines
target/arm/cpu.h +2 -0
@@ -2496,6 +2496,8 @@ FIELD(TBFLAG_A64, ZT0EXC_EL, 39, 2)
  FIELD(TBFLAG_A64, GCS_EN, 41, 1)
  FIELD(TBFLAG_A64, GCS_RVCEN, 42, 1)
  FIELD(TBFLAG_A64, GCSSTR_EL, 43, 2)
+FIELD(TBFLAG_A64, MTE_STORE_ONLY, 45, 1)
+FIELD(TBFLAG_A64, MTE0_STORE_ONLY, 46, 1)

Skipping bit 44?

Quoted 22 lines
target/arm/tcg/hflags.c +10 -0
index 5c9b9bec3b..c4696af5d8 100644
@@ -425,6 +425,16 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
                       */
                      DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
                  }
+                /*
+                 * Repeat for MTE_STORE_ONLY
+                 */
+                if (cpu_isar_feature(aa64_mte4, env_archcpu(env)) &&
+                    ((el == 0 ? SCTLR_TCSO0 : SCTLR_TCSO) & sctlr)) {
+                    DP_TBFLAG_A64(flags, MTE_STORE_ONLY, 1);
+                    if (!EX_TBFLAG_A64(flags, UNPRIV)) {
+                        DP_TBFLAG_A64(flags, MTE0_STORE_ONLY, 1);
+                    }
+                }
              }
          }
          /* And again for unprivileged accesses, if required.  */

target/arm/tcg/hflags.c static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el, · line 451

Quoted 8 lines
target/arm/tcg/hflags.c +4 -0
@@ -434,6 +444,10 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
              && (sctlr & SCTLR_TCF0)
              && allocation_tag_access_enabled(env, 0, sctlr)) {
              DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
+            if (cpu_isar_feature(aa64_mte4, env_archcpu(env)) &&
+                (SCTLR_TCSO0 & sctlr)) {
+                DP_TBFLAG_A64(flags, MTE0_STORE_ONLY, 1);
+            }
          }

These two hunks don't need to check cpu_isar_feature, because we already did that when filtering the valid SCTLR bits.

r~

#8 lore raw

Re: [PATCH RFC v3 04/12] linux-user: add MTE_STORE_ONLY to prctl

In reply to Gabriel Brookman
Quoted 40 lines · Gabriel Brookman

Linux-user processes can now control whether MTE_STORE_ONLY is enabled using the prctl syscall.

---
  linux-user/aarch64/mte_user_helper.h |  3 +++
  linux-user/aarch64/target_prctl.h    | 11 +++++++++++
  tests/tcg/aarch64/mte.h              |  3 +++
  3 files changed, 17 insertions(+)
linux-user/aarch64/mte_user_helper.h +3 -0
index 0c53abda22..afd2d6dbda 100644
@@ -20,6 +20,9 @@
  # define PR_MTE_TAG_SHIFT       3
  # define PR_MTE_TAG_MASK        (0xffffUL << PR_MTE_TAG_SHIFT)
  #endif
+#ifndef PR_MTE_STORE_ONLY
+# define PR_MTE_STORE_ONLY      (1UL << 19)
+#endif
 
  /**
   * arm_set_mte_tcf0 - Set TCF0 field in SCTLR_EL1 register
linux-user/aarch64/target_prctl.h +3 -0
index 621be5727f..5207580c1a 100644
@@ -168,6 +168,9 @@ static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2)
      if (cpu_isar_feature(aa64_mte, cpu)) {
          valid_mask |= PR_MTE_TCF_MASK;
          valid_mask |= PR_MTE_TAG_MASK;
+        if (cpu_isar_feature(aa64_mte4, cpu)) {
+            valid_mask |= PR_MTE_STORE_ONLY;
+        }
      }
 
      if (arg2 & ~valid_mask) {
@@ -185,6 +188,14 @@ static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2)
           */

linux-user/aarch64/target_prctl.h static abi_long do_prctl_set_tagged_addr_ctrl(CPUArchState *env, abi_long arg2) · line 198

Quoted 10 lines
linux-user/aarch64/target_prctl.h +8 -0
          env->cp15.gcr_el1 =
              deposit64(env->cp15.gcr_el1, 0, 16, ~arg2 >> PR_MTE_TAG_SHIFT);
+
+        /*
+         * If MTE_STORE_ONLY is enabled, set the corresponding sctlr_el1 bit
+         */
+        if (cpu_isar_feature(aa64_mte4, cpu)) {
+            env->cp15.sctlr_el[1] =
+                deposit64(env->cp15.sctlr_el[1], 58, 1, extract64(arg2, 19, 1));
+        }

Let's not hard-code bit numbers. There's also no need to re-check cpu_isar_feature.

So more like

	if (arg2 & PR_MTE_STORE_ONLY) {
	    env->cp15.sctlr_el[1] |= SCTLR_TCSO0;
	} else {
	    env->cp15.sctlr_el[1] &= ~SCTLR_TCSO0;
	}

In addition, should be done in arm_set_mte_tcf0, which needs to be expanded to match linux's set_tagged_addr_ctrl. This code is common to the prctl and to gdbstub. The function should be renamed at the same time, since it's no longer just tcf0.

r~

#10 lore raw

Re: [PATCH RFC v3 05/12] target/arm: removed TBI bits from MTE check logic

In reply to Gabriel Brookman
Quoted 3 lines · Gabriel Brookman

Previously, the TBI bit was used to mediate whether MTE checks happened. This dependency isn't described in the ARM ARM. See D10.4.1, Tag Checked Memory Accesses, specifically.

I disagree.

See R_DRGYL: "Logical Address Tagging is disabled for the VA range being accessed", and R_TQHWL: "TCR_ELx.TBIn".

I can agree that you might want to factor this logical address tagging enabled test, since with FEAT_MTE_NO_ADDRESS_TAGS it then includes TCR_ELx.MTXn.

r~

#14 lore raw

Re: [PATCH RFC v3 08/12] target/arm: storing to canonical tags faults

In reply to Gabriel Brookman
Quoted 35 lines · Gabriel Brookman

According to ARM ARM, section "Memory region tagging types", tag-store instructions targeting canonically tagged regions cause a stage 1 permission fault.

---
  target/arm/tcg/mte_helper.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 44 insertions(+)
target/arm/tcg/mte_helper.c +14 -0
index 795a5ad20b..8f06ed3162 100644
@@ -227,6 +227,20 @@ uint8_t *allocation_tag_mem_probe(CPUARMState *env, int ptr_mmu_idx,
  #endif
  }
 
+static void canonical_tag_write_fail(CPUARMState *env,
+                                uint64_t dirty_ptr, uintptr_t ra)
+{
+    uint64_t syn;
+
+    env->exception.vaddress = dirty_ptr;
+
+    syn = syn_data_abort_no_iss(arm_current_el(env) != 0, 0, 0, 0, 0, 1, 0);
+    syn |= BIT_ULL(42); /* TnD is bit 42 */
+
+    raise_exception_ra(env, EXCP_DATA_ABORT, syn, exception_target_el(env), ra);
+    g_assert_not_reached();
+}
+
  static uint8_t *allocation_tag_mem(CPUARMState *env, int ptr_mmu_idx,
                                     uint64_t ptr, MMUAccessType ptr_access,
                                     int ptr_size, MMUAccessType tag_access,

target/arm/tcg/mte_helper.c static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt, · line 385

Quoted 9 lines
target/arm/tcg/mte_helper.c +5 -0
@@ -363,6 +377,11 @@ static inline void do_stg(CPUARMState *env, uint64_t ptr, uint64_t xt,
      int mmu_idx = arm_env_mmu_index(env);
      uint8_t *mem;
 
+    if (mtx_check(env, 1 & (ptr >> 55))) {
+        canonical_tag_write_fail(env, ptr, ra);
+        return;
+    }
+
      check_tag_aligned(env, ptr, ra);

These are mis-placed, as the alignment check has priority, as does pte_attrs == 0xf0. Compare S1DecodeMemAttrs vs allocation_tag_mem_probe.

r~

Reviewed-by on v3 12/12
#19 lore raw

Re: [PATCH RFC v3 12/12] tests/tcg: add test for MTE_STORE_ONLY

In reply to Gabriel Brookman

tests/tcg/aarch64/mte.h static void enable_mte(int tcf) · line 59

Quoted 12 lines · Gabriel Brookman
tests/tcg/aarch64/mte.h +6 -0
@@ -51,6 +51,18 @@ static void enable_mte(int tcf)
      }
  }
 
+static void enable_mte_store_only(int tcf)
+{
+    int r = prctl(PR_SET_TAGGED_ADDR_CTRL,
+                  PR_TAGGED_ADDR_ENABLE | PR_MTE_STORE_ONLY | tcf |
+                  (0xfffe << PR_MTE_TAG_SHIFT),
+                  0, 0, 0);

Just rename the parameter for enable_mte() to 'flags' and use 'PR_MTE_TCF_SYNC | PR_MTE_STORE_ONLY' in the new test.

With that,

r~