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
- Create a new stack using
StackBuilder::new. - Push data (e.g., arguments, environment variables) onto the stack.
- Align the stack for proper memory layout.
- Finalize the stack using
StackBuilder::finishto 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>
impl<'a, P: Pager> StackBuilder<'a, P>
Sourcepub fn new(mm_state: &'a mut MmStruct<P>) -> Result<Self, KernelError>
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.
Sourcepub fn finish(self) -> Va
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.
Sourcepub fn align(&mut self, align: usize)
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.
Sourcepub fn push_bytes(&mut self, bytes: &[u8]) -> Va
pub fn push_bytes(&mut self, bytes: &[u8]) -> Va
Sourcepub fn push_usize(&mut self, v: usize) -> Va
pub fn push_usize(&mut self, v: usize) -> Va
Sourcepub fn push_str(&mut self, s: &str) -> Va
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.