refactor: Changing file structure
This commit is contained in:
parent
d3b458b6ad
commit
ce0ba28c55
@ -5,6 +5,7 @@ use acpi::rsdp::Rsdp;
|
||||
use core::ptr::NonNull;
|
||||
use core::mem;
|
||||
use crate::kprintln;
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct CureAcpiHandler {
|
||||
@ -202,100 +203,95 @@ pub fn init(rsdp_addr: u64, physical_memory_offset: u64) -> Option<AcpiInfo> {
|
||||
handler.map_physical_region::<Rsdp>(rsdp_addr as usize, mem::size_of::<Rsdp>())
|
||||
};
|
||||
let revision = rsdp_mapping.revision();
|
||||
kprintln!(" ACPI Revision: {}", revision);
|
||||
log_info!("ACPI Revision: {}", revision);
|
||||
|
||||
let tables = unsafe {
|
||||
match AcpiTables::from_rsdp(handler, rsdp_addr as usize) {
|
||||
Ok(tables) => tables,
|
||||
Err(e) => {
|
||||
kprintln!(" Failed to parse ACPI tables: {:?}", e);
|
||||
log_error!("Failed to parse ACPI tables: {:?}", e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
};
|
||||
// kprintln!(" ACPI Revision: {}", tables.rsdp_revision);
|
||||
|
||||
|
||||
let platform = match AcpiPlatform::new(tables, handler) {
|
||||
Ok(platform) => platform,
|
||||
Err(e) => {
|
||||
kprintln!(" Failed to create ACPI platform: {:?}", e);
|
||||
log_error!("Failed to create ACPI platform: {:?}", e);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
kprintln!(" Platform Info:");
|
||||
kprintln!(" Power Profile: {:?}", platform.power_profile);
|
||||
log_info!("Power Profile: {:?}", platform.power_profile);
|
||||
|
||||
let (boot_processor, cpu_count) = if let Some(proc_info) = &platform.processor_info {
|
||||
let boot_proc = Some(proc_info.boot_processor.processor_uid);
|
||||
let cpu_cnt = proc_info.application_processors.len() + 1;
|
||||
|
||||
kprintln!(" Boot Processor UID: {:?}", boot_proc);
|
||||
kprintln!(" Total CPU Count: {}", cpu_cnt);
|
||||
log_info!("Boot Processor UID: {:?}", boot_proc);
|
||||
log_info!("Total CPU Count: {}", cpu_cnt);
|
||||
|
||||
(boot_proc, cpu_cnt)
|
||||
} else {
|
||||
kprintln!(" No processor info found");
|
||||
log_warn!("No processor info found");
|
||||
(None, 0)
|
||||
};
|
||||
|
||||
// 檢查中斷模型
|
||||
let has_apic = match &platform.interrupt_model {
|
||||
InterruptModel::Apic(apic) => {
|
||||
kprintln!(" Interrupt Model: APIC");
|
||||
kprintln!(" Local APIC Address: {:#x}", apic.local_apic_address);
|
||||
kprintln!(" IO APICs: {} controller(s)", apic.io_apics.len());
|
||||
log_info!("Local APIC Address: {:#x}", apic.local_apic_address);
|
||||
log_info!("IO APICs: {} controller(s)", apic.io_apics.len());
|
||||
|
||||
for (i, io_apic) in apic.io_apics.iter().enumerate() {
|
||||
kprintln!(" IO APIC {}: ID={}, Address={:#x}, GSI Base={}",
|
||||
log_info!("IO APIC {}: ID={}, Address={:#x}, GSI Base={}",
|
||||
i, io_apic.id, io_apic.address, io_apic.global_system_interrupt_base);
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
InterruptModel::Unknown => {
|
||||
kprintln!(" Interrupt Model: Unknown (not APIC)");
|
||||
log_warn!("Interrupt Model: Unknown (not APIC)");
|
||||
false
|
||||
}
|
||||
_ => {
|
||||
kprintln!(" Interrupt Model: Other");
|
||||
log_warn!("Interrupt Model: Other");
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
// 檢查 HPET (High Precision Event Timer)
|
||||
let has_hpet = match HpetInfo::new(&platform.tables) {
|
||||
Ok(hpet) => {
|
||||
kprintln!(" HPET (High Precision Event Timer):");
|
||||
kprintln!(" Base Address: {:#x}", hpet.base_address);
|
||||
kprintln!(" Hardware Rev: {}", hpet.hardware_rev);
|
||||
kprintln!(" Comparator Count: {}", hpet.num_comparators);
|
||||
kprintln!(" Counter Size: {} bit", if hpet.main_counter_is_64bits { 64 } else { 32 });
|
||||
kprintln!(" Legacy IRQ Capable: {}", hpet.legacy_irq_capable);
|
||||
kprintln!(" PCI Vendor ID: {:#x}", hpet.pci_vendor_id);
|
||||
log_info!("Base Address: {:#x}", hpet.base_address);
|
||||
log_info!("Hardware Rev: {}", hpet.hardware_rev);
|
||||
log_info!("Comparator Count: {}", hpet.num_comparators);
|
||||
log_info!("Counter Size: {} bit", if hpet.main_counter_is_64bits { 64 } else { 32 });
|
||||
log_info!("Legacy IRQ Capable: {}", hpet.legacy_irq_capable);
|
||||
log_info!("PCI Vendor ID: {:#x}", hpet.pci_vendor_id);
|
||||
true
|
||||
}
|
||||
Err(_) => {
|
||||
kprintln!(" HPET: Not available");
|
||||
log_warn!("HPET: Not available");
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if let Ok(mcfg) = PciConfigRegions::new(&platform.tables) {
|
||||
kprintln!(" PCI Express Configuration:");
|
||||
for (i, entry) in mcfg.regions.iter().enumerate() {
|
||||
let segment_group = entry.pci_segment_group;
|
||||
let base_addr = entry.base_address;
|
||||
let bus_start = entry.bus_number_start;
|
||||
let bus_end = entry.bus_number_end;
|
||||
|
||||
kprintln!(" Entry {}: Segment Group {}", i, segment_group);
|
||||
kprintln!(" Base Address: {:#x}", base_addr);
|
||||
kprintln!(" Bus Range: {}-{}", bus_start, bus_end);
|
||||
log_info!("Entry {}: Segment Group {}", i, segment_group);
|
||||
log_info!("Base Address: {:#x}", base_addr);
|
||||
log_info!("Bus Range: {}-{}", bus_start, bus_end);
|
||||
}
|
||||
}
|
||||
|
||||
kprintln!(" ACPI initialized successfully!");
|
||||
log_info!("ACPI initialized successfully!");
|
||||
|
||||
Some(AcpiInfo {
|
||||
revision,
|
||||
@ -308,12 +304,11 @@ pub fn init(rsdp_addr: u64, physical_memory_offset: u64) -> Option<AcpiInfo> {
|
||||
|
||||
pub fn print_info(info: &AcpiInfo) {
|
||||
kprintln!();
|
||||
kprintln!("ACPI Summary:");
|
||||
kprintln!(" Revision: ACPI {}.0", info.revision);
|
||||
kprintln!(" CPUs: {} processor(s)", info.cpu_count);
|
||||
log_info!("Revision: ACPI {}.0", info.revision);
|
||||
log_info!("CPUs: {} processor(s)", info.cpu_count);
|
||||
if let Some(boot_proc) = info.boot_processor {
|
||||
kprintln!(" Boot Processor: UID {}", boot_proc);
|
||||
log_info!("Boot Processor: UID {}", boot_proc);
|
||||
}
|
||||
kprintln!(" APIC: {}", if info.has_apic { "Available " } else { "Not available" });
|
||||
kprintln!(" HPET: {}", if info.has_hpet { "Available " } else { "Not available" });
|
||||
log_info!("APIC: {}", if info.has_apic { "Available " } else { "Not available" });
|
||||
log_info!("HPET: {}", if info.has_hpet { "Available " } else { "Not available" });
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
// kernel/src/kernel/asm/x86/gdt.rs
|
||||
// kernel/src/kernel/asm/amd64/gdt.rs
|
||||
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector};
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use x86_64::instructions::segmentation::{Segment, CS, DS, ES, SS};
|
||||
@ -6,6 +6,7 @@ use x86_64::instructions::tables::load_tss;
|
||||
use x86_64::VirtAddr;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::kprintln;
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
|
||||
|
||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
||||
|
||||
@ -119,10 +120,9 @@ pub fn init() {
|
||||
pub fn print_info() {
|
||||
let selectors = get_selectors();
|
||||
|
||||
kprintln!("GDT Information:");
|
||||
kprintln!(" Kernel Code: {:#x} (ring 0)", selectors.kernel_code_selector.0);
|
||||
kprintln!(" Kernel Data: {:#x} (ring 0)", selectors.kernel_data_selector.0);
|
||||
kprintln!(" User Code: {:#x} (ring 3)", selectors.user_code_selector.0);
|
||||
kprintln!(" User Data: {:#x} (ring 3)", selectors.user_data_selector.0);
|
||||
kprintln!(" TSS: {:#x}", selectors.tss_selector.0);
|
||||
log_info!("Kernel Code: {:#x} (ring 0)", selectors.kernel_code_selector.0);
|
||||
log_info!("Kernel Data: {:#x} (ring 0)", selectors.kernel_data_selector.0);
|
||||
log_info!("User Code: {:#x} (ring 3)", selectors.user_code_selector.0);
|
||||
log_info!("User Data: {:#x} (ring 3)", selectors.user_data_selector.0);
|
||||
log_info!("TSS: {:#x}", selectors.tss_selector.0);
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
// kernel/src/kernel/asm/x86/idt.rs
|
||||
// kernel/src/kernel/asm/amd64/idt.rs
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable};
|
||||
use lazy_static::lazy_static;
|
||||
use crate::kprintln;
|
||||
use super::interrupt::*;
|
||||
use super::isr::*;
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: InterruptDescriptorTable = {
|
||||
@ -20,7 +21,7 @@ lazy_static! {
|
||||
unsafe {
|
||||
idt.double_fault
|
||||
.set_handler_fn(double_fault_handler)
|
||||
.set_stack_index(crate::kernel::asm::x86::gdt::DOUBLE_FAULT_IST_INDEX);
|
||||
.set_stack_index(super::gdt::DOUBLE_FAULT_IST_INDEX);
|
||||
}
|
||||
|
||||
idt.invalid_tss.set_handler_fn(invalid_tss_handler);
|
||||
@ -43,7 +44,6 @@ pub fn init() {
|
||||
}
|
||||
|
||||
pub fn print_info() {
|
||||
kprintln!("IDT Information:");
|
||||
kprintln!(" IDT loaded and active");
|
||||
kprintln!(" Exception handlers registered");
|
||||
log_info!(" IDT loaded and active");
|
||||
log_info!(" Exception handlers registered");
|
||||
}
|
||||
@ -1,12 +1,13 @@
|
||||
// kernel/src/kernel/asm/x86/interrupt.rs
|
||||
// kernel/src/kernel/asm/amd64/isr
|
||||
use x86_64::structures::idt::{InterruptStackFrame, PageFaultErrorCode};
|
||||
use crate::kprintln;
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
|
||||
|
||||
/// Divide Error (#DE)
|
||||
pub extern "x86-interrupt" fn divide_error_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: DIVIDE ERROR (#DE)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: DIVIDE ERROR (#DE)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -15,29 +16,29 @@ pub extern "x86-interrupt" fn divide_error_handler(stack_frame: InterruptStackFr
|
||||
/// Debug Exception (#DB)
|
||||
pub extern "x86-interrupt" fn debug_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: DEBUG (#DB)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_debug!("EXCEPTION: DEBUG (#DB)");
|
||||
log_debug!("{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
/// Non-Maskable Interrupt (NMI)
|
||||
pub extern "x86-interrupt" fn nmi_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: NON-MASKABLE INTERRUPT (NMI)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: NON-MASKABLE INTERRUPT (NMI)");
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
/// Breakpoint (#BP)
|
||||
pub extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: BREAKPOINT (#BP)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_debug!("EXCEPTION: BREAKPOINT (#BP)");
|
||||
log_debug!("{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
/// Overflow (#OF)
|
||||
pub extern "x86-interrupt" fn overflow_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: OVERFLOW (#OF)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: OVERFLOW (#OF)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -46,8 +47,8 @@ pub extern "x86-interrupt" fn overflow_handler(stack_frame: InterruptStackFrame)
|
||||
/// Bound Range Exceeded (#BR)
|
||||
pub extern "x86-interrupt" fn bound_range_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: BOUND RANGE EXCEEDED (#BR)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: BOUND RANGE EXCEEDED (#BR)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -56,8 +57,8 @@ pub extern "x86-interrupt" fn bound_range_handler(stack_frame: InterruptStackFra
|
||||
/// Invalid Opcode (#UD)
|
||||
pub extern "x86-interrupt" fn invalid_opcode_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: INVALID OPCODE (#UD)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: INVALID OPCODE (#UD)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -66,8 +67,8 @@ pub extern "x86-interrupt" fn invalid_opcode_handler(stack_frame: InterruptStack
|
||||
/// Device Not Available (#NM)
|
||||
pub extern "x86-interrupt" fn device_not_available_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: DEVICE NOT AVAILABLE (#NM)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: DEVICE NOT AVAILABLE (#NM)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -78,7 +79,11 @@ pub extern "x86-interrupt" fn double_fault_handler(
|
||||
stack_frame: InterruptStackFrame,
|
||||
error_code: u64,
|
||||
) -> ! {
|
||||
panic!("EXCEPTION: DOUBLE FAULT (#DF)\nError Code: {}\n{:#?}", error_code, stack_frame);
|
||||
kprintln!();
|
||||
log_fatal!("EXCEPTION: DOUBLE FAULT (#DF)");
|
||||
log_fatal!("Error Code: {:#x}", error_code);
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
panic!("DOUBLE FAULT - System cannot continue");
|
||||
}
|
||||
|
||||
/// Invalid TSS (#TS)
|
||||
@ -87,9 +92,9 @@ pub extern "x86-interrupt" fn invalid_tss_handler(
|
||||
error_code: u64,
|
||||
) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: INVALID TSS (#TS)");
|
||||
kprintln!("Error Code: {:#x}", error_code);
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: INVALID TSS (#TS)");
|
||||
log_fatal!("Error Code: {:#x}", error_code);
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -101,9 +106,9 @@ pub extern "x86-interrupt" fn segment_not_present_handler(
|
||||
error_code: u64,
|
||||
) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: SEGMENT NOT PRESENT (#NP)");
|
||||
kprintln!("Error Code: {:#x}", error_code);
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: SEGMENT NOT PRESENT (#NP)");
|
||||
log_fatal!("Error Code: {:#x}", error_code);
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -115,9 +120,9 @@ pub extern "x86-interrupt" fn stack_segment_fault_handler(
|
||||
error_code: u64,
|
||||
) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: STACK SEGMENT FAULT (#SS)");
|
||||
kprintln!("Error Code: {:#x}", error_code);
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: STACK SEGMENT FAULT (#SS)");
|
||||
log_fatal!("Error Code: {:#x}", error_code);
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -129,9 +134,9 @@ pub extern "x86-interrupt" fn general_protection_fault_handler(
|
||||
error_code: u64,
|
||||
) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: GENERAL PROTECTION FAULT (#GP)");
|
||||
kprintln!("Error Code: {:#x}", error_code);
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: GENERAL PROTECTION FAULT (#GP)");
|
||||
log_fatal!("Error Code: {:#x}", error_code);
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -145,15 +150,15 @@ pub extern "x86-interrupt" fn page_fault_handler(
|
||||
use x86_64::registers::control::Cr2;
|
||||
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: PAGE FAULT (#PF)");
|
||||
kprintln!("Accessed Address: {:?}", Cr2::read());
|
||||
kprintln!("Error Code: {:?}", error_code);
|
||||
kprintln!(" - Present: {}", error_code.contains(PageFaultErrorCode::PROTECTION_VIOLATION));
|
||||
kprintln!(" - Write: {}", error_code.contains(PageFaultErrorCode::CAUSED_BY_WRITE));
|
||||
kprintln!(" - User: {}", error_code.contains(PageFaultErrorCode::USER_MODE));
|
||||
kprintln!(" - Reserved Write: {}", error_code.contains(PageFaultErrorCode::MALFORMED_TABLE));
|
||||
kprintln!(" - Instruction Fetch: {}", error_code.contains(PageFaultErrorCode::INSTRUCTION_FETCH));
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_fatal!("EXCEPTION: PAGE FAULT (#PF)");
|
||||
log_fatal!("Accessed Address: {:?}", Cr2::read());
|
||||
log_fatal!("Error Code: {:?}", error_code);
|
||||
log_fatal!("Present: {}", error_code.contains(PageFaultErrorCode::PROTECTION_VIOLATION));
|
||||
log_fatal!("Write: {}", error_code.contains(PageFaultErrorCode::CAUSED_BY_WRITE));
|
||||
log_fatal!("User: {}", error_code.contains(PageFaultErrorCode::USER_MODE));
|
||||
log_fatal!("Reserved Write: {}", error_code.contains(PageFaultErrorCode::MALFORMED_TABLE));
|
||||
log_fatal!("Instruction Fetch: {}", error_code.contains(PageFaultErrorCode::INSTRUCTION_FETCH));
|
||||
log_fatal!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -162,8 +167,8 @@ pub extern "x86-interrupt" fn page_fault_handler(
|
||||
/// x87 Floating-Point Exception (#MF)
|
||||
pub extern "x86-interrupt" fn x87_floating_point_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: x87 FLOATING POINT (#MF)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: x87 FLOATING POINT (#MF)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -175,9 +180,9 @@ pub extern "x86-interrupt" fn alignment_check_handler(
|
||||
error_code: u64,
|
||||
) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: ALIGNMENT CHECK (#AC)");
|
||||
kprintln!("Error Code: {:#x}", error_code);
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_error!("EXCEPTION: ALIGNMENT CHECK (#AC)");
|
||||
log_error!("Error Code: {:#x}", error_code);
|
||||
log_error!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -185,7 +190,10 @@ pub extern "x86-interrupt" fn alignment_check_handler(
|
||||
|
||||
/// Machine Check (#MC)
|
||||
pub extern "x86-interrupt" fn machine_check_handler(stack_frame: InterruptStackFrame) -> ! {
|
||||
panic!("EXCEPTION: MACHINE CHECK (#MC)\n{:#?}", stack_frame);
|
||||
kprintln!();
|
||||
log_error!("EXCEPTION: MACHINE CHECK (#MC)");
|
||||
log_error!("{:#?}", stack_frame);
|
||||
panic!("MACHINE CHECK - System cannot continue");
|
||||
}
|
||||
|
||||
/// SIMD Floating-Point Exception (#XM/#XF)
|
||||
@ -201,8 +209,8 @@ pub extern "x86-interrupt" fn simd_floating_point_handler(stack_frame: Interrupt
|
||||
/// Virtualization Exception (#VE)
|
||||
pub extern "x86-interrupt" fn virtualization_handler(stack_frame: InterruptStackFrame) {
|
||||
kprintln!();
|
||||
kprintln!("EXCEPTION: VIRTUALIZATION (#VE)");
|
||||
kprintln!("{:#?}", stack_frame);
|
||||
log_warn!("EXCEPTION: VIRTUALIZATION (#VE)");
|
||||
log_warn!("{:#?}", stack_frame);
|
||||
loop {
|
||||
crate::hal::cpu::cpu_halt();
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
pub mod gdt;
|
||||
pub mod idt;
|
||||
pub mod interrupt;
|
||||
pub(crate) mod acpi;
|
||||
pub mod isr;
|
||||
pub mod acpi;
|
||||
// pub mod segment;
|
||||
1
kernel/src/arch/mod.rs
Normal file
1
kernel/src/arch/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod amd64;
|
||||
@ -2,12 +2,12 @@
|
||||
use bootloader_api::BootInfo;
|
||||
use x86_64::structures::paging::OffsetPageTable;
|
||||
use x86_64::VirtAddr;
|
||||
use crate::kernel::asm::x86::{gdt, idt, acpi};
|
||||
use crate::kernel::mm::{heap, frame_allocator};
|
||||
use crate::kernel::tty::tty;
|
||||
use crate::arch::amd64::{gdt, idt, acpi};
|
||||
use crate::mm::{heap, frame_allocator};
|
||||
use crate::tty::tty;
|
||||
use crate::kprintln;
|
||||
use crate::k_main;
|
||||
use crate::logger::{init as logger_init, LoggerConfig, LogLevel};
|
||||
use crate::libs::logger::{init as logger_init, LoggerConfig, LogLevel};
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error};
|
||||
|
||||
fn _logger_init() {
|
||||
@ -56,7 +56,6 @@ fn _display_init(framebuffer: &'static mut bootloader_api::info::FrameBuffer) {
|
||||
}
|
||||
|
||||
fn _boot_report(memory_regions: &bootloader_api::info::MemoryRegions, physical_memory_offset: u64) {
|
||||
log_info!("Stage 1: Critical Hardware");
|
||||
log_info!("GDT initialized");
|
||||
gdt::print_info();
|
||||
|
||||
@ -65,7 +64,6 @@ fn _boot_report(memory_regions: &bootloader_api::info::MemoryRegions, physical_m
|
||||
idt::print_info();
|
||||
|
||||
kprintln!();
|
||||
log_info!("Stage 2: Memory Management");
|
||||
log_info!("Physical Memory Offset: {:#x}", physical_memory_offset);
|
||||
|
||||
log_debug!("Memory Regions:");
|
||||
@ -88,14 +86,12 @@ fn _boot_report(memory_regions: &bootloader_api::info::MemoryRegions, physical_m
|
||||
log_info!("Total Usable Memory: {} MiB", total_usable / (1024 * 1024));
|
||||
|
||||
kprintln!();
|
||||
log_info!("Stage 3: Heap Allocator");
|
||||
log_info!("Heap Start: {:#x}", heap::HEAP_START);
|
||||
log_info!("Heap Size: {} KiB", heap::HEAP_SIZE / 1024);
|
||||
}
|
||||
|
||||
fn _acpi_init(rsdp_addr: Option<u64>, physical_memory_offset: u64) {
|
||||
kprintln!();
|
||||
log_info!("Stage 4: ACPI");
|
||||
|
||||
if let Some(rsdp) = rsdp_addr {
|
||||
log_debug!("RSDP Address: {:#x}", rsdp);
|
||||
@ -114,11 +110,10 @@ fn _post_init() {
|
||||
kprintln!();
|
||||
log_info!("Post Initialization");
|
||||
// TODO: 釋放 bootloader 佔用的內存
|
||||
// TODO: 釋放初始化代碼段(.init 段)
|
||||
log_debug!("Cleanup completed");
|
||||
}
|
||||
|
||||
pub fn kernel_init(boot_info: &'static mut BootInfo) -> ! {
|
||||
pub fn _kernel_init(boot_info: &'static mut BootInfo) -> ! {
|
||||
if let Some(framebuffer) = boot_info.framebuffer.as_mut() {
|
||||
|
||||
_critical_init();
|
||||
@ -151,7 +146,7 @@ pub fn kernel_init(boot_info: &'static mut BootInfo) -> ! {
|
||||
kprintln!("========================================");
|
||||
kprintln!();
|
||||
|
||||
k_main::kernel_main();
|
||||
k_main::_kernel_main();
|
||||
|
||||
} else {
|
||||
panic!("No framebuffer provided by bootloader");
|
||||
|
||||
@ -3,7 +3,7 @@ use crate::hal::cpu;
|
||||
use crate::kprintln;
|
||||
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
|
||||
|
||||
pub fn kernel_main() -> ! {
|
||||
pub fn _kernel_main() -> ! {
|
||||
kprintln!();
|
||||
kprintln!("=== Welcome to CureOS! ===");
|
||||
kprintln!();
|
||||
@ -16,7 +16,6 @@ pub fn kernel_main() -> ! {
|
||||
);
|
||||
|
||||
kprintln!();
|
||||
log_debug!("Control Registers:");
|
||||
log_debug!("CR0: 0x{:016x}", cpu::cpu_r_cr0().bits());
|
||||
log_debug!("CR2: 0x{:016x}", cpu::cpu_r_cr2());
|
||||
log_debug!("CR3: 0x{:016x}", cpu::cpu_r_cr3());
|
||||
|
||||
@ -1 +0,0 @@
|
||||
pub mod x86;
|
||||
@ -1,5 +0,0 @@
|
||||
pub mod tty;
|
||||
pub mod asm;
|
||||
pub(crate) mod mm;
|
||||
// 重新導出由 boot.S 調用的入口點
|
||||
// pub use kernel::{_kernel_init, _kernel_main};
|
||||
@ -1 +0,0 @@
|
||||
pub mod print;
|
||||
@ -1,6 +1,6 @@
|
||||
// kernel/src/logger.rs
|
||||
use core::fmt;
|
||||
use crate::kernel::tty::tty;
|
||||
use crate::tty::tty;
|
||||
use spin::Mutex;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
@ -158,8 +158,8 @@ fn fmt_to_buffer<'a>(buf: &'a mut [u8], args: fmt::Arguments) -> Result<&'a str,
|
||||
#[macro_export]
|
||||
macro_rules! log_trace {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Trace,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Trace,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -171,8 +171,8 @@ macro_rules! log_trace {
|
||||
#[macro_export]
|
||||
macro_rules! log_debug {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Debug,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Debug,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -184,8 +184,8 @@ macro_rules! log_debug {
|
||||
#[macro_export]
|
||||
macro_rules! log_info {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Info,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Info,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -197,8 +197,8 @@ macro_rules! log_info {
|
||||
#[macro_export]
|
||||
macro_rules! log_warn {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Warn,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Warn,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -210,8 +210,8 @@ macro_rules! log_warn {
|
||||
#[macro_export]
|
||||
macro_rules! log_error {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Error,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Error,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -223,8 +223,8 @@ macro_rules! log_error {
|
||||
#[macro_export]
|
||||
macro_rules! log_fatal {
|
||||
($($arg:tt)*) => {
|
||||
$crate::logger::_log(
|
||||
$crate::logger::LogLevel::Fatal,
|
||||
$crate::libs::logger::_log(
|
||||
$crate::libs::logger::LogLevel::Fatal,
|
||||
format_args!($($arg)*),
|
||||
Some(file!()),
|
||||
Some(line!())
|
||||
@ -1 +1,2 @@
|
||||
pub mod libc;
|
||||
pub mod print;
|
||||
pub mod logger;
|
||||
@ -1,7 +1,7 @@
|
||||
// src/libs/libc/print.rs
|
||||
|
||||
use core::fmt;
|
||||
use crate::kernel::tty::tty;
|
||||
use crate::tty::tty;
|
||||
|
||||
pub struct Writer;
|
||||
|
||||
@ -19,15 +19,15 @@ pub fn _print(args: fmt::Arguments) {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ($crate::libs::libc::print::_print(format_args!($($arg)*)));
|
||||
($($arg:tt)*) => ($crate::libs::print::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! kprintln {
|
||||
() => {
|
||||
$crate::libs::libc::print::_print(format_args!("\n"))
|
||||
$crate::libs::print::_print(format_args!("\n"))
|
||||
};
|
||||
($($arg:tt)*) => {
|
||||
$crate::libs::libc::print::_print(format_args!("{}\n", format_args!($($arg)*)))
|
||||
$crate::libs::print::_print(format_args!("{}\n", format_args!($($arg)*)))
|
||||
};
|
||||
}
|
||||
@ -15,12 +15,13 @@ use x86_64::{
|
||||
VirtAddr,
|
||||
};
|
||||
|
||||
mod kernel;
|
||||
mod hal;
|
||||
mod libs;
|
||||
mod logger;
|
||||
mod k_init;
|
||||
mod k_main;
|
||||
pub mod arch;
|
||||
pub mod mm;
|
||||
pub mod tty;
|
||||
|
||||
use hal::cpu;
|
||||
const CONFIG: BootloaderConfig = {
|
||||
@ -29,7 +30,7 @@ const CONFIG: BootloaderConfig = {
|
||||
config
|
||||
};
|
||||
|
||||
entry_point!(k_init::kernel_init, config = &CONFIG);
|
||||
entry_point!(k_init::_kernel_init, config = &CONFIG);
|
||||
|
||||
|
||||
#[cfg(not(test))]
|
||||
@ -40,14 +41,13 @@ fn panic(info: &PanicInfo) -> ! {
|
||||
kprintln!(" KERNEL PANIC!" );
|
||||
kprintln!("================================");
|
||||
kprintln!();
|
||||
kprintln!("{}", info);
|
||||
log_fatal!("{}", info);
|
||||
kprintln!();
|
||||
kprintln!("Control Registers:");
|
||||
kprintln!(" CR0: 0x{:016x}", cpu::cpu_r_cr0().bits());
|
||||
kprintln!(" CR2: 0x{:016x}", cpu::cpu_r_cr2());
|
||||
kprintln!(" CR3: 0x{:016x}", cpu::cpu_r_cr3());
|
||||
kprintln!(" CR4: 0x{:016x}", cpu::cpu_r_cr4().bits());
|
||||
|
||||
log_fatal!(" CR0: 0x{:016x}", cpu::cpu_r_cr0().bits());
|
||||
log_fatal!(" CR2: 0x{:016x}", cpu::cpu_r_cr2());
|
||||
log_fatal!(" CR3: 0x{:016x}", cpu::cpu_r_cr3());
|
||||
log_fatal!(" CR4: 0x{:016x}", cpu::cpu_r_cr4().bits());
|
||||
|
||||
loop {
|
||||
cpu::cpu_halt();
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use bootloader_api::info::{FrameBuffer, FrameBufferInfo, PixelFormat};
|
||||
use crate::kernel::tty::font::FONT_BASIC;
|
||||
use crate::tty::font::FONT_BASIC;
|
||||
use spin::Mutex;
|
||||
|
||||
pub struct TTYState {
|
||||
Loading…
x
Reference in New Issue
Block a user