From 24d5e34bab65718824447be26c2c0992e3f1f251 Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 17:09:44 +0200 Subject: [PATCH 01/23] Implement PMM, paging manager and VMM (wip) --- src/mem/mod.rs | 2 + src/mem/pmm.rs | 427 +++++++++++++++++++++++++++++++++++++++++++++++++ src/mem/vmm.rs | 1 + 3 files changed, 430 insertions(+) create mode 100644 src/mem/pmm.rs create mode 100644 src/mem/vmm.rs diff --git a/src/mem/mod.rs b/src/mem/mod.rs index cc02a28..62f54a5 100644 --- a/src/mem/mod.rs +++ b/src/mem/mod.rs @@ -2,3 +2,5 @@ pub mod bump; pub mod c; pub mod heap; pub mod pages; +pub mod pmm; +pub mod vmm; diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs new file mode 100644 index 0000000..169d277 --- /dev/null +++ b/src/mem/pmm.rs @@ -0,0 +1,427 @@ +static mut PHYSICAL_BITMAP: [[u64; 8]; 512] = [[0; 8]; 512]; + +static mut PHYSICAL_BASE: u64 = 0; + +// Physical memory manager +// +// + 64 bits encode 256 KiB of memory (64 * 4096 bytes) +// + 512 bits encode 2 MiB of memory (8 * 256 KiB) +// + 512 entries in p1 table * 512 tables in p2 table = 1 GiB of memory + +// 0xFFFF_FF80_0000_0000 +// +// p4[511] +// ... +// + +extern "C" { + pub static mut p4_table: *mut u64; +} + +pub enum PhysicalPageStatus { + FREE = 0x00, + USED, + BAD, +} + +const P: u64 = 1 << 0; +const RW: u64 = 1 << 1; +const US: u64 = 1 << 2; +const PWT: u64 = 1 << 3; +const PCD: u64 = 1 << 4; +const PS: u64 = 1 << 7; +const NX: u64 = 1u64 << 63; + +const ADDR_MASK: u64 = 0x000f_ffff_ffff_f000; // 4-KiB aligned phys addr +const TWO_MIB: u64 = 2 * 1024 * 1024; + +#[inline(always)] +fn check_bit(word: u64, bit: u32) -> bool { + (word & (1u64 << bit)) != 0 +} + +#[inline(always)] +fn set_bit(word: &mut u64, bit: u32) { + *word |= 1u64 << bit; +} + +#[inline(always)] +fn clear_bit(word: &mut u64, bit: u32) { + *word &= !(1u64 << bit); +} + +// +// +// + +extern "C" { + static __kernel_start: u8; + static __kernel_end: u8; +} + +pub unsafe fn reserve_initial_frames() { + // Reserved frame + pmm_mark(0, true); + + let kstart = (&__kernel_start as *const _ as u64) >> 12; + let kend = (&__kernel_end as *const _ as u64) >> 12; + for f in kstart..=kend { + pmm_mark(f as u32, true); + } +} + +pub unsafe fn build_physmap_2m(p4_virt: *mut u64, mut phys_limit: u64) { + // Round down phys_limit to a multiple of 2MiB + phys_limit &= !(TWO_MIB - 1); + + let mut phys: u64 = 0; + while phys < phys_limit { + let virt = PHYSICAL_BASE + phys; + + rprint!("Mapping phys "); + rprintn!(phys); + rprint!(" address to virt "); + rprintn!(virt); + rprint!(" address\n"); + + //map_2m(p4_virt, virt, phys, P | RW); + + // + // + // + + let p4_idx = pml4_index(virt); + let p3_idx = pdpt_index(virt); + let p2_idx = pd_index(virt); + + let l3_frame = match pmm_alloc() { + Some(p) => p, + None => { + rprint!("Out of physical memory for page tables\n"); + 0 + } + }; + + let l2_frame = match pmm_alloc() { + Some(p) => p, + None => { + rprint!("Out of physical memory for page tables\n"); + 0 + } + }; + + //let l3_virt = phys_to_virt_table(l3_frame); + let l3_virt = l3_frame as *mut u64; + //let l2_virt = phys_to_virt_table(l2_frame); + let l2_virt = l2_frame as *mut u64; + + write64(p4_virt.add(p4_idx), (l3_frame & ADDR_MASK_4K) | P | RW); + write64(l3_virt.add(p3_idx), (l2_frame & ADDR_MASK_4K) | P | RW); + write64(l2_virt.add(p2_idx), (phys & ADDR_MASK_2M) | P | RW | PS); + + invlpg(virt as usize); + + // + // + // + + phys += TWO_MIB; + } +} + +pub unsafe fn pmm_init() { + rprint!("CR3 physical addr: "); + rprintn!(read_cr3_phys()); + rprint!("\n"); + + rprint!("pml4 virtual addr: "); + rprintn!(&p4_table as *const _ as u64); + rprint!("\n"); + + rprint!("Setting physical memory base address\n"); + set_physical_base(0xFFFF_8000_0000_0000); + + rprint!("Enabling recursive mapping\n"); + enable_recursive_mapping(&p4_table as *const _ as *mut u64); + + rprint!("Marking reserved physical memory frames as used\n"); + reserve_initial_frames(); + + rprint!("Building physmap\n"); + build_physmap_2m(&p4_table as *const _ as *mut u64, 8 * 1024 * 1024); + + //map_2m(&p4_table as *const _ as *mut u64, 0xfd00_0000, 0xfd00_0000, P | RW); + //map_2m(&p4_table as *const _ as *mut u64, 0xffff_8000_fd00_0000, 0xfd00_0000, P | RW); + + rprint!("Mapping framebuffer\n"); + map_framebuffer(0xfd00_0000u64, 0xffff_8000_fd00_0000); + + rprint!("Reloading CR3\n"); + reload_cr3(); +} + +/// Mark a frame by index (0..262143) as used/free +pub unsafe fn pmm_mark(frame_idx: u32, used: bool) { + let row = (frame_idx / (8 * 64)) as usize; // 0..511 + let off = (frame_idx % (8 * 64)) as u32; // 0..511 + + let col = (off / 64) as usize; // 0..7 + let bit = off % 64; // 0..63 + + if used { + set_bit(&mut PHYSICAL_BITMAP[row][col], bit); + } else { + clear_bit(&mut PHYSICAL_BITMAP[row][col], bit); + } +} + +pub unsafe fn pmm_alloc() -> Option { + for row in 0..512 { + for col in 0..8 { + let mut w = PHYSICAL_BITMAP[row][col]; + + if w != u64::MAX { + for bit in 0..64 { + if !check_bit(w, bit) { + set_bit(&mut w, bit); + PHYSICAL_BITMAP[row][col] = w; + + let frame_idx = (row as u32) * (8 * 64) + (col as u32) * 64 + bit; + + return Some((frame_idx as u64) << 12); + } + } + } + } + } + None +} + +pub unsafe fn pmm_free(phys: u64) { + let frame_idx = (phys >> 12) as u32; + pmm_mark(frame_idx, false); +} + +// +// +// + +#[inline(always)] +unsafe fn reload_cr3() { + let mut cr3: u64; + + core::arch::asm!("mov {}, cr3", out(reg) cr3, options(nostack, preserves_flags)); + core::arch::asm!("mov cr3, {}", in(reg) cr3, options(nostack, preserves_flags)); +} + +#[inline(always)] +unsafe fn read_cr3_phys() -> u64 { + let (frame, _flags) = x86_64::registers::control::Cr3::read(); + + frame.start_address().as_u64() +} + +pub unsafe fn enable_recursive_mapping(p4_virt: *mut u64) { + let p4_phys = read_cr3_phys(); + + write64(p4_virt.add(510), (p4_phys & ADDR_MASK) | P | RW); + reload_cr3(); +} + + +#[inline(always)] +fn pml4_index(va: u64) -> usize { + ((va >> 39) & 0x1FF) as usize +} + +#[inline(always)] +fn pdpt_index(va: u64) -> usize { + ((va >> 30) & 0x1FF) as usize +} + +#[inline(always)] +fn pd_index(va: u64) -> usize { + ((va >> 21) & 0x1FF) as usize +} + +#[inline(always)] +fn pt_index(va: u64) -> usize { + ((va >> 12) & 0x1FF) as usize +} + +// +// +// + +pub unsafe fn set_physical_base(base: u64) { + PHYSICAL_BASE = base; +} + +#[inline(always)] +pub unsafe fn phys_to_virt(paddr: u64) -> *mut u8 { + (PHYSICAL_BASE + paddr) as *mut u8 +} + +#[inline(always)] +pub unsafe fn virt_to_phys(vaddr: *const u8) -> u64 { + (vaddr as u64) - PHYSICAL_BASE +} + +#[inline(always)] +unsafe fn phys_to_virt_table(phys: u64) -> *mut u64 { + (PHYSICAL_BASE + (phys & ADDR_MASK)) as *mut u64 +} + +// +// +// + +#[inline(always)] +unsafe fn read64(p: *const u64) -> u64 { + core::ptr::read_volatile(p) +} + +#[inline(always)] +unsafe fn write64(p: *mut u64, val: u64) { + core::ptr::write_volatile(p, val); +} + +#[inline(always)] +unsafe fn invlpg(addr: usize) { + core::arch::asm!("invlpg [{}]", in(reg) addr, options(nostack, preserves_flags)); +} + +// +// +// + +// +// VMM +// + +unsafe fn ensure_table(parent_tbl: *mut u64, idx: usize, table_flags: u64) -> *mut u64 { + let e = read64(parent_tbl.add(idx)); + + if e & P != 0 { + return e as *mut u64; + //return phys_to_virt_table(e); + } + + let phys = match pmm_alloc() { + Some(p) => p, + None => panic!("Out of physical memory for page tables"), + }; + + // Zero the new table (512 * 8 bytes) + //let tbl = phys_to_virt_table(phys); + let tbl = phys as *mut u64; + for i in 0..512 { + write64(tbl.add(i), 0); + } + write64(parent_tbl.add(idx), (phys & ADDR_MASK) | table_flags); + tbl +} + +// +// +// + +const ADDR_MASK_4K: u64 = 0x000F_FFFF_FFFF_F000; +const ADDR_MASK_2M: u64 = 0x000F_FFFF_FFE0_0000; // bits 20..0 zero + +#[inline(always)] +fn is_aligned_2m(x: u64) -> bool { (x & 0x1F_FFFF) == 0 } // 2 MiB + +pub unsafe fn map_2m(p4_virt: *mut u64, virt: u64, phys: u64, pde_flags: u64) { + // L3 (PDPT) + rprint!("L3 ensure_table()\n"); + let l3 = ensure_table(p4_virt, pml4_index(virt), P | RW); + + // Clear 1GiB if present + let l3e_ptr = l3.add(pdpt_index(virt)); + let l3e = read64(l3e_ptr); + if (l3e & P) != 0 && (l3e & PS) != 0 { + rprint!("Clearing 1GiB mapping\n"); + write64(l3e_ptr, 0); + invlpg(virt as usize); + } + + // L2 (PD) + rprint!("L2 ensure_table()\n"); + let l2 = ensure_table(l3, pdpt_index(virt), P | RW); + + // Write 2MiB PDE (PS=1) + let l2e_ptr = l2.add(pd_index(virt)); + let entry = (phys & ADDR_MASK_2M) | pde_flags | PS; + write64(l2e_ptr, entry); + + invlpg(virt as usize); +} + +pub unsafe fn map_4k(p4_virt: *mut u64, virt: u64, phys: u64, pte_flags: u64) { + let l3 = ensure_table(p4_virt, pml4_index(virt), P | RW); + + let l3e_ptr = l3.add(pdpt_index(virt)); + let l3e = read64(l3e_ptr); + if l3e & P != 0 && (l3e & PS) != 0 { + write64(l3e_ptr, 0); + invlpg(virt as usize); + } + + let l2 = ensure_table(l3, pdpt_index(virt), P | RW); + + let l2e_ptr = l2.add(pd_index(virt)); + let l2e = read64(l2e_ptr); + if l2e & P != 0 && (l2e & PS) != 0 { + write64(l2e_ptr, 0); + invlpg(virt as usize); + } + + let l1 = ensure_table(l2, pd_index(virt), P | RW); + + let pte_ptr = l1.add(pt_index(virt)); + let entry = (phys & ADDR_MASK_4K) | pte_flags; + write64(pte_ptr, entry); + + invlpg(virt as usize); +} + +// +// +// + +unsafe fn map_framebuffer(phys: u64, virt: u64) { + let p4_virt = &p4_table as *const _ as *mut u64; + + let p4_idx = pml4_index(virt); + let p3_idx = pdpt_index(virt); + let p2_idx = pd_index(virt); + + let l3_frame = match pmm_alloc() { + Some(p) => p, + None => { + rprint!("Out of physical memory for page tables\n"); + 0 + } + }; + + let l2_frame = match pmm_alloc() { + Some(p) => p, + None => { + rprint!("Out of physical memory for page tables\n"); + 0 + } + }; + + //let l3_virt = phys_to_virt_table(l3_frame); + let l3_virt = l3_frame as *mut u64; + //let l2_virt = phys_to_virt_table(l2_frame); + let l2_virt = l2_frame as *mut u64; + + write64(p4_virt.add(p4_idx), (l3_frame & ADDR_MASK_4K) | P | RW); + write64(l3_virt.add(p3_idx), (l2_frame & ADDR_MASK_4K) | P | RW); + write64(l2_virt.add(p2_idx), (phys & ADDR_MASK_2M) | P | RW | PS); + + invlpg(virt as usize); +} + diff --git a/src/mem/vmm.rs b/src/mem/vmm.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/mem/vmm.rs @@ -0,0 +1 @@ + From 622da4542dea69c7e999bcf3b4b5447903bf917d Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 17:10:05 +0200 Subject: [PATCH 02/23] Disable a portion of paging setup in asm --- iso/boot/boot.asm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/iso/boot/boot.asm b/iso/boot/boot.asm index 4fdabfb..b64d1d3 100644 --- a/iso/boot/boot.asm +++ b/iso/boot/boot.asm @@ -381,6 +381,8 @@ set_up_page_tables: ; cmp ecx, 0x40000 ; jb .map_self_2 + ret + ; Framebuffer init mov eax, p3_fb_table From 9e5bbf6a2f93cf96ae768a106014c55c7b9233b1 Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 17:10:46 +0200 Subject: [PATCH 03/23] Adjust the init procedures to new memory mgmt (wip) --- linker.ld | 4 ++++ src/init/boot.rs | 12 ++++++------ src/init/font.rs | 12 ++++++------ src/init/heap.rs | 4 ++++ src/init/idt.rs | 1 + 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/linker.ld b/linker.ld index 9d7199d..48d2f84 100644 --- a/linker.ld +++ b/linker.ld @@ -7,6 +7,8 @@ SECTIONS { KEEP(*(.multiboot2_header)) } + __kernel_start = .; + .text : { *(.text.entry) *(.text*) @@ -63,6 +65,8 @@ SECTIONS { p1_fb_table_2 = .; . = . + 4K; p1_fb_table_3 = .; . = . + 4K; + __kernel_end = .; + . = 0x650000; .user_task : { *(.user_task*) diff --git a/src/init/boot.rs b/src/init/boot.rs index 550ecdc..11e71e1 100644 --- a/src/init/boot.rs +++ b/src/init/boot.rs @@ -191,11 +191,11 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa let p4_phys = p4_virt; // + + let virt_base = 0xffff_8000_fd00_0000u64; + let fb_ptr = virt_base as *mut u64; - let virt_base = 0xFFFF_FF80_0000_0000u64; - let fb_ptr = virt_base as *mut u32; - - let test_ptr = virt_base as *mut u32; + let test_ptr = virt_base as *mut u64; *test_ptr = 0xFFFFFFFF; //crate::mem::pages::identity_map(p4_table as *mut u64, 4 * 1024 * 1024); @@ -283,12 +283,12 @@ fn align_up(x: usize, align: usize) -> usize { (x + align - 1) & !(align - 1) } -pub unsafe fn draw_rect(ptr: *mut u32, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { +pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { for y in y0..(y0 + h) { for x in x0..(x0 + w) { let offset = y * (pitch / 4) + x; - ptr.add(offset).write_volatile(color); + ptr.add(offset).write_volatile(color as u64); } } } diff --git a/src/init/font.rs b/src/init/font.rs index ee4fe8d..ac94a61 100644 --- a/src/init/font.rs +++ b/src/init/font.rs @@ -1,4 +1,4 @@ -pub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u32, pitch: usize, fg: u32, font: &[u8]) { +pub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u64, pitch: usize, fg: u32, font: &[u8]) { let char_size = font[3] as usize; //let glyph = &font[4 + (c as usize * char_size)..]; @@ -11,8 +11,8 @@ pub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u32, pitch: usize, fg: u32, let py = y + row; let offset = py * pitch + px * 4; unsafe { - let pixel_ptr = fb.add(offset) as *mut u32; - *pixel_ptr = fg; + let pixel_ptr = fb.add(offset) as *mut u64; + *pixel_ptr = fg as u64; } } } @@ -69,7 +69,7 @@ pub fn parse_psf(psf: &[u8]) -> Option { } } -fn draw_char_psf(font: &PsfFont, ch: u8, x: usize, y: usize, color: u32, framebuffer: *mut u32, pitch: usize, bpp: usize) { +fn draw_char_psf(font: &PsfFont, ch: u8, x: usize, y: usize, color: u32, framebuffer: *mut u64, pitch: usize, bpp: usize) { let glyph_start = ch as usize * font.bytes_per_glyph; //let glyph = &font.glyphs[glyph_start..glyph_start + font.bytes_per_glyph]; @@ -81,7 +81,7 @@ fn draw_char_psf(font: &PsfFont, ch: u8, x: usize, y: usize, color: u32, framebu unsafe { let offset = (y + row) * 4096 / 4 + (x + col); - framebuffer.add(offset as usize + 1).write_volatile(color); + framebuffer.add(offset as usize + 1).write_volatile(color as u64); //framebuffer.add(offset as usize + 1).write_volatile(0xfefab0); //framebuffer.add(offset as usize + 2).write_volatile(0xdeadbeef); } @@ -91,7 +91,7 @@ fn draw_char_psf(font: &PsfFont, ch: u8, x: usize, y: usize, color: u32, framebu } } -pub fn draw_text_psf(text: &str, font: &PsfFont, x: usize, y: usize, color: u32, framebuffer: *mut u32, pitch: usize, bpp: usize) { +pub fn draw_text_psf(text: &str, font: &PsfFont, x: usize, y: usize, color: u32, framebuffer: *mut u64, pitch: usize, bpp: usize) { let mut cx = x; for ch in text.bytes() { diff --git a/src/init/heap.rs b/src/init/heap.rs index 2517374..6a6ac80 100644 --- a/src/init/heap.rs +++ b/src/init/heap.rs @@ -8,6 +8,10 @@ pub fn print_result() -> InitResult { return InitResult::Failed; }*/ + unsafe { + crate::mem::pmm::pmm_init(); + } + crate::mem::heap::init(); unsafe { diff --git a/src/init/idt.rs b/src/init/idt.rs index 041d6b7..4a31c54 100644 --- a/src/init/idt.rs +++ b/src/init/idt.rs @@ -22,6 +22,7 @@ pub fn get_result() -> super::result::InitResult { debugln!("Loading TSS"); load_tss(0x28); + debugln!("Done"); super::result::InitResult::Passed } From 4379d6c333792d21b5b7936cf17d858b1eb021b5 Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 17:38:24 +0200 Subject: [PATCH 04/23] Use CR3 physical address directly --- src/mem/pmm.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs index 169d277..b89a6bb 100644 --- a/src/mem/pmm.rs +++ b/src/mem/pmm.rs @@ -130,6 +130,8 @@ pub unsafe fn build_physmap_2m(p4_virt: *mut u64, mut phys_limit: u64) { } pub unsafe fn pmm_init() { + let pml4 = read_cr3_phys() as *mut u64; + rprint!("CR3 physical addr: "); rprintn!(read_cr3_phys()); rprint!("\n"); @@ -142,13 +144,13 @@ pub unsafe fn pmm_init() { set_physical_base(0xFFFF_8000_0000_0000); rprint!("Enabling recursive mapping\n"); - enable_recursive_mapping(&p4_table as *const _ as *mut u64); + enable_recursive_mapping(pml4); rprint!("Marking reserved physical memory frames as used\n"); reserve_initial_frames(); rprint!("Building physmap\n"); - build_physmap_2m(&p4_table as *const _ as *mut u64, 8 * 1024 * 1024); + build_physmap_2m(pml4, 8 * 1024 * 1024); //map_2m(&p4_table as *const _ as *mut u64, 0xfd00_0000, 0xfd00_0000, P | RW); //map_2m(&p4_table as *const _ as *mut u64, 0xffff_8000_fd00_0000, 0xfd00_0000, P | RW); @@ -391,7 +393,7 @@ pub unsafe fn map_4k(p4_virt: *mut u64, virt: u64, phys: u64, pte_flags: u64) { // unsafe fn map_framebuffer(phys: u64, virt: u64) { - let p4_virt = &p4_table as *const _ as *mut u64; + let p4_virt = read_cr3_phys() as *mut u64; let p4_idx = pml4_index(virt); let p3_idx = pdpt_index(virt); @@ -421,6 +423,9 @@ unsafe fn map_framebuffer(phys: u64, virt: u64) { write64(p4_virt.add(p4_idx), (l3_frame & ADDR_MASK_4K) | P | RW); write64(l3_virt.add(p3_idx), (l2_frame & ADDR_MASK_4K) | P | RW); write64(l2_virt.add(p2_idx), (phys & ADDR_MASK_2M) | P | RW | PS); + write64(l2_virt.add(p2_idx + 1), ((phys + 0x200000) & ADDR_MASK_2M) | P | RW | PS); + write64(l2_virt.add(p2_idx + 2), ((phys + 0x400000) & ADDR_MASK_2M) | P | RW | PS); + write64(l2_virt.add(p2_idx + 3), ((phys + 0x600000) & ADDR_MASK_2M) | P | RW | PS); invlpg(virt as usize); } From e9441310b0f47653772296b0dbc5857d0ec13b9b Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 19:38:33 +0200 Subject: [PATCH 05/23] Tidy up a bit --- src/init/boot.rs | 54 +++++++++++++----------------------------------- src/mem/pmm.rs | 20 +++++++----------- 2 files changed, 21 insertions(+), 53 deletions(-) diff --git a/src/init/boot.rs b/src/init/boot.rs index 11e71e1..0d5ff41 100644 --- a/src/init/boot.rs +++ b/src/init/boot.rs @@ -151,9 +151,9 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa if entry.typ == 1 { debug!("Usable memory region: "); debugn!(entry.base_addr as u64); - debug!(" - "); + debug!(": "); debugn!(entry.length as u64); - debugln!(" B"); + debugln!(" bytes"); } } } @@ -185,49 +185,22 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa use x86_64::registers::control::Cr3; unsafe { - let p4_ptr = &p4_table as *const _ as *mut u64; - - let p4_virt = &p4_table as *const _ as usize; - let p4_phys = p4_virt; - - // - - let virt_base = 0xffff_8000_fd00_0000u64; - let fb_ptr = virt_base as *mut u64; - - let test_ptr = virt_base as *mut u64; - *test_ptr = 0xFFFFFFFF; - - //crate::mem::pages::identity_map(p4_table as *mut u64, 4 * 1024 * 1024); - //crate::mem::pages::identity_map(p4_table as *mut u64, 0x1000); - - /*crate::mem::pages::map_32mb( - p4_ptr, - fb_tag.addr as usize, - virt_base as usize, - );*/ + if fb_tag.addr == 0xb8000 { + ptr += align_up(tag.size as usize, 8); + continue; + } - //x86_64::instructions::tlb::flush_all(); - //Cr3::write(PhysFrame::from_start_address(PhysAddr::new(p4_phys as u64)).unwrap(), Cr3Flags::empty()); + rprint!("Mapping framebuffer\n"); + let virt_base = 0xffff_8000_0000_0000u64 + fb_tag.addr as u64; + //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, 0xffff_8000_0000_0000 + fb_tag.addr as u64); + //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, virt_base); + crate::mem::pmm::map_framebuffer(0xfd00_0000, 0xffff_8000_fd00_0000); - /*for y in 0..500 { - for x in 0..500 { - //let offset = y * fb_tag.pitch + x * (fb_tag.bpp as u32 / 8); - let offset = y * fb_tag.pitch / 4 + x; - //let color = 0x00ff00ff; + let fb_ptr = 0xffff_8000_fd00_0000 as *mut u64; - fb_ptr.add(offset as usize).write_volatile(0xdeadbeef); - fb_ptr.add(offset as usize + 1).write_volatile(0xfefab0); - fb_ptr.add(offset as usize + 2).write_volatile(0xdeadbeef); - } - }*/ + *fb_ptr = 0xFFFFFFFF; - /*for y in 0..150 { - for x in 0..200 { - super::font::put_pixel(x, y, 0xdeadbeef, fb_ptr, 4096, 32); - } - }*/ draw_rect(fb_ptr, 150, 150, 100, 100, 4096, 0x00ffffff); draw_rect(fb_ptr, 250, 250, 100, 100, 4096, 0x00ff0000); draw_rect(fb_ptr, 350, 350, 100, 100, 4096, 0x0000ff00); @@ -269,6 +242,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa } ptr += align_up(tag.size as usize, 8); + tag_count += 1; if tag_count > 64 { debugln!("Too many tags, aborting"); diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs index b89a6bb..0d951ba 100644 --- a/src/mem/pmm.rs +++ b/src/mem/pmm.rs @@ -1,4 +1,4 @@ -static mut PHYSICAL_BITMAP: [[u64; 8]; 512] = [[0; 8]; 512]; +static mut PHYSICAL_BITMAP: [[u64; 8]; 1024] = [[0; 8]; 1024]; static mut PHYSICAL_BASE: u64 = 0; @@ -152,23 +152,17 @@ pub unsafe fn pmm_init() { rprint!("Building physmap\n"); build_physmap_2m(pml4, 8 * 1024 * 1024); - //map_2m(&p4_table as *const _ as *mut u64, 0xfd00_0000, 0xfd00_0000, P | RW); - //map_2m(&p4_table as *const _ as *mut u64, 0xffff_8000_fd00_0000, 0xfd00_0000, P | RW); - - rprint!("Mapping framebuffer\n"); - map_framebuffer(0xfd00_0000u64, 0xffff_8000_fd00_0000); - rprint!("Reloading CR3\n"); reload_cr3(); } /// Mark a frame by index (0..262143) as used/free pub unsafe fn pmm_mark(frame_idx: u32, used: bool) { - let row = (frame_idx / (8 * 64)) as usize; // 0..511 - let off = (frame_idx % (8 * 64)) as u32; // 0..511 + let row = (frame_idx / (8 * 64)) as usize; + let off = (frame_idx % (8 * 64)) as u32; - let col = (off / 64) as usize; // 0..7 - let bit = off % 64; // 0..63 + let col = (off / 64) as usize; + let bit = off % 64; if used { set_bit(&mut PHYSICAL_BITMAP[row][col], bit); @@ -178,7 +172,7 @@ pub unsafe fn pmm_mark(frame_idx: u32, used: bool) { } pub unsafe fn pmm_alloc() -> Option { - for row in 0..512 { + for row in 0..1024 { for col in 0..8 { let mut w = PHYSICAL_BITMAP[row][col]; @@ -392,7 +386,7 @@ pub unsafe fn map_4k(p4_virt: *mut u64, virt: u64, phys: u64, pte_flags: u64) { // // -unsafe fn map_framebuffer(phys: u64, virt: u64) { +pub unsafe fn map_framebuffer(phys: u64, virt: u64) { let p4_virt = read_cr3_phys() as *mut u64; let p4_idx = pml4_index(virt); From 2c496db33337825eca64b4dc41b0d814c8a0e455 Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 19:58:04 +0200 Subject: [PATCH 06/23] Refactor framebuf mapping --- src/mem/pmm.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs index 0d951ba..8acef99 100644 --- a/src/mem/pmm.rs +++ b/src/mem/pmm.rs @@ -224,7 +224,6 @@ pub unsafe fn enable_recursive_mapping(p4_virt: *mut u64) { reload_cr3(); } - #[inline(always)] fn pml4_index(va: u64) -> usize { ((va >> 39) & 0x1FF) as usize @@ -416,10 +415,10 @@ pub unsafe fn map_framebuffer(phys: u64, virt: u64) { write64(p4_virt.add(p4_idx), (l3_frame & ADDR_MASK_4K) | P | RW); write64(l3_virt.add(p3_idx), (l2_frame & ADDR_MASK_4K) | P | RW); - write64(l2_virt.add(p2_idx), (phys & ADDR_MASK_2M) | P | RW | PS); - write64(l2_virt.add(p2_idx + 1), ((phys + 0x200000) & ADDR_MASK_2M) | P | RW | PS); - write64(l2_virt.add(p2_idx + 2), ((phys + 0x400000) & ADDR_MASK_2M) | P | RW | PS); - write64(l2_virt.add(p2_idx + 3), ((phys + 0x600000) & ADDR_MASK_2M) | P | RW | PS); + + for i in 0..4 { + write64(l2_virt.add(p2_idx + i), (phys + (i as u64 * 0x200000) & ADDR_MASK_2M) | P | RW | PS); + } invlpg(virt as usize); } From 6536add813b5a49871ce4384dc1cd2a669f8ef22 Mon Sep 17 00:00:00 2001 From: krusty Date: Wed, 20 Aug 2025 23:49:05 +0200 Subject: [PATCH 07/23] Add the page walker implementation(s), minor refactor --- src/mem/heap.rs | 6 +-- src/mem/mod.rs | 1 + src/mem/pmm.rs | 14 +++++-- src/mem/walk.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 src/mem/walk.rs diff --git a/src/mem/heap.rs b/src/mem/heap.rs index 03d2c92..ce8b236 100644 --- a/src/mem/heap.rs +++ b/src/mem/heap.rs @@ -9,7 +9,7 @@ pub struct HeapNode { #[derive(PartialEq)] pub enum HeapNodeStatus { - UNKNWON = 0x00, + //UNKNWON = 0x00, FREE, USED, } @@ -160,11 +160,11 @@ pub unsafe fn alloc(alloc_size: usize) -> u64 { // Return VAddr to allocated area //(cur_node as *mut HeapNode as u64) + (core::mem::size_of::() as u64) - (cur_node as *mut HeapNode as u64) + cur_node as *mut HeapNode as u64 } pub unsafe fn free(vaddr: u64) { - let mut cur_node = vaddr as *mut HeapNode; + let cur_node = vaddr as *mut HeapNode; (*cur_node).status = HeapNodeStatus::FREE; diff --git a/src/mem/mod.rs b/src/mem/mod.rs index 62f54a5..d66ac2f 100644 --- a/src/mem/mod.rs +++ b/src/mem/mod.rs @@ -4,3 +4,4 @@ pub mod heap; pub mod pages; pub mod pmm; pub mod vmm; +pub mod walk; diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs index 8acef99..3392d01 100644 --- a/src/mem/pmm.rs +++ b/src/mem/pmm.rs @@ -84,8 +84,11 @@ pub unsafe fn build_physmap_2m(p4_virt: *mut u64, mut phys_limit: u64) { rprintn!(virt); rprint!(" address\n"); - //map_2m(p4_virt, virt, phys, P | RW); + map_2m(p4_virt, virt, phys, P | RW); + phys += TWO_MIB; + continue; + // // // @@ -131,6 +134,7 @@ pub unsafe fn build_physmap_2m(p4_virt: *mut u64, mut phys_limit: u64) { pub unsafe fn pmm_init() { let pml4 = read_cr3_phys() as *mut u64; + let p4_virt = &p4_table as *const _ as *mut u64; rprint!("CR3 physical addr: "); rprintn!(read_cr3_phys()); @@ -144,13 +148,15 @@ pub unsafe fn pmm_init() { set_physical_base(0xFFFF_8000_0000_0000); rprint!("Enabling recursive mapping\n"); - enable_recursive_mapping(pml4); + enable_recursive_mapping(p4_virt); rprint!("Marking reserved physical memory frames as used\n"); reserve_initial_frames(); rprint!("Building physmap\n"); - build_physmap_2m(pml4, 8 * 1024 * 1024); + build_physmap_2m(p4_virt, 8 * 1024 * 1024); + + map_2m(pml4, 0xFFFF_8000_0000_0000, 0x000_000, P | RW); rprint!("Reloading CR3\n"); reload_cr3(); @@ -211,7 +217,7 @@ unsafe fn reload_cr3() { } #[inline(always)] -unsafe fn read_cr3_phys() -> u64 { +pub unsafe fn read_cr3_phys() -> u64 { let (frame, _flags) = x86_64::registers::control::Cr3::read(); frame.start_address().as_u64() diff --git a/src/mem/walk.rs b/src/mem/walk.rs new file mode 100644 index 0000000..303f770 --- /dev/null +++ b/src/mem/walk.rs @@ -0,0 +1,101 @@ +use crate::mem::pmm::{phys_to_virt, read_cr3_phys}; + +pub unsafe fn walk_memory() { + let pml4 = super::pmm::read_cr3_phys() as *mut u64; + + print!("Mapped memory total: "); + printn!( count_mapped(read_cr3_phys()) ); + print!(" bytes\n"); + + let pml4_entries = walk_directory_table(pml4, 3); + + print!("PML4 present entries: "); + printn!(pml4_entries); + print!("\n"); +} + +unsafe fn walk_directory_table(parent: *mut u64, level: u8) -> usize { + let mut present_count: usize = 0; + + if level < 1 { + return 0; + } + + for i in 0..612 { + if i == 510 { + continue; + } + + let entry = parent.add(i).read_volatile(); + + if entry & 0x1 != 0 { + present_count += 1; + + let child_present = walk_directory_table((entry & 0xffffffff_fffff000) as *mut u64, level - 1); + + if child_present == 0 { + continue; + } + + print!("Child present entries: "); + printn!(child_present); + print!(" (level: "); + printn!(level); + print!(")\n"); + } + } + + present_count +} + +unsafe fn count_mapped(cr3: u64) -> u64 { + let mut total = 0; + + let pml4 = phys_to_virt(cr3 & 0xFFFFFFFFFFFFF000); + + for i in 0..512 { + if pml4.add(i).read_volatile() & 1 == 0 { + continue; + } + + let pdpt = phys_to_virt(pml4.add(i).read_volatile() as u64 & 0xFFFFFFFFFFFFF000); + + for j in 0..512 { + if pdpt.add(j).read_volatile() & 1 == 0 { + continue; + } + + // Huge 1 GiB page + if pdpt.add(j).read_volatile() & (1 << 7) != 0 { + total += 1u64 << 30; + continue; + } + + let pd = phys_to_virt(pdpt.add(j).read_volatile() as u64 & 0xFFFFFFFFFFFFF000); + + for k in 0..512 { + if pd.add(k).read_volatile() & 1 == 0 { + continue; + } + + if pd.add(k).read_volatile() & (1 << 7) != 0 { + total += 1u64 << 21; + continue; + } + + let pt = phys_to_virt(pd.add(k).read_volatile() as u64 & 0xFFFFFFFFFFFFF000); + + for l in 0..512 { + if pt.add(l).read_volatile() & 1 == 0 { + continue; + } + + // 4 KiB page + total += 1u64 << 12; + } + } + } + } + total +} + From 3d4813aeba8838b9fb8234868c3a33ae8c71e12f Mon Sep 17 00:00:00 2001 From: krusty Date: Thu, 21 Aug 2025 00:03:47 +0200 Subject: [PATCH 08/23] Add new hidden kernel shell cmd to walk mem pages --- src/input/cmd.rs | 10 ++++++++++ src/mem/pmm.rs | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/input/cmd.rs b/src/input/cmd.rs index 7cc5404..b0d6503 100644 --- a/src/input/cmd.rs +++ b/src/input/cmd.rs @@ -98,6 +98,12 @@ static COMMANDS: &[Command] = &[ function: cmd_http, hidden: true, }, + Command { + name: b"mem", + description: b"prints the memory stats", + function: cmd_mem, + hidden: true, + }, Command { name: b"menu", description: b"renders a sample menu", @@ -522,6 +528,10 @@ fn cmd_http(_args: &[u8]) { } } +fn cmd_mem(_args: &[u8]) { + unsafe { crate::mem::walk::walk_memory(); } +} + /// Experimental command function to evaluate the current TUI rendering options. fn cmd_menu(_args: &[u8]) { // Working sample, but loops without exit diff --git a/src/mem/pmm.rs b/src/mem/pmm.rs index 3392d01..f04cf50 100644 --- a/src/mem/pmm.rs +++ b/src/mem/pmm.rs @@ -259,8 +259,8 @@ pub unsafe fn set_physical_base(base: u64) { } #[inline(always)] -pub unsafe fn phys_to_virt(paddr: u64) -> *mut u8 { - (PHYSICAL_BASE + paddr) as *mut u8 +pub unsafe fn phys_to_virt(paddr: u64) -> *mut u64 { + (PHYSICAL_BASE + paddr) as *mut u64 } #[inline(always)] From 55e925882aad7c9fbccf6ed8414b8ba96114414c Mon Sep 17 00:00:00 2001 From: krusty Date: Thu, 21 Aug 2025 18:53:40 +0200 Subject: [PATCH 09/23] Clean the assembly stub --- iso/boot/boot.asm | 213 +--------------------------------------------- 1 file changed, 3 insertions(+), 210 deletions(-) diff --git a/iso/boot/boot.asm b/iso/boot/boot.asm index b64d1d3..51c175a 100644 --- a/iso/boot/boot.asm +++ b/iso/boot/boot.asm @@ -12,53 +12,12 @@ align 4096 ;dma: ; resb 4096 -pml4_table: - resq 512 -pdpt_table: - resq 512 -pd_table: - resq 512 -pt_table: - resq 512 - p4_table: resb 4096 p3_table: resb 4096 p2_table: resb 4096 -p1_page_tables: - resb 4096 -p1_page_tables_2: - resb 4096 - -p1_userspace_0: - resb 4096 -p1_userspace_1: - resb 4096 - -p3_fb_table: - resq 512 -p2_fb_table: - resq 512 - -p1_fb_table: - resb 4096 -p1_fb_table_0: - resb 4096 -p1_fb_table_1: - resb 4096 -p1_fb_table_2: - resb 4096 -p1_fb_table_3: - resb 4096 - -p1_low_table: - resq 512 -p1_extra_table: - resb 4096 -p1_table_page_tables: - resb 4096 align 16 ist1_stack: @@ -100,11 +59,6 @@ global p4_table global p3_table global p2_table -global p3_fb_table -global p2_fb_table -global p1_fb_table -global p1_fb_table_2 - global gdt_start global gdt_end global gdt_descriptor @@ -225,29 +179,12 @@ idt_start: times 256 dq 0 idt_end: -; -; -; - -section .data - -FB_P1_TABLES: - dq p1_fb_table_0 - dq p1_fb_table_1 - dq p1_fb_table_2 - dq p1_fb_table_3 - ; ; Page Tables Zeroing & Mapping ; section .text -%define FB_PHYS 0xFD000000 -%define FB_VIRT 0xFFFFFF8000000000 -%define PAGE_COUNT 4096 -%define PAGE_FLAGS 0b11 - zero_table: mov ecx, 512 xor eax, eax @@ -266,36 +203,9 @@ set_up_page_tables: lea edi, [p3_table] call zero_table - lea edi, [p3_fb_table] - call zero_table - lea edi, [p2_table] call zero_table - lea edi, [p2_fb_table] - call zero_table - - lea edi, [p1_fb_table_0] - call zero_table - - lea edi, [p1_fb_table_1] - call zero_table - - lea edi, [p1_fb_table_2] - call zero_table - - lea edi, [p1_fb_table_3] - call zero_table - - lea edi, [p1_page_tables] - call zero_table - - lea edi, [p1_userspace_0] - call zero_table - - lea edi, [p1_userspace_1] - call zero_table - ; Map P4[0] → P3 mov eax, p3_table or eax, 0b111 @@ -319,9 +229,11 @@ set_up_page_tables: mov dword [p2_table + ecx * 8 + 4], 0 inc ecx - cmp ecx, 512 + cmp ecx, 1 jne .map_1gib + ret + ; Allow CPL=3 access at 0x600_000--0x800_000 mov eax, 0x600000 @@ -334,125 +246,6 @@ set_up_page_tables: mov [p2_table + 4 * 8], eax mov dword [p2_table + 4 * 8 + 4], 0 - ; Identity-map - -; mov eax, p1_page_tables -; or eax, PAGE_FLAGS -; mov [p2_table + 1 * 8], eax -; mov dword [p2_table + 1 * 8 + 4], 0 - -; mov eax, p1_page_tables_2 -; or eax, PAGE_FLAGS -; mov [p2_table + 2 * 8], eax -; mov dword [p2_table + 2 * 8 + 4], 0 - -; xor ecx, 0 -;.map_self: -; mov eax, 0x131000 -; add eax, ecx -; or eax, PAGE_FLAGS -; mov edi, p1_page_tables -; mov ebx, ecx -; shr ebx, 12 -; shl ebx, 3 -; add edi, ebx - -; mov [edi], eax -; mov dword [edi + 4], 0 - -; add ecx, 0x1000 -; cmp ecx, 0x80000 -; jb .map_self - -;.map_self_2: -; mov eax, 0x13e000 -; add eax, ecx -; or eax, PAGE_FLAGS -; mov edi, p1_page_tables_2 -; mov ebx, ecx -; shr ebx, 12 -; shl ebx, 3 -; add edi, ebx - -; mov [edi], eax -; mov dword [edi + 4], 0 - -; add ecx, 0x1000 -; cmp ecx, 0x40000 -; jb .map_self_2 - - ret - - ; Framebuffer init - - mov eax, p3_fb_table - or eax, PAGE_FLAGS - mov [p4_table + 511 * 8], eax - mov dword [p4_table + 511 * 8 + 4], 0 - - mov eax, p2_fb_table - or eax, PAGE_FLAGS - mov [p3_fb_table + 0 * 8], eax - mov dword [p3_fb_table + 0 * 8 + 4], 0 - - mov eax, p1_fb_table_0 - or eax, PAGE_FLAGS - mov [p2_fb_table + 0 * 8], eax - mov dword [p2_fb_table + 0 * 8 + 4], 0 - - mov eax, p1_fb_table_1 - or eax, PAGE_FLAGS - mov [p2_fb_table + 1 * 8], eax - mov dword [p2_fb_table + 1 * 8 + 4], 0 - - mov eax, p1_fb_table_2 - or eax, PAGE_FLAGS - mov [p2_fb_table + 2 * 8], eax - mov dword [p2_fb_table + 2 * 8 + 4], 0 - - mov eax, p1_fb_table_3 - or eax, PAGE_FLAGS - mov [p2_fb_table + 3 * 8], eax - mov dword [p2_fb_table + 3 * 8 + 4], 0 - - ; Framebuffer P1 pages - - xor ecx, ecx - xor esi, esi -.map_fb_dynamic: - mov eax, FB_PHYS - add eax, ecx - or eax, PAGE_FLAGS - - mov ebx, esi - shl ebx, 3 - mov edi, [FB_P1_TABLES + ebx] - - mov ebx, ecx - shr ebx, 12 - and ebx, 511 - shl ebx, 3 - - add edi, ebx - mov [edi], eax - mov dword [edi + 4], 0 - - add ecx, 0x1000 - cmp ecx, PAGE_COUNT * 0x1000 - jae .done_fb_map - - ; Switch to next p1 table every 512 pages - mov ebx, ecx - shr ebx, 12 - and ebx, 511 - cmp ebx, 0 - jne .map_fb_dynamic - inc esi - cmp esi, (PAGE_COUNT + 511) / 512 - jae .done_fb_map - jmp .map_fb_dynamic - -.done_fb_map: ret enable_paging: From cc2750a87de96bf71502efe779171bc478a024be Mon Sep 17 00:00:00 2001 From: mikikichi Date: Fri, 22 Aug 2025 04:40:47 +0300 Subject: [PATCH 10/23] start of multiboot parsing improvement DOESNT COMPILE BTW! --- Makefile | 6 --- src/init/boot.rs | 35 ++++++++++++++++-- src/init/mod.rs | 1 + src/init/multiboot_helpers.rs | 69 +++++++++++++++++++++++++++++++++++ src/mem/frame.rs | 0 5 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 src/init/multiboot_helpers.rs create mode 100644 src/mem/frame.rs diff --git a/Makefile b/Makefile index 8dfbbe5..938b211 100644 --- a/Makefile +++ b/Makefile @@ -87,12 +87,6 @@ build_floppy: fat.img @echo "Hello from floppy!" > /tmp/hello.txt @mcopy -i fat.img /tmp/hello.txt ::HELLO.TXT - @mcopy -i fat.img ./print.bin ::PRINT.BIN - @mcopy -i fat.img ./print.elf ::PRINT.ELF - @mcopy -i fat.img ./go.elf ::GO.ELF - @mcopy -i fat.img ./sh.elf ::SH.ELF - @mcopy -i fat.img ./icmpresp.elf ::ICMPRESP.ELF - @mcopy -i fat.img ./garn.elf ::GARN.ELF # # RUN diff --git a/src/init/boot.rs b/src/init/boot.rs index 0d5ff41..c2c922a 100644 --- a/src/init/boot.rs +++ b/src/init/boot.rs @@ -74,7 +74,7 @@ pub struct AcpiRSDPTag { #[repr(C, packed)] pub struct AcpiSDTHeader { - pub signature: [u8; 4], + pub signature: [u8; 4], //array pub length: u32, pub revision: u8, pub checksum: u8, @@ -85,6 +85,23 @@ pub struct AcpiSDTHeader { pub creatpr_revision: u32, } +#[repr(C, packed)] //directive?? status kinda idfk +pub struct UsableMemory { + pub base: u64, + pub length: u64, + pub memtype: u32, + pub reserved: u32, + +} + + + + +static mut U_MEM: [UsableMemory: 200] = [default::default()u64; 200]; //change this accordingly!!! placeholder for now + +//&&&&&&& reference variable borrower cannot change +//usize like size_t from C +//main parser pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTag) -> usize { // Ensure alignment (Multiboot2 requires 8-byte aligned structure) let addr = align_up(base_addr, 8); @@ -111,6 +128,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa } 1 => { + debug!("Boot command line tag: "); let str_ptr = ptr + 8; @@ -121,7 +139,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa debugln!(cmdline); } - 3 => { + 3 => { debug!("Module tag found: "); //let start = *((ptr + 8) as *const u32); @@ -136,9 +154,9 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa 6 => { debugln!("Memory map tag"); - + //ptr casted to const memorytag pointer casted again to a pointer and marked as borrowed??? let mmap_tag = &*(ptr as *const MemoryMapTag); - let entries_start = (addr + core::mem::size_of::()) as *const u8; + let entries_start = (addr + core::mem::size_of::()) as *const u8; //jump to actual memory entries array let entry_size = mmap_tag.entry_size as usize; if entry_size > 0 { @@ -149,10 +167,19 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa let entry = &*entry_ptr; if entry.typ == 1 { + debug!("Usable memory region: "); debugn!(entry.base_addr as u64); + U_MEM[i].addr = entry.base_addr; + U_MEM[i].length = entry.length; + U_MEM[i].memtype = entry.typ; + U_MEM[i].reserved = entry.reserved; + + debug!(": "); + debugn!(entry.length as u64); + debugln!(" bytes"); } } diff --git a/src/init/mod.rs b/src/init/mod.rs index 2fb39df..afa3ff3 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -10,6 +10,7 @@ pub mod heap; pub mod pit; pub mod result; pub mod video; +pub mod multiboot_helpers; use spin::Mutex; diff --git a/src/init/multiboot_helpers.rs b/src/init/multiboot_helpers.rs new file mode 100644 index 0000000..a7d3160 --- /dev/null +++ b/src/init/multiboot_helpers.rs @@ -0,0 +1,69 @@ +const MULTIBOOT_HEADER: u32 = 1; + +const MULTIBOOT_SEARCH: u32 = 32768; +const MULTIBOOT_HEADER_ALIGN: u8 = 8; + +const MULTIBOOT2_HEADER_MAGIC: u64 = 0xe85250d6; + + +#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289 + + +#define MULTIBOOT_MOD_ALIGN 0x00001000 + + +#define MULTIBOOT_INFO_ALIGN 0x00000008 + +#define MULTIBOOT_TAG_ALIGN 8 +#define MULTIBOOT_TAG_TYPE_END 0 +#define MULTIBOOT_TAG_TYPE_CMDLINE 1 +#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 +#define MULTIBOOT_TAG_TYPE_MODULE 3 +#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 +#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 +#define MULTIBOOT_TAG_TYPE_MMAP 6 +#define MULTIBOOT_TAG_TYPE_VBE 7 +#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 +#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 +#define MULTIBOOT_TAG_TYPE_APM 10 +#define MULTIBOOT_TAG_TYPE_EFI32 11 +#define MULTIBOOT_TAG_TYPE_EFI64 12 +#define MULTIBOOT_TAG_TYPE_SMBIOS 13 +#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 +#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 +#define MULTIBOOT_TAG_TYPE_NETWORK 16 +#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 +#define MULTIBOOT_TAG_TYPE_EFI_BS 18 +#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 +#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 +#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 + +#define MULTIBOOT_HEADER_TAG_END 0 +#define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1 +#define MULTIBOOT_HEADER_TAG_ADDRESS 2 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS 3 +#define MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS 4 +#define MULTIBOOT_HEADER_TAG_FRAMEBUFFER 5 +#define MULTIBOOT_HEADER_TAG_MODULE_ALIGN 6 +#define MULTIBOOT_HEADER_TAG_EFI_BS 7 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 8 +#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 9 +#define MULTIBOOT_HEADER_TAG_RELOCATABLE 10 + +#define MULTIBOOT_MEMORY_AVAILABLE 1 +#define MULTIBOOT_MEMORY_RESERVED 2 +#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 +#define MULTIBOOT_MEMORY_NVS 4 +#define MULTIBOOT_MEMORY_BADRAM 5 + + +#define MULTIBOOT_ARCHITECTURE_I386 0 +#define MULTIBOOT_ARCHITECTURE_MIPS32 4 +#define MULTIBOOT_HEADER_TAG_OPTIONAL 1 + +#define MULTIBOOT_LOAD_PREFERENCE_NONE 0 +#define MULTIBOOT_LOAD_PREFERENCE_LOW 1 +#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2 + +#define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1 +#define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2 \ No newline at end of file diff --git a/src/mem/frame.rs b/src/mem/frame.rs new file mode 100644 index 0000000..e69de29 From 1b2309372c6b5f574984d0a551b464935026112c Mon Sep 17 00:00:00 2001 From: krusty Date: Fri, 22 Aug 2025 04:55:49 +0200 Subject: [PATCH 11/23] Fix syntax --- src/init/multiboot_helpers.rs | 117 +++++++++++++++++----------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/src/init/multiboot_helpers.rs b/src/init/multiboot_helpers.rs index a7d3160..d20a576 100644 --- a/src/init/multiboot_helpers.rs +++ b/src/init/multiboot_helpers.rs @@ -1,69 +1,72 @@ const MULTIBOOT_HEADER: u32 = 1; const MULTIBOOT_SEARCH: u32 = 32768; -const MULTIBOOT_HEADER_ALIGN: u8 = 8; - -const MULTIBOOT2_HEADER_MAGIC: u64 = 0xe85250d6; - - -#define MULTIBOOT2_BOOTLOADER_MAGIC 0x36d76289 +const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6; +const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; -#define MULTIBOOT_MOD_ALIGN 0x00001000 - - -#define MULTIBOOT_INFO_ALIGN 0x00000008 +const MULTIBOOT_MOD_ALIGN: u32 = 0x00001000; +const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; +const MULTIBOOT_TAG_ALIGN: u32 = 8; +const MULTIBOOT_HEADER_ALIGN: u8 = 8; -#define MULTIBOOT_TAG_ALIGN 8 -#define MULTIBOOT_TAG_TYPE_END 0 -#define MULTIBOOT_TAG_TYPE_CMDLINE 1 -#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2 -#define MULTIBOOT_TAG_TYPE_MODULE 3 -#define MULTIBOOT_TAG_TYPE_BASIC_MEMINFO 4 -#define MULTIBOOT_TAG_TYPE_BOOTDEV 5 -#define MULTIBOOT_TAG_TYPE_MMAP 6 -#define MULTIBOOT_TAG_TYPE_VBE 7 -#define MULTIBOOT_TAG_TYPE_FRAMEBUFFER 8 -#define MULTIBOOT_TAG_TYPE_ELF_SECTIONS 9 -#define MULTIBOOT_TAG_TYPE_APM 10 -#define MULTIBOOT_TAG_TYPE_EFI32 11 -#define MULTIBOOT_TAG_TYPE_EFI64 12 -#define MULTIBOOT_TAG_TYPE_SMBIOS 13 -#define MULTIBOOT_TAG_TYPE_ACPI_OLD 14 -#define MULTIBOOT_TAG_TYPE_ACPI_NEW 15 -#define MULTIBOOT_TAG_TYPE_NETWORK 16 -#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17 -#define MULTIBOOT_TAG_TYPE_EFI_BS 18 -#define MULTIBOOT_TAG_TYPE_EFI32_IH 19 -#define MULTIBOOT_TAG_TYPE_EFI64_IH 20 -#define MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR 21 +enum MULTIBOOT_TAG_TYPE { + MULTIBOOT_TAG_TYPE_END = 0, + MULTIBOOT_TAG_TYPE_CMDLINE = 1, + MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2, + MULTIBOOT_TAG_TYPE_MODULE = 3, + MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4, + MULTIBOOT_TAG_TYPE_BOOTDEV = 5, + MULTIBOOT_TAG_TYPE_MMAP = 6, + MULTIBOOT_TAG_TYPE_VBE = 7, + MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8, + MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9, + MULTIBOOT_TAG_TYPE_APM = 10, + MULTIBOOT_TAG_TYPE_EFI32 = 11, + MULTIBOOT_TAG_TYPE_EFI64 = 12, + MULTIBOOT_TAG_TYPE_SMBIOS = 13, + MULTIBOOT_TAG_TYPE_ACPI_OLD = 14, + MULTIBOOT_TAG_TYPE_ACPI_NEW = 15, + MULTIBOOT_TAG_TYPE_NETWORK = 16, + MULTIBOOT_TAG_TYPE_EFI_MMAP = 17, + MULTIBOOT_TAG_TYPE_EFI_BS = 18, + MULTIBOOT_TAG_TYPE_EFI32_IH = 19, + MULTIBOOT_TAG_TYPE_EFI64_IH = 20, + MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21, +} -#define MULTIBOOT_HEADER_TAG_END 0 -#define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1 -#define MULTIBOOT_HEADER_TAG_ADDRESS 2 -#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS 3 -#define MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS 4 -#define MULTIBOOT_HEADER_TAG_FRAMEBUFFER 5 -#define MULTIBOOT_HEADER_TAG_MODULE_ALIGN 6 -#define MULTIBOOT_HEADER_TAG_EFI_BS 7 -#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 8 -#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 9 -#define MULTIBOOT_HEADER_TAG_RELOCATABLE 10 +enum MULTIBOOT_HEADER_TAG { + MULTIBOOT_HEADER_TAG_END = 0, + MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST = 1, + MULTIBOOT_HEADER_TAG_ADDRESS = 2, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3, + MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4, + MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5, + MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6, + MULTIBOOT_HEADER_TAG_EFI_BS = 7, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 = 8, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 = 9, + MULTIBOOT_HEADER_TAG_RELOCATABLE = 10, +} -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 +enum MULTIBOOT_MEMORY { + MULTIBOOT_MEMORY_AVAILABLE = 1, + MULTIBOOT_MEMORY_RESERVED = 2, + MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3, + MULTIBOOT_MEMORY_NVS = 4, + MULTIBOOT_MEMORY_BADRAM = 5, +} +const MULTIBOOT_ARCHITECTURE_I386: u8 = 0; +const MULTIBOOT_ARCHITECTURE_MIPS32: u8 = 4; +const MULTIBOOT_HEADER_TAG_OPTIONAL: u8 = 1; -#define MULTIBOOT_ARCHITECTURE_I386 0 -#define MULTIBOOT_ARCHITECTURE_MIPS32 4 -#define MULTIBOOT_HEADER_TAG_OPTIONAL 1 +enum MULTIBOOT_LOAD_PREFERENCE { + MULTIBOOT_LOAD_PREFERENCE_NONE = 0, + MULTIBOOT_LOAD_PREFERENCE_LOW = 1, + MULTIBOOT_LOAD_PREFERENCE_HIGH = 2, +} -#define MULTIBOOT_LOAD_PREFERENCE_NONE 0 -#define MULTIBOOT_LOAD_PREFERENCE_LOW 1 -#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2 +const MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED: u8 = 1; +const MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED: u8 = 2; -#define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1 -#define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2 \ No newline at end of file From 45dd6f7b6610945de3afbd3983b149a24e9c0398 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Sat, 23 Aug 2025 11:05:36 +0300 Subject: [PATCH 12/23] Start of multiboot parsing --- src/init/mod.rs | 4 +- src/init/multiboot_helpers.rs | 72 ----------- src/init/{boot.rs => multiboot_parser.rs} | 150 +++++++++++++++------- 3 files changed, 109 insertions(+), 117 deletions(-) delete mode 100644 src/init/multiboot_helpers.rs rename src/init/{boot.rs => multiboot_parser.rs} (67%) diff --git a/src/init/mod.rs b/src/init/mod.rs index afa3ff3..e14e385 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -1,5 +1,5 @@ pub mod ascii; -pub mod boot; +pub mod multiboot_parser; pub mod color; pub mod config; pub mod cpu; @@ -10,7 +10,7 @@ pub mod heap; pub mod pit; pub mod result; pub mod video; -pub mod multiboot_helpers; + use spin::Mutex; diff --git a/src/init/multiboot_helpers.rs b/src/init/multiboot_helpers.rs deleted file mode 100644 index d20a576..0000000 --- a/src/init/multiboot_helpers.rs +++ /dev/null @@ -1,72 +0,0 @@ -const MULTIBOOT_HEADER: u32 = 1; - -const MULTIBOOT_SEARCH: u32 = 32768; - -const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6; -const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; - -const MULTIBOOT_MOD_ALIGN: u32 = 0x00001000; -const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; -const MULTIBOOT_TAG_ALIGN: u32 = 8; -const MULTIBOOT_HEADER_ALIGN: u8 = 8; - -enum MULTIBOOT_TAG_TYPE { - MULTIBOOT_TAG_TYPE_END = 0, - MULTIBOOT_TAG_TYPE_CMDLINE = 1, - MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2, - MULTIBOOT_TAG_TYPE_MODULE = 3, - MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4, - MULTIBOOT_TAG_TYPE_BOOTDEV = 5, - MULTIBOOT_TAG_TYPE_MMAP = 6, - MULTIBOOT_TAG_TYPE_VBE = 7, - MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8, - MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9, - MULTIBOOT_TAG_TYPE_APM = 10, - MULTIBOOT_TAG_TYPE_EFI32 = 11, - MULTIBOOT_TAG_TYPE_EFI64 = 12, - MULTIBOOT_TAG_TYPE_SMBIOS = 13, - MULTIBOOT_TAG_TYPE_ACPI_OLD = 14, - MULTIBOOT_TAG_TYPE_ACPI_NEW = 15, - MULTIBOOT_TAG_TYPE_NETWORK = 16, - MULTIBOOT_TAG_TYPE_EFI_MMAP = 17, - MULTIBOOT_TAG_TYPE_EFI_BS = 18, - MULTIBOOT_TAG_TYPE_EFI32_IH = 19, - MULTIBOOT_TAG_TYPE_EFI64_IH = 20, - MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21, -} - -enum MULTIBOOT_HEADER_TAG { - MULTIBOOT_HEADER_TAG_END = 0, - MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST = 1, - MULTIBOOT_HEADER_TAG_ADDRESS = 2, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3, - MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4, - MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5, - MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6, - MULTIBOOT_HEADER_TAG_EFI_BS = 7, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 = 8, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 = 9, - MULTIBOOT_HEADER_TAG_RELOCATABLE = 10, -} - -enum MULTIBOOT_MEMORY { - MULTIBOOT_MEMORY_AVAILABLE = 1, - MULTIBOOT_MEMORY_RESERVED = 2, - MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3, - MULTIBOOT_MEMORY_NVS = 4, - MULTIBOOT_MEMORY_BADRAM = 5, -} - -const MULTIBOOT_ARCHITECTURE_I386: u8 = 0; -const MULTIBOOT_ARCHITECTURE_MIPS32: u8 = 4; -const MULTIBOOT_HEADER_TAG_OPTIONAL: u8 = 1; - -enum MULTIBOOT_LOAD_PREFERENCE { - MULTIBOOT_LOAD_PREFERENCE_NONE = 0, - MULTIBOOT_LOAD_PREFERENCE_LOW = 1, - MULTIBOOT_LOAD_PREFERENCE_HIGH = 2, -} - -const MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED: u8 = 1; -const MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED: u8 = 2; - diff --git a/src/init/boot.rs b/src/init/multiboot_parser.rs similarity index 67% rename from src/init/boot.rs rename to src/init/multiboot_parser.rs index c2c922a..f37802d 100644 --- a/src/init/boot.rs +++ b/src/init/multiboot_parser.rs @@ -1,3 +1,76 @@ +pub const MULTIBOOT_HEADER: u32 = 1; + +pub const MULTIBOOT_SEARCH: u32 = 32768; + +pub const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6; +pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; + +pub const MULTIBOOT_MOD_ALIGN: u32 = 0x00001000; +pub const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; +pub const MULTIBOOT_TAG_ALIGN: u32 = 8; +pub const MULTIBOOT_HEADER_ALIGN: u8 = 8; + +pub enum MULTIBOOT_TAG_TYPE { + MULTIBOOT_TAG_TYPE_END = 0, + MULTIBOOT_TAG_TYPE_CMDLINE = 1, + MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2, + MULTIBOOT_TAG_TYPE_MODULE = 3, + MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4, + MULTIBOOT_TAG_TYPE_BOOTDEV = 5, + MULTIBOOT_TAG_TYPE_MMAP = 6, + MULTIBOOT_TAG_TYPE_VBE = 7, + MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8, + MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9, + MULTIBOOT_TAG_TYPE_APM = 10, + MULTIBOOT_TAG_TYPE_EFI32 = 11, + MULTIBOOT_TAG_TYPE_EFI64 = 12, + MULTIBOOT_TAG_TYPE_SMBIOS = 13, + MULTIBOOT_TAG_TYPE_ACPI_OLD = 14, + MULTIBOOT_TAG_TYPE_ACPI_NEW = 15, + MULTIBOOT_TAG_TYPE_NETWORK = 16, + MULTIBOOT_TAG_TYPE_EFI_MMAP = 17, + MULTIBOOT_TAG_TYPE_EFI_BS = 18, + MULTIBOOT_TAG_TYPE_EFI32_IH = 19, + MULTIBOOT_TAG_TYPE_EFI64_IH = 20, + MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21, +} + +pub enum MULTIBOOT_HEADER_TAG { + MULTIBOOT_HEADER_TAG_END = 0, + MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST = 1, + MULTIBOOT_HEADER_TAG_ADDRESS = 2, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3, + MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4, + MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5, + MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6, + MULTIBOOT_HEADER_TAG_EFI_BS = 7, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 = 8, + MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 = 9, + MULTIBOOT_HEADER_TAG_RELOCATABLE = 10, +} + +pub enum MULTIBOOT_MEMORY { + MULTIBOOT_MEMORY_AVAILABLE = 1, + MULTIBOOT_MEMORY_RESERVED = 2, + MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3, + MULTIBOOT_MEMORY_NVS = 4, + MULTIBOOT_MEMORY_BADRAM = 5, +} + +pub const MULTIBOOT_ARCHITECTURE_I386: u8 = 0; +pub const MULTIBOOT_ARCHITECTURE_MIPS32: u8 = 4; +pub const MULTIBOOT_HEADER_TAG_OPTIONAL: u8 = 1; + +pub enum MULTIBOOT_LOAD_PREFERENCE { + MULTIBOOT_LOAD_PREFERENCE_NONE = 0, + MULTIBOOT_LOAD_PREFERENCE_LOW = 1, + MULTIBOOT_LOAD_PREFERENCE_HIGH = 2, +} + +const MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED: u8 = 1; +const MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED: u8 = 2; + + use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ buffer::Color, write::{newline, number, string} } }; @@ -24,7 +97,7 @@ pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult #[repr(C)] #[derive(Debug)] pub struct TagHeader { - pub typ: u32, + pub typ: MULTIBOOT_TAG_TYPE, pub size: u32, } @@ -87,17 +160,16 @@ pub struct AcpiSDTHeader { #[repr(C, packed)] //directive?? status kinda idfk pub struct UsableMemory { - pub base: u64, - pub length: u64, - pub memtype: u32, - pub reserved: u32, + start: u64, + end: u64, + count: u8, } -static mut U_MEM: [UsableMemory: 200] = [default::default()u64; 200]; //change this accordingly!!! placeholder for now +static mut U_MEM: UsableMemory = UsableMemory{start: 0, end: 0, count: 0}; //change this accordingly!!! placeholder for now //&&&&&&& reference variable borrower cannot change //usize like size_t from C @@ -114,20 +186,22 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa let mut tag_count = 0; + + while ptr < end { let tag = &*(ptr as *const TagHeader); if tag.size < 8 || tag.size > 4096 { debugln!("Invalid tag size: abort"); break; } - + match tag.typ { - 0 => { + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_END => { debugln!("End tag found"); break; } - 1 => { + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_CMDLINE => { debug!("Boot command line tag: "); @@ -139,7 +213,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa debugln!(cmdline); } - 3 => { + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_MODULE => { debug!("Module tag found: "); //let start = *((ptr + 8) as *const u32); @@ -152,41 +226,13 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa debugln!(cmdline); } - 6 => { - debugln!("Memory map tag"); - //ptr casted to const memorytag pointer casted again to a pointer and marked as borrowed??? + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_MMAP => { + //ptr as *const foo ; immutable raw pointer let mmap_tag = &*(ptr as *const MemoryMapTag); - let entries_start = (addr + core::mem::size_of::()) as *const u8; //jump to actual memory entries array - let entry_size = mmap_tag.entry_size as usize; - - if entry_size > 0 { - let entries_count = (mmap_tag.size as usize - core::mem::size_of::()) / entry_size; - - for i in 0..entries_count { - let entry_ptr = entries_start.add(i * entry_size) as *const MemoryMapEntry; - let entry = &*entry_ptr; - - if entry.typ == 1 { - - debug!("Usable memory region: "); - debugn!(entry.base_addr as u64); - U_MEM[i].addr = entry.base_addr; - U_MEM[i].length = entry.length; - U_MEM[i].memtype = entry.typ; - U_MEM[i].reserved = entry.reserved; - - - debug!(": "); - - debugn!(entry.length as u64); - - debugln!(" bytes"); - } - } - } + memory_map_tag(mmap_tag); } - 8 => { + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_FRAMEBUFFER => { debugln!("Framebuffer tag: "); fb_tag = &*(ptr as *const FramebufferTag); @@ -248,7 +294,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa } - 14 => { + MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_ACPI_OLD => { debugln!("ACPI v1 Root System Descriptor Pointer tag"); let acpi_tag = &*(ptr as *const AcpiRSDPTag); @@ -280,6 +326,24 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa tag_count } +pub unsafe fn memory_map_tag(mmap_tag: &MemoryMapTag) { + debugln!("Memory map tag"); + let entries_start = (mmap_tag + core::mem::size_of::()) as *const u8; //wont compile look into this + let entry_size = mmap_tag.entry_size as usize; + let end + + + +} + + + + +pub unsafe fn boot_line_tag() { + +} + + fn align_up(x: usize, align: usize) -> usize { (x + align - 1) & !(align - 1) } From 9e97e4c7a6903e806217eb7fcbd1a6769d9389ed Mon Sep 17 00:00:00 2001 From: mikikichi Date: Mon, 25 Aug 2025 14:51:45 +0300 Subject: [PATCH 13/23] wip parser --- src/init/multiboot_parser.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/init/multiboot_parser.rs b/src/init/multiboot_parser.rs index f37802d..b2610b8 100644 --- a/src/init/multiboot_parser.rs +++ b/src/init/multiboot_parser.rs @@ -103,7 +103,7 @@ pub struct TagHeader { #[repr(C)] #[derive(Debug)] -struct MemoryMapTag { +pub struct MemoryMapTag { typ: u32, size: u32, entry_size: u32, @@ -228,7 +228,7 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_MMAP => { //ptr as *const foo ; immutable raw pointer - let mmap_tag = &*(ptr as *const MemoryMapTag); + let mmap_tag = &*(ptr as *mut MemoryMapTag); memory_map_tag(mmap_tag); } @@ -326,11 +326,19 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa tag_count } -pub unsafe fn memory_map_tag(mmap_tag: &MemoryMapTag) { +pub unsafe fn memory_map_tag(mut mmap_tag: &MemoryMapTag) { + debugln!("Memory map tag"); - let entries_start = (mmap_tag + core::mem::size_of::()) as *const u8; //wont compile look into this - let entry_size = mmap_tag.entry_size as usize; - let end + let tag_size = mmap_tag.size as usize; + let entry_size = mmap_tag.entry_size as usize; + let mut entries_start = &mut mmap_tag as *mut _ as *mut u8; + let mut entries_end = entries_start.add(tag_size); + let entry: MemoryMapEntry = MemoryMapEntry {base_addr: 0, length: 0, typ: 0, reserved: 0}; + + while entries_start <= entries_end { + + } + From c0538100c8d1b05f7672053eaf7f4fe5b3595ec3 Mon Sep 17 00:00:00 2001 From: krusty Date: Mon, 25 Aug 2025 16:13:33 +0200 Subject: [PATCH 14/23] Fix errors --- src/init/mod.rs | 6 +++--- src/init/multiboot_parser.rs | 2 +- src/init/video.rs | 2 +- src/video/mode.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/init/mod.rs b/src/init/mod.rs index e14e385..f847cc2 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -17,12 +17,12 @@ use spin::Mutex; const BUFFER_SIZE: usize = 1024; static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -static mut FRAMEBUFFER: Option = None; +static mut FRAMEBUFFER: Option = None; pub fn init(multiboot_ptr: u64) { debugln!("Kernel init start"); - let mut framebuffer_tag: boot::FramebufferTag = boot::FramebufferTag{ + let mut framebuffer_tag: multiboot_parser::FramebufferTag = multiboot_parser::FramebufferTag{ ..Default::default() }; @@ -48,7 +48,7 @@ pub fn init(multiboot_ptr: u64) { result::print_result( "Read Multiboot2 tags", - boot::print_info(multiboot_ptr, &mut framebuffer_tag), + multiboot_parser::print_info(multiboot_ptr, &mut framebuffer_tag), ); let video_result = video::print_result(&framebuffer_tag); diff --git a/src/init/multiboot_parser.rs b/src/init/multiboot_parser.rs index b2610b8..887e4ad 100644 --- a/src/init/multiboot_parser.rs +++ b/src/init/multiboot_parser.rs @@ -10,6 +10,7 @@ pub const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; pub const MULTIBOOT_TAG_ALIGN: u32 = 8; pub const MULTIBOOT_HEADER_ALIGN: u8 = 8; +#[derive(Copy,Clone)] pub enum MULTIBOOT_TAG_TYPE { MULTIBOOT_TAG_TYPE_END = 0, MULTIBOOT_TAG_TYPE_CMDLINE = 1, @@ -95,7 +96,6 @@ pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult } #[repr(C)] -#[derive(Debug)] pub struct TagHeader { pub typ: MULTIBOOT_TAG_TYPE, pub size: u32, diff --git a/src/init/video.rs b/src/init/video.rs index 586cbb1..80cbb5e 100644 --- a/src/init/video.rs +++ b/src/init/video.rs @@ -49,7 +49,7 @@ pub fn map_framebuffer( } } -pub fn print_result(fb: &super::boot::FramebufferTag) -> super::result::InitResult { +pub fn print_result(fb: &super::multiboot_parser::FramebufferTag) -> super::result::InitResult { use crate::video; video::mode::init_video(fb); diff --git a/src/video/mode.rs b/src/video/mode.rs index fd92c07..04bd391 100644 --- a/src/video/mode.rs +++ b/src/video/mode.rs @@ -12,7 +12,7 @@ pub enum VideoMode { static mut VIDEO_MODE: Option = Some(VideoMode::TextMode); -pub fn init_video(fb: &crate::init::boot::FramebufferTag) { +pub fn init_video(fb: &crate::init::multiboot_parser::FramebufferTag) { unsafe { VIDEO_MODE = Some(VideoMode::Framebuffer { address: fb.addr as *mut u8, From 4567cdc6ea1399648c51eea4b62fb7feeb918e47 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Wed, 27 Aug 2025 02:57:00 +0300 Subject: [PATCH 15/23] wip small changes --- iso/boot/grub/grub.cfg | 5 +++++ src/init/multiboot_parser.rs | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/iso/boot/grub/grub.cfg b/iso/boot/grub/grub.cfg index e489448..fb2ebfa 100644 --- a/iso/boot/grub/grub.cfg +++ b/iso/boot/grub/grub.cfg @@ -10,6 +10,11 @@ menuentry "rou2exOS Rusted Edition (text mode)" { boot } +menuentry "rou2exOS Rusted Edition (text debug mode)" { + multiboot2 /boot/kernel_text.elf debug + boot +} + menuentry "rou2exOS Rusted Edition (graphics)" { multiboot2 /boot/kernel_graphics.elf grafix boot diff --git a/src/init/multiboot_parser.rs b/src/init/multiboot_parser.rs index 887e4ad..79a0c81 100644 --- a/src/init/multiboot_parser.rs +++ b/src/init/multiboot_parser.rs @@ -329,15 +329,17 @@ pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTa pub unsafe fn memory_map_tag(mut mmap_tag: &MemoryMapTag) { debugln!("Memory map tag"); - let tag_size = mmap_tag.size as usize; + let tag_size = mmap_tag.size as u8; let entry_size = mmap_tag.entry_size as usize; - let mut entries_start = &mut mmap_tag as *mut _ as *mut u8; - let mut entries_end = entries_start.add(tag_size); + let mut entries_start = *(&mut mmap_tag as *mut _ as *mut u8); //wrong size here + let entry: MemoryMapEntry = MemoryMapEntry {base_addr: 0, length: 0, typ: 0, reserved: 0}; - while entries_start <= entries_end { - - } + + debugln!("Tag size"); + debugn!(tag_size); + debugln!("\nEntry size"); + debugn!(entry_size); From a6576850c90f7acda669632e647530d58eed8c55 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Sun, 31 Aug 2025 06:04:29 +0300 Subject: [PATCH 16/23] stash of wip code --- src/init/main.rs | 119 +++++++++++ src/init/mod.rs | 117 +---------- src/init/multiboot2/header.rs | 73 +++++++ src/init/multiboot2/info.rs | 0 src/init/multiboot2/mod.rs | 3 + src/init/multiboot2/parser.rs | 234 +++++++++++++++++++++ src/init/multiboot2/tags.rs | 74 +++++++ src/init/multiboot_parser.rs | 371 ---------------------------------- src/main.rs | 9 +- 9 files changed, 511 insertions(+), 489 deletions(-) create mode 100644 src/init/main.rs create mode 100644 src/init/multiboot2/header.rs create mode 100644 src/init/multiboot2/info.rs create mode 100644 src/init/multiboot2/mod.rs create mode 100644 src/init/multiboot2/parser.rs create mode 100644 src/init/multiboot2/tags.rs delete mode 100644 src/init/multiboot_parser.rs diff --git a/src/init/main.rs b/src/init/main.rs new file mode 100644 index 0000000..8b3ed10 --- /dev/null +++ b/src/init/main.rs @@ -0,0 +1,119 @@ +use crate::init::multiboot2; +use super::{cpu,result,idt,heap,video,pit,fs}; +use spin::Mutex; + +const BUFFER_SIZE: usize = 1024; + +static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); +static mut FRAMEBUFFER: Option = None; + +pub fn init(m2_ptr: *mut usize, m2_magic: u32) { + unsafe { + multiboot2::parser::parse_multiboot2_info(m2_ptr, m2_magic); + } + debugln!("Kernel init start"); + + /*let mut framebuffer_tag: multiboot_parser::FramebufferTag = multiboot_parser::FramebufferTag{ + ..Default::default() + }; */ + + result::print_result( + "Load kernel", + result::InitResult::Passed, + ); + + result::print_result( + "Check 64-bit Long Mode", + cpu::check_mode(), + ); + + result::print_result( + "Reload IDT, GDT and TSS", + idt::get_result() + ); + + result::print_result( + "Initialize heap allocator", + heap::print_result(), + ); + + /*result::print_result( + "Read Multiboot2 tags", + multiboot_parser::print_info(m2_ptr, &mut framebuffer_tag), + ); + */ + let video_result = video::print_result(&framebuffer_tag); + + result::print_result( + "Initialize video", + video_result, + ); + + result::print_result( + "Start PIC timer", + pit::get_result(), + ); + + result::print_result( + "Check floppy drive", + fs::check_floppy(), + ); + + // TODO: Fallback to floppy to dump debug logs + init buffer + if video_result == result::InitResult::Passed { + INIT_BUFFER.lock().flush(); + } + /* + color::color_demo(); + ascii::ascii_art(); + */ + /*commented out cuz EAR PAINN + // Play startup melody + crate::audio::midi::play_melody(); + crate::audio::fs::play_midi_file(); + let freqs = [440, 880, 660, 550]; + for f in freqs { + crate::audio::beep::stop_beep(); + crate::audio::beep::beep(f); + crate::audio::midi::wait_millis(300); + } + crate::audio::beep::stop_beep(); + */ +} + +struct Buffer { + buf: [u8; 1024], + pos: usize, +} + +impl Buffer { + /// Creates and returns a new instance of Buffer. + const fn new() -> Self { + Self { + buf: [0u8; BUFFER_SIZE], + pos: 0, + } + } + + /// Adds given byte slice to the buffer at offset of self.pos. + fn append(&mut self, s: &[u8]) { + // Take the input length, or the offset + let len = s.len().min(self.buf.len() - self.pos); + + if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { + if let Some(slice) = s.get(..len) { + // Copy the slice into buffer at offset of self.pos + buf.copy_from_slice(slice); + self.pos += len; + } + } + } + + /// Puts the contents of buf into the printb! macro. + fn flush(&self) { + if let Some(buf) = self.buf.get(..self.pos) { + printb!(buf); + } + } +} + diff --git a/src/init/mod.rs b/src/init/mod.rs index f847cc2..fd2e338 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -1,5 +1,4 @@ pub mod ascii; -pub mod multiboot_parser; pub mod color; pub mod config; pub mod cpu; @@ -10,117 +9,5 @@ pub mod heap; pub mod pit; pub mod result; pub mod video; - - -use spin::Mutex; - -const BUFFER_SIZE: usize = 1024; - -static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -static mut FRAMEBUFFER: Option = None; - -pub fn init(multiboot_ptr: u64) { - debugln!("Kernel init start"); - - let mut framebuffer_tag: multiboot_parser::FramebufferTag = multiboot_parser::FramebufferTag{ - ..Default::default() - }; - - result::print_result( - "Load kernel", - result::InitResult::Passed, - ); - - result::print_result( - "Check 64-bit Long Mode", - cpu::check_mode(), - ); - - result::print_result( - "Reload IDT, GDT and TSS", - idt::get_result() - ); - - result::print_result( - "Initialize heap allocator", - heap::print_result(), - ); - - result::print_result( - "Read Multiboot2 tags", - multiboot_parser::print_info(multiboot_ptr, &mut framebuffer_tag), - ); - - let video_result = video::print_result(&framebuffer_tag); - - result::print_result( - "Initialize video", - video_result, - ); - - result::print_result( - "Start PIC timer", - pit::get_result(), - ); - - result::print_result( - "Check floppy drive", - fs::check_floppy(), - ); - - // TODO: Fallback to floppy to dump debug logs + init buffer - if video_result == result::InitResult::Passed { - INIT_BUFFER.lock().flush(); - } - - color::color_demo(); - ascii::ascii_art(); - - // Play startup melody - //crate::audio::midi::play_melody(); - //crate::audio::fs::play_midi_file(); - let freqs = [440, 880, 660, 550]; - for f in freqs { - crate::audio::beep::stop_beep(); - crate::audio::beep::beep(f); - crate::audio::midi::wait_millis(300); - } - crate::audio::beep::stop_beep(); -} - -struct Buffer { - buf: [u8; 1024], - pos: usize, -} - -impl Buffer { - /// Creates and returns a new instance of Buffer. - const fn new() -> Self { - Self { - buf: [0u8; BUFFER_SIZE], - pos: 0, - } - } - - /// Adds given byte slice to the buffer at offset of self.pos. - fn append(&mut self, s: &[u8]) { - // Take the input length, or the offset - let len = s.len().min(self.buf.len() - self.pos); - - if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { - if let Some(slice) = s.get(..len) { - // Copy the slice into buffer at offset of self.pos - buf.copy_from_slice(slice); - self.pos += len; - } - } - } - - /// Puts the contents of buf into the printb! macro. - fn flush(&self) { - if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf); - } - } -} - +pub mod main; +pub mod multiboot2; \ No newline at end of file diff --git a/src/init/multiboot2/header.rs b/src/init/multiboot2/header.rs new file mode 100644 index 0000000..b6c2ee9 --- /dev/null +++ b/src/init/multiboot2/header.rs @@ -0,0 +1,73 @@ +pub const MULTIBOOT_HEADER: u32 = 1; + +pub const MULTIBOOT_SEARCH: u32 = 32768; + +pub const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6; +pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; + +pub const MULTIBOOT_MOD_ALIGN: u32 = 0x00001000; +pub const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; +pub const MULTIBOOT_TAG_ALIGN: u32 = 8; +pub const MULTIBOOT_HEADER_ALIGN: u8 = 8; + +#[derive(Copy,Clone,PartialEq)] + +pub enum M2TagType { + End = 0, + CmdLine = 1, + BootLoaderName = 2, + Module = 3, + BasicMemoryInfo = 4, + BootDev = 5, + Mmap = 6, + VBE = 7, + Framebuffer = 8, + ElfSections = 9, + APM = 10, + EFI32 = 11, + EFI64 = 12, + SMBIOS = 13, + AcpiOLD = 14, + AcpiNEW = 15, + Network = 16, + EFIMmap = 17, + EFIBs = 18, + EFI32IH = 19, + EFI64IH = 20, + LoadBaseAddr = 21, +} + +pub enum M2HeaderTag { + End = 0, + InformationRequest = 1, + Address = 2, + EntryAddress = 3, + ConsoleFlags = 4, + Framebuffer = 5, + ModuleAlign = 6, + EFIBS = 7, + EntryAddressEFI32 = 8, + EntryAddressEFI64 = 9, + Relocatable = 10, +} + +pub enum M2Memory { + Avaible = 1, + Reserved = 2, + AcpiReclaimable = 3, + NVS = 4, + BADRAM = 5, +} + +pub const MULTIBOOT_ARCHITECTURE_I386: u8 = 0; +pub const MULTIBOOT_ARCHITECTURE_MIPS32: u8 = 4; +pub const MULTIBOOT_HEADER_TAG_OPTIONAL: u8 = 1; + +pub enum MultibootLoadPreference { + MultibootLoadPreferenceNone = 0, + MultibootLoadPreferenceLow = 1, + MultibootLoadPreferenceHigh = 2, +} + +const MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED: u8 = 1; +const MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED: u8 = 2; diff --git a/src/init/multiboot2/info.rs b/src/init/multiboot2/info.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/init/multiboot2/mod.rs b/src/init/multiboot2/mod.rs new file mode 100644 index 0000000..11c7fd2 --- /dev/null +++ b/src/init/multiboot2/mod.rs @@ -0,0 +1,3 @@ +pub mod header; +pub mod tags; +pub mod parser; \ No newline at end of file diff --git a/src/init/multiboot2/parser.rs b/src/init/multiboot2/parser.rs new file mode 100644 index 0000000..05938cf --- /dev/null +++ b/src/init/multiboot2/parser.rs @@ -0,0 +1,234 @@ +use super::{m2_header,tags}; + +/*use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ + buffer::Color, write::{newline, number, string} +} }; +use super::{result::InitResult}; */ + +/*pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult { + unsafe { + debug!("Multiboot2 pointer: "); + debugn!(multiboot_ptr); + debugln!(""); + + if parse_multiboot2_info((multiboot_ptr as u64) as usize, fb_tag) > 0 { + return InitResult::Passed; + } + } + + debug!("Multiboot2 pointer: "); + debugn!(multiboot_ptr); + debugln!(""); + + InitResult::Failed +} +*/ + + +/* let addr = align_up(base_addr, 8); + + // First 4 bytes: total size of the multiboot info + let total_size = *(addr as *const u32) as usize; + + let mut ptr = addr + 8; + let end = addr + total_size; + + let mut tag_count = 0; + +*/ + + + + +//static mut U_MEM: UsableMemory = UsableMemory{start: 0, end: 0, count: 0}; //change this accordingly!!! placeholder for now + + +pub unsafe fn parse_multiboot2_info(m2_ptr: *mut usize, m2_magic: u32) { + //check magic, needs to match + if m2_magic != m2_header::MULTIBOOT2_BOOTLOADER_MAGIC { + return; //return 1 here??? + }; + //alignment to 8 + let mut m2_tag = &*((m2_ptr as *mut u8).add(8) as *mut tags::TagHeader); + + + while m2_tag.typ != m2_header::M2TagType::End { + + match m2_tag.typ { + + m2_header::M2TagType::CmdLine => { + break; + } + + m2_header::M2TagType::Module => { + break; + } + + m2_header::M2TagType::Mmap => { + let mmap_tag = &*(m2_tag as *const _ as *const tags::MemoryMapTag); + memory_map_tag(mmap_tag); + } + + m2_header::M2TagType::Framebuffer => { + break; + + } + + m2_header::M2TagType::AcpiOLD => { + break; + } + + _ => { + break; + } + } + + m2_tag = &*((align_up(m2_tag.size as usize, 8) as *mut tags::TagHeader)); + + + } + + +} + +pub unsafe fn memory_map_tag(mut mmap_tag: &tags::MemoryMapTag) { + + let tag_size = mmap_tag.size as usize; + let entry_size = mmap_tag.entry_size as usize; //incrementation + + let mut entries_start = mmap_tag as *const _ as *mut u8; + let mut tag_end = entries_start.add(tag_size); + + + + + + +} + +//stashed code for now!!! + + +pub unsafe fn acpi_old_tag() { + /* + debugln!("ACPI v1 Root System Descriptor Pointer tag"); + + let acpi_tag = &*(ptr as *const AcpiRSDPTag); + debug!("Signature: "); + debug!(acpi_tag.signature); + debug!("\nOEM: "); + debug!(acpi_tag.oemid); + debugln!(""); + + let acpi_sdt = &*(acpi_tag.rsdt_addr as *const AcpiSDTHeader); + */ +} + +pub unsafe fn module_tag() { + debug!("Module tag found: "); + /* + //let start = *((ptr + 8) as *const u32); + //let end = *((ptr + 12) as *const u32); + let str_ptr = ptr + 16; + let str_len = tag.size as usize - 16; + let raw_bytes = core::slice::from_raw_parts(str_ptr as *const u8, str_len); + + let cmdline = core::str::from_utf8_unchecked(raw_bytes); + debugln!(cmdline); + */ +} + + + +pub unsafe fn boot_line_tag() { + debug!("Boot command line tag: "); + + /*let str_ptr = ptr + 8; + let str_len = tag.size as usize - 8; + let raw_bytes = core::slice::from_raw_parts(str_ptr as *const u8, str_len); + + let cmdline = core::str::from_utf8_unchecked(raw_bytes); + debugln!(cmdline); + */ +} + +pub unsafe fn framebuffer_tag() { + debugln!("Framebuffer tag: "); + /* + b_tag = &*(ptr as *const FramebufferTag); + + debug!("Framebuffer address: "); + debugn!(fb_tag.addr as u64); + debugln!(""); + + debug!("(bpp + res): "); + debugn!(fb_tag.bpp as u64); + debug!(" + "); + debugn!(fb_tag.width as u64); + debug!("x"); + debugn!(fb_tag.height as u64); + debugln!(""); + + debug!("Pitch: "); + debugn!(fb_tag.pitch); + debugln!(""); + + + use core::ptr; + use x86_64::registers::control::Cr3; + + unsafe { + if fb_tag.addr == 0xb8000 { + ptr += align_up(tag.size as usize, 8); + continue; + } + + rprint!("Mapping framebuffer\n"); + let virt_base = 0xffff_8000_0000_0000u64 + fb_tag.addr as u64; + + //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, 0xffff_8000_0000_0000 + fb_tag.addr as u64); + //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, virt_base); + crate::mem::pmm::map_framebuffer(0xfd00_0000, 0xffff_8000_fd00_0000); + + let fb_ptr = 0xffff_8000_fd00_0000 as *mut u64; + + *fb_ptr = 0xFFFFFFFF; + + draw_rect(fb_ptr, 150, 150, 100, 100, 4096, 0x00ffffff); + draw_rect(fb_ptr, 250, 250, 100, 100, 4096, 0x00ff0000); + draw_rect(fb_ptr, 350, 350, 100, 100, 4096, 0x0000ff00); + draw_rect(fb_ptr, 450, 450, 100, 100, 4096, 0x000000ff); + + if let Some(font) = parse_psf(super::font::PSF_FONT) { + draw_text_psf("[guest@rou2ex:/] > ", &font, 25, 30, 0x0000ff00, fb_ptr, fb_tag.pitch as usize, fb_tag.bpp as usize); + draw_text_psf("[guest@rou2ex:/] > ", &font, 25, 50, 0x00ffd700, fb_ptr, fb_tag.pitch as usize, fb_tag.bpp as usize); + + //draw_char("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 35, 35, fb_ptr, fb_tag.pitch as usize, 0xdeadbeef, FONT_RAW); + } + + //draw_test_char(35, 35, fb_ptr); + //draw_text_psf("ABCDEFGHIJKLMNOPQRSTUVWXYZ",&FONT_RAW, 35, 35, 0x00ff00, fb_ptr, fb_tag.pitch, fb_tag.bpp); + } + + //dump_debug_log_to_file(); + */ +} + + + +fn align_up(x: usize, align: usize) -> usize { + (x + align - 1) & !(align - 1) +} + + +pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { + for y in y0..(y0 + h) { + for x in x0..(x0 + w) { + let offset = y * (pitch / 4) + x; + + ptr.add(offset).write_volatile(color as u64); + } + } +} + + diff --git a/src/init/multiboot2/tags.rs b/src/init/multiboot2/tags.rs new file mode 100644 index 0000000..bb52b89 --- /dev/null +++ b/src/init/multiboot2/tags.rs @@ -0,0 +1,74 @@ + +#[repr(C)] +#[derive(Copy,Clone)] +pub struct TagHeader { + pub typ: header::M2TagType, + pub size: u32, +} + +#[repr(C)] +#[derive(Debug)] +pub struct MemoryMapTag { + pub typ: u32, + pub size: u32, + pub entry_size: u32, + pub entry_version: u32, + pub entries: MemoryMapEntry +} + + +#[repr(C, packed)] +#[derive(Debug)] +pub struct MemoryMapEntry { + pub base_addr: u64, + pub length: u64, + pub typ: u32, + pub reserved: u32, +} + +#[derive(Clone,Copy,Default)] +#[repr(C, packed)] +pub struct FramebufferTag { + pub typ: u32, + pub size: u32, + pub addr: u64, + pub pitch: u32, + pub width: u32, + pub height: u32, + pub bpp: u8, + pub fb_type: u8, + pub reserved: u16, +} + +#[repr(C, packed)] +pub struct AcpiRSDPTag { + pub typ: u32, + pub size: u32, + pub signature: [u8; 8], + pub checksum: u8, + pub oemid: [u8; 6], + pub revision: u8, + pub rsdt_addr: u32, +} + +#[repr(C, packed)] +pub struct AcpiSDTHeader { + pub signature: [u8; 4], //array + pub length: u32, + pub revision: u8, + pub checksum: u8, + pub oemid: [u8; 6], + pub oem_table_id: [u8; 8], + pub oem_revision: u32, + pub creator_id: u32, + pub creatpr_revision: u32, +} + +#[repr(C, packed)] +pub struct UsableMemory { + start: u64, + end: u64, + count: u8, + +} + diff --git a/src/init/multiboot_parser.rs b/src/init/multiboot_parser.rs deleted file mode 100644 index 79a0c81..0000000 --- a/src/init/multiboot_parser.rs +++ /dev/null @@ -1,371 +0,0 @@ -pub const MULTIBOOT_HEADER: u32 = 1; - -pub const MULTIBOOT_SEARCH: u32 = 32768; - -pub const MULTIBOOT2_HEADER_MAGIC: u32 = 0xe85250d6; -pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; - -pub const MULTIBOOT_MOD_ALIGN: u32 = 0x00001000; -pub const MULTIBOOT_INFO_ALIGN: u32 = 0x00000008; -pub const MULTIBOOT_TAG_ALIGN: u32 = 8; -pub const MULTIBOOT_HEADER_ALIGN: u8 = 8; - -#[derive(Copy,Clone)] -pub enum MULTIBOOT_TAG_TYPE { - MULTIBOOT_TAG_TYPE_END = 0, - MULTIBOOT_TAG_TYPE_CMDLINE = 1, - MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2, - MULTIBOOT_TAG_TYPE_MODULE = 3, - MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4, - MULTIBOOT_TAG_TYPE_BOOTDEV = 5, - MULTIBOOT_TAG_TYPE_MMAP = 6, - MULTIBOOT_TAG_TYPE_VBE = 7, - MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8, - MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9, - MULTIBOOT_TAG_TYPE_APM = 10, - MULTIBOOT_TAG_TYPE_EFI32 = 11, - MULTIBOOT_TAG_TYPE_EFI64 = 12, - MULTIBOOT_TAG_TYPE_SMBIOS = 13, - MULTIBOOT_TAG_TYPE_ACPI_OLD = 14, - MULTIBOOT_TAG_TYPE_ACPI_NEW = 15, - MULTIBOOT_TAG_TYPE_NETWORK = 16, - MULTIBOOT_TAG_TYPE_EFI_MMAP = 17, - MULTIBOOT_TAG_TYPE_EFI_BS = 18, - MULTIBOOT_TAG_TYPE_EFI32_IH = 19, - MULTIBOOT_TAG_TYPE_EFI64_IH = 20, - MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21, -} - -pub enum MULTIBOOT_HEADER_TAG { - MULTIBOOT_HEADER_TAG_END = 0, - MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST = 1, - MULTIBOOT_HEADER_TAG_ADDRESS = 2, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3, - MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4, - MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5, - MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6, - MULTIBOOT_HEADER_TAG_EFI_BS = 7, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 = 8, - MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 = 9, - MULTIBOOT_HEADER_TAG_RELOCATABLE = 10, -} - -pub enum MULTIBOOT_MEMORY { - MULTIBOOT_MEMORY_AVAILABLE = 1, - MULTIBOOT_MEMORY_RESERVED = 2, - MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3, - MULTIBOOT_MEMORY_NVS = 4, - MULTIBOOT_MEMORY_BADRAM = 5, -} - -pub const MULTIBOOT_ARCHITECTURE_I386: u8 = 0; -pub const MULTIBOOT_ARCHITECTURE_MIPS32: u8 = 4; -pub const MULTIBOOT_HEADER_TAG_OPTIONAL: u8 = 1; - -pub enum MULTIBOOT_LOAD_PREFERENCE { - MULTIBOOT_LOAD_PREFERENCE_NONE = 0, - MULTIBOOT_LOAD_PREFERENCE_LOW = 1, - MULTIBOOT_LOAD_PREFERENCE_HIGH = 2, -} - -const MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED: u8 = 1; -const MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED: u8 = 2; - - -use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ - buffer::Color, write::{newline, number, string} -} }; -use super::{result::InitResult}; - -pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult { - unsafe { - debug!("Multiboot2 pointer: "); - debugn!(multiboot_ptr); - debugln!(""); - - if parse_multiboot2_info((multiboot_ptr as u32) as usize, fb_tag) > 0 { - return InitResult::Passed; - } - } - - debug!("Multiboot2 pointer: "); - debugn!(multiboot_ptr); - debugln!(""); - - InitResult::Failed -} - -#[repr(C)] -pub struct TagHeader { - pub typ: MULTIBOOT_TAG_TYPE, - pub size: u32, -} - -#[repr(C)] -#[derive(Debug)] -pub struct MemoryMapTag { - typ: u32, - size: u32, - entry_size: u32, - entry_version: u32, - -} - -#[repr(C, packed)] -#[derive(Debug)] -struct MemoryMapEntry { - base_addr: u64, - length: u64, - typ: u32, - reserved: u32, -} - -#[derive(Clone,Copy,Default)] -#[repr(C, packed)] -pub struct FramebufferTag { - pub typ: u32, - pub size: u32, - pub addr: u64, - pub pitch: u32, - pub width: u32, - pub height: u32, - pub bpp: u8, - pub fb_type: u8, - pub reserved: u16, -} - -#[repr(C, packed)] -pub struct AcpiRSDPTag { - pub typ: u32, - pub size: u32, - pub signature: [u8; 8], - pub checksum: u8, - pub oemid: [u8; 6], - pub revision: u8, - pub rsdt_addr: u32, -} - -#[repr(C, packed)] -pub struct AcpiSDTHeader { - pub signature: [u8; 4], //array - pub length: u32, - pub revision: u8, - pub checksum: u8, - pub oemid: [u8; 6], - pub oem_table_id: [u8; 8], - pub oem_revision: u32, - pub creator_id: u32, - pub creatpr_revision: u32, -} - -#[repr(C, packed)] //directive?? status kinda idfk -pub struct UsableMemory { - start: u64, - end: u64, - count: u8, - -} - - - - -static mut U_MEM: UsableMemory = UsableMemory{start: 0, end: 0, count: 0}; //change this accordingly!!! placeholder for now - -//&&&&&&& reference variable borrower cannot change -//usize like size_t from C -//main parser -pub unsafe fn parse_multiboot2_info(base_addr: usize, mut fb_tag: &FramebufferTag) -> usize { - // Ensure alignment (Multiboot2 requires 8-byte aligned structure) - let addr = align_up(base_addr, 8); - - // First 4 bytes: total size of the multiboot info - let total_size = *(addr as *const u32) as usize; - - let mut ptr = addr + 8; - let end = addr + total_size; - - let mut tag_count = 0; - - - - while ptr < end { - let tag = &*(ptr as *const TagHeader); - if tag.size < 8 || tag.size > 4096 { - debugln!("Invalid tag size: abort"); - break; - } - - match tag.typ { - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_END => { - debugln!("End tag found"); - break; - } - - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_CMDLINE => { - - debug!("Boot command line tag: "); - - let str_ptr = ptr + 8; - let str_len = tag.size as usize - 8; - let raw_bytes = core::slice::from_raw_parts(str_ptr as *const u8, str_len); - - let cmdline = core::str::from_utf8_unchecked(raw_bytes); - debugln!(cmdline); - } - - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_MODULE => { - debug!("Module tag found: "); - - //let start = *((ptr + 8) as *const u32); - //let end = *((ptr + 12) as *const u32); - let str_ptr = ptr + 16; - let str_len = tag.size as usize - 16; - let raw_bytes = core::slice::from_raw_parts(str_ptr as *const u8, str_len); - - let cmdline = core::str::from_utf8_unchecked(raw_bytes); - debugln!(cmdline); - } - - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_MMAP => { - //ptr as *const foo ; immutable raw pointer - let mmap_tag = &*(ptr as *mut MemoryMapTag); - memory_map_tag(mmap_tag); - } - - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_FRAMEBUFFER => { - debugln!("Framebuffer tag: "); - - fb_tag = &*(ptr as *const FramebufferTag); - - debug!("Framebuffer address: "); - debugn!(fb_tag.addr as u64); - debugln!(""); - - debug!("(bpp + res): "); - debugn!(fb_tag.bpp as u64); - debug!(" + "); - debugn!(fb_tag.width as u64); - debug!("x"); - debugn!(fb_tag.height as u64); - debugln!(""); - - debug!("Pitch: "); - debugn!(fb_tag.pitch); - debugln!(""); - - - use core::ptr; - use x86_64::registers::control::Cr3; - - unsafe { - if fb_tag.addr == 0xb8000 { - ptr += align_up(tag.size as usize, 8); - continue; - } - - rprint!("Mapping framebuffer\n"); - let virt_base = 0xffff_8000_0000_0000u64 + fb_tag.addr as u64; - - //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, 0xffff_8000_0000_0000 + fb_tag.addr as u64); - //crate::mem::pmm::map_framebuffer(fb_tag.addr as u64, virt_base); - crate::mem::pmm::map_framebuffer(0xfd00_0000, 0xffff_8000_fd00_0000); - - let fb_ptr = 0xffff_8000_fd00_0000 as *mut u64; - - *fb_ptr = 0xFFFFFFFF; - - draw_rect(fb_ptr, 150, 150, 100, 100, 4096, 0x00ffffff); - draw_rect(fb_ptr, 250, 250, 100, 100, 4096, 0x00ff0000); - draw_rect(fb_ptr, 350, 350, 100, 100, 4096, 0x0000ff00); - draw_rect(fb_ptr, 450, 450, 100, 100, 4096, 0x000000ff); - - if let Some(font) = parse_psf(super::font::PSF_FONT) { - draw_text_psf("[guest@rou2ex:/] > ", &font, 25, 30, 0x0000ff00, fb_ptr, fb_tag.pitch as usize, fb_tag.bpp as usize); - draw_text_psf("[guest@rou2ex:/] > ", &font, 25, 50, 0x00ffd700, fb_ptr, fb_tag.pitch as usize, fb_tag.bpp as usize); - - //draw_char("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 35, 35, fb_ptr, fb_tag.pitch as usize, 0xdeadbeef, FONT_RAW); - } - - //draw_test_char(35, 35, fb_ptr); - //draw_text_psf("ABCDEFGHIJKLMNOPQRSTUVWXYZ",&FONT_RAW, 35, 35, 0x00ff00, fb_ptr, fb_tag.pitch, fb_tag.bpp); - } - - //dump_debug_log_to_file(); - - } - - MULTIBOOT_TAG_TYPE::MULTIBOOT_TAG_TYPE_ACPI_OLD => { - debugln!("ACPI v1 Root System Descriptor Pointer tag"); - - let acpi_tag = &*(ptr as *const AcpiRSDPTag); - debug!("Signature: "); - debug!(acpi_tag.signature); - debug!("\nOEM: "); - debug!(acpi_tag.oemid); - debugln!(""); - - let acpi_sdt = &*(acpi_tag.rsdt_addr as *const AcpiSDTHeader); - } - - _ => { - debug!("Unknown tag: "); - debugn!(tag.typ); - debugln!(""); - } - } - - ptr += align_up(tag.size as usize, 8); - - tag_count += 1; - if tag_count > 64 { - debugln!("Too many tags, aborting"); - break; - } - } - - tag_count -} - -pub unsafe fn memory_map_tag(mut mmap_tag: &MemoryMapTag) { - - debugln!("Memory map tag"); - let tag_size = mmap_tag.size as u8; - let entry_size = mmap_tag.entry_size as usize; - let mut entries_start = *(&mut mmap_tag as *mut _ as *mut u8); //wrong size here - - let entry: MemoryMapEntry = MemoryMapEntry {base_addr: 0, length: 0, typ: 0, reserved: 0}; - - - debugln!("Tag size"); - debugn!(tag_size); - debugln!("\nEntry size"); - debugn!(entry_size); - - - - -} - - - - -pub unsafe fn boot_line_tag() { - -} - - -fn align_up(x: usize, align: usize) -> usize { - (x + align - 1) & !(align - 1) -} - -pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { - for y in y0..(y0 + h) { - for x in x0..(x0 + w) { - let offset = y * (pitch / 4) + x; - - ptr.add(offset).write_volatile(color as u64); - } - } -} - - diff --git a/src/main.rs b/src/main.rs index cd98c33..957aaa5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,9 +34,9 @@ mod tui; mod vga; /// Kernel entrypoint -#[unsafe(no_mangle)] +#[unsafe(no_mangle)]//attribute pub extern "C" fn kernel_main(multiboot2_magic: u32, multiboot_ptr: u32) { - debugln!("Kernel loaded"); + //debugln!("Kernel loaded"); // VGA buffer position (LEGACY) clear_screen!(); @@ -45,12 +45,15 @@ pub extern "C" fn kernel_main(multiboot2_magic: u32, multiboot_ptr: u32) { video::vga::init_writer(); // Run init checks - init::init(multiboot_ptr as u64); + init::init(multiboot_ptr as *mut usize, multiboot2_magic as u32); + + //commented out for now // Run the shell loop debugln!("Starting shell..."); println!("Starting shell...\n"); input::keyboard::keyboard_loop(); + } // From 4796102bb1a22f1573b2b94c5e6063f1b8d3d589 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Sun, 31 Aug 2025 06:33:16 +0300 Subject: [PATCH 17/23] merged with main --- src/init/font.rs | 4 ++-- src/init/main.rs | 2 +- src/init/mod.rs | 20 ++++++++++---------- src/init/video.rs | 3 ++- src/main.rs | 2 +- src/video/mode.rs | 4 ++-- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/init/font.rs b/src/init/font.rs index a203943..bc26f2e 100644 --- a/src/init/font.rs +++ b/src/init/font.rs @@ -1,4 +1,4 @@ -gipub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u64, pitch: usize, fg: u32, font: &[u8]) { +pub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u64, pitch: usize, fg: u32, font: &[u8]) { let char_size = font[3] as usize; //let glyph = &font[4 + (c as usize * char_size)..]; @@ -11,7 +11,7 @@ gipub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u64, pitch: usize, fg: u3 let offset = py * pitch + px * 4; unsafe { let pixel_ptr = fb.add(offset); - *pixel_ptr = fg; + //*pixel_ptr = fg; let pixel_ptr = fb.add(offset) as *mut u64; *pixel_ptr = fg as u64; } diff --git a/src/init/main.rs b/src/init/main.rs index 8b3ed10..ec91925 100644 --- a/src/init/main.rs +++ b/src/init/main.rs @@ -5,7 +5,7 @@ use spin::Mutex; const BUFFER_SIZE: usize = 1024; static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -static mut FRAMEBUFFER: Option = None; +static mut FRAMEBUFFER: Option = None; pub fn init(m2_ptr: *mut usize, m2_magic: u32) { unsafe { diff --git a/src/init/mod.rs b/src/init/mod.rs index a736292..1d9d530 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -15,15 +15,15 @@ use spin::Mutex; const BUFFER_SIZE: usize = 1024; static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -static mut FRAMEBUFFER: Option = None; +//static mut FRAMEBUFFER: Option = None; pub fn init(multiboot_ptr: u64) { debugln!("Kernel init start"); - let framebuffer_tag: boot::FramebufferTag = boot::FramebufferTag{ + /*let framebuffer_tag: boot::FramebufferTag = boot::FramebufferTag{ ..Default::default() }; - + */ result::print_result( "Load kernel", result::InitResult::Passed, @@ -44,17 +44,17 @@ pub fn init(multiboot_ptr: u64) { heap::print_result(), ); - result::print_result( + /*result::print_result( "Read Multiboot2 tags", boot::print_info(multiboot_ptr, &framebuffer_tag), - ); + ); */ - let video_result = video::print_result(&framebuffer_tag); + //let video_result = video::print_result(&framebuffer_tag); - result::print_result( + /*result::print_result( "Initialize video", video_result, - ); + ); */ result::print_result( "Start PIC timer", @@ -67,10 +67,10 @@ pub fn init(multiboot_ptr: u64) { ); // TODO: Fallback to floppy to dump debug logs + init buffer - if video_result == result::InitResult::Passed { + /*if video_result == result::InitResult::Passed { INIT_BUFFER.lock().flush(); } - + */ color::color_demo(); ascii::ascii_art(); diff --git a/src/init/video.rs b/src/init/video.rs index 6fc8ca5..454991a 100644 --- a/src/init/video.rs +++ b/src/init/video.rs @@ -49,7 +49,7 @@ pub fn map_framebuffer( } } -pub fn print_result(fb: &super::multiboot_parser::FramebufferTag) -> super::result::InitResult { +/*pub fn print_result(fb: &super::multiboot_parser::FramebufferTag) -> super::result::InitResult { use crate::video; video::mode::init_video(fb); @@ -60,3 +60,4 @@ pub fn print_result(fb: &super::multiboot_parser::FramebufferTag) -> super::resu super::result::InitResult::Failed } +*/ \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index dc3046c..b91c41d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ pub extern "C" fn kernel_main(_multiboot2_magic: u32, multiboot_ptr: u32) { video::vga::init_writer(); // Run init checks - init::init(multiboot_ptr as *mut usize, multiboot2_magic as u32); + //init::init(multiboot_ptr as *mut usize, multiboot2_magic as u32); //commented out for now diff --git a/src/video/mode.rs b/src/video/mode.rs index 7fce2d5..bd960f1 100644 --- a/src/video/mode.rs +++ b/src/video/mode.rs @@ -12,7 +12,7 @@ pub enum VideoMode { static mut VIDEO_MODE: Option = Some(VideoMode::TextMode); -pub fn init_video(fb: &crate::init::multiboot_parser::FramebufferTag) { +/*pub fn init_video(fb: &crate::init::multiboot_parser::FramebufferTag) { unsafe { VIDEO_MODE = Some(VideoMode::Framebuffer { address: fb.addr as *mut u8, @@ -23,7 +23,7 @@ pub fn init_video(fb: &crate::init::multiboot_parser::FramebufferTag) { }); } } - +*/ pub fn get_video_mode() -> Option { unsafe { VIDEO_MODE From 0181fe82e4192454a7a7c970a39b1bb0f586f33f Mon Sep 17 00:00:00 2001 From: mikikichi Date: Mon, 1 Sep 2025 09:22:18 +0300 Subject: [PATCH 18/23] huge init system refactor wip --- src/init/check.rs | 13 +++ src/init/{video.rs => framebuffermap.rs} | 0 src/init/macros/placeholder.rs | 0 src/init/main.rs | 119 ----------------------- src/init/mod.rs | 116 +--------------------- src/init/multiboot2/mod.rs | 3 +- src/init/multiboot2/parser.rs | 1 + src/init/pit.rs | 2 +- src/init/result.rs | 47 --------- src/main.rs | 15 +-- src/video/{ => macros}/macros.rs | 83 +++++++++++++++- src/video/macros/result.rs | 6 ++ tempstash.txt | 22 +++++ 13 files changed, 134 insertions(+), 293 deletions(-) create mode 100644 src/init/check.rs rename src/init/{video.rs => framebuffermap.rs} (100%) create mode 100644 src/init/macros/placeholder.rs delete mode 100644 src/init/main.rs delete mode 100644 src/init/result.rs rename src/video/{ => macros}/macros.rs (60%) create mode 100644 src/video/macros/result.rs create mode 100644 tempstash.txt diff --git a/src/init/check.rs b/src/init/check.rs new file mode 100644 index 0000000..94012cb --- /dev/null +++ b/src/init/check.rs @@ -0,0 +1,13 @@ +use crate::init::multiboot2::{tags, parser}; +use crate::init::{cpu, idt, heap, video, pit, fs}; +use spin::Mutex; + + +pub fn init(m2_ptr: *mut usize, m2_magic: u32) { + debugln!("Kernel init start"); + unsafe { + parser::parse_multiboot2_info(m2_ptr, m2_magic); + } + + +} diff --git a/src/init/video.rs b/src/init/framebuffermap.rs similarity index 100% rename from src/init/video.rs rename to src/init/framebuffermap.rs diff --git a/src/init/macros/placeholder.rs b/src/init/macros/placeholder.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/init/main.rs b/src/init/main.rs deleted file mode 100644 index ec91925..0000000 --- a/src/init/main.rs +++ /dev/null @@ -1,119 +0,0 @@ -use crate::init::multiboot2; -use super::{cpu,result,idt,heap,video,pit,fs}; -use spin::Mutex; - -const BUFFER_SIZE: usize = 1024; - -static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -static mut FRAMEBUFFER: Option = None; - -pub fn init(m2_ptr: *mut usize, m2_magic: u32) { - unsafe { - multiboot2::parser::parse_multiboot2_info(m2_ptr, m2_magic); - } - debugln!("Kernel init start"); - - /*let mut framebuffer_tag: multiboot_parser::FramebufferTag = multiboot_parser::FramebufferTag{ - ..Default::default() - }; */ - - result::print_result( - "Load kernel", - result::InitResult::Passed, - ); - - result::print_result( - "Check 64-bit Long Mode", - cpu::check_mode(), - ); - - result::print_result( - "Reload IDT, GDT and TSS", - idt::get_result() - ); - - result::print_result( - "Initialize heap allocator", - heap::print_result(), - ); - - /*result::print_result( - "Read Multiboot2 tags", - multiboot_parser::print_info(m2_ptr, &mut framebuffer_tag), - ); - */ - let video_result = video::print_result(&framebuffer_tag); - - result::print_result( - "Initialize video", - video_result, - ); - - result::print_result( - "Start PIC timer", - pit::get_result(), - ); - - result::print_result( - "Check floppy drive", - fs::check_floppy(), - ); - - // TODO: Fallback to floppy to dump debug logs + init buffer - if video_result == result::InitResult::Passed { - INIT_BUFFER.lock().flush(); - } - /* - color::color_demo(); - ascii::ascii_art(); - */ - /*commented out cuz EAR PAINN - // Play startup melody - crate::audio::midi::play_melody(); - crate::audio::fs::play_midi_file(); - let freqs = [440, 880, 660, 550]; - for f in freqs { - crate::audio::beep::stop_beep(); - crate::audio::beep::beep(f); - crate::audio::midi::wait_millis(300); - } - crate::audio::beep::stop_beep(); - */ -} - -struct Buffer { - buf: [u8; 1024], - pos: usize, -} - -impl Buffer { - /// Creates and returns a new instance of Buffer. - const fn new() -> Self { - Self { - buf: [0u8; BUFFER_SIZE], - pos: 0, - } - } - - /// Adds given byte slice to the buffer at offset of self.pos. - fn append(&mut self, s: &[u8]) { - // Take the input length, or the offset - let len = s.len().min(self.buf.len() - self.pos); - - if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { - if let Some(slice) = s.get(..len) { - // Copy the slice into buffer at offset of self.pos - buf.copy_from_slice(slice); - self.pos += len; - } - } - } - - /// Puts the contents of buf into the printb! macro. - fn flush(&self) { - if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf); - } - } -} - diff --git a/src/init/mod.rs b/src/init/mod.rs index 1d9d530..afd01f5 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -7,118 +7,6 @@ pub mod fs; pub mod idt; pub mod heap; pub mod pit; -pub mod result; pub mod video; - -use spin::Mutex; - -const BUFFER_SIZE: usize = 1024; - -static INIT_BUFFER: Mutex = Mutex::new(Buffer::new()); -//static mut FRAMEBUFFER: Option = None; - -pub fn init(multiboot_ptr: u64) { - debugln!("Kernel init start"); - - /*let framebuffer_tag: boot::FramebufferTag = boot::FramebufferTag{ - ..Default::default() - }; - */ - result::print_result( - "Load kernel", - result::InitResult::Passed, - ); - - result::print_result( - "Check 64-bit Long Mode", - cpu::check_mode(), - ); - - result::print_result( - "Reload IDT, GDT and TSS", - idt::get_result() - ); - - result::print_result( - "Initialize heap allocator", - heap::print_result(), - ); - - /*result::print_result( - "Read Multiboot2 tags", - boot::print_info(multiboot_ptr, &framebuffer_tag), - ); */ - - //let video_result = video::print_result(&framebuffer_tag); - - /*result::print_result( - "Initialize video", - video_result, - ); */ - - result::print_result( - "Start PIC timer", - pit::get_result(), - ); - - result::print_result( - "Check floppy drive", - fs::check_floppy(), - ); - - // TODO: Fallback to floppy to dump debug logs + init buffer - /*if video_result == result::InitResult::Passed { - INIT_BUFFER.lock().flush(); - } - */ - color::color_demo(); - ascii::ascii_art(); - - // Play startup melody - //crate::audio::midi::play_melody(); - //crate::audio::fs::play_midi_file(); - let freqs = [440, 880, 660, 550]; - for f in freqs { - crate::audio::beep::stop_beep(); - crate::audio::beep::beep(f); - crate::audio::midi::wait_millis(300); - } - crate::audio::beep::stop_beep(); -} - -struct Buffer { - buf: [u8; 1024], - pos: usize, -} - -impl Buffer { - /// Creates and returns a new instance of Buffer. - const fn new() -> Self { - Self { - buf: [0u8; BUFFER_SIZE], - pos: 0, - } - } - - /// Adds given byte slice to the buffer at offset of self.pos. - fn append(&mut self, s: &[u8]) { - // Take the input length, or the offset - let len = s.len().min(self.buf.len() - self.pos); - - if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { - if let Some(slice) = s.get(..len) { - // Copy the slice into buffer at offset of self.pos - buf.copy_from_slice(slice); - self.pos += len; - } - } - } - - /// Puts the contents of buf into the printb! macro. - fn flush(&self) { - if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf); - } - } -} - +mod multiboot2; +pub mod check; diff --git a/src/init/multiboot2/mod.rs b/src/init/multiboot2/mod.rs index 11c7fd2..c322733 100644 --- a/src/init/multiboot2/mod.rs +++ b/src/init/multiboot2/mod.rs @@ -1,3 +1,4 @@ pub mod header; pub mod tags; -pub mod parser; \ No newline at end of file +pub mod parser; +pub mod info; diff --git a/src/init/multiboot2/parser.rs b/src/init/multiboot2/parser.rs index 05938cf..50cfc97 100644 --- a/src/init/multiboot2/parser.rs +++ b/src/init/multiboot2/parser.rs @@ -1,4 +1,5 @@ use super::{m2_header,tags}; +pub mod init; /*use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ buffer::Color, write::{newline, number, string} diff --git a/src/init/pit.rs b/src/init/pit.rs index ebfc9a3..76d6027 100644 --- a/src/init/pit.rs +++ b/src/init/pit.rs @@ -74,7 +74,7 @@ pub unsafe fn io_wait() { write(0x80, 0); } -pub fn get_result() -> super::result::InitResult { +pub fn get_result() -> super::result::InitResult { //why??? why do this why not just ret value or something?? debugln!("Remapping PIC"); unsafe { remap_pic(); } diff --git a/src/init/result.rs b/src/init/result.rs deleted file mode 100644 index d42f44d..0000000 --- a/src/init/result.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::{init::Buffer, video::vga::Color}; - -use super::INIT_BUFFER; - -#[derive(PartialEq, Copy, Clone)] -pub enum InitResult { - Unknown, - Passed, - Failed, - Skipped, -} - -impl InitResult { - pub fn format(&self) -> (&[u8; 6], Color) { - match self { - InitResult::Unknown => - (b"UNKNWN", Color::Cyan), - InitResult::Passed => - (b" OK ", Color::Green), - InitResult::Failed => - (b" FAIL ", Color::Red), - InitResult::Skipped => - (b" SKIP ", Color::Yellow), - } - } -} - -const MAX_MSG_LEN: usize = 60; - -pub fn print_result(message: &'static str, result: InitResult) { - let mut buf = Buffer::new(); - - buf.append(message.as_bytes()); - - for _ in 0..MAX_MSG_LEN - message.len() { - buf.append(b"."); - } - - buf.append(b" ["); - buf.append(result.format().0); - buf.append(b"]\n"); - - if let Some(slice) = buf.buf.get(..buf.pos) { - // - INIT_BUFFER.lock().append(slice); - } -} diff --git a/src/main.rs b/src/main.rs index b91c41d..115db1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,21 +24,16 @@ mod time; mod tui; mod vga; + /// Kernel entrypoint #[unsafe(no_mangle)] -pub extern "C" fn kernel_main(_multiboot2_magic: u32, multiboot_ptr: u32) { +pub extern "C" fn kernel_main(multiboot2_magic: u32, multiboot_ptr: u32) { debugln!("Kernel loaded"); - // VGA buffer position (LEGACY) - clear_screen!(); - - // TODO: REmove: Instantiate new VGA Writer - video::vga::init_writer(); - - // Run init checks - //init::init(multiboot_ptr as *mut usize, multiboot2_magic as u32); + // make it so this init function initializes everything so noo init messes + init::check::init(multiboot_ptr as *mut usize, multiboot2_magic as u32); + //this is so stupid OMG - //commented out for now // Run the shell loop debugln!("Starting shell..."); diff --git a/src/video/macros.rs b/src/video/macros/macros.rs similarity index 60% rename from src/video/macros.rs rename to src/video/macros/macros.rs index eae6450..b8c47b7 100644 --- a/src/video/macros.rs +++ b/src/video/macros/macros.rs @@ -49,7 +49,7 @@ macro_rules! printb { }; } -/// Special macro to print u64 numbers as a slice of u8 bytes. +/// Special macro to print u64 numbers as a slice of u8 bytes. why? #[macro_export] macro_rules! printn { ($arg:expr) => { @@ -111,3 +111,84 @@ macro_rules! print { }); } + +#[derive(PartialEq, Copy, Clone)] +pub enum InitResult { + Unknown, + Passed, + Failed, + Skipped, +} + +impl InitResult { + pub fn format(&self) -> (&[u8; 6], Color) { + match self { + InitResult::Unknown => + (b"UNKNWN", Color::Cyan), + InitResult::Passed => + (b" OK ", Color::Green), + InitResult::Failed => + (b" FAIL ", Color::Red), + InitResult::Skipped => + (b" SKIP ", Color::Yellow), + } + } +} + +const MAX_MSG_LEN: usize = 60; + +pub fn print_result(message: &'static str, result: InitResult) { + let mut buf = Buffer::new(); + + buf.append(message.as_bytes()); + + for _ in 0..MAX_MSG_LEN - message.len() { + buf.append(b"."); + } + + buf.append(b" ["); + buf.append(result.format().0); + buf.append(b"]\n"); + + if let Some(slice) = buf.buf.get(..buf.pos) { + // + INIT_BUFFER.lock().append(slice); + } +} + +struct Buffer { + buf: [u8; 1024], + pos: usize, +} + +impl Buffer { + /// Creates and returns a new instance of Buffer. why multiple instances?? + const fn new() -> Self { + Self { + buf: [0u8; BUFFER_SIZE], + pos: 0, + } + } + + /// Adds given byte slice to the buffer at offset of self.pos. + fn append(&mut self, s: &[u8]) { + // Take the input length, or the offset + let len = s.len().min(self.buf.len() - self.pos); + + if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { + if let Some(slice) = s.get(..len) { + // Copy the slice into buffer at offset of self.pos + buf.copy_from_slice(slice); + self.pos += len; + } + } + } + + /// Puts the contents of buf into the printb! macro. + fn flush(&self) { + if let Some(buf) = self.buf.get(..self.pos) { + printb!(buf); + } + } +} + diff --git a/src/video/macros/result.rs b/src/video/macros/result.rs new file mode 100644 index 0000000..9c9b4ee --- /dev/null +++ b/src/video/macros/result.rs @@ -0,0 +1,6 @@ + + +#[macro_export] +macro_rules! result { + +} \ No newline at end of file diff --git a/tempstash.txt b/tempstash.txt new file mode 100644 index 0000000..fd92e91 --- /dev/null +++ b/tempstash.txt @@ -0,0 +1,22 @@ +temp stash of comments or code + + // TODO: Fallback to floppy to dump debug logs + init buffer + /*if video_result == result::InitResult::Passed { + INIT_BUFFER.lock().flush(); + } */ + /* + color::color_demo(); + ascii::ascii_art(); + */ + /*commented out cuz EAR PAINN + // Play startup melody + crate::audio::midi::play_melody(); + crate::audio::fs::play_midi_file(); + let freqs = [440, 880, 660, 550]; + for f in freqs { + crate::audio::beep::stop_beep(); + crate::audio::beep::beep(f); + crate::audio::midi::wait_millis(300); + } + crate::audio::beep::stop_beep(); + */ \ No newline at end of file From 81ae4882dd70c5374b9a8751a03d42c24477432d Mon Sep 17 00:00:00 2001 From: mikikichi Date: Wed, 3 Sep 2025 20:43:46 +0300 Subject: [PATCH 19/23] wip print refactor and init refactor --- src/app/editor.rs | 3 +- src/app/snake/score.rs | 3 +- src/init/check.rs | 17 ++- src/init/cpu.rs | 4 +- src/init/font.rs | 4 +- src/init/fs.rs | 4 +- src/init/heap.rs | 6 +- src/init/idt.rs | 4 +- src/init/mod.rs | 1 - src/init/multiboot2/parser.rs | 26 ++--- src/init/multiboot2/tags.rs | 4 +- src/init/pit.rs | 4 +- src/main.rs | 1 - src/video/macros/macros.rs | 194 ---------------------------------- src/video/macros/mod.rs | 3 + src/video/macros/print.rs | 91 ++++++++++++++++ src/video/macros/result.rs | 6 -- src/video/macros/system.rs | 70 ++++++++++++ src/video/mod.rs | 2 + src/video/sysprints.rs | 74 +++++++++++++ src/video/vga.rs | 9 +- 21 files changed, 290 insertions(+), 240 deletions(-) delete mode 100644 src/video/macros/macros.rs create mode 100644 src/video/macros/mod.rs create mode 100644 src/video/macros/print.rs delete mode 100644 src/video/macros/result.rs create mode 100644 src/video/macros/system.rs create mode 100644 src/video/sysprints.rs diff --git a/src/app/editor.rs b/src/app/editor.rs index 84aae45..fe44030 100644 --- a/src/app/editor.rs +++ b/src/app/editor.rs @@ -1,4 +1,4 @@ -use crate::fs::fat12::{fs::Filesystem, block::Floppy}; +/*use crate::fs::fat12::{fs::Filesystem, block::Floppy}; use crate::init::config::PATH_CLUSTER; use crate::input::keyboard::{self, keyboard_read_scancode}; use crate::video::vga::Writer; @@ -403,3 +403,4 @@ pub fn split_filename(input: &[u8]) -> (&[u8], &[u8]) { } } +*/ \ No newline at end of file diff --git a/src/app/snake/score.rs b/src/app/snake/score.rs index ec581d7..31b6877 100644 --- a/src/app/snake/score.rs +++ b/src/app/snake/score.rs @@ -1,4 +1,4 @@ -use crate::{ +/*use crate::{ app::snake::menu::{draw_menu, draw_window}, fs::fat12::{ block::Floppy, fs::Filesystem}, init::config::PATH_CLUSTER, input::keyboard::keyboard_read_scancode @@ -202,3 +202,4 @@ pub fn sprintf_score<'a>(prefix: &'static [u8], buf: &'a mut [u8], score: u32) - unsafe { core::str::from_utf8_unchecked(&buf[..i]) } } +*/ \ No newline at end of file diff --git a/src/init/check.rs b/src/init/check.rs index 94012cb..88137fa 100644 --- a/src/init/check.rs +++ b/src/init/check.rs @@ -1,13 +1,22 @@ use crate::init::multiboot2::{tags, parser}; -use crate::init::{cpu, idt, heap, video, pit, fs}; +use crate::init::{cpu, idt, heap,pit, fs}; +use crate::video::{vga}; use spin::Mutex; +static const tempbuff = vga::SysBuffer::new(); + pub fn init(m2_ptr: *mut usize, m2_magic: u32) { debugln!("Kernel init start"); - unsafe { - parser::parse_multiboot2_info(m2_ptr, m2_magic); - } + //unsafe { + //parser::parse_multiboot2_info(m2_ptr, m2_magic); + //} + + + + + + } diff --git a/src/init/cpu.rs b/src/init/cpu.rs index a5cbd84..1adb096 100644 --- a/src/init/cpu.rs +++ b/src/init/cpu.rs @@ -1,5 +1,5 @@ use core::arch::asm; -use super::result; +/*use super::result; pub fn check_mode() -> crate::init::result::InitResult { let mode = check_cpu_mode(); @@ -12,7 +12,7 @@ pub fn check_mode() -> crate::init::result::InitResult { } result::InitResult::Failed -} +} */ fn enable_sse() { unsafe { diff --git a/src/init/font.rs b/src/init/font.rs index bc26f2e..06735ae 100644 --- a/src/init/font.rs +++ b/src/init/font.rs @@ -25,9 +25,9 @@ pub fn draw_char(c: u8, x: usize, y: usize, fb: *mut u64, pitch: usize, fg: u32, // // -pub fn print_result() -> super::result::InitResult { +/*pub fn print_result() -> super::result::InitResult { super::result::InitResult::Unknown -} +} */ // // diff --git a/src/init/fs.rs b/src/init/fs.rs index f327be3..d1a93fd 100644 --- a/src/init/fs.rs +++ b/src/init/fs.rs @@ -1,5 +1,5 @@ use crate::fs::fat12::{block::Floppy, fs::Filesystem}; -use super::{ +/*use super::{ config::{PATH_CLUSTER, set_path}, result, }; @@ -25,7 +25,7 @@ pub fn check_floppy() -> result::InitResult { } res -} +} */ /*pub fn print_info(vga_index: &mut isize) { let floppy = Floppy; diff --git a/src/init/heap.rs b/src/init/heap.rs index 6a6ac80..1519a14 100644 --- a/src/init/heap.rs +++ b/src/init/heap.rs @@ -1,9 +1,9 @@ use crate::mem::bump::{ALLOCATOR}; use core::ptr; -use super::result::InitResult; +//use super::result::InitResult; -pub fn print_result() -> InitResult { +/*pub fn print_result() -> InitResult { /*if !init_heap_allocator() { return InitResult::Failed; }*/ @@ -63,7 +63,7 @@ pub fn print_result() -> InitResult { } InitResult::Passed -} +} */ fn init_heap_allocator() -> bool { debugln!("Heap allocator init start"); diff --git a/src/init/idt.rs b/src/init/idt.rs index 2dc5a43..0defa47 100644 --- a/src/init/idt.rs +++ b/src/init/idt.rs @@ -1,6 +1,6 @@ use crate::abi::idt::{install_isrs, load_idt}; -pub fn get_result() -> super::result::InitResult { +/*pub fn get_result() -> super::result::InitResult { debugln!("Installing Exception handlers and ISRs"); install_isrs(); @@ -22,7 +22,7 @@ pub fn get_result() -> super::result::InitResult { debugln!("Done"); super::result::InitResult::Passed -} +} */ extern "C" { static mut tss64: Tss64; diff --git a/src/init/mod.rs b/src/init/mod.rs index afd01f5..cfc2d54 100644 --- a/src/init/mod.rs +++ b/src/init/mod.rs @@ -7,6 +7,5 @@ pub mod fs; pub mod idt; pub mod heap; pub mod pit; -pub mod video; mod multiboot2; pub mod check; diff --git a/src/init/multiboot2/parser.rs b/src/init/multiboot2/parser.rs index 50cfc97..7396c94 100644 --- a/src/init/multiboot2/parser.rs +++ b/src/init/multiboot2/parser.rs @@ -1,5 +1,5 @@ -use super::{m2_header,tags}; -pub mod init; + + /*use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ buffer::Color, write::{newline, number, string} @@ -43,17 +43,17 @@ use super::{result::InitResult}; */ //static mut U_MEM: UsableMemory = UsableMemory{start: 0, end: 0, count: 0}; //change this accordingly!!! placeholder for now - +/* pub unsafe fn parse_multiboot2_info(m2_ptr: *mut usize, m2_magic: u32) { //check magic, needs to match - if m2_magic != m2_header::MULTIBOOT2_BOOTLOADER_MAGIC { + if m2_magic != super::header::MULTIBOOT2_BOOTLOADER_MAGIC { return; //return 1 here??? }; //alignment to 8 let mut m2_tag = &*((m2_ptr as *mut u8).add(8) as *mut tags::TagHeader); - while m2_tag.typ != m2_header::M2TagType::End { + /*while m2_tag.typ != m2_header::M2TagType::End { match m2_tag.typ { @@ -91,7 +91,7 @@ pub unsafe fn parse_multiboot2_info(m2_ptr: *mut usize, m2_magic: u32) { } - +*/ pub unsafe fn memory_map_tag(mut mmap_tag: &tags::MemoryMapTag) { let tag_size = mmap_tag.size as usize; @@ -106,9 +106,9 @@ pub unsafe fn memory_map_tag(mut mmap_tag: &tags::MemoryMapTag) { } - +*/ //stashed code for now!!! - +/* pub unsafe fn acpi_old_tag() { /* @@ -214,15 +214,15 @@ pub unsafe fn framebuffer_tag() { //dump_debug_log_to_file(); */ } +*/ - - +/* fn align_up(x: usize, align: usize) -> usize { (x + align - 1) & !(align - 1) -} +} */ -pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { +/*pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, pitch: usize, color: u32) { for y in y0..(y0 + h) { for x in x0..(x0 + w) { let offset = y * (pitch / 4) + x; @@ -232,4 +232,4 @@ pub unsafe fn draw_rect(ptr: *mut u64, x0: usize, y0: usize, w: usize, h: usize, } } - +*/ diff --git a/src/init/multiboot2/tags.rs b/src/init/multiboot2/tags.rs index bb52b89..873a988 100644 --- a/src/init/multiboot2/tags.rs +++ b/src/init/multiboot2/tags.rs @@ -1,8 +1,8 @@ - +//pub mod header; #[repr(C)] #[derive(Copy,Clone)] pub struct TagHeader { - pub typ: header::M2TagType, + //pub typ: header::M2TagType, pub size: u32, } diff --git a/src/init/pit.rs b/src/init/pit.rs index 76d6027..0f9c9e5 100644 --- a/src/init/pit.rs +++ b/src/init/pit.rs @@ -74,7 +74,7 @@ pub unsafe fn io_wait() { write(0x80, 0); } -pub fn get_result() -> super::result::InitResult { //why??? why do this why not just ret value or something?? +/*pub fn get_result() -> super::result::InitResult { //why??? why do this why not just ret value or something?? debugln!("Remapping PIC"); unsafe { remap_pic(); } @@ -82,4 +82,4 @@ pub fn get_result() -> super::result::InitResult { //why??? why do this why not init_pit(1); // 100Hz -> 10ms per tick??? super::result::InitResult::Passed -} +}*/ diff --git a/src/main.rs b/src/main.rs index 115db1b..6f21043 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,6 @@ mod time; mod tui; mod vga; - /// Kernel entrypoint #[unsafe(no_mangle)] pub extern "C" fn kernel_main(multiboot2_magic: u32, multiboot_ptr: u32) { diff --git a/src/video/macros/macros.rs b/src/video/macros/macros.rs deleted file mode 100644 index b8c47b7..0000000 --- a/src/video/macros/macros.rs +++ /dev/null @@ -1,194 +0,0 @@ -// -// PRINT MACROS -// - -/// Macro to render all rows with the currently set ColorCode. -#[macro_export] -macro_rules! clear_screen { - () => { - if let Some(mut writer) = $crate::video::vga::get_writer() { - writer.clear_screen(); - } - }; -} - -/// Prints the error string to screen in red. -#[macro_export] -macro_rules! error { - () => { - $crate::print!("\n"); - }; - ($arg:expr $(,)?) => { - // Set yellow chars on black - $crate::print!($arg, $crate::video::vga::Color::Red, $crate::video::vga::Color::Black); - }; -} - -/// Prints the warning string to screen in yellow. -#[macro_export] -macro_rules! warn { - () => { - $crate::print!("\n"); - }; - ($arg:expr $(,)?) => { - // Set yellow chars on black - $crate::print!($arg, $crate::video::vga::Color::Yellow, $crate::video::vga::Color::Black); - }; -} - -/// This macro takes in a reference to byte slice (&[u8]) and prints all its contents to display. -#[macro_export] -macro_rules! printb { - ($arg:expr) => { - if let Some(mut writer) = $crate::video::vga::get_writer() { - //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); - for b in $arg { - writer.write_byte(*b); - } - } - }; -} - -/// Special macro to print u64 numbers as a slice of u8 bytes. why? -#[macro_export] -macro_rules! printn { - ($arg:expr) => { - // - let mut buf = [0u8; 20]; - let mut len = buf.len(); - - if $arg == 0 { - print!("0"); - //return - } - - let mut num = $arg; - - while num > 0 { - len -= 1; - if let Some(b) = buf.get_mut(len) { - *b = b'0' + (num % 10) as u8; - } - num /= 10; - } - - printb!(buf.get(len..).unwrap_or(&[])); - }; -} - -/// Meta macro to include the newline implicitly at the end of provided string. -#[macro_export] -macro_rules! println { - () => ($crate::print!("\n")); - ($arg:expr) => ({ - $crate::print!($arg); - $crate::print!("\n"); - }); -} - -/// The main string printing macro. Takes in 1-3 arguments. The first one is always the string to -/// print, and the latter ones are to change the foreground color (and background respectively) of -/// the characters printed to screen. -#[macro_export] -macro_rules! print { - ($arg:expr) => { - if let Some(mut writer) = $crate::video::vga::get_writer() { - //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); - writer.write_str_raw($arg); - } - }; - ($arg:expr, $fg:expr) => { - if let Some(mut writer) = $crate::video::vga::get_writer() { - writer.set_color_num($fg as u8, $crate::video::vga::Color::Black as u8); - writer.write_str_raw($arg); - } - }; - ($arg:expr, $fg:expr, $bg:expr) => ({ - if let Some(mut writer) = $crate::video::vga::get_writer() { - writer.set_color_num($fg as u8, $bg as u8); - writer.write_str_raw($arg); - } - }); -} - - -#[derive(PartialEq, Copy, Clone)] -pub enum InitResult { - Unknown, - Passed, - Failed, - Skipped, -} - -impl InitResult { - pub fn format(&self) -> (&[u8; 6], Color) { - match self { - InitResult::Unknown => - (b"UNKNWN", Color::Cyan), - InitResult::Passed => - (b" OK ", Color::Green), - InitResult::Failed => - (b" FAIL ", Color::Red), - InitResult::Skipped => - (b" SKIP ", Color::Yellow), - } - } -} - -const MAX_MSG_LEN: usize = 60; - -pub fn print_result(message: &'static str, result: InitResult) { - let mut buf = Buffer::new(); - - buf.append(message.as_bytes()); - - for _ in 0..MAX_MSG_LEN - message.len() { - buf.append(b"."); - } - - buf.append(b" ["); - buf.append(result.format().0); - buf.append(b"]\n"); - - if let Some(slice) = buf.buf.get(..buf.pos) { - // - INIT_BUFFER.lock().append(slice); - } -} - -struct Buffer { - buf: [u8; 1024], - pos: usize, -} - -impl Buffer { - /// Creates and returns a new instance of Buffer. why multiple instances?? - const fn new() -> Self { - Self { - buf: [0u8; BUFFER_SIZE], - pos: 0, - } - } - - /// Adds given byte slice to the buffer at offset of self.pos. - fn append(&mut self, s: &[u8]) { - // Take the input length, or the offset - let len = s.len().min(self.buf.len() - self.pos); - - if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { - if let Some(slice) = s.get(..len) { - // Copy the slice into buffer at offset of self.pos - buf.copy_from_slice(slice); - self.pos += len; - } - } - } - - /// Puts the contents of buf into the printb! macro. - fn flush(&self) { - if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf); - } - } -} - diff --git a/src/video/macros/mod.rs b/src/video/macros/mod.rs new file mode 100644 index 0000000..282f6b5 --- /dev/null +++ b/src/video/macros/mod.rs @@ -0,0 +1,3 @@ +#[macro_use] //not sure if needed? +pub mod print; +pub mod system; \ No newline at end of file diff --git a/src/video/macros/print.rs b/src/video/macros/print.rs new file mode 100644 index 0000000..6e3044e --- /dev/null +++ b/src/video/macros/print.rs @@ -0,0 +1,91 @@ +// +// PRINT MACROS +// + +/// Macro to render all rows with the currently set ColorCode. +#[macro_export] +macro_rules! clear_screen { + () => { + if let Some(mut writer) = $crate::video::vga::get_writer() { + writer.clear_screen(); + } + }; +} + + +/// This macro takes in a reference to byte slice (&[u8]) and prints all its contents to display. +#[macro_export] +macro_rules! printb { + ($arg:expr) => { + if let Some(mut writer) = $crate::video::vga::get_writer() { + //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); + for b in $arg { + writer.write_byte(*b); + } + } + }; +} + +/// Special macro to print u64 numbers as a slice of u8 bytes. why? +#[macro_export] +macro_rules! printn { + ($arg:expr) => { + // + let mut buf = [0u8; 20]; + let mut len = buf.len(); + + if $arg == 0 { + print!("0"); + //return + } + + let mut num = $arg; + + while num > 0 { + len -= 1; + if let Some(b) = buf.get_mut(len) { + *b = b'0' + (num % 10) as u8; + } + num /= 10; + } + + printb!(buf.get(len..).unwrap_or(&[])); + }; +} + +/// Meta macro to include the newline implicitly at the end of provided string. +#[macro_export] +macro_rules! println { + () => ($crate::print!("\n")); + ($arg:expr) => ({ + $crate::print!($arg); + $crate::print!("\n"); + }); +} + +/// The main string printing macro. Takes in 1-3 arguments. The first one is always the string to +/// print, and the latter ones are to change the foreground color (and background respectively) of +/// the characters printed to screen. +#[macro_export] +macro_rules! print { + ($arg:expr) => { + if let Some(mut writer) = $crate::video::vga::get_writer() { + //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); + writer.write_str_raw($arg); + } + }; + ($arg:expr, $fg:expr) => { + if let Some(mut writer) = $crate::video::vga::get_writer() { + writer.set_color_num($fg as u8, $crate::video::vga::Color::Black as u8); + writer.write_str_raw($arg); + } + }; + ($arg:expr, $fg:expr, $bg:expr) => ({ + if let Some(mut writer) = $crate::video::vga::get_writer() { + writer.set_color_num($fg as u8, $bg as u8); + writer.write_str_raw($arg); + } + }); +} + + diff --git a/src/video/macros/result.rs b/src/video/macros/result.rs deleted file mode 100644 index 9c9b4ee..0000000 --- a/src/video/macros/result.rs +++ /dev/null @@ -1,6 +0,0 @@ - - -#[macro_export] -macro_rules! result { - -} \ No newline at end of file diff --git a/src/video/macros/system.rs b/src/video/macros/system.rs new file mode 100644 index 0000000..bdfc51c --- /dev/null +++ b/src/video/macros/system.rs @@ -0,0 +1,70 @@ +use crate::video::{vga}; +//system warning macros etc +#[macro_export] +macro_rules! error { + () => { + $crate::print!("\n"); + }; + ($arg:expr $(,)?) => { + // Set yellow chars on black + $crate::print!($arg, $crate::video::vga::Color::Red, $crate::video::vga::Color::Black); + }; +} + +/// Prints the warning string to screen in yellow. +#[macro_export] +macro_rules! warn { + () => { + $crate::print!("\n"); + }; + ($arg:expr $(,)?) => { + // Set yellow chars on black + $crate::print!($arg, $crate::video::vga::Color::Yellow, $crate::video::vga::Color::Black); + }; +} + + +const MAX_MSG_LEN: usize = 60; + +pub fn print_result(message: &'static str, result: vga::Result) { + let mut buf = vga::SysBuffer::new(); //new buffer instance, make the init initialize this instead? + + buf.append(message.as_bytes()); //append as bytes + + for _ in 0..MAX_MSG_LEN - message.len() { + buf.append(b"."); //write, not going past max + } + + buf.append(b" ["); + buf.append(result.format().0); + buf.append(b"]\n"); + + //in range ... + if let Some(slice) = buf.buf.get(..buf.pos) { + // + INIT_BUFFER.lock().append(slice); //buffer lock? + } +} + + + +//arg1 is the message, arg2 is the status for it +#[macro_export] +macro_rules! result { + () => { + $crate::print!("\n"); + }; + ($str:expr, $res: expr) => { + let mut buf = vga::SysBuffer; + let mut len = str.len(); + buf.append(str); + for _ in 0 MAX_MSG_LEN - len { + buf.append(b"."); + } + buf.append(b" ["); + + + $crate::print!(str); + }; + +} \ No newline at end of file diff --git a/src/video/mod.rs b/src/video/mod.rs index d48a04b..24a1087 100644 --- a/src/video/mod.rs +++ b/src/video/mod.rs @@ -2,3 +2,5 @@ pub mod macros; pub mod mode; pub mod vga; + +pub mod sysprints; \ No newline at end of file diff --git a/src/video/sysprints.rs b/src/video/sysprints.rs new file mode 100644 index 0000000..7aa3f14 --- /dev/null +++ b/src/video/sysprints.rs @@ -0,0 +1,74 @@ +//system prints such as warnings etc + +use crate::{vga}; +pub struct SysBuffer { + buf: [u8; 1024], + pos: usize, +} +const MAX_MSG_LEN: usize = 60; + +impl SysBuffer { + //get buffer instance + pub const fn new() -> Self { + Self { + buf: [0u8; BUFFER_SIZE], //array of u8 of 1024 + pos: 0, + } + } + + pub fn format(&mut self, message: &'static str) { + let len = message.len(); + + //move -> then add [1234] + + +} + + /// Adds given byte slice to the buffer at offset of self.pos. + pub fn append(&mut self, s: &[u8]) { + // Take the input length, or the offset + //s.len gives length of whats to be written, min compares minimum of comparison of + //selfs buf len - position + let len = s.len().min(self.buf.len() - self.pos); + //get mut returns mutable reference of self pos + len + if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { + if let Some(slice) = s.get(..len) { + // Copy the slice into buffer at offset of self.pos + buf.copy_from_slice(slice); + self.pos += len; + } + } + } + + /// Puts the contents of buf into the printb! macro. + pub fn flush(&self) { + if let Some(buf) = self.buf.get(..self.pos) { + printb!(buf); + } + } +} + +#[derive(PartialEq, Copy, Clone)] //make this global?? could be reused for a bunch of things +pub enum Result { + Unknown, + Passed, + Failed, + Skipped, +} +const BUFFER_SIZE: usize = 1024; +//match +impl Result { + pub fn format(&self) -> (&[u8; 6], Color) { + match self { + Result::Unknown => + (b"UNKNWN", Color::Cyan), + Result::Passed => + (b" OK ", Color::Green), + Result::Failed => + (b" FAIL ", Color::Red), + Result::Skipped => + (b" SKIP ", Color::Yellow), + } + } +} +//make the sys print call this diff --git a/src/video/vga.rs b/src/video/vga.rs index 36d922c..865d3c9 100644 --- a/src/video/vga.rs +++ b/src/video/vga.rs @@ -4,12 +4,12 @@ use spin::{mutex::Mutex}; use core::sync::atomic::{AtomicBool, Ordering}; /// VGA text mode buffer dimensions. -const BUFFER_WIDTH: usize = 80; -const BUFFER_HEIGHT: usize = 25; -const BUFFER_ADDRESS: usize = 0xb8000; +pub const BUFFER_WIDTH: usize = 80; +pub const BUFFER_HEIGHT: usize = 25; +pub const BUFFER_ADDRESS: usize = 0xb8000; /// Wrapped Writer instance guarded by Mutex. -pub static mut WRITER: Option> = None; +pub static mut WRITER: Option> = None; //get rid of this static mut /// Helper static boolean to ensure that the global Writer instance is created just once. static WRITER_INIT: AtomicBool = AtomicBool::new(false); @@ -258,3 +258,4 @@ impl Writer { } } + From d01cd87c9a8b953f6ef3ab736071419389227f8e Mon Sep 17 00:00:00 2001 From: mikikichi Date: Wed, 10 Sep 2025 08:16:59 +0300 Subject: [PATCH 20/23] wip, printing result macro works --- Makefile | 2 +- src/app/editor.rs | 3 +- src/app/snake/score.rs | 3 +- src/init/check.rs | 6 ++- src/input/keyboard.rs | 3 +- src/video/{sysprints.rs => bufmg.rs} | 61 ++++++++++++---------------- src/video/macros/mod.rs | 1 + src/video/macros/print.rs | 2 +- src/video/macros/system.rs | 45 ++++++-------------- src/video/mod.rs | 3 +- src/video/sysprint.rs | 53 ++++++++++++++++++++++++ 11 files changed, 107 insertions(+), 75 deletions(-) rename src/video/{sysprints.rs => bufmg.rs} (56%) create mode 100644 src/video/sysprint.rs diff --git a/Makefile b/Makefile index e4a7754..86b0dfa 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ compile_kernel: @cp target/kernel_graphics/x86_64-r2/release/kernel.elf iso/boot/kernel_graphics.elf build_iso: - @grub-mkrescue \ + @grub2-mkrescue \ -o r2.iso iso/ \ --modules="multiboot2 video video_bochs video_cirrus gfxterm all_video" diff --git a/src/app/editor.rs b/src/app/editor.rs index fe44030..84aae45 100644 --- a/src/app/editor.rs +++ b/src/app/editor.rs @@ -1,4 +1,4 @@ -/*use crate::fs::fat12::{fs::Filesystem, block::Floppy}; +use crate::fs::fat12::{fs::Filesystem, block::Floppy}; use crate::init::config::PATH_CLUSTER; use crate::input::keyboard::{self, keyboard_read_scancode}; use crate::video::vga::Writer; @@ -403,4 +403,3 @@ pub fn split_filename(input: &[u8]) -> (&[u8], &[u8]) { } } -*/ \ No newline at end of file diff --git a/src/app/snake/score.rs b/src/app/snake/score.rs index 31b6877..ec581d7 100644 --- a/src/app/snake/score.rs +++ b/src/app/snake/score.rs @@ -1,4 +1,4 @@ -/*use crate::{ +use crate::{ app::snake::menu::{draw_menu, draw_window}, fs::fat12::{ block::Floppy, fs::Filesystem}, init::config::PATH_CLUSTER, input::keyboard::keyboard_read_scancode @@ -202,4 +202,3 @@ pub fn sprintf_score<'a>(prefix: &'static [u8], buf: &'a mut [u8], score: u32) - unsafe { core::str::from_utf8_unchecked(&buf[..i]) } } -*/ \ No newline at end of file diff --git a/src/init/check.rs b/src/init/check.rs index 88137fa..8e199a4 100644 --- a/src/init/check.rs +++ b/src/init/check.rs @@ -1,13 +1,17 @@ use crate::init::multiboot2::{tags, parser}; use crate::init::{cpu, idt, heap,pit, fs}; use crate::video::{vga}; +use crate::video::macros::{system}; use spin::Mutex; -static const tempbuff = vga::SysBuffer::new(); +//static tempbuff = vga::SysBuffer::new(); pub fn init(m2_ptr: *mut usize, m2_magic: u32) { debugln!("Kernel init start"); + vga::init_writer(); + result!("Test"); + //unsafe { //parser::parse_multiboot2_info(m2_ptr, m2_magic); //} diff --git a/src/input/keyboard.rs b/src/input/keyboard.rs index 872ef95..e647603 100644 --- a/src/input/keyboard.rs +++ b/src/input/keyboard.rs @@ -3,7 +3,8 @@ use crate::init::config::PATH_CLUSTER; use crate::input::cmd; use crate::input::port; use crate::init::config::{HOST, USER, get_path}; -use crate::video::{self, vga}; +use crate::video::{self, vga, sysprint}; +use crate::video::macros::{print, system}; /// The macimum size of an input to the shell console. const INPUT_BUFFER_SIZE: usize = 128; diff --git a/src/video/sysprints.rs b/src/video/bufmg.rs similarity index 56% rename from src/video/sysprints.rs rename to src/video/bufmg.rs index 7aa3f14..aaa2d77 100644 --- a/src/video/sysprints.rs +++ b/src/video/bufmg.rs @@ -1,13 +1,31 @@ -//system prints such as warnings etc +//generic buffer reusables -use crate::{vga}; -pub struct SysBuffer { - buf: [u8; 1024], - pos: usize, +const BUFFER_SIZE: usize = 1024; + + +pub struct Buffer { + pub buf: [u8; 1024], + pub pos: usize, } const MAX_MSG_LEN: usize = 60; -impl SysBuffer { +/*pub fn placeholder() { + //key created + if let Some(mut key) = sysprint::SysBuffer.trylock() { + key.pos; + + + + } + + + + +}; */ + + + +impl Buffer { //get buffer instance pub const fn new() -> Self { Self { @@ -24,11 +42,11 @@ impl SysBuffer { } - /// Adds given byte slice to the buffer at offset of self.pos. pub fn append(&mut self, s: &[u8]) { - // Take the input length, or the offset + // input length or offset //s.len gives length of whats to be written, min compares minimum of comparison of //selfs buf len - position + //make self buf len maybe a const? let len = s.len().min(self.buf.len() - self.pos); //get mut returns mutable reference of self pos + len if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { @@ -46,29 +64,4 @@ impl SysBuffer { printb!(buf); } } -} - -#[derive(PartialEq, Copy, Clone)] //make this global?? could be reused for a bunch of things -pub enum Result { - Unknown, - Passed, - Failed, - Skipped, -} -const BUFFER_SIZE: usize = 1024; -//match -impl Result { - pub fn format(&self) -> (&[u8; 6], Color) { - match self { - Result::Unknown => - (b"UNKNWN", Color::Cyan), - Result::Passed => - (b" OK ", Color::Green), - Result::Failed => - (b" FAIL ", Color::Red), - Result::Skipped => - (b" SKIP ", Color::Yellow), - } - } -} -//make the sys print call this +} \ No newline at end of file diff --git a/src/video/macros/mod.rs b/src/video/macros/mod.rs index 282f6b5..ddbcdde 100644 --- a/src/video/macros/mod.rs +++ b/src/video/macros/mod.rs @@ -1,3 +1,4 @@ #[macro_use] //not sure if needed? pub mod print; +#[macro_use] pub mod system; \ No newline at end of file diff --git a/src/video/macros/print.rs b/src/video/macros/print.rs index 6e3044e..4cc883c 100644 --- a/src/video/macros/print.rs +++ b/src/video/macros/print.rs @@ -1,7 +1,7 @@ // // PRINT MACROS // - +//generic print macros /// Macro to render all rows with the currently set ColorCode. #[macro_export] macro_rules! clear_screen { diff --git a/src/video/macros/system.rs b/src/video/macros/system.rs index bdfc51c..78149b5 100644 --- a/src/video/macros/system.rs +++ b/src/video/macros/system.rs @@ -1,4 +1,4 @@ -use crate::video::{vga}; +use crate::video::{vga, sysprint, bufmg}; //system warning macros etc #[macro_export] macro_rules! error { @@ -24,47 +24,28 @@ macro_rules! warn { } -const MAX_MSG_LEN: usize = 60; - -pub fn print_result(message: &'static str, result: vga::Result) { - let mut buf = vga::SysBuffer::new(); //new buffer instance, make the init initialize this instead? - - buf.append(message.as_bytes()); //append as bytes - - for _ in 0..MAX_MSG_LEN - message.len() { - buf.append(b"."); //write, not going past max - } - - buf.append(b" ["); - buf.append(result.format().0); - buf.append(b"]\n"); - - //in range ... - if let Some(slice) = buf.buf.get(..buf.pos) { - // - INIT_BUFFER.lock().append(slice); //buffer lock? - } -} - +//result printing macro //arg1 is the message, arg2 is the status for it #[macro_export] macro_rules! result { () => { $crate::print!("\n"); - }; - ($str:expr, $res: expr) => { - let mut buf = vga::SysBuffer; - let mut len = str.len(); - buf.append(str); - for _ in 0 MAX_MSG_LEN - len { - buf.append(b"."); + }; + //could be problematic? + ($arg:expr) => { + //key created + if let Some(mut instance) = $crate::video::sysprint::SysBuffer.try_lock() { + instance.append($arg.as_bytes()); + instance.flush(); + //make this arg to a temporary value + } - buf.append(b" ["); - $crate::print!(str); + + }; } \ No newline at end of file diff --git a/src/video/mod.rs b/src/video/mod.rs index 24a1087..fa226b9 100644 --- a/src/video/mod.rs +++ b/src/video/mod.rs @@ -3,4 +3,5 @@ pub mod macros; pub mod mode; pub mod vga; -pub mod sysprints; \ No newline at end of file +pub mod sysprint; +pub mod bufmg; \ No newline at end of file diff --git a/src/video/sysprint.rs b/src/video/sysprint.rs new file mode 100644 index 0000000..220c605 --- /dev/null +++ b/src/video/sysprint.rs @@ -0,0 +1,53 @@ +//system prints such as warnings etc +use crate::video::{vga, bufmg}; +use spin::Mutex; + +pub static SysBuffer: Mutex = Mutex::new(bufmg::Buffer::new()); + +const MAX_MSG_LEN: usize = 60; + +/*pub fn format_result(message: &'static str, result: sysprint::Result) { + let mut buf = bufmg::Buffer::new(); //new buffer instance, make the init initialize this instead? + + buf.append(message.as_bytes()); //append as bytes + + for _ in 0..MAX_MSG_LEN - message.len() { + buf.append(b"."); //write, not going past max + } + + buf.append(b" ["); + buf.append(result.format().0); + buf.append(b"]\n"); + + //in range ... + if let Some(slice) = buf.buf.get(..buf.pos) { + // + INIT_BUFFER.lock().append(slice); //buffer lock? + } +}*/ + + +#[derive(PartialEq, Copy, Clone)] //make this global?? could be reused for a bunch of things +pub enum Result { + Unknown, + Passed, + Failed, + Skipped, +} + +//match +impl Result { + pub fn format(&self) -> (&[u8; 6], vga::Color) { + match self { + Result::Unknown => + (b"UNKNWN", vga::Color::Cyan), + Result::Passed => + (b" OK ", vga::Color::Green), + Result::Failed => + (b" FAIL ", vga::Color::Red), + Result::Skipped => + (b" SKIP ", vga::Color::Yellow), + } + } +} +//make the sys print call this From 03ebb6a697e236cca2361f80710a2a2b8fd1e422 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Sat, 13 Sep 2025 14:59:36 +0300 Subject: [PATCH 21/23] almost done wip --- src/init/check.rs | 11 ++-- src/main.rs | 2 - src/video/bufmg.rs | 66 -------------------- src/video/macros/print.rs | 4 +- src/video/macros/system.rs | 13 +--- src/video/sysprint.rs | 125 ++++++++++++++++++++++++++++--------- 6 files changed, 106 insertions(+), 115 deletions(-) diff --git a/src/init/check.rs b/src/init/check.rs index 8e199a4..c866a81 100644 --- a/src/init/check.rs +++ b/src/init/check.rs @@ -1,8 +1,7 @@ use crate::init::multiboot2::{tags, parser}; use crate::init::{cpu, idt, heap,pit, fs}; -use crate::video::{vga}; -use crate::video::macros::{system}; -use spin::Mutex; +use crate::video::{vga, sysprint::Result as res}; + //static tempbuff = vga::SysBuffer::new(); @@ -10,7 +9,11 @@ use spin::Mutex; pub fn init(m2_ptr: *mut usize, m2_magic: u32) { debugln!("Kernel init start"); vga::init_writer(); - result!("Test"); + clear_screen!(); + result!("First", res::Unknown); + + + //unsafe { //parser::parse_multiboot2_info(m2_ptr, m2_magic); diff --git a/src/main.rs b/src/main.rs index 6f21043..70ca73a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,8 +35,6 @@ pub extern "C" fn kernel_main(multiboot2_magic: u32, multiboot_ptr: u32) { // Run the shell loop - debugln!("Starting shell..."); - println!("Starting shell...\n"); input::keyboard::keyboard_loop(); } diff --git a/src/video/bufmg.rs b/src/video/bufmg.rs index aaa2d77..8b13789 100644 --- a/src/video/bufmg.rs +++ b/src/video/bufmg.rs @@ -1,67 +1 @@ -//generic buffer reusables -const BUFFER_SIZE: usize = 1024; - - -pub struct Buffer { - pub buf: [u8; 1024], - pub pos: usize, -} -const MAX_MSG_LEN: usize = 60; - -/*pub fn placeholder() { - //key created - if let Some(mut key) = sysprint::SysBuffer.trylock() { - key.pos; - - - - } - - - - -}; */ - - - -impl Buffer { - //get buffer instance - pub const fn new() -> Self { - Self { - buf: [0u8; BUFFER_SIZE], //array of u8 of 1024 - pos: 0, - } - } - - pub fn format(&mut self, message: &'static str) { - let len = message.len(); - - //move -> then add [1234] - - -} - - pub fn append(&mut self, s: &[u8]) { - // input length or offset - //s.len gives length of whats to be written, min compares minimum of comparison of - //selfs buf len - position - //make self buf len maybe a const? - let len = s.len().min(self.buf.len() - self.pos); - //get mut returns mutable reference of self pos + len - if let Some(buf) = self.buf.get_mut(self.pos..self.pos + len) { - if let Some(slice) = s.get(..len) { - // Copy the slice into buffer at offset of self.pos - buf.copy_from_slice(slice); - self.pos += len; - } - } - } - - /// Puts the contents of buf into the printb! macro. - pub fn flush(&self) { - if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf); - } - } -} \ No newline at end of file diff --git a/src/video/macros/print.rs b/src/video/macros/print.rs index 4cc883c..dc09b8d 100644 --- a/src/video/macros/print.rs +++ b/src/video/macros/print.rs @@ -3,6 +3,7 @@ // //generic print macros /// Macro to render all rows with the currently set ColorCode. +use crate::video::{vga}; #[macro_export] macro_rules! clear_screen { () => { @@ -16,9 +17,10 @@ macro_rules! clear_screen { /// This macro takes in a reference to byte slice (&[u8]) and prints all its contents to display. #[macro_export] macro_rules! printb { - ($arg:expr) => { + ($arg:expr $(,$col: ident)?) => { if let Some(mut writer) = $crate::video::vga::get_writer() { //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); + $(writer.set_color($crate::video::vga::Color::$col, $crate::video::vga::Color::Black);)? for b in $arg { writer.write_byte(*b); } diff --git a/src/video/macros/system.rs b/src/video/macros/system.rs index 78149b5..11a0275 100644 --- a/src/video/macros/system.rs +++ b/src/video/macros/system.rs @@ -30,22 +30,13 @@ macro_rules! warn { //arg1 is the message, arg2 is the status for it #[macro_export] macro_rules! result { - () => { - $crate::print!("\n"); - }; - //could be problematic? - ($arg:expr) => { + ($msg:expr, $res: expr) => { //key created if let Some(mut instance) = $crate::video::sysprint::SysBuffer.try_lock() { - instance.append($arg.as_bytes()); - instance.flush(); - //make this arg to a temporary value + instance.format($msg, $res); } - - - }; } \ No newline at end of file diff --git a/src/video/sysprint.rs b/src/video/sysprint.rs index 220c605..ea2367b 100644 --- a/src/video/sysprint.rs +++ b/src/video/sysprint.rs @@ -1,33 +1,12 @@ //system prints such as warnings etc -use crate::video::{vga, bufmg}; +use crate::video::{vga, vga::Color as Color}; use spin::Mutex; -pub static SysBuffer: Mutex = Mutex::new(bufmg::Buffer::new()); +pub static SysBuffer: Mutex = Mutex::new(Buffer::new()); const MAX_MSG_LEN: usize = 60; -/*pub fn format_result(message: &'static str, result: sysprint::Result) { - let mut buf = bufmg::Buffer::new(); //new buffer instance, make the init initialize this instead? - - buf.append(message.as_bytes()); //append as bytes - - for _ in 0..MAX_MSG_LEN - message.len() { - buf.append(b"."); //write, not going past max - } - - buf.append(b" ["); - buf.append(result.format().0); - buf.append(b"]\n"); - - //in range ... - if let Some(slice) = buf.buf.get(..buf.pos) { - // - INIT_BUFFER.lock().append(slice); //buffer lock? - } -}*/ - - -#[derive(PartialEq, Copy, Clone)] //make this global?? could be reused for a bunch of things +#[derive(PartialEq, Copy, Clone)] pub enum Result { Unknown, Passed, @@ -35,19 +14,103 @@ pub enum Result { Skipped, } -//match +//here for future proofing, if needed to call from another caller outside of sys buffer impl Result { - pub fn format(&self) -> (&[u8; 6], vga::Color) { + pub fn format(&self) -> (&[u8; 6], Color) { match self { Result::Unknown => - (b"UNKNWN", vga::Color::Cyan), + (b"UNKNWN", Color::Cyan), Result::Passed => - (b" OK ", vga::Color::Green), + (b" OK ", Color::Green), Result::Failed => - (b" FAIL ", vga::Color::Red), + (b" FAIL ", Color::Red), Result::Skipped => - (b" SKIP ", vga::Color::Yellow), + (b" SKIP ", Color::Yellow), } } } -//make the sys print call this + +const BUFFER_SIZE: usize = 160; + + +pub struct Buffer { + pub buf: [u8; 160], + pub pos: usize, +} + + + + +impl Buffer { + //get buffer instance + pub const fn new() -> Self { + Self { + buf: [0u8; BUFFER_SIZE], + pos: 0, + } + + } + + + + + pub fn format(&mut self, msg: &'static str, res: Result) { + if msg.len() <= MAX_MSG_LEN { + + self.append(msg.as_bytes()); + + while self.pos <= vga::BUFFER_WIDTH - 9 { + self.append(b"."); + + } + self.append(b"[ ]"); //make this aligned with a for loop or while + self.pos -= 7; + self.flush(None); + self.append(res.format().0); + + self.flush(Some(res.format().1)); + + + } + } + + pub fn append(&mut self, s: &[u8]) { + + + let len = s.len().min(BUFFER_SIZE - self.pos); + + self.buf[self.pos..self.pos + len].copy_from_slice(&s); + + self.pos += len; + + + } + + + /// Puts the contents of buf into the printb! macro. + pub fn flush(&mut self, c: Option) { + match c { + Some(Color::Cyan) => { + if let Some(buf) = self.buf.get(..self.pos) { + printb!(buf, Cyan); + self.pos = 0; + + } + } + + None => { + if let Some(buf) = self.buf.get(..self.pos) { + printb!(buf); + self.pos = 0; + } + + } + + _ => () + } + + } + +} + + From 583ffa22c91d540b25bf20d9ec2d9b3c79c140e8 Mon Sep 17 00:00:00 2001 From: mikikichi Date: Wed, 17 Sep 2025 05:02:28 +0300 Subject: [PATCH 22/23] result macro functional --- src/init/check.rs | 6 +++--- src/video/macros/print.rs | 25 ++++++++++++++++++++--- src/video/macros/system.rs | 4 ++-- src/video/sysprint.rs | 41 ++++++++++++++++++++++++++++---------- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/init/check.rs b/src/init/check.rs index c866a81..9f82bfe 100644 --- a/src/init/check.rs +++ b/src/init/check.rs @@ -4,14 +4,14 @@ use crate::video::{vga, sysprint::Result as res}; -//static tempbuff = vga::SysBuffer::new(); pub fn init(m2_ptr: *mut usize, m2_magic: u32) { - debugln!("Kernel init start"); vga::init_writer(); clear_screen!(); result!("First", res::Unknown); - + result!("Second", res::Passed); + result!("Third", res::Failed); + result!("Fourth", res::Skipped); diff --git a/src/video/macros/print.rs b/src/video/macros/print.rs index dc09b8d..02970e5 100644 --- a/src/video/macros/print.rs +++ b/src/video/macros/print.rs @@ -17,16 +17,35 @@ macro_rules! clear_screen { /// This macro takes in a reference to byte slice (&[u8]) and prints all its contents to display. #[macro_export] macro_rules! printb { - ($arg:expr $(,$col: ident)?) => { + ($arg:expr) => { if let Some(mut writer) = $crate::video::vga::get_writer() { - //writer.set_color($crate::vga::writer::Color::White, $crate::vga::writer::Color::Black); - $(writer.set_color($crate::video::vga::Color::$col, $crate::video::vga::Color::Black);)? for b in $arg { writer.write_byte(*b); } + } + }; } +//Same as the macro above, except takes in color then resets +#[macro_export] +macro_rules! printb_color { + + ($arg:expr, $col: ident) => { + if let Some(mut writer) = $crate::video::vga::get_writer() { + writer.set_color($crate::video::vga::Color::$col, $crate::video::vga::Color::Black); + for b in $arg { + writer.write_byte(*b); + } + writer.set_color($crate::video::vga::Color::White, $crate::video::vga::Color::Black); //resets color + } + + } + + +} + + /// Special macro to print u64 numbers as a slice of u8 bytes. why? #[macro_export] diff --git a/src/video/macros/system.rs b/src/video/macros/system.rs index 11a0275..8a4a560 100644 --- a/src/video/macros/system.rs +++ b/src/video/macros/system.rs @@ -32,10 +32,10 @@ macro_rules! warn { macro_rules! result { ($msg:expr, $res: expr) => { //key created - if let Some(mut instance) = $crate::video::sysprint::SysBuffer.try_lock() { + if let Some(mut instance) = $crate::video::sysprint::SYSBUFFER.try_lock() { instance.format($msg, $res); - } + }; }; diff --git a/src/video/sysprint.rs b/src/video/sysprint.rs index ea2367b..0f471b0 100644 --- a/src/video/sysprint.rs +++ b/src/video/sysprint.rs @@ -1,8 +1,8 @@ -//system prints such as warnings etc +//System prints such as warnings etc use crate::video::{vga, vga::Color as Color}; use spin::Mutex; -pub static SysBuffer: Mutex = Mutex::new(Buffer::new()); +pub static SYSBUFFER: Mutex = Mutex::new(Buffer::new()); const MAX_MSG_LEN: usize = 60; @@ -14,7 +14,7 @@ pub enum Result { Skipped, } -//here for future proofing, if needed to call from another caller outside of sys buffer +//Here for future proofing, if needed to call from another caller outside of sysbuffer impl Result { pub fn format(&self) -> (&[u8; 6], Color) { match self { @@ -53,23 +53,25 @@ impl Buffer { - +//TODO: add safety check so self.pos doesnt go out of bounds pub fn format(&mut self, msg: &'static str, res: Result) { if msg.len() <= MAX_MSG_LEN { - self.append(msg.as_bytes()); - + //9 is size of the brackets while self.pos <= vga::BUFFER_WIDTH - 9 { self.append(b"."); } - self.append(b"[ ]"); //make this aligned with a for loop or while - self.pos -= 7; + self.append(b"["); self.flush(None); + self.append(res.format().0); self.flush(Some(res.format().1)); + self.append(b"]"); + self.flush(None); + } } @@ -87,23 +89,40 @@ impl Buffer { } - /// Puts the contents of buf into the printb! macro. + /// Puts the contents of buf into the printb! or printb_color! macro. pub fn flush(&mut self, c: Option) { match c { Some(Color::Cyan) => { if let Some(buf) = self.buf.get(..self.pos) { - printb!(buf, Cyan); + printb_color!(buf, Cyan); + self.pos = 0; + } + } + Some(Color::Green) => { + if let Some(buf) = self.buf.get(..self.pos) { + printb_color!(buf, Green); self.pos = 0; } } + Some(Color::Red) => { + if let Some(buf) = self.buf.get(..self.pos) { + printb_color!(buf, Red); + self.pos = 0; + } + } + Some(Color::Yellow) => { + if let Some(buf) = self.buf.get(..self.pos) { + printb_color!(buf, Yellow); + self.pos = 0; + } + } None => { if let Some(buf) = self.buf.get(..self.pos) { printb!(buf); self.pos = 0; } - } _ => () From 0d18ac93fef343a5444b525d47ec1c51a76d42aa Mon Sep 17 00:00:00 2001 From: mikikichi Date: Thu, 2 Oct 2025 03:49:54 +0300 Subject: [PATCH 23/23] wip --- src/debug.rs | 4 +- src/init/check.rs | 15 ++--- src/init/multiboot2/mod.rs | 1 + src/init/multiboot2/parser.rs | 111 ++++++++++++++++++++++------------ src/init/multiboot2/tags.rs | 12 ++-- src/video/sysprint.rs | 4 +- 6 files changed, 89 insertions(+), 58 deletions(-) diff --git a/src/debug.rs b/src/debug.rs index c875b21..8e41856 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,6 +1,6 @@ use core::fmt::{self, Write}; use spin::Mutex; -use crate::{clear_screen, error, printb}; +use crate::{clear_screen, error, printb, println}; const DEBUG_LOG_SIZE: usize = 8192; @@ -74,7 +74,7 @@ macro_rules! debugn { let mut buf = [0u8; 20]; let s = $crate::debug::u64_to_dec_str($n as u64, &mut buf); log.append(s); - } + } }}; } diff --git a/src/init/check.rs b/src/init/check.rs index 9f82bfe..e3333e5 100644 --- a/src/init/check.rs +++ b/src/init/check.rs @@ -1,3 +1,4 @@ +use crate::debug::dump_debug_log_to_file; use crate::init::multiboot2::{tags, parser}; use crate::init::{cpu, idt, heap,pit, fs}; use crate::video::{vga, sysprint::Result as res}; @@ -8,17 +9,11 @@ use crate::video::{vga, sysprint::Result as res}; pub fn init(m2_ptr: *mut usize, m2_magic: u32) { vga::init_writer(); clear_screen!(); - result!("First", res::Unknown); - result!("Second", res::Passed); - result!("Third", res::Failed); - result!("Fourth", res::Skipped); - - - - //unsafe { - //parser::parse_multiboot2_info(m2_ptr, m2_magic); - //} + unsafe { + parser::parse_multiboot2_info(m2_ptr, m2_magic); + } + dump_debug_log_to_file(); diff --git a/src/init/multiboot2/mod.rs b/src/init/multiboot2/mod.rs index c322733..608d501 100644 --- a/src/init/multiboot2/mod.rs +++ b/src/init/multiboot2/mod.rs @@ -2,3 +2,4 @@ pub mod header; pub mod tags; pub mod parser; pub mod info; + diff --git a/src/init/multiboot2/parser.rs b/src/init/multiboot2/parser.rs index 7396c94..d4c152e 100644 --- a/src/init/multiboot2/parser.rs +++ b/src/init/multiboot2/parser.rs @@ -1,12 +1,11 @@ - - /*use crate::{debug::dump_debug_log_to_file, init::{config::{p1_fb_table, p1_fb_table_2, p2_fb_table, p3_fb_table, p4_table}, font::{draw_text_psf, parse_psf}}, mem, vga::{ buffer::Color, write::{newline, number, string} -} }; -use super::{result::InitResult}; */ - -/*pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult { +} };*/ +use crate::init::multiboot2::{header,tags, header::M2TagType as TagType, tags::BasicTag as BasicTag, tags::MemoryMapTag as MMapTag}; +use crate::{debug}; +/* +pub fn print_info(multiboot_ptr: u64, mut fb_tag: &FramebufferTag) -> InitResult { unsafe { debug!("Multiboot2 pointer: "); debugn!(multiboot_ptr); @@ -23,10 +22,10 @@ use super::{result::InitResult}; */ InitResult::Failed } -*/ -/* let addr = align_up(base_addr, 8); + + let addr = align_up(base_addr, 8); // First 4 bytes: total size of the multiboot info let total_size = *(addr as *const u32) as usize; @@ -36,77 +35,111 @@ use super::{result::InitResult}; */ let mut tag_count = 0; -*/ +*/ //static mut U_MEM: UsableMemory = UsableMemory{start: 0, end: 0, count: 0}; //change this accordingly!!! placeholder for now -/* + pub unsafe fn parse_multiboot2_info(m2_ptr: *mut usize, m2_magic: u32) { - //check magic, needs to match - if m2_magic != super::header::MULTIBOOT2_BOOTLOADER_MAGIC { - return; //return 1 here??? + + if m2_magic != header::MULTIBOOT2_BOOTLOADER_MAGIC { + return; //return sysfail here }; //alignment to 8 - let mut m2_tag = &*((m2_ptr as *mut u8).add(8) as *mut tags::TagHeader); + //is the & not needed here? + let mut m2_tag = m2_ptr.add(8) as *mut BasicTag; + while (*m2_tag).typ != TagType::End { - /*while m2_tag.typ != m2_header::M2TagType::End { + match (*m2_tag).typ { - match m2_tag.typ { + TagType::CmdLine => { + debugn!((*m2_tag).typ); + debug!("Cmd"); - m2_header::M2TagType::CmdLine => { - break; } - m2_header::M2TagType::Module => { - break; + TagType::Module => { + debugn!((*m2_tag).typ); + debugln!("Module"); + } - m2_header::M2TagType::Mmap => { - let mmap_tag = &*(m2_tag as *const _ as *const tags::MemoryMapTag); - memory_map_tag(mmap_tag); + TagType::Mmap => { + debugn!((*m2_tag).typ); + let mmap_tag = m2_tag as *mut MMapTag; + memory_map_tag(mmap_tag); + debugln!("MMap"); + } - m2_header::M2TagType::Framebuffer => { - break; + TagType::Framebuffer => { + debugn!((*m2_tag).typ); + debugln!("Frame"); } - m2_header::M2TagType::AcpiOLD => { - break; + TagType::AcpiOLD => { + debugn!((*m2_tag).typ); + debugln!("acpi"); + } _ => { - break; + debugn!((*m2_tag).typ); + debugln!("Empty"); + } + } + //Could be cleaned up + //m2_tag = (((m2_tag as usize) + ((*m2_tag).size as usize) + 7) & !(7)) as *mut BasicTag; + m2_tag = (((m2_tag as *mut u8).add((*m2_tag).size as usize + 7)) as usize & !(7)) as *mut BasicTag; - m2_tag = &*((align_up(m2_tag.size as usize, 8) as *mut tags::TagHeader)); + } - } + } -} -*/ -pub unsafe fn memory_map_tag(mut mmap_tag: &tags::MemoryMapTag) { - let tag_size = mmap_tag.size as usize; - let entry_size = mmap_tag.entry_size as usize; //incrementation - let mut entries_start = mmap_tag as *const _ as *mut u8; - let mut tag_end = entries_start.add(tag_size); - +pub unsafe fn memory_map_tag(mmap_tag: *mut MMapTag) { + debugln!("Tag start"); + debugn!(mmap_tag as usize); + debugln!("Entry initial"); + let mut entries = &mut (*mmap_tag).entries as *mut tags::MemoryMapEntry; + debugn!(entries); + let end = (mmap_tag as *mut u8).add((*mmap_tag).size as usize) as *mut tags::MemoryMapEntry; + debugln!("End"); + debugn!(end); + let mut i = 0; + while entries < end { + entries = ((entries as *mut u8).add((*mmap_tag).entry_size as usize)) as *mut tags::MemoryMapEntry; + i+=1; + } + debugln!("Ran"); + debugn!(i); + debugln!("Tag size"); + debugn!((*mmap_tag).size as usize); + + debugln!("Tag entry sizes"); + debugn!((*mmap_tag).entry_size as u8); + + debugln!("Last entry"); + debugn!(entries); + + } -*/ + //stashed code for now!!! /* diff --git a/src/init/multiboot2/tags.rs b/src/init/multiboot2/tags.rs index 873a988..df1b1ad 100644 --- a/src/init/multiboot2/tags.rs +++ b/src/init/multiboot2/tags.rs @@ -1,8 +1,9 @@ -//pub mod header; +use crate::init::multiboot2::{header}; + #[repr(C)] #[derive(Copy,Clone)] -pub struct TagHeader { - //pub typ: header::M2TagType, +pub struct BasicTag { + pub typ: header::M2TagType, pub size: u32, } @@ -18,7 +19,7 @@ pub struct MemoryMapTag { #[repr(C, packed)] -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct MemoryMapEntry { pub base_addr: u64, pub length: u64, @@ -53,7 +54,7 @@ pub struct AcpiRSDPTag { #[repr(C, packed)] pub struct AcpiSDTHeader { - pub signature: [u8; 4], //array + pub signature: [u8; 4], pub length: u32, pub revision: u8, pub checksum: u8, @@ -71,4 +72,3 @@ pub struct UsableMemory { count: u8, } - diff --git a/src/video/sysprint.rs b/src/video/sysprint.rs index 0f471b0..24c8fa6 100644 --- a/src/video/sysprint.rs +++ b/src/video/sysprint.rs @@ -57,11 +57,13 @@ impl Buffer { pub fn format(&mut self, msg: &'static str, res: Result) { if msg.len() <= MAX_MSG_LEN { self.append(msg.as_bytes()); - //9 is size of the brackets + //9 is size of the brackets could be added as a more readable constant while self.pos <= vga::BUFFER_WIDTH - 9 { self.append(b"."); } + //Color characters must be appended and flushed immediately. For now theres no function + //to keep track of color, may be added in the future. self.append(b"["); self.flush(None);