xref: /kvm-unit-tests/lib/vmalloc.c (revision 937e239220378a7417e3b6e7cfc6e0fe1223c440)
1 /*
2  * Copyright (C) 2012, 2017, Red Hat Inc.
3  *
4  * This allocator provides contiguous physical addresses with page
5  * granularity.
6  */
7 
8 #include "libcflat.h"
9 #include "asm/spinlock.h"
10 #include "asm/page.h"
11 #include "asm/io.h"
12 #include "alloc_phys.h"
13 #include "alloc_page.h"
14 #include "vmalloc.h"
15 
16 static struct spinlock lock;
17 static void *vfree_top = 0;
18 static void *page_root;
19 
20 void *alloc_vpages(ulong nr)
21 {
22 	spin_lock(&lock);
23 	vfree_top -= PAGE_SIZE * nr;
24 	spin_unlock(&lock);
25 	return vfree_top;
26 }
27 
28 void *alloc_vpage(void)
29 {
30 	return alloc_vpages(1);
31 }
32 
33 void init_alloc_vpage(void *top)
34 {
35 	vfree_top = top;
36 }
37 
38 void setup_vm()
39 {
40 	phys_addr_t base, top;
41 	phys_alloc_get_unused(&base, &top);
42 	base = (base + PAGE_SIZE - 1) & -PAGE_SIZE;
43 	top = top & -PAGE_SIZE;
44 	free_pages(phys_to_virt(base), top - base);
45 	page_root = setup_mmu(top);
46 }
47