Skip to contentSkip to conversation

iothread v2

PATCH In review Marcelo Tosatti 3 participants 16 messages Apr 2, 2009 – Apr 6, 2009 Kbuild

lore
Share & tools
lore
#1 lore raw

PATCH 1/11 qemu: create helper for event notification

vl.c +21 -34
Index: trunk/vl.c
===================================================================
@@ -1183,9 +1183,8 @@ void qemu_mod_timer(QEMUTimer *ts, int64
             qemu_rearm_alarm_timer(alarm_timer);
         }
         /* Interrupt execution to force deadline recalculation.  */
-        if (use_icount && cpu_single_env) {
-            cpu_exit(cpu_single_env);
-        }
+        if (use_icount)
+            qemu_notify_event();
     }
 }
 
@@ -1338,8 +1337,6 @@ static void host_alarm_handler(int host_
                                qemu_get_clock(vm_clock))) ||
         qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
                            qemu_get_clock(rt_clock))) {
-        CPUState *env = next_cpu;
-
 #ifdef _WIN32
         struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
         SetEvent(data->host_alarm);
@@ -1349,16 +1346,7 @@ static void host_alarm_handler(int host_
 #endif
         alarm_timer->flags |= ALARM_FLAG_EXPIRED;
 
-        if (env) {
-            /* stop the currently executing cpu because a timer occured */
-            cpu_exit(env);
-#ifdef USE_KQEMU
-            if (env->kqemu_enabled) {
-                kqemu_cpu_interrupt(env);
-            }
-#endif
-        }
-        event_pending = 1;
+        qemu_notify_event();
     }
 }
 
@@ -3334,15 +3322,7 @@ static int ram_load(QEMUFile *f, void *o
 
 void qemu_service_io(void)
 {
-    CPUState *env = cpu_single_env;
-    if (env) {
-        cpu_exit(env);
-#ifdef USE_KQEMU
-        if (env->kqemu_enabled) {
-            kqemu_cpu_interrupt(env);
-        }
-#endif
-    }
+    qemu_notify_event();
 }
 
 /***********************************************************/
@@ -3410,15 +3390,12 @@ void qemu_bh_schedule_idle(QEMUBH *bh)
 
 void qemu_bh_schedule(QEMUBH *bh)
 {
-    CPUState *env = cpu_single_env;
     if (bh->scheduled)
         return;
     bh->scheduled = 1;
     bh->idle = 0;
     /* stop the currently executing CPU to execute the BH ASAP */
-    if (env) {
-        cpu_exit(env);
-    }
+    qemu_notify_event();
 }
 
 void qemu_bh_cancel(QEMUBH *bh)
@@ -3627,22 +3604,32 @@ void qemu_system_reset_request(void)
     } else {
         reset_requested = 1;
     }
-    if (cpu_single_env)
-        cpu_exit(cpu_single_env);
+    qemu_notify_event();
 }
 
 void qemu_system_shutdown_request(void)
 {
     shutdown_requested = 1;
-    if (cpu_single_env)
-        cpu_exit(cpu_single_env);
+    qemu_notify_event();
 }
 
 void qemu_system_powerdown_request(void)
 {
     powerdown_requested = 1;
-    if (cpu_single_env)
-        cpu_exit(cpu_single_env);
+    qemu_notify_event();
+}
+
+void qemu_notify_event(void)
+{
+    CPUState *env = cpu_single_env;
+
+    if (env) {
+        cpu_exit(env);
+#ifdef USE_KQEMU
+        if (env->kqemu_enabled)
+            kqemu_cpu_interrupt(env);
+#endif
+     }
 }
 
 #ifdef _WIN32
qemu-common.h +3 -0
Index: trunk/qemu-common.h
===================================================================
@@ -186,6 +186,9 @@ int cpu_load(QEMUFile *f, void *opaque, 
 /* Force QEMU to stop what it's doing and service IO */
 void qemu_service_io(void);
 
+/* Force QEMU to process pending events */
+void qemu_notify_event(void);
+
 typedef struct QEMUIOVector {
     struct iovec *iov;
     int niov;
hw/mac_dbdma.c +1 -3
Index: trunk/hw/mac_dbdma.c
===================================================================
@@ -651,9 +651,7 @@ void DBDMA_register_channel(void *dbdma,
 
 void DBDMA_schedule(void)
 {
-    CPUState *env = cpu_single_env;
-    if (env)
-        cpu_exit(env);
+    qemu_notify_event();
 }
 
 static void
#2 lore raw

PATCH 2/11 qemu: mutex/thread/cond wrappers

In reply to Marcelo Tosatti
qemu-thread.c new file +151 -0
Index: trunk/qemu-thread.c
===================================================================
@@ -0,0 +1,151 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include "qemu-thread.h"
+
+static void error_exit(int err, const char *msg)
+{
+    fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
+    exit(1);
+}
+
+void qemu_mutex_init(QemuMutex *mutex)
+{
+    int err;
+
+    err = pthread_mutex_init(&mutex->lock, NULL);
+    if (err)
+        error_exit(err, __func__);
+}
+
+void qemu_mutex_lock(QemuMutex *mutex)
+{
+    int err;
+
+    err = pthread_mutex_lock(&mutex->lock);
+    if (err)
+        error_exit(err, __func__);
+}
+
+int qemu_mutex_trylock(QemuMutex *mutex)
+{
+    return pthread_mutex_trylock(&mutex->lock);
+}
+
+static void timespec_add_ms(struct timespec *ts, uint64_t msecs)
+{
+    ts->tv_sec = ts->tv_sec + (long)(msecs / 1000);
+    ts->tv_nsec = (ts->tv_nsec + ((long)msecs % 1000) * 1000000);
+    if (ts->tv_nsec >= 1000000000) {
+        ts->tv_nsec -= 1000000000;
+        ts->tv_sec++;
+    }
+}
+
+int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
+{
+    int err;
+    struct timespec ts;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    err = pthread_mutex_timedlock(&mutex->lock, &ts);
+    if (err && err != ETIMEDOUT)
+        error_exit(err, __func__);
+    return err;
+}
+
+void qemu_mutex_unlock(QemuMutex *mutex)
+{
+    int err;
+
+    err = pthread_mutex_unlock(&mutex->lock);
+    if (err)
+        error_exit(err, __func__);
+}
+
+void qemu_cond_init(QemuCond *cond)
+{
+    int err;
+
+    err = pthread_cond_init(&cond->cond, NULL);
+    if (err)
+        error_exit(err, __func__);
+}
+
+void qemu_cond_signal(QemuCond *cond)
+{
+    int err;
+
+    err = pthread_cond_signal(&cond->cond);
+    if (err)
+        error_exit(err, __func__);
+}
+
+void qemu_cond_broadcast(QemuCond *cond)
+{
+    int err;
+
+    err = pthread_cond_broadcast(&cond->cond);
+    if (err)
+        error_exit(err, __func__);
+}
+
+void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
+{
+    int err;
+
+    err = pthread_cond_wait(&cond->cond, &mutex->lock);
+    if (err)
+        error_exit(err, __func__);
+}
+
+int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
+{
+    struct timespec ts;
+    int err;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
+    if (err && err != ETIMEDOUT)
+        error_exit(err, __func__);
+    return err;
+}
+
+void qemu_thread_create(QemuThread *thread,
+                       void *(*start_routine)(void*),
+                       void *arg)
+{
+    int err;
+
+    err = pthread_create(&thread->thread, NULL, start_routine, arg);
+    if (err)
+        error_exit(err, __func__);
+}
+
+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_self(QemuThread *thread)
+{
+    thread->thread = pthread_self();
+}
+
+int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2)
+{
+   return (thread1->thread == thread2->thread);
+}
+
qemu-thread.h new file +40 -0
Index: trunk/qemu-thread.h
===================================================================
@@ -0,0 +1,40 @@
+#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;
+
+void qemu_mutex_init(QemuMutex *mutex);
+void qemu_mutex_lock(QemuMutex *mutex);
+int qemu_mutex_trylock(QemuMutex *mutex);
+int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs);
+void qemu_mutex_unlock(QemuMutex *mutex);
+
+void qemu_cond_init(QemuCond *cond);
+void qemu_cond_signal(QemuCond *cond);
+void qemu_cond_broadcast(QemuCond *cond);
+void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex);
+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_self(QemuThread *thread);
+int qemu_thread_equal(QemuThread *thread1, QemuThread *thread2);
+#endif
Makefile.target +1 -0
Index: trunk/Makefile.target
===================================================================
@@ -501,6 +501,7 @@ endif #CONFIG_BSD_USER
 ifndef CONFIG_USER_ONLY
 
 OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o dma-helpers.o
+OBJS+=qemu-thread.o
 # virtio has to be here due to weird dependency between PCI and virtio-net.
 # need to fix this properly
 OBJS+=virtio.o virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o
#3 lore raw

Re: [Qemu-devel] [patch 02/11] qemu: mutex/thread/cond wrappers

This patch breaks the build on win32. Even with all of the patches applied, the win32 build is broken.

qemu-thread.c

Quoted 7 lines · Marcelo Tosatti

Index: trunk/qemu-thread.c
===================================================================

qemu-thread.c new file +0 -0

Please make sure to include Copyright/License information in all new files.

Signature · 3 lines
Regards,

Anthony Liguori
#4 lore raw

Re: [Qemu-devel] [patch 02/11] qemu: mutex/thread/cond wrappers

Quoted 2 lines · Anthony Liguori

This patch breaks the build on win32. Even with all of the patches applied, the win32 build is broken.

qemu-thread.c compilation fails? Can you show the logs please?

Need to skip qemu-thread.c in the Makefile for WIN32.

Quoted 10 lines

qemu-thread.c

Quoted 7 lines · Marcelo Tosatti

Index: trunk/qemu-thread.c
===================================================================

qemu-thread.c new file +0 -0

Please make sure to include Copyright/License information in all new files.

Will do.

#5 lore raw

Re: [Qemu-devel] [patch 02/11] qemu: mutex/thread/cond wrappers

Quoted 9 lines · Marcelo Tosatti
Quoted 2 lines · Anthony Liguori

This patch breaks the build on win32. Even with all of the patches applied, the win32 build is broken.

qemu-thread.c compilation fails? Can you show the logs please?

Need to skip qemu-thread.c in the Makefile for WIN32.

It's trying to build qemu-thread.c on WIN32 which uses pthreads which isn't present on WIN32 so that's the source of the error.

Regards,

Anthony Liguori

#6 lore raw

PATCH 3/11 qemu: per-arch cpu_has_work

In reply to Marcelo Tosatti

Blue Swirl: fix Sparc32 breakage

target-alpha/exec.h +6 -1
Index: trunk/target-alpha/exec.h
===================================================================
@@ -48,10 +48,15 @@ static always_inline void regs_to_env(vo
 {
 }
 
+static always_inline int cpu_has_work(CPUState *env)
+{
+    return (env->interrupt_request & CPU_INTERRUPT_HARD);
+}
+
 static always_inline int cpu_halted(CPUState *env) {
     if (!env->halted)
         return 0;
-    if (env->interrupt_request & CPU_INTERRUPT_HARD) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
target-i386/exec.h +12 -3
Index: trunk/target-i386/exec.h
===================================================================
@@ -338,14 +338,23 @@ static inline void regs_to_env(void)
 #endif
 }
 
+static inline int cpu_has_work(CPUState *env)
+{
+    int work;
+
+    work = (env->interrupt_request & CPU_INTERRUPT_HARD) &&
+           (env->eflags & IF_MASK);
+    work |= env->interrupt_request & CPU_INTERRUPT_NMI;
+
+    return work;
+}
+
 static inline int cpu_halted(CPUState *env) {
     /* handle exit of HALTED state */
     if (!env->halted)
         return 0;
     /* disable halt condition */
-    if (((env->interrupt_request & CPU_INTERRUPT_HARD) &&
-         (env->eflags & IF_MASK)) ||
-        (env->interrupt_request & CPU_INTERRUPT_NMI)) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
cpu-all.h +2 -0
Index: trunk/cpu-all.h
===================================================================
@@ -775,6 +775,8 @@ void cpu_reset_interrupt(CPUState *env, 
 
 void cpu_exit(CPUState *s);
 
+int qemu_cpu_has_work(CPUState *env);
+
 /* Breakpoint/watchpoint flags */
 #define BP_MEM_READ           0x01
 #define BP_MEM_WRITE          0x02
cpu-exec.c +5 -0
Index: trunk/cpu-exec.c
===================================================================
@@ -50,6 +50,11 @@ int tb_invalidated_flag;
 //#define DEBUG_EXEC
 //#define DEBUG_SIGNAL
 
+int qemu_cpu_has_work(CPUState *env)
+{
+    return cpu_has_work(env);
+}
+
 void cpu_loop_exit(void)
 {
     /* NOTE: the register at this point must be saved by hand because
target-arm/exec.h +7 -2
Index: trunk/target-arm/exec.h
===================================================================
@@ -37,14 +37,19 @@ static inline void regs_to_env(void)
 {
 }
 
+static inline int cpu_has_work(CPUState *env)
+{
+    return (env->interrupt_request &
+            (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 (env->interrupt_request &
-        (CPU_INTERRUPT_FIQ | CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB)) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
target-cris/exec.h +5 -0
Index: trunk/target-cris/exec.h
===================================================================
@@ -40,6 +40,11 @@ static inline void regs_to_env(void)
 void cpu_cris_flush_flags(CPUCRISState *env, int cc_op);
 void helper_movec(CPUCRISState *env, int reg, uint32_t val);
 
+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;
target-m68k/exec.h +6 -1
Index: trunk/target-m68k/exec.h
===================================================================
@@ -41,10 +41,15 @@ static inline void regs_to_env(void)
 #include "softmmu_exec.h"
 #endif
 
+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 (env->interrupt_request & CPU_INTERRUPT_HARD) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
target-mips/exec.h +8 -2
Index: trunk/target-mips/exec.h
===================================================================
@@ -33,12 +33,18 @@ static inline void regs_to_env(void)
 {
 }
 
+static inline int cpu_has_work(CPUState *env)
+{
+    return (env->interrupt_request &
+            (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER));
+}
+
+
 static inline int cpu_halted(CPUState *env)
 {
     if (!env->halted)
         return 0;
-    if (env->interrupt_request &
-        (CPU_INTERRUPT_HARD | CPU_INTERRUPT_TIMER)) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
target-ppc/exec.h +7 -1
Index: trunk/target-ppc/exec.h
===================================================================
@@ -44,11 +44,17 @@ static always_inline void regs_to_env (v
 {
 }
 
+static always_inline int cpu_has_work(CPUState *env)
+{
+    return (msr_ee && (env->interrupt_request & CPU_INTERRUPT_HARD));
+}
+
+
 static always_inline int cpu_halted (CPUState *env)
 {
     if (!env->halted)
         return 0;
-    if (msr_ee && (env->interrupt_request & CPU_INTERRUPT_HARD)) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         return 0;
     }
target-sh4/exec.h +6 -1
Index: trunk/target-sh4/exec.h
===================================================================
@@ -28,10 +28,15 @@ register struct CPUSH4State *env asm(ARE
 #include "cpu.h"
 #include "exec-all.h"
 
+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 (env->interrupt_request & CPU_INTERRUPT_HARD) {
+    if (cpu_has_work(env)) {
         env->halted = 0;
         env->intr_at_halt = 1;
         return 0;
target-sparc/exec.h +8 -1
Index: trunk/target-sparc/exec.h
===================================================================
@@ -24,10 +24,17 @@ static inline void regs_to_env(void)
 /* op_helper.c */
 void do_interrupt(CPUState *env);
 
+static inline int cpu_has_work(CPUState *env1)
+{
+    return (env1->interrupt_request & CPU_INTERRUPT_HARD) &&
+           (env1->psret != 0);
+}
+
+
 static inline int cpu_halted(CPUState *env1) {
     if (!env1->halted)
         return 0;
-    if ((env1->interrupt_request & CPU_INTERRUPT_HARD) && (env1->psret != 0)) {
+    if (cpu_has_work(env1)) {
         env1->halted = 0;
         return 0;
     }
#7 lore raw

PATCH 4/11 qemu: introduce main_loop_break

In reply to Marcelo Tosatti

Use a pipe to signal pending work for the iothread.

qemu-common.h +2 -0
Index: trunk/qemu-common.h
===================================================================
@@ -186,6 +186,8 @@ int cpu_load(QEMUFile *f, void *opaque, 
 /* Force QEMU to stop what it's doing and service IO */
 void qemu_service_io(void);
 
+void main_loop_break(void);
+
 /* Force QEMU to process pending events */
 void qemu_notify_event(void);
 
vl.c +65 -0

83 lines not on this page · raw message

#8 lore raw

PATCH 5/11 qemu: separate thread for io

In reply to Marcelo Tosatti

Introduce a thread to handle host IO events.

qemu-common.h +4 -0
Index: trunk/qemu-common.h
===================================================================
@@ -191,6 +191,10 @@ void main_loop_break(void);
 /* Force QEMU to process pending events */
 void qemu_notify_event(void);
 
+/* Unblock cpu */
+void qemu_cpu_kick(void *env);
+int qemu_cpu_self(void *env);
+
 typedef struct QEMUIOVector {
     struct iovec *iov;
     int niov;
vl.c +154 -23

283 lines not on this page · raw message

exec.c +7 -0
Index: trunk/exec.c
===================================================================
@@ -1532,6 +1532,13 @@ void cpu_interrupt(CPUState *env, int ma
     old_mask = env->interrupt_request;
     env->interrupt_request |= mask;
 
+#ifndef CONFIG_USER_ONLY
+    if (!qemu_cpu_self(env)) {
+        qemu_cpu_kick(env);
+        return;
+    }
+#endif
+
     if (use_icount) {
         env->icount_decr.u16.high = 0xffff;
 #ifndef CONFIG_USER_ONLY
#9 lore raw

PATCH 6/11 qemu: per-cpu thread information

In reply to Marcelo Tosatti

Move per-cpu thread information to CPUState. Initialize through per-arch cpu_init.

cpu-defs.h +5 -0
Index: trunk/cpu-defs.h
===================================================================
@@ -158,6 +158,8 @@ typedef struct CPUWatchpoint {
     TAILQ_ENTRY(CPUWatchpoint) entry;
 } CPUWatchpoint;
 
+#include "qemu-thread.h"
+
 #define CPU_TEMP_BUF_NLONGS 128
 #define CPU_COMMON                                                      \
     struct TranslationBlock *current_tb; /* currently executing TB  */  \
@@ -209,6 +211,9 @@ typedef struct CPUWatchpoint {
     /* user data */                                                     \
     void *opaque;                                                       \
                                                                         \
+    uint32_t created;                                                   \
+    struct QemuThread *thread;                                          \
+    struct QemuCond *halt_cond;                                         \
     const char *cpu_model_str;                                          \
     struct KVMState *kvm_state;                                         \
     struct kvm_run *kvm_run;                                            \
target-i386/helper.c +3 -0
Index: trunk/target-i386/helper.c
===================================================================
@@ -1670,5 +1670,8 @@ CPUX86State *cpu_x86_init(const char *cp
 #endif
     if (kvm_enabled())
         kvm_init_vcpu(env);
+
+    qemu_init_vcpu(env);
+
     return env;
 }
vl.c +49 -11

117 lines not on this page · raw message

qemu-common.h +6 -0
Index: trunk/qemu-common.h
===================================================================
@@ -195,6 +195,12 @@ void qemu_notify_event(void);
 void qemu_cpu_kick(void *env);
 int qemu_cpu_self(void *env);
 
+#ifdef CONFIG_USER_ONLY
+#define qemu_init_vcpu(env) do { } while (0)
+#else
+void qemu_init_vcpu(void *env);
+#endif
+
 typedef struct QEMUIOVector {
     struct iovec *iov;
     int niov;
target-arm/helper.c +1 -0
Index: trunk/target-arm/helper.c
===================================================================
@@ -267,6 +267,7 @@ CPUARMState *cpu_arm_init(const char *cp
         gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
                                  19, "arm-vfp.xml", 0);
     }
+    qemu_init_vcpu(env);
     return env;
 }
 
target-m68k/helper.c +1 -0
Index: trunk/target-m68k/helper.c
===================================================================
@@ -180,6 +180,7 @@ CPUM68KState *cpu_m68k_init(const char *
     }
 
     cpu_reset(env);
+    qemu_init_vcpu(env);
     return env;
 }
 
target-ppc/helper.c +1 -0
Index: trunk/target-ppc/helper.c
===================================================================
@@ -2833,6 +2833,7 @@ CPUPPCState *cpu_ppc_init (const char *c
 
     if (kvm_enabled())
         kvm_init_vcpu(env);
+    qemu_init_vcpu(env);
 
     return env;
 }
target-sparc/helper.c +1 -0
Index: trunk/target-sparc/helper.c
===================================================================
@@ -723,6 +723,7 @@ CPUSPARCState *cpu_sparc_init(const char
         return NULL;
     }
     cpu_reset(env);
+    qemu_init_vcpu(env);
 
     return env;
 }
target-alpha/translate.c +1 -0
Index: trunk/target-alpha/translate.c
===================================================================
@@ -2494,6 +2494,7 @@ CPUAlphaState * cpu_alpha_init (const ch
     env->ipr[IPR_SISR] = 0;
     env->ipr[IPR_VIRBND] = -1ULL;
 
+    qemu_init_vcpu(env);
     return env;
 }
 
target-cris/translate.c +1 -0
Index: trunk/target-cris/translate.c
===================================================================
@@ -3404,6 +3404,7 @@ CPUCRISState *cpu_cris_init (const char 
 
 	cpu_exec_init(env);
 	cpu_reset(env);
+	qemu_init_vcpu(env);
 
 	if (tcg_initialized)
 		return env;
target-mips/translate.c +1 -0
Index: trunk/target-mips/translate.c
===================================================================
@@ -8366,6 +8366,7 @@ CPUMIPSState *cpu_mips_init (const char 
     env->cpu_model_str = cpu_model;
     mips_tcg_init();
     cpu_reset(env);
+    qemu_init_vcpu(env);
     return env;
 }
 
target-sh4/translate.c +1 -0
Index: trunk/target-sh4/translate.c
===================================================================
@@ -290,6 +290,7 @@ CPUSH4State *cpu_sh4_init(const char *cp
     cpu_sh4_reset(env);
     cpu_sh4_register(env, def);
     tlb_flush(env, 1);
+    qemu_init_vcpu(env);
     return env;
 }
 
#10 lore raw

PATCH 7/11 qemu: handle reset/poweroff/shutdown in iothread

In reply to Marcelo Tosatti

Its simpler to handle these events from only one context.

vl.c +11 -24
Index: trunk/vl.c
===================================================================
@@ -4031,25 +4031,6 @@ static void *cpu_main_loop(void *arg)
             }
             cur_cpu = env;
 
-            if (shutdown_requested) {
-                ret = EXCP_INTERRUPT;
-                if (no_shutdown) {
-                    vm_stop(0);
-                    no_shutdown = 0;
-                }
-                else
-                    break;
-            }
-            if (reset_requested) {
-                reset_requested = 0;
-                qemu_system_reset();
-                ret = EXCP_INTERRUPT;
-            }
-            if (powerdown_requested) {
-                powerdown_requested = 0;
-		qemu_system_powerdown();
-                ret = EXCP_INTERRUPT;
-            }
             if (unlikely(ret == EXCP_DEBUG)) {
                 gdb_set_stop_cpu(cur_cpu);
                 vm_stop(EXCP_DEBUG);
@@ -4098,10 +4079,6 @@ static void *cpu_main_loop(void *arg)
             }
         } else {
             env = env->next_cpu ?: first_cpu;
-            if (shutdown_requested) {
-                ret = EXCP_INTERRUPT;
-                break;
-            }
             timeout = 5000;
         }
 #ifdef CONFIG_PROFILER
@@ -4152,8 +4129,18 @@ static void main_loop(void)
     qemu_system_ready = 1;
     qemu_cond_broadcast(&qemu_system_cond);
 
-    while (1)
+    while (1) {
         main_loop_wait(1000);
+        if (qemu_shutdown_requested()) {
+            if (no_shutdown)
+                no_shutdown = 0;
+            else
+                    break;
+        } else if (qemu_powerdown_requested())
+            qemu_system_powerdown();
+        else if (qemu_reset_requested())
+            qemu_system_reset();
+    }
 }
 
 static void help(int exitcode)
#11 lore raw

PATCH 8/11 qemu: pause and resume cpu threads

In reply to Marcelo Tosatti

Since cpu emulation happens on a separate thread, it is necessary to pause/resume it upon certain events such as reset, debug exception, live migration, etc.

cpu-defs.h +2 -0
Index: trunk/cpu-defs.h
===================================================================
@@ -172,6 +172,8 @@ 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 stop;   /* Stop request */                                 \
+    uint32_t stopped; /* Artificially stopped */                        \
     uint32_t interrupt_request;                                         \
     volatile sig_atomic_t exit_request;                                 \
     /* The meaning of the MMU modes is defined in the target code. */   \
vl.c +89 -2

154 lines not on this page · raw message

#12 lore raw

PATCH 9/11 qemu: handle vmstop from cpu context

In reply to Marcelo Tosatti

There are certain cases where cpu context requests a vm stop, such as -ENOSPC handling.

IMO its simpler to handle vmstop only through the iothread.

Note there is change in behaviour: now the cpu thread which requested vm_stop will actually only stop when it exits back to cpu_main_loop. It might cause further damage in between vmstop and cpu_main_loop.

vl.c +39 -3
Index: trunk/vl.c
===================================================================
@@ -3551,7 +3551,22 @@ void vm_start(void)
     }
 }
 
-void vm_stop(int reason)
+static int vmstop_requested;
+
+static int qemu_vmstop_requested(void)
+{
+    int r = vmstop_requested;
+    vmstop_requested = 0;
+    return r;
+}
+
+static void qemu_system_vmstop_request(int reason)
+{
+    vmstop_requested = reason;
+    main_loop_break();
+}
+
+static void __vm_stop(int reason)
 {
     if (vm_running) {
         cpu_disable_ticks();
@@ -3561,6 +3576,21 @@ void vm_stop(int reason)
     }
 }
 
+void vm_stop(int reason)
+{
+    QemuThread me;
+    qemu_thread_self(&me);
+
+    if (!qemu_thread_equal(&me, &io_thread)) {
+        qemu_system_vmstop_request(reason);
+        /* make sure we can't return to cpu_exec */
+        if (cpu_single_env)
+            cpu_single_env->stop = 1;
+        return;
+    }
+    __vm_stop(reason);
+}
+
 /* reset/shutdown handler */
 
 typedef struct QEMUResetEntry {
@@ -3758,6 +3788,8 @@ static int cpu_can_run(CPUState *env)
         return 0;
     if (reset_requested)
         return 0;
+    if (vmstop_requested)
+        return 0;
     return 1;
 }
 
@@ -4204,6 +4236,8 @@ static void qemu_init_state(void)
 
 static void main_loop(void)
 {
+    int r;
+
     qemu_thread_self(&io_thread);
     setup_iothread_fd();
 
@@ -4220,12 +4254,14 @@ static void main_loop(void)
                 no_shutdown = 0;
             else
                     break;
-        } else if (qemu_powerdown_requested())
+        } else if (qemu_powerdown_requested()) {
             qemu_system_powerdown();
-        else if (qemu_reset_requested()) {
+        } else if (qemu_reset_requested()) {
             pause_all_vcpus();
             qemu_system_reset();
             resume_all_vcpus();
+        } else if ((r = qemu_vmstop_requested())) {
+            vm_stop(r);
         }
     }
 }
#13 lore raw

Re: [Qemu-devel] [patch 09/11] qemu: handle vmstop from cpu context

Quoted 9 lines · Marcelo Tosatti

There are certain cases where cpu context requests a vm stop, such as -ENOSPC handling.

IMO its simpler to handle vmstop only through the iothread.

Note there is change in behaviour: now the cpu thread which requested vm_stop will actually only stop when it exits back to cpu_main_loop. It might cause further damage in between vmstop and cpu_main_loop.

Missing SoB line.

Regards,

Anthony Liguori

Quoted 87 lines

Index: trunk/vl.c
===================================================================

vl.c +39 -3
@@ -3551,7 +3551,22 @@ void vm_start(void)
     }
 }
 
-void vm_stop(int reason)
+static int vmstop_requested;
+
+static int qemu_vmstop_requested(void)
+{
+    int r = vmstop_requested;
+    vmstop_requested = 0;
+    return r;
+}
+
+static void qemu_system_vmstop_request(int reason)
+{
+    vmstop_requested = reason;
+    main_loop_break();
+}
+
+static void __vm_stop(int reason)
 {
     if (vm_running) {
         cpu_disable_ticks();
@@ -3561,6 +3576,21 @@ void vm_stop(int reason)
     }
 }
 
+void vm_stop(int reason)
+{
+    QemuThread me;
+    qemu_thread_self(&me);
+
+    if (!qemu_thread_equal(&me, &io_thread)) {
+        qemu_system_vmstop_request(reason);
+        /* make sure we can't return to cpu_exec */
+        if (cpu_single_env)
+            cpu_single_env->stop = 1;
+        return;
+    }
+    __vm_stop(reason);
+}
+
 /* reset/shutdown handler */
 
 typedef struct QEMUResetEntry {
@@ -3758,6 +3788,8 @@ static int cpu_can_run(CPUState *env)
         return 0;
     if (reset_requested)
         return 0;
+    if (vmstop_requested)
+        return 0;
     return 1;
 }
 
@@ -4204,6 +4236,8 @@ static void qemu_init_state(void)
 
 static void main_loop(void)
 {
+    int r;
+
     qemu_thread_self(&io_thread);
     setup_iothread_fd();
 
@@ -4220,12 +4254,14 @@ static void main_loop(void)
                 no_shutdown = 0;
             else
                     break;
-        } else if (qemu_powerdown_requested())
+        } else if (qemu_powerdown_requested()) {
             qemu_system_powerdown();
-        else if (qemu_reset_requested()) {
+        } else if (qemu_reset_requested()) {
             pause_all_vcpus();
             qemu_system_reset();
             resume_all_vcpus();
+        } else if ((r = qemu_vmstop_requested())) {
+            vm_stop(r);
         }
     }
 }
Signature · 3 lines
Regards,

Anthony Liguori
#14 lore raw

PATCH 10/11 qemu: make iothread selectable at compile time

In reply to Marcelo Tosatti

Turned off by default.

vl.c +317 -236
Index: trunk/vl.c
===================================================================
@@ -277,6 +277,7 @@ static QEMUTimer *nographic_timer;
 
 uint8_t qemu_uuid[16];
 
+#ifdef QEMU_THREADED
 static int io_thread_fd = -1;
 
 QemuMutex qemu_global_mutex;
@@ -294,6 +295,7 @@ QemuCond qemu_cpu_cond;
 QemuCond qemu_system_cond;
 
 QemuCond qemu_pause_cond;
+#endif
 
 static void pause_all_vcpus(void);
 static void resume_all_vcpus(void);
@@ -1367,6 +1369,14 @@ static void host_alarm_handler(int host_
         write(alarm_timer_wfd, &byte, sizeof(byte));
 #endif
         alarm_timer->flags |= ALARM_FLAG_EXPIRED;
+#ifndef QEMU_THREADED
+        {
+            CPUState *env = next_cpu;
+            if (env)
+                cpu_exit(env);
+            event_pending = 1;
+        }
+#endif
     }
 }
 
@@ -3343,6 +3353,7 @@ static int ram_load(QEMUFile *f, void *o
 
 void qemu_service_io(void)
 {
+    qemu_notify_event();
 }
 
 /***********************************************************/
@@ -3560,11 +3571,13 @@ static int qemu_vmstop_requested(void)
     return r;
 }
 
+#ifdef QEMU_THREADED
 static void qemu_system_vmstop_request(int reason)
 {
     vmstop_requested = reason;
     main_loop_break();
 }
+#endif
 
 static void __vm_stop(int reason)
 {
@@ -3578,6 +3591,7 @@ static void __vm_stop(int reason)
 
 void vm_stop(int reason)
 {
+#ifdef QEMU_THREADED
     QemuThread me;
     qemu_thread_self(&me);
 
@@ -3588,6 +3602,7 @@ void vm_stop(int reason)
             cpu_single_env->stop = 1;
         return;
     }
+#endif
     __vm_stop(reason);
 }
 
@@ -3671,6 +3686,7 @@ void qemu_system_powerdown_request(void)
     main_loop_break();
 }
 
+#ifdef QEMU_THREADED
 void qemu_notify_event(void)
 {
     main_loop_break();
@@ -3725,74 +3741,6 @@ static void io_thread_wakeup(void *opaqu
     }
 }
 
-#ifdef _WIN32
-static void host_main_loop_wait(int *timeout)
-{
-    int ret, ret2, i;
-    PollingEntry *pe;
-
-
-    /* XXX: need to suppress polling by better using win32 events */
-    ret = 0;
-    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
-        ret |= pe->func(pe->opaque);
-    }
-    if (ret == 0) {
-        int err;
-        WaitObjects *w = &wait_objects;
-
-        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
-        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]);
-
-            /* Check for additional signaled events */
-            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
-
-                /* Check if event is signaled */
-                ret2 = WaitForSingleObject(w->events[i], 0);
-                if(ret2 == WAIT_OBJECT_0) {
-                    if (w->func[i])
-                        w->func[i](w->opaque[i]);
-                } else if (ret2 == WAIT_TIMEOUT) {
-                } else {
-                    err = GetLastError();
-                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
-                }
-            }
-        } else if (ret == WAIT_TIMEOUT) {
-        } else {
-            err = GetLastError();
-            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
-        }
-    }
-
-    *timeout = 0;
-}
-#else
-static void host_main_loop_wait(int *timeout)
-{
-}
-#endif
-
-/* Q: is it allowed to enter cpu_exec */
-static int cpu_can_run(CPUState *env)
-{
-    if (env->stop)
-        return 0;
-    if (env->stopped)
-        return 0;
-    if (shutdown_requested)
-        return 0;
-    if (powerdown_requested)
-        return 0;
-    if (reset_requested)
-        return 0;
-    if (vmstop_requested)
-        return 0;
-    return 1;
-}
-
 /* Q: should break out of the wait loop */
 static int cpu_has_work(CPUState *env)
 {
@@ -3950,6 +3898,187 @@ static void resume_all_vcpus(void)
     }
 }
 
+static void *cpu_thread(void *arg);
+
+void qemu_init_vcpu(void *_env)
+{
+    CPUState *env = _env;
+    /* share a single thread for all cpus with TCG */
+    if (!tcg_cpu_thread) {
+        env->thread = qemu_mallocz(sizeof(QemuThread));
+        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
+        qemu_cond_init(env->halt_cond);
+        qemu_thread_create(env->thread, cpu_thread, env);
+        while (env->created == 0)
+            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
+        tcg_cpu_thread = env->thread;
+        tcg_halt_cond = env->halt_cond;
+    } else {
+        env->thread = tcg_cpu_thread;
+        env->halt_cond = tcg_halt_cond;
+    }
+}
+
+static void qemu_init_state(void)
+{
+    qemu_cond_init(&qemu_pause_cond);
+    qemu_mutex_init(&qemu_fair_mutex);
+    qemu_mutex_init(&qemu_global_mutex);
+    qemu_mutex_lock(&qemu_global_mutex);
+}
+
+static void setup_iothread_fd(void);
+static void pre_main_loop(void)
+{
+    qemu_thread_self(&io_thread);
+    setup_iothread_fd();
+
+    unblock_io_signals();
+
+    qemu_system_ready = 1;
+    qemu_cond_broadcast(&qemu_system_cond);
+}
+
+static int qemu_cpu_exec(CPUState *env);
+static void *cpu_thread(void *arg)
+{
+    int r;
+    CPUState *env = arg;
+
+    block_io_signals();
+    qemu_thread_self(env->thread);
+
+    /* signal CPU creation */
+    qemu_mutex_lock(&qemu_global_mutex);
+    env->created = 1;
+    qemu_cond_signal(&qemu_cpu_cond);
+
+    /* and wait for machine initialization */
+    while (!qemu_system_ready)
+        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
+
+    while (1) {
+        r = qemu_cpu_exec(env);
+        qemu_wait_io_event(env, r);
+    }
+
+    return NULL;
+}
+
+static void setup_iothread_fd(void)
+{
+    int fds[2];
+
+    if (pipe(fds) == -1) {
+        fprintf(stderr, "failed to create iothread pipe");
+        exit(0);
+    }
+
+    qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
+                         (void *)(unsigned long)fds[0]);
+    io_thread_fd = fds[1];
+    fcntl(io_thread_fd, F_SETFL, O_NONBLOCK);
+}
+
+#else /* QEMU_THREADED */
+#define qemu_signal_lock(a)         do { } while(0)
+#define qemu_mutex_unlock(a)         do { } while(0)
+#define pre_main_loop()             do { } while(0)
+#define qemu_init_state()           do { } while(0)
+
+void pause_all_vcpus(void)
+{
+    return;
+}
+
+void resume_all_vcpus(void)
+{
+    return;
+}
+
+void qemu_cpu_kick(void *env)
+{
+    return;
+}
+
+void qemu_init_vcpu(void *_env)
+{
+    CPUState *env = _env;
+
+    if (kvm_enabled())
+        kvm_init_vcpu(env);
+    return;
+}
+
+void qemu_notify_event(void)
+{
+    CPUState *env = cpu_single_env;
+
+    if (env)
+        cpu_exit(env);
+}
+
+void main_loop_break(void)
+{
+    qemu_notify_event();
+}
+
+int qemu_cpu_self(void *env)
+{
+    return 1;
+}
+#endif
+
+#ifdef _WIN32
+static void host_main_loop_wait(int *timeout)
+{
+    int ret, ret2, i;
+    PollingEntry *pe;
+
+
+    /* XXX: need to suppress polling by better using win32 events */
+    ret = 0;
+    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
+        ret |= pe->func(pe->opaque);
+    }
+    if (ret == 0) {
+        int err;
+        WaitObjects *w = &wait_objects;
+
+        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
+        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]);
+
+            /* Check for additional signaled events */
+            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
+
+                /* Check if event is signaled */
+                ret2 = WaitForSingleObject(w->events[i], 0);
+                if(ret2 == WAIT_OBJECT_0) {
+                    if (w->func[i])
+                        w->func[i](w->opaque[i]);
+                } else if (ret2 == WAIT_TIMEOUT) {
+                } else {
+                    err = GetLastError();
+                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
+                }
+            }
+        } else if (ret == WAIT_TIMEOUT) {
+        } else {
+            err = GetLastError();
+            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
+        }
+    }
+
+    *timeout = 0;
+}
+#else
+static void host_main_loop_wait(int *timeout)
+{
+}
+#endif
+
 void main_loop_wait(int timeout)
 {
     IOHandlerRecord *ioh;
@@ -4051,203 +4180,155 @@ void main_loop_wait(int timeout)
 
 }
 
-static void setup_iothread_fd(void)
+/* Q: is it allowed to enter cpu_exec */
+static int cpu_can_run(CPUState *env)
 {
-    int fds[2];
-
-    if (pipe(fds) == -1) {
-        fprintf(stderr, "failed to create iothread pipe");
-        exit(0);
-    }
-
-    qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
-                         (void *)(unsigned long)fds[0]);
-    io_thread_fd = fds[1];
-    fcntl(io_thread_fd, F_SETFL, O_NONBLOCK);
+    if (env->stop)
+        return 0;
+    if (env->stopped)
+        return 0;
+    if (shutdown_requested)
+        return 0;
+    if (powerdown_requested)
+        return 0;
+    if (reset_requested)
+        return 0;
+    if (vmstop_requested)
+        return 0;
+    return 1;
 }
 
-static void *cpu_main_loop(void *arg)
+static int qemu_cpu_exec(CPUState *env)
 {
     int ret, timeout;
 #ifdef CONFIG_PROFILER
     int64_t ti;
 #endif
-    CPUState *env = arg;
-
-    block_io_signals();
-    qemu_thread_self(env->thread);
-
-    /* signal CPU creation */
-    qemu_mutex_lock(&qemu_global_mutex);
-    env->created = 1;
-    qemu_cond_signal(&qemu_cpu_cond);
-
-    /* and wait for machine initialization */
-    while (!qemu_system_ready)
-        qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
-
-    cur_cpu = env = first_cpu;
-    next_cpu = cur_cpu->next_cpu ?: first_cpu;
     for(;;) {
-        if (vm_running) {
-
-            for(;;) {
-                /* get next cpu */
-                env = next_cpu;
+        /* get next cpu */
+        env = next_cpu;
 #ifdef CONFIG_PROFILER
-                ti = profile_getclock();
+        ti = profile_getclock();
 #endif
-                if (use_icount) {
-                    int64_t count;
-                    int decr;
-                    qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
-                    env->icount_decr.u16.low = 0;
-                    env->icount_extra = 0;
-                    count = qemu_next_deadline();
-                    count = (count + (1 << icount_time_shift) - 1)
-                            >> icount_time_shift;
-                    qemu_icount += count;
-                    decr = (count > 0xffff) ? 0xffff : count;
-                    count -= decr;
-                    env->icount_decr.u16.low = decr;
-                    env->icount_extra = count;
-                }
-                ret = EXCP_HALTED;
-                if (cpu_can_run(env))
-                    ret = cpu_exec(env);
+        if (use_icount) {
+            int64_t count;
+            int decr;
+            qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
+            env->icount_decr.u16.low = 0;
+            env->icount_extra = 0;
+            count = qemu_next_deadline();
+            count = (count + (1 << icount_time_shift) - 1)
+                    >> icount_time_shift;
+            qemu_icount += count;
+            decr = (count > 0xffff) ? 0xffff : count;
+            count -= decr;
+            env->icount_decr.u16.low = decr;
+            env->icount_extra = count;
+        }
+        ret = EXCP_HALTED;
+        if (cpu_can_run(env))
+            ret = cpu_exec(env);
 #ifdef CONFIG_PROFILER
-                qemu_time += profile_getclock() - ti;
+        qemu_time += profile_getclock() - ti;
 #endif
-                if (use_icount) {
-                    /* Fold pending instructions back into the
-                       instruction counter, and clear the interrupt flag.  */
-                    qemu_icount -= (env->icount_decr.u16.low
-                                    + env->icount_extra);
-                    env->icount_decr.u32 = 0;
-                    env->icount_extra = 0;
-                }
-                next_cpu = env->next_cpu ?: first_cpu;
-                if (event_pending && likely(ret != EXCP_DEBUG)) {
-                    ret = EXCP_INTERRUPT;
-                    event_pending = 0;
-                    break;
-                }
-                if (ret == EXCP_HLT) {
-                    /* Give the next CPU a chance to run.  */
-                    cur_cpu = env;
-                    continue;
-                }
-                if (ret != EXCP_HALTED)
-                    break;
-                /* all CPUs are halted ? */
-                if (env == cur_cpu)
-                    break;
-            }
+        if (use_icount) {
+            /* Fold pending instructions back into the
+            instruction counter, and clear the interrupt flag.  */
+            qemu_icount -= (env->icount_decr.u16.low
+                            + env->icount_extra);
+            env->icount_decr.u32 = 0;
+            env->icount_extra = 0;
+        }
+        next_cpu = env->next_cpu ?: first_cpu;
+        if (event_pending && likely(ret != EXCP_DEBUG)) {
+            ret = EXCP_INTERRUPT;
+            event_pending = 0;
+            break;
+        }
+        if (ret == EXCP_HLT) {
+            /* Give the next CPU a chance to run.  */
             cur_cpu = env;
-
-            if (unlikely(ret == EXCP_DEBUG)) {
-                gdb_set_stop_cpu(cur_cpu);
-                vm_stop(EXCP_DEBUG);
-            }
-            /* If all cpus are halted then wait until the next IRQ */
-            /* XXX: use timeout computed from timers */
-            if (ret == EXCP_HALTED) {
-                if (use_icount) {
-                    int64_t add;
-                    int64_t delta;
-                    /* Advance virtual time to the next event.  */
-                    if (use_icount == 1) {
-                        /* When not using an adaptive execution frequency
-                           we tend to get badly out of sync with real time,
-                           so just delay for a reasonable amount of time.  */
-                        delta = 0;
-                    } else {
-                        delta = cpu_get_icount() - cpu_get_clock();
-                    }
-                    if (delta > 0) {
-                        /* If virtual time is ahead of real time then just
-                           wait for IO.  */
-                        timeout = (delta / 1000000) + 1;
-                    } else {
-                        /* Wait for either IO to occur or the next
-                           timer event.  */
-                        add = qemu_next_deadline();
-                        /* We advance the timer before checking for IO.
-                           Limit the amount we advance so that early IO
-                           activity won't get the guest too far ahead.  */
-                        if (add > 10000000)
-                            add = 10000000;
-                        delta += add;
-                        add = (add + (1 << icount_time_shift) - 1)
-                              >> icount_time_shift;
-                        qemu_icount += add;
-                        timeout = delta / 1000000;
-                        if (timeout < 0)
-                            timeout = 0;
-                    }
-                } else {
-                    timeout = 5000;
-                }
-            } else {
-                timeout = 0;
-            }
-        } else {
-            env = env->next_cpu ?: first_cpu;
-            timeout = 5000;
+            continue;
         }
+        if (ret != EXCP_HALTED)
+            break;
+        /* all CPUs are halted ? */
+        if (env == cur_cpu)
+            break;
+     }
+     cur_cpu = env;
+
+     if (unlikely(ret == EXCP_DEBUG)) {
+         gdb_set_stop_cpu(cur_cpu);
+         vm_stop(EXCP_DEBUG);
+     }
+     /* If all cpus are halted then wait until the next IRQ */
+     /* XXX: use timeout computed from timers */
+     if (ret == EXCP_HALTED) {
+         if (use_icount) {
+             int64_t add;
+             int64_t delta;
+             /* Advance virtual time to the next event.  */
+             if (use_icount == 1) {
+                 /* When not using an adaptive execution frequency
+                    we tend to get badly out of sync with real time,
+                    so just delay for a reasonable amount of time.  */
+                 delta = 0;
+             } else {
+                 delta = cpu_get_icount() - cpu_get_clock();
+             }
+             if (delta > 0) {
+                 /* If virtual time is ahead of real time then just
+                    wait for IO.  */
+                 timeout = (delta / 1000000) + 1;
+             } else {
+                 /* Wait for either IO to occur or the next
+                    timer event.  */
+                 add = qemu_next_deadline();
+                 /* We advance the timer before checking for IO.
+                    Limit the amount we advance so that early IO
+                    activity won't get the guest too far ahead.  */
+                 if (add > 10000000)
+                     add = 10000000;
+                 delta += add;
+                 add = (add + (1 << icount_time_shift) - 1)
+                       >> icount_time_shift;
+                 qemu_icount += add;
+                 timeout = delta / 1000000;
+                 if (timeout < 0)
+                     timeout = 0;
+             }
+         } else {
+             timeout = 5000;
+         }
+     } else {
+         timeout = 0;
+     }
 #ifdef CONFIG_PROFILER
-        ti = profile_getclock();
+     ti = profile_getclock();
 #endif
-        qemu_wait_io_event(env, timeout);
 #ifdef CONFIG_PROFILER
-        dev_time += profile_getclock() - ti;
+     dev_time += profile_getclock() - ti;
 #endif
-    }
-    cpu_disable_ticks();
-    return NULL;
-}
-
-void qemu_init_vcpu(void *_env)
-{
-    CPUState *env = _env;
-    /* share a single thread for all cpus with TCG */
-    if (!tcg_cpu_thread) {
-        env->thread = qemu_mallocz(sizeof(QemuThread));
-        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
-        qemu_cond_init(env->halt_cond);
-        qemu_thread_create(env->thread, cpu_main_loop, env);
-        while (env->created == 0)
-            qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
-        tcg_cpu_thread = env->thread;
-        tcg_halt_cond = env->halt_cond;
-    } else {
-        env->thread = tcg_cpu_thread;
-        env->halt_cond = tcg_halt_cond;
-    }
-}
-
-static void qemu_init_state(void)
-{
-    qemu_cond_init(&qemu_pause_cond);
-    qemu_mutex_init(&qemu_fair_mutex);
-    qemu_mutex_init(&qemu_global_mutex);
-    qemu_mutex_lock(&qemu_global_mutex);
+    return timeout;
 }
 
 static void main_loop(void)
 {
     int r;
 
-    qemu_thread_self(&io_thread);
-    setup_iothread_fd();
-
-    unblock_io_signals();
+    pre_main_loop();
 
-    qemu_system_ready = 1;
-    qemu_cond_broadcast(&qemu_system_cond);
+    cur_cpu = first_cpu;
+    next_cpu = cur_cpu->next_cpu ?: first_cpu;
 
     while (1) {
+#ifdef QEMU_THREADED
         main_loop_wait(1000);
+#else
+        r = qemu_cpu_exec(next_cpu);
+        main_loop_wait(r);
+#endif
         if (qemu_shutdown_requested()) {
             pause_all_vcpus();
             if (no_shutdown)
#15 lore raw

PATCH 11/11 qemu: basic kvm iothread support

In reply to Marcelo Tosatti

Allow the iothread to run while vcpu is in guest mode.

kvm-all.c +2 -0
Index: trunk/kvm-all.c
===================================================================
@@ -451,6 +451,7 @@ int kvm_cpu_exec(CPUState *env)
 
     do {
         kvm_arch_pre_run(env, run);
+        kvm_pre_run(env);
 
         if (env->exit_request) {
             dprintf("interrupt exit requested\n");
@@ -459,6 +460,7 @@ int kvm_cpu_exec(CPUState *env)
         }
 
         ret = kvm_vcpu_ioctl(env, KVM_RUN, 0);
+        kvm_post_run(env);
         kvm_arch_post_run(env, run);
 
         if (ret == -EINTR || ret == -EAGAIN) {
kvm.h +4 -0
Index: trunk/kvm.h
===================================================================
@@ -27,6 +27,10 @@ extern int kvm_allowed;
 
 struct kvm_run;
 
+/* main loop interface, vl.c */
+void kvm_pre_run(CPUState *env);
+void kvm_post_run(CPUState *env);
+
 /* external API */
 
 int kvm_init(int smp_cpus);
target-i386/helper.c +0 -3
Index: trunk/target-i386/helper.c
===================================================================
@@ -1668,9 +1668,6 @@ CPUX86State *cpu_x86_init(const char *cp
 #ifdef USE_KQEMU
     kqemu_init(env);
 #endif
-    if (kvm_enabled())
-        kvm_init_vcpu(env);
-
     qemu_init_vcpu(env);
 
     return env;
target-ppc/helper.c +0 -2
Index: trunk/target-ppc/helper.c
===================================================================
@@ -2831,8 +2831,6 @@ CPUPPCState *cpu_ppc_init (const char *c
     cpu_ppc_register_internal(env, def);
     cpu_ppc_reset(env);
 
-    if (kvm_enabled())
-        kvm_init_vcpu(env);
     qemu_init_vcpu(env);
 
     return env;
vl.c +58 -2

102 lines not on this page · raw message