2023-12-07 12:50:54 +02:00
|
|
|
//! Physical memory management utilities
|
|
|
|
|
2024-01-31 19:56:49 +02:00
|
|
|
use abi::{error::Error, system::SystemMemoryStats};
|
2024-02-05 11:55:50 +02:00
|
|
|
use libk_mm::{
|
|
|
|
address::{FromRaw, IntoRaw, PhysicalAddress},
|
|
|
|
phys::PhysicalMemoryRegion,
|
2023-12-10 18:52:33 +02:00
|
|
|
};
|
2024-02-05 12:35:09 +02:00
|
|
|
use libk_util::{sync::IrqSafeSpinlock, OneTimeInit};
|
2023-07-18 18:03:45 +03:00
|
|
|
|
|
|
|
use crate::{
|
2023-09-13 18:21:45 +03:00
|
|
|
arch::{Architecture, ARCHITECTURE},
|
2023-12-10 18:52:33 +02:00
|
|
|
mem::phys::reserved::is_reserved,
|
2023-07-18 18:03:45 +03:00
|
|
|
};
|
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
use self::{
|
2023-10-01 16:58:46 +03:00
|
|
|
manager::{PhysicalMemoryManager, BITMAP_WORD_SIZE, TRACKED_PAGE_LIMIT},
|
2023-09-13 18:21:45 +03:00
|
|
|
reserved::reserve_region,
|
|
|
|
};
|
2023-07-30 16:40:30 +03:00
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
// 8 * 4096 bits per page, 1 page per bit
|
|
|
|
const MEMORY_UPPER_LIMIT: PhysicalAddress = PhysicalAddress::from_raw(TRACKED_PAGE_LIMIT * 4096);
|
|
|
|
|
|
|
|
mod manager;
|
2023-07-18 18:03:45 +03:00
|
|
|
pub mod reserved;
|
2023-10-01 16:58:46 +03:00
|
|
|
|
2023-07-18 18:03:45 +03:00
|
|
|
/// Global physical memory manager
|
2023-07-22 00:39:08 +03:00
|
|
|
pub static PHYSICAL_MEMORY: OneTimeInit<IrqSafeSpinlock<PhysicalMemoryManager>> =
|
|
|
|
OneTimeInit::new();
|
2023-07-18 18:03:45 +03:00
|
|
|
|
|
|
|
/// Allocates a single physical page from the global manager
|
2023-09-13 18:21:45 +03:00
|
|
|
pub fn alloc_page() -> Result<PhysicalAddress, Error> {
|
|
|
|
PHYSICAL_MEMORY.get().lock().alloc_page()
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocates a contiguous range of physical pages from the global manager
|
2023-09-13 18:21:45 +03:00
|
|
|
pub fn alloc_pages_contiguous(count: usize) -> Result<PhysicalAddress, Error> {
|
|
|
|
PHYSICAL_MEMORY.get().lock().alloc_contiguous_pages(count)
|
|
|
|
}
|
|
|
|
|
2023-12-07 12:50:54 +02:00
|
|
|
/// Allocates a single 2MiB page of physical memory from the global manager
|
2023-09-13 18:21:45 +03:00
|
|
|
pub fn alloc_2m_page() -> Result<PhysicalAddress, Error> {
|
|
|
|
PHYSICAL_MEMORY.get().lock().alloc_2m_page()
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
|
|
|
|
2024-01-31 19:56:49 +02:00
|
|
|
/// Returns physical memory stats
|
|
|
|
pub fn stats() -> SystemMemoryStats {
|
|
|
|
PhysicalMemoryManager::stats()
|
|
|
|
}
|
|
|
|
|
2023-07-22 00:45:14 +03:00
|
|
|
/// Deallocates a physical memory page.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// `addr` must be a page-aligned physical address previously allocated by this implementation.
|
2023-09-13 18:21:45 +03:00
|
|
|
pub unsafe fn free_page(addr: PhysicalAddress) {
|
2023-07-22 00:39:08 +03:00
|
|
|
PHYSICAL_MEMORY.get().lock().free_page(addr)
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:03:45 +03:00
|
|
|
fn physical_memory_range<I: Iterator<Item = PhysicalMemoryRegion>>(
|
|
|
|
it: I,
|
2023-09-13 18:21:45 +03:00
|
|
|
) -> Option<(PhysicalAddress, PhysicalAddress)> {
|
|
|
|
let mut start = PhysicalAddress::MAX;
|
|
|
|
let mut end = PhysicalAddress::MIN;
|
2023-07-18 18:03:45 +03:00
|
|
|
|
2024-02-04 15:18:34 +02:00
|
|
|
for (reg_start, reg_end) in it.into_iter().filter_map(|r| r.clamp(MEMORY_UPPER_LIMIT)) {
|
2023-09-13 18:21:45 +03:00
|
|
|
if reg_start < start {
|
|
|
|
start = reg_start;
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
2023-09-13 18:21:45 +03:00
|
|
|
if reg_end > end {
|
|
|
|
end = reg_end;
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
if start == PhysicalAddress::MAX || end == PhysicalAddress::MIN {
|
2023-07-18 18:03:45 +03:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((start, end))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 12:50:54 +02:00
|
|
|
/// Locates a contiguous region of available physical memory within the memory region list
|
2023-10-03 10:17:13 +03:00
|
|
|
pub fn find_contiguous_region<I: Iterator<Item = PhysicalMemoryRegion>>(
|
2023-07-18 18:03:45 +03:00
|
|
|
it: I,
|
|
|
|
count: usize,
|
2023-09-13 18:21:45 +03:00
|
|
|
) -> Option<PhysicalAddress> {
|
2024-02-04 15:18:34 +02:00
|
|
|
for (reg_start, reg_end) in it.into_iter().filter_map(|r| r.clamp(MEMORY_UPPER_LIMIT)) {
|
2023-07-18 18:03:45 +03:00
|
|
|
let mut collected = 0;
|
|
|
|
let mut base_addr = None;
|
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
for addr in (reg_start..reg_end).step_by(0x1000) {
|
2023-07-18 18:03:45 +03:00
|
|
|
if is_reserved(addr) {
|
|
|
|
collected = 0;
|
|
|
|
base_addr = None;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if base_addr.is_none() {
|
|
|
|
base_addr = Some(addr);
|
|
|
|
}
|
|
|
|
collected += 1;
|
|
|
|
if collected == count {
|
|
|
|
return base_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
todo!()
|
|
|
|
}
|
2023-09-13 18:21:45 +03:00
|
|
|
//
|
2023-07-18 18:03:45 +03:00
|
|
|
/// Initializes physical memory manager from given available memory region iterator.
|
|
|
|
///
|
|
|
|
/// 1. Finds a non-reserved range to place the page tracking array.
|
|
|
|
/// 2. Adds all non-reserved pages to the manager.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller must ensure this function has not been called before and that the regions
|
|
|
|
/// are valid and actually available.
|
|
|
|
pub unsafe fn init_from_iter<I: Iterator<Item = PhysicalMemoryRegion> + Clone>(
|
|
|
|
it: I,
|
|
|
|
) -> Result<(), Error> {
|
2023-09-13 18:21:45 +03:00
|
|
|
// Map the physical memory
|
2023-07-18 18:03:45 +03:00
|
|
|
let (phys_start, phys_end) = physical_memory_range(it.clone()).unwrap();
|
|
|
|
|
2023-10-03 10:17:13 +03:00
|
|
|
reserve_region("kernel", kernel_physical_memory_region());
|
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
ARCHITECTURE.map_physical_memory(it.clone(), phys_start, phys_end)?;
|
|
|
|
|
|
|
|
let total_count = (phys_end - phys_start) / 0x1000;
|
2024-01-26 16:31:11 +02:00
|
|
|
let page_bitmap_size = (total_count + BITMAP_WORD_SIZE - 1) / (BITMAP_WORD_SIZE / 8);
|
2023-09-13 18:21:45 +03:00
|
|
|
let page_bitmap_page_count = (page_bitmap_size + 0xFFF) / 0x1000;
|
2023-07-18 18:03:45 +03:00
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
let page_bitmap_phys_base = find_contiguous_region(it.clone(), page_bitmap_page_count).unwrap();
|
2023-07-18 18:03:45 +03:00
|
|
|
|
|
|
|
reserve_region(
|
2023-09-13 18:21:45 +03:00
|
|
|
"page-bitmap",
|
2023-07-18 18:03:45 +03:00
|
|
|
PhysicalMemoryRegion {
|
2023-09-13 18:21:45 +03:00
|
|
|
base: page_bitmap_phys_base,
|
|
|
|
size: page_bitmap_page_count * 0x1000,
|
2023-07-18 18:03:45 +03:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-16 00:16:38 +02:00
|
|
|
if IntoRaw::<u64>::into_raw(phys_start) & 0x1FFFFFF != 0 {
|
2023-11-16 11:11:10 +02:00
|
|
|
todo!();
|
2023-11-16 00:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut manager =
|
|
|
|
PhysicalMemoryManager::new(page_bitmap_phys_base, phys_start.into_raw(), total_count);
|
2023-11-06 10:29:53 +02:00
|
|
|
let mut collected = 0;
|
2024-02-01 17:23:51 +02:00
|
|
|
const MAX_MEMORY: usize = 64 * 1024;
|
2023-07-30 16:40:30 +03:00
|
|
|
|
2024-02-04 15:18:34 +02:00
|
|
|
for (start, end) in it.into_iter().filter_map(|r| r.clamp(MEMORY_UPPER_LIMIT)) {
|
2023-09-13 18:21:45 +03:00
|
|
|
for page in (start..end).step_by(0x1000) {
|
2023-11-06 10:29:53 +02:00
|
|
|
if collected >= MAX_MEMORY {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:03:45 +03:00
|
|
|
if is_reserved(page) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.add_available_page(page);
|
2023-11-06 10:29:53 +02:00
|
|
|
collected += 1;
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 00:39:08 +03:00
|
|
|
PHYSICAL_MEMORY.init(IrqSafeSpinlock::new(manager));
|
2023-09-13 18:21:45 +03:00
|
|
|
|
2023-07-18 18:03:45 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-01 16:58:46 +03:00
|
|
|
|
2023-07-18 18:03:45 +03:00
|
|
|
fn kernel_physical_memory_region() -> PhysicalMemoryRegion {
|
|
|
|
extern "C" {
|
2023-08-13 21:23:58 +03:00
|
|
|
static __kernel_phys_start: u8;
|
2023-07-18 18:03:45 +03:00
|
|
|
static __kernel_size: u8;
|
|
|
|
}
|
2023-08-13 21:23:58 +03:00
|
|
|
|
2023-09-13 18:21:45 +03:00
|
|
|
let base = PhysicalAddress::from_raw(absolute_address!(__kernel_phys_start));
|
2023-07-18 18:03:45 +03:00
|
|
|
let size = absolute_address!(__kernel_size);
|
|
|
|
|
2023-08-13 21:23:58 +03:00
|
|
|
PhysicalMemoryRegion { base, size }
|
2023-07-18 18:03:45 +03:00
|
|
|
}
|
2023-12-10 20:54:15 +02:00
|
|
|
|
2024-01-02 14:01:33 +02:00
|
|
|
#[no_mangle]
|
|
|
|
fn __allocate_page() -> Result<PhysicalAddress, Error> {
|
|
|
|
alloc_page()
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:54:15 +02:00
|
|
|
#[no_mangle]
|
|
|
|
fn __allocate_contiguous_pages(count: usize) -> Result<PhysicalAddress, Error> {
|
|
|
|
alloc_pages_contiguous(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe fn __free_page(page: PhysicalAddress) {
|
|
|
|
free_page(page)
|
|
|
|
}
|