diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ab5446a --- /dev/null +++ b/.vscode/launch.json @@ -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" + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bcf69d2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "bitset": "c", + "algorithm": "c", + "format": "c" + } +} \ No newline at end of file diff --git a/README.md b/README.md index ab66815..5e60065 100644 --- a/README.md +++ b/README.md @@ -31,5 +31,5 @@ qemu-system-x86_64 -cdrom .build/nub-os.iso ```sh 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" ``` diff --git a/linker.ld b/linker.ld index e56537d..ae37e27 100644 --- a/linker.ld +++ b/linker.ld @@ -3,6 +3,7 @@ ENTRY(_start) SECTIONS { . = 2M; + kernel_start = .; .text BLOCK(4K) : ALIGN(4K) { @@ -25,4 +26,6 @@ SECTIONS *(COMMON) *(.bss) } + + kernel_end = .; } diff --git a/src/arch/x86_64/boot/boot.asm b/src/arch/x86_64/boot/boot.asm index e053b9d..16f370f 100644 --- a/src/arch/x86_64/boot/boot.asm +++ b/src/arch/x86_64/boot/boot.asm @@ -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 diff --git a/src/arch/x86_64/mem.c b/src/arch/x86_64/mem.c index 3182336..2981373 100644 --- a/src/arch/x86_64/mem.c +++ b/src/arch/x86_64/mem.c @@ -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"); + } } }