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 switch_to_task(struct task *next_task);
void exec_fn(void (*fn)(void));
int create_kernel_task(void);

View File

@ -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();
}

View File

@ -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);

View File

@ -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

View File

@ -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;
}
/*