ostd/mm/frame/
mod.rs

1// SPDX-License-Identifier: MPL-2.0
2
3//! Frame (physical memory page) management.
4//!
5//! A frame is an aligned, contiguous range of bytes in physical memory. The
6//! sizes of base frames and huge frames (that are mapped as "huge pages") are
7//! architecture-dependent. A frame can be mapped to virtual address spaces
8//! using the page table.
9//!
10//! Frames can be accessed through frame handles, namely, [`Frame`]. A frame
11//! handle is a reference-counted pointer to a frame. When all handles to a
12//! frame are dropped, the frame is released and can be reused.  Contiguous
13//! frames are managed with [`Segment`].
14//!
15//! There are various kinds of frames. The top-level grouping of frame kinds
16//! are "typed" frames and "untyped" frames. Typed frames host Rust objects
17//! that must follow the visibility, lifetime and borrow rules of Rust, thus
18//! not being able to be directly manipulated. Untyped frames are raw memory
19//! that can be manipulated directly. So only untyped frames can be
20//!  - safely shared to external entities such as device drivers or user-space
21//!    applications.
22//!  - or directly manipulated with readers and writers that neglect Rust's
23//!    "alias XOR mutability" rule.
24//!
25//! The kind of a frame is determined by the type of its metadata. Untyped
26//! frames have its metadata type that implements the [`AnyUFrameMeta`]
27//! trait, while typed frames don't.
28//!
29//! Frames can have dedicated metadata, which is implemented in the [`meta`]
30//! module. The reference count and usage of a frame are stored in the metadata
31//! as well, leaving the handle only a pointer to the metadata slot. Users
32//! can create custom metadata types by implementing the [`AnyFrameMeta`] trait.
33
34pub mod allocator;
35pub mod linked_list;
36pub mod meta;
37pub mod segment;
38pub mod unique;
39pub mod untyped;
40
41mod frame_ref;
42pub use frame_ref::FrameRef;
43
44#[cfg(ktest)]
45mod test;
46
47use core::{
48    marker::PhantomData,
49    mem::ManuallyDrop,
50    sync::atomic::{AtomicUsize, Ordering},
51};
52
53pub use allocator::GlobalFrameAllocator;
54use meta::{AnyFrameMeta, GetFrameError, MetaSlot, REF_COUNT_UNUSED, mapping};
55pub use segment::Segment;
56use untyped::{AnyUFrameMeta, UFrame};
57
58use crate::{
59    mm::{HasPaddr, HasSize, PAGE_SIZE, Paddr, PagingConsts, PagingLevel, Vaddr},
60    sync::RcuDrop,
61};
62
63static MAX_PADDR: AtomicUsize = AtomicUsize::new(0);
64
65/// Returns the maximum physical address that is tracked by frame metadata.
66pub(in crate::mm) fn max_paddr() -> Paddr {
67    let max_paddr = MAX_PADDR.load(Ordering::Relaxed) as Paddr;
68    debug_assert_ne!(max_paddr, 0);
69    max_paddr
70}
71
72/// A smart pointer to a frame.
73///
74/// A frame is a contiguous range of bytes in physical memory. The [`Frame`]
75/// type is a smart pointer to a frame that is reference-counted.
76///
77/// Frames are associated with metadata. The type of the metadata `M` is
78/// determines the kind of the frame. If `M` implements [`AnyUFrameMeta`], the
79/// frame is a untyped frame. Otherwise, it is a typed frame.
80#[repr(transparent)]
81pub struct Frame<M: AnyFrameMeta + ?Sized> {
82    ptr: *const MetaSlot,
83    _marker: PhantomData<M>,
84}
85
86unsafe impl<M: AnyFrameMeta + ?Sized> Send for Frame<M> {}
87
88unsafe impl<M: AnyFrameMeta + ?Sized> Sync for Frame<M> {}
89
90impl<M: AnyFrameMeta + ?Sized> core::fmt::Debug for Frame<M> {
91    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
92        write!(f, "Frame({:#x})", self.paddr())
93    }
94}
95
96impl<M: AnyFrameMeta + ?Sized> PartialEq for Frame<M> {
97    fn eq(&self, other: &Self) -> bool {
98        self.paddr() == other.paddr()
99    }
100}
101impl<M: AnyFrameMeta + ?Sized> Eq for Frame<M> {}
102
103impl<M: AnyFrameMeta> Frame<M> {
104    /// Gets a [`Frame`] with a specific usage from a raw, unused page.
105    ///
106    /// The caller should provide the initial metadata of the page.
107    ///
108    /// If the provided frame is not truly unused at the moment, it will return
109    /// an error. If wanting to acquire a frame that is already in use, use
110    /// [`Frame::from_in_use`] instead.
111    pub fn from_unused(paddr: Paddr, metadata: M) -> Result<Self, GetFrameError> {
112        Ok(Self {
113            ptr: MetaSlot::get_from_unused(paddr, metadata, false)?,
114            _marker: PhantomData,
115        })
116    }
117
118    /// Gets the metadata of this page.
119    pub fn meta(&self) -> &M {
120        // SAFETY: The type is tracked by the type system.
121        unsafe { &*self.slot().as_meta_ptr::<M>() }
122    }
123}
124
125impl Frame<dyn AnyFrameMeta> {
126    /// Gets a dynamically typed [`Frame`] from a raw, in-use page.
127    ///
128    /// If the provided frame is not in use at the moment, it will return an error.
129    ///
130    /// The returned frame will have an extra reference count to the frame.
131    pub fn from_in_use(paddr: Paddr) -> Result<Self, GetFrameError> {
132        Ok(Self {
133            ptr: MetaSlot::get_from_in_use(paddr)?,
134            _marker: PhantomData,
135        })
136    }
137}
138
139impl<M: AnyFrameMeta + ?Sized> Frame<M> {
140    /// Gets the map level of this page.
141    ///
142    /// This is the level of the page table entry that maps the frame,
143    /// which determines the size of the frame.
144    ///
145    /// Currently, the level is always 1, which means the frame is a regular
146    /// page frame.
147    pub const fn map_level(&self) -> PagingLevel {
148        1
149    }
150
151    /// Gets the dynamically-typed metadata of this frame.
152    ///
153    /// If the type is known at compile time, use [`Frame::meta`] instead.
154    pub fn dyn_meta(&self) -> &dyn AnyFrameMeta {
155        // SAFETY: The metadata is initialized and valid.
156        unsafe { &*self.slot().dyn_meta_ptr() }
157    }
158
159    /// Gets the reference count of the frame.
160    ///
161    /// It returns the number of all references to the frame, including all the
162    /// existing frame handles ([`Frame`], [`Frame<dyn AnyFrameMeta>`]), and all
163    /// the mappings in the page table that points to the frame.
164    ///
165    /// # Safety
166    ///
167    /// The function is safe to call, but using it requires extra care. The
168    /// reference count can be changed by other threads at any time including
169    /// potentially between calling this method and acting on the result.
170    pub fn reference_count(&self) -> u64 {
171        let refcnt = self.slot().ref_count.load(Ordering::Relaxed);
172        debug_assert!(refcnt < meta::REF_COUNT_MAX);
173        refcnt
174    }
175
176    /// Borrows a reference from the given frame.
177    pub fn borrow(&self) -> FrameRef<'_, M> {
178        // SAFETY: Both the lifetime and the type matches `self`.
179        unsafe { FrameRef::borrow_paddr(self.paddr()) }
180    }
181
182    /// Forgets the handle to the frame.
183    ///
184    /// This will result in the frame being leaked without calling the custom dropper.
185    ///
186    /// A physical address to the frame is returned in case the frame needs to be
187    /// restored using [`Frame::from_raw`] later. This is useful when some architectural
188    /// data structures need to hold the frame handle such as the page table.
189    pub(in crate::mm) fn into_raw(self) -> Paddr {
190        let this = ManuallyDrop::new(self);
191        this.paddr()
192    }
193
194    /// Restores a forgotten [`Frame`] from a physical address.
195    ///
196    /// # Safety
197    ///
198    /// The caller should only restore a `Frame` that was previously forgotten using
199    /// [`Frame::into_raw`].
200    ///
201    /// And the restoring operation should only be done once for a forgotten
202    /// [`Frame`]. Otherwise double-free will happen.
203    ///
204    /// Also, the caller ensures that the usage of the frame is correct. There's
205    /// no checking of the usage in this function.
206    pub(in crate::mm) unsafe fn from_raw(paddr: Paddr) -> Self {
207        debug_assert!(paddr < max_paddr());
208
209        let vaddr = mapping::frame_to_meta::<PagingConsts>(paddr);
210        let ptr = vaddr as *const MetaSlot;
211
212        Self {
213            ptr,
214            _marker: PhantomData,
215        }
216    }
217
218    fn slot(&self) -> &MetaSlot {
219        // SAFETY: `ptr` points to a valid `MetaSlot` that will never be
220        // mutably borrowed, so taking an immutable reference to it is safe.
221        unsafe { &*self.ptr }
222    }
223}
224
225impl<M: AnyFrameMeta + ?Sized> HasPaddr for Frame<M> {
226    fn paddr(&self) -> Paddr {
227        self.slot().frame_paddr()
228    }
229}
230
231impl<M: AnyFrameMeta + ?Sized> HasSize for Frame<M> {
232    fn size(&self) -> usize {
233        PAGE_SIZE
234    }
235}
236
237impl<M: AnyFrameMeta + ?Sized> Clone for Frame<M> {
238    fn clone(&self) -> Self {
239        // SAFETY: We have already held a reference to the frame.
240        unsafe { self.slot().inc_ref_count() };
241
242        Self {
243            ptr: self.ptr,
244            _marker: PhantomData,
245        }
246    }
247}
248
249impl<M: AnyFrameMeta + ?Sized> Drop for Frame<M> {
250    fn drop(&mut self) {
251        let last_ref_cnt = self.slot().ref_count.fetch_sub(1, Ordering::Release);
252        debug_assert!(last_ref_cnt != 0 && last_ref_cnt != REF_COUNT_UNUSED);
253
254        if last_ref_cnt == 1 {
255            // A fence is needed here with the same reasons stated in the implementation of
256            // `Arc::drop`: <https://doc.rust-lang.org/std/sync/struct.Arc.html#method.drop>.
257            core::sync::atomic::fence(Ordering::Acquire);
258
259            // SAFETY: this is the last reference and is about to be dropped.
260            unsafe { self.slot().drop_last_in_place() };
261
262            allocator::get_global_frame_allocator().dealloc(self.paddr(), PAGE_SIZE);
263        }
264    }
265}
266
267impl<M: AnyFrameMeta> TryFrom<Frame<dyn AnyFrameMeta>> for Frame<M> {
268    type Error = Frame<dyn AnyFrameMeta>;
269
270    /// Tries converting a [`Frame<dyn AnyFrameMeta>`] into the statically-typed [`Frame`].
271    ///
272    /// If the usage of the frame is not the same as the expected usage, it will
273    /// return the dynamic frame itself as is.
274    fn try_from(dyn_frame: Frame<dyn AnyFrameMeta>) -> Result<Self, Self::Error> {
275        if (dyn_frame.dyn_meta() as &dyn core::any::Any).is::<M>() {
276            // SAFETY: The metadata is coerceable and the struct is transmutable.
277            Ok(unsafe { core::mem::transmute::<Frame<dyn AnyFrameMeta>, Frame<M>>(dyn_frame) })
278        } else {
279            Err(dyn_frame)
280        }
281    }
282}
283
284impl Frame<dyn AnyFrameMeta> {
285    /// Converts a [`Frame`] with a specific metadata type into a
286    /// [`Frame<dyn AnyFrameMeta>`].
287    ///
288    /// This exists because:
289    ///
290    /// ```ignore
291    /// impl<M: AnyFrameMeta + ?Sized> From<Frame<M>> for Frame<dyn AnyFrameMeta>
292    /// ```
293    ///
294    /// will conflict with `impl<T> core::convert::From<T> for T` in crate `core`.
295    pub fn from_unsized<M: AnyFrameMeta + ?Sized>(frame: Frame<M>) -> Frame<dyn AnyFrameMeta> {
296        // SAFETY: The metadata is coerceable and the struct is transmutable.
297        unsafe { core::mem::transmute(frame) }
298    }
299
300    /// Converts an RCU-dropped [`Frame`] with a specific metadata type into a
301    /// RCU-dropped [`Frame<dyn AnyFrameMeta>`].
302    ///
303    /// See also [`Frame::from_unsized`] for the reason of why not implementing
304    /// [`From`] directly.
305    pub fn rcu_from_unsized<M: AnyFrameMeta + ?Sized>(frame: RcuDrop<Frame<M>>) -> RcuDrop<Self> {
306        // SAFETY: The resulting frame will be dropped after the RCU grace period.
307        let (frame, panic_guard) = unsafe { RcuDrop::into_inner(frame) };
308        let dyn_frame = Self::from_unsized(frame);
309        panic_guard.forget();
310        RcuDrop::new(dyn_frame)
311    }
312}
313
314impl<M: AnyFrameMeta> From<Frame<M>> for Frame<dyn AnyFrameMeta> {
315    fn from(frame: Frame<M>) -> Self {
316        Self::from_unsized(frame)
317    }
318}
319
320impl<M: AnyUFrameMeta> From<Frame<M>> for UFrame {
321    fn from(frame: Frame<M>) -> Self {
322        // SAFETY: The metadata is coerceable and the struct is transmutable.
323        unsafe { core::mem::transmute(frame) }
324    }
325}
326
327impl From<UFrame> for Frame<dyn AnyFrameMeta> {
328    fn from(frame: UFrame) -> Self {
329        // SAFETY: The metadata is coerceable and the struct is transmutable.
330        unsafe { core::mem::transmute(frame) }
331    }
332}
333
334impl TryFrom<Frame<dyn AnyFrameMeta>> for UFrame {
335    type Error = Frame<dyn AnyFrameMeta>;
336
337    /// Tries converting a [`Frame<dyn AnyFrameMeta>`] into [`UFrame`].
338    ///
339    /// If the usage of the frame is not the same as the expected usage, it will
340    /// return the dynamic frame itself as is.
341    fn try_from(dyn_frame: Frame<dyn AnyFrameMeta>) -> Result<Self, Self::Error> {
342        if dyn_frame.dyn_meta().is_untyped() {
343            // SAFETY: The metadata is coerceable and the struct is transmutable.
344            Ok(unsafe { core::mem::transmute::<Frame<dyn AnyFrameMeta>, UFrame>(dyn_frame) })
345        } else {
346            Err(dyn_frame)
347        }
348    }
349}
350
351/// Increases the reference count of the frame by one.
352///
353/// # Safety
354///
355/// The caller should ensure the following conditions:
356///  1. The physical address must represent a valid frame;
357///  2. The caller must have already held a reference to the frame.
358pub(in crate::mm) unsafe fn inc_frame_ref_count(paddr: Paddr) {
359    debug_assert!(paddr.is_multiple_of(PAGE_SIZE));
360    debug_assert!(paddr < max_paddr());
361
362    let vaddr: Vaddr = mapping::frame_to_meta::<PagingConsts>(paddr);
363    // SAFETY: `vaddr` points to a valid `MetaSlot` that will never be mutably borrowed, so taking
364    // an immutable reference to it is always safe.
365    let slot = unsafe { &*(vaddr as *const MetaSlot) };
366
367    // SAFETY: We have already held a reference to the frame.
368    unsafe { slot.inc_ref_count() };
369}