Skip to main content

kernel/lib/types/
aligned_stack.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! 16-byte-aligned stack storage, used for things like x86_64 IST stacks
3//!
4//! Authors: MarioS271
5
6use crate::lib::addr::VirtAddr;
7
8/// An `N`-byte array aligned to a 16-byte boundary
9#[repr(align(16))]
10pub struct AlignedStack<const N: usize>([u8; N]);
11
12impl<const N: usize> AlignedStack<N> {
13    /// Constructor; creates a new zeroed [`AlignedStack`] of size N
14    pub const fn new() -> Self {
15        Self([0u8; N])
16    }
17
18    /// Getter for the inner array
19    pub fn get(&self) -> [u8; N] {
20        self.0
21    }
22
23    /// Returns the top of the stack as a [`VirtAddr`]
24    pub fn get_stack_top(&self) -> VirtAddr {
25        // Safety: .add(len) computes a one-past-the-end pointer, which is valid per rust's pointer rules
26        unsafe { VirtAddr::from_ptr(self.0.as_ptr().add(self.0.len())) }
27    }
28}