[PATCH] 1/2: Fall back to plain Qemu if KVM is not present, report errors from libkvm

Simon Kagstrom
2006-11-14T10:30:09Z

[-- Attachment #1: Type: text/plain, Size: 303 bytes --]

This patch to libkvm returns error values from kvm_init and
kvm_create. It also adds a kvm_finalize to close /dev/kvm and cleanup
the kvm context.

Error messages are now printed on stderr and handle_io has been made
static.

Signed-off-by: Simon Kagstrom <simon.kagstrom-6Uc+yI3S9P8@public.gmane.org>

[-- Attachment #2: libkvm-report-errors.patch --]
[-- Type: application/octet-stream, Size: 5835 bytes --]

diff -u ../orig/kvm-2/user/kvmctl.c user/kvmctl.c
--- ../orig/kvm-2/user/kvmctl.c 2006-10-09 15:24:53.000000000 +0200
+++ user/kvmctl.c 2006-11-14 11:27:09.000000000 +0100
@@ -61,8 +61,8 @@

fd = open("/dev/kvm", O_RDWR);
if (fd == -1) {
- printf("open: %m\n");
- exit(1);
+ perror("open");
+ return NULL;
}
kvm = malloc(sizeof(*kvm));
kvm->fd = fd;
@@ -71,6 +71,12 @@
return kvm;
}

+void kvm_finalize(kvm_context_t kvm)
+{
+ close(kvm->fd);
+ free(kvm);
+}
+
int kvm_create(kvm_context_t kvm, unsigned long memory, void **vm_mem)
{
unsigned long dosmem = 0xa0000;
@@ -91,28 +97,28 @@
/* 640K should be enough. */
r = ioctl(fd, KVM_SET_MEMORY_REGION, &low_memory);
if (r == -1) {
- printf("kvm_create_memory_region: %m\n");
- exit(1);
+ fprintf(stderr, "kvm_create_memory_region: %m\n");
+ return -1;
}
if (extended_memory.memory_size) {
r = ioctl(fd, KVM_SET_MEMORY_REGION, &extended_memory);
if (r == -1) {
- printf("kvm_create_memory_region: %m\n");
- exit(1);
+ fprintf(stderr, "kvm_create_memory_region: %m\n");
+ return -1;
}
}

*vm_mem = mmap(0, memory, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (*vm_mem == MAP_FAILED) {
- printf("mmap: %m\n");
- exit(1);
+ fprintf(stderr, "mmap: %m\n");
+ return -1;
}
kvm->physical_memory = *vm_mem;

r = ioctl(fd, KVM_CREATE_VCPU, 0);
if (r == -1) {
- printf("kvm_create_vcpu: %m\n");
- exit(1);
+ fprintf(stderr, "kvm_create_vcpu: %m\n");
+ return -1;
}
return 0;
}
@@ -174,7 +180,7 @@
return run->io.count != 0;
}

-int handle_io(kvm_context_t kvm, struct kvm_run *run)
+static int handle_io(kvm_context_t kvm, struct kvm_run *run)
{
uint16_t addr = run->io.port;
struct kvm_regs regs;
@@ -199,8 +205,8 @@
r = translate(kvm, run->vcpu, &tr, run->io.address,
&value_addr);
if (r) {
- printf("failed translating I/O address %x\n",
- run->io.address);
+ fprintf(stderr, "failed translating I/O address %x\n",
+ run->io.address);
exit(1);
}
}
@@ -227,7 +233,7 @@
break;
}
default:
- printf("bad I/O size\n");
+ fprintf(stderr, "bad I/O size\n");
exit(1);
}
break;
@@ -247,12 +253,12 @@
*(uint32_t *)value_addr);
break;
default:
- printf("bad I/O size\n");
+ fprintf(stderr, "bad I/O size\n");
exit(1);
}
break;
default:
- printf("bad I/O size\n");
+ fprintf(stderr, "bad I/O size\n");
exit(1);
}
if (run->io.string) {
@@ -319,16 +325,17 @@
perror("KVM_GET_REGS");
exit(1);
}
- printf("rax %016llx rbx %016llx rcx %016llx rdx %016llx\n"
- "rsi %016llx rdi %016llx rsp %016llx rbp %016llx\n"
- "r8 %016llx r9 %016llx r10 %016llx r11 %016llx\n"
- "r12 %016llx r13 %016llx r14 %016llx r15 %016llx\n"
- "rip %016llx rflags %08llx\n",
- regs.rax, regs.rbx, regs.rcx, regs.rdx,
- regs.rsi, regs.rdi, regs.rsp, regs.rbp,
- regs.r8, regs.r9, regs.r10, regs.r11,
- regs.r12, regs.r13, regs.r14, regs.r15,
- regs.rip, regs.rflags);
+ fprintf(stderr,
+ "rax %016llx rbx %016llx rcx %016llx rdx %016llx\n"
+ "rsi %016llx rdi %016llx rsp %016llx rbp %016llx\n"
+ "r8 %016llx r9 %016llx r10 %016llx r11 %016llx\n"
+ "r12 %016llx r13 %016llx r14 %016llx r15 %016llx\n"
+ "rip %016llx rflags %08llx\n",
+ regs.rax, regs.rbx, regs.rcx, regs.rdx,
+ regs.rsi, regs.rdi, regs.rsp, regs.rbp,
+ regs.r8, regs.r9, regs.r10, regs.r11,
+ regs.r12, regs.r13, regs.r14, regs.r15,
+ regs.rip, regs.rflags);
}

static int handle_cpuid(kvm_context_t kvm, struct kvm_run *run)
@@ -423,20 +430,20 @@
}
switch (kvm_run.exit_type) {
case KVM_EXIT_TYPE_FAIL_ENTRY:
- printf("kvm_run: failed entry, reason %u\n",
- kvm_run.exit_reason & 0xffff);
+ fprintf(stderr, "kvm_run: failed entry, reason %u\n",
+ kvm_run.exit_reason & 0xffff);
exit(1);
break;
case KVM_EXIT_TYPE_VM_EXIT:
switch (kvm_run.exit_reason) {
case KVM_EXIT_UNKNOWN:
- printf("unhandled vm exit: 0x%x\n",
+ fprintf(stderr, "unhandled vm exit: 0x%x\n",
kvm_run.hw.hardware_exit_reason);
kvm_show_regs(kvm, vcpu);
abort();
break;
case KVM_EXIT_EXCEPTION:
- printf("exception %d (%x)\n",
+ fprintf(stderr, "exception %d (%x)\n",
kvm_run.ex.exception,
kvm_run.ex.error_code);
abort();
@@ -457,7 +464,7 @@
r = handle_halt(kvm, &kvm_run);
break;
default:
- printf("unhandled vm exit: 0x%x\n", kvm_run.exit_reason);
+ fprintf(stderr, "unhandled vm exit: 0x%x\n", kvm_run.exit_reason);
kvm_show_regs(kvm, vcpu);
abort();
break;
diff -u ../orig/kvm-2/user/kvmctl.h user/kvmctl.h
--- ../orig/kvm-2/user/kvmctl.h 2006-10-22 11:39:42.000000000 +0200
+++ user/kvmctl.h 2006-11-14 11:02:54.000000000 +0100
@@ -31,8 +31,11 @@
int (*io_window)(void *opaque);
};

+/* Create a new kvm context */
kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
void *opaque);
+/* Cleanup the kvm context */
+void kvm_finalize(kvm_context_t kvm);
int kvm_create(kvm_context_t kvm,
unsigned long phys_mem_bytes,
void **phys_mem);
diff -u ../orig/kvm-2/user/main.c user/main.c
--- ../orig/kvm-2/user/main.c 2006-10-23 12:48:50.000000000 +0200
+++ user/main.c 2006-11-14 11:13:46.000000000 +0100
@@ -120,7 +120,15 @@
void *vm_mem;

kvm = kvm_init(&test_callbacks, 0);
- kvm_create(kvm, 128 * 1024 * 1024, &vm_mem);
+ if (!kvm) {
+ fprintf(stderr, "kvm_init failed\n");
+ return 1;
+ }
+ if (kvm_create(kvm, 128 * 1024 * 1024, &vm_mem) < 0) {
+ kvm_finalize(kvm);
+ fprintf(stderr, "kvm_create failed\n");
+ return 1;
+ }
if (ac > 1)
load_file(vm_mem + 0xf0000, av[1]);
if (ac > 2)
@@ -128,4 +136,6 @@
kvm_show_regs(kvm, 0);

kvm_run(kvm, 0);
+
+ return 0;
}

[-- Attachment #3: Type: text/plain, Size: 373 bytes --]

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

[-- Attachment #4: Type: text/plain, Size: 186 bytes --]

_______________________________________________
kvm-devel mailing list
kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org