smp: enable smp

This is a very large patch. Don't feel like splitting it up.

Change locking discipline for rbtrees. Most changes are this.

Fix stack init logic for application processors, and start them up.

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-13 14:07:55 -04:00
commit 3e5abada9b
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
17 changed files with 123 additions and 52 deletions

View file

@ -1,10 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/tty.h>
#include <kernel/atomic.h>
#include <kernel/buddy.h>
#include <kernel/requests.h>
#include <arch/proc.h>
#include <arch/init.h>
#include <drivers/acpi.h>
#include <drivers/apic.h>
#include <drivers/serial.h>
#include <lib/stdio.h>
#include <lib/sem.h>
#include "arch/msr.h"
#include "arch/reg.h"
#include "descriptor.h"
@ -55,14 +60,20 @@ void arch_init()
wrmsr(IA32_FMASK, 0x3F7FD5);
}
void arch_ap_startup()
/* entry point for application processors */
int ap_init(bool bsp, struct limine_mp_info *info)
{
sem_t *sem = info ? (void *)info->extra_argument : NULL;
u64 id = atomic_xadd(&cpu_ticker, 1);
struct cpu_local *this_local = &cpu_local_arr[id];
this_local->cpu_id = id;
wrmsr(IA32_GS_BASE, (u64)this_local);
lidt();
lgdt();
tss_init(id, this_local);
char buf[64];
@ -76,4 +87,43 @@ void arch_ap_startup()
panic("failed to create idle thread for cpu %d\n", id);
this_local->idle_thread = idle;
if (sem)
sem_post(sem);
if (!bsp)
schedule();
return id;
}
static void ap_trampoline(struct limine_mp_info *info)
{
void *ap_stack = buddy_alloc(buddy_order(P_STACK_SIZE));
void ap_trampoline_2(void *stack_addr, size_t stack_size, struct limine_mp_info *info);
ap_trampoline_2(ap_stack, P_STACK_SIZE, info);
}
/* start the application processors */
void arch_ap_startup()
{
ap_init(true, NULL);
size_t n = mp_res->cpu_count;
sem_t sem;
sem_init(&sem, 0);
for (size_t i = 0; i < n; i++) {
struct limine_mp_info *cpu = mp_res->cpus[i];
if (cpu->lapic_id == mp_res->bsp_lapic_id)
continue;
cpu->extra_argument = (u64)&sem;
*((volatile limine_goto_address *)&cpu->goto_address) = ap_trampoline;
while (!sem_trywait(&sem))
;
}
krdy("AP startup complete\n");
}