My Kernel v0.1.0
Collaboration diagram for Processes:

Modules

 Processes - x86
 
 Processes - arch specifics
 

Data Structures

struct  process
 A single process. More...
 
struct  thread
 A single thread. More...
 

Macros

#define PROCESS_NAME_MAX_LEN   32U
 The max length of a process's name.
 
#define PROCESS_FD_COUNT   32
 Maximum number of files that one process can have open at any one time.
 

Typedefs

typedef void(* thread_entry_t) (void *data)
 A function used as an entry point when creating a new thread. More...
 

Enumerations

enum  thread_state { SCHED_RUNNING , SCHED_WAITING , SCHED_KILLED }
 The different states a thread can be in. More...
 
enum  thread_flags { THREAD_KERNEL = BIT(0) }
 

Functions

static ALWAYS_INLINE bool thread_is_initial (thread_t *thread)
 The initial thread is the thread created along with the process. More...
 
static void thread_set_stack_pointer (struct thread *thread, void *stack)
 Set the thread's current stack pointer.
 
static void * thread_get_stack_pointer (struct thread *thread)
 Get the thread's current stack pointer.
 
static void thread_set_kernel_stack (struct thread *thread, void *stack)
 Set the thread's kernel stack bottom address.
 
static void * thread_get_kernel_stack_top (const struct thread *thread)
 Get the thread's kernel stack top address.
 
static void * thread_get_kernel_stack (const struct thread *thread)
 Get the thread's kernel stack bottom address.
 
static void thread_set_user_stack (struct thread *thread, void *stack)
 Set the thread's user stack bottom address.
 
static void * thread_get_user_stack_top (const struct thread *thread)
 Get the thread's user stack top address.
 
static void * thread_get_user_stack (const struct thread *thread)
 Get the thread's user stack bottom address.
 
void process_init_kernel_process (void)
 Initialize the kernel process's address space. More...
 
int process_register_file (struct process *, struct file *)
 Register an open file inside the process's open file descriptor table. More...
 
error_t process_unregister_file (struct process *, int fd)
 Remove an open file from the process's open file descriptor table.
 
struct fileprocess_file_get (struct process *, int fd)
 
static void process_file_put (struct process *process, struct file *file)
 Release a file description retreived using process_file_get().
 
struct threadprocess_execute_in_userland (const char *exec_path)
 Run an executable. More...
 
bool thread_switch (thread_t *)
 Switch the currently running thread. More...
 
thread_tthread_spawn (struct process *, thread_entry_t, void *, void *, u32)
 Create and initialize a new thread. More...
 
NO_RETURN void thread_jump_to_userland (thread_entry_t, void *)
 Start executing code in userland. More...
 
void thread_set_mmu (struct thread *thread, paddr_t mmu)
 Set the MMU address saved inside the thread's structure. More...
 
void thread_kill (thread_t *)
 Effectively kill a thread. More...
 
struct threadthread_fork (struct thread *, thread_entry_t, void *)
 Create a new fork of the given thread. More...
 

Variables

struct process kernel_process
 Process used when starting up the kernel. More...
 
struct processinit_process
 The init process is the very first executed userland process. More...
 
thread_tcurrent
 The currently running thread.
 

Detailed Description

Typedef Documentation

◆ thread_entry_t

typedef void(* thread_entry_t) (void *data)
Parameters
dataGeneric data passed on to the function when starting the thread
Note
We never return from this function

Enumeration Type Documentation

◆ thread_flags

Enumerator
THREAD_KERNEL 

This thread runs in kernel mode.

◆ thread_state

Enumerator
SCHED_RUNNING 

Currently running (or ready to run)

SCHED_WAITING 

Currently waiting for a resource (timer, lock ...)

SCHED_KILLED 

The thread has been killed waiting to be destroyed.

Function Documentation

◆ process_execute_in_userland()

struct thread * process_execute_in_userland ( const char *  exec_path)

As the kernel should never run external executables, this function instead creates a new userland process that will in turn be used to execute the executable.

Parameters
exec_pathAbsolute path to the executable file

◆ process_file_get()

struct file * process_file_get ( struct process process,
int  fd 
)
Returns
A reference to the file description at index fd inside Processes 's file descriptor table.

◆ process_init_kernel_process()

void process_init_kernel_process ( void  )

The kernel process's instance being defined satically, it is required to be explicitely initialize during the startup phase so that it can be used just like any other process later on.

◆ process_register_file()

int process_register_file ( struct process process,
struct file file 
)
Returns
The registered file's index inside the open file descriptor table.

◆ thread_fork()

struct thread * thread_fork ( struct thread thread,
thread_entry_t  entrypoint,
void *  arg 
)

A new process is created to execute the forked thread. The address space of the original thread's process is duplicated inside the newly created process.

If the duplicated process had multiple threads, only the one that called this function is replicated.

Returns
The newly created thread

◆ thread_is_initial()

static ALWAYS_INLINE bool thread_is_initial ( thread_t thread)
static

The TID of the initial thread is the same as its process's PID.

◆ thread_jump_to_userland()

NO_RETURN void thread_jump_to_userland ( thread_entry_t  entrypoint,
void *  data 
)

This function resets the user execution context (eip, stack content). It then changes the privilege level to be that of userland, and jumps onto the given address.

@info This serves as a temporary equivalent to the execve syscall for testing purposes.

Parameters
entrypointThe entrypoint address to jump onto
dataThe data passed as an argument to the 'entrypoint' function (ignored)

◆ thread_kill()

void thread_kill ( thread_t thread)
  • Free all private memory used by the thread
  • Synchronize resources (write to files, ...)

◆ thread_set_mmu()

void thread_set_mmu ( struct thread thread,
paddr_t  mmu 
)
Note
This function does not change the MMU currently in use, see mmu_load for this instead.

◆ thread_spawn()

thread_t * thread_spawn ( struct process process,
thread_entry_t  entrypoint,
void *  data,
void *  esp,
u32  flags 
)

When starting a userland thread, the data field is not passed to the entrypoint function.

Parameters
processThe process the newly created thread belongs to
entrypointThe function called when starting the thread
dataData passed to the entry function (can be NULL)
espThe value to place inside the stack pointer register before first kicking off the thread. This is mainly useful when forking an existing thread. Ignored if NULL.
flagsFeature flags: a combination of thread_flags enum values

◆ thread_switch()

bool thread_switch ( thread_t thread)
Parameters
processThe new thread to switch into
Returns
false if the new thread was previously killed

Variable Documentation

◆ init_process

struct process* init_process
extern

It is the parent of all other userland processes, and all zombie processes are attached to it when their parent dies.

The init process uses the reserved PID 1.

◆ kernel_process

struct process kernel_process
extern

It is necesary to define it statically, since memory management functions are not yet set up when first starting up.

We should not reference this process anymore once we have an up and running scheduler.