mirror of
https://github.com/rdmsr/zag.git
synced 2026-10-30 14:58:21 +00:00
feat(pl/ke): initial amd64 support
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
const b = @import("base");
|
||||
const ke = b.ke;
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
pub const Cpu = struct {
|
||||
self_ptr: *Cpu,
|
||||
percpu_offset: usize,
|
||||
};
|
||||
|
||||
pub const ThreadContext = struct {
|
||||
rdi: u64,
|
||||
rsi: u64,
|
||||
rbx: u64,
|
||||
rsp: u64,
|
||||
rbp: u64,
|
||||
r12: u64,
|
||||
r13: u64,
|
||||
r14: u64,
|
||||
r15: u64,
|
||||
|
||||
fn thread_trampoline(entry: usize, arg: usize) callconv(.c) void {
|
||||
const entry_fn: *const fn (?*anyopaque) void = @ptrFromInt(entry);
|
||||
entry_fn(@ptrFromInt(arg));
|
||||
// TODO: This shouldn't be reached!!!
|
||||
}
|
||||
|
||||
pub fn init(stack: b.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) @This() {
|
||||
var ctx: @This() = undefined;
|
||||
ctx.rdi = @intFromPtr(entry);
|
||||
ctx.rsi = @intFromPtr(arg);
|
||||
|
||||
var sp = stack + stack_size;
|
||||
{
|
||||
sp -= @sizeOf(usize);
|
||||
@as(*usize, @ptrFromInt(sp)).* = 0;
|
||||
|
||||
sp -= @sizeOf(usize);
|
||||
@as(*usize, @ptrFromInt(sp)).* = @intFromPtr(&thread_trampoline);
|
||||
}
|
||||
ctx.rsp = sp;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export fn do_switch_to() callconv(.naked) void {
|
||||
const asm_template = std.fmt.comptimePrint(
|
||||
\\movq %%rbx, {d}(%%rdi)
|
||||
\\movq %%rsp, {d}(%%rdi)
|
||||
\\movq %%rbp, {d}(%%rdi)
|
||||
\\movq %%r12, {d}(%%rdi)
|
||||
\\movq %%r13, {d}(%%rdi)
|
||||
\\movq %%r14, {d}(%%rdi)
|
||||
\\movq %%r15, {d}(%%rdi)
|
||||
\\
|
||||
\\movq {d}(%%rsi), %%rbx
|
||||
\\movq {d}(%%rsi), %%rsp
|
||||
\\movq {d}(%%rsi), %%rbp
|
||||
\\movq {d}(%%rsi), %%r12
|
||||
\\movq {d}(%%rsi), %%r13
|
||||
\\movq {d}(%%rsi), %%r14
|
||||
\\movq {d}(%%rsi), %%r15
|
||||
\\movq {d}(%%rsi), %%rdi
|
||||
\\movq {d}(%%rsi), %%rsi
|
||||
\\
|
||||
\\ret
|
||||
, .{
|
||||
@offsetOf(ThreadContext, "rbx"),
|
||||
@offsetOf(ThreadContext, "rsp"),
|
||||
@offsetOf(ThreadContext, "rbp"),
|
||||
@offsetOf(ThreadContext, "r12"),
|
||||
@offsetOf(ThreadContext, "r13"),
|
||||
@offsetOf(ThreadContext, "r14"),
|
||||
@offsetOf(ThreadContext, "r15"),
|
||||
|
||||
@offsetOf(ThreadContext, "rbx"),
|
||||
@offsetOf(ThreadContext, "rsp"),
|
||||
@offsetOf(ThreadContext, "rbp"),
|
||||
@offsetOf(ThreadContext, "r12"),
|
||||
@offsetOf(ThreadContext, "r13"),
|
||||
@offsetOf(ThreadContext, "r14"),
|
||||
@offsetOf(ThreadContext, "r15"),
|
||||
@offsetOf(ThreadContext, "rdi"),
|
||||
@offsetOf(ThreadContext, "rsi"),
|
||||
});
|
||||
|
||||
asm volatile (asm_template);
|
||||
}
|
||||
|
||||
pub fn switch_to(self: *ThreadContext, new: *ThreadContext) callconv(.c) void {
|
||||
asm volatile (
|
||||
\\call do_switch_to
|
||||
:
|
||||
: [_] "{rdi}" (self),
|
||||
[_] "{rsi}" (new),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn curcpu() *ke.Cpu {
|
||||
const asm_template = std.fmt.comptimePrint("movq %%gs:{d}, %[ret]", .{@offsetOf(Cpu, "self_ptr")});
|
||||
const impl_cpu = asm volatile (asm_template
|
||||
: [ret] "=r" (-> *Cpu),
|
||||
);
|
||||
|
||||
return @fieldParentPtr("impl", impl_cpu);
|
||||
}
|
||||
|
||||
inline fn percpu_ptr_for(variable: anytype, cpu: *ke.Cpu) @TypeOf(variable) {
|
||||
return @ptrFromInt(@intFromPtr(variable) +% cpu.impl.percpu_offset);
|
||||
}
|
||||
|
||||
pub inline fn percpu_ptr_other(variable: anytype, id: usize) @TypeOf(variable) {
|
||||
return percpu_ptr_for(variable, ke.cpus[id]);
|
||||
}
|
||||
|
||||
pub inline fn percpu_ptr(variable: anytype) @TypeOf(variable) {
|
||||
return percpu_ptr_for(variable, curcpu());
|
||||
}
|
||||
|
||||
pub inline fn set_hardware_ipl(_: ke.Ipl) void {}
|
||||
|
||||
pub inline fn enable_interrupts() void {
|
||||
asm volatile ("sti");
|
||||
}
|
||||
|
||||
pub inline fn disable_interrupts() bool {
|
||||
const rflags = asm volatile (
|
||||
\\cli
|
||||
\\pushfq
|
||||
\\popq %[rflags]
|
||||
: [rflags] "=r" (-> usize),
|
||||
);
|
||||
|
||||
return rflags & (1 << 9) != 0;
|
||||
}
|
||||
|
||||
pub inline fn restore_interrupts(state: bool) void {
|
||||
if (state) {
|
||||
enable_interrupts();
|
||||
} else {
|
||||
_ = disable_interrupts();
|
||||
}
|
||||
}
|
||||
|
||||
pub inline fn send_resched_ipi(_: *ke.Cpu) void {
|
||||
@panic("TODO send_resched_ipi");
|
||||
}
|
||||
|
||||
+12
-3
@@ -14,8 +14,17 @@ fn handler(_: ?*anyopaque) void {
|
||||
while (true) {}
|
||||
}
|
||||
|
||||
var temp_stack1: [8192]u8 align(16) = undefined;
|
||||
var temp_stack2: [8192]u8 align(16) = undefined;
|
||||
var temp_stack3: [8192]u8 align(16) = undefined;
|
||||
|
||||
var stacks: []const []u8 = &.{&temp_stack1, &temp_stack2, &temp_stack3};
|
||||
var which_stack: usize = 0;
|
||||
|
||||
fn make_thread(entry: *const fn (?*anyopaque) void, td: *ke.Thread, arg: ?*anyopaque) void {
|
||||
const _stack = std.heap.page_allocator.alloc(u8, 16384) catch @panic("wtf");
|
||||
std.debug.assert(which_stack < 3);
|
||||
const _stack = stacks[which_stack];
|
||||
which_stack += 1;
|
||||
|
||||
td.init(@intFromPtr(_stack.ptr), 16384, entry, arg);
|
||||
}
|
||||
@@ -49,8 +58,8 @@ pub fn init(boot_info: *pl.BootInfo) linksection(b.init) void {
|
||||
|
||||
ke.ipl.lower(ipl);
|
||||
} else {
|
||||
make_thread(&b.ex.fireworks.start, &threads[0], boot_info);
|
||||
ke.sched.enqueue(&threads[0]);
|
||||
// make_thread(&b.ex.fireworks.start, &threads[0], boot_info);
|
||||
// ke.sched.enqueue(&threads[0]);
|
||||
}
|
||||
|
||||
while (true) {}
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ pub fn raise(new: Ipl) Ipl {
|
||||
const old = cpu.ipl;
|
||||
|
||||
if (@intFromEnum(new) < @intFromEnum(old)) {
|
||||
ke.panic("ke.ipl.raise(): Target IPL ({}) is lower than current IPL ({})\n", .{ new, old });
|
||||
std.debug.panic("ke.ipl.raise(): Target IPL ({}) is lower than current IPL ({})", .{ new, old });
|
||||
}
|
||||
|
||||
if (@intFromEnum(new) > @intFromEnum(Ipl.get_max_software())) {
|
||||
@@ -53,7 +53,7 @@ pub fn lower(new: Ipl) void {
|
||||
const old = cpu.ipl;
|
||||
|
||||
if (@intFromEnum(new) > @intFromEnum(old)) {
|
||||
ke.panic("ke.ipl.lower(): Target IPL ({}) is higher than current IPL ({})\n", .{ new, old });
|
||||
std.debug.panic("ke.ipl.lower(): Target IPL ({}) is higher than current IPL ({})", .{ new, old });
|
||||
}
|
||||
|
||||
if (@intFromEnum(new) <= @intFromEnum(Ipl.get_max_software()) and @intFromEnum(old) > @intFromEnum(Ipl.get_max_software())) {
|
||||
|
||||
+7
-4
@@ -3,10 +3,13 @@ const b = @import("base");
|
||||
const ke = b.ke;
|
||||
const ki = ke.private;
|
||||
|
||||
/// Panics!
|
||||
pub fn panic(comptime fmt: []const u8, args: anytype) noreturn {
|
||||
// FIXME: this is probably broken.
|
||||
std.log.err("KERNEL PANIC: " ++ fmt, args);
|
||||
pub fn panic(
|
||||
msg: []const u8,
|
||||
first_trace_addr: ?usize,
|
||||
) noreturn {
|
||||
_ = first_trace_addr;
|
||||
|
||||
std.log.err("KERNEL PANIC: {s}", .{msg});
|
||||
|
||||
while (true) {}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ fn set_signals(enabled: bool) bool {
|
||||
const how = if (enabled) c.SIG_UNBLOCK else c.SIG_BLOCK;
|
||||
|
||||
if (c.pthread_sigmask(how, &newmask, &oldmask) != 0) {
|
||||
ke.panic("pthread_sigmask failed", .{});
|
||||
@panic("pthread_sigmask failed");
|
||||
}
|
||||
|
||||
return c.sigismember(&oldmask, c.SIGUSR1) == 0;
|
||||
@@ -67,7 +67,7 @@ pub const ThreadContext = struct {
|
||||
var new: ThreadContext = .{ .ucontext = undefined };
|
||||
|
||||
if (c.getcontext(&new.ucontext) == -1) {
|
||||
ke.panic("getcontext() failed", .{});
|
||||
@panic("getcontext() failed");
|
||||
}
|
||||
|
||||
new.ucontext.uc_stack.ss_sp = @ptrFromInt(stack);
|
||||
@@ -99,7 +99,7 @@ pub const ThreadContext = struct {
|
||||
thread.lock.release_no_ipl();
|
||||
|
||||
if (c.swapcontext(&self.ucontext, &new.ucontext) == -1) {
|
||||
ke.panic("swapcontext() failed", .{});
|
||||
@panic("swapcontext() failed");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
const std = @import("std");
|
||||
const ksyms = @import("ksyms");
|
||||
const pl = @import("base").pl;
|
||||
pub const rtl = @import("rtl");
|
||||
|
||||
const DebugWriter = struct {
|
||||
pub const Error = error{};
|
||||
pub const Writer = std.io.GenericWriter(*DebugWriter, Error, write);
|
||||
|
||||
fn write(self: *DebugWriter, bytes: []const u8) Error!usize {
|
||||
_ = self;
|
||||
for (bytes) |c| pl.debug_write(c);
|
||||
return bytes.len;
|
||||
}
|
||||
|
||||
fn writer(self: *DebugWriter) Writer {
|
||||
return .{ .context = self };
|
||||
}
|
||||
};
|
||||
|
||||
var debug_writer = DebugWriter{};
|
||||
|
||||
export fn kmain() callconv(.c) void {
|
||||
print("hello from kernel!\n", .{});
|
||||
|
||||
for (ksyms.ksyms) |sym| {
|
||||
print(" 0x{x:0>16} {s}\n", .{ sym.addr, sym.name });
|
||||
}
|
||||
|
||||
pl.first_init();
|
||||
|
||||
while (true) {
|
||||
asm volatile ("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
export fn memset(dest: [*]u8, c: u8, n: usize) [*]u8 {
|
||||
for (dest[0..n]) |*b| b.* = c;
|
||||
return dest;
|
||||
}
|
||||
|
||||
export fn memcpy(dest: [*]u8, src: [*]const u8, n: usize) [*]u8 {
|
||||
for (0..n) |i| dest[i] = src[i];
|
||||
return dest;
|
||||
}
|
||||
|
||||
export fn memmove(dest: [*]u8, src: [*]const u8, n: usize) [*]u8 {
|
||||
if (@intFromPtr(dest) < @intFromPtr(src)) {
|
||||
for (0..n) |i| dest[i] = src[i];
|
||||
} else {
|
||||
var i = n;
|
||||
while (i > 0) {
|
||||
i -= 1;
|
||||
dest[i] = src[i];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
const b = @import("base");
|
||||
|
||||
pub fn first_init() linksection(b.init) void {}
|
||||
@@ -1,6 +1,25 @@
|
||||
const entry = @import("entry.zig");
|
||||
const b = @import("base");
|
||||
const ki = b.ke.private;
|
||||
|
||||
pub const first_init = entry.first_init;
|
||||
pub const name = "AMD64";
|
||||
|
||||
pub fn first_init() void {}
|
||||
|
||||
pub fn early_init() void {
|
||||
const impl_cpu = &ki.bootstrap_cpu.impl;
|
||||
impl_cpu.self_ptr = impl_cpu;
|
||||
impl_cpu.percpu_offset = 0;
|
||||
|
||||
const impl_cpu_ptr = @intFromPtr(impl_cpu);
|
||||
asm volatile ("wrmsr"
|
||||
:
|
||||
: [_] "{eax}" (@as(u32, @intCast(impl_cpu_ptr & 0xFFFFFFFF))),
|
||||
[_] "{edx}" (@as(u32, @truncate(impl_cpu_ptr >> 32))),
|
||||
[_] "{ecx}" (0xC000_0101),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn late_init() void {}
|
||||
|
||||
pub fn devices_init() void {}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
const b = @import("base");
|
||||
const ksyms = @import("ksyms");
|
||||
|
||||
const pl = b.pl;
|
||||
const ke = b.ke;
|
||||
|
||||
pub const std_options = b.std_options;
|
||||
pub const panic = b.panic;
|
||||
|
||||
pub const rtl = @import("rtl");
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
var boot_info: pl.BootInfo = undefined;
|
||||
|
||||
export fn kmain() callconv(.c) void {
|
||||
std.log.info("hello from kernel!", .{});
|
||||
|
||||
for (ksyms.ksyms) |sym| {
|
||||
std.log.info(" 0x{x:0>16} {s}", .{ sym.addr, sym.name });
|
||||
}
|
||||
|
||||
boot_info.memory_map.entry_count = 0;
|
||||
|
||||
var cpus: [1]*ke.Cpu = .{&ke.private.bootstrap_cpu};
|
||||
|
||||
ke.ncpus = 1;
|
||||
ke.cpus = &cpus;
|
||||
|
||||
ke.private.init(&boot_info);
|
||||
}
|
||||
@@ -10,6 +10,10 @@ else if (@hasDecl(config, "CONFIG_ARCH_UM"))
|
||||
else
|
||||
@compileError("unsupported architecture");
|
||||
|
||||
pub inline fn first_init() void {
|
||||
return impl.first_init();
|
||||
}
|
||||
|
||||
/// Early platform-specific initialization, called before anything else.
|
||||
pub inline fn early_init() void {
|
||||
return impl.early_init();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const b = @import("base");
|
||||
|
||||
pub const std_options = b.std_options;
|
||||
pub const panic = b.panic;
|
||||
|
||||
pub const rtl = @import("rtl");
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ pub fn arm_timer(ns: b.Nanoseconds) void {
|
||||
}, .it_interval = .{ .tv_sec = 0, .tv_nsec = 0 } };
|
||||
|
||||
if (c.timer_settime(timerid, 0, &value, null) == -1) {
|
||||
ke.panic("timer_settime failed", .{});
|
||||
@panic("timer_settime failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,6 @@ pub fn init_cpu() void {
|
||||
sev._sigev_un._tid = linux.gettid();
|
||||
|
||||
if (c.timer_create(c.CLOCK_MONOTONIC, &sev, &timerid) == -1) {
|
||||
ke.panic("timer_create failed", .{});
|
||||
@panic("timer_create failed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,3 +40,5 @@ pub fn tib(comptime N: u32) usize {
|
||||
pub const std_options = std.Options{
|
||||
.logFn = ke.log.log,
|
||||
};
|
||||
|
||||
pub const panic = std.debug.FullPanic(ke.panic);
|
||||
|
||||
Reference in New Issue
Block a user