From 1d3ff04f2dd47c71aa0b2d7e7601f0d31b3eab60 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 26 Sep 2019 22:58:06 +0200 Subject: Added higher half kernel with paging --- src/kernel/boot.asm | 109 ++++++++++++++++++++++++++++++---------------- src/kernel/graphics/vga.c | 2 +- src/kernel/kernel.c | 7 ++- src/kernel/linker.ld | 47 ++++++++++---------- 4 files changed, 100 insertions(+), 65 deletions(-) diff --git a/src/kernel/boot.asm b/src/kernel/boot.asm index 197e13c..ef6bf0e 100644 --- a/src/kernel/boot.asm +++ b/src/kernel/boot.asm @@ -1,39 +1,72 @@ -[bits 32] -global start -global vbe_init_structure +global start ; Make start available to linker +extern kernel_main ; Get main function from kernel.c + +; Set Multiboot headers for GRUB +MODULEALIGN equ 1<<0 +MEMINFO equ 1<<1 +FLAGS equ MODULEALIGN | MEMINFO +MAGIC equ 0x1BADB002 +CHECKSUM equ -(MAGIC + FLAGS) + +; Set virtual base address of kernel space +KERNEL_VIRTUAL_BASE equ 0xC0000000 ; 3GB +KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22) ; Page directory index of kernel's 4MB PTE. + +section .data +align 0x1000 +BootPageDirectory: + ; Create page directory entry to identity-map the first 4MB of the 32-bit physical address space. + dd 0x00000083 + times (KERNEL_PAGE_NUMBER - 1) dd 0 + ; Create 4MB page directory entry to contain the kernel + dd 0x00000083 + times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0 + +section .text +align 4 +MultiBootHeader: + dd MAGIC + dd FLAGS + dd CHECKSUM + +; Reserve 16k for initial kernel stack space +STACKSIZE equ 0x4000 + +; Set up entry point for linker +loader equ (start - 0xC0000000) +global loader + start: - mov esp, _sys_stack ; Points stack to stack area - jmp stublet - -; Align with 4 Bytes -ALIGN 4 -mboot: - ; Multiboot macros - MULTIBOOT_PAGE_ALIGN equ 1<<0 - MULTIBOOT_MEMORY_INFO equ 1<<1 - MULTIBOOT_AOUT_KLUDGE equ 1<<16 - MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 - MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE - MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) - EXTERN code, bss, end - - ; GRUB Multiboot header - dd MULTIBOOT_HEADER_MAGIC - dd MULTIBOOT_HEADER_FLAGS - dd MULTIBOOT_CHECKSUM - - ; AOUT kludge - dd mboot - dd code - dd bss - dd end - dd start - -; Endless loop -stublet: - extern kernel_main + ; Using physical addresses for now + mov ecx, (BootPageDirectory - KERNEL_VIRTUAL_BASE) + mov cr3, ecx ; Load Page Directory Base Register + + mov ecx, cr4 + or ecx, 0x00000010 ; Set PSE in CR4 to enable 4MB pages + mov cr4, ecx + + mov ecx, cr0 + or ecx, 0x80000000 ; Enable paging + mov cr0, ecx + + ; Long jump to StartInHigherHalf + lea ecx, [StartInHigherHalf] + jmp ecx + +StartInHigherHalf: + mov dword [BootPageDirectory], 0 + invlpg [0] + + mov esp, stack+STACKSIZE ; Set up the stack + push eax ; Pass Multiboot magic number + + ; Pass Multiboot info structure + push ebx + add ebx, KERNEL_VIRTUAL_BASE + push ebx + call kernel_main - jmp $ + hlt %include "src/kernel/gdt/gdt.asm" @@ -45,7 +78,7 @@ stublet: %include "src/kernel/interact.asm" -; Store the stack -SECTION .bss - resb 8192 ; Reserve 8KiB -_sys_stack: \ No newline at end of file +section .bss +align 32 +stack: + resb STACKSIZE ; reserve 16k stack on a uint64_t boundary diff --git a/src/kernel/graphics/vga.c b/src/kernel/graphics/vga.c index fa1f330..36f5e04 100644 --- a/src/kernel/graphics/vga.c +++ b/src/kernel/graphics/vga.c @@ -74,7 +74,7 @@ void terminal_initialize(void) { terminal_row = 0; terminal_column = 0; terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); - terminal_buffer = (uint16_t *) 0xB8000; + terminal_buffer = (uint16_t *) 0xC00B8000; terminal_clear(); } diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index dd68536..383966d 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -6,11 +6,11 @@ #include "timer/timer.h" void init() { - asm volatile ("sti"); gdt_install(); idt_install(); isrs_install(); irq_install(); + asm volatile ("sti"); timer_install(); terminal_initialize(); keyboard_install(); @@ -19,9 +19,12 @@ void init() { void kernel_main(void) { // vbe_set_mode(0x11B); - set_optimal_resolution(); + // set_optimal_resolution(); + init(); + keyboard_install(); terminal_write_string("Melvix loaded successfully!\n"); // __asm__ ("div %0" :: "r"(0)); // Exception testing x/0 + for (;;); } \ No newline at end of file diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld index 6461b2b..3e209fc 100644 --- a/src/kernel/linker.ld +++ b/src/kernel/linker.ld @@ -1,25 +1,24 @@ -OUTPUT_FORMAT("binary") ENTRY(start) -phys = 0x00100000; -SECTIONS -{ - .text phys : AT(phys) { - code = .; - *(.text) - *(.rodata*) - . = ALIGN(4096); - } - .data : AT(phys + (data - code)) - { - data = .; - *(.data) - . = ALIGN(4096); - } - .bss : AT(phys + (bss - code)) - { - bss = .; - *(.bss) - . = ALIGN(4096); - } - end = .; -} +OUTPUT_FORMAT(elf32-i386) + +SECTIONS { + . = 0xC0100000; + + .text : AT(ADDR(.text) - 0xC0000000) { + *(.text) + *(.rodata*) + } + + .data ALIGN (0x1000) : AT(ADDR(.data) - 0xC0000000) { + *(.data) + } + + .bss : AT(ADDR(.bss) - 0xC0000000) { + _sbss = .; + *(COMMON) + *(.bss) + _ebss = .; + } + + end = .; +} \ No newline at end of file -- cgit v1.2.3