add: kernel task

This commit is contained in:
Starnakin 2025-01-20 15:11:22 +01:00
parent b3be29246e
commit d889a251ef
5 changed files with 34 additions and 21 deletions

View File

@ -28,3 +28,4 @@ struct task {
void scheduler(void); void scheduler(void);
void switch_to_task(struct task *next_task); void switch_to_task(struct task *next_task);
void exec_fn(void (*fn)(void)); void exec_fn(void (*fn)(void));
int create_kernel_task(void);

View File

@ -32,20 +32,19 @@
static void uwu(void) static void uwu(void)
{ {
sleep(1000);
kprintf("uwu\n"); kprintf("uwu\n");
} }
static void owo(void) static void owo(void)
{ {
sleep(1000); while (true)
kprintf("owo\n"); kprintf("owo\n");
} }
static void awa(void) static void awa(void)
{ {
sleep(1000); while (true)
kprintf("awa\n"); ; // kprintf("awa\n");
} }
void kernel_main(multiboot_info_t *mbd, uint32_t magic) 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 " /* "complex 8*unknown quantity -byte descriptor table. -- Troy "
*/ */
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */ /* "Martin 03:50, 22 March 2009 (UTC)\n"); */
exec_fn(uwu); create_kernel_task();
exec_fn(owo);
exec_fn(awa); exec_fn(awa);
// exec_fn(owo);
shell_init(); shell_init();
} }

View File

@ -1,3 +1,4 @@
#include "debug.h"
#include "kprintf.h" #include "kprintf.h"
#include "task.h" #include "task.h"
#include "time.h" #include "time.h"
@ -6,11 +7,9 @@ struct task *current_task;
void scheduler(void) void scheduler(void)
{ {
kprintf("camille mon bebou\n");
sleep(1000);
if (!current_task) if (!current_task)
return; return;
struct task *it = current_task; struct task *it = current_task->next;
while (it->status != RUN) while (it->status != RUN)
it = it->next; it = it->next;
switch_to_task(it); switch_to_task(it);

View File

@ -11,6 +11,8 @@ switch_to_task:
// save the current stack pointer to the old stack // save the current stack pointer to the old stack
mov [current_task], esp mov [current_task], esp
mov edi, [eip_backup]
mov [current_task+16], edi // save instruction before Interrupt
// stack pointer + the 4 regs pushed // stack pointer + the 4 regs pushed
// and + 1 to get the argument (next task) // and + 1 to get the argument (next task)
@ -26,7 +28,9 @@ switch_to_task:
// if cr3 hasn't change, do nothing // if cr3 hasn't change, do nothing
cmp ecx, ebx cmp ecx, ebx
je .END je .END
mov cr3, ebx // mov cr3, ebx
// TODO replace the eip store in stack by the [current_task+16](current_task->eip)
.END: .END:
pop esi pop esi
@ -34,6 +38,4 @@ switch_to_task:
pop ebp pop ebp
pop ebx pop ebx
push [eip_backup] ret // this will also change eip to the next task's instructions
iretd // this will also change eip to the next task's instructions

View File

@ -1,6 +1,8 @@
#include "task.h" #include "task.h"
#include "alloc.h" #include "alloc.h"
#include "debug.h"
#include "kpanic.h" #include "kpanic.h"
#include "kprintf.h"
#include "memory.h" #include "memory.h"
uint32_t eip_backup; uint32_t eip_backup;
@ -21,6 +23,20 @@ static struct task *create_task(uint8_t owner_id)
return new_task; 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)) void exec_fn(void (*fn)(void))
{ {
struct task *new_task = create_task(OWNER_KERNEL); struct task *new_task = create_task(OWNER_KERNEL);
@ -28,13 +44,9 @@ void exec_fn(void (*fn)(void))
kpanic("failed to create new task"); kpanic("failed to create new task");
new_task->status = RUN; new_task->status = RUN;
new_task->eip = (uint32_t *)fn; new_task->eip = (uint32_t *)fn;
new_task->next = current_task; new_task->prev = current_task;
new_task->prev = new_task; new_task->next = current_task->next;
if (current_task) { current_task->next = new_task;
new_task->prev = current_task->prev;
current_task->prev = new_task;
}
current_task = new_task;
} }
/* /*