Spinlock save cpu flags

This commit is contained in:
2026-03-12 22:48:34 +01:00
parent 19793e9126
commit 4760818118
50 changed files with 704 additions and 461 deletions

View File

@@ -57,24 +57,25 @@ void cpu_request_sched (struct cpu* cpu) {
}
struct cpu* cpu_find_lightest (void) {
uint64_t fc;
struct limine_mp_response* mp = limine_mp_request.response;
int start = atomic_fetch_add (&last_cpu_index, 1) % mp->cpu_count;
struct cpu* best_cpu = &cpus[start];
spin_lock (&best_cpu->lock);
spin_lock (&best_cpu->lock, &fc);
int best_load = best_cpu->proc_run_q_count;
spin_unlock (&best_cpu->lock);
spin_unlock (&best_cpu->lock, fc);
for (int i = 1; i < (int)mp->cpu_count; i++) {
int idx = (start + i) % mp->cpu_count;
struct cpu* cpu = &cpus[idx];
spin_lock (&cpu->lock);
spin_lock (&cpu->lock, &fc);
int l = cpu->proc_run_q_count;
spin_unlock (&cpu->lock);
spin_unlock (&cpu->lock, fc);
if (l < best_load) {
best_load = l;
@@ -87,6 +88,8 @@ struct cpu* cpu_find_lightest (void) {
/// Bootstrap code for non-BSP CPUs
static void smp_bootstrap (struct limine_mp_info* mp_info) {
uint64_t fc;
load_kernel_cr3 ();
struct cpu* cpu = cpu_make (mp_info->lapic_id, mp_info->processor_id);
@@ -110,8 +113,8 @@ static void smp_bootstrap (struct limine_mp_info* mp_info) {
struct proc* spin_proc = proc_from_file (thiscpu->kproc, "RD", "/spin", &rctx);
proc_register (spin_proc, thiscpu, &rctx);
spin_lock (&spin_proc->cpu->lock);
do_sched (spin_proc, &spin_proc->cpu->lock);
spin_lock (&spin_proc->cpu->lock, &fc);
do_sched (spin_proc, &spin_proc->cpu->lock, fc);
}
/// Initialize SMP subsystem for AMD64. Start AP CPUs