fix: simple multitasking work !!!!

add a stack at every task, and execute task while multitasking switching
This commit is contained in:
Starnakin 2025-01-24 12:33:29 +01:00
parent dc84d5856b
commit 95fec015f2
5 changed files with 40 additions and 24 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include "list.h"
#include "memory.h"
#include <stdint.h>
@ -9,6 +10,8 @@ extern struct task *current_task;
enum status { ZOMBIE, THREAD, RUN };
enum owner { OWNER_KERNEL, OWNER_USER };
#define STACK_SIZE PAGE_SIZE * 4
struct task {
uint32_t *esp;
uint32_t *esp0;

View File

@ -41,6 +41,7 @@ static void clock_handler(struct registers *regs)
if (scheduler_counter % 10 == 0) {
cli();
scheduler();
sti();
}
scheduler_counter++;
sleep_counter--;

View File

@ -43,8 +43,9 @@ static void owo(void)
static void awa(void)
{
kprintf("awa\n");
while (true)
; // kprintf("awa\n");
;
}
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
@ -62,5 +63,8 @@ void kernel_main(multiboot_info_t *mbd, uint32_t magic)
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */
create_kernel_task();
exec_fn(awa);
exec_fn(awa);
exec_fn(awa);
exec_fn(awa);
shell_init();
}

View File

@ -14,41 +14,40 @@ switch_to_task:
// save the current stack pointer to the old stack
mov [eax+0], esp
// save the old eip pointer
mov ebx, [[[esp_backup]]]
mov [eax+16], ebx
// stack pointer + the 4 regs pushed
// and + 1 to get the argument (next task)
mov esi, [esp+(4+1)*4]
mov [current_task], esi
mov edi, [current_task]
mov eax, [current_task]
mov esp, [edi+0] // esp
mov eax, [edi+4] // esp0
// mov ebx, [edi+8] // cr3
// mov edi, [edi+12] // page_directory
// mov [page_directory], edi
// mov [TSS+4], eax // tss.esp0
// mov ecx, cr3
mov esp, [eax+0] // get esp
// if cr3 hasn't change, do nothing
// cmp ecx, ebx
// je .END
// mov cr3, ebx
.END:
mov eax, [edi+16]
mov ebx, [esp_backup]
mov [ebx], eax
pop esi
pop edi
pop ebp
pop ebx
mov ecx, 0
cmp [eax+16], ecx
je .END
sti
push [eax+16] // store func_ptr
mov [eax+16], ecx // clear eip in current_task
push 0
call pic_send_eoi
pop edx // remove pic_send_eoi argument
pop edx // get func_ptr
call edx
// TODO HANDLE TASK RETURN
.END:
ret

View File

@ -19,9 +19,16 @@ static struct task *create_task(uint8_t owner_id)
new_task->prev = current_task;
current_task->next = new_task;
new_task->owner_id = owner_id;
new_task->esp0 = alloc_pages(STACK_SIZE, NULL);
if (!new_task->esp0) {
vfree(new_task);
return NULL;
}
new_task->esp = new_task->esp0 + STACK_SIZE;
new_task->pid = pid++;
new_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
if (!new_task->heap) {
free_pages(new_task->esp0, STACK_SIZE);
vfree(new_task);
return NULL;
}
@ -33,9 +40,11 @@ int create_kernel_task(void)
struct task *new_task = vmalloc(sizeof(struct task));
if (!new_task)
return -1;
new_task->status = RUN;
new_task->owner_id = 0;
new_task->pid = 0;
new_task->heap = page_directory;
new_task->cr3 = page_directory - KERNEL_START;
new_task->prev = new_task;
new_task->next = new_task;
current_task = new_task;