feat: fireworks!

This commit is contained in:
Loïc Grégoire
2026-04-18 20:44:52 -04:00
parent 712a8012af
commit 8fc133cc3c
18 changed files with 186 additions and 127 deletions
+18 -2
View File
@@ -17,10 +17,12 @@ extern var AP_TRAMPOLINE_DATA: u8;
extern var __percpu_start: u8;
extern var __percpu_end: u8;
export var cpu_id_to_apic_id: [config.CONFIG_NCPUS]u32 = undefined;
pub export var cpu_id_to_apic_id: [config.CONFIG_NCPUS]u32 = undefined;
const start_stack = ke.ExportedCpuLocal(usize, 0, "ap_start_stack");
pub const start_thread = ke.CpuLocal(*ke.Thread, undefined);
var aps_booted: std.atomic.Value(usize) = .init(0);
const ApData = extern struct {
@@ -35,6 +37,16 @@ fn ap_entry(cpu_id: u32) callconv(.c) noreturn {
ki.impl.init.ap_entry(cpu_id, &aps_booted);
}
fn make_thread(entrypoint: *const fn (?*anyopaque) void, stack: usize) *ke.Thread {
const td = mm.zone.gpa.create(ke.Thread) catch @panic("Failed to allocate thread for AP");
td.init(stack, r.kib(32), entrypoint, null);
td.priority = 0;
td.priority_class = .Idle;
return td;
}
pub fn init() linksection(r.init) void {
const trampoline_start = @intFromPtr(&AP_TRAMPOLINE_START);
const trampoline_size = @intFromPtr(&AP_TRAMPOLINE_END) - trampoline_start;
@@ -94,10 +106,12 @@ pub fn init() linksection(r.init) void {
// copy the offset into the AP's self_offset variable
@as(*usize, @ptrCast(@alignCast(&cpu_data[self_offset_offset]))).* = ki.impl.cpu_offsets[cpu_id];
const stack_top = @intFromPtr(mm.heap.alloc(r.kib(16)) catch @panic("Failed to allocate AP stack")) + r.kib(16);
const stack_top = @intFromPtr(mm.heap.alloc(r.kib(32)) catch @panic("Failed to allocate AP stack")) + r.kib(32);
start_stack.remote(@intCast(cpu_id)).* = stack_top & ~@as(usize, 15);
start_thread.remote(@intCast(cpu_id)).* = make_thread(@ptrCast(&ap_entry), stack_top - r.kib(32));
rtl.barrier.wmb();
// Send the INIT-SIPI-SIPI sequence to start the AP.
@@ -109,6 +123,8 @@ pub fn init() linksection(r.init) void {
apic.send_sipi(apic_id, 0x08);
}
log.info("Waiting for APs...", .{});
while (aps_booted.load(.acquire) < apic.apics.items.len) {
std.atomic.spinLoopHint();
}