Skip to content

Commit

Permalink
dainkrnl: builds all 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Jul 15, 2024
1 parent fc78497 commit 193250c
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 112 deletions.
49 changes: 26 additions & 23 deletions dainkrnl/build.zig
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
const Builder = std.build.Builder;

const dbuild = @import("src/common/dbuild.zig");

pub fn build(b: *Builder) !void {
pub fn build(b: *std.Build) !void {
const board = try dbuild.getBoard(b);
var target = dbuild.crossTargetFor(board);
target.os_tag = .freestanding;
target.abi = .none;
var targetQuery = dbuild.queryFor(board);
targetQuery.os_tag = .freestanding;
targetQuery.abi = .none;
const resolvedTarget = b.resolveTargetQuery(targetQuery);

const arch_tag = dbuild.getArch(board);
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable(.{
.name = b.fmt("dainkrnl.{s}", .{@tagName(board)}),
.root_source_file = .{ .path = "src/root.zig" },
.target = target,
.optimize = b.standardOptimizeOption(.{}),
.root_source_file = b.path("src/root.zig"),
.target = resolvedTarget,
.optimize = optimize,
});

if (dbuild.getArch(board) == .riscv64) {
exe.code_model = .medium;
exe.root_module.code_model = .medium;
}
exe.addAssemblyFile(b.fmt("src/{s}/exception.s", .{@tagName(arch_tag)}));
const dtb = b.addModule("dtb", .{
.source_file = .{ .path = "../dtb/src/dtb.zig" },
});
exe.addModule("dtb", dtb);
exe.setLinkerScriptPath(.{ .path = b.fmt("linker.{s}.ld", .{@tagName(arch_tag)}) });
exe.addAssemblyFile(b.path(b.fmt("src/{s}/exception.s", .{@tagName(arch_tag)})));
const dtb = b.dependency("dtb.zig", .{
.target = resolvedTarget,
.optimize = optimize,
}).module("dtb");
exe.root_module.addImport("dtb", dtb);

exe.setLinkerScriptPath(b.path(b.fmt("linker.{s}.ld", .{@tagName(arch_tag)})));
exe.setVerboseLink(true);

// Avoid using atomic stores/loads in suspend/resume code.
// Right now they fail with ESR 96000035 which suggests the PE doesn't think
// the stack is appropriate -- see B2.9.2 (page B2-137~139). It looks fine?
// Need to see what other settings might be causing the area to appear non-
// cacheable. It's weird, because we definitely do see caching activity ...
// I saw something online mentioning PSCI core init at some point? Surely not.
exe.single_threaded = true;
// Commented out because we don't use (= Zig doesn't have) suspend/resume code any more.
// // Avoid using atomic stores/loads in suspend/resume code.
// // Right now they fail with ESR 96000035 which suggests the PE doesn't think
// // the stack is appropriate -- see B2.9.2 (page B2-137~139). It looks fine?
// // Need to see what other settings might be causing the area to appear non-
// // cacheable. It's weird, because we definitely do see caching activity ...
// // I saw something online mentioning PSCI core init at some point? Surely not.
// exe.single_threaded = true;

try dbuild.addBuildOptions(b, exe, board);
b.installArtifact(exe);
Expand Down
17 changes: 17 additions & 0 deletions dainkrnl/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.{
.name = "dainkrnl",
.version = "0.0.0",
.dependencies = .{
.@"dtb.zig" = .{
.url = "https://github.com/kivikakk/dtb.zig/archive/e5f50cb3dfba304802c899d5a5fec3ad7b66443c.tar.gz",
.hash = "1220c4cf97bc6df21c6b1e21e242058828d2abbdc53f0c1e3b60600ebc18c1e24543",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"linker.arm64.ld",
"linker.riscv64.ld",
},
}
6 changes: 3 additions & 3 deletions dainkrnl/src/arm64/arch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_
if (error_return_trace) |ert| {
hw.entry_uart.carefully(.{"trying to print stack ... \r\n"});
var frame_index: usize = 0;
var frames_left: usize = std.math.min(ert.index, ert.instruction_addresses.len);
var frames_left: usize = @min(ert.index, ert.instruction_addresses.len);
while (frames_left != 0) : ({
frames_left -= 1;
frame_index = (frame_index + 1) % ert.instruction_addresses.len;
Expand All @@ -26,7 +26,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_
hw.entry_uart.carefully(.{ "ret_addr: ", ret_addr, "\r\n" });
hw.entry_uart.carefully(.{ "@returnAddress: ", @returnAddress(), "\r\n" });

hw.entry_uart.carefully(.{ "panic message ptr: ", @ptrToInt(msg.ptr), "\r\n<" });
hw.entry_uart.carefully(.{ "panic message ptr: ", @intFromPtr(msg.ptr), "\r\n<" });
hw.entry_uart.carefully(.{ hw.entry_uart.Escape.Runtime, msg, ">\r\n" });

if (fb.present()) {
Expand Down Expand Up @@ -95,8 +95,8 @@ pub fn sleep(ms: u64) void {
\\ mrs x1, cntpct_el0
\\ b 1b
\\2: nop
: [ms] "={x0}" (ms),
:
: [ms] "{x0}" (ms),
: "x1", "x2", "x3"
);
}
Expand Down
16 changes: 8 additions & 8 deletions dainkrnl/src/arm64/entry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub export fn daintree_mmu_start(entry_data: *dcommon.EntryData) noreturn {
var l2 = bump.alloc(p.PageTable);
var l3 = bump.alloc(p.PageTable);

const ttbr0_el1 = @ptrToInt(p.TTBR0_IDENTITY) | 1;
const ttbr1_el1 = @ptrToInt(p.K_DIRECTORY) | 1;
const ttbr0_el1 = @intFromPtr(p.TTBR0_IDENTITY) | 1;
const ttbr1_el1 = @intFromPtr(p.K_DIRECTORY) | 1;
hw.entry_uart.carefully(.{ "setting TTBR0_EL1: ", ttbr0_el1, "\r\n" });
hw.entry_uart.carefully(.{ "setting TTBR1_EL1: ", ttbr1_el1, "\r\n" });
arch.writeRegister(.TTBR0_EL1, ttbr0_el1);
Expand All @@ -72,8 +72,8 @@ pub export fn daintree_mmu_start(entry_data: *dcommon.EntryData) noreturn {
p.TTBR0_IDENTITY.map(r.page, r.address, .block, .kernel_promisc);
}

p.K_DIRECTORY.map(0, @ptrToInt(l2), .table, .non_leaf);
l2.map(0, @ptrToInt(l3), .table, .non_leaf);
p.K_DIRECTORY.map(0, @intFromPtr(l2), .table, .non_leaf);
l2.map(0, @intFromPtr(l3), .table, .non_leaf);

var end: u64 = (daintree_end - daintree_base) >> p.PAGING.page_bits;
entryAssert(end <= 512, "end got too big (1)");
Expand Down Expand Up @@ -122,7 +122,7 @@ pub export fn daintree_mmu_start(entry_data: *dcommon.EntryData) noreturn {
// address now points to the stack. make space for common.EntryData, align.
var entry_address = bump.next - @sizeOf(dcommon.EntryData);
entry_address &= ~@as(u64, 15);
var new_entry = @intToPtr(*dcommon.EntryData, entry_address);
var new_entry = @as(*dcommon.EntryData, @ptrFromInt(entry_address));
new_entry.* = .{
.memory_map = entry_data.memory_map,
.memory_map_size = entry_data.memory_map_size,
Expand All @@ -146,13 +146,13 @@ pub export fn daintree_mmu_start(entry_data: *dcommon.EntryData) noreturn {
// I hate that I'm doing this. Put the DTB in here.
{
i += 1;
new_entry.dtb_ptr = @intToPtr([*]const u8, p.PAGING.kernelPageAddress(i));
var dtb_target = @intToPtr([*]u8, bump.next)[0..entry_data.dtb_len];
new_entry.dtb_ptr = @as([*]const u8, @ptrFromInt(p.PAGING.kernelPageAddress(i)));
const dtb_target = @as([*]u8, @ptrFromInt(bump.next))[0..entry_data.dtb_len];

// How many pages?
const dtb_pages = (entry_data.dtb_len + p.PAGING.page_size - 1) / p.PAGING.page_size;

var new_end = end + 1 + dtb_pages; // Skip 1 page since UART is there
const new_end = end + 1 + dtb_pages; // Skip 1 page since UART is there
entryAssert(new_end <= 512, "end got too big (3)");

hw.entry_uart.carefully(.{ "MAP: DTB at ", p.PAGING.kernelPageAddress(i), "~\r\n" });
Expand Down
2 changes: 1 addition & 1 deletion dainkrnl/src/arm64/exception.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ inline fn handle(ctx: *ExceptionContext, comptime name: []const u8) noreturn {
}

fn dumpRegs(ctx: *ExceptionContext) void {
hw.entry_uart.carefully(.{ "ctx ptr:", @ptrToInt(ctx), "\r\n" });
hw.entry_uart.carefully(.{ "ctx ptr:", @intFromPtr(ctx), "\r\n" });
hw.entry_uart.carefully(.{ "x0 ", ctx.regs[0], " x1 ", ctx.regs[1], "\r\n" });
hw.entry_uart.carefully(.{ "x2 ", ctx.regs[2], " x3 ", ctx.regs[3], "\r\n" });
hw.entry_uart.carefully(.{ "x4 ", ctx.regs[4], " x5 ", ctx.regs[5], "\r\n" });
Expand Down
18 changes: 9 additions & 9 deletions dainkrnl/src/arm64/paging.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ pub const PageTable = extern struct {
// Recurse into subtables.
while (i < self.entries.len) : (i += 1) {
if ((self.entries[i] & 0x1) == 0x0) {
var new_phys = paging.bump.alloc(PageTable);
self.map(i, @ptrToInt(new_phys), .table, .non_leaf);
const new_phys = paging.bump.alloc(PageTable);
self.map(i, @intFromPtr(new_phys), .table, .non_leaf);
}

if ((self.entries[i] & 0x3) == 0x3) {
Expand Down Expand Up @@ -85,7 +85,7 @@ pub const PageTable = extern struct {
fn pageAt(self: *const PageTable, index: usize) *PageTable {
const entry = self.entries[index];
if ((entry & 0x3) != 0x3) @panic("pageAt on non-page");
return @intToPtr(*PageTable, entry & ArchPte.OA_MASK_Lx_TABLE);
return @as(*PageTable, @ptrFromInt(entry & ArchPte.OA_MASK_Lx_TABLE));
}
};

Expand Down Expand Up @@ -135,11 +135,11 @@ pub const ArchPte = struct {
pub const OA_MASK_Lx_TABLE: u64 = 0x0000ffff_fffff000;
pub inline fn toU64(pte: ArchPte) u64 {
return @as(u64, pte.valid) |
(@as(u64, @enumToInt(pte.type)) << 1) |
(@as(u64, @intFromEnum(pte.type)) << 1) |
(@as(u64, pte.attr_index) << 2) |
(@as(u64, pte.ns) << 5) |
(@as(u64, @enumToInt(pte.ap)) << 6) |
(@as(u64, @enumToInt(pte.sh)) << 8) |
(@as(u64, @intFromEnum(pte.ap)) << 6) |
(@as(u64, @intFromEnum(pte.sh)) << 8) |
(@as(u64, pte.af) << 10) |
(@as(u64, pte.pxn) << 53) |
(@as(u64, pte.uxn) << 54);
Expand Down Expand Up @@ -186,15 +186,15 @@ pub const TCR_EL1 = struct {
(@as(u64, tcr.irgn0) << 8) |
(@as(u64, tcr.orgn0) << 10) |
(@as(u64, tcr.sh0) << 12) |
(@as(u64, @enumToInt(tcr.tg0)) << 14) |
(@as(u64, @intFromEnum(tcr.tg0)) << 14) |
(@as(u64, tcr.t1sz) << 16) |
(@as(u64, tcr.a1) << 22) |
(@as(u64, tcr.epd1) << 23) |
(@as(u64, tcr.irgn1) << 24) |
(@as(u64, tcr.orgn1) << 26) |
(@as(u64, tcr.sh1) << 28) |
(@as(u64, @enumToInt(tcr.tg1)) << 30) |
(@as(u64, @enumToInt(tcr.ips)) << 32);
(@as(u64, @intFromEnum(tcr.tg1)) << 30) |
(@as(u64, @intFromEnum(tcr.ips)) << 32);
}

t0sz: u6, // TTBR0_EL1 addresses 2**(64-25)
Expand Down
16 changes: 8 additions & 8 deletions dainkrnl/src/console/fb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn init(in_fb: [*]u32, in_vert: u32, in_horiz: u32) void {
fb_vert = in_vert;
fb_horiz = in_horiz;

console_height = @truncate(CONSOLE_DIMENSION, fb_vert / font.FONT_HEIGHT);
console_width = @truncate(CONSOLE_DIMENSION, fb_horiz / font.FONT_WIDTH);
console_height = @as(CONSOLE_DIMENSION, @truncate(fb_vert / font.FONT_HEIGHT));
console_width = @as(CONSOLE_DIMENSION, @truncate(fb_horiz / font.FONT_WIDTH));
if (console_height * console_width > console_buf.len) {
@panic("can't fit console");
}
Expand All @@ -45,18 +45,18 @@ pub fn init(in_fb: [*]u32, in_vert: u32, in_horiz: u32) void {
}

fn mapFb(base: [*]u32, pixels: usize) [*]u32 {
var page_count = (pixels * 4 + paging.PAGING.page_size - 1) / paging.PAGING.page_size;
var virt = paging.mapPagesConsecutive(@ptrToInt(base), page_count, .peripheral) catch @panic("oom");
const page_count = (pixels * 4 + paging.PAGING.page_size - 1) / paging.PAGING.page_size;
const virt = paging.mapPagesConsecutive(@intFromPtr(base), page_count, .peripheral) catch @panic("oom");
hw.entry_uart.carefully(.{ "MAP: FB at ", virt, "~\r\n" });
return @intToPtr([*]u32, virt);
return @as([*]u32, @ptrFromInt(virt));
}

pub inline fn present() bool {
return fb != null;
}

pub fn panicMessage(msg: []const u8) void {
const msg_len: CONSOLE_DIMENSION = @truncate(CONSOLE_DIMENSION, "kernel panic: ".len + msg.len);
const msg_len: CONSOLE_DIMENSION = @as(CONSOLE_DIMENSION, @truncate("kernel panic: ".len + msg.len));
const left: CONSOLE_DIMENSION = console_width - msg_len - 2;

colour(0x4f);
Expand All @@ -83,7 +83,7 @@ fn drawEnergyStar(comptime allWhite: bool) void {
while (y < HEIGHT) : (y += 1) {
var x: u32 = 0;
while (x < WIDTH) : (x += 1) {
var s = DATA[(WIDTH * y + x) * 3 .. (WIDTH * y + x + 1) * 3];
const s = DATA[(WIDTH * y + x) * 3 .. (WIDTH * y + x + 1) * 3];
const c = (@as(u32, s[0]) << 16) | (@as(u32, s[1]) << 8) | @as(u32, s[2]);
if (allWhite) {
plot(left + x, y, if (c != 0) 0xffffff else 0);
Expand Down Expand Up @@ -180,7 +180,7 @@ fn refresh() void {
var col: CONSOLE_DIMENSION = 0;
while (col < console_width) : (col += 1) {
const pair = console_buf[row * console_width + col];
font.putChar(row, col, @truncate(u8, pair), @truncate(u8, pair >> 8));
font.putChar(row, col, @as(u8, @truncate(pair)), @as(u8, @truncate(pair >> 8)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dainkrnl/src/console/font.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn putChar(row: CONSOLE_DIMENSION, col: CONSOLE_DIMENSION, ch: u8, bgfg: u8)
const y_origin = @as(u32, row) * FONT_HEIGHT;
const x_origin = @as(u32, col) * FONT_WIDTH;

var char = CP437VGA[@as(usize, FONT_HEIGHT) * ch ..][0..FONT_HEIGHT];
const char = CP437VGA[@as(usize, FONT_HEIGHT) * ch ..][0..FONT_HEIGHT];

var y: u32 = 0;
while (y < FONT_HEIGHT) : (y += 1) {
Expand Down
2 changes: 1 addition & 1 deletion dainkrnl/src/hw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn init(dtb: []const u8) !void {

// XXX these things are copied everywhere lol
fn readU32(value: []const u8) u32 {
return std.mem.bigToNative(u32, @ptrCast(*const u32, @alignCast(@alignOf(u32), value.ptr)).*);
return std.mem.bigToNative(u32, @as(*const u32, @ptrCast(@alignCast(value.ptr))).*);
}
fn readU64(value: []const u8) u64 {
return (@as(u64, readU32(value[0..4])) << 32) | readU32(value[4..8]);
Expand Down
8 changes: 4 additions & 4 deletions dainkrnl/src/hw/arm,pl011.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const REGMASK_UARTFR_TXFF = 1 << 5;
pub fn write(base: u64, reg_shift: u4, data: []const u8) void {
_ = reg_shift;

const uartdr = @intToPtr(*volatile u8, base + REGOFF_UARTDR);
const uartfr = @intToPtr(*volatile u8, base + REGOFF_UARTFR);
const uartdr = @as(*volatile u8, @ptrFromInt(base + REGOFF_UARTDR));
const uartfr = @as(*volatile u8, @ptrFromInt(base + REGOFF_UARTFR));
for (data) |c| {
while (uartfr.* & REGMASK_UARTFR_TXFF == REGMASK_UARTFR_TXFF) {
// transmit FIFO full ...
Expand All @@ -24,8 +24,8 @@ pub fn write(base: u64, reg_shift: u4, data: []const u8) void {
pub fn readBlock(base: u64, reg_shift: u4, buf: []u8) usize {
_ = reg_shift;

const uartdr = @intToPtr(*volatile u8, base + REGOFF_UARTDR);
const uartfr = @intToPtr(*volatile u8, base + REGOFF_UARTFR);
const uartdr = @as(*volatile u8, @ptrFromInt(base + REGOFF_UARTDR));
const uartfr = @as(*volatile u8, @ptrFromInt(base + REGOFF_UARTFR));
while (uartfr.* & REGMASK_UARTFR_RXFE == REGMASK_UARTFR_RXFE) {
// receive FIFO empty ...
asm volatile ("nop");
Expand Down
Loading

0 comments on commit 193250c

Please sign in to comment.