Skip to content

Commit be6cf89

Browse files
frame: make the frame allocator static not mut
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent ffa7b35 commit be6cf89

File tree

8 files changed

+55
-71
lines changed

8 files changed

+55
-71
lines changed

src/aero_kernel/src/fs/ramfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl INodeInterface for LockedRamINode {
332332
// TODO: Support shared static content ramfs file mappings.
333333
assert!(!flags.contains(MMapFlags::MAP_SHARED));
334334

335-
let private_cp: PhysFrame = unsafe { FRAME_ALLOCATOR.allocate_frame().unwrap() };
335+
let private_cp: PhysFrame = FRAME_ALLOCATOR.allocate_frame().unwrap();
336336
private_cp.as_slice_mut()[..size].copy_from_slice(&contents[offset..offset + size]);
337337

338338
Ok(private_cp)

src/aero_kernel/src/mem/alloc.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ impl Slab {
4646
}
4747

4848
fn init(&mut self) {
49-
unsafe {
50-
let frame: PhysFrame<Size4KiB> = FRAME_ALLOCATOR
51-
.allocate_frame()
52-
.expect("slab_init: failed to allocate frame");
49+
let frame: PhysFrame<Size4KiB> = FRAME_ALLOCATOR
50+
.allocate_frame()
51+
.expect("slab_init: failed to allocate frame");
5352

54-
self.first_free = frame.start_address().as_hhdm_virt().as_u64() as usize;
55-
}
53+
self.first_free = frame.start_address().as_hhdm_virt().as_u64() as usize;
5654

5755
let hdr_size = core::mem::size_of::<SlabHeader>() as u64;
5856
let aligned_hdr_size = align_up(hdr_size, self.size as u64) as usize;

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

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl LockedFrameAllocator {
5252
}
5353

5454
unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
55-
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
55+
fn allocate_frame(&self) -> Option<PhysFrame<Size4KiB>> {
5656
self.0
5757
.get()
5858
.map(|m| {
@@ -63,7 +63,7 @@ unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
6363
.unwrap_or(None)
6464
}
6565

66-
fn deallocate_frame(&mut self, frame: PhysFrame<Size4KiB>) {
66+
fn deallocate_frame(&self, frame: PhysFrame<Size4KiB>) {
6767
self.0
6868
.get()
6969
.map(|m| m.lock().deallocate_frame_inner(frame.start_address(), 0))
@@ -72,7 +72,7 @@ unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
7272
}
7373

7474
unsafe impl FrameAllocator<Size2MiB> for LockedFrameAllocator {
75-
fn allocate_frame(&mut self) -> Option<PhysFrame<Size2MiB>> {
75+
fn allocate_frame(&self) -> Option<PhysFrame<Size2MiB>> {
7676
self.0
7777
.get()
7878
.map(|m| {
@@ -83,7 +83,7 @@ unsafe impl FrameAllocator<Size2MiB> for LockedFrameAllocator {
8383
.unwrap_or(None)
8484
}
8585

86-
fn deallocate_frame(&mut self, frame: PhysFrame<Size2MiB>) {
86+
fn deallocate_frame(&self, frame: PhysFrame<Size2MiB>) {
8787
self.0
8888
.get()
8989
.map(|m| m.lock().deallocate_frame_inner(frame.start_address(), 1))
@@ -142,31 +142,29 @@ impl Iterator for RangeMemoryIter {
142142
pub enum BuddyOrdering {
143143
Size4KiB = 0,
144144
Size8KiB = 1,
145-
Size2MiB = 2,
146145
}
147146

148147
pub fn pmm_alloc(ordering: BuddyOrdering) -> PhysAddr {
149148
let ordering = ordering as usize;
150149
debug_assert!(ordering <= BUDDY_SIZE.len());
151150

152-
unsafe {
153-
let addr = super::FRAME_ALLOCATOR
154-
.0
155-
.get()
156-
.expect("pmm: frame allocator not initialized")
157-
.lock()
158-
.allocate_frame_inner(ordering)
159-
.expect("pmm: out of memory");
151+
let addr = super::FRAME_ALLOCATOR
152+
.0
153+
.get()
154+
.expect("pmm: frame allocator not initialized")
155+
.lock()
156+
.allocate_frame_inner(ordering)
157+
.expect("pmm: out of memory");
160158

161-
let virt = crate::PHYSICAL_MEMORY_OFFSET + addr.as_u64();
162-
let fill_size = BUDDY_SIZE[ordering] as usize;
163-
let slice = core::slice::from_raw_parts_mut(virt.as_mut_ptr::<u8>(), fill_size);
159+
let virt = addr.as_hhdm_virt();
164160

165-
// We always zero out memory for security reasons.
166-
slice.fill(0x00);
161+
let fill_size = BUDDY_SIZE[ordering] as usize;
162+
let slice = unsafe { core::slice::from_raw_parts_mut(virt.as_mut_ptr::<u8>(), fill_size) };
167163

168-
addr
169-
}
164+
// We always zero out memory for security reasons.
165+
slice.fill(0x00);
166+
167+
addr
170168
}
171169

172170
#[derive(Debug)]
@@ -514,14 +512,12 @@ impl GlobalFrameAllocator {
514512

515513
pub fn init_vm_frames() {
516514
VM_FRAMES.call_once(|| {
517-
let frame_count = unsafe {
518-
super::FRAME_ALLOCATOR
519-
.0
520-
.get()
521-
.unwrap()
522-
.lock_irq()
523-
.frame_count()
524-
};
515+
let frame_count = super::FRAME_ALLOCATOR
516+
.0
517+
.get()
518+
.unwrap()
519+
.lock_irq()
520+
.frame_count();
525521

526522
let mut frames = Vec::<VmFrame>::new();
527523
frames.resize_with(frame_count, VmFrame::new);
@@ -585,14 +581,12 @@ mod tests {
585581
// The frame is not mapped yet, so the ref count should be 0.
586582
assert_eq!(vm_frame.ref_count(), 0);
587583

588-
unsafe {
589-
assert!(!FRAME_ALLOCATOR
590-
.0
591-
.get()
592-
.unwrap()
593-
.lock()
594-
.is_free(frame.start_address(), 0));
595-
}
584+
assert!(!FRAME_ALLOCATOR
585+
.0
586+
.get()
587+
.unwrap()
588+
.lock()
589+
.is_free(frame.start_address(), 0));
596590

597591
unsafe { offset_table.map_to(page, frame, PageTableFlags::PRESENT) }
598592
.unwrap()
@@ -607,13 +601,11 @@ mod tests {
607601
// the frame should be deallocated.
608602
assert_eq!(vm_frame.ref_count(), 0);
609603

610-
unsafe {
611-
assert!(FRAME_ALLOCATOR
612-
.0
613-
.get()
614-
.unwrap()
615-
.lock()
616-
.is_free(frame.start_address(), 0));
617-
}
604+
assert!(FRAME_ALLOCATOR
605+
.0
606+
.get()
607+
.unwrap()
608+
.lock()
609+
.is_free(frame.start_address(), 0));
618610
}
619611
}

src/aero_kernel/src/mem/paging/mapper.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use super::{
3838
/// the `allocate_frame` method returns only unique unused frames.
3939
pub unsafe trait FrameAllocator<S: PageSize> {
4040
/// Allocate a frame of the appropriate size and return it if possible.
41-
fn allocate_frame(&mut self) -> Option<PhysFrame<S>>;
42-
fn deallocate_frame(&mut self, frame: PhysFrame<S>);
41+
fn allocate_frame(&self) -> Option<PhysFrame<S>>;
42+
fn deallocate_frame(&self, frame: PhysFrame<S>);
4343
}
4444

4545
/// An empty convencience trait that requires the `Mapper` trait for all page sizes.
@@ -886,7 +886,7 @@ impl<P: PageTableFrameMapping> PageTableWalker<P> {
886886
let created;
887887

888888
if entry.is_unused() {
889-
if let Some(frame) = unsafe { FRAME_ALLOCATOR.allocate_frame() } {
889+
if let Some(frame) = FRAME_ALLOCATOR.allocate_frame() {
890890
entry.set_frame(frame, insert_flags);
891891
created = true;
892892
} else {
@@ -1159,7 +1159,8 @@ impl<'a> OffsetPageTable<'a> {
11591159
let created;
11601160

11611161
if !entry.flags().contains(PageTableFlags::PRESENT) {
1162-
let frame = unsafe { FRAME_ALLOCATOR.allocate_frame() }
1162+
let frame = FRAME_ALLOCATOR
1163+
.allocate_frame()
11631164
.ok_or(MapToError::FrameAllocationFailed)?;
11641165

11651166
entry.set_frame(

src/aero_kernel/src/mem/paging/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use frame::LockedFrameAllocator;
3737
use crate::arch::controlregs;
3838
use crate::PHYSICAL_MEMORY_OFFSET;
3939

40-
pub static mut FRAME_ALLOCATOR: LockedFrameAllocator = LockedFrameAllocator::new_uninit();
40+
pub static FRAME_ALLOCATOR: LockedFrameAllocator = LockedFrameAllocator::new_uninit();
4141

4242
bitflags::bitflags! {
4343
/// Describes an page fault error code.
@@ -87,10 +87,7 @@ pub fn init(
8787
let active_level_4 = unsafe { active_level_4_table() };
8888
let offset_table = unsafe { OffsetPageTable::new(active_level_4, PHYSICAL_MEMORY_OFFSET) };
8989

90-
unsafe {
91-
FRAME_ALLOCATOR.init(memory_regions);
92-
}
93-
90+
FRAME_ALLOCATOR.init(memory_regions);
9491
Ok(offset_table)
9592
}
9693

src/aero_kernel/src/mem/paging/page_table.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ impl PageTableEntry {
8282

8383
if count == 0 {
8484
// No references to this frame, deallocate it.
85-
unsafe {
86-
FRAME_ALLOCATOR.deallocate_frame(
87-
PhysFrame::<Size4KiB>::containing_address(self.addr()),
88-
);
89-
}
85+
FRAME_ALLOCATOR
86+
.deallocate_frame(PhysFrame::<Size4KiB>::containing_address(self.addr()));
9087

9188
return true;
9289
}

src/aero_kernel/src/mem/vmalloc.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,9 @@ impl Vmalloc {
100100

101101
// map the pages at the allocated address.
102102
for page in page_range {
103-
let frame: PhysFrame<Size4KiB> = unsafe {
104-
FRAME_ALLOCATOR
105-
.allocate_frame()
106-
.expect("vmalloc: physical memory exhausted")
107-
};
103+
let frame: PhysFrame<Size4KiB> = FRAME_ALLOCATOR
104+
.allocate_frame()
105+
.expect("vmalloc: physical memory exhausted");
108106

109107
unsafe {
110108
offset_table.map_to(

src/aero_kernel/src/userland/vm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ impl Mapping {
374374
protection: MMapProt,
375375
) -> Result<(), MapToError<Size4KiB>> {
376376
// Allocate a new frame to hold the contents.
377-
let new_frame: PhysFrame<Size4KiB> = unsafe { FRAME_ALLOCATOR.allocate_frame() }
377+
let new_frame: PhysFrame<Size4KiB> = FRAME_ALLOCATOR
378+
.allocate_frame()
378379
.expect("map_copied: failed to allocate frame");
379380

380381
let old_slice = unsafe {

0 commit comments

Comments
 (0)