Skip to content

Commit c81df5c

Browse files
committed
Data structure definitions for the new virtual allocator.
Signed-off-by: Nathan Ringo <me@remexre.com>
1 parent 6b61f17 commit c81df5c

File tree

4 files changed

+308
-200
lines changed

4 files changed

+308
-200
lines changed

doc/kernel/mm/overview.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ukoOS has multiple strategies for memory management, that manage memory at diffe
1414

1515
- The kernel manages its own virtual memory, in the RAM region of the memory map.
1616

17-
This is a buddy allocator, and can serve power-of-two sized allocation requests.
1817
The allocator that handles these requests is called **the virtual memory allocator**.
1918

2019
Each hart has its own root page table, since it can be running a different userspace process.

src/kernel/include/mm/virtual_alloc.h

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,29 @@
77
#ifndef UKO_OS_KERNEL__MM_VIRTUAL_ALLOC_H
88
#define UKO_OS_KERNEL__MM_VIRTUAL_ALLOC_H 1
99

10-
#include <types.h>
10+
#include <list.h>
1111

1212
/**
13-
* Flags about a link of the free list.
13+
* A virtual memory area. This struct represents a region of either free or
14+
* allocated memory, with page granularity or greater.
1415
*/
15-
enum virtual_buddy_free_list_flags : u16 {
16-
/**
17-
* This link was allocated in static memory as part of bootstrapping the
18-
* allocator, and should not be freed.
19-
*/
20-
VIRTUAL_BUDDY_FREE_LIST_BOOTSTRAP = 1 << 0,
21-
};
16+
struct vma;
2217

2318
/**
24-
* A link in the free list for the buddy allocator for virtual address space.
25-
* These are heap-allocated.
19+
* An instance of the virtual memory allocator.
2620
*/
27-
struct virtual_buddy_free_list {
28-
/**
29-
* The next link in the list.
30-
*/
31-
struct virtual_buddy_free_list *next;
32-
33-
/**
34-
* Flags about this link.
35-
*/
36-
enum virtual_buddy_free_list_flags flags : 12;
37-
38-
/**
39-
* The high bits of the address of the allocatable block of memory.
40-
*/
41-
uptr addr_hi_bits : (sizeof(uptr) * 8) - 12;
42-
};
43-
44-
static_assert(sizeof(struct virtual_buddy_free_list) == 2 * sizeof(uptr));
45-
static_assert(alignof(struct virtual_buddy_free_list) == alignof(uptr));
46-
47-
/**
48-
* A buddy allocator for a 26-bit space. If pages are the leaves, this covers
49-
* 256GiB, i.e. either the lower or upper half of Sv39 (and still a convenient
50-
* size on x86_64, or on ARMv8-A with a 4KiB granule).
51-
*/
52-
struct virtual_buddy {
53-
/**
54-
* The range of virtual addresses this allocator covers.
55-
*/
56-
uaddr start, end;
57-
58-
/**
59-
* The free lists.
60-
*/
61-
struct virtual_buddy_free_list *free_lists[27];
62-
63-
/**
64-
* The bitmap.
65-
*/
66-
u8 bitmap[1 << 25];
67-
};
21+
struct vma_allocator;
6822

6923
/**
70-
* The kernel's virtual memory allocator.
24+
* Creates a new instance of the virtual memory allocator, set to cover the
25+
* given address range.
7126
*/
72-
extern struct virtual_buddy *const mm_kernel_virtual_buddy;
27+
struct vma_allocator *vma_allocator_new(uaddr lo, uaddr hi);
7328

7429
/**
75-
* Sets up `mm_kernel_virtual_buddy`.
30+
* Prints the VMA allocator's state, for debugging purposes.
7631
*/
77-
void mm_init_virtual(uptr free_va_start, uptr free_va_end);
32+
void vma_allocator_print(struct vma_allocator *allocator);
7833

7934
/**
8035
* Allocates a range of virtual address space from the given allocator.

src/kernel/main.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ void main(u64 hart_id, paddr devicetree_start, paddr kernel_start,
3434
panic("Failed to parse Devicetree");
3535
devicetree_add_entropy(devicetree);
3636
mm_init_physical(devicetree);
37-
devicetree_free(devicetree);
38-
39-
// uptr a = mm_va_alloc(mm_kernel_virtual_buddy, 2 * 1024 * 1024);
40-
// uptr b = mm_va_alloc(mm_kernel_virtual_buddy, 4096);
41-
// print("{uptr} {uptr}", a, b);
42-
usize i = 0;
43-
paddr addr;
44-
while (mm_alloc_physical(&addr))
45-
print("{usize} {paddr}", i++, addr);
37+
struct vma_allocator *kernel_virtual_allocator =
38+
vma_allocator_new(0xffffffe000000000, 0xffffffffc0000000);
39+
vma_allocator_print(kernel_virtual_allocator);
4640

4741
TODO();
4842
}

0 commit comments

Comments
 (0)