This commit is contained in:
nub31
2025-09-05 23:59:59 +02:00
parent 2aa44eaa51
commit bcf05c055e
6 changed files with 57 additions and 10 deletions

View File

@@ -108,7 +108,7 @@ section .text
add edi, 8
loop .setup_pd
; Load cr3 with the address of pml4
; Tell the cpu where pml4 is
mov eax, pml4
mov cr3, eax

View File

@@ -18,14 +18,29 @@ typedef struct
static memory_region_t usable_regions[USABLE_REGION_SIZE];
static size_t num_regions = 0;
#define BITMAP_SIZE 32768 // Supports up to 1GB of RAM
#define USABLE_REGION_SIZE 32
#define PAGE_SIZE 4096
#define KiB(count) ((u64)count * 1024)
#define MiB(count) (KiB((u64)count) * 1024)
#define GiB(count) (MiB((u64)count) * 1024)
#define TiB(count) (GiB((u64)count) * 1024)
// Fixed at 2mb for now
#define PAGE_SIZE MiB(2)
// Defines the theoretical max memory the kernel can allocate, not the actual memory of the system
#define MAX_MEMORY GiB(64)
#define BITMAP_PAGE_COUNT (MAX_MEMORY / PAGE_SIZE)
#define BITMAP_SIZE (BITMAP_PAGE_COUNT / 8)
static u8 page_bitmap[BITMAP_SIZE];
static u64 total_pages = 0;
static u64 free_pages = 0;
#define PTE_MASK 0x00000000FFFFF000
#define PTE_PRESENT 1
extern u64* pml4;
void mem_init(multiboot_info_t* info)
{
if (!(info->flags & (1 << 6)))
@@ -51,7 +66,7 @@ void mem_init(multiboot_info_t* info)
}
else
{
printf("System has more memory than the memory map can hold\n");
printf("System has more memory than the usable memory map can hold\n");
break;
}
}
@@ -84,11 +99,11 @@ void mem_init(multiboot_info_t* info)
}
}
// Reserve first 64MB which is reserved by boot code
// todo(nub31): Later we should only identity map only the kernel pages or make a higher half kernel
for (u64 page = 0; page < (64 * 1024 * 1024) / PAGE_SIZE; page++)
// Mark first 32 pages (64mb) as unusable since it is reserved by the bootloader
// todo(nub31): This should be revisited. Maybe do a higher half kernel?
for (u64 page = 0; page < 32; page++)
{
if (page < BITMAP_SIZE * 8)
if (page < BITMAP_PAGE_COUNT)
{
if (!(page_bitmap[page / 8] & (1 << (page % 8))))
{
@@ -96,6 +111,10 @@ void mem_init(multiboot_info_t* info)
free_pages--;
}
}
else
{
panic("Bitmap is not large enough to hold the bootloader reserved memory");
}
}
}