Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mm): replace freelist module with free-list crate #1182

Merged
merged 7 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ cfg-if = "1"
crossbeam-utils = { version = "0.8", default-features = false }
dyn-clone = "1.0"
fdt = "0.1"
free-list = "0.3"
hashbrown = { version = "0.14", default-features = false }
hermit-entry = { version = "0.10", features = ["kernel"] }
hermit-sync = "0.1"
Expand Down Expand Up @@ -127,6 +128,7 @@ features = [
]

[target.'cfg(target_arch = "x86_64")'.dependencies]
free-list = { version = "0.3", features = ["x86_64"] }
multiboot = "0.8"
uart_16550 = "0.3"
x86 = { version = "0.52", default-features = false }
Expand Down
49 changes: 27 additions & 22 deletions src/arch/aarch64/mm/physicalmem.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use core::alloc::AllocError;
use core::sync::atomic::{AtomicUsize, Ordering};

use free_list::{AllocError, FreeList, PageLayout, PageRange};
use hermit_sync::InterruptTicketMutex;

use crate::arch::aarch64::kernel::get_limit;
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::arch::aarch64::mm::PhysAddr;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};

static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList> =
static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
InterruptTicketMutex::new(FreeList::new());
static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0);

Expand All @@ -19,15 +18,14 @@ fn detect_from_limits() -> Result<(), ()> {
return Err(());
}

let entry = FreeListEntry {
start: mm::kernel_end_address().as_usize(),
end: limit,
};
let range = PageRange::new(mm::kernel_end_address().as_usize(), limit).unwrap();
TOTAL_MEMORY.store(
limit - mm::kernel_end_address().as_usize(),
Ordering::SeqCst,
);
PHYSICAL_FREE_LIST.lock().push(entry);
unsafe {
PHYSICAL_FREE_LIST.lock().deallocate(range).unwrap();
}

Ok(())
}
Expand All @@ -52,35 +50,41 @@ pub fn allocate(size: usize) -> Result<PhysAddr, AllocError> {
BasePageSize::SIZE
);

let layout = PageLayout::from_size(size).unwrap();

Ok(PhysAddr(
PHYSICAL_FREE_LIST
.lock()
.allocate(size, None)?
.allocate(layout)?
.start()
.try_into()
.unwrap(),
))
}

pub fn allocate_aligned(size: usize, alignment: usize) -> Result<PhysAddr, AllocError> {
pub fn allocate_aligned(size: usize, align: usize) -> Result<PhysAddr, AllocError> {
assert!(size > 0);
assert!(alignment > 0);
assert!(align > 0);
assert_eq!(
size % alignment,
size % align,
0,
"Size {size:#X} is not a multiple of the given alignment {alignment:#X}"
"Size {size:#X} is not a multiple of the given alignment {align:#X}"
);
assert_eq!(
alignment % BasePageSize::SIZE as usize,
align % BasePageSize::SIZE as usize,
0,
"Alignment {:#X} is not a multiple of {:#X}",
alignment,
align,
BasePageSize::SIZE
);

let layout = PageLayout::from_size_align(size, align).unwrap();

Ok(PhysAddr(
PHYSICAL_FREE_LIST
.lock()
.allocate(size, Some(alignment))?
.allocate(layout)?
.start()
.try_into()
.unwrap(),
))
Expand All @@ -102,13 +106,14 @@ pub fn deallocate(physical_address: PhysAddr, size: usize) {
BasePageSize::SIZE
);

PHYSICAL_FREE_LIST
.lock()
.deallocate(physical_address.as_usize(), size);
let range = PageRange::from_start_len(physical_address.as_usize(), size).unwrap();

unsafe {
PHYSICAL_FREE_LIST.lock().deallocate(range).unwrap();
}
}

pub fn print_information() {
PHYSICAL_FREE_LIST
.lock()
.print_information(" PHYSICAL MEMORY FREE LIST ");
let free_list = PHYSICAL_FREE_LIST.lock();
info!("Physical memory free list:\n{free_list}");
}
54 changes: 31 additions & 23 deletions src/arch/aarch64/mm/virtualmem.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use core::alloc::AllocError;

use free_list::{AllocError, FreeList, PageLayout, PageRange};
use hermit_sync::InterruptTicketMutex;

use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::arch::aarch64::mm::VirtAddr;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};

static KERNEL_FREE_LIST: InterruptTicketMutex<FreeList> =
static KERNEL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
InterruptTicketMutex::new(FreeList::new());

/// End of the virtual memory address space reserved for kernel memory (4 GiB).
/// This also marks the start of the virtual memory address space reserved for the task heap.
const KERNEL_VIRTUAL_MEMORY_END: VirtAddr = VirtAddr(0x1_0000_0000);

pub fn init() {
let entry = FreeListEntry {
start: mm::kernel_end_address().as_usize(),
end: KERNEL_VIRTUAL_MEMORY_END.as_usize(),
};
KERNEL_FREE_LIST.lock().push(entry);
let range = PageRange::new(
mm::kernel_end_address().as_usize(),
KERNEL_VIRTUAL_MEMORY_END.as_usize(),
)
.unwrap();
unsafe {
KERNEL_FREE_LIST.lock().deallocate(range).unwrap();
}
}

pub fn allocate(size: usize) -> Result<VirtAddr, AllocError> {
Expand All @@ -32,35 +33,41 @@ pub fn allocate(size: usize) -> Result<VirtAddr, AllocError> {
BasePageSize::SIZE
);

let layout = PageLayout::from_size(size).unwrap();

Ok(VirtAddr(
KERNEL_FREE_LIST
.lock()
.allocate(size, None)?
.allocate(layout)?
.start()
.try_into()
.unwrap(),
))
}

pub fn allocate_aligned(size: usize, alignment: usize) -> Result<VirtAddr, AllocError> {
pub fn allocate_aligned(size: usize, align: usize) -> Result<VirtAddr, AllocError> {
assert!(size > 0);
assert!(alignment > 0);
assert!(align > 0);
assert_eq!(
size % alignment,
size % align,
0,
"Size {size:#X} is not a multiple of the given alignment {alignment:#X}"
"Size {size:#X} is not a multiple of the given alignment {align:#X}"
);
assert_eq!(
alignment % BasePageSize::SIZE as usize,
align % BasePageSize::SIZE as usize,
0,
"Alignment {:#X} is not a multiple of {:#X}",
alignment,
align,
BasePageSize::SIZE
);

let layout = PageLayout::from_size_align(size, align).unwrap();

Ok(VirtAddr(
KERNEL_FREE_LIST
.lock()
.allocate(size, Some(alignment))?
.allocate(layout)?
.start()
.try_into()
.unwrap(),
))
Expand Down Expand Up @@ -91,9 +98,11 @@ pub fn deallocate(virtual_address: VirtAddr, size: usize) {
BasePageSize::SIZE
);

KERNEL_FREE_LIST
.lock()
.deallocate(virtual_address.as_usize(), size);
let range = PageRange::from_start_len(virtual_address.as_usize(), size).unwrap();

unsafe {
KERNEL_FREE_LIST.lock().deallocate(range).unwrap();
}
}

/*pub fn reserve(virtual_address: VirtAddr, size: usize) {
Expand Down Expand Up @@ -133,7 +142,6 @@ pub fn deallocate(virtual_address: VirtAddr, size: usize) {
}*/

pub fn print_information() {
KERNEL_FREE_LIST
.lock()
.print_information(" KERNEL VIRTUAL MEMORY FREE LIST ");
let free_list = KERNEL_FREE_LIST.lock();
info!("Virtual memory free list:\n{free_list}");
}
Loading