Skip to contentSkip to conversation

kvm tools: Add BIOS INT10 handler

PATCH Superseded Sasha Levin 2 participants 7 messages May 23, 2011 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 kvm tools: Add BIOS INT10 handler

INT10 handler is a basic implementation of BIOS video services.

The handler implements a VESA interface which is initialized at the very beginning of loading the kernel.

---
 tools/kvm/bios/bios-rom.S |   56 ++++++++--------
 tools/kvm/bios/int10.c    |  161 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 189 insertions(+), 28 deletions(-)
 create mode 100644 tools/kvm/bios/int10.c
tools/kvm/bios/bios-rom.S +28 -28
index 8a53dcd..b636cb8 100644
@@ -27,36 +27,36 @@ ENTRY_END(bios_intfake)
  * We ignore bx settings
  */
 ENTRY(bios_int10)
-	test $0x0e, %ah
-	jne 1f
+	pushw	%fs
+	pushl	%es
+	pushl	%edi
+	pushl	%esi
+	pushl	%ebp
+	pushl	%esp
+	pushl	%edx
+	pushl	%ecx
+	pushl	%ebx
+	pushl	%eax
+
+	movl		%esp, %eax
+	/* this is way easier than doing it in assembly */
+	/* just push all the regs and jump to a C handler */
+	call	int10handler
+
+	popl	%eax
+	popl	%ebx
+	popl	%ecx
+	popl	%edx
+	popl	%esp
+	popl	%ebp
+	popl	%esi
+	popl	%edi
+	popl	%es
+	popw	%fs
 
-/*
- * put char in AL at current cursor and
- * increment cursor position
- */
-putchar:
-	stack_swap
-
-	push %fs
-	push %bx
-
-	mov $VGA_RAM_SEG, %bx
-	mov %bx, %fs
-	mov %cs:(cursor), %bx
-	mov %al, %fs:(%bx)
-	inc %bx
-	test $VGA_PAGE_SIZE, %bx
-	jb putchar_new
-	xor %bx, %bx
-putchar_new:
-	mov %bx, %fs:(cursor)
-
-	pop %bx
-	pop %fs
-
-	stack_restore
-1:
 	IRET
+
+
 /*
  * private IRQ data
  */
tools/kvm/bios/int10.c new file +161 -0

161 lines not on this page · raw message

Signature · 1 line
1.7.5.rc3
#1 lore raw

PATCH 2/5 kvm tools: Add video mode to kernel initialization

Allow setting video mode in guest kernel.

For possible values see Documentation/fb/vesafb.txt

---
 tools/kvm/include/kvm/kvm.h |    2 +-
 tools/kvm/kvm.c             |    7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
tools/kvm/include/kvm/kvm.h +1 -1
index 3cf6e6c..49ebd95 100644
@@ -39,7 +39,7 @@ int kvm__max_cpus(struct kvm *kvm);
 void kvm__init_ram(struct kvm *kvm);
 void kvm__delete(struct kvm *kvm);
 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
-			const char *initrd_filename, const char *kernel_cmdline);
+			const char *initrd_filename, const char *kernel_cmdline, u16 vidmode);
 void kvm__setup_bios(struct kvm *kvm);
 void kvm__start_timer(struct kvm *kvm);
 void kvm__stop_timer(struct kvm *kvm);
tools/kvm/kvm.c +4 -3
index 4393a41..7284211 100644
@@ -320,7 +320,7 @@ static int load_flat_binary(struct kvm *kvm, int fd)
 static const char *BZIMAGE_MAGIC	= "HdrS";
 
 static bool load_bzimage(struct kvm *kvm, int fd_kernel,
-			int fd_initrd, const char *kernel_cmdline)
+			int fd_initrd, const char *kernel_cmdline, u16 vidmode)
 {
 	struct boot_params *kern_boot;
 	unsigned long setup_sects;
@@ -383,6 +383,7 @@ static bool load_bzimage(struct kvm *kvm, int fd_kernel,
 	kern_boot->hdr.type_of_loader	= 0xff;
 	kern_boot->hdr.heap_end_ptr	= 0xfe00;
 	kern_boot->hdr.loadflags	|= CAN_USE_HEAP;
+	kern_boot->hdr.vid_mode		= vidmode;
 
 	/*
 	 * Read initrd image into guest memory
@@ -441,7 +442,7 @@ static bool initrd_check(int fd)
 }
 
 bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
-		const char *initrd_filename, const char *kernel_cmdline)
+		const char *initrd_filename, const char *kernel_cmdline, u16 vidmode)
 {
 	bool ret;
 	int fd_kernel = -1, fd_initrd = -1;
@@ -459,7 +460,7 @@ bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename,
 			die("%s is not an initrd", initrd_filename);
 	}
 
-	ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline);
+	ret = load_bzimage(kvm, fd_kernel, fd_initrd, kernel_cmdline, vidmode);
 
 	if (initrd_filename)
 		close(fd_initrd);
Signature · 1 line
1.7.5.rc3
#2 lore raw

PATCH 3/5 kvm tools: Add VESA device

In reply to Sasha Levin

Add a simple VESA device which simply moves a framebuffer from guest kernel to a VNC server.

VESA device PCI code is very similar to virtio-* PCI code.

---
 tools/kvm/hw/vesa.c                    |  106 ++++++++++++++++++++++++++++++++
 tools/kvm/include/kvm/ioport.h         |    2 +
 tools/kvm/include/kvm/vesa.h           |   31 +++++++++
 tools/kvm/include/kvm/virtio-pci-dev.h |    3 +
 4 files changed, 142 insertions(+), 0 deletions(-)
 create mode 100644 tools/kvm/hw/vesa.c
 create mode 100644 tools/kvm/include/kvm/vesa.h
tools/kvm/hw/vesa.c new file +106 -0
new file mode 100644
index 0000000..c1a4c64
@@ -0,0 +1,106 @@
+#include "kvm/vesa.h"
+#include "kvm/ioport.h"
+#include "kvm/util.h"
+#include "kvm/kvm.h"
+#include "kvm/pci.h"
+#include "kvm/kvm-cpu.h"
+#include "kvm/irq.h"
+#include "kvm/virtio-pci-dev.h"
+
+#include <rfb/rfb.h>
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <inttypes.h>
+#include <unistd.h>
+
+#define VESA_QUEUE_SIZE		128
+#define VESA_IRQ		14
+
+/*
+ * This "6000" value is pretty much the result of experimentation
+ * It seems that around this value, things update pretty smoothly
+ */
+#define VESA_UPDATE_TIME	6000
+
+u8 videomem[VESA_MEM_SIZE];
+
+static bool vesa_pci_io_in(struct kvm *kvm, u16 port, void *data, int size, u32 count)
+{
+	printf("vesa in port=%u\n", port);
+	return true;
+}
+
+static bool vesa_pci_io_out(struct kvm *kvm, u16 port, void *data, int size, u32 count)
+{
+	printf("vesa out port=%u\n", port);
+	return true;
+}
+
+static struct ioport_operations vesa_io_ops = {
+	.io_in	= vesa_pci_io_in,
+	.io_out	= vesa_pci_io_out,
+};
+
+static struct pci_device_header vesa_pci_device = {
+	.vendor_id		= PCI_VENDOR_ID_REDHAT_QUMRANET,
+	.device_id		= PCI_DEVICE_ID_VESA,
+	.header_type		= PCI_HEADER_TYPE_NORMAL,
+	.revision_id		= 0,
+	.class			= 0x030000,
+	.subsys_vendor_id	= PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET,
+	.subsys_id		= PCI_SUBSYSTEM_ID_VESA,
+	.bar[0]			= IOPORT_VESA | PCI_BASE_ADDRESS_SPACE_IO,
+	.bar[1]			= VESA_MEM_ADDR | PCI_BASE_ADDRESS_SPACE_MEMORY,
+};
+
+
+void vesa_mmio_callback(u64 addr, u8 *data, u32 len, u8 is_write)
+{
+	if (is_write)
+		memcpy(&videomem[addr - VESA_MEM_ADDR], data, len);
+
+	return;
+}
+
+void vesa__init(struct kvm *kvm)
+{
+	u8 dev, line, pin;
+
+	if (irq__register_device(PCI_DEVICE_ID_VESA, &dev, &pin, &line) < 0)
+		return;
+
+	vesa_pci_device.irq_pin = pin;
+	vesa_pci_device.irq_line = line;
+	pci__register(&vesa_pci_device, dev);
+	ioport__register(IOPORT_VESA, &vesa_io_ops, IOPORT_VESA_SIZE);
+
+	kvm__register_mmio(VESA_MEM_ADDR, VESA_MEM_SIZE, &vesa_mmio_callback);
+}
+
+/*
+ * This starts a VNC server to display the framebuffer.
+ * It's not altogether clear this belongs here rather than in kvm-run.c
+ */
+void *vesa__dovnc(void *v)
+{
+	/*
+	 * Make a fake argc and argv because the getscreen function
+	 * seems to want it.
+	 */
+	int ac = 1;
+	char av[1][1] = {{0} };
+	rfbScreenInfoPtr server;
+
+	server = rfbGetScreen(&ac, (char **)av, VESA_WIDTH, VESA_HEIGHT, 8, 3, 4);
+	server->frameBuffer = (char *)videomem;
+	server->alwaysShared = TRUE;
+	rfbInitServer(server);
+
+	while (rfbIsActive(server)) {
+		rfbMarkRectAsModified(server, 0, 0, VESA_WIDTH, VESA_HEIGHT);
+		rfbProcessEvents(server, server->deferUpdateTime * VESA_UPDATE_TIME);
+	}
+	return NULL;
+}
+
tools/kvm/include/kvm/ioport.h +2 -0
index 218530c..8253938 100644
@@ -7,6 +7,8 @@
 
 /* some ports we reserve for own use */
 #define IOPORT_DBG			0xe0
+#define IOPORT_VESA			0xa200
+#define IOPORT_VESA_SIZE		256
 #define IOPORT_VIRTIO_P9		0xb200	/* Virtio 9P device */
 #define IOPORT_VIRTIO_P9_SIZE		256
 #define IOPORT_VIRTIO_BLK		0xc200	/* Virtio block device */
tools/kvm/include/kvm/vesa.h new file +31 -0
new file mode 100644
index 0000000..dfa3d941
@@ -0,0 +1,31 @@
+#ifndef KVM__VESA_H
+#define KVM__VESA_H
+
+#include <linux/types.h>
+
+#define VESA_WIDTH	640
+#define VESA_HEIGHT	480
+
+#define VESA_MEM_ADDR	0xd0000000
+#define VESA_MEM_SIZE	(4*VESA_WIDTH*VESA_HEIGHT)
+#define VESA_BPP	32
+
+struct kvm;
+struct int10args;
+
+#ifdef CONFIG_HAS_VNCSERVER
+void vesa_mmio_callback(u64, u8*, u32, u8);
+void vesa__init(struct kvm *self);
+void *vesa__dovnc(void *);
+#else
+void vesa__init(struct kvm *self);
+void *vesa__dovnc(void *);
+void vesa__init(struct kvm *self) { }
+void *vesa__dovnc(void *param) { return NULL; }
+#endif
+
+void int10handler(struct int10args *args);
+extern u8 videomem[VESA_MEM_SIZE];
+
+
+#endif
tools/kvm/include/kvm/virtio-pci-dev.h +3 -0
index 1b7862e..ca373df 100644
@@ -13,8 +13,11 @@
 #define PCI_DEVICE_ID_VIRTIO_CONSOLE		0x1003
 #define PCI_DEVICE_ID_VIRTIO_RNG		0x1004
 #define PCI_DEVICE_ID_VIRTIO_P9			0x1009
+#define PCI_DEVICE_ID_VESA			0x2000
 
 #define PCI_VENDOR_ID_REDHAT_QUMRANET		0x1af4
 #define PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET	0x1af4
 
+#define PCI_SUBSYSTEM_ID_VESA			0x0004
+
 #endif /* VIRTIO_PCI_DEV_H_ */
Signature · 1 line
1.7.5.rc3
#3 lore raw

PATCH 4/5 kvm tools: Update makefile and feature tests

In reply to Sasha Levin

Update feature tests to test for libvncserver.

VESA support doesn't get compiled in unless libvncserver is installed.

---
 tools/kvm/Makefile                 |   11 ++++++++++-
 tools/kvm/config/feature-tests.mak |   10 ++++++++++
 2 files changed, 20 insertions(+), 1 deletions(-)
tools/kvm/Makefile +10 -1
index e6e8d4e..2ebc86c 100644
@@ -58,6 +58,14 @@ ifeq ($(has_bfd),y)
 	LIBS	+= -lbfd
 endif
 
+FLAGS_VNCSERVER=$(CFLAGS) -lvncserver
+has_vncserver := $(call try-cc,$(SOURCE_VNCSERVER),$(FLAGS_VNCSERVER))
+ifeq ($(has_vncserver),y)
+	CFLAGS	+= -DCONFIG_HAS_VNCSERVER
+	OBJS	+= hw/vesa.o
+	LIBS	+= -lvncserver
+endif
+
 DEPS	:= $(patsubst %.o,%.d,$(OBJS))
 
 # Exclude BIOS object files from header dependencies.
@@ -153,9 +161,10 @@ bios/bios.o: bios/bios.S bios/bios-rom.bin
 bios/bios-rom.bin: bios/bios-rom.S bios/e820.c
 	$(E) "  CC      " $@
 	$(Q) $(CC) -include code16gcc.h $(CFLAGS) $(BIOS_CFLAGS) -c -s bios/e820.c -o bios/e820.o
+	$(Q) $(CC) -include code16gcc.h $(CFLAGS) $(BIOS_CFLAGS) -c -s bios/int10.c -o bios/int10.o
 	$(Q) $(CC) $(CFLAGS) $(BIOS_CFLAGS) -c -s bios/bios-rom.S -o bios/bios-rom.o
 	$(E) "  LD      " $@
-	$(Q) ld -T bios/rom.ld.S -o bios/bios-rom.bin.elf bios/bios-rom.o bios/e820.o
+	$(Q) ld -T bios/rom.ld.S -o bios/bios-rom.bin.elf bios/bios-rom.o bios/e820.o bios/int10.o
 	$(E) "  OBJCOPY " $@
 	$(Q) objcopy -O binary -j .text bios/bios-rom.bin.elf bios/bios-rom.bin
 	$(E) "  NM      " $@
tools/kvm/config/feature-tests.mak +10 -0
index 6170fd2..0801b54 100644
@@ -126,3 +126,13 @@ int main(void)
 	return 0;
 }
 endef
+
+define SOURCE_VNCSERVER
+#include <rfb/rfb.h>
+
+int main(void)
+{
+	rfbIsActive((void *)0);
+	return 0;
+}
+endef
Signature · 1 line
1.7.5.rc3
#4 lore raw

PATCH 5/5 kvm tools: Initialize and use VESA and VNC

In reply to Sasha Levin

Requirements - Kernel compiled with:
CONFIG_FB_BOOT_VESA_SUPPORT=y
CONFIG_FB_VESA=y
CONFIG_FRAMEBUFFER_CONSOLE=y

Start VNC server by starting kvm tools with "--vnc". Connect to the VNC server by running: "vncviewer :0".

Since there is no support for input devices at this time, it may be useful starting kvm tools with an additional ' -p "console=ttyS0" ' parameter so that it would be possible to use a serial console alongside with a graphic one.

---
 tools/kvm/kvm-run.c |   21 +++++++++++++++++++--
 1 files changed, 19 insertions(+), 2 deletions(-)
tools/kvm/kvm-run.c +19 -2
index f7de0fb..5acddb2 100644
@@ -28,6 +28,7 @@
 #include <kvm/barrier.h>
 #include <kvm/symbol.h>
 #include <kvm/virtio-9p.h>
+#include <kvm/vesa.h>
 
 /* header files for gitish interface  */
 #include <kvm/kvm-run.h>
@@ -67,6 +68,7 @@ static const char *virtio_9p_dir;
 static bool single_step;
 static bool readonly_image[MAX_DISK_IMAGES];
 static bool virtio_rng;
+static bool vnc;
 extern bool ioport_debug;
 extern int  active_console;
 
@@ -111,6 +113,7 @@ static const struct option options[] = {
 	OPT_STRING('\0', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
 	OPT_STRING('\0', "virtio-9p", &virtio_9p_dir, "root dir",
 			"Enable 9p over virtio"),
+	OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"),
 
 	OPT_GROUP("Kernel options:"),
 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
@@ -414,6 +417,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	char *hi;
 	int i;
 	void *ret;
+	u16 vidmode = 0;
 
 	signal(SIGALRM, handle_sigalrm);
 	signal(SIGQUIT, handle_sigquit);
@@ -512,7 +516,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	kvm->nrcpus = nrcpus;
 
 	memset(real_cmdline, 0, sizeof(real_cmdline));
-	strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 console=ttyS0 earlyprintk=serial");
+	strcpy(real_cmdline, "notsc noapic noacpi pci=conf1");
+	if (vnc) {
+		strcat(real_cmdline, " video=vesafb:ypan console=tty0");
+		vidmode = 0x312;
+	} else {
+		strcat(real_cmdline, " console=ttyS0 earlyprintk=serial");
+	}
 	strcat(real_cmdline, " ");
 	if (kernel_cmdline)
 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
@@ -544,7 +554,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	printf("  # kvm run -k %s -m %Lu -c %d\n", kernel_filename, ram_size / 1024 / 1024, nrcpus);
 
 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
-				real_cmdline))
+				real_cmdline, vidmode))
 		die("unable to load kernel %s", kernel_filename);
 
 	kvm->vmlinux		= vmlinux_filename;
@@ -598,6 +608,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	kvm__init_ram(kvm);
 
+	if (vnc) {
+		pthread_t thread;
+
+		vesa__init(kvm);
+		pthread_create(&thread, NULL, vesa__dovnc, kvm);
+	}
+
 	thread_pool__init(nr_online_cpus);
 
 	for (i = 0; i < nrcpus; i++) {
Signature · 1 line
1.7.5.rc3
#5 lore raw

Re: [PATCH 5/5] kvm tools: Initialize and use VESA and VNC

tools/kvm/kvm-run.c int kvm_cmd_run(int argc, const char **argv, const char *prefix) · line 617

Quoted 11 lines · Sasha Levin
tools/kvm/kvm-run.c +7 -0
@@ -598,6 +608,13 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	kvm__init_ram(kvm);
 
+	if (vnc) {
+		pthread_t thread;
+
+		vesa__init(kvm);
+		pthread_create(&thread, NULL, vesa__dovnc, kvm);
+	}
+

This should be encapsulated better, it should probably be all be done within vesa__init() and the only kv_cmd_run() exposure should be:

	vesa__init(kvm);

vesa__init() would wrap to an empty inline function if the library prereqs are not present.

Thanks,

Ingo

#6 lore raw
In reply to Sasha Levin
Quoted 7 lines · Sasha Levin

INT10 handler is a basic implementation of BIOS video services.

The handler implements a VESA interface which is initialized at the very beginning of loading the kernel.

Btw., the signoff chain looks broken - this will look odd in Git.

If you took most of this from John then please put this in the first line of the patch:

From: John Floren <[email protected]>

That way John will be marked by Git as the author and you are the patch maintainer who nursed along the patch.

If you did significant changes to the patch (such as splitting it off a larger patch, cleaning it up, etc.) you can mark this before your SOB entry:

[ split up the patch and cleaned it up ]

If you did so many changes to a patch that you can reasonably be called the main author then you can be the From line and can mark John's first version as:

Originally-From: John Floren <[email protected]>

If John has put copyright notices into the file then those should be preserved, and you can add yours as well, if you so wish.

Thanks,

Ingo