diff --git a/kernel.c b/kernel.c index 95352b3..b83bbb0 100644 --- a/kernel.c +++ b/kernel.c @@ -7,7 +7,7 @@ void read_binary_table(); -extern ProcessControlBlock processes[PROCESS_COUNT]; +extern struct process_control_block processes[PROCESS_COUNT]; // this array is populated when the memory image is built, therefore it should // resign in a section which is not overwritten with zeros on startup diff --git a/kinclude/ecall.c b/kinclude/ecall.c index 6474a4b..98e3ced 100644 --- a/kinclude/ecall.c +++ b/kinclude/ecall.c @@ -5,7 +5,7 @@ #include "io.h" // this type is only used here, therefore we don't need it in the ktypes header -typedef optional_int (*ecall_handler)(int*, ProcessControlBlock*); +typedef optional_int (*ecall_handler)(int*, struct process_control_block*); ecall_handler ecall_table[ECALL_TABLE_LEN] = { 0 }; @@ -14,7 +14,7 @@ ecall_handler ecall_table[ECALL_TABLE_LEN] = { 0 }; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" -optional_int ecall_handle_spawn_thread(int* args_ptr, ProcessControlBlock* pcb) +optional_int ecall_handle_spawn_thread(int* args_ptr, struct process_control_block* pcb) { void* entry = (void*) args_ptr[0]; // a0 void* args = (void*) args_ptr[1]; // a1 @@ -28,7 +28,7 @@ optional_int ecall_handle_spawn_thread(int* args_ptr, ProcessControlBlock* pcb) return (optional_int) { .value = pcb_or_err.value->pid }; } -optional_int ecall_handle_sleep(int* args, ProcessControlBlock* pcb) +optional_int ecall_handle_sleep(int* args, struct process_control_block* pcb) { int len = args[0]; @@ -44,11 +44,11 @@ optional_int ecall_handle_sleep(int* args, ProcessControlBlock* pcb) return (optional_int) { .value = 0 }; } -optional_int ecall_handle_join(int* args, ProcessControlBlock* pcb) +optional_int ecall_handle_join(int* args, struct process_control_block* pcb) { int pid = args[0]; // a0 - ProcessControlBlock* target = process_from_pid(pid); + struct process_control_block* target = process_from_pid(pid); if (target == NULL) return (optional_int) { .error = ESRCH }; @@ -71,10 +71,10 @@ optional_int ecall_handle_join(int* args, ProcessControlBlock* pcb) return (optional_int) { .value = 0 }; } -optional_int ecall_handle_kill(int* args, ProcessControlBlock* pcb) +optional_int ecall_handle_kill(int* args, struct process_control_block* pcb) { int pid = args[0]; - ProcessControlBlock* target = process_from_pid(pid); + struct process_control_block* target = process_from_pid(pid); // return error if no process has that id if (target == NULL) @@ -95,7 +95,7 @@ optional_int ecall_handle_kill(int* args, ProcessControlBlock* pcb) return (optional_int) { .value = 1 }; } -optional_int ecall_handle_exit(int* args, ProcessControlBlock* pcb) +optional_int ecall_handle_exit(int* args, struct process_control_block* pcb) { pcb->status = PROC_DEAD; pcb->exit_code = *args; @@ -120,7 +120,7 @@ optional_int ecall_handle_exit(int* args, ProcessControlBlock* pcb) void trap_handle_ecall() { mark_ecall_entry(); - ProcessControlBlock* pcb = get_current_process(); + struct process_control_block* pcb = get_current_process(); int *regs = pcb->regs; int code = regs[REG_A0 + 7]; // code is stored inside a7 @@ -199,7 +199,7 @@ void init_ecall_table() void handle_exception(int ecode, int mtval) { // kill off offending process - ProcessControlBlock* pcb = get_current_process(); + struct process_control_block* pcb = get_current_process(); pcb->status = PROC_DEAD; pcb->exit_code = -99; diff --git a/kinclude/ktypes.h b/kinclude/ktypes.h index 083f9d7..642ccc5 100644 --- a/kinclude/ktypes.h +++ b/kinclude/ktypes.h @@ -28,21 +28,21 @@ enum process_status { }; // forward define structs for recursive references -typedef struct ProcessControlBlock ProcessControlBlock; +struct process_control_block; struct loaded_binary; -struct ProcessControlBlock { +struct process_control_block { int pid; int pc; int regs[31]; int exit_code; // scheduling information enum process_status status; - ProcessControlBlock* waiting_for_process; + struct process_control_block* waiting_for_process; struct loaded_binary* binary; unsigned long long int asleep_until; // parent - ProcessControlBlock* parent; + struct process_control_block* parent; void* stack_top; }; @@ -98,7 +98,7 @@ typedef struct loaded_binary { // define some type aliases that only contain ascii character // they are used in the name of the struct optional_ typedef void* voidptr; -typedef ProcessControlBlock* pcbptr; +typedef struct process_control_block* pcbptr; // size_t is another standard type typedef unsigned int size_t; diff --git a/kinclude/sched.c b/kinclude/sched.c index f01ddbe..63d41e0 100644 --- a/kinclude/sched.c +++ b/kinclude/sched.c @@ -11,9 +11,9 @@ extern void memset(int, void*, void*); extern int thread_finalizer; // process list, holds all active and some dead processes -ProcessControlBlock processes[PROCESS_COUNT]; +struct process_control_block processes[PROCESS_COUNT]; // pointer to the currently scheduled process -ProcessControlBlock* current_process = NULL; +struct process_control_block* current_process = NULL; // timer variables to add kernel time back to the processes time slice unsigned long long int scheduling_interrupted_start; unsigned long long int next_interrupt_scheduled_for; @@ -35,7 +35,7 @@ void scheudler_init() } // try to return to a process -void scheduler_try_return_to(ProcessControlBlock* pcb) +void scheduler_try_return_to(struct process_control_block* pcb) { // if the process isn't ready, schedule a new one if (pcb->status != PROC_RDY) { @@ -58,15 +58,15 @@ void scheduler_try_return_to(ProcessControlBlock* pcb) } // select a new process to run next -ProcessControlBlock* scheduler_select_free() +struct process_control_block* scheduler_select_free() { unsigned long long int mtime; - int timeout_available = false; // note if a timeout is available + int timeout_available = 0; // note if a timeout is available - while (true) { + while (1) { mtime = read_time(); // start at the last scheduled process - ProcessControlBlock* pcb = current_process; + struct process_control_block* pcb = current_process; // iterate once over the whole list do { @@ -122,7 +122,7 @@ ProcessControlBlock* scheduler_select_free() } } -void scheduler_switch_to(ProcessControlBlock* pcb) +void scheduler_switch_to(struct process_control_block* pcb) { CSR_WRITE(CSR_MEPC, pcb->pc); @@ -167,7 +167,7 @@ void scheduler_switch_to(ProcessControlBlock* pcb) __builtin_unreachable(); } -ProcessControlBlock* process_from_pid(int pid) +struct process_control_block* process_from_pid(int pid) { for (int i = 0; i < PROCESS_COUNT; i++) { if (processes[i].pid == pid) @@ -181,7 +181,7 @@ int* get_current_process_registers() return current_process->regs; } -ProcessControlBlock* get_current_process() +struct process_control_block* get_current_process() { return current_process; } @@ -201,7 +201,7 @@ optional_pcbptr find_available_pcb_slot() { static int index = 0; int start_index = index; - ProcessControlBlock* pcb = processes + index; + struct process_control_block* pcb = processes + index; while (pcb->status != PROC_DEAD) { index = (index + 1) % PROCESS_COUNT; @@ -234,7 +234,7 @@ optional_pcbptr create_new_process(loaded_binary* bin, int stack_size) return (optional_pcbptr) { .error = stack_top_or_err.error }; } - ProcessControlBlock* pcb = slot_or_err.value; + struct process_control_block* pcb = slot_or_err.value; // determine next pid int pid = next_process_id++; @@ -259,7 +259,7 @@ optional_pcbptr create_new_process(loaded_binary* bin, int stack_size) return (optional_pcbptr) { .value = pcb }; } -optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint, void* args, int stack_size) +optional_pcbptr create_new_thread(struct process_control_block* parent, void* entrypoint, void* args, int stack_size) { // try to get a position in the processes list optional_pcbptr slot_or_err = find_available_pcb_slot(); @@ -279,7 +279,7 @@ optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint, return (optional_pcbptr) { .error = stack_top_or_err.error }; } - ProcessControlBlock* pcb = slot_or_err.value; + struct process_control_block* pcb = slot_or_err.value; // determine next pid int pid = next_process_id++; @@ -308,10 +308,10 @@ optional_pcbptr create_new_thread(ProcessControlBlock* parent, void* entrypoint, return (optional_pcbptr) { .value = pcb }; } -void kill_child_processes(ProcessControlBlock* pcb) +void kill_child_processes(struct process_control_block* pcb) { for (int i = 0; i < PROCESS_COUNT; i++) { - ProcessControlBlock* proc = processes + i; + struct process_control_block* proc = processes + i; // if this is not a child process or already exited if (proc->parent != pcb || proc->status == PROC_DEAD) continue; @@ -322,7 +322,7 @@ void kill_child_processes(ProcessControlBlock* pcb) } } -void destroy_process(ProcessControlBlock* pcb) +void destroy_process(struct process_control_block* pcb) { // kill child processes kill_child_processes(pcb); diff --git a/kinclude/sched.h b/kinclude/sched.h index bceb069..da549ff 100644 --- a/kinclude/sched.h +++ b/kinclude/sched.h @@ -5,23 +5,23 @@ #include "ktypes.h" // scheduling data: -extern ProcessControlBlock processes[PROCESS_COUNT]; +extern struct process_control_block processes[PROCESS_COUNT]; // scheduler methods void scheudler_init(); -ProcessControlBlock* scheduler_select_free(); +struct process_control_block* scheduler_select_free(); void set_next_interrupt(); void __attribute__((noreturn)) scheduler_run_next(); -void __attribute__((noreturn)) scheduler_try_return_to(ProcessControlBlock*); -void __attribute__((noreturn)) scheduler_switch_to(ProcessControlBlock*); -ProcessControlBlock* process_from_pid(int pid); +void __attribute__((noreturn)) scheduler_try_return_to(struct process_control_block*); +void __attribute__((noreturn)) scheduler_switch_to(struct process_control_block*); +struct process_control_block* process_from_pid(int pid); int* get_current_process_registers(); -ProcessControlBlock* get_current_process(); +struct process_control_block* get_current_process(); void mark_ecall_entry(); // process creation / destruction optional_pcbptr create_new_process(loaded_binary*, int); -optional_pcbptr create_new_thread(ProcessControlBlock*, void*, void*, int); -void destroy_process(ProcessControlBlock* pcb); -void kill_child_processes(ProcessControlBlock* pcb); +optional_pcbptr create_new_thread(struct process_control_block*, void*, void*, int); +void destroy_process(struct process_control_block* pcb); +void kill_child_processes(struct process_control_block* pcb); #endif