Skip to contentSkip to conversation

Guest debugging support for KVM - v2

PATCH Superseded Jan Kiszka 2 participants 3 messages Jan 25, 2009 – Jan 26, 2009

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/2 Guest debugging support for KVM - v2

[ Recent kvm headers no longer require CONFIG_<ARCH>, so I dropped the related configure change. Moreover, this version is not capable of setting soft breakpoint in ROM memory. ]

This is a backport of the guest debugging support for the KVM accelerator that is now part of the KVM tree. It implements the reworked KVM kernel API for guest debugging (KVM_CAP_SET_GUEST_DEBUG) which is not yet part of any mainline kernel but will probably be 2.6.30 stuff. So far supported is x86, but PPC is expected to catch up soon.

Core features are:
- unlimited soft-breakpoints via code patching
- hardware-assisted x86 breakpoints and watchpoints

---

 exec.c            |   10 ++-
 gdbstub.c         |   29 ++++++--
 gdbstub.h         |    7 ++
 kvm-all.c         |  172 ++++++++++++++++++++++++++++++++++++++++++++++++
 kvm.h             |   41 +++++++++++
 target-i386/kvm.c |  191 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 440 insertions(+), 10 deletions(-)
exec.c +7 -3
index 56e5e48..84c82ec 100644
@@ -1456,9 +1456,13 @@ void cpu_single_step(CPUState *env, int enabled)
 #if defined(TARGET_HAS_ICE)
     if (env->singlestep_enabled != enabled) {
         env->singlestep_enabled = enabled;
-        /* must flush all the translated code to avoid inconsistancies */
-        /* XXX: only flush what is necessary */
-        tb_flush(env);
+        if (kvm_enabled())
+            kvm_update_guest_debug(env, 0);
+        else {
+            /* must flush all the translated code to avoid inconsistancies */
+            /* XXX: only flush what is necessary */
+            tb_flush(env);
+        }
     }
 #endif
 }
gdbstub.c +22 -7
index b4b8292..0a91c7d 100644
@@ -38,6 +38,7 @@
 #define MAX_PACKET_LENGTH 4096
 
 #include "qemu_socket.h"
+#include "kvm.h"
 
 
 enum {
@@ -1416,13 +1417,6 @@ void gdb_register_coprocessor(CPUState * env,
     }
 }
 
-/* GDB breakpoint/watchpoint types */
-#define GDB_BREAKPOINT_SW        0
-#define GDB_BREAKPOINT_HW        1
-#define GDB_WATCHPOINT_WRITE     2
-#define GDB_WATCHPOINT_READ      3
-#define GDB_WATCHPOINT_ACCESS    4
-
 #ifndef CONFIG_USER_ONLY
 static const int xlat_gdb_type[] = {
     [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
@@ -1436,6 +1430,9 @@ static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
+
     switch (type) {
     case GDB_BREAKPOINT_SW:
     case GDB_BREAKPOINT_HW:
@@ -1467,6 +1464,9 @@ static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
+
     switch (type) {
     case GDB_BREAKPOINT_SW:
     case GDB_BREAKPOINT_HW:
@@ -1496,6 +1496,11 @@ static void gdb_breakpoint_remove_all(void)
 {
     CPUState *env;
 
+    if (kvm_enabled()) {
+        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
+        return;
+    }
+
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu_breakpoint_remove_all(env, BP_GDB);
 #ifndef CONFIG_USER_ONLY
@@ -1536,6 +1541,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             s->c_cpu->eip = addr;
+            if (kvm_enabled())
+                kvm_put_registers(s->c_cpu);
 #elif defined (TARGET_PPC)
             s->c_cpu->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -1577,6 +1584,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             s->c_cpu->eip = addr;
+            if (kvm_enabled())
+                kvm_put_registers(s->c_cpu);
 #elif defined (TARGET_PPC)
             s->c_cpu->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -1622,6 +1631,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
         }
         break;
     case 'g':
+        if (kvm_enabled())
+            kvm_get_registers(s->g_cpu);
         len = 0;
         for (addr = 0; addr < num_g_regs; addr++) {
             reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
@@ -1639,6 +1650,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
             len -= reg_size;
             registers += reg_size;
         }
+        if (kvm_enabled())
+            kvm_put_registers(s->g_cpu);
         put_packet(s, "OK");
         break;
     case 'm':
@@ -1797,6 +1810,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
             thread = strtoull(p+16, (char **)&p, 16);
             for (env = first_cpu; env != NULL; env = env->next_cpu)
                 if (env->cpu_index + 1 == thread) {
+                    if (kvm_enabled())
+                        kvm_get_registers(env);
                     len = snprintf((char *)mem_buf, sizeof(mem_buf),
                                    "CPU#%d [%s]", env->cpu_index,
                                    env->halted ? "halted " : "running");
gdbstub.h +7 -0
index c7d1c4b..5740041 100644
@@ -3,6 +3,13 @@
 
 #define DEFAULT_GDBSTUB_PORT "1234"
 
+/* GDB breakpoint/watchpoint types */
+#define GDB_BREAKPOINT_SW        0
+#define GDB_BREAKPOINT_HW        1
+#define GDB_WATCHPOINT_WRITE     2
+#define GDB_WATCHPOINT_READ      3
+#define GDB_WATCHPOINT_ACCESS    4
+
 typedef void (*gdb_syscall_complete_cb)(CPUState *env,
                                         target_ulong ret, target_ulong err);
 
kvm-all.c +172 -0

193 lines not on this page · raw message

kvm.h +41 -0

57 lines not on this page · raw message

target-i386/kvm.c +191 -0

200 lines not on this page · raw message

#1 lore raw
Quoted 105 lines · Jan Kiszka

[ Recent kvm headers no longer require CONFIG_<ARCH>, so I dropped the related configure change. Moreover, this version is not capable of setting soft breakpoint in ROM memory. ]

This is a backport of the guest debugging support for the KVM accelerator that is now part of the KVM tree. It implements the reworked KVM kernel API for guest debugging (KVM_CAP_SET_GUEST_DEBUG) which is not yet part of any mainline kernel but will probably be 2.6.30 stuff. So far supported is x86, but PPC is expected to catch up soon.

Core features are:
- unlimited soft-breakpoints via code patching
- hardware-assisted x86 breakpoints and watchpoints

---

 exec.c            |   10 ++-
 gdbstub.c         |   29 ++++++--
 gdbstub.h         |    7 ++
 kvm-all.c         |  172 ++++++++++++++++++++++++++++++++++++++++++++++++
 kvm.h             |   41 +++++++++++
 target-i386/kvm.c |  191 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 440 insertions(+), 10 deletions(-)
exec.c +7 -3
index 56e5e48..84c82ec 100644
@@ -1456,9 +1456,13 @@ void cpu_single_step(CPUState *env, int enabled)
 #if defined(TARGET_HAS_ICE)
     if (env->singlestep_enabled != enabled) {
         env->singlestep_enabled = enabled;
-        /* must flush all the translated code to avoid inconsistancies */
-        /* XXX: only flush what is necessary */
-        tb_flush(env);
+        if (kvm_enabled())
+            kvm_update_guest_debug(env, 0);
+        else {
+            /* must flush all the translated code to avoid inconsistancies */
+            /* XXX: only flush what is necessary */
+            tb_flush(env);
+        }
     }
 #endif
 }
gdbstub.c +12 -7
index b4b8292..0a91c7d 100644
@@ -38,6 +38,7 @@
 #define MAX_PACKET_LENGTH 4096
 
 #include "qemu_socket.h"
+#include "kvm.h"
 
 
 enum {
@@ -1416,13 +1417,6 @@ void gdb_register_coprocessor(CPUState * env,
     }
 }
 
-/* GDB breakpoint/watchpoint types */
-#define GDB_BREAKPOINT_SW        0
-#define GDB_BREAKPOINT_HW        1
-#define GDB_WATCHPOINT_WRITE     2
-#define GDB_WATCHPOINT_READ      3
-#define GDB_WATCHPOINT_ACCESS    4
-
 #ifndef CONFIG_USER_ONLY
 static const int xlat_gdb_type[] = {
     [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
@@ -1436,6 +1430,9 @@ static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
+
     switch (type) {
     case GDB_BREAKPOINT_SW:
     case GDB_BREAKPOINT_HW:
@@ -1467,6 +1464,9 @@ static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
+
     switch (type) {
     case GDB_BREAKPOINT_SW:
     case GDB_BREAKPOINT_HW:
@@ -1496,6 +1496,11 @@ static void gdb_breakpoint_remove_all(void)
 {
     CPUState *env;
 
+    if (kvm_enabled()) {
+        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
+        return;
+    }
+
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu_breakpoint_remove_all(env, BP_GDB);
 #ifndef CONFIG_USER_ONLY

gdbstub.c static int gdb_handle_packet(GDBState *s, const char *line_buf) · line 1546

Quoted 6 lines
gdbstub.c +2 -0
@@ -1536,6 +1541,8 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             s->c_cpu->eip = addr;
+            if (kvm_enabled())
+                kvm_put_registers(s->c_cpu);
 

I really dislike sprinkling kvm_enabled() all over the place (which is why this isn't here already).

Can you introduce a qemu function as a generic hook? Something like qemu_load_cpu_state(s->cpu)?

gdbstub.c static int gdb_handle_packet(GDBState *s, const char *line_buf)

Quoted 5 lines
gdbstub.c +4 -0
+
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
+    TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);

Please make this part of KVMState.

Regards,

Anthony Liguori

author
#2 lore raw
Quoted 116 lines · Anthony Liguori
Quoted 115 lines · Jan Kiszka

[ Recent kvm headers no longer require CONFIG_<ARCH>, so I dropped the related configure change. Moreover, this version is not capable of setting soft breakpoint in ROM memory. ]

This is a backport of the guest debugging support for the KVM accelerator that is now part of the KVM tree. It implements the reworked KVM kernel API for guest debugging (KVM_CAP_SET_GUEST_DEBUG) which is not yet part of any mainline kernel but will probably be 2.6.30 stuff. So far supported is x86, but PPC is expected to catch up soon.

Core features are:
- unlimited soft-breakpoints via code patching
- hardware-assisted x86 breakpoints and watchpoints

---

 exec.c            |   10 ++-
 gdbstub.c         |   29 ++++++--
 gdbstub.h         |    7 ++
 kvm-all.c         |  172
++++++++++++++++++++++++++++++++++++++++++++++++
 kvm.h             |   41 +++++++++++
 target-i386/kvm.c |  191
+++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 440 insertions(+), 10 deletions(-)
exec.c +0 -1
index 56e5e48..84c82ec 100644
@@ -1456,9 +1456,13 @@ void cpu_single_step(CPUState *env, int enabled)
 #if defined(TARGET_HAS_ICE)
     if (env->singlestep_enabled != enabled) {
         env->singlestep_enabled = enabled;
-        /* must flush all the translated code to avoid

inconsistancies */

exec.c +4 -2
-        /* XXX: only flush what is necessary */
-        tb_flush(env);
+        if (kvm_enabled())
+            kvm_update_guest_debug(env, 0);
+        else {
+            /* must flush all the translated code to avoid

inconsistancies */

exec.c +3 -0
+            /* XXX: only flush what is necessary */
+            tb_flush(env);
+        }
     }
 #endif
 }
gdbstub.c +4 -7
index b4b8292..0a91c7d 100644
@@ -38,6 +38,7 @@
 #define MAX_PACKET_LENGTH 4096
 
 #include "qemu_socket.h"
+#include "kvm.h"
 
 
 enum {
@@ -1416,13 +1417,6 @@ void gdb_register_coprocessor(CPUState * env,
     }
 }
 
-/* GDB breakpoint/watchpoint types */
-#define GDB_BREAKPOINT_SW        0
-#define GDB_BREAKPOINT_HW        1
-#define GDB_WATCHPOINT_WRITE     2
-#define GDB_WATCHPOINT_READ      3
-#define GDB_WATCHPOINT_ACCESS    4
-
 #ifndef CONFIG_USER_ONLY
 static const int xlat_gdb_type[] = {
     [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
@@ -1436,6 +1430,9 @@ static int gdb_breakpoint_insert(target_ulong
 addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr,
 len, type);
+
     switch (type) {

case GDB_BREAKPOINT_SW:
case GDB_BREAKPOINT_HW:

gdbstub.c +3 -0
@@ -1467,6 +1464,9 @@ static int gdb_breakpoint_remove(target_ulong
 addr, target_ulong len, int type)
     CPUState *env;
     int err = 0;
 
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr,
 len, type);
+
     switch (type) {

case GDB_BREAKPOINT_SW:
case GDB_BREAKPOINT_HW:

gdbstub.c +5 -0
@@ -1496,6 +1496,11 @@ static void gdb_breakpoint_remove_all(void)
 {
     CPUState *env;
 
+    if (kvm_enabled()) {
+        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
+        return;
+    }
+
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu_breakpoint_remove_all(env, BP_GDB);
 #ifndef CONFIG_USER_ONLY
@@ -1536,6 +1541,8 @@ static int gdb_handle_packet(GDBState *s, const
 char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
Quoted 8 lines

gdbstub.c static int gdb_handle_packet(GDBState *s, const char *line_buf)

Quoted 5 lines
gdbstub.c +2 -0
 #if defined(TARGET_I386)
             s->c_cpu->eip = addr;
+            if (kvm_enabled())
+                kvm_put_registers(s->c_cpu);
 

I really dislike sprinkling kvm_enabled() all over the place (which is why this isn't here already).

This is news.

Quoted 3 lines

Can you introduce a qemu function as a generic hook? Something like qemu_load_cpu_state(s->cpu)?

It's no big deal to do this, but

a) So far I thought the thing about "if (kvm_enabled()) code" was letting the compiler remove any traces of it in case kvm is not built-in (or unsupported on some arch). Or is a static-inline qemu_load_cpu_state acceptable?

b) I don't want to start reinventing Glauber's accel abstractions. Wouldn't it make sense to do this in one larger step instead of permanently changing APIs?

Quoted 8 lines

gdbstub.c static int gdb_handle_packet(GDBState *s, const

Quoted 6 lines
gdbstub.c +4 -0
+
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+struct kvm_sw_breakpoint_head kvm_sw_breakpoints =
+    TAILQ_HEAD_INITIALIZER(kvm_sw_breakpoints);

Please make this part of KVMState.

OK, will do.

Jan