Skip to content

Commit

Permalink
test: paging breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrgoblings committed Dec 26, 2024
1 parent e1f754f commit 032eab7
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 60 deletions.
3 changes: 2 additions & 1 deletion anasos-kernel/bootloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 10 additions & 3 deletions anasos-kernel/bootloader/boot-64.asm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


22 changes: 17 additions & 5 deletions anasos-kernel/bootloader/boot.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ 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
CALL check_long_mode

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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
49 changes: 49 additions & 0 deletions anasos-kernel/linker copy.ld
Original file line number Diff line number Diff line change
@@ -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 = .;
}
}
55 changes: 11 additions & 44 deletions anasos-kernel/linker.ld
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 9 additions & 7 deletions anasos-kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 032eab7

Please sign in to comment.