const console = @import("console"); const flanterm = @import("flanterm"); const common = @import("../root.zig"); const mm = common.mm; const std = @import("std"); const arch = @import("arch"); const spinlock = @import("spinlock"); const limine = @import("limine"); // Types pub const HardwareDescription = union(enum) { /// Physical address of ACPI RSDP acpi_rsdp: usize, /// Virtual pointer to DTB dtb: *anyopaque, none, }; pub const InitState = struct { bootmem: mm.bootmem.BootPmm = .{}, console: ?flanterm.Context = null, framebuffer: ?common.aux.Framebuffer = null, hardware_description: HardwareDescription = .none, root_task: []align(4096) u8 = undefined, hhdm_slide: usize = 0, }; pub const Framebuffer = struct { const Self = @This(); address: [*]u32, width: u64, height: u64, pitch: u64, bypp: u16, red_mask_size: u8, red_mask_shift: u8, green_mask_size: u8, green_mask_shift: u8, blue_mask_size: u8, blue_mask_shift: u8, pub fn from_limine(fb: *const limine.Framebuffer) Self { return .{ .address = @ptrCast(@alignCast(fb.address)), .width = fb.width, .height = fb.height, .pitch = fb.pitch, .red_mask_size = fb.red_mask_size, .red_mask_shift = fb.red_mask_shift, .green_mask_size = fb.green_mask_size, .green_mask_shift = fb.green_mask_shift, .blue_mask_size = fb.blue_mask_size, .blue_mask_shift = fb.blue_mask_shift, .bypp = fb.bpp / 8, }; } }; var stdout_lock: spinlock.Spinlock = .{}; pub fn logFn( comptime message_level: std.log.Level, comptime scope: @TypeOf(.enum_literal), comptime format: []const u8, args: anytype, ) void { if (common.init_data.console == null) return; // Use the same naming as the default logger const level, const color: flanterm.Colors.Color = switch (message_level) { .debug => .{ "D", .green }, .err => .{ "E", .red }, .info => .{ "I", .cyan }, .warn => .{ "W", .yellow }, }; // Use same format as default once again const scope_text = switch (scope) { .default => "", else => "<" ++ @tagName(scope) ++ ">", }; const prefix = std.fmt.comptimePrint("{s}{s}: ", .{ level, scope_text }); { const color_default: flanterm.Colors.Color = .default; stdout_lock.lock(); defer stdout_lock.unlock(); var backing_buf = std.mem.zeroes([512]u8); const buf = std.fmt.bufPrint(backing_buf[0..], color.esc_seq() ++ prefix ++ format ++ color_default.esc_seq() ++ "\n", args) catch return; common.init_data.console.?.write_slice(buf); // cons.setColor(color, 0); // cons.writer().print(prefix ++ format ++ "\n", args) catch return; } } pub fn panic(msg: []const u8, first_trace_addr: ?usize) noreturn { _ = first_trace_addr; const log = std.log.scoped(.panic); log.err("PANIC: {s}", .{msg}); var it = std.debug.StackIterator.init(@returnAddress(), @frameAddress()); defer it.deinit(); while (it.next()) |addr| { if (addr == 0) break; log.err("Addr: 0x{x:0>16}", .{addr}); } arch.instructions.die(); }