Skip to contentSkip to conversation

Rework guest debug interface / x86 debug register support -v2

PATCH In review Jan Kiszka 5 participants 21 messages May 26, 2008 – Jun 12, 2008

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems
lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover Rework guest debug interface / x86 debug register support -v2

Hi,

here comes the full series of patches to fix and enhance debugging support of KVM. After completing hardware breakpoint support and reorganizing the patches, I felt like I should post them all in a single series, because my queue became quite long and potentially confusing in the meantime.

The major changes or this revision are full hardware-assisted guest debugging and a lot of refactoring on the userspace side. The latter was motivated by an increasing inconsistency regarding which arch-specific part should be handled in libkvm and which by qemu. I decided to push everything into qemu, keeping the lib small (and easier to overcome one day :->).

When I was trying to test the hardware breakpoint/watchpoint support with gdb, the first result with quite disappointing: current gdb is unable to handle such breakpoint remotely. There is hope, latest cvs has it fixed - but suffers from other regressions which make it unusable for kernel debugging. In the meantime, you can still write your own "maintenance packet Z2,..." requests. :p

To give an overview of the series:
Patch 1..3 - Critical debugger fixes for KVM (should be merge in any
case)
Patch 4..5 - QEMU refactoring, required for following KVM patches, but
also of generic use for QEMU (rebase and feature enhancements are required to get them in shape for upstream)
Patch 6..8 - New guest debugging interface and SMP support Patch 9..11 - Kernel-side changes for new guest debugging interface and
proper x86 debug register virtualization

Jan

PS: If anyone wants to be dropped from CC, just let me know.

author
#1 lore raw

PATCH 1/11 QEMU/KVM: Fix deadlocks in monitor and debugger

Some monitor commands as well as the vm_stop() issued by the gdbstub on external interruption so far deadlock on vcpu locks in the kernel. Patch below resolves the issue by temporarily or permanently stopping all vcpu threads before issuing the related KVM IOCTLs. It enables, e.g., to break into guest code spinning in the vcpu and to use things like "info cpus" in the monitor.

---
 qemu/qemu-kvm.c |   41 +++++++++++++++++++++++++++--------------
 qemu/vl.c       |    2 +-
 2 files changed, 28 insertions(+), 15 deletions(-)
qemu/qemu-kvm.c +27 -14
Index: b/qemu/qemu-kvm.c
===================================================================
@@ -137,18 +137,6 @@ static int pre_kvm_run(void *opaque, int
     return 0;
 }
 
-void kvm_load_registers(CPUState *env)
-{
-    if (kvm_enabled())
-	kvm_arch_load_regs(env);
-}
-
-void kvm_save_registers(CPUState *env)
-{
-    if (kvm_enabled())
-	kvm_arch_save_regs(env);
-}
-
 int kvm_cpu_exec(CPUState *env)
 {
     int r;
@@ -252,6 +240,26 @@ static void kvm_vm_state_change_handler(
 	pause_all_threads();
 }
 
+void kvm_load_registers(CPUState *env)
+{
+    assert(!vm_running);
+
+    if (kvm_enabled())
+	kvm_arch_load_regs(env);
+}
+
+void kvm_save_registers(CPUState *env)
+{
+    if (!kvm_enabled())
+        return;
+
+    if (vm_running)
+        pause_all_threads();
+    kvm_arch_save_regs(env);
+    if (vm_running)
+        resume_all_threads();
+}
+
 static void update_regs_for_sipi(CPUState *env)
 {
     kvm_arch_update_regs_for_sipi(env);
@@ -740,7 +748,7 @@ int kvm_qemu_init_env(CPUState *cenv)
 int kvm_update_debugger(CPUState *env)
 {
     struct kvm_debug_guest dbg;
-    int i;
+    int i, r;
 
     memset(dbg.breakpoints, 0, sizeof(dbg.breakpoints));
 
@@ -753,7 +761,12 @@ int kvm_update_debugger(CPUState *env)
 	}
 	dbg.singlestep = env->singlestep_enabled;
     }
-    return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
+    if (vm_running)
+        pause_all_threads();
+    r = kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
+    if (vm_running)
+        resume_all_threads();
+    return r;
 }
 
 
qemu/vl.c +1 -1
Index: b/qemu/vl.c
===================================================================
@@ -7312,12 +7312,12 @@ void vm_stop(int reason)
 {
     if (vm_running) {
         cpu_disable_ticks();
-        vm_running = 0;
         if (reason != 0) {
             if (vm_stop_cb) {
                 vm_stop_cb(vm_stop_opaque, reason);
             }
         }
+        vm_running = 0;
         vm_state_notify(0);
     }
 }
#2 lore raw

Re: [PATCH 1/11] QEMU/KVM: Fix deadlocks in monitor and debugger

Quoted 7 lines · Jan Kiszka

Some monitor commands as well as the vm_stop() issued by the gdbstub on external interruption so far deadlock on vcpu locks in the kernel. Patch below resolves the issue by temporarily or permanently stopping all vcpu threads before issuing the related KVM IOCTLs. It enables, e.g., to break into guest code spinning in the vcpu and to use things like "info cpus" in the monitor.

I implemented the alternative on_vcpu() approach for this (similar to smp_call_function_single in the kernel) which solves the livelock without resorting to stopping the VM.

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

Re: [PATCH 1/11] QEMU/KVM: Fix deadlocks in monitor and debugger

Quoted 9 lines · Avi Kivity
Quoted 7 lines · Jan Kiszka

Some monitor commands as well as the vm_stop() issued by the gdbstub on external interruption so far deadlock on vcpu locks in the kernel. Patch below resolves the issue by temporarily or permanently stopping all vcpu threads before issuing the related KVM IOCTLs. It enables, e.g., to break into guest code spinning in the vcpu and to use things like "info cpus" in the monitor.

Quoted 3 lines

I implemented the alternative on_vcpu() approach for this (similar to smp_call_function_single in the kernel) which solves the livelock without resorting to stopping the VM.

I assume this pattern should then be applied to kvm_guest_debug (and later on kvm_set_guest_debug) as well? You missed to fix that bug.

Here is a quick patch to complete the work, will post rebased versions of my remaining patches later today:
-----------

Use on_vcpu to call into kvm_guest_debug. Fix on_vpuc usage in kvm_load_registers.

---
 qemu/qemu-kvm.c |   32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
qemu/qemu-kvm.c +23 -9
Index: b/qemu/qemu-kvm.c
===================================================================
@@ -187,7 +187,7 @@ static void kvm_do_load_registers(void *
 void kvm_load_registers(CPUState *env)
 {
     if (kvm_enabled())
-        on_vcpu(env->cpu_index, kvm_do_load_registers, env);
+        on_vcpu(env, kvm_do_load_registers, env);
 }
 
 static void kvm_do_save_registers(void *_env)
@@ -816,23 +816,37 @@ int kvm_qemu_init_env(CPUState *cenv)
     return kvm_arch_qemu_init_env(cenv);
 }
 
+struct kvm_guest_debug_data {
+    struct kvm_debug_guest dbg;
+    int err;
+};
+
+void kvm_invoke_guest_debug(void *data)
+{
+    struct kvm_guest_debug_data *dbg_data = data;
+
+    dbg_data->err = kvm_guest_debug(kvm_context, cpu_single_env->cpu_index,
+                                    &dbg_data->dbg);
+}
+
 int kvm_update_debugger(CPUState *env)
 {
-    struct kvm_debug_guest dbg;
+    struct kvm_guest_debug_data data;
     int i;
 
-    memset(dbg.breakpoints, 0, sizeof(dbg.breakpoints));
+    memset(data.dbg.breakpoints, 0, sizeof(data.dbg.breakpoints));
 
-    dbg.enabled = 0;
+    data.dbg.enabled = 0;
     if (env->nb_breakpoints || env->singlestep_enabled) {
-	dbg.enabled = 1;
+	data.dbg.enabled = 1;
 	for (i = 0; i < 4 && i < env->nb_breakpoints; ++i) {
-	    dbg.breakpoints[i].enabled = 1;
-	    dbg.breakpoints[i].address = env->breakpoints[i];
+	    data.dbg.breakpoints[i].enabled = 1;
+	    data.dbg.breakpoints[i].address = env->breakpoints[i];
 	}
-	dbg.singlestep = env->singlestep_enabled;
+	data.dbg.singlestep = env->singlestep_enabled;
     }
-    return kvm_guest_debug(kvm_context, env->cpu_index, &dbg);
+    on_vcpu(env, kvm_invoke_guest_debug, &data);
+    return data.err;
 }
 
 
#4 lore raw

Re: [PATCH 1/11] QEMU/KVM: Fix deadlocks in monitor and debugger

Quoted 12 lines · Jan Kiszka
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Jan Kiszka

Some monitor commands as well as the vm_stop() issued by the gdbstub on external interruption so far deadlock on vcpu locks in the kernel. Patch below resolves the issue by temporarily or permanently stopping all vcpu threads before issuing the related KVM IOCTLs. It enables, e.g., to break into guest code spinning in the vcpu and to use things like "info cpus" in the monitor.

Quoted 8 lines
Quoted 4 lines

I implemented the alternative on_vcpu() approach for this (similar to smp_call_function_single in the kernel) which solves the livelock without resorting to stopping the VM.

I assume this pattern should then be applied to kvm_guest_debug (and later on kvm_set_guest_debug) as well? You missed to fix that bug.

Yeah, I just patched the register stuff so I could test on_vcpu().

Quoted 5 lines

Here is a quick patch to complete the work, will post rebased versions of my remaining patches later today:
-----------

Use on_vcpu to call into kvm_guest_debug.

Applied that, thanks.

Quoted 3 lines

Fix on_vpuc usage in
kvm_load_registers.

How embarrasing. I had that queued already, though.

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

PATCH 2/11 QEMU/KVM: Cleanup and improve kvm_load/save_registers usage

In reply to Jan Kiszka

Remove redundant checkes for kvm_enabled() on register updates between userspace and kvm kernel driver. Ensure register update across all CPUs on "info cpus" monitor command.

---
 qemu/gdbstub.c |   12 ++++--------
 qemu/monitor.c |    4 ++--
 2 files changed, 6 insertions(+), 10 deletions(-)
qemu/monitor.c +2 -2
Index: b/qemu/monitor.c
===================================================================
@@ -286,8 +286,7 @@ static CPUState *mon_get_cpu(void)
         mon_set_cpu(0);
     }
 
-    if (kvm_enabled())
-	kvm_save_registers(mon_cpu);
+    kvm_save_registers(mon_cpu);
 
     return mon_cpu;
 }
@@ -315,6 +314,7 @@ static void do_info_cpus(void)
     mon_get_cpu();
 
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
+        kvm_save_registers(env);
         term_printf("%c CPU #%d:",
                     (env == mon_cpu) ? '*' : ' ',
                     env->cpu_index);
qemu/gdbstub.c +4 -8
Index: b/qemu/gdbstub.c
===================================================================
@@ -904,8 +904,7 @@ static int gdb_handle_packet(GDBState *s
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-	    if (kvm_enabled())
-		kvm_load_registers(env);
+            kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -928,8 +927,7 @@ static int gdb_handle_packet(GDBState *s
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-	    if (kvm_enabled())
-		kvm_load_registers(env);
+            kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -973,8 +971,7 @@ static int gdb_handle_packet(GDBState *s
         }
         break;
     case 'g':
-	if (kvm_enabled())
-	    kvm_save_registers(env);
+        kvm_save_registers(env);
         reg_size = cpu_gdb_read_registers(env, mem_buf);
         memtohex(buf, mem_buf, reg_size);
         put_packet(s, buf);
@@ -984,8 +981,7 @@ static int gdb_handle_packet(GDBState *s
         len = strlen(p) / 2;
         hextomem((uint8_t *)registers, p, len);
         cpu_gdb_write_registers(env, mem_buf, len);
-	if (kvm_enabled())
-	    kvm_load_registers(env);
+        kvm_load_registers(env);
         put_packet(s, "OK");
         break;
     case 'm':
#6 lore raw

Re: [PATCH 2/11] QEMU/KVM: Cleanup and improve kvm_load/save_registers usage

Quoted 4 lines · Jan Kiszka

Remove redundant checkes for kvm_enabled() on register updates between userspace and kvm kernel driver. Ensure register update across all CPUs on "info cpus" monitor command.

This breaks the build when KVM is disabled. The explicit guard is needed to avoid having #ifdefs. Please revert.

Regards,

Anthony Liguori

Quoted 79 lines
---
 qemu/gdbstub.c |   12 ++++--------
 qemu/monitor.c |    4 ++--
 2 files changed, 6 insertions(+), 10 deletions(-)

Index: b/qemu/monitor.c
===================================================================

qemu/monitor.c +2 -2
@@ -286,8 +286,7 @@ static CPUState *mon_get_cpu(void)
         mon_set_cpu(0);
     }
 
-    if (kvm_enabled())
-	kvm_save_registers(mon_cpu);
+    kvm_save_registers(mon_cpu);
 
     return mon_cpu;
 }
@@ -315,6 +314,7 @@ static void do_info_cpus(void)
     mon_get_cpu();
 
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
+        kvm_save_registers(env);
         term_printf("%c CPU #%d:",
                     (env == mon_cpu) ? '*' : ' ',
                     env->cpu_index);

Index: b/qemu/gdbstub.c
===================================================================

qemu/gdbstub.c +4 -9
@@ -904,8 +904,7 @@ static int gdb_handle_packet(GDBState *s
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-	    if (kvm_enabled())
-		kvm_load_registers(env);
+            kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -928,8 +927,7 @@ static int gdb_handle_packet(GDBState *s
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-	    if (kvm_enabled())
-		kvm_load_registers(env);
+            kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -973,8 +971,7 @@ static int gdb_handle_packet(GDBState *s
         }
         break;
     case 'g':
-	if (kvm_enabled())
-	    kvm_save_registers(env);
+        kvm_save_registers(env);
         reg_size = cpu_gdb_read_registers(env, mem_buf);
         memtohex(buf, mem_buf, reg_size);
         put_packet(s, buf);
@@ -984,8 +981,7 @@ static int gdb_handle_packet(GDBState *s
         len = strlen(p) / 2;
         hextomem((uint8_t *)registers, p, len);
         cpu_gdb_write_registers(env, mem_buf, len);
-	if (kvm_enabled())
-	    kvm_load_registers(env);
+        kvm_load_registers(env);
         put_packet(s, "OK");
         break;
     case 'm':
 
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

Re: [PATCH 2/11] QEMU/KVM: Cleanup and improve kvm_load/save_registers usage

Quoted 8 lines · Anthony Liguori
Quoted 4 lines · Jan Kiszka

Remove redundant checkes for kvm_enabled() on register updates between userspace and kvm kernel driver. Ensure register update across all CPUs on "info cpus" monitor command.

This breaks the build when KVM is disabled. The explicit guard is needed to avoid having #ifdefs. Please revert.

You mean a link time error? Well in that case we're relying on gcc optimizing away the call. It will break with -O0.

The correct fix is to define stubs for the case kvm is configured out (or just use ifdefs). But perhaps Glauber's accelerator framework will take care of all this in a generic fashion.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
author
#8 lore raw

PATCH 3/11 QEMU/KVM: Proper vm_stop on debug events

In reply to Jan Kiszka

[ Changes to last revision: Reordered kvm_debug_stop_requested and
vm_stop to avoid race. ]

When a vcpu exits after hitting a debug exception, we have to invoke vm_stop(EXCP_DEBUG). But this has to take place over the io-thread.

This patch introduces kvm_debug_stop_requested to signal this event, and it takes care that the interrupted vcpu itself goes immediately into stop state.

---
 qemu/qemu-kvm.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
qemu/qemu-kvm.c +8 -1
Index: b/qemu/qemu-kvm.c
===================================================================
@@ -58,6 +58,8 @@ pthread_t io_thread;
 static int io_thread_fd = -1;
 static int io_thread_sigfd = -1;
 
+static int kvm_debug_stop_requested;
+
 static inline unsigned long kvm_get_thread_id(void)
 {
     return syscall(SYS_gettid);
@@ -517,6 +519,10 @@ int kvm_main_loop(void)
             qemu_system_powerdown();
         else if (qemu_reset_requested())
 	    qemu_kvm_system_reset();
+	else if (kvm_debug_stop_requested) {
+	    vm_stop(EXCP_DEBUG);
+	    kvm_debug_stop_requested = 0;
+	}
     }
 
     pause_all_threads();
@@ -529,7 +535,8 @@ static int kvm_debug(void *opaque, int v
 {
     CPUState *env = cpu_single_env;
 
-    env->exception_index = EXCP_DEBUG;
+    kvm_debug_stop_requested = 1;
+    vcpu_info[vcpu].stopped = 1;
     return 1;
 }
 
author
#9 lore raw

PATCH 4/11 QEMU: Enhance cpu_break/watchpoint API and gdbstub integration

In reply to Jan Kiszka

This patch prepares the QEMU cpu_watchpoint/breakpoint API to allow us hooking in with KVM and doing guest debugging differently (maybe QEMUAccel should provide appropriate callbacks for this, too). But it also prepares to extend QEMU's debugging features one day, specifically /wrt different watchpoint types. So far these extensions are only used by KVM.

---
 qemu/cpu-all.h  |   14 ++++++++---
 qemu/cpu-defs.h |    2 +
 qemu/exec.c     |   30 ++++++++++++++++---------
 qemu/gdbstub.c  |   66 ++++++++++++++++++++++++++++++++++++--------------------
 4 files changed, 75 insertions(+), 37 deletions(-)
qemu/cpu-all.h +10 -4
Index: b/qemu/cpu-all.h
===================================================================
@@ -758,10 +758,16 @@ extern int code_copy_enabled;
 void cpu_interrupt(CPUState *s, int mask);
 void cpu_reset_interrupt(CPUState *env, int mask);
 
-int cpu_watchpoint_insert(CPUState *env, target_ulong addr);
-int cpu_watchpoint_remove(CPUState *env, target_ulong addr);
-int cpu_breakpoint_insert(CPUState *env, target_ulong pc);
-int cpu_breakpoint_remove(CPUState *env, target_ulong pc);
+#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
+
+int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len, int type);
+int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len, int type);
+int cpu_breakpoint_insert(CPUState *env, target_ulong pc, target_ulong len, int type);
+int cpu_breakpoint_remove(CPUState *env, target_ulong pc, target_ulong len, int type);
 void cpu_single_step(CPUState *env, int enabled);
 void cpu_reset(CPUState *s);
 
qemu/exec.c +20 -10
Index: b/qemu/exec.c
===================================================================
@@ -1104,16 +1104,20 @@ static void breakpoint_invalidate(CPUSta
 #endif
 
 /* Add a watchpoint.  */
-int  cpu_watchpoint_insert(CPUState *env, target_ulong addr)
+int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
+                          int type)
 {
     int i;
 
+    if (type != GDB_WATCHPOINT_WRITE)
+        return -ENOSYS;
+
     for (i = 0; i < env->nb_watchpoints; i++) {
         if (addr == env->watchpoint[i].vaddr)
             return 0;
     }
     if (env->nb_watchpoints >= MAX_WATCHPOINTS)
-        return -1;
+        return -ENOBUFS;
 
     i = env->nb_watchpoints++;
     env->watchpoint[i].vaddr = addr;
@@ -1126,10 +1130,14 @@ int  cpu_watchpoint_insert(CPUState *env
 }
 
 /* Remove a watchpoint.  */
-int cpu_watchpoint_remove(CPUState *env, target_ulong addr)
+int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
+                          int type)
 {
     int i;
 
+    if (type != GDB_WATCHPOINT_WRITE)
+        return -ENOSYS;
+
     for (i = 0; i < env->nb_watchpoints; i++) {
         if (addr == env->watchpoint[i].vaddr) {
             env->nb_watchpoints--;
@@ -1138,12 +1146,13 @@ int cpu_watchpoint_remove(CPUState *env,
             return 0;
         }
     }
-    return -1;
+    return -ENOENT;
 }
 
 /* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
    breakpoint is reached */
-int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
+int cpu_breakpoint_insert(CPUState *env, target_ulong pc, target_ulong len,
+                          int type)
 {
 #if defined(TARGET_HAS_ICE)
     int i;
@@ -1154,7 +1163,7 @@ int cpu_breakpoint_insert(CPUState *env,
     }
 
     if (env->nb_breakpoints >= MAX_BREAKPOINTS)
-        return -1;
+        return -ENOBUFS;
     env->breakpoints[env->nb_breakpoints++] = pc;
 
     if (kvm_enabled())
@@ -1163,12 +1172,13 @@ int cpu_breakpoint_insert(CPUState *env,
     breakpoint_invalidate(env, pc);
     return 0;
 #else
-    return -1;
+    return -ENOSYS;
 #endif
 }
 
 /* remove a breakpoint */
-int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
+int cpu_breakpoint_remove(CPUState *env, target_ulong pc, target_ulong len,
+                          int type)
 {
 #if defined(TARGET_HAS_ICE)
     int i;
@@ -1176,7 +1186,7 @@ int cpu_breakpoint_remove(CPUState *env,
         if (env->breakpoints[i] == pc)
             goto found;
     }
-    return -1;
+    return -ENOENT;
  found:
     env->nb_breakpoints--;
     if (i < env->nb_breakpoints)
@@ -1188,7 +1198,7 @@ int cpu_breakpoint_remove(CPUState *env,
     breakpoint_invalidate(env, pc);
     return 0;
 #else
-    return -1;
+    return -ENOSYS;
 #endif
 }
 
qemu/gdbstub.c +43 -23

102 lines not on this page · raw message

qemu/cpu-defs.h +2 -0
Index: b/qemu/cpu-defs.h
===================================================================
@@ -155,6 +155,8 @@ typedef struct CPUTLBEntry {
     struct {                                                            \
         target_ulong vaddr;                                             \
         target_phys_addr_t addend;                                      \
+        target_ulong len;                                               \
+        int type;                                                       \
     } watchpoint[MAX_WATCHPOINTS];                                      \
     int nb_watchpoints;                                                 \
     int watchpoint_hit;                                                 \
author
#10 lore raw

PATCH 5/11 QEMU: Improve SMP debugging support

In reply to Jan Kiszka

This patch enhances QEMU's built-in debugger for SMP guest debugging. It allows to set the debugger focus explicitly via the monitor command "cpu", and it automatically switches the focus on breakpoint hit to the reporting CPU.

Furthermore, the patch propagates breakpoint and watchpoint insertions or removals to all CPUs, not just the current one as it was the case so far.

---
 qemu/cpu-defs.h |    3 +++
 qemu/exec.c     |   30 +++++++++++++++++++++---------
 qemu/gdbstub.c  |   25 ++++++++++++-------------
 qemu/monitor.c  |   19 ++++++++++++-------
 qemu/monitor.h  |    7 +++++++
 qemu/vl.c       |    2 ++
 6 files changed, 57 insertions(+), 29 deletions(-)
qemu/gdbstub.c +12 -13
Index: b/qemu/gdbstub.c
===================================================================
@@ -35,6 +35,7 @@
 #include "gdbstub.h"
 #include "qemu-kvm.h"
 #endif
+#include "monitor.h"
 
 #include "qemu_socket.h"
 #ifdef _WIN32
@@ -59,7 +60,6 @@ enum RSState {
     RS_SYSCALL,
 };
 typedef struct GDBState {
-    CPUState *env; /* current CPU */
     enum RSState state; /* parsing state */
     char line_buf[4096];
     int line_buf_index;
@@ -962,7 +962,7 @@ static int gdb_handle_packet(GDBState *s
                 p++;
             type = *p;
             if (gdb_current_syscall_cb)
-                gdb_current_syscall_cb(s->env, ret, err);
+                gdb_current_syscall_cb(mon_get_cpu(), ret, err);
             if (type == 'C') {
                 put_packet(s, "T02");
             } else {
@@ -1092,6 +1092,7 @@ extern void tb_flush(CPUState *env);
 static void gdb_vm_stopped(void *opaque, int reason)
 {
     GDBState *s = opaque;
+    CPUState *env = mon_get_cpu();
     char buf[256];
     char *type;
     int ret;
@@ -1100,11 +1101,11 @@ static void gdb_vm_stopped(void *opaque,
         return;
 
     /* disable single step if it was enable */
-    cpu_single_step(s->env, 0);
+    cpu_single_step(env, 0);
 
     if (reason == EXCP_DEBUG) {
-        if (s->env->watchpoint_hit) {
-            switch (s->env->watchpoint[s->env->watchpoint_hit - 1].type) {
+        if (env->watchpoint_hit) {
+            switch (env->watchpoint[env->watchpoint_hit - 1].type) {
             case GDB_WATCHPOINT_READ:
                 type = "r";
                 break;
@@ -1117,12 +1118,12 @@ static void gdb_vm_stopped(void *opaque,
             }
             snprintf(buf, sizeof(buf), "T%02x%swatch:" TARGET_FMT_lx ";",
                      SIGTRAP, type,
-                     s->env->watchpoint[s->env->watchpoint_hit - 1].vaddr);
+                     env->watchpoint[env->watchpoint_hit - 1].vaddr);
             put_packet(s, buf);
-            s->env->watchpoint_hit = 0;
+            env->watchpoint_hit = 0;
             return;
         }
-	tb_flush(s->env);
+	tb_flush(env);
         ret = SIGTRAP;
     } else if (reason == EXCP_INTERRUPT) {
         ret = SIGINT;
@@ -1192,15 +1193,15 @@ void gdb_do_syscall(gdb_syscall_complete
     va_end(va);
     put_packet(s, buf);
 #ifdef CONFIG_USER_ONLY
-    gdb_handlesig(s->env, 0);
+    gdb_handlesig(mon_get_cpu(), 0);
 #else
-    cpu_interrupt(s->env, CPU_INTERRUPT_EXIT);
+    cpu_interrupt(mon_get_cpu(), CPU_INTERRUPT_EXIT);
 #endif
 }
 
 static void gdb_read_byte(GDBState *s, int ch)
 {
-    CPUState *env = s->env;
+    CPUState *env = mon_get_cpu();
     int i, csum;
     uint8_t reply;
 
@@ -1360,7 +1361,6 @@ static void gdb_accept(void *opaque)
 
     s = &gdbserver_state;
     memset (s, 0, sizeof (GDBState));
-    s->env = first_cpu; /* XXX: allow to change CPU */
     s->fd = fd;
 
     gdb_syscall_state = s;
@@ -1463,7 +1463,6 @@ int gdbserver_start(const char *port)
     if (!s) {
         return -1;
     }
-    s->env = first_cpu; /* XXX: allow to change CPU */
     s->chr = chr;
     qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
                           gdb_chr_event, s);
qemu/monitor.c +12 -7
Index: b/qemu/monitor.c
===================================================================
@@ -266,8 +266,7 @@ static void do_info_blockstats(void)
     bdrv_info_stats();
 }
 
-/* get the current CPU defined by the user */
-static int mon_set_cpu(int cpu_index)
+static int mon_set_cpu_index(int cpu_index)
 {
     CPUState *env;
 
@@ -280,10 +279,16 @@ static int mon_set_cpu(int cpu_index)
     return -1;
 }
 
-static CPUState *mon_get_cpu(void)
+void mon_set_cpu(CPUState *env)
+{
+    mon_cpu = env;
+}
+
+/* get the current CPU defined by the user or by a breakpoint hit */
+CPUState *mon_get_cpu(void)
 {
     if (!mon_cpu) {
-        mon_set_cpu(0);
+        mon_set_cpu(first_cpu);
     }
 
     kvm_save_registers(mon_cpu);
@@ -310,8 +315,8 @@ static void do_info_cpus(void)
 {
     CPUState *env;
 
-    /* just to set the default cpu if not already done */
-    mon_get_cpu();
+    if (!mon_cpu)
+        mon_set_cpu(first_cpu);
 
     for(env = first_cpu; env != NULL; env = env->next_cpu) {
         kvm_save_registers(env);
@@ -342,7 +347,7 @@ static void do_info_cpus(void)
 
 static void do_cpu_set(int index)
 {
-    if (mon_set_cpu(index) < 0)
+    if (mon_set_cpu_index(index) < 0)
         term_printf("Invalid CPU index\n");
 }
 
qemu/monitor.h new file +7 -0
Index: b/qemu/monitor.h
===================================================================
@@ -0,0 +1,7 @@
+#ifndef QEMU_MONITOR_H
+#define QEMU_MONITOR_H
+
+void mon_set_cpu(CPUState *env);
+CPUState *mon_get_cpu(void);
+
+#endif /* QEMU_MONITOR_H */
qemu/vl.c +2 -0
Index: b/qemu/vl.c
===================================================================
@@ -33,6 +33,7 @@
 #include "console.h"
 #include "sysemu.h"
 #include "gdbstub.h"
+#include "monitor.h"
 #include "qemu-timer.h"
 #include "qemu-char.h"
 #include "block.h"
@@ -7644,6 +7645,7 @@ static int main_loop(void)
                 ret = EXCP_INTERRUPT;
             }
             if (unlikely(ret == EXCP_DEBUG)) {
+                mon_set_cpu(cur_cpu);
                 vm_stop(EXCP_DEBUG);
             }
             /* If all cpus are halted then wait until the next IRQ */
qemu/cpu-defs.h +3 -0
Index: b/qemu/cpu-defs.h
===================================================================
@@ -170,3 +170,6 @@ typedef struct CPUTLBEntry {
     const char *cpu_model_str;
 
 #endif
+
+#define foreach_cpu(env) \
+    for(env = first_cpu; env != NULL; env = env->next_cpu)
qemu/exec.c +21 -9
Index: b/qemu/exec.c
===================================================================
@@ -1107,6 +1107,7 @@ static void breakpoint_invalidate(CPUSta
 int cpu_watchpoint_insert(CPUState *env, target_ulong addr, target_ulong len,
                           int type)
 {
+    CPUState *env_iter;
     int i;
 
     if (type != GDB_WATCHPOINT_WRITE)
@@ -1119,8 +1120,10 @@ int cpu_watchpoint_insert(CPUState *env,
     if (env->nb_watchpoints >= MAX_WATCHPOINTS)
         return -ENOBUFS;
 
-    i = env->nb_watchpoints++;
-    env->watchpoint[i].vaddr = addr;
+    foreach_cpu(env_iter) {
+        i = env_iter->nb_watchpoints++;
+        env_iter->watchpoint[i].vaddr = addr;
+    }
     tlb_flush_page(env, addr);
     /* FIXME: This flush is needed because of the hack to make memory ops
        terminate the TB.  It can be removed once the proper IO trap and
@@ -1133,6 +1136,7 @@ int cpu_watchpoint_insert(CPUState *env,
 int cpu_watchpoint_remove(CPUState *env, target_ulong addr, target_ulong len,
                           int type)
 {
+    CPUState *env_iter;
     int i;
 
     if (type != GDB_WATCHPOINT_WRITE)
@@ -1140,8 +1144,11 @@ int cpu_watchpoint_remove(CPUState *env,
 
     for (i = 0; i < env->nb_watchpoints; i++) {
         if (addr == env->watchpoint[i].vaddr) {
-            env->nb_watchpoints--;
-            env->watchpoint[i] = env->watchpoint[env->nb_watchpoints];
+            foreach_cpu(env_iter) {
+                env_iter->nb_watchpoints--;
+                env_iter->watchpoint[i] =
+                    env_iter->watchpoint[env_iter->nb_watchpoints];
+            }
             tlb_flush_page(env, addr);
             return 0;
         }
@@ -1155,6 +1162,7 @@ int cpu_breakpoint_insert(CPUState *env,
                           int type)
 {
 #if defined(TARGET_HAS_ICE)
+    CPUState *env_iter;
     int i;
 
     for(i = 0; i < env->nb_breakpoints; i++) {
@@ -1164,7 +1172,8 @@ int cpu_breakpoint_insert(CPUState *env,
 
     if (env->nb_breakpoints >= MAX_BREAKPOINTS)
         return -ENOBUFS;
-    env->breakpoints[env->nb_breakpoints++] = pc;
+    foreach_cpu(env_iter)
+        env_iter->breakpoints[env_iter->nb_breakpoints++] = pc;
 
     if (kvm_enabled())
 	kvm_update_debugger(env);
@@ -1181,6 +1190,7 @@ int cpu_breakpoint_remove(CPUState *env,
                           int type)
 {
 #if defined(TARGET_HAS_ICE)
+    CPUState *env_iter;
     int i;
     for(i = 0; i < env->nb_breakpoints; i++) {
         if (env->breakpoints[i] == pc)
@@ -1188,13 +1198,15 @@ int cpu_breakpoint_remove(CPUState *env,
     }
     return -ENOENT;
  found:
-    env->nb_breakpoints--;
-    if (i < env->nb_breakpoints)
-      env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
+    foreach_cpu(env_iter) {
+        env_iter->nb_breakpoints--;
+        env_iter->breakpoints[i] =
+            env_iter->breakpoints[env_iter->nb_breakpoints];
+    }
 
     if (kvm_enabled())
 	kvm_update_debugger(env);
-    
+
     breakpoint_invalidate(env, pc);
     return 0;
 #else
author
#11 lore raw

PATCH 6/11 QEMU/KVM: Introduce single vcpu pause/resume

In reply to Jan Kiszka

Small helpers that allow to pause and resume only a single vcpu thread.

---
 qemu/qemu-kvm.c |   29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
qemu/qemu-kvm.c +22 -7
Index: b/qemu/qemu-kvm.c
===================================================================
@@ -207,6 +207,17 @@ static int all_threads_paused(void)
     return 1;
 }
 
+static void pause_vcpu_thread(int index)
+{
+    assert(!cpu_single_env || cpu_single_env->cpu_index != index);
+
+    vcpu_info[index].stop = 1;
+    pthread_kill(vcpu_info[index].thread, SIG_IPI);
+
+    while (!vcpu_info[index].stopped)
+	qemu_cond_wait(&qemu_pause_cond);
+}
+
 static void pause_all_threads(void)
 {
     int i;
@@ -221,17 +232,21 @@ static void pause_all_threads(void)
 	qemu_cond_wait(&qemu_pause_cond);
 }
 
+static void resume_vcpu_thread(int index)
+{
+    assert(!cpu_single_env || cpu_single_env->cpu_index != index);
+
+    vcpu_info[index].stop = 0;
+    vcpu_info[index].stopped = 0;
+    pthread_kill(vcpu_info[index].thread, SIG_IPI);
+}
+
 static void resume_all_threads(void)
 {
     int i;
 
-    assert(!cpu_single_env);
-
-    for (i = 0; i < smp_cpus; ++i) {
-	vcpu_info[i].stop = 0;
-	vcpu_info[i].stopped = 0;
-	pthread_kill(vcpu_info[i].thread, SIG_IPI);
-    }
+    for (i = 0; i < smp_cpus; ++i)
+	resume_vcpu_thread(i);
 }
 
 static void kvm_vm_state_change_handler(void *context, int running)
author
#12 lore raw

PATCH 7/11 QEMU/KVM: New guest debugging interface

In reply to Jan Kiszka

This patch switches both libkvm as well as the qemu pieces over to the new guest debug interface. It comes with full support for software-based breakpoints (via guest code modification), hardware-assisted breakpoints and watchpoints (x86-only so far).

Breakpoint management is done inside qemu-kvm, transparent to gdbstub, and also avoiding that the gdb frontend takes over. This allows for running debuggers inside the guest while guest debugging it active, because the host can cleanly tell apart host- and guest-originated breakpoint events.

Yet improvable are x86 corner cases when using single-step ("forgotten" debug flags on the guest's stack). And, of courese, the yet empty non-x86 helper functions have to be populated.

---
 libkvm/libkvm.c         |   14 +++-
 libkvm/libkvm.h         |    9 ++
 qemu/exec.c             |   31 +++++---
 qemu/qemu-kvm-ia64.c    |   33 +++++++++
 qemu/qemu-kvm-powerpc.c |   33 +++++++++
 qemu/qemu-kvm-x86.c     |  167 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu/qemu-kvm.c         |  157 ++++++++++++++++++++++++++++++++++++++-------
 qemu/qemu-kvm.h         |   33 ++++++++-
 user/main.c             |    7 +-
 9 files changed, 442 insertions(+), 42 deletions(-)
libkvm/libkvm.c +11 -3
Index: b/libkvm/libkvm.c
===================================================================
@@ -740,7 +740,13 @@ static int handle_io(kvm_context_t kvm, 
 
 int handle_debug(kvm_context_t kvm, int vcpu)
 {
-	return kvm->callbacks->debug(kvm->opaque, vcpu);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+	struct kvm_run *run = kvm->run[vcpu];
+
+	return kvm->callbacks->debug(kvm->opaque, vcpu, &run->debug.arch);
+#else
+	return 0;
+#endif
 }
 
 int kvm_get_regs(kvm_context_t kvm, int vcpu, struct kvm_regs *regs)
@@ -945,10 +951,12 @@ int kvm_inject_irq(kvm_context_t kvm, in
 	return ioctl(kvm->vcpu_fd[vcpu], KVM_INTERRUPT, &intr);
 }
 
-int kvm_guest_debug(kvm_context_t kvm, int vcpu, struct kvm_debug_guest *dbg)
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+int kvm_set_guest_debug(kvm_context_t kvm, int vcpu, struct kvm_guest_debug *dbg)
 {
-	return ioctl(kvm->vcpu_fd[vcpu], KVM_DEBUG_GUEST, dbg);
+	return ioctl(kvm->vcpu_fd[vcpu], KVM_SET_GUEST_DEBUG, dbg);
 }
+#endif
 
 int kvm_set_signal_mask(kvm_context_t kvm, int vcpu, const sigset_t *sigset)
 {
qemu/exec.c +20 -11
Index: b/qemu/exec.c
===================================================================
@@ -1110,6 +1110,9 @@ int cpu_watchpoint_insert(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(env, addr, len, type);
+
     if (type != GDB_WATCHPOINT_WRITE)
         return -ENOSYS;
 
@@ -1139,6 +1142,9 @@ int cpu_watchpoint_remove(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(env, addr, len, type);
+
     if (type != GDB_WATCHPOINT_WRITE)
         return -ENOSYS;
 
@@ -1165,6 +1171,9 @@ int cpu_breakpoint_insert(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(env, pc, len, type);
+
     for(i = 0; i < env->nb_breakpoints; i++) {
         if (env->breakpoints[i] == pc)
             return 0;
@@ -1175,9 +1184,6 @@ int cpu_breakpoint_insert(CPUState *env,
     foreach_cpu(env_iter)
         env_iter->breakpoints[env_iter->nb_breakpoints++] = pc;
 
-    if (kvm_enabled())
-	kvm_update_debugger(env);
-
     breakpoint_invalidate(env, pc);
     return 0;
 #else
@@ -1192,6 +1198,10 @@ int cpu_breakpoint_remove(CPUState *env,
 #if defined(TARGET_HAS_ICE)
     CPUState *env_iter;
     int i;
+
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(env, pc, len, type);
+
     for(i = 0; i < env->nb_breakpoints; i++) {
         if (env->breakpoints[i] == pc)
             goto found;
@@ -1204,9 +1214,6 @@ int cpu_breakpoint_remove(CPUState *env,
             env_iter->breakpoints[env_iter->nb_breakpoints];
     }
 
-    if (kvm_enabled())
-	kvm_update_debugger(env);
-
     breakpoint_invalidate(env, pc);
     return 0;
 #else
@@ -1221,12 +1228,14 @@ void cpu_single_step(CPUState *env, int 
 #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);
+        }
     }
-    if (kvm_enabled())
-	kvm_update_debugger(env);
 #endif
 }
 
qemu/qemu-kvm.c +135 -22

186 lines not on this page · raw message

libkvm/libkvm.h +7 -2
Index: b/libkvm/libkvm.h
===================================================================
@@ -51,7 +51,10 @@ struct kvm_callbacks {
 	/// generic memory writes to unmapped memory (For MMIO devices)
     int (*mmio_write)(void *opaque, uint64_t addr, uint8_t *data,
 					int len);
-    int (*debug)(void *opaque, int vcpu);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    int (*debug)(void *opaque, int vcpu,
+		 struct kvm_debug_exit_arch *arch_info);
+#endif
 	/*!
 	 * \brief Called when the VCPU issues an 'hlt' instruction.
 	 *
@@ -337,7 +340,9 @@ static inline int kvm_reset_mpstate(kvm_
  */
 int kvm_inject_irq(kvm_context_t kvm, int vcpu, unsigned irq);
 
-int kvm_guest_debug(kvm_context_t, int vcpu, struct kvm_debug_guest *dbg);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+int kvm_set_guest_debug(kvm_context_t, int vcpu, struct kvm_guest_debug *dbg);
+#endif
 
 #if defined(__i386__) || defined(__x86_64__)
 /*!
qemu/qemu-kvm.h +30 -3
Index: b/qemu/qemu-kvm.h
===================================================================
@@ -12,8 +12,6 @@
 
 #include <signal.h>
 
-#include <signal.h>
-
 int kvm_main_loop(void);
 int kvm_qemu_init(void);
 int kvm_qemu_create_context(void);
@@ -25,7 +23,11 @@ void kvm_save_registers(CPUState *env);
 void kvm_load_mpstate(CPUState *env);
 void kvm_save_mpstate(CPUState *env);
 int kvm_cpu_exec(CPUState *env);
-int kvm_update_debugger(CPUState *env);
+int kvm_insert_breakpoint(CPUState *env, target_ulong addr,
+			  target_ulong len, int type);
+int kvm_remove_breakpoint(CPUState *env, target_ulong addr,
+			  target_ulong len, int type);
+int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
 int kvm_qemu_init_env(CPUState *env);
 int kvm_qemu_check_extension(int ext);
 void kvm_apic_init(CPUState *env);
@@ -61,6 +63,31 @@ int kvm_arch_try_push_interrupts(void *o
 void kvm_arch_update_regs_for_sipi(CPUState *env);
 void kvm_arch_cpu_reset(CPUState *env);
 
+struct kvm_guest_debug;
+struct kvm_debug_exit_arch;
+
+struct kvm_sw_breakpoint {
+	target_ulong pc;
+	target_ulong saved_insn;
+	int usecount;
+	struct kvm_sw_breakpoint *prev;
+	struct kvm_sw_breakpoint *next;
+};
+
+extern struct kvm_sw_breakpoint *first_sw_breakpoint;
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
+struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc);
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp);
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp);
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type);
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type);
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
+
 CPUState *qemu_kvm_cpu_env(int index);
 
 void qemu_kvm_aio_wait_start(void);
qemu/qemu-kvm-ia64.c +33 -0
Index: b/qemu/qemu-kvm-ia64.c
===================================================================
@@ -65,3 +65,36 @@ void kvm_arch_update_regs_for_sipi(CPUSt
 void kvm_arch_cpu_reset(CPUState *env)
 {
 }
+
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
+{
+    return 0;
+}
+
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
+{
+}
qemu/qemu-kvm-powerpc.c +33 -0
Index: b/qemu/qemu-kvm-powerpc.c
===================================================================
@@ -217,3 +217,36 @@ int handle_powerpc_dcr_write(int vcpu, u
 void kvm_arch_cpu_reset(CPUState *env)
 {
 }
+
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
+{
+    return 0;
+}
+
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
+{
+}
qemu/qemu-kvm-x86.c +167 -0

170 lines not on this page · raw message

user/main.c +6 -1
Index: b/user/main.c
===================================================================
@@ -284,11 +284,14 @@ static int test_outl(void *opaque, uint1
 	return 0;
 }
 
-static int test_debug(void *opaque, int vcpu)
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+static int test_debug(void *opaque, int vcpu,
+		      struct kvm_debug_exit_arch *arch_info)
 {
 	printf("test_debug\n");
 	return 0;
 }
+#endif
 
 static int test_halt(void *opaque, int vcpu)
 {
@@ -343,7 +346,9 @@ static struct kvm_callbacks test_callbac
 	.outl        = test_outl,
 	.mmio_read   = test_mem_read,
 	.mmio_write  = test_mem_write,
+#ifdef KVM_CAP_SET_GUEST_DEBUG
 	.debug       = test_debug,
+#endif
 	.halt        = test_halt,
 	.io_window = test_io_window,
 	.try_push_interrupts = test_try_push_interrupts,
author
#13 lore raw

Re: [PATCH 7/11] QEMU/KVM: New guest debugging interface

[ Updated to latest git. Patch 6/11 is obsolete now. ]

This patch switches both libkvm as well as the qemu pieces over to the new guest debug interface. It comes with full support for software-based breakpoints (via guest code modification), hardware-assisted breakpoints and watchpoints (x86-only so far).

Breakpoint management is done inside qemu-kvm, transparent to gdbstub, and also avoiding that the gdb frontend takes over. This allows for running debuggers inside the guest while guest debugging it active, because the host can cleanly tell apart host- and guest-originated breakpoint events.

Yet improvable are x86 corner cases when using single-step ("forgotten" debug flags on the guest's stack). And, of courese, the yet empty non-x86 helper functions have to be populated.

---
 libkvm/libkvm.c         |   14 +++-
 libkvm/libkvm.h         |    9 ++
 qemu/exec.c             |   31 +++++---
 qemu/qemu-kvm-ia64.c    |   33 +++++++++
 qemu/qemu-kvm-powerpc.c |   33 +++++++++
 qemu/qemu-kvm-x86.c     |  167 ++++++++++++++++++++++++++++++++++++++++++++++++
 qemu/qemu-kvm.c         |  160 ++++++++++++++++++++++++++++++++++++++-------
 qemu/qemu-kvm.h         |   33 ++++++++-
 user/main.c             |    7 +-
 9 files changed, 442 insertions(+), 45 deletions(-)
libkvm/libkvm.c +11 -3
Index: b/libkvm/libkvm.c
===================================================================
@@ -740,7 +740,13 @@ static int handle_io(kvm_context_t kvm, 
 
 int handle_debug(kvm_context_t kvm, int vcpu)
 {
-	return kvm->callbacks->debug(kvm->opaque, vcpu);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+	struct kvm_run *run = kvm->run[vcpu];
+
+	return kvm->callbacks->debug(kvm->opaque, vcpu, &run->debug.arch);
+#else
+	return 0;
+#endif
 }
 
 int kvm_get_regs(kvm_context_t kvm, int vcpu, struct kvm_regs *regs)
@@ -945,10 +951,12 @@ int kvm_inject_irq(kvm_context_t kvm, in
 	return ioctl(kvm->vcpu_fd[vcpu], KVM_INTERRUPT, &intr);
 }
 
-int kvm_guest_debug(kvm_context_t kvm, int vcpu, struct kvm_debug_guest *dbg)
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+int kvm_set_guest_debug(kvm_context_t kvm, int vcpu, struct kvm_guest_debug *dbg)
 {
-	return ioctl(kvm->vcpu_fd[vcpu], KVM_DEBUG_GUEST, dbg);
+	return ioctl(kvm->vcpu_fd[vcpu], KVM_SET_GUEST_DEBUG, dbg);
 }
+#endif
 
 int kvm_set_signal_mask(kvm_context_t kvm, int vcpu, const sigset_t *sigset)
 {
qemu/exec.c +20 -11
Index: b/qemu/exec.c
===================================================================
@@ -1110,6 +1110,9 @@ int cpu_watchpoint_insert(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(env, addr, len, type);
+
     if (type != GDB_WATCHPOINT_WRITE)
         return -ENOSYS;
 
@@ -1139,6 +1142,9 @@ int cpu_watchpoint_remove(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(env, addr, len, type);
+
     if (type != GDB_WATCHPOINT_WRITE)
         return -ENOSYS;
 
@@ -1165,6 +1171,9 @@ int cpu_breakpoint_insert(CPUState *env,
     CPUState *env_iter;
     int i;
 
+    if (kvm_enabled())
+        return kvm_insert_breakpoint(env, pc, len, type);
+
     for(i = 0; i < env->nb_breakpoints; i++) {
         if (env->breakpoints[i] == pc)
             return 0;
@@ -1175,9 +1184,6 @@ int cpu_breakpoint_insert(CPUState *env,
     foreach_cpu(env_iter)
         env_iter->breakpoints[env_iter->nb_breakpoints++] = pc;
 
-    if (kvm_enabled())
-	kvm_update_debugger(env);
-
     breakpoint_invalidate(env, pc);
     return 0;
 #else
@@ -1192,6 +1198,10 @@ int cpu_breakpoint_remove(CPUState *env,
 #if defined(TARGET_HAS_ICE)
     CPUState *env_iter;
     int i;
+
+    if (kvm_enabled())
+        return kvm_remove_breakpoint(env, pc, len, type);
+
     for(i = 0; i < env->nb_breakpoints; i++) {
         if (env->breakpoints[i] == pc)
             goto found;
@@ -1204,9 +1214,6 @@ int cpu_breakpoint_remove(CPUState *env,
             env_iter->breakpoints[env_iter->nb_breakpoints];
     }
 
-    if (kvm_enabled())
-	kvm_update_debugger(env);
-
     breakpoint_invalidate(env, pc);
     return 0;
 #else
@@ -1221,12 +1228,14 @@ void cpu_single_step(CPUState *env, int 
 #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);
+        }
     }
-    if (kvm_enabled())
-	kvm_update_debugger(env);
 #endif
 }
 
qemu/qemu-kvm.c +135 -25

201 lines not on this page · raw message

libkvm/libkvm.h +7 -2
Index: b/libkvm/libkvm.h
===================================================================
@@ -51,7 +51,10 @@ struct kvm_callbacks {
 	/// generic memory writes to unmapped memory (For MMIO devices)
     int (*mmio_write)(void *opaque, uint64_t addr, uint8_t *data,
 					int len);
-    int (*debug)(void *opaque, int vcpu);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+    int (*debug)(void *opaque, int vcpu,
+		 struct kvm_debug_exit_arch *arch_info);
+#endif
 	/*!
 	 * \brief Called when the VCPU issues an 'hlt' instruction.
 	 *
@@ -337,7 +340,9 @@ static inline int kvm_reset_mpstate(kvm_
  */
 int kvm_inject_irq(kvm_context_t kvm, int vcpu, unsigned irq);
 
-int kvm_guest_debug(kvm_context_t, int vcpu, struct kvm_debug_guest *dbg);
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+int kvm_set_guest_debug(kvm_context_t, int vcpu, struct kvm_guest_debug *dbg);
+#endif
 
 #if defined(__i386__) || defined(__x86_64__)
 /*!
qemu/qemu-kvm.h +30 -3
Index: b/qemu/qemu-kvm.h
===================================================================
@@ -12,8 +12,6 @@
 
 #include <signal.h>
 
-#include <signal.h>
-
 int kvm_main_loop(void);
 int kvm_qemu_init(void);
 int kvm_qemu_create_context(void);
@@ -25,7 +23,11 @@ void kvm_save_registers(CPUState *env);
 void kvm_load_mpstate(CPUState *env);
 void kvm_save_mpstate(CPUState *env);
 int kvm_cpu_exec(CPUState *env);
-int kvm_update_debugger(CPUState *env);
+int kvm_insert_breakpoint(CPUState *env, target_ulong addr,
+			  target_ulong len, int type);
+int kvm_remove_breakpoint(CPUState *env, target_ulong addr,
+			  target_ulong len, int type);
+int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
 int kvm_qemu_init_env(CPUState *env);
 int kvm_qemu_check_extension(int ext);
 void kvm_apic_init(CPUState *env);
@@ -61,6 +63,31 @@ int kvm_arch_try_push_interrupts(void *o
 void kvm_arch_update_regs_for_sipi(CPUState *env);
 void kvm_arch_cpu_reset(CPUState *env);
 
+struct kvm_guest_debug;
+struct kvm_debug_exit_arch;
+
+struct kvm_sw_breakpoint {
+	target_ulong pc;
+	target_ulong saved_insn;
+	int usecount;
+	struct kvm_sw_breakpoint *prev;
+	struct kvm_sw_breakpoint *next;
+};
+
+extern struct kvm_sw_breakpoint *first_sw_breakpoint;
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info);
+struct kvm_sw_breakpoint *kvm_find_sw_breakpoint(target_ulong pc);
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp);
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp);
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type);
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type);
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg);
+
 CPUState *qemu_kvm_cpu_env(int index);
 
 void qemu_kvm_aio_wait_start(void);
qemu/qemu-kvm-ia64.c +33 -0
Index: b/qemu/qemu-kvm-ia64.c
===================================================================
@@ -65,3 +65,36 @@ void kvm_arch_update_regs_for_sipi(CPUSt
 void kvm_arch_cpu_reset(CPUState *env)
 {
 }
+
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
+{
+    return 0;
+}
+
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
+{
+}
qemu/qemu-kvm-powerpc.c +33 -0
Index: b/qemu/qemu-kvm-powerpc.c
===================================================================
@@ -217,3 +217,36 @@ int handle_powerpc_dcr_write(int vcpu, u
 void kvm_arch_cpu_reset(CPUState *env)
 {
 }
+
+int kvm_arch_insert_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_remove_sw_breakpoint(CPUState *env,
+				  struct kvm_sw_breakpoint *bp)
+{
+    return -EINVAL;
+}
+
+int kvm_arch_insert_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_remove_hw_breakpoint(target_ulong addr,
+				  target_ulong len, int type)
+{
+    return -ENOSYS;
+}
+
+int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info)
+{
+    return 0;
+}
+
+void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg)
+{
+}
qemu/qemu-kvm-x86.c +167 -0

170 lines not on this page · raw message

user/main.c +6 -1
Index: b/user/main.c
===================================================================
@@ -284,11 +284,14 @@ static int test_outl(void *opaque, uint1
 	return 0;
 }
 
-static int test_debug(void *opaque, int vcpu)
+#ifdef KVM_CAP_SET_GUEST_DEBUG
+static int test_debug(void *opaque, int vcpu,
+		      struct kvm_debug_exit_arch *arch_info)
 {
 	printf("test_debug\n");
 	return 0;
 }
+#endif
 
 static int test_halt(void *opaque, int vcpu)
 {
@@ -343,7 +346,9 @@ static struct kvm_callbacks test_callbac
 	.outl        = test_outl,
 	.mmio_read   = test_mem_read,
 	.mmio_write  = test_mem_write,
+#ifdef KVM_CAP_SET_GUEST_DEBUG
 	.debug       = test_debug,
+#endif
 	.halt        = test_halt,
 	.io_window = test_io_window,
 	.try_push_interrupts = test_try_push_interrupts,
author
#14 lore raw

PATCH 8/11 QEMU/KVM: Support for SMP guest debugging

In reply to Jan Kiszka

Enables debugging of SMP guests via the new KVM debug interface. It updates the monitored CPU on breakpoints and unsures that breakpoint changes are propagested to all virtual CPUs.

---
 qemu/qemu-kvm.c |   24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
qemu/qemu-kvm.c +18 -6
Index: b/qemu/qemu-kvm.c
===================================================================
@@ -19,6 +19,7 @@ int kvm_pit = 1;
 #include "qemu-common.h"
 #include "console.h"
 #include "block.h"
+#include "monitor.h"
 
 #include "qemu-kvm.h"
 #include <libkvm.h>
@@ -58,7 +59,7 @@ pthread_t io_thread;
 static int io_thread_fd = -1;
 static int io_thread_sigfd = -1;
 
-static int kvm_debug_stop_requested;
+static CPUState *kvm_debug_cpu_requested;
 
 static inline unsigned long kvm_get_thread_id(void)
 {
@@ -534,9 +535,10 @@ int kvm_main_loop(void)
             qemu_system_powerdown();
         else if (qemu_reset_requested())
 	    qemu_kvm_system_reset();
-	else if (kvm_debug_stop_requested) {
+	else if (kvm_debug_cpu_requested) {
+	    mon_set_cpu(kvm_debug_cpu_requested);
 	    vm_stop(EXCP_DEBUG);
-	    kvm_debug_stop_requested = 0;
+	    kvm_debug_cpu_requested = NULL;
 	}
     }
 
@@ -552,7 +554,7 @@ int kvm_debug(void *opaque, int vcpu, st
     int handle = kvm_arch_debug(arch_info);
 
     if (handle) {
-	kvm_debug_stop_requested = 1;
+	kvm_debug_cpu_requested = cpu_single_env;
 	vcpu_info[vcpu].stopped = 1;
     }
     return handle;
@@ -846,7 +848,12 @@ int kvm_insert_breakpoint(CPUState *env,
 	if (err)
 	    return err;
     }
-    return kvm_update_guest_debug(env, 0);
+    foreach_cpu(env) {
+	err = kvm_update_guest_debug(env, 0);
+	if (err)
+	    break;
+    }
+    return err;
 }
 
 int kvm_remove_breakpoint(CPUState *env, target_ulong addr,
@@ -882,7 +889,12 @@ int kvm_remove_breakpoint(CPUState *env,
 	if (err)
 	    return err;
     }
-    return kvm_update_guest_debug(env, 0);
+    foreach_cpu(env) {
+	err = kvm_update_guest_debug(env, 0);
+	if (err)
+	    break;
+    }
+    return err;
 }
 
 #else /* !KVM_CAP_SET_GUEST_DEBUG */
author
#15 lore raw

PATCH 9/11 KVM: New guest debugging interface

In reply to Jan Kiszka

This rips out the support for KVM_DEBUG_GUEST and introduces a new IOCTL
instead: KVM_SET_GUEST_DEBUG. The IOCTL payload consists of a generic part, controlling the "main switch" and the single-step feature. The arch specific part adds an x86 interface for intercepting both types of debug exceptions separately and re-injecting them when the host was not interested. Moveover, the foundation for guest debugging via debug registers is layed.

To signal breakpoint events properly back to userland, an arch-specific data block is now returned along KVM_EXIT_DEBUG. For x86, the arch block contains the PC, the debug exception, and relevant debug registers to tell debug events properly apart.

The availability of this new interface is signaled by KVM_CAP_SET_GUEST_DEBUG. Empty stubs for not yet supported archs are provided.

Note that both svm and vtx are supported, but only the latter was tested yet.

---
 arch/ia64/kvm/kvm-ia64.c   |    4 +-
 arch/powerpc/kvm/powerpc.c |    4 +-
 arch/s390/kvm/kvm-s390.c   |    4 +-
 arch/x86/kvm/svm.c         |   45 +++++++++++++++++++++++-
 arch/x86/kvm/vmx.c         |   83 ++++++++++++++-------------------------------
 arch/x86/kvm/x86.c         |   17 ++++-----
 include/asm-ia64/kvm.h     |    7 +++
 include/asm-powerpc/kvm.h  |    7 +++
 include/asm-s390/kvm.h     |    7 +++
 include/asm-x86/kvm.h      |   18 +++++++++
 include/asm-x86/kvm_host.h |   11 +----
 include/linux/kvm.h        |   51 ++++++++++++++++++---------
 include/linux/kvm_host.h   |    6 +--
 virt/kvm/kvm_main.c        |    6 +--
 14 files changed, 168 insertions(+), 102 deletions(-)
arch/x86/kvm/x86.c +9 -8
Index: b/arch/x86/kvm/x86.c
===================================================================
@@ -2729,10 +2729,6 @@ static int __vcpu_run(struct kvm_vcpu *v
 	down_read(&vcpu->kvm->slots_lock);
 	vapic_enter(vcpu);
 
-preempted:
-	if (vcpu->guest_debug.enabled)
-		kvm_x86_ops->guest_debug_pre(vcpu);
-
 again:
 	if (vcpu->requests)
 		if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
@@ -2868,7 +2864,7 @@ out:
 	if (r > 0) {
 		kvm_resched(vcpu);
 		down_read(&vcpu->kvm->slots_lock);
-		goto preempted;
+		goto again;
 	}
 
 	post_kvm_run_save(vcpu, kvm_run);
@@ -2972,7 +2968,7 @@ int kvm_arch_vcpu_ioctl_get_regs(struct 
 	/*
 	 * Don't leak debug flags in case they were set for guest debugging
 	 */
-	if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
+	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
 		regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
 
 	vcpu_put(vcpu);
@@ -3588,8 +3584,8 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct
 	return 0;
 }
 
-int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
-				    struct kvm_debug_guest *dbg)
+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
+					struct kvm_guest_debug *dbg)
 {
 	int r;
 
@@ -3597,6 +3593,11 @@ int kvm_arch_vcpu_ioctl_debug_guest(stru
 
 	r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
 
+	if (dbg->control & KVM_GUESTDBG_INJECT_DB)
+		kvm_queue_exception(vcpu, DB_VECTOR);
+	else if (dbg->control & KVM_GUESTDBG_INJECT_BP)
+		kvm_queue_exception(vcpu, BP_VECTOR);
+
 	vcpu_put(vcpu);
 
 	return r;
include/asm-x86/kvm.h +18 -0
Index: b/include/asm-x86/kvm.h
===================================================================
@@ -230,4 +230,22 @@ struct kvm_pit_state {
 #define KVM_TRC_APIC_ACCESS      (KVM_TRC_HANDLER + 0x14)
 #define KVM_TRC_TDP_FAULT        (KVM_TRC_HANDLER + 0x15)
 
+struct kvm_debug_exit_arch {
+	__u32 exception;
+	__u32 pad;
+	__u64 pc;
+	__u64 dr6;
+	__u64 dr7;
+};
+
+#define KVM_GUESTDBG_USE_SW_BP		0x00010000
+#define KVM_GUESTDBG_USE_HW_BP		0x00020000
+#define KVM_GUESTDBG_INJECT_DB		0x00040000
+#define KVM_GUESTDBG_INJECT_BP		0x00080000
+
+/* for KVM_SET_GUEST_DEBUG */
+struct kvm_guest_debug_arch {
+	__u64 debugreg[8];
+};
+
 #endif
include/asm-x86/kvm_host.h +3 -8
Index: b/include/asm-x86/kvm_host.h
===================================================================
@@ -54,6 +54,8 @@
 #define KVM_PAGES_PER_HPAGE (KVM_HPAGE_SIZE / PAGE_SIZE)
 
 #define DE_VECTOR 0
+#define DB_VECTOR 1
+#define BP_VECTOR 3
 #define UD_VECTOR 6
 #define NM_VECTOR 7
 #define DF_VECTOR 8
@@ -122,12 +124,6 @@ enum {
 
 #define KVM_NR_MEM_OBJS 40
 
-struct kvm_guest_debug {
-	int enabled;
-	unsigned long bp[4];
-	int singlestep;
-};
-
 /*
  * We don't want allocation failures within the mmu code, so we preallocate
  * enough memory for a single page fault in a cache.
@@ -383,8 +379,7 @@ struct kvm_x86_ops {
 	void (*vcpu_put)(struct kvm_vcpu *vcpu);
 
 	int (*set_guest_debug)(struct kvm_vcpu *vcpu,
-			       struct kvm_debug_guest *dbg);
-	void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
+			       struct kvm_guest_debug *dbg);
 	int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
 	int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
 	u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
include/linux/kvm.h +35 -16

86 lines not on this page · raw message

include/linux/kvm_host.h +3 -3
Index: b/include/linux/kvm_host.h
===================================================================
@@ -66,7 +66,7 @@ struct kvm_vcpu {
 	struct kvm_run *run;
 	int guest_mode;
 	unsigned long requests;
-	struct kvm_guest_debug guest_debug;
+	unsigned long guest_debug;
 	int fpu_active;
 	int guest_fpu_loaded;
 	wait_queue_head_t wq;
@@ -237,8 +237,8 @@ int kvm_arch_vcpu_ioctl_get_mpstate(stru
 				    struct kvm_mp_state *mp_state);
 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
 				    struct kvm_mp_state *mp_state);
-int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
-				    struct kvm_debug_guest *dbg);
+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
+					struct kvm_guest_debug *dbg);
 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
 
 int kvm_arch_init(void *opaque);
arch/x86/kvm/vmx.c +27 -56

127 lines not on this page · raw message

arch/x86/kvm/svm.c +43 -2

64 lines not on this page · raw message

virt/kvm/kvm_main.c +3 -3
Index: b/virt/kvm/kvm_main.c
===================================================================
@@ -1042,13 +1042,13 @@ out_free2:
 		r = 0;
 		break;
 	}
-	case KVM_DEBUG_GUEST: {
-		struct kvm_debug_guest dbg;
+	case KVM_SET_GUEST_DEBUG: {
+		struct kvm_guest_debug dbg;
 
 		r = -EFAULT;
 		if (copy_from_user(&dbg, argp, sizeof dbg))
 			goto out;
-		r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
+		r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
 		if (r)
 			goto out;
 		r = 0;
arch/ia64/kvm/kvm-ia64.c +2 -2
Index: b/arch/ia64/kvm/kvm-ia64.c
===================================================================
@@ -1299,8 +1299,8 @@ int kvm_arch_vcpu_ioctl_set_fpu(struct k
 	return -EINVAL;
 }
 
-int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
-		struct kvm_debug_guest *dbg)
+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
+					struct kvm_guest_debug *dbg)
 {
 	return -EINVAL;
 }
arch/powerpc/kvm/powerpc.c +2 -2
Index: b/arch/powerpc/kvm/powerpc.c
===================================================================
@@ -240,8 +240,8 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *
 {
 }
 
-int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
-                                    struct kvm_debug_guest *dbg)
+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
+					struct kvm_guest_debug *dbg)
 {
 	return -ENOTSUPP;
 }
arch/s390/kvm/kvm-s390.c +2 -2
Index: b/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -401,8 +401,8 @@ int kvm_arch_vcpu_ioctl_translate(struct
 	return -EINVAL; /* not implemented yet */
 }
 
-int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
-				    struct kvm_debug_guest *dbg)
+int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
+					struct kvm_guest_debug *dbg)
 {
 	return -EINVAL; /* not implemented yet */
 }
include/asm-ia64/kvm.h +7 -0
Index: b/include/asm-ia64/kvm.h
===================================================================
@@ -208,4 +208,11 @@ struct kvm_sregs {
 struct kvm_fpu {
 };
 
+struct kvm_debug_exit_arch {
+};
+
+/* for KVM_SET_GUEST_DEBUG */
+struct kvm_guest_debug_arch {
+};
+
 #endif
include/asm-powerpc/kvm.h +7 -0
Index: b/include/asm-powerpc/kvm.h
===================================================================
@@ -52,4 +52,11 @@ struct kvm_fpu {
 	__u64 fpr[32];
 };
 
+struct kvm_debug_exit_arch {
+};
+
+/* for KVM_SET_GUEST_DEBUG */
+struct kvm_guest_debug_arch {
+};
+
 #endif /* __LINUX_KVM_POWERPC_H */
include/asm-s390/kvm.h +7 -0
Index: b/include/asm-s390/kvm.h
===================================================================
@@ -42,4 +42,11 @@ struct kvm_fpu {
 	__u64 fprs[16];
 };
 
+struct kvm_debug_exit_arch {
+};
+
+/* for KVM_SET_GUEST_DEBUG */
+struct kvm_guest_debug_arch {
+};
+
 #endif
author
#16 lore raw

PATCH 10/11 KVM-x86: Properly virtualize debug registers

In reply to Jan Kiszka

So far KVM only had basic x86 debug register support, once introduced to realize guest debugging that way. The guest itself was not able to use those registers.

This patch now adds (almost) full support for guest self-debugging via hardware registers. It refactors the code, moving generic parts out of svm (vmx was already cleaned up by the KVM_SET_GUEST_DEBUG patches), it ensures that the registers are properly switched between host and guest, and it also wires up debug register usage by the host. The latter allows for hardware breakpoints/watchpoints in guest code. If this is enabled, the guest will only see faked debug registers without functionality, but with content reflecting the guest's modifications.

Tested on Intel only, but SVM /should/ work as well.

Known limitations: Trapping on tss switch won't work - most probably on Intel.

Credits also go to Joerg Roedel - I used his debugging series as platform for this patch.

---
 arch/x86/kvm/kvm_svm.h     |    6 --
 arch/x86/kvm/svm.c         |  125 +++++++++++++++++----------------------------
 arch/x86/kvm/vmx.c         |  114 +++++++++++++++++++++++++++++++++++------
 arch/x86/kvm/vmx.h         |    2 
 arch/x86/kvm/x86.c         |   32 +++++++++++
 include/asm-x86/kvm_host.h |   22 +++++++
 6 files changed, 200 insertions(+), 101 deletions(-)
arch/x86/kvm/kvm_svm.h +0 -6
Index: b/arch/x86/kvm/kvm_svm.h
===================================================================
@@ -18,7 +18,6 @@ static const u32 host_save_user_msrs[] =
 };
 
 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
-#define NUM_DB_REGS 4
 
 struct kvm_vcpu;
 
@@ -29,16 +28,11 @@ struct vcpu_svm {
 	struct svm_cpu_data *svm_data;
 	uint64_t asid_generation;
 
-	unsigned long db_regs[NUM_DB_REGS];
-
 	u64 next_rip;
 
 	u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
 	u64 host_gs_base;
 	unsigned long host_cr2;
-	unsigned long host_db_regs[NUM_DB_REGS];
-	unsigned long host_dr6;
-	unsigned long host_dr7;
 
 	u32 *msrpm;
 };
arch/x86/kvm/svm.c +48 -77

200 lines not on this page · raw message

arch/x86/kvm/vmx.c +98 -16

174 lines not on this page · raw message

arch/x86/kvm/vmx.h +1 -1
Index: b/arch/x86/kvm/vmx.h
===================================================================
@@ -304,7 +304,7 @@ enum vmcs_field {
 #define DEBUG_REG_ACCESS_TYPE           0x10    /* 4, direction of access */
 #define TYPE_MOV_TO_DR                  (0 << 4)
 #define TYPE_MOV_FROM_DR                (1 << 4)
-#define DEBUG_REG_ACCESS_REG            0xf00   /* 11:8, general purpose reg. */
+#define DEBUG_REG_ACCESS_REG(eq)        (((eq) >> 8) & 0xf) /* 11:8, general purpose reg. */
 
 
 /* segment AR */
arch/x86/kvm/x86.c +31 -1
Index: b/arch/x86/kvm/x86.c
===================================================================
@@ -2720,7 +2720,7 @@ static int __vcpu_run(struct kvm_vcpu *v
 		pr_debug("vcpu %d received sipi with vector # %x\n",
 		       vcpu->vcpu_id, vcpu->arch.sipi_vector);
 		kvm_lapic_reset(vcpu);
-		r = kvm_x86_ops->vcpu_reset(vcpu);
+		r = kvm_arch_vcpu_reset(vcpu);
 		if (r)
 			return r;
 		vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
@@ -2811,9 +2811,34 @@ again:
 		if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
 			kvm_x86_ops->tlb_flush(vcpu);
 
+	get_debugreg(vcpu->arch.host_dr6, 6);
+	get_debugreg(vcpu->arch.host_dr7, 7);
+	if (unlikely(vcpu->arch.switch_db_regs)) {
+		get_debugreg(vcpu->arch.host_db[0], 0);
+		get_debugreg(vcpu->arch.host_db[1], 1);
+		get_debugreg(vcpu->arch.host_db[2], 2);
+		get_debugreg(vcpu->arch.host_db[3], 3);
+
+		set_debugreg(0, 7);
+		set_debugreg(vcpu->arch.eff_db[0], 0);
+		set_debugreg(vcpu->arch.eff_db[1], 1);
+		set_debugreg(vcpu->arch.eff_db[2], 2);
+		set_debugreg(vcpu->arch.eff_db[3], 3);
+	}
+
 	KVMTRACE_0D(VMENTRY, vcpu, entryexit);
 	kvm_x86_ops->run(vcpu, kvm_run);
 
+	if (unlikely(vcpu->arch.switch_db_regs)) {
+		set_debugreg(0, 7);
+		set_debugreg(vcpu->arch.host_db[0], 0);
+		set_debugreg(vcpu->arch.host_db[1], 1);
+		set_debugreg(vcpu->arch.host_db[2], 2);
+		set_debugreg(vcpu->arch.host_db[3], 3);
+	}
+	set_debugreg(vcpu->arch.host_dr6, 6);
+	set_debugreg(vcpu->arch.host_dr7, 7);
+
 	vcpu->guest_mode = 0;
 	local_irq_enable();
 
@@ -3781,6 +3806,11 @@ void kvm_arch_vcpu_destroy(struct kvm_vc
 
 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 {
+	vcpu->arch.switch_db_regs = 0;
+	memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
+	vcpu->arch.dr6 = DR6_FIXED_1;
+	vcpu->arch.dr7 = DR7_FIXED_1;
+
 	return kvm_x86_ops->vcpu_reset(vcpu);
 }
 
include/asm-x86/kvm_host.h +22 -0
Index: b/include/asm-x86/kvm_host.h
===================================================================
@@ -124,6 +124,19 @@ enum {
 
 #define KVM_NR_MEM_OBJS 40
 
+#define KVM_NR_DB_REGS	4
+
+#define DR6_BD		(1 << 13)
+#define DR6_BS		(1 << 14)
+#define DR6_FIXED_1	0xffff0ff0
+#define DR6_VOLATILE	0x0000e00f
+
+#define DR7_BP_EN_MASK	0x000000ff
+#define DR7_GE		(1 << 9)
+#define DR7_GD		(1 << 13)
+#define DR7_FIXED_1	0x00000400
+#define DR7_VOLATILE	0xffff23ff
+
 /*
  * We don't want allocation failures within the mmu code, so we preallocate
  * enough memory for a single page fault in a cache.
@@ -284,6 +297,15 @@ struct kvm_vcpu_arch {
 	struct page *time_page;
 
 	bool nmi_pending;
+
+	int switch_db_regs;
+	unsigned long host_db[KVM_NR_DB_REGS];
+	unsigned long host_dr6;
+	unsigned long host_dr7;
+	unsigned long db[KVM_NR_DB_REGS];
+	unsigned long dr6;
+	unsigned long dr7;
+	unsigned long eff_db[KVM_NR_DB_REGS];
 };
 
 struct kvm_mem_alias {
author
#17 lore raw

PATCH 11/11 KVM-x86: Wire up host-managed debug registers

In reply to Jan Kiszka

Add the remaining bits to make use of debug registers also for guest debugging, thus enabling the use of hardware breakpoints and watchpoints.

---
 arch/x86/kvm/svm.c |    5 +++++
 arch/x86/kvm/vmx.c |    5 +++++
 arch/x86/kvm/x86.c |   14 +++++++++++++-
 3 files changed, 23 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +5 -0
Index: b/arch/x86/kvm/svm.c
===================================================================
@@ -889,6 +889,11 @@ static int svm_guest_debug(struct kvm_vc
 	} else
 		vcpu->guest_debug = 0;
 
+	if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
+		svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
+	else
+		svm->vmcb->save.dr7 = vcpu->arch.dr7;
+
 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
 		svm->vmcb->save.rflags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
 	else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
arch/x86/kvm/vmx.c +5 -0
Index: b/arch/x86/kvm/vmx.c
===================================================================
@@ -964,6 +964,11 @@ static int set_guest_debug(struct kvm_vc
 	if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
 		vcpu->guest_debug = 0;
 
+	if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
+		vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
+	else
+		vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
+
 	flags = vmcs_readl(GUEST_RFLAGS);
 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
 		flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
arch/x86/kvm/x86.c +13 -1
Index: b/arch/x86/kvm/x86.c
===================================================================
@@ -3612,10 +3612,22 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct
 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
 					struct kvm_guest_debug *dbg)
 {
-	int r;
+	int i, r;
 
 	vcpu_load(vcpu);
 
+	if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) ==
+	    (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) {
+		for (i = 0; i < KVM_NR_DB_REGS; ++i)
+			vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
+		vcpu->arch.switch_db_regs =
+			(dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
+	} else {
+		for (i = 0; i < KVM_NR_DB_REGS; i++)
+			vcpu->arch.eff_db[i] = vcpu->arch.db[i];
+		vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
+	}
+
 	r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
 
 	if (dbg->control & KVM_GUESTDBG_INJECT_DB)
#18 lore raw
In reply to Jan Kiszka
Quoted 22 lines · Jan Kiszka

Hi,

here comes the full series of patches to fix and enhance debugging support of KVM. After completing hardware breakpoint support and reorganizing the patches, I felt like I should post them all in a single series, because my queue became quite long and potentially confusing in the meantime.

The major changes or this revision are full hardware-assisted guest debugging and a lot of refactoring on the userspace side. The latter was motivated by an increasing inconsistency regarding which arch-specific part should be handled in libkvm and which by qemu. I decided to push everything into qemu, keeping the lib small (and easier to overcome one day :->).

When I was trying to test the hardware breakpoint/watchpoint support with gdb, the first result with quite disappointing: current gdb is unable to handle such breakpoint remotely. There is hope, latest cvs has it fixed - but suffers from other regressions which make it unusable for kernel debugging. In the meantime, you can still write your own "maintenance packet Z2,..." requests. :p

Quoted 4 lines

To give an overview of the series:
Patch 1..3 - Critical debugger fixes for KVM (should be merge in any
case)

Applied 2, 3 (and my version of 1).

Quoted 5 lines

Patch 4..5 - QEMU refactoring, required for following KVM patches, but
also of generic use for QEMU (rebase and feature enhancements are required to get them in shape for upstream)

These need to go into upstream first, otherwise we're just moving one patch queue into another.

Quoted 4 lines

Patch 6..8 - New guest debugging interface and SMP support Patch 9..11 - Kernel-side changes for new guest debugging interface and
proper x86 debug register virtualization

I'll do a proper review of these, but from a cursory look, all is well.

Signature · 1 line
error compiling committee.c: too many arguments to function
#19 lore raw
Quoted 9 lines · Avi Kivity
Quoted 5 lines · Jan Kiszka

Patch 4..5 - QEMU refactoring, required for following KVM patches, but
also of generic use for QEMU (rebase and feature enhancements are required to get them in shape for upstream)

These need to go into upstream first, otherwise we're just moving one patch queue into another.

I was afraid that you'll say this. This is definitely on my to-do list, but will likely take some time to finish - and to get accepted upstream (QEMU's feedback/merge round trip time is always hard to estimate :-/).

OK, let's see how far we get. Maybe it'll take an intermediate step for KVM, ie. userspace patches with limited functionality, but without dependencies on generic QEMU changes.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#20 lore raw
In reply to Avi Kivity
Quoted 6 lines · On Tuesday 27 May 2008 04:50:20 Avi Kivity wrote:
Quoted 4 lines

 Patch 6..8  - New guest debugging interface and SMP support Patch 9..11 - Kernel-side changes for new guest debugging interface and proper x86 debug register virtualization

I'll do a proper review of these, but from a cursory look, all is well.

I agree; these look good, and good interface layering. We'll be able to provide a PPC implementation for the arch-specific hooks at some point.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center