Skip to contentSkip to conversation

Split kvmctl for architectures

PATCH In review Jerone Young 4 participants 7 messages Oct 18, 2007 2 messages not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 Split kvmctl for architectures

This patch splits kvmctl architecture specific declarations & implementations into their own files & headers. Common interfaces are kept in kvmctl.h, as well as the arch specific headers are included from within kvmctl.h. This may or may not want to be changed.

Also includes the having different main.c files for different
architectures (otherwise it's a lot of ifdefs).

user/kvmctl-powerpc.c new file +351 -0
new file mode 100644
@@ -0,0 +1,351 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Copyright IBM Corp. 2007
+ *
+ * Authors: Christian Ehrhardt <[email protected]>
+*/
+
+#ifndef __user
+#define __user /* temporary, until installed via make headers_install */
+#endif
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include "kvmctl.h"
+
+/*!
+ * \brief The KVM context
+ *
+ * The verbose KVM context to pass our states 
+ * between different calls if needed
+ */
+struct kvm_context {
+	// Filedescriptor to /dev/kvm
+	int fd;
+	/* 
+	 * this represents our kvm_run for ppc e.g. 
+	 * in drivers/kvm/powerpc/kvm.h while the 
+	 *
+	 * original represents what comes from <linux/kvm.h>
+	 */
+	struct kvm_run run; /* we are not multi cpu so one 
+                               struct in userspace should be enough */
+	/// Callbacks that KVM uses to emulate various unvirtualizable functionality
+	struct kvm_callbacks *callbacks;
+	void *opaque;	
+};
+
+kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
+		       void *opaque){
+	kvm_context_t kvm;
+	int fd;
+	int r;
+
+	fd = open("/dev/kvm", O_RDWR);
+	if (fd == -1) {
+		char *errstr;
+		r = errno;
+		errstr = strerror(errno);
+                printf("open /dev/kvm: %s\n", errstr);
+		return NULL;
+	}
+
+	kvm = malloc(sizeof(*kvm));
+	kvm->fd = fd;
+	kvm->callbacks = callbacks;
+	kvm->opaque = opaque;
+	return kvm;
+}
+
+void kvm_finalize(kvm_context_t kvm)
+{
+	close(kvm->fd);
+	free(kvm);
+}
+
+int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem)
+{
+	struct create {
+		uint32_t guest_ram;
+		uint32_t guest_size;
+	} create;
+	int r;
+
+        *vm_mem = mmap(NULL, phys_mem_bytes, PROT_EXEC|PROT_READ|PROT_WRITE,
+                         MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+        if (*vm_mem == (void *)-1) {
+                perror("mmap");
+                return 2;
+        }
+	create.guest_ram = (uint32_t)(*vm_mem);
+	create.guest_size = phys_mem_bytes;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_CREATE, &create);
+	printf("create ioctl returned %d (errno %d)\n", r, errno);
+	return 0;
+}
+
+int kvm_create_vcpu(kvm_context_t kvm, int slot)
+{
+	/* atm we do all (1vcpu) in create and have no smp, ... */
+	return 0;
+}
+
+int kvm_set_pc(kvm_context_t kvm, uint32_t pc)
+{
+	struct kvm_regs regs;
+	int r;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_GETREGS, &regs);
+	if (r)
+		return r;
+
+	regs.pc = pc;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_SETREGS, &regs);
+	if (r)
+		return r;
+
+	return 0;
+}
+
+int emulate_dcr(kvm_context_t kvm, struct kvm_run *run)
+{
+	int r;
+ 
+	if (run->dcr.is_write)
+		r = kvm->callbacks->ppc_dcr_write(kvm,
+			run->dcr.dcrn,run->dcr.data);
+	else
+		r = kvm->callbacks->ppc_dcr_read(kvm,
+			run->dcr.dcrn,&(run->dcr.data));
+
+#ifdef DEBUG_KVMPPC
+	if (r)
+		fprintf(stderr,"%s:%d - failed write '%d' dcrn '0x%08X' "
+			"data '%d' retval '%d'\n",
+			__FILE__,__LINE__,
+			run->dcr.is_write,run->dcr.dcrn,run->dcr.data,r);
+	else
+		fprintf(stderr,"%s:%d - wrote '%d' dcrn '0x%08X' "
+			"data '%d' retval '%d'\n",
+			__FILE__,__LINE__,
+			run->dcr.is_write,run->dcr.dcrn,run->dcr.data,r);
+#endif
+
+	/* currently we live with nop for unimplemented dcr registers */
+	return 0;
+}
+
+int kvm_get_regs(kvm_context_t kvm, int vcpu, struct kvm_regs *regs)
+{
+	int r = 0;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_GETREGS, regs);
+	if (r) {
+		fprintf(stderr,"%s - KVM_PPC_IOCTL_GETREGS failed (r='%d')\n",
+								__func__,r);
+	}
+
+	return r;
+}
+
+int kvm_set_regs(kvm_context_t kvm, int vcpu, struct kvm_regs *regs)
+{
+	int r = 0;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_SETREGS, regs);
+	if (r) {
+		fprintf(stderr,"%s - KVM_PPC_IOCTL_SETREGS failed (r='%d')\n",
+								__func__,r);
+	}
+
+	return r;
+}
+
+int kvm_get_sregs(kvm_context_t kvm, int vcpu, struct kvm_sregs *sregs)
+{
+	fprintf(stderr,"%s - not implemented \n",__func__);
+	return -ENOTSUP;
+}
+
+int kvm_set_sregs(kvm_context_t kvm, int vcpu, struct kvm_sregs *sregs)
+{
+	fprintf(stderr,"%s - not implemented \n",__func__);
+	return -ENOTSUP;
+}
+
+int kvm_get_fpu(kvm_context_t kvm, int vcpu, struct kvm_fpu *fpu)
+{
+	fprintf(stderr,"%s - not implemented \n",__func__);
+	return -ENOTSUP;
+}
+
+int kvm_set_fpu(kvm_context_t kvm, int vcpu, struct kvm_fpu *fpu)
+{
+	fprintf(stderr,"%s - not implemented \n",__func__);
+	return -ENOTSUP;
+}
+
+int kvm_guest_debug(kvm_context_t kvm, int vcpu, struct kvm_debug_guest *dbg)
+{
+	fprintf(stderr,"%s - not implemented \n",__func__);
+	return -ENOTSUP;
+}
+
+int kvm_get_tlb(kvm_context_t kvm, struct kvm_tlb *tlb)
+{
+	int r = 0;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_GETTLB, tlb);
+	if (r) {
+		fprintf(stderr,"%s - KVM_PPC_IOCTL_GETTLB failed (r='%d')\n",
+								__func__,r);
+	}
+
+	return r;
+}
+
+int kvm_set_tlb(kvm_context_t kvm, struct kvm_tlb *tlb)
+{
+	int r = 0;
+
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_SETTLB, tlb);
+	if (r) {
+		fprintf(stderr,"%s - KVM_PPC_IOCTL_SETTLB failed (r='%d')\n",
+								__func__,r);
+	}
+
+	return r;
+}
+
+int kvm_dump_vcpu(kvm_context_t kvm, int vcpu)
+{
+	int i;
+	struct kvm_regs regs;
+
+	kvm_get_regs(kvm,-1,&regs);
+
+	fprintf(stderr,"Guest vcpu\n");
+	fprintf(stderr,"pc:   %08x msr:  %08x\n", regs.pc, regs.msr);
+	fprintf(stderr,"lr:   %08x ctr:  %08x\n", regs.lr, regs.ctr);
+	fprintf(stderr,"srr0: %08x srr1: %08x\n", regs.srr0, regs.srr1);
+	for (i = 0; i < 32; i += 4) {
+		fprintf(stderr,"gpr%02d: %08x %08x %08x %08x\n", i,
+			regs.gpr[i],
+			regs.gpr[i+1],
+			regs.gpr[i+2],
+			regs.gpr[i+3]);
+	}
+	fflush(stdout);
+	return 0;
+}
+
+void kvm_show_regs(kvm_context_t kvm, int vcpu) {
+	struct kvm_regs regs;
+	int i;
+
+	kvm_get_regs(kvm,-1,&regs);
+
+	for (i = 0; i < 32; i += 4) {
+		fprintf(stderr,"gpr%02d: %08x %08x %08x %08x\n", i,
+			regs.gpr[i],
+			regs.gpr[i+1],
+			regs.gpr[i+2],
+			regs.gpr[i+3]);
+	}
+	fflush(stdout);
+
+}
+
+static void print_tlb(struct tlbe tlb[PPC44x_TLB_SIZE], char* name, char* flag)
+{
+	int i;
+
+	fprintf(stderr,"%s:\n",name);
+	fprintf(stderr,"| %2s | %8s | %8s | %8s | %8s |\n","nr","mmucr",
+						"word0","word1","word2");
+	for (i = 0; i < PPC44x_TLB_SIZE; i++) {
+		if (tlb[i].word0 & PPC44x_TLB_VALID)
+			fprintf(stderr,"%s%2d | %08X | %08X | %08X | %08X |\n",
+				flag,i,
+				tlb[i].mmucr,
+				tlb[i].word0,
+				tlb[i].word1,
+				tlb[i].word2);
+	}
+	fflush(stdout);
+}
+
+static void kvm_dump_tlb(kvm_context_t kvm)
+{
+	struct kvm_tlb tlb;
+
+	kvm_get_tlb(kvm,&tlb);
+
+	print_tlb(tlb.guest_tlb,"Guest TLB","G");
+	print_tlb(tlb.shadow_tlb,"Shadow TLB","S");
+}
+
+int kvm_run(kvm_context_t kvm, int vcpu)
+{
+	int r=0;
+	struct kvm_run *run = &(kvm->run);
+again:
+	r = ioctl(kvm->fd, KVM_PPC_IOCTL_RUN, run);
+
+	if (r == -1 && errno != EINTR) {
+		r = -errno;
+		printf("kvm_run: %m\n");
+		return r;
+	}
+	if (1) {
+		switch (kvm->run.exit_reason) {
+		case KVM_EXIT_MMIO:
+			kvm->callbacks->cpu_physical_memory_rw(run->mmio.phys_addr, 
+					run->mmio.data,
+					run->mmio.len,
+					run->mmio.is_write);
+			break;
+		case KVM_EXIT_DCR:
+			r = emulate_dcr(kvm,run);
+			break;
+
+		case KVM_EXIT_SHUTDOWN:
+			printf("guest exited\n");
+			r = 1;
+			break;
+		default:
+			fprintf(stderr, "unhandled vm exit_reason: 0x%x\n", 
+							  run->exit_reason);
+			kvm_dump_vcpu(kvm,-1);
+			kvm_dump_tlb(kvm);
+			r=-EINVAL;
+			break;
+		}
+	}
+
+	if (!r)
+		goto again;
+	return r;
+
+}
user/kvmctl-powerpc.h new file +152 -0

152 lines not on this page · raw message

user/kvmctl-x86.c new file +1437 -0

1,437 lines not on this page · raw message

user/kvmctl-x86.h new file +292 -0

292 lines not on this page · raw message

user/kvmctl.c deleted +0 -1437

768 lines not on this page · raw message

user/kvmctl.h +18 -280

298 lines not on this page · raw message

user/main-powerpc.c new file +298 -0

298 lines not on this page · raw message

user/main-x86.c new file +590 -0

590 lines not on this page · raw message

user/main.c deleted +0 -590

590 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

Part of the diff is not shown here. View the raw message.

#1 lore raw
Quoted 11 lines · Jerone Young

This patch splits kvmctl architecture specific declarations & implementations into their own files & headers. Common interfaces are kept in kvmctl.h, as well as the arch specific headers are included from within kvmctl.h. This may or may not want to be changed.

Also includes the having different main.c files for different
architectures (otherwise it's a lot of ifdefs).

This patch is pretty hard to reasonably review since diff is so large.

A quick glance suggests that there's a lot of code duplication between things like main-x86.c and main-ppc.c. Perhaps the common code could be unified?

Regards,

Anthony Liguori

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

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

#3 lore raw
Quoted 6 lines · Anthony Liguori
Quoted 5 lines · Jerone Young

This patch splits kvmctl architecture specific declarations & implementations into their own files & headers. Common interfaces are kept in kvmctl.h, as well as the arch specific headers are included from within kvmctl.h. This may or may not want to be changed.

Quoted 8 lines
Quoted 6 lines

Also includes the having different main.c files for different
architectures (otherwise it's a lot of ifdefs).

This patch is pretty hard to reasonably review since diff is so large.

Yeah.. I'm going to break it down to try and make it more reviewable.

Quoted 4 lines

A quick glance suggests that there's a lot of code duplication between things like main-x86.c and main-ppc.c. Perhaps the common code could be unified?

I do have a main.c with ifdefs. But the feeling here is that as things are added this gets hair really quick. Right now it's ok with only 2 main architectures. But if others come in, this file could turn into an ifdef mess.

I really have no strong feelings on it. I can combine them.

Quoted 5 lines

Regards,

Anthony Liguori

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#4 lore raw
Quoted 6 lines · Jerone Young
Quoted 4 lines · Anthony Liguori

A quick glance suggests that there's a lot of code duplication between things like main-x86.c and main-ppc.c. Perhaps the common code could be unified?

Quoted 6 lines

I do have a main.c with ifdefs. But the feeling here is that as things are added this gets hair really quick. Right now it's ok with only 2 main architectures. But if others come in, this file could turn into an ifdef mess.

I really have no strong feelings on it. I can combine them.

Contrary to popular belief, we can share code between multiple architectures without using ifdefs...

Signature · 9 lines
Hollis Blanchard
IBM Linux Technology Center


-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
#5 lore raw

Ok, so some useful feedback :-). I'm going to rework these patches completely. Have them share a LOT more then these now. Which is basically a total spilt.

Quoted 12 lines · Hollis Blanchard
Quoted 11 lines · Jerone Young
Quoted 4 lines · Anthony Liguori

A quick glance suggests that there's a lot of code duplication between things like main-x86.c and main-ppc.c. Perhaps the common code could be unified?

I do have a main.c with ifdefs. But the feeling here is that as things are added this gets hair really quick. Right now it's ok with only 2 main architectures. But if others come in, this file could turn into an ifdef mess.

Quoted 5 lines
Quoted 1 line

I really have no strong feelings on it. I can combine them.

Contrary to popular belief, we can share code between multiple architectures without using ifdefs...

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

#6 lore raw
In reply to Jerone Young
Quoted 15 lines · Jerone Young
user/kvmctl-powerpc.c new file +10 -0
new file mode 100644
@@ -0,0 +1,351 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

user/kvmctl-powerpc.c

Quoted 10 lines
user/kvmctl-powerpc.c new file +9 -1
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Copyright IBM Corp. 2007
+ *
+ * Authors: Christian Ehrhardt <[email protected]>
+*/
-

shouldnt the userspace libary be lgpl?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/

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

#8 lore raw
Quoted 19 lines · Izik Eidus
Quoted 18 lines · Jerone Young
user/kvmctl-powerpc.c new file +13 -0
new file mode 100644
@@ -0,0 +1,351 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
Quoted 8 lines

user/kvmctl-powerpc.c

Quoted 7 lines
user/kvmctl-powerpc.c new file +6 -1
+ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Copyright IBM Corp. 2007
+ *
+ * Authors: Christian Ehrhardt <[email protected]>
+*/
-

shouldnt the userspace libary be lgpl?

You would be correct. I need to totally rework this patch. But I will make sure that the preamble is lgpl. Thanks for pointing that out.

Quoted 10 lines

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/