Skip to contentSkip to conversation

Hexagon: ll/sc fixes, emit TCG barriers, TLB locks

PATCH In review Brian Cain 2 participants 9 messages Sep 27 – Oct 2

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

cover Hexagon: ll/sc fixes, emit TCG barriers, TLB locks

While preparing to enable MTTCG, audits and testing revealed gaps and bugs in our implementation.

LL/SC reservations should have been cleared at traps/exceptions.

TLB accesses should hold a lock to make the operations atomic.

Hexagon instructions with barrier or acquire/release semantics should emit corresponding TCG semantics.

Brian Cain (5):
target/hexagon: emit explicit TCG memory barriers target/hexagon: clear LL/SC reservations at context boundaries hw/hexagon: protect the shared architectural TLB target/hexagon: publish TLB updates before flushing tests/tcg/hexagon: cover LL/SC reservation clearing

 include/hw/hexagon/hexagon_tlb.h |  5 +++-
 hw/hexagon/hexagon_tlb.c         | 27 ++++++++++++++++++++-
 linux-user/hexagon/cpu_loop.c    |  4 ++++
 target/hexagon/cpu.c             |  3 +++
 target/hexagon/hex_mmu.c         |  6 ++---
 target/hexagon/translate.c       | 15 ++++++++++++
 tests/tcg/hexagon/atomics.c      | 40 ++++++++++++++++++++++++++++++++
 7 files changed, 94 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/5 target/hexagon: emit explicit TCG memory barriers

Generate release barriers before release and HVX scatter-release operations, acquire barriers after acquire loads, and full barriers for barrier, syncht, isync, and dmsyncht.

---
 target/hexagon/translate.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
target/hexagon/translate.c +15 -0
index 827859e052c..1dae60c0390 100644
@@ -831,12 +831,27 @@ static void mark_store_width(DisasContext *ctx)
 
 static void gen_insn(DisasContext *ctx)
 {
+    uint16_t opcode = ctx->insn->opcode;
+
+    if (GET_ATTRIB(opcode, A_RELEASE) ||
+        GET_ATTRIB(opcode, A_CVI_SCATTER_RELEASE)) {
+        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
+    }
+
     if (ctx->insn->generate) {
         ctx->insn->generate(ctx);
         mark_store_width(ctx);
     } else {
         hex_gen_exception_end_tb(ctx, HEX_CAUSE_INVALID_OPCODE);
     }
+
+    if (GET_ATTRIB(opcode, A_ACQUIRE)) {
+        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
+    }
+    if (opcode == Y2_barrier || opcode == Y2_syncht ||
+        opcode == Y2_isync || opcode == Y6_dmsyncht) {
+        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
+    }
 }
 
 /*
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/5 target/hexagon: clear LL/SC reservations at context boundaries

In reply to Brian Cain

Set llsc_addr to ~0 during reset and soft reset, and whenever linux-user returns to guest code for a trap or a signal.

Reset previously left llsc_addr zero-initialized, so a store-conditional issued without a matching load-locked could match a guest address of zero.

---
 linux-user/hexagon/cpu_loop.c | 4 ++++
 target/hexagon/cpu.c          | 3 +++
 2 files changed, 7 insertions(+)
linux-user/hexagon/cpu_loop.c +4 -0
index e4ef97a1184..e6bb3b62a9f 100644
@@ -39,6 +39,10 @@ void cpu_loop(CPUHexagonState *env)
         cpu_exec_end(cs);
         qemu_process_cpu_events(cs);
 
+        if (trapnr != EXCP_ATOMIC && trapnr != EXCP_YIELD) {
+            env->llsc_addr = ~0;
+        }
+
         switch (trapnr) {
         case EXCP_INTERRUPT:
             /* just indicate that signals should be handled asap */
target/hexagon/cpu.c +3 -0
index 1f7d8a8df70..7347a3342ce 100644
@@ -411,6 +411,7 @@ void hexagon_cpu_soft_reset(CPUHexagonState *env)
     HexagonCPU *cpu;
 
     BQL_LOCK_GUARD();
+    env->llsc_addr = ~0;
     env->t_sreg[HEX_SREG_SSR] = 0;
     hexagon_ssr_set_cause(env, HEX_CAUSE_RESET);
 
@@ -446,6 +447,8 @@ static void hexagon_cpu_reset_hold(Object *obj, ResetType type)
 
     set_default_nan_mode(1, &env->hvx_fp_status);
     set_float_default_nan_pattern(0b01111111, &env->hvx_fp_status);
+
+    env->llsc_addr = ~0;
 #ifndef CONFIG_USER_ONLY
     memset(env->t_sreg, 0, sizeof(uint32_t) * NUM_SREGS);
     memset(env->greg, 0, sizeof(uint32_t) * NUM_GREGS);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/5 hw/hexagon: protect the shared architectural TLB

In reply to Brian Cain

Add a mutex to HexagonTLBState and hold it while interacting with TLB entries.

---
 include/hw/hexagon/hexagon_tlb.h |  5 ++++-
 hw/hexagon/hexagon_tlb.c         | 27 ++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)
include/hw/hexagon/hexagon_tlb.h +4 -1
index 6bc33c05452..56aa0b2beed 100644
@@ -13,6 +13,7 @@
 #include "exec/hwaddr.h"
 #include "exec/mmu-access-type.h"
 #include "monitor/hmp.h"
+#include "qemu/thread.h"
 
 #define TYPE_HEXAGON_TLB "hexagon-tlb"
 OBJECT_DECLARE_SIMPLE_TYPE(HexagonTLBState, HEXAGON_TLB)
@@ -22,10 +23,12 @@ struct HexagonTLBState {
 
     uint32_t num_entries;
     uint64_t *entries;
+    QemuMutex lock;
 };
 
 uint64_t hexagon_tlb_read(HexagonTLBState *tlb, uint32_t index);
-void hexagon_tlb_write(HexagonTLBState *tlb, uint32_t index, uint64_t value);
+uint64_t hexagon_tlb_write(HexagonTLBState *tlb, uint32_t index,
+                           uint64_t value);
 
 bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,
                             uint32_t VA, MMUAccessType access_type,
hw/hexagon/hexagon_tlb.c +26 -1
index 157d03e51bf..28601b78a6f 100644
@@ -7,6 +7,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/log.h"
+#include "qemu/lockable.h"
 #include "hw/hexagon/hexagon_tlb.h"
 #include "hw/core/qdev-properties.h"
 #include "hw/core/resettable.h"
@@ -162,6 +163,7 @@ bool hexagon_tlb_dump_entry(MonitorHMP *hmp, uint64_t entry)
 
 void hexagon_tlb_dump(MonitorHMP *hmp, HexagonTLBState *tlb)
 {
+    QEMU_LOCK_GUARD(&tlb->lock);
     for (uint32_t i = 0; i < tlb->num_entries; i++) {
         hexagon_tlb_dump_entry(hmp, tlb->entries[i]);
     }
@@ -303,13 +305,20 @@ static bool hex_tlb_is_match(uint64_t entry1, uint64_t entry2,
 uint64_t hexagon_tlb_read(HexagonTLBState *tlb, uint32_t index)
 {
     g_assert(index < tlb->num_entries);
+    QEMU_LOCK_GUARD(&tlb->lock);
     return tlb->entries[index];
 }
 
-void hexagon_tlb_write(HexagonTLBState *tlb, uint32_t index, uint64_t value)
+uint64_t hexagon_tlb_write(HexagonTLBState *tlb, uint32_t index,
+                           uint64_t value)
 {
+    uint64_t old;
+
     g_assert(index < tlb->num_entries);
+    QEMU_LOCK_GUARD(&tlb->lock);
+    old = tlb->entries[index];
     tlb->entries[index] = value;
+    return old;
 }
 
 bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,
@@ -323,6 +332,7 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,
     *excp = 0;
     *cause_code = 0;
 
+    QEMU_LOCK_GUARD(&tlb->lock);
     for (uint32_t i = 0; i < tlb->num_entries; i++) {
         if (hex_tlb_entry_match(tlb->entries[i], asid, VA, access_type,
                                 PA, prot, size, excp, cause_code, mmu_idx)) {
@@ -349,6 +359,7 @@ uint32_t hexagon_tlb_lookup(HexagonTLBState *tlb, uint32_t asid,
     uint32_t not_found = 0x80000000;
     uint32_t idx = not_found;
 
+    QEMU_LOCK_GUARD(&tlb->lock);
     *imprecise_exception = 0;
     for (uint32_t i = 0; i < tlb->num_entries; i++) {
         uint64_t entry = tlb->entries[i];
@@ -387,6 +398,7 @@ int hexagon_tlb_check_overlap(HexagonTLBState *tlb, uint64_t entry,
     int matches = 0;
     int last_match = 0;
 
+    QEMU_LOCK_GUARD(&tlb->lock);
     for (uint32_t i = 0; i < tlb->num_entries; i++) {
         if (hex_tlb_is_match(entry, tlb->entries[i], false)) {
             matches++;
@@ -412,6 +424,16 @@ uint32_t hexagon_tlb_get_num_entries(HexagonTLBState *tlb)
 
 static void hexagon_tlb_init(Object *obj)
 {
+    HexagonTLBState *s = HEXAGON_TLB(obj);
+
+    qemu_mutex_init(&s->lock);
+}
+
+static void hexagon_tlb_finalize(Object *obj)
+{
+    HexagonTLBState *s = HEXAGON_TLB(obj);
+
+    qemu_mutex_destroy(&s->lock);
 }
 
 static void hexagon_tlb_realize(DeviceState *dev, Error **errp)
@@ -436,6 +458,8 @@ static void hexagon_tlb_unrealize(DeviceState *dev)
 static void hexagon_tlb_reset_hold(Object *obj, ResetType type)
 {
     HexagonTLBState *s = HEXAGON_TLB(obj);
+
+    QEMU_LOCK_GUARD(&s->lock);
     if (s->entries) {
         memset(s->entries, 0, sizeof(uint64_t) * s->num_entries);
     }
@@ -476,6 +500,7 @@ static const TypeInfo hexagon_tlb_info = {
     .parent = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(HexagonTLBState),
     .instance_init = hexagon_tlb_init,
+    .instance_finalize = hexagon_tlb_finalize,
     .class_init = hexagon_tlb_class_init,
 };
 
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 4/5 target/hexagon: publish TLB updates before flushing

In reply to Brian Cain

Replace the shared architectural entry first, release its lock, and only then call tlb_flush_all_cpus_synced() when a valid mapping was replaced.

The old ordering flushed remote soft TLBs before publishing the replacement, allowing a resumed vCPU to refill the stale mapping.

---
 target/hexagon/hex_mmu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
target/hexagon/hex_mmu.c +2 -4
index d6ee7c40798..aa35f631447 100644
@@ -31,14 +31,12 @@ void hex_tlbw(CPUHexagonState *env, uint32_t index, uint64_t value)
 {
     uint32_t myidx = fTLB_NONPOW2WRAP(fTLB_IDXMASK(index));
     HexagonTLBState *tlb = env_archcpu(env)->tlb;
-    uint64_t old_entry = hexagon_tlb_read(tlb, myidx);
-
+    uint64_t old_entry = hexagon_tlb_write(tlb, myidx, value);
     bool old_entry_valid = extract64(old_entry, 63, 1);
     if (old_entry_valid && hexagon_cpu_mmu_enabled(env)) {
         CPUState *cs = env_cpu(env);
-        tlb_flush(cs);
+        tlb_flush_all_cpus_synced(cs);
     }
-    hexagon_tlb_write(tlb, myidx, value);
     hex_log_tlbw(myidx, value);
 }
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 5/5 tests/tcg/hexagon: cover LL/SC reservation clearing

In reply to Brian Cain

Add load-locked and store-conditional helpers and check that a store-conditional with no preceding load-locked fails, and that returning from a syscall drops the reservation.

---
 tests/tcg/hexagon/atomics.c | 40 +++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
tests/tcg/hexagon/atomics.c +40 -0
index 1c2169b28bd..5017ca003f1 100644
@@ -27,6 +27,30 @@ int err;
 
 #include "hex_test.h"
 
+static inline int32_t load_locked32(int32_t *x)
+{
+    int32_t value;
+
+    __asm__ __volatile__("%[value] = memw_locked(%[addr])"
+                         : [value] "=r"(value)
+                         : [addr] "r"(x)
+                         : "memory");
+    return value;
+}
+
+static inline int store_conditional32(int32_t *x, int32_t value)
+{
+    int result;
+
+    __asm__ __volatile__(
+        "memw_locked(%[addr], p0) = %[value]\n\t"
+        "%[result] = p0"
+        : [result] "=r"(result)
+        : [addr] "r"(x), [value] "r"(value)
+        : "p0", "memory");
+    return result != 0;
+}
+
 static inline int32_t atomic_inc32(int32_t *x)
 {
     int32_t old, dummy;
@@ -89,6 +113,21 @@ static inline int64_t atomic_dec64(int64_t *x)
 volatile int32_t tick32 = 1; /* Using volatile because we are testing atomics */
 volatile int64_t tick64 = 1; /* Using volatile because we are testing atomics */
 
+static void test_reservations(void)
+{
+    int32_t value = 1;
+
+    /* A store-conditional with no preceding load-locked must fail. */
+    check32(store_conditional32(&value, 2), 0);
+    check32(value, 1);
+
+    /* Returning from a syscall drops the reservation. */
+    check32(load_locked32(&value), 1);
+    getpid();
+    check32(store_conditional32(&value, 3), 0);
+    check32(value, 1);
+}
+
 void *thread1_func(void *arg)
 {
     for (int i = 0; i < LOOP_CNT; i++) {
@@ -122,6 +161,7 @@ void test_pthread(void)
 
 int main(int argc, char **argv)
 {
+    test_reservations();
     test_pthread();
     puts(err ? "FAIL" : "PASS");
     return err;
Signature · 1 line
2.34.1
#6 lore raw
In reply to Brian Cain
Quoted 4 lines · Brian Cain

While preparing to enable MTTCG, audits and testing revealed gaps and bugs in our implementation.

Which tests revealed those gaps/bugs?

Commits LGTM

author
#7 lore raw
Quoted 5 lines · Marco Liebel
Quoted 3 lines · Brian Cain

While preparing to enable MTTCG, audits and testing revealed gaps and bugs in our implementation.

Which tests revealed those gaps/bugs?

Mostly the qemu-hexagon-testing test cases but also h2.  This commit series is necessary-but-not-sufficient to pass those, so we'll enable them after we enable some other features too.

Quoted 1 line

Commits LGTM

Can you add an official R-b that will get picked up by tools like b4?  Just on the cover letter is fine, if it applies to all of the patches in the series.

Reviewed-by on the v1 series
#8 lore raw
In reply to Brian Cain
Quoted 27 lines · Brian Cain

While preparing to enable MTTCG, audits and testing revealed gaps and bugs in our implementation.

LL/SC reservations should have been cleared at traps/exceptions.

TLB accesses should hold a lock to make the operations atomic.

Hexagon instructions with barrier or acquire/release semantics should emit corresponding TCG semantics.

Brian Cain (5):
target/hexagon: emit explicit TCG memory barriers target/hexagon: clear LL/SC reservations at context boundaries hw/hexagon: protect the shared architectural TLB target/hexagon: publish TLB updates before flushing tests/tcg/hexagon: cover LL/SC reservation clearing

 include/hw/hexagon/hexagon_tlb.h |  5 +++-
 hw/hexagon/hexagon_tlb.c         | 27 ++++++++++++++++++++-
 linux-user/hexagon/cpu_loop.c    |  4 ++++
 target/hexagon/cpu.c             |  3 +++
 target/hexagon/hex_mmu.c         |  6 ++---
 target/hexagon/translate.c       | 15 ++++++++++++
 tests/tcg/hexagon/atomics.c      | 40 ++++++++++++++++++++++++++++++++
 7 files changed, 94 insertions(+), 6 deletions(-)
Quoted 2 lines

--
2.34.1