Skip to contentSkip to conversation

Implementing FEAT_BTIE

RFC PATCH In review Mohamed Mediouni 2 participants 14 messages Dec 1

lore
Share & tools
lore
#4 lore raw

RFC PATCH 4/11 target/arm: translate-a64: implementing btype_destination_ok for BTIE

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

---
 target/arm/tcg/translate-a64.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
target/arm/tcg/translate-a64.c +32 -0
index 12e4f366e3..1d2356b281 100644
@@ -10982,6 +10982,34 @@ static bool trans_FAIL(DisasContext *s, arg_OK *a)
     return true;
 }
 
+/* BTIE variant of btype_destination_ok */
+static bool btype_destination_ok_btie(uint32_t insn, int btype)
+{
+    if ((insn & 0xfffff01fu) == 0xd503201fu) {
+        /* HINT space */
+        switch (extract32(insn, 5, 7)) {
+        case 0b011001: /* PACIASP */
+        case 0b011011: /* PACIBSP */
+        case 0b100010: /* BTI c */
+            return btype == 0b110 || btype == 0b010;
+        case 0b100000: /* BTI r */
+            return btype == 0b100;
+        case 0b100100: /* BTI j */
+            return btype == 0b011 || btype == 0b101 || btype == 0b001;
+        case 0b100110: /* BTI jc */
+            return btype == 0b101 || btype == 0b110 || btype < 0b100;
+        }
+    } else {
+        switch (insn & 0xffe0001fu) {
+        case 0xd4200000u: /* BRK */
+        case 0xd4400000u: /* HLT */
+            /* Give priority to the breakpoint exception.  */
+            return true;
+        }
+    }
+    return false;
+}
+
 /**
  * btype_destination_ok:
  * @insn: The instruction at the branch destination
@@ -11001,6 +11029,10 @@ static bool trans_FAIL(DisasContext *s, arg_OK *a)
  */
 static bool btype_destination_ok(uint32_t insn, bool bt, int btype, bool btie)
 {
+    if (btie) {
+        return btype_destination_ok_btie(insn, btype);
+    }
+
     if ((insn & 0xfffff01fu) == 0xd503201fu) {
         /* HINT space */
         switch (extract32(insn, 5, 7)) {
Signature · 1 line
2.54.0 (Apple Git-157)
#0 lore raw

cover Implementing FEAT_BTIE

FEAT_BTIE is an Arm extension part of Future Architecture Technologies.

It has a high level overview documentation at:
https://support.arm.com/documentation/109697/2026_09/Feature-descriptions/Future-Architecture-Technologies?lang=en

And is documented more in detail in the Arm FAT system register and instruction documentation, together with the pseudocode in the XML provided as part of the Exploration Tools.

FEAT_BTIE is a prerequisite for FEAT_S1POE2.

Mohamed Mediouni (11):
target/arm: ptw: implement enhanced guarded page bit target/arm: cpu: handle 3-bit BTYPE
target/arm: translate-a64: query whether the page is an EGP target/arm: translate-a64: implementing btype_destination_ok for BTIE target/arm: translate-a64: determine BTI state ahead of time target/arm: translate-a64: set btype on returns target/arm: translate-a64: set BTYPE for b/bl
target/arm: translate-a64: set BTYPE on conditional branches target/arm: translate-a64: set BTYPE for an always taken B.cond target/arm: translate-a64: rework BTI for BR
target/arm: cpu64: add FAT architecture

 include/hw/core/cpu.h            |   4 +
 target/arm/cpu-features.h        |   5 +
 target/arm/cpu.h                 |  12 +-
 target/arm/internals.h           |   1 +
 target/arm/ptw.c                 |  34 +++--
 target/arm/tcg/cpu64.c           |  21 ++++
 target/arm/tcg/helper-a64-defs.h |   3 +-
 target/arm/tcg/helper-a64.c      |  41 ++----
 target/arm/tcg/translate-a64.c   | 207 ++++++++++++++++++++++++++++---
 target/arm/tcg/translate.h       |   2 +
 10 files changed, 265 insertions(+), 65 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
#7 lore raw

RFC PATCH 7/11 target/arm: translate-a64: set BTYPE for b/bl

From: Mohamed Mediouni <[email protected]>

FEAT_BTIE has BTI on PC-rel branches, add support.

---
 target/arm/tcg/translate-a64.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
target/arm/tcg/translate-a64.c +32 -2
index 7c58cd7d5e..19f625f55c 100644
@@ -1778,6 +1778,36 @@ static inline void gen_check_sp_alignment(DisasContext *s)
      */
 }
 
+static void set_btype_for_b(DisasContext *s, int64_t diff)
+{
+    if (dc_isar_feature(aa64_btie, s)) {
+        if (!s->btie) {
+            set_btype(s, 5);
+        } else if (diff >= -32768 && diff < 32768) {
+            /* small displacement: btype = 0 */
+        } else {
+            set_btype(s, 5);
+        }
+    } else {
+        /* no BTIE: btype = 0 */
+    }
+}
+
+static void set_btype_for_bl(DisasContext *s, int64_t diff)
+{
+    if (dc_isar_feature(aa64_btie, s)) {
+        if (!s->btie) {
+            set_btype(s, 6);
+        } else if (diff >= -32768 && diff < 32768) {
+            /* small displacement: btype = 0 */
+        } else {
+            set_btype(s, 6);
+        }
+    } else {
+        /* no BTIE: btype = 0 */
+    }
+}
+
 /*
  * The instruction disassembly implemented here matches
  * the instruction encoding classifications in chapter C4
@@ -1788,7 +1818,7 @@ static inline void gen_check_sp_alignment(DisasContext *s)
 
 static bool trans_B(DisasContext *s, arg_i *a)
 {
-    reset_btype(s);
+    set_btype_for_b(s, a->imm);
     return gen_direct_branch(s, a->imm);
 }
 
@@ -1802,7 +1832,7 @@ static bool trans_BL(DisasContext *s, arg_i *a)
     }
     tcg_gen_mov_i64(cpu_reg(s, 30), link);
 
-    reset_btype(s);
+    set_btype_for_bl(s, a->imm);
     return gen_direct_branch(s, a->imm);
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

RFC PATCH 2/11 target/arm: cpu: handle 3-bit BTYPE

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

FEAT_BTIE extends the btype to 3 bits.

Take that into account for PSTATE and TBFLAG.

---
 target/arm/cpu.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
target/arm/cpu.h +8 -4
index b2f393f501..0602accd0f 100644
@@ -1599,6 +1599,7 @@ void pmu_init(ARMCPU *cpu);
 #define PSTATE_A (1 << 8)
 #define PSTATE_D (1 << 9)
 #define PSTATE_BTYPE (3 << 10)
+#define PSTATE_BTYPE2 (1 << 14)
 #define PSTATE_SSBS (1 << 12)
 #define PSTATE_ALLINT (1 << 13)
 #define PSTATE_IL (1 << 20)
@@ -1615,7 +1616,8 @@ void pmu_init(ARMCPU *cpu);
 #define PSTATE_UINJ (1ULL << 36)
 #define PSTATE_NZCV (PSTATE_N | PSTATE_Z | PSTATE_C | PSTATE_V)
 #define PSTATE_DAIF (PSTATE_D | PSTATE_A | PSTATE_I | PSTATE_F)
-#define CACHED_PSTATE_BITS (PSTATE_NZCV | PSTATE_DAIF | PSTATE_BTYPE)
+#define CACHED_PSTATE_BITS (PSTATE_NZCV | PSTATE_DAIF | PSTATE_BTYPE \
+    | PSTATE_BTYPE2)
 /* Mode values for AArch64 */
 #define PSTATE_MODE_EL3h 13
 #define PSTATE_MODE_EL3t 12
@@ -1656,7 +1658,8 @@ static inline uint64_t pstate_read(CPUARMState *env)
     ZF = (env->ZF == 0);
     return (env->NF & 0x80000000) | (ZF << 30)
         | (env->CF << 29) | ((env->VF & 0x80000000) >> 3)
-        | env->pstate | env->daif | (env->btype << 10);
+        | env->pstate | env->daif | ((env->btype & 3) << 10)
+        | (((env->btype >> 2) & 1) << 14);
 }
 
 static inline void pstate_write(CPUARMState *env, uint64_t val)
@@ -1666,7 +1669,7 @@ static inline void pstate_write(CPUARMState *env, uint64_t val)
     env->CF = (val >> 29) & 1;
     env->VF = (val << 3) & 0x80000000;
     env->daif = val & PSTATE_DAIF;
-    env->btype = (val >> 10) & 3;
+    env->btype = ((val >> 14) & 1) << 2 | ((val >> 10) & 3);
     env->pstate = val & ~CACHED_PSTATE_BITS;
 }
 
@@ -2580,7 +2583,7 @@ FIELD(TBFLAG_A64, SVEEXC_EL, 2, 2)
 FIELD(TBFLAG_A64, VL, 4, 4)
 FIELD(TBFLAG_A64, PAUTH_ACTIVE, 8, 1)
 FIELD(TBFLAG_A64, BT, 9, 1)
-FIELD(TBFLAG_A64, BTYPE, 10, 2)         /* Not cached. */
+/* formerly BTYPE, can be reused */
 FIELD(TBFLAG_A64, TBID, 12, 2)
 FIELD(TBFLAG_A64, UNPRIV, 14, 1)
 FIELD(TBFLAG_A64, ATA, 15, 1)
@@ -2612,6 +2615,7 @@ FIELD(TBFLAG_A64, FPMR_EL, 45, 2)
 FIELD(TBFLAG_A64, MTE_STORE_ONLY, 47, 1)
 FIELD(TBFLAG_A64, MTE0_STORE_ONLY, 48, 1)
 FIELD(TBFLAG_A64, MTX, 49, 2)
+FIELD(TBFLAG_A64, BTYPE, 51, 3)         /* Not cached. */
 
 /*
  * Helpers for using the above. Note that only the A64 accessors use
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

RFC PATCH 1/11 target/arm: ptw: implement enhanced guarded page bit

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

FEAT_BTIE exposes an enhanced guarded page bit
through FEAT_S1PIE.

---
 include/hw/core/cpu.h     |  4 ++++
 target/arm/cpu-features.h |  5 +++++
 target/arm/ptw.c          | 34 +++++++++++++++++++++++++---------
 3 files changed, 34 insertions(+), 9 deletions(-)
include/hw/core/cpu.h +4 -0
index 5d1b624e6c..f95552e67b 100644
@@ -271,11 +271,15 @@ struct CPUTLBEntryFull {
          * Otherwise, pte_attrs is the same as the MAIR_EL1 8-bit format.
          * For shareability and guarded, as in the SH and GP fields respectively
          * of the VMSAv8-64 PTEs.
+         *
+         * Enhanced Guarded Page is defined only for FEAT_S1PIE indirect
+         * permissions.
          */
         struct {
             uint8_t pte_attrs;
             uint8_t shareability;
             bool guarded;
+            bool enhanced_guarded;
         } arm;
     } extra;
 };
target/arm/cpu-features.h +5 -0
index b0edec2837..c6b8afbdf5 100644
@@ -1201,6 +1201,11 @@ static inline bool isar_feature_aa64_bti(const ARMISARegisters *id)
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, BT) != 0;
 }
 
+static inline bool isar_feature_aa64_btie(const ARMISARegisters *id)
+{
+    return FIELD_EX64_IDREG(id, ID_AA64PFR1, BT) >= 2;
+}
+
 static inline bool isar_feature_aa64_mte_insn_reg(const ARMISARegisters *id)
 {
     return FIELD_EX64_IDREG(id, ID_AA64PFR1, MTE) != 0;
target/arm/ptw.c +25 -9
index e36d2ff938..651935db99 100644
@@ -1625,7 +1625,10 @@ static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
 #define PAGE_GCS      (1 << 3)
 #define PAGE_WXN      (1 << 4)
 #define PAGE_OVERLAY  (1 << 5)
-QEMU_BUILD_BUG_ON(PAGE_RWX & (PAGE_GCS | PAGE_WXN | PAGE_OVERLAY));
+/* PAGE_BTIE is passed through to get_phys_addr_lpae */
+#define PAGE_BTIE     (1 << 6)
+
+QEMU_BUILD_BUG_ON(PAGE_RWX & (PAGE_GCS | PAGE_WXN | PAGE_OVERLAY | PAGE_BTIE));
 
 static int get_S1prot_indirect(CPUARMState *env, S1Translate *ptw,
                                ARMMMUIdx mmu_idx, int pi_index, int po_index,
@@ -1636,14 +1639,14 @@ static int get_S1prot_indirect(CPUARMState *env, S1Translate *ptw,
         /* 1 */ PAGE_OVERLAY | PAGE_READ,
         /* 2 */ PAGE_OVERLAY | PAGE_EXEC,
         /* 3 */ PAGE_OVERLAY | PAGE_READ | PAGE_EXEC,
-        /* 4 */ PAGE_OVERLAY,  /* reserved */
+        /* 4 */ PAGE_OVERLAY | PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_BTIE,
         /* 5 */ PAGE_OVERLAY | PAGE_READ | PAGE_WRITE,
         /* 6 */ PAGE_OVERLAY | PAGE_READ | PAGE_WRITE | PAGE_EXEC | PAGE_WXN,
         /* 7 */ PAGE_OVERLAY | PAGE_READ | PAGE_WRITE | PAGE_EXEC,
         /* 8 */ PAGE_READ,
         /* 9 */ PAGE_READ | PAGE_GCS,
         /* A */ PAGE_READ | PAGE_EXEC,
-        /* B */ 0,             /* reserved */
+        /* B */ PAGE_READ | PAGE_EXEC | PAGE_BTIE,
         /* C */ PAGE_READ | PAGE_WRITE,
         /* D */ 0,             /* reserved */
         /* E */ PAGE_READ | PAGE_WRITE | PAGE_EXEC,
@@ -1722,7 +1725,12 @@ static int get_S1prot_indirect(CPUARMState *env, S1Translate *ptw,
         perm &= ~PAGE_EXEC;
     }
 
-    return perm & PAGE_RWX;
+    /* Treated as no access when FEAT_BTIE is not implemented */
+    if (!cpu_isar_feature(aa64_btie, env_archcpu(env)) && (perm & PAGE_BTIE)) {
+        perm = 0;
+    }
+
+    return perm & (PAGE_RWX | PAGE_BTIE);
 }
 
 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
@@ -2380,6 +2388,11 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,
             g_assert_not_reached();
         }
 
+        /* When in aarch64 mode, and BTI is enabled, remember GP in the TLB. */
+        if (aarch64 && cpu_isar_feature(aa64_bti, cpu)) {
+            result->f.extra.arm.guarded = extract64(attrs, 50, 1); /* GP */
+        }
+
         if (param.pie) {
             int pi = extract64(attrs, 6, 1)
                    | (extract64(attrs, 51, 1) << 1)
@@ -2391,6 +2404,10 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,
              */
             prot = get_S1prot_indirect(env, ptw, mmu_idx, pi, po,
                                        result->f.attrs.space, out_space);
+            if (result->f.extra.arm.guarded && prot & PAGE_BTIE) {
+                result->f.extra.arm.enhanced_guarded = 1;
+            }
+            prot &= PAGE_RWX;
         } else if (regime_is_gcs(mmu_idx)) {
             /*
              * While one must use indirect permissions to successfully
@@ -2435,10 +2452,6 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,
         result->cacheattrs.is_s2_format = false;
         result->cacheattrs.attrs = extract64(mair, attrindx * 8, 8);
 
-        /* When in aarch64 mode, and BTI is enabled, remember GP in the TLB. */
-        if (aarch64 && cpu_isar_feature(aa64_bti, cpu)) {
-            result->f.extra.arm.guarded = extract64(attrs, 50, 1); /* GP */
-        }
         device = S1_attrs_are_device(result->cacheattrs.attrs);
     }
 
@@ -3715,7 +3728,7 @@ static bool get_phys_addr_twostage(CPUARMState *env, S1Translate *ptw,
     hwaddr ipa;
     int s1_prot, s1_lgpgsz;
     ARMSecuritySpace in_space = ptw->in_space;
-    bool ret, ipa_secure, s1_guarded;
+    bool ret, ipa_secure, s1_guarded, s1_enhanced_guarded;
     ARMCacheAttrs cacheattrs1;
     ARMSecuritySpace ipa_space;
     uint64_t hcr;
@@ -3744,6 +3757,8 @@ static bool get_phys_addr_twostage(CPUARMState *env, S1Translate *ptw,
     s1_prot = result->f.prot;
     s1_lgpgsz = result->f.lg_page_size;
     s1_guarded = result->f.extra.arm.guarded;
+    s1_enhanced_guarded = result->f.extra.arm.enhanced_guarded;
+
     cacheattrs1 = result->cacheattrs;
     memset(result, 0, sizeof(*result));
 
@@ -3797,6 +3812,7 @@ static bool get_phys_addr_twostage(CPUARMState *env, S1Translate *ptw,
 
     /* No BTI GP information in stage 2, we just use the S1 value */
     result->f.extra.arm.guarded = s1_guarded;
+    result->f.extra.arm.enhanced_guarded = s1_enhanced_guarded;
 
     /*
      * Check if IPA translates to secure or non-secure PA space.
Signature · 1 line
2.54.0 (Apple Git-157)
#6 lore raw

RFC PATCH 6/11 target/arm: translate-a64: set btype on returns

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

FEAT_BTIE introduces 0b100 as the btype on a return.

---
 target/arm/tcg/translate-a64.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/arm/tcg/translate-a64.c +9 -0
index 2a9fa7c198..7c58cd7d5e 100644
@@ -1944,6 +1944,13 @@ static void set_btype_for_blr(DisasContext *s)
     }
 }
 
+static void set_btype_for_ret(DisasContext *s)
+{
+    if (dc_isar_feature(aa64_btie, s)) {
+        set_btype(s, 4);
+    }
+}
+
 static bool trans_BR(DisasContext *s, arg_r *a)
 {
     set_btype_for_br(s, a->rn);
@@ -1970,6 +1977,7 @@ static bool trans_RET(DisasContext *s, arg_r *a)
 {
     TCGv_i64 target = cpu_reg(s, a->rn);
 
+    set_btype_for_ret(s);
     if (s->gcs_en) {
         gen_load_check_gcs_record(s, target, GCS_IT_RET_nPauth, a->rn);
     } else {
@@ -2043,6 +2051,7 @@ static bool trans_RETA(DisasContext *s, arg_reta *a)
         return false;
     }
 
+    set_btype_for_ret(s);
     dst = auth_branch_target(s, cpu_reg(s, 30), cpu_X[31], !a->m);
     if (s->gcs_en) {
         GCSInstructionType it = a->m ? GCS_IT_RET_PauthB : GCS_IT_RET_PauthA;
Signature · 1 line
2.54.0 (Apple Git-157)
#5 lore raw

RFC PATCH 5/11 target/arm: translate-a64: determine BTI state ahead of time

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

As disambiguating between BTI and BTIE is necessary, also determine whether BTI is needed for the page ahead of time.

---
 target/arm/tcg/helper-a64-defs.h |  2 +-
 target/arm/tcg/helper-a64.c      | 16 +++++++--------
 target/arm/tcg/translate-a64.c   | 34 +++++++++++++++++++++++++++-----
 target/arm/tcg/translate.h       |  2 ++
 4 files changed, 39 insertions(+), 15 deletions(-)
target/arm/tcg/helper-a64-defs.h +1 -1
index c1e5d86b27..2bf870619c 100644
@@ -129,7 +129,7 @@ DEF_HELPER_4(cpyfp, void, env, i32, i32, i32)
 DEF_HELPER_4(cpyfm, void, env, i32, i32, i32)
 DEF_HELPER_4(cpyfe, void, env, i32, i32, i32)
 
-DEF_HELPER_FLAGS_1(guarded_page_check, TCG_CALL_NO_WG, void, env)
+DEF_HELPER_FLAGS_1(bti_trap, TCG_CALL_NO_WG, void, env)
 DEF_HELPER_FLAGS_2(guarded_page_br, TCG_CALL_NO_RWG, void, env, vaddr)
 
 DEF_HELPER_FLAGS_5(gvec_fdiv_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, fpst, i32)
target/arm/tcg/helper-a64.c +7 -9
index fdfa220574..7e3f758613 100644
@@ -1750,18 +1750,16 @@ static bool is_guarded_page(CPUARMState *env, vaddr addr, uintptr_t ra)
 #endif
 }
 
-void HELPER(guarded_page_check)(CPUARMState *env)
+void HELPER(bti_trap)(CPUARMState *env)
 {
     /*
-     * We have already verified that bti is enabled, and that the
-     * instruction at PC is not ok for BTYPE.  This is always at
-     * the beginning of a block, so PC is always up-to-date and
-     * no unwind is required.
+     * We have already verified that bti is enabled for the page,
+     * and that the instruction at PC is not ok for BTYPE.  This is
+     * always at the beginning of a block, so PC is always up-to-date
+     * and no unwind is required.
      */
-    if (is_guarded_page(env, env->pc, 0)) {
-        raise_exception(env, EXCP_UDEF, syn_btitrap(env->btype),
-                        exception_target_el(env));
-    }
+    raise_exception(env, EXCP_UDEF, syn_btitrap(env->btype),
+                    exception_target_el(env));
 }
 
 void HELPER(guarded_page_br)(CPUARMState *env, vaddr pc)
target/arm/tcg/translate-a64.c +29 -5
index 1d2356b281..2a9fa7c198 100644
@@ -32,7 +32,9 @@
 #include "accel/tcg/cpu-mmu-index.h"
 #include "accel/tcg/probe.h"
 #include "exec/tlb-flags.h"
-
+#ifdef CONFIG_USER_ONLY
+#include "user/page-protection.h"
+#endif
 
 static TCGv_i64 cpu_X[32];
 static TCGv_i64 cpu_gcspr[4];
@@ -11195,6 +11197,22 @@ static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
     dc->insn_start_updated = false;
 }
 
+static bool is_guarded_page(CPUARMState *env, vaddr addr, uintptr_t ra)
+{
+#ifdef CONFIG_USER_ONLY
+    return page_get_flags(addr) & PAGE_BTI;
+#else
+    CPUTLBEntryFull *full;
+    void *host;
+    int mmu_idx = cpu_mmu_index(env_cpu(env), true);
+    int flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
+                                  false, &host, &full, ra);
+
+    assert(!(flags & TLB_INVALID_MASK));
+    return full->extra.arm.guarded;
+#endif
+}
+
 /* Disambiguating between EGP and GP, for FEAT_BTIE */
 static bool is_enhanced_guarded_page(CPUARMState *env, vaddr addr, uintptr_t ra)
 {
@@ -11260,6 +11278,8 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 
     s->fp_access_checked = 0;
     s->sve_access_checked = 0;
+    s->bti = false;
+    s->btie = false;
 
     if (check_il_uinj(s)) {
         return;
@@ -11267,10 +11287,13 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 
     if (dc_isar_feature(aa64_bti, s)) {
         int btype = EX_TBFLAG_A64(s->tbflags, BTYPE);
-        bool btie = false;
+
+        if (dc_isar_feature(aa64_bti, s)) {
+            s->bti = is_guarded_page(env, pc, 0);
+        }
 
         if (dc_isar_feature(aa64_btie, s)) {
-            btie = is_enhanced_guarded_page(env, pc, 0);
+            s->btie = is_enhanced_guarded_page(env, pc, 0);
         }
 
         tcg_debug_assert(!s->btype_set);
@@ -11284,8 +11307,9 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
              * We can check all but the guarded page check here;
              * defer the latter to a helper.
              */
-            if (btype && !btype_destination_ok(insn, s->bt, btype, btie)) {
-                gen_helper_guarded_page_check(tcg_env);
+            if (s->bti && btype &&
+                !btype_destination_ok(insn, s->bt, btype, s->btie)) {
+                gen_helper_bti_trap(tcg_env);
             }
         } else {
             /* Not the first insn: btype must be 0.  */
target/arm/tcg/translate.h +2 -0
index 029107f4c0..96fb1f3d48 100644
@@ -218,6 +218,8 @@ typedef struct DisasContext {
     uint8_t fpmr_el;
     /* Offset from VNCR_EL2 when FEAT_NV2 redirects this reg to memory */
     uint32_t nv2_redirect_offset;
+    /* If the instruction is in a guarded or enhanced guarded page */
+    bool bti, btie;
 } DisasContext;
 
 typedef struct DisasCompare {
Signature · 1 line
2.54.0 (Apple Git-157)
#10 lore raw

RFC PATCH 10/11 target/arm: translate-a64: rework BTI for BR

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

Switch to the pre-determined BTI state instead of calling a helper during block execution.

---
 target/arm/tcg/helper-a64-defs.h |  1 -
 target/arm/tcg/helper-a64.c      | 25 -------------------------
 target/arm/tcg/translate-a64.c   | 10 ++++------
 3 files changed, 4 insertions(+), 32 deletions(-)
target/arm/tcg/helper-a64-defs.h +0 -1
index 2bf870619c..786f842cf9 100644
@@ -130,7 +130,6 @@ DEF_HELPER_4(cpyfm, void, env, i32, i32, i32)
 DEF_HELPER_4(cpyfe, void, env, i32, i32, i32)
 
 DEF_HELPER_FLAGS_1(bti_trap, TCG_CALL_NO_WG, void, env)
-DEF_HELPER_FLAGS_2(guarded_page_br, TCG_CALL_NO_RWG, void, env, vaddr)
 
 DEF_HELPER_FLAGS_5(gvec_fdiv_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, fpst, i32)
 DEF_HELPER_FLAGS_5(gvec_fdiv_s, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, fpst, i32)
target/arm/tcg/helper-a64.c +0 -25
index 7e3f758613..d253ea26fa 100644
@@ -1734,22 +1734,6 @@ void HELPER(cpyfe)(CPUARMState *env, uint32_t syndrome, uint32_t wdesc,
     do_cpye(env, syndrome, wdesc, rdesc, false, GETPC());
 }
 
-static bool is_guarded_page(CPUARMState *env, vaddr addr, uintptr_t ra)
-{
-#ifdef CONFIG_USER_ONLY
-    return page_get_flags(addr) & PAGE_BTI;
-#else
-    CPUTLBEntryFull *full;
-    void *host;
-    int mmu_idx = cpu_mmu_index(env_cpu(env), true);
-    int flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
-                                  false, &host, &full, ra);
-
-    assert(!(flags & TLB_INVALID_MASK));
-    return full->extra.arm.guarded;
-#endif
-}
-
 void HELPER(bti_trap)(CPUARMState *env)
 {
     /*
@@ -1761,12 +1745,3 @@ void HELPER(bti_trap)(CPUARMState *env)
     raise_exception(env, EXCP_UDEF, syn_btitrap(env->btype),
                     exception_target_el(env));
 }
-
-void HELPER(guarded_page_br)(CPUARMState *env, vaddr pc)
-{
-    /*
-     * We have already checked for branch via x16 and x17.
-     * What remains for choosing BTYPE is checking for a guarded page.
-     */
-    env->btype = is_guarded_page(env, pc, GETPC()) ? 3 : 1;
-}
target/arm/tcg/translate-a64.c +4 -6
index d9c0ae489d..bcca320742 100644
@@ -2008,14 +2008,12 @@ static void set_btype_for_br(DisasContext *s, int rn)
 {
     if (dc_isar_feature(aa64_bti, s)) {
         /* BR to {x16,x17} or !guard -> 1, else 3.  */
-        if (rn == 16 || rn == 17) {
+        if (!s->bti) {
+            set_btype(s, 1);
+        } else if (rn == 16 || rn == 17) {
             set_btype(s, 1);
         } else {
-            TCGv_i64 pc = tcg_temp_new_i64();
-            gen_pc_plus_diff(s, pc, 0);
-            gen_helper_guarded_page_br(tcg_env, pc);
-            s->btype_set = true;
-            s->tbflags_invalid = true;
+            set_btype(s, 3);
         }
     }
 }
Signature · 1 line
2.54.0 (Apple Git-157)
#9 lore raw

RFC PATCH 9/11 target/arm: translate-a64: set BTYPE for an always taken B.cond

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

---
 target/arm/tcg/translate-a64.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/tcg/translate-a64.c +1 -0
index b02a537dd3..d9c0ae489d 100644
@@ -1931,6 +1931,7 @@ static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
         return gen_cbranch(s, &c, a->imm);
     }
     /* 0xe and 0xf are both "always" conditions */
+    set_btype_for_cond_taken(s, a->imm);
     return gen_direct_branch(s, a->imm);
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

RFC PATCH 3/11 target/arm: translate-a64: query whether the page is an EGP

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

The set of allowed BTYPEs depends on whether
code is running on an enhanced guarded page.

---
 target/arm/tcg/translate-a64.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
target/arm/tcg/translate-a64.c +30 -2
index b356393661..12e4f366e3 100644
@@ -29,6 +29,10 @@
 #include "qemu/log.h"
 #include "semihosting/semihost.h"
 #include "cpregs.h"
+#include "accel/tcg/cpu-mmu-index.h"
+#include "accel/tcg/probe.h"
+#include "exec/tlb-flags.h"
+
 
 static TCGv_i64 cpu_X[32];
 static TCGv_i64 cpu_gcspr[4];
@@ -10983,6 +10987,7 @@ static bool trans_FAIL(DisasContext *s, arg_OK *a)
  * @insn: The instruction at the branch destination
  * @bt: SCTLR_ELx.BT
  * @btype: PSTATE.BTYPE, and is non-zero
+ * @btie: Whether the instruction is in an enhanced guarded page
  *
  * On a guarded page, there are a limited number of insns
  * that may be present at the branch target:
@@ -10994,7 +10999,7 @@ static bool trans_FAIL(DisasContext *s, arg_OK *a)
  *
  * Return true if the branch is compatible, false to raise BTITRAP.
  */
-static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
+static bool btype_destination_ok(uint32_t insn, bool bt, int btype, bool btie)
 {
     if ((insn & 0xfffff01fu) == 0xd503201fu) {
         /* HINT space */
@@ -11158,6 +11163,23 @@ static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
     dc->insn_start_updated = false;
 }
 
+/* Disambiguating between EGP and GP, for FEAT_BTIE */
+static bool is_enhanced_guarded_page(CPUARMState *env, vaddr addr, uintptr_t ra)
+{
+#ifdef CONFIG_USER_ONLY
+    return false;
+#else
+    CPUTLBEntryFull *full;
+    void *host;
+    int mmu_idx = cpu_mmu_index(env_cpu(env), true);
+    int flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
+                                  false, &host, &full, ra);
+
+    assert(!(flags & TLB_INVALID_MASK));
+    return full->extra.arm.enhanced_guarded;
+#endif
+}
+
 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 {
     DisasContext *s = container_of(dcbase, DisasContext, base);
@@ -11213,6 +11235,12 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 
     if (dc_isar_feature(aa64_bti, s)) {
         int btype = EX_TBFLAG_A64(s->tbflags, BTYPE);
+        bool btie = false;
+
+        if (dc_isar_feature(aa64_btie, s)) {
+            btie = is_enhanced_guarded_page(env, pc, 0);
+        }
+
         tcg_debug_assert(!s->btype_set);
         if (s->base.num_insns == 1) {
             /*
@@ -11224,7 +11252,7 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
              * We can check all but the guarded page check here;
              * defer the latter to a helper.
              */
-            if (btype && !btype_destination_ok(insn, s->bt, btype)) {
+            if (btype && !btype_destination_ok(insn, s->bt, btype, btie)) {
                 gen_helper_guarded_page_check(tcg_env);
             }
         } else {
Signature · 1 line
2.54.0 (Apple Git-157)
#8 lore raw

RFC PATCH 8/11 target/arm: translate-a64: set BTYPE on conditional branches

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

Not sure if this design is the right way of doing it though.

---
 target/arm/tcg/translate-a64.c | 63 +++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 5 deletions(-)
target/arm/tcg/translate-a64.c +58 -5
index 19f625f55c..b02a537dd3 100644
@@ -23,6 +23,7 @@
 #include "helper-sme.h"
 #include "helper-sve.h"
 #include "helper-fp8.h"
+#include "tcg/tcg-op-common.h"
 #include "translate.h"
 #include "translate-a64.h"
 #include "tcg/tcg-op.h"
@@ -593,6 +594,49 @@ static bool gen_direct_branch(DisasContext *s, int64_t diff)
     return gen_indirect_branch(s);
 }
 
+static void set_btype_for_cond_taken(DisasContext *s, int64_t diff)
+{
+    if (dc_isar_feature(aa64_btie, s)) {
+        if (s->bti && diff >= -32768 && diff < 32768) {
+            /* small displacement: btype = 0 */
+        } else if (s->bti) {
+            set_btype(s, 5);
+        } else {
+            set_btype(s, 7);
+        }
+    } else {
+        /* no BTIE: btype = 0 */
+    }
+}
+
+static void set_btype_for_cond_rt(DisasContext *s,
+    const DisasCompare64 *c, int64_t diff)
+{
+    TCGv_i64 btype_var = tcg_temp_new_i64();
+    TCGv_i32 btype_var32 = tcg_temp_new_i32();
+    uint32_t btype_val = 0;
+
+    if (dc_isar_feature(aa64_btie, s)) {
+        if (s->bti && diff >= -32768 && diff < 32768) {
+            /* small displacement: btype = 0 */
+        } else if (s->bti) {
+            btype_val = 5;
+        } else {
+            btype_val = 7;
+        }
+    } else {
+        /* no BTIE: btype = 0 */
+    }
+
+    tcg_constant_i32(btype_val);
+    tcg_constant_i32(0);
+    tcg_gen_movcond_i64(c->cond, btype_var, c->v1, c->v2,
+        tcg_constant_i64(btype_val), tcg_constant_i64(0));
+    tcg_gen_trunc_tl_i32(btype_var32, btype_var);
+    tcg_gen_st_i32(btype_var32, tcg_env,
+                   offsetof(CPUARMState, btype));
+}
+
 static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
                         int64_t taken_diff)
 {
@@ -600,6 +644,11 @@ static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
     bool may_goto_t = use_goto_tb(s, s->pc_curr + taken_diff);
     bool may_goto_f = use_goto_tb(s, s->pc_curr + not_taken_diff);
 
+    /* in the BTIE case, BTYPE varies on whether the branch is taken */
+    if (dc_isar_feature(aa64_btie, s)) {
+        s->tbflags_invalid = true;
+    }
+
     /*
      * If there is a choice, place a real goto_tb path first.
      * If neither path can use goto_tb, convert to an indirect branch.
@@ -608,6 +657,7 @@ static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
         DisasLabel l = gen_disas_label(s);
 
         tcg_gen_brcond_i64(c->cond, c->v1, c->v2, l.label);
+        reset_btype(s);
         gen_goto_tb_int(s, 0, not_taken_diff);
 
         set_disas_label(s, l);
@@ -617,18 +667,25 @@ static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
             return true;
         }
         gen_a64_update_pc(s, taken_diff);
+        set_btype_for_cond_taken(s, taken_diff);
     } else if (may_goto_t) {
         DisasLabel l = gen_disas_label(s);
 
         tcg_gen_brcond_i64(tcg_invert_cond(c->cond), c->v1, c->v2, l.label);
+        set_btype_for_cond_taken(s, taken_diff);
         gen_goto_tb_int(s, 1, taken_diff);
 
         set_disas_label(s, l);
         gen_a64_update_pc(s, not_taken_diff);
+        reset_btype(s);
     } else {
         TCGv_i64 pc_t = tcg_temp_new_i64();
         TCGv_i64 pc_f = tcg_temp_new_i64();
 
+        if (dc_isar_feature(aa64_btie, s)) {
+            set_btype_for_cond_rt(s, c, taken_diff);
+        }
+
         gen_pc_plus_diff(s, pc_t, taken_diff);
         gen_pc_plus_diff(s, pc_f, not_taken_diff);
         tcg_gen_movcond_i64(c->cond, cpu_pc, c->v1, c->v2, pc_t, pc_f);
@@ -1845,7 +1902,6 @@ static bool trans_CBZ(DisasContext *s, arg_cbz *a)
         .v2 = tcg_constant_i64(0),
     };
 
-    reset_btype(s);
     return gen_cbranch(s, &c, a->imm);
 }
 
@@ -1857,7 +1913,6 @@ static bool trans_TBZ(DisasContext *s, arg_tbz *a)
         .v2 = tcg_constant_i64(1ULL << a->bitpos),
     };
 
-    reset_btype(s);
     return gen_cbranch(s, &c, a->imm);
 }
 
@@ -1867,7 +1922,7 @@ static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
     if (a->c && !dc_isar_feature(aa64_hbc, s)) {
         return false;
     }
-    reset_btype(s);
+
     if (a->cond < 0x0e) {
         /* genuinely conditional branches */
         DisasCompare64 c;
@@ -1910,7 +1965,6 @@ static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a)
         c.v1 = tm;
     }
 
-    reset_btype(s);
     return gen_cbranch(s, &c, a->imm);
 }
 
@@ -1946,7 +2000,6 @@ static bool trans_CB_cond_imm(DisasContext *s, arg_CB_cond_imm *a)
     }
     c.v2 = tcg_constant_i64(a->imm6);
 
-    reset_btype(s);
     return gen_cbranch(s, &c, a->imm9);
 }
 
Signature · 1 line
2.54.0 (Apple Git-157)
#11 lore raw

RFC PATCH 11/11 target/arm: cpu64: add FAT architecture

In reply to Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

ARMvFAT contains features that are documented in the XML of Exploration Tools, but are not in the Arm ARM.

---
 target/arm/internals.h |  1 +
 target/arm/tcg/cpu64.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
target/arm/internals.h +1 -0
index 033d6d66da..0c770f7ecb 100644
@@ -1817,6 +1817,7 @@ void aarch64_cpu_pauth_finalize(ARMCPU *cpu, Error **errp);
 void aarch64_cpu_lpa2_finalize(ARMCPU *cpu, Error **errp);
 void aarch64_max_v8_tcg_initfn(Object *obj);
 void aarch64_max_v9_tcg_initfn(Object *obj);
+void aarch64_max_fat_tcg_initfn(Object *obj);
 void aarch64_add_pauth_properties(Object *obj);
 void aarch64_add_sve_properties(Object *obj);
 void aarch64_add_sme_properties(Object *obj);
target/arm/tcg/cpu64.c +21 -0
index e46cdd3167..3c23e63ed9 100644
@@ -1536,6 +1536,26 @@ void aarch64_max_v9_tcg_initfn(Object *obj)
                         cpu_max_set_l0gptsz, NULL, NULL);
 }
 
+/*
+ * -cpu x-max-fat: an ARMvFAT CPU with as many features enabled as
+ * our emulation supports.
+ *
+ * The FAT version may be reused for other architectures. This is
+ * not a stable target.
+ */
+void aarch64_max_fat_tcg_initfn(Object *obj)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+    ARMISARegisters *isar = &cpu->isar;
+    uint64_t t;
+
+    aarch64_max_v9_tcg_initfn(obj);
+
+    t = GET_IDREG(isar, ID_AA64PFR1);
+    t = FIELD_DP64(t, ID_AA64PFR1, BT, 2); /* FEAT_BTIE */
+    SET_IDREG(isar, ID_AA64PFR0, t);
+}
+
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a35",         .initfn = aarch64_a35_initfn },
     { .name = "cortex-a55",         .initfn = aarch64_a55_initfn },
@@ -1552,6 +1572,7 @@ static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "neoverse-v1",        .initfn = aarch64_neoverse_v1_initfn },
     { .name = "neoverse-n2",        .initfn = aarch64_neoverse_n2_initfn },
     { .name = "max-v9",             .initfn = aarch64_max_v9_tcg_initfn },
+    { .name = "x-max-fat",             .initfn = aarch64_max_fat_tcg_initfn },
 };
 
 static void aarch64_cpu_register_types(void)
Signature · 1 line
2.54.0 (Apple Git-157)
#13 lore raw
In reply to Mohamed Mediouni
Quoted 13 lines · Mohamed Mediouni

FEAT_BTIE is an Arm extension part of Future Architecture Technologies.

It has a high level overview documentation at:
https://support.arm.com/documentation/109697/2026_09/Feature-descriptions/Future-Architecture-Technologies?lang=en

And is documented more in detail in the Arm FAT system register and instruction documentation, together with the pseudocode in the XML provided as part of the Exploration Tools.

FEAT_BTIE is a prerequisite for FEAT_S1POE2.

Oops, to clarify this patchset applies on top of Richard Henderson’s target/arm: Use tcg_gen_goto_jc* for aarch64 series at
https://patchew.org/QEMU/[email protected]/

Did push a tag to GitHub at https://github.com/mediouni-m/qemu/tree/btie-v1

Quoted 29 lines

Mohamed Mediouni (11):
target/arm: ptw: implement enhanced guarded page bit target/arm: cpu: handle 3-bit BTYPE
target/arm: translate-a64: query whether the page is an EGP target/arm: translate-a64: implementing btype_destination_ok for BTIE target/arm: translate-a64: determine BTI state ahead of time target/arm: translate-a64: set btype on returns target/arm: translate-a64: set BTYPE for b/bl
target/arm: translate-a64: set BTYPE on conditional branches target/arm: translate-a64: set BTYPE for an always taken B.cond target/arm: translate-a64: rework BTI for BR
target/arm: cpu64: add FAT architecture

include/hw/core/cpu.h            |   4 +
target/arm/cpu-features.h        |   5 +
target/arm/cpu.h                 |  12 +-
target/arm/internals.h           |   1 +
target/arm/ptw.c                 |  34 +++--
target/arm/tcg/cpu64.c           |  21 ++++
target/arm/tcg/helper-a64-defs.h |   3 +-
target/arm/tcg/helper-a64.c      |  41 ++----
target/arm/tcg/translate-a64.c   | 207 ++++++++++++++++++++++++++++---
target/arm/tcg/translate.h       |   2 +
10 files changed, 265 insertions(+), 65 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
#12 lore raw

Re: [RFC PATCH 11/11] target/arm: cpu64: add FAT architecture

In reply to Mohamed Mediouni
Quoted 42 lines · Mohamed Mediouni

From: Mohamed Mediouni <[email protected]>

ARMvFAT contains features that are documented in the XML of Exploration Tools,
but are not in the Arm ARM.

---
 target/arm/internals.h |  1 +
 target/arm/tcg/cpu64.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)
target/arm/internals.h +1 -0
index 033d6d66da..0c770f7ecb 100644
@@ -1817,6 +1817,7 @@ void aarch64_cpu_pauth_finalize(ARMCPU *cpu, Error
 **errp);
 void aarch64_cpu_lpa2_finalize(ARMCPU *cpu, Error **errp);
 void aarch64_max_v8_tcg_initfn(Object *obj);
 void aarch64_max_v9_tcg_initfn(Object *obj);
+void aarch64_max_fat_tcg_initfn(Object *obj);
 void aarch64_add_pauth_properties(Object *obj);
 void aarch64_add_sve_properties(Object *obj);

void aarch64_add_sme_properties(Object *obj);

target/arm/tcg/cpu64.c +9 -0
index e46cdd3167..3c23e63ed9 100644
@@ -1536,6 +1536,26 @@ void aarch64_max_v9_tcg_initfn(Object *obj)
                         cpu_max_set_l0gptsz, NULL, NULL);
 }
 
+/*
+ * -cpu x-max-fat: an ARMvFAT CPU with as many features enabled as
+ * our emulation supports.
+ *
+ * The FAT version may be reused for other architectures. This is
+ * not a stable target.
+ */
+void aarch64_max_fat_tcg_initfn(Object *obj)
+{

target/arm/tcg/cpu64.c void aarch64_max_v9_tcg_initfn(Object *obj) · line 1557

Quoted 10 lines
target/arm/tcg/cpu64.c +9 -0
+    ARMCPU *cpu = ARM_CPU(obj);
+    ARMISARegisters *isar = &cpu->isar;
+    uint64_t t;
+
+    aarch64_max_v9_tcg_initfn(obj);
+
+    t = GET_IDREG(isar, ID_AA64PFR1);
+    t = FIELD_DP64(t, ID_AA64PFR1, BT, 2); /* FEAT_BTIE */
+    SET_IDREG(isar, ID_AA64PFR0, t);
 

Oops, to read ID_AA64PFR1 here.

+}

Quoted 19 lines
target/arm/tcg/cpu64.c +2 -0
+
 static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "cortex-a35",         .initfn = aarch64_a35_initfn },
     { .name = "cortex-a55",         .initfn = aarch64_a55_initfn },
@@ -1552,6 +1572,7 @@ static const ARMCPUInfo aarch64_cpus[] = {
     { .name = "neoverse-v1",        .initfn = aarch64_neoverse_v1_initfn
 },
     { .name = "neoverse-n2",        .initfn = aarch64_neoverse_n2_initfn
 },
     { .name = "max-v9",             .initfn = aarch64_max_v9_tcg_initfn },
+    { .name = "x-max-fat",             .initfn =
 aarch64_max_fat_tcg_initfn },

};

static void aarch64_cpu_register_types(void)
--
2.54.0 (Apple Git-157)