LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
scheduler.c File Reference

(29312b1 fbconsole, panic: render panic after framebuffer was given to userspace - without clearing screen contents)

#include <scheduler.h>
#include <string.h>
#include <errno.h>
#include <mm.h>
#include <sc.h>
#include <bitmap.h>
#include <panic.h>
#include <log.h>
#include <mutex.h>
#include <mq.h>
#include <signal.h>
#include <sd.h>
#include <unused_param.h>
+ Include dependency graph for scheduler.c:

Go to the source code of this file.

Data Structures

struct  process_t
 

Macros

#define MAX_PROCS   4096
 

Enumerations

enum  process_state {
  process_state_empty = 0 , process_state_waiting , process_state_runnable , process_state_running ,
  process_state_exited , process_state_killed
}
 

Functions

void sc_handle_clock_read (uint64_t *nanoseconds)
 
void * process_alloc (allocator_t *alloc, size_t size)
 
void process_dealloc (allocator_t *alloc, void *ptr)
 
 __attribute__ ((naked))
 
void init_scheduler (void)
 
pid_t free_pid (void)
 
pid_t setup_process (void)
 
void start_task (struct vm_table *context, ptr_t entry, ptr_t data_start, ptr_t data_end, const char *name)
 
void scheduler_process_save (cpu_state *cpu)
 
bool scheduler_idle_if_needed (cpu_state **cpu, struct vm_table **context)
 
void schedule_next (cpu_state **cpu, struct vm_table **context)
 
bool schedule_next_if_needed (cpu_state **cpu, struct vm_table **context)
 
void scheduler_process_cleanup (pid_t pid)
 
void scheduler_kill_current (enum kill_reason reason)
 
void sc_handle_scheduler_exit (uint64_t exit_code)
 
void sc_handle_scheduler_clone (bool share_memory, ptr_t entry, pid_t *newPid)
 
bool scheduler_handle_pf (ptr_t fault_address, uint64_t error_code)
 
void scheduler_wait_for (pid_t pid, enum wait_reason reason, union wait_data data)
 
void scheduler_waitable_done (enum wait_reason reason, union wait_data data, size_t max_amount)
 
void sc_handle_memory_sbrk (int64_t inc, ptr_t *data_end)
 
void sc_handle_scheduler_sleep (uint64_t nanoseconds)
 
void sc_handle_hardware_ioperm (uint16_t from, uint16_t num, bool turn_on, uint16_t *error)
 
void sc_handle_hardware_interrupt_notify (uint8_t interrupt, bool enable, uint64_t mq, uint64_t *error)
 
void sc_handle_ipc_mq_create (bool global_read, bool global_write, size_t msg_limit, size_t data_limit, uint64_t *mq, uint64_t *error)
 
void sc_handle_ipc_mq_destroy (uint64_t mq, uint64_t *error)
 
void sc_handle_ipc_mq_poll (uint64_t mq, bool wait, struct Message *msg, uint64_t *error)
 
void sc_handle_ipc_mq_send (uint64_t mq, pid_t pid, struct Message *msg, uint64_t *error)
 
void sc_handle_ipc_service_register (uuid_t *uuid, uint64_t mq, uint64_t *error)
 
void sc_handle_ipc_service_discover (uuid_t *uuid, uint64_t mq, struct Message *msg, uint64_t *error)
 
void sc_handle_scheduler_get_pid (bool parent, pid_t *pid)
 
ptr_t scheduler_map_hardware (ptr_t hw, size_t len)
 Map a given memory area in the currently running userspace process at a random location.
 

Variables

volatile pid_t scheduler_current_process = -1
 
static process_t processes [MAX_PROCS]
 

Data Structure Documentation

◆ process_t

struct process_t

Definition at line 26 of file scheduler.c.

+ Collaboration diagram for process_t:
Data Fields
size_t allocatedMemory
allocator_t allocator
struct vm_table * context
cpu_state cpu
uint8_t exit_code
region_t heap
region_t hw
ptr_t iopb Physical address of the first of two IOPB pages, 0 if no IO privilege granted.
mq_id_t mq
char name[1024]
pid_t parent
region_t stack
process_state state
union wait_data waiting_data
enum wait_reason waiting_for

Macro Definition Documentation

◆ MAX_PROCS

#define MAX_PROCS   4096

Definition at line 53 of file scheduler.c.

Enumeration Type Documentation

◆ process_state

Enumerator
process_state_empty 
process_state_waiting 
process_state_runnable 
process_state_running 
process_state_exited 
process_state_killed 

Definition at line 17 of file scheduler.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (naked)  )

Definition at line 83 of file scheduler.c.

◆ free_pid()

pid_t free_pid ( void  )

Definition at line 91 of file scheduler.c.

References MAX_PROCS, process_state_empty, processes, and state.

+ Here is the caller graph for this function:

◆ init_scheduler()

void init_scheduler ( void  )

Definition at line 87 of file scheduler.c.

References MAX_PROCS, memset(), and processes.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ process_alloc()

void * process_alloc ( allocator_t alloc,
size_t  size 
)

Definition at line 56 of file scheduler.c.

References alloc, process_t::allocatedMemory, logw, process_state_empty, process_state_exited, process_state_killed, processes, size, process_t::state, allocator::tag, and vm_alloc().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ process_dealloc()

void process_dealloc ( allocator_t alloc,
void *  ptr 
)

Definition at line 71 of file scheduler.c.

References alloc, process_t::allocatedMemory, logw, process_state_empty, processes, size, process_t::state, allocator::tag, and vm_free().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sc_handle_clock_read()

void sc_handle_clock_read ( uint64_t nanoseconds)
extern

Definition at line 141 of file hpet.c.

+ Here is the caller graph for this function:

◆ sc_handle_hardware_interrupt_notify()

void sc_handle_hardware_interrupt_notify ( uint8_t  interrupt,
bool  enable,
uint64_t  mq,
uint64_t error 
)

Definition at line 478 of file scheduler.c.

References ENOENT, error, interrupt_add_queue(), interrupt_del_queue(), mq, process_t::mq, processes, and scheduler_current_process.

+ Here is the call graph for this function:

◆ sc_handle_hardware_ioperm()

void sc_handle_hardware_ioperm ( uint16_t  from,
uint16_t  num,
bool  turn_on,
uint16_t error 
)

Definition at line 436 of file scheduler.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, bitmap_clear(), bitmap_set(), error, process_t::iopb, KiB, memset(), mm_alloc_pages(), MM_FREE, mm_mark_physical_pages(), num, process, processes, scheduler_current_process, set_iopb(), and turn_on.

+ Here is the call graph for this function:

◆ sc_handle_ipc_mq_create()

void sc_handle_ipc_mq_create ( bool  global_read,
bool  global_write,
size_t  msg_limit,
size_t  data_limit,
uint64_t mq,
uint64_t error 
)

Definition at line 498 of file scheduler.c.

References data_limit, ENOSYS, error, global_write, mq, and UNUSED_PARAM.

◆ sc_handle_ipc_mq_destroy()

void sc_handle_ipc_mq_destroy ( uint64_t  mq,
uint64_t error 
)

Definition at line 508 of file scheduler.c.

References ENOSYS, error, mq, and UNUSED_PARAM.

◆ sc_handle_ipc_mq_poll()

void sc_handle_ipc_mq_poll ( uint64_t  mq,
bool  wait,
struct Message msg,
uint64_t error 
)

Definition at line 514 of file scheduler.c.

References EAGAIN, EMSGSIZE, ENOMSG, error, wait_data::message_queue, mq, process_t::mq, mq_peek(), mq_pop(), MT_Invalid, processes, scheduler_current_process, scheduler_wait_for(), Message::size, Message::type, wait, and wait_reason_message.

+ Here is the call graph for this function:

◆ sc_handle_ipc_mq_send()

void sc_handle_ipc_mq_send ( uint64_t  mq,
pid_t  pid,
struct Message msg,
uint64_t error 
)

Definition at line 549 of file scheduler.c.

References error, MAX_PROCS, mq, process_t::mq, mq_push(), pid, process_state_empty, processes, scheduler_current_process, Message::sender, and state.

+ Here is the call graph for this function:

◆ sc_handle_ipc_service_discover()

void sc_handle_ipc_service_discover ( uuid_t uuid,
uint64_t  mq,
struct Message msg,
uint64_t error 
)

Definition at line 576 of file scheduler.c.

References uuid_t::data, EINVAL, error, memcpy(), mq, Message::UserData::ServiceDiscoveryData::mq, process_t::mq, MT_ServiceDiscovery, processes, scheduler_current_process, sd_send(), Message::sender, Message::UserData::ServiceDiscovery, Message::UserData::ServiceDiscoveryData::serviceIdentifier, Message::type, and Message::user_data.

+ Here is the call graph for this function:

◆ sc_handle_ipc_service_register()

void sc_handle_ipc_service_register ( uuid_t uuid,
uint64_t  mq,
uint64_t error 
)

Definition at line 568 of file scheduler.c.

References error, mq, process_t::mq, processes, scheduler_current_process, and sd_register().

+ Here is the call graph for this function:

◆ sc_handle_memory_sbrk()

void sc_handle_memory_sbrk ( int64_t  inc,
ptr_t data_end 
)

Definition at line 396 of file scheduler.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, data_end, region_t::end, process_t::heap, memset(), mm_alloc_pages(), MM_FREE, mm_mark_physical_pages(), processes, scheduler_current_process, vm_context_get_physical_for_virtual(), vm_context_map(), and vm_context_unmap().

+ Here is the call graph for this function:

◆ sc_handle_scheduler_clone()

void sc_handle_scheduler_clone ( bool  share_memory,
ptr_t  entry,
pid_t newPid 
)

Definition at line 277 of file scheduler.c.

References process_t::context, process_t::cpu, region_t::end, ENOMEM, entry, process_t::heap, process_t::hw, memcpy(), process_t::name, pid, processes, scheduler_current_process, setup_process(), process_t::stack, region_t::start, strncpy(), vm_context_get_physical_for_virtual(), vm_context_map(), vm_context_new(), and vm_copy_range().

+ Here is the call graph for this function:

◆ sc_handle_scheduler_exit()

void sc_handle_scheduler_exit ( uint64_t  exit_code)

Definition at line 269 of file scheduler.c.

References process_t::exit_code, logd, name, process_state_exited, processes, scheduler_current_process, scheduler_process_cleanup(), and process_t::state.

+ Here is the call graph for this function:

◆ sc_handle_scheduler_get_pid()

void sc_handle_scheduler_get_pid ( bool  parent,
pid_t pid 
)

Definition at line 600 of file scheduler.c.

References process_t::parent, pid, processes, and scheduler_current_process.

◆ sc_handle_scheduler_sleep()

void sc_handle_scheduler_sleep ( uint64_t  nanoseconds)

Definition at line 422 of file scheduler.c.

References sc_handle_clock_read(), scheduler_current_process, scheduler_wait_for(), wait_data::timestamp_ns_since_boot, and wait_reason_time.

+ Here is the call graph for this function:

◆ schedule_next()

void schedule_next ( cpu_state **  cpu,
struct vm_table **  context 
)

Definition at line 181 of file scheduler.c.

References process_t::context, process_t::cpu, MAX_PROCS, pid, process, process_state_runnable, process_state_running, process_state_waiting, processes, sc_handle_clock_read(), scheduler_current_process, scheduler_idle_if_needed(), process_t::state, state, and wait_reason_time.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ schedule_next_if_needed()

bool schedule_next_if_needed ( cpu_state **  cpu,
struct vm_table **  context 
)

Definition at line 221 of file scheduler.c.

References process_state_runnable, process_state_running, processes, schedule_next(), scheduler_current_process, and state.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_handle_pf()

bool scheduler_handle_pf ( ptr_t  fault_address,
uint64_t  error_code 
)

Definition at line 334 of file scheduler.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, ALLOCATOR_REGION_USER_STACK, logw, memset(), mm_alloc_pages(), name, processes, scheduler_current_process, process_t::stack, region_t::start, and vm_context_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_idle_if_needed()

bool scheduler_idle_if_needed ( cpu_state **  cpu,
struct vm_table **  context 
)

Definition at line 164 of file scheduler.c.

References process_state_runnable, process_state_running, processes, scheduler_current_process, state, and VM_KERNEL_CONTEXT.

+ Here is the caller graph for this function:

◆ scheduler_kill_current()

void scheduler_kill_current ( enum kill_reason  reason)

Definition at line 261 of file scheduler.c.

References process_t::exit_code, logd, name, process_state_killed, processes, scheduler_current_process, scheduler_process_cleanup(), and process_t::state.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_map_hardware()

ptr_t scheduler_map_hardware ( ptr_t  hw,
size_t  len 
)

Map a given memory area in the currently running userspace process at a random location.

Definition at line 605 of file scheduler.c.

References region_t::end, process_t::hw, processes, scheduler_current_process, and vm_map_hardware().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_process_cleanup()

void scheduler_process_cleanup ( pid_t  pid)

Definition at line 232 of file scheduler.c.

References mq, process_t::mq, mq_destroy(), mq_push(), MT_Signal, mutex_unlock_holder(), process_t::parent, pid, process_state_empty, process_state_exited, process_state_killed, processes, Message::sender, SIGCHLD, Message::UserData::SignalUserData::signal, Message::UserData::Signal, size, Message::size, process_t::state, Message::type, Message::user_data, user_size, Message::user_size, vm_alloc(), and vm_free().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_process_save()

void scheduler_process_save ( cpu_state cpu)

Definition at line 155 of file scheduler.c.

References memcpy(), process_state_running, process_state_waiting, processes, scheduler_current_process, and state.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scheduler_wait_for()

void scheduler_wait_for ( pid_t  pid,
enum wait_reason  reason,
union wait_data  data 
)

Definition at line 353 of file scheduler.c.

References pid, process_state_waiting, processes, scheduler_current_process, process_t::state, process_t::waiting_data, and process_t::waiting_for.

+ Here is the caller graph for this function:

◆ scheduler_waitable_done()

void scheduler_waitable_done ( enum wait_reason  reason,
union wait_data  data,
size_t  max_amount 
)

Definition at line 363 of file scheduler.c.

References wait_data::condvar, MAX_PROCS, wait_data::message_queue, wait_data::mutex, mutex_lock(), pid, process_state_runnable, process_state_waiting, processes, scheduler_current_process, process_t::state, wait_reason_condvar, wait_reason_message, wait_reason_mutex, wait_reason_time, process_t::waiting_data, and process_t::waiting_for.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setup_process()

pid_t setup_process ( void  )

Definition at line 102 of file scheduler.c.

References ALLOCATOR_REGION_USER_HARDWARE, ALLOCATOR_REGION_USER_STACK, free_pid(), memset(), mq_create(), panic_message(), pid, process, process_alloc(), process_dealloc(), process_state_runnable, and processes.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ start_task()

void start_task ( struct vm_table context,
ptr_t  entry,
ptr_t  data_start,
ptr_t  data_end,
const char *  name 
)

Definition at line 136 of file scheduler.c.

References ALLOCATOR_REGION_USER_STACK, data_end, entry, name, panic_message(), process_t::parent, pid, process, processes, setup_process(), and strncpy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ processes

process_t processes[MAX_PROCS]
static

Definition at line 54 of file scheduler.c.

◆ scheduler_current_process

volatile pid_t scheduler_current_process = -1

Definition at line 50 of file scheduler.c.