diff --git a/anasos-kernel/bootloader/Makefile b/anasos-kernel/bootloader/Makefile index 652d973..8db8ff3 100644 --- a/anasos-kernel/bootloader/Makefile +++ b/anasos-kernel/bootloader/Makefile @@ -2,7 +2,8 @@ all: nasm -f elf64 header.asm -o header.o nasm -f elf64 boot-64.asm -o boot-64.o nasm -f elf64 boot.asm -o boot.o + nasm -f elf64 e820.asm -o e820.o - ld -m elf_x86_64 -T ../linker.ld -o ../../AnasOS/boot/kernel boot.o boot-64.o header.o + ld -m elf_x86_64 -T ../linker.ld -o ../../AnasOS/boot/kernel boot.o boot-64.o header.o #e820.o grub-mkrescue -o AnasOS.iso ../../AnasOS/ qemu-system-x86_64 AnasOS.iso \ No newline at end of file diff --git a/anasos-kernel/bootloader/boot-64.asm b/anasos-kernel/bootloader/boot-64.asm index 814a342..9a6ffb1 100644 --- a/anasos-kernel/bootloader/boot-64.asm +++ b/anasos-kernel/bootloader/boot-64.asm @@ -1,9 +1,9 @@ -GLOBAL long_mode_start +GLOBAL start_long_mode EXTERN _start SECTION .text BITS 64 -long_mode_start: +start_long_mode: ; load null to all data segmant registers (needed for the cpu to work as intended) ; documented here - https://www.gnu.org/software/grub/manual/multiboot2/multiboot.html#Machine-state MOV ax, 0 @@ -13,7 +13,14 @@ long_mode_start: MOV gs, ax MOV ss, ax - CALL _start + ; CALL _start + + ; ; Write the letter "W" to the VGA text buffer + ; MOV rdi, 0xB8000 ; VGA text buffer address (identity-mapped in page tables) + ; MOV ax, 0x0F57 ; "W" (ASCII 0x57) with attribute 0x0F (white on black) + ; MOV word [rdi], ax ; Write the word (character + attribute) to the VGA buffer + HLT + diff --git a/anasos-kernel/bootloader/boot.asm b/anasos-kernel/bootloader/boot.asm index fe675aa..81c3487 100644 --- a/anasos-kernel/bootloader/boot.asm +++ b/anasos-kernel/bootloader/boot.asm @@ -10,15 +10,13 @@ GLOBAL PT GLOBAL stack_bottom GLOBAL stack_top -EXTERN long_mode_start +EXTERN start_long_mode EXTERN save_boot_info SECTION .text BITS 32 -start: - ; CALL save_boot_info - +start_protected_mode: MOV esp, stack_top CALL check_multiboot CALL check_cpuid @@ -26,9 +24,10 @@ start: CALL setup_page_tables CALL enable_paging + call write_W_to_vga LGDT [gdt64.pointer] - JMP gdt64.code_segment:long_mode_start + JMP gdt64.code_segment:start_long_mode HLT @@ -109,6 +108,11 @@ setup_page_tables: CMP ecx, 512 ; Fill all 512 entries (2 MiB) JL .loop_setup_first_pt + ; Ensure VGA buffer (0xB8000) is identity-mapped + MOV eax, 0xB8000 ; Physical address of VGA buffer + OR eax, 0b11 ; Present, Writable + MOV [PT + (0xB8 * 8)], eax ; Map 0xB8000 in the first PT + ; Fill second PT for the next 2 MiB MOV ecx, 0 .loop_setup_second_pt: @@ -155,6 +159,14 @@ error: MOV byte [0xB800C], al HLT +write_W_to_vga: + ; Write the letter "W" to the VGA text buffer + mov edi, 0xB8000 ; VGA text buffer address + mov ax, 0x0F57 ; "W" (ASCII 0x57) with attribute 0x0F (white on black) + mov word [edi], ax ; Write the word (character + attribute) to VGA memory + HLT + ret ; Return to the caller + SECTION .bss ALIGN 4096 start_page_table: diff --git a/anasos-kernel/linker copy.ld b/anasos-kernel/linker copy.ld new file mode 100644 index 0000000..0abc7fb --- /dev/null +++ b/anasos-kernel/linker copy.ld @@ -0,0 +1,49 @@ +ENTRY(start_protected_mode) + +SECTIONS +{ + . = 0x0; + .boot : + { + KEEP(*(.multiboot_header)); + *(.boot) + } + + /* Page table addresses already defined in boot.asm, but ensure alignment */ + . = ALIGN(4096); /* Page size */ + __page_table_start = .; + _p4 = PML4; /* Reference PML4 address from boot.asm */ + _p3 = PDPT; /* Reference PDPT address from boot.asm */ + _p2 = PD; /* Reference PD address from boot.asm */ + _p1 = PT; /* Reference PT address from boot.asm */ + __page_table_end = .; + + /* Stack setup handled in boot.asm */ + _stack_start = stack_bottom; /* Stack defined in boot.asm */ + _stack_end = stack_top; + + /* Memory map section for bootloader metadata */ + __bootloader_start = .; + _memory_map = .; /* Memory map entry */ + . += 4096; /* Reserve 4KB for memory map */ + + /* Kernel metadata section */ + .kernel : + { + KEEP(*(.kernel)) + } + + /* Text section: contains code */ + .text : + { + *(.text) + } + + .bss ALIGN(4096): + { + __bss_start = .; + *(.bss) + *(COMMON) + __bss_end = .; + } +} diff --git a/anasos-kernel/linker.ld b/anasos-kernel/linker.ld index 06b936c..f4b82f9 100644 --- a/anasos-kernel/linker.ld +++ b/anasos-kernel/linker.ld @@ -1,50 +1,17 @@ -ENTRY(start) +ENTRY(start_protected_mode) SECTIONS { - /* Reserve a buffer for kernel loading */ - . = 0x0; - .boot : - { - KEEP(*(.multiboot_header)); - *(.boot) - } - - /* Page table addresses already defined in boot.asm, but ensure alignment */ - . = ALIGN(4096); /* Page size */ - __page_table_start = .; - _p4 = PML4; /* Reference PML4 address from boot.asm */ - _p3 = PDPT; /* Reference PDPT address from boot.asm */ - _p2 = PD; /* Reference PD address from boot.asm */ - _p1 = PT; /* Reference PT address from boot.asm */ - __page_table_end = .; - - /* Stack setup handled in boot.asm */ - _stack_start = stack_bottom; /* Stack defined in boot.asm */ - _stack_end = stack_top; + . = 1M; - /* Memory map section for bootloader metadata */ - __bootloader_start = .; - _memory_map = .; /* Memory map entry */ - . += 4096; /* Reserve 4KB for memory map */ - - /* Kernel metadata section */ - .kernel : - { - KEEP(*(.kernel)) - } - - /* Text section: contains code */ - .text : - { - *(.text) - } + .boot : + { + KEEP(*(.multiboot_header)) + *(.boot) + } - .bss ALIGN(4096): - { - __bss_start = .; - *(.bss) - *(COMMON) - __bss_end = .; - } + .text : + { + *(.text) + } } diff --git a/anasos-kernel/src/main.rs b/anasos-kernel/src/main.rs index 95466b6..6cd09cd 100644 --- a/anasos-kernel/src/main.rs +++ b/anasos-kernel/src/main.rs @@ -12,19 +12,21 @@ use anasos_kernel::{ }; use x86_64::VirtAddr; -// Symbols defined in `linker.ld` +// TODO: Symbols defined in `linker.ld` #[no_mangle] pub extern "C" fn _start() -> ! { - lazy_static! { - static ref BOOT_INFO: BootInfo = unsafe { bootinfo::get() }; - } + println!("Hello World{}", "!"); + + // lazy_static! { + // static ref BOOT_INFO: BootInfo = unsafe { bootinfo::get() }; + // } - println!("boot_info: {:?}", *BOOT_INFO); + // println!("boot_info: {:?}", *BOOT_INFO); - // #[cfg(notest)] - kernel_main(&*BOOT_INFO); + // // #[cfg(notest)] + // kernel_main(&*BOOT_INFO); // #[cfg(test)] // test_kernel_main(&*BOOT_INFO);