PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
loader.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdarg.h>
6
7/**
8 * @brief Program loading and user stack management.
9 * @defgroup kernel_sched_loader Program Loader
10 * @ingroup kernel_sched
11 *
12 * The loader is responsible for loading programs into memory, setting up the initial user stack, and performing the
13 * jump to userspace.
14 *
15 * ## Initial User Stack and Registers
16 *
17 * When a new program is loaded, we pass the command line arguments (argv) to the program via the user stack and
18 * registers.
19 *
20 * The stack is set up as follows:
21 *
22 * <div align="center">
23 * | Stack Contents |
24 * |---------------------|
25 * | *argv[argc - 1] |
26 * | ... |
27 * | *argv[0] |
28 * | NULL |
29 * | argv[argc - 1] |
30 * | ... |
31 * | argv[0] |
32 * | padding |
33 * </div>
34 *
35 * The `argv` pointer is placed in the `rsi` register, and the `argc` value is placed in the `rdi` register.
36 *
37 * Note that rsp points to argc when the program starts executing.
38 *
39 * @{
40 */
41
42/**
43 * @brief Causes the currently running thread to load and execute a new program.
44 *
45 * Intended to be used as the entry point for a newly created process, causing it to run the specified executable with
46 * the given arguments and environment variables.
47 *
48 * @note This function does not return, instead it transfers execution to the new program in user space, if it fails it
49 * will exit the process.
50 *
51 * @warning The arguments `executable` and `argv` along with their contents will be freed by this function, they must be
52 * heap allocated and not used after calling this function.
53 *
54 * @param executable The path to the executable file, will be freed by the loader.
55 * @param argv The argument vector for the new program, will be freed by the loader along with its contents, can be
56 * `NULL` if `argc` is `0`.
57 * @param argc The number of arguments in `argv`.
58 */
59_NORETURN void loader_exec(const char* executable, char** argv, uint64_t argc);
60
61/** @} */
_NORETURN void loader_exec(const char *executable, char **argv, uint64_t argc)
Causes the currently running thread to load and execute a new program.
Definition loader.c:39
#define _NORETURN
Definition config.h:28
__UINT64_TYPE__ uint64_t
Definition stdint.h:17