Skip to contentSkip to conversation

Add support for atomic operations on sh user emulation

PATCH In review Lionel Landwerlin 1 participant 3 messages Jan 18, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add support for atomic operations on sh user emulation

Hi all,

I've got some problem with threads and sh4 user emulation. Let's take a little example :

------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>

#include <pthread.h>

static pthread_mutex_t mutex;

static void *
thread_task (int id)

{
        while (1)
        {
                pthread_mutex_lock (&mutex);

                printf ("%i Got it !\n", id);

                pthread_mutex_unlock (&mutex);
        }
}

int
main (int argc, char *argv[])

{
        int i, n = 42;
        pthread_t th;

        pthread_mutex_init (&mutex, NULL);

        for (i = 0 ; i < n ; i++)
        {
                pthread_create (&th, NULL, thread_task, (void *) i);
        }
        
        thread_task (n);

        return EXIT_SUCCESS;
}

------------------------------------------------------

For those who would like to test, here is a stripped rootfs
http://djdeath.atr.free.fr/rootfs_sh4.tar.gz (from stlinux.com) with everything to execute this like test (you will find the previous example compiled in /tmp).

So when starting this program with the current qemu, you will probably got an assertion (after a random amonth of time) from the libc :
mutex->__data.__owner == 0

The problem is that on SH4 (and probably others) uniprocessor architectures, there is no atomic instructions (or very few). Atomic operations related to locking etc... are provided by a kernel emulation (gUSA). In user mode emulation qemu has to provide this emulation instead of the kernel that is unaware of the state of the emulated program (register, stack, etc...) and is probably everytime from a different architecture.

The following patch provide a way to emulate gUSA, maybe not the best. This patch is a rewrite of the previous patch I sent on tas.b atomic operation.

Regards,

Signature · 3 lines
Lione Landwerlin                                         

O p e n W i d e                    14, rue Gaillon 75002 Paris
#1 lore raw
Quoted 1 line

From 741b3213357687005062f36885ccdffd186f627c Mon Sep 17 00:00:00 2001

From: Lionel Landwerlin <[email protected]>
Date: Thu, 15 Jan 2009 14:29:23 +0100
Subject: [PATCH] Add support for atomic operation on sh user emulation

On SH uniprocessor architectures there are no atomic instructions (except tas.b) so emulation of atomic operations is done by the kernel (gUSA). It's done by convention, on sh4 the register r15 is positioned to a negative value N, telling the kernel to not prempt the current task until its pc register hasn't growth of N.

This patch solve the problem of user emulation by taking a global (inter thread) lock each time the r15 register is positioned to a negative value and releasing it when r15 leaves the negative value. Tas.b instruction must also take the lock because it's the only one (afaik) atomic instruction.

---
 linux-user/main.c      |    4 ++++
 target-sh4/helper.h    |    5 +++++
 target-sh4/op_helper.c |   21 +++++++++++++++++++++
 target-sh4/translate.c |   23 ++++++++++++++++++++---
 4 files changed, 50 insertions(+), 3 deletions(-)
linux-user/main.c +4 -0
index 7ceea08..0178603 100644
@@ -31,6 +31,7 @@
 #include "syscall_hook.h"
 /* For tb_lock */
 #include "exec-all.h"
+#include "helper.h"
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
@@ -1902,12 +1903,14 @@ void cpu_loop (CPUState *env)
             env->gregs[0] = ret;
             break;
         case EXCP_INTERRUPT:
+            helper_exit_critical_region();
             /* just indicate that signals should be handled asap */
             break;
         case EXCP_DEBUG:
             {
                 int sig;
 
+                helper_exit_critical_region();
                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
                 if (sig)
                   {
@@ -1920,6 +1923,7 @@ void cpu_loop (CPUState *env)
             break;
 	case 0xa0:
 	case 0xc0:
+            helper_exit_critical_region();
             info.si_signo = SIGSEGV;
             info.si_errno = 0;
             info.si_code = TARGET_SEGV_MAPERR;
target-sh4/helper.h +5 -0
index e665185..f35aabd 100644
@@ -46,4 +46,9 @@ DEF_HELPER_1(fsqrt_DT, i64, i64)
 DEF_HELPER_1(ftrc_FT, i32, i32)
 DEF_HELPER_1(ftrc_DT, i32, i64)
 
+#ifdef CONFIG_USER_ONLY
+DEF_HELPER_0(enter_critical_region, void)
+DEF_HELPER_0(exit_critical_region, void)
+#endif
+
 #include "def-helper.h"
target-sh4/op_helper.c +21 -0
index ead14e3..827b37b 100644
@@ -20,6 +20,7 @@
 #include <assert.h>
 #include "exec.h"
 #include "helper.h"
+#include "qemu.h"
 
 #ifndef CONFIG_USER_ONLY
 
@@ -615,3 +616,23 @@ uint32_t helper_ftrc_DT(uint64_t t0)
     d.ll = t0;
     return float64_to_int32_round_to_zero(d.d, &env->fp_status);
 }
+
+#ifdef CONFIG_USER_ONLY
+static spinlock_t global_threads_lock = SPIN_LOCK_UNLOCKED;
+static CPUState *cpu_locking_threads = NULL;
+
+void helper_enter_critical_region(void)
+{
+    spin_lock(&global_threads_lock);
+    cpu_locking_threads = thread_env;
+}
+
+void helper_exit_critical_region(void)
+{
+    if (cpu_locking_threads == thread_env)
+    {
+        cpu_locking_threads = NULL;
+        spin_unlock(&global_threads_lock);
+    }
+}
+#endif /* CONFIG_USER_ONLY */
target-sh4/translate.c +20 -3
index bce7463..23761d5 100644
@@ -564,7 +564,12 @@ static void _decode_opc(DisasContext * ctx)
 	}
 	return;
     case 0xe000:		/* mov #imm,Rn */
-	tcg_gen_movi_i32(REG(B11_8), B7_0s);
+#ifdef CONFIG_USER_ONLY
+        /* Handle critical region (gUSA for sh4) */
+        if ((REG(B11_8) == 16) && ((signed char) B7_0s < 0))
+            gen_helper_enter_critical_region();
+#endif
+        tcg_gen_movi_i32(REG(B11_8), B7_0s);
 	return;
     case 0x9000:		/* mov.w @(disp,PC),Rn */
 	{
@@ -600,7 +605,12 @@ static void _decode_opc(DisasContext * ctx)
 
     switch (ctx->opcode & 0xf00f) {
     case 0x6003:		/* mov Rm,Rn */
-	tcg_gen_mov_i32(REG(B11_8), REG(B7_4));
+        tcg_gen_mov_i32(REG(B11_8), REG(B7_4));
+#ifdef CONFIG_USER_ONLY
+        /* Handle critical region (gUSA for sh4) */
+        if (REG(B11_8) == 16)
+            gen_helper_exit_critical_region();
+#endif
 	return;
     case 0x2000:		/* mov.b Rm,@Rn */
 	tcg_gen_qemu_st8(REG(B7_4), REG(B11_8), ctx->memidx);
@@ -1656,7 +1666,11 @@ static void _decode_opc(DisasContext * ctx)
     case 0x401b:		/* tas.b @Rn */
 	{
 	    TCGv addr, val;
-	    addr = tcg_temp_local_new();
+
+#ifdef CONFIG_USER_ONLY
+            gen_helper_enter_critical_region();
+#endif
+            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 +1679,9 @@ static void _decode_opc(DisasContext * ctx)
 	    tcg_gen_qemu_st8(val, addr, ctx->memidx);
 	    tcg_temp_free(val);
 	    tcg_temp_free(addr);
+#ifdef CONFIG_USER_ONLY
+            gen_helper_exit_critical_region();
+#endif
 	}
 	return;
     case 0xf00d: /* fsts FPUL,FRn - FPSCR: Nothing */
Signature · 1 line
1.5.6.5
#2 lore raw

Laurent Desnogues just found a problem on register check. Here the corrected patch.

------

On SH uniprocessor architectures there are no atomic instructions (except tas.b) so emulation of atomic operations is done by the kernel (gUSA). It's done by convention, on sh4 the register r15 is positioned to a negative value N, telling the kernel to not prempt the current task until its pc register hasn't growth of N.

This patch solve the problem of user emulation by taking a global (inter thread) lock each time the r15 register is positioned to a negative value and releasing it when r15 leaves the negative value. Tas.b instruction must also take the lock because it's the only one (afaik) atomic instruction.

---
 linux-user/main.c      |    4 ++++
 target-sh4/helper.h    |    5 +++++
 target-sh4/op_helper.c |   21 +++++++++++++++++++++
 target-sh4/translate.c |   23 ++++++++++++++++++++---
 4 files changed, 50 insertions(+), 3 deletions(-)
linux-user/main.c +4 -0
index 59e99e7..92fdfd6 100644
@@ -31,6 +31,7 @@
 #include "syscall_hook.h"
 /* For tb_lock */
 #include "exec-all.h"
+#include "helper.h"
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
@@ -1902,12 +1903,14 @@ void cpu_loop (CPUState *env)
             env->gregs[0] = ret;
             break;
         case EXCP_INTERRUPT:
+            helper_exit_critical_region();
             /* just indicate that signals should be handled asap */
             break;
         case EXCP_DEBUG:
             {
                 int sig;
 
+                helper_exit_critical_region();
                 sig = gdb_handlesig (env, TARGET_SIGTRAP);
                 if (sig)
                   {
@@ -1920,6 +1923,7 @@ void cpu_loop (CPUState *env)
             break;
 	case 0xa0:
 	case 0xc0:
+            helper_exit_critical_region();
             info.si_signo = SIGSEGV;
             info.si_errno = 0;
             info.si_code = TARGET_SEGV_MAPERR;
target-sh4/helper.h +5 -0
index e665185..f35aabd 100644
@@ -46,4 +46,9 @@ DEF_HELPER_1(fsqrt_DT, i64, i64)
 DEF_HELPER_1(ftrc_FT, i32, i32)
 DEF_HELPER_1(ftrc_DT, i32, i64)
 
+#ifdef CONFIG_USER_ONLY
+DEF_HELPER_0(enter_critical_region, void)
+DEF_HELPER_0(exit_critical_region, void)
+#endif
+
 #include "def-helper.h"
target-sh4/op_helper.c +21 -0
index ead14e3..827b37b 100644
@@ -20,6 +20,7 @@
 #include <assert.h>
 #include "exec.h"
 #include "helper.h"
+#include "qemu.h"
 
 #ifndef CONFIG_USER_ONLY
 
@@ -615,3 +616,23 @@ uint32_t helper_ftrc_DT(uint64_t t0)
     d.ll = t0;
     return float64_to_int32_round_to_zero(d.d, &env->fp_status);
 }
+
+#ifdef CONFIG_USER_ONLY
+static spinlock_t global_threads_lock = SPIN_LOCK_UNLOCKED;
+static CPUState *cpu_locking_threads = NULL;
+
+void helper_enter_critical_region(void)
+{
+    spin_lock(&global_threads_lock);
+    cpu_locking_threads = thread_env;
+}
+
+void helper_exit_critical_region(void)
+{
+    if (cpu_locking_threads == thread_env)
+    {
+        cpu_locking_threads = NULL;
+        spin_unlock(&global_threads_lock);
+    }
+}
+#endif /* CONFIG_USER_ONLY */
target-sh4/translate.c +20 -3
index bce7463..57b276b 100644
@@ -564,7 +564,12 @@ static void _decode_opc(DisasContext * ctx)
 	}
 	return;
     case 0xe000:		/* mov #imm,Rn */
-	tcg_gen_movi_i32(REG(B11_8), B7_0s);
+#ifdef CONFIG_USER_ONLY
+        /* Handle critical region (gUSA for sh4) */
+        if ((B11_8 == 15) && ((signed char) B7_0s < 0))
+            gen_helper_enter_critical_region();
+#endif
+        tcg_gen_movi_i32(REG(B11_8), B7_0s);
 	return;
     case 0x9000:		/* mov.w @(disp,PC),Rn */
 	{
@@ -600,7 +605,12 @@ static void _decode_opc(DisasContext * ctx)
 
     switch (ctx->opcode & 0xf00f) {
     case 0x6003:		/* mov Rm,Rn */
-	tcg_gen_mov_i32(REG(B11_8), REG(B7_4));
+        tcg_gen_mov_i32(REG(B11_8), REG(B7_4));
+#ifdef CONFIG_USER_ONLY
+        /* Handle critical region (gUSA for sh4) */
+        if (B11_8 == 15)
+                gen_helper_exit_critical_region();
+#endif
 	return;
     case 0x2000:		/* mov.b Rm,@Rn */
 	tcg_gen_qemu_st8(REG(B7_4), REG(B11_8), ctx->memidx);
@@ -1656,7 +1666,11 @@ static void _decode_opc(DisasContext * ctx)
     case 0x401b:		/* tas.b @Rn */
 	{
 	    TCGv addr, val;
-	    addr = tcg_temp_local_new();
+
+#ifdef CONFIG_USER_ONLY
+            gen_helper_enter_critical_region();
+#endif
+            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 +1679,9 @@ static void _decode_opc(DisasContext * ctx)
 	    tcg_gen_qemu_st8(val, addr, ctx->memidx);
 	    tcg_temp_free(val);
 	    tcg_temp_free(addr);
+#ifdef CONFIG_USER_ONLY
+            gen_helper_exit_critical_region();
+#endif
 	}
 	return;
     case 0xf00d: /* fsts FPUL,FRn - FPSCR: Nothing */
Signature · 1 line
1.5.6.5