From 8704b5d2491113288dca23536ff556fa5743027f Mon Sep 17 00:00:00 2001 From: zxq5 Date: Tue, 4 Mar 2025 12:44:42 +0000 Subject: [PATCH] FIXED THE APIC HELL YEAH --- .idea/FoundryOS.iml | 1 + .idea/codeStyles/codeStyleConfig.xml | 5 ++ kernel/src/arch/x86_64/cpu/apic.rs | 40 ++++++--------- kernel/src/lib.rs | 13 ++--- libm/src/lib.rs | 77 +++++++++++----------------- 5 files changed, 59 insertions(+), 77 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/FoundryOS.iml b/.idea/FoundryOS.iml index 4c628e5..e1befda 100644 --- a/.idea/FoundryOS.iml +++ b/.idea/FoundryOS.iml @@ -6,6 +6,7 @@ + diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/kernel/src/arch/x86_64/cpu/apic.rs b/kernel/src/arch/x86_64/cpu/apic.rs index 25ad2e0..3672f38 100644 --- a/kernel/src/arch/x86_64/cpu/apic.rs +++ b/kernel/src/arch/x86_64/cpu/apic.rs @@ -1,7 +1,7 @@ use core::arch::x86_64::__cpuid; use crate::arch::x86_64::memory::mapping::PHYSICAL_MEMORY_OFFSET; -use crate::serial_print; +use crate::{serial_print, serial_println}; use x86_64::{ PhysAddr, VirtAddr, structures::paging::{Mapper, Page, PageTableFlags, PhysFrame, Size4KiB}, @@ -40,19 +40,21 @@ fn get_apic_base() -> PhysAddr { PhysAddr::new(value & 0xfffff0000) } -fn write_apic_register(apic_base: &VirtAddr, reg: u8, value: u32) { +fn write_apic_register(apic_base: &PhysAddr, reg: u8, value: u32) { let apic_base = apic_base.as_u64(); let reg_addr = (apic_base & 0xFFFFF0000) + reg as u64; - unsafe { *(reg_addr as *mut u32) = value }; + let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) }; + unsafe { *(virt_addr.as_u64() as *mut u32) = value }; } -fn read_apic_register(apic_base: &VirtAddr, reg: u8) -> u32 { +fn read_apic_register(apic_base: &PhysAddr, reg: u8) -> u32 { let apic_base = apic_base.as_u64(); - - serial_print!("got apic base"); - let reg_addr = (apic_base & 0xFFFFF0000) + reg as u64; - unsafe { *(reg_addr as *const u32) } + + serial_println!("reading: {:?}", VirtAddr::new(reg_addr)); + + let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) }; + unsafe { *(virt_addr.as_u64() as *const u32) } } pub fn check_apic() -> bool { @@ -74,27 +76,15 @@ pub fn enable_apic() { let apic_phys_addr = get_apic_base(); set_apic_base_enable(apic_phys_addr); // map virt address of apic - let apic_virt = unsafe { phys_to_virt(apic_phys_addr) }; - let page: Page = Page::containing_address(apic_virt); - let frame: PhysFrame = PhysFrame::containing_address(apic_phys_addr); - let flags: PageTableFlags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE; - unsafe { - match mapper.map_to(page, frame, flags, &mut *frame_allocator) { - Ok(_) => {} - Err(why) => panic!("failed to map apic: {:?}", why), - } - } + serial_println!("Trying to map: {:?} -> {:?}", apic_virt, apic_phys_addr); - // // FIXME: this causes a page fault - // // TODO: map to virtual memory + // FIXME: this causes a page fault + // TODO: map to virtual memory - let reg = read_apic_register(&apic_virt, 0xF0); - - serial_print!("ok2"); - - write_apic_register(&apic_virt, 0xF0, reg | 0x100); + let reg = read_apic_register(&apic_phys_addr, 0xF0); + write_apic_register(&apic_phys_addr, 0xF0, reg | 0x100); } pub struct Apic {} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 20340e1..1a692bc 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -22,6 +22,7 @@ use core::arch::asm; use limine::BaseRevision; use x86_64::VirtAddr; use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator; +use crate::arch::x86_64::cpu::apic::enable_apic; pub mod arch; pub mod resources; @@ -99,17 +100,17 @@ pub fn boot() -> Result<(), &'static str> { } println_log!("[Success]"); - print_log!(" Enabling PICs... "); - interrupts::enable_pic(); - println_log!("[Success]"); + // print_log!(" Enabling PICs... "); + // interrupts::enable_pic(); + // println_log!("[Success]"); // print_log!(" Disabling PICs... "); // interrupts::disable_pic(); // println_log!("[Success]"); - // print_log!(" Initialising APIC"); - // enable_apic(); - // println_log!("[Success]"); + print_log!(" Initialising APIC... "); + enable_apic(); + println_log!("[Success]"); print_log!(" Enabling Interrupts... "); x86_64::instructions::interrupts::enable(); diff --git a/libm/src/lib.rs b/libm/src/lib.rs index f6de287..7480206 100644 --- a/libm/src/lib.rs +++ b/libm/src/lib.rs @@ -40,13 +40,13 @@ pub fn include_font(item: TokenStream) -> TokenStream { filename.value() ); - let font_bytes = match load_file(file_path) { + let font_bytes = match std::fs::read(file_path) { Ok(bytes) => bytes, Err(why) => panic!("{}", why), }; - let font_data = match Font::new(font_bytes) { - Ok(font) => font.0, + let font_data = match FontBuilder::load(font_bytes) { + Ok(font) => font.data, Err(why) => panic!("{}", why), }; @@ -66,40 +66,39 @@ struct FontData { width: u8, height: u8, length: u16, - data: Vec, - unicode_table: [[u8; 2]; 512], + pub data: [[u8; 16]; 512], } - -struct Glyph { - bytes: Vec, -} - enum FontBuilder { - Psf1, - Psf2, + Psf1(FontData), + Psf2(FontData), } impl FontBuilder { const PSF1_MAGIC: u16 = 0x3604; const PSF2_MAGIC: u32 = 0x72b54a86; - pub fn load() -> Option { - None + fn revision(data: &[u8]) -> u8 { + if (data[0] as u16) << 8 | data[1] as u16 == Self::PSF1_MAGIC { + 1 + } else if (data[0] as u32) << 24 | (data[1] as u32) << 16 | (data[2] as u32) << 8 | data[3] as u32 == Self::PSF2_MAGIC { + 2 + } else { + 0 + } } -} -impl Font { - const MAGIC: u16 = 0x3604; + pub fn load(data: Vec) -> Result { + match Self::revision(&data[0..4]) { + 1 => Self::parse_psf1(&data), + 2 => Self::parse_psf2(&data), + _ => panic!("invalid font revision result"), + } + } - pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result { - let magic: u16 = (data[0] as u16) << 8 | data[1] as u16; + fn parse_psf1(data: &[u8]) -> Result { let mode = data[2]; let size = data[3]; - if magic != Self::MAGIC { - return Err("Magic value is invalid!"); - } - let has_512_glyphs = (mode & 0x01) != 0; let mut glyphs = [[0; 16]; 512]; let glyph_count = if has_512_glyphs { 512 } else { 256 }; @@ -112,29 +111,15 @@ impl Font { glyphs[i] = buff; } - Ok(Self(glyphs)) - } -} - -type FileContents = [u8; (32 + 2) * 512 + 4]; -fn load_file(filename: String) -> Result { - let mut buf = [0; (32 + 2) * 512 + 4]; - let mut f = File::open(filename).unwrap(); - f.seek(SeekFrom::Start(0)).unwrap(); - - loop { - match f.read(&mut buf) { - Ok(read) => { - if read == 0 { - break; - } - } - Err(why) => { - eprintln!("Failed to read PS1 font file: {}", why); - return Err(why); - } - } + Ok(FontData { + width: 8, + height: size, + length: glyph_count, + data: glyphs, + }) } - Ok(buf) + fn parse_psf2(data: &[u8]) -> Result { + Err("PSF2 support is not implemented yet!") + } }