* Re: [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number
2011-04-08 2:05 [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number Asias He
@ 2011-04-07 3:56 ` David Ahern
2011-04-07 4:40 ` Pekka Enberg
2011-04-08 2:05 ` [PATCH 2/3] kvm tools: add some helper functions for virt_queue Asias He
2011-04-08 2:05 ` [PATCH 3/3] kvm tools: add virtio console support v2 Asias He
2 siblings, 1 reply; 8+ messages in thread
From: David Ahern @ 2011-04-07 3:56 UTC (permalink / raw)
To: Asias He, Pekka Enberg, Cyrill Gorcunov; +Cc: Ingo Molnar, Prasad Joshi, kvm
Interesting overload of the kvm mailing list. Intended for changes to
the 'native kvm' (ie., tools/kvm)?
David
On 04/07/11 20:05, Asias He wrote:
> Signed-off-by: Asias He <asias.hejun@gmail.com>
> ---
> tools/kvm/blk-virtio.c | 6 ++++--
> tools/kvm/include/kvm/ioport.h | 7 +++++--
> 2 files changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/tools/kvm/blk-virtio.c b/tools/kvm/blk-virtio.c
> index 48211b5..9a723cc 100644
> --- a/tools/kvm/blk-virtio.c
> +++ b/tools/kvm/blk-virtio.c
> @@ -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);
> }
> diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h
> index 7b6af53..b77f565 100644
> --- a/tools/kvm/include/kvm/ioport.h
> +++ b/tools/kvm/include/kvm/ioport.h
> @@ -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;
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number
2011-04-07 3:56 ` David Ahern
@ 2011-04-07 4:40 ` Pekka Enberg
2011-04-07 7:38 ` Avi Kivity
0 siblings, 1 reply; 8+ messages in thread
From: Pekka Enberg @ 2011-04-07 4:40 UTC (permalink / raw)
To: David Ahern
Cc: Asias He, Cyrill Gorcunov, Ingo Molnar, Prasad Joshi, kvm,
Avi Kivity
On Thu, Apr 7, 2011 at 6:56 AM, David Ahern <dsahern@gmail.com> wrote:
> Interesting overload of the kvm mailing list. Intended for changes to
> the 'native kvm' (ie., tools/kvm)?
Yup, we're starting to use kvm@ for our patches and discussion as
suggested by Avi.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] kvm tools: add virtio console support v2
2011-04-08 2:05 ` [PATCH 3/3] kvm tools: add virtio console support v2 Asias He
@ 2011-04-07 5:50 ` Pekka Enberg
2011-04-08 14:18 ` Asias He
0 siblings, 1 reply; 8+ messages in thread
From: Pekka Enberg @ 2011-04-07 5:50 UTC (permalink / raw)
To: Asias He; +Cc: Cyrill Gorcunov, Ingo Molnar, Prasad Joshi, kvm
Hi Asias,
On 4/8/11 5:05 AM, Asias He wrote:
> - Consolidate all console related code to term.c
> - Use timer based approach instead of thread to deal with user input
> - Introduce --enable-virtio-console option, this option disables
> serial console output
>
> 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.
>
> Signed-off-by: Asias He<asias.hejun@gmail.com>
> ---
> tools/kvm/8250-serial.c | 40 +-----
> tools/kvm/Makefile | 2 +
> tools/kvm/console-virtio.c | 218 ++++++++++++++++++++++++++++++++
> tools/kvm/include/kvm/8250-serial.h | 2 +-
> tools/kvm/include/kvm/console-virtio.h | 9 ++
> tools/kvm/include/kvm/term.h | 17 +++
> tools/kvm/main.c | 48 ++-----
> tools/kvm/term.c | 89 +++++++++++++
Is it too much trouble to ask you to split this patch into two: (1) the
term.c refactoring and (2) virtio console implementation? That would
make the series much easier to review.
Pekka
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number
2011-04-07 4:40 ` Pekka Enberg
@ 2011-04-07 7:38 ` Avi Kivity
0 siblings, 0 replies; 8+ messages in thread
From: Avi Kivity @ 2011-04-07 7:38 UTC (permalink / raw)
To: Pekka Enberg
Cc: David Ahern, Asias He, Cyrill Gorcunov, Ingo Molnar, Prasad Joshi,
kvm
On 04/07/2011 07:40 AM, Pekka Enberg wrote:
> On Thu, Apr 7, 2011 at 6:56 AM, David Ahern<dsahern@gmail.com> wrote:
> > Interesting overload of the kvm mailing list. Intended for changes to
> > the 'native kvm' (ie., tools/kvm)?
>
> Yup, we're starting to use kvm@ for our patches and discussion as
> suggested by Avi.
Right, if the load becomes too high we can split it off to a separate list.
--
error compiling committee.c: too many arguments to function
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number
@ 2011-04-08 2:05 Asias He
2011-04-07 3:56 ` David Ahern
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Asias He @ 2011-04-08 2:05 UTC (permalink / raw)
To: Pekka Enberg, Cyrill Gorcunov; +Cc: Ingo Molnar, Prasad Joshi, kvm, Asias He
Signed-off-by: Asias He <asias.hejun@gmail.com>
---
tools/kvm/blk-virtio.c | 6 ++++--
tools/kvm/include/kvm/ioport.h | 7 +++++--
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/tools/kvm/blk-virtio.c b/tools/kvm/blk-virtio.c
index 48211b5..9a723cc 100644
--- a/tools/kvm/blk-virtio.c
+++ b/tools/kvm/blk-virtio.c
@@ -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);
}
diff --git a/tools/kvm/include/kvm/ioport.h b/tools/kvm/include/kvm/ioport.h
index 7b6af53..b77f565 100644
--- a/tools/kvm/include/kvm/ioport.h
+++ b/tools/kvm/include/kvm/ioport.h
@@ -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;
--
1.7.4.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/3] kvm tools: add some helper functions for virt_queue
2011-04-08 2:05 [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number Asias He
2011-04-07 3:56 ` David Ahern
@ 2011-04-08 2:05 ` Asias He
2011-04-08 2:05 ` [PATCH 3/3] kvm tools: add virtio console support v2 Asias He
2 siblings, 0 replies; 8+ messages in thread
From: Asias He @ 2011-04-08 2:05 UTC (permalink / raw)
To: Pekka Enberg, Cyrill Gorcunov; +Cc: Ingo Molnar, Prasad Joshi, kvm, Asias He
Signed-off-by: Asias He <asias.hejun@gmail.com>
---
tools/kvm/include/kvm/virtqueue.h | 42 +++++++++++++++++++++++++++++++++++++
1 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/tools/kvm/include/kvm/virtqueue.h b/tools/kvm/include/kvm/virtqueue.h
index 83f5293..de360ae 100644
--- a/tools/kvm/include/kvm/virtqueue.h
+++ b/tools/kvm/include/kvm/virtqueue.h
@@ -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 */
--
1.7.4.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/3] kvm tools: add virtio console support v2
2011-04-08 2:05 [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number Asias He
2011-04-07 3:56 ` David Ahern
2011-04-08 2:05 ` [PATCH 2/3] kvm tools: add some helper functions for virt_queue Asias He
@ 2011-04-08 2:05 ` Asias He
2011-04-07 5:50 ` Pekka Enberg
2 siblings, 1 reply; 8+ messages in thread
From: Asias He @ 2011-04-08 2:05 UTC (permalink / raw)
To: Pekka Enberg, Cyrill Gorcunov; +Cc: Ingo Molnar, Prasad Joshi, kvm, Asias He
- Consolidate all console related code to term.c
- Use timer based approach instead of thread to deal with user input
- Introduce --enable-virtio-console option, this option disables
serial console output
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.
Signed-off-by: Asias He <asias.hejun@gmail.com>
---
tools/kvm/8250-serial.c | 40 +-----
tools/kvm/Makefile | 2 +
tools/kvm/console-virtio.c | 218 ++++++++++++++++++++++++++++++++
tools/kvm/include/kvm/8250-serial.h | 2 +-
tools/kvm/include/kvm/console-virtio.h | 9 ++
tools/kvm/include/kvm/term.h | 17 +++
tools/kvm/main.c | 48 ++-----
tools/kvm/term.c | 89 +++++++++++++
8 files changed, 357 insertions(+), 68 deletions(-)
create mode 100644 tools/kvm/console-virtio.c
create mode 100644 tools/kvm/include/kvm/console-virtio.h
create mode 100644 tools/kvm/include/kvm/term.h
create mode 100644 tools/kvm/term.c
diff --git a/tools/kvm/8250-serial.c b/tools/kvm/8250-serial.c
index 939e9f5..e92ff89 100644
--- a/tools/kvm/8250-serial.c
+++ b/tools/kvm/8250-serial.c
@@ -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;
diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile
index 747aa51..55f342d 100644
--- a/tools/kvm/Makefile
+++ b/tools/kvm/Makefile
@@ -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
@@ -24,6 +25,7 @@ OBJS += main.o
OBJS += mmio.o
OBJS += pci.o
OBJS += util.o
+OBJS += term.o
DEPS := $(patsubst %.o,%.d,$(OBJS))
diff --git a/tools/kvm/console-virtio.c b/tools/kvm/console-virtio.c
new file mode 100644
index 0000000..366d0ab
--- /dev/null
+++ b/tools/kvm/console-virtio.c
@@ -0,0 +1,218 @@
+#include "kvm/console-virtio.h"
+#include "kvm/virtio_pci.h"
+#include "kvm/disk-image.h"
+#include "kvm/virtqueue.h"
+#include "kvm/ioport.h"
+#include "kvm/util.h"
+#include "kvm/term.h"
+#include "kvm/kvm.h"
+#include "kvm/pci.h"
+
+#include <linux/virtio_console.h>
+#include <linux/virtio_ring.h>
+#include <linux/virtio_blk.h>
+
+#include <sys/uio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <inttypes.h>
+#include <termios.h>
+#include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define VIRTIO_CONSOLE_IRQ 14
+#define VIRTIO_CONSOLE_QUEUE_SIZE 128
+#define VIRTIO_CONSOLE_NUM_QUEUES 2
+#define VIRTIO_CONSOLE_RX_QUEUE 0
+#define VIRTIO_CONSOLE_TX_QUEUE 1
+#define PCI_VIRTIO_CONSOLE_DEVNUM 2
+
+struct console_device {
+ struct virt_queue vqs[VIRTIO_CONSOLE_NUM_QUEUES];
+ struct virtio_console_config console_config;
+ uint32_t host_features;
+ uint32_t guest_features;
+ uint16_t config_vector;
+ uint8_t status;
+ uint16_t queue_selector;
+};
+
+static struct console_device console_device = {
+ .console_config = {
+ .cols = 80,
+ .rows = 24,
+ .max_nr_ports = 1,
+ },
+
+ .host_features = 0,
+};
+
+/*
+ * Interrupts are injected for hvc0 only.
+ */
+void virtio_console__inject_interrupt(struct kvm *self)
+{
+ struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE];
+ struct virt_queue *vq;
+ uint16_t out, in;
+ uint16_t head;
+ int len;
+
+ vq = &console_device.vqs[VIRTIO_CONSOLE_RX_QUEUE];
+
+ if (term_readable(CONSOLE_VIRTIO) && virt_queue__available(vq)) {
+ head = virt_queue__get_iov(vq, iov, &out, &in, self);
+ len = term_getc_iov(CONSOLE_VIRTIO, iov, in);
+ virt_queue__set_used_elem(vq, head, len);
+ kvm__irq_line(self, VIRTIO_CONSOLE_IRQ, 1);
+ }
+}
+
+static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long offset, int size, uint32_t count)
+{
+ uint8_t *config_space = (uint8_t *) &console_device.console_config;
+
+ if (size != 1 || count != 1)
+ return false;
+
+ if ((offset - VIRTIO_PCI_CONFIG_NOMSI) > sizeof(struct virtio_console_config))
+ error("config offset is too big: %li", offset - VIRTIO_PCI_CONFIG_NOMSI);
+
+ ioport__write8(data, config_space[offset - VIRTIO_PCI_CONFIG_NOMSI]);
+
+ return true;
+}
+
+static bool virtio_console_pci_io_in(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
+{
+ unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
+
+ switch (offset) {
+ case VIRTIO_PCI_HOST_FEATURES:
+ ioport__write32(data, console_device.host_features);
+ break;
+ case VIRTIO_PCI_GUEST_FEATURES:
+ return false;
+ case VIRTIO_PCI_QUEUE_PFN:
+ ioport__write32(data, console_device.vqs[console_device.queue_selector].pfn);
+ break;
+ case VIRTIO_PCI_QUEUE_NUM:
+ ioport__write16(data, VIRTIO_CONSOLE_QUEUE_SIZE);
+ break;
+ case VIRTIO_PCI_QUEUE_SEL:
+ case VIRTIO_PCI_QUEUE_NOTIFY:
+ return false;
+ case VIRTIO_PCI_STATUS:
+ ioport__write8(data, console_device.status);
+ break;
+ case VIRTIO_PCI_ISR:
+ ioport__write8(data, 0x1);
+ kvm__irq_line(self, VIRTIO_CONSOLE_IRQ, 0);
+ break;
+ case VIRTIO_MSI_CONFIG_VECTOR:
+ ioport__write16(data, console_device.config_vector);
+ break;
+ default:
+ return virtio_console_pci_io_device_specific_in(data, offset, size, count);
+ };
+
+ return true;
+}
+
+static void virtio_console_handle_callback(struct kvm *self, uint16_t queue_index)
+{
+ struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE];
+ struct virt_queue *vq;
+ uint16_t out, in;
+ uint16_t head;
+ uint32_t len;
+
+ vq = &console_device.vqs[queue_index];
+
+ if (queue_index == VIRTIO_CONSOLE_TX_QUEUE) {
+
+ while (virt_queue__available(vq)) {
+ head = virt_queue__get_iov(vq, iov, &out, &in, self);
+ len = term_putc_iov(CONSOLE_VIRTIO, iov, out);
+ virt_queue__set_used_elem(vq, head, len);
+ }
+
+ kvm__irq_line(self, VIRTIO_CONSOLE_IRQ, 1);
+ }
+}
+
+static bool virtio_console_pci_io_out(struct kvm *self, uint16_t port, void *data, int size, uint32_t count)
+{
+ unsigned long offset = port - IOPORT_VIRTIO_CONSOLE;
+
+ switch (offset) {
+ case VIRTIO_PCI_GUEST_FEATURES:
+ console_device.guest_features = ioport__read32(data);
+ break;
+ case VIRTIO_PCI_QUEUE_PFN: {
+ struct virt_queue *queue;
+ void *p;
+
+ assert(console_device.queue_selector < VIRTIO_CONSOLE_NUM_QUEUES);
+
+ queue = &console_device.vqs[console_device.queue_selector];
+ queue->pfn = ioport__read32(data);
+ p = guest_flat_to_host(self, queue->pfn << 12);
+
+ vring_init(&queue->vring, VIRTIO_CONSOLE_QUEUE_SIZE, p, 4096);
+
+ break;
+ }
+ case VIRTIO_PCI_QUEUE_SEL:
+ console_device.queue_selector = ioport__read16(data);
+ break;
+ case VIRTIO_PCI_QUEUE_NOTIFY: {
+ uint16_t queue_index;
+ queue_index = ioport__read16(data);
+ virtio_console_handle_callback(self, queue_index);
+ break;
+ }
+ case VIRTIO_PCI_STATUS:
+ console_device.status = ioport__read8(data);
+ break;
+ case VIRTIO_MSI_CONFIG_VECTOR:
+ console_device.config_vector = VIRTIO_MSI_NO_VECTOR;
+ break;
+ case VIRTIO_MSI_QUEUE_VECTOR:
+ break;
+ default:
+ return false;
+ };
+
+ return true;
+}
+
+static struct ioport_operations virtio_console_io_ops = {
+ .io_in = virtio_console_pci_io_in,
+ .io_out = virtio_console_pci_io_out,
+};
+
+#define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4
+#define PCI_DEVICE_ID_VIRTIO_CONSOLE 0x1002
+#define PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET 0x1af4
+#define PCI_SUBSYSTEM_ID_VIRTIO_CONSOLE 0x0003
+
+static struct pci_device_header virtio_console_pci_device = {
+ .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
+ .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
+ .header_type = PCI_HEADER_TYPE_NORMAL,
+ .revision_id = 0,
+ .class = (0x07 << 8) | (0x80 << 4) | (0x0 << 0),
+ .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
+ .subsys_id = PCI_SUBSYSTEM_ID_VIRTIO_CONSOLE,
+ .bar[0] = IOPORT_VIRTIO_CONSOLE | PCI_BASE_ADDRESS_SPACE_IO,
+ .irq_pin = 3,
+ .irq_line = VIRTIO_CONSOLE_IRQ,
+};
+
+void virtio_console__init(struct kvm *self)
+{
+ pci__register(&virtio_console_pci_device, PCI_VIRTIO_CONSOLE_DEVNUM);
+ ioport__register(IOPORT_VIRTIO_CONSOLE, &virtio_console_io_ops, IOPORT_VIRTIO_CONSOLE_SIZE);
+}
diff --git a/tools/kvm/include/kvm/8250-serial.h b/tools/kvm/include/kvm/8250-serial.h
index 05ee096..73ed078 100644
--- a/tools/kvm/include/kvm/8250-serial.h
+++ b/tools/kvm/include/kvm/8250-serial.h
@@ -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 */
diff --git a/tools/kvm/include/kvm/console-virtio.h b/tools/kvm/include/kvm/console-virtio.h
new file mode 100644
index 0000000..d2e5d19
--- /dev/null
+++ b/tools/kvm/include/kvm/console-virtio.h
@@ -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 */
diff --git a/tools/kvm/include/kvm/term.h b/tools/kvm/include/kvm/term.h
new file mode 100644
index 0000000..4d580e1
--- /dev/null
+++ b/tools/kvm/include/kvm/term.h
@@ -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 */
diff --git a/tools/kvm/main.c b/tools/kvm/main.c
index 7beb6a6..5bcefde 100644
--- a/tools/kvm/main.c
+++ b/tools/kvm/main.c
@@ -2,12 +2,13 @@
#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"
+#include "kvm/term.h"
#include <inttypes.h>
-#include <termios.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
@@ -16,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]);
@@ -29,32 +31,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 +41,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 +67,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];
@@ -115,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)
@@ -138,6 +112,8 @@ int main(int argc, char *argv[])
if (!kernel_filename)
usage(argv);
+ term_init();
+
kvm = kvm__init(kvm_dev, ram_size);
if (image_filename) {
@@ -168,10 +144,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 (;;) {
@@ -210,7 +189,8 @@ int main(int argc, char *argv[])
break;
}
case KVM_EXIT_INTR: {
- serial8250__interrupt(kvm);
+ serial8250__inject_interrupt(kvm);
+ virtio_console__inject_interrupt(kvm);
break;
}
case KVM_EXIT_SHUTDOWN:
diff --git a/tools/kvm/term.c b/tools/kvm/term.c
new file mode 100644
index 0000000..0ac53c0
--- /dev/null
+++ b/tools/kvm/term.c
@@ -0,0 +1,89 @@
+#include <poll.h>
+#include <stdbool.h>
+#include <termios.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/uio.h>
+
+#include "kvm/read-write.h"
+#include "kvm/term.h"
+#include "kvm/util.h"
+
+static struct termios orig_term;
+
+int active_console = CONSOLE_8250;
+
+int term_getc(int who)
+{
+ int c;
+
+ if (who != active_console)
+ return -1;
+
+ if (read_in_full(STDIN_FILENO, &c, 1) < 0)
+ return -1;
+ return c;
+}
+
+int term_putc(int who, char *addr, int cnt)
+{
+ if (who != active_console)
+ return -1;
+
+ while (cnt--) {
+ fprintf(stdout, "%c", *addr++);
+ }
+
+ fflush(stdout);
+ return cnt;
+}
+
+int term_getc_iov(int who, struct iovec *iov, int iovcnt)
+{
+ if (who != active_console)
+ return -1;
+
+ return readv(STDIN_FILENO, iov, iovcnt);
+}
+
+int term_putc_iov(int who, struct iovec *iov, int iovcnt)
+{
+ if (who != active_console)
+ return -1;
+
+ return writev(STDOUT_FILENO, iov, iovcnt);
+}
+
+bool term_readable(int who)
+{
+ struct pollfd pollfd = (struct pollfd) {
+ .fd = STDIN_FILENO,
+ .events = POLLIN,
+ .revents = 0,
+ };
+
+ if (who != active_console)
+ return false;
+
+ return poll(&pollfd, 1, 0) > 0;
+}
+
+static void term_cleanup(void)
+{
+ tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
+}
+
+void term_init(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 | ISIG);
+ tcsetattr(STDIN_FILENO, TCSANOW, &term);
+
+ atexit(term_cleanup);
+}
+
--
1.7.4.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3] kvm tools: add virtio console support v2
2011-04-07 5:50 ` Pekka Enberg
@ 2011-04-08 14:18 ` Asias He
0 siblings, 0 replies; 8+ messages in thread
From: Asias He @ 2011-04-08 14:18 UTC (permalink / raw)
To: Pekka Enberg; +Cc: Cyrill Gorcunov, Ingo Molnar, Prasad Joshi, kvm
On 04/07/2011 01:50 PM, Pekka Enberg wrote:
> Hi Asias,
>
> On 4/8/11 5:05 AM, Asias He wrote:
>> - Consolidate all console related code to term.c
>> - Use timer based approach instead of thread to deal with user input
>> - Introduce --enable-virtio-console option, this option disables
>> serial console output
>>
>> 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.
>>
>> Signed-off-by: Asias He<asias.hejun@gmail.com>
>> ---
>> tools/kvm/8250-serial.c | 40 +-----
>> tools/kvm/Makefile | 2 +
>> tools/kvm/console-virtio.c | 218
>> ++++++++++++++++++++++++++++++++
>> tools/kvm/include/kvm/8250-serial.h | 2 +-
>> tools/kvm/include/kvm/console-virtio.h | 9 ++
>> tools/kvm/include/kvm/term.h | 17 +++
>> tools/kvm/main.c | 48 ++-----
>> tools/kvm/term.c | 89 +++++++++++++
>
> Is it too much trouble to ask you to split this patch into two: (1) the
> term.c refactoring and (2) virtio console implementation? That would
> make the series much easier to review.
>
Hi, Pekka
I have split the patch and sent it out.
--
Best Regards,
Asias He
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-04-07 14:19 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-08 2:05 [PATCH 1/3] kvm tools: define marcros for virtio device io space size and pci device number Asias He
2011-04-07 3:56 ` David Ahern
2011-04-07 4:40 ` Pekka Enberg
2011-04-07 7:38 ` Avi Kivity
2011-04-08 2:05 ` [PATCH 2/3] kvm tools: add some helper functions for virt_queue Asias He
2011-04-08 2:05 ` [PATCH 3/3] kvm tools: add virtio console support v2 Asias He
2011-04-07 5:50 ` Pekka Enberg
2011-04-08 14:18 ` Asias He
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox