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: RegistersInitial CPU register values for the user process, including the instruction pointer.
Implementations§
Source§impl<P: Pager> LoadContext<P>
impl<P: Pager> LoadContext<P>
Sourcepub fn load_phdr(&mut self, elf: Elf<'_>) -> Result<(), KernelError>
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.
Sourcepub fn build_stack(&mut self, arguments: &[&str]) -> Result<(), KernelError>
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
argvandargcfor 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.
Sourcepub fn load(
self,
file: &RegularFile,
args: &[&str],
) -> Result<Self, KernelError>
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:Selfis the initialized memory state.Registerscontains 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.).