multi-cpu scheduling WIP

This commit is contained in:
2026-01-25 15:54:00 +01:00
parent 7bb3b77ede
commit 95f590fb3b
23 changed files with 103 additions and 61 deletions

View File

@@ -6,6 +6,8 @@
#include <proc/proc.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/smp.h>
#include <sys/spin_lock.h>
bool proc_create_resource_mutex (struct proc_mutex* mutex) {
memset (mutex, 0, sizeof (*mutex));
@@ -34,6 +36,8 @@ static void proc_mutex_suspend (struct proc* proc, struct proc_suspension_q* sq,
proc->suspension_q = sq;
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
atomic_fetch_sub (&cpu->proc_run_q_count, 1);
if (cpu->proc_current == proc)
cpu->proc_current = NULL;
@@ -64,6 +68,7 @@ static void proc_mutex_resume (struct proc* proc) {
atomic_store (&proc->state, PROC_READY);
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
atomic_fetch_add (&cpu->proc_run_q_count, 1);
spin_unlock (&sq->lock, &ctxsq);
}

View File

@@ -181,7 +181,7 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
return aux;
}
static struct proc* proc_spawn_rd (char* name) {
struct proc* proc_spawn_rd (char* name) {
struct rd_file* rd_file = rd_get_file (name);
bool ok = proc_check_elf (rd_file->content);
@@ -204,10 +204,13 @@ struct proc* proc_find_pid (int pid) {
return proc;
}
void proc_register (struct proc* proc, struct cpu* cpu) {
void proc_register (struct proc* proc, struct cpu* cpu1) {
spin_lock_ctx_t ctxcpu, ctxprtr;
proc->cpu = cpu;
proc->cpu = cpu1 != NULL ? cpu1 : cpu_find_lightest ();
DEBUG ("Assigning CPU %d to PID %d\n", proc->cpu->id, proc->pid);
struct cpu* cpu = proc->cpu;
rw_spin_write_lock (&proc_tree_lock, &ctxprtr);
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
@@ -215,6 +218,7 @@ void proc_register (struct proc* proc, struct cpu* cpu) {
spin_lock (&cpu->lock, &ctxcpu);
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
atomic_fetch_add (&cpu->proc_run_q_count, 1);
if (cpu->proc_current == NULL)
cpu->proc_current = proc;
@@ -306,15 +310,15 @@ void proc_sched (void) {
next = proc_find_sched (cpu);
if (prev != NULL) {
spin_lock (&prev->lock, &ctxpr);
memcpy (&prev->pdata.regs, &cpu->regs, sizeof (struct saved_regs));
spin_unlock (&prev->lock, &ctxpr);
}
if (next) {
cpu->proc_current = next;
spin_unlock (&cpu->lock, &ctxcpu);
do_sched (next);
do_sched (next, &cpu->lock, &ctxcpu);
} else {
cpu->proc_current = NULL;
spin_unlock (&cpu->lock, &ctxcpu);
@@ -329,11 +333,13 @@ void proc_kill (struct proc* proc) {
spin_lock (&proc->lock, &ctxpr);
atomic_store (&proc->state, PROC_DEAD);
proc->cpu = NULL;
spin_unlock (&proc->lock, &ctxpr);
spin_lock (&cpu->lock, &ctxcpu);
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
atomic_fetch_sub (&cpu->proc_run_q_count, 1);
if (cpu->proc_current == proc)
cpu->proc_current = NULL;
@@ -409,8 +415,13 @@ void proc_init (void) {
proc_kpproc_init ();
struct proc* init = proc_spawn_rd ("init.exe");
proc_register (init, thiscpu);
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
proc_register (spin_proc, thiscpu);
do_sched (init);
struct proc* init = proc_spawn_rd ("init.exe");
proc_register (init, NULL);
spin_lock_ctx_t ctxcpu;
spin_lock (&init->cpu->lock, &ctxcpu);
do_sched (init, &init->cpu->lock, &ctxcpu);
}

View File

@@ -68,6 +68,7 @@ bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages);
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
void proc_register (struct proc* proc, struct cpu* cpu);
struct proc* proc_find_pid (int pid);
struct proc* proc_spawn_rd (char* name);
void proc_init (void);
#endif // _KERNEL_PROC_PROC_H