From d889a251ef60187b6e99eeb2bf33a6295145ab25 Mon Sep 17 00:00:00 2001 From: Starnakin Date: Mon, 20 Jan 2025 15:11:22 +0100 Subject: [PATCH] add: kernel task --- headers/task.h | 1 + src/kernel.c | 13 ++++++------- src/multitasking/scheduler.c | 5 ++--- src/multitasking/switch_to_task.s | 10 ++++++---- src/multitasking/task.c | 26 +++++++++++++++++++------- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/headers/task.h b/headers/task.h index 4ce9f9b..cec7e88 100644 --- a/headers/task.h +++ b/headers/task.h @@ -28,3 +28,4 @@ struct task { void scheduler(void); void switch_to_task(struct task *next_task); void exec_fn(void (*fn)(void)); +int create_kernel_task(void); diff --git a/src/kernel.c b/src/kernel.c index 95c8d22..4ac2a46 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -32,20 +32,19 @@ static void uwu(void) { - sleep(1000); kprintf("uwu\n"); } static void owo(void) { - sleep(1000); - kprintf("owo\n"); + while (true) + kprintf("owo\n"); } static void awa(void) { - sleep(1000); - kprintf("awa\n"); + while (true) + ; // kprintf("awa\n"); } void kernel_main(multiboot_info_t *mbd, uint32_t magic) @@ -61,8 +60,8 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic) /* "complex 8*unknown quantity -byte descriptor table. -- Troy " */ /* "Martin 03:50, 22 March 2009 (UTC)\n"); */ - exec_fn(uwu); - exec_fn(owo); + create_kernel_task(); exec_fn(awa); + // exec_fn(owo); shell_init(); } diff --git a/src/multitasking/scheduler.c b/src/multitasking/scheduler.c index 1ee506e..0b51fa0 100644 --- a/src/multitasking/scheduler.c +++ b/src/multitasking/scheduler.c @@ -1,3 +1,4 @@ +#include "debug.h" #include "kprintf.h" #include "task.h" #include "time.h" @@ -6,11 +7,9 @@ struct task *current_task; void scheduler(void) { - kprintf("camille mon bebou\n"); - sleep(1000); if (!current_task) return; - struct task *it = current_task; + struct task *it = current_task->next; while (it->status != RUN) it = it->next; switch_to_task(it); diff --git a/src/multitasking/switch_to_task.s b/src/multitasking/switch_to_task.s index 8310729..5512de4 100644 --- a/src/multitasking/switch_to_task.s +++ b/src/multitasking/switch_to_task.s @@ -11,6 +11,8 @@ switch_to_task: // save the current stack pointer to the old stack mov [current_task], esp + mov edi, [eip_backup] + mov [current_task+16], edi // save instruction before Interrupt // stack pointer + the 4 regs pushed // and + 1 to get the argument (next task) @@ -26,7 +28,9 @@ switch_to_task: // if cr3 hasn't change, do nothing cmp ecx, ebx je .END - mov cr3, ebx +// mov cr3, ebx + + // TODO replace the eip store in stack by the [current_task+16](current_task->eip) .END: pop esi @@ -34,6 +38,4 @@ switch_to_task: pop ebp pop ebx - push [eip_backup] - - iretd // this will also change eip to the next task's instructions + ret // this will also change eip to the next task's instructions diff --git a/src/multitasking/task.c b/src/multitasking/task.c index ff5ba06..4a9453a 100644 --- a/src/multitasking/task.c +++ b/src/multitasking/task.c @@ -1,6 +1,8 @@ #include "task.h" #include "alloc.h" +#include "debug.h" #include "kpanic.h" +#include "kprintf.h" #include "memory.h" uint32_t eip_backup; @@ -21,6 +23,20 @@ static struct task *create_task(uint8_t owner_id) return new_task; } +int create_kernel_task(void) +{ + struct task *new_task = vmalloc(sizeof(struct task)); + if (!new_task) + return 1; + new_task->owner_id = 0; + new_task->pid = 0; + new_task->heap = page_directory; + new_task->prev = new_task; + new_task->next = new_task; + current_task = new_task; + return 0; +} + void exec_fn(void (*fn)(void)) { struct task *new_task = create_task(OWNER_KERNEL); @@ -28,13 +44,9 @@ void exec_fn(void (*fn)(void)) kpanic("failed to create new task"); new_task->status = RUN; new_task->eip = (uint32_t *)fn; - new_task->next = current_task; - new_task->prev = new_task; - if (current_task) { - new_task->prev = current_task->prev; - current_task->prev = new_task; - } - current_task = new_task; + new_task->prev = current_task; + new_task->next = current_task->next; + current_task->next = new_task; } /*