Microkernel thing OS experiment (Zig ⚡)
1const console = @import("console"); 2const common = @import("root.zig"); 3const mm = common.mm; 4const std = @import("std"); 5const arch = @import("arch"); 6const spinlock = @import("spinlock"); 7 8// Types 9pub const HardwareDescription = union(enum) { 10 /// Physical address of ACPI RSDP 11 acpi_rsdp: usize, 12 /// Virtual pointer to DTB 13 dtb: *anyopaque, 14 none, 15}; 16 17pub const InitState = struct { 18 bootmem: mm.bootmem.BootPmm = .{}, 19 console: ?console.Console = null, 20 framebuffer: ?console.Framebuffer = null, 21 hardware_description: HardwareDescription = .none, 22 root_task: []align(4096) u8 = undefined, 23 hhdm_slide: usize = 0, 24}; 25 26var stdout_lock: spinlock.Spinlock = .{}; 27 28pub fn logFn( 29 comptime message_level: std.log.Level, 30 comptime scope: @TypeOf(.enum_literal), 31 comptime format: []const u8, 32 args: anytype, 33) void { 34 if (common.init_data.console == null) return; 35 36 // Use the same naming as the default logger 37 const level, const color = switch (message_level) { 38 .debug => .{ "D", 0x3bcf1d }, 39 .err => .{ "E", 0xff0000 }, 40 .info => .{ "I", 0x00bbbb }, 41 .warn => .{ "W", 0xfee409 }, 42 }; 43 // Use same format as default once again 44 const scope_text = switch (scope) { 45 .default => "", 46 else => "<" ++ @tagName(scope) ++ ">", 47 }; 48 const prefix = std.fmt.comptimePrint("{s}{s}: ", .{ level, scope_text }); 49 50 { 51 stdout_lock.lock(); 52 defer stdout_lock.unlock(); 53 54 const cons = &common.init_data.console.?; 55 56 cons.setColor(color, 0); 57 cons.writer().print(prefix ++ format ++ "\n", args) catch return; 58 } 59} 60 61pub fn panic(msg: []const u8, first_trace_addr: ?usize) noreturn { 62 _ = first_trace_addr; 63 const log = std.log.scoped(.panic); 64 common.init_data.console.?.setColor(0xff0000, 0); 65 log.err("PANIC: {s}", .{msg}); 66 var it = std.debug.StackIterator.init(@returnAddress(), @frameAddress()); 67 defer it.deinit(); 68 while (it.next()) |addr| { 69 if (addr == 0) break; 70 log.err("Addr: 0x{x:0>16}", .{addr}); 71 } 72 arch.instructions.die(); 73}