Mass convert vmalloc -> vsalloc
authorJack Miller <jack@codezen.org>
Mon, 21 Mar 2016 18:40:26 +0000 (13:40 -0500)
committerJack Miller <jack@codezen.org>
Mon, 21 Mar 2016 18:47:59 +0000 (13:47 -0500)
Malloc implies there's actual memory allocated (like Linux's vmalloc()),
so rename it to vsalloc (virtual space alloc), since valloc() also has a
loaded (if obsolete) meaning.

include/memmap.h
include/vmalloc.h [deleted file]
include/vsalloc.h [new file with mode: 0644]
kernel/acpi.c
kernel/apic.c
kernel/vga.c
mm/page_alloc.c
mm/vmalloc.c [deleted file]
mm/vsalloc.c [new file with mode: 0644]

index 85cfeca..a9f3ad2 100644 (file)
 #define KERNEL_VIRT            (VIRT_BASE | KERNEL_START)
 
 #define VGA_MEM_VIRTUAL                        0xFFFFFFFC00000000
-#define VMALLOC_HEAP_VIRTUAL   0xFFFFFFFD00000000
+#define VSALLOC_HEAP_VIRTUAL   0xFFFFFFFD00000000
 #define PAGE_HEAP_VIRTUAL              0xFFFFFFFE00000000
 #define ACPI_HEAP_VIRTUAL              0xFFFFFFFF00000000
 
diff --git a/include/vmalloc.h b/include/vmalloc.h
deleted file mode 100644 (file)
index f4827fa..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef VMALLOC_H
-#define VMALLOC_H
-
-#include <types.h>
-
-void vmalloc_init(void);
-
-void *vmalloc(char *name, u64 address_hint, u64 size);
-void vfree(u64 address, u64 size);
-void vmalloc_vspace(char *name, u64 address, u64 size);
-
-#endif
diff --git a/include/vsalloc.h b/include/vsalloc.h
new file mode 100644 (file)
index 0000000..7837239
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef VSALLOC_H
+#define VSALLOC_H
+
+#include <types.h>
+
+void vsalloc_init(void);
+
+void *vsalloc(char *name, u64 address_hint, u64 size);
+void vfree(u64 address, u64 size);
+void vsalloc_vspace(char *name, u64 address, u64 size);
+
+#endif
index cd946ec..2492b16 100644 (file)
@@ -1,7 +1,7 @@
 /* vim: set ts=4 sw=4 sts=4 noet : */
 
 #include <types.h>
-#include <vmalloc.h>
+#include <vsalloc.h>
 #include <memmap.h>
 #include <map.h>
 #include <console.h>
@@ -121,7 +121,7 @@ struct rsdp *__acpi_find_rsdp(u64 *bios_mem)
 
 void __acpi_handle_table(u32 pa)
 {
-       void *allocation = vmalloc("ACPI", ACPI_HEAP_VIRTUAL, 1);
+       void *allocation = vsalloc("ACPI", ACPI_HEAP_VIRTUAL, 1);
        struct description_header *hdr;
        struct acpi_section_handler *cur = handlers;
 
@@ -154,7 +154,7 @@ int acpi_init(void)
        int ret = 0;
        int i;
 
-       u64 *mem = vmalloc("ACPI", ACPI_HEAP_VIRTUAL, BIOS_ROM_PAGES);
+       u64 *mem = vsalloc("ACPI", ACPI_HEAP_VIRTUAL, BIOS_ROM_PAGES);
 
        if(map_pages((u64) mem, BIOS_ROM_START, BIOS_ROM_PAGES)) {
                printk("Failed to map.\n");
@@ -171,7 +171,7 @@ int acpi_init(void)
                goto cleanup;
        }
 
-       hdr = vmalloc("ACPI", ACPI_HEAP_VIRTUAL, 1);
+       hdr = vsalloc("ACPI", ACPI_HEAP_VIRTUAL, 1);
 
        map_pages((u64) hdr, rsdp->rsdt_pa, 1);
        hdr = (struct description_header *)  ((u64) hdr + PAGE_ALIGN_OFF(rsdp->rsdt_pa));
index 4221cc8..624c40a 100644 (file)
@@ -14,7 +14,7 @@
 #include <console.h>
 #include <cpuid.h>
 #include <memmap.h>
-#include <vmalloc.h>
+#include <vsalloc.h>
 #include <map.h>
 #include <apic.h>
 #include <mmio.h>
@@ -103,7 +103,7 @@ static void xapic_init(void)
        wrmsr(MSR_IA32_APIC_BASE, msr | XAPIC_ENABLE);
 
        /* Get a nocache virtual memory of the base memory */
-       mmio_mem = vmalloc("APIC", VMALLOC_HEAP_VIRTUAL, PAGE_SIZE);
+       mmio_mem = vsalloc("APIC", VSALLOC_HEAP_VIRTUAL, PAGE_SIZE);
 
        map_page_nocache((u64) mmio_mem, msr & ~(0xFFFUL));
 }
index 1bd0d9e..9f75c2e 100644 (file)
@@ -1,6 +1,6 @@
 /* vim: set ts=4 sw=4 sts=4 noet : */
 
-#include <vmalloc.h>
+#include <vsalloc.h>
 #include <memmap.h>
 #include <string.h>
 #include <map.h>
@@ -53,7 +53,7 @@ void vga_clear(void)
 
 void vga_init(void)
 {
-       vga_mem = vmalloc("VGA mem", VGA_MEM_VIRTUAL, PAGE_SIZE);
+       vga_mem = vsalloc("VGA mem", VGA_MEM_VIRTUAL, PAGE_SIZE);
        map_page((u64) vga_mem, 0xB8000);
        vga_clear();
 }
index 2f355ed..c80d904 100644 (file)
@@ -18,7 +18,7 @@
 #include <grub.h>
 #include <errno.h>
 #include <types.h>
-#include <vmalloc.h>
+#include <vsalloc.h>
 #include <map.h>
 
 #define TEST_PAGE_ALLOC
@@ -290,7 +290,7 @@ void *page_alloc(u32 order)
        if (order > MAX_ORDER)
                return NULL;
 
-       virtual = vmalloc("page heap", PAGE_HEAP_VIRTUAL, (1 << (order + PAGE_SHIFT)));
+       virtual = vsalloc("page heap", PAGE_HEAP_VIRTUAL, (1 << (order + PAGE_SHIFT)));
 
        if (virtual == NULL)
                return NULL;
@@ -829,12 +829,12 @@ int page_alloc_init(struct grub_info *info_phys, u64 end_structures)
        /* and all of our virtual address space as allocated  */
        /* page_alloc_to_virtual has to be working before now */
 
-       vmalloc_init();
-       vmalloc_vspace("grub info", (u64) info, 1);
-       vmalloc_vspace("grub mmap", (u64) mmap, 1);
-       vmalloc_vspace("kernel stack", (KERNEL_VIRT - stack_size), stack_size);
-       vmalloc_vspace("kernel", KERNEL_VIRT, kernel_size);
-       vmalloc_vspace("page allocator", (VIRT_BASE | alloc_address), alloc_size);
+       vsalloc_init();
+       vsalloc_vspace("grub info", (u64) info, 1);
+       vsalloc_vspace("grub mmap", (u64) mmap, 1);
+       vsalloc_vspace("kernel stack", (KERNEL_VIRT - stack_size), stack_size);
+       vsalloc_vspace("kernel", KERNEL_VIRT, kernel_size);
+       vsalloc_vspace("page allocator", (VIRT_BASE | alloc_address), alloc_size);
 
        return 0;
 }
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
deleted file mode 100644 (file)
index 3dc3ff2..0000000
+++ /dev/null
@@ -1,344 +0,0 @@
-/* vim: set ts=4 sw=4 sts=4 noet : */
-
-/* Virtual address space allocator */
-
-#include <memmap.h>
-#include <page_alloc.h>
-#include <vmalloc.h>
-#include <types.h>
-#include <errno.h>
-
-#define TEST_VMALLOC
-
-/* NOTE: The names are included here, but as we have no string functions, or
- * even display capability, these are unused for the moment */
-
-struct virtual_block {
-       u64 address;
-       u64 size;
-       char *name;
-       struct virtual_block *next;
-};
-
-static struct virtual_block *virtual_alloc = NULL;
-static struct virtual_block *spare_blocks = NULL;
-static int alloc_order = 0;
-u32 used_vblocks = 0;
-
-/* Eventually this will be part of the kernel's task struct */
-struct virtual_block *kernel_virtual_space = NULL;
-
-static struct virtual_block *pop_virtual_block(void)
-{
-       struct virtual_block *ret = &virtual_alloc[used_vblocks];
-
-       if (spare_blocks) {
-               ret = spare_blocks;
-               spare_blocks = ret->next;
-               return ret;
-       }
-
-       if (ret > (virtual_alloc + (1 << (alloc_order + PAGE_SHIFT)))) {
-                       while(1);
-       }
-
-       used_vblocks++;
-       return ret;
-}
-
-static void push_virtual_block(struct virtual_block *block)
-{
-       block->next = spare_blocks;
-       spare_blocks = block;
-}
-
-void __alloc_vspace(struct virtual_block **head, char *name, u64 virtual, u64 size)
-{
-       struct virtual_block *cur, *prev, *new;
-       u64 end, cur_end;
-
-       size = PAGE_ALIGN_UP(size + PAGE_ALIGN_OFF(virtual));
-       virtual = PAGE_ALIGN(virtual);
-
-       end = virtual + size;
-
-       prev = NULL;
-       for (cur = *head; cur; prev = cur, cur = cur->next) {
-               cur_end = (cur->address + cur->size);
-
-               /* If our virtual is in this block or adjacent, expand it */
-
-               if (cur->address <= virtual) {
-                       /* cur is adjacent left */
-
-                       /* Start in block */
-                       if (virtual <= cur_end) {
-                               /* Overhangs right */
-                               if (end > cur_end) {
-                                       cur->size += (end - cur_end);
-
-                                       /* Since we're adjusting this size, check if we're now
-                                        * adjacent to the next one */
-
-                                       if (cur->next && cur->next->address <= end) {
-                                               cur->size += cur->next->size - (end - cur->next->address);
-
-                                               /* pointer reuse, new is actually old =P */
-                                               new = cur->next;
-                                               cur->next = new->next;
-                                               push_virtual_block(new);
-                                       }
-                               }
-
-                               return;
-                       }
-
-               /* cur is adjacent right */
-
-               /* Ends in block */
-
-               } else if (cur->address <= end) {
-                       cur->size += size - (end - cur->address);
-                       cur->address = virtual;
-                       return;
-               }
-
-               /* Is not connected, but this is virtual's place in the list */
-               else if (cur->address > virtual) {
-                       break;
-               }
-       }
-
-       new = pop_virtual_block();
-       new->name = name;
-       new->address = virtual;
-       new->size = size;
-
-       if (prev)
-               prev->next = new;
-       else
-               *head = new;
-       new->next = cur;
-}
-
-int __free_vspace(struct virtual_block **head, u64 virtual, u64 size)
-{
-       struct virtual_block *cur, *prev, *new;
-       u64 end, cur_end;
-
-       end = virtual + size;
-
-       prev = NULL;
-       for (cur = *head; cur; prev = cur, cur = cur->next) {
-               cur_end = cur->address + cur->size;
-
-               if (cur->address > virtual)
-                       return -ENOENT;
-
-               if(cur->address == virtual) {
-                       if(size < cur->size) {
-                               /* Shrink */
-                               cur->address = virtual + size;
-                               cur->size = cur->size - size;
-                       }
-                       else {
-                               /* Remove */
-                               if (prev)
-                                       prev->next = cur->next;
-                               else
-                                       *head = cur->next;
-                               push_virtual_block(cur);
-                       }
-                       break;
-               }
-               else if (cur_end > virtual) {
-                       if (end >= cur_end)
-                               cur->size -= (cur_end - virtual);
-                       else {
-                               /* Shrink cur, add a new block for the remaining shard */
-                               cur->size = (virtual - cur->address);
-
-                               new = pop_virtual_block();
-                               new->address = end;
-                               new->size = (cur_end - end);
-                               new->name = cur->name;
-
-                               new->next = cur->next;
-                               cur->next = new;
-                       }
-                       break;
-               }
-       }
-
-       return 0;
-}
-
-void *vmalloc(char *name, u64 address_hint, u64 size)
-{
-       struct virtual_block *cur;
-       u64 prev_end, end;
-
-       prev_end = 0;
-       for(cur = kernel_virtual_space; cur; cur = cur->next) {
-               end = cur->address + cur->size;
-               if ((cur->address >= address_hint)) {
-                       if ((cur->address != address_hint) && size <= (cur->address - prev_end)) {
-                               __alloc_vspace(&kernel_virtual_space, name, address_hint, size);
-                               return (void *) address_hint;
-                       } else
-                               address_hint = end;
-               }
-
-               prev_end = end;
-       }
-
-       if ((MAX_VIRT_ADDRESS - prev_end) >= size) {
-               __alloc_vspace(&kernel_virtual_space, name, address_hint, size);
-               return (void *) prev_end;
-       }
-
-       return NULL;
-}
-
-
-#ifdef TEST_VMALLOC
-
-struct virtual_block *test_virtual_space = NULL;
-
-static inline void __vblock_assert(u32 index, u64 address, u64 size, int next_null)
-{
-       struct virtual_block *block;
-       int i = 0;
-
-       block = test_virtual_space;
-       for (i = 0; i < index; i++)
-               block = block->next;
-
-       if (block->address != address)
-               while(1) {};
-       if (block->size != size)
-               while(1) {};
-       if (next_null && block->next)
-               while(1) {};
-       if (!next_null && !block->next)
-               while(1) {};
-}
-
-void test_alloc_vspace(void)
-{
-       __alloc_vspace(&test_virtual_space, "test", 0x1000, 0x1000);
-
-       __vblock_assert(0, 0x1000, 0x1000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x2000, 0x1000);
-
-       __vblock_assert(0, 0x1000, 0x2000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x0, 0x1000);
-
-       __vblock_assert(0, 0x0, 0x3000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x3000, 0x10000);
-
-       __vblock_assert(0, 0x0, 0x13000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x20000, 0x10000);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x10000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x70000, 0x10000);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x10000, 0);
-       __vblock_assert(2, 0x70000, 0x10000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0xf0000, 0x10000);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x10000, 0);
-       __vblock_assert(2, 0x70000, 0x10000, 0);
-       __vblock_assert(3, 0xf0000, 0x10000, 1);
-
-       __alloc_vspace(&test_virtual_space, "test", 0x80000, 0x70000);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x10000, 0);
-       __vblock_assert(2, 0x70000, 0x90000, 1);
-
-       __free_vspace(&test_virtual_space, 0xf0000, 0x800);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x10000, 0);
-       __vblock_assert(2, 0x70000, 0x80000, 0);
-       __vblock_assert(3, 0xf0800, 0xf800, 1);
-
-       __free_vspace(&test_virtual_space, 0x21000, 0x8000);
-
-       __vblock_assert(0, 0x0, 0x13000, 0);
-       __vblock_assert(1, 0x20000, 0x1000, 0);
-       __vblock_assert(2, 0x29000, 0x7000, 0);
-       __vblock_assert(3, 0x70000, 0x80000, 0);
-       __vblock_assert(4, 0xf0800, 0xf800, 1);
-
-       __free_vspace(&test_virtual_space, 0x11000, 0x2000);
-
-       __vblock_assert(0, 0x0, 0x11000, 0);
-       __vblock_assert(1, 0x20000, 0x1000, 0);
-       __vblock_assert(2, 0x29000, 0x7000, 0);
-       __vblock_assert(3, 0x70000, 0x80000, 0);
-       __vblock_assert(4, 0xf0800, 0xf800, 1);
-
-       __free_vspace(&test_virtual_space, 0x70000, 0x10000);
-
-       __vblock_assert(0, 0x0, 0x11000, 0);
-       __vblock_assert(1, 0x20000, 0x1000, 0);
-       __vblock_assert(2, 0x29000, 0x7000, 0);
-       __vblock_assert(3, 0x80000, 0x70000, 0);
-       __vblock_assert(4, 0xf0800, 0xf800, 1);
-
-       /* Reset */
-       used_vblocks = 0;
-       test_virtual_space = NULL;
-}
-
-void test_vmalloc(void)
-{
-       u64 *test = vmalloc("test", VMALLOC_HEAP_VIRTUAL, 0);
-       u64 *test2 = vmalloc("test", VMALLOC_HEAP_VIRTUAL, 0);
-
-       if (test == test2)
-               while(1) {}
-
-       used_vblocks = 0;
-       kernel_virtual_space = NULL;
-}
-
-#endif
-
-void vmalloc_vspace(char *name, u64 address, u64 size)
-{
-       __alloc_vspace(&kernel_virtual_space, name, address, size);
-}
-
-void vfree(u64 address, u64 size)
-{
-       __free_vspace(&kernel_virtual_space, address, size);
-}
-
-extern struct early_reserve early_reserves_start;
-extern struct early_reserve early_reserves_end;
-
-void vmalloc_init(void)
-{
-       /* Grab a page to work with */
-
-       virtual_alloc = page_alloc_to_virtual(VMALLOC_HEAP_VIRTUAL, alloc_order);
-
-#ifdef TEST_VMALLOC
-       test_alloc_vspace();
-       test_vmalloc();
-#endif
-
-       __alloc_vspace(&kernel_virtual_space, "vmalloc", VMALLOC_HEAP_VIRTUAL, (1 << (alloc_order + PAGE_SHIFT)));
-}
diff --git a/mm/vsalloc.c b/mm/vsalloc.c
new file mode 100644 (file)
index 0000000..7039d52
--- /dev/null
@@ -0,0 +1,344 @@
+/* vim: set ts=4 sw=4 sts=4 noet : */
+
+/* Virtual address space allocator */
+
+#include <memmap.h>
+#include <page_alloc.h>
+#include <vsalloc.h>
+#include <types.h>
+#include <errno.h>
+
+#define TEST_VSALLOC
+
+/* NOTE: The names are included here, but as we have no string functions, or
+ * even display capability, these are unused for the moment */
+
+struct virtual_block {
+       u64 address;
+       u64 size;
+       char *name;
+       struct virtual_block *next;
+};
+
+static struct virtual_block *virtual_alloc = NULL;
+static struct virtual_block *spare_blocks = NULL;
+static int alloc_order = 0;
+u32 used_vblocks = 0;
+
+/* Eventually this will be part of the kernel's task struct */
+struct virtual_block *kernel_virtual_space = NULL;
+
+static struct virtual_block *pop_virtual_block(void)
+{
+       struct virtual_block *ret = &virtual_alloc[used_vblocks];
+
+       if (spare_blocks) {
+               ret = spare_blocks;
+               spare_blocks = ret->next;
+               return ret;
+       }
+
+       if (ret > (virtual_alloc + (1 << (alloc_order + PAGE_SHIFT)))) {
+                       while(1);
+       }
+
+       used_vblocks++;
+       return ret;
+}
+
+static void push_virtual_block(struct virtual_block *block)
+{
+       block->next = spare_blocks;
+       spare_blocks = block;
+}
+
+void __alloc_vspace(struct virtual_block **head, char *name, u64 virtual, u64 size)
+{
+       struct virtual_block *cur, *prev, *new;
+       u64 end, cur_end;
+
+       size = PAGE_ALIGN_UP(size + PAGE_ALIGN_OFF(virtual));
+       virtual = PAGE_ALIGN(virtual);
+
+       end = virtual + size;
+
+       prev = NULL;
+       for (cur = *head; cur; prev = cur, cur = cur->next) {
+               cur_end = (cur->address + cur->size);
+
+               /* If our virtual is in this block or adjacent, expand it */
+
+               if (cur->address <= virtual) {
+                       /* cur is adjacent left */
+
+                       /* Start in block */
+                       if (virtual <= cur_end) {
+                               /* Overhangs right */
+                               if (end > cur_end) {
+                                       cur->size += (end - cur_end);
+
+                                       /* Since we're adjusting this size, check if we're now
+                                        * adjacent to the next one */
+
+                                       if (cur->next && cur->next->address <= end) {
+                                               cur->size += cur->next->size - (end - cur->next->address);
+
+                                               /* pointer reuse, new is actually old =P */
+                                               new = cur->next;
+                                               cur->next = new->next;
+                                               push_virtual_block(new);
+                                       }
+                               }
+
+                               return;
+                       }
+
+               /* cur is adjacent right */
+
+               /* Ends in block */
+
+               } else if (cur->address <= end) {
+                       cur->size += size - (end - cur->address);
+                       cur->address = virtual;
+                       return;
+               }
+
+               /* Is not connected, but this is virtual's place in the list */
+               else if (cur->address > virtual) {
+                       break;
+               }
+       }
+
+       new = pop_virtual_block();
+       new->name = name;
+       new->address = virtual;
+       new->size = size;
+
+       if (prev)
+               prev->next = new;
+       else
+               *head = new;
+       new->next = cur;
+}
+
+int __free_vspace(struct virtual_block **head, u64 virtual, u64 size)
+{
+       struct virtual_block *cur, *prev, *new;
+       u64 end, cur_end;
+
+       end = virtual + size;
+
+       prev = NULL;
+       for (cur = *head; cur; prev = cur, cur = cur->next) {
+               cur_end = cur->address + cur->size;
+
+               if (cur->address > virtual)
+                       return -ENOENT;
+
+               if(cur->address == virtual) {
+                       if(size < cur->size) {
+                               /* Shrink */
+                               cur->address = virtual + size;
+                               cur->size = cur->size - size;
+                       }
+                       else {
+                               /* Remove */
+                               if (prev)
+                                       prev->next = cur->next;
+                               else
+                                       *head = cur->next;
+                               push_virtual_block(cur);
+                       }
+                       break;
+               }
+               else if (cur_end > virtual) {
+                       if (end >= cur_end)
+                               cur->size -= (cur_end - virtual);
+                       else {
+                               /* Shrink cur, add a new block for the remaining shard */
+                               cur->size = (virtual - cur->address);
+
+                               new = pop_virtual_block();
+                               new->address = end;
+                               new->size = (cur_end - end);
+                               new->name = cur->name;
+
+                               new->next = cur->next;
+                               cur->next = new;
+                       }
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+void *vsalloc(char *name, u64 address_hint, u64 size)
+{
+       struct virtual_block *cur;
+       u64 prev_end, end;
+
+       prev_end = 0;
+       for(cur = kernel_virtual_space; cur; cur = cur->next) {
+               end = cur->address + cur->size;
+               if ((cur->address >= address_hint)) {
+                       if ((cur->address != address_hint) && size <= (cur->address - prev_end)) {
+                               __alloc_vspace(&kernel_virtual_space, name, address_hint, size);
+                               return (void *) address_hint;
+                       } else
+                               address_hint = end;
+               }
+
+               prev_end = end;
+       }
+
+       if ((MAX_VIRT_ADDRESS - prev_end) >= size) {
+               __alloc_vspace(&kernel_virtual_space, name, address_hint, size);
+               return (void *) prev_end;
+       }
+
+       return NULL;
+}
+
+
+#ifdef TEST_VSALLOC
+
+struct virtual_block *test_virtual_space = NULL;
+
+static inline void __vblock_assert(u32 index, u64 address, u64 size, int next_null)
+{
+       struct virtual_block *block;
+       int i = 0;
+
+       block = test_virtual_space;
+       for (i = 0; i < index; i++)
+               block = block->next;
+
+       if (block->address != address)
+               while(1) {};
+       if (block->size != size)
+               while(1) {};
+       if (next_null && block->next)
+               while(1) {};
+       if (!next_null && !block->next)
+               while(1) {};
+}
+
+void test_alloc_vspace(void)
+{
+       __alloc_vspace(&test_virtual_space, "test", 0x1000, 0x1000);
+
+       __vblock_assert(0, 0x1000, 0x1000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x2000, 0x1000);
+
+       __vblock_assert(0, 0x1000, 0x2000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x0, 0x1000);
+
+       __vblock_assert(0, 0x0, 0x3000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x3000, 0x10000);
+
+       __vblock_assert(0, 0x0, 0x13000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x20000, 0x10000);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x10000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x70000, 0x10000);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x10000, 0);
+       __vblock_assert(2, 0x70000, 0x10000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0xf0000, 0x10000);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x10000, 0);
+       __vblock_assert(2, 0x70000, 0x10000, 0);
+       __vblock_assert(3, 0xf0000, 0x10000, 1);
+
+       __alloc_vspace(&test_virtual_space, "test", 0x80000, 0x70000);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x10000, 0);
+       __vblock_assert(2, 0x70000, 0x90000, 1);
+
+       __free_vspace(&test_virtual_space, 0xf0000, 0x800);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x10000, 0);
+       __vblock_assert(2, 0x70000, 0x80000, 0);
+       __vblock_assert(3, 0xf0800, 0xf800, 1);
+
+       __free_vspace(&test_virtual_space, 0x21000, 0x8000);
+
+       __vblock_assert(0, 0x0, 0x13000, 0);
+       __vblock_assert(1, 0x20000, 0x1000, 0);
+       __vblock_assert(2, 0x29000, 0x7000, 0);
+       __vblock_assert(3, 0x70000, 0x80000, 0);
+       __vblock_assert(4, 0xf0800, 0xf800, 1);
+
+       __free_vspace(&test_virtual_space, 0x11000, 0x2000);
+
+       __vblock_assert(0, 0x0, 0x11000, 0);
+       __vblock_assert(1, 0x20000, 0x1000, 0);
+       __vblock_assert(2, 0x29000, 0x7000, 0);
+       __vblock_assert(3, 0x70000, 0x80000, 0);
+       __vblock_assert(4, 0xf0800, 0xf800, 1);
+
+       __free_vspace(&test_virtual_space, 0x70000, 0x10000);
+
+       __vblock_assert(0, 0x0, 0x11000, 0);
+       __vblock_assert(1, 0x20000, 0x1000, 0);
+       __vblock_assert(2, 0x29000, 0x7000, 0);
+       __vblock_assert(3, 0x80000, 0x70000, 0);
+       __vblock_assert(4, 0xf0800, 0xf800, 1);
+
+       /* Reset */
+       used_vblocks = 0;
+       test_virtual_space = NULL;
+}
+
+void test_vsalloc(void)
+{
+       u64 *test = vsalloc("test", VSALLOC_HEAP_VIRTUAL, 0);
+       u64 *test2 = vsalloc("test", VSALLOC_HEAP_VIRTUAL, 0);
+
+       if (test == test2)
+               while(1) {}
+
+       used_vblocks = 0;
+       kernel_virtual_space = NULL;
+}
+
+#endif
+
+void vsalloc_vspace(char *name, u64 address, u64 size)
+{
+       __alloc_vspace(&kernel_virtual_space, name, address, size);
+}
+
+void vfree(u64 address, u64 size)
+{
+       __free_vspace(&kernel_virtual_space, address, size);
+}
+
+extern struct early_reserve early_reserves_start;
+extern struct early_reserve early_reserves_end;
+
+void vsalloc_init(void)
+{
+       /* Grab a page to work with */
+
+       virtual_alloc = page_alloc_to_virtual(VSALLOC_HEAP_VIRTUAL, alloc_order);
+
+#ifdef TEST_VSALLOC
+       test_alloc_vspace();
+       test_vsalloc();
+#endif
+
+       __alloc_vspace(&kernel_virtual_space, "vsalloc", VSALLOC_HEAP_VIRTUAL, (1 << (alloc_order + PAGE_SHIFT)));
+}