Skip to content

Commit e3a2fde

Browse files
refactor: use PageRangeAllocators instead of FREE_LISTs
This replaces uses of KERNEL_FREE_LIST with PageAlloc. This replaces uses of PHYSICAL_FREE_LIST with FrameAlloc. Note that this will slow down parts of x86-64 paging right now, since it is not possible to hold a global `FrameAlloc` lock anymore. This means the lock has to be reacquired again and again until we improve the paging code or the `FrameAlloc` code. Co-authored-by: m-mueller678 <[email protected]>
1 parent d32f419 commit e3a2fde

File tree

23 files changed

+125
-139
lines changed

23 files changed

+125
-139
lines changed

src/arch/aarch64/kernel/interrupts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::drivers::mmio::get_interrupt_handlers;
2222
use crate::drivers::pci::get_interrupt_handlers;
2323
use crate::drivers::{InterruptHandlerQueue, InterruptLine};
2424
use crate::kernel::serial::handle_uart_interrupt;
25-
use crate::mm::virtualmem::KERNEL_FREE_LIST;
25+
use crate::mm::{PageAlloc, PageRangeAllocator};
2626
use crate::scheduler::{self, CoreId};
2727
use crate::{core_id, core_scheduler, env};
2828

@@ -313,7 +313,7 @@ pub(crate) fn init() {
313313
);
314314

315315
let layout = PageLayout::from_size_align(gicd_size.try_into().unwrap(), 0x10000).unwrap();
316-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
316+
let page_range = PageAlloc::allocate(layout).unwrap();
317317
let gicd_address = VirtAddr::from(page_range.start());
318318
debug!("Mapping GIC Distributor interface to virtual address {gicd_address:p}");
319319

@@ -327,7 +327,7 @@ pub(crate) fn init() {
327327
);
328328

329329
let layout = PageLayout::from_size_align(gicr_size.try_into().unwrap(), 0x10000).unwrap();
330-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
330+
let page_range = PageAlloc::allocate(layout).unwrap();
331331
let gicr_address = VirtAddr::from(page_range.start());
332332
debug!("Mapping generic interrupt controller to virtual address {gicr_address:p}");
333333
paging::map::<BasePageSize>(

src/arch/aarch64/kernel/pci.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use pci_types::{
1212
use crate::arch::aarch64::kernel::interrupts::GIC;
1313
use crate::arch::aarch64::mm::paging::{self, BasePageSize, PageSize, PageTableEntryFlags};
1414
use crate::drivers::pci::{PCI_DEVICES, PciDevice};
15-
use crate::mm::virtualmem::KERNEL_FREE_LIST;
15+
use crate::mm::{PageAlloc, PageRangeAllocator};
1616
use crate::{core_id, env};
1717

1818
const PCI_MAX_DEVICE_NUMBER: u8 = 32;
@@ -225,7 +225,7 @@ pub fn init() {
225225
let size = u64::try_from(reg.size.unwrap()).unwrap();
226226

227227
let layout = PageLayout::from_size_align(size.try_into().unwrap(), 0x1000_0000).unwrap();
228-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
228+
let page_range = PageAlloc::allocate(layout).unwrap();
229229
let pci_address = VirtAddr::from(page_range.start());
230230
info!(
231231
"Mapping PCI Enhanced Configuration Space interface to virtual address {pci_address:p} (size {size:#X})"

src/arch/aarch64/kernel/scheduler.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use memory_addresses::arch::aarch64::{PhysAddr, VirtAddr};
1111
use crate::arch::aarch64::kernel::CURRENT_STACK_ADDRESS;
1212
use crate::arch::aarch64::kernel::core_local::core_scheduler;
1313
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
14-
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
15-
use crate::mm::virtualmem::KERNEL_FREE_LIST;
14+
use crate::mm::{FrameAlloc, PageAlloc, PageRangeAllocator};
1615
#[cfg(target_os = "none")]
1716
use crate::scheduler::PerCoreSchedulerExt;
1817
use crate::scheduler::task::{Task, TaskFrame};
@@ -128,12 +127,10 @@ impl TaskStacks {
128127
};
129128
let total_size = user_stack_size + DEFAULT_STACK_SIZE;
130129
let layout = PageLayout::from_size(total_size + 3 * BasePageSize::SIZE as usize).unwrap();
131-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
130+
let page_range = PageAlloc::allocate(layout).unwrap();
132131
let virt_addr = VirtAddr::from(page_range.start());
133132
let frame_layout = PageLayout::from_size(total_size).unwrap();
134-
let frame_range = PHYSICAL_FREE_LIST
135-
.lock()
136-
.allocate(frame_layout)
133+
let frame_range = FrameAlloc::allocate(frame_layout)
137134
.expect("Failed to allocate Physical Memory for TaskStacks");
138135
let phys_addr = PhysAddr::from(frame_range.start());
139136

@@ -238,14 +235,14 @@ impl Drop for TaskStacks {
238235
)
239236
.unwrap();
240237
unsafe {
241-
KERNEL_FREE_LIST.lock().deallocate(range).unwrap();
238+
PageAlloc::deallocate(range);
242239
}
243240

244241
let range =
245242
PageRange::from_start_len(stacks.phys_addr.as_usize(), stacks.total_size)
246243
.unwrap();
247244
unsafe {
248-
PHYSICAL_FREE_LIST.lock().deallocate(range).unwrap();
245+
FrameAlloc::deallocate(range);
249246
}
250247
}
251248
}

src/arch/aarch64/kernel/systemtime.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use time::OffsetDateTime;
1212

1313
use crate::arch::aarch64::mm::paging::{self, BasePageSize, PageSize, PageTableEntryFlags};
1414
use crate::env;
15-
use crate::mm::virtualmem;
16-
use crate::mm::virtualmem::KERNEL_FREE_LIST;
15+
use crate::mm::{PageAlloc, PageRangeAllocator};
1716

1817
static PL031_ADDRESS: OnceCell<VirtAddr> = OnceCell::new();
1918
static BOOT_TIME: OnceCell<u64> = OnceCell::new();
@@ -66,7 +65,7 @@ pub fn init() {
6665
debug!("Found RTC at {addr:p} (size {size:#X})");
6766

6867
let layout = PageLayout::from_size(size.try_into().unwrap()).unwrap();
69-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
68+
let page_range = PageAlloc::allocate(layout).unwrap();
7069
let pl031_address = VirtAddr::from(page_range.start());
7170
PL031_ADDRESS.set(pl031_address).unwrap();
7271
debug!("Mapping RTC to virtual address {pl031_address:p}");

src/arch/aarch64/mm/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
pub mod paging;
22

3+
use crate::mm::{FrameAlloc, PageAlloc, PageRangeAllocator};
4+
35
pub fn init() {
46
unsafe {
57
paging::init();
68
}
7-
crate::mm::physicalmem::init();
8-
crate::mm::virtualmem::init();
9+
unsafe {
10+
FrameAlloc::init();
11+
}
12+
unsafe {
13+
PageAlloc::init();
14+
}
915
}
1016

1117
pub fn init_page_tables() {}

src/arch/aarch64/mm/paging.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use memory_addresses::{PhysAddr, VirtAddr};
1010

1111
use crate::arch::aarch64::kernel::{get_base_address, get_image_size, get_ram_address, processor};
1212
use crate::env::is_uhyve;
13-
use crate::mm::physicalmem;
14-
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
13+
use crate::mm::{FrameAlloc, PageRangeAllocator};
1514
use crate::{KERNEL_STACK_SIZE, mm, scheduler};
1615

1716
/// Pointer to the root page table (called "Level 0" in ARM terminology).
@@ -497,10 +496,8 @@ where
497496
if !self.entries[index].is_present() {
498497
// Allocate a single 4 KiB page for the new entry and mark it as a valid, writable subtable.
499498
let frame_layout = PageLayout::from_size(BasePageSize::SIZE as usize).unwrap();
500-
let frame_range = PHYSICAL_FREE_LIST
501-
.lock()
502-
.allocate(frame_layout)
503-
.expect("Unable to allocate physical memory");
499+
let frame_range =
500+
FrameAlloc::allocate(frame_layout).expect("Unable to allocate physical memory");
504501
let physical_address = PhysAddr::from(frame_range.start());
505502
self.entries[index].set(
506503
physical_address,
@@ -669,7 +666,7 @@ pub fn map_heap<S: PageSize>(virt_addr: VirtAddr, nr_pages: usize) -> Result<(),
669666
let size = (nr_pages - map_counter) * S::SIZE as usize;
670667
for i in (S::SIZE as usize..=size).rev().step_by(S::SIZE as usize) {
671668
let layout = PageLayout::from_size_align(i, S::SIZE as usize).unwrap();
672-
let frame_range = PHYSICAL_FREE_LIST.lock().allocate(layout);
669+
let frame_range = FrameAlloc::allocate(layout);
673670

674671
if let Ok(frame_range) = frame_range {
675672
let phys_addr = PhysAddr::from(frame_range.start());

src/arch/riscv64/kernel/devicetree.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,13 @@ pub fn init_drivers() {
218218
if cfg!(debug_assertions) {
219219
use free_list::PageRange;
220220

221-
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
221+
use crate::mm::{FrameAlloc, PageRangeAllocator};
222222

223223
let start = virtio_region.starting_address.addr();
224224
let len = virtio_region.size.unwrap();
225225
let frame_range = PageRange::from_start_len(start, len).unwrap();
226226

227-
PHYSICAL_FREE_LIST
228-
.lock()
229-
.allocate_at(frame_range)
230-
.unwrap_err();
227+
FrameAlloc::allocate_at(frame_range).unwrap_err();
231228
}
232229

233230
match id {

src/arch/riscv64/kernel/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::arch::riscv64::kernel::processor::lsb;
2929
use crate::config::KERNEL_STACK_SIZE;
3030
use crate::env;
3131
use crate::init_cell::InitCell;
32-
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
32+
use crate::mm::{FrameAlloc, PageRangeAllocator};
3333

3434
// Used to store information about available harts. The index of the hart in the vector
3535
// represents its CpuId and does not need to match its hart_id
@@ -162,9 +162,7 @@ pub fn boot_next_processor() {
162162
{
163163
debug!("Allocating stack for hard_id {next_hart_id}");
164164
let frame_layout = PageLayout::from_size(KERNEL_STACK_SIZE).unwrap();
165-
let frame_range = PHYSICAL_FREE_LIST
166-
.lock()
167-
.allocate(frame_layout)
165+
let frame_range = FrameAlloc::allocate(frame_layout)
168166
.expect("Failed to allocate boot stack for new core");
169167
let stack = PhysAddr::from(frame_range.start());
170168
CURRENT_STACK_ADDRESS.store(stack.as_usize() as _, Ordering::Relaxed);

src/arch/riscv64/kernel/scheduler.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use memory_addresses::{PhysAddr, VirtAddr};
77
use crate::arch::riscv64::kernel::core_local::core_scheduler;
88
use crate::arch::riscv64::kernel::processor::set_oneshot_timer;
99
use crate::arch::riscv64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags};
10-
use crate::mm::physicalmem::PHYSICAL_FREE_LIST;
11-
use crate::mm::virtualmem::KERNEL_FREE_LIST;
10+
use crate::mm::{FrameAlloc, PageAlloc, PageRangeAllocator};
1211
use crate::scheduler::task::{Task, TaskFrame};
1312
use crate::{DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE};
1413

@@ -112,12 +111,10 @@ impl TaskStacks {
112111
};
113112
let total_size = user_stack_size + DEFAULT_STACK_SIZE + KERNEL_STACK_SIZE;
114113
let layout = PageLayout::from_size(total_size + 4 * BasePageSize::SIZE as usize).unwrap();
115-
let page_range = KERNEL_FREE_LIST.lock().allocate(layout).unwrap();
114+
let page_range = PageAlloc::allocate(layout).unwrap();
116115
let virt_addr = VirtAddr::from(page_range.start());
117116
let frame_layout = PageLayout::from_size(total_size).unwrap();
118-
let frame_range = PHYSICAL_FREE_LIST
119-
.lock()
120-
.allocate(frame_layout)
117+
let frame_range = FrameAlloc::allocate(frame_layout)
121118
.expect("Failed to allocate Physical Memory for TaskStacks");
122119
let phys_addr = PhysAddr::from(frame_range.start());
123120

@@ -255,14 +252,14 @@ impl Drop for TaskStacks {
255252
)
256253
.unwrap();
257254
unsafe {
258-
KERNEL_FREE_LIST.lock().deallocate(range).unwrap();
255+
PageAlloc::deallocate(range);
259256
}
260257

261258
let range =
262259
PageRange::from_start_len(stacks.phys_addr.as_usize(), stacks.total_size)
263260
.unwrap();
264261
unsafe {
265-
PHYSICAL_FREE_LIST.lock().deallocate(range).unwrap();
262+
FrameAlloc::deallocate(range);
266263
}
267264
}
268265
}

src/arch/riscv64/mm/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
pub mod paging;
22

33
pub use self::paging::init_page_tables;
4+
use crate::mm::{FrameAlloc, PageAlloc, PageRangeAllocator};
45

56
pub fn init() {
6-
crate::mm::physicalmem::init();
7-
crate::mm::virtualmem::init();
7+
unsafe {
8+
FrameAlloc::init();
9+
}
10+
unsafe {
11+
PageAlloc::init();
12+
}
813
}

0 commit comments

Comments
 (0)