Skip to contentSkip to conversation

Win32 iothread support

PATCH Superseded Paolo Bonzini 3 participants 31 messages Feb 26, 2011 – Feb 27, 2011 Kbuild

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Win32 iothread support

After gathering the comments about the two series I sent separately, here is the full series for Win32 iothread support, ready to be applied to uq/master.

Patches 1 to 5 are generic Win32 improvements, including the qemu-thread implementation. Because of complex dependencies, I think it's better if this part is also routed through uq/master.

Patches 6 to 8 are generic threading improvements, including using PTHREAD_MUTEX_ERRORCHECK as suggested by Jan.

Patches 9 to 17 eliminate polling, replacing condition variable timedwait with wait.

Patch 18 removes a redundant condition from the TCG cpu_exec_all function.

Patches 19 to 21 add all necessary stubs to make iothread compile with Win32, except the IPI calls. These are provided by patch 22.

Tested on Wine and Linux, not on "real" Windows. The series introduces a dependency on Windows 2K or newer. I don't think either 95/98/ME or Windows NT 3.x are reasonable host systems for QEMU, anyway.

I incorporated all suggestions from Jan, including his renaming patch for qemu_*_is_self, and included Aurelien's sh4 tweak to cpu_halted.

Aurelien Jarno (1):
target-sh4: move intr_at_halt out of cpu_halted()

Jan Kiszka (1):
Refactor thread retrieval and check

Paolo Bonzini (20):
unlock iothread during WaitForMultipleObjects implement win32 dynticks timer
use win32 timer queues
add win32 qemu-thread implementation include qemu-thread.h early
add assertions on the owner of a QemuMutex remove CONFIG_THREAD
inline cpu_halted into sole caller
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 move blocking of signals to qemu_signalfd_init provide dummy signal init functions for win32 protect qemu_cpu_kick_self for Win32
add Win32 IPI service

 Makefile.objs                        |    4 +-
 configure                            |    2 -
 cpu-exec.c                           |    9 +-
 cpus.c                               |  298 +++++++++++++++++-----------------
 exec.c                               |    2 +-
 hw/ppc.c                             |    2 +
 hw/sun4m.c                           |   10 +-
 hw/sun4u.c                           |    4 +-
 os-win32.c                           |    2 +
 qemu-common.h                        |    2 +-
 qemu-thread.c => qemu-thread-posix.c |   38 +++--
 qemu-thread-posix.h                  |   18 ++
 qemu-thread-win32.c                  |  260 +++++++++++++++++++++++++++++
 qemu-thread-win32.h                  |   21 +++
 qemu-thread.h                        |   31 ++--
 qemu-timer.c                         |   89 +++++------
 target-alpha/exec.h                  |   11 --
 target-arm/exec.h                    |   13 --
 target-cris/exec.h                   |   11 --
 target-i386/exec.h                   |   12 --
 target-i386/kvm.c                    |    4 +-
 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-s390x/kvm.c                   |    1 +
 target-sh4/cpu.h                     |    2 +-
 target-sh4/exec.h                    |   11 --
 target-sh4/helper.c                  |    4 +-
 target-sh4/op_helper.c               |    1 +
 target-sparc/exec.h                  |   10 --
 ui/vnc-jobs-async.c                  |    2 +-
 33 files changed, 562 insertions(+), 367 deletions(-)
 rename qemu-thread.c => qemu-thread-posix.c (81%)
 create mode 100644 qemu-thread-posix.h
 create mode 100644 qemu-thread-win32.c
 create mode 100644 qemu-thread-win32.h
Signature · 1 line
1.7.4
#1 lore raw

PATCH v2 1/22 unlock iothread during WaitForMultipleObjects

---
 os-win32.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
os-win32.c +2 -0
index b214e6a..c971d92 100644
@@ -140,7 +140,9 @@ void os_host_main_loop_wait(int *timeout)
         int err;
         WaitObjects *w = &wait_objects;
 
+        qemu_mutex_unlock_iothread();
         ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
+        qemu_mutex_lock_iothread();
         if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
             if (w->func[ret - WAIT_OBJECT_0])
                 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
Signature · 1 line
1.7.4
#2 lore raw

PATCH v2 2/22 implement win32 dynticks timer

In reply to Paolo Bonzini
---
 qemu-timer.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-timer.c +6 -1
index 88c7b28..122e7ed 100644
@@ -1004,6 +1004,7 @@ static void win32_stop_timer(struct qemu_alarm_timer *t)
 static void win32_rearm_timer(struct qemu_alarm_timer *t)
 {
     struct qemu_alarm_win32 *data = t->priv;
+    int nearest_delta_ms;
 
     assert(alarm_has_dynticks(t));
     if (!active_timers[QEMU_CLOCK_REALTIME] &&
@@ -1013,7 +1014,11 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t)
 
     timeKillEvent(data->timerId);
 
-    data->timerId = timeSetEvent(1,
+    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
+    if (nearest_delta_ms < 1) {
+        nearest_delta_ms = 1;
+    }
+    data->timerId = timeSetEvent(nearest_delta_ms,
                         data->period,
                         host_alarm_handler,
                         (DWORD)t,
Signature · 1 line
1.7.4
#3 lore raw

PATCH v2 3/22 use win32 timer queues

In reply to Paolo Bonzini

Multimedia timers are only useful for compatibility with Windows NT 4.0 and earlier. Plus, the implementation in Wine is extremely heavyweight.

---
 qemu-timer.c |   86 +++++++++++++++++++++++----------------------------------
 1 files changed, 35 insertions(+), 51 deletions(-)
qemu-timer.c +35 -51
index 122e7ed..1939d6b 100644
@@ -200,11 +200,6 @@ static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
 
 #ifdef _WIN32
 
-struct qemu_alarm_win32 {
-    MMRESULT timerId;
-    unsigned int period;
-} alarm_win32_data = {0, 0};
-
 static int win32_start_timer(struct qemu_alarm_timer *t);
 static void win32_stop_timer(struct qemu_alarm_timer *t);
 static void win32_rearm_timer(struct qemu_alarm_timer *t);
@@ -298,9 +293,9 @@ static struct qemu_alarm_timer alarm_timers[] = {
     {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
 #else
     {"dynticks", win32_start_timer,
-     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
+     win32_stop_timer, win32_rearm_timer, NULL},
     {"win32", win32_start_timer,
-     win32_stop_timer, NULL, &alarm_win32_data},
+     win32_stop_timer, NULL, NULL},
 #endif
     {NULL, }
 };
@@ -636,9 +631,7 @@ void qemu_run_all_timers(void)
 static int64_t qemu_next_alarm_deadline(void);
 
 #ifdef _WIN32
-static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
-                                        DWORD_PTR dwUser, DWORD_PTR dw1,
-                                        DWORD_PTR dw2)
+static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
 #else
 static void host_alarm_handler(int host_signum)
 #endif
@@ -961,50 +954,45 @@ static void unix_stop_timer(struct qemu_alarm_timer *t)
 
 static int win32_start_timer(struct qemu_alarm_timer *t)
 {
-    TIMECAPS tc;
-    struct qemu_alarm_win32 *data = t->priv;
-    UINT flags;
-
-    memset(&tc, 0, sizeof(tc));
-    timeGetDevCaps(&tc, sizeof(tc));
-
-    data->period = tc.wPeriodMin;
-    timeBeginPeriod(data->period);
-
-    flags = TIME_CALLBACK_FUNCTION;
-    if (alarm_has_dynticks(t))
-        flags |= TIME_ONESHOT;
-    else
-        flags |= TIME_PERIODIC;
-
-    data->timerId = timeSetEvent(1,         // interval (ms)
-                        data->period,       // resolution
-                        host_alarm_handler, // function
-                        (DWORD)t,           // parameter
-                        flags);
-
-    if (!data->timerId) {
+    HANDLE hTimer;
+    BOOLEAN success;
+
+    /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
+       is zero) that has already expired, the timer is not updated.  Since
+       creating a new timer is relatively expensive, set a bogus one-hour
+       interval in the dynticks case.  */
+    success = CreateTimerQueueTimer(&hTimer,
+                          NULL,
+                          host_alarm_handler,
+                          t,
+                          1,
+                          alarm_has_dynticks(t) ? 3600000 : 1,
+                          WT_EXECUTEINTIMERTHREAD);
+
+    if (!success) {
         fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
                 GetLastError());
-        timeEndPeriod(data->period);
         return -1;
     }
 
+    t->priv = (PVOID) hTimer;
     return 0;
 }
 
 static void win32_stop_timer(struct qemu_alarm_timer *t)
 {
-    struct qemu_alarm_win32 *data = t->priv;
+    HANDLE hTimer = t->priv;
 
-    timeKillEvent(data->timerId);
-    timeEndPeriod(data->period);
+    if (hTimer) {
+        DeleteTimerQueueTimer(NULL, hTimer, NULL);
+    }
 }
 
 static void win32_rearm_timer(struct qemu_alarm_timer *t)
 {
-    struct qemu_alarm_win32 *data = t->priv;
+    HANDLE hTimer = t->priv;
     int nearest_delta_ms;
+    BOOLEAN success;
 
     assert(alarm_has_dynticks(t));
     if (!active_timers[QEMU_CLOCK_REALTIME] &&
@@ -1012,25 +1000,21 @@ static void win32_rearm_timer(struct qemu_alarm_timer *t)
         !active_timers[QEMU_CLOCK_HOST])
         return;
 
-    timeKillEvent(data->timerId);
-
     nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
     if (nearest_delta_ms < 1) {
         nearest_delta_ms = 1;
     }
-    data->timerId = timeSetEvent(nearest_delta_ms,
-                        data->period,
-                        host_alarm_handler,
-                        (DWORD)t,
-                        TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
-
-    if (!data->timerId) {
-        fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
-                GetLastError());
+    success = ChangeTimerQueueTimer(NULL,
+                                    hTimer,
+                                    nearest_delta_ms,
+                                    3600000);
 
-        timeEndPeriod(data->period);
-        exit(1);
+    if (!success) {
+        fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
+                GetLastError());
+        exit(-1);
     }
+
 }
 
 #endif /* _WIN32 */
Signature · 1 line
1.7.4
#4 lore raw

PATCH v2 4/22 Refactor thread retrieval and check

In reply to Paolo Bonzini

From: Jan Kiszka <[email protected]>

We have qemu_cpu_self and qemu_thread_self. The latter is retrieving the current thread, the former is checking for equality (using CPUState). We also have qemu_thread_equal which is only used like qemu_cpu_self.

This refactors the interfaces, creating qemu_cpu_is_self and qemu_thread_is_self as well ass qemu_thread_get_self.

---
 cpus.c              |   22 ++++++++--------------
 exec.c              |    2 +-
 qemu-common.h       |    2 +-
 qemu-thread.c       |    6 +++---
 qemu-thread.h       |    4 ++--
 target-i386/kvm.c   |    4 ++--
 ui/vnc-jobs-async.c |    2 +-
 7 files changed, 18 insertions(+), 24 deletions(-)
cpus.c +8 -14
index 0f33945..09ce6fe 100644
@@ -531,7 +531,7 @@ void qemu_init_vcpu(void *_env)
     }
 }
 
-int qemu_cpu_self(void *env)
+int qemu_cpu_is_self(void *env)
 {
     return 1;
 }
@@ -699,7 +699,7 @@ int qemu_init_main_loop(void)
     qemu_mutex_init(&qemu_global_mutex);
     qemu_mutex_lock(&qemu_global_mutex);
 
-    qemu_thread_self(&io_thread);
+    qemu_thread_get_self(&io_thread);
 
     return 0;
 }
@@ -714,7 +714,7 @@ void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
 {
     struct qemu_work_item wi;
 
-    if (qemu_cpu_self(env)) {
+    if (qemu_cpu_is_self(env)) {
         func(data);
         return;
     }
@@ -808,7 +808,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg)
     int r;
 
     qemu_mutex_lock(&qemu_global_mutex);
-    qemu_thread_self(env->thread);
+    qemu_thread_get_self(env->thread);
 
     r = kvm_init_vcpu(env);
     if (r < 0) {
@@ -845,7 +845,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
     CPUState *env = arg;
 
     qemu_tcg_init_cpu_signals();
-    qemu_thread_self(env->thread);
+    qemu_thread_get_self(env->thread);
 
     /* signal CPU creation */
     qemu_mutex_lock(&qemu_global_mutex);
@@ -888,14 +888,11 @@ void qemu_cpu_kick_self(void)
     }
 }
 
-int qemu_cpu_self(void *_env)
+int qemu_cpu_is_self(void *_env)
 {
     CPUState *env = _env;
-    QemuThread this;
 
-    qemu_thread_self(&this);
-
-    return qemu_thread_equal(&this, env->thread);
+    return qemu_thread_is_self(env->thread);
 }
 
 void qemu_mutex_lock_iothread(void)
@@ -1023,10 +1020,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_is_self(&io_thread)) {
         qemu_system_vmstop_request(reason);
         /*
          * FIXME: should not return to device code in case
exec.c +1 -1
index d611100..439a6f6 100644
@@ -1640,7 +1640,7 @@ void cpu_interrupt(CPUState *env, int mask)
      * If called from iothread context, wake the target cpu in
      * case its halted.
      */
-    if (!qemu_cpu_self(env)) {
+    if (!qemu_cpu_is_self(env)) {
         qemu_cpu_kick(env);
         return;
     }
qemu-common.h +1 -1
index 40dad52..958a002 100644
@@ -289,7 +289,7 @@ void qemu_notify_event(void);
 /* Unblock cpu */
 void qemu_cpu_kick(void *env);
 void qemu_cpu_kick_self(void);
-int qemu_cpu_self(void *env);
+int qemu_cpu_is_self(void *env);
 
 /* work queue */
 struct qemu_work_item {
qemu-thread.c +3 -3
index fbc78fe..e307773 100644
@@ -176,14 +176,14 @@ void qemu_thread_signal(QemuThread *thread, int sig)
         error_exit(err, __func__);
 }
 
-void qemu_thread_self(QemuThread *thread)
+void qemu_thread_get_self(QemuThread *thread)
 {
     thread->thread = pthread_self();
 }
 
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
+int qemu_thread_is_self(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 +2 -2
index 19bb30c..add97bf 100644
@@ -37,8 +37,8 @@ void qemu_thread_create(QemuThread *thread,
                        void *(*start_routine)(void*),
                        void *arg);
 void qemu_thread_signal(QemuThread *thread, int sig);
-void qemu_thread_self(QemuThread *thread);
-int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+void qemu_thread_get_self(QemuThread *thread);
+int qemu_thread_is_self(QemuThread *thread);
 void qemu_thread_exit(void *retval);
 
 #endif
target-i386/kvm.c +2 -2
index 0aa0a41..1f12cbf 100644
@@ -1355,7 +1355,7 @@ int kvm_arch_put_registers(CPUState *env, int level)
 {
     int ret;
 
-    assert(cpu_is_stopped(env) || qemu_cpu_self(env));
+    assert(cpu_is_stopped(env) || qemu_cpu_is_self(env));
 
     ret = kvm_getput_regs(env, 1);
     if (ret < 0) {
@@ -1403,7 +1403,7 @@ int kvm_arch_get_registers(CPUState *env)
 {
     int ret;
 
-    assert(cpu_is_stopped(env) || qemu_cpu_self(env));
+    assert(cpu_is_stopped(env) || qemu_cpu_is_self(env));
 
     ret = kvm_getput_regs(env, 0);
     if (ret < 0) {
ui/vnc-jobs-async.c +1 -1
index f596247..1dfa6c3 100644
@@ -303,7 +303,7 @@ static void *vnc_worker_thread(void *arg)
 {
     VncJobQueue *queue = arg;
 
-    qemu_thread_self(&queue->thread);
+    qemu_thread_get_self(&queue->thread);
 
     while (!vnc_worker_thread_loop(queue)) ;
     vnc_queue_clear(queue);
Signature · 1 line
1.7.4
#5 lore raw

PATCH v2 5/22 add win32 qemu-thread implementation

In reply to Paolo Bonzini

For now, qemu_cond_timedwait and qemu_mutex_timedlock are left as POSIX-only functions. They can be removed later, once the patches that remove their uses are in.

---
 Makefile.objs                        |    4 +-
 qemu-thread.c => qemu-thread-posix.c |    0
 qemu-thread-posix.h                  |   18 +++
 qemu-thread-win32.c                  |  260 ++++++++++++++++++++++++++++++++++
 qemu-thread-win32.h                  |   21 +++
 qemu-thread.h                        |   27 ++--
 6 files changed, 313 insertions(+), 17 deletions(-)
 rename qemu-thread.c => qemu-thread-posix.c (100%)
 create mode 100644 qemu-thread-posix.h
 create mode 100644 qemu-thread-win32.c
 create mode 100644 qemu-thread-win32.h
Makefile.objs +2 -2
index 9e98a66..a52f42f 100644
@@ -142,8 +142,8 @@ endif
 common-obj-y += $(addprefix ui/, $(ui-obj-y))
 
 common-obj-y += iov.o acl.o
-common-obj-$(CONFIG_THREAD) += qemu-thread.o
-common-obj-$(CONFIG_POSIX) += compatfd.o
+common-obj-$(CONFIG_POSIX) += qemu-thread-posix.o compatfd.o
+common-obj-$(CONFIG_WIN32) += qemu-thread-win32.o
 common-obj-y += notify.o event_notifier.o
 common-obj-y += qemu-timer.o qemu-timer-common.o
 
qemu-thread-posix.c renamed from qemu-thread.c +0 -0
similarity index 100%
rename from qemu-thread.c
rename to qemu-thread-posix.c
qemu-thread-posix.h new file +18 -0
new file mode 100644
index 0000000..7af371c
@@ -0,0 +1,18 @@
+#ifndef __QEMU_THREAD_POSIX_H
+#define __QEMU_THREAD_POSIX_H 1
+#include "pthread.h"
+
+struct QemuMutex {
+    pthread_mutex_t lock;
+};
+
+struct QemuCond {
+    pthread_cond_t cond;
+};
+
+struct QemuThread {
+    pthread_t thread;
+};
+
+void qemu_thread_signal(QemuThread *thread, int sig);
+#endif
qemu-thread-win32.c new file +260 -0

260 lines not on this page · raw message

qemu-thread-win32.h new file +21 -0
new file mode 100644
index 0000000..878f86a
@@ -0,0 +1,21 @@
+#ifndef __QEMU_THREAD_WIN32_H
+#define __QEMU_THREAD_WIN32_H 1
+#include "windows.h"
+
+struct QemuMutex {
+    CRITICAL_SECTION lock;
+    LONG owner;
+};
+
+struct QemuCond {
+    LONG waiters, target;
+    HANDLE sema;
+    HANDLE continue_event;
+};
+
+struct QemuThread {
+    HANDLE thread;
+    void *ret;
+};
+
+#endif
qemu-thread.h +12 -15
index add97bf..acdb6b2 100644
@@ -1,24 +1,16 @@
 #ifndef __QEMU_THREAD_H
 #define __QEMU_THREAD_H 1
-#include "semaphore.h"
-#include "pthread.h"
-
-struct QemuMutex {
-    pthread_mutex_t lock;
-};
-
-struct QemuCond {
-    pthread_cond_t cond;
-};
-
-struct QemuThread {
-    pthread_t thread;
-};
 
 typedef struct QemuMutex QemuMutex;
 typedef struct QemuCond QemuCond;
 typedef struct QemuThread QemuThread;
 
+#ifdef _WIN32
+#include "qemu-thread-win32.h"
+#else
+#include "qemu-thread-posix.h"
+#endif
+
 void qemu_mutex_init(QemuMutex *mutex);
 void qemu_mutex_destroy(QemuMutex *mutex);
 void qemu_mutex_lock(QemuMutex *mutex);
@@ -28,6 +20,12 @@ void qemu_mutex_unlock(QemuMutex *mutex);
 
 void qemu_cond_init(QemuCond *cond);
 void qemu_cond_destroy(QemuCond *cond);
+
+/*
+ * IMPORTANT: The implementation does not guarantee that pthread_cond_signal
+ * and pthread_cond_broadcast can be called except while the same mutex is
+ * held as in the corresponding pthread_cond_wait calls!
+ */
 void qemu_cond_signal(QemuCond *cond);
 void qemu_cond_broadcast(QemuCond *cond);
 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
@@ -36,7 +34,6 @@ int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs);
 void qemu_thread_create(QemuThread *thread,
                        void *(*start_routine)(void*),
                        void *arg);
-void qemu_thread_signal(QemuThread *thread, int sig);
 void qemu_thread_get_self(QemuThread *thread);
 int qemu_thread_is_self(QemuThread *thread);
 void qemu_thread_exit(void *retval);
Signature · 1 line
1.7.4
#6 lore raw

Re: [Qemu-devel] [PATCH v2 upstream 05/22] add win32 qemu-thread implementation

Quoted 231 lines · Paolo Bonzini

For now, qemu_cond_timedwait and qemu_mutex_timedlock are left as POSIX-only functions.  They can be removed later, once the patches that remove their uses are in.

---
 Makefile.objs                        |    4 +-
 qemu-thread.c => qemu-thread-posix.c |    0

 qemu-thread-posix.h                  |   18 +++
 qemu-thread-win32.c                  |  260 ++++++++++++++++++++++++++++++++++
 qemu-thread-win32.h                  |   21 +++
 qemu-thread.h                        |   27 ++--
 6 files changed, 313 insertions(+), 17 deletions(-)

 rename qemu-thread.c => qemu-thread-posix.c (100%) create mode 100644 qemu-thread-posix.h create mode 100644 qemu-thread-win32.c create mode 100644 qemu-thread-win32.h

Makefile.objs +0 -0
index 9e98a66..a52f42f 100644
@@ -142,8 +142,8 @@ endif
  common-obj-y += $(addprefix ui/, $(ui-obj-y))
 

 common-obj-y += iov.o acl.o

Makefile.objs +2 -2
-common-obj-$(CONFIG_THREAD) += qemu-thread.o
-common-obj-$(CONFIG_POSIX) += compatfd.o
+common-obj-$(CONFIG_POSIX) += qemu-thread-posix.o compatfd.o
+common-obj-$(CONFIG_WIN32) += qemu-thread-win32.o

 common-obj-y += notify.o event_notifier.o
 common-obj-y += qemu-timer.o qemu-timer-common.o

qemu-thread-posix.c renamed from qemu-thread.c +0 -0
similarity index 100%
rename from qemu-thread.c
rename to qemu-thread-posix.c
qemu-thread-posix.h new file +18 -0
new file mode 100644
index 0000000..7af371c
@@ -0,0 +1,18 @@
+#ifndef __QEMU_THREAD_POSIX_H
+#define __QEMU_THREAD_POSIX_H 1
+#include "pthread.h"
+
+struct QemuMutex {
+    pthread_mutex_t lock;
+};
+
+struct QemuCond {
+    pthread_cond_t cond;
+};
+
+struct QemuThread {
+    pthread_t thread;
+};
+
+void qemu_thread_signal(QemuThread *thread, int sig);
+#endif
qemu-thread-win32.c new file +164 -0
new file mode 100644
index 0000000..89422ce
@@ -0,0 +1,260 @@
+/*
+ * Win32 implementation for mutex/cond/thread functions
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Author:
+ *  Paolo Bonzini <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "qemu-common.h"
+#include "qemu-thread.h"
+#include <process.h>
+#include <assert.h>
+#include <limits.h>
+
+static void error_exit(int err, const char *msg)
+{
+    char *pstr;
+
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+                  NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
+    fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
+    LocalFree(pstr);
+    exit(1);
+}
+
+void qemu_mutex_init(QemuMutex *mutex)
+{
+    mutex->owner = 0;
+    InitializeCriticalSection(&mutex->lock);
+}
+
+void qemu_mutex_lock(QemuMutex *mutex)
+{
+    EnterCriticalSection(&mutex->lock);
+
+    /* Win32 CRITICAL_SECTIONs are recursive.  Assert that we're not
+     * using them as such.
+     */
+    assert(mutex->owner == 0);
+    mutex->owner = GetCurrentThreadId();
+}
+
+int qemu_mutex_trylock(QemuMutex *mutex)
+{
+    int owned;
+
+    owned = TryEnterCriticalSection(&mutex->lock);
+    if (owned) {
+        assert(mutex->owner == 0);
+        mutex->owner = GetCurrentThreadId();
+    }
+    return !owned;
+}
+
+void qemu_mutex_unlock(QemuMutex *mutex)
+{
+    assert(mutex->owner == GetCurrentThreadId());
+    mutex->owner = 0;
+    LeaveCriticalSection(&mutex->lock);
+}
+
+void qemu_cond_init(QemuCond *cond)
+{
+    memset(cond, 0, sizeof(*cond));
+
+    cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+    if (!cond->sema) {
+        error_exit(GetLastError(), __func__);
+    }
+    cond->continue_event = CreateEvent(NULL,    /* security */
+                                       FALSE,   /* auto-reset */
+                                       FALSE,   /* not signaled */
+                                       NULL);   /* name */
+    if (!cond->continue_event) {
+        error_exit(GetLastError(), __func__);
+    }
+}
+
+void qemu_cond_signal(QemuCond *cond)
+{
+    DWORD result;
+
+    /*
+     * Signal only when there are waiters.  cond->waiters is
+     * incremented by pthread_cond_wait under the external lock,
+     * so we are safe about that.
+     */
+    if (cond->waiters == 0) {
+        return;
+    }
+
+    /*
+     * Waiting threads decrement it outside the external lock, but
+     * only if another thread is executing pthread_cond_broadcast and
+     * has the mutex.  So, it also cannot be decremented concurrently
+     * with this particular access.
+     */
+    cond->target = cond->waiters - 1;
+    result = SignalObjectAndWait(cond->sema, cond->continue_event,
+                                 INFINITE, FALSE);
+    if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
+        error_exit(GetLastError(), __func__);
+    }
+}
+
+void qemu_cond_broadcast(QemuCond *cond)
+{
+    BOOLEAN result;
+    /*
+     * As in pthread_cond_signal, access to cond->waiters and
+     * cond->target is locked via the external mutex.
+     */
+    if (cond->waiters == 0) {
+        return;
+    }
+
+    cond->target = 0;
+    result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
+    if (!result) {
+        error_exit(GetLastError(), __func__);
+    }
+
+    /*
+     * At this point all waiters continue. Each one takes its
+     * slice of the semaphore. Now it's our turn to wait: Since
+     * the external mutex is held, no thread can leave cond_wait,
+     * yet. For this reason, we can be sure that no thread gets
+     * a chance to eat *more* than one slice. OTOH, it means
+     * that the last waiter must send us a wake-up.
+     */
+    WaitForSingleObject(cond->continue_event, INFINITE);
+}
+
+void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
+{
+    /*
+     * This access is protected under the mutex.
+     */
+    cond->waiters++;
+
+    /*
+     * Unlock external mutex and wait for signal.
+     * NOTE: we've held mutex locked long enough to increment
+     * waiters count above, so there's no problem with
+     * leaving mutex unlocked before we wait on semaphore.
+     */
+    qemu_mutex_unlock(mutex);
+    WaitForSingleObject(cond->sema, INFINITE);
+
+    /* Now waiters must rendez-vous with the signaling thread and
+     * let it continue.  For cond_broadcast this has heavy contention
+     * and triggers thundering herd.  So goes life.
+     *
+     * Decrease waiters count.  The mutex is not taken, so we have
+     * to do this atomically.
+     *
+     * All waiters contend for the mutex at the end of this function
+     * until the signaling thread relinquishes it.  To ensure
+     * each waiter consumes exactly one slice of the semaphore,
+     * the signaling thread stops until it is told by the last

qemu-thread-win32.c line 174

Quoted 10 lines
qemu-thread-win32.c new file +10 -0
+     * waiter that it can go on.
+     */
+    if (InterlockedDecrement(&cond->waiters) == cond->target) {
+        SetEvent(cond->continue_event);
+    }
+
+    qemu_mutex_lock(mutex);
+}
+
+struct qemu_thread_data {

QemuThreadData?

#7 lore raw

[Qemu-devel] Re: [PATCH v2 upstream 05/22] add win32 qemu-thread implementation

Quoted 236 lines · Blue Swirl
Quoted 235 lines · Paolo Bonzini

For now, qemu_cond_timedwait and qemu_mutex_timedlock are left as POSIX-only functions. They can be removed later, once the patches that remove their uses are in.

---
  Makefile.objs                        |    4 +-
  qemu-thread.c =>  qemu-thread-posix.c |    0
  qemu-thread-posix.h                  |   18 +++
  qemu-thread-win32.c                  |  260 ++++++++++++++++++++++++++++++++++
  qemu-thread-win32.h                  |   21 +++
  qemu-thread.h                        |   27 ++--
  6 files changed, 313 insertions(+), 17 deletions(-)

rename qemu-thread.c => qemu-thread-posix.c (100%) create mode 100644 qemu-thread-posix.h create mode 100644 qemu-thread-win32.c create mode 100644 qemu-thread-win32.h

Makefile.objs +2 -2
index 9e98a66..a52f42f 100644
@@ -142,8 +142,8 @@ endif
  common-obj-y += $(addprefix ui/, $(ui-obj-y))
 
  common-obj-y += iov.o acl.o
-common-obj-$(CONFIG_THREAD) += qemu-thread.o
-common-obj-$(CONFIG_POSIX) += compatfd.o
+common-obj-$(CONFIG_POSIX) += qemu-thread-posix.o compatfd.o
+common-obj-$(CONFIG_WIN32) += qemu-thread-win32.o
  common-obj-y += notify.o event_notifier.o
  common-obj-y += qemu-timer.o qemu-timer-common.o
 
qemu-thread-posix.c renamed from qemu-thread.c +0 -0
similarity index 100%
rename from qemu-thread.c
rename to qemu-thread-posix.c
qemu-thread-posix.h new file +18 -0
new file mode 100644
index 0000000..7af371c
@@ -0,0 +1,18 @@
+#ifndef __QEMU_THREAD_POSIX_H
+#define __QEMU_THREAD_POSIX_H 1
+#include "pthread.h"
+
+struct QemuMutex {
+    pthread_mutex_t lock;
+};
+
+struct QemuCond {
+    pthread_cond_t cond;
+};
+
+struct QemuThread {
+    pthread_t thread;
+};
+
+void qemu_thread_signal(QemuThread *thread, int sig);
+#endif
qemu-thread-win32.c new file +168 -0
new file mode 100644
index 0000000..89422ce
@@ -0,0 +1,260 @@
+/*
+ * Win32 implementation for mutex/cond/thread functions
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Author:
+ *  Paolo Bonzini<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#include "qemu-common.h"
+#include "qemu-thread.h"
+#include<process.h>
+#include<assert.h>
+#include<limits.h>
+
+static void error_exit(int err, const char *msg)
+{
+    char *pstr;
+
+    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
+                  NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
+    fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
+    LocalFree(pstr);
+    exit(1);
+}
+
+void qemu_mutex_init(QemuMutex *mutex)
+{
+    mutex->owner = 0;
+    InitializeCriticalSection(&mutex->lock);
+}
+
+void qemu_mutex_lock(QemuMutex *mutex)
+{
+    EnterCriticalSection(&mutex->lock);
+
+    /* Win32 CRITICAL_SECTIONs are recursive.  Assert that we're not
+     * using them as such.
+     */
+    assert(mutex->owner == 0);
+    mutex->owner = GetCurrentThreadId();
+}
+
+int qemu_mutex_trylock(QemuMutex *mutex)
+{
+    int owned;
+
+    owned = TryEnterCriticalSection(&mutex->lock);
+    if (owned) {
+        assert(mutex->owner == 0);
+        mutex->owner = GetCurrentThreadId();
+    }
+    return !owned;
+}
+
+void qemu_mutex_unlock(QemuMutex *mutex)
+{
+    assert(mutex->owner == GetCurrentThreadId());
+    mutex->owner = 0;
+    LeaveCriticalSection(&mutex->lock);
+}
+
+void qemu_cond_init(QemuCond *cond)
+{
+    memset(cond, 0, sizeof(*cond));
+
+    cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+    if (!cond->sema) {
+        error_exit(GetLastError(), __func__);
+    }
+    cond->continue_event = CreateEvent(NULL,    /* security */
+                                       FALSE,   /* auto-reset */
+                                       FALSE,   /* not signaled */
+                                       NULL);   /* name */
+    if (!cond->continue_event) {
+        error_exit(GetLastError(), __func__);
+    }
+}
+
+void qemu_cond_signal(QemuCond *cond)
+{
+    DWORD result;
+
+    /*
+     * Signal only when there are waiters.  cond->waiters is
+     * incremented by pthread_cond_wait under the external lock,
+     * so we are safe about that.
+     */
+    if (cond->waiters == 0) {
+        return;
+    }
+
+    /*
+     * Waiting threads decrement it outside the external lock, but
+     * only if another thread is executing pthread_cond_broadcast and
+     * has the mutex.  So, it also cannot be decremented concurrently
+     * with this particular access.
+     */
+    cond->target = cond->waiters - 1;
+    result = SignalObjectAndWait(cond->sema, cond->continue_event,
+                                 INFINITE, FALSE);
+    if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
+        error_exit(GetLastError(), __func__);
+    }
+}
+
+void qemu_cond_broadcast(QemuCond *cond)
+{
+    BOOLEAN result;
+    /*
+     * As in pthread_cond_signal, access to cond->waiters and
+     * cond->target is locked via the external mutex.
+     */
+    if (cond->waiters == 0) {
+        return;
+    }
+
+    cond->target = 0;
+    result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
+    if (!result) {
+        error_exit(GetLastError(), __func__);
+    }
+
+    /*
+     * At this point all waiters continue. Each one takes its
+     * slice of the semaphore. Now it's our turn to wait: Since
+     * the external mutex is held, no thread can leave cond_wait,
+     * yet. For this reason, we can be sure that no thread gets
+     * a chance to eat *more* than one slice. OTOH, it means
+     * that the last waiter must send us a wake-up.
+     */
+    WaitForSingleObject(cond->continue_event, INFINITE);
+}
+
+void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
+{
+    /*
+     * This access is protected under the mutex.
+     */
+    cond->waiters++;
+
+    /*
+     * Unlock external mutex and wait for signal.
+     * NOTE: we've held mutex locked long enough to increment
+     * waiters count above, so there's no problem with
+     * leaving mutex unlocked before we wait on semaphore.
+     */
+    qemu_mutex_unlock(mutex);
+    WaitForSingleObject(cond->sema, INFINITE);
+
+    /* Now waiters must rendez-vous with the signaling thread and
+     * let it continue.  For cond_broadcast this has heavy contention
+     * and triggers thundering herd.  So goes life.
+     *
+     * Decrease waiters count.  The mutex is not taken, so we have
+     * to do this atomically.
+     *
+     * All waiters contend for the mutex at the end of this function
+     * until the signaling thread relinquishes it.  To ensure
+     * each waiter consumes exactly one slice of the semaphore,
+     * the signaling thread stops until it is told by the last
+     * waiter that it can go on.
+     */
+    if (InterlockedDecrement(&cond->waiters) == cond->target) {
+        SetEvent(cond->continue_event);
Quoted 8 lines

qemu-thread-win32.c line 174

Quoted 6 lines
qemu-thread-win32.c new file +6 -0
+    }
+
+    qemu_mutex_lock(mutex);
+}
+
+struct qemu_thread_data {

QemuThreadData?

Ok, will resubmit asap. Can you add this convention to checkpatch?

Paolo

#8 lore raw

PATCH v2 6/22 include qemu-thread.h early

In reply to Paolo Bonzini
---
 cpus.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
cpus.c +1 -2
index 09ce6fe..c5743c4 100644
@@ -32,6 +32,7 @@
 #include "kvm.h"
 #include "exec-all.h"
 
+#include "qemu-thread.h"
 #include "cpus.h"
 #include "compatfd.h"
 
@@ -592,8 +593,6 @@ void vm_stop(int reason)
 
 #else /* CONFIG_IOTHREAD */
 
-#include "qemu-thread.h"
-
 QemuMutex qemu_global_mutex;
 static QemuMutex qemu_fair_mutex;
 
Signature · 1 line
1.7.4
#9 lore raw

PATCH v2 7/22 add assertions on the owner of a QemuMutex

In reply to Paolo Bonzini

These are already present in the Win32 implementation, add them to the pthread wrappers as well. Use PTHREAD_MUTEX_ERRORCHECK for mutex operations, and track the owner separately for cond_signal/broadcast.

---
 qemu-thread-posix.c |   23 +++++++++++++++++++++--
 qemu-thread-posix.h |    1 +
 2 files changed, 22 insertions(+), 2 deletions(-)
qemu-thread-posix.c +21 -2
index e307773..a4c6e25 100644
@@ -16,9 +16,12 @@
 #include <time.h>
 #include <signal.h>
 #include <stdint.h>
+#include <assert.h>
 #include <string.h>
 #include "qemu-thread.h"
 
+static pthread_t pthread_null;
+
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
@@ -28,8 +31,13 @@ static void error_exit(int err, const char *msg)
 void qemu_mutex_init(QemuMutex *mutex)
 {
     int err;
+    pthread_mutexattr_t mutexattr;
 
-    err = pthread_mutex_init(&mutex->lock, NULL);
+    mutex->owner = pthread_null;
+    pthread_mutexattr_init(&mutexattr);
+    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
+    err = pthread_mutex_init(&mutex->lock, &mutexattr);
+    pthread_mutexattr_destroy(&mutexattr);
     if (err)
         error_exit(err, __func__);
 }
@@ -48,13 +56,20 @@ void qemu_mutex_lock(QemuMutex *mutex)
     int err;
 
     err = pthread_mutex_lock(&mutex->lock);
+    mutex->owner = pthread_self();
     if (err)
         error_exit(err, __func__);
 }
 
 int qemu_mutex_trylock(QemuMutex *mutex)
 {
-    return pthread_mutex_trylock(&mutex->lock);
+    int err;
+    err = pthread_mutex_trylock(&mutex->lock);
+    if (err == 0) {
+        mutex->owner = pthread_self();
+    }
+
+    return !!err;
 }
 
 static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
@@ -85,6 +100,7 @@ void qemu_mutex_unlock(QemuMutex *mutex)
 {
     int err;
 
+    mutex->owner = pthread_null;
     err = pthread_mutex_unlock(&mutex->lock);
     if (err)
         error_exit(err, __func__);
@@ -130,7 +146,10 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
 {
     int err;
 
+    assert(pthread_equal(mutex->owner, pthread_self()));
+    mutex->owner = pthread_null;
     err = pthread_cond_wait(&cond->cond, &mutex->lock);
+    mutex->owner = pthread_self();
     if (err)
         error_exit(err, __func__);
 }
qemu-thread-posix.h +1 -0
index 7af371c..11978db 100644
@@ -4,6 +4,7 @@
 
 struct QemuMutex {
     pthread_mutex_t lock;
+    pthread_t owner;
 };
 
 struct QemuCond {
Signature · 1 line
1.7.4
#10 lore raw

[Qemu-devel] Re: [PATCH v2 upstream 07/22] add assertions on the owner of a QemuMutex

Quoted 73 lines · Paolo Bonzini

These are already present in the Win32 implementation, add them to the pthread wrappers as well. Use PTHREAD_MUTEX_ERRORCHECK for mutex operations, and track the owner separately for cond_signal/broadcast.

---
 qemu-thread-posix.c |   23 +++++++++++++++++++++--
 qemu-thread-posix.h |    1 +
 2 files changed, 22 insertions(+), 2 deletions(-)
qemu-thread-posix.c +18 -2
index e307773..a4c6e25 100644
@@ -16,9 +16,12 @@
 #include <time.h>
 #include <signal.h>
 #include <stdint.h>
+#include <assert.h>
 #include <string.h>
 #include "qemu-thread.h"
 
+static pthread_t pthread_null;
+
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
@@ -28,8 +31,13 @@ static void error_exit(int err, const char *msg)
 void qemu_mutex_init(QemuMutex *mutex)
 {
     int err;
+    pthread_mutexattr_t mutexattr;
 
-    err = pthread_mutex_init(&mutex->lock, NULL);
+    mutex->owner = pthread_null;
+    pthread_mutexattr_init(&mutexattr);
+    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
+    err = pthread_mutex_init(&mutex->lock, &mutexattr);
+    pthread_mutexattr_destroy(&mutexattr);
     if (err)
         error_exit(err, __func__);
 }
@@ -48,13 +56,20 @@ void qemu_mutex_lock(QemuMutex *mutex)
     int err;
 
     err = pthread_mutex_lock(&mutex->lock);
+    mutex->owner = pthread_self();
     if (err)
         error_exit(err, __func__);
 }
 
 int qemu_mutex_trylock(QemuMutex *mutex)
 {
-    return pthread_mutex_trylock(&mutex->lock);
+    int err;
+    err = pthread_mutex_trylock(&mutex->lock);
+    if (err == 0) {
+        mutex->owner = pthread_self();
+    }
+
+    return !!err;
 }
 
 static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
@@ -85,6 +100,7 @@ void qemu_mutex_unlock(QemuMutex *mutex)
 {
     int err;
 
+    mutex->owner = pthread_null;
     err = pthread_mutex_unlock(&mutex->lock);
     if (err)
         error_exit(err, __func__);

qemu-thread-posix.c void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) · line 151

Quoted 6 lines
qemu-thread-posix.c +2 -0
@@ -130,7 +146,10 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
 {
     int err;
 
+    assert(pthread_equal(mutex->owner, pthread_self()));
+    mutex->owner = pthread_null;
     err = pthread_cond_wait(&cond->cond, &mutex->lock);

Though POSIX is not 100% explicit on this, every sane pthread_cond_wait implementation will apply the same error checking as on pthread_mutex_unlock when the given mutex is of PTHREAD_MUTEX_ERRORCHECK. So, this assert is actually redundant as well.

Now that we are left without any assertions, I start wondering about one of the original missions: enforce qemu_cond_signal/broadcast to be called under a mutex. What about extending those services with a mutex argument and applying the assert there? Could become a static-inline wrapper so that the argument is optimized away if assert() is inactive.

Jan

#11 lore raw

[Qemu-devel] Re: [PATCH v2 upstream 07/22] add assertions on the owner of a QemuMutex

Quoted 4 lines · Jan Kiszka

Now that we are left without any assertions, I start wondering about one of the original missions: enforce qemu_cond_signal/broadcast to be called under a mutex. What about extending those services with a mutex argument and applying the assert there?

That is one of the patches in my queue that I haven't submitted yet. :)

Paolo

#12 lore raw

PATCH v2 8/22 remove CONFIG_THREAD

In reply to Paolo Bonzini
---
 configure |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)
configure +0 -2
index 3036faf..4de26d2 100755
@@ -2662,7 +2662,6 @@ if test "$vnc_png" != "no" ; then
 fi
 if test "$vnc_thread" != "no" ; then
   echo "CONFIG_VNC_THREAD=y" >> $config_host_mak
-  echo "CONFIG_THREAD=y" >> $config_host_mak
 fi
 if test "$fnmatch" = "yes" ; then
   echo "CONFIG_FNMATCH=y" >> $config_host_mak
@@ -2758,7 +2757,6 @@ if test "$xen" = "yes" ; then
 fi
 if test "$io_thread" = "yes" ; then
   echo "CONFIG_IOTHREAD=y" >> $config_host_mak
-  echo "CONFIG_THREAD=y" >> $config_host_mak
 fi
 if test "$linux_aio" = "yes" ; then
   echo "CONFIG_LINUX_AIO=y" >> $config_host_mak
Signature · 1 line
1.7.4
#13 lore raw

PATCH v2 9/22 target-sh4: move intr_at_halt out of cpu_halted()

In reply to Paolo Bonzini

From: Aurelien Jarno <[email protected]>

All targets except SH4 have the same cpu_halted() routine, and it has only one caller. It is therefore a good candidate for inlining.

The difference is the handling of the intr_at_halt, which is necessary to ignore SR.BL when sleeping. Move intr_at_halt handling out of it, by setting this variable while executing the sleep instruction, and clearing it when the CPU has been woken-up by an interrupt, whatever the state of SR.BL. Also rename this variable in_sleep.

---
 target-sh4/cpu.h       |    2 +-
 target-sh4/exec.h      |    1 -
 target-sh4/helper.c    |    4 ++--
 target-sh4/op_helper.c |    1 +
 4 files changed, 4 insertions(+), 4 deletions(-)
target-sh4/cpu.h +1 -1
index 789d188..74ff97a 100644
@@ -184,7 +184,7 @@ typedef struct CPUSH4State {
     uint32_t cvr;		/* Cache Version Register */
 
     void *intc_handle;
-    int intr_at_halt;		/* SR_BL ignored during sleep */
+    int in_sleep;		/* SR_BL ignored during sleep */
     memory_content *movcal_backup;
     memory_content **movcal_backup_tail;
 } CPUSH4State;
target-sh4/exec.h +0 -1
index 2999c02..61bc121 100644
@@ -37,7 +37,6 @@ static inline int cpu_halted(CPUState *env) {
         return 0;
     if (cpu_has_work(env)) {
         env->halted = 0;
-        env->intr_at_halt = 1;
         return 0;
     }
     return EXCP_HALTED;
target-sh4/helper.c +2 -2
index d2038bd..8f36d31 100644
@@ -90,11 +90,11 @@ void do_interrupt(CPUState * env)
         if (do_exp && env->exception_index != 0x1e0) {
             env->exception_index = 0x000; /* masked exception -> reset */
         }
-        if (do_irq && !env->intr_at_halt) {
+        if (do_irq && !env->in_sleep) {
             return; /* masked */
         }
-        env->intr_at_halt = 0;
     }
+    env->in_sleep = 0;
 
     if (do_irq) {
         irq_vector = sh_intc_get_pending_vector(env->intc_handle,
target-sh4/op_helper.c +1 -0
index 30f9842..b8f4ca2 100644
@@ -119,6 +119,7 @@ void helper_debug(void)
 void helper_sleep(uint32_t next_pc)
 {
     env->halted = 1;
+    env->in_sleep = 1;
     env->exception_index = EXCP_HLT;
     env->pc = next_pc;
     cpu_loop_exit();
Signature · 1 line
1.7.4
#14 lore raw

PATCH v2 10/22 inline cpu_halted into sole caller

In reply to Paolo Bonzini

All implementations are now the same, and there is only one caller, so inline the function there.

---
 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/exec.h        |   10 ----------
 target-sparc/exec.h      |   10 ----------
 12 files changed, 7 insertions(+), 124 deletions(-)
cpu-exec.c +7 -2
index b03b3a7..eed9282 100644
@@ -208,8 +208,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;
+    }
 
     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/exec.h +0 -10
index 61bc121..9f1c1f6 100644
@@ -32,16 +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;
-        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.4
#15 lore raw

PATCH v2 11/22 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/ppc.c           |    2 ++
 hw/sun4m.c         |   10 ++++++++--
 hw/sun4u.c         |    4 ++--
 target-s390x/kvm.c |    1 +
 4 files changed, 13 insertions(+), 4 deletions(-)
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..df3aa32 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/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 b349812..6e94274 100644
@@ -194,6 +194,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.4
#16 lore raw

PATCH v2 12/22 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 c5743c4..785a104 100644
@@ -1098,7 +1098,7 @@ bool cpu_exec_all(void)
                 cpu_handle_debug_exception(env);
                 break;
             }
-        } else if (env->stop) {
+        } else if (env->stop || env->stopped) {
             break;
         }
     }
Signature · 1 line
1.7.4
#17 lore raw

PATCH v2 13/22 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 785a104..6cfb45b 100644
@@ -1012,8 +1012,10 @@ void qemu_notify_event(void)
 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.4
#18 lore raw

PATCH v2 14/22 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 6cfb45b..4c3837f 100644
@@ -771,7 +771,7 @@ static void qemu_tcg_wait_io_event(void)
     CPUState *env;
 
     while (all_cpu_threads_idle()) {
-        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);
@@ -794,7 +794,7 @@ static void qemu_tcg_wait_io_event(void)
 static void qemu_kvm_wait_io_event(CPUState *env)
 {
     while (cpu_thread_is_idle(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);
Signature · 1 line
1.7.4
#19 lore raw

PATCH v2 15/22 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.

---
 cpus.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
index 4c3837f..e367b3b 100644
@@ -823,7 +823,7 @@ static void *qemu_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) {
@@ -855,7 +855,7 @@ static void *qemu_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) {
Signature · 1 line
1.7.4
#20 lore raw

PATCH v2 16/22 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 e367b3b..8f169ad 100644
@@ -938,7 +938,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.4
#21 lore raw

PATCH v2 17/22 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.

---
 cpus.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
index 8f169ad..4305184 100644
@@ -970,7 +970,7 @@ static void qemu_tcg_init_vcpu(void *_env)
         qemu_cond_init(env->halt_cond);
         qemu_thread_create(env->thread, qemu_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;
@@ -987,7 +987,7 @@ static void qemu_kvm_start_vcpu(CPUState *env)
     qemu_cond_init(env->halt_cond);
     qemu_thread_create(env->thread, qemu_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);
     }
 }
 
Signature · 1 line
1.7.4
#22 lore raw

PATCH v2 18/22 iothread stops the vcpu thread via IPI

In reply to Paolo Bonzini
---
 cpus.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
cpus.c +2 -0
index 4305184..32e9352 100644
@@ -1086,9 +1086,11 @@ 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)) {
             if (kvm_enabled()) {
                 r = kvm_cpu_exec(env);
Signature · 1 line
1.7.4
#23 lore raw

PATCH v2 19/22 move blocking of signals to qemu_signalfd_init

In reply to Paolo Bonzini
---
 cpus.c |   87 ++++++++++++++++++++++++++-------------------------------------
 1 files changed, 36 insertions(+), 51 deletions(-)
cpus.c +36 -51
index 32e9352..8c440f1 100644
@@ -346,11 +346,37 @@ static void sigfd_handler(void *opaque)
     }
 }
 
-static int qemu_signalfd_init(sigset_t mask)
+static int qemu_signalfd_init(void)
 {
     int sigfd;
+    sigset_t set;
 
-    sigfd = qemu_signalfd(&mask);
+#ifdef CONFIG_IOTHREAD
+    /* SIGUSR2 used by posix-aio-compat.c */
+    sigemptyset(&set);
+    sigaddset(&set, SIGUSR2);
+    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
+
+    sigemptyset(&set);
+    sigaddset(&set, SIGIO);
+    sigaddset(&set, SIGALRM);
+    sigaddset(&set, SIG_IPI);
+    sigaddset(&set, SIGBUS);
+    pthread_sigmask(SIG_BLOCK, &set, NULL);
+#else
+    sigemptyset(&set);
+    sigaddset(&set, SIGBUS);
+    if (kvm_enabled()) {
+        /*
+         * We need to process timer signals synchronously to avoid a race
+         * between exit_request check and KVM vcpu entry.
+         */
+        sigaddset(&set, SIGIO);
+        sigaddset(&set, SIGALRM);
+    }
+#endif
+
+    sigfd = qemu_signalfd(&set);
     if (sigfd == -1) {
         fprintf(stderr, "failed to create signalfd\n");
         return -errno;
@@ -438,6 +464,12 @@ static void qemu_event_increment(void)
 static void qemu_kvm_eat_signals(CPUState *env)
 {
 }
+
+static int qemu_signalfd_init(void)
+{
+    return 0;
+}
+
 #endif /* _WIN32 */
 
 #ifndef CONFIG_IOTHREAD
@@ -471,39 +503,14 @@ static void qemu_kvm_init_cpu_signals(CPUState *env)
 #endif
 }
 
-#ifndef _WIN32
-static sigset_t block_synchronous_signals(void)
-{
-    sigset_t set;
-
-    sigemptyset(&set);
-    sigaddset(&set, SIGBUS);
-    if (kvm_enabled()) {
-        /*
-         * We need to process timer signals synchronously to avoid a race
-         * between exit_request check and KVM vcpu entry.
-         */
-        sigaddset(&set, SIGIO);
-        sigaddset(&set, SIGALRM);
-    }
-
-    return set;
-}
-#endif
-
 int qemu_init_main_loop(void)
 {
-#ifndef _WIN32
-    sigset_t blocked_signals;
     int ret;
 
-    blocked_signals = block_synchronous_signals();
-
-    ret = qemu_signalfd_init(blocked_signals);
+    ret = qemu_signalfd_init();
     if (ret) {
         return ret;
     }
-#endif
 
     qemu_init_sigbus();
 
@@ -651,35 +658,13 @@ static void qemu_tcg_init_cpu_signals(void)
     pthread_sigmask(SIG_UNBLOCK, &set, NULL);
 }
 
-static sigset_t block_io_signals(void)
-{
-    sigset_t set;
-
-    /* SIGUSR2 used by posix-aio-compat.c */
-    sigemptyset(&set);
-    sigaddset(&set, SIGUSR2);
-    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
-
-    sigemptyset(&set);
-    sigaddset(&set, SIGIO);
-    sigaddset(&set, SIGALRM);
-    sigaddset(&set, SIG_IPI);
-    sigaddset(&set, SIGBUS);
-    pthread_sigmask(SIG_BLOCK, &set, NULL);
-
-    return set;
-}
-
 int qemu_init_main_loop(void)
 {
     int ret;
-    sigset_t blocked_signals;
 
     qemu_init_sigbus();
 
-    blocked_signals = block_io_signals();
-
-    ret = qemu_signalfd_init(blocked_signals);
+    ret = qemu_signalfd_init();
     if (ret) {
         return ret;
     }
Signature · 1 line
1.7.4
#24 lore raw

[Qemu-devel] Re: [PATCH v2 upstream 19/22] move blocking of signals to qemu_signalfd_init

Quoted 15 lines · Paolo Bonzini
---
 cpus.c |   87 ++++++++++++++++++++++++++-------------------------------------
 1 files changed, 36 insertions(+), 51 deletions(-)
cpus.c +1 -1
index 32e9352..8c440f1 100644
@@ -346,11 +346,37 @@ static void sigfd_handler(void *opaque)
     }
 }
 
-static int qemu_signalfd_init(sigset_t mask)
+static int qemu_signalfd_init(void)

cpus.c static void sigfd_handler(void *opaque) · line 358

Quoted 10 lines
cpus.c +6 -1
 {
     int sigfd;
+    sigset_t set;
 
-    sigfd = qemu_signalfd(&mask);
+#ifdef CONFIG_IOTHREAD
+    /* SIGUSR2 used by posix-aio-compat.c */
+    sigemptyset(&set);
+    sigaddset(&set, SIGUSR2);
+    pthread_sigmask(SIG_UNBLOCK, &set, NULL);

Didn't you want to rename the function for the sake of non-signalfd blocks like above?

cpus.c static void sigfd_handler(void *opaque) · line 367

Quoted 9 lines
cpus.c +9 -0
+
+    sigemptyset(&set);
+    sigaddset(&set, SIGIO);
+    sigaddset(&set, SIGALRM);
+    sigaddset(&set, SIG_IPI);
+    sigaddset(&set, SIGBUS);
+    pthread_sigmask(SIG_BLOCK, &set, NULL);
+#else
+    sigemptyset(&set);

This line is shared and can be moved out of the #ifdef.

Quoted 96 lines
cpus.c +19 -46
+    sigaddset(&set, SIGBUS);
+    if (kvm_enabled()) {
+        /*
+         * We need to process timer signals synchronously to avoid a race
+         * between exit_request check and KVM vcpu entry.
+         */
+        sigaddset(&set, SIGIO);
+        sigaddset(&set, SIGALRM);
+    }
+#endif
+
+    sigfd = qemu_signalfd(&set);
     if (sigfd == -1) {
         fprintf(stderr, "failed to create signalfd\n");
         return -errno;
@@ -438,6 +464,12 @@ static void qemu_event_increment(void)
 static void qemu_kvm_eat_signals(CPUState *env)
 {
 }
+
+static int qemu_signalfd_init(void)
+{
+    return 0;
+}
+
 #endif /* _WIN32 */
 
 #ifndef CONFIG_IOTHREAD
@@ -471,39 +503,14 @@ static void qemu_kvm_init_cpu_signals(CPUState *env)
 #endif
 }
 
-#ifndef _WIN32
-static sigset_t block_synchronous_signals(void)
-{
-    sigset_t set;
-
-    sigemptyset(&set);
-    sigaddset(&set, SIGBUS);
-    if (kvm_enabled()) {
-        /*
-         * We need to process timer signals synchronously to avoid a race
-         * between exit_request check and KVM vcpu entry.
-         */
-        sigaddset(&set, SIGIO);
-        sigaddset(&set, SIGALRM);
-    }
-
-    return set;
-}
-#endif
-
 int qemu_init_main_loop(void)
 {
-#ifndef _WIN32
-    sigset_t blocked_signals;
     int ret;
 
-    blocked_signals = block_synchronous_signals();
-
-    ret = qemu_signalfd_init(blocked_signals);
+    ret = qemu_signalfd_init();
     if (ret) {
         return ret;
     }
-#endif
 
     qemu_init_sigbus();
 
@@ -651,35 +658,13 @@ static void qemu_tcg_init_cpu_signals(void)
     pthread_sigmask(SIG_UNBLOCK, &set, NULL);
 }
 
-static sigset_t block_io_signals(void)
-{
-    sigset_t set;
-
-    /* SIGUSR2 used by posix-aio-compat.c */
-    sigemptyset(&set);
-    sigaddset(&set, SIGUSR2);
-    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
-
-    sigemptyset(&set);
-    sigaddset(&set, SIGIO);
-    sigaddset(&set, SIGALRM);
-    sigaddset(&set, SIG_IPI);
-    sigaddset(&set, SIGBUS);
-    pthread_sigmask(SIG_BLOCK, &set, NULL);
-
-    return set;
-}
-
 int qemu_init_main_loop(void)
 {
     int ret;
-    sigset_t blocked_signals;

cpus.c static void qemu_tcg_init_cpu_signals(void) · line 670

Quoted 10 lines
cpus.c +1 -3
 
     qemu_init_sigbus();
 
-    blocked_signals = block_io_signals();
-
-    ret = qemu_signalfd_init(blocked_signals);
+    ret = qemu_signalfd_init();
     if (ret) {
         return ret;
     }

Beside the minor nits, this looks good.

Jan

#25 lore raw

[Qemu-devel] Re: [PATCH v2 upstream 19/22] move blocking of signals to qemu_signalfd_init

Quoted 8 lines · Jan Kiszka
Quoted 5 lines
Quoted 5 lines

+#ifdef CONFIG_IOTHREAD

 +    /* SIGUSR2 used by posix-aio-compat.c */
 +    sigemptyset(&set);
 +    sigaddset(&set, SIGUSR2);
 +    pthread_sigmask(SIG_UNBLOCK,&set, NULL);

Didn't you want to rename the function for the sake of non-signalfd blocks like above?

Right.

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

+

 +    sigemptyset(&set);
 +    sigaddset(&set, SIGIO);
 +    sigaddset(&set, SIGALRM);
 +    sigaddset(&set, SIG_IPI);
 +    sigaddset(&set, SIGBUS);
 +    pthread_sigmask(SIG_BLOCK,&set, NULL);

+#else
+ sigemptyset(&set);

This line is shared and can be moved out of the #ifdef.

It's shared but for different purposes (UNBLOCK for iothread, BLOCK/signalfd for !iothread), so I decided not to hoist it out.

Paolo

#26 lore raw

PATCH v2 20/22 provide dummy signal init functions for win32

In reply to Paolo Bonzini
---
 cpus.c |  143 ++++++++++++++++++++++++++++++++-------------------------------
 1 files changed, 73 insertions(+), 70 deletions(-)
cpus.c +73 -70
index 8c440f1..3951111 100644
@@ -196,6 +196,16 @@ static void cpu_handle_debug_exception(CPUState *env)
 #endif
 }
 
+#ifdef CONFIG_IOTHREAD
+static void cpu_signal(int sig)
+{
+    if (cpu_single_env) {
+        cpu_exit(cpu_single_env);
+    }
+    exit_request = 1;
+}
+#endif
+
 #ifdef CONFIG_LINUX
 static void sigbus_reraise(void)
 {
@@ -390,6 +400,61 @@ static int qemu_signalfd_init(void)
     return 0;
 }
 
+static void qemu_kvm_init_cpu_signals(CPUState *env)
+{
+    int r;
+    sigset_t set;
+    struct sigaction sigact;
+
+    memset(&sigact, 0, sizeof(sigact));
+    sigact.sa_handler = dummy_signal;
+    sigaction(SIG_IPI, &sigact, NULL);
+
+#ifdef CONFIG_IOTHREAD
+    pthread_sigmask(SIG_BLOCK, NULL, &set);
+    sigdelset(&set, SIG_IPI);
+    sigdelset(&set, SIGBUS);
+    r = kvm_set_signal_mask(env, &set);
+    if (r) {
+        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
+        exit(1);
+    }
+#else
+    sigemptyset(&set);
+    sigaddset(&set, SIG_IPI);
+    sigaddset(&set, SIGIO);
+    sigaddset(&set, SIGALRM);
+    pthread_sigmask(SIG_BLOCK, &set, NULL);
+
+    pthread_sigmask(SIG_BLOCK, NULL, &set);
+    sigdelset(&set, SIGIO);
+    sigdelset(&set, SIGALRM);
+#endif
+    sigdelset(&set, SIG_IPI);
+    sigdelset(&set, SIGBUS);
+    r = kvm_set_signal_mask(env, &set);
+    if (r) {
+        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
+        exit(1);
+    }
+}
+
+static void qemu_tcg_init_cpu_signals(void)
+{
+#ifdef CONFIG_IOTHREAD
+    sigset_t set;
+    struct sigaction sigact;
+
+    memset(&sigact, 0, sizeof(sigact));
+    sigact.sa_handler = cpu_signal;
+    sigaction(SIG_IPI, &sigact, NULL);
+
+    sigemptyset(&set);
+    sigaddset(&set, SIG_IPI);
+    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
+#endif
+}
+
 static void qemu_kvm_eat_signals(CPUState *env)
 {
     struct timespec ts = { 0, 0 };
@@ -470,39 +535,17 @@ static int qemu_signalfd_init(void)
     return 0;
 }
 
-#endif /* _WIN32 */
-
-#ifndef CONFIG_IOTHREAD
 static void qemu_kvm_init_cpu_signals(CPUState *env)
 {
-#ifndef _WIN32
-    int r;
-    sigset_t set;
-    struct sigaction sigact;
-
-    memset(&sigact, 0, sizeof(sigact));
-    sigact.sa_handler = dummy_signal;
-    sigaction(SIG_IPI, &sigact, NULL);
-
-    sigemptyset(&set);
-    sigaddset(&set, SIG_IPI);
-    sigaddset(&set, SIGIO);
-    sigaddset(&set, SIGALRM);
-    pthread_sigmask(SIG_BLOCK, &set, NULL);
+    abort();
+}
 
-    pthread_sigmask(SIG_BLOCK, NULL, &set);
-    sigdelset(&set, SIG_IPI);
-    sigdelset(&set, SIGBUS);
-    sigdelset(&set, SIGIO);
-    sigdelset(&set, SIGALRM);
-    r = kvm_set_signal_mask(env, &set);
-    if (r) {
-        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
-        exit(1);
-    }
-#endif
+static void qemu_tcg_init_cpu_signals(void)
+{
 }
+#endif /* _WIN32 */
 
+#ifndef CONFIG_IOTHREAD
 int qemu_init_main_loop(void)
 {
     int ret;
@@ -536,6 +579,8 @@ void qemu_init_vcpu(void *_env)
             exit(1);
         }
         qemu_kvm_init_cpu_signals(env);
+    } else {
+        qemu_tcg_init_cpu_signals();
     }
 }
 
@@ -616,48 +661,6 @@ static QemuCond qemu_system_cond;
 static QemuCond qemu_pause_cond;
 static QemuCond qemu_work_cond;
 
-static void cpu_signal(int sig)
-{
-    if (cpu_single_env) {
-        cpu_exit(cpu_single_env);
-    }
-    exit_request = 1;
-}
-
-static void qemu_kvm_init_cpu_signals(CPUState *env)
-{
-    int r;
-    sigset_t set;
-    struct sigaction sigact;
-
-    memset(&sigact, 0, sizeof(sigact));
-    sigact.sa_handler = dummy_signal;
-    sigaction(SIG_IPI, &sigact, NULL);
-
-    pthread_sigmask(SIG_BLOCK, NULL, &set);
-    sigdelset(&set, SIG_IPI);
-    sigdelset(&set, SIGBUS);
-    r = kvm_set_signal_mask(env, &set);
-    if (r) {
-        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
-        exit(1);
-    }
-}
-
-static void qemu_tcg_init_cpu_signals(void)
-{
-    sigset_t set;
-    struct sigaction sigact;
-
-    memset(&sigact, 0, sizeof(sigact));
-    sigact.sa_handler = cpu_signal;
-    sigaction(SIG_IPI, &sigact, NULL);
-
-    sigemptyset(&set);
-    sigaddset(&set, SIG_IPI);
-    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
-}
-
 int qemu_init_main_loop(void)
 {
     int ret;
Signature · 1 line
1.7.4
#27 lore raw

PATCH v2 21/22 protect qemu_cpu_kick_self for Win32

In reply to Paolo Bonzini
---
 cpus.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
cpus.c +4 -0
index 3951111..34ef862 100644
@@ -867,12 +867,16 @@ void qemu_cpu_kick(void *_env)
 
 void qemu_cpu_kick_self(void)
 {
+#ifndef _WIN32
     assert(cpu_single_env);
 
     if (!cpu_single_env->thread_kicked) {
         qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
         cpu_single_env->thread_kicked = true;
     }
+#else
+    abort();
+#endif
 }
 
 int qemu_cpu_is_self(void *_env)
Signature · 1 line
1.7.4
#28 lore raw

PATCH v2 22/22 add Win32 IPI service

In reply to Paolo Bonzini
---
 cpus.c              |   25 ++++++++++++++++++++++---
 qemu-thread-posix.c |    9 ---------
 qemu-thread-posix.h |    1 -
 3 files changed, 22 insertions(+), 13 deletions(-)
cpus.c +22 -3
index 34ef862..a2de3c2 100644
@@ -854,13 +854,32 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
     return NULL;
 }
 
+static void qemu_cpu_kick_thread(CPUState *env)
+{
+#ifndef _WIN32
+    int err;
+
+    err = pthread_kill(env->thread->thread, SIG_IPI);
+    if (err) {
+        fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
+        exit(1);
+    }
+#else /* _WIN32 */
+    if (!qemu_cpu_is_self(env)) {
+        SuspendThread(env->thread->thread);
+        cpu_signal(0);
+        ResumeThread(env->thread->thread);
+    }
+#endif
+}
+
 void qemu_cpu_kick(void *_env)
 {
     CPUState *env = _env;
 
     qemu_cond_broadcast(env->halt_cond);
     if (!env->thread_kicked) {
-        qemu_thread_signal(env->thread, SIG_IPI);
+        qemu_cpu_kick_thread(env);
         env->thread_kicked = true;
     }
 }
@@ -871,7 +890,7 @@ void qemu_cpu_kick_self(void)
     assert(cpu_single_env);
 
     if (!cpu_single_env->thread_kicked) {
-        qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
+        qemu_cpu_kick_thread(cpu_single_env);
         cpu_single_env->thread_kicked = true;
     }
 #else
@@ -893,7 +912,7 @@ void qemu_mutex_lock_iothread(void)
     } else {
         qemu_mutex_lock(&qemu_fair_mutex);
         if (qemu_mutex_trylock(&qemu_global_mutex)) {
-            qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
+            qemu_cpu_kick_thread(first_cpu);
             qemu_mutex_lock(&qemu_global_mutex);
         }
         qemu_mutex_unlock(&qemu_fair_mutex);
qemu-thread-posix.c +0 -9
index a4c6e25..9cceda7 100644
@@ -186,15 +186,6 @@ void qemu_thread_create(QemuThread *thread,
     pthread_sigmask(SIG_SETMASK, &oldset, NULL);
 }
 
-void qemu_thread_signal(QemuThread *thread, int sig)
-{
-    int err;
-
-    err = pthread_kill(thread->thread, sig);
-    if (err)
-        error_exit(err, __func__);
-}
-
 void qemu_thread_get_self(QemuThread *thread)
 {
     thread->thread = pthread_self();
qemu-thread-posix.h +0 -1
index 11978db..35e0a8b 100644
@@ -15,5 +15,4 @@ struct QemuThread {
     pthread_t thread;
 };
 
-void qemu_thread_signal(QemuThread *thread, int sig);
 #endif
Signature · 1 line
1.7.4
#29 lore raw
In reply to Paolo Bonzini
Quoted 20 lines · Paolo Bonzini

After gathering the comments about the two series I sent separately, here is the full series for Win32 iothread support, ready to be applied to uq/master.

Patches 1 to 5 are generic Win32 improvements, including the qemu-thread implementation.  Because of complex dependencies, I think it's better if this part is also routed through uq/master.

Patches 6 to 8 are generic threading improvements, including using PTHREAD_MUTEX_ERRORCHECK as suggested by Jan.

Patches 9 to 17 eliminate polling, replacing condition variable timedwait with wait.

Patch 18 removes a redundant condition from the TCG cpu_exec_all function.

Patches 19 to 21 add all necessary stubs to make iothread compile with Win32, except the IPI calls.  These are provided by patch 22.

Quoted 6 lines

Tested on Wine and Linux, not on "real" Windows.  The series introduces a dependency on Windows 2K or newer.  I don't think either 95/98/ME or Windows NT 3.x are reasonable host systems for QEMU, anyway.

I incorporated all suggestions from Jan, including his renaming patch for qemu_*_is_self, and included Aurelien's sh4 tweak to cpu_halted.

All look OK except for a minor structure naming nitpick.

#30 lore raw
In reply to Paolo Bonzini
Quoted 7 lines · Paolo Bonzini

After gathering the comments about the two series I sent separately, here is the full series for Win32 iothread support, ready to be applied to uq/master.

Patches 1 to 5 are generic Win32 improvements, including the qemu-thread implementation. Because of complex dependencies, I think it's better if this part is also routed through uq/master.

Agreed. This also ensures that the non-Win32 parts receive autotest blessing before being applied.

Quoted 13 lines

Patches 6 to 8 are generic threading improvements, including using PTHREAD_MUTEX_ERRORCHECK as suggested by Jan.

Patches 9 to 17 eliminate polling, replacing condition variable timedwait with wait.

Patch 18 removes a redundant condition from the TCG cpu_exec_all function.

Patches 19 to 21 add all necessary stubs to make iothread compile with Win32, except the IPI calls. These are provided by patch 22.

Quoted 7 lines

Tested on Wine and Linux, not on "real" Windows. The series introduces a dependency on Windows 2K or newer. I don't think either 95/98/ME or Windows NT 3.x are reasonable host systems for QEMU, anyway.

I incorporated all suggestions from Jan, including his renaming patch for qemu_*_is_self, and included Aurelien's sh4 tweak to cpu_halted.

#ifndef _WIN32
Except for the minor remarks, looks very good to me.
#else
Can't asses.
#endif

Jan