wip: scheduler needs to change eip or smth in order to call the fn

This commit is contained in:
2025-01-17 12:56:05 +01:00
parent b9691b1948
commit fa3ef311ad
9 changed files with 95 additions and 14 deletions

View File

@ -10,7 +10,9 @@
#include "power.h"
#include "shell.h"
#include "string.h"
#include "task.h"
#include "terminal.h"
#include "time.h"
#include "vbe.h"
#include <stdbool.h>
@ -28,6 +30,24 @@
#error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif
static void uwu(void)
{
sleep(1000);
kprintf("uwu\n");
}
static void owo(void)
{
sleep(1000);
kprintf("owo\n");
}
static void awa(void)
{
sleep(1000);
kprintf("awa\n");
}
void kernel_main(multiboot_info_t *mbd, uint32_t magic)
{
terminal_initialize();
@ -41,5 +61,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);
exec_fn(awa);
shell_init();
}

View File

@ -32,7 +32,7 @@ static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr,
return -1;
}
void *alloc_pages(size_t size)
void *alloc_pages(size_t size, void **phys_addr)
{
const uint32_t nb_pages = CEIL(size, PAGE_SIZE);
uint16_t pd_index;
@ -51,6 +51,8 @@ void *alloc_pages(size_t size)
free_frame((void *)(page_table[j] & PAGE_MASK));
return NULL;
}
if (phys_addr)
*phys_addr = frame;
page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS;
}
memset((void *)GET_PAGE_ADDR(pd_index, index), 0, nb_pages * PAGE_SIZE);

View File

@ -48,7 +48,7 @@ static void new_block(Zone *zone, uint32_t zone_size)
int new_vzone(block_type_t type, uint32_t size)
{
void *heap = alloc_pages(size);
void *heap = alloc_pages(size, NULL);
if (heap == NULL) {
kprintf(KERN_ERR "error: alloc_pages failed\n");
return (-1);

View File

@ -1,10 +1,14 @@
#include "kprintf.h"
#include "task.h"
struct task *current_task;
void scheduler(void)
{
struct task *it = current_task->next;
kprintf("camille mon bebou\n");
if (!current_task)
return;
struct task *it = current_task;
while (it->status != RUN)
it = it->next;
switch_to_task(it);

View File

@ -16,11 +16,11 @@ switch_to_task:
// and + 1 to get the argument (next task)
mov esi, [esp+(4+1)*4]
mov [current_task], esi
mov esp, [current_task]
mov eax, [current_task+4]
mov ebx, [current_task+8]
mov [TSS + 4], eax
mov esp, [current_task] // esp
mov eax, [current_task+4] // esp0
mov ebx, [current_task+8] // cr3
mov [TSS+4], eax // tss.esp0
mov ecx, cr3
// if cr3 hasn't change, do nothing
@ -34,4 +34,4 @@ switch_to_task:
pop ebp
pop ebx
ret
ret // this will also change eip to the next task's instructions

45
src/multitasking/task.c Normal file
View File

@ -0,0 +1,45 @@
#include "task.h"
#include "alloc.h"
#include "kpanic.h"
#include "memory.h"
static void set_eip(void (*fn)(void), struct task *task)
{
// TODO or not TODO
}
static struct task *create_task(uint8_t owner_id)
{
static uint32_t pid = 1;
struct task *new_task = vmalloc(sizeof(struct task));
if (!new_task)
return NULL;
new_task->owner_id = owner_id;
new_task->pid = pid++;
new_task->heap = alloc_pages(4096, (void **)&new_task->cr3);
if (!new_task->heap) {
vfree(new_task);
return NULL;
}
return new_task;
}
void exec_fn(void (*fn)(void))
{
struct task *new_task = create_task(OWNER_KERNEL);
if (!new_task)
kpanic("failed to create new task");
new_task->status = RUN;
new_task->next = current_task;
new_task->prev = current_task->prev;
current_task->prev = new_task;
current_task = new_task;
set_eip(fn, new_task);
}
/*
* Create task
* Allocate new pd => kmalloc(4096)
* Add pd address to the struct task
*
*/