ke: rework kernel initialization

This commit is contained in:
Loïc Grégoire
2026-08-05 16:10:37 -04:00
parent 4aaae63349
commit 2a09642f4d
11 changed files with 58 additions and 31 deletions
+5 -1
View File
@@ -26,6 +26,7 @@ const ThreadFrame = extern struct {
extern fn asm_thread_entry() void;
extern fn do_context_switch(old: *ThreadContext, new: *ThreadContext, lock: *u8, switching: *bool) callconv(.c) void;
extern fn do_context_load(td: *ThreadContext) callconv(.c) void;
pub const ThreadContext = extern struct {
rsp: u64 align(1),
@@ -35,7 +36,6 @@ pub const ThreadContext = extern struct {
const entry_fn: *const fn (?*anyopaque) void = @ptrFromInt(entry);
ke.ipl.lower(.Passive);
entry_fn(@ptrFromInt(arg));
// TODO: This shouldn't be reached!!!
}
pub fn init(stack: r.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) @This() {
@@ -62,6 +62,10 @@ pub const ThreadContext = extern struct {
const thread: *ke.Thread = @alignCast(@fieldParentPtr("context", self));
do_context_switch(self, new, &thread.lock.inner.locked.raw, &thread.switching.raw);
}
pub fn load(self: *ThreadContext) callconv(.c) void {
do_context_load(self);
}
};
inline fn percpu_ptr_for(variable: anytype, cpu: u32) @TypeOf(variable) {
+5 -4
View File
@@ -91,16 +91,17 @@ pub fn ap_entry(cpu_id: u32, booted: *std.atomic.Value(usize)) noreturn {
ki.cpu.init_cpu(cpu_id);
pl.impl.init_ap();
ki.sched.percpu.local().current_thread = pl.impl.smp.start_thread.local().*;
const sched = ki.sched.percpu.local();
sched.current_thread = pl.impl.smp.start_thread.local().*;
sched.idle_thread = sched.current_thread;
amd64.sti();
_ = booted.fetchAdd(1, .monotonic);
ke.ipl.lower(.Passive);
while (true) {
std.atomic.spinLoopHint();
}
ki.sched.idle(null);
}
var initial_offsets: [1]usize = .{0};
+13
View File
@@ -166,6 +166,19 @@ do_context_switch:
pop %r15
ret
.global do_context_load
do_context_load:
/* Load new stack pointer. */
mov (%rdi), %rsp
/* Restore callee-saved registers. */
pop %rbp
pop %rbx
pop %r12
pop %r13
pop %r14
pop %r15
ret
.extern thread_entry
.global asm_thread_entry
+21 -14
View File
@@ -7,10 +7,24 @@ const ki = ke.private;
const ex = r.ex;
var thread0: ke.Thread = undefined;
var stack: [r.kib(16)]u8 align(16) = undefined;
pub fn init(boot_info: *r.BootInfo) linksection(r.init) void {
export fn kmain(boot_info: *r.BootInfo) callconv(.c) void {
r.boot_info = boot_info;
thread0.init(
boot_info.kernel_stack,
boot_info.kernel_stack_size,
ke.Thread.Priority.idle_low,
init,
null,
);
thread0.pinned = true;
// Jump into the thread.
thread0.context.load();
}
pub fn init(_: ?*anyopaque) linksection(r.init) void {
ke.ncpus = 1;
ki.impl.early_init();
ki.tunable.init();
@@ -20,18 +34,11 @@ pub fn init(boot_info: *r.BootInfo) linksection(r.init) void {
ki.cpu.init_cpu(0);
ki.turnstile.init_turnstiles();
thread0.init(
@intFromPtr(&stack),
r.kib(16),
ke.Thread.Priority.idle_low,
ki.sched.idle,
null,
);
thread0.priority = 0;
thread0.base_priority = 0;
thread0.pinned = true;
ki.sched.percpu.local().current_thread = &thread0;
ki.sched.percpu.local().idle_thread = &thread0;
std.log.info("Zag for {s} ({s}), cmdline is \"{?s}\"", .{ pl.name, arch.name, boot_info.cmdline });
std.log.info("Zag for {s} ({s}), cmdline is \"{?s}\"", .{ pl.name, arch.name, r.boot_info.cmdline });
ex.init();
ki.sched.idle(null);
}