FIXED THE APIC HELL YEAH

This commit is contained in:
2025-03-04 12:44:42 +00:00
parent 2186b829aa
commit 8704b5d249
5 changed files with 59 additions and 77 deletions
+1
View File
@@ -6,6 +6,7 @@
<sourceFolder url="file://$MODULE_DIR$/lib_example/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/lib_example/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/libk/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/libk/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/libm/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/libm/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/kernel_fonts/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
+5
View File
@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
+15 -25
View File
@@ -1,7 +1,7 @@
use core::arch::x86_64::__cpuid; use core::arch::x86_64::__cpuid;
use crate::arch::x86_64::memory::mapping::PHYSICAL_MEMORY_OFFSET; use crate::arch::x86_64::memory::mapping::PHYSICAL_MEMORY_OFFSET;
use crate::serial_print; use crate::{serial_print, serial_println};
use x86_64::{ use x86_64::{
PhysAddr, VirtAddr, PhysAddr, VirtAddr,
structures::paging::{Mapper, Page, PageTableFlags, PhysFrame, Size4KiB}, structures::paging::{Mapper, Page, PageTableFlags, PhysFrame, Size4KiB},
@@ -40,19 +40,21 @@ fn get_apic_base() -> PhysAddr {
PhysAddr::new(value & 0xfffff0000) 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 apic_base = apic_base.as_u64();
let reg_addr = (apic_base & 0xFFFFF0000) + reg 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(); let apic_base = apic_base.as_u64();
serial_print!("got apic base");
let reg_addr = (apic_base & 0xFFFFF0000) + reg as u64; 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 { pub fn check_apic() -> bool {
@@ -74,27 +76,15 @@ pub fn enable_apic() {
let apic_phys_addr = get_apic_base(); let apic_phys_addr = get_apic_base();
set_apic_base_enable(apic_phys_addr); set_apic_base_enable(apic_phys_addr);
// map virt address of apic // map virt address of apic
let apic_virt = unsafe { phys_to_virt(apic_phys_addr) }; let apic_virt = unsafe { phys_to_virt(apic_phys_addr) };
let page: Page<Size4KiB> = Page::containing_address(apic_virt);
let frame: PhysFrame<Size4KiB> = PhysFrame::containing_address(apic_phys_addr);
let flags: PageTableFlags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { serial_println!("Trying to map: {:?} -> {:?}", apic_virt, apic_phys_addr);
match mapper.map_to(page, frame, flags, &mut *frame_allocator) {
Ok(_) => {}
Err(why) => panic!("failed to map apic: {:?}", why),
}
}
// // FIXME: this causes a page fault // FIXME: this causes a page fault
// // TODO: map to virtual memory // TODO: map to virtual memory
let reg = read_apic_register(&apic_virt, 0xF0); let reg = read_apic_register(&apic_phys_addr, 0xF0);
write_apic_register(&apic_phys_addr, 0xF0, reg | 0x100);
serial_print!("ok2");
write_apic_register(&apic_virt, 0xF0, reg | 0x100);
} }
pub struct Apic {} pub struct Apic {}
+7 -6
View File
@@ -22,6 +22,7 @@ use core::arch::asm;
use limine::BaseRevision; use limine::BaseRevision;
use x86_64::VirtAddr; use x86_64::VirtAddr;
use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator; use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator;
use crate::arch::x86_64::cpu::apic::enable_apic;
pub mod arch; pub mod arch;
pub mod resources; pub mod resources;
@@ -99,17 +100,17 @@ pub fn boot() -> Result<(), &'static str> {
} }
println_log!("[Success]"); println_log!("[Success]");
print_log!(" Enabling PICs... "); // print_log!(" Enabling PICs... ");
interrupts::enable_pic(); // interrupts::enable_pic();
println_log!("[Success]"); // println_log!("[Success]");
// print_log!(" Disabling PICs... "); // print_log!(" Disabling PICs... ");
// interrupts::disable_pic(); // interrupts::disable_pic();
// println_log!("[Success]"); // println_log!("[Success]");
// print_log!(" Initialising APIC"); print_log!(" Initialising APIC... ");
// enable_apic(); enable_apic();
// println_log!("[Success]"); println_log!("[Success]");
print_log!(" Enabling Interrupts... "); print_log!(" Enabling Interrupts... ");
x86_64::instructions::interrupts::enable(); x86_64::instructions::interrupts::enable();
+32 -47
View File
@@ -40,13 +40,13 @@ pub fn include_font(item: TokenStream) -> TokenStream {
filename.value() filename.value()
); );
let font_bytes = match load_file(file_path) { let font_bytes = match std::fs::read(file_path) {
Ok(bytes) => bytes, Ok(bytes) => bytes,
Err(why) => panic!("{}", why), Err(why) => panic!("{}", why),
}; };
let font_data = match Font::new(font_bytes) { let font_data = match FontBuilder::load(font_bytes) {
Ok(font) => font.0, Ok(font) => font.data,
Err(why) => panic!("{}", why), Err(why) => panic!("{}", why),
}; };
@@ -66,40 +66,39 @@ struct FontData {
width: u8, width: u8,
height: u8, height: u8,
length: u16, length: u16,
data: Vec<Glyph>, pub data: [[u8; 16]; 512],
unicode_table: [[u8; 2]; 512],
} }
struct Glyph {
bytes: Vec<u8>,
}
enum FontBuilder { enum FontBuilder {
Psf1, Psf1(FontData),
Psf2, Psf2(FontData),
} }
impl FontBuilder { impl FontBuilder {
const PSF1_MAGIC: u16 = 0x3604; const PSF1_MAGIC: u16 = 0x3604;
const PSF2_MAGIC: u32 = 0x72b54a86; const PSF2_MAGIC: u32 = 0x72b54a86;
pub fn load() -> Option<Self> { fn revision(data: &[u8]) -> u8 {
None 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 { pub fn load(data: Vec<u8>) -> Result<FontData, &'static str> {
const MAGIC: u16 = 0x3604; 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<Self, &'static str> { fn parse_psf1(data: &[u8]) -> Result<FontData, &'static str> {
let magic: u16 = (data[0] as u16) << 8 | data[1] as u16;
let mode = data[2]; let mode = data[2];
let size = data[3]; let size = data[3];
if magic != Self::MAGIC {
return Err("Magic value is invalid!");
}
let has_512_glyphs = (mode & 0x01) != 0; let has_512_glyphs = (mode & 0x01) != 0;
let mut glyphs = [[0; 16]; 512]; let mut glyphs = [[0; 16]; 512];
let glyph_count = if has_512_glyphs { 512 } else { 256 }; let glyph_count = if has_512_glyphs { 512 } else { 256 };
@@ -112,29 +111,15 @@ impl Font {
glyphs[i] = buff; glyphs[i] = buff;
} }
Ok(Self(glyphs)) Ok(FontData {
width: 8,
height: size,
length: glyph_count,
data: glyphs,
})
}
fn parse_psf2(data: &[u8]) -> Result<FontData, &'static str> {
Err("PSF2 support is not implemented yet!")
} }
} }
type FileContents = [u8; (32 + 2) * 512 + 4];
fn load_file(filename: String) -> Result<FileContents, std::io::Error> {
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(buf)
}