refactor: use root instead of base

This commit is contained in:
Loïc Grégoire
2026-04-17 20:58:25 -04:00
parent c68a93b302
commit 1aa6743f43
45 changed files with 199 additions and 229 deletions
+5 -40
View File
@@ -76,8 +76,7 @@ pub fn build(b: *std.Build) void {
},
});
const base_nosym = makeBaseModule(b, optimize, config_module, rtl_module, ksyms_module);
const kernel_nosym = addKernel(b, plat, optimize, config_module, ksyms_module, rtl_module, base_nosym, arch_module);
const kernel_nosym = addKernel(b, plat, optimize, config_module, ksyms_module, rtl_module, arch_module);
kernel_nosym.root_module.addAssemblyFile(empty_ksyms_s);
@@ -91,10 +90,8 @@ pub fn build(b: *std.Build) void {
ksyms.addArg("--output");
const ksyms_s = ksyms.addOutputFileArg("ksyms.s");
const base_module = makeBaseModule(b, optimize, config_module, rtl_module, ksyms_module);
// second pass
const kernel = addKernel(b, plat, optimize, config_module, ksyms_module, rtl_module, base_module, arch_module);
const kernel = addKernel(b, plat, optimize, config_module, ksyms_module, rtl_module, arch_module);
kernel.root_module.addAssemblyFile(ksyms_s);
b.default_step.dependOn(&b.addInstallArtifact(kernel, .{}).step);
@@ -108,7 +105,6 @@ pub fn build(b: *std.Build) void {
.{ .name = "rtl", .module = rtl_module },
},
});
docs_module.addImport("base", docs_module);
const docs_obj = b.addObject(.{
.name = "kernel_docs",
@@ -173,54 +169,23 @@ fn targetQueryForPlatform(plat: config.Platform) std.Target.Query {
return q;
}
fn makeBaseModule(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
config_module: *std.Build.Module,
rtl_module: *std.Build.Module,
ksyms_module: *std.Build.Module,
) *std.Build.Module {
const base = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.imports = &.{
.{ .name = "config", .module = config_module },
.{ .name = "rtl", .module = rtl_module },
.{ .name = "ksyms", .module = ksyms_module },
},
.optimize = optimize,
});
base.addImport("base", base);
return base;
}
fn addKernel(b: *std.Build, plat: config.Platform, optimize: std.builtin.OptimizeMode, config_module: *std.Build.Module, ksyms_module: *std.Build.Module, rtl: *std.Build.Module, base: *std.Build.Module, arch: *std.Build.Module) *std.Build.Step.Compile {
fn addKernel(b: *std.Build, plat: config.Platform, optimize: std.builtin.OptimizeMode, config_module: *std.Build.Module, ksyms_module: *std.Build.Module, rtl: *std.Build.Module, arch: *std.Build.Module) *std.Build.Step.Compile {
const target = b.resolveTargetQuery(targetQueryForPlatform(plat));
const name = b.fmt("kernel-{s}", .{@tagName(plat.arch)});
const platform_name = if (plat.bootloader) |bl|
switch (bl) {
.Limine => "boot/limine",
}
else if (plat.os == .linux) "platform/um" else @tagName(plat.arch);
base.addImport("arch", arch);
const root_source_file = b.fmt("src/{s}/entry.zig", .{platform_name});
const kernel = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(root_source_file),
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
}),
});
base.addImport("ksyms", ksyms_module);
kernel.root_module.addImport("arch", arch);
kernel.root_module.addImport("config", config_module);
kernel.root_module.addImport("ksyms", ksyms_module);
kernel.root_module.addImport("base", base);
kernel.root_module.addImport("rtl", rtl);
kernel.use_llvm = true;
+1 -1
View File
@@ -1,4 +1,4 @@
const b = @import("base");
const b = @import("root");
const ksyms = @import("ksyms");
const limine = @import("limine.zig");
+1 -1
View File
@@ -1,6 +1,6 @@
//! Fireworks test ported from the Boron operating system.
const std = @import("std");
const base = @import("base");
const base = @import("root");
const ke = base.ke;
const sintab = @import("sintab.zig");
+1 -1
View File
@@ -1,5 +1,5 @@
const std = @import("std");
const b = @import("base");
const b = @import("root");
const pl = b.pl;
const ke = b.ke;
const mm = b.mm;
+6 -6
View File
@@ -1,14 +1,14 @@
const b = @import("base");
const ke = b.ke;
const pl = b.pl;
const amd64 = b.arch;
const r = @import("root");
const ke = r.ke;
const pl = r.pl;
const amd64 = r.arch;
const std = @import("std");
pub const init = @import("init.zig");
pub const early_init = init.early_init;
pub export var cpu_self_offset: usize linksection(b.percpu) = 0;
pub export var cpu_self_offset: usize linksection(r.percpu) = 0;
pub export var cpu_offsets: [*]usize = undefined;
pub const ThreadContext = struct {
@@ -28,7 +28,7 @@ pub const ThreadContext = struct {
// TODO: This shouldn't be reached!!!
}
pub fn init(stack: b.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) @This() {
pub fn init(stack: r.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) @This() {
var ctx: @This() = undefined;
ctx.rdi = @intFromPtr(entry);
ctx.rsi = @intFromPtr(arg);
+6 -6
View File
@@ -1,11 +1,11 @@
const amd64 = @import("arch");
const int = @import("int.zig");
const b = @import("base");
const r = @import("root");
const std = @import("std");
const impl = @import("impl.zig");
const ke = b.ke;
const ki = b.ke.private;
const pl = b.pl;
const ke = r.ke;
const ki = r.ke.private;
const pl = r.pl;
var gdt = extern struct {
entries: [9]u64 align(1),
@@ -28,7 +28,7 @@ var gdt = extern struct {
extern fn gdt_load(gdtr: *const amd64.Gdtr) callconv(.{ .x86_64_sysv = .{} }) void;
fn early_cpu_init() linksection(b.init) void {
fn early_cpu_init() linksection(r.init) void {
const gdtr: amd64.Gdtr = .{
.limit = @sizeOf(@TypeOf(gdt)) - 1,
.base = @intFromPtr(&gdt),
@@ -96,7 +96,7 @@ pub fn ap_entry(cpu_id: u32) noreturn {
var initial_offsets: [1]usize = undefined;
extern var __percpu_start: u8;
pub fn early_init() linksection(b.init) void {
pub fn early_init() linksection(r.init) void {
amd64.detect_cpu_features();
early_cpu_init();
+3 -3
View File
@@ -1,7 +1,7 @@
const amd64 = @import("arch");
const std = @import("std");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
extern const __interrupt_vectors: [256]usize;
@@ -79,7 +79,7 @@ export fn isr_handler_main(frame: *const amd64.IrqFrame) callconv(.{ .x86_64_sys
extern fn idt_load(idt_ptr: *const amd64.Idtr) callconv(.{ .x86_64_sysv = .{} }) void;
pub fn init() linksection(b.init) void {
pub fn init() linksection(r.init) void {
const idtr: amd64.Idtr = .{
.limit = @sizeOf(@TypeOf(idt)) - 1,
.base = @intFromPtr(&idt),
+3 -3
View File
@@ -1,9 +1,9 @@
const std = @import("std");
const rtl = @import("rtl");
const b = @import("base");
const r = @import("root");
const config = @import("config");
const ke = b.ke;
const ki = b.ke.private;
const ke = r.ke;
const ki = r.ke.private;
extern var __init_array_percpu_start: u8;
extern var __init_array_percpu_end: u8;
+4 -4
View File
@@ -1,8 +1,8 @@
const std = @import("std");
const config = @import("config");
const rtl = @import("rtl");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
/// Deferred Procedure Call (DPC) structure.
@@ -40,9 +40,9 @@ const pcpu = ke.CpuLocal(PerCpu, .{
.queue = undefined,
});
export const dpc_percpu_init linksection(b.percpu_init) = &pcpu_init;
export const dpc_percpu_init linksection(r.percpu_init) = &pcpu_init;
fn pcpu_init() linksection(b.init) callconv(.c) void {
fn pcpu_init() linksection(r.init) callconv(.c) void {
pcpu.local().queue.init();
}
+6 -6
View File
@@ -1,15 +1,15 @@
const std = @import("std");
const b = @import("base");
const pl = b.pl;
const ke = b.ke;
const arch = b.arch;
const r = @import("root");
const pl = r.pl;
const ke = r.ke;
const arch = r.arch;
const ki = ke.private;
const ex = b.ex;
const ex = r.ex;
var thread0: ke.Thread = undefined;
var stack: [8192]u8 align(16) = undefined;
pub fn init(boot_info: *pl.BootInfo) linksection(b.init) void {
pub fn init(boot_info: *pl.BootInfo) linksection(r.init) void {
ki.impl.early_init();
std.log.info("hello, world", .{});
pl.early_init(boot_info);
+2 -2
View File
@@ -1,6 +1,6 @@
const std = @import("std");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
/// Interrupt priority level (IPL)
+3 -3
View File
@@ -1,8 +1,8 @@
const std = @import("std");
const b = @import("base");
const r = @import("root");
const rtl = @import("rtl");
const pl = b.pl;
const ke = b.ke;
const pl = r.pl;
const ke = r.ke;
const ki = ke.private;
const config = @import("config");
+2 -2
View File
@@ -58,9 +58,9 @@
//! the next generation and a stub (containing only the descriptor ID) is left at
//! the original position so the data ring can be walked linearly during eviction.
const std = @import("std");
const base = @import("base");
const r = @import("root");
const rtl = @import("rtl");
const ke = base.ke;
const ke = r.ke;
const ki = ke.private;
const BlkPos = struct {
+2 -2
View File
@@ -1,8 +1,8 @@
const std = @import("std");
const config = @import("config");
const b = @import("base");
const r = @import("root");
const ksyms = @import("ksyms");
const ke = b.ke;
const ke = r.ke;
const ki = ke.private;
extern var text_start_addr: u8;
-4
View File
@@ -1,10 +1,6 @@
const std = @import("std");
const config = @import("config");
const b = @import("base");
const ke = b.ke;
const pl = b.pl;
const init_mod = @import("init.zig");
pub const impl = if (@hasDecl(config, "CONFIG_ARCH_AMD64"))
+5 -5
View File
@@ -84,8 +84,8 @@
const std = @import("std");
const rtl = @import("rtl");
const config = @import("config");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
comptime {
@@ -274,10 +274,10 @@ pub fn unblock(td: *ke.Thread) void {
td.lock.release(ipl);
}
export const sched_percpu_init linksection(b.percpu_init) = &init_cpu;
export const sched_percpu_init linksection(r.percpu_init) = &init_cpu;
/// Initialize a CPU for use by the scheduler.
fn init_cpu() linksection(b.init) callconv(.c) void {
fn init_cpu() linksection(r.init) callconv(.c) void {
var cpu = percpu.local();
cpu.* = .{
@@ -312,7 +312,7 @@ fn init_cpu() linksection(b.init) callconv(.c) void {
}
/// Called on CPU 0 to initialize load balancing mechanisms.
pub fn late_init() linksection(b.init) void {
pub fn late_init() linksection(r.init) void {
balance_dpc = ke.Dpc.init(balance);
balance_timer.init();
ke.timer.set(&balance_timer, balance_interval * std.time.ns_per_ms, &balance_dpc);
+1 -2
View File
@@ -1,6 +1,5 @@
const std = @import("std");
const b = @import("base");
const ke = b.ke;
const ke = @import("root").ke;
/// Simple Spin lock implementation.
pub const SpinLock = struct {
+3 -3
View File
@@ -1,8 +1,8 @@
const std = @import("std");
const b = @import("base");
const r = @import("root");
const rtl = @import("rtl");
const ke = b.ke;
const ke = r.ke;
const ki = ke.private;
/// Structure representing a kernel thread.
@@ -95,7 +95,7 @@ pub const Thread = struct {
/// - `stack_size`: Size of the stack
/// - `entry`: Entry point of the thread
/// - `arg`: Extraneous argument to be passed to `entry`
pub fn init(thread: *Thread, stack: b.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) void {
pub fn init(thread: *Thread, stack: r.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) void {
thread.* = .{
.context = .init(stack, stack_size, entry, arg),
.lock = .init(),
+4 -4
View File
@@ -1,7 +1,7 @@
const std = @import("std");
const rtl = @import("rtl");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
/// TimeCounter structure.
@@ -88,7 +88,7 @@ pub fn register_source(tc: *TimeCounter) void {
}
/// Return the time elapsed since boot in nanoseconds.
pub fn read_time_nano() b.Nanoseconds {
pub fn read_time_nano() r.Nanoseconds {
const tc = best_tc orelse return 0;
const s = state.load();
@@ -103,7 +103,7 @@ pub fn best() ?*TimeCounter {
}
/// Do a busy sleep of `ns` nanoseconds using TimeCounter.
pub fn sleep(ns: b.Nanoseconds) void {
pub fn sleep(ns: r.Nanoseconds) void {
const start = read_time_nano();
while (true) {
+5 -5
View File
@@ -2,10 +2,10 @@
//! Timer objects are useful when one wants to wait a fixed amount of time for an event to occur.
const std = @import("std");
const rtl = @import("rtl");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const ki = ke.private;
const pl = b.pl;
const pl = r.pl;
const PerCpu = struct {
/// Heap of pending timers on this CPU.
@@ -30,7 +30,7 @@ pub const Timer = struct {
/// Timer state.
state: std.atomic.Value(State),
/// When the timer is bound to expire.
deadline: b.Nanoseconds,
deadline: r.Nanoseconds,
/// Attached DPC.
dpc: ?*ke.Dpc,
/// Intrusive pairing heap node.
@@ -60,7 +60,7 @@ const percpu = ke.CpuLocal(PerCpu, .{
/// Start a timer with an expiration time.
/// A DPC that will be enqueued upon expiration can be passed.
pub fn set(timer: *Timer, time: b.Nanoseconds, dpc: ?*ke.Dpc) void {
pub fn set(timer: *Timer, time: r.Nanoseconds, dpc: ?*ke.Dpc) void {
const ipl = timer.hdr.lock.acquire();
defer timer.hdr.lock.release(ipl);
+8 -8
View File
@@ -1,7 +1,7 @@
const std = @import("std");
const b = @import("base");
const c = b.pl.impl.c;
const ke = b.ke;
const r = @import("root");
const c = r.pl.impl.c;
const ke = r.ke;
const ki = ke.private;
fn set_signals(enabled: bool) bool {
@@ -45,7 +45,7 @@ pub fn restore_interrupts(val: bool) void {
}
pub fn send_resched_ipi(cpu: u32) void {
_ = c.pthread_kill(b.pl.impl.percpu.remote(cpu).pthread, c.SIGUSR1);
_ = c.pthread_kill(r.pl.impl.percpu.remote(cpu).pthread, c.SIGUSR1);
}
pub const ThreadContext = struct {
@@ -58,7 +58,7 @@ pub const ThreadContext = struct {
entry(arg);
}
pub fn init(stack: b.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) ThreadContext {
pub fn init(stack: r.VAddr, stack_size: usize, entry: *const fn (?*anyopaque) void, arg: ?*anyopaque) ThreadContext {
var new: ThreadContext = .{ .ucontext = undefined };
if (c.getcontext(&new.ucontext) == -1) {
@@ -103,13 +103,13 @@ const bootstrap_offsets = [1]usize{0};
pub fn early_init() void {
// Ensure we can use per-cpu data.
b.pl.impl.cpu_offsets = @constCast(&bootstrap_offsets);
r.pl.impl.cpu_offsets = @constCast(&bootstrap_offsets);
}
pub inline fn percpu_ptr_other(variable: anytype, cpu_id: usize) @TypeOf(variable) {
return @ptrFromInt(@intFromPtr(variable) +% b.pl.impl.cpu_offsets[cpu_id]);
return @ptrFromInt(@intFromPtr(variable) +% r.pl.impl.cpu_offsets[cpu_id]);
}
pub inline fn percpu_ptr(variable: anytype) @TypeOf(variable) {
return @ptrFromInt(@intFromPtr(variable) +% b.pl.impl.cpu_offsets[b.pl.impl.my_cpu_id]);
return @ptrFromInt(@intFromPtr(variable) +% r.pl.impl.cpu_offsets[r.pl.impl.my_cpu_id]);
}
+4 -4
View File
@@ -3,8 +3,8 @@
//! See more here: https://youtu.be/OAAiOEQhsK0
const rtl = @import("rtl");
const std = @import("std");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
/// Header for waitable objects.
/// This must be added to any structured which is considered waitable.
@@ -73,7 +73,7 @@ pub const Status = enum(u8) {
};
/// Wait for the provided object to be signaled.
pub fn wait_one(object: *DispatchHeader, timeout: ?b.Nanoseconds) !usize {
pub fn wait_one(object: *DispatchHeader, timeout: ?r.Nanoseconds) !usize {
var objects = [_]*DispatchHeader{object};
return wait_any(&objects, timeout, null);
}
@@ -83,7 +83,7 @@ pub fn wait_one(object: *DispatchHeader, timeout: ?b.Nanoseconds) !usize {
/// If `timeout` is not provided, it will wait indefinitely.
/// If `waitblocks` is specified, then the wait will use those waitblocks for the operation.
/// Note that if `timeout` is provided, then one additional waitblock must be allocated.
pub fn wait_any(objects: []*DispatchHeader, timeout: ?b.Nanoseconds, waitblocks: ?[*]WaitBlock) !usize {
pub fn wait_any(objects: []*DispatchHeader, timeout: ?r.Nanoseconds, waitblocks: ?[*]WaitBlock) !usize {
const ipl = ke.ipl.raise(.Dispatch);
defer ke.ipl.lower(ipl);
const curtd = ke.curcpu().current_thread.?;
+10 -10
View File
@@ -1,15 +1,15 @@
const amd64 = @import("arch");
const b = @import("base");
const mm = b.mm;
const r = @import("root");
const mm = r.mm;
const mi = mm.private;
pub const hhdm_minimum_max_address = b.gib(4);
pub const hhdm_minimum_max_address = r.gib(4);
pub fn phys_to_virt(addr: b.PAddr) b.VAddr {
pub fn phys_to_virt(addr: r.PAddr) r.VAddr {
return addr + 0xffff800000000000;
}
pub fn virt_to_phys(vaddr: b.VAddr) usize {
pub fn virt_to_phys(vaddr: r.VAddr) usize {
return vaddr - 0xffff800000000000;
}
@@ -42,12 +42,12 @@ pub const PagingImpl = struct {
ignored_2: u11,
nx: bool,
pub fn address(self: Pte) b.PAddr {
return @as(b.PAddr, self.addr) << 12;
pub fn address(self: Pte) r.PAddr {
return @as(r.PAddr, self.addr) << 12;
}
};
pub inline fn make_table_pte(pa: b.PAddr) Pte {
pub inline fn make_table_pte(pa: r.PAddr) Pte {
return Pte{
.present = true,
.writable = true,
@@ -65,7 +65,7 @@ pub const PagingImpl = struct {
};
}
pub inline fn make_leaf_pte(pa: b.PAddr, flags: mm.MapFlags, level: usize) Pte {
pub inline fn make_leaf_pte(pa: r.PAddr, flags: mm.MapFlags, level: usize) Pte {
return Pte{
.present = true,
.writable = flags.write,
@@ -83,7 +83,7 @@ pub const PagingImpl = struct {
};
}
pub inline fn activate(root_pa: b.PAddr) void {
pub inline fn activate(root_pa: r.PAddr) void {
amd64.write_cr(3, root_pa);
}
+4 -4
View File
@@ -1,13 +1,13 @@
const b = @import("base");
const ke = b.ke;
const mm = b.mm;
const r = @import("root");
const ke = r.ke;
const mm = r.mm;
const mi = mm.private;
var heap_lock: ke.SpinLock = .init();
var heap_arena: mm.vmem.Arena = undefined;
pub fn init() void {
heap_arena.init("kernel heap", mi.impl.kernel_heap_base, b.tib(16), mm.page_size) catch @panic("failed to initialize kernel heap arena");
heap_arena.init("kernel heap", mi.impl.kernel_heap_base, r.tib(16), mm.page_size) catch @panic("failed to initialize kernel heap arena");
}
pub fn alloc(size: usize) mm.Error!*anyopaque {
+4 -4
View File
@@ -1,6 +1,6 @@
const b = @import("base");
const pl = b.pl;
const mm = b.mm;
const r = @import("root");
const pl = r.pl;
const mm = r.mm;
const mi = mm.private;
const config = @import("config");
@@ -87,7 +87,7 @@ else
pub fn call(_: *pl.BootInfo) void {}
}.call;
pub fn init(boot_info: *pl.BootInfo) linksection(b.init) void {
pub fn init(boot_info: *pl.BootInfo) linksection(r.init) void {
mi.phys.init(boot_info);
map_kernel(boot_info);
mi.kernel_pmap.activate();
+7 -7
View File
@@ -6,13 +6,13 @@
//! When freeing pages, they are added to the active batch until it reaches capacity,
//! at which point the full batch is moved to the local depot.
//! If the depot is full when trying to add a new batch, one batch is pushed back to the global stack to make room.
const b = @import("base");
const r = @import("root");
const std = @import("std");
const rtl = @import("rtl");
const pl = b.pl;
const mm = b.mm;
const pl = r.pl;
const mm = r.mm;
const mi = mm.private;
const ke = b.ke;
const ke = r.ke;
const log = std.log.scoped(.@"mm/phys");
@@ -113,7 +113,7 @@ fn early_alloc() usize {
}
/// Allocate a page of physical memory.
pub fn alloc() b.PAddr {
pub fn alloc() r.PAddr {
if (!bootstrapped) {
return early_alloc();
}
@@ -160,7 +160,7 @@ pub fn alloc() b.PAddr {
}
/// Free a page of physical memory.
pub fn free(addr: b.PAddr) void {
pub fn free(addr: r.PAddr) void {
const ipl = ke.ipl.raise(.High);
defer ke.ipl.lower(ipl);
@@ -200,7 +200,7 @@ pub fn free(addr: b.PAddr) void {
}
}
pub fn init(boot_info: *pl.BootInfo) linksection(b.init) void {
pub fn init(boot_info: *pl.BootInfo) linksection(r.init) void {
memory_map = &boot_info.memory_map;
var total_usable_memory: usize = 0;
+9 -9
View File
@@ -20,13 +20,13 @@
//! cases (contiguous PA range and on-demand allocation); custom sources can
//! be passed directly to `map_from` for other needs such as file-backed or
//! scatter-gather mappings.
const b = @import("base");
const mm = b.mm;
const r = @import("root");
const mm = r.mm;
const mi = mm.private;
/// Describes a contiguous region of physical memory to be mapped at a given virtual address.
pub const MapItem = struct {
pa: b.PAddr,
pa: r.PAddr,
len: usize,
flags: mm.MapFlags,
};
@@ -40,7 +40,7 @@ const ContiguousSource = struct {
base_va: usize,
base_pa: usize,
pub fn next(self: *const ContiguousSource, _: b.VAddr) MapItem {
pub fn next(self: *const ContiguousSource, _: r.VAddr) MapItem {
return .{
.pa = self.base_pa,
.len = self.size,
@@ -54,7 +54,7 @@ const ContiguousSource = struct {
const AllocatingSource = struct {
flags: mm.MapFlags,
pub fn next(self: *const AllocatingSource, _: b.VAddr) MapItem {
pub fn next(self: *const AllocatingSource, _: r.VAddr) MapItem {
const pa = mi.phys.alloc();
return .{
.pa = pa,
@@ -72,7 +72,7 @@ pub const PMap = struct {
impl: mi.impl.PMap,
/// Map a virtual address range to physical addresses provided by the source.
pub fn map_from(self: *Self, va_start: b.VAddr, size: usize, source: anytype) void {
pub fn map_from(self: *Self, va_start: r.VAddr, size: usize, source: anytype) void {
const Source = @TypeOf(source);
comptime {
@@ -83,7 +83,7 @@ pub const PMap = struct {
}
/// Map a contiguous virtual address range to a contiguous physical address range.
pub fn map_contiguous_range(self: *Self, va: b.VAddr, pa: b.PAddr, size: usize, flags: mm.MapFlags) void {
pub fn map_contiguous_range(self: *Self, va: r.VAddr, pa: r.PAddr, size: usize, flags: mm.MapFlags) void {
const src = ContiguousSource{
.flags = flags,
.size = size,
@@ -95,7 +95,7 @@ pub const PMap = struct {
}
/// Map a virtual address range to physical addresses allocated on demand.
pub fn map_range_allocating(self: *Self, va: b.VAddr, size: usize, flags: mm.MapFlags) void {
pub fn map_range_allocating(self: *Self, va: r.VAddr, size: usize, flags: mm.MapFlags) void {
const src = AllocatingSource{
.flags = flags,
};
@@ -104,7 +104,7 @@ pub const PMap = struct {
}
/// Map a single page. Convenience wrapper around map_contiguous_range.
pub fn map_page(self: *Self, va: b.VAddr, pa: b.PAddr, flags: mm.MapFlags) void {
pub fn map_page(self: *Self, va: r.VAddr, pa: r.PAddr, flags: mm.MapFlags) void {
self.map_contiguous_range(va, pa, mm.page_size, flags);
}
+7 -7
View File
@@ -24,8 +24,8 @@
//! - `activate(root_pa: PAddr) void`: install the root page table into
//! the CPU (e.g. write CR3 or satp).
const b = @import("base");
const mm = b.mm;
const r = @import("root");
const mm = r.mm;
const mi = mm.private;
const std = @import("std");
@@ -42,7 +42,7 @@ pub fn RadixPmap(comptime Impl: type) type {
const entries_per_table = 512;
const index_mask = entries_per_table - 1;
root_pa: b.PAddr,
root_pa: r.PAddr,
fn level_shift(level: usize) u6 {
return Impl.levels[level].shift;
@@ -79,7 +79,7 @@ pub fn RadixPmap(comptime Impl: type) type {
Impl.activate(self.root_pa);
}
pub fn map_from(self: *Self, va: b.VAddr, size: usize, source: anytype) void {
pub fn map_from(self: *Self, va: r.VAddr, size: usize, source: anytype) void {
var c = self.cursor(va);
var remain = size;
@@ -94,7 +94,7 @@ pub fn RadixPmap(comptime Impl: type) type {
/// the given virtual address range, allocating them as needed. Useful for
/// pre-populating a shared region (e.g. the kernel half of a user pmap)
/// before any leaf mappings are installed.
pub fn preallocate(self: *Self, va: b.VAddr, size: usize, target_level: usize) void {
pub fn preallocate(self: *Self, va: r.VAddr, size: usize, target_level: usize) void {
var c = self.cursor(va);
var remain = size;
@@ -112,7 +112,7 @@ pub fn RadixPmap(comptime Impl: type) type {
pub const Cursor = struct {
pmap: *Self,
va: b.VAddr,
va: r.VAddr,
/// Cached pointers to each level's page table, filled as the cursor
/// descends. Only indices in `[0, top_level]` are valid.
tables: [num_levels][*]Impl.Pte,
@@ -176,7 +176,7 @@ pub fn RadixPmap(comptime Impl: type) type {
@panic("no leaf level can map current alignment/size");
}
pub fn map_range(self: *Cursor, pa: b.PAddr, size: usize, flags: mm.MapFlags) void {
pub fn map_range(self: *Cursor, pa: r.PAddr, size: usize, flags: mm.MapFlags) void {
var remain = size;
var current_pa = pa;
-1
View File
@@ -1,6 +1,5 @@
pub const private = @import("private.zig");
const b = @import("base");
const std = @import("std");
pub const page_size = 4096;
+9 -9
View File
@@ -1,4 +1,4 @@
const b = @import("base");
const r = @import("root");
const std = @import("std");
const linux = std.os.linux;
@@ -9,39 +9,39 @@ pub const hhdm_minimum_max_address: usize = 0;
pub fn init_kernel() void {
kernel_heap_base = linux.mmap(
null,
b.gib(8),
r.gib(8),
.{ .READ = true, .WRITE = true },
.{ .ANONYMOUS = true, .TYPE = .PRIVATE },
-1,
0,
);
b.pl.impl.check_function("mmap", kernel_heap_base);
r.pl.impl.check_function("mmap", kernel_heap_base);
pfndb_base = linux.mmap(
null,
b.gib(1),
r.gib(1),
.{ .READ = true, .WRITE = true },
.{ .ANONYMOUS = true, .TYPE = .PRIVATE },
-1,
0,
);
b.pl.impl.check_function("mmap", pfndb_base);
r.pl.impl.check_function("mmap", pfndb_base);
}
pub fn phys_to_virt(pa: usize) usize {
return b.pl.impl.global_state.phys_base + pa;
return r.pl.impl.global_state.phys_base + pa;
}
pub fn virt_to_phys(va: usize) usize {
return va - b.pl.impl.global_state.phys_base;
return va - r.pl.impl.global_state.phys_base;
}
pub const PMap = struct {
const Self = @This();
pub fn map_from(_: *Self, va: b.VAddr, size: usize, source: anytype) void {
pub fn map_from(_: *Self, va: r.VAddr, size: usize, source: anytype) void {
var remain = size;
while (remain > 0) {
@@ -58,7 +58,7 @@ pub const PMap = struct {
size,
posix_flags,
.{ .FIXED = true, .TYPE = .PRIVATE },
b.pl.impl.global_state.phys_memory_memfd,
r.pl.impl.global_state.phys_memory_memfd,
@bitCast(item.pa),
);
+1 -1
View File
@@ -57,7 +57,7 @@
//! complexity.
const std = @import("std");
const rtl = @import("rtl");
const mm = @import("base").mm;
const mm = @import("root").mm;
const mi = mm.private;
pub const Policy = enum {
+4 -4
View File
@@ -2,11 +2,11 @@
//! As described in Bonwick's "The Slab Allocator: An Object-Caching Kernel Memory Allocator"
//! and "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources".
const rtl = @import("rtl");
const b = @import("base");
const r = @import("root");
const config = @import("config");
const std = @import("std");
const ke = b.ke;
const mm = b.mm;
const ke = r.ke;
const mm = r.mm;
const mi = mm.private;
// TODO: when SMP is setup, do proper magazine initialization.
@@ -714,7 +714,7 @@ pub fn TypedZone(comptime T: type) type {
};
}
pub fn early_init() linksection(b.init) void {
pub fn early_init() linksection(r.init) void {
bufctl_zone.init("bufctl", .{});
slab_zone.init("slab", .{});
+1 -1
View File
@@ -1,4 +1,4 @@
const b = @import("base");
const b = @import("root");
const std = @import("std");
const acpi = b.pl.acpi;
const mm = b.mm;
+1 -1
View File
@@ -1,5 +1,5 @@
const std = @import("std");
const b = @import("base");
const b = @import("root");
const acpi = b.pl.acpi;
pub var madt_ptr: ?*acpi.Madt = null;
+1 -1
View File
@@ -1,4 +1,4 @@
const b = @import("base");
const b = @import("root");
const mm = b.mm;
const pl = b.pl;
const arch = @import("arch");
+1 -1
View File
@@ -1,4 +1,4 @@
const b = @import("base");
const b = @import("root");
const std = @import("std");
const acpi = b.pl.acpi;
const ke = b.ke;
+7 -7
View File
@@ -1,9 +1,9 @@
const std = @import("std");
const amd64 = @import("arch");
const b = @import("base");
const acpi = b.pl.acpi;
const mm = b.mm;
const ke = b.ke;
const r = @import("root");
const acpi = r.pl.acpi;
const mm = r.mm;
const ke = r.ke;
const MadtLapic = extern struct {
header: acpi.MadtEntryHeader,
@@ -79,12 +79,12 @@ const log = std.log.scoped(.apic);
fn xapic_write(register: ApicRegisters, value: u32) void {
const lapic_base = xapic_address orelse return;
b.mmio_write(u32, lapic_base + @intFromEnum(register), value);
r.mmio_write(u32, lapic_base + @intFromEnum(register), value);
}
fn xapic_read(register: ApicRegisters) u32 {
const lapic_base = xapic_address orelse return 0;
return b.mmio_read(u32, lapic_base + @intFromEnum(register));
return r.mmio_read(u32, lapic_base + @intFromEnum(register));
}
fn x2apic_write(register: ApicRegisters, value: u64) void {
@@ -154,7 +154,7 @@ pub fn send_ipi(apic_id: u32, vector: u8, delivery_mode: u8) void {
}
}
pub fn init() linksection(b.init) void {
pub fn init() linksection(r.init) void {
var iter = acpi.madt.iterator();
const cur_cpu_id = get_id();
+7 -7
View File
@@ -1,13 +1,13 @@
const b = @import("base");
const pl = b.pl;
const amd64 = b.arch;
const r = @import("root");
const pl = r.pl;
const amd64 = r.arch;
const std = @import("std");
const tsc = @import("tsc.zig");
const pvclock = @import("pvclock.zig");
const apic = @import("apic.zig");
const smp = @import("smp.zig");
const ki = b.ke.private;
const ki = r.ke.private;
pub const name = "PC";
@@ -15,7 +15,7 @@ const com1 = 0x3F8;
const log = std.log.scoped(.@"amd64/pc");
fn com1_init() linksection(b.init) void {
fn com1_init() linksection(r.init) void {
amd64.outb(com1 + 0x3, 0x80);
amd64.outb(com1 + 0x0, 0x0c);
amd64.outb(com1 + 0x1, 0x00);
@@ -32,7 +32,7 @@ fn com1_write(c: u8) void {
amd64.outb(com1, c);
}
pub fn early_init(boot_info: *pl.BootInfo) linksection(b.init) void {
pub fn early_init(boot_info: *pl.BootInfo) linksection(r.init) void {
com1_init();
const f = amd64.cpu_features;
@@ -79,7 +79,7 @@ pub fn init_ap() void {
apic.init_local();
}
pub fn late_init(boot_info: *pl.BootInfo) linksection(b.init) void {
pub fn late_init(boot_info: *pl.BootInfo) linksection(r.init) void {
pl.acpi.init(boot_info);
if (amd64.hypervisor.info) |hv| {
+4 -4
View File
@@ -1,9 +1,9 @@
const amd64 = @import("arch");
const kvm = amd64.hypervisor.kvm;
const b = @import("base");
const r = @import("root");
const std = @import("std");
const mm = b.mm;
const ke = b.ke;
const mm = r.mm;
const ke = r.ke;
var pvclock_src: kvm.PVClockSource = undefined;
var pvclock_tc: ke.TimeCounter = .{
@@ -16,7 +16,7 @@ var pvclock_tc: ke.TimeCounter = .{
.n = 0,
};
pub fn init() linksection(b.init) void {
pub fn init() linksection(r.init) void {
const page = mm.phys.alloc();
pvclock_src = kvm.PVClockSource.init(page, mm.p2v(page));
ke.time.register_source(&pvclock_tc);
+5 -5
View File
@@ -1,10 +1,10 @@
const apic = @import("apic.zig");
const b = @import("base");
const r = @import("root");
const std = @import("std");
const rtl = @import("rtl");
const config = @import("config");
const mm = b.mm;
const ke = b.ke;
const mm = r.mm;
const ke = r.ke;
const ki = ke.private;
const amd64 = @import("arch");
@@ -36,7 +36,7 @@ fn ap_entry(cpu_id: u32) callconv(.c) noreturn {
ki.impl.init.ap_entry(cpu_id);
}
pub fn init() linksection(b.init) void {
pub fn init() linksection(r.init) void {
const trampoline_start = @intFromPtr(&AP_TRAMPOLINE_START);
const trampoline_size = @intFromPtr(&AP_TRAMPOLINE_END) - trampoline_start;
@@ -91,7 +91,7 @@ pub fn init() linksection(b.init) void {
ki.impl.cpu_offsets[cpu_id] = @intFromPtr(cpu_data.ptr) -% @intFromPtr(&__percpu_start);
const stack_top = @intFromPtr(mm.heap.alloc(b.kib(16)) catch @panic("Failed to allocate AP stack")) + b.kib(16);
const stack_top = @intFromPtr(mm.heap.alloc(r.kib(16)) catch @panic("Failed to allocate AP stack")) + r.kib(16);
start_stack.remote(@intCast(cpu_id)).* = stack_top & ~@as(usize, 15);
+3 -3
View File
@@ -1,8 +1,8 @@
// Not sure if this should be in platform/pc or in kern/
const std = @import("std");
const amd64 = @import("arch");
const b = @import("base");
const ke = b.ke;
const r = @import("root");
const ke = r.ke;
const log = std.log.scoped(.@"amd64/tsc");
@@ -16,7 +16,7 @@ var tsc_timer: ke.TimeCounter = .{
.n = 0,
};
pub fn init() linksection(b.init) void {
pub fn init() linksection(r.init) void {
const features = amd64.cpu_features;
if (!features.invariant_tsc) {
+9 -9
View File
@@ -1,7 +1,7 @@
const b = @import("base");
const r = @import("root");
pub const std_options = b.std_options;
pub const panic = b.panic;
pub const std_options = r.std_options;
pub const panic = r.panic;
const c = @cImport({
@cInclude("SDL.h");
@@ -10,10 +10,10 @@ const c = @cImport({
});
const std = @import("std");
const ke = b.ke;
const pl = b.pl;
const ke = r.ke;
const pl = r.pl;
const default_mem_size = b.gib(1);
const default_mem_size = r.gib(1);
fn parse_mem_size(str: []const u8) !usize {
if (str.len == 0) return default_mem_size;
@@ -21,9 +21,9 @@ fn parse_mem_size(str: []const u8) !usize {
const last = str[str.len - 1];
const mul: usize = switch (std.ascii.toLower(last)) {
'k' => b.kib(1),
'm' => b.mib(1),
'g' => b.gib(1),
'k' => r.kib(1),
'm' => r.mib(1),
'g' => r.gib(1),
else => 1,
};
+17 -15
View File
@@ -1,11 +1,13 @@
const std = @import("std");
const linux = std.os.linux;
const b = @import("base");
const pl = b.pl;
const ke = b.ke;
const ki = b.ke.private;
const r = @import("root");
const pl = r.pl;
const ke = r.ke;
const ki = r.ke.private;
const posix = std.posix;
pub const entry = @import("entry.zig");
const timer = @import("timer.zig");
pub const c = @cImport({
@@ -61,7 +63,7 @@ pub fn check_function(fn_name: []const u8, ret: usize) void {
pub var cpu_offsets: []usize = undefined;
pub fn early_init(_: *pl.BootInfo) linksection(b.init) void {
pub fn early_init(_: *pl.BootInfo) linksection(r.init) void {
percpu.local().pthread = c.pthread_self();
my_cpu_id = 0;
@@ -82,16 +84,16 @@ pub fn early_init(_: *pl.BootInfo) linksection(b.init) void {
global_state.phys_memory_memfd = memfd;
global_state.phys_base = phys_base;
const pfndb_base = linux.mmap(null, b.tib(1), .{}, .{ .ANONYMOUS = true, .TYPE = .PRIVATE }, -1, 0);
const pfndb_base = linux.mmap(null, r.tib(1), .{}, .{ .ANONYMOUS = true, .TYPE = .PRIVATE }, -1, 0);
check_function("mmap", pfndb_base);
const heap_base = linux.mmap(null, b.gib(8), .{}, .{ .ANONYMOUS = true, .TYPE = .PRIVATE }, -1, 0);
const heap_base = linux.mmap(null, r.gib(8), .{}, .{ .ANONYMOUS = true, .TYPE = .PRIVATE }, -1, 0);
check_function("mmap", heap_base);
b.kernel_pfndb_base = pfndb_base;
b.kernel_heap_base = heap_base;
r.kernel_pfndb_base = pfndb_base;
r.kernel_heap_base = heap_base;
std.log.info("um: kernel heap lives at {X}", .{heap_base});
}
@@ -102,10 +104,10 @@ fn sigusr1_handler(_: posix.SIG, _: *const posix.siginfo_t, _: ?*anyopaque) call
}
}
fn make_thread(entry: *const fn (?*anyopaque) void, td: *ke.Thread) void {
fn make_thread(entrypoint: *const fn (?*anyopaque) void, td: *ke.Thread) void {
const stack = std.heap.page_allocator.alloc(u8, 16384) catch @panic("wtf");
td.init(@intFromPtr(stack.ptr), 16384, entry, null);
td.init(@intFromPtr(stack.ptr), 16384, entrypoint, null);
td.priority = 0;
td.priority_class = .Idle;
@@ -127,14 +129,14 @@ fn other_cpu_entry(_: ?*anyopaque) callconv(.c) ?*anyopaque {
return null;
}
var cpu: ke.Cpu linksection(b.percpu) = undefined;
var cpu: ke.Cpu linksection(r.percpu) = undefined;
fn idle(_: ?*anyopaque) void {}
extern var __percpu_start: u8;
extern var __percpu_end: u8;
pub fn late_init(_: *pl.BootInfo) linksection(b.init) void {
pub fn late_init(_: *pl.BootInfo) linksection(r.init) void {
timer.init();
timer.init_cpu();
@@ -180,14 +182,14 @@ pub fn late_init(_: *pl.BootInfo) linksection(b.init) void {
cpuset.__bits[i / 64] |= @as(c_ulong, 1) << @intCast(i % 64);
_ = c.pthread_attr_setaffinity_np(&attr, @sizeOf(c.cpu_set_t), &cpuset);
const r = c.pthread_create(
const ret = c.pthread_create(
&other_threads[i - 1],
&attr,
other_cpu_entry,
null,
);
if (r != 0) {
if (ret != 0) {
@panic("pthread_create() failed");
}
}
+4 -4
View File
@@ -1,9 +1,9 @@
const b = @import("base");
const r = @import("root");
const std = @import("std");
const posix = std.posix;
const linux = std.os.linux;
const c = b.pl.impl.c;
const ke = b.ke;
const c = r.pl.impl.c;
const ke = r.ke;
const ki = ke.private;
threadlocal var timerid: c.timer_t = undefined;
@@ -20,7 +20,7 @@ fn timer_handler(_: posix.SIG, _: *const posix.siginfo_t, _: ?*anyopaque) callco
}
}
pub fn arm_timer(ns: b.Nanoseconds) void {
pub fn arm_timer(ns: r.Nanoseconds) void {
const value: c.struct_itimerspec = .{ .it_value = .{
.tv_sec = @intCast(ns / std.time.ns_per_s),
.tv_nsec = @intCast(ns % std.time.ns_per_s),
+9
View File
@@ -7,6 +7,8 @@ pub const ksyms = @import("ksyms");
pub const arch = @import("arch");
pub const mm = @import("mm/root.zig");
const config = @import("config");
pub const init = ".text.init";
pub const percpu_init = ".percpu_init_array";
pub const percpu = ".data.percpu";
@@ -52,3 +54,10 @@ pub const std_options = std.Options{
};
pub const panic = std.debug.FullPanic(ke.panic);
pub const main = if (@hasDecl(config, "CONFIG_ARCH_UM")) pl.impl.entry.main else undefined;
comptime {
// Ensure the entry point is linked in.
_ = @import("boot/limine/entry.zig");
}