Skip to contentSkip to conversation

hook cpu running at a higher level.

PATCH In review Glauber Costa 4 participants 8 messages Dec 29, 2008 – Dec 30, 2008 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hook cpu running at a higher level.

This patch removes the kvm_enabled() check from cpu-exec.c. This file is highly tcg-specific, and we'll probably want it out when tcg is not compiled in (coming soon, in a theathe near you)

Instead, we hook at the main loop level. The amount of code duplication introduced is at worst, acceptable, and I believe it pays. The tcg mainloop is likely to be different from the hypervisors ones, since tcg runs all cpus in lockstep. KVM (and probably xen), will be able to span threads out for its vcpus.

---
 cpu-exec.c |    5 -----
 kvm-all.c  |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c       |   16 ++++++++++------
 3 files changed, 60 insertions(+), 11 deletions(-)
cpu-exec.c +0 -5
index ed1545b..be8ceac 100644
@@ -333,11 +333,6 @@ int cpu_exec(CPUState *env1)
             }
 #endif
 
-            if (kvm_enabled()) {
-                kvm_cpu_exec(env);
-                longjmp(env->jmp_env, 1);
-            }
-
             next_tb = 0; /* force lookup of first TB */
             for(;;) {
                 interrupt_request = env->interrupt_request;
kvm-all.c +50 -0

53 lines not on this page · raw message

vl.c +10 -6
index 0a02151..bcaccc3 100644
@@ -248,8 +248,8 @@ static struct drive_opt {
     char opt[1024];
 } drives_opt[MAX_DRIVES];
 
-static CPUState *cur_cpu;
-static CPUState *next_cpu;
+CPUState *cur_cpu;
+CPUState *next_cpu;
 static int event_pending = 1;
 /* Conversion factor from emulated instructions to virtual clock ticks.  */
 static int icount_time_shift;
@@ -3452,9 +3452,9 @@ typedef struct QEMUResetEntry {
 } QEMUResetEntry;
 
 static QEMUResetEntry *first_reset_entry;
-static int reset_requested;
-static int shutdown_requested;
-static int powerdown_requested;
+int reset_requested;
+int shutdown_requested;
+int powerdown_requested;
 
 int qemu_shutdown_requested(void)
 {
@@ -5535,7 +5535,11 @@ int main(int argc, char **argv, char **envp)
 	close(fd);
     }
 
-    main_loop();
+    if (kvm_enabled())
+        kvm_main_loop();
+    else
+        main_loop();
+
     quit_timers();
     net_cleanup();
 
Signature · 1 line
1.5.6.5
#1 lore raw

Glauber Costa writes ("[PATCH] hook cpu running at a higher level."):

Quoted 3 lines

This patch removes the kvm_enabled() check from cpu-exec.c. This file is highly tcg-specific, and we'll probably want it out when tcg is not compiled in (coming soon, in a theathe near you)

That would be interesting, certainly. We don't compile that file anyway right now, and have our own version of exec.c too (sadly it's somewhat clone-and-hack and it would be nice to make that neater in due course).

Quoted 6 lines

Instead, we hook at the main loop level. The amount of code duplication introduced is at worst, acceptable, and I believe it pays. The tcg mainloop is likely to be different from the hypervisors ones, since tcg runs all cpus in lockstep. KVM (and probably xen), will be able to span threads out for its vcpus.

I don't have an opinion about this patch really, but I did want to comment slightly on the way Xen does things just for future reference.

The Xen qemu process runs only in one thread which is fine because it doesn't need to be involved with actual processor execution. In theory parallel execution (in different threads and thus on different physical cpus) of IO emulations requested by different guest vcpus might make some small performance difference but I doubt it would be worth our while. So I think the Xen setup will still from qemu's point of view look like a single vcpu no matter how many vcpus the guest aactually has.

vl.c int main(int argc, char **argv, char **envp) · line 5542

Quoted 6 lines
vl.c +5 -1
-    main_loop();
+    if (kvm_enabled())
+        kvm_main_loop();
+    else
+        main_loop();
+

The way we have approached these problems in the Xen tree is to supply an alternative implementation of (say) main_loop and arrange for the standard one not to be compiled. Is it the intent to make kvm a run-time selectable option ? It seems to me that that given that we already have different qemu builds for all of the various target (guest) cpu architectures, it might be simpler to continue that approach. With a bit of judicious movement of code into appropriate files, this will avoid the need for ifs and ifdefs.

Regards,
Ian.

#2 lore raw
Quoted 9 lines · Ian Jackson

The way we have approached these problems in the Xen tree is to supply an alternative implementation of (say) main_loop and arrange for the standard one not to be compiled. Is it the intent to make kvm a run-time selectable option ? It seems to me that that given that we already have different qemu builds for all of the various target (guest) cpu architectures, it might be simpler to continue that approach. With a bit of judicious movement of code into appropriate files, this will avoid the need for ifs and ifdefs.

kvm is run-time selectable, both in upstream and in kvm-userspace. If kvm is not detected (or the caller lacks sufficient privileges), we fall back to tcg (of course we'd also like the option of not compiling tcg where emulation is unacceptable like server deployments).

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
In reply to Ian Jackson

On Tue, Dec 30, 2008 at 8:24 AM, Ian Jackson > The Xen qemu process runs only in one thread which is fine because it

Quoted 7 lines

doesn't need to be involved with actual processor execution. In theory parallel execution (in different threads and thus on different physical cpus) of IO emulations requested by different guest vcpus might make some small performance difference but I doubt it would be worth our while. So I think the Xen setup will still from qemu's point of view look like a single vcpu no matter how many vcpus the guest aactually has.

Is it one vcpu or various in lockstep ? If you have only one vcpu, your main loop can be even simpler than qemu's, which holds them all in a for loop.

Quoted 9 lines

The way we have approached these problems in the Xen tree is to supply an alternative implementation of (say) main_loop and arrange for the standard one not to be compiled. Is it the intent to make kvm a run-time selectable option ? It seems to me that that given that we already have different qemu builds for all of the various target (guest) cpu architectures, it might be simpler to continue that approach. With a bit of judicious movement of code into appropriate files, this will avoid the need for ifs and ifdefs.

I believe the idea here is to bring up something close to the accel patches (this one, plus the ones for memory_rw and registered I already posted). So you would not see this, but simply "cpu_main_loop()", that would call into the right thing, no matter what it is. This allow for both runtime and compile time selection of what you're running.

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
In reply to Avi Kivity

Avi Kivity writes ("Re: [PATCH] hook cpu running at a higher level."):

Quoted 4 lines

kvm is run-time selectable, both in upstream and in kvm-userspace. If kvm is not detected (or the caller lacks sufficient privileges), we fall back to tcg (of course we'd also like the option of not compiling tcg where emulation is unacceptable like server deployments).

Ah, right, well then the approach in the patch seems sensible.

Thanks,
Ian.

#6 lore raw
In reply to Ian Jackson
Quoted 9 lines · Ian Jackson

The Xen qemu process runs only in one thread which is fine because it doesn't need to be involved with actual processor execution. In theory parallel execution (in different threads and thus on different physical cpus) of IO emulations requested by different guest vcpus might make some small performance difference but I doubt it would be worth our while. So I think the Xen setup will still from qemu's point of view look like a single vcpu no matter how many vcpus the guest aactually has.

I thing it makes sense for you to have multiple vcpus -- that is multiple CPUState objects. You can still have just one thread (which would be the iothread in kvm's terminology).

So:

tcg - multiplexes all vcpus and io on one thread
kvm - iothread + per-vcpu thread
xen - iothread, vcpus scheduled by hypervisor

tcg also ought to move to an iothread model, so it can take advantage of multicore for things like display updates. For x86-on-x86, it may even be possible to have vcpus running in parallel (with some kind of shared/exclusive lock on softmmu).

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

Avi Kivity writes ("Re: [PATCH] hook cpu running at a higher level."):

Quoted 3 lines

I thing it makes sense for you to have multiple vcpus -- that is multiple CPUState objects. You can still have just one thread (which would be the iothread in kvm's terminology).

Xen does have multiple vcpus (of course) but the vcpus are modelled entirely in the hypervisor. No significant amount of state about these vcpus is in qemu-dm. Because of the way qemu is constructed we have to have a CPUState object in qemu-dm but there is only one of it and it's basically a dummy. It doesn't contain any of the usual i386 registers.

This will all be a bit weird if you're used to thinking of qemu as a whole system emulator (which is what it qemu, of course), but of course Xen didn't need or want the whole system emulator.

Quoted 1 line

From the point of view of qemu's architecture, it's as if qemu were

emulating a machine consisting of the peripherals plus a (single) pseudo-CPU which doesn't do anything resembling the execution of machine instructions (at least as far as qemu is concerned). This pseudo-CPU actually consists of much of the rest of the machine, including the hypervisor and the guest (including all of the guest's RAM). Since the state of this pseudo-CPU resides outside qemu, there is no representation of it inside the qemu-dm process.

The the pseudo-CPU code in qemu-dm is mostly a stub which is responsible for talking to the parts which actually run the pseudo-CPU (ie, which actually run the guest's vcpus, maintain the guest's RAM, and so forth).

Quoted 4 lines

So:
tcg - multiplexes all vcpus and io on one thread kvm - iothread + per-vcpu thread
xen - iothread, vcpus scheduled by hypervisor

So qemu-dm _is_ the iothread. The vcpus are elsewhere.

Ian.

#5 lore raw
In reply to Glauber Costa

Glauber Costa writes ("Re: [Qemu-devel] Re: [PATCH] hook cpu running at a higher level."):

Quoted 10 lines

On Tue, Dec 30, 2008 at 8:24 AM, Ian Jackson

Quoted 8 lines

The Xen qemu process runs only in one thread which is fine because it doesn't need to be involved with actual processor execution. In theory parallel execution (in different threads and thus on different physical cpus) of IO emulations requested by different guest vcpus might make some small performance difference but I doubt it would be worth our while. So I think the Xen setup will still from qemu's point of view look like a single vcpu no matter how many vcpus the guest aactually has.

Quoted 3 lines

Is it one vcpu or various in lockstep ? If you have only one vcpu, your main loop can be even simpler than qemu's, which holds them all in a for loop.

Whether it's one vcpu or several depends how you think about it. The guest and Xen see several vcpus, which execute (without any involvement from qemu) on the various physical cpus according to the Xen hypervisor's scheduler. qemu only sees one vcpu object which appears to generate all of the IO requests.

Our main loop is really just
read io request from magic fd
call cpu_something_rw which eventually calls appropriate io emulation send response to io request (ack or any data `read') back to magic fd
so yes it is very simple.

Execution of the guest's vcpus happens asynchronously (from qemu's point of view) until they need to do IO; then the hypervisor passes the request to qemu and stops the vcpu (by not scheduling it) until it gets a response from qemu. While no IO requests are being processed, qemu sleeps.

Ian.