Skip to contentSkip to conversation

kvm tools: Add NMI ability to 'kvm debug'

PATCH New Sasha Levin 1 participant 1 message Dec 11, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Add NMI ability to 'kvm debug'

This allows triggering NMI on guests using 'kvm debug -m [cpu]'.

Please note that the default behaviour of 'kvm debug' dumping guest's cpu state has been modified to require a '-d'/--dump.

---
 tools/kvm/builtin-debug.c                |   22 +++++++++++----
 tools/kvm/builtin-run.c                  |   16 +++++++++++-
 tools/kvm/include/kvm/builtin-debug.h    |   11 ++++++++
 tools/kvm/include/kvm/kvm-cpu.h          |    1 +
 tools/kvm/kvm-cpu.c                      |    5 +++
 tools/kvm/x86/include/kvm/kvm-cpu-arch.h |    1 +
 tools/kvm/x86/kvm-cpu.c                  |   41 ++++++++++++++++++++++++++++++
 7 files changed, 90 insertions(+), 7 deletions(-)
tools/kvm/builtin-debug.c +16 -6
index 045dc2c..eee26c3 100644
@@ -14,13 +14,10 @@
 
 static bool all;
 static int instance;
+static int nmi = -1;
+static bool dump;
 static const char *instance_name;
 
-struct debug_cmd {
-	u32 type;
-	u32 len;
-};
-
 static const char * const debug_usage[] = {
 	"kvm debug [--all] [-n name]",
 	NULL
@@ -30,6 +27,8 @@ static const struct option debug_options[] = {
 	OPT_GROUP("General options:"),
 	OPT_BOOLEAN('a', "all", &all, "Debug all instances"),
 	OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
+	OPT_BOOLEAN('d', "dump", &dump, "Generate a debug dump from guest"),
+	OPT_INTEGER('m', "nmi", &nmi, "Generate NMI on VCPU"),
 	OPT_END()
 };
 
@@ -51,13 +50,24 @@ void kvm_debug_help(void)
 static int do_debug(const char *name, int sock)
 {
 	char buff[BUFFER_SIZE];
-	struct debug_cmd cmd = {KVM_IPC_DEBUG, 0};
+	struct debug_cmd cmd = {KVM_IPC_DEBUG, 2 * sizeof(u32)};
 	int r;
 
+	if (dump)
+		cmd.dbg_type |= KVM_DEBUG_CMD_TYPE_DUMP;
+
+	if (nmi != -1) {
+		cmd.dbg_type |= KVM_DEBUG_CMD_TYPE_NMI;
+		cmd.cpu = nmi;
+	}
+
 	r = xwrite(sock, &cmd, sizeof(cmd));
 	if (r < 0)
 		return r;
 
+	if (!dump)
+		return 0;
+
 	do {
 		r = xread(sock, buff, BUFFER_SIZE);
 		if (r < 0)
tools/kvm/builtin-run.c +15 -1
index 7969901..7709edb 100644
@@ -31,6 +31,7 @@
 #include "kvm/guest_compat.h"
 #include "kvm/pci-shmem.h"
 #include "kvm/kvm-ipc.h"
+#include "kvm/builtin-debug.h"
 
 #include <linux/types.h>
 
@@ -464,7 +465,7 @@ static void handle_sigusr1(int sig)
 	struct kvm_cpu *cpu = current_kvm_cpu;
 	int fd = kvm_cpu__get_debug_fd();
 
-	if (!cpu)
+	if (!cpu || cpu->needs_nmi)
 		return;
 
 	dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
@@ -495,6 +496,19 @@ static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
 static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
 {
 	int i;
+	u32 dbg_type = *(u32 *)msg;
+	int vcpu = *(((u32 *)msg) + 1);
+
+	if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) {
+		if (vcpu >= kvm->nrcpus)
+			return;
+
+		kvm_cpus[vcpu]->needs_nmi = 1;
+		pthread_kill(kvm_cpus[vcpu]->thread, SIGUSR1);
+	}
+
+	if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP))
+		return;
 
 	for (i = 0; i < nrcpus; i++) {
 		struct kvm_cpu *cpu = kvm_cpus[i];
tools/kvm/include/kvm/builtin-debug.h +11 -0
index 3fc2469..b24b501 100644
@@ -1,6 +1,17 @@
 #ifndef KVM__DEBUG_H
 #define KVM__DEBUG_H
 
+#include <linux/types.h>
+
+struct debug_cmd {
+	u32 type;
+	u32 len;
+	u32 dbg_type;
+#define KVM_DEBUG_CMD_TYPE_DUMP	(1 << 0)
+#define KVM_DEBUG_CMD_TYPE_NMI	(1 << 1)
+	u32 cpu;
+};
+
 int kvm_cmd_debug(int argc, const char **argv, const char *prefix);
 void kvm_debug_help(void);
 
tools/kvm/include/kvm/kvm-cpu.h +1 -0
index 15618f1..d4448f6 100644
@@ -19,5 +19,6 @@ void kvm_cpu__set_debug_fd(int fd);
 void kvm_cpu__show_code(struct kvm_cpu *vcpu);
 void kvm_cpu__show_registers(struct kvm_cpu *vcpu);
 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu);
+void kvm_cpu__arch_nmi(struct kvm_cpu *cpu);
 
 #endif /* KVM__KVM_CPU_H */
tools/kvm/kvm-cpu.c +5 -0
index 884a89f..8ec4efa 100644
@@ -94,6 +94,11 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 			cpu->paused = 0;
 		}
 
+		if (cpu->needs_nmi) {
+			kvm_cpu__arch_nmi(cpu);
+			cpu->needs_nmi = 0;
+		}
+
 		kvm_cpu__run(cpu);
 
 		switch (cpu->kvm_run->exit_reason) {
tools/kvm/x86/include/kvm/kvm-cpu-arch.h +1 -0
index ed1c727..822d966 100644
@@ -26,6 +26,7 @@ struct kvm_cpu {
 
 	u8			is_running;
 	u8			paused;
+	u8			needs_nmi;
 
 	struct kvm_coalesced_mmio_ring	*ring;
 };
tools/kvm/x86/kvm-cpu.c +41 -0

62 lines not on this page · raw message

Signature · 1 line
1.7.8