Skip to contentSkip to conversation

kvm tools: Make keyboard termination go through regular termination path

PATCH In review Sasha Levin 3 participants 9 messages Aug 11, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 kvm tools: Make keyboard termination go through regular termination path

Instead of exiting directly when a user enters 'ctrl x + a', go through the regular termination path by stopping all VCPUs and letting the main thread handle it.

---
 tools/kvm/builtin-run.c |    9 +++++----
 tools/kvm/kvm-cpu.c     |    8 ++++++--
 tools/kvm/term.c        |    6 +-----
 3 files changed, 12 insertions(+), 11 deletions(-)
tools/kvm/builtin-run.c +5 -4
index fa5de27..c7ed3fa 100644
@@ -719,10 +719,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		exit_code = 1;
 
 	for (i = 1; i < nrcpus; i++) {
-		pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
-		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
-			die("pthread_join");
-
+		if (kvm_cpus[i]->is_running) {
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
+			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
+				die("pthread_join");
+		}
 		if (ret != NULL)
 			exit_code = 1;
 	}
tools/kvm/kvm-cpu.c +6 -2
index 2f5d23c..fc0d6d4 100644
@@ -421,7 +421,11 @@ static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
 
 void kvm_cpu__reboot(void)
 {
-	pthread_kill(kvm_cpus[0]->thread, SIGKVMEXIT);
+	int i;
+
+	for (i = 0; i < KVM_NR_CPUS; i++)
+		if (kvm_cpus[i])
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
 }
 
 int kvm_cpu__start(struct kvm_cpu *cpu)
@@ -442,7 +446,7 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 	if (cpu->kvm->single_step)
 		kvm_cpu__enable_singlestep(cpu);
 
-	for (;;) {
+	while (cpu->is_running) {
 		if (cpu->paused) {
 			kvm__notify_paused();
 			cpu->paused = 0;
tools/kvm/term.c +1 -5
index 2a3e1f0..fa4382d 100644
@@ -34,12 +34,8 @@ int term_getc(int who)
 
 	if (term_got_escape) {
 		term_got_escape = false;
-		if (c == 'x') {
+		if (c == 'x')
 			kvm_cpu__reboot();
-			kvm__delete(kvm);
-			printf("\n  # KVM session terminated.\n");
-			exit(1);
-		}
 		if (c == term_escape_char)
 			return c;
 	}
Signature · 1 line
1.7.6
#1 lore raw

PATCH 2/3 kvm tools: Add kvm__trigger_irq()

Add a helper function to trigger an IRQ.

This function is usefull when an IRQ line has to be raised and lowered such as when using MSI-X.

---
 tools/kvm/include/kvm/kvm.h |    1 +
 tools/kvm/kvm.c             |    6 ++++++
 2 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/kvm.h +1 -0
index 1cbe3c7..ccb20bf 100644
@@ -63,6 +63,7 @@ void kvm__setup_bios(struct kvm *kvm);
 void kvm__start_timer(struct kvm *kvm);
 void kvm__stop_timer(struct kvm *kvm);
 void kvm__irq_line(struct kvm *kvm, int irq, int level);
+void kvm__irq_trigger(struct kvm *kvm, int irq);
 bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int size, u32 count);
 bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write);
 void kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, void *userspace_addr);
tools/kvm/kvm.c +6 -0
index 740851d..be4a02a 100644
@@ -676,6 +676,12 @@ void kvm__irq_line(struct kvm *kvm, int irq, int level)
 		die_perror("KVM_IRQ_LINE failed");
 }
 
+void kvm__irq_trigger(struct kvm *kvm, int irq)
+{
+	kvm__irq_line(kvm, irq, 1);
+	kvm__irq_line(kvm, irq, 0);
+}
+
 void kvm__dump_mem(struct kvm *kvm, unsigned long addr, unsigned long size)
 {
 	unsigned char *p;
Signature · 1 line
1.7.6
#2 lore raw

PATCH 3/3 kvm tools: Add MSI-X support to virtio-net

In reply to Sasha Levin

The device uses the virtio preferred method of working with MSI-X by creating one vector for configuration and one vector for each vq in the device.

---
 tools/kvm/virtio/net.c |   54 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 49 insertions(+), 5 deletions(-)
tools/kvm/virtio/net.c +49 -5
index e865b7f..35d4997 100644
@@ -60,6 +60,9 @@ struct net_dev {
 	u8				isr;
 	u16				queue_selector;
 	u16				base_addr;
+	u32				vq_vector[VIRTIO_NET_NUM_QUEUES];
+	u32				gsis[VIRTIO_NET_NUM_QUEUES];
+	u32				msix_io_block;
 
 	pthread_t			io_rx_thread;
 	pthread_mutex_t			io_rx_lock;
@@ -125,7 +128,7 @@ static void *virtio_net_rx_thread(void *p)
 			virt_queue__set_used_elem(vq, head, len);
 
 			/* We should interrupt guest right now, otherwise latency is huge. */
-			virt_queue__trigger_irq(vq, pci_header.irq_line, &ndev.isr, kvm);
+			kvm__irq_trigger(kvm, ndev.gsis[VIRTIO_NET_RX_QUEUE]);
 		}
 
 	}
@@ -162,8 +165,7 @@ static void *virtio_net_tx_thread(void *p)
 			virt_queue__set_used_elem(vq, head, len);
 		}
 
-		virt_queue__trigger_irq(vq, pci_header.irq_line, &ndev.isr, kvm);
-
+		kvm__irq_trigger(kvm, ndev.gsis[VIRTIO_NET_TX_QUEUE]);
 	}
 
 	pthread_exit(NULL);
@@ -219,6 +221,12 @@ static bool virtio_net_pci_io_in(struct ioport *ioport, struct kvm *kvm, u16 por
 		kvm__irq_line(kvm, pci_header.irq_line, VIRTIO_IRQ_LOW);
 		ndev.isr = VIRTIO_IRQ_LOW;
 		break;
+	case VIRTIO_MSI_CONFIG_VECTOR:
+		ioport__write16(data, ndev.config_vector);
+		break;
+	case VIRTIO_MSI_QUEUE_VECTOR:
+		ioport__write16(data, ndev.vq_vector[ndev.queue_selector]);
+		break;
 	default:
 		ret = virtio_net_pci_io_device_specific_in(data, offset, size, count);
 	};
@@ -285,10 +293,22 @@ static bool virtio_net_pci_io_out(struct ioport *ioport, struct kvm *kvm, u16 po
 		ndev.status		= ioport__read8(data);
 		break;
 	case VIRTIO_MSI_CONFIG_VECTOR:
-		ndev.config_vector	= VIRTIO_MSI_NO_VECTOR;
+		ndev.config_vector	= ioport__read16(data);
 		break;
-	case VIRTIO_MSI_QUEUE_VECTOR:
+	case VIRTIO_MSI_QUEUE_VECTOR: {
+		u32 gsi;
+		u32 vec;
+
+		vec = ndev.vq_vector[ndev.queue_selector] = ioport__read16(data);
+
+		gsi = irq__add_msix_route(kvm,
+					  pci_header.msix.table[vec].low,
+					  pci_header.msix.table[vec].high,
+					  pci_header.msix.table[vec].data);
+
+		ndev.gsis[ndev.queue_selector] = gsi;
 		break;
+	}
 	default:
 		ret			= false;
 	};
@@ -308,6 +328,15 @@ static struct ioport_operations virtio_net_io_ops = {
 	.io_out	= virtio_net_pci_io_out,
 };
 
+static void callback_mmio(u64 addr, u8 *data, u32 len, u8 is_write, void *ptr)
+{
+	void *table = pci_header.msix.table;
+	if (is_write)
+		memcpy(table + addr - ndev.msix_io_block, data, len);
+	else
+		memcpy(data, table + addr - ndev.msix_io_block, len);
+}
+
 static bool virtio_net__tap_init(const struct virtio_net_parameters *params)
 {
 	int sock = socket(AF_INET, SOCK_STREAM, 0);
@@ -467,6 +496,21 @@ void virtio_net__init(const struct virtio_net_parameters *params)
 		ndev.ops = &uip_ops;
 	}
 
+	ndev.msix_io_block = pci_get_io_space_block();
+	kvm__register_mmio(params->kvm, ndev.msix_io_block, 0x100, callback_mmio, NULL);
+	pci_header.bar[1]	= ndev.msix_io_block |
+				PCI_BASE_ADDRESS_SPACE_MEMORY |
+				PCI_BASE_ADDRESS_MEM_TYPE_64;
+	/* bar[2] is the continuation of bar[1] for 64bit addressing */
+	pci_header.bar[2]	= 0;
+	pci_header.status	= PCI_STATUS_CAP_LIST;
+	pci_header.capabilities	= (void *)&pci_header.msix - (void *)&pci_header;
+
+	pci_header.msix.cap = PCI_CAP_ID_MSIX;
+	pci_header.msix.next = 0;
+	pci_header.msix.table_size = (VIRTIO_NET_NUM_QUEUES + 1) | PCI_MSIX_FLAGS_ENABLE;
+	pci_header.msix.table_offset = 1; /* Use BAR 1 */
+
 	virtio_net__io_thread_init(params->kvm);
 
 	for (i = 0; i < VIRTIO_NET_NUM_QUEUES; i++) {
Signature · 1 line
1.7.6
#3 lore raw
In reply to Sasha Levin
Quoted 62 lines · Sasha Levin

Instead of exiting directly when a user enters 'ctrl x + a', go through the regular termination path by stopping all VCPUs and letting the main thread handle it.

---

tools/kvm/builtin-run.c |    9 +++++----
tools/kvm/kvm-cpu.c     |    8 ++++++--
tools/kvm/term.c        |    6 +-----
3 files changed, 12 insertions(+), 11 deletions(-)
tools/kvm/builtin-run.c +5 -4
index fa5de27..c7ed3fa 100644
@@ -719,10 +719,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		exit_code = 1;
 
 	for (i = 1; i < nrcpus; i++) {
-		pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
-		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
-			die("pthread_join");
-
+		if (kvm_cpus[i]->is_running) {
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
+			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
+				die("pthread_join");
+		}
 		if (ret != NULL)
 			exit_code = 1;
 	}
tools/kvm/kvm-cpu.c +6 -2
index 2f5d23c..fc0d6d4 100644
@@ -421,7 +421,11 @@ static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
 
 void kvm_cpu__reboot(void)
 {
-	pthread_kill(kvm_cpus[0]->thread, SIGKVMEXIT);
+	int i;
+
+	for (i = 0; i < KVM_NR_CPUS; i++)
+		if (kvm_cpus[i])
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
 }
 
 int kvm_cpu__start(struct kvm_cpu *cpu)
@@ -442,7 +446,7 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 	if (cpu->kvm->single_step)
 		kvm_cpu__enable_singlestep(cpu);
 
-	for (;;) {
+	while (cpu->is_running) {
 		if (cpu->paused) {
 			kvm__notify_paused();
 			cpu->paused = 0;
tools/kvm/term.c +0 -0
index 2a3e1f0..fa4382d 100644

tools/kvm/term.c int term_getc(int who) · line 40

Quoted 8 lines
tools/kvm/term.c +1 -3
@@ -34,12 +34,8 @@ int term_getc(int who)
 
 	if (term_got_escape) {
 		term_got_escape = false;
-		if (c == 'x') {
+		if (c == 'x')
 			kvm_cpu__reboot();
-			kvm__delete(kvm);
-			printf("\n  # KVM session terminated.\n");

This is a nice cleanup but I'm not happy about the fact that you also nuke the above printf(). Is there a simple way to keep it there?

Quoted 9 lines
tools/kvm/term.c +0 -2
-			exit(1);
-		}
 		if (c == term_escape_char)
 			return c;
 	}
Signature · 1 line
1.7.6
#4 lore raw
Quoted 64 lines · Pekka Enberg
Quoted 63 lines · Sasha Levin

Instead of exiting directly when a user enters 'ctrl x + a', go through the regular termination path by stopping all VCPUs and letting the main thread handle it.

---

tools/kvm/builtin-run.c |    9 +++++----
tools/kvm/kvm-cpu.c     |    8 ++++++--
tools/kvm/term.c        |    6 +-----
3 files changed, 12 insertions(+), 11 deletions(-)
tools/kvm/builtin-run.c +5 -4
index fa5de27..c7ed3fa 100644
@@ -719,10 +719,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		exit_code = 1;
 
 	for (i = 1; i < nrcpus; i++) {
-		pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
-		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
-			die("pthread_join");
-
+		if (kvm_cpus[i]->is_running) {
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
+			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
+				die("pthread_join");
+		}
 		if (ret != NULL)
 			exit_code = 1;
 	}
tools/kvm/kvm-cpu.c +6 -2
index 2f5d23c..fc0d6d4 100644
@@ -421,7 +421,11 @@ static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
 
 void kvm_cpu__reboot(void)
 {
-	pthread_kill(kvm_cpus[0]->thread, SIGKVMEXIT);
+	int i;
+
+	for (i = 0; i < KVM_NR_CPUS; i++)
+		if (kvm_cpus[i])
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
 }
 
 int kvm_cpu__start(struct kvm_cpu *cpu)
@@ -442,7 +446,7 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 	if (cpu->kvm->single_step)
 		kvm_cpu__enable_singlestep(cpu);
 
-	for (;;) {
+	while (cpu->is_running) {
 		if (cpu->paused) {
 			kvm__notify_paused();
 			cpu->paused = 0;
tools/kvm/term.c +0 -0
index 2a3e1f0..fa4382d 100644
@@ -34,12 +34,8 @@ int term_getc(int who)
 
Quoted 11 lines

tools/kvm/term.c int term_getc(int who) · line 40

Quoted 7 lines
tools/kvm/term.c +1 -3
 	if (term_got_escape) {
 		term_got_escape = false;
-		if (c == 'x') {
+		if (c == 'x')
 			kvm_cpu__reboot();
-			kvm__delete(kvm);
-			printf("\n  # KVM session terminated.\n");

This is a nice cleanup but I'm not happy about the fact that you also nuke the above printf(). Is there a simple way to keep it there?

You get that printf from the normal exit path.

Quoted 9 lines
Quoted 9 lines
tools/kvm/term.c +0 -2
-			exit(1);
-		}
 		if (c == term_escape_char)
 			return c;
 	}
Signature · 1 line
1.7.6
Signature · 1 line
Sasha.
#5 lore raw
Quoted 5 lines · Sasha Levin
Quoted 3 lines

This is a nice cleanup but I'm not happy about the fact that you also nuke the above printf(). Is there a simple way to keep it there?

You get that printf from the normal exit path.

You get a different printf:

$ grep -r "KVM session" *.c
builtin-run.c:		printf("\n  # KVM session ended normally.\n");
term.c:			printf("\n  # KVM session terminated.\n");

It's nice to see that the user terminated the session without going through reboot cycle. Dunno how much it matters but it'd be nice to keep it since it's already there.

Pekka

#6 lore raw
Quoted 9 lines · Pekka Enberg
Quoted 5 lines · Sasha Levin
Quoted 3 lines

This is a nice cleanup but I'm not happy about the fact that you also nuke the above printf(). Is there a simple way to keep it there?

You get that printf from the normal exit path.

You get a different printf:

Quoted 7 lines
$ grep -r "KVM session" *.c
builtin-run.c:		printf("\n  # KVM session ended normally.\n");
term.c:			printf("\n  # KVM session terminated.\n");

It's nice to see that the user terminated the session without going through reboot cycle. Dunno how much it matters but it'd be nice to keep it since it's already there.

Hm... In that case, how about changing it to "# KVM session terminating..." which will be followed by "# KVM session terminated" when the guest is actually dead?

Signature · 1 line
Sasha.
#7 lore raw
Quoted 6 lines · Sasha Levin
Quoted 6 lines

You get a different printf:

$ grep -r "KVM session" *.c
builtin-run.c:		printf("\n  # KVM session ended normally.\n");
term.c:			printf("\n  # KVM session terminated.\n");
Quoted 7 lines
Quoted 3 lines

It's nice to see that the user terminated the session without going through reboot cycle. Dunno how much it matters but it'd be nice to keep it since it's already there.

Hm... In that case, how about changing it to "# KVM session terminating..." which will be followed by "# KVM session terminated" when the guest is actually dead?

It would just add more noise. Oh well, I applied your patch.

Pekka

#8 lore raw
In reply to Sasha Levin
Quoted 36 lines · Sasha Levin

Instead of exiting directly when a user enters 'ctrl x + a', go through the regular termination path by stopping all VCPUs and letting the main thread handle it.

---

tools/kvm/builtin-run.c |    9 +++++----
tools/kvm/kvm-cpu.c     |    8 ++++++--
tools/kvm/term.c        |    6 +-----
3 files changed, 12 insertions(+), 11 deletions(-)
tools/kvm/builtin-run.c +5 -4
index fa5de27..c7ed3fa 100644
@@ -719,10 +719,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 		exit_code = 1;
 
 	for (i = 1; i < nrcpus; i++) {
-		pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
-		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
-			die("pthread_join");
-
+		if (kvm_cpus[i]->is_running) {
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
+			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
+				die("pthread_join");
+		}
 		if (ret != NULL)
 			exit_code = 1;
 	}
tools/kvm/kvm-cpu.c +0 -0
index 2f5d23c..fc0d6d4 100644

tools/kvm/kvm-cpu.c static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu) · line 428

Quoted 9 lines
tools/kvm/kvm-cpu.c +5 -1
@@ -421,7 +421,11 @@ static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
 
 void kvm_cpu__reboot(void)
 {
-	pthread_kill(kvm_cpus[0]->thread, SIGKVMEXIT);
+	int i;
+
+	for (i = 0; i < KVM_NR_CPUS; i++)
+		if (kvm_cpus[i])
+			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);

if so, can we remove "pthread_kill(pthread_self(), SIGKVMEXIT)" from kvm_cpu_signal_handler?

walimis

Quoted 37 lines
tools/kvm/kvm-cpu.c +1 -1
 }
 
 int kvm_cpu__start(struct kvm_cpu *cpu)
@@ -442,7 +446,7 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
 	if (cpu->kvm->single_step)
 		kvm_cpu__enable_singlestep(cpu);
 
-	for (;;) {
+	while (cpu->is_running) {
 		if (cpu->paused) {
 			kvm__notify_paused();
 			cpu->paused = 0;
tools/kvm/term.c +1 -5
index 2a3e1f0..fa4382d 100644
@@ -34,12 +34,8 @@ int term_getc(int who)
 
 	if (term_got_escape) {
 		term_got_escape = false;
-		if (c == 'x') {
+		if (c == 'x')
 			kvm_cpu__reboot();
-			kvm__delete(kvm);
-			printf("\n  # KVM session terminated.\n");
-			exit(1);
-		}
 		if (c == term_escape_char)
 			return c;
 	}
Signature · 6 lines
1.7.6

--
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