Skip to content

Commit 6e8c5e9

Browse files
frame: document the basics of buddy allocator
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 70cae5a commit 6e8c5e9

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

src/aero_kernel/src/mem/paging/frame.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ impl LockedFrameAllocator {
5454
}
5555

5656
unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
57+
#[track_caller]
5758
fn allocate_frame(&self) -> Option<PhysFrame<Size4KiB>> {
59+
// let caller = core::panic::Location::caller();
60+
// log::debug!("allocation request of 4KiB by {:?}", caller);
61+
5862
self.0
5963
.get()
6064
.map(|m| {
@@ -69,7 +73,11 @@ unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
6973
})
7074
}
7175

76+
#[track_caller]
7277
fn deallocate_frame(&self, frame: PhysFrame<Size4KiB>) {
78+
// let caller = core::panic::Location::caller();
79+
// log::debug!("deallocation request of 4KiB by {:?}", caller);
80+
7381
self.0
7482
.get()
7583
.map(|m| m.lock().deallocate_frame_inner(frame.start_address(), 0))
@@ -78,7 +86,11 @@ unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
7886
}
7987

8088
unsafe impl FrameAllocator<Size2MiB> for LockedFrameAllocator {
89+
#[track_caller]
8190
fn allocate_frame(&self) -> Option<PhysFrame<Size2MiB>> {
91+
// let caller = core::panic::Location::caller();
92+
// log::debug!("allocation request of 2MiB by {:?}", caller);
93+
8294
self.0
8395
.get()
8496
.map(|m| {
@@ -93,7 +105,11 @@ unsafe impl FrameAllocator<Size2MiB> for LockedFrameAllocator {
93105
})
94106
}
95107

108+
#[track_caller]
96109
fn deallocate_frame(&self, frame: PhysFrame<Size2MiB>) {
110+
// let caller = core::panic::Location::caller();
111+
// log::debug!("deallocation request of 2MiB by {:?}", caller);
112+
97113
self.0
98114
.get()
99115
.map(|m| m.lock().deallocate_frame_inner(frame.start_address(), 1))
@@ -230,6 +246,25 @@ const CONVENTIONAL_MEM_END: PhysAddr = unsafe { PhysAddr::new_unchecked(Size4KiB
230246

231247
static VM_FRAMES: Once<Vec<VmFrame>> = Once::new();
232248

249+
/// Buddy allocator combines power-of-two allocator with free buffer
250+
/// coalescing.
251+
///
252+
/// ## Overview
253+
///
254+
/// Overview of the buddy allocation algorithm:
255+
///
256+
/// * Memory is broken up into large blocks of pages where each block
257+
/// is a power of two number of pages.
258+
///
259+
/// * If a block of the desired size is not available, a larger block is
260+
/// broken up in half and the two blocks are marked as buddies then one half
261+
/// is used for the allocation and the other half is marked free.
262+
///
263+
/// * The blocks are continuously halved as necessary until a block of the
264+
/// desired size is available.
265+
///
266+
/// * When a block is later freed, the buddy is examined and the two coalesced
267+
/// if it is free.
233268
pub struct GlobalFrameAllocator {
234269
buddies: [&'static mut [u64]; 3],
235270
free: [usize; 3],

src/aero_kernel/src/unwind.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,23 @@ pub fn prepare_panic() {
6161
}
6262
}
6363

64+
// todo: return `ElfLoadResult` (defined in `vm.rs`) instead of returning an optional value.
65+
fn get_kernel_elf() -> Option<ElfFile<'static>> {
66+
let unwind_info = crate::UNWIND_INFO.get()?;
67+
68+
let elf_start = PhysAddr::new(unwind_info.kernel_start).as_hhdm_virt();
69+
let elf_size = unwind_info.kernel_size as usize;
70+
71+
// SAFETY: The kernel start and end addresses are valid.
72+
let elf_slice = unsafe { core::slice::from_raw_parts(elf_start.as_ptr::<u8>(), elf_size) };
73+
ElfFile::new(elf_slice).ok()
74+
}
75+
6476
pub fn unwind_stack_trace() {
6577
let mut address_space = AddressSpace::this();
6678
let offset_table = address_space.offset_page_table();
6779

68-
let unwind_info = crate::UNWIND_INFO
69-
.get()
70-
.expect("unwind: failed to retrieve the unwind information");
71-
72-
let kernel_slice: &[u8] = unsafe {
73-
core::slice::from_raw_parts(
74-
PhysAddr::new(unwind_info.kernel_start)
75-
.as_hhdm_virt()
76-
.as_ptr(),
77-
unwind_info.kernel_size as usize,
78-
)
79-
};
80-
81-
let kernel_elf = ElfFile::new(kernel_slice).expect("unwind: invalid kernel slice");
80+
let kernel_elf = get_kernel_elf().unwrap();
8281
let mut symbol_table = None;
8382

8483
for section in kernel_elf.section_iter() {

0 commit comments

Comments
 (0)