LoadContext

Struct LoadContext 

Source
pub struct LoadContext<P: Pager> {
    pub mm_struct: MmStruct<P>,
    pub regs: Registers,
}
Expand description

A context that holds the necessary state for loading and initializing a user program.

LoadContext is used during the loading an ELF binary into memory. It encapsulates both the memory layout for the program and its initial register state, allowing the loader to fully prepare the user-space execution context.

Fields§

§mm_struct: MmStruct<P>

Virtual memory layout for the new user program.

§regs: Registers

Initial CPU register values for the user process, including the instruction pointer.

Implementations§

Source§

impl<P: Pager> LoadContext<P>

Source

pub fn load_phdr(&mut self, elf: Elf<'_>) -> Result<(), KernelError>

Loads program headers (Phdrs) from an ELF binary into memory.

This function iterates over the ELF program headers and maps the corresponding segments into the process’s memory space. It ensures that each segment is correctly mapped according to its permissions and alignment requirements.

§Parameters
  • elf: The ELF binary representation containing program headers.
§Returns
  • Ok(()) on success, indicating that all segments were successfully loaded.
  • Err(KernelError) if any error occurs during the loading process, such as an invalid memory mapping, insufficient memory, or an unsupported segment type.
§Behavior
  • Iterates over all program headers using Elf::phdrs.
  • Maps each segment into memory if its type is PType::Load.
  • Applies appropriate memory permissions using Phdr::permission.
  • Ensures proper alignment and memory allocation before mapping.
Source

pub fn build_stack(&mut self, arguments: &[&str]) -> Result<(), KernelError>

Builds a user stack and initializes it with arguments.

This function sets up a new stack for the process by allocating memory, pushing program arguments (argv), and preparing the initial register state.

§Parameters
  • arguments: A slice of strs representing the command-line arguments (argv).
  • regs: A mutable reference to the register state, which will be updated with the initial stack pointer (sp) and argument count (argc).
§Returns
  • Ok(()) on success, indicating that the stack has been built correctly.
  • Err(KernelError) if an error occurs during memory allocation or argument copying.
§Behavior
  • Pushes the argument strings onto the stack.
  • Sets up argv and argc for the process.
  • Aligns the stack pointer to ensure proper function call execution.
§Safety
  • The function must be called before transferring control to user space.
  • The memory layout should follow the standard calling convention for argument passing.
Source

pub fn load( self, file: &RegularFile, args: &[&str], ) -> Result<Self, KernelError>

Creates a new memory state and initializes a user process from an ELF executable.

This function loads an ELF binary into memory, sets up the program headers, and constructs the initial user stack with arguments. It also prepares the register state for execution.

§Parameters
  • file: A reference to the ELF executable file.
  • args: A slice of strs representing the command-line arguments (argv).
§Returns
  • Ok((Self, Registers)) on success, where:
    • Self is the initialized memory state.
    • Registers contains the initial register values.
  • Err(KernelError) if an error occurs while loading the ELF file or setting up memory.
§Behavior
  • Parses the ELF file and validates its format.
  • Loads program headers (PType::Load) into memory.
  • Allocates and builds the user stack.
  • Initializes the register state (rip -> entry point, rsp -> stack pointer, arg1 -> the number of arguments, arg1 -> address of arguments vector.).

Auto Trait Implementations§

§

impl<P> Freeze for LoadContext<P>
where P: Freeze,

§

impl<P> RefUnwindSafe for LoadContext<P>
where P: RefUnwindSafe,

§

impl<P> Send for LoadContext<P>
where P: Send,

§

impl<P> Sync for LoadContext<P>
where P: Sync,

§

impl<P> Unpin for LoadContext<P>
where P: Unpin,

§

impl<P> UnwindSafe for LoadContext<P>
where P: UnwindSafe,

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.