Skip to content

Commit

Permalink
move request definition to submodules & parse limine memory map result
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyukai0403 committed Jan 26, 2025
1 parent 23dd94c commit 5d420f7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
9 changes: 7 additions & 2 deletions kernel/src/acpi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::mm::phys_to_virt;
use crate::println;
use limine::response::RsdpResponse;
use limine::request::RsdpRequest;
use spin::Mutex;

#[used]
#[link_section = ".requests"]
static RSDP_REQUEST: RsdpRequest = RsdpRequest::new();

#[derive(Debug, Copy, Clone)]
#[repr(packed)]
pub struct RSDP {
Expand Down Expand Up @@ -131,8 +135,9 @@ pub static mcfg: Mutex<MCFG> = Mutex::new(MCFG {
},
});

pub unsafe fn init(res: &RsdpResponse) {
pub unsafe fn init() {
println!("[INFO] acpi: init() called");
let res = RSDP_REQUEST.get_response().unwrap();
let mut rsdp_lock = rsdp.lock();
let mut xsdt_lock = xsdt.lock();
*rsdp_lock = *(res.address() as *const RSDP);
Expand Down
16 changes: 3 additions & 13 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ use DoglinkOS_2nd::println;
#[link_section = ".requests"]
static BASE_REVISION: BaseRevision = BaseRevision::new();

#[used]
#[link_section = ".requests"]
static HHDM_REQUEST: HhdmRequest = HhdmRequest::new();

#[used]
#[link_section = ".requests"]
static RSDP_REQUEST: RsdpRequest = RsdpRequest::new();

/// Define the stand and end markers for Limine requests.
#[used]
#[link_section = ".requests_start_marker"]
Expand All @@ -39,10 +31,8 @@ static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
#[no_mangle]
extern "C" fn kmain() -> ! {
assert!(BASE_REVISION.is_supported());
let hhdm_response = HHDM_REQUEST.get_response().unwrap();
init_mm(&hhdm_response);
init_mm();
init_terminal();
// println!("[INFO] Loading DoglinkOS GNU/MicroFish...");
println!(r" ____ _ _ _ ___ ____ ____ _
| _ \ ___ __ _ | | (_) _ __ | | __ / _ \ / ___| |___ \ _ __ __| |
| | | | / _ \ / _` | | | | | | '_ \ | |/ / | | | | \___ \ _____ __) | | '_ \ / _` |
Expand All @@ -51,13 +41,13 @@ extern "C" fn kmain() -> ! {
|___/");
init_interrupt();
init_lapic();
let rsdp_response = RSDP_REQUEST.get_response().unwrap();
unsafe { init_acpi(&rsdp_response) };
unsafe { init_acpi() };
init_ioapic(parse_madt());
show_cpu_info();
doit();
println!("\x1b[31mCOLOR\x1b[0m");
DoglinkOS_2nd::blockdev::ramdisk::test();
DoglinkOS_2nd::mm::show_mmap();
hang();
}

Expand Down
39 changes: 35 additions & 4 deletions kernel/src/mm.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use limine::response::HhdmResponse;
use limine::request::{HhdmRequest, MemoryMapRequest};
use spin::Mutex;
use good_memory_allocator::SpinLockedAllocator;
use crate::println;

#[used]
#[link_section = ".requests"]
static HHDM_REQUEST: HhdmRequest = HhdmRequest::new();

#[used]
#[link_section = ".requests"]
static MMAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new();

#[global_allocator]
static ALLOCATOR: SpinLockedAllocator = SpinLockedAllocator::empty();

pub static offset: Mutex<u64> = Mutex::new(0);

pub fn init(res: &HhdmResponse) {
// println!("[INFO] mm: init() called");
pub fn init() {
let res = HHDM_REQUEST.get_response().unwrap();
*offset.lock() = res.offset();
let heap_address = phys_to_virt(0x10000);
// println!("[INFO] RESERVED_HEAP is at {:?}", heap_address as *const ());
unsafe {
ALLOCATOR.init(heap_address as usize, 8 * 1024 * 1024);
}
Expand All @@ -20,3 +28,26 @@ pub fn init(res: &HhdmResponse) {
pub fn phys_to_virt(addr: u64) -> u64 {
addr + *offset.lock()
}

pub fn get_entry_type_string(entry: &limine::memory_map::Entry) -> &str {
match entry.entry_type {
limine::memory_map::EntryType::USABLE => {"USABLE"},
limine::memory_map::EntryType::RESERVED => {"RESERVED"},
limine::memory_map::EntryType::ACPI_RECLAIMABLE => {"ACPI_RECLAIMABLE"},
limine::memory_map::EntryType::ACPI_NVS => {"ACPI_NVS"},
limine::memory_map::EntryType::BAD_MEMORY => {"BAD_MEMORY"},
limine::memory_map::EntryType::BOOTLOADER_RECLAIMABLE => {"BOOTLOADER_RECLAIMABLE"},
limine::memory_map::EntryType::KERNEL_AND_MODULES => {"KERNEL_AND_MODULES"},
limine::memory_map::EntryType::FRAMEBUFFER => {"FRAMEBUFFER"},
_ => {"UNK"}
}
}

pub fn show_mmap() {
let res = MMAP_REQUEST.get_response().unwrap();
for entry in res.entries() {
println!("Base: 0x{:x}, Length: 0x{:x}, Type: {}",
entry.base, entry.length,
get_entry_type_string(entry));
}
}

0 comments on commit 5d420f7

Please sign in to comment.