The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
usermode.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
4#include "arch/arch.h"
5#include "common/error.h"
6
7struct process;
8typedef struct process process_t;
9
10struct registers;
11
12// most bytes the argv and envp of a new program take together: the strings
13// with their NULs and one pointer for each (ARG_MAX in the libc, as Linux)
14#define USER_ARG_MAX (128u * 1024u)
15
16/**
17 * back the first pages of the user stack in an address space and put a new
18 * program's arguments on it, laid out the way crt0 reads them:
19 * [esp] 0 (fake return address), argc, argv, envp, then the two
20 * NULL-terminated pointer arrays and the strings.
21 * the address space must be the active one: the stack is written through
22 * its user addresses.
23 * @param as the address space.
24 * @param argv NULL-terminated argument strings (may be NULL).
25 * @param envp NULL-terminated "KEY=VALUE" strings (may be NULL).
26 * @return the initial user stack pointer, or 0 if memory ran out.
27 */
28uintptr_t usermode_build_stack(addr_space_t as, const char **argv, const char **envp);
29
30/**
31 * start a new process's program: build its user stack (usermode_build_stack),
32 * point the TSS at the current kernel stack and iret to ring 3. the address
33 * space must be the active one. never returns.
34 * @param entry the program's entry point.
35 * @param as the process's address space.
36 * @param argv NULL-terminated argument strings (may be NULL).
37 * @param envp NULL-terminated environment strings (may be NULL).
38 */
39void jump_to_usermode(uintptr_t entry, addr_space_t as, const char **argv, const char **envp)
40 __attribute__((noreturn));
41
42/**
43 * make a syscall frame return into a fresh program: clear the general
44 * registers and set the user entry point, stack, flags and segments.
45 * @param regs the syscall frame.
46 * @param entry the program's entry point.
47 * @param user_esp the initial user stack pointer.
48 */
49void usermode_reset_frame(struct registers *regs, uintptr_t entry, uintptr_t user_esp);
50
51/**
52 * replace the current process's program (execve). the new image is loaded
53 * into a new address space first, so on failure the caller keeps running
54 * its old program. on success the old address space is gone, the areas
55 * and break are the new program's, its stack holds argv and envp, and the
56 * syscall frame returns into its entry point. pid, parent, cwd and fds stay
57 * (except FD_CLOEXEC ones, which are closed).
58 * @param regs the caller's syscall frame.
59 * a script that starts with "#!interpreter [arg]" runs as
60 * "interpreter [arg] path argv[1]...".
61 * @param path absolute path of the ELF executable or script.
62 * @param argv NULL-terminated argument strings in kernel memory.
63 * @param envp NULL-terminated environment strings in kernel memory.
64 * @return KERR_OK, KERR_NOENT if the file is not there, KERR_NOEXEC if it
65 * is not an ELF executable or a script, KERR_PERM if the caller may
66 * not run it, or KERR_NOMEM.
67 */
68kerr_t exec_replace(struct registers *regs, const char *path, const char **argv,
69 const char **envp);
70
71/**
72 * start a program as a new process. creates the process, loads the ELF at
73 * path into its address space, copies argv, inherits the caller's cwd and
74 * its fds without FD_CLOEXEC (when called from a process) and queues it on
75 * the scheduler. with no current process (boot), the new process has no
76 * parent: PID 1.
77 *
78 * @param path absolute path to the ELF executable.
79 * @param argv null-terminated array of argument strings (may be NULL).
80 * @return the new process's PID, or -1 on failure.
81 */
82int exec_program(const char *path, const char **argv);
83
84/**
85 * called by SYS_exit: end the current thread, and never run again. if it
86 * is the last thread of its process, the process ends: it becomes a
87 * zombie for its parent to reap, with a wait status that says it exited
88 * with exit_code. if PID 1 ends, the system halts.
89 * @param exit_code the exit code passed by the user program (low 8 bits).
90 */
91void usermode_exit(int exit_code) __attribute__((noreturn));
92
93/**
94 * called by SYS_exit_group (and so by exit()): end every thread of the
95 * current process, which then ends with exit_code.
96 * @param exit_code the exit code (low 8 bits).
97 */
98void usermode_exit_group(int exit_code) __attribute__((noreturn));
99
100/**
101 * end the current process (every thread) as killed by a signal: the wait
102 * status names the signal (SIGSEGV for a bad memory access).
103 * @param signal the signal number (kernel/signal/signal.h).
104 */
105void usermode_kill(int signal) __attribute__((noreturn));
106
107/**
108 * end the current thread because its process is ending (exit_group or a
109 * fatal signal in another thread), with the process's status.
110 */
111void usermode_end_thread(void) __attribute__((noreturn));
112
113/**
114 * get the current program break (end of the user heap).
115 * @return current break address.
116 */
118
119/**
120 * set the program break.
121 * called by exec_program after ELF loading to initialize the break.
122 * @param brk the initial break address (page-aligned end of loaded segments).
123 */
125
126/**
127 * set up a child process's kernel stack so that context_switch
128 * returns into the process entry trampoline.
129 * @param child pointer to the child process control block.
130 * @param entry ELF entry point for the child.
131 */
132void setup_child_stack(process_t *child, uintptr_t entry);
133
uintptr_t addr_space_t
an address space handle.
Definition arch.h:105
uintptr_t usermode_get_brk(void)
get the current program break (end of the user heap).
Definition brk.c:5
void usermode_set_brk(uintptr_t brk)
set the program break.
Definition brk.c:15
kerr_t
unified kernel error codes.
Definition error.h:14
void usermode_kill(int signal)
end the current process (every thread) as killed by a signal: the wait status names the signal (SIGSE...
Definition exec.c:600
kerr_t exec_replace(struct registers *regs, const char *path, const char **argv, const char **envp)
replace the current process's program (execve).
Definition exec.c:362
void usermode_exit(int exit_code)
called by SYS_exit: end the current thread, and never run again.
Definition exec.c:590
int exec_program(const char *path, const char **argv)
start a program as a new process.
Definition exec.c:105
void usermode_end_thread(void)
end the current thread because its process is ending (exit_group or a fatal signal in another thread)...
Definition exec.c:605
void usermode_exit_group(int exit_code)
called by SYS_exit_group (and so by exit()): end every thread of the current process,...
Definition exec.c:595
sighandler_t signal(int sig, sighandler_t handler)
set a handler (with SA_RESTART, as BSD signal() does).
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
struct process process_t
process control block.
ELF32 file header.
Definition elf.h:51
process control block.
Definition process.h:69
saved register state pushed onto the stack by ISR stubs.
Definition isr.h:41
void setup_child_stack(process_t *child, uintptr_t entry)
set up a child process's kernel stack so that when arch_context_switch switches to it,...
Definition trampoline.c:42
void usermode_reset_frame(struct registers *regs, uintptr_t entry, uintptr_t user_esp)
make a syscall frame return into a fresh program: clear the general registers and set the user entry ...
Definition usermode_arch.c:124
uintptr_t usermode_build_stack(addr_space_t as, const char **argv, const char **envp)
back the first pages of the user stack in an address space and put a new program's arguments on it,...
Definition usermode_arch.c:57
void jump_to_usermode(uintptr_t entry, addr_space_t as, const char **argv, const char **envp) __attribute__((noreturn))
start a new process's program: build its user stack (usermode_build_stack), point the TSS at the curr...
Definition usermode_arch.c:106