[PATCH 05 of 17] Modify out arch specific code from kvm_create function

Jerone Young
2007-11-06T16:48:46Z

# Date 1194367648 21600
# Node ID 4bab1bb82930ade04b5a796ca79e75e392265ba7
# Parent 5a8dcd2c2316ade1bcb332c57b74b8cc071cfa95
Modify out arch specific code from kvm_create function

This function removes all x86 specific code and creates
a hook function kv_arch_create to accomidate for this code.

This patch also moves the following funcitons to libkvm-x86.c:
kvm_set_tss_addr
kvm_set_init_tss
kvm_create_default_phys_mem

This patch moves function kvm_create_default_phys_mem to libkvm-x86.
This function is arch specific to x86 and also today no one allocates
guest memory in the kernel. To remove confusion the function has
been renameed kvm_x86_create_default_phys_mem

Signed-off-by: Jerone Young <jyoung5-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

diff --git a/libkvm/kvm-common.h b/libkvm/kvm-common.h
--- a/libkvm/kvm-common.h
+++ b/libkvm/kvm-common.h
@@ -59,4 +59,10 @@ void kvm_userspace_memory_region_save_pa
struct kvm_userspace_memory_region *mem);
void kvm_memory_region_clear_params(kvm_context_t kvm, int regnum);

+int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
+ void **vm_mem);
+int kvm_arch_create_default_phys_mem(kvm_context_t kvm,
+ unsigned long phys_mem_bytes,
+ void **vm_mem);
+
#endif
diff --git a/libkvm/kvm-x86.h b/libkvm/kvm-x86.h
--- a/libkvm/kvm-x86.h
+++ b/libkvm/kvm-x86.h
@@ -30,4 +30,7 @@ int kvm_alloc_userspace_memory(kvm_conte
int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
void **vm_mem);

+int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr);
+
+
#endif
diff --git a/libkvm/libkvm-x86.c b/libkvm/libkvm-x86.c
--- a/libkvm/libkvm-x86.c
+++ b/libkvm/libkvm-x86.c
@@ -7,6 +7,10 @@
#include <stropts.h>
#include <sys/mman.h>
#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>

int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
void **vm_mem)
@@ -186,3 +190,75 @@ int kvm_alloc_userspace_memory(kvm_conte

#endif

+int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr)
+{
+#ifdef KVM_CAP_SET_TSS_ADDR
+ int r;
+
+ r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
+ if (r > 0) {
+ r = ioctl(kvm->vm_fd, KVM_SET_TSS_ADDR, addr);
+ if (r == -1) {
+ fprintf(stderr, "kvm_set_tss_addr: %m\n");
+ return -errno;
+ }
+ return 0;
+ }
+#endif
+ return -ENOSYS;
+}
+
+static int kvm_init_tss(kvm_context_t kvm)
+{
+#ifdef KVM_CAP_SET_TSS_ADDR
+ int r;
+
+ r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
+ if (r > 0) {
+ /*
+ * this address is 3 pages before the bios, and the bios should present
+ * as unavaible memory
+ */
+ r = kvm_set_tss_addr(kvm, 0xfffbd000);
+ if (r < 0) {
+ printf("kvm_init_tss: unable to set tss addr\n");
+ return r;
+ }
+
+ }
+#endif
+ return 0;
+}
+
+int kvm_arch_create_default_phys_mem(kvm_context_t kvm,
+ unsigned long phys_mem_bytes,
+ void **vm_mem)
+{
+ int zfd;
+
+ zfd = open("/dev/zero", O_RDONLY);
+ if (zfd == -1) {
+ perror("open /dev/zero");
+ return -1;
+ }
+ mmap(*vm_mem + 0xa8000, 0x8000, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE|MAP_FIXED, zfd, 0);
+ close(zfd);
+
+ return 0;
+}
+
+
+int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,
+ void **vm_mem)
+{
+ int r = 0;
+
+ r = kvm_init_tss(kvm);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+
diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c
--- a/libkvm/libkvm.c
+++ b/libkvm/libkvm.c
@@ -333,52 +333,11 @@ int kvm_create_vm(kvm_context_t kvm)
return 0;
}

-int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr)
-{
-#ifdef KVM_CAP_SET_TSS_ADDR
- int r;
-
- r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
- if (r > 0) {
- r = ioctl(kvm->vm_fd, KVM_SET_TSS_ADDR, addr);
- if (r == -1) {
- fprintf(stderr, "kvm_set_tss_addr: %m\n");
- return -errno;
- }
- return 0;
- }
-#endif
- return -ENOSYS;
-}
-
-static int kvm_init_tss(kvm_context_t kvm)
-{
-#ifdef KVM_CAP_SET_TSS_ADDR
- int r;
-
- r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
- if (r > 0) {
- /*
- * this address is 3 pages before the bios, and the bios should present
- * as unavaible memory
- */
- r = kvm_set_tss_addr(kvm, 0xfffbd000);
- if (r < 0) {
- printf("kvm_init_tss: unable to set tss addr\n");
- return r;
- }
-
- }
-#endif
- return 0;
-}
-
static int kvm_create_default_phys_mem(kvm_context_t kvm,
unsigned long phys_mem_bytes,
void **vm_mem)
{
unsigned long memory = (phys_mem_bytes + PAGE_SIZE - 1) & PAGE_MASK;
- int zfd;
int r;

#ifdef KVM_CAP_USER_MEMORY
@@ -391,10 +350,9 @@ static int kvm_create_default_phys_mem(k
if (r < 0)
return r;

- zfd = open("/dev/zero", O_RDONLY);
- mmap(*vm_mem + 0xa8000, 0x8000, PROT_READ|PROT_WRITE,
- MAP_PRIVATE|MAP_FIXED, zfd, 0);
- close(zfd);
+ r = kvm_arch_create_default_phys_mem(kvm, phys_mem_bytes, vm_mem);
+ if (r < 0)
+ return r;

kvm->physical_memory = *vm_mem;
return 0;
@@ -426,7 +384,7 @@ int kvm_create(kvm_context_t kvm, unsign
r = kvm_create_vm(kvm);
if (r < 0)
return r;
- r = kvm_init_tss(kvm);
+ r = kvm_arch_create(kvm, phys_mem_bytes, vm_mem);
if (r < 0)
return r;
init_slots();
diff --git a/libkvm/libkvm.h b/libkvm/libkvm.h
--- a/libkvm/libkvm.h
+++ b/libkvm/libkvm.h
@@ -412,7 +412,6 @@ int kvm_dump_vcpu(kvm_context_t kvm, int
*/
void kvm_show_regs(kvm_context_t kvm, int vcpu);

-int kvm_set_tss_addr(kvm_context_t kvm, unsigned long addr);

void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start,
unsigned long len, int log, int writable);