[PATCH 04 of 19] Move kvm_alloc_userspace_memory to libkvm-x86.c

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

# Date 1194027872 18000
# Node ID 486d6818fcf62f9fda006e9e090bf1eba40e0e14
# Parent d92515231c6345b7a4b5388089a5edb4c323a392
Move kvm_alloc_userspace_memory to libkvm-x86.c

This moves x86 specific function kvm_alloc_userspace_memory() out
of libkvm.c into libkvm-x86.c.

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

diff --git a/libkvm/kvm-x86.h b/libkvm/kvm-x86.h
--- a/libkvm/kvm-x86.h
+++ b/libkvm/kvm-x86.h
@@ -25,4 +25,7 @@ int kvm_alloc_kernel_memory(kvm_context_
int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
void **vm_mem);

+int kvm_alloc_userspace_memory(kvm_context_t kvm, unsigned long memory,
+ void **vm_mem);
+
#endif
diff --git a/libkvm/libkvm-x86.c b/libkvm/libkvm-x86.c
--- a/libkvm/libkvm-x86.c
+++ b/libkvm/libkvm-x86.c
@@ -1,5 +1,8 @@
#include "libkvm.h"
#include "kvm-x86.h"
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <string.h>
#include <unistd.h>
#include <stropts.h>
#include <sys/mman.h>
@@ -79,3 +82,107 @@ int kvm_alloc_kernel_memory(kvm_context_

return 0;
}
+
+
+#ifdef KVM_CAP_USER_MEMORY
+
+int kvm_alloc_userspace_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_userspace_memory_region low_memory = {
+ .memory_size = memory < dosmem ? memory : dosmem,
+ .guest_phys_addr = 0,
+ };
+ struct kvm_userspace_memory_region extended_memory = {
+ .memory_size = memory < exmem ? 0 : memory - exmem,
+ .guest_phys_addr = exmem,
+ };
+ struct kvm_userspace_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;
+ *vm_mem = mmap(NULL, memory + 0x100000000ULL - pcimem,
+ PROT_READ|PROT_WRITE, MAP_ANONYMOUS |
+ MAP_SHARED, -1, 0);
+ }
+ else
+ *vm_mem = mmap(NULL, memory, PROT_READ|PROT_WRITE, MAP_ANONYMOUS
+ | MAP_SHARED, -1, 0);
+ if (*vm_mem == MAP_FAILED) {
+ fprintf(stderr, "kvm_alloc_userspace_memory: %s", strerror(errno));
+ return -1;
+ }
+
+ low_memory.userspace_addr = (unsigned long)*vm_mem;
+ low_memory.slot = get_free_slot(kvm);
+ /* 640K should be enough. */
+ r = ioctl(kvm->vm_fd, KVM_SET_USER_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) {
+ r = munmap(*vm_mem + dosmem, exmem - dosmem);
+ if (r == -1) {
+ fprintf(stderr, "kvm_alloc_userspace_memory: %s",
+ strerror(errno));
+ return -1;
+ }
+ extended_memory.userspace_addr = (unsigned long)(*vm_mem + exmem);
+ if (tss_ext > 0)
+ extended_memory.slot = get_free_slot(kvm);
+ else
+ extended_memory.slot = 0;
+ r = ioctl(kvm->vm_fd, KVM_SET_USER_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) {
+ r = munmap(*vm_mem + pcimem, 0x100000000ULL - pcimem);
+ if (r == -1) {
+ fprintf(stderr, "kvm_alloc_userspace_memory: %s",
+ strerror(errno));
+ return -1;
+ }
+ above_4g_memory.userspace_addr = (unsigned long)(*vm_mem + 0x100000000ULL);
+ above_4g_memory.slot = get_free_slot(kvm);
+ r = ioctl(kvm->vm_fd, KVM_SET_USER_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_userspace_memory_region_save_params(kvm, &low_memory);
+ kvm_userspace_memory_region_save_params(kvm, &extended_memory);
+ if (above_4g_memory.memory_size)
+ kvm_userspace_memory_region_save_params(kvm, &above_4g_memory);
+
+ return 0;
+}
+
+#endif
+
diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c
--- a/libkvm/libkvm.c
+++ b/libkvm/libkvm.c
@@ -317,108 +317,6 @@ int kvm_get_shadow_pages(kvm_context_t k
return -1;
}

-
-#ifdef KVM_CAP_USER_MEMORY
-
-int kvm_alloc_userspace_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_userspace_memory_region low_memory = {
- .memory_size = memory < dosmem ? memory : dosmem,
- .guest_phys_addr = 0,
- };
- struct kvm_userspace_memory_region extended_memory = {
- .memory_size = memory < exmem ? 0 : memory - exmem,
- .guest_phys_addr = exmem,
- };
- struct kvm_userspace_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;
- *vm_mem = mmap(NULL, memory + 0x100000000ULL - pcimem,
- PROT_READ|PROT_WRITE, MAP_ANONYMOUS |
- MAP_SHARED, -1, 0);
- }
- else
- *vm_mem = mmap(NULL, memory, PROT_READ|PROT_WRITE, MAP_ANONYMOUS
- | MAP_SHARED, -1, 0);
- if (*vm_mem == MAP_FAILED) {
- fprintf(stderr, "kvm_alloc_userspace_memory: %s", strerror(errno));
- return -1;
- }
-
- low_memory.userspace_addr = (unsigned long)*vm_mem;
- low_memory.slot = get_free_slot(kvm);
- /* 640K should be enough. */
- r = ioctl(kvm->vm_fd, KVM_SET_USER_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) {
- r = munmap(*vm_mem + dosmem, exmem - dosmem);
- if (r == -1) {
- fprintf(stderr, "kvm_alloc_userspace_memory: %s",
- strerror(errno));
- return -1;
- }
- extended_memory.userspace_addr = (unsigned long)(*vm_mem + exmem);
- if (tss_ext > 0)
- extended_memory.slot = get_free_slot(kvm);
- else
- extended_memory.slot = 0;
- r = ioctl(kvm->vm_fd, KVM_SET_USER_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) {
- r = munmap(*vm_mem + pcimem, 0x100000000ULL - pcimem);
- if (r == -1) {
- fprintf(stderr, "kvm_alloc_userspace_memory: %s",
- strerror(errno));
- return -1;
- }
- above_4g_memory.userspace_addr = (unsigned long)(*vm_mem + 0x100000000ULL);
- above_4g_memory.slot = get_free_slot(kvm);
- r = ioctl(kvm->vm_fd, KVM_SET_USER_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_userspace_memory_region_save_params(kvm, &low_memory);
- kvm_userspace_memory_region_save_params(kvm, &extended_memory);
- if (above_4g_memory.memory_size)
- kvm_userspace_memory_region_save_params(kvm, &above_4g_memory);
-
- return 0;
-}
-
-#endif

int kvm_create_vm(kvm_context_t kvm)
{