Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fa3d640

Browse files
Rename PageAllocator module
1 parent f6190f8 commit fa3d640

File tree

13 files changed

+177
-176
lines changed

13 files changed

+177
-176
lines changed

‎14_virtual_mem_part2_mmio_remap/README.md

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub unsafe fn kernel_map_mmio(
279279
// omitted
280280

281281
let virt_region =
282-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
282+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
283283

284284
kernel_map_at_unchecked(
285285
name,
@@ -296,10 +296,11 @@ pub unsafe fn kernel_map_mmio(
296296
}
297297
```
298298

299-
This allocator is defined and implemented in the added file `src/memory/mmu/alloc.rs`. Like other
300-
parts of the mapping code, its implementation makes use of the newly introduced `PageAddress<ATYPE>`
301-
and `MemoryRegion<ATYPE>` types (in [`src/memory/mmu/types.rs`](kernel/src/memory/mmu/types.rs)),
302-
but apart from that is rather straight forward. Therefore, it won't be covered in details here.
299+
This allocator is defined and implemented in the added file `src/memory/mmu/paeg_alloc.rs`. Like
300+
other parts of the mapping code, its implementation makes use of the newly introduced
301+
`PageAddress<ATYPE>` and `MemoryRegion<ATYPE>` types (in
302+
[`src/memory/mmu/types.rs`](kernel/src/memory/mmu/types.rs)), but apart from that is rather straight
303+
forward. Therefore, it won't be covered in details here.
303304

304305
The more interesting question is: How does the allocator get to learn which VAs it can use?
305306

@@ -313,7 +314,7 @@ been turned on.
313314
fn kernel_init_mmio_va_allocator() {
314315
let region = bsp::memory::mmu::virt_mmio_remap_region();
315316

316-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
317+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
317318
}
318319
```
319320

@@ -2227,81 +2228,6 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/main.rs 14_virtual_mem_
22272228
let (_, privilege_level) = exception::current_privilege_level();
22282229
info!("Current privilege level: {}", privilege_level);
22292230

2230-
diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/alloc.rs 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/alloc.rs
2231-
--- 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/alloc.rs
2232-
+++ 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/alloc.rs
2233-
@@ -0,0 +1,70 @@
2234-
+// SPDX-License-Identifier: MIT OR Apache-2.0
2235-
+//
2236-
+// Copyright (c) 2021-2022 Andre Richter <andre.o.richter@gmail.com>
2237-
+
2238-
+//! Allocation.
2239-
+
2240-
+use super::MemoryRegion;
2241-
+use crate::{
2242-
+ memory::{AddressType, Virtual},
2243-
+ synchronization::IRQSafeNullLock,
2244-
+ warn,
2245-
+};
2246-
+use core::num::NonZeroUsize;
2247-
+
2248-
+//--------------------------------------------------------------------------------------------------
2249-
+// Public Definitions
2250-
+//--------------------------------------------------------------------------------------------------
2251-
+
2252-
+/// A page allocator that can be lazyily initialized.
2253-
+pub struct PageAllocator<ATYPE: AddressType> {
2254-
+ pool: Option<MemoryRegion<ATYPE>>,
2255-
+}
2256-
+
2257-
+//--------------------------------------------------------------------------------------------------
2258-
+// Global instances
2259-
+//--------------------------------------------------------------------------------------------------
2260-
+
2261-
+static KERNEL_MMIO_VA_ALLOCATOR: IRQSafeNullLock<PageAllocator<Virtual>> =
2262-
+ IRQSafeNullLock::new(PageAllocator::new());
2263-
+
2264-
+//--------------------------------------------------------------------------------------------------
2265-
+// Public Code
2266-
+//--------------------------------------------------------------------------------------------------
2267-
+
2268-
+/// Return a reference to the kernel's MMIO virtual address allocator.
2269-
+pub fn kernel_mmio_va_allocator() -> &'static IRQSafeNullLock<PageAllocator<Virtual>> {
2270-
+ &KERNEL_MMIO_VA_ALLOCATOR
2271-
+}
2272-
+
2273-
+impl<ATYPE: AddressType> PageAllocator<ATYPE> {
2274-
+ /// Create an instance.
2275-
+ pub const fn new() -> Self {
2276-
+ Self { pool: None }
2277-
+ }
2278-
+
2279-
+ /// Initialize the allocator.
2280-
+ pub fn initialize(&mut self, pool: MemoryRegion<ATYPE>) {
2281-
+ if self.pool.is_some() {
2282-
+ warn!("Already initialized");
2283-
+ return;
2284-
+ }
2285-
+
2286-
+ self.pool = Some(pool);
2287-
+ }
2288-
+
2289-
+ /// Allocate a number of pages.
2290-
+ pub fn alloc(
2291-
+ &mut self,
2292-
+ num_requested_pages: NonZeroUsize,
2293-
+ ) -> Result<MemoryRegion<ATYPE>, &'static str> {
2294-
+ if self.pool.is_none() {
2295-
+ return Err("Allocator not initialized");
2296-
+ }
2297-
+
2298-
+ self.pool
2299-
+ .as_mut()
2300-
+ .unwrap()
2301-
+ .take_first_n_pages(num_requested_pages)
2302-
+ }
2303-
+}
2304-
23052231
diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/mapping_record.rs 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/mapping_record.rs
23062232
--- 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/mapping_record.rs
23072233
+++ 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/mapping_record.rs
@@ -2540,6 +2466,81 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/mapping_reco
25402466
+ KERNEL_MAPPING_RECORD.read(|mr| mr.print());
25412467
+}
25422468

2469+
diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/page_alloc.rs 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/page_alloc.rs
2470+
--- 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/page_alloc.rs
2471+
+++ 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/page_alloc.rs
2472+
@@ -0,0 +1,70 @@
2473+
+// SPDX-License-Identifier: MIT OR Apache-2.0
2474+
+//
2475+
+// Copyright (c) 2021-2022 Andre Richter <andre.o.richter@gmail.com>
2476+
+
2477+
+//! Page allocation.
2478+
+
2479+
+use super::MemoryRegion;
2480+
+use crate::{
2481+
+ memory::{AddressType, Virtual},
2482+
+ synchronization::IRQSafeNullLock,
2483+
+ warn,
2484+
+};
2485+
+use core::num::NonZeroUsize;
2486+
+
2487+
+//--------------------------------------------------------------------------------------------------
2488+
+// Public Definitions
2489+
+//--------------------------------------------------------------------------------------------------
2490+
+
2491+
+/// A page allocator that can be lazyily initialized.
2492+
+pub struct PageAllocator<ATYPE: AddressType> {
2493+
+ pool: Option<MemoryRegion<ATYPE>>,
2494+
+}
2495+
+
2496+
+//--------------------------------------------------------------------------------------------------
2497+
+// Global instances
2498+
+//--------------------------------------------------------------------------------------------------
2499+
+
2500+
+static KERNEL_MMIO_VA_ALLOCATOR: IRQSafeNullLock<PageAllocator<Virtual>> =
2501+
+ IRQSafeNullLock::new(PageAllocator::new());
2502+
+
2503+
+//--------------------------------------------------------------------------------------------------
2504+
+// Public Code
2505+
+//--------------------------------------------------------------------------------------------------
2506+
+
2507+
+/// Return a reference to the kernel's MMIO virtual address allocator.
2508+
+pub fn kernel_mmio_va_allocator() -> &'static IRQSafeNullLock<PageAllocator<Virtual>> {
2509+
+ &KERNEL_MMIO_VA_ALLOCATOR
2510+
+}
2511+
+
2512+
+impl<ATYPE: AddressType> PageAllocator<ATYPE> {
2513+
+ /// Create an instance.
2514+
+ pub const fn new() -> Self {
2515+
+ Self { pool: None }
2516+
+ }
2517+
+
2518+
+ /// Initialize the allocator.
2519+
+ pub fn initialize(&mut self, pool: MemoryRegion<ATYPE>) {
2520+
+ if self.pool.is_some() {
2521+
+ warn!("Already initialized");
2522+
+ return;
2523+
+ }
2524+
+
2525+
+ self.pool = Some(pool);
2526+
+ }
2527+
+
2528+
+ /// Allocate a number of pages.
2529+
+ pub fn alloc(
2530+
+ &mut self,
2531+
+ num_requested_pages: NonZeroUsize,
2532+
+ ) -> Result<MemoryRegion<ATYPE>, &'static str> {
2533+
+ if self.pool.is_none() {
2534+
+ return Err("Allocator not initialized");
2535+
+ }
2536+
+
2537+
+ self.pool
2538+
+ .as_mut()
2539+
+ .unwrap()
2540+
+ .take_first_n_pages(num_requested_pages)
2541+
+ }
2542+
+}
2543+
25432544
diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/translation_table.rs 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/translation_table.rs
25442545
--- 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu/translation_table.rs
25452546
+++ 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/translation_table.rs
@@ -3037,8 +3038,8 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu.rs 14_virtua
30373038
#[path = "../_arch/aarch64/memory/mmu.rs"]
30383039
mod arch_mmu;
30393040

3040-
+mod alloc;
30413041
+mod mapping_record;
3042+
+mod page_alloc;
30423043
mod translation_table;
30433044
+mod types;
30443045

@@ -3140,7 +3141,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu.rs 14_virtua
31403141
+fn kernel_init_mmio_va_allocator() {
31413142
+ let region = bsp::memory::mmu::virt_mmio_remap_region();
31423143
+
3143-
+ alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
3144+
+ page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
31443145
+}
31453146
+
31463147
+/// Map a region in the kernel's translation tables.
@@ -3280,7 +3281,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu.rs 14_virtua
32803281
- "PX"
32813282
- };
32823283
+ let virt_region =
3283-
+ alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
3284+
+ page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
32843285

32853286
- write!(
32863287
- f,
@@ -3399,7 +3400,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/kernel/src/memory/mmu.rs 14_virtua
33993400
+ let phys_region = MemoryRegion::new(phys_start_page_addr, phys_end_exclusive_page_addr);
34003401
+
34013402
+ let num_pages = NonZeroUsize::new(phys_region.num_pages()).unwrap();
3402-
+ let virt_region = alloc::kernel_mmio_va_allocator()
3403+
+ let virt_region = page_alloc::kernel_mmio_va_allocator()
34033404
+ .lock(|allocator| allocator.alloc(num_pages))
34043405
+ .unwrap();
34053406

‎14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#[path = "../_arch/aarch64/memory/mmu.rs"]
99
mod arch_mmu;
1010

11-
mod alloc;
1211
mod mapping_record;
12+
mod page_alloc;
1313
mod translation_table;
1414
mod types;
1515

@@ -81,7 +81,7 @@ use translation_table::interface::TranslationTable;
8181
fn kernel_init_mmio_va_allocator() {
8282
let region = bsp::memory::mmu::virt_mmio_remap_region();
8383

84-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
84+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
8585
}
8686

8787
/// Map a region in the kernel's translation tables.
@@ -205,7 +205,7 @@ pub unsafe fn kernel_map_mmio(
205205
};
206206

207207
let virt_region =
208-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
208+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
209209

210210
kernel_map_at_unchecked(
211211
name,
@@ -281,7 +281,7 @@ mod tests {
281281
let phys_region = MemoryRegion::new(phys_start_page_addr, phys_end_exclusive_page_addr);
282282

283283
let num_pages = NonZeroUsize::new(phys_region.num_pages()).unwrap();
284-
let virt_region = alloc::kernel_mmio_va_allocator()
284+
let virt_region = page_alloc::kernel_mmio_va_allocator()
285285
.lock(|allocator| allocator.alloc(num_pages))
286286
.unwrap();
287287

‎14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/alloc.rs renamed to ‎14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu/page_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Copyright (c) 2021-2022 Andre Richter <andre.o.richter@gmail.com>
44

5-
//! Allocation.
5+
//! Page allocation.
66
77
use super::MemoryRegion;
88
use crate::{

‎15_virtual_mem_part3_precomputed_tables/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/kernel/src/memory/mmu.rs 15_virtual_me
16631663
- let phys_region = MemoryRegion::new(phys_start_page_addr, phys_end_exclusive_page_addr);
16641664
-
16651665
- let num_pages = NonZeroUsize::new(phys_region.num_pages()).unwrap();
1666-
- let virt_region = alloc::kernel_mmio_va_allocator()
1666+
- let virt_region = page_alloc::kernel_mmio_va_allocator()
16671667
- .lock(|allocator| allocator.alloc(num_pages))
16681668
- .unwrap();
16691669
-

‎15_virtual_mem_part3_precomputed_tables/kernel/src/memory/mmu.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#[path = "../_arch/aarch64/memory/mmu.rs"]
99
mod arch_mmu;
1010

11-
mod alloc;
1211
mod mapping_record;
12+
mod page_alloc;
1313
mod translation_table;
1414
mod types;
1515

@@ -82,7 +82,7 @@ use translation_table::interface::TranslationTable;
8282
fn kernel_init_mmio_va_allocator() {
8383
let region = bsp::memory::mmu::virt_mmio_remap_region();
8484

85-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
85+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
8686
}
8787

8888
/// Map a region in the kernel's translation tables.
@@ -203,7 +203,7 @@ pub unsafe fn kernel_map_mmio(
203203
};
204204

205205
let virt_region =
206-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
206+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
207207

208208
kernel_map_at_unchecked(
209209
name,

‎15_virtual_mem_part3_precomputed_tables/kernel/src/memory/mmu/alloc.rs renamed to ‎15_virtual_mem_part3_precomputed_tables/kernel/src/memory/mmu/page_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Copyright (c) 2021-2022 Andre Richter <andre.o.richter@gmail.com>
44

5-
//! Allocation.
5+
//! Page allocation.
66
77
use super::MemoryRegion;
88
use crate::{

‎16_virtual_mem_part4_higher_half_kernel/kernel/src/memory/mmu.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#[path = "../_arch/aarch64/memory/mmu.rs"]
99
mod arch_mmu;
1010

11-
mod alloc;
1211
mod mapping_record;
12+
mod page_alloc;
1313
mod translation_table;
1414
mod types;
1515

@@ -87,7 +87,7 @@ use translation_table::interface::TranslationTable;
8787
fn kernel_init_mmio_va_allocator() {
8888
let region = bsp::memory::mmu::virt_mmio_remap_region();
8989

90-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
90+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
9191
}
9292

9393
/// Map a region in the kernel's translation tables.
@@ -208,7 +208,7 @@ pub unsafe fn kernel_map_mmio(
208208
};
209209

210210
let virt_region =
211-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
211+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
212212

213213
kernel_map_at_unchecked(
214214
name,

‎16_virtual_mem_part4_higher_half_kernel/kernel/src/memory/mmu/alloc.rs renamed to ‎16_virtual_mem_part4_higher_half_kernel/kernel/src/memory/mmu/page_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Copyright (c) 2021-2022 Andre Richter <andre.o.richter@gmail.com>
44

5-
//! Allocation.
5+
//! Page allocation.
66
77
use super::MemoryRegion;
88
use crate::{

‎17_kernel_symbols/kernel/src/memory/mmu.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#[path = "../_arch/aarch64/memory/mmu.rs"]
99
mod arch_mmu;
1010

11-
mod alloc;
1211
mod mapping_record;
12+
mod page_alloc;
1313
mod translation_table;
1414
mod types;
1515

@@ -87,7 +87,7 @@ use translation_table::interface::TranslationTable;
8787
fn kernel_init_mmio_va_allocator() {
8888
let region = bsp::memory::mmu::virt_mmio_remap_region();
8989

90-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
90+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.initialize(region));
9191
}
9292

9393
/// Map a region in the kernel's translation tables.
@@ -208,7 +208,7 @@ pub unsafe fn kernel_map_mmio(
208208
};
209209

210210
let virt_region =
211-
alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
211+
page_alloc::kernel_mmio_va_allocator().lock(|allocator| allocator.alloc(num_pages))?;
212212

213213
kernel_map_at_unchecked(
214214
name,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /