Skip to contentSkip to conversation

sh4: Make atomic tas.b instruction thread safe

PATCH In review Lionel Landwerlin 2 participants 2 messages Jan 15, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sh4: Make atomic tas.b instruction thread safe

This is usefull for user emulation where guest threads are emulated with host threads and atomic guest instructions must keep atomic in generated host instructions.

---
 target-sh4/helper.h    |    3 +++
 target-sh4/op_helper.c |   12 ++++++++++++
 target-sh4/translate.c |    5 ++++-
 3 files changed, 19 insertions(+), 1 deletions(-)
target-sh4/helper.h +3 -0
index e665185..887d57a 100644
@@ -46,4 +46,7 @@ DEF_HELPER_1(fsqrt_DT, i64, i64)
 DEF_HELPER_1(ftrc_FT, i32, i32)
 DEF_HELPER_1(ftrc_DT, i32, i64)
 
+DEF_HELPER_0(lock, void)
+DEF_HELPER_0(unlock, void)
+
 #include "def-helper.h"
target-sh4/op_helper.c +12 -0
index ead14e3..a11e8d6 100644
@@ -615,3 +615,15 @@ uint32_t helper_ftrc_DT(uint64_t t0)
     d.ll = t0;
     return float64_to_int32_round_to_zero(d.d, &env->fp_status);
 }
+
+static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
+
+void helper_lock(void)
+{
+    spin_lock(&global_cpu_lock);
+}
+
+void helper_unlock(void)
+{
+    spin_unlock(&global_cpu_lock);
+}
target-sh4/translate.c +4 -1
index bce7463..4762ef7 100644
@@ -1656,7 +1656,9 @@ static void _decode_opc(DisasContext * ctx)
     case 0x401b:		/* tas.b @Rn */
 	{
 	    TCGv addr, val;
-	    addr = tcg_temp_local_new();
+
+           gen_helper_lock();
+           addr = tcg_temp_local_new();
 	    tcg_gen_mov_i32(addr, REG(B11_8));
 	    val = tcg_temp_local_new();
 	    tcg_gen_qemu_ld8u(val, addr, ctx->memidx);
@@ -1665,6 +1667,7 @@ static void _decode_opc(DisasContext * ctx)
 	    tcg_gen_qemu_st8(val, addr, ctx->memidx);
 	    tcg_temp_free(val);
 	    tcg_temp_free(addr);
+           gen_helper_unlock();
 	}
 	return;
     case 0xf00d: /* fsts FPUL,FRn - FPSCR: Nothing */
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 3 lines · Lionel Landwerlin

This is usefull for user emulation where guest threads are emulated with host threads and atomic guest instructions must keep atomic in generated host instructions.

Doesn't this deadlock if the instruction faults?

Paul