Skip to contentSkip to conversation

make vmmouse work with KVM

Qemu-devel Reimar Döffinger 3 participants 10 messages Aug 17, 2009 – Aug 28, 2009 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,
vmmouse uses a giant hack: it uses io ports (in instruction) but passes data via registers.
This currently does not work since the qemu CPU registers are (understandably) not kept in sync with the real KVM registers for this operation.
Attached patch detects access to the vmmouse port and loads/stores CPU registers into/from the QEMU state.

kvm-all.c +10 -0
index f669c3a..207378b 100644
@@ -611,11 +611,21 @@ int kvm_cpu_exec(CPUState *env)
         switch (run->exit_reason) {
         case KVM_EXIT_IO:
             dprintf("handle_io\n");
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+            // HACK to make vmport/vmmouse work
+            if (run->io.port == 0x5658)
+                kvm_arch_get_registers(env);
+#endif
             ret = kvm_handle_io(env, run->io.port,
                                 (uint8_t *)run + run->io.data_offset,
                                 run->io.direction,
                                 run->io.size,
                                 run->io.count);
+#if defined(TARGET_I386) || defined(TARGET_X86_64)
+            // HACK to make vmport/vmmouse work
+            if (run->io.port == 0x5658)
+                kvm_arch_put_registers(env);
+#endif
             break;
         case KVM_EXIT_MMIO:
             dprintf("handle_mmio\n");
#1 lore raw
Quoted 9 lines · Reimar Döffinger

Hello,
vmmouse uses a giant hack: it uses io ports (in instruction) but passes data via registers.
This currently does not work since the qemu CPU registers are (understandably) not kept in sync with the real KVM registers for this operation.
Attached patch detects access to the vmmouse port and loads/stores CPU registers into/from the QEMU state.

Should use cpu_synchronize_state() in vmport.c

Quoted 2 lines

Regards,

Anthony Liguori

#2 lore raw
Quoted 11 lines · Anthony Liguori
Quoted 9 lines · Reimar Döffinger

Hello,
vmmouse uses a giant hack: it uses io ports (in instruction) but passes data via registers.
This currently does not work since the qemu CPU registers are (understandably) not kept in sync with the real KVM registers for this operation.
Attached patch detects access to the vmmouse port and loads/stores CPU registers into/from the QEMU state.

Quoted 1 line

Should use cpu_synchronize_state() in vmport.c

Ah, missed that function...
Does attached patch look good?

hw/vmport.c +10 -1
index 884af3f..9dc94a3 100644
@@ -25,6 +25,7 @@
 #include "isa.h"
 #include "pc.h"
 #include "sysemu.h"
+#include "kvm.h"
 
 //#define VMPORT_DEBUG
 
@@ -57,6 +58,9 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
     CPUState *env = cpu_single_env;
     unsigned char command;
     uint32_t eax;
+    uint32_t result;
+
+    cpu_synchronize_state(env, 0);
 
     eax = env->regs[R_EAX];
     if (eax != VMPORT_MAGIC)
@@ -73,14 +77,19 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
         return eax;
     }
 
-    return s->func[command](s->opaque[command], addr);
+    result = s->func[command](s->opaque[command], addr);
+    cpu_synchronize_state(env, 1);
+
+    return result;
 }
 
 static void vmport_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
     CPUState *env = cpu_single_env;
 
+    cpu_synchronize_state(env, 0);
     env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);
 }
 
 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
#3 lore raw

hw/vmport.c static uint32_t vmport_ioport_read(void *opaque, uint32_t addr) · line 92

Quoted 3 lines · Reimar Döffinger
hw/vmport.c +2 -0
+    cpu_synchronize_state(env, 0);
      env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Paolo

#4 lore raw
Quoted 6 lines · Paolo Bonzini

hw/vmport.c static uint32_t vmport_ioport_read(void *opaque, uint32_t addr) · line 92

Quoted 3 lines · Reimar Döffinger
hw/vmport.c +2 -0
+    cpu_synchronize_state(env, 0);
      env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Well... The cpu_synchronize_state could be dropped you are right, but here we write R_EAX so the cpu_synchronize_state(env, 1) is necessary. Want me to remove the cpu_synchronize_state(env, 0)?
It all seems a bit messy, because despite the "synchronize" name of the function any change to the registers before the call to vmport_ioport_read would be overwritten by the cpu_synchronize_state in there.
It might be slightly cleaner to rename the vmport_ioport_read (any name suggestions?) and add a wrapper for register_ioport_read that does the cpu_synchronize_state (so it looks similar to vmport_ioport_write).

Greetings,
Reimar Döffinger

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 6 lines · Reimar Döffinger
Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · Reimar Döffinger
hw/vmport.c +2 -0
+    cpu_synchronize_state(env, 0);
       env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);
Quoted 7 lines
Quoted 1 line

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Well... The cpu_synchronize_state could be dropped you are right, but here we write R_EAX so the cpu_synchronize_state(env, 1) is necessary. It might be slightly cleaner to rename the vmport_ioport_read (any name suggestions?) and add a wrapper for register_ioport_read that does the cpu_synchronize_state (so it looks similar to vmport_ioport_write).

Yes, that would look best and wouldn't have fooled me. Could vmport_ioport_trigger be a decent name?

Paolo

Paolo

#7 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Reimar Döffinger
Quoted 6 lines · Paolo Bonzini

Review

Quoted 3 lines · Reimar Döffinger
(quoted diff) +2 -0
+    cpu_synchronize_state(env, 0);
       env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Quoted 8 lines
Quoted 5 lines

Well... The cpu_synchronize_state could be dropped you are right, but here we write R_EAX so the cpu_synchronize_state(env, 1) is necessary. It might be slightly cleaner to rename the vmport_ioport_read (any name suggestions?) and add a wrapper for register_ioport_read that does the cpu_synchronize_state (so it looks similar to vmport_ioport_write).

Yes, that would look best and wouldn't have fooled me. Could vmport_ioport_trigger be a decent name?

Since what that function actually does is execute a specific command it gets from ecx I went with vmport_ioport_command, but I'll happily leave the naming to whoever applies it :-)

Greetings,
Reimar Döffinger

hw/vmport.c +16 -2
index 884af3f..7ba6e2e 100644
@@ -25,6 +25,7 @@
 #include "isa.h"
 #include "pc.h"
 #include "sysemu.h"
+#include "kvm.h"
 
 //#define VMPORT_DEBUG
 
@@ -51,7 +52,7 @@ void vmport_register(unsigned char command, IOPortReadFunc *func, void *opaque)
     port_state.opaque[command] = opaque;
 }
 
-static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
+static uint32_t vmport_ioport_command(void *opaque, uint32_t addr)
 {
     VMPortState *s = opaque;
     CPUState *env = cpu_single_env;
@@ -76,11 +77,24 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
     return s->func[command](s->opaque[command], addr);
 }
 
+static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
+{
+    CPUState *env = cpu_single_env;
+    uint32_t result;
+
+    cpu_synchronize_state(env, 0);
+    result = vmport_ioport_command(opaque, addr);
+    cpu_synchronize_state(env, 1);
+    return result;
+}
+
 static void vmport_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
     CPUState *env = cpu_single_env;
 
-    env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 0);
+    env->regs[R_EAX] = vmport_ioport_command(opaque, addr);
+    cpu_synchronize_state(env, 1);
 }
 
 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
#8 lore raw
Quoted 16 lines · Reimar Döffinger
Quoted 15 lines · Paolo Bonzini
Quoted 13 lines · Reimar Döffinger
Quoted 6 lines · Paolo Bonzini

Review

Quoted 3 lines · Reimar Döffinger
(quoted diff) +2 -0
+    cpu_synchronize_state(env, 0);
       env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Well... The cpu_synchronize_state could be dropped you are right, but here we write R_EAX so the cpu_synchronize_state(env, 1) is necessary. It might be slightly cleaner to rename the vmport_ioport_read (any name suggestions?) and add a wrapper for register_ioport_read that does the cpu_synchronize_state (so it looks similar to vmport_ioport_write).

Quoted 6 lines
Quoted 2 lines

Yes, that would look best and wouldn't have fooled me. Could vmport_ioport_trigger be a decent name?

Since what that function actually does is execute a specific command it gets from ecx I went with vmport_ioport_command, but I'll happily leave the naming to whoever applies it :-)

Simpler version due to new cpu_synchronize_state behaviour.

---

hw/vmport.c +2 -0
index 884af3f..9a942ee 100644
@@ -25,6 +25,7 @@
 #include "isa.h"
 #include "pc.h"
 #include "sysemu.h"
+#include "kvm.h"
 
 //#define VMPORT_DEBUG
 
@@ -58,6 +59,7 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
     unsigned char command;
     uint32_t eax;
 
+    cpu_synchronize_state(env);
     eax = env->regs[R_EAX];
     if (eax != VMPORT_MAGIC)
         return eax;
#9 lore raw
Quoted 22 lines · Reimar Döffinger
Quoted 20 lines · Reimar Döffinger
Quoted 18 lines · Paolo Bonzini
Quoted 16 lines · Reimar Döffinger
Quoted 8 lines · Paolo Bonzini

Review

Quoted 4 lines · Reimar Döffinger
(quoted diff) +2 -0
+    cpu_synchronize_state(env, 0);
       env->regs[R_EAX] = vmport_ioport_read(opaque, addr);
+    cpu_synchronize_state(env, 1);

This is not needed because the sync is done in vmport_ioport_read, isn't it?

Well... The cpu_synchronize_state could be dropped you are right, but here we write R_EAX so the cpu_synchronize_state(env, 1) is necessary. It might be slightly cleaner to rename the vmport_ioport_read (any name suggestions?) and add a wrapper for register_ioport_read that does the cpu_synchronize_state (so it looks similar to vmport_ioport_write).

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Yes, that would look best and wouldn't have fooled me. Could vmport_ioport_trigger be a decent name?

Since what that function actually does is execute a specific command it gets from ecx I went with vmport_ioport_command, but I'll happily leave the naming to whoever applies it :-)

Simpler version due to new cpu_synchronize_state behaviour.

Please top post patches.

Regards,

Anthony Liguori

#10 lore raw
In reply to Reimar Döffinger
Quoted 10 lines · Reimar Döffinger

Hello,
vmmouse uses a giant hack: it uses io ports (in instruction) but passes data via registers.
This currently does not work since the qemu CPU registers are (understandably) not kept in sync with the real KVM registers for this operation.
Attached patch detects access to the vmmouse port and loads/stores CPU registers into/from the QEMU state.

Slightly simplified since TARGET_X86_64 implies TARGET_I386.

kvm-all.c +10 -0
index f669c3a..639bd77 100644
@@ -611,11 +611,21 @@ int kvm_cpu_exec(CPUState *env)
         switch (run->exit_reason) {
         case KVM_EXIT_IO:
             dprintf("handle_io\n");
+#ifdef TARGET_I386
+            // HACK to make vmport/vmmouse work
+            if (run->io.port == 0x5658)
+                kvm_arch_get_registers(env);
+#endif
             ret = kvm_handle_io(env, run->io.port,
                                 (uint8_t *)run + run->io.data_offset,
                                 run->io.direction,
                                 run->io.size,
                                 run->io.count);
+#ifdef TARGET_I386
+            // HACK to make vmport/vmmouse work
+            if (run->io.port == 0x5658)
+                kvm_arch_put_registers(env);
+#endif
             break;
         case KVM_EXIT_MMIO:
             dprintf("handle_mmio\n");