Skip to contentSkip to conversation

iothread (candidate for inclusion)

PATCH In review Marcelo Tosatti 7 participants 31 messages Mar 25, 2009 – Apr 7, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover iothread (candidate for inclusion)

This version is simplified thanks to the removal of CPU_INTERRUPT_EXIT.

There are two pending issues: vm_stop() from ENOSPC handling and GDB contexes. IMO those can be worked while testing is performed by developers.

KVM SMP support is missing, but most of the infra required is present, so it should be relatively simple.

There's also lots of room for TCG perf improvements.

Tested with target-x86_64 and target-ppc.

#1 lore raw

PATCH 1/10 qemu: create helper for event notification

hw/dma.c +1 -3
Index: trunk/hw/dma.c
===================================================================
@@ -445,9 +445,7 @@ int DMA_write_memory (int nchan, void *b
 /* request the emulator to transfer a new DMA memory block ASAP */
 void DMA_schedule(int nchan)
 {
-    CPUState *env = cpu_single_env;
-    if (env)
-        cpu_exit(env);
+    qemu_notify_event();
 }
 
 static void dma_reset(void *opaque)
vl.c +21 -34

106 lines not on this page · raw message

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

Re: [Qemu-devel] [patch 01/10] qemu: create helper for event notification

Quoted 11 lines
vl.c +11 -0
+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
+     }
Quoted 1 line

 }

Have you tested this with kqemu? It will fire kqemu interrupts in a lot of circunstances
it didn't before. That said, I don't even think kqemu is that important, but if it's in the tree,
we don't want to introduce regressions. I'd suggest passing a flag indicating the type of
event we're notifying. It's easy to foresee other uses for that as well.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#3 lore raw

Re: [Qemu-devel] [patch 01/10] qemu: create helper for event notification

Quoted 6 lines · Glauber Costa
Quoted 6 lines
vl.c +6 -0
+void qemu_notify_event(void)
+{
+    CPUState *env = cpu_single_env;
+
+    if (env) {
+        cpu_exit(env);
Quoted 8 lines
Quoted 6 lines
vl.c +5 -0
+#ifdef USE_KQEMU
+        if (env->kqemu_enabled)
+            kqemu_cpu_interrupt(env);
+#endif
+     }

 }

Have you tested this with kqemu?

Nope.

Quoted 5 lines

It will fire kqemu interrupts in a lot of circunstances it didn't before. That said, I don't even think kqemu is that important, but if it's in the tree, we don't want to introduce regressions. I'd suggest passing a flag indicating the type of event we're notifying. It's easy to foresee other uses for that as well.

qemu_notify_event() basically means "process events handled by main_loop_wait ASAP". Maybe it needs a better name.

I'll make sure there's no serious regression with kqemu.

There are things like

/* reset soft MMU for next block (it can currently
only be set by a memory fault) */
#if defined(USE_KQEMU)

#define MIN_CYCLE_BEFORE_SWITCH (100 * 1000)
                if (kqemu_is_ok(env) &&
                    (cpu_get_time_fast() - env->last_io_time) >=
MIN_CYCLE_BEFORE_SWITCH) {
                    cpu_loop_exit();
                }

#endif

Which needs some rethinking.

#4 lore raw

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

In reply to Marcelo Tosatti
qemu-thread.c new file +123 -0
Index: trunk/qemu-thread.c
===================================================================
@@ -0,0 +1,123 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include <signal.h>
+#include <stdint.h>
+#include "qemu-thread.h"
+
+static void error_exit(const char *msg)
+{
+    perror(msg);
+    exit(1);
+}
+
+void qemu_mutex_init(QemuMutex *mutex)
+{
+    if (pthread_mutex_init(&mutex->lock, NULL))
+        error_exit(__func__);
+}
+
+void qemu_mutex_lock(QemuMutex *mutex)
+{
+    if (pthread_mutex_lock(&mutex->lock))
+        error_exit(__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 r;
+    struct timespec ts;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    r = pthread_mutex_timedlock(&mutex->lock, &ts);
+    if (r && r != ETIMEDOUT)
+        error_exit(__func__);
+    return r;
+}
+
+void qemu_mutex_unlock(QemuMutex *mutex)
+{
+    if (pthread_mutex_unlock(&mutex->lock))
+        error_exit(__func__);
+}
+
+void qemu_cond_init(QemuCond *cond)
+{
+    if (pthread_cond_init(&cond->cond, NULL))
+        error_exit(__func__);
+}
+
+void qemu_cond_signal(QemuCond *cond)
+{
+    if (pthread_cond_signal(&cond->cond))
+        error_exit(__func__);
+}
+
+void qemu_cond_broadcast(QemuCond *cond)
+{
+    if (pthread_cond_broadcast(&cond->cond))
+        error_exit(__func__);
+}
+
+void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
+{
+    if (pthread_cond_wait(&cond->cond, &mutex->lock))
+        error_exit(__func__);
+}
+
+int qemu_cond_timedwait(QemuCond *cond, QemuMutex *mutex, uint64_t msecs)
+{
+    struct timespec ts;
+    int r;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    r = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
+    if (r && r != ETIMEDOUT)
+        error_exit(__func__);
+    return r;
+}
+
+void qemu_thread_create(QemuThread *thread,
+                       void *(*start_routine)(void*),
+                       void *arg)
+{
+    if (pthread_create(&thread->thread, NULL, start_routine, arg))
+        error_exit(__func__);
+}
+
+void qemu_thread_signal(QemuThread *thread, int sig)
+{
+    if (pthread_kill(thread->thread, sig))
+        error_exit(__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
#5 lore raw

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

qemu-thread.c

Quoted 6 lines · Marcelo Tosatti

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

qemu-thread.c new file +0 -0

I think it all fits better as static inline defined in qemu-thread.h file. It'll surely generate better code.

qemu-thread.c line 54

Quoted 13 lines
qemu-thread.c new file +13 -0
+int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
+{
+    int r;
+    struct timespec ts;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    r = pthread_mutex_timedlock(&mutex->lock, &ts);
+    if (r && r != ETIMEDOUT)
+        error_exit(__func__);
+    return r;
+}

Do we have in-tree users of that? Although it is a matter of personal taste, I don't like timed locks. So if we don't have any imediate users, we might as well
remove it for the sake of simplicity.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#6 lore raw

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

Quoted 9 lines · Glauber Costa

qemu-thread.c

Quoted 6 lines · Marcelo Tosatti

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

qemu-thread.c new file +0 -0

I think it all fits better as static inline defined in qemu-thread.h file. It'll surely generate better code.

Sure, can be done.

Quoted 10 lines
Quoted 10 lines
qemu-thread.c new file +10 -0
+int qemu_mutex_timedlock(QemuMutex *mutex, uint64_t msecs)
+{
+    int r;
+    struct timespec ts;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    timespec_add_ms(&ts, msecs);
+
+    r = pthread_mutex_timedlock(&mutex->lock, &ts);
+    if (r && r != ETIMEDOUT)
Quoted 8 lines
Quoted 3 lines

+        error_exit(__func__);
+    return r;
+}

Do we have in-tree users of that? Although it is a matter of personal taste, I don't like timed locks. So if we don't have any imediate users, we might as well
remove it for the sake of simplicity.

There are. See patch 5.

#7 lore raw

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

In reply to Marcelo Tosatti
Quoted 16 lines · Marcelo Tosatti

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

qemu-thread.c new file +9 -0
@@ -0,0 +1,123 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include <signal.h>
+#include <stdint.h>
+#include "qemu-thread.h"
+
+static void error_exit(const char *msg)

qemu-thread.c line 19

Quoted 10 lines
qemu-thread.c new file +10 -0
+{
+    perror(msg);
+    exit(1);
+}
+
+void qemu_mutex_init(QemuMutex *mutex)
+{
+    if (pthread_mutex_init(&mutex->lock, NULL))
+        error_exit(__func__);
+}

This is wrong, pthread functions return the error code and do not set the errno thus using perror is meaningless.

[..snip..]

Signature · 1 line
mailto:[email protected]
#8 lore raw

PATCH 3/10 qemu: per-arch cpu_has_work

In reply to Marcelo Tosatti
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 *env)
+{
+    return (env->interrupt_request & CPU_INTERRUPT_HARD) &&
+           (env->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(env)) {
         env1->halted = 0;
         return 0;
     }
#9 lore raw

Re: [Qemu-devel] [patch 03/10] qemu: per-arch cpu_has_work

Quoted 8 lines · Marcelo Tosatti

Index: trunk/target-alpha/exec.h
===================================================================

target-alpha/exec.h +0 -0

From my POV, this patch is okay, and can go in regardless of the io-thread work.

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#11 lore raw

Re: [Qemu-devel] [patch 03/10] qemu: per-arch cpu_has_work

Quoted 6 lines · Blue Swirl
Quoted 1 line · Marcelo Tosatti

While the patch looks safe, it breaks Sparc32. I think you have mixed
env1, s and env (which is defined as a fixed host register).

This version works.

[patch 03/10] qemu: per-arch cpu_has_work

target-alpha/exec.h +6 -1
Index: qemu/target-alpha/exec.h
===================================================================
@@ -48,10 +48,15 @@
 {
 }
 
+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: qemu/target-i386/exec.h
===================================================================
@@ -338,14 +338,23 @@
 #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: qemu/cpu-all.h
===================================================================
@@ -775,6 +775,8 @@
 
 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: qemu/cpu-exec.c
===================================================================
@@ -50,6 +50,11 @@
 //#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: qemu/target-arm/exec.h
===================================================================
@@ -37,14 +37,19 @@
 {
 }
 
+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: qemu/target-cris/exec.h
===================================================================
@@ -40,6 +40,11 @@
 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: qemu/target-m68k/exec.h
===================================================================
@@ -41,10 +41,15 @@
 #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: qemu/target-mips/exec.h
===================================================================
@@ -33,12 +33,18 @@
 {
 }
 
+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: qemu/target-ppc/exec.h
===================================================================
@@ -44,11 +44,17 @@
 {
 }
 
+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: qemu/target-sh4/exec.h
===================================================================
@@ -28,10 +28,15 @@
 #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: qemu/target-sparc/exec.h
===================================================================
@@ -24,10 +24,17 @@
 /* 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;
     }
#12 lore raw

Re: [Qemu-devel] [patch 03/10] qemu: per-arch cpu_has_work

Quoted 9 lines · Blue Swirl
Quoted 6 lines · Blue Swirl
Quoted 1 line · Marcelo Tosatti

While the patch looks safe, it breaks Sparc32. I think you have mixed
env1, s and env (which is defined as a fixed host register).

This version works.

New patchset include this version. Thanks.

#13 lore raw

PATCH 4/10 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

#14 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

vl.c void main_loop_wait(int timeout) · line 3832

Quoted 6 lines
vl.c +6 -0
+static void setup_iothread_fd(void)
+{
+    int fds[2];
+
+    if (pipe(fds) == -1) {
+        fprintf(stderr, "failed to create iothread pipe");

qemu_log() ?

in general, looks good

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#15 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

In reply to Marcelo Tosatti
Quoted 18 lines · Marcelo Tosatti

Use a pipe to signal pending work for the iothread.

vl.c +16 -0
+void main_loop_break(void)
+{
+    uint64_t value = 1;
+    char buffer[8];
+    size_t offset = 0;
+
+    if (io_thread_fd == -1)
+        return;
+
+    memcpy(buffer, &value, sizeof(value));
+
+    while (offset < 8) {
+        ssize_t len;
+
+        len = write(io_thread_fd, buffer + offset, 8 - offset);
+        if (len == -1 && errno == EINTR)

vl.c void qemu_notify_event(void) · line 3661

Quoted 10 lines
vl.c +10 -0
+            continue;
+
+        if (len <= 0)
+            break;
+
+        offset += len;
+    }
+
+    if (offset != 8)
+        fprintf(stderr, "failed to notify io thread\n");

1: Why do you write 8 bytes instead of 1 byte? If you're thinking of
passing a value at some point, you could change it later when it's needed. But beware that requiring the pipe to hold all values written is what made Netscape 4 lock up on too-new Linux kernels some 10 years ago :-)

2: Do you know that writes <= PIPE_BUF are atomic so you don't need
the offset calculation?

Quoted 10 lines
vl.c +10 -0
+/* Used to break IO thread out of select */
+static void io_thread_wakeup(void *opaque)
+{
+    int fd = (unsigned long)opaque;
+    char buffer[8];
+    size_t offset = 0;
+
+    while (offset < 8) {
+        ssize_t len;
+

vl.c void qemu_notify_event(void) · line 3683

Quoted 10 lines
vl.c +10 -0
+        len = read(fd, buffer + offset, 8 - offset);
+        if (len == -1 && errno == EINTR)
+            continue;
+
+        if (len <= 0)
+            break;
+
+        offset += len;
+    }
+}

You should read until the pipe is empty, in case more than one signal was raised and called main_loop_break() between calls to io_thread_wait().

Since reads <= PIPE_BUF are atomic too, the easiest way to do that is try to read at least one more byte than you wrote per main_loop_break() call, and if you don't get that many, you've emptied the pipe.

vl.c void main_loop_wait(int timeout) · line 3839

Quoted 13 lines
vl.c +13 -0
+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];
+}

To avoid deadlock in perverse conditions where lots of signals call main_loop_break() enough to fill the pipe before it's read, set the write side non-blocking.

To use the trick of reading more bytes than you expect to detect EOF without an extra read, set the read side non-blocking too.

-- Jamie

#16 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

Quoted 36 lines · Jamie Lokier
Quoted 18 lines · Marcelo Tosatti

Use a pipe to signal pending work for the iothread.

vl.c +16 -0
+void main_loop_break(void)
+{
+    uint64_t value = 1;
+    char buffer[8];
+    size_t offset = 0;
+
+    if (io_thread_fd == -1)
+        return;
+
+    memcpy(buffer, &value, sizeof(value));
+
+    while (offset < 8) {
+        ssize_t len;
+
+        len = write(io_thread_fd, buffer + offset, 8 - offset);
+        if (len == -1 && errno == EINTR)

vl.c void qemu_notify_event(void) · line 3661

Quoted 10 lines
vl.c +10 -0
+            continue;
+
+        if (len <= 0)
+            break;
+
+        offset += len;
+    }
+
+    if (offset != 8)
+        fprintf(stderr, "failed to notify io thread\n");

1: Why do you write 8 bytes instead of 1 byte? If you're thinking of
passing a value at some point, you could change it later when it's needed. But beware that requiring the pipe to hold all values written is what made Netscape 4 lock up on too-new Linux kernels some 10 years ago :-)

Quoted 2 lines

2: Do you know that writes <= PIPE_BUF are atomic so you don't need
the offset calculation?

I didnt know. Anthony wrote this code, so I'll let him comment.

Quoted 39 lines
Quoted 10 lines
vl.c +10 -0
+/* Used to break IO thread out of select */
+static void io_thread_wakeup(void *opaque)
+{
+    int fd = (unsigned long)opaque;
+    char buffer[8];
+    size_t offset = 0;
+
+    while (offset < 8) {
+        ssize_t len;
+

vl.c void qemu_notify_event(void) · line 3683

Quoted 10 lines
vl.c +10 -0
+        len = read(fd, buffer + offset, 8 - offset);
+        if (len == -1 && errno == EINTR)
+            continue;
+
+        if (len <= 0)
+            break;
+
+        offset += len;
+    }
+}

You should read until the pipe is empty, in case more than one signal was raised and called main_loop_break() between calls to io_thread_wait().

Since reads <= PIPE_BUF are atomic too, the easiest way to do that is try to read at least one more byte than you wrote per main_loop_break() call, and if you don't get that many, you've emptied the pipe.

Quoted 9 lines
vl.c +9 -0
+static void setup_iothread_fd(void)
+{
+    int fds[2];
+
+    if (pipe(fds) == -1) {
+        fprintf(stderr, "failed to create iothread pipe");
+        exit(0);
+    }
+
Quoted 8 lines

vl.c void main_loop_wait(int timeout) · line 3839

Quoted 4 lines
vl.c +4 -0
+    qemu_set_fd_handler2(fds[0], NULL, io_thread_wakeup, NULL,
+                         (void *)(unsigned long)fds[0]);
+    io_thread_fd = fds[1];
+}

To avoid deadlock in perverse conditions where lots of signals call main_loop_break() enough to fill the pipe before it's read, set the write side non-blocking.

The pipe is set nonblocking by a later patch. Perhaps the hunk should be part of this patch.

Quoted 2 lines

To use the trick of reading more bytes than you expect to detect EOF without an extra read, set the read side non-blocking too.

#17 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

Quoted 2 lines · Marcelo Tosatti

I didnt know. Anthony wrote this code, so I'll let him comment.

Is this guaranteed by Posix and reliable across Unixes? In general, I think it's better to be conservative with this sort of thing.

Regards,

Anthony Liguori

#18 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

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

I didnt know. Anthony wrote this code, so I'll let him comment.

Is this guaranteed by Posix and reliable across Unixes? In general, I think it's better to be conservative with this sort of thing.

For what it's worth, POSIX.1-2001 guarantees atomicity of up to PIPE_BUF bytes, and PIPE_BUF must be at least 512. See pipe(7). More modern chapter & verse is online:

http://www.opengroup.org/onlinepubs/009695399/functions/write.html
http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html

A note on select() bugs related to PIPE_BUF in some ancient crap:

http://cr.yp.to/docs/unixport.html

#19 lore raw

Re: [Qemu-devel] [patch 04/10] qemu: introduce main_loop_break

In reply to Anthony Liguori
Quoted 5 lines · Anthony Liguori
Quoted 2 lines · Marcelo Tosatti

I didnt know. Anthony wrote this code, so I'll let him comment.

Is this guaranteed by Posix and reliable across Unixes? In general, I think it's better to be conservative with this sort of thing.

All the changes I suggested don't depend on pipe atomicity. (My mistake for bringing it up).

As long as you're only writing 1 byte, write atomicity isn't relevant. (If the current code actually did anything with its 8 byte chunks it writes, _that_ would depend on atomicity.)

The trick of reading 2 bytes and assuming the pipe is emptied if you only get 1 byte doesn't dependent on read atomicity, because in the unlikely event there is another byte, that's ok: the select() will return immediately and you'll read again anyway. That can happen even with atomicity due to parallelism. So reading 2 bytes is just a heuristic trick to avoid an unnecessary system call in most cases.

So here's the signal-safe, thread-safe self-pipe trick as I see it:

- Set both ends of the pipe to non-blocking.

- Write 1 byte in signal handler, other threads etc. Don't care if you get EAGAIN; it just means the pipe is full, which is fine. (As an optimisation you can set a flag and avoid writing a second time when you know the pipe is non-empty. But to do this right, you have to be careful with memory ordering etc. Maybe this is not worth it.)

- Read 2 or more bytes in the select() handler for the read side. If you get all the bytes you asked for, loop doing it again. The loop is to drain the pipe, avoiding spurious select() wakeups. When you get fewer bytes than you asked for, the pipe is probably empty, but if it isn't that's fine.

-- Jamie

#20 lore raw

PATCH 5/10 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
#21 lore raw

PATCH 6/10 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
===================================================================
@@ -2831,6 +2831,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
===================================================================
@@ -2495,6 +2495,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
===================================================================
@@ -8480,6 +8480,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
===================================================================
@@ -288,6 +288,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;
 }
 
#22 lore raw

Re: [Qemu-devel] [patch 06/10] qemu: per-cpu thread information

Quoted 2 lines · Marcelo Tosatti

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

What prevents it to happen in common code?

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#23 lore raw

Re: [Qemu-devel] [patch 06/10] qemu: per-cpu thread information

Quoted 5 lines · Glauber Costa
Quoted 2 lines · Marcelo Tosatti

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

What prevents it to happen in common code?

To be accessed in non-cpu context, you mean? Or to be accessed from linux-user context?

#24 lore raw

Re: [Qemu-devel] [patch 06/10] qemu: per-cpu thread information

Quoted 9 lines · Marcelo Tosatti
Quoted 5 lines · Glauber Costa
Quoted 2 lines · Marcelo Tosatti

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

What prevents it to happen in common code?

To be accessed in non-cpu context, you mean? Or to be accessed from linux-user context?

non-per-arch

Signature · 5 lines
Glauber  Costa.
"Free as in Freedom"
http://glommer.net

"The less confident you are, the more serious you have to act."
#25 lore raw

Re: [Qemu-devel] [patch 06/10] qemu: per-cpu thread information

Quoted 11 lines · Glauber Costa
Quoted 9 lines · Marcelo Tosatti
Quoted 5 lines · Glauber Costa
Quoted 2 lines · Marcelo Tosatti

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

What prevents it to happen in common code?

To be accessed in non-cpu context, you mean? Or to be accessed from linux-user context?

non-per-arch

Huh?

A informacao e' acessada de codigo comum, vl.c. Nao entendi o problema?

#26 lore raw

PATCH 7/10 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
===================================================================
@@ -4030,25 +4030,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);
@@ -4097,10 +4078,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
@@ -4151,8 +4128,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)
#27 lore raw

PATCH 8/10 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

#28 lore raw

PATCH 9/10 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
===================================================================
@@ -3550,7 +3550,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();
@@ -3560,6 +3575,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 {
@@ -3757,6 +3787,8 @@ static int cpu_can_run(CPUState *env)
         return 0;
     if (reset_requested)
         return 0;
+    if (vmstop_requested)
+        return 0;
     return 1;
 }
 
@@ -4203,6 +4235,8 @@ static void qemu_init_state(void)
 
 static void main_loop(void)
 {
+    int r;
+
     qemu_thread_self(&io_thread);
     setup_iothread_fd();
 
@@ -4219,12 +4253,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);
         }
     }
 }
#29 lore raw

PATCH 10/10 qemu: basic kvm iothread support

In reply to Marcelo Tosatti

Allow the iothread to run while vcpu is in guest mode. To be tuned with SMP support.

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
===================================================================
@@ -2829,8 +2829,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 +47 -3

81 lines not on this page · raw message

#30 lore raw
In reply to Marcelo Tosatti
Quoted 11 lines · Marcelo Tosatti

This version is simplified thanks to the removal of CPU_INTERRUPT_EXIT.

There are two pending issues: vm_stop() from ENOSPC handling and GDB contexes. IMO those can be worked while testing is performed by developers.

KVM SMP support is missing, but most of the infra required is present, so it should be relatively simple.

There's also lots of room for TCG perf improvements.

Tested with target-x86_64 and target-ppc.

With the updated patch #3, Sparc32 also works.

I think I see a minor performance gain on a dual core host.