fixed triple fault

This commit is contained in:
2025-02-27 16:52:31 +00:00
parent 821759ec63
commit 2178215a01
4 changed files with 53 additions and 18 deletions
+30 -12
View File
@@ -1,5 +1,6 @@
use core::arch::x86_64::__cpuid;
use spin::Lazy;
use x86_64::{
PhysAddr, VirtAddr,
instructions::port::Port,
@@ -23,7 +24,12 @@ const IA32_APIC_BASE_MSR_DISABLE: u64 = !IA32_APIC_BASE_MSR_ENABLE;
const CPUID_FEAT_EDX_APIC: u64 = 1 << 9; // the cpuid instruction will return this flag if it supports APIC
const APIC_VIRTUAL_ADDRESS: u64 = 0xFEC00000;
// const APIC_VIRTUAL_ADDRESS: Lazy<VirtAddr> = Lazy::new(|| {
// let apic_base = get_apic_base();
// let virt_addr = unsafe { phys_to_virt(apic_base) };
// virt_addr
// });
fn set_apic_base_enable(apic: PhysAddr) {
let rax = (apic.as_u64() & 0xfffff0000) | IA32_APIC_BASE_MSR_ENABLE;
@@ -65,27 +71,39 @@ pub fn check_apic() -> bool {
unsafe fn phys_to_virt(phys: PhysAddr) -> VirtAddr {
let phys = phys.as_u64();
match phys.checked_add(*PHYSICAL_MEMORY_OFFSET) {
Some(virt) => VirtAddr::new(virt),
None => panic!("overflow"),
Some(virt) => {
serial_print!("map worked!");
VirtAddr::new(virt)
}
None => {
serial_print!("THIS IS A PROBLEM");
panic!("overflow")
}
}
}
pub fn enable_apic() {
pub fn enable_apic(
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut FoundryOSFrameAllocator,
) {
let apic_phys_addr = get_apic_base();
set_apic_base_enable(apic_phys_addr);
// map virt address of apic
// let frame = PhysFrame::containing_address(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 {
// mapper.map_to(page, frame, flags, frame_allocator);
// match mapper.map_to(page, frame, flags, frame_allocator) {
// Ok(_) => {}
// Err(why) => panic!("failed to map apic: {:?}", why),
// }
// }
set_apic_base_enable(apic_phys_addr);
// FIXME: this causes a page fault
// TODO: map to virtual memor
let apic_virt = unsafe { phys_to_virt(apic_phys_addr) };
// // FIXME: this causes a page fault
// // TODO: map to virtual memor
let reg = read_apic_register(&apic_virt, 0xF0);