armOS  0.1.0
Minimal ARM Operating System for the Raspberry Pi - Documentation generated for Pi 4.
processes

Code for managing processes. More...

Files

file  fork.c
 Implementation of fork-related functions.
 
file  process.c
 Implementation of functions for creating and killing processes.
 
file  scheduler.c
 Implementation of scheduler functions.
 
file  scheduler.h
 Definition of armv6 scheduler helper functions.
 
file  scheduler.h
 Definition of armv8-a scheduler helper functions.
 
file  fork.h
 Definition of fork-related functions.
 
file  process.h
 Definition of functions for creating and killing processes.
 
file  scheduler.h
 Definition of scheduler structs and functions.
 

Data Structures

struct  cpu_context
 
struct  task_struct
 

Macros

#define THREAD_CPU_CONTEXT   0
 
#define THREAD_CPU_CONTEXT   0
 
#define TASK_RUNNING   0
 
#define NR_TASKS   64
 
#define FIRST_TASK   task[0]
 
#define LAST_TASK   task[NR_TASKS-1]
 
#define THREAD_SIZE   4096
 
#define INIT_TASK   { /* cpu_context */ {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* state etc */ 0,0,1,0 }
 

Functions

int copy_process (uint64_t fn, uint64_t arg)
 
void create_processes (size_t proc_num)
 
void kill_processes ()
 
void ret_from_fork ()
 
void cpu_switch_to (task_struct *prev, task_struct *next)
 
void preempt_disable ()
 
void preempt_enable ()
 
void schedule_tail ()
 
void switch_to (task_struct *next)
 
void schedule ()
 
void timer_tick ()
 

Variables

task_structtask [NR_TASKS]
 
task_structcurrent
 
int nr_tasks
 

Detailed Description

Code for managing processes.

Macro Definition Documentation

◆ FIRST_TASK

#define FIRST_TASK   task[0]

Define the first task.

See also
task

◆ INIT_TASK

#define INIT_TASK   { /* cpu_context */ {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* state etc */ 0,0,1,0 }

Defines the init task's task_struct ( kernel_main()) that is run on kernel startup.

Fields:

  • cpu_context: All registers initialized to 0.
  • state = 0 ( TASK_RUNNING)
  • counter = 0
  • priority = 1
  • preempt_count = 0 (can be rescheduled)
See also
task_struct, cpu_context, TASK_RUNNING

◆ LAST_TASK

#define LAST_TASK   task[NR_TASKS-1]

Define the last task.

See also
task

◆ NR_TASKS

#define NR_TASKS   64

Maximum number of concurrent tasks

◆ TASK_RUNNING

#define TASK_RUNNING   0

Task States

◆ THREAD_CPU_CONTEXT [1/2]

#define THREAD_CPU_CONTEXT   0

Offset of cpu_context in task_struct.

◆ THREAD_CPU_CONTEXT [2/2]

#define THREAD_CPU_CONTEXT   0

Offset of cpu_context in task_struct.

◆ THREAD_SIZE

#define THREAD_SIZE   4096

Defines size of each process.

Equal to the Page size ( PAGE_SIZE).

Function Documentation

◆ copy_process()

int copy_process ( uint64_t  fn,
uint64_t  arg 
)

Function for creating a new process.

Parameters
fnA function to execute in a new thread.
argAn argument that need to be passed to this function.
Returns
  • On success: 0
  • On failure: 1

Allocates a new task_struct and makes it available for the scheduler.

See also
task_struct, get_free_page()

◆ cpu_switch_to()

void cpu_switch_to ( task_struct prev,
task_struct next 
)

Performs the context switch, by changing the registers.

Parameters
prevPointer to the task_struct of the task we are switching (Starting address of memory page of the new task).
nextPointer to the next task_struct.
See also
scheduler.S

◆ create_processes()

void create_processes ( size_t  proc_num)

Creates processes using copy_process().

Parameters
proc_numNumber of processes to fork.

Maximum number of processes that can be created is NR_TASKS.

See also
copy_process(), NR_TASKS, task_struct

◆ kill_processes()

void kill_processes ( )

Kills all running processes (except init_task).

Do for each process:

  • Get starting address of process.
  • Check that it is not the current process (init_task).
  • Free page.
  • Decrease number of processes (Decrease nr_tasks).
  • Remove task_struct from task array (Set task[pid] = 0).
See also
free_page(), preempt_disable(), task_struct

◆ preempt_disable()

void preempt_disable ( )

Disables preemption for current running process.

◆ preempt_enable()

void preempt_enable ( )

Enables preemption for current running process.

◆ ret_from_fork()

void ret_from_fork ( )

Starting point for each new task.

PC register (cpu_context.pc) of each new task points here.

See also
scheduler.S

◆ schedule()

void schedule ( )

Main scheduler function.

Wrapper for core scheduler function _schedule(). Checks whether there is a new task, that needs to preempt the current one.
Gets called:

  • Explicitly by another task.
  • From timer interrupt handler.
See also
switch_to(), task, preempt_disable(), preempt_enable()

◆ schedule_tail()

void schedule_tail ( )

Tail function that enables preemption for current process.

It is the last function a new task calls, after its initialization, in order for it to be rescheduled.

See also
preempt_enable()

◆ switch_to()

void switch_to ( task_struct next)

Function that switches from current to the next task.

Parameters
nextA task_struct pointer, that points to the next task.

Calls cpu_next_to, in order to perform the necessary context switch.

See also
task_struct

◆ timer_tick()

void timer_tick ( )

Calls _schedule() after a time interval.

Gets called from System Timer interrupt handler.

See also
handle_timer_3_irq(), _schedule()

Variable Documentation

◆ current

task_struct* current

Pointer that points to the currently executing task.

Holds a pointer to the init_task.

◆ nr_tasks

int nr_tasks

Number of currently running tasks.

Number of currently running tasks = 1 (init_task)

◆ task

Array that holds all the tasks.

Appends a pointer to the init_task.