Skip to main content

sys_execve

Function sys_execve 

Source
pub fn sys_execve(
    frame: &mut SyscallFrame,
    path_ptr: u64,
    argv_ptr: u64,
    envp_ptr: u64,
) -> Result<u64, SyscallError>
Expand description

SYS_PROC_EXECVE (301): replace current process image. On success, does not return. On failure, returns an appropriate error code. This is the main syscall handler for execve, which performs the entire execve sequence:

  1. Validate and read the executable image from the given path.
  2. Create a new address space and load the ELF segments.
  3. Set up the user stack with arguments, environment variables, and auxiliary vector.
  4. Perform cleanup of the current process state (close fds, reset signals, clear TLS and TID pointer, etc) according to POSIX exec semantics.
  5. Switch to the new address space and transfer control to the new image’s entry point. The setup_user_stack function is a helper that performs step 3, which is complex enough to warrant its own function. The implementation assumes a simple model where sibling threads are not runnable during execve, which allows it to replace the entire address space without complex synchronization. This is a common approach in many kernels, but it does mean that multithreaded execve is not supported until the kernel can safely handle it. It also includes robust error handling to ensure that any failure during the execve sequence results in an appropriate error code without leaving the process in an inconsistent state. Note: This implementation does not currently support some features like setuid binaries, but it lays the groundwork for a full execve implementation with proper ELF loading and stack setup.