Skip to contentSkip to conversation

qemu-kvm

PATCH New Baruch Even 3 participants 15 messages Nov 14, 2006 12 messages not in the archive KVM

lore
Share & tools
lore
#0 lore raw

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

I'd like to push the KVM patches to Debian Qemu and need to know if they need to create new executables or just continue to build the existing ones. To me it seems the former.

Cheers,
Baruch

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

At Tue, 14 Nov 2006 09:26:01 +0200,

Quoted 4 lines · Baruch Even

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

There is the -no-kvm option which you can pass when starting qemu.

// Simon

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

#3 lore raw
In reply to a message not in the archive
Quoted 4 lines · Baruch Even

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

qemu-kvm.patch is an older release. With the new release, it should be possible to run without kvm. Let me know (or better, send patches) if there are any such places in kvm-2.

Quoted 5 lines

I'd like to push the KVM patches to Debian Qemu and need to know if they need to create new executables or just continue to build the existing ones. To me it seems the former.

Signature · 8 lines
error compiling committee.c: too many arguments to function


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

A message that is not in the archive (sent off-list or not yet received): 4559834F.80905-atKUWr5tajBWk0Htik3J/[email protected]

#5 lore raw

At Tue, 14 Nov 2006 10:50:23 +0200,

Quoted 9 lines · Avi Kivity
Quoted 3 lines · Baruch Even

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

qemu-kvm.patch is an older release. With the new release, it should be possible to run without kvm. Let me know (or better, send patches) if there are any such places in kvm-2.

One thing I've thought about (and quickly hacked on) is to "detect" the presense of KVM and use it if it is there and use "plain" qemu if it is not. The question is if this is behavior that the rest of you would like, I'll send a patch if so.

One thing which I've changed is to return errors from libkvm instead of directly exiting. Personally I think this behavior is preferred since it does not force decisions upon the program using the library.

// Simon

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

A message that is not in the archive (sent off-list or not yet received): 8764di9y30.wl%[email protected]

#7 lore raw
Quoted 8 lines · Simon Kagstrom

At Tue, 14 Nov 2006 10:50:23 +0200,

Quoted 6 lines · Avi Kivity
Quoted 3 lines · Baruch Even

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

Quoted 8 lines
Quoted 3 lines

qemu-kvm.patch is an older release. With the new release, it should be possible to run without kvm. Let me know (or better, send patches) if there are any such places in kvm-2.

One thing I've thought about (and quickly hacked on) is to "detect" the presense of KVM and use it if it is there and use "plain" qemu if it is not. The question is if this is behavior that the rest of you would like, I'll send a patch if so.

That would be best IMO. In this way qemu will just work and it can run (much?) faster if the user has kvm. Though this means some loss in branding for kvm as users are less likely to know they are running under kvm.

In usch a mode the user should be notified if KVM is not found and if KVM is found but the machine doesn't have VT support. Though I guess the KVM module will fail to load if there is no VT support in the CPU.

Cheers,
Baruch

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

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

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.

kvmctl.c renamed from orig/kvm-2/user/kvmctl.c +38 -31
diff -u ../orig/kvm-2/user/kvmctl.c user/kvmctl.c
@@ -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;
kvmctl.h renamed from orig/kvm-2/user/kvmctl.h +3 -0
diff -u ../orig/kvm-2/user/kvmctl.h user/kvmctl.h
@@ -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);
main.c renamed from orig/kvm-2/user/main.c +11 -1
diff -u ../orig/kvm-2/user/main.c user/main.c
@@ -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;
 }

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

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

#10 lore raw
In reply to a message not in the archive
Quoted 14 lines · Simon Kagstrom

At Tue, 14 Nov 2006 10:50:23 +0200,

Quoted 10 lines · Avi Kivity
Quoted 4 lines · Baruch Even

Can the Qemu built with KVM support run without KVM? Froma quick perusal of the kvm.patch it seems that there are places where USE_KVM implies a qemu-panic without any runtime conditionals.

qemu-kvm.patch is an older release. With the new release, it should be possible to run without kvm. Let me know (or better, send patches) if there are any such places in kvm-2.

Quoted 5 lines

One thing I've thought about (and quickly hacked on) is to "detect" the presense of KVM and use it if it is there and use "plain" qemu if it is not. The question is if this is behavior that the rest of you would like, I'll send a patch if so.

I think it is indeed desirable. Please send the patch.

Of course, -no-kvm should not use kvm even if it there.

Quoted 4 lines

One thing which I've changed is to return errors from libkvm instead of directly exiting. Personally I think this behavior is preferred since it does not force decisions upon the program using the library.

The exit()s are a hack, of course.

Signature · 8 lines
error compiling committee.c: too many arguments to function


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

A message that is not in the archive (sent off-list or not yet received): 45598F42.5090400-atKUWr5tajBWk0Htik3J/[email protected]

#12 lore raw

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

This patch implements fall-back to plain Qemu if KVM is not present. Some logic has been moved into kvm_qemu_init.

qemu-kvm.c renamed from orig/kvm-2/qemu/qemu-kvm.c +12 -2
diff -u ../orig/kvm-2/qemu/qemu-kvm.c qemu/qemu-kvm.c
@@ -450,10 +450,20 @@
     .io_window = kvm_io_window,
 };
 
-void kvm_qemu_init()
+int kvm_qemu_init()
 {
+    /* Try to initialize kvm */
     kvm_context = kvm_init(&qemu_kvm_ops, saved_env);
-    kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base);
+    if (!kvm_context) {
+      	return -1;
+    }
+    phys_ram_size += KVM_EXTRA_PAGES * 4096;
+    if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
+	kvm_finalize(kvm_context);
+	return -1;
+    }
+
+    return 0;
 }
 
 int kvm_update_debugger(CPUState *env)
qemu-kvm.h renamed from orig/kvm-2/qemu/qemu-kvm.h +3 -1
diff -u ../orig/kvm-2/qemu/qemu-kvm.h qemu/qemu-kvm.h
@@ -3,7 +3,9 @@
 
 #include "kvmctl.h"
 
-void kvm_qemu_init(void);
+#define KVM_EXTRA_PAGES 3
+
+int kvm_qemu_init(void);
 void kvm_load_registers(CPUState *env);
 int kvm_cpu_exec(CPUState *env);
 int kvm_update_debugger(CPUState *env);
vl.c renamed from orig/kvm-2/qemu/vl.c +14 -10
diff -u ../orig/kvm-2/qemu/vl.c qemu/vl.c
@@ -6099,20 +6099,24 @@
     }
 
     /* init the memory */
-#if USE_KVM
     phys_ram_size = ram_size + vga_ram_size + bios_size;
+#if USE_KVM
+    /* Initialize kvm */
     if (kvm_allowed) {
-	    phys_ram_size += KVM_EXTRA_PAGES * 4096;
-	    kvm_qemu_init();
-    } else {
-	    phys_ram_base = qemu_vmalloc(phys_ram_size);
-	    if (!phys_ram_base) {
-		    fprintf(stderr, "Could not allocate physical memory\n");
-		    exit(1);
-	    }
+	if (kvm_qemu_init() < 0) {
+	    fprintf(stderr, "Could not initialize KVM\n");
+	    kvm_allowed = 0;
+	}
+    }
+    /* Fall back to normal qemu if KVM fails */
+    if (!kvm_allowed) {
+	phys_ram_base = qemu_vmalloc(phys_ram_size);
+	if (!phys_ram_base) {
+	    fprintf(stderr, "Could not allocate physical memory\n");
+	    exit(1);
+	}
     }
 #else
-    phys_ram_size = ram_size + vga_ram_size + bios_size;
     phys_ram_base = qemu_vmalloc(phys_ram_size);
     if (!phys_ram_base) {
         fprintf(stderr, "Could not allocate physical memory\n");
vl.h renamed from orig/kvm-2/qemu/vl.h +0 -4
diff -u ../orig/kvm-2/qemu/vl.h qemu/vl.h
@@ -162,10 +162,6 @@
 #define BIOS_SIZE ((256 + 64) * 1024)
 #endif
 
-#if USE_KVM
-#define KVM_EXTRA_PAGES 3
-#endif
-
 /* keyboard/mouse support */
 
 #define MOUSE_EVENT_LBUTTON 0x01

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

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): 871wo69uxb.wl%[email protected]

#14 lore raw

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

Quoted 6 lines · Simon Kagstrom

This patch implements fall-back to plain Qemu if KVM is not present. Some logic has been moved into kvm_qemu_init.

[please inline patches in the future for easier reviewing]

-#if USE_KVM

     phys_ram_size = ram_size + vga_ram_size + bios_size;
+#if USE_KVM
+    /* Initialize kvm */
     if (kvm_allowed) {

- phys_ram_size += KVM_EXTRA_PAGES * 4096;
- kvm_qemu_init();
- } else {
- phys_ram_base = qemu_vmalloc(phys_ram_size);
- if (!phys_ram_base) {
- fprintf(stderr, "Could not allocate physical memory\n");
- exit(1);
- }
+ if (kvm_qemu_init() < 0) {
+ fprintf(stderr, "Could not initialize KVM\n");
+ kvm_allowed = 0;
+ }

+    }
+    /* Fall back to normal qemu if KVM fails */
+    if (!kvm_allowed) {

+ phys_ram_base = qemu_vmalloc(phys_ram_size);
+ if (!phys_ram_base) {
+ fprintf(stderr, "Could not allocate physical memory\n");
+ exit(1);
+ }

     }

#else
- phys_ram_size = ram_size + vga_ram_size + bios_size;

This bit is rather awkward, since it disabled kvm after it has been enabled. It's a cleaner to never enable kvm_allowed in the first place (i.e. during argument parsing).

How about a

    int libkvm_init(kvm_libcontext_t *libcontext);
    void libkvm_exit(kvm_libcontext_t libcontext);
   
    int kvm_create(kvm_libcontext_t libcontext, kvm_context_t* vm);
    void kvm_destroy(kvm_context_t *vm);

at present, libkvm_init() can just call access(2) on /dev/kvm. In the future, it is planned that open("/dev/kvm") will not create a VM (it just opens a communication channel to the driver), so that libkvm_init() will open the device and store the fd.

Signature · 8 lines
error compiling committee.c: too many arguments to function


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

A message that is not in the archive (sent off-list or not yet received): 4559AC1F.9080304-atKUWr5tajBWk0Htik3J/[email protected]

#16 lore raw

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

(I've edited the patch comments to show the reasoning)

At Tue, 14 Nov 2006 13:44:31 +0200,

Quoted 16 lines · Avi Kivity
    /* Initialize kvm */
    if (kvm_allowed) {
	if (kvm_qemu_init() < 0) {
	    fprintf(stderr, "Could not initialize KVM\n");
	    kvm_allowed = 0;
	}
    }
    /* Fall back to normal qemu if KVM fails */
    if (!kvm_allowed) {
	phys_ram_base = qemu_vmalloc(phys_ram_size);
	if (!phys_ram_base) {
	    fprintf(stderr, "Could not allocate physical memory\n");
	    exit(1);
	}
    }
Quoted 3 lines

This bit is rather awkward, since it disabled kvm after it has been enabled. It's a cleaner to never enable kvm_allowed in the first place (i.e. during argument parsing).

OK. My reasoning was this: qemu with KVM defaults to use kvm and therefore tries to enable it (kvm_qemu_init), if it cannot be initialized, it will disable it again.

I can submit a new patch where kvm_init and kvm_create is separated and kvm_init is done at argument-parsing time if you like, but personally I think this way is clearer :-)

Quoted 8 lines

How about a

    int libkvm_init(kvm_libcontext_t *libcontext);
    void libkvm_exit(kvm_libcontext_t libcontext);
   
    int kvm_create(kvm_libcontext_t libcontext, kvm_context_t* vm);
    void kvm_destroy(kvm_context_t *vm);
Quoted 4 lines

at present, libkvm_init() can just call access(2) on /dev/kvm. In the future, it is planned that open("/dev/kvm") will not create a VM (it just opens a communication channel to the driver), so that libkvm_init() will open the device and store the fd.

Personally, I would prefer something like

   int kvm_open(kvm_t *kvm;)
   void kvm_close(kvm_t *kvm);

   int kvm_create_vm(kvm_t *kvm, kvm_context_t *vm);
   void kvm_destroy_vm(kvm_t *kvm, kvm_context_t *vm);

// Simon

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

A message that is not in the archive (sent off-list or not yet received): 87y7qe8c43.wl%[email protected]

#18 lore raw

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

Quoted 21 lines · Simon Kagstrom

(I've edited the patch comments to show the reasoning)

At Tue, 14 Nov 2006 13:44:31 +0200,

Quoted 16 lines · Avi Kivity
    /* Initialize kvm */
    if (kvm_allowed) {
	if (kvm_qemu_init() < 0) {
	    fprintf(stderr, "Could not initialize KVM\n");
	    kvm_allowed = 0;
	}
    }
    /* Fall back to normal qemu if KVM fails */
    if (!kvm_allowed) {
	phys_ram_base = qemu_vmalloc(phys_ram_size);
	if (!phys_ram_base) {
	    fprintf(stderr, "Could not allocate physical memory\n");
	    exit(1);
	}
    }
Quoted 9 lines
Quoted 4 lines

This bit is rather awkward, since it disabled kvm after it has been enabled. It's a cleaner to never enable kvm_allowed in the first place (i.e. during argument parsing).

OK. My reasoning was this: qemu with KVM defaults to use kvm and therefore tries to enable it (kvm_qemu_init), if it cannot be initialized, it will disable it again.

Yes, but it leads to code duplication (there's a qemu_vmalloc() outside USE_KVM). Also, if some code prior to this depended on kvm_allowed, it would be invalidated.

Best to set kvm_allowed once you're sure it's really allowed, and then don't touch it.

Quoted 13 lines

I can submit a new patch where kvm_init and kvm_create is separated and kvm_init is done at argument-parsing time if you like, but personally I think this way is clearer :-)

Quoted 8 lines

How about a

    int libkvm_init(kvm_libcontext_t *libcontext);
    void libkvm_exit(kvm_libcontext_t libcontext);
   
    int kvm_create(kvm_libcontext_t libcontext, kvm_context_t* vm);
    void kvm_destroy(kvm_context_t *vm);

I meant kvm_destroy(kvm_context_t vm);

Quoted 6 lines
Quoted 5 lines

at present, libkvm_init() can just call access(2) on /dev/kvm. In the future, it is planned that open("/dev/kvm") will not create a VM (it just opens a communication channel to the driver), so that libkvm_init() will open the device and store the fd.

Quoted 8 lines

Personally, I would prefer something like

   int kvm_open(kvm_t *kvm;)
   void kvm_close(kvm_t *kvm);

   int kvm_create_vm(kvm_t *kvm, kvm_context_t *vm);
   void kvm_destroy_vm(kvm_t *kvm, kvm_context_t *vm);

That means that if sizeof(kvm_t) changes, we need to recompile everything. With kvm_context_t and kvm_t as opaque pointers, we preserve binary compatibility when their definitions change.

Signature · 8 lines
error compiling committee.c: too many arguments to function


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

A message that is not in the archive (sent off-list or not yet received): 4559B574.5000000-atKUWr5tajBWk0Htik3J/[email protected]

#20 lore raw

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

At Tue, 14 Nov 2006 14:24:20 +0200,

Quoted 6 lines · Avi Kivity

Yes, but it leads to code duplication (there's a qemu_vmalloc() outside USE_KVM). Also, if some code prior to this depended on kvm_allowed, it would be invalidated.

Best to set kvm_allowed once you're sure it's really allowed, and then don't touch it.

I've attached a new patch below. It's still setting kvm_allowed to 0 in two places - if kvm_qemu_init() fails and if the -no-kvm option is passed, but it now does a kind of "try-and-see" and sets it to zero if it fails. This is done regardless of if -no-kvm is passed or not.

I split initialization and creation of a vm in qemu-kvm.c.

The code duplication was there before, and I have not changed that.

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

at present, libkvm_init() can just call access(2) on /dev/kvm. In the future, it is planned that open("/dev/kvm") will not create a VM (it just opens a communication channel to the driver), so that libkvm_init() will open the device and store the fd.

Personally, I would prefer something like

   int kvm_open(kvm_t *kvm;)
   void kvm_close(kvm_t *kvm);
Quoted 7 lines
Quoted 3 lines
   int kvm_create_vm(kvm_t *kvm, kvm_context_t *vm);
   void kvm_destroy_vm(kvm_t *kvm, kvm_context_t *vm);

That means that if sizeof(kvm_t) changes, we need to recompile everything. With kvm_context_t and kvm_t as opaque pointers, we preserve binary compatibility when their definitions change.

Sorry, true.

I was actually mostly thinking of the naming (I prefer the simpler kvm_t).

// Simon

qemu-kvm.c renamed from orig/kvm-2/qemu/qemu-kvm.c +22 -2
diff -u ../orig/kvm-2/qemu/qemu-kvm.c qemu/qemu-kvm.c
@@ -450,10 +450,30 @@
     .io_window = kvm_io_window,
 };
 
-void kvm_qemu_init()
+int kvm_qemu_init()
 {
+    /* Try to initialize kvm */
     kvm_context = kvm_init(&qemu_kvm_ops, saved_env);
-    kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base);
+    if (!kvm_context) {
+      	return -1;
+    }
+
+    return 0;
+}
+
+int kvm_qemu_create_context(void)
+{
+    if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
+	kvm_qemu_destroy();
+	return -1;
+    }
+
+    return 0;
+}
+
+void kvm_qemu_destroy(void)
+{
+    kvm_finalize(kvm_context);
 }
 
 int kvm_update_debugger(CPUState *env)
qemu-kvm.h renamed from orig/kvm-2/qemu/qemu-kvm.h +3 -1
diff -u ../orig/kvm-2/qemu/qemu-kvm.h qemu/qemu-kvm.h
@@ -3,7 +3,9 @@
 
 #include "kvmctl.h"
 
-void kvm_qemu_init(void);
+int kvm_qemu_init(void);
+int kvm_qemu_create_context(void);
+void kvm_qemu_destroy(void);
 void kvm_load_registers(CPUState *env);
 int kvm_cpu_exec(CPUState *env);
 int kvm_update_debugger(CPUState *env);
vl.c renamed from orig/kvm-2/qemu/vl.c +10 -3
diff -u ../orig/kvm-2/qemu/vl.c qemu/vl.c
@@ -5706,6 +5706,10 @@
 
     nb_nics = 0;
     /* default mac address of the first network interface */
+#if USE_KVM
+    if (kvm_qemu_init() < 0)
+	kvm_allowed = 0;
+#endif
     
     optind = 1;
     for(;;) {
@@ -6099,11 +6103,15 @@
     }
 
     /* init the memory */
-#if USE_KVM
     phys_ram_size = ram_size + vga_ram_size + bios_size;
+#if USE_KVM
+    /* Initialize kvm */
     if (kvm_allowed) {
 	    phys_ram_size += KVM_EXTRA_PAGES * 4096;
-	    kvm_qemu_init();
+	    if (kvm_qemu_create_context() < 0) {
+		    fprintf(stderr, "Could not create KVM context\n");
+		    exit(1);
+	    }
     } else {
 	    phys_ram_base = qemu_vmalloc(phys_ram_size);
 	    if (!phys_ram_base) {
@@ -6112,7 +6120,6 @@
 	    }
     }
 #else
-    phys_ram_size = ram_size + vga_ram_size + bios_size;
     phys_ram_base = qemu_vmalloc(phys_ram_size);
     if (!phys_ram_base) {
         fprintf(stderr, "Could not allocate physical memory\n");

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

A message that is not in the archive (sent off-list or not yet received): 87wt5y89ik.wl%[email protected]

#22 lore raw

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

vl.c line 5713

Quoted 12 lines · Simon Kagstrom

diff -u ../orig/kvm-2/qemu/vl.c qemu/vl.c

vl.c renamed from orig/kvm-2/qemu/vl.c +4 -0
@@ -5706,6 +5706,10 @@
 
     nb_nics = 0;
     /* default mac address of the first network interface */
+#if USE_KVM
+    if (kvm_qemu_init() < 0)
+	kvm_allowed = 0;
+#endif
 

Doesn't this bit will initialize kvm even if the user specified -no-kvm?

Signature · 8 lines
error compiling committee.c: too many arguments to function


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

A message that is not in the archive (sent off-list or not yet received): 4559C393.70001-atKUWr5tajBWk0Htik3J/[email protected]

#24 lore raw

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

At Tue, 14 Nov 2006 15:24:35 +0200,

Quoted 7 lines · Avi Kivity
Quoted 5 lines · Simon Kagstrom

diff -u ../orig/kvm-2/qemu/vl.c qemu/vl.c

vl.c renamed from orig/kvm-2/qemu/vl.c +0 -0
@@ -5706,6 +5706,10 @@
 
Quoted 9 lines

vl.c line 5713

Quoted 7 lines
vl.c renamed from orig/kvm-2/qemu/vl.c +4 -0
     nb_nics = 0;
     /* default mac address of the first network interface */
+#if USE_KVM
+    if (kvm_qemu_init() < 0)
+	kvm_allowed = 0;
+#endif
 

Doesn't this bit will initialize kvm even if the user specified -no-kvm?

Yes. Maybe I misunderstood you, but this is how I understood your comments.

Sorry for spamming the list with patches. I'll send yet another one. This will check and possibly disable KVM after the command-line options have been parsed. If -no-kvm has been given, nothing will happen.

Hopefully this will be the final patch! :-)

// Simon

qemu-kvm.c renamed from orig/kvm-2/qemu/qemu-kvm.c +22 -2
diff -u ../orig/kvm-2/qemu/qemu-kvm.c qemu/qemu-kvm.c
@@ -450,10 +450,30 @@
     .io_window = kvm_io_window,
 };
 
-void kvm_qemu_init()
+int kvm_qemu_init()
 {
+    /* Try to initialize kvm */
     kvm_context = kvm_init(&qemu_kvm_ops, saved_env);
-    kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base);
+    if (!kvm_context) {
+      	return -1;
+    }
+
+    return 0;
+}
+
+int kvm_qemu_create_context(void)
+{
+    if (kvm_create(kvm_context, phys_ram_size, (void**)&phys_ram_base) < 0) {
+	kvm_qemu_destroy();
+	return -1;
+    }
+
+    return 0;
+}
+
+void kvm_qemu_destroy(void)
+{
+    kvm_finalize(kvm_context);
 }
 
 int kvm_update_debugger(CPUState *env)
qemu-kvm.h renamed from orig/kvm-2/qemu/qemu-kvm.h +3 -1
diff -u ../orig/kvm-2/qemu/qemu-kvm.h qemu/qemu-kvm.h
@@ -3,7 +3,9 @@
 
 #include "kvmctl.h"
 
-void kvm_qemu_init(void);
+int kvm_qemu_init(void);
+int kvm_qemu_create_context(void);
+void kvm_qemu_destroy(void);
 void kvm_load_registers(CPUState *env);
 int kvm_cpu_exec(CPUState *env);
 int kvm_update_debugger(CPUState *env);
vl.c renamed from orig/kvm-2/qemu/vl.c +15 -3
diff -u ../orig/kvm-2/qemu/vl.c qemu/vl.c
@@ -6054,6 +6054,15 @@
         }
     }
 
+#if USE_KVM
+    if (kvm_allowed) {
+	if (kvm_qemu_init() < 0) {
+	    fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
+	    kvm_allowed = 0;
+	}
+    }
+#endif
+
 #ifdef USE_KQEMU
     if (smp_cpus > 1)
         kqemu_allowed = 0;
@@ -6099,11 +6108,15 @@
     }
 
     /* init the memory */
-#if USE_KVM
     phys_ram_size = ram_size + vga_ram_size + bios_size;
+#if USE_KVM
+    /* Initialize kvm */
     if (kvm_allowed) {
 	    phys_ram_size += KVM_EXTRA_PAGES * 4096;
-	    kvm_qemu_init();
+	    if (kvm_qemu_create_context() < 0) {
+		    fprintf(stderr, "Could not create KVM context\n");
+		    exit(1);
+	    }
     } else {
 	    phys_ram_base = qemu_vmalloc(phys_ram_size);
 	    if (!phys_ram_base) {
@@ -6112,7 +6125,6 @@
 	    }
     }
 #else
-    phys_ram_size = ram_size + vga_ram_size + bios_size;
     phys_ram_base = qemu_vmalloc(phys_ram_size);
     if (!phys_ram_base) {
         fprintf(stderr, "Could not allocate physical memory\n");

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

A message that is not in the archive (sent off-list or not yet received): 87veli87vf.wl%[email protected]

#26 lore raw

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

Quoted 9 lines · Simon Kagstrom
Quoted 4 lines
Quoted 2 lines

Doesn't this bit will initialize kvm even if the user specified -no-kvm?

Yes. Maybe I misunderstood you, but this is how I understood your comments.

I intended something like

start with kvm_allowed = 1
if (-no-kvm) kvm_allowed = 0
if (kvm_allowed && !kvm_init() < 0) kvm_allowed = 0

Quoted 2 lines

Sorry for spamming the list with patches. I'll send yet another one.

Keep on patching :)

Quoted 7 lines

This will check and possibly disable KVM after the command-line options have been parsed. If -no-kvm has been given, nothing will happen.

vl.c line 6065

Quoted 9 lines
vl.c renamed from orig/kvm-2/qemu/vl.c +9 -0
+#if USE_KVM
+    if (kvm_allowed) {
+	if (kvm_qemu_init() < 0) {
+	    fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
+	    kvm_allowed = 0;
+	}
+    }
+#endif
+

Yes, exactly what I meant.

I'll apply this. Thanks!

Signature · 8 lines
error compiling committee.c: too many arguments to function


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