PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
Program Loader

Program loading and user stack management. More...

Collaboration diagram for Program Loader:

Detailed Description

Program loading and user stack management.

The loader is responsible for loading programs into memory, setting up the initial user stack, and performing the jump to userspace.

Initial User Stack and Registers

When a new program is loaded, we pass the command line arguments (argv) to the program via the user stack and registers.

The stack is set up as follows:

+---------------------+
| *argv[argc - 1] |
| ... |
| *argv[0] |
+---------------------+
| NULL |
+---------------------+
| argv[argc - 1] |
| ... |
| argv[0] |
+---------------------+
| padding |
+---------------------+
#define NULL
Pointer error value.
Definition NULL.h:23

The argv pointer is placed in the rsi register, and the argc value is placed in the rdi register.

Note that rsp points to argc when the program starts executing.

Functions

_NORETURN void loader_exec (const char *executable, char **argv, uint64_t argc)
 Causes the currently running thread to load and execute a new program.
 

Function Documentation

◆ loader_exec()

_NORETURN void loader_exec ( const char *  executable,
char **  argv,
uint64_t  argc 
)

Causes the currently running thread to load and execute a new program.

Intended to be used as the entry point for a newly created process, causing it to run the specified executable with the given arguments and environment variables.

Note
This function does not return, instead it transfers execution to the new program in user space, if it fails it will exit the process.
Warning
The arguments executable and argv along with their contents will be freed by this function, they must be heap allocated and not used after calling this function.
Parameters
executableThe path to the executable file, will be freed by the loader.
argvThe argument vector for the new program, will be freed by the loader along with its contents, can be NULL if argc is 0.
argcThe number of arguments in argv.

Definition at line 39 of file loader.c.

Here is the call graph for this function:
Here is the caller graph for this function: