refactor: remove ke.Cpu struct

This commit is contained in:
Loïc Grégoire
2026-03-30 17:45:49 -04:00
parent 84500e2ff3
commit faf76ca485
15 changed files with 221 additions and 261 deletions
+23 -59
View File
@@ -5,69 +5,33 @@ const config = @import("config");
const ke = b.ke;
const ki = b.ke.private;
comptime {
if (!@hasDecl(ki.impl, "Cpu")) @compileError("impl must provide Cpu");
}
pub const PreemptionReason = enum(u8) {
None,
/// A higher priority thread was readied.
HigherPriority,
};
extern var __init_array_percpu_start: u8;
extern var __init_array_percpu_end: u8;
/// Global Per-CPU state.
pub const Cpu = struct {
/// Implementation-dependent CPU data.
impl: ki.impl.Cpu,
/// Unique ID of this CPU.
id: u32,
/// Current IPL on this CPU.
ipl: ke.Ipl,
/// Currently running thread.
current_thread: ?*ke.Thread,
/// Per-CPU idle thread.
idle_thread: ?*ke.Thread,
/// Thread selected for preemption.
next_thread: ?*ke.Thread,
/// The reason why the current thread was preempted.
preemption_reason: PreemptionReason,
/// Flag indicating the start of the scheduling timer.
start_timer: bool,
/// DPCs
resched_dpc: ke.Dpc,
resched_timer: ke.Timer,
const id = CpuLocal(u32, 0);
var curr_id: std.atomic.Value(u32) = .init(0);
/// Initialize `cpu` for usage with its idle thread being `thread`.
pub fn init(cpu: *Cpu, thread: *ke.Thread) void {
cpu.* = .{
.id = cpu.id,
.ipl = .Passive,
.idle_thread = thread,
.current_thread = thread,
.resched_dpc = .init(ki.sched.clock),
.next_thread = null,
.preemption_reason = .None,
.start_timer = false,
.resched_timer = undefined,
.impl = cpu.impl,
};
/// Initialize a CPU. Must be called on all CPUs.
pub fn init_cpu() void {
const start = @intFromPtr(&__init_array_percpu_start);
const end = @intFromPtr(&__init_array_percpu_end);
const count = (end - start) / @sizeOf(*const fn () void);
const funcs: [*]const *const fn () callconv(.c) void = @ptrFromInt(start);
// Call per-cpu init functions.
const start = @intFromPtr(&__init_array_percpu_start);
const end = @intFromPtr(&__init_array_percpu_end);
const count = (end - start) / @sizeOf(*const fn () void);
const funcs: [*]const *const fn () callconv(.c) void = @ptrFromInt(start);
for (0..count) |i| {
funcs[i]();
}
cpu.resched_timer.init();
if (curr_id.load(.monotonic) >= config.CONFIG_NCPUS) {
std.debug.panic("cpu: Too many CPUs booted! Maximum supported is {}", .{config.CONFIG_NCPUS});
}
};
id.local().* = curr_id.fetchAdd(1, .monotonic);
for (0..count) |i| {
funcs[i]();
}
}
pub fn current() u32 {
return id.local().*;
}
comptime {
if (!@hasDecl(ki.impl, "percpu_ptr")) @compileError("impl must provide percpu_ptr()");
@@ -85,8 +49,8 @@ pub fn CpuLocal(comptime T: type, comptime init: T) type {
}
/// Return a pointer to remote CPU data.
pub fn remote(cpu: *ke.Cpu) *T {
return ki.impl.percpu_ptr_other(&storage, cpu.id);
pub fn remote(cpu: u32) *T {
return ki.impl.percpu_ptr_other(&storage, cpu);
}
};
}