feat: use per-cpu sections

This commit is contained in:
Loïc Grégoire
2026-03-23 20:29:34 -04:00
parent 33c796c958
commit 020e81f34b
14 changed files with 235 additions and 117 deletions
+24 -3
View File
@@ -56,8 +56,15 @@ fn check_function(fn_name: []const u8, ret: usize) void {
}
}
const bootstrap_offsets = [1]usize{0};
pub var cpu_offsets: []usize = undefined;
pub fn early_init() linksection(b.init) void {
// Set current CPU to the bootstrap CPU
// Ensure we can use per-cpu data.
cpu_offsets = @constCast(&bootstrap_offsets);
// Set current CPU to the bootstrap CPU.
my_cpu = &ki.bootstrap_cpu;
my_cpu.impl.pthread = c.pthread_self();
@@ -96,7 +103,7 @@ pub fn early_init() linksection(b.init) void {
}
fn sigusr1_handler(_: posix.SIG, _: *const posix.siginfo_t, _: ?*anyopaque) callconv(.c) void {
if (@intFromEnum(ke.curcpu().ipl) < @intFromEnum(ke.Ipl.Dispatch) and ki.ipl.is_softint_pending(ke.curcpu(), .Dispatch)) {
if (@intFromEnum(ke.curcpu().ipl) < @intFromEnum(ke.Ipl.Dispatch) and ki.ipl.is_softint_pending(.Dispatch)) {
ki.dpc.dispatch(ke.curcpu());
}
}
@@ -128,8 +135,13 @@ fn other_cpu_entry(arg: ?*anyopaque) callconv(.c) ?*anyopaque {
return null;
}
var cpu: ke.Cpu linksection(b.percpu) = undefined;
fn idle(_: ?*anyopaque) void {}
extern var __percpu_start: u8;
extern var __percpu_end: u8;
pub fn late_init() linksection(b.init) void {
timer.init();
timer.init_cpu();
@@ -158,10 +170,19 @@ pub fn late_init() linksection(b.init) void {
allocator.alloc(c.pthread_t, other_count) catch @panic("oom");
ke.cpus = allocator.alloc(*ke.Cpu, ke.ncpus) catch @panic("oom");
cpu_offsets = allocator.alloc(usize, ke.ncpus) catch @panic("oom");
ke.cpus[0] = &ki.bootstrap_cpu;
cpu_offsets[0] = 0;
const size = @intFromPtr(&__percpu_end) - @intFromPtr(&__percpu_start);
for (1..ke.ncpus) |i| {
ke.cpus[i] = allocator.create(ke.Cpu) catch @panic("oom");
const block = allocator.alloc(u8, size) catch @panic("oom");
cpu_offsets[i] = @intFromPtr(block.ptr) -% @intFromPtr(&__percpu_start);
ke.cpus[i] = ki.impl.percpu_ptr_other(&cpu, i);
@memcpy(block, @as([*]u8, @ptrCast(&__percpu_start))[0..size]);
ke.cpus[i].idle_thread = allocator.create(ke.Thread) catch @panic("oom");
ke.cpus[i].id = @intCast(i);
+2 -2
View File
@@ -11,11 +11,11 @@ threadlocal var timerid: c.timer_t = undefined;
fn timer_handler(_: posix.SIG, _: *const posix.siginfo_t, _: ?*anyopaque) callconv(.c) void {
const old_ipl = ke.ipl.set_hardware(.Device);
ke.dpc.enqueue(&ke.curcpu().timer_dpc, null);
ki.timer.clock();
_ = ke.ipl.set_hardware(old_ipl);
if (@intFromEnum(old_ipl) < @intFromEnum(ke.Ipl.Dispatch) and ki.ipl.is_softint_pending(ke.curcpu(), .Dispatch)) {
if (@intFromEnum(old_ipl) < @intFromEnum(ke.Ipl.Dispatch) and ki.ipl.is_softint_pending(.Dispatch)) {
ki.dpc.dispatch(ke.curcpu());
}
}