feat(kernel): implement Phase 4 - preemptive multitasking, round-robin scheduler, PCB, x86_64 context switch & syscalls

This commit is contained in:
Adam Eehan committed 2026-10-23 17:36:52 +05:30
1 parent 7a584ea6e6
commit 6ee659ac6b
9 files changed
+444 -6

No files matched your search

+60
View File
@@ -0,0 +1,60 @@
#ifndef AIRLYS_SCHED_H
#define AIRLYS_SCHED_H
#include <airlys/types.h>
#define MAX_TASKS 32
#define TASK_STACK_SIZE (16 * 1024) /* 16 KB Kernel Stack per task */
#define DEFAULT_TIME_SLICE 5 /* 5 timer ticks (50ms) */
typedef enum {
TASK_STATE_UNUSED = 0,
TASK_STATE_READY,
TASK_STATE_RUNNING,
TASK_STATE_SLEEPING,
TASK_STATE_TERMINATED
} task_state_t;
typedef struct task {
uint32_t pid;
char name[32];
task_state_t state;
uintptr_t rsp; /* Saved kernel stack pointer */
uintptr_t stack_base; /* Base allocated stack pointer */
uint64_t sleep_until; /* Target tick count for sleep wakeup */
uint64_t time_slice; /* Ticks remaining in current execution burst */
uint64_t total_ticks; /* Cumulative execution ticks */
struct task* next; /* Next pointer in circular queue */
} task_t;
/* Initialize preemptive scheduler */
void sched_init(void);
/* Create a new kernel task thread */
task_t* task_create(const char* name, void (*entry_point)(void));
/* Trigger context switch to next ready task */
void schedule(void);
/* Voluntarily yield the remaining time slice */
void task_yield(void);
/* Terminate the currently running task */
void task_exit(void);
/* Put the current task to sleep for specified ticks (1 tick = 10ms) */
void task_sleep(uint64_t ticks);
/* Preemptive timer tick handler called from PIT ISR */
void sched_timer_tick(void);
/* Get currently executing task pointer */
task_t* sched_get_current(void);
/* Print all active tasks in system to console */
void sched_print_tasks(void);
/* Low-level assembly context switch */
extern void task_switch(uintptr_t* prev_rsp, uintptr_t* next_rsp);
#endif /* AIRLYS_SCHED_H */
+18
View File
@@ -0,0 +1,18 @@
#ifndef AIRLYS_SYSCALL_H
#define AIRLYS_SYSCALL_H
#include <airlys/types.h>
#define SYS_YIELD 0
#define SYS_EXIT 1
#define SYS_SLEEP 2
#define SYS_PRINT 3
#define SYS_GETPID 4
/* Initialize System Call infrastructure (MSRs / Dispatcher) */
void syscall_init(void);
/* Kernel System Call Dispatcher */
int64_t syscall_dispatch(uint64_t num, uint64_t arg1, uint64_t arg2, uint64_t arg3);
#endif /* AIRLYS_SYSCALL_H */