mm: improve stability by making the page bitmap full by default, also implement liballoc_lock and liballoc_unlock

This commit is contained in:
2025-12-09 23:35:15 +03:00
parent 2a7f62e275
commit 51dc254aa3
3 changed files with 16 additions and 18 deletions
+9 -14
View File
@@ -8,11 +8,13 @@
int liballoc_lock() {
// Implement locking (disable interrupts or use spinlock)
asm volatile("cli");
return 0;
}
int liballoc_unlock() {
// Implement unlocking
asm volatile("sti");
return 0;
}
@@ -81,19 +83,18 @@ void* kvalloc(size_t npages) {
}
// Temporarily store physical addresses
void* phys_addrs[npages];
uint32_t vaddr = KHEAP_START + start * PAGE_SIZE;
// Allocate physical pages first
for (uint32_t i = 0; i < npages; i++) {
phys_addrs[i] = alloc_page();
if (!phys_addrs[i]) {
// Cleanup on failure
for (uint32_t j = 0; j < i; j++) {
free_page(phys_addrs[j]);
}
void* phys = alloc_page();
if (!phys) {
// Out of physical memory! Unmap what we just did and fail.
kvfree((void*)vaddr, i);
return NULL;
}
// Map the page
map_page(phys, (void*)(vaddr + i * PAGE_SIZE), PAGE_PRESENT | PAGE_RW);
}
// Mark virtual pages as allocated
@@ -101,12 +102,6 @@ void* kvalloc(size_t npages) {
kheap_bitmap_set(i);
}
// Map virtual to physical pages
for (uint32_t i = 0; i < npages; i++) {
map_page(phys_addrs[i], (void*)(vaddr + i * PAGE_SIZE),
PAGE_PRESENT | PAGE_RW);
}
return (void*)vaddr;
}
+1 -1
View File
@@ -45,7 +45,7 @@ void clear_bit(uint32_t page_index) {
void init_allocator() {
int i;
memset(page_bitmap, 0x00, BITMAP_SIZE);
memset(page_bitmap, 0xFF, BITMAP_SIZE);
}
void* alloc_page() {
+6 -3
View File
@@ -73,14 +73,14 @@ void grub_memory_map(unsigned int magic, struct multiboot_info* mbi)
printf("Start Addr: %x | Length: %x | Size: %x | Type: %x\n",
mmmt->addr_low, mmmt->len_low, mmmt->size, mmmt->type);
if(mmmt->type != MULTIBOOT_MEMORY_AVAILABLE) {
if(mmmt->type == MULTIBOOT_MEMORY_AVAILABLE) {
/*
* Do something with this memory block!
* BE WARNED that some of memory shown as availiable is actually
* actively being used by the kernel! You'll need to take that
* into account before writing to memory!
*/
printf("not available\n");
printf("available\n");
uint32_t size_in_pages = mmmt->len_low / PAGE_SIZE;
printf("size_in_pages: 0x%X\n", size_in_pages);
@@ -88,7 +88,10 @@ void grub_memory_map(unsigned int magic, struct multiboot_info* mbi)
uint32_t page_count = mmmt->len_low / PAGE_SIZE;
printf("page_base: %X, page_count: %X\n", base_page, page_count);
for (uint32_t j = 0; j < page_count; j++) {
set_bit(base_page + j);
// Only clear the bit if it fits in our bitmap
if ((base_page + j) < (BITMAP_SIZE * 8)) {
clear_bit(base_page + j);
}
}
}
}