[PATCH 03 of 19] Move fuction kvm_alloc_kernel_memory to libkvm-x86.c

Jerone Young
2007-11-02T18:24:52Z

# Date 1194027872 18000
# Node ID d92515231c6345b7a4b5388089a5edb4c323a392
# Parent 17bf778405bb2aea7eab2e4625f913941efeab1c
Move fuction kvm_alloc_kernel_memory to libkvm-x86.c

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

diff --git a/libkvm/Makefile b/libkvm/Makefile
--- a/libkvm/Makefile
+++ b/libkvm/Makefile
@@ -21,7 +21,7 @@ autodepend-flags = -MMD -MF $(dir $*).$(

all: libkvm.a

-libkvm.a: libkvm.o
+libkvm.a: libkvm.o $(libkvm-$(ARCH)-objs)
$(AR) rcs $@ $^

install:
diff --git a/libkvm/config-i386.mak b/libkvm/config-i386.mak
--- a/libkvm/config-i386.mak
+++ b/libkvm/config-i386.mak
@@ -2,3 +2,5 @@ LIBDIR := /lib
LIBDIR := /lib
CFLAGS += -m32
CFLAGS += -D__i386__
+
+libkvm-$(ARCH)-objs := libkvm-x86.o
diff --git a/libkvm/config-x86_64.mak b/libkvm/config-x86_64.mak
--- a/libkvm/config-x86_64.mak
+++ b/libkvm/config-x86_64.mak
@@ -2,3 +2,5 @@ LIBDIR := /lib64
LIBDIR := /lib64
CFLAGS += -m64
CFLAGS += -D__x86_64__
+
+libkvm-$(ARCH)-objs := libkvm-x86.o
diff --git a/libkvm/kvm-x86.h b/libkvm/kvm-x86.h
--- a/libkvm/kvm-x86.h
+++ b/libkvm/kvm-x86.h
@@ -21,4 +21,8 @@ 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_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
+ void **vm_mem);
+
#endif
diff --git a/libkvm/libkvm-x86.c b/libkvm/libkvm-x86.c
new file mode 100644
--- /dev/null
+++ b/libkvm/libkvm-x86.c
@@ -0,0 +1,81 @@
+#include "libkvm.h"
+#include "kvm-x86.h"
+#include <unistd.h>
+#include <stropts.h>
+#include <sys/mman.h>
+#include <stdio.h>
+
+int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
+ void **vm_mem)
+{
+ unsigned long dosmem = 0xa0000;
+ unsigned long exmem = 0xc0000;
+ unsigned long pcimem = 0xe0000000;
+ int r;
+ int tss_ext;
+ struct kvm_memory_region low_memory = {
+ .memory_size = memory < dosmem ? memory : dosmem,
+ .guest_phys_addr = 0,
+ };
+ struct kvm_memory_region extended_memory = {
+ .memory_size = memory < exmem ? 0 : memory - exmem,
+ .guest_phys_addr = exmem,
+ };
+ struct kvm_memory_region above_4g_memory = {
+ .memory_size = memory < pcimem ? 0 : memory - pcimem,
+ .guest_phys_addr = 0x100000000ULL,
+ };
+
+#ifdef KVM_CAP_SET_TSS_ADDR
+ tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
+#else
+ tss_ext = 0;
+#endif
+
+ if (memory >= pcimem)
+ extended_memory.memory_size = pcimem - exmem;
+
+ /* 640K should be enough. */
+ low_memory.slot = get_free_slot(kvm);
+ r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &low_memory);
+ if (r == -1) {
+ fprintf(stderr, "kvm_create_memory_region: %m\n");
+ return -1;
+ }
+ register_slot(low_memory.slot, low_memory.guest_phys_addr);
+
+ if (extended_memory.memory_size) {
+ if (tss_ext > 0)
+ extended_memory.slot = get_free_slot(kvm);
+ else
+ extended_memory.slot = 0;
+ r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &extended_memory);
+ if (r == -1) {
+ fprintf(stderr, "kvm_create_memory_region: %m\n");
+ return -1;
+ }
+ register_slot(extended_memory.slot,
+ extended_memory.guest_phys_addr);
+ }
+
+ if (above_4g_memory.memory_size) {
+ above_4g_memory.slot = get_free_slot(kvm);
+ r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &above_4g_memory);
+ if (r == -1) {
+ fprintf(stderr, "kvm_create_memory_region: %m\n");
+ return -1;
+ }
+ register_slot(above_4g_memory.slot,
+ above_4g_memory.guest_phys_addr);
+ }
+
+ kvm_memory_region_save_params(kvm, &low_memory);
+ kvm_memory_region_save_params(kvm, &extended_memory);
+ kvm_memory_region_save_params(kvm, &above_4g_memory);
+ if (above_4g_memory.memory_size)
+ kvm_memory_region_save_params(kvm, &above_4g_memory);
+
+ *vm_mem = mmap(NULL, memory, PROT_READ|PROT_WRITE, MAP_SHARED, kvm->vm_fd, 0);
+
+ return 0;
+}
diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c
--- a/libkvm/libkvm.c
+++ b/libkvm/libkvm.c
@@ -317,79 +317,6 @@ int kvm_get_shadow_pages(kvm_context_t k
return -1;
}

-int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
- void **vm_mem)
-{
- unsigned long dosmem = 0xa0000;
- unsigned long exmem = 0xc0000;
- unsigned long pcimem = 0xe0000000;
- int r;
- int tss_ext;
- struct kvm_memory_region low_memory = {
- .memory_size = memory < dosmem ? memory : dosmem,
- .guest_phys_addr = 0,
- };
- struct kvm_memory_region extended_memory = {
- .memory_size = memory < exmem ? 0 : memory - exmem,
- .guest_phys_addr = exmem,
- };
- struct kvm_memory_region above_4g_memory = {
- .memory_size = memory < pcimem ? 0 : memory - pcimem,
- .guest_phys_addr = 0x100000000ULL,
- };
-
-#ifdef KVM_CAP_SET_TSS_ADDR
- tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
-#else
- tss_ext = 0;
-#endif
-
- if (memory >= pcimem)
- extended_memory.memory_size = pcimem - exmem;
-
- /* 640K should be enough. */
- low_memory.slot = get_free_slot(kvm);
- r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &low_memory);
- if (r == -1) {
- fprintf(stderr, "kvm_create_memory_region: %m\n");
- return -1;
- }
- register_slot(low_memory.slot, low_memory.guest_phys_addr);
-
- if (extended_memory.memory_size) {
- if (tss_ext > 0)
- extended_memory.slot = get_free_slot(kvm);
- else
- extended_memory.slot = 0;
- r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &extended_memory);
- if (r == -1) {
- fprintf(stderr, "kvm_create_memory_region: %m\n");
- return -1;
- }
- register_slot(extended_memory.slot,
- extended_memory.guest_phys_addr);
- }
-
- if (above_4g_memory.memory_size) {
- above_4g_memory.slot = get_free_slot(kvm);
- r = ioctl(kvm->vm_fd, KVM_SET_MEMORY_REGION, &above_4g_memory);
- if (r == -1) {
- fprintf(stderr, "kvm_create_memory_region: %m\n");
- return -1;
- }
- register_slot(above_4g_memory.slot,
- above_4g_memory.guest_phys_addr);
- }
-
- kvm_memory_region_save_params(kvm, &low_memory);
- kvm_memory_region_save_params(kvm, &extended_memory);
- if (above_4g_memory.memory_size)
- kvm_memory_region_save_params(kvm, &above_4g_memory);
-
- *vm_mem = mmap(NULL, memory, PROT_READ|PROT_WRITE, MAP_SHARED, kvm->vm_fd, 0);
-
- return 0;
-}

#ifdef KVM_CAP_USER_MEMORY