[kvm-unit-tests PATCH 4/4] lib/x86/vm: enable malloc and friends

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



We've had malloc, calloc, free, and memalign available for quite a
while, and arm and powerpc make use of them. x86 hasn't yet, but
let's give it the option. arm and powerpc use the early_alloc_ops,
built on phys_alloc, but x86 already has virtual memory management,
so we can build on that instead.

Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx>
---
 lib/x86/vm.c | 26 +++++++++++++++++++++++++-
 lib/x86/vm.h |  2 +-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/lib/x86/vm.c b/lib/x86/vm.c
index 0882e0a478ca..1e1b74bfb645 100644
--- a/lib/x86/vm.c
+++ b/lib/x86/vm.c
@@ -118,15 +118,39 @@ static void setup_mmu(unsigned long len)
     printf("cr4 = %lx\n", read_cr4());
 }
 
+static void *vcalloc(size_t nmemb, size_t size)
+{
+    return vmalloc(nmemb * size);
+}
+
+static void *vmemalign(size_t alignment, size_t size)
+{
+    size_t size_aligned;
+    void *addr;
+
+    assert(alignment && !(alignment & (alignment - 1)));
+    size_aligned = ALIGN(size, alignment);
+    addr = vmalloc(size_aligned);
+    return addr + (size_aligned - size);
+}
+
+static struct alloc_ops vm_alloc_ops = {
+    .malloc = vmalloc,
+    .calloc = vcalloc,
+    .free = vfree,
+    .memalign = vmemalign,
+};
+
 void setup_vm()
 {
     assert(!end_of_memory);
     end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
     heap_init(&edata, end_of_memory - (unsigned long)&edata);
     setup_mmu(end_of_memory);
+    alloc_ops = &vm_alloc_ops;
 }
 
-void *vmalloc(unsigned long size)
+void *vmalloc(size_t size)
 {
     void *mem, *p;
     unsigned pages;
diff --git a/lib/x86/vm.h b/lib/x86/vm.h
index 6a4384f5a48d..3c9a71d03cf9 100644
--- a/lib/x86/vm.h
+++ b/lib/x86/vm.h
@@ -7,7 +7,7 @@
 
 void setup_vm();
 
-void *vmalloc(unsigned long size);
+void *vmalloc(size_t size);
 void vfree(void *mem);
 void *vmap(unsigned long long phys, unsigned long size);
 void *alloc_vpage(void);
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux