diff --git a/arch/x86_64/cpu/panic.c b/arch/x86_64/cpu/panic.c index 8498eb7..fdb1d69 100644 --- a/arch/x86_64/cpu/panic.c +++ b/arch/x86_64/cpu/panic.c @@ -25,9 +25,7 @@ void panic(const char *reason, int_frame_t frame) draw_image(img, 0, 0, 0); } - nighterm_set_cursor_position(0, 20); - - printf("\tComputer no workie :( \n\n"); + nighterm_set_cursor_position(0, 24); printf("\t* %s\n\n", reason); diff --git a/arch/x86_64/idt/idt.h b/arch/x86_64/idt/idt.h index eb876c2..5faa121 100644 --- a/arch/x86_64/idt/idt.h +++ b/arch/x86_64/idt/idt.h @@ -3,22 +3,25 @@ #include -typedef struct { - uint16_t offset_low; - uint16_t selector; - uint8_t ist; - uint8_t flags; - uint16_t offset_middle; - uint32_t offset_high; - uint32_t zero; +typedef struct +{ + uint16_t offset_low; + uint16_t selector; + uint8_t ist; + uint8_t flags; + uint16_t offset_middle; + uint32_t offset_high; + uint32_t zero; } __attribute__((packed)) idt_entry_t; -typedef struct { - uint16_t limit; - uint64_t base; +typedef struct +{ + uint16_t limit; + uint64_t base; } __attribute__((packed)) idt_pointer_t; -typedef struct { +typedef struct +{ uint64_t r15; uint64_t r14; uint64_t r13; @@ -49,7 +52,7 @@ void load_idt(uint64_t); void trigger_interupt(uint64_t a); void set_idt_gate(int num, uint64_t base, uint16_t sel, uint8_t flags); void init_idt(); -void irq_register(uint8_t irq, void* handler); +void irq_register(uint8_t irq, void *handler); void irq_deregister(uint8_t irq); #endif /* IDT_H */ \ No newline at end of file diff --git a/kernel/entry/kernel.c b/kernel/entry/kernel.c index 30e059b..81aded0 100644 --- a/kernel/entry/kernel.c +++ b/kernel/entry/kernel.c @@ -31,18 +31,15 @@ return codes) #include #include +void test() +{ + printf("Hello from PID 0!\n"); +} + int main() { keyboard.out = false; register_pci(); - printf("-------------------------\n\n"); - vfs_op_status status; - char *motd; - status = driver_read(vfs, 0, "/etc/motd", &motd); - - if (status != STATUS_OK) - return KERNEL_QUIT_ERROR; - - printf("%s\n", motd); + spawn_process(0, test); return KERNEL_QUIT_HANG; } \ No newline at end of file diff --git a/kernel/system/pit/pit.c b/kernel/system/pit/pit.c index 07e094f..9973778 100644 --- a/kernel/system/pit/pit.c +++ b/kernel/system/pit/pit.c @@ -65,7 +65,6 @@ void pit_set_count(uint16_t count) void pit_handler(int_frame_t *frame) { cur_frame = frame; - // update_wm(); pit_int(); } diff --git a/kernel/system/processes/processes.c b/kernel/system/processes/processes.c index 24ad4ab..03ea406 100644 --- a/kernel/system/processes/processes.c +++ b/kernel/system/processes/processes.c @@ -2,22 +2,85 @@ #include #include -struct Process processes[MAX_PROCESSES]; - -int_frame_t *cur_ctx = NULL; +struct Process *processes[MAX_PROCESSES]; void switch_context(uint16_t pid) { - (void)pid; + if (processes[pid] == NULL || processes[pid]->context == NULL) + { + dprintf("[\e[0;31mProcesses\e[0m] Process with PID \"%d\" does not exist or has no context!", pid); + return; + } + + if (cur_frame != NULL) + { + memcpy(cur_frame, &processes[pid]->context, sizeof(int_frame_t)); + } + + cur_frame = processes[pid]->context; + memcpy(&processes[pid]->context, cur_frame, sizeof(int_frame_t)); } void spawn_process(uint16_t pid, void *entry) { - (void)pid; - (void)entry; + if (pid >= MAX_PROCESSES) + { + dprintf("[\e[0;31mProcesses\e[0m] PID \"%d\" exceeds maximum allowed value!", pid); + return; + } + + if (processes[pid] != NULL) + { + dprintf("[\e[0;31mProcesses\e[0m] PID \"%d\" is currently taken!", pid); + return; + } + + struct Process *temp = (struct Process *)malloc(sizeof(struct Process)); + if (temp == NULL) + { + dprintf("[\e[0;31mProcesses\e[0m] Failed to allocate memory for process!"); + return; + } + + temp->context = (int_frame_t *)malloc(sizeof(int_frame_t)); + if (temp->context == NULL) + { + dprintf("[\e[0;31mProcesses\e[0m] Failed to allocate memory for process context!"); + free(temp); + return; + } + + temp->id = pid; + temp->context->rip = (uint64_t)entry; + temp->context->rsp = (uint64_t)malloc(1); + + if (temp->context->rsp == 0) + { + dprintf("[\e[0;31mProcesses\e[0m] Failed to allocate stack memory for process!"); + free(temp->context); + free(temp); + return; + } + + processes[pid] = temp; + dprintf("[\e[0;32mProcesses\e[0m] Spawned process with PID \"%d\"!\n", pid); + switch_context(pid); } void kill_process(uint16_t pid) { - (void)pid; + if (pid >= MAX_PROCESSES) + { + dprintf("[\e[0;31mProcesses\e[0m] PID \"%d\" exceeds maximum allowed value!", pid); + return; + } + + if (processes[pid] == NULL) + { + dprintf("[\e[0;31mProcesses\e[0m] No process found with PID \"%d\"", pid); + return; + } + + free(processes[pid]); + processes[pid] = NULL; } \ No newline at end of file diff --git a/kernel/system/processes/processes.h b/kernel/system/processes/processes.h index b6571e2..1ad5b73 100644 --- a/kernel/system/processes/processes.h +++ b/kernel/system/processes/processes.h @@ -13,7 +13,7 @@ struct Process int_frame_t *context; }; -extern struct Process processes[MAX_PROCESSES]; +extern struct Process *processes[MAX_PROCESSES]; void switch_context(uint16_t pid);