From 95f590fb3b5ff6b4c72e5a1ddfeb518ccfd7ea50 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sun, 25 Jan 2026 15:54:00 +0100 Subject: [PATCH] multi-cpu scheduling WIP --- init/init.c | 8 ++++++++ init/src.mk | 2 +- kernel/amd64/apic.c | 4 +--- kernel/amd64/bootmain.c | 9 ++++++--- kernel/amd64/intr.c | 12 ------------ kernel/amd64/intr.h | 1 - kernel/amd64/sched1.c | 10 ++++++++-- kernel/amd64/smp.c | 38 +++++++++++++++++++++++++------------ kernel/amd64/smp.h | 5 ++++- kernel/amd64/syscall.c | 14 ++++++++------ kernel/amd64/syscallentry.S | 2 ++ kernel/irq/irq.c | 5 ----- kernel/proc/mutex.c | 5 +++++ kernel/proc/proc.c | 27 ++++++++++++++++++-------- kernel/proc/proc.h | 1 + kernel/sys/sched.h | 2 +- kernel/syscall/syscall.c | 6 +----- make/apps.mk | 2 +- spin/.gitignore | 2 ++ spin/Makefile | 1 + spin/app.mk | 1 + spin/spin.c | 4 ++++ spin/src.mk | 3 +++ 23 files changed, 103 insertions(+), 61 deletions(-) create mode 100644 spin/.gitignore create mode 100644 spin/Makefile create mode 100644 spin/app.mk create mode 100644 spin/spin.c create mode 100644 spin/src.mk diff --git a/init/init.c b/init/init.c index b77df85..938c546 100644 --- a/init/init.c +++ b/init/init.c @@ -47,6 +47,8 @@ void app_main (void) { spawn (&app_thread1); + /* for (volatile int i = 0; i < 1000*1000; i++) */ + /* ; */ for (;;) { lock_mutex (MUTEX, RV_PRIVATE); @@ -54,6 +56,9 @@ void app_main (void) { test ('a'); unlock_mutex (MUTEX, RV_PRIVATE); + + /* for (volatile int i = 0; i < 1000*1000; i++) */ + /* ; */ } } @@ -65,6 +70,9 @@ void app_thread1 (void) { test ('b'); unlock_mutex (MUTEX, RV_PRIVATE); + + /* for (volatile int i = 0; i < 1000*1000; i++) */ + /* ; */ } quit (); diff --git a/init/src.mk b/init/src.mk index c0fe52f..58a0071 100644 --- a/init/src.mk +++ b/init/src.mk @@ -1,3 +1,3 @@ -S += init.S +c += init.c o += init.o diff --git a/kernel/amd64/apic.c b/kernel/amd64/apic.c index 16b1e5d..79a5f67 100644 --- a/kernel/amd64/apic.c +++ b/kernel/amd64/apic.c @@ -82,8 +82,6 @@ static struct ioapic* amd64_ioapic_find (uint32_t irq) { for (size_t i = 0; i < ioapic_entries; i++) { ioapic = &ioapics[i]; - /* uint32_t version = amd64_ioapic_read ((uintptr_t)hhdm->offset + - * (uintptr_t)ioapic->table_data.address, 1); */ uint32_t version = amd64_ioapic_read (ioapic, 1); uint32_t max = ((version >> 16) & 0xFF); @@ -233,7 +231,7 @@ static uint32_t amd64_lapic_calibrate (uint32_t us) { static void amd64_lapic_start (uint32_t ticks) { amd64_lapic_write (LAPIC_DCR, DIVIDER_VALUE); amd64_lapic_write (LAPIC_TIMICT, ticks); - amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 17) | (1 << 16)); + amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 17)); } /* diff --git a/kernel/amd64/bootmain.c b/kernel/amd64/bootmain.c index 52f5813..7df6ba3 100644 --- a/kernel/amd64/bootmain.c +++ b/kernel/amd64/bootmain.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -29,7 +30,9 @@ ALIGNED (16) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX]; * the necessary platform-dependent subsystems/drivers and jump into the init app. */ void bootmain (void) { - struct cpu* bsp_cpu = cpu_make (); + struct limine_mp_response* mp = limine_mp_request.response; + + struct cpu* bsp_cpu = cpu_make (mp->bsp_lapic_id); amd64_init (bsp_cpu, false); syscall_init (); @@ -44,9 +47,9 @@ void bootmain (void) { amd64_ioapic_init (); amd64_hpet_init (); - smp_init (); - mm_init2 (); + + smp_init (); proc_init (); diff --git a/kernel/amd64/intr.c b/kernel/amd64/intr.c index d750874..cf0815a 100644 --- a/kernel/amd64/intr.c +++ b/kernel/amd64/intr.c @@ -213,15 +213,3 @@ void irq_save (spin_lock_ctx_t* ctx) { *ctx = amd64_irq_save_flags (); } /* Restore interrupt state */ void irq_restore (spin_lock_ctx_t* ctx) { amd64_irq_restore_flags (*ctx); } - -/* Map custom IRQ mappings to legacy IRQs */ -uint32_t amd64_resolve_irq (uint32_t irq) { - static const uint32_t mappings[] = { - [SCHED_PREEMPT_TIMER] = 0, - [TLB_SHOOTDOWN] = 6, - [CPU_REQUEST_SCHED] = 3, - [CPU_SPURIOUS] = 5, - }; - - return mappings[irq]; -} diff --git a/kernel/amd64/intr.h b/kernel/amd64/intr.h index 910b7e7..5717a7a 100644 --- a/kernel/amd64/intr.h +++ b/kernel/amd64/intr.h @@ -32,7 +32,6 @@ struct saved_regs { } PACKED; void amd64_load_idt (void); -uint32_t amd64_resolve_irq (uint32_t irq); void amd64_intr_init (void); #endif // _KERNEL_AMD64_INTR_H diff --git a/kernel/amd64/sched1.c b/kernel/amd64/sched1.c index 6290b87..5a5cba3 100644 --- a/kernel/amd64/sched1.c +++ b/kernel/amd64/sched1.c @@ -5,12 +5,18 @@ #include #include #include +#include -void do_sched (struct proc* proc) { - __asm__ volatile ("cli"); +void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu) { + spin_lock_ctx_t ctxpr; + + spin_lock (&proc->lock, &ctxpr); thiscpu->tss.rsp0 = proc->pdata.kernel_stack; thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack; + + spin_unlock (&proc->lock, &ctxpr); + spin_unlock (cpu_lock, ctxcpu); amd64_do_sched ((void*)&proc->pdata.regs, (void*)proc->pd->cr3_paddr); } diff --git a/kernel/amd64/smp.c b/kernel/amd64/smp.c index 7651cc4..717ff50 100644 --- a/kernel/amd64/smp.c +++ b/kernel/amd64/smp.c @@ -13,6 +13,7 @@ #include #include #include +#include /// Cpu ID counter static atomic_uint cpu_counter = 0; @@ -22,7 +23,7 @@ static struct cpu cpus[CPUS_MAX]; static atomic_int cpu_init_count; /// Allocate a CPU structure -struct cpu* cpu_make (void) { +struct cpu* cpu_make (uint64_t lapic_id) { int id = atomic_fetch_add (&cpu_counter, 1); struct cpu* cpu = &cpus[id]; @@ -30,6 +31,7 @@ struct cpu* cpu_make (void) { memset (cpu, 0, sizeof (*cpu)); cpu->lock = SPIN_LOCK_INIT; cpu->id = id; + cpu->lapic_id = lapic_id; amd64_wrmsr (MSR_GS_BASE, (uint64_t)cpu); @@ -47,21 +49,31 @@ void cpu_request_sched (struct cpu* cpu) { return; } - struct limine_mp_response* mp = limine_mp_request.response; + amd64_lapic_ipi (cpu->lapic_id, CPU_REQUEST_SCHED); +} - for (size_t i = 0; i < mp->cpu_count; i++) { - if (cpu->id == i) { - amd64_lapic_ipi (mp->cpus[i]->lapic_id, CPU_REQUEST_SCHED); - break; +struct cpu* cpu_find_lightest (void) { + struct cpu* cpu = &cpus[0]; + + int load = atomic_load (&cpu->proc_run_q_count); + + for (unsigned int i = 1; i < cpu_counter; i++) { + struct cpu* new_cpu = &cpus[i]; + int new_load = atomic_load (&new_cpu->proc_run_q_count); + if (new_load < load) { + load = new_load; + cpu = new_cpu; } } + + return cpu; } /// Bootstrap code for non-BSP CPUs static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) { amd64_load_kernel_cr3 (); - struct cpu* cpu = cpu_make (); + struct cpu* cpu = cpu_make (mp_info->lapic_id); amd64_init (cpu, true); /* gdt + idt */ syscall_init (); @@ -70,12 +82,14 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) { DEBUG ("CPU %u is online!\n", thiscpu->id); - __asm__ volatile ("sti"); - atomic_fetch_sub (&cpu_init_count, 1); + + struct proc* spin_proc = proc_spawn_rd ("spin.exe"); + proc_register (spin_proc, thiscpu); - for (;;) - ; + spin_lock_ctx_t ctxcpu; + spin_lock (&spin_proc->cpu->lock, &ctxcpu); + do_sched (spin_proc, &spin_proc->cpu->lock, &ctxcpu); } /// Initialize SMP subsystem for AMD64. Start AP CPUs @@ -87,7 +101,7 @@ void smp_init (void) { cpu_init_count = mp->cpu_count - 1; /* Don't include BSP */ for (size_t i = 0; i < mp->cpu_count; i++) { - if (mp->cpus[i]->lapic_id != thiscpu->id) { + if (mp->cpus[i]->lapic_id != thiscpu->lapic_id) { DEBUG ("Trying CPU %u\n", mp->cpus[i]->lapic_id); mp->cpus[i]->goto_address = &amd64_smp_bootstrap; } diff --git a/kernel/amd64/smp.h b/kernel/amd64/smp.h index a8c7ac8..51130a4 100644 --- a/kernel/amd64/smp.h +++ b/kernel/amd64/smp.h @@ -25,17 +25,20 @@ struct cpu { uintptr_t lapic_mmio_base; uint64_t lapic_ticks; + uint64_t lapic_id; uint32_t id; spin_lock_t lock; struct list_node_link* proc_run_q; struct proc* proc_current; + atomic_int proc_run_q_count; }; -struct cpu* cpu_make (void); +struct cpu* cpu_make (uint64_t lapic_id); struct cpu* cpu_get (void); void cpu_request_sched (struct cpu* cpu); +struct cpu* cpu_find_lightest (void); #define thiscpu (cpu_get ()) diff --git a/kernel/amd64/syscall.c b/kernel/amd64/syscall.c index dd7da56..126db5c 100644 --- a/kernel/amd64/syscall.c +++ b/kernel/amd64/syscall.c @@ -15,25 +15,27 @@ extern void amd64_syscall_entry (void); int amd64_syscall_dispatch (void* stack_ptr) { spin_lock_ctx_t ctxcpu; + + spin_lock (&thiscpu->lock, &ctxcpu); amd64_load_kernel_cr3 (); - struct saved_regs* regs = stack_ptr; - spin_lock (&thiscpu->lock, &ctxcpu); memcpy (&thiscpu->regs, regs, sizeof (struct saved_regs)); - spin_unlock (&thiscpu->lock, &ctxcpu); int syscall_num = regs->rax; syscall_handler_func_t func = syscall_find_handler (syscall_num); - if (func == NULL) + if (func == NULL) { + spin_unlock (&thiscpu->lock, &ctxcpu); return -ST_SYSCALL_NOT_FOUND; - + } + struct proc* caller = thiscpu->proc_current; + + spin_unlock (&thiscpu->lock, &ctxcpu); int result = func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9); - return result; } diff --git a/kernel/amd64/syscallentry.S b/kernel/amd64/syscallentry.S index 5d26a12..dd26bef 100644 --- a/kernel/amd64/syscallentry.S +++ b/kernel/amd64/syscallentry.S @@ -4,6 +4,8 @@ .global amd64_syscall_entry amd64_syscall_entry: + cli + movq %rsp, %gs:0 movq %gs:8, %rsp diff --git a/kernel/irq/irq.c b/kernel/irq/irq.c index 4fb3bbd..5f86c55 100644 --- a/kernel/irq/irq.c +++ b/kernel/irq/irq.c @@ -30,11 +30,6 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num) { irq_table[irq_num] = irq; rw_spin_write_unlock (&irqs_lock, &ctxiqa); -#if defined(__x86_64__) - uint8_t resolution = amd64_resolve_irq (irq_num); - amd64_ioapic_route_irq (irq_num, resolution, 0, amd64_lapic_id ()); -#endif - return true; } diff --git a/kernel/proc/mutex.c b/kernel/proc/mutex.c index 7cccaf7..c18e307 100644 --- a/kernel/proc/mutex.c +++ b/kernel/proc/mutex.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include 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); } diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index f58b480..486ef1d 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -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); } diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index c21fa82..8843a9d 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -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 diff --git a/kernel/sys/sched.h b/kernel/sys/sched.h index 09254f4..7207ad9 100644 --- a/kernel/sys/sched.h +++ b/kernel/sys/sched.h @@ -4,6 +4,6 @@ #include #include -void do_sched (struct proc* proc); +void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu); #endif // _KERNEL_SYS_SCHED_H diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 766a370..d608bde 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -160,8 +160,6 @@ DEFINE_SYSCALL (sys_clone) { size_t stack_size = (size_t)a2; uintptr_t entry = a3; - struct cpu* cpu = proc->cpu; - struct proc* new = proc_clone (proc, vstack_top, stack_size, entry); DEBUG ("new=%p\n", new); @@ -172,7 +170,7 @@ DEFINE_SYSCALL (sys_clone) { int pid = new->pid; - proc_register (new, cpu); + proc_register (new, NULL); return pid; } @@ -234,7 +232,6 @@ DEFINE_SYSCALL (sys_lock_mutex) { if (mutex_resource == NULL) return -ST_NOT_FOUND; - DEBUG ("locking %d\n", proc->pid); proc_mutex_lock (proc, &mutex_resource->u.mutex); return ST_OK; @@ -253,7 +250,6 @@ DEFINE_SYSCALL (sys_unlock_mutex) { if (mutex_resource == NULL) return -ST_NOT_FOUND; - DEBUG ("unlocking %d\n", proc->pid); return proc_mutex_unlock (proc, &mutex_resource->u.mutex) ? ST_OK : -ST_PERMISSION_ERROR; } diff --git a/make/apps.mk b/make/apps.mk index 4e7c723..0ee4058 100644 --- a/make/apps.mk +++ b/make/apps.mk @@ -1,4 +1,4 @@ -apps := init +apps := init spin all_apps: @for d in $(apps); do make -C $$d platform=$(platform) all; done diff --git a/spin/.gitignore b/spin/.gitignore new file mode 100644 index 0000000..25a7384 --- /dev/null +++ b/spin/.gitignore @@ -0,0 +1,2 @@ +*.o +*.exe diff --git a/spin/Makefile b/spin/Makefile new file mode 100644 index 0000000..d16094b --- /dev/null +++ b/spin/Makefile @@ -0,0 +1 @@ +include ../make/user.mk diff --git a/spin/app.mk b/spin/app.mk new file mode 100644 index 0000000..5a386ed --- /dev/null +++ b/spin/app.mk @@ -0,0 +1 @@ +app := spin.exe diff --git a/spin/spin.c b/spin/spin.c new file mode 100644 index 0000000..0c606c2 --- /dev/null +++ b/spin/spin.c @@ -0,0 +1,4 @@ +void app_main (void) { + for (;;) + ; +} diff --git a/spin/src.mk b/spin/src.mk new file mode 100644 index 0000000..010faa8 --- /dev/null +++ b/spin/src.mk @@ -0,0 +1,3 @@ +c += spin.c + +o += spin.o