I am following raspi3 barebones kernel guides and translating them to Zig to learn as I go through.
I have encountered an issue however where my kernel seems to crash when using std.fmt.format()
.
I’ve implemented Writer
like so:
const TerminalWriter = struct {
const Self = @This();
pub const Error = error{};
pub fn write(_: Self, bytes: []const u8) !usize {
writeString(bytes);
return bytes.len;
}
pub fn writeByte(self: Self, byte: u8) !void {
_ = try self.write(&.{byte});
}
pub fn writeBytesNTimes(self: Self, bytes: []const u8, n: usize) !void {
for (0..n) |_| {
_ = try self.write(bytes);
}
}
pub fn writeAll(_: Self, bytes: []const u8) !void {
writeString(bytes);
}
};
which is called here:
pub fn print(comptime format: []const u8, args: anytype) void {
std.fmt.format(TerminalWriter{}, format, args) catch unreachable;
}
It results in the following output:
Synchronous: Unknown:
ESR_EL1 000000001FE00000 ELR_EL1 00000000000845E0
SPSR_EL1 00000000600003C4 FAR_EL1 0000000000000000
Interestingly, if I change the writer functions to all call writeString()
directly, it works, but fails again as soon as I add any args.
Any ideas - am I missing anything? I’ve looked through the code for format()
and can’t see anything obvious…
For reference, my target is:
const target = b.standardTargetOptions(.{ .default_target = .{
.abi = .eabihf,
.os_tag = .freestanding,
.cpu_arch = .aarch64,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_a53 },
} });