Skip to content

Commit 369d8a2

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

File tree

4 files changed

+181
-206
lines changed

4 files changed

+181
-206
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
@@ -1,74 +1,29 @@
11
#ifndef UKO_OS_KERNEL__MM_VIRTUAL_ALLOC_H
22
#define UKO_OS_KERNEL__MM_VIRTUAL_ALLOC_H 1
33

4-
#include <types.h>
4+
#include <list.h>
55

66
/**
7-
* Flags about a link of the free list.
7+
* A virtual memory area. This struct represents a region of either free or
8+
* allocated memory, with page granularity or greater.
89
*/
9-
enum virtual_buddy_free_list_flags : u16 {
10-
/**
11-
* This link was allocated in static memory as part of bootstrapping the
12-
* allocator, and should not be freed.
13-
*/
14-
VIRTUAL_BUDDY_FREE_LIST_BOOTSTRAP = 1 << 0,
15-
};
10+
struct vma;
1611

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

6317
/**
64-
* The kernel's virtual memory allocator.
18+
* Creates a new instance of the virtual memory allocator, set to cover the
19+
* given address range.
6520
*/
66-
extern struct virtual_buddy *const mm_kernel_virtual_buddy;
21+
struct vma_alloc *vma_alloc_new(uaddr lo, uaddr hi);
6722

6823
/**
69-
* Sets up `mm_kernel_virtual_buddy`.
24+
* Prints the VMA allocator's state, for debugging purposes.
7025
*/
71-
void mm_init_virtual(uptr free_va_start, uptr free_va_end);
26+
void vma_alloc_print(struct vma_alloc *vma_alloc);
7227

7328
/**
7429
* 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
@@ -28,15 +28,9 @@ void main(u64 hart_id, paddr devicetree_start, paddr kernel_start,
2828
panic("Failed to parse Devicetree");
2929
devicetree_add_entropy(devicetree);
3030
mm_init_physical(devicetree);
31-
devicetree_free(devicetree);
32-
33-
// uptr a = mm_va_alloc(mm_kernel_virtual_buddy, 2 * 1024 * 1024);
34-
// uptr b = mm_va_alloc(mm_kernel_virtual_buddy, 4096);
35-
// print("{uptr} {uptr}", a, b);
36-
usize i = 0;
37-
paddr addr;
38-
while (mm_alloc_physical(&addr))
39-
print("{usize} {paddr}", i++, addr);
31+
struct vma_alloc *kernel_virtual_allocator =
32+
vma_alloc_new(0xffffffe000000000, 0xffffffffc0000000);
33+
vma_alloc_print(kernel_virtual_allocator);
4034

4135
TODO();
4236
}

0 commit comments

Comments
 (0)