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

18
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "attach",
"name": "Attach to QEMU",
"executable": "${workspaceFolder}/.build/kernel/kernel",
"target": "localhost:1234",
"remote": true,
"cwd": "${workspaceRoot}",
"gdbpath": "/usr/bin/gdb",
"autorun": [
"b _start"
]
}
]
}

7
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,7 @@
{
"files.associations": {
"bitset": "c",
"algorithm": "c",
"format": "c"
}
}

View File

@@ -31,5 +31,5 @@ qemu-system-x86_64 -cdrom .build/nub-os.iso
```sh ```sh
qemu-system-x86_64 -s -S -cdrom .build/nub-os.iso qemu-system-x86_64 -s -S -cdrom .build/nub-os.iso
gdb -tui .build/kernel -ex "target remote localhost:1234" gdb -tui .build/kernel/kernel -ex "target remote localhost:1234"
``` ```

View File

@@ -3,6 +3,7 @@ ENTRY(_start)
SECTIONS SECTIONS
{ {
. = 2M; . = 2M;
kernel_start = .;
.text BLOCK(4K) : ALIGN(4K) .text BLOCK(4K) : ALIGN(4K)
{ {
@@ -25,4 +26,6 @@ SECTIONS
*(COMMON) *(COMMON)
*(.bss) *(.bss)
} }
kernel_end = .;
} }

View File

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

View File

@@ -18,14 +18,29 @@ typedef struct
static memory_region_t usable_regions[USABLE_REGION_SIZE]; static memory_region_t usable_regions[USABLE_REGION_SIZE];
static size_t num_regions = 0; static size_t num_regions = 0;
#define BITMAP_SIZE 32768 // Supports up to 1GB of RAM #define KiB(count) ((u64)count * 1024)
#define USABLE_REGION_SIZE 32 #define MiB(count) (KiB((u64)count) * 1024)
#define PAGE_SIZE 4096 #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 u8 page_bitmap[BITMAP_SIZE];
static u64 total_pages = 0; static u64 total_pages = 0;
static u64 free_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) void mem_init(multiboot_info_t* info)
{ {
if (!(info->flags & (1 << 6))) if (!(info->flags & (1 << 6)))
@@ -51,7 +66,7 @@ void mem_init(multiboot_info_t* info)
} }
else 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; break;
} }
} }
@@ -84,11 +99,11 @@ void mem_init(multiboot_info_t* info)
} }
} }
// Reserve first 64MB which is reserved by boot code // Mark first 32 pages (64mb) as unusable since it is reserved by the bootloader
// todo(nub31): Later we should only identity map only the kernel pages or make a higher half kernel // todo(nub31): This should be revisited. Maybe do a higher half kernel?
for (u64 page = 0; page < (64 * 1024 * 1024) / PAGE_SIZE; page++) 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)))) if (!(page_bitmap[page / 8] & (1 << (page % 8))))
{ {
@@ -96,6 +111,10 @@ void mem_init(multiboot_info_t* info)
free_pages--; free_pages--;
} }
} }
else
{
panic("Bitmap is not large enough to hold the bootloader reserved memory");
}
} }
} }