refactor: Change file structure and rename

This commit is contained in:
ParrotXray 2025-10-12 14:14:04 +08:00
parent ce0ba28c55
commit 6202cfb8bf
21 changed files with 59 additions and 54 deletions

View File

@ -4,7 +4,7 @@
// - ACPI 初始化 - OK // - ACPI 初始化 - OK
// - PIC/APIC 初始化 // - PIC/APIC 初始化
// - 滾動 // - 滾動 - OK
// 2. 內存管理 // 2. 內存管理
// - 物理內存分配器 // - 物理內存分配器

View File

@ -20,8 +20,8 @@ fn main() {
.create_disk_image(&uefi_path) .create_disk_image(&uefi_path)
.expect("Failed to create UEFI disk image"); .expect("Failed to create UEFI disk image");
println!("cargo:warning=BIOS image: {:?}", bios_path); println!("cargo:warning=BIOS image: {:?}", bios_path);
println!("cargo:warning=UEFI image: {:?}", uefi_path); println!("cargo:warning=UEFI image: {:?}", uefi_path);
println!("cargo:rustc-env=BIOS_IMAGE={}", bios_path.display()); println!("cargo:rustc-env=BIOS_IMAGE={}", bios_path.display());
println!("cargo:rustc-env=UEFI_IMAGE={}", uefi_path.display()); println!("cargo:rustc-env=UEFI_IMAGE={}", uefi_path.display());

View File

@ -2,6 +2,7 @@
use x86_64::structures::idt::{InterruptStackFrame, PageFaultErrorCode}; use x86_64::structures::idt::{InterruptStackFrame, PageFaultErrorCode};
use crate::kprintln; use crate::kprintln;
use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal}; use crate::{log_trace, log_debug, log_info, log_warn, log_error, log_fatal};
use crate::hal::cpu;
/// Divide Error (#DE) /// Divide Error (#DE)
pub extern "x86-interrupt" fn divide_error_handler(stack_frame: InterruptStackFrame) { pub extern "x86-interrupt" fn divide_error_handler(stack_frame: InterruptStackFrame) {
@ -151,7 +152,7 @@ pub extern "x86-interrupt" fn page_fault_handler(
kprintln!(); kprintln!();
log_fatal!("EXCEPTION: PAGE FAULT (#PF)"); log_fatal!("EXCEPTION: PAGE FAULT (#PF)");
log_fatal!("Accessed Address: {:?}", Cr2::read()); log_fatal!("Accessed Address: {:?}", cpu::cpu_r_cr2());
log_fatal!("Error Code: {:?}", error_code); log_fatal!("Error Code: {:?}", error_code);
log_fatal!("Present: {}", error_code.contains(PageFaultErrorCode::PROTECTION_VIOLATION)); log_fatal!("Present: {}", error_code.contains(PageFaultErrorCode::PROTECTION_VIOLATION));
log_fatal!("Write: {}", error_code.contains(PageFaultErrorCode::CAUSED_BY_WRITE)); log_fatal!("Write: {}", error_code.contains(PageFaultErrorCode::CAUSED_BY_WRITE));

View File

@ -1,5 +1,4 @@
pub mod gdt; pub mod gdt;
pub mod idt; pub mod idt;
pub mod isr; pub mod isr;
pub mod acpi;
// pub mod segment; // pub mod segment;

View File

@ -2,6 +2,7 @@
use x86_64::registers::control::{Cr0, Cr0Flags, Cr2, Cr3, Cr4, Cr4Flags}; use x86_64::registers::control::{Cr0, Cr0Flags, Cr2, Cr3, Cr4, Cr4Flags};
use x86_64::instructions::{interrupts, hlt}; use x86_64::instructions::{interrupts, hlt};
use core::arch::asm; use core::arch::asm;
use x86_64::{PhysAddr, structures::paging::PhysFrame};
use x86_64::VirtAddr; use x86_64::VirtAddr;
/// 64 位元暫存器類型 /// 64 位元暫存器類型
@ -51,8 +52,8 @@ pub struct SgReg {
/// 讀取 CR0 暫存器 /// 讀取 CR0 暫存器
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub fn cpu_r_cr0() -> Cr0Flags { pub fn cpu_r_cr0() -> u64 {
Cr0::read() Cr0::read_raw()
} }
/// 讀取 CR2 暫存器 /// 讀取 CR2 暫存器
@ -65,15 +66,34 @@ pub fn cpu_r_cr2() -> u64 {
/// 讀取 CR3 暫存器 /// 讀取 CR3 暫存器
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub fn cpu_r_cr3() -> u64 { pub fn cpu_r_cr3_frame() -> PhysFrame {
Cr3::read().0.start_address().as_u64() Cr3::read_raw().0
} }
#[allow(dead_code)]
#[inline]
pub fn cpu_r_cr3_flag() -> u16 {
Cr3::read_raw().1
}
#[allow(dead_code)]
#[inline]
pub fn cpu_r_cr3() -> u64 {
cpu_r_cr3_addr().as_u64()
}
#[allow(dead_code)]
#[inline]
pub fn cpu_r_cr3_addr() -> PhysAddr {
cpu_r_cr3_frame().start_address()
}
/// 讀取 CR4 暫存器 /// 讀取 CR4 暫存器
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub fn cpu_r_cr4() -> Cr4Flags { pub fn cpu_r_cr4() -> u64 {
Cr4::read() Cr4::read_raw()
} }
/// 寫入 CR0 暫存器 /// 寫入 CR0 暫存器
@ -87,8 +107,6 @@ pub fn cpu_w_cr0(val: Cr0Flags) {
#[allow(dead_code)] #[allow(dead_code)]
#[inline] #[inline]
pub fn cpu_w_cr3(val: u64) { pub fn cpu_w_cr3(val: u64) {
use x86_64::{PhysAddr, structures::paging::PhysFrame};
let frame = PhysFrame::containing_address(PhysAddr::new(val)); let frame = PhysFrame::containing_address(PhysAddr::new(val));
unsafe { unsafe {
Cr3::write(frame, Cr3::read().1); Cr3::write(frame, Cr3::read().1);

View File

@ -1,22 +1,4 @@
pub mod io; pub mod io;
pub mod cpu; pub mod cpu;
pub mod acpi;
pub use io::io_port_wb; mod rtc;
pub use io::io_port_wl;
pub use io::io_port_rb;
pub use io::io_port_rl;
pub use cpu::cpu_r_cr0;
pub use cpu::cpu_r_cr2;
pub use cpu::cpu_r_cr3;
pub use cpu::cpu_w_cr0;
pub use cpu::cpu_w_cr3;
pub use cpu::cpu_get_model;
pub use cpu::cpu_brand_string_supported;
pub use cpu::cpu_get_brand;
pub use cpu::cpu_rdtsc;
pub use cpu::cpu_pause;
pub use cpu::cpu_halt;
pub use cpu::cpu_idle;
pub use cpu::cpu_enable_interrupts;
pub use cpu::cpu_disable_interrupts;

0
kernel/src/hal/rtc.rs Normal file
View File

View File

@ -2,13 +2,14 @@
use bootloader_api::BootInfo; use bootloader_api::BootInfo;
use x86_64::structures::paging::OffsetPageTable; use x86_64::structures::paging::OffsetPageTable;
use x86_64::VirtAddr; use x86_64::VirtAddr;
use crate::arch::amd64::{gdt, idt, acpi}; use crate::arch::amd64::{gdt, idt};
use crate::mm::{heap, frame_allocator}; use crate::mm::allocator::{heap, frame};
use crate::tty::tty; use crate::tty::tty;
use crate::kprintln; use crate::kprintln;
use crate::k_main; use crate::kernel::k_main;
use crate::libs::logger::{init as logger_init, LoggerConfig, LogLevel}; use crate::libs::logger::{init as logger_init, LogLevel, LoggerConfig};
use crate::{log_trace, log_debug, log_info, log_warn, log_error}; use crate::{log_debug, log_error, log_info, log_trace, log_warn};
use crate::hal::acpi;
fn _logger_init() { fn _logger_init() {
logger_init( logger_init(
@ -28,7 +29,7 @@ fn _critical_init() {
fn _memory_init( fn _memory_init(
memory_regions: &'static bootloader_api::info::MemoryRegions, memory_regions: &'static bootloader_api::info::MemoryRegions,
physical_memory_offset: u64 physical_memory_offset: u64
) -> (OffsetPageTable<'static>, frame_allocator::BootInfoFrameAllocator) { ) -> (OffsetPageTable<'static>, frame::BootInfoFrameAllocator) {
let phys_mem_offset = VirtAddr::new(physical_memory_offset); let phys_mem_offset = VirtAddr::new(physical_memory_offset);
let mut mapper = unsafe { let mut mapper = unsafe {
@ -36,7 +37,7 @@ fn _memory_init(
}; };
let mut frame_allocator = unsafe { let mut frame_allocator = unsafe {
frame_allocator::BootInfoFrameAllocator::init(memory_regions) frame::BootInfoFrameAllocator::init(memory_regions)
}; };
heap::init_heap(&mut mapper, &mut frame_allocator) heap::init_heap(&mut mapper, &mut frame_allocator)

View File

@ -16,10 +16,10 @@ pub fn _kernel_main() -> ! {
); );
kprintln!(); kprintln!();
log_debug!("CR0: 0x{:016x}", cpu::cpu_r_cr0().bits()); log_debug!("CR0: 0x{:016x}", cpu::cpu_r_cr0());
log_debug!("CR2: 0x{:016x}", cpu::cpu_r_cr2()); log_debug!("CR2: 0x{:016x}", cpu::cpu_r_cr2());
log_debug!("CR3: 0x{:016x}", cpu::cpu_r_cr3()); log_debug!("CR3: 0x{:016x}", cpu::cpu_r_cr3());
log_debug!("CR4: 0x{:016x}", cpu::cpu_r_cr4().bits()); log_debug!("CR4: 0x{:016x}", cpu::cpu_r_cr4());
// kprintln!(); // kprintln!();
// kprintln!("=== Logger Level Demonstration ==="); // kprintln!("=== Logger Level Demonstration ===");

2
kernel/src/kernel/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod k_init;
pub mod k_main;

View File

@ -92,7 +92,6 @@ impl LoggerState {
tty::write_str(level.tag(), level.color()); tty::write_str(level.tag(), level.color());
tty::write_str(" ", 0xFFFFFF); tty::write_str(" ", 0xFFFFFF);
// 如果啟用位置信息
if self.config.show_location { if self.config.show_location {
if let (Some(f), Some(l)) = (file, line) { if let (Some(f), Some(l)) = (file, line) {
let mut location_buf = [0u8; 128]; let mut location_buf = [0u8; 128];

View File

@ -1,2 +1,3 @@
pub mod print; pub mod print;
pub mod logger; pub mod logger;
pub mod string;

View File

View File

@ -17,11 +17,10 @@ use x86_64::{
mod hal; mod hal;
mod libs; mod libs;
mod k_init;
mod k_main;
pub mod arch; pub mod arch;
pub mod mm; pub mod mm;
pub mod tty; pub mod tty;
pub mod kernel;
use hal::cpu; use hal::cpu;
const CONFIG: BootloaderConfig = { const CONFIG: BootloaderConfig = {
@ -30,8 +29,7 @@ const CONFIG: BootloaderConfig = {
config config
}; };
entry_point!(k_init::_kernel_init, config = &CONFIG); entry_point!(kernel::k_init::_kernel_init, config = &CONFIG);
#[cfg(not(test))] #[cfg(not(test))]
#[panic_handler] #[panic_handler]
@ -43,10 +41,10 @@ fn panic(info: &PanicInfo) -> ! {
kprintln!(); kprintln!();
log_fatal!("{}", info); log_fatal!("{}", info);
kprintln!(); kprintln!();
log_fatal!(" CR0: 0x{:016x}", cpu::cpu_r_cr0().bits()); log_fatal!(" CR0: 0x{:016x}", cpu::cpu_r_cr0());
log_fatal!(" CR2: 0x{:016x}", cpu::cpu_r_cr2()); log_fatal!(" CR2: 0x{:016x}", cpu::cpu_r_cr2());
log_fatal!(" CR3: 0x{:016x}", cpu::cpu_r_cr3()); log_fatal!(" CR3: 0x{:016x}", cpu::cpu_r_cr3());
log_fatal!(" CR4: 0x{:016x}", cpu::cpu_r_cr4().bits()); log_fatal!(" CR4: 0x{:016x}", cpu::cpu_r_cr4());
loop { loop {
cpu::cpu_halt(); cpu::cpu_halt();

View File

@ -1,5 +1,5 @@
// kernel/src/mm/frame_allocator.rs // kernel/src/mm/frame
use bootloader_api::info::{MemoryRegion, MemoryRegionKind, MemoryRegions}; use bootloader_api::info::{MemoryRegion, MemoryRegionKind, MemoryRegions};
use x86_64::{ use x86_64::{
structures::paging::{FrameAllocator, PhysFrame, Size4KiB}, structures::paging::{FrameAllocator, PhysFrame, Size4KiB},

View File

@ -61,7 +61,8 @@ pub fn init_heap(
} }
pub unsafe fn get_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable { pub unsafe fn get_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
let virt = physical_memory_offset + cpu::cpu_r_cr3(); let phys = cpu::cpu_r_cr3_addr().as_u64();
let virt = physical_memory_offset + phys;
let page_table_ptr: *mut PageTable = virt.as_mut_ptr(); let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
&mut *page_table_ptr &mut *page_table_ptr

View File

@ -0,0 +1,2 @@
pub mod frame;
pub mod heap;

View File

@ -1,2 +1,3 @@
pub mod frame_allocator; pub mod allocator;
pub mod heap; pub mod paging;
pub mod vmm;

0
kernel/src/mm/paging.rs Normal file
View File

0
kernel/src/mm/vmm.rs Normal file
View File