Skip to contentSkip to conversation

Tricky parts of my iothread-for-win32 stuff

PATCH Applied Paolo Bonzini 5 participants 24 messages Feb 8, 2011 – Feb 14, 2011

lore
Share & tools
lore
#0 lore raw

cover Tricky parts of my iothread-for-win32 stuff

Ok, so let's start the dance...

These are the generic parts of the iothread-for-win32 work I made last year, rebased to today's qemu (qemu-kvm.git/uq/master merged with qemu.git/master, because it conflicts with some patches from Jan). I never tested them too much, and that's one of the reasons why I never submitted the stuff. Perhaps with some help they can make it this time.

Testing I made now is still a bit limited, but the patches can boot RHEL and Fedora under both Windows iothread and Linux iothread. FWIW, under Wine I need iothread to boot Linux.

The main idea is to replace timedwaits with waits. First because the timedwaits were such only out of laziness; second because timedwait is a pain to implement for Win32 where we have to manually implement condvars.

Anthony Liguori (1):
io-thread: make sure to initialize qemu_work_cond and qemu_cpu_cond

Paolo Bonzini (11):
cris, microblaze: use cpu_has_work
inline cpu_halted into sole caller
change qemu_thread_equal API to always compare with current thread always qemu_cpu_kick after unhalting a cpu
exit round-robin vcpu loop if cpu->stopped is true always signal pause_cond after stopping a VCPU
do not use timedwait on qemu_halt_cond
do not use timedwait on qemu_system_cond
do not use timedwait on qemu_pause_cond
do not use timedwait on qemu_cpu_cond
iothread stops the vcpu thread via IPI

 cpu-defs.h               |    1 +
 cpu-exec.c               |    9 +++++++--
 cpus.c                   |   38 +++++++++++++++++++-------------------
 hw/apic.c                |    1 +
 hw/ppc.c                 |    2 ++
 hw/sun4m.c               |   11 +++++++++--
 hw/sun4u.c               |    4 ++--
 qemu-thread.c            |    4 ++--
 qemu-thread.h            |    2 +-
 target-alpha/exec.h      |   11 -----------
 target-arm/exec.h        |   13 -------------
 target-cris/exec.h       |   13 -------------
 target-i386/exec.h       |   12 ------------
 target-m68k/exec.h       |   10 ----------
 target-microblaze/exec.h |   13 -------------
 target-mips/exec.h       |   11 -----------
 target-ppc/exec.h        |   11 -----------
 target-s390x/exec.h      |   12 ------------
 target-s390x/kvm.c       |    1 +
 target-sh4/cpu.h         |    1 -
 target-sh4/exec.h        |   11 -----------
 target-sparc/exec.h      |   10 ----------
 22 files changed, 45 insertions(+), 156 deletions(-)
Signature · 1 line
1.7.3.5
#1 lore raw

PATCH 1/12 io-thread: make sure to initialize qemu_work_cond and qemu_cpu_cond

From: Anthony Liguori <[email protected]>

---
 cpus.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
cpus.c +3 -1
index 6a85dc8..474be16 100644
@@ -667,8 +667,10 @@ int qemu_init_main_loop(void)
     if (ret)
         return ret;
 
-    qemu_cond_init(&qemu_pause_cond);
+    qemu_cond_init(&qemu_cpu_cond);
     qemu_cond_init(&qemu_system_cond);
+    qemu_cond_init(&qemu_pause_cond);
+    qemu_cond_init(&qemu_work_cond);
     qemu_mutex_init(&qemu_fair_mutex);
     qemu_mutex_init(&qemu_global_mutex);
     qemu_mutex_lock(&qemu_global_mutex);
Signature · 1 line
1.7.3.5
#2 lore raw

Re: [Qemu-devel] [CFT PATCH 01/12] io-thread: make sure to initialize qemu_work_cond and qemu_cpu_cond

Quoted 4 lines · Paolo Bonzini

From: Anthony Liguori<[email protected]>

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 21 lines
---
  cpus.c |    4 +++-
  1 files changed, 3 insertions(+), 1 deletions(-)
cpus.c +3 -1
index 6a85dc8..474be16 100644
@@ -667,8 +667,10 @@ int qemu_init_main_loop(void)
      if (ret)
          return ret;
 
-    qemu_cond_init(&qemu_pause_cond);
+    qemu_cond_init(&qemu_cpu_cond);
      qemu_cond_init(&qemu_system_cond);
+    qemu_cond_init(&qemu_pause_cond);
+    qemu_cond_init(&qemu_work_cond);
      qemu_mutex_init(&qemu_fair_mutex);
      qemu_mutex_init(&qemu_global_mutex);
      qemu_mutex_lock(&qemu_global_mutex);
#3 lore raw

PATCH 2/12 cris, microblaze: use cpu_has_work

In reply to Paolo Bonzini
---
 target-cris/exec.h       |    4 +---
 target-microblaze/exec.h |    4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
target-cris/exec.h +1 -3
index 93ce768..34c0132 100644
@@ -37,9 +37,7 @@ static inline int cpu_halted(CPUState *env) {
 	if (!env->halted)
 		return 0;
 
-	/* IRQ, NMI and GURU execeptions wakes us up.  */
-	if (env->interrupt_request
-	    & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI)) {
+	if (cpu_has_work(env)) {
 		env->halted = 0;
 		return 0;
 	}
target-microblaze/exec.h +1 -3
index 87b2494..ab19828 100644
@@ -36,9 +36,7 @@ static inline int cpu_halted(CPUState *env) {
 	if (!env->halted)
 		return 0;
 
-	/* IRQ, NMI and GURU execeptions wakes us up.  */
-	if (env->interrupt_request
-	    & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI)) {
+	if (cpu_has_work(env)) {
 		env->halted = 0;
 		return 0;
 	}
Signature · 1 line
1.7.3.5
#4 lore raw

Re: [Qemu-devel] [CFT PATCH 02/12] cris, microblaze: use cpu_has_work

Quoted 1 line · Paolo Bonzini

Thanks, I've applied this one.

Cheers

Quoted 40 lines
---
 target-cris/exec.h       |    4 +---
 target-microblaze/exec.h |    4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
target-cris/exec.h +1 -3
index 93ce768..34c0132 100644
@@ -37,9 +37,7 @@ static inline int cpu_halted(CPUState *env) {
 	if (!env->halted)
 		return 0;
 
-	/* IRQ, NMI and GURU execeptions wakes us up.  */
-	if (env->interrupt_request
-	    & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI)) {
+	if (cpu_has_work(env)) {
 		env->halted = 0;
 		return 0;
 	}
target-microblaze/exec.h +1 -3
index 87b2494..ab19828 100644
@@ -36,9 +36,7 @@ static inline int cpu_halted(CPUState *env) {
 	if (!env->halted)
 		return 0;
 
-	/* IRQ, NMI and GURU execeptions wakes us up.  */
-	if (env->interrupt_request
-	    & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI)) {
+	if (cpu_has_work(env)) {
 		env->halted = 0;
 		return 0;
 	}
Signature · 1 line
1.7.3.5
#5 lore raw

PATCH 3/12 inline cpu_halted into sole caller

In reply to Paolo Bonzini

All implementations are now the same except SH, which can fit in the default implementation easily. The newly added flag will not make much sense on non-SH platforms, but I left it anyway. Alternatively you could #ifdef it out on non-SH.

This reduces the number of places that have to be audited for patch 5 ("always qemu_cpu_kick after unhalting a cpu").

---
 cpu-defs.h               |    1 +
 cpu-exec.c               |    9 +++++++--
 target-alpha/exec.h      |   11 -----------
 target-arm/exec.h        |   13 -------------
 target-cris/exec.h       |   11 -----------
 target-i386/exec.h       |   12 ------------
 target-m68k/exec.h       |   10 ----------
 target-microblaze/exec.h |   11 -----------
 target-mips/exec.h       |   11 -----------
 target-ppc/exec.h        |   11 -----------
 target-s390x/exec.h      |   12 ------------
 target-sh4/cpu.h         |    1 -
 target-sh4/exec.h        |   11 -----------
 target-sparc/exec.h      |   10 ----------
 14 files changed, 8 insertions(+), 126 deletions(-)
cpu-defs.h +1 -0
index db809ed..e4dee97 100644
@@ -159,6 +159,7 @@ typedef struct CPUWatchpoint {
     target_ulong mem_io_vaddr; /* target virtual addr at which the      \
                                      memory was accessed */             \
     uint32_t halted; /* Nonzero if the CPU is in suspend state */       \
+    uint32_t intr_at_halt; /* Nonzero if an irq woke CPU from halted state */ \
     uint32_t interrupt_request;                                         \
     volatile sig_atomic_t exit_request;                                 \
     CPU_COMMON_TLB                                                      \
cpu-exec.c +7 -2
index 8c9fb8b..3d6ff35 100644
@@ -230,8 +230,13 @@ int cpu_exec(CPUState *env1)
     uint8_t *tc_ptr;
     unsigned long next_tb;
 
-    if (cpu_halted(env1) == EXCP_HALTED)
-        return EXCP_HALTED;
+    if (env1->halted) {
+        if (!cpu_has_work(env1))
+            return EXCP_HALTED;
+
+        env1->halted = 0;
+        env1->intr_at_halt = 1;
+    }
 
     cpu_single_env = env1;
 
target-alpha/exec.h +0 -11
index a8a38d2..6ae96d1 100644
@@ -42,17 +42,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & CPU_INTERRUPT_HARD);
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-arm/exec.h +0 -13
index e4c35a3..44e1b55 100644
@@ -32,19 +32,6 @@ static inline int cpu_has_work(CPUState *env)
             (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB));
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    /* An interrupt wakes the CPU even if the I and F CPSR bits are
-       set.  We use EXITTB to silently wake CPU without causing an
-       actual interrupt.  */
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 #if !defined(CONFIG_USER_ONLY)
 #include "softmmu_exec.h"
 #endif
target-cris/exec.h +0 -11
index 34c0132..2d5d297 100644
@@ -33,17 +33,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI));
 }
 
-static inline int cpu_halted(CPUState *env) {
-	if (!env->halted)
-		return 0;
-
-	if (cpu_has_work(env)) {
-		env->halted = 0;
-		return 0;
-	}
-	return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-i386/exec.h +0 -12
index fc8945b..3e7386e 100644
@@ -304,18 +304,6 @@ static inline int cpu_has_work(CPUState *env)
     return work;
 }
 
-static inline int cpu_halted(CPUState *env) {
-    /* handle exit of HALTED state */
-    if (!env->halted)
-        return 0;
-    /* disable halt condition */
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 /* load efer and update the corresponding hflags. XXX: do consistency
    checks with cpuid bits ? */
 static inline void cpu_load_efer(CPUState *env, uint64_t val)
target-m68k/exec.h +0 -10
index f31e06e..91daa6b 100644
@@ -33,16 +33,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD));
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-microblaze/exec.h +0 -11
index ab19828..1efff30 100644
@@ -32,17 +32,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI));
 }
 
-static inline int cpu_halted(CPUState *env) {
-	if (!env->halted)
-		return 0;
-
-	if (cpu_has_work(env)) {
-		env->halted = 0;
-		return 0;
-	}
-	return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->sregs[SR_PC] = tb->pc;
target-mips/exec.h +0 -11
index 1273654..b3c5a13 100644
@@ -36,17 +36,6 @@ static inline int cpu_has_work(CPUState *env)
     return has_work;
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void compute_hflags(CPUState *env)
 {
     env->hflags &= ~(MIPS_HFLAG_COP1X | MIPS_HFLAG_64 | MIPS_HFLAG_CP0 |
target-ppc/exec.h +0 -11
index 4688ef5..f87847a 100644
@@ -38,17 +38,6 @@ static inline int cpu_has_work(CPUState *env)
 }
 
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->nip = tb->pc;
target-s390x/exec.h +0 -12
index bf3f264..f7893f3 100644
@@ -34,18 +34,6 @@ static inline int cpu_has_work(CPUState *env)
     return env->interrupt_request & CPU_INTERRUPT_HARD; // guess
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted) {
-       return 0;
-    }
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock* tb)
 {
     env->psw.addr = tb->pc;
target-sh4/cpu.h +0 -1
index 789d188..7188c4d 100644
@@ -184,7 +184,6 @@ typedef struct CPUSH4State {
     uint32_t cvr;		/* Cache Version Register */
 
     void *intc_handle;
-    int intr_at_halt;		/* SR_BL ignored during sleep */
     memory_content *movcal_backup;
     memory_content **movcal_backup_tail;
 } CPUSH4State;
target-sh4/exec.h +0 -11
index 2999c02..9f1c1f6 100644
@@ -32,17 +32,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & CPU_INTERRUPT_HARD);
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        env->intr_at_halt = 1;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 #ifndef CONFIG_USER_ONLY
 #include "softmmu_exec.h"
 #endif
target-sparc/exec.h +0 -10
index f811571..f5c221e 100644
@@ -22,16 +22,6 @@ static inline int cpu_has_work(CPUState *env1)
 }
 
 
-static inline int cpu_halted(CPUState *env1) {
-    if (!env1->halted)
-        return 0;
-    if (cpu_has_work(env1)) {
-        env1->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
Signature · 1 line
1.7.3.5
#6 lore raw

[Qemu-devel] Re: [CFT PATCH 03/12] inline cpu_halted into sole caller

Quoted 4 lines · Paolo Bonzini

All implementations are now the same except SH, which can fit in the default implementation easily. The newly added flag will not make much sense on non-SH platforms, but I left it anyway. Alternatively you could #ifdef it out on non-SH.

I think we can live with that additional variable setting. Just add a comment why it's there.

Quoted 300 lines

This reduces the number of places that have to be audited for patch 5 ("always qemu_cpu_kick after unhalting a cpu").

---
 cpu-defs.h               |    1 +
 cpu-exec.c               |    9 +++++++--
 target-alpha/exec.h      |   11 -----------
 target-arm/exec.h        |   13 -------------
 target-cris/exec.h       |   11 -----------
 target-i386/exec.h       |   12 ------------
 target-m68k/exec.h       |   10 ----------
 target-microblaze/exec.h |   11 -----------
 target-mips/exec.h       |   11 -----------
 target-ppc/exec.h        |   11 -----------
 target-s390x/exec.h      |   12 ------------
 target-sh4/cpu.h         |    1 -
 target-sh4/exec.h        |   11 -----------
 target-sparc/exec.h      |   10 ----------
 14 files changed, 8 insertions(+), 126 deletions(-)
cpu-defs.h +1 -0
index db809ed..e4dee97 100644
@@ -159,6 +159,7 @@ typedef struct CPUWatchpoint {
     target_ulong mem_io_vaddr; /* target virtual addr at which the      \
                                      memory was accessed */             \
     uint32_t halted; /* Nonzero if the CPU is in suspend state */       \
+    uint32_t intr_at_halt; /* Nonzero if an irq woke CPU from halted state */ \
     uint32_t interrupt_request;                                         \
     volatile sig_atomic_t exit_request;                                 \
     CPU_COMMON_TLB                                                      \
cpu-exec.c +7 -2
index 8c9fb8b..3d6ff35 100644
@@ -230,8 +230,13 @@ int cpu_exec(CPUState *env1)
     uint8_t *tc_ptr;
     unsigned long next_tb;
 
-    if (cpu_halted(env1) == EXCP_HALTED)
-        return EXCP_HALTED;
+    if (env1->halted) {
+        if (!cpu_has_work(env1))
+            return EXCP_HALTED;
+
+        env1->halted = 0;
+        env1->intr_at_halt = 1;
+    }
 
     cpu_single_env = env1;
 
target-alpha/exec.h +0 -11
index a8a38d2..6ae96d1 100644
@@ -42,17 +42,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & CPU_INTERRUPT_HARD);
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-arm/exec.h +0 -13
index e4c35a3..44e1b55 100644
@@ -32,19 +32,6 @@ static inline int cpu_has_work(CPUState *env)
             (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB));
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    /* An interrupt wakes the CPU even if the I and F CPSR bits are
-       set.  We use EXITTB to silently wake CPU without causing an
-       actual interrupt.  */
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 #if !defined(CONFIG_USER_ONLY)
 #include "softmmu_exec.h"
 #endif
target-cris/exec.h +0 -11
index 34c0132..2d5d297 100644
@@ -33,17 +33,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI));
 }
 
-static inline int cpu_halted(CPUState *env) {
-	if (!env->halted)
-		return 0;
-
-	if (cpu_has_work(env)) {
-		env->halted = 0;
-		return 0;
-	}
-	return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-i386/exec.h +0 -12
index fc8945b..3e7386e 100644
@@ -304,18 +304,6 @@ static inline int cpu_has_work(CPUState *env)
     return work;
 }
 
-static inline int cpu_halted(CPUState *env) {
-    /* handle exit of HALTED state */
-    if (!env->halted)
-        return 0;
-    /* disable halt condition */
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 /* load efer and update the corresponding hflags. XXX: do consistency
    checks with cpuid bits ? */
 static inline void cpu_load_efer(CPUState *env, uint64_t val)
target-m68k/exec.h +0 -10
index f31e06e..91daa6b 100644
@@ -33,16 +33,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD));
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;
target-microblaze/exec.h +0 -11
index ab19828..1efff30 100644
@@ -32,17 +32,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI));
 }
 
-static inline int cpu_halted(CPUState *env) {
-	if (!env->halted)
-		return 0;
-
-	if (cpu_has_work(env)) {
-		env->halted = 0;
-		return 0;
-	}
-	return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->sregs[SR_PC] = tb->pc;
target-mips/exec.h +0 -11
index 1273654..b3c5a13 100644
@@ -36,17 +36,6 @@ static inline int cpu_has_work(CPUState *env)
     return has_work;
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void compute_hflags(CPUState *env)
 {
     env->hflags &= ~(MIPS_HFLAG_COP1X | MIPS_HFLAG_64 | MIPS_HFLAG_CP0 |
target-ppc/exec.h +0 -11
index 4688ef5..f87847a 100644
@@ -38,17 +38,6 @@ static inline int cpu_has_work(CPUState *env)
 }
 
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->nip = tb->pc;
target-s390x/exec.h +0 -12
index bf3f264..f7893f3 100644
@@ -34,18 +34,6 @@ static inline int cpu_has_work(CPUState *env)
     return env->interrupt_request & CPU_INTERRUPT_HARD; // guess
 }
 
-static inline int cpu_halted(CPUState *env)
-{
-    if (!env->halted) {
-       return 0;
-    }
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock* tb)
 {
     env->psw.addr = tb->pc;
target-sh4/cpu.h +0 -1
index 789d188..7188c4d 100644
@@ -184,7 +184,6 @@ typedef struct CPUSH4State {
     uint32_t cvr;		/* Cache Version Register */
 
     void *intc_handle;
-    int intr_at_halt;		/* SR_BL ignored during sleep */
     memory_content *movcal_backup;
     memory_content **movcal_backup_tail;
 } CPUSH4State;
target-sh4/exec.h +0 -11
index 2999c02..9f1c1f6 100644
@@ -32,17 +32,6 @@ static inline int cpu_has_work(CPUState *env)
     return (env->interrupt_request & CPU_INTERRUPT_HARD);
 }
 
-static inline int cpu_halted(CPUState *env) {
-    if (!env->halted)
-        return 0;
-    if (cpu_has_work(env)) {
-        env->halted = 0;
-        env->intr_at_halt = 1;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 #ifndef CONFIG_USER_ONLY
 #include "softmmu_exec.h"
 #endif
target-sparc/exec.h +0 -3
index f811571..f5c221e 100644
@@ -22,16 +22,6 @@ static inline int cpu_has_work(CPUState *env1)
 }
 
 
-static inline int cpu_halted(CPUState *env1) {
-    if (!env1->halted)
-        return 0;

target-sparc/exec.h static inline int cpu_has_work(CPUState *env1) · line 27

Quoted 10 lines
target-sparc/exec.h +0 -7
-    if (cpu_has_work(env1)) {
-        env1->halted = 0;
-        return 0;
-    }
-    return EXCP_HALTED;
-}
-
 static inline void cpu_pc_from_tb(CPUState *env, TranslationBlock *tb)
 {
     env->pc = tb->pc;

Nice cleanup.

Jan

#7 lore raw

PATCH 4/12 change qemu_thread_equal API to always compare with current thread

In reply to Paolo Bonzini
---
 cpus.c        |   10 ++--------
 qemu-thread.c |    4 ++--
 qemu-thread.h |    2 +-
 3 files changed, 5 insertions(+), 11 deletions(-)
cpus.c +2 -8
index 474be16..c1d0ceb 100644
@@ -857,11 +857,8 @@ void qemu_cpu_kick_self(void)
 int qemu_cpu_self(void *_env)
 {
     CPUState *env = _env;
-    QemuThread this;
 
-    qemu_thread_self(&this);
-
-    return qemu_thread_equal(&this, env->thread);
+    return qemu_thread_equal(env->thread);
 }
 
 void qemu_mutex_lock_iothread(void)
@@ -990,10 +987,7 @@ void cpu_stop_current(void)
 
 void vm_stop(int reason)
 {
-    QemuThread me;
-    qemu_thread_self(&me);
-
-    if (!qemu_thread_equal(&me, &io_thread)) {
+    if (!qemu_thread_equal(&io_thread)) {
         qemu_system_vmstop_request(reason);
         /*
          * FIXME: should not return to device code in case
qemu-thread.c +2 -2
index fbc78fe..7f5c0b6 100644
@@ -181,9 +181,9 @@ void qemu_thread_self(QemuThread *thread)
     thread->thread = pthread_self();
 }
 
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
+int qemu_thread_equal(QemuThread *thread)
 {
-   return pthread_equal(thread1->thread, thread2->thread);
+   return pthread_equal(pthread_self(), thread->thread);
 }
 
 void qemu_thread_exit(void *retval)
qemu-thread.h +1 -1
index 19bb30c..fa4bfb9 100644
@@ -38,7 +38,7 @@ void qemu_thread_create(QemuThread *thread,
                        void *arg);
 void qemu_thread_signal(QemuThread *thread, int sig);
 void qemu_thread_self(QemuThread *thread);
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+int qemu_thread_equal(QemuThread *thread);
 void qemu_thread_exit(void *retval);
 
 #endif
Signature · 1 line
1.7.3.5
#8 lore raw

[Qemu-devel] Re: [CFT PATCH 04/12] change qemu_thread_equal API to always compare with current thread

Quoted 57 lines · Paolo Bonzini
---
 cpus.c        |   10 ++--------
 qemu-thread.c |    4 ++--
 qemu-thread.h |    2 +-
 3 files changed, 5 insertions(+), 11 deletions(-)
cpus.c +2 -8
index 474be16..c1d0ceb 100644
@@ -857,11 +857,8 @@ void qemu_cpu_kick_self(void)
 int qemu_cpu_self(void *_env)
 {
     CPUState *env = _env;
-    QemuThread this;
 
-    qemu_thread_self(&this);
-
-    return qemu_thread_equal(&this, env->thread);
+    return qemu_thread_equal(env->thread);
 }
 
 void qemu_mutex_lock_iothread(void)
@@ -990,10 +987,7 @@ void cpu_stop_current(void)
 
 void vm_stop(int reason)
 {
-    QemuThread me;
-    qemu_thread_self(&me);
-
-    if (!qemu_thread_equal(&me, &io_thread)) {
+    if (!qemu_thread_equal(&io_thread)) {
         qemu_system_vmstop_request(reason);
         /*
          * FIXME: should not return to device code in case
qemu-thread.c +2 -2
index fbc78fe..7f5c0b6 100644
@@ -181,9 +181,9 @@ void qemu_thread_self(QemuThread *thread)
     thread->thread = pthread_self();
 }
 
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
+int qemu_thread_equal(QemuThread *thread)
 {
-   return pthread_equal(thread1->thread, thread2->thread);
+   return pthread_equal(pthread_self(), thread->thread);
 }
 
 void qemu_thread_exit(void *retval)
qemu-thread.h +0 -0
index 19bb30c..fa4bfb9 100644

qemu-thread.h void qemu_thread_create(QemuThread *thread, · line 44

Quoted 8 lines
qemu-thread.h +1 -1
@@ -38,7 +38,7 @@ void qemu_thread_create(QemuThread *thread,
                        void *arg);
 void qemu_thread_signal(QemuThread *thread, int sig);
 void qemu_thread_self(QemuThread *thread);
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+int qemu_thread_equal(QemuThread *thread);
 void qemu_thread_exit(void *retval);
 
 #endif

I would call this "qemu_thread_is_current" or so to clarify what is tested here.

JAn

#9 lore raw

PATCH 5/12 always qemu_cpu_kick after unhalting a cpu

In reply to Paolo Bonzini

This ensures env->halt_cond is broadcast, and the loop in qemu_tcg_wait_io_event and qemu_kvm_wait_io_event is exited naturally rather than through a timeout.

---
 hw/apic.c          |    1 +
 hw/pc.c            |    1 +
 hw/ppc.c           |    2 ++
 hw/sun4m.c         |   11 +++++++++--
 hw/sun4u.c         |    4 ++--
 target-s390x/kvm.c |    1 +
 6 files changed, 16 insertions(+), 4 deletions(-)
hw/apic.c +1 -0
index 2f8376a..97e3b2b 100644
@@ -527,6 +527,7 @@ void apic_sipi(DeviceState *d)
         return;
     cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
     s->wait_for_sipi = 0;
+    qemu_cpu_kick(s->cpu_env);
 }
 
 static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
hw/pc.c +1 -0
index 4dfdc0b..fc1aa27 100644
@@ -907,6 +907,7 @@ static void pc_cpu_reset(void *opaque)
 
     cpu_reset(env);
     env->halted = !cpu_is_bsp(env);
+    qemu_cpu_kick(env);
 }
 
 static CPUState *pc_new_cpu(const char *cpu_model)
hw/ppc.c +2 -0
index 968aec1..de02d33 100644
@@ -208,6 +208,7 @@ static void ppc970_set_irq (void *opaque, int pin, int level)
             } else {
                 LOG_IRQ("%s: restart the CPU\n", __func__);
                 env->halted = 0;
+                qemu_cpu_kick(env);
             }
             break;
         case PPC970_INPUT_HRESET:
@@ -300,6 +301,7 @@ static void ppc40x_set_irq (void *opaque, int pin, int level)
             } else {
                 LOG_IRQ("%s: restart the CPU\n", __func__);
                 env->halted = 0;
+                qemu_cpu_kick(env);
             }
             break;
         case PPC40x_INPUT_DEBUG:
hw/sun4m.c +9 -2
index 30e8a21..47d88be 100644
@@ -253,15 +253,21 @@ void cpu_check_irqs(CPUState *env)
     }
 }
 
+static void cpu_kick_irq(CPUState *env)
+{
+    env->halted = 0;
+    cpu_check_irqs(env);
+    qemu_cpu_kick(env);
+}
+
 static void cpu_set_irq(void *opaque, int irq, int level)
 {
     CPUState *env = opaque;
 
     if (level) {
         trace_sun4m_cpu_set_irq_raise(irq);
-        env->halted = 0;
         env->pil_in |= 1 << irq;
-        cpu_check_irqs(env);
+        cpu_kick_irq(env);
     } else {
         trace_sun4m_cpu_set_irq_lower(irq);
         env->pil_in &= ~(1 << irq);
@@ -279,6 +285,7 @@ static void main_cpu_reset(void *opaque)
 
     cpu_reset(env);
     env->halted = 0;
+    qemu_cpu_kick(env);
 }
 
 static void secondary_cpu_reset(void *opaque)
hw/sun4u.c +2 -2
index 90b1ce2..d282324 100644
@@ -298,6 +298,7 @@ static void cpu_kick_irq(CPUState *env)
 {
     env->halted = 0;
     cpu_check_irqs(env);
+    qemu_cpu_kick(env);
 }
 
 static void cpu_set_irq(void *opaque, int irq, int level)
@@ -306,9 +307,8 @@ static void cpu_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         CPUIRQ_DPRINTF("Raise CPU IRQ %d\n", irq);
-        env->halted = 0;
         env->pil_in |= 1 << irq;
-        cpu_check_irqs(env);
+        cpu_kick_irq(env);
     } else {
         CPUIRQ_DPRINTF("Lower CPU IRQ %d\n", irq);
         env->pil_in &= ~(1 << irq);
target-s390x/kvm.c +1 -0
index 1702c46..fc90b8d 100644
@@ -196,6 +196,7 @@ static void kvm_s390_interrupt_internal(CPUState *env, int type, uint32_t parm,
 
     env->halted = 0;
     env->exception_index = -1;
+    qemu_cpu_kick (env);
 
     kvmint.type = type;
     kvmint.parm = parm;
Signature · 1 line
1.7.3.5
#10 lore raw

[Qemu-devel] Re: [CFT PATCH 05/12] always qemu_cpu_kick after unhalting a cpu

Quoted 31 lines · Paolo Bonzini

This ensures env->halt_cond is broadcast, and the loop in qemu_tcg_wait_io_event and qemu_kvm_wait_io_event is exited naturally rather than through a timeout.

---
 hw/apic.c          |    1 +
 hw/pc.c            |    1 +
 hw/ppc.c           |    2 ++
 hw/sun4m.c         |   11 +++++++++--
 hw/sun4u.c         |    4 ++--
 target-s390x/kvm.c |    1 +
 6 files changed, 16 insertions(+), 4 deletions(-)
hw/apic.c +1 -0
index 2f8376a..97e3b2b 100644
@@ -527,6 +527,7 @@ void apic_sipi(DeviceState *d)
         return;
     cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
     s->wait_for_sipi = 0;
+    qemu_cpu_kick(s->cpu_env);
 }
 
 static void apic_deliver(DeviceState *d, uint8_t dest, uint8_t dest_mode,
hw/pc.c +0 -0
index 4dfdc0b..fc1aa27 100644

hw/pc.c static void pc_cpu_reset(void *opaque) · line 910

Quoted 4 lines
hw/pc.c +1 -0
@@ -907,6 +907,7 @@ static void pc_cpu_reset(void *opaque)
 
     cpu_reset(env);
     env->halted = !cpu_is_bsp(env);
+    qemu_cpu_kick(env);

That should be unneeded, reset is performed with all vcpus stopped.

Quoted 52 lines
hw/pc.c +0 -0
 }
 
 static CPUState *pc_new_cpu(const char *cpu_model)
hw/ppc.c +2 -0
index 968aec1..de02d33 100644
@@ -208,6 +208,7 @@ static void ppc970_set_irq (void *opaque, int pin, int level)
             } else {
                 LOG_IRQ("%s: restart the CPU\n", __func__);
                 env->halted = 0;
+                qemu_cpu_kick(env);
             }
             break;
         case PPC970_INPUT_HRESET:
@@ -300,6 +301,7 @@ static void ppc40x_set_irq (void *opaque, int pin, int level)
             } else {
                 LOG_IRQ("%s: restart the CPU\n", __func__);
                 env->halted = 0;
+                qemu_cpu_kick(env);
             }
             break;
         case PPC40x_INPUT_DEBUG:
hw/sun4m.c +8 -2
index 30e8a21..47d88be 100644
@@ -253,15 +253,21 @@ void cpu_check_irqs(CPUState *env)
     }
 }
 
+static void cpu_kick_irq(CPUState *env)
+{
+    env->halted = 0;
+    cpu_check_irqs(env);
+    qemu_cpu_kick(env);
+}
+
 static void cpu_set_irq(void *opaque, int irq, int level)
 {
     CPUState *env = opaque;
 
     if (level) {
         trace_sun4m_cpu_set_irq_raise(irq);
-        env->halted = 0;
         env->pil_in |= 1 << irq;
-        cpu_check_irqs(env);
+        cpu_kick_irq(env);
     } else {
         trace_sun4m_cpu_set_irq_lower(irq);
         env->pil_in &= ~(1 << irq);

hw/sun4m.c static void main_cpu_reset(void *opaque) · line 288

Quoted 4 lines
hw/sun4m.c +1 -0
@@ -279,6 +285,7 @@ static void main_cpu_reset(void *opaque)
 
     cpu_reset(env);
     env->halted = 0;
+    qemu_cpu_kick(env);

Same here.

Quoted 31 lines
hw/sun4m.c +0 -0
 }
 
 static void secondary_cpu_reset(void *opaque)
hw/sun4u.c +2 -2
index 90b1ce2..d282324 100644
@@ -298,6 +298,7 @@ static void cpu_kick_irq(CPUState *env)
 {
     env->halted = 0;
     cpu_check_irqs(env);
+    qemu_cpu_kick(env);
 }
 
 static void cpu_set_irq(void *opaque, int irq, int level)
@@ -306,9 +307,8 @@ static void cpu_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         CPUIRQ_DPRINTF("Raise CPU IRQ %d\n", irq);
-        env->halted = 0;
         env->pil_in |= 1 << irq;
-        cpu_check_irqs(env);
+        cpu_kick_irq(env);
     } else {
         CPUIRQ_DPRINTF("Lower CPU IRQ %d\n", irq);
         env->pil_in &= ~(1 << irq);
target-s390x/kvm.c +0 -0
index 1702c46..fc90b8d 100644

target-s390x/kvm.c static void kvm_s390_interrupt_internal(CPUState *env, int type, uint32_t parm, · line 202

Quoted 7 lines
target-s390x/kvm.c +1 -0
@@ -196,6 +196,7 @@ static void kvm_s390_interrupt_internal(CPUState *env, int type, uint32_t parm,
 
     env->halted = 0;
     env->exception_index = -1;
+    qemu_cpu_kick (env);
 
     kvmint.type = type;
     kvmint.parm = parm;

Makes sense otherwise.

Jan

#11 lore raw

PATCH 6/12 exit round-robin vcpu loop if cpu->stopped is true

In reply to Paolo Bonzini

Sometimes vcpus are stopped directly without going through ->stop = 1. Exit the VCPU execution loop in this case as well.

---
 cpus.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
cpus.c +1 -1
index c1d0ceb..5b13961 100644
@@ -1061,7 +1061,7 @@ bool cpu_exec_all(void)
             if (r == EXCP_DEBUG) {
                 break;
             }
-        } else if (env->stop) {
+        } else if (env->stop || env->stopped) {
             break;
         }
     }
Signature · 1 line
1.7.3.5
#12 lore raw

[Qemu-devel] Re: [CFT PATCH 06/12] exit round-robin vcpu loop if cpu->stopped is true

Quoted 13 lines · Paolo Bonzini

Sometimes vcpus are stopped directly without going through ->stop = 1. Exit the VCPU execution loop in this case as well.

---
 cpus.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
cpus.c +0 -0
index c1d0ceb..5b13961 100644

cpus.c bool cpu_exec_all(void) · line 1067

Quoted 8 lines
cpus.c +1 -1
@@ -1061,7 +1061,7 @@ bool cpu_exec_all(void)
             if (r == EXCP_DEBUG) {
                 break;
             }
-        } else if (env->stop) {
+        } else if (env->stop || env->stopped) {
             break;
         }
     }

Hmm, does this path actually trigger? If yes, does it happen to obsolete the global exit_request hack?

Jan

#13 lore raw

[Qemu-devel] Re: [CFT PATCH 06/12] exit round-robin vcpu loop if cpu->stopped is true

Quoted 2 lines · Jan Kiszka

Hmm, does this path actually trigger? If yes, does it happen to obsolete the global exit_request hack?

No idea, I wanted to make the initial work as mechanical as possible. By inspection, cpu_stop_current is doing

         cpu_single_env->stopped = 1;
         cpu_exit(cpu_single_env);

without setting ->stop. (I'm changing that in patch 7, but I'm setting ->stop to 0, not 1).

Paolo

#14 lore raw

[Qemu-devel] Re: [CFT PATCH 06/12] exit round-robin vcpu loop if cpu->stopped is true

Quoted 4 lines · Paolo Bonzini
Quoted 2 lines · Jan Kiszka

Hmm, does this path actually trigger? If yes, does it happen to obsolete the global exit_request hack?

Quoted 8 lines

No idea, I wanted to make the initial work as mechanical as possible. By inspection, cpu_stop_current is doing

         cpu_single_env->stopped = 1;
         cpu_exit(cpu_single_env);

without setting ->stop. (I'm changing that in patch 7, but I'm setting ->stop to 0, not 1).

Checked my own patches again: :) exit_request is not obsoleted this way, at least as long as we have !CONFIG_IOTHREAD hanging around.

Also, I don't see any compelling reason now why that test should be unneeded. Rather, this looks like a sleeping race between asynchronous and synchronous vcpu stop, probably papered over by polling the cond variables so far.

Jan

#15 lore raw

[Qemu-devel] Re: [CFT PATCH 06/12] exit round-robin vcpu loop if cpu->stopped is true

In reply to Jan Kiszka
Quoted 2 lines · Jan Kiszka

Hmm, does this path actually trigger? If yes, does it happen to obsolete the global exit_request hack?

No idea, I wanted to make the initial work as mechanical as possible. By inspection, cpu_stop_current is doing

         cpu_single_env->stopped = 1;
         cpu_exit(cpu_single_env);

without setting ->stop. (I'm changing that in patch 7, but I'm setting ->stop to 0, not 1).

Paolo

#16 lore raw

PATCH 7/12 always signal pause_cond after stopping a VCPU

In reply to Paolo Bonzini
---
 cpus.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
cpus.c +2 -0
index 5b13961..ee2a6cc 100644
@@ -980,8 +980,10 @@ static void qemu_system_vmstop_request(int reason)
 void cpu_stop_current(void)
 {
     if (cpu_single_env) {
+        cpu_single_env->stop = 0;
         cpu_single_env->stopped = 1;
         cpu_exit(cpu_single_env);
+        qemu_cond_signal(&qemu_pause_cond);
     }
 }
 
Signature · 1 line
1.7.3.5
#17 lore raw

PATCH 8/12 do not use timedwait on qemu_halt_cond

In reply to Paolo Bonzini

The following conditions can cause cpu_has_work(env) to become true:

- env->queued_work_first: run_on_cpu is already kicking the VCPU

- env->stop = 1: pause_all_vcpus is already kicking the VCPU

- env->stopped = 0: resume_all_vcpus is already kicking the VCPU

- vm_running = 1: vm_start is calling resume_all_vcpus

- env->halted = 0: see previous patch

- qemu_cpu_has_work(env): when it becomes true, board code should set env->halted = 0 too.

---
 cpus.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
index ee2a6cc..be3d82d 100644
@@ -746,7 +746,7 @@ static void qemu_tcg_wait_io_event(void)
     CPUState *env;
 
     while (!any_cpu_has_work())
-        qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
+        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
 
     qemu_mutex_unlock(&qemu_global_mutex);
 
@@ -768,7 +768,7 @@ static void qemu_tcg_wait_io_event(void)
 static void qemu_kvm_wait_io_event(CPUState *env)
 {
     while (!cpu_has_work(env))
-        qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
+        qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
 
     qemu_kvm_eat_signals(env);
     qemu_wait_io_event_common(env);
Signature · 1 line
1.7.3.5
#18 lore raw

PATCH 9/12 do not use timedwait on qemu_system_cond

In reply to Paolo Bonzini

qemu_main_loop_start is the only place where qemu_system_ready is set to 1, and it signals qemu_system_cond.

---
 cpus.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
index be3d82d..3d12c50 100644
@@ -798,7 +798,7 @@ static void *kvm_cpu_thread_fn(void *arg)
 
     /* and wait for machine initialization */
     while (!qemu_system_ready)
-        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
+        qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
 
     while (1) {
         if (cpu_can_run(env))
@@ -824,7 +824,7 @@ static void *tcg_cpu_thread_fn(void *arg)
 
     /* and wait for machine initialization */
     while (!qemu_system_ready)
-        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
+        qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
 
     while (1) {
         cpu_exec_all();
Signature · 1 line
1.7.3.5
#19 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Ok, so let's start the dance...

These are the generic parts of the iothread-for-win32 work I made last year, rebased to today's qemu (qemu-kvm.git/uq/master merged with qemu.git/master, because it conflicts with some patches from Jan). I never tested them too much, and that's one of the reasons why I never submitted the stuff. Perhaps with some help they can make it this time.

Quoted 8 lines

Testing I made now is still a bit limited, but the patches can boot RHEL and Fedora under both Windows iothread and Linux iothread. FWIW, under Wine I need iothread to boot Linux.

The main idea is to replace timedwaits with waits. First because the timedwaits were such only out of laziness; second because timedwait is a pain to implement for Win32 where we have to manually implement condvars.

Thanks a lot for those patches, if we don't have anybody picking up the work with this basis, we can definitely consider Windows support dead.

Quoted 45 lines

Anthony Liguori (1):
io-thread: make sure to initialize qemu_work_cond and qemu_cpu_cond

Paolo Bonzini (11):
cris, microblaze: use cpu_has_work
inline cpu_halted into sole caller
change qemu_thread_equal API to always compare with current thread always qemu_cpu_kick after unhalting a cpu
exit round-robin vcpu loop if cpu->stopped is true always signal pause_cond after stopping a VCPU
do not use timedwait on qemu_halt_cond
do not use timedwait on qemu_system_cond
do not use timedwait on qemu_pause_cond
do not use timedwait on qemu_cpu_cond
iothread stops the vcpu thread via IPI

 cpu-defs.h               |    1 +
 cpu-exec.c               |    9 +++++++--
 cpus.c                   |   38 +++++++++++++++++++-------------------
 hw/apic.c                |    1 +
 hw/ppc.c                 |    2 ++
 hw/sun4m.c               |   11 +++++++++--
 hw/sun4u.c               |    4 ++--
 qemu-thread.c            |    4 ++--
 qemu-thread.h            |    2 +-
 target-alpha/exec.h      |   11 -----------
 target-arm/exec.h        |   13 -------------
 target-cris/exec.h       |   13 -------------
 target-i386/exec.h       |   12 ------------
 target-m68k/exec.h       |   10 ----------
 target-microblaze/exec.h |   13 -------------
 target-mips/exec.h       |   11 -----------
 target-ppc/exec.h        |   11 -----------
 target-s390x/exec.h      |   12 ------------
 target-s390x/kvm.c       |    1 +
 target-sh4/cpu.h         |    1 -
 target-sh4/exec.h        |   11 -----------
 target-sparc/exec.h      |   10 ----------
 22 files changed, 45 insertions(+), 156 deletions(-)
Signature · 1 line
1.7.3.5
Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#20 lore raw

PATCH 10/12 do not use timedwait on qemu_pause_cond

In reply to Paolo Bonzini

all_vcpus_paused can start returning true after penv->stopped changes from 0 to 1. When this is done, qemu_pause_cond is always signaled.

---
 cpus.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
cpus.c +1 -1
index 3d12c50..97ba166 100644
@@ -904,7 +904,7 @@ void pause_all_vcpus(void)
     }
 
     while (!all_vcpus_paused()) {
-        qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
+        qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
         penv = first_cpu;
         while (penv) {
             qemu_cpu_kick(penv);
Signature · 1 line
1.7.3.5
#21 lore raw

PATCH 11/12 do not use timedwait on qemu_cpu_cond

In reply to Paolo Bonzini

Whenever env->created becomes true, qemu_cpu_cond is signaled by {kvm,tcg}_cpu_thread_fn.

I change qemu_cond_signal to qemu_cond_broadcast because right now there is only one listened but, if there were more, there would be no reason to wake up only one.

---
 cpus.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
cpus.c +4 -4
index 97ba166..67fd672 100644
@@ -794,7 +794,7 @@ static void *kvm_cpu_thread_fn(void *arg)
 
     /* signal CPU creation */
     env->created = 1;
-    qemu_cond_signal(&qemu_cpu_cond);
+    qemu_cond_broadcast(&qemu_cpu_cond);
 
     /* and wait for machine initialization */
     while (!qemu_system_ready)
@@ -820,7 +820,7 @@ static void *tcg_cpu_thread_fn(void *arg)
     qemu_mutex_lock(&qemu_global_mutex);
     for (env = first_cpu; env != NULL; env = env->next_cpu)
         env->created = 1;
-    qemu_cond_signal(&qemu_cpu_cond);
+    qemu_cond_broadcast(&qemu_cpu_cond);
 
     /* and wait for machine initialization */
     while (!qemu_system_ready)
@@ -935,7 +935,7 @@ static void tcg_init_vcpu(void *_env)
         qemu_cond_init(env->halt_cond);
         qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
         while (env->created == 0)
-            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
+            qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
         tcg_cpu_thread = env->thread;
         tcg_halt_cond = env->halt_cond;
     } else {
@@ -951,7 +951,7 @@ static void kvm_start_vcpu(CPUState *env)
     qemu_cond_init(env->halt_cond);
     qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
     while (env->created == 0)
-        qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
+        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
 }
 
 void qemu_init_vcpu(void *_env)
Signature · 1 line
1.7.3.5
#22 lore raw

PATCH 12/12 iothread stops the vcpu thread via IPI

In reply to Paolo Bonzini

This "if" has no reason to exist in the iothread world, and it breaks Wine because of the slowness of the thread primitives there.

---
 cpus.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
cpus.c +2 -0
index 67fd672..3d74ad7 100644
@@ -1053,8 +1053,10 @@ bool cpu_exec_all(void)
         qemu_clock_enable(vm_clock,
                           (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
 
+#ifndef CONFIG_IOTHREAD
         if (qemu_alarm_pending())
             break;
+#endif
         if (cpu_can_run(env)) {
             r = qemu_cpu_exec(env);
             if (kvm_enabled()) {
Signature · 1 line
1.7.3.5
#23 lore raw
In reply to Paolo Bonzini
Quoted 5 lines · Paolo Bonzini

Ok, so let's start the dance...

These are the generic parts of the iothread-for-win32 work I made last year, rebased to today's qemu (qemu-kvm.git/uq/master merged with qemu.git/master, because it conflicts with some patches from Jan).

More breakage to come :). But that can be sorted out.

Quoted 11 lines

I never tested them too much, and that's one of the reasons why I never submitted the stuff. Perhaps with some help they can make it this time.

Testing I made now is still a bit limited, but the patches can boot RHEL and Fedora under both Windows iothread and Linux iothread. FWIW, under Wine I need iothread to boot Linux.

The main idea is to replace timedwaits with waits. First because the timedwaits were such only out of laziness; second because timedwait is a pain to implement for Win32 where we have to manually implement condvars.

I bet that timed polling was just to work around early fragility of the iothread code /wrt TCG. qemu-kvm does not poll either (but uses an "interesting" mixture of !IOTHREAD and private code instead...). The conversion makes sense, though I haven't looked at every detail.

Jan