feature: fork added and exit_task is almost working

This commit is contained in:
0x35c 2025-01-28 13:38:39 +01:00
parent d7626df19c
commit dbdf851dd2
9 changed files with 80 additions and 19 deletions

View File

@ -25,7 +25,8 @@ static inline void cli(void)
__asm__ volatile("cli");
}
static inline void sti(void)
// aka sti
static inline void toris(void)
{
__asm__ volatile("sti");
}

View File

@ -8,7 +8,7 @@
extern struct task *current_task;
enum status { ZOMBIE, THREAD, RUN };
enum status { ZOMBIE, THREAD, RUN, WAIT, SLEEP, STOPPED };
enum owner { OWNER_KERNEL, OWNER_USER };
#define STACK_SIZE PAGE_SIZE * 4
@ -32,4 +32,8 @@ struct task {
void scheduler(void);
void switch_to_task(struct task *next_task);
void exec_fn(void (*fn)(void));
struct task *create_task(u8 uid);
i8 create_kernel_task(void);
void remove_task(struct task *task);
u16 fork(void);
u16 wait(void);

View File

@ -25,7 +25,7 @@ static void set_pit_count(unsigned count)
outb(0x40, count & 0xFF); // Low byte
outb(0x40, (count & 0xFF00) >> 8); // High byte
sti();
toris();
}
void clock_init(struct registers *regs)
@ -41,7 +41,7 @@ static void clock_handler(struct registers *regs)
if (scheduler_counter % 100 == 0) {
cli();
scheduler();
sti();
toris();
}
scheduler_counter++;
sleep_counter--;

View File

@ -43,8 +43,10 @@ static void owo(void)
static void awa(void)
{
while (true)
kprintf("awaille\n");
/* if (fork() < 0) */
/* kprintf("camille il a une grosse bite (18cm)\n"); */
kprintf("awaille\n");
/* wait(); */
}
void kernel_main(multiboot_info_t *mbd, u32 magic)
@ -61,7 +63,6 @@ void kernel_main(multiboot_info_t *mbd, u32 magic)
*/
/* "Martin 03:50, 22 March 2009 (UTC)\n"); */
create_kernel_task();
exec_fn(owo);
exec_fn(awa);
/* exec_fn(owo); */
/* exec_fn(owo); */

16
src/multitasking/fork.c Normal file
View File

@ -0,0 +1,16 @@
#include "interrupts.h"
#include "string.h"
#include "task.h"
u16 fork(void)
{
cli();
struct task *child = create_task(current_task->uid);
if (!child)
return -1;
child->daddy = current_task;
current_task->child = child;
memcpy(child->esp0, current_task->esp0, STACK_SIZE);
toris();
return current_task == child ? 0 : child->uid;
}

View File

@ -1,3 +1,4 @@
#include "alloc.h"
#include "debug.h"
#include "kprintf.h"
#include "task.h"
@ -5,14 +6,20 @@
#include <stddef.h>
struct task *current_task = NULL;
struct task *current_task;
void scheduler(void)
{
if (!current_task || current_task->next == current_task)
if (!current_task) // || current_task->next == current_task)
return;
struct task *it = current_task->next;
while (it && it->status != RUN)
while (it && it->status != RUN) {
if (it->status == STOPPED || it->status == ZOMBIE) {
struct task *new_it = it->prev;
remove_task(it);
it = new_it;
}
it = it->next;
}
switch_to_task(it);
}

View File

@ -47,7 +47,7 @@ switch_to_task:
pop edx // get func_ptr
call edx
// TODO HANDLE TASK RETURN
call exit_task
.END:
ret

View File

@ -1,6 +1,7 @@
#include "task.h"
#include "alloc.h"
#include "debug.h"
#include "interrupts.h"
#include "kpanic.h"
#include "kprintf.h"
#include "memory.h"
@ -9,7 +10,7 @@
u32 esp_backup;
static struct task *create_task(u8 owner_id)
struct task *create_task(u8 uid)
{
static u32 pid = 1;
struct task *new_task = vmalloc(sizeof(struct task));
@ -18,7 +19,7 @@ static struct task *create_task(u8 owner_id)
new_task->next = current_task->next;
new_task->prev = current_task;
current_task->next = new_task;
new_task->uid = owner_id;
new_task->uid = uid;
new_task->esp0 = alloc_pages(STACK_SIZE, NULL);
if (!new_task->esp0) {
vfree(new_task);
@ -60,9 +61,31 @@ void exec_fn(void (*fn)(void))
new_task->eip = (u32 *)fn;
}
/*
* Create task
* Allocate new pd => kmalloc(4096)
* Add pd address to the struct task
*
*/
void remove_task(struct task *task)
{
struct task *left = task->prev;
struct task *right = task->next;
if (task->child)
remove_task(task->child);
if (task->heap)
vfree(task->heap);
if (task->esp0)
vfree(task->esp0);
task->heap = NULL;
task->esp0 = NULL;
if (task->status != ZOMBIE) {
left->next = right;
right->prev = left;
vfree(task);
}
}
void exit_task(void)
{
cli();
if (current_task->daddy && current_task->daddy->status != WAIT)
current_task->status = ZOMBIE;
else
current_task->status = STOPPED;
toris();
}

9
src/multitasking/wait.c Normal file
View File

@ -0,0 +1,9 @@
#include "interrupts.h"
#include "task.h"
u16 wait(void)
{
cli();
toris();
return 0;
}