The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
usermode.h File Reference
#include "common/stdint.h"
#include "arch/arch.h"
#include "common/error.h"

Go to the source code of this file.

Macros

#define USER_ARG_MAX   (128u * 1024u)

Functions

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, laid out the way crt0 reads them: [esp] 0 (fake return address), argc, argv, envp, then the two NULL-terminated pointer arrays and the strings.
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 current kernel stack and iret to ring 3.
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 point, stack, flags and segments.
kerr_t exec_replace (struct registers *regs, const char *path, const char **argv, const char **envp)
 replace the current process's program (execve).
int exec_program (const char *path, const char **argv)
 start a program as a new process.
void usermode_exit (int exit_code) __attribute__((noreturn))
 called by SYS_exit: end the current thread, and never run again.
void usermode_exit_group (int exit_code) __attribute__((noreturn))
 called by SYS_exit_group (and so by exit()): end every thread of the current process, which then ends with exit_code.
void usermode_kill (int signal) __attribute__((noreturn))
 end the current process (every thread) as killed by a signal: the wait status names the signal (SIGSEGV for a bad memory access).
void usermode_end_thread (void) __attribute__((noreturn))
 end the current thread because its process is ending (exit_group or a fatal signal in another thread), with the process's status.
uintptr_t usermode_get_brk (void)
 get the current program break (end of the user heap).
void usermode_set_brk (uintptr_t brk)
 set the program break.
void setup_child_stack (process_t *child, uintptr_t entry)
 set up a child process's kernel stack so that context_switch returns into the process entry trampoline.

Macro Definition Documentation

◆ USER_ARG_MAX

#define USER_ARG_MAX   (128u * 1024u)

Function Documentation

◆ usermode_build_stack()

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, laid out the way crt0 reads them: [esp] 0 (fake return address), argc, argv, envp, then the two NULL-terminated pointer arrays and the strings.

the address space must be the active one: the stack is written through its user addresses.

Parameters
asthe address space.
argvNULL-terminated argument strings (may be NULL).
envpNULL-terminated "KEY=VALUE" strings (may be NULL).
Returns
the initial user stack pointer, or 0 if memory ran out.

◆ jump_to_usermode()

void jump_to_usermode ( uintptr_t entry,
addr_space_t as,
const char ** argv,
const char ** envp )

start a new process's program: build its user stack (usermode_build_stack), point the TSS at the current kernel stack and iret to ring 3.

the address space must be the active one. never returns.

Parameters
entrythe program's entry point.
asthe process's address space.
argvNULL-terminated argument strings (may be NULL).
envpNULL-terminated environment strings (may be NULL).

◆ usermode_reset_frame()

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 point, stack, flags and segments.

Parameters
regsthe syscall frame.
entrythe program's entry point.
user_espthe initial user stack pointer.

◆ exec_replace()

kerr_t exec_replace ( struct registers * regs,
const char * path,
const char ** argv,
const char ** envp )

replace the current process's program (execve).

the new image is loaded into a new address space first, so on failure the caller keeps running its old program. on success the old address space is gone, the areas and break are the new program's, its stack holds argv and envp, and the syscall frame returns into its entry point. pid, parent, cwd and fds stay (except FD_CLOEXEC ones, which are closed).

Parameters
regsthe caller's syscall frame. a script that starts with "#!interpreter [arg]" runs as "interpreter [arg] path argv[1]...".
pathabsolute path of the ELF executable or script.
argvNULL-terminated argument strings in kernel memory.
envpNULL-terminated environment strings in kernel memory.
Returns
KERR_OK, KERR_NOENT if the file is not there, KERR_NOEXEC if it is not an ELF executable or a script, KERR_PERM if the caller may not run it, or KERR_NOMEM.

◆ exec_program()

int exec_program ( const char * path,
const char ** argv )

start a program as a new process.

creates the process, loads the ELF at path into its address space, copies argv, inherits the caller's cwd and its fds without FD_CLOEXEC (when called from a process) and queues it on the scheduler. with no current process (boot), the new process has no parent: PID 1.

Parameters
pathabsolute path to the ELF executable.
argvnull-terminated array of argument strings (may be NULL).
Returns
the new process's PID, or -1 on failure.

◆ usermode_exit()

void usermode_exit ( int exit_code)

called by SYS_exit: end the current thread, and never run again.

if it is the last thread of its process, the process ends: it becomes a zombie for its parent to reap, with a wait status that says it exited with exit_code. if PID 1 ends, the system halts.

Parameters
exit_codethe exit code passed by the user program (low 8 bits).

◆ usermode_exit_group()

void usermode_exit_group ( int exit_code)

called by SYS_exit_group (and so by exit()): end every thread of the current process, which then ends with exit_code.

Parameters
exit_codethe exit code (low 8 bits).

◆ usermode_kill()

void usermode_kill ( int signal)

end the current process (every thread) as killed by a signal: the wait status names the signal (SIGSEGV for a bad memory access).

Parameters
signalthe signal number (kernel/signal/signal.h).

◆ usermode_end_thread()

void usermode_end_thread ( void )

end the current thread because its process is ending (exit_group or a fatal signal in another thread), with the process's status.

◆ usermode_get_brk()

uintptr_t usermode_get_brk ( void )

get the current program break (end of the user heap).

Returns
current break address.

◆ usermode_set_brk()

void usermode_set_brk ( uintptr_t brk)

set the program break.

called by exec_program after ELF loading to initialize the break.

Parameters
brkthe initial break address (page-aligned end of loaded segments).

◆ setup_child_stack()

void setup_child_stack ( process_t * child,
uintptr_t entry )

set up a child process's kernel stack so that context_switch returns into the process entry trampoline.

set up a child process's kernel stack so that when arch_context_switch switches to it, it "returns" into process_entry_trampoline.

Parameters
childpointer to the child process control block.
entryELF entry point for the child.

set up a child process's kernel stack so that context_switch returns into the process entry trampoline.

the ELF entry point is stashed in the process so the trampoline can read it later.