Files
XunilOS/kernel/src/task/scheduler.rs
csd4ni3l ae3915147a Add a usermode address space which can be switched to, make
FRAME_ALLOCATOR global, make XunilFrameAllocator not hold Limine entries
so it can be used without lifetimes, implement the process struct, add
user heap by giving back heap_start from ELF and adding sbrk syscall,
align ELF loading in userspace_stub, implement lots of libc functions in
libxunil, remove x86_64 dependency from libxunil, add malloc and all
required heap functions to libxunil and more syscall numbers, add a util
file to libxunil, add build scripts for libxunil and doomgeneric
2026-04-05 20:12:59 +02:00

56 lines
1.5 KiB
Rust

use alloc::collections::btree_map::BTreeMap;
use lazy_static::lazy_static;
use crate::{
arch::{arch::enter_usermode, x86_64::paging::XunilFrameAllocator},
task::process::Process,
util::Locked,
};
pub struct Scheduler {
pub processes: BTreeMap<u64, Process>,
pub current_process: i64,
next_pid: u64,
}
impl Scheduler {
pub const fn new() -> Scheduler {
Scheduler {
processes: BTreeMap::new(),
current_process: -1,
next_pid: 1,
}
}
}
impl Locked<Scheduler> {
pub fn spawn_process(&self, entry_point: u64, stack_top: u64, heap_base: u64) -> Option<u64> {
let mut guard = self.lock();
let pid = guard.next_pid;
guard.next_pid += 1;
let process = Process::new(pid, entry_point, stack_top, heap_base, heap_base)?;
guard.processes.insert(pid, process);
Some(pid)
}
pub fn run_process(&self, pid: u64, entry_point: *const u8) {
let mut guard = self.lock();
let stack_top = guard.processes[&pid].stack_top;
guard.current_process = pid as i64;
enter_usermode(entry_point as u64, (stack_top & !0xF) - 8);
}
pub fn with_process<F, R>(&self, index: u64, f: F) -> Option<R>
where
F: FnOnce(&mut Process) -> R,
{
let mut guard = self.lock();
let process = guard.processes.get_mut(&index)?;
Some(f(process))
}
}
pub static SCHEDULER: Locked<Scheduler> = Locked::new(Scheduler::new());