Skip to content

Commit 91f99bb

Browse files
kernel: Added brief comments for new memory management related code
Signed-off-by: Ioan-Cristian CÎRSTEA <ioan.cirstea@oxidos.io>
1 parent 0bb62f5 commit 91f99bb

18 files changed

Lines changed: 185 additions & 8 deletions

File tree

kernel/src/memory_management/allocators/allocator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Memory allocators.
6+
57
use super::super::slices::MutablePhysicalSlice;
68

79
use core::num::NonZero;
810

11+
/// Memory allocator trait.
912
pub trait Allocator<'a, Granule> {
13+
/// Allocate `count` granules of memory.
1014
fn allocate(&self, count: NonZero<usize>) -> Result<MutablePhysicalSlice<'a, Granule>, ()>;
1115
}

kernel/src/memory_management/allocators/static.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Static allocator.
6+
57
use super::allocator::Allocator;
68

79
use super::super::slices::MutablePhysicalSlice;
@@ -10,6 +12,7 @@ use crate::utilities::cells::OptionalCell;
1012

1113
use core::num::NonZero;
1214

15+
/// A static allocator, i.e. an allocator that allocates memory without freeing it ever.
1316
pub struct StaticAllocator<'a, Granule>(OptionalCell<MutablePhysicalSlice<'a, Granule>>);
1417

1518
impl<'a, Granule> StaticAllocator<'a, Granule> {

kernel/src/memory_management/configuration.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Memory configurations.
6+
57
use super::pointers::{
68
KernelVirtualPointer, PhysicalPointer, UserVirtualPointer, ValidVirtualPointer,
79
};
@@ -15,6 +17,7 @@ use crate::platform::mmu::Asid;
1517

1618
use crate::utilities::alignment::AlwaysAligned;
1719

20+
/// Memory configuration.
1821
#[repr(transparent)]
1922
pub(super) struct Configuration<'a, const IS_USER: bool, const NUMBER_REGIONS: usize, Granule> {
2023
regions: [DirtyMappedProtectedAllocatedRegion<'a, IS_USER, Granule>; NUMBER_REGIONS],
@@ -89,6 +92,7 @@ impl<'a, const IS_USER: bool, const NUMBER_REGIONS: usize, Granule>
8992
}
9093
}
9194

95+
/// Process memory configuration.
9296
pub(crate) struct ProcessConfiguration<'a, Granule> {
9397
asid: Asid,
9498
configuration: Configuration<'a, true, 2, Granule>,
@@ -184,6 +188,8 @@ impl<'a, Granule> ProcessConfiguration<'a, Granule> {
184188
}
185189
}
186190

191+
/// Valid process memory configuration, that is, it doesn't overlap kernel's
192+
/// virtual address space.
187193
#[repr(transparent)]
188194
pub struct ValidProcessConfiguration<'a, Granule>(ProcessConfiguration<'a, Granule>);
189195

@@ -265,6 +271,7 @@ const KERNEL_PROG_REGION_INDEX: usize = 1;
265271
const KERNEL_RAM_REGION_INDEX: usize = 2;
266272
//const KERNEL_PERIPHERAL_REGION_INDEX: usize = 3;
267273

274+
/// Kernel memory configuration.
268275
#[repr(transparent)]
269276
pub(crate) struct KernelConfiguration<'a, Granule>(Configuration<'a, false, 4, Granule>);
270277

kernel/src/memory_management/granules.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Memory granules.
6+
57
use crate::utilities::alignment::Alignment;
68
use crate::utilities::misc::{create_non_zero_usize, divide_non_zero_usize, modulo_non_zero_usize};
79

810
use core::num::NonZero;
911

12+
/// A memory granule.
1013
pub trait Granule: Alignment {
14+
/// The size of the granule, in bytes.
1115
const SIZE_U8: NonZero<usize>;
1216

1317
fn ceil_from_byte_count(byte_count: NonZero<usize>) -> NonZero<usize> {

kernel/src/memory_management/memory_managers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Memory managers.
6+
57
use super::allocators::Allocator as AllocatorTrait;
68
use super::configuration::{KernelConfiguration, ProcessConfiguration, ValidProcessConfiguration};
79
use super::granules::Granule as GranuleTrait;
@@ -17,6 +19,9 @@ use crate::utilities::alignment::AlwaysAligned;
1719
use core::marker::PhantomData;
1820
use core::num::NonZero;
1921

22+
/// A memory manager.
23+
///
24+
/// A memory manager currently handles only memory allocation.
2025
struct MemoryManager<'a, Granule, Allocator: AllocatorTrait<'a, Granule>> {
2126
allocator: Allocator,
2227
phantom_data: PhantomData<&'a Granule>,
@@ -40,6 +45,7 @@ impl<'a, Granule, Allocator: AllocatorTrait<'a, Granule>> MemoryManager<'a, Gran
4045
}
4146
}
4247

48+
/// Errors on process memory mapping.
4349
#[derive(Debug)]
4450
pub enum ProcessMemoryMappingError {
4551
/// The PROG memory is not a valid user virtual memory.
@@ -48,6 +54,9 @@ pub enum ProcessMemoryMappingError {
4854
InvalidRamVirtualMemory,
4955
}
5056

57+
/// Process memory manager.
58+
///
59+
/// Handles process memory allocations and process memory configurations.
5160
pub struct ProcessMemoryManager<'a, Granule, Allocator: AllocatorTrait<'a, Granule>> {
5261
memory_manager: MemoryManager<'a, Granule, Allocator>,
5362
}
@@ -79,6 +88,10 @@ impl<'a, Granule: 'a + GranuleTrait, Allocator: AllocatorTrait<'a, Granule>>
7988
}
8089
}
8190

91+
/// Kernel memory manager.
92+
///
93+
/// Validates process memory configurations and performs offline user -> kernel
94+
/// address translation.
8295
pub(crate) struct KernelMemoryManager<'a, Granule, const USING_MPU: bool> {
8396
configuration: KernelConfiguration<'a, Granule>,
8497
}

kernel/src/memory_management/pages.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Pages supported by Tock.
6+
57
use super::granules::Granule;
68

79
use crate::utilities::misc::create_non_zero_usize;
810

911
use core::num::NonZero;
1012

13+
/// A standard 4KiB page.
1114
#[repr(align(4096))]
1215
#[derive(Clone)]
1316
#[allow(dead_code)]

kernel/src/memory_management/permissions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Memory permissions.
6+
7+
/// Permissions that might be associated with a region of memory.
58
#[derive(Clone, Copy, PartialEq, Eq)]
69
pub enum Permissions {
710
ReadOnly,

kernel/src/memory_management/pointers.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
// Copyright OxidOS Automotive SRL 2025.
44

5+
//! Physical and virtual pointers.
6+
57
use crate::utilities::alignment::{Alignment, AlwaysAligned};
68
use crate::utilities::ordering::SmallerPair;
79
use crate::utilities::pointers::{
@@ -14,70 +16,105 @@ use core::num::NonZero;
1416
use core::ops::Sub;
1517
use core::ptr::NonNull;
1618

19+
/// A pointer from the perspective of the memory management system.
1720
#[repr(transparent)]
1821
pub struct Pointer<const IS_VIRTUAL: bool, const IS_MUTABLE: bool, T: Alignment>(
1922
Ptr<IS_MUTABLE, T>,
2023
);
2124

25+
/// An immutable pointer from the perspective of the memory management system.
2226
pub type ImmutablePointer<const IS_VIRTUAL: bool, T> = Pointer<IS_VIRTUAL, false, T>;
27+
/// A mutable pointer from the perspective of the memory management system.
2328
pub type MutablePointer<const IS_VIRTUAL: bool, T> = Pointer<IS_VIRTUAL, true, T>;
2429

30+
/// A physical pointer.
2531
pub type PhysicalPointer<const IS_MUTABLE: bool, T> = Pointer<false, IS_MUTABLE, T>;
32+
/// An immutable physical pointer.
2633
pub type ImmutablePhysicalPointer<T> = PhysicalPointer<false, T>;
34+
/// A mutable physical pointer.
2735
pub type MutablePhysicalPointer<T> = PhysicalPointer<true, T>;
2836

37+
/// A virtual pointer.
2938
pub type VirtualPointer<const IS_MUTABLE: bool, T> = Pointer<true, IS_MUTABLE, T>;
39+
/// An immutable virtual pointer.
3040
pub type ImmutableVirtualPointer<T> = VirtualPointer<false, T>;
41+
/// A mutable virtual pointer.
3142
pub type MutableVirtualPointer<T> = VirtualPointer<true, T>;
3243

44+
/// A valid virtual pointer, that is, a virtual pointer that belongs to the kernel or to a process.
3345
#[repr(transparent)]
3446
pub struct ValidVirtualPointer<const IS_USER: bool, const IS_MUTABLE: bool, T: Alignment>(
3547
Pointer<true, IS_MUTABLE, T>,
3648
);
3749

50+
/// A valid immutable virtual pointer.
3851
pub type ValidImmutableVirtualPointer<const IS_USER: bool, T> =
3952
ValidVirtualPointer<IS_USER, false, T>;
53+
/// A valid mutable virtual pointer.
4054
pub type ValidMutableVirtualPointer<const IS_USER: bool, T> = ValidVirtualPointer<IS_USER, true, T>;
4155

56+
/// A user virtual pointer.
4257
pub type UserVirtualPointer<const IS_MUTABLE: bool, T> = ValidVirtualPointer<true, IS_MUTABLE, T>;
58+
/// An immutable user virtual pointer.
4359
pub type ImmutableUserVirtualPointer<T> = UserVirtualPointer<false, T>;
60+
/// A mutable user virtual pointer.
4461
pub type MutableUserVirtualPointer<T> = UserVirtualPointer<true, T>;
4562

63+
/// A kernel virtual pointer.
4664
pub type KernelVirtualPointer<const IS_MUTABLE: bool, T> =
4765
ValidVirtualPointer<false, IS_MUTABLE, T>;
66+
/// An immutable kernel virtual pointer.
4867
pub type ImmutableKernelVirtualPointer<T> = KernelVirtualPointer<false, T>;
68+
/// A mutable kernel virtual pointer.
4969
pub type MutableKernelVirtualPointer<T> = KernelVirtualPointer<true, T>;
5070

71+
/// A nullable pointer from the perspective of the memory management system.
5172
pub enum NullablePointer<const IS_VIRTUAL: bool, const IS_MUTABLE: bool, T: Alignment> {
5273
Null,
5374
NonNull(Pointer<IS_VIRTUAL, IS_MUTABLE, T>),
5475
}
5576

77+
/// An immutable nullable pointer from the perspective of the memory management system.
5678
pub type ImmutableNullablePointer<const IS_VIRTUAL: bool, T> =
5779
NullablePointer<IS_VIRTUAL, false, T>;
80+
/// A mutable nullable pointer from the memory management system pe
5881
pub type MutableNullablePointer<const IS_VIRTUAL: bool, T> = NullablePointer<IS_VIRTUAL, true, T>;
5982

83+
/// A nullable physical pointer.
6084
pub type NullablePhysicalPointer<const IS_MUTABLE: bool, T> = NullablePointer<false, IS_MUTABLE, T>;
85+
/// An immutable nullable physical pointer.
6186
pub type ImmutableNullablePhysicalPointer<T> = NullablePhysicalPointer<false, T>;
87+
/// A mutable nullable physical pointer.
6288
pub type MutableNullablePhysicalPointer<T> = NullablePhysicalPointer<true, T>;
6389

90+
/// A nullable virtual pointer.
6491
pub type NullableVirtualPointer<const IS_MUTABLE: bool, T> = NullablePointer<true, IS_MUTABLE, T>;
92+
/// An immutable nullable virtual pointer.
6593
pub type ImmutableNullableVirtualPointer<T> = NullableVirtualPointer<false, T>;
94+
/// A mutable nullable virtual pointer.
6695
pub type MutableNullableVirtualPointer<T> = NullableVirtualPointer<true, T>;
6796

97+
/// A valid nullable virtual pointer, that is, a virtual pointer that belongs to the kernel or to a
98+
/// process.
6899
pub enum ValidNullableVirtualPointer<const IS_USER: bool, const IS_MUTABLE: bool, T: Alignment> {
69100
Null,
70101
NonNull(ValidVirtualPointer<IS_USER, IS_MUTABLE, T>),
71102
}
72103

104+
/// A user nullable virtual pointer.
73105
pub type UserNullableVirtualPointer<const IS_MUTABLE: bool, T> =
74106
ValidNullableVirtualPointer<true, IS_MUTABLE, T>;
107+
/// An immutable user nullable virtual pointer.
75108
pub type ImmutableUserNullableVirtualPointer<T> = UserNullableVirtualPointer<false, T>;
109+
/// A mutable user nullable virtual pointer.
76110
pub type MutableUserNullableVirtualPointer<T> = UserNullableVirtualPointer<true, T>;
77111

112+
/// A kernel nullable virtual pointer.
78113
pub type KernelNullableVirtualPointer<const IS_MUTABLE: bool, T> =
79114
ValidNullableVirtualPointer<false, IS_MUTABLE, T>;
115+
/// An immutable kernel virtual pointer.
80116
pub type ImmutableKernelNullableVirtualPointer<T> = KernelNullableVirtualPointer<false, T>;
117+
/// A mutable kernel virtual pointer.
81118
pub type MutableKernelNullableVirtualPointer<T> = KernelNullableVirtualPointer<true, T>;
82119

83120
impl<const IS_VIRTUAL: bool, const IS_MUTABLE: bool, T: Alignment>

0 commit comments

Comments
 (0)