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

@@ -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));
}
/*

View File

@@ -9,6 +9,7 @@
#include <irq/irq.h>
#include <libk/std.h>
#include <limine/limine.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <proc/proc.h>
@@ -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 ();

View File

@@ -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];
}

View File

@@ -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

View File

@@ -5,12 +5,18 @@
#include <proc/proc.h>
#include <sys/mm.h>
#include <sys/smp.h>
#include <sync/spin_lock.h>
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);
}

View File

@@ -13,6 +13,7 @@
#include <sys/debug.h>
#include <sys/smp.h>
#include <sys/syscall.h>
#include <sys/sched.h>
/// 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;
}

View File

@@ -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 ())

View File

@@ -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;
}

View File

@@ -4,6 +4,8 @@
.global amd64_syscall_entry
amd64_syscall_entry:
cli
movq %rsp, %gs:0
movq %gs:8, %rsp