2024-07-07 14:37:21 -05:00
|
|
|
use crate::{
|
|
|
|
bootinfo::BOOTINFO,
|
|
|
|
println,
|
|
|
|
virtual_memory::{AsVirt, PHYS_OFFSET},
|
|
|
|
};
|
2023-09-29 15:44:53 -05:00
|
|
|
use bootloader_api::info::MemoryRegionKind;
|
2024-07-14 10:45:40 -05:00
|
|
|
use cast::{u64, usize};
|
2024-07-07 08:01:52 -05:00
|
|
|
use core::{alloc::Layout, ptr::NonNull};
|
|
|
|
use derive_try_from_primitive::TryFromPrimitive;
|
2024-07-14 10:45:40 -05:00
|
|
|
use humansize::{SizeFormatter, BINARY};
|
2024-07-07 08:01:52 -05:00
|
|
|
use linked_list_allocator::hole::HoleList;
|
2022-11-01 07:24:50 -05:00
|
|
|
use spin::{Lazy, Mutex};
|
|
|
|
use x86_64::{
|
2024-08-12 13:19:06 -05:00
|
|
|
structures::paging::{
|
|
|
|
frame::PhysFrameRange, FrameAllocator, FrameDeallocator, PhysFrame, Size4KiB,
|
|
|
|
},
|
2022-11-01 07:24:50 -05:00
|
|
|
PhysAddr,
|
|
|
|
};
|
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
#[repr(u32)]
|
|
|
|
#[derive(Copy, Clone, Debug, TryFromPrimitive)]
|
|
|
|
enum EfiMemoryTypes {
|
|
|
|
Reserved,
|
|
|
|
LoaderCode,
|
|
|
|
LoaderData,
|
|
|
|
BootServicesCode,
|
|
|
|
BootServicesData,
|
|
|
|
RuntimeServicesCode,
|
|
|
|
RuntimeServicesData,
|
|
|
|
Conventional,
|
|
|
|
Unusable,
|
|
|
|
ACPIReclaim,
|
|
|
|
ACPIMemoryNVS,
|
2024-07-14 10:45:40 -05:00
|
|
|
Mmio,
|
|
|
|
MmioPortSpace,
|
2024-07-07 08:01:52 -05:00
|
|
|
PalCode,
|
|
|
|
Persistent,
|
2024-07-14 10:45:40 -05:00
|
|
|
Invalid = u32::MAX,
|
2024-07-07 08:01:52 -05:00
|
|
|
}
|
2022-11-01 07:24:50 -05:00
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
pub struct PhysicalMemory {
|
|
|
|
alloc: HoleList,
|
2024-07-14 10:45:40 -05:00
|
|
|
bytes_used: usize,
|
2024-08-12 13:19:06 -05:00
|
|
|
peak_used: usize,
|
2022-11-01 07:24:50 -05:00
|
|
|
}
|
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
unsafe impl Send for PhysicalMemory {}
|
|
|
|
unsafe impl Sync for PhysicalMemory {}
|
|
|
|
|
|
|
|
impl PhysicalMemory {
|
|
|
|
pub fn print_stats(&self) {
|
2024-08-12 13:19:06 -05:00
|
|
|
println!(
|
|
|
|
"[PMM] {} currently allocated ({})",
|
|
|
|
SizeFormatter::new(self.bytes_used, BINARY),
|
|
|
|
self.bytes_used / 4096,
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"[PMM] {} allocated at peak ({})",
|
|
|
|
SizeFormatter::new(self.peak_used, BINARY),
|
|
|
|
self.peak_used / 4096,
|
|
|
|
);
|
2022-11-01 07:24:50 -05:00
|
|
|
}
|
2024-07-07 08:01:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const FRAME_LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(4096, 4096) };
|
2022-11-01 07:24:50 -05:00
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
unsafe impl FrameAllocator<Size4KiB> for PhysicalMemory {
|
|
|
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
2024-07-14 10:45:40 -05:00
|
|
|
self.bytes_used = self.bytes_used.saturating_add(4096);
|
2024-08-12 13:19:06 -05:00
|
|
|
if self.bytes_used > self.peak_used {
|
|
|
|
self.peak_used = self.bytes_used;
|
|
|
|
}
|
2024-07-07 08:01:52 -05:00
|
|
|
self.alloc.allocate_first_fit(FRAME_LAYOUT).ok().map(|(ptr, _)| {
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(clippy::unwrap_used, reason = "PhysFrame requires its argument to be 4k aligned, which is guaranteed by the allocator")]
|
|
|
|
#[expect(clippy::arithmetic_side_effects, reason = "All addresses passed to the allocator are > PHYS_OFFSET, so this cannot underflow")]
|
2024-07-07 08:01:52 -05:00
|
|
|
PhysFrame::from_start_address(PhysAddr::new(
|
2024-07-14 10:45:40 -05:00
|
|
|
(u64(ptr.as_ptr().expose_provenance())) - PHYS_OFFSET.as_u64(),
|
2024-07-07 08:01:52 -05:00
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
2022-11-01 07:24:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
impl FrameDeallocator<Size4KiB> for PhysicalMemory {
|
2022-11-01 07:24:50 -05:00
|
|
|
unsafe fn deallocate_frame(&mut self, frame: PhysFrame) {
|
2024-07-14 10:45:40 -05:00
|
|
|
self.bytes_used = self.bytes_used.saturating_sub(4096);
|
|
|
|
#[expect(
|
|
|
|
clippy::unwrap_used,
|
|
|
|
reason = "The virtual mapping for the frame will never be null as the physical mapping offset is in kernel space"
|
|
|
|
)]
|
|
|
|
unsafe {
|
|
|
|
self.alloc.deallocate(NonNull::new(frame.as_virt_ptr()).unwrap(), FRAME_LAYOUT)
|
|
|
|
};
|
2022-11-01 07:24:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 13:19:06 -05:00
|
|
|
impl PhysicalMemory {
|
|
|
|
pub fn allocate_frame_range(&mut self, len: usize) -> Option<PhysFrameRange> {
|
|
|
|
self.bytes_used = self.bytes_used.saturating_add(4096 * len);
|
|
|
|
if self.bytes_used > self.peak_used {
|
|
|
|
self.peak_used = self.bytes_used;
|
|
|
|
}
|
|
|
|
if len >= 0x10_0000_0000_0000 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
#[expect(clippy::arithmetic_side_effects, reason = "The check above makes sure the multiplication cannot overflow.")]
|
|
|
|
#[expect(clippy::unwrap_used, reason = "4096 is a nonzero power of two, and len is already a multiple of 4096 so rounding cannot overflow.")]
|
|
|
|
self.alloc.allocate_first_fit(Layout::from_size_align(4096 * len, 4096).unwrap()).ok().map(|(ptr, _)| {
|
|
|
|
#[expect(clippy::unwrap_used, reason = "PhysFrame requires its argument to be 4k aligned, which is guaranteed by the allocator")]
|
|
|
|
#[expect(clippy::arithmetic_side_effects, reason = "All addresses passed to the allocator are > PHYS_OFFSET, so this cannot underflow")]
|
|
|
|
let start = PhysFrame::from_start_address(PhysAddr::new(
|
|
|
|
(u64(ptr.as_ptr().expose_provenance())) - PHYS_OFFSET.as_u64(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
#[expect(clippy::unwrap_used, reason = "PhysFrame requires its argument to be 4k aligned, which is guaranteed by the allocator")]
|
|
|
|
#[expect(clippy::arithmetic_side_effects, reason = "All addresses passed to the allocator are > PHYS_OFFSET, so this cannot underflow")]
|
|
|
|
let end = PhysFrame::from_start_address(PhysAddr::new(
|
|
|
|
(u64(ptr.as_ptr().expose_provenance()) + (u64(len) * 4096)) - PHYS_OFFSET.as_u64(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
PhysFrameRange {
|
|
|
|
start,
|
|
|
|
end
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 08:01:52 -05:00
|
|
|
pub static PHYSICAL_MEMORY: Lazy<Mutex<PhysicalMemory>> = Lazy::new(|| {
|
|
|
|
println!("[PMM] Bootloader reports the following regions:");
|
|
|
|
let mut region_iter = BOOTINFO.memory_regions.iter().peekable();
|
|
|
|
let mut total_mem = 0;
|
|
|
|
let mut usable_mem = 0;
|
|
|
|
let mut alloc = HoleList::empty();
|
2024-07-14 10:45:40 -05:00
|
|
|
while let Some(&(mut region)) = region_iter.next() {
|
|
|
|
while let Some(next_region) = region_iter.peek() {
|
|
|
|
if (next_region.kind == region.kind) && (next_region.start == region.end) {
|
|
|
|
region.end = next_region.end;
|
|
|
|
region_iter.next();
|
2024-07-07 08:01:52 -05:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-07-14 10:45:40 -05:00
|
|
|
let fmtd_size = SizeFormatter::new(region.end - region.start, BINARY);
|
2024-07-07 08:01:52 -05:00
|
|
|
if let MemoryRegionKind::UnknownUefi(efi_type) = region.kind {
|
|
|
|
println!(
|
2024-07-14 10:45:40 -05:00
|
|
|
"[PMM] Efi{:?}: {:#x} - {:#x} ({})",
|
|
|
|
EfiMemoryTypes::try_from(efi_type).unwrap_or(EfiMemoryTypes::Invalid),
|
2024-07-07 08:01:52 -05:00
|
|
|
region.start,
|
|
|
|
region.end,
|
|
|
|
fmtd_size,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!(
|
2024-07-14 10:45:40 -05:00
|
|
|
"[PMM] {:?}: {:#x} - {:#x} ({})",
|
|
|
|
region.kind, region.start, region.end, fmtd_size
|
2024-07-07 08:01:52 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
total_mem += region.end - region.start;
|
|
|
|
if region.kind == MemoryRegionKind::Usable {
|
2024-07-14 10:45:40 -05:00
|
|
|
region.end &= !(0xFFF);
|
2024-07-07 08:01:52 -05:00
|
|
|
if region.start & 0xFFF != 0 {
|
|
|
|
region.start = (region.start & !(0xFFF)) + 0x1000;
|
|
|
|
}
|
|
|
|
usable_mem += region.end - region.start;
|
|
|
|
unsafe {
|
|
|
|
alloc.deallocate(
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(clippy::unwrap_used, reason = "The virtual mapping for the frame will never be null as the physical mapping offset is in kernel space")]
|
2024-07-07 08:01:52 -05:00
|
|
|
NonNull::new(PhysAddr::new(region.start).as_virt_ptr()).unwrap(),
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(clippy::unwrap_used, reason = "
|
|
|
|
from_size_align requires align to be a nonzero power of two, which it is.
|
|
|
|
Also, size must be less than isize when rounded up to a multiple of align.
|
|
|
|
Since size is already a multiple of align due to start and end being page aligned, no overflow will occur.
|
|
|
|
")]
|
|
|
|
Layout::from_size_align(usize(region.end - region.start), 4096).unwrap(),
|
2024-07-07 08:01:52 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-11-01 07:24:50 -05:00
|
|
|
}
|
2024-07-07 08:01:52 -05:00
|
|
|
println!(
|
2024-07-14 10:45:40 -05:00
|
|
|
"[PMM] Initialized, found {} of usable memory, {} total",
|
|
|
|
SizeFormatter::new(usable_mem, BINARY),
|
|
|
|
SizeFormatter::new(total_mem, BINARY),
|
2024-07-07 08:01:52 -05:00
|
|
|
);
|
2024-08-12 13:19:06 -05:00
|
|
|
Mutex::new(PhysicalMemory { alloc, bytes_used: 0, peak_used: 0 })
|
2022-11-01 07:24:50 -05:00
|
|
|
});
|