Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4e3451b
[kernel][lib][arch] Refactor arch as real dependency
bherrera May 27, 2025
27ed0b2
[kernel][assert] Fix zig name convention
bherrera May 27, 2025
963532c
[ulib][affine] Initial version
bherrera May 28, 2025
9355bfc
[kernel][x86] Set TLS stackguard
bherrera May 28, 2025
562f150
[kernel][x86] Track early boot timestamps
bherrera May 28, 2025
5beb427
[system][public] Initial version
bherrera May 29, 2025
30c7231
[ulib][hwreg] Initial version
bherrera May 29, 2025
5607bfd
[ulib][mock_function] Initial version
bherrera Jun 3, 2025
6a8b15b
[ulib][hwreg] Add visitor pattern and Reserved-zero
bherrera May 30, 2025
d5d52da
[ulib][mmio-ptr] Initial version
bherrera Jun 9, 2025
cfa1365
[ulib][hwreg] Add mmio support
bherrera Jun 4, 2025
5ab6bfd
[ulib][hwreg] Fix unaligned mmio access
bherrera Jun 5, 2025
c2fe696
[ulib][hwreg] Refactor bitfields
bherrera Jun 4, 2025
53fbffe
[ulib][hwreg] Export internal impl
bherrera Jun 5, 2025
a75ec69
[ulib][hwreg] Small refactor
bherrera Jun 6, 2025
1f000d7
[sdk][lib][zbi-format] Initial version
bherrera Jun 11, 2025
e184b82
[ulib][uart] Initial version
bherrera Jun 9, 2025
492efee
[ulib][hwreg] Fix const correctness
bherrera Jun 9, 2025
635d90c
[ulib][hwreg] Fix interrupt callback
bherrera Jun 10, 2025
18735d5
[ulib][uart] Add pl011 impl
bherrera Jun 10, 2025
3d5418d
[ulib][uart] Refactor interrupt
bherrera Jun 11, 2025
b97abd6
[ulib][uart] Remove print
bherrera Jun 11, 2025
9501a44
[ulib][uart] Refactor init signature|usage
bherrera Jun 11, 2025
6e875dc
[git] Fix zig-cache exclusion
bherrera Jun 11, 2025
507f460
[ulib][uart] Add all::Config type
bherrera Jun 12, 2025
88e9fb1
[ulib][uart] Refactor ConfigVariant to be generated in comptime
bherrera Jun 12, 2025
6ab489c
[ulib][uart] Fix all::KernelDriver impl
bherrera Jun 13, 2025
41e3dcc
[ulib][mock_function] Abort in case expect fail in verifyAndClear
bherrera Jun 13, 2025
976abae
[kernel][x86] Fix import order/pattern
bherrera Jun 16, 2025
d1661fe
[kernel] Fix import order/pattern
bherrera Jun 16, 2025
4f08d74
[ulib][hwreg] Fix import order/pattern
bherrera Jun 16, 2025
58ad180
[ulib][uart] Fix import order/pattern
bherrera Jun 16, 2025
437b330
[ulib][hwlib] Rename mock.zig to Mock.zig
bherrera Jun 18, 2025
ccb5456
[ulib][hwreg] Fix dependencies paths
bherrera Jun 18, 2025
d47575a
[ulib][hwreg] Fix FieldPrinter printer
bherrera Jun 18, 2025
b7aa248
[ulib][hwreg] Minor tweaks
bherrera Jun 18, 2025
a79b05c
[ulib][hwreg] Add pio support
bherrera Jun 18, 2025
e861bdc
[ulib][uart] Fix dependencies paths
bherrera Jun 18, 2025
3ee73bd
[ulib][uart] Refactor to use IoProviderFactory
bherrera Jun 18, 2025
2c0a211
[system][public] Refactor to avoid cyclic dependency
bherrera Jun 18, 2025
55915da
[kernel] Rename threadInitEarly to initEarly
bherrera Jun 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ tmp/
!/third_party/rust_crates/vendor/**/tmp/
json_generator_tests_*.txt
tables_generator_tests_*.txt
zig-cache/
zig-out/

### fx configuration and cache files.
# NOTE: For any new files, please write files under the top-level .fx/ directory
Expand Down Expand Up @@ -92,7 +94,6 @@ tables_generator_tests_*.txt
/out/
/prebuilt/
/test_data/
/zig-out/

# Third party repos.
/third_party/*
Expand Down
25 changes: 25 additions & 0 deletions sdk/lib/zbi-format/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! Copyright 2025 The Drift Authors. All rights reserved.
//! Use of this source code is governed by a BSD-style license that can be
//! found in the LICENSE file.

const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const mod = b.addModule("zbi_format", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});

const test_step = b.step("test", "Run unit tests");
const unit_tests = b.addTest(.{
.root_module = mod,
.target = b.graph.host,
});

const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);
}
6 changes: 6 additions & 0 deletions sdk/lib/zbi-format/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.{
.name = .zbi_format,
.fingerprint = 0x53525b7b9572617,
.version = "0.0.1",
.paths = .{""},
}
118 changes: 118 additions & 0 deletions sdk/lib/zbi-format/src/driver_config.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! Copyright 2025 The Drift Authors. All rights reserved.
//! Use of this source code is governed by a BSD-style license that can be
//! found in the LICENSE file.

const std = @import("std");

/// ZBI_TYPE_KERNEL_DRIVER item types (for zbi_header_t.extra)
pub const KernelDriverType = u32;

// 'PSCI'
pub const ZBI_KERNEL_DRIVER_ARM_PSCI: KernelDriverType = 0x49435350;

// 'GIC2'
pub const ZBI_KERNEL_DRIVER_ARM_GIC_V2: KernelDriverType = 0x32434947;

// 'GIC3'
pub const ZBI_KERNEL_DRIVER_ARM_GIC_V3: KernelDriverType = 0x33434947;

// 'ATIM'
pub const ZBI_KERNEL_DRIVER_ARM_GENERIC_TIMER: KernelDriverType = 0x4d495441;

// 'ATMM'
pub const ZBI_KERNEL_DRIVER_ARM_GENERIC_TIMER_MMIO: KernelDriverType = 0x4d4d5441;

// 'PL0U'
pub const ZBI_KERNEL_DRIVER_PL011_UART: KernelDriverType = 0x55304c50;

// 'AMLU'
pub const ZBI_KERNEL_DRIVER_AMLOGIC_UART: KernelDriverType = 0x554c4d41;

// 'AMLH'
pub const ZBI_KERNEL_DRIVER_AMLOGIC_HDCP: KernelDriverType = 0x484c4d41;

// 'DW8U'
pub const ZBI_KERNEL_DRIVER_DW8250_UART: KernelDriverType = 0x44573855;

// 'RMLH' (typoed, originally intended to by 'AMLR')
pub const ZBI_KERNEL_DRIVER_AMLOGIC_RNG_V1: KernelDriverType = 0x484c4d52;

// 'AMLR'
pub const ZBI_KERNEL_DRIVER_AMLOGIC_RNG_V2: KernelDriverType = 0x524c4d41;

// 'WD32'
pub const ZBI_KERNEL_DRIVER_GENERIC32_WATCHDOG: KernelDriverType = 0x32334457;

// 'GENI'
pub const ZBI_KERNEL_DRIVER_GENI_UART: KernelDriverType = 0x494e4547;

// '8250'
pub const ZBI_KERNEL_DRIVER_I8250_PIO_UART: KernelDriverType = 0x30353238;

// '825M'
pub const ZBI_KERNEL_DRIVER_I8250_MMIO32_UART: KernelDriverType = 0x4d353238;

// '825B'
pub const ZBI_KERNEL_DRIVER_I8250_MMIO8_UART: KernelDriverType = 0x42353238;

// 'MMTP'
pub const ZBI_KERNEL_DRIVER_MOTMOT_POWER: KernelDriverType = 0x4d4d5450;

// '370P'
pub const ZBI_KERNEL_DRIVER_AS370_POWER: KernelDriverType = 0x50303733;

// 'MNFP'
pub const ZBI_KERNEL_DRIVER_MOONFLOWER_POWER: KernelDriverType = 0x4d4e4650;

// 'IMXU'
pub const ZBI_KERNEL_DRIVER_IMX_UART: KernelDriverType = 0x55584d49;

// 'PLIC'
pub const ZBI_KERNEL_DRIVER_RISCV_PLIC: KernelDriverType = 0x43494c50;

// 'RTIM'
pub const ZBI_KERNEL_DRIVER_RISCV_GENERIC_TIMER: KernelDriverType = 0x4d495452;

// 'PXAU'
pub const ZBI_KERNEL_DRIVER_PXA_UART: KernelDriverType = 0x50584155;

// 'EXYU'
pub const ZBI_KERNEL_DRIVER_EXYNOS_USI_UART: KernelDriverType = 0x45585955;

/// Kernel driver struct that can be used for simple drivers.
/// Used by ZBI_KERNEL_DRIVER_PL011_UART, ZBI_KERNEL_DRIVER_AMLOGIC_UART, and
/// ZBI_KERNEL_DRIVER_GENI_UART, ZBI_KERNEL_DRIVER_I8250_MMIO_UART.
pub const SimpleDriverConfig = packed struct {
mmio_phys: u64,
irq: u32,
flags: u32,
};

/// IRQ flags for kernel drivers
pub const IrqFlags = packed struct(u32) {
/// When no flag is set, implies no information was obtained, and the
/// kernel will apply default configuration as it sees fit.
edge_triggered: bool = false,
level_triggered: bool = false,
polarity_low: bool = false,
polarity_high: bool = false,

_padding: u28 = 0,

comptime {
std.debug.assert(@sizeOf(@This()) == @sizeOf(u32));
std.debug.assert(@bitSizeOf(@This()) == @bitSizeOf(u32));
}

pub fn toInt(self: @This()) u32 {
return @bitCast(self);
}
};

/// Simple PIO driver configuration
/// Used by ZBI_KERNEL_DRIVER_I8250_PIO_UART.
pub const SimplePioConfig = packed struct {
base: u16,
reserved: u16,
irq: u32,
};
9 changes: 9 additions & 0 deletions sdk/lib/zbi-format/src/root.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Copyright 2025 The Drift Authors. All rights reserved.
//! Use of this source code is governed by a BSD-style license that can be
//! found in the LICENSE file.

pub const driver_config = @import("driver_config.zig");

comptime {
_ = driver_config;
}
4 changes: 2 additions & 2 deletions slipstream/kernel/arch/x86/.build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
//! found in the LICENSE file.

const std = @import("std");
const cfg = @import("drift/build_config");

const Target = std.Target;
const Module = std.Build.Module;

const cfg = @import("drift/build_config");
const BuildConfig = cfg.BuildConfig;

// These set the ABI contract between C++ and assembly code.
Expand Down
3 changes: 2 additions & 1 deletion slipstream/kernel/arch/x86/IdleStates.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//! Use of this source code is governed by a BSD-style license that can be
//! found in the LICENSE file.

const std = @import("std");
const IdleState = @This();

const std = @import("std");

pub const X86_MAX_CSTATES = 12;

// A human-readable name for the state
Expand Down
1 change: 1 addition & 0 deletions slipstream/kernel/arch/x86/faults.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! found in the LICENSE file.

const std = @import("std");

const regs = @import("regs.zig");
const x86 = @import("x86.zig");

Expand Down
13 changes: 7 additions & 6 deletions slipstream/kernel/arch/x86/mp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! found in the LICENSE file.

const std = @import("std");

const cpu = @import("../../kernel/cpu.zig");
const Thread = @import("../../kernel/Thread.zig");
const PerCpu = @import("../../kernel/PerCpu.zig");
Expand Down Expand Up @@ -181,12 +182,12 @@ inline fn percpuFor(cpu_num: CpuNum) *X86PerCpu {
/// Called from assembly.
export fn initPercpu(cpu_num: CpuNum) callconv(.C) void {
const percpu = percpuFor(cpu_num);
assert.debug_assert(@src(), percpu.cpu_num == cpu_num);
assert.debug_assert(@src(), percpu.direct == percpu);
assert.debugAssert(@src(), percpu.cpu_num == cpu_num, "percpu.cpu_num == cpu_num");
assert.debugAssert(@src(), percpu.direct == percpu, "percpu.direct == percpu");

// Assembly code has already set up %gs.base so that this function's own code can use it
// implicitly for stack-protector or safe-stack.
assert.debug_assert(@src(), x86.readMsr(registers.X86_MSR_IA32_GS_BASE) == @intFromPtr(percpu));
assert.debugAssert(@src(), x86.readMsr(registers.X86_MSR_IA32_GS_BASE) == @intFromPtr(percpu), "x86.readMsr(registers.X86_MSR_IA32_GS_BASE) == @intFromPtr(percpu)");

// Set the KERNEL_GS_BASE MSR to 0
// When we enter user space, this will be populated via a swapgs
Expand Down Expand Up @@ -273,9 +274,9 @@ pub fn forceHaltAllButLocalAndBsp() void {

pub fn setupPercpu(cpu_num: CpuNum, percpu: *PerCpu) void {
const arch_percpu = percpuFor(cpu_num);
//assert.debug_assert(@src(), arch_percpu != null);
assert.debug_assert(@src(), arch_percpu.high_level_percpu == null or
arch_percpu.high_level_percpu == percpu);
//assert.debugAssert(@src(), arch_percpu != null, "arch_percpu != null");
assert.debugAssert(@src(), arch_percpu.high_level_percpu == null or
arch_percpu.high_level_percpu == percpu, "arch_percpu.high_level_percpu == null or arch_percpu.high_level_percpu == percpu");
arch_percpu.high_level_percpu = percpu;
}

Expand Down
3 changes: 2 additions & 1 deletion slipstream/kernel/arch/x86/spin_lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
//! found in the LICENSE file.

const std = @import("std");
const arch = @import("lib/arch").intrin;

const assert = @import("../../kernel/assert.zig");
const mp = @import("mp.zig");
const arch = @import("../../lib/arch/x86/intrin.zig");
const ArchSpinLock = @import("../../kernel/arch/SpinLock.zig");

inline fn archSpinLockCore(lock: *ArchSpinLock, val: u32) void {
Expand Down
24 changes: 23 additions & 1 deletion slipstream/kernel/arch/x86/start.S
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <arch/x86/descriptor.h>
#include <arch/x86/mmu.h>
#include <arch/x86/registers.h>
#include <slipstream/tls.h>
#include "multiboot.h"

#define MSR_EFER 0xc0000080
Expand All @@ -21,6 +22,14 @@
#define PHYS_ADDR_DELTA (KERNEL_BASE + KERNEL_LOAD_OFFSET - PHYS_LOAD_ADDRESS)
#define PHYS(x) ((x) - PHYS_ADDR_DELTA)

// Clobbers %rax, %rdx.
.macro sample_ticks out
rdtsc
shl $32, %rdx
or %rdx, %rax
mov %rax, \out
.endm

.section ".text.boot"
.code32
.global _start
Expand Down Expand Up @@ -197,9 +206,14 @@ farjump64:
jmp *%rax

highaddr:
// As early as possible collect the time stamp.
sample_ticks %r15

/* load the high kernel stack */
mov $(_kstack + 4096), %rsp

mov %r15, kernel_entry_ticks(%rip)

/* reload the gdtr */
lgdt _gdtr

Expand Down Expand Up @@ -232,7 +246,15 @@ highaddr:
// call would make it eligible for stack-guard checking itself. But
// %gs is not set up yet in the prologue of the function, so it would
// crash if it tried to use the stack-guard.
call choose_stack_guard
call chooseStackGuard

// Move it into place.
mov %rax, %gs:SX_TLS_STACK_GUARD_OFFSET
// Don't leak that value to other code.
xor %eax, %eax

// Collect the time stamp of entering "normal" Zig code in virtual space.
sample_ticks kernel_virtual_entry_ticks(%rip)

/* call the main module */
call lk_main
Expand Down
1 change: 1 addition & 0 deletions slipstream/kernel/arch/x86/x86.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! found in the LICENSE file.

const std = @import("std");

const regs = @import("regs.zig");

pub const registers = @cImport({
Expand Down
13 changes: 7 additions & 6 deletions slipstream/kernel/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
//! found in the LICENSE file.

const std = @import("std");
const cfg = @import("drift/build_config");

const Build = std.Build;
const Target = std.Target;
const Module = std.Build.Module;

const cfg = @import("drift/build_config");

pub fn build(b: *Build) !void {
const build_config: *cfg.BuildConfig = try cfg.BuildConfig.init(b);

const options = b.addOptions();
options.addOption(u32, "SMP_MAX_CPUS", 1);
options.addOption(bool, "DEBUG_ASSERT_IMPLEMENTED", true);

const kernel = b.createModule(.{
const kernel = b.addModule("kernel", .{
.root_source_file = b.path("root.zig"),
.optimize = .Debug,
});
Expand All @@ -41,9 +41,10 @@ pub fn build(b: *Build) !void {
standalone(build_config, kernel);

const deps = [_]struct { name: []const u8, dep_name: []const u8, module_name: []const u8 }{
.{ .name = "dbl", .dep_name = "dbl", .module_name = "dbl" },
.{ .name = "lazy_init", .dep_name = "lazy_init", .module_name = "lazy_init" },
.{ .name = "lockdep", .dep_name = "lockdep", .module_name = "lockdep" },
.{ .name = "lib/arch", .dep_name = "lib/arch", .module_name = "arch" },
.{ .name = "ulib/dbl", .dep_name = "ulib/dbl", .module_name = "dbl" },
.{ .name = "ulib/lazy_init", .dep_name = "ulib/lazy_init", .module_name = "lazy_init" },
.{ .name = "ulib/lockdep", .dep_name = "ulib/lockdep", .module_name = "lockdep" },
};

for (deps) |dep| {
Expand Down
15 changes: 9 additions & 6 deletions slipstream/kernel/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
.@"drift/build_config" = .{
.path = "../../build",
},
.lazy_init = .{
.path = "../system/ulib/lazy_init",
.@"lib/arch" = .{
.path = "../../slipstream/kernel/lib/arch",
},
.dbl = .{
.path = "../system/ulib/dbl",
.@"ulib/dbl" = .{
.path = "../../slipstream/system/ulib/dbl",
},
.lockdep = .{
.path = "../system/ulib/lockdep",
.@"ulib/lazy_init" = .{
.path = "../../slipstream/system/ulib/lazy_init",
},
.@"ulib/lockdep" = .{
.path = "../../slipstream/system/ulib/lockdep",
},
},
}
Loading