Files
RockOS/kernel/scheduler.c

170 lines
4.7 KiB
C
Raw Normal View History

#include <lib/tasks.h>
2026-07-01 20:00:22 -05:00
#include <lib/print.h>
#include <lib/memory.h>
#include <memory/mm.h>
#include <stddef.h>
#include "common.h"
#include "process.h"
#include "scheduler.h"
#include "gdt.h"
static process_t* process_table;
static int current_process = 0;
static int total_processes = 0;
2026-07-03 22:25:24 -05:00
#define IDLE_TASK_PID 0
static void lock_scheduler() {
disable_interrupts();
}
static void unlock_scheduler() {
enable_interrupts();
}
// called from isr and yield, interrupts are already disabled and re enabled in the isr/yield func
uint32_t switch_context(uint32_t current_esp) {
process_table[current_process].esp = current_esp;
for (int i = 0; i < total_processes; i++) {
if (process_table[i].state == STATE_SLEEPING) {
if (process_table[i].sleep > 0) {
process_table[i].sleep--;
}
if (process_table[i].sleep == 0) {
process_table[i].state = STATE_READY;
}
}
}
int next_process = current_process;
int found_ready = 0;
while (1) {
next_process = (next_process + 1) % total_processes;
if (next_process != IDLE_TASK_PID && process_table[next_process].state == STATE_READY) {
found_ready = 1;
break;
}
if (next_process == current_process) {
break;
}
}
if (!found_ready) {
if (process_table[current_process].state == STATE_READY) {
next_process = current_process;
} else {
next_process = IDLE_TASK_PID;
}
}
current_process = next_process;
2026-07-04 00:26:20 -05:00
uint32_t current_cr3 = fetch_cr3();
if (current_cr3 != process_table[current_process].cr3) {
kprintf("switching cr3 | old: 0x%x | new: 0x%x\n", current_cr3, process_table[current_process].cr3);
load_pd((void*)process_table[current_process].cr3);
2026-07-01 20:00:22 -05:00
}
2026-07-04 00:26:20 -05:00
if (process_table[current_process].flags & PROCESS_FLAG_USER) {
tss.esp0 = process_table[current_process].kstack_top;
}
2026-07-04 00:26:20 -05:00
if (found_ready) {
kprintf("found ready task %d | cr3: 0x%x\n", current_process, process_table[current_process].cr3);
}
return process_table[current_process].esp;
}
void init_scheduler() {
process_table = kalloc(16 * sizeof(process_t));
2026-07-04 00:26:20 -05:00
memset((void*)process_table, 0, 16 * sizeof(process_t));
total_processes = 0;
process_t* process = &process_table[total_processes];
process->pid = total_processes;
process->esp = 0;
process->cr3 = fetch_cr3();
process->state = STATE_READY;
process->sleep = 0;
2026-07-01 20:00:22 -05:00
process->flags = PROCESS_FLAG_KERNEL;
total_processes++;
current_process = 0;
}
2026-07-01 20:00:22 -05:00
int create_task(uint32_t entry_point, uint32_t cr3, int user, uint32_t u_esp) {
if (total_processes >= 16) {
return 0;
}
// default to a new task, but if we find a dead one, re use it
process_t* process = &process_table[total_processes];
uint32_t pid = total_processes;
process->pid = pid;
process->cr3 = cr3;
process->state = STATE_READY;
process->sleep = 0;
2026-07-01 20:00:22 -05:00
process->flags = user ? PROCESS_FLAG_USER : PROCESS_FLAG_KERNEL;
2026-07-04 00:26:20 -05:00
total_processes++;
2026-07-01 20:00:22 -05:00
void* kstack_bottom = kalloc(PAGE_SIZE);
uint32_t kstack_top = (uint32_t)kstack_bottom + PAGE_SIZE;
process->kstack_top = kstack_top;
2026-07-01 20:00:22 -05:00
uint32_t* esp = (uint32_t*)kstack_top;
if (user) {
*(--esp) = 0x23; // User Data Segment (SS) with RPL 3 (0x20 | 3)
2026-07-03 22:25:24 -05:00
*(--esp) = u_esp; // User Stack Pointer (ESP) - location mapped in user space
*(--esp) = 0x0202; // EFLAGS (Interrupts enabled)
*(--esp) = 0x1B; // User Code Segment (CS) with RPL 3 (0x18 | 3)
*(--esp) = entry_point; // EIP
} else {
// IRET values for Ring 0
*(--esp) = 0x0202; // EFLAGS
*(--esp) = 0x08; // Kernel Code Segment (CS)
*(--esp) = entry_point; // EIP
}
*(--esp) = 0; // EAX
*(--esp) = 0; // ECX
*(--esp) = 0; // EDX
*(--esp) = 0; // EBX
*(--esp) = 0; // ESP
*(--esp) = 0; // EBP
*(--esp) = 0; // ESI
*(--esp) = 0; // EDI
process->esp = (uint32_t)esp;
return 1;
}
process_t* current_task() {
process_t* rtn = &process_table[current_process];
return rtn;
}
void sleep(uint32_t ms) {
if (ms == 0) return;
lock_scheduler();
process_table[current_process].sleep = ms;
process_table[current_process].state = STATE_SLEEPING;
yield();
unlock_scheduler();
}
void exit() {
lock_scheduler();
process_table[current_process].state = STATE_DEAD;
yield(); // yield forever
unlock_scheduler();
}