vbos/mem/buddy.c
2026-04-11 11:35:31 -04:00

277 lines
6 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <limine/limine.h>
#include <kernel/common.h>
#include <kernel/buddy.h>
#include <kernel/mem.h>
#include <kernel/spinlock.h>
#include <arch/page.h>
#include <lib/stdio.h>
#include <lib/math.h>
#include <lib/bitwise.h>
#include <lib/string.h>
#define BBMAP_FREE 0
#define BBMAP_SPLIT 1
#define BBMAP_FULL 2
#define LEFT 0
#define RIGHT 1
struct buddy_zone {
size_t length;
size_t bitmap_length;
size_t bitmap_max_depth;
size_t pow2len;
spinlock_t lock;
char bitmap[];
};
static struct mem_zone *zones;
static size_t n_zones;
static void buddy_bitmap_set(char *bitmap, size_t depth, size_t n, int val)
{
size_t a = 2 << depth;
a -= 2;
if (depth == 0) {
a = 0;
n = 0;
}
n <<= 1;
bitmap_set(bitmap, n + a, val & 0x02);
bitmap_set(bitmap, n + a + 1, val & 0x01);
}
static uint8_t buddy_bitmap_get(char *bitmap, size_t depth, size_t n)
{
size_t a = 2 << depth;
a -= 2;
if (depth == 0) {
a = 0;
n = 0;
}
n <<= 1;
uint8_t ret = 0;
if (bitmap_get(bitmap, n + a))
ret |= 0x02;
if (bitmap_get(bitmap, n + a + 1))
ret |= 0x01;
return ret;
}
static void buddy_bitmap_mark_allocated(struct buddy_zone *zone, uintptr_t addr, size_t size)
{
if (npow2(size) != size) {
kerr( "buddy_bitmap_mark_allocated: bad size\n");
return;
}
size_t offset = addr - (uintptr_t)zone;
size_t depth = log2(npow2(zone->length)) - log2(size);
size_t n = offset / size;
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_FULL);
while (depth > 0) {
size_t buddy_n = n ^ 1;
u8 buddy_state = buddy_bitmap_get(zone->bitmap, depth, buddy_n);
depth -= 1;
n >>= 1;
if (buddy_state == BBMAP_FULL) {
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_FULL);
} else {
u8 parent_state = buddy_bitmap_get(zone->bitmap, depth, n);
if (parent_state == BBMAP_SPLIT)
break;
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_SPLIT);
}
}
}
static void buddy_bitmap_mark_free(struct buddy_zone *zone, uintptr_t addr, size_t size)
{
if (npow2(size) != size) {
kerr( "buddy_bitmap_mark_free: bad size\n");
return;
}
size_t offset = addr - ((uintptr_t)zone);
size_t depth = log2(npow2(zone->length)) - log2(size);
size_t n = offset / size;
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_FREE);
while (depth > 0) {
size_t buddy_n = n ^ 1;
depth -= 1;
n >>= 1;
u8 buddy_val = buddy_bitmap_get(zone->bitmap, depth + 1, buddy_n);
if (buddy_val == BBMAP_FREE) {
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_FREE);
} else {
buddy_bitmap_set(zone->bitmap, depth, n, BBMAP_SPLIT);
break;
}
}
}
static int buddy_find_recursive(struct buddy_zone *zone, size_t depth, size_t n, size_t tgt_depth, size_t *out_n)
{
u8 state = buddy_bitmap_get(zone->bitmap, depth, n);
if (state == BBMAP_FULL)
return -1;
if (depth == tgt_depth) {
if (state == BBMAP_FREE) {
*out_n = n;
return 0;
}
return -1;
}
if (buddy_find_recursive(zone, depth + 1, n << 1, tgt_depth, out_n) == 0)
return 0;
if (buddy_find_recursive(zone, depth + 1, (n << 1) | 1, tgt_depth, out_n) == 0)
return 0;
return -1;
}
static uintptr_t buddy_bitmap_find_free(struct buddy_zone *zone, size_t size)
{
if (size == 0 || size > zone->pow2len)
return 0;
size_t s = npow2(size);
size_t tgt_depth = log2(zone->pow2len) - log2(s);
size_t n = 0;
if (buddy_find_recursive(zone, 0, 0, tgt_depth, &n) == 0) {
return ((uintptr_t)zone) + (n * s);
}
return 0;
}
static size_t get_allocation_size(struct buddy_zone *zone, uintptr_t addr)
{
size_t offset = addr - (uintptr_t)zone;
size_t depth = zone->bitmap_max_depth;
while (depth >= 0) {
size_t n = offset / (PAGE_SIZE << (zone->bitmap_max_depth - depth));
if (buddy_bitmap_get(zone->bitmap, depth, n) == BBMAP_FULL)
return (PAGE_SIZE << (zone->bitmap_max_depth - depth));
if (depth == 0)
break;
depth--;
}
return 0;
}
static void *alloc_zone(struct buddy_zone *zone, size_t size)
{
if (size > npow2(zone->length) >> 1)
return NULL;
size = npow2(size);
uintptr_t ret = buddy_bitmap_find_free(zone, size);
if (ret)
buddy_bitmap_mark_allocated(zone, ret, size);
return (void *)ret;
}
void *buddy_alloc(size_t size)
{
for (int i = 0; i < n_zones; i++) {
struct buddy_zone *zone = (void *)zones[i].virt_base;
spinlock_acquire(&zone->lock);
void *ret = alloc_zone(zone, size);
spinlock_release(&zone->lock);
if (ret)
return ret;
}
return NULL;
}
void buddy_free(void *ptr)
{
uintptr_t addr = (uintptr_t)ptr;
for (int i = 0; i < n_zones; i++) {
struct buddy_zone *zone = (void *)zones[i].virt_base;
uintptr_t start = zones[i].virt_base;
uintptr_t end = start + zones[i].len;
spinlock_acquire(&zone->lock);
if (addr >= start && addr <= end) {
size_t alloc_size = get_allocation_size(zone, addr);
buddy_bitmap_mark_free(zone, addr, alloc_size);
}
spinlock_release(&zone->lock);
}
}
static void buddy_init_zone(struct mem_zone *zone)
{
struct buddy_zone *header = (void *)zone->virt_base;
memset(header, 0, sizeof(*header));
header->length = zone->len;
size_t pow2len = npow2(header->length);
header->bitmap_length = pow2len >> 13; /* 4 bits per page */
header->pow2len = pow2len;
header->bitmap_max_depth = log2(pow2len / PAGE_SIZE);
memset(header->bitmap, 0, header->bitmap_length);
size_t zone_meta_size = sizeof(struct buddy_zone) + header->bitmap_length;
buddy_bitmap_mark_allocated(header, zone->virt_base, npow2(zone_meta_size));
size_t current_offset = zone->len;
size_t total_end = pow2len;
while (current_offset < total_end) {
size_t remaining = total_end - current_offset;
size_t block_size = current_offset & -current_offset;
if (current_offset == 0)
block_size = pow2len;
while (block_size > remaining)
block_size >>= 1;
buddy_bitmap_mark_allocated(header, zone->virt_base + current_offset, block_size);
current_offset += block_size;
}
}
static inline void buddy_init_zones()
{
for (int i = 0; i < n_zones; i++) {
struct mem_zone *zone = zones + i;
buddy_init_zone(zone);
}
}
void buddy_init()
{
n_zones = mem_get_zones(&zones);
buddy_init_zones();
}