The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
usermode_arch.c File Reference
#include "usermode.h"
#include "arch/arch.h"
#include "arch/x86/cpu/gdt.h"
#include "arch/x86/cpu/paging.h"
#include "kernel/memory/pmm.h"
#include "kernel/memory/memory_layout.h"
#include "kernel/panic.h"
#include "common/memory.h"
#include "common/string.h"
#include "arch/x86/cpu/isr.h"

Macros

#define USER_EFLAGS   0x202u

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)
 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.

Macro Definition Documentation

◆ USER_EFLAGS

#define USER_EFLAGS   0x202u

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.