Skip to contentSkip to conversation

kvm tools: define marcros for virtio device io space size and pci device number

PATCH New Asias He 1 participant 6 messages Apr 7, 2011 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH 1/6 kvm tools: define marcros for virtio device io space size and pci device number

---
 tools/kvm/blk-virtio.c         |    6 ++++--
 tools/kvm/include/kvm/ioport.h |    7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)
tools/kvm/blk-virtio.c +4 -2
index 48211b5..9a723cc 100644
@@ -278,6 +278,8 @@ static struct pci_device_header blk_virtio_pci_device = {
 	.irq_line		= VIRTIO_BLK_IRQ,
 };
 
+#define PCI_VIRTIO_BLK_DEVNUM 1
+
 void blk_virtio__init(struct kvm *self)
 {
 	if (!self->disk_image)
@@ -285,7 +287,7 @@ void blk_virtio__init(struct kvm *self)
 
 	device.blk_config.capacity = self->disk_image->size / SECTOR_SIZE;
 
-	pci__register(&blk_virtio_pci_device, 1);
+	pci__register(&blk_virtio_pci_device, PCI_VIRTIO_BLK_DEVNUM);
 
-	ioport__register(IOPORT_VIRTIO_BLK, &blk_virtio_io_ops, 256);
+	ioport__register(IOPORT_VIRTIO_BLK, &blk_virtio_io_ops, IOPORT_VIRTIO_BLK_SIZE);
 }
tools/kvm/include/kvm/ioport.h +5 -2
index 7b6af53..b77f565 100644
@@ -5,8 +5,11 @@
 #include <stdint.h>
 
 /* some ports we reserve for own use */
-#define IOPORT_DBG		0xe0
-#define IOPORT_VIRTIO_BLK	0xc200	/* Virtio block device */
+#define IOPORT_DBG			0xe0
+#define IOPORT_VIRTIO_BLK		0xc200	/* Virtio block device */
+#define IOPORT_VIRTIO_BLK_SIZE		256
+#define IOPORT_VIRTIO_CONSOLE		0xd200	/* Virtio console device */
+#define IOPORT_VIRTIO_CONSOLE_SIZE	256
 
 struct kvm;
 
Signature · 1 line
1.7.4.1
author
#1 lore raw

PATCH 2/6 kvm tools: add some helper functions for virt_queue

---
 tools/kvm/include/kvm/virtqueue.h |   42 +++++++++++++++++++++++++++++++++++++
 1 files changed, 42 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/virtqueue.h +42 -0
index 83f5293..de360ae 100644
@@ -4,6 +4,9 @@
 #include <linux/virtio_ring.h>
 
 #include <stdint.h>
+#include <sys/uio.h>
+
+#include "kvm/kvm.h"
 
 struct virt_queue {
 	struct vring			vring;
@@ -28,4 +31,43 @@ static inline struct vring_used_elem *virt_queue__get_used_elem(struct virt_queu
 	return &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num];
 }
 
+static inline struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, uint32_t head, uint32_t len)
+{
+	struct vring_used_elem *used_elem;
+	used_elem	= &queue->vring.used->ring[queue->vring.used->idx++ % queue->vring.num];
+	used_elem->id	= head;
+	used_elem->len	= len;
+	return used_elem;
+}
+
+static inline bool virt_queue__available(struct virt_queue *vq)
+{
+	return vq->vring.avail->idx !=  vq->last_avail_idx;
+}
+
+static inline uint16_t virt_queue__get_iov(struct virt_queue *queue, struct iovec iov[], uint16_t *out, uint16_t *in, struct kvm *kvm)
+{
+	struct vring_desc *desc;
+	uint16_t head, idx;
+
+	idx = head = virt_queue__pop(queue);
+	*out = *in = 0;
+
+	do {
+		desc 				= virt_queue__get_desc(queue, idx);
+		iov[*out + *in].iov_base	= guest_flat_to_host(kvm, desc->addr);
+		iov[*out + *in].iov_len		= desc->len;
+		if (desc->flags & VRING_DESC_F_WRITE)
+			(*in)++;
+		else
+			(*out)++;
+		if (desc->flags & VRING_DESC_F_NEXT)
+			idx = desc->next;
+		else
+			break;
+	} while (1);
+
+	return head;
+}
+
 #endif /* KVM__VIRTQUEUE_H */
Signature · 1 line
1.7.4.1
author
#2 lore raw

PATCH 3/6 kvm tools: move terminal related code to a new file term.c

In reply to Asias He

preparing the virtio console and 8250 serial console consolidation

---
 tools/kvm/Makefile           |    1 +
 tools/kvm/include/kvm/term.h |   17 ++++++++
 tools/kvm/main.c             |   35 +---------------
 tools/kvm/term.c             |   89 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 32 deletions(-)
 create mode 100644 tools/kvm/include/kvm/term.h
 create mode 100644 tools/kvm/term.c
tools/kvm/Makefile +1 -0
index 747aa51..0244f9b 100644
@@ -24,6 +24,7 @@ OBJS	+= main.o
 OBJS	+= mmio.o
 OBJS	+= pci.o
 OBJS	+= util.o
+OBJS	+= term.o
 
 DEPS	:= $(patsubst %.o,%.d,$(OBJS))
 
tools/kvm/include/kvm/term.h new file +17 -0
new file mode 100644
index 0000000..4d580e1
@@ -0,0 +1,17 @@
+#ifndef KVM__TERM_H
+#define KVM__TERM_H
+
+#include <sys/uio.h>
+
+#define CONSOLE_8250	1
+#define CONSOLE_VIRTIO	2
+
+int term_putc_iov(int who, struct iovec *iov, int iovcnt);
+int term_getc_iov(int who, struct iovec *iov, int iovcnt);
+int term_putc(int who, char *addr, int cnt);
+int term_getc(int who);
+
+bool term_readable(int who);
+void term_init(void);
+
+#endif /* KVM__TERM_H */
tools/kvm/main.c +3 -32
index 7beb6a6..46fb9fd 100644
@@ -5,9 +5,9 @@
 #include "kvm/disk-image.h"
 #include "kvm/util.h"
 #include "kvm/pci.h"
+#include "kvm/term.h"
 
 #include <inttypes.h>
-#include <termios.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -29,32 +29,6 @@ static void usage(char *argv[])
 
 static struct kvm *kvm;
 
-static struct termios orig_term;
-
-static void setup_console(void)
-{
-	struct termios term;
-
-	if (tcgetattr(STDIN_FILENO, &orig_term) < 0)
-		die("unable to save initial standard input settings");
-
-	term			= orig_term;
-
-	term.c_lflag		&= ~(ICANON|ECHO);
-
-	tcsetattr(STDIN_FILENO, TCSANOW, &term);
-}
-
-static void cleanup_console(void)
-{
-	tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
-}
-
-static void shutdown(void)
-{
-	cleanup_console();
-}
-
 static void handle_sigint(int sig)
 {
 	exit(1);
@@ -65,7 +39,6 @@ static void handle_sigquit(int sig)
 	kvm__show_registers(kvm);
 	kvm__show_code(kvm);
 	kvm__show_page_tables(kvm);
-
 	kvm__delete(kvm);
 
 	exit(1);
@@ -92,10 +65,6 @@ int main(int argc, char *argv[])
 	signal(SIGQUIT, handle_sigquit);
 	signal(SIGINT, handle_sigint);
 
-	setup_console();
-
-	atexit(shutdown);
-
 	for (i = 1; i < argc; i++) {
 		if (option_matches(argv[i], "--kernel=")) {
 			kernel_filename	= &argv[i][9];
@@ -138,6 +107,8 @@ int main(int argc, char *argv[])
 	if (!kernel_filename)
 		usage(argv);
 
+	term_init();
+
 	kvm = kvm__init(kvm_dev, ram_size);
 
 	if (image_filename) {
tools/kvm/term.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
1.7.4.1
author
#3 lore raw

PATCH 4/6 kvm tools: make 8250 serial use infrastructure provided by term.c

In reply to Asias He
---
 tools/kvm/8250-serial.c             |   40 ++++++----------------------------
 tools/kvm/include/kvm/8250-serial.h |    2 +-
 tools/kvm/main.c                    |    2 +-
 3 files changed, 9 insertions(+), 35 deletions(-)
tools/kvm/8250-serial.c +7 -33
index 939e9f5..e92ff89 100644
@@ -3,12 +3,11 @@
 #include "kvm/read-write.h"
 #include "kvm/ioport.h"
 #include "kvm/util.h"
+#include "kvm/term.h"
 #include "kvm/kvm.h"
 
 #include <linux/serial_reg.h>
 
-#include <stdbool.h>
-#include <poll.h>
 
 struct serial8250_device {
 	uint16_t		iobase;
@@ -54,26 +53,6 @@ static struct serial8250_device devices[] = {
 	},
 };
 
-static int read_char(int fd)
-{
-	char c;
-
-	if (read_in_full(fd, &c, 1) == 0)
-		return -1;
-
-	return c;
-}
-
-static bool is_readable(int fd)
-{
-	struct pollfd pollfd = (struct pollfd) {
-		.fd	= fd,
-		.events	= POLLIN,
-	};
-
-	return poll(&pollfd, 1, 0) > 0;
-}
-
 static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)
 {
 	int c;
@@ -81,10 +60,11 @@ static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)
 	if (dev->lsr & UART_LSR_DR)
 		return;
 
-	if (!is_readable(STDIN_FILENO))
+	if (!term_readable(CONSOLE_8250))
 		return;
 
-	c		= read_char(STDIN_FILENO);
+	c		= term_getc(CONSOLE_8250);
+
 	if (c < 0)
 		return;
 
@@ -95,7 +75,7 @@ static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)
 /*
  * Interrupts are injected for ttyS0 only.
  */
-void serial8250__interrupt(struct kvm *self)
+void serial8250__inject_interrupt(struct kvm *self)
 {
 	struct serial8250_device *dev = &devices[0];
 
@@ -170,15 +150,9 @@ static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size
 	} else {
 		switch (offset) {
 		case UART_TX: {
-			char *p = data;
-			int i;
-
+			char *addr = data;
 			if (!(dev->mcr & UART_MCR_LOOP)) {
-				while (count--) {
-					for (i = 0; i < size; i++)
-						fprintf(stdout, "%c", *p++);
-				}
-				fflush(stdout);
+				term_putc(CONSOLE_8250, addr, size * count);
 			}
 			dev->iir		= UART_IIR_NO_INT;
 			break;
tools/kvm/include/kvm/8250-serial.h +1 -1
index 05ee096..73ed078 100644
@@ -4,6 +4,6 @@
 struct kvm;
 
 void serial8250__init(struct kvm *kvm);
-void serial8250__interrupt(struct kvm *kvm);
+void serial8250__inject_interrupt(struct kvm *kvm);
 
 #endif /* KVM__8250_SERIAL_H */
tools/kvm/main.c +1 -1
index 46fb9fd..1594535 100644
@@ -181,7 +181,7 @@ int main(int argc, char *argv[])
 			break;
 		}
 		case KVM_EXIT_INTR: {
-			serial8250__interrupt(kvm);
+			serial8250__inject_interrupt(kvm);
 			break;
 		}
 		case KVM_EXIT_SHUTDOWN:
Signature · 1 line
1.7.4.1
author
#4 lore raw

PATCH 5/6 kvm tools: add virtio console support v2

In reply to Asias He

- consolidate all console related code to term.c
- use timer based approach instead of thread to deal with user input

NOTE:
Please add something like this:

    T2:23:respawn:/sbin/getty -L hvc0 9600 vt100

to your /etc/inittab file to get a virtio console login.

---
 tools/kvm/Makefile                     |    1 +
 tools/kvm/console-virtio.c             |  218 ++++++++++++++++++++++++++++++++
 tools/kvm/include/kvm/console-virtio.h |    9 ++
 tools/kvm/main.c                       |    5 +
 4 files changed, 233 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/console-virtio.c
 create mode 100644 tools/kvm/include/kvm/console-virtio.h
tools/kvm/Makefile +1 -0
index 0244f9b..55f342d 100644
@@ -14,6 +14,7 @@ TAGS = ctags
 
 OBJS	+= 8250-serial.o
 OBJS	+= blk-virtio.o
+OBJS	+= console-virtio.o
 OBJS	+= cpuid.o
 OBJS	+= read-write.o
 OBJS	+= disk-image.o
tools/kvm/console-virtio.c new file +218 -0

218 lines not on this page · raw message

tools/kvm/include/kvm/console-virtio.h new file +9 -0
new file mode 100644
index 0000000..d2e5d19
@@ -0,0 +1,9 @@
+#ifndef KVM__CONSOLE_VIRTIO_H
+#define KVM__CONSOLE_VIRTIO_H
+
+struct kvm;
+
+void virtio_console__init(struct kvm *self);
+void virtio_console__inject_interrupt(struct kvm *self);
+
+#endif /* KVM__CONSOLE_VIRTIO_H */
tools/kvm/main.c +5 -0
index 1594535..90906d2 100644
@@ -2,6 +2,7 @@
 
 #include "kvm/8250-serial.h"
 #include "kvm/blk-virtio.h"
+#include "kvm/console-virtio.h"
 #include "kvm/disk-image.h"
 #include "kvm/util.h"
 #include "kvm/pci.h"
@@ -139,10 +140,13 @@ int main(int argc, char *argv[])
 		kvm__enable_singlestep(kvm);
 
 	serial8250__init(kvm);
+
 	pci__init();
 
 	blk_virtio__init(kvm);
 
+	virtio_console__init(kvm);
+
 	kvm__start_timer(kvm);
 
 	for (;;) {
@@ -182,6 +186,7 @@ int main(int argc, char *argv[])
 		}
 		case KVM_EXIT_INTR: {
 			serial8250__inject_interrupt(kvm);
+			virtio_console__inject_interrupt(kvm);
 			break;
 		}
 		case KVM_EXIT_SHUTDOWN:
Signature · 1 line
1.7.4.1
author
#5 lore raw

PATCH 6/6 kvm tools: introduce --enable-virtio-console option

In reply to Asias He

This option enables virtio console and disables serial console. At this time, the virtio and serial console can not work simultaneously. Becasue we can not put both console's output to user at the same time. However, once we redirect the virtio console to a pipe or socket. We can use both console simultaneously.

---
 tools/kvm/main.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
tools/kvm/main.c +5 -1
index 90906d2..5bcefde 100644
@@ -17,11 +17,12 @@
 #include <stdio.h>
 
 extern bool ioport_debug;
+extern int active_console;
 
 static void usage(char *argv[])
 {
 	fprintf(stderr, "  usage: %s "
-		"[--single-step] [--ioport-debug] "
+		"[--single-step] [--ioport-debug] [--enable-virtio-console] "
 		"[--kvm-dev=<device>] [--mem=<size-in-MiB>] [--params=<kernel-params>] "
 		"[--initrd=<initrd>] [--kernel=]<kernel-image> [--image=]<disk-image>\n",
 		argv[0]);
@@ -85,6 +86,9 @@ int main(int argc, char *argv[])
 		} else if (option_matches(argv[i], "--single-step")) {
 			single_step	= true;
 			continue;
+		} else if (option_matches(argv[i], "--enable-virtio-console")) {
+			active_console	= CONSOLE_VIRTIO;
+			continue;
 		} else if (option_matches(argv[i], "--mem=")) {
 			unsigned long val = atol(&argv[i][6]) << 20;
 			if (val < ram_size)
Signature · 1 line
1.7.4.1