Skip to contentSkip to conversation

target/arm: Fix BTI versus CF_PCREL

PATCH Applied Richard Henderson 2 participants 2 messages Aug 2, 2024 – Aug 8, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 target/arm: Fix BTI versus CF_PCREL

With pcrel, we cannot check the guarded page bit at translation time, as different mappings of the same physical page may or may not have the GP bit set.

Instead, add a couple of helpers to check the page at runtime, after all other filters that might obviate the need for the check.

The set_btype_for_br call must be moved after the gen_a64_set_pc call to ensure the current pc can still be computed.

---

v2: Enable unwind from guarded_page_br.
Since guarded_page_check is called exclusively with btype != 0, it must immediately follow an indirect branch or eret, so cpu state is up-to-date, so unwind is not required.

---
 target/arm/tcg/helper-a64.h    |  3 ++
 target/arm/tcg/translate.h     |  2 --
 target/arm/tcg/helper-a64.c    | 39 +++++++++++++++++++++
 target/arm/tcg/translate-a64.c | 64 ++++++++--------------------------
 4 files changed, 56 insertions(+), 52 deletions(-)
target/arm/tcg/helper-a64.h +3 -0
index 371388f61b..481007bf39 100644
@@ -133,6 +133,9 @@ 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_2(guarded_page_br, TCG_CALL_NO_RWG, void, env, tl)
+
 DEF_HELPER_FLAGS_5(gvec_fdiv_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_5(gvec_fdiv_s, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_5(gvec_fdiv_d, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
target/arm/tcg/translate.h +0 -2
index a8672c857c..01c217f4a4 100644
@@ -163,8 +163,6 @@ typedef struct DisasContext {
     uint8_t dcz_blocksize;
     /* A copy of cpu->gm_blocksize. */
     uint8_t gm_blocksize;
-    /* True if this page is guarded.  */
-    bool guarded_page;
     /* True if the current insn_start has been updated. */
     bool insn_start_updated;
     /* Bottom two bits of XScale c15_cpar coprocessor access control reg */
target/arm/tcg/helper-a64.c +39 -0
index c60d2a7ec9..21a9abd90a 100644
@@ -1877,3 +1877,42 @@ 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, target_ulong 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(guarded_page_check)(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.
+     */
+    if (is_guarded_page(env, env->pc, 0)) {
+        raise_exception(env, EXCP_UDEF, syn_btitrap(env->btype),
+                        exception_target_el(env));
+    }
+}
+
+void HELPER(guarded_page_br)(CPUARMState *env, target_ulong 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 +14 -50

105 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 11 lines · Richard Henderson

With pcrel, we cannot check the guarded page bit at translation time, as different mappings of the same physical page may or may not have the GP bit set.

Instead, add a couple of helpers to check the page at runtime, after all other filters that might obviate the need for the check.

The set_btype_for_br call must be moved after the gen_a64_set_pc call to ensure the current pc can still be computed.

Quoted 9 lines

---

v2: Enable unwind from guarded_page_br.
Since guarded_page_check is called exclusively with btype != 0, it must immediately follow an indirect branch or eret, so cpu state is up-to-date, so unwind is not required.

Applied to target-arm.next, thanks.

-- PMM