Skip to contentSkip to conversation

target/arm: Implement FEAT_FGWTE3

PATCH In review Richard Henderson 2 participants 16 messages Jul 24 – Aug 1

lore
Share & tools
lore
#0 lore raw

cover target/arm: Implement FEAT_FGWTE3

Similar but different to FEAT_FGT.

The EL3 version traps writes to specific EL3 registers from EL3, and one cannot disable the trapping once enabled. The idea being that after firmware boots, system settings are not expected to change for any reason.

r~

Richard Henderson (6):
target/arm: Add FGWTE3_EL3
target/arm: Add FGWTE3_EL3 bit definitions
target/arm: Add FGWTE3 FGT bit definitions
target/arm: Implement FGWTE3 traps
target/arm: Set fgt in cpregs for FGWTE3
target/arm: Enable FEAT_FGWTE3 for -cpu max

 target/arm/cpregs.h            | 56 +++++++++++++++++++++++++++++
 target/arm/cpu-features.h      |  5 +++
 target/arm/cpu.h               |  2 +-
 target/arm/cpregs-gcs.c        |  5 +--
 target/arm/helper.c            | 66 ++++++++++++++++++++++------------
 target/arm/tcg/cpu64.c         |  1 +
 target/arm/tcg/op_helper.c     | 36 ++++++++++++-------
 target/arm/tcg/translate-a64.c | 13 ++++++-
 docs/system/arm/emulation.rst  |  1 +
 9 files changed, 146 insertions(+), 39 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/6 target/arm: Add FGWTE3_EL3

---
 target/arm/cpregs.h       |  1 +
 target/arm/cpu-features.h |  5 +++++
 target/arm/cpu.h          |  2 +-
 target/arm/helper.c       | 19 +++++++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)
target/arm/cpregs.h +1 -0
index 391c0e322b..34592679e4 100644
@@ -385,6 +385,7 @@ typedef enum CPAccessResult {
 /* Indexes into fgt_write[] */
 #define FGTREG_HFGWTR 0
 #define FGTREG_HDFGWTR 1
+#define FGTREG_FGWTE3 2
 /* Indexes into fgt_exec[] */
 #define FGTREG_HFGITR 0
 
target/arm/cpu-features.h +5 -0
index fb5ed25ad0..75f0fc4169 100644
@@ -1467,6 +1467,11 @@ static inline bool isar_feature_aa64_asid2(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64MMFR4, ASID2) != 0;
 }
 
+static inline bool isar_feature_aa64_fgwte3(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64MMFR4, FGWTE3) != 0;
+}
+
 /*
  * Note the E2H0 ID fields is signed, increasingly negative as more
  * isn't implemented.
target/arm/cpu.h +1 -1
index 03a30afcbe..ca75e379eb 100644
@@ -553,7 +553,7 @@ typedef struct CPUArchState {
          * FEAT_FGT2 will add more elements to these arrays.
          */
         uint64_t fgt_read[2]; /* HFGRTR, HDFGRTR */
-        uint64_t fgt_write[2]; /* HFGWTR, HDFGWTR */
+        uint64_t fgt_write[3]; /* HFGWTR, HDFGWTR, FGWTE3 */
         uint64_t fgt_exec[1]; /* HFGITR */
 
         /* RME registers */
target/arm/helper.c +19 -0
index af45234ad2..d410a9a1ec 100644
@@ -6331,6 +6331,21 @@ static const ARMCPRegInfo fpmr_reginfo[] = {
     }
 };
 
+static void fgwte3_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                         uint64_t value)
+{
+    env->cp15.fgt_write[FGTREG_FGWTE3] |= value;
+}
+
+static const ARMCPRegInfo fgwte3_reginfo[] = {
+    { .name = "FGWTE3_EL3", .state = ARM_CP_STATE_AA64,
+      .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 5,
+      .access = PL3_RW, .resetvalue = 0,
+      .writefn = fgwte3_write, .raw_writefn = raw_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_FGWTE3])
+    },
+};
+
 void register_cp_regs_for_features(ARMCPU *cpu)
 {
     /* Register all the coprocessor registers based on feature bits */
@@ -7642,6 +7657,10 @@ void register_cp_regs_for_features(ARMCPU *cpu)
         define_arm_cp_regs(cpu, ccsidr2_reginfo);
     }
 
+    if (cpu_isar_feature(aa64_fgwte3, cpu)) {
+        define_arm_cp_regs(cpu, fgwte3_reginfo);
+    }
+
     define_pm_cpregs(cpu);
     define_gcs_cpregs(cpu);
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] target/arm: Add FGWTE3_EL3

Quoted 42 lines · Richard Henderson
---
 target/arm/cpregs.h       |  1 +
 target/arm/cpu-features.h |  5 +++++
 target/arm/cpu.h          |  2 +-
 target/arm/helper.c       | 19 +++++++++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)
target/arm/cpregs.h +1 -0
index 391c0e322b..34592679e4 100644
@@ -385,6 +385,7 @@ typedef enum CPAccessResult {
 /* Indexes into fgt_write[] */
 #define FGTREG_HFGWTR 0
 #define FGTREG_HDFGWTR 1
+#define FGTREG_FGWTE3 2
 /* Indexes into fgt_exec[] */
 #define FGTREG_HFGITR 0
 
target/arm/cpu-features.h +5 -0
index fb5ed25ad0..75f0fc4169 100644
@@ -1467,6 +1467,11 @@ static inline bool isar_feature_aa64_asid2(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64MMFR4, ASID2) != 0;
 }
 
+static inline bool isar_feature_aa64_fgwte3(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64MMFR4, FGWTE3) != 0;
+}
+
 /*
  * Note the E2H0 ID fields is signed, increasingly negative as more
  * isn't implemented.
target/arm/cpu.h +0 -0
index 03a30afcbe..ca75e379eb 100644

target/arm/cpu.h typedef struct CPUArchState { · line 557

Quoted 6 lines
target/arm/cpu.h +1 -1
@@ -553,7 +553,7 @@ typedef struct CPUArchState {
          * FEAT_FGT2 will add more elements to these arrays.
          */
         uint64_t fgt_read[2]; /* HFGRTR, HDFGRTR */
-        uint64_t fgt_write[2]; /* HFGWTR, HDFGWTR */
+        uint64_t fgt_write[3]; /* HFGWTR, HDFGWTR, FGWTE3 */
         uint64_t fgt_exec[1]; /* HFGITR */

GICv5 also has some new fgt registers, incidentally; implementation of them in my w-i-p gicv5 branch:
https://gitlab.com/pm215/qemu/-/commit/31900667e440a136c60b984c9e7c9e9a95d708df

I don't think there's any particular clash here, though. The one thing I do wonder about is that FGWTE3 has only a write trap version, whereas the gicv5 regs have both read and write trap versions. We rely on being able to use the same index for fgt_read[] and fgt_write[]. So I think maybe here we should make fgt_read[] also 3 entries and comment it as "FGWTE3 (unused)"? Unless we deliberately land the gicv5 traps first, we'll need to add a gap there later anyway, and doing it now means we won't forget this oddity.

target/arm/helper.c static const ARMCPRegInfo fpmr_reginfo[] = { · line 6337

Quoted 13 lines
target/arm/cpu.h +0 -0
         /* RME registers */
target/arm/helper.c +4 -0
index af45234ad2..d410a9a1ec 100644
@@ -6331,6 +6331,21 @@ static const ARMCPRegInfo fpmr_reginfo[] = {
     }
 };
 
+static void fgwte3_write(CPUARMState *env, const ARMCPRegInfo *ri,
+                         uint64_t value)
+{
+    env->cp15.fgt_write[FGTREG_FGWTE3] |= value;

I think I would comment this as

     /* All bits are sticky and can only be cleared by CPU reset */

because the semantics are odd enough to look like maybe a mistake otherwise.

Quoted 15 lines
target/arm/helper.c +11 -0
+}
+
+static const ARMCPRegInfo fgwte3_reginfo[] = {
+    { .name = "FGWTE3_EL3", .state = ARM_CP_STATE_AA64,
+      .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 5,
+      .access = PL3_RW, .resetvalue = 0,
+      .writefn = fgwte3_write, .raw_writefn = raw_write,
+      .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_FGWTE3])
+    },
+};
+
 void register_cp_regs_for_features(ARMCPU *cpu)
 {
     /* Register all the coprocessor registers based on feature bits */

target/arm/helper.c void register_cp_regs_for_features(ARMCPU *cpu) · line 7666

Quoted 10 lines
target/arm/helper.c +4 -0
@@ -7642,6 +7657,10 @@ void register_cp_regs_for_features(ARMCPU *cpu)
         define_arm_cp_regs(cpu, ccsidr2_reginfo);
     }
 
+    if (cpu_isar_feature(aa64_fgwte3, cpu)) {
+        define_arm_cp_regs(cpu, fgwte3_reginfo);
+    }
+
     define_pm_cpregs(cpu);
     define_gcs_cpregs(cpu);
 }

Otherwise

thanks
-- PMM

#3 lore raw

Re: [PATCH 1/6] target/arm: Add FGWTE3_EL3

Quoted 12 lines · Peter Maydell

target/arm/cpu.h typedef struct CPUArchState { · line 557

Quoted 7 lines
target/arm/cpu.h +1 -1
@@ -553,7 +553,7 @@ typedef struct CPUArchState {
           * FEAT_FGT2 will add more elements to these arrays.
           */
          uint64_t fgt_read[2]; /* HFGRTR, HDFGRTR */
-        uint64_t fgt_write[2]; /* HFGWTR, HDFGWTR */
+        uint64_t fgt_write[3]; /* HFGWTR, HDFGWTR, FGWTE3 */
          uint64_t fgt_exec[1]; /* HFGITR */

GICv5 also has some new fgt registers, incidentally; implementation of them in my w-i-p gicv5 branch:
https://gitlab.com/pm215/qemu/-/commit/31900667e440a136c60b984c9e7c9e9a95d708df

Quoted 5 lines

I don't think there's any particular clash here, though. The one thing I do wonder about is that FGWTE3 has only a write trap version, whereas the gicv5 regs have both read and write trap versions. We rely on being able to use the same index for fgt_read[] and fgt_write[].

We do? Where?

E.g. HFGRTR and HFGWTR have separate names, and could therefore can have separate indexes in cpregs.h.

That said, there's also nothing in migration for a given name to keep a given index across releases, so we can always sort FGWTE3 to the end so that the other names keep nice clean columns.

r~

#4 lore raw

Re: [PATCH 1/6] target/arm: Add FGWTE3_EL3

Quoted 20 lines · Richard Henderson
Quoted 12 lines · Peter Maydell

target/arm/cpu.h typedef struct CPUArchState { · line 557

Quoted 7 lines
target/arm/cpu.h +1 -1
@@ -553,7 +553,7 @@ typedef struct CPUArchState {
           * FEAT_FGT2 will add more elements to these arrays.
           */
          uint64_t fgt_read[2]; /* HFGRTR, HDFGRTR */
-        uint64_t fgt_write[2]; /* HFGWTR, HDFGWTR */
+        uint64_t fgt_write[3]; /* HFGWTR, HDFGWTR, FGWTE3 */
          uint64_t fgt_exec[1]; /* HFGITR */

GICv5 also has some new fgt registers, incidentally; implementation of them in my w-i-p gicv5 branch:
https://gitlab.com/pm215/qemu/-/commit/31900667e440a136c60b984c9e7c9e9a95d708df

Quoted 5 lines

I don't think there's any particular clash here, though. The one thing I do wonder about is that FGWTE3 has only a write trap version, whereas the gicv5 regs have both read and write trap versions. We rely on being able to use the same index for fgt_read[] and fgt_write[].

Quoted 4 lines

We do? Where?

E.g. HFGRTR and HFGWTR have separate names, and could therefore can have separate indexes in cpregs.h.

For registers that can be FGT-trapped independently for both read and write, we put only a single index and bitpos into ri->fgt, and then access_check_cp_reg will look the index up in either fgt_read[] or fgt_write[] depending on whether the access is a read or not. Essentially we rely on the read-trap config registers and the write-trap config registers being in pairs with identical layout so we don't have to separately store an (index,bitpos) for R and W.

(fgt_exec[] has an independent order because "this is a sysreg we check for an exec-trap" overrides looking at whether it's a read or a write.)

thanks
-- PMM

#5 lore raw

PATCH 2/6 target/arm: Add FGWTE3_EL3 bit definitions

In reply to Richard Henderson
---
 target/arm/cpregs.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
target/arm/cpregs.h +24 -0
index 34592679e4..2c9927b1d9 100644
@@ -689,6 +689,30 @@ FIELD(HDFGWTR_EL2, NBRBCTL, 60, 1)
 FIELD(HDFGWTR_EL2, NBRBDATA, 61, 1)
 FIELD(HDFGWTR_EL2, NPMSNEVFR_EL1, 62, 1)
 
+FIELD(FGWTE3_EL3, ACTLR_EL3, 0, 1)
+FIELD(FGWTE3_EL3, AFSR0_EL3, 1, 1)
+FIELD(FGWTE3_EL3, AFSR1_EL3, 2, 1)
+FIELD(FGWTE3_EL3, AMAIR_EL3, 3, 1)
+FIELD(FGWTE3_EL3, AMAIR2_EL3, 4, 1)
+FIELD(FGWTE3_EL3, GCSCR_EL3, 5, 1)
+FIELD(FGWTE3_EL3, GCSPR_EL3, 6, 1)
+FIELD(FGWTE3_EL3, GPCCR_EL3, 7, 1)
+FIELD(FGWTE3_EL3, GPTBR_EL3, 8, 1)
+FIELD(FGWTE3_EL3, MAIR_EL3, 9, 1)
+FIELD(FGWTE3_EL3, MAIR2_EL3, 10, 1)
+FIELD(FGWTE3_EL3, MDCR_EL3, 11, 1)
+FIELD(FGWTE3_EL3, MECID_RL_A_EL3, 12, 1)
+FIELD(FGWTE3_EL3, MPAM3_EL3, 13, 1)
+FIELD(FGWTE3_EL3, PIR_EL3, 14, 1)
+FIELD(FGWTE3_EL3, SCTLR_EL3, 15, 1)
+FIELD(FGWTE3_EL3, SCTLR2_EL3, 16, 1)
+FIELD(FGWTE3_EL3, SPMROOTCR_EL3, 17, 1)
+FIELD(FGWTE3_EL3, TCR_EL3, 18, 1)
+FIELD(FGWTE3_EL3, TPIDR_EL3, 19, 1)
+FIELD(FGWTE3_EL3, TTBR0_EL3, 20, 1)
+FIELD(FGWTE3_EL3, VBAR_EL3, 21, 1)
+FIELD(FGWTE3_EL3, GPCBW_EL3, 22, 1)
+
 FIELD(FGT, NXS, 13, 1) /* Honour HCR_EL2.FGTnXS to suppress FGT */
 /* Which fine-grained trap bit register to check, if any */
 FIELD(FGT, TYPE, 10, 3)
Signature · 1 line
2.43.0
#7 lore raw

PATCH 3/6 target/arm: Add FGWTE3 FGT bit definitions

In reply to Richard Henderson
---
 target/arm/cpregs.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
target/arm/cpregs.h +31 -0
index 2c9927b1d9..9f2532fe66 100644
@@ -728,6 +728,9 @@ FIELD(FGT, BITPOS, 0, 6) /* Bit position within the uint64_t */
 #define DO_BIT(REG, BITNAME)                                    \
     FGT_##BITNAME = FGT_##REG | R_##REG##_EL2_##BITNAME##_SHIFT
 
+#define DO_EL3_BIT(REG, BITNAME)                                \
+    FGT_##BITNAME = FGT_##REG | R_##REG##_EL3_##BITNAME##_SHIFT
+
 /* Some bits have reversed sense, so 0 means trap and 1 means not */
 #define DO_REV_BIT(REG, BITNAME)                                        \
     FGT_##BITNAME = FGT_##REG | FGT_REV | R_##REG##_EL2_##BITNAME##_SHIFT
@@ -787,6 +790,7 @@ typedef enum FGTBit {
     FGT_HDFGRTR = FGT_RW | (FGTREG_HDFGRTR << R_FGT_IDX_SHIFT),
     FGT_HDFGWTR = FGT_W | (FGTREG_HDFGWTR << R_FGT_IDX_SHIFT),
     FGT_HFGITR = FGT_EXEC | (FGTREG_HFGITR << R_FGT_IDX_SHIFT),
+    FGT_FGWTE3 = FGT_W | (FGTREG_FGWTE3 << R_FGT_IDX_SHIFT),
 
     /* Trap bits in HFGRTR_EL2 / HFGWTR_EL2, starting from bit 0. */
     DO_BIT(HFGRTR, AFSR0_EL1),
@@ -919,10 +923,37 @@ typedef enum FGTBit {
     DO_REV_BIT(HFGITR, NGCSPUSHM_EL1),
     DO_REV_BIT(HFGITR, NGCSEPP),
     DO_BIT(HFGITR, ATS1E1A),
+
+    /* Trap bits in FGWTE3_EL3, starting from bit 0 */
+    DO_EL3_BIT(FGWTE3, ACTLR_EL3),
+    DO_EL3_BIT(FGWTE3, AFSR0_EL3),
+    DO_EL3_BIT(FGWTE3, AFSR1_EL3),
+    DO_EL3_BIT(FGWTE3, AMAIR_EL3),
+    DO_EL3_BIT(FGWTE3, AMAIR2_EL3),
+    DO_EL3_BIT(FGWTE3, GCSCR_EL3),
+    DO_EL3_BIT(FGWTE3, GCSPR_EL3),
+    DO_EL3_BIT(FGWTE3, GPCCR_EL3),
+    DO_EL3_BIT(FGWTE3, GPTBR_EL3),
+    DO_EL3_BIT(FGWTE3, MAIR_EL3),
+    DO_EL3_BIT(FGWTE3, MAIR2_EL3),
+    DO_EL3_BIT(FGWTE3, MDCR_EL3),
+    DO_EL3_BIT(FGWTE3, MECID_RL_A_EL3),
+    DO_EL3_BIT(FGWTE3, MPAM3_EL3),
+    DO_EL3_BIT(FGWTE3, PIR_EL3),
+    DO_EL3_BIT(FGWTE3, SCTLR_EL3),
+    DO_EL3_BIT(FGWTE3, SCTLR2_EL3),
+    DO_EL3_BIT(FGWTE3, SPMROOTCR_EL3),
+    DO_EL3_BIT(FGWTE3, TCR_EL3),
+    DO_EL3_BIT(FGWTE3, TPIDR_EL3),
+    DO_EL3_BIT(FGWTE3, TTBR0_EL3),
+    DO_EL3_BIT(FGWTE3, VBAR_EL3),
+    DO_EL3_BIT(FGWTE3, GPCBW_EL3),
 } FGTBit;
 
 #undef DO_BIT
+#undef DO_EL3_BIT
 #undef DO_REV_BIT
+#undef DO_TLBINXS_BIT
 
 typedef struct ARMCPRegInfo ARMCPRegInfo;
 
Signature · 1 line
2.43.0
#9 lore raw

PATCH 4/6 target/arm: Implement FGWTE3 traps

In reply to Richard Henderson
---
 target/arm/tcg/op_helper.c     | 36 ++++++++++++++++++++++------------
 target/arm/tcg/translate-a64.c | 13 +++++++++++-
 2 files changed, 35 insertions(+), 14 deletions(-)
target/arm/tcg/op_helper.c +23 -13
index c4433be2ed..f040a3fc87 100644
@@ -1067,12 +1067,10 @@ const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key,
      * higher priority than trap-to-EL3, and we don't care about priority
      * order with other EL2 traps because the syndrome value is the same.
      */
-    if (arm_fgt_active(env, arm_current_el(env))) {
+    if (ri->fgt) {
         uint64_t trapword = 0;
         unsigned int idx = FIELD_EX32(ri->fgt, FGT, IDX);
         unsigned int bitpos = FIELD_EX32(ri->fgt, FGT, BITPOS);
-        bool rev = FIELD_EX32(ri->fgt, FGT, REV);
-        bool nxs = FIELD_EX32(ri->fgt, FGT, NXS);
         bool trapbit;
 
         if (ri->fgt & FGT_EXEC) {
@@ -1085,19 +1083,31 @@ const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key,
             assert(idx < ARRAY_SIZE(env->cp15.fgt_write));
             trapword = env->cp15.fgt_write[idx];
         }
+        trapbit = extract64(trapword, bitpos, 1);
 
-        if (nxs && (arm_hcrx_el2_eff(env) & HCRX_FGTNXS)) {
+        if ((ri->access & ~PL3_RW) == 0) {
             /*
-             * If HCRX_EL2.FGTnXS is 1 then the fine-grained trap for
-             * TLBI maintenance insns does *not* apply to the nXS variant.
+             * EL3 cpreg -- must be FGWTE3, and FGWTE3_EL3 can only be
+             * set from AArch64, and if the feature is enabled.
              */
-            trapbit = 0;
-        } else {
-            trapbit = extract64(trapword, bitpos, 1);
-        }
-        if (trapbit != rev) {
-            res = CP_ACCESS_TRAP_EL2;
-            goto fail;
+            if (trapbit) {
+                res = CP_ACCESS_TRAP_EL3;
+                goto fail;
+            }
+        } else if (arm_fgt_active(env, arm_current_el(env))) {
+            bool nxs = FIELD_EX32(ri->fgt, FGT, NXS);
+            bool rev = FIELD_EX32(ri->fgt, FGT, REV);
+            if (nxs && (arm_hcrx_el2_eff(env) & HCRX_FGTNXS)) {
+                /*
+                 * If HCRX_EL2.FGTnXS is 1 then the fine-grained trap for
+                 * TLBI maintenance insns does *not* apply to the nXS variant.
+                 */
+                trapbit = 0;
+            }
+            if (trapbit != rev) {
+                res = CP_ACCESS_TRAP_EL2;
+                goto fail;
+            }
         }
     }
 
target/arm/tcg/translate-a64.c +12 -1
index 1780490065..ff880eab8d 100644
@@ -2886,6 +2886,7 @@ static void handle_sys(DisasContext *s, bool isread,
 {
     uint32_t key = ENCODE_AA64_CP_REG(op0, op1, crn, crm, op2);
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
+    bool need_helper = false;
     bool need_exit_tb = false;
     bool nv_trap_to_el2 = false;
     bool nv_redirect_reg = false;
@@ -2999,7 +3000,17 @@ static void handle_sys(DisasContext *s, bool isread,
         ri = redirect_cpreg(s, key, isread);
     }
 
-    if (ri->accessfn || (ri->fgt && s->fgt_active)) {
+    if (ri->accessfn) {
+        need_helper = true;
+    } else if (ri->fgt) {
+        if ((ri->access & ~PL3_RW) == 0) {
+            need_helper = (arm_dc_feature(s, ARM_FEATURE_EL3) &&
+                           dc_isar_feature(aa64_fgwte3, s));
+        } else {
+            need_helper = s->fgt_active;
+        }
+    }
+    if (need_helper) {
         /* Emit code to perform further access permissions checks at
          * runtime; this may result in an exception.
          */
Signature · 1 line
2.43.0
Reviewed-by on v1 4/6
#10 lore raw

Re: [PATCH 4/6] target/arm: Implement FGWTE3 traps

Quoted 12 lines · Richard Henderson
---
 target/arm/tcg/op_helper.c     | 36 ++++++++++++++++++++++------------
 target/arm/tcg/translate-a64.c | 13 +++++++++++-
 2 files changed, 35 insertions(+), 14 deletions(-)
target/arm/tcg/op_helper.c +0 -0
index c4433be2ed..f040a3fc87 100644

target/arm/tcg/op_helper.c const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key, · line 1069

Quoted 3 lines
target/arm/tcg/op_helper.c +0 -0
@@ -1067,12 +1067,10 @@ const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key,
      * higher priority than trap-to-EL3, and we don't care about priority
      * order with other EL2 traps because the syndrome value is the same.
      */

We should extend this comment, e.g something like:
FGWTE3 traps are exclusively traps to EL3 on EL3-accessible-only registers, so there's no possibility of a trap to EL2, and we can check these here also.

Quoted 65 lines
target/arm/tcg/op_helper.c +23 -13
-    if (arm_fgt_active(env, arm_current_el(env))) {
+    if (ri->fgt) {
         uint64_t trapword = 0;
         unsigned int idx = FIELD_EX32(ri->fgt, FGT, IDX);
         unsigned int bitpos = FIELD_EX32(ri->fgt, FGT, BITPOS);
-        bool rev = FIELD_EX32(ri->fgt, FGT, REV);
-        bool nxs = FIELD_EX32(ri->fgt, FGT, NXS);
         bool trapbit;
 
         if (ri->fgt & FGT_EXEC) {
@@ -1085,19 +1083,31 @@ const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key,
             assert(idx < ARRAY_SIZE(env->cp15.fgt_write));
             trapword = env->cp15.fgt_write[idx];
         }
+        trapbit = extract64(trapword, bitpos, 1);
 
-        if (nxs && (arm_hcrx_el2_eff(env) & HCRX_FGTNXS)) {
+        if ((ri->access & ~PL3_RW) == 0) {
             /*
-             * If HCRX_EL2.FGTnXS is 1 then the fine-grained trap for
-             * TLBI maintenance insns does *not* apply to the nXS variant.
+             * EL3 cpreg -- must be FGWTE3, and FGWTE3_EL3 can only be
+             * set from AArch64, and if the feature is enabled.
              */
-            trapbit = 0;
-        } else {
-            trapbit = extract64(trapword, bitpos, 1);
-        }
-        if (trapbit != rev) {
-            res = CP_ACCESS_TRAP_EL2;
-            goto fail;
+            if (trapbit) {
+                res = CP_ACCESS_TRAP_EL3;
+                goto fail;
+            }
+        } else if (arm_fgt_active(env, arm_current_el(env))) {
+            bool nxs = FIELD_EX32(ri->fgt, FGT, NXS);
+            bool rev = FIELD_EX32(ri->fgt, FGT, REV);
+            if (nxs && (arm_hcrx_el2_eff(env) & HCRX_FGTNXS)) {
+                /*
+                 * If HCRX_EL2.FGTnXS is 1 then the fine-grained trap for
+                 * TLBI maintenance insns does *not* apply to the nXS variant.
+                 */
+                trapbit = 0;
+            }
+            if (trapbit != rev) {
+                res = CP_ACCESS_TRAP_EL2;
+                goto fail;
+            }
         }
     }
 
target/arm/tcg/translate-a64.c +1 -0
index 1780490065..ff880eab8d 100644
@@ -2886,6 +2886,7 @@ static void handle_sys(DisasContext *s, bool isread,
 {
     uint32_t key = ENCODE_AA64_CP_REG(op0, op1, crn, crm, op2);
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
+    bool need_helper = false;
     bool need_exit_tb = false;
     bool nv_trap_to_el2 = false;
     bool nv_redirect_reg = false;

target/arm/tcg/translate-a64.c static void handle_sys(DisasContext *s, bool isread, · line 3005

Quoted 7 lines
target/arm/tcg/translate-a64.c +3 -1
@@ -2999,7 +3000,17 @@ static void handle_sys(DisasContext *s, bool isread,
         ri = redirect_cpreg(s, key, isread);
     }
 
-    if (ri->accessfn || (ri->fgt && s->fgt_active)) {
+    if (ri->accessfn) {
+        need_helper = true;
+    } else if (ri->fgt) {

I might add a comment here:
/*
* EL3-only access means this must be an FGWTE3 trap (which are always
* active); otherwise it's an FGT trap to EL2.
*/

target/arm/tcg/translate-a64.c static void handle_sys(DisasContext *s, bool isread, · line 3016

Quoted 11 lines
target/arm/tcg/translate-a64.c +8 -0
+        if ((ri->access & ~PL3_RW) == 0) {
+            need_helper = (arm_dc_feature(s, ARM_FEATURE_EL3) &&
+                           dc_isar_feature(aa64_fgwte3, s));
+        } else {
+            need_helper = s->fgt_active;
+        }
+    }
+    if (need_helper) {
         /* Emit code to perform further access permissions checks at
          * runtime; this may result in an exception.
          */

Otherwise

thanks
-- PMM

#11 lore raw

PATCH 5/6 target/arm: Set fgt in cpregs for FGWTE3

In reply to Richard Henderson
---
 target/arm/cpregs-gcs.c |  5 +++--
 target/arm/helper.c     | 47 ++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 24 deletions(-)
target/arm/cpregs-gcs.c +3 -2
index e31827342d..9c9ae9cd71 100644
@@ -97,7 +97,7 @@ static const ARMCPRegInfo gcs_reginfo[] = {
       .fieldoffset = offsetof(CPUARMState, cp15.gcscr_el[2]) },
     { .name = "GCSCR_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 5, .opc2 = 0,
-      .access = PL3_RW,
+      .access = PL3_RW, .fgt = FGT_GCSCR_EL3,
       .fieldoffset = offsetof(CPUARMState, cp15.gcscr_el[3]) },
 
     { .name = "GCSPR_EL0", .state = ARM_CP_STATE_AA64,
@@ -119,7 +119,8 @@ static const ARMCPRegInfo gcs_reginfo[] = {
       .fieldoffset = offsetof(CPUARMState, cp15.gcspr_el[2]) },
     { .name = "GCSPR_EL3", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 5, .opc2 = 1,
-      .access = PL3_RW, .writefn = gcspr_write,
+      .access = PL3_RW, .fgt = FGT_GCSPR_EL3,
+      .writefn = gcspr_write,
       .fieldoffset = offsetof(CPUARMState, cp15.gcspr_el[2]) },
 
     { .name = "GCSPUSHM", .state = ARM_CP_STATE_AA64,
target/arm/helper.c +25 -22

135 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 5/6
#12 lore raw

Re: [PATCH 5/6] target/arm: Set fgt in cpregs for FGWTE3

Quoted 6 lines · Richard Henderson
---
 target/arm/cpregs-gcs.c |  5 +++--
 target/arm/helper.c     | 47 ++++++++++++++++++++++-------------------
 2 files changed, 28 insertions(+), 24 deletions(-)

You missed setting fgt for MECID_RL_A_EL3.

Otherwise

thanks
-- PMM

#13 lore raw

Re: [PATCH 5/6] target/arm: Set fgt in cpregs for FGWTE3

Quoted 10 lines · Peter Maydell
Quoted 6 lines · Richard Henderson
---
  target/arm/cpregs-gcs.c |  5 +++--
  target/arm/helper.c     | 47 ++++++++++++++++++++++-------------------
  2 files changed, 28 insertions(+), 24 deletions(-)

You missed setting fgt for MECID_RL_A_EL3.

How odd. I searched for it, missed it, and thought it might be coming in Gustavo's next patch set. Perhaps just a typo on the search...

Anyway, thanks.

r~

#14 lore raw

PATCH 6/6 target/arm: Enable FEAT_FGWTE3 for -cpu max

In reply to Richard Henderson
---
 target/arm/tcg/cpu64.c        | 1 +
 docs/system/arm/emulation.rst | 1 +
 2 files changed, 2 insertions(+)
target/arm/tcg/cpu64.c +1 -0
index 8a3ebf6321..a0d8d0bac8 100644
@@ -1362,6 +1362,7 @@ void aarch64_max_tcg_initfn(Object *obj)
 
     t = GET_IDREG(isar, ID_AA64MMFR4);
     t = FIELD_DP64(t, ID_AA64MMFR4, ASID2, 1);    /* FEAT_ASID2 */
+    t = FIELD_DP64(t, ID_AA64MMFR4, FGWTE3, 1);   /* FEAT_FGWTE3 */
     SET_IDREG(isar, ID_AA64MMFR4, t);
 
     t = GET_IDREG(isar, ID_AA64ZFR0);
docs/system/arm/emulation.rst +1 -0
index 9930974a50..9528afbb18 100644
@@ -78,6 +78,7 @@ the following architecture extensions:
 - FEAT_FAMINMAX (Floating-point maximum and minimum absolute value instructions)
 - FEAT_FCMA (Floating-point complex number instructions)
 - FEAT_FGT (Fine-Grained Traps)
+- FEAT_FGWTE3 (Fine-Grained Write Trap EL3)
 - FEAT_FHM (Floating-point half-precision multiplication instructions)
 - FEAT_FP (Floating Point extensions)
 - FEAT_FP16 (Half-precision floating-point data processing)
Signature · 1 line
2.43.0