StackBuilder

Struct StackBuilder 

Source
pub struct StackBuilder<'a, P: Pager> {
    sp: Va,
    mm_state: &'a mut MmStruct<P>,
}
Expand description

A utility for constructing a user-space stack layout.

StackBuilder provides methods to allocate, align, and push data onto a stack before mapping it into a user process. It is primarily used to prepare the initial stack for a new process, including setting up argv and other necessary data.

The stack starts at virtual address 0x4748_0000 and grows downward.

§Fields

  • sp: The current stack pointer, representing the top of the stack.
  • pages: A list of allocated pages that will back the stack.

§Usage

  1. Create a new stack using StackBuilder::new.
  2. Push data (e.g., arguments, environment variables) onto the stack.
  3. Align the stack for proper memory layout.
  4. Finalize the stack using StackBuilder::finish to map it into the process’s address space.

Fields§

§sp: Va§mm_state: &'a mut MmStruct<P>

Implementations§

Source§

impl<'a, P: Pager> StackBuilder<'a, P>

Source

pub fn new(mm_state: &'a mut MmStruct<P>) -> Result<Self, KernelError>

Creates a new StackBuilder instance for building a user-space stack.

The stack is initialized at virtual address 0x4748_0000 and grows downward as data is pushed onto it.

§Returns

A new StackBuilder with an empty stack and no allocated pages.

Source

pub fn finish(self) -> Va

Consume the StackBuilder and return the stack pointer.

§Returns
  • Ok(Va): The final stack pointer after mapping.
  • Err(KernelError): If the stack mapping fails.
Source

pub fn sp(&self) -> Va

Returns the current stack pointer.

The stack pointer (sp) indicates the top of the stack, where the next value would be pushed. The stack grows downward, meaning the pointer decreases as more data is pushed onto it.

§Returns
  • The current stack pointer as a virtual address (Va).
Source

pub fn align(&mut self, align: usize)

Aligns the stack pointer to the given alignment.

This function ensures that the stack pointer is aligned to the specified byte boundary, which is useful for maintaining proper data alignment when pushing values.

§Parameters
  • align: The byte alignment requirement.
§Behavior
  • If the stack pointer is not already aligned, it is adjusted downward to meet the alignment requirement.
Source

pub fn push_bytes(&mut self, bytes: &[u8]) -> Va

Pushes a byte array onto the stack.

This function decreases the stack pointer to allocate space for the value and stores it at the new top of the stack.

§Parameters
  • v: The [u8] value to be pushed onto the stack.
§Returns
  • The updated stack pointer after pushing the value.
Source

pub fn push_usize(&mut self, v: usize) -> Va

Pushes a usize value onto the stack.

This function decreases the stack pointer to allocate space for the value and stores it at the new top of the stack.

§Parameters
  • v: The usize value to be pushed onto the stack.
§Returns
  • The updated stack pointer after pushing the value.
Source

pub fn push_str(&mut self, s: &str) -> Va

Pushes a string onto the stack as a C-style string (null-terminated).

This function copies the given string onto the stack, appends a null terminator (\0), and returns the virtual address (Va) where the string is stored.

§Parameters
  • s: The string to push onto the stack.
§Returns
  • The virtual address (Va) pointing to the beginning of the stored string in memory.
§Behavior
  • The stack pointer is adjusted downward to allocate space for the string.
  • The string is stored in memory in a null-terminated format, making it compatible with C-style APIs.

Auto Trait Implementations§

§

impl<'a, P> Freeze for StackBuilder<'a, P>

§

impl<'a, P> RefUnwindSafe for StackBuilder<'a, P>
where P: RefUnwindSafe,

§

impl<'a, P> Send for StackBuilder<'a, P>
where P: Send,

§

impl<'a, P> Sync for StackBuilder<'a, P>
where P: Sync,

§

impl<'a, P> Unpin for StackBuilder<'a, P>

§

impl<'a, P> !UnwindSafe for StackBuilder<'a, P>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.