Properly round MMIO page addresses

This commit is contained in:
August 2025-10-02 10:27:28 -04:00
parent 50322be602
commit 65c36cfa60
Signed by: shibedrill
GPG Key ID: 5FE0CB25945EFAA2
2 changed files with 26 additions and 23 deletions

View File

@ -141,8 +141,6 @@ unsafe extern "C" fn main() -> ! {
log_info!("Hypervisor: {:?}", string.identify());
}
SERIAL_3F8.lock().log_write("hi");
loop {
arch::asm::nop();
}

View File

@ -27,6 +27,22 @@ lazy_static! {
.expect("Bootloader did not supply memory map");
}
pub fn round_up_to(multiple: usize, input: usize) -> usize {
if input % multiple == 0 {
input
} else {
input - (input % multiple) + multiple
}
}
fn round_down_to(multiple: usize, input: usize) -> usize {
if input % multiple == 0 {
input
} else {
input - (input % multiple)
}
}
pub fn deallocate_usable() -> Result<(), AllocError> {
init_statics();
let mut any_error: Option<AllocError> = None;
@ -58,31 +74,19 @@ pub fn deallocate_hardware() -> Result<(), AllocError> {
| (entry.entry_type == EntryType::FRAMEBUFFER)
{
if let Err(error) = {
// Special handling for regions that are too small
// TODO: fix
let adjusted_base = if (entry.length as usize) < PAGE_SIZE {
entry.base as usize - (PAGE_SIZE - entry.length as usize)
} else {
entry.base as usize
};
let adjusted_length = if (entry.length as usize) < PAGE_SIZE {
PAGE_SIZE
} else {
entry.length as usize
};
let range = PageRange::from_start_len(adjusted_base, adjusted_length);
log_trace!(
"Deallocating 0x{:x} @ 0x{:x} hardware reserved memory",
adjusted_length,
adjusted_base
);
let base: usize = round_down_to(PAGE_SIZE, entry.base as usize);
let mut length = entry.length as usize + (entry.base as usize - base);
length = round_up_to(PAGE_SIZE, length);
let range = PageRange::from_start_len(base, length);
match range {
Ok(range_inner) => unsafe { HW_FREELIST.lock().deallocate(range_inner) },
Err(err) => {
log_error!(
"Failed to convert range 0x{:x} @ 0x{:x}: {}",
adjusted_length,
adjusted_base,
"Failed to convert range 0x{:x} @ 0x{:x} (original: 0x{:x} @ 0x{:x}): {}",
length,
base,
entry.length,
entry.base,
err
);
Ok(())
@ -170,6 +174,7 @@ pub fn log_memory() {
);
log_trace!("Deallocating available memory...");
let _ = deallocate_usable();
log_trace!("Deallocating hardware reserved memory...");
let _ = deallocate_hardware();
let free_bytes = { FREELIST.lock().free_space() };
log_info!(