diff --git a/src/kernel/arch/x86/arch.zig b/src/kernel/arch/x86/arch.zig index 3de9009..d9c7324 100644 --- a/src/kernel/arch/x86/arch.zig +++ b/src/kernel/arch/x86/arch.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const log = std.log.scoped(.x86_arch); const builtin = @import("builtin"); const cmos = @import("cmos.zig"); const gdt = @import("gdt.zig"); @@ -326,16 +327,16 @@ pub fn initTTY(boot_payload: BootPayload) TTY { /// Allocator.Error.OutOfMemory - There wasn't enough memory in the allocated created to populate the memory profile, consider increasing mem.FIXED_ALLOC_SIZE /// pub fn initMem(mb_info: BootPayload) Allocator.Error!MemProfile { - std.log.info(.arch_x86, "Init\n", .{}); - defer std.log.info(.arch_x86, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); - std.log.debug(.arch_x86, "KERNEL_ADDR_OFFSET: 0x{X}\n", .{@ptrToInt(&KERNEL_ADDR_OFFSET)}); - std.log.debug(.arch_x86, "KERNEL_STACK_START: 0x{X}\n", .{@ptrToInt(&KERNEL_STACK_START)}); - std.log.debug(.arch_x86, "KERNEL_STACK_END: 0x{X}\n", .{@ptrToInt(&KERNEL_STACK_END)}); - std.log.debug(.arch_x86, "KERNEL_VADDR_START: 0x{X}\n", .{@ptrToInt(&KERNEL_VADDR_START)}); - std.log.debug(.arch_x86, "KERNEL_VADDR_END: 0x{X}\n", .{@ptrToInt(&KERNEL_VADDR_END)}); - std.log.debug(.arch_x86, "KERNEL_PHYSADDR_START: 0x{X}\n", .{@ptrToInt(&KERNEL_PHYSADDR_START)}); - std.log.debug(.arch_x86, "KERNEL_PHYSADDR_END: 0x{X}\n", .{@ptrToInt(&KERNEL_PHYSADDR_END)}); + log.debug("KERNEL_ADDR_OFFSET: 0x{X}\n", .{@ptrToInt(&KERNEL_ADDR_OFFSET)}); + log.debug("KERNEL_STACK_START: 0x{X}\n", .{@ptrToInt(&KERNEL_STACK_START)}); + log.debug("KERNEL_STACK_END: 0x{X}\n", .{@ptrToInt(&KERNEL_STACK_END)}); + log.debug("KERNEL_VADDR_START: 0x{X}\n", .{@ptrToInt(&KERNEL_VADDR_START)}); + log.debug("KERNEL_VADDR_END: 0x{X}\n", .{@ptrToInt(&KERNEL_VADDR_END)}); + log.debug("KERNEL_PHYSADDR_START: 0x{X}\n", .{@ptrToInt(&KERNEL_PHYSADDR_START)}); + log.debug("KERNEL_PHYSADDR_END: 0x{X}\n", .{@ptrToInt(&KERNEL_PHYSADDR_END)}); const mods_count = mb_info.mods_count; mem.ADDR_OFFSET = @ptrToInt(&KERNEL_ADDR_OFFSET); diff --git a/src/kernel/arch/x86/gdt.zig b/src/kernel/arch/x86/gdt.zig index b6478c1..f69d83f 100644 --- a/src/kernel/arch/x86/gdt.zig +++ b/src/kernel/arch/x86/gdt.zig @@ -1,6 +1,7 @@ const std = @import("std"); const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; +const log = std.log.scoped(.x86_gdt); const builtin = @import("builtin"); const is_test = builtin.is_test; const panic = @import("../../panic.zig").panic; @@ -405,8 +406,8 @@ fn makeGdtEntry(base: u32, limit: u20, access: AccessBits, flags: FlagBits) GdtE /// Initialise the Global Descriptor table. /// pub fn init() void { - std.log.info(.gdt, "Init\n", .{}); - defer std.log.info(.gdt, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Initiate TSS gdt_entries[TSS_INDEX] = makeGdtEntry(@ptrToInt(&main_tss_entry), @sizeOf(Tss) - 1, TSS_SEGMENT, NULL_FLAGS); @@ -567,7 +568,7 @@ fn rt_loadedGDTSuccess() void { if (gdt_ptr.base != loaded_gdt.base) { panic(@errorReturnTrace(), "FAILURE: GDT not loaded properly: 0x{X} != {X}\n", .{ gdt_ptr.base, loaded_gdt.base }); } - std.log.info(.gdt, "Tested loading GDT\n", .{}); + log.info("Tested loading GDT\n", .{}); } /// diff --git a/src/kernel/arch/x86/idt.zig b/src/kernel/arch/x86/idt.zig index 2eb16bf..92b3802 100644 --- a/src/kernel/arch/x86/idt.zig +++ b/src/kernel/arch/x86/idt.zig @@ -2,6 +2,7 @@ const std = @import("std"); const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_idt); const builtin = @import("builtin"); const is_test = builtin.is_test; const panic = @import("../../panic.zig").panic; @@ -178,8 +179,8 @@ pub fn openInterruptGate(index: u8, handler: InterruptHandler) IdtError!void { /// Initialise the Interrupt descriptor table /// pub fn init() void { - std.log.info(.idt, "Init\n", .{}); - defer std.log.info(.idt, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); idt_ptr.base = @ptrToInt(&idt_entries); @@ -333,7 +334,7 @@ fn rt_loadedIDTSuccess() void { if (idt_ptr.base != loaded_idt.base) { panic(@errorReturnTrace(), "FAILURE: IDT not loaded properly: 0x{X} != {X}\n", .{ idt_ptr.base, loaded_idt.base }); } - std.log.info(.idt, " Tested loading IDT\n", .{}); + log.info("Tested loading IDT\n", .{}); } /// diff --git a/src/kernel/arch/x86/irq.zig b/src/kernel/arch/x86/irq.zig index 179d43f..7be21b1 100644 --- a/src/kernel/arch/x86/irq.zig +++ b/src/kernel/arch/x86/irq.zig @@ -4,6 +4,7 @@ const is_test = builtin.is_test; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_irq); const build_options = @import("build_options"); const panic = @import("../../panic.zig").panic; const mock_path = build_options.arch_mock_path; @@ -130,8 +131,8 @@ pub fn registerIrq(irq_num: u8, handler: IrqHandler) IrqError!void { /// the IDT interrupt gates for each IRQ. /// pub fn init() void { - std.log.info(.irq, "Init\n", .{}); - defer std.log.info(.irq, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); comptime var i = IRQ_OFFSET; inline while (i < IRQ_OFFSET + 16) : (i += 1) { @@ -246,7 +247,7 @@ fn rt_unregisteredHandlers() void { } } - std.log.info(.irq, "Tested registered handlers\n", .{}); + log.info("Tested registered handlers\n", .{}); } /// @@ -264,7 +265,7 @@ fn rt_openedIdtEntries() void { } } - std.log.info(.irq, "Tested opened IDT entries\n", .{}); + log.info("Tested opened IDT entries\n", .{}); } /// diff --git a/src/kernel/arch/x86/isr.zig b/src/kernel/arch/x86/isr.zig index 88e8870..d6cc9d8 100644 --- a/src/kernel/arch/x86/isr.zig +++ b/src/kernel/arch/x86/isr.zig @@ -4,6 +4,7 @@ const is_test = builtin.is_test; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_isr); const build_options = @import("build_options"); const mock_path = build_options.arch_mock_path; const syscalls = @import("syscalls.zig"); @@ -158,7 +159,7 @@ export fn isrHandler(ctx: *arch.CpuState) usize { // Regular ISR exception, if there is one registered. ret_esp = handler(ctx); } else { - std.log.info(.isr, "State: {X}\n", .{ctx}); + log.info("State: {X}\n", .{ctx}); panic(@errorReturnTrace(), "ISR {} ({}) triggered with error code 0x{X} but not registered\n", .{ exception_msg[isr_num], isr_num, ctx.error_code }); } } @@ -237,8 +238,8 @@ pub fn registerIsr(isr_num: u16, handler: IsrHandler) IsrError!void { /// Initialise the exception and opening up all the IDT interrupt gates for each exception. /// pub fn init() void { - std.log.info(.isr, "Init\n", .{}); - defer std.log.info(.isr, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); comptime var i = 0; inline while (i < 32) : (i += 1) { @@ -384,7 +385,7 @@ fn rt_unregisteredHandlers() void { panic(@errorReturnTrace(), "FAILURE: Pre-testing failed for syscall: {}\n", .{h}); } - std.log.info(.isr, "Tested registered handlers\n", .{}); + log.info("Tested registered handlers\n", .{}); } /// @@ -402,7 +403,7 @@ fn rt_openedIdtEntries() void { } } - std.log.info(.isr, "Tested opened IDT entries\n", .{}); + log.info("Tested opened IDT entries\n", .{}); } /// diff --git a/src/kernel/arch/x86/keyboard.zig b/src/kernel/arch/x86/keyboard.zig index 3ea30b1..a62b171 100644 --- a/src/kernel/arch/x86/keyboard.zig +++ b/src/kernel/arch/x86/keyboard.zig @@ -3,10 +3,10 @@ const build_options = @import("build_options"); const std = @import("std"); const Allocator = std.mem.Allocator; const testing = std.testing; +const log = std.log.scoped(.x86_keyboard); const irq = @import("irq.zig"); const pic = @import("pic.zig"); const arch = if (builtin.is_test) @import(build_options.arch_mock_path ++ "arch_mock.zig") else @import("arch.zig"); -const log = if (builtin.is_test) @import(build_options.arch_mock_path ++ "log_mock.zig") else @import("../../log.zig"); const panic = @import("../../panic.zig").panic; const kb = @import("../../keyboard.zig"); const Keyboard = kb.Keyboard; @@ -164,7 +164,7 @@ fn onKeyEvent(ctx: *arch.CpuState) usize { const scan_code = readKeyboardBuffer(); if (parseScanCode(scan_code)) |action| { if (!keyboard.writeKey(action)) { - std.log.notice(.x86_keyboard, "No room for keyboard action {}\n", .{action}); + log.notice("No room for keyboard action {}\n", .{action}); } } return @ptrToInt(ctx); diff --git a/src/kernel/arch/x86/paging.zig b/src/kernel/arch/x86/paging.zig index 14adf2b..3e3e599 100644 --- a/src/kernel/arch/x86/paging.zig +++ b/src/kernel/arch/x86/paging.zig @@ -2,6 +2,7 @@ const std = @import("std"); const testing = std.testing; const expectEqual = testing.expectEqual; const expect = testing.expect; +const log = std.log.scoped(.x86_paging); const builtin = @import("builtin"); const is_test = builtin.is_test; const panic = @import("../../panic.zig").panic; @@ -383,7 +384,7 @@ pub fn unmap(virtual_start: usize, virtual_end: usize, dir: *Directory) (Allocat /// IN state: *arch.CpuState - The CPU's state when the fault occurred. /// fn pageFault(state: *arch.CpuState) u32 { - std.log.info(.paging, "State: {X}\n", .{state}); + log.info("State: {X}\n", .{state}); var cr0 = asm volatile ("mov %%cr0, %[cr0]" : [cr0] "=r" (-> u32) ); @@ -396,7 +397,7 @@ fn pageFault(state: *arch.CpuState) u32 { var cr4 = asm volatile ("mov %%cr4, %[cr4]" : [cr4] "=r" (-> u32) ); - std.log.info(.paging, "CR0: 0x{X}, CR2: 0x{X}, CR3: 0x{X}, CR4: 0x{X}\n", .{ cr0, cr2, cr3, cr4 }); + log.info("CR0: 0x{X}, CR2: 0x{X}, CR3: 0x{X}, CR4: 0x{X}\n", .{ cr0, cr2, cr3, cr4 }); @panic("Page fault"); } @@ -407,8 +408,8 @@ fn pageFault(state: *arch.CpuState) u32 { /// IN mem_profile: *const MemProfile - The memory profile of the system and kernel /// pub fn init(mem_profile: *const MemProfile) void { - std.log.info(.paging, "Init\n", .{}); - defer std.log.info(.paging, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); isr.registerIsr(isr.PAGE_FAULT, if (build_options.test_mode == .Initialisation) rt_pageFault else pageFault) catch |e| { panic(@errorReturnTrace(), "Failed to register page fault ISR: {}\n", .{e}); @@ -594,7 +595,7 @@ fn rt_accessUnmappedMem(v_end: u32) void { var ptr = @intToPtr(*u8, v_end); var value = ptr.*; // Need this as in release builds the above is optimised out so it needs to be use - std.log.emerg(.paging, "FAILURE: Value: {}\n", .{value}); + log.emerg("FAILURE: Value: {}\n", .{value}); // This is the label that we return to after processing the page fault asm volatile ( \\.global rt_fault_callback @@ -603,7 +604,7 @@ fn rt_accessUnmappedMem(v_end: u32) void { if (!faulted) { panic(@errorReturnTrace(), "FAILURE: Paging should have faulted\n", .{}); } - std.log.info(.paging, "Tested accessing unmapped memory\n", .{}); + log.info("Tested accessing unmapped memory\n", .{}); } fn rt_accessMappedMem(v_end: u32) void { @@ -619,7 +620,7 @@ fn rt_accessMappedMem(v_end: u32) void { if (faulted) { panic(@errorReturnTrace(), "FAILURE: Paging shouldn't have faulted\n", .{}); } - std.log.info(.paging, "Tested accessing mapped memory\n", .{}); + log.info("Tested accessing mapped memory\n", .{}); } pub fn runtimeTests(v_end: u32) void { diff --git a/src/kernel/arch/x86/pic.zig b/src/kernel/arch/x86/pic.zig index 7c9183b..3003bd8 100644 --- a/src/kernel/arch/x86/pic.zig +++ b/src/kernel/arch/x86/pic.zig @@ -1,6 +1,7 @@ const std = @import("std"); const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; +const log = std.log.scoped(.x86_pic); const builtin = @import("builtin"); const is_test = builtin.is_test; const build_options = @import("build_options"); @@ -431,8 +432,8 @@ pub fn clearMask(irq_num: u8) void { /// by Intel up to 0x1F. So this will move the IRQs from 0x00-0x0F to 0x20-0x2F. /// pub fn init() void { - std.log.info(.pic, "Init\n", .{}); - defer std.log.info(.pic, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Initiate sendCommandMaster(ICW1_INITIALISATION | ICW1_EXPECT_ICW4); @@ -823,7 +824,7 @@ fn rt_picAllMasked() void { panic(@errorReturnTrace(), "FAILURE: Slave masks are not set, found: {}\n", .{readDataSlave()}); } - std.log.info(.pic, "Tested masking\n", .{}); + log.info("Tested masking\n", .{}); } /// diff --git a/src/kernel/arch/x86/pit.zig b/src/kernel/arch/x86/pit.zig index f80838f..a1ec3f5 100644 --- a/src/kernel/arch/x86/pit.zig +++ b/src/kernel/arch/x86/pit.zig @@ -5,6 +5,7 @@ const is_test = builtin.is_test; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_pit); const build_options = @import("build_options"); const mock_path = build_options.arch_mock_path; const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig"); @@ -362,8 +363,8 @@ pub fn getFrequency() u32 { /// Initialise the PIT with a handler to IRQ 0. /// pub fn init() void { - std.log.info(.pit, "Init\n", .{}); - defer std.log.info(.pit, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Set up counter 0 at 10000hz in a square wave mode counting in binary const freq: u32 = 10000; @@ -371,7 +372,7 @@ pub fn init() void { panic(@errorReturnTrace(), "Invalid frequency: {}\n", .{freq}); }; - std.log.debug(.pit, "Set frequency at: {}Hz, real frequency: {}Hz\n", .{ freq, getFrequency() }); + log.debug("Set frequency at: {}Hz, real frequency: {}Hz\n", .{ freq, getFrequency() }); // Installs 'pitHandler' to IRQ0 (pic.IRQ_PIT) irq.registerIrq(pic.IRQ_PIT, pitHandler) catch |err| switch (err) { @@ -549,7 +550,7 @@ fn rt_waitTicks() void { panic(@errorReturnTrace(), "FAILURE: Waiting failed. difference: {}, previous_count: {}. Epsilon: {}\n", .{ difference, previous_count, epsilon }); } - std.log.info(.pit, "Tested wait ticks\n", .{}); + log.info("Tested wait ticks\n", .{}); } /// @@ -576,7 +577,7 @@ fn rt_waitTicks2() void { // Reset ticks ticks = 0; - std.log.info(.pit, "Tested wait ticks 2\n", .{}); + log.info("Tested wait ticks 2\n", .{}); } /// @@ -621,7 +622,7 @@ fn rt_initCounter_0() void { panic(@errorReturnTrace(), "FAILURE: Operating mode don't not set properly. Found: {}, expecting: {}\n", .{ actual_mode, expected_mode }); } - std.log.info(.pit, "Tested init\n", .{}); + log.info("Tested init\n", .{}); } /// diff --git a/src/kernel/arch/x86/rtc.zig b/src/kernel/arch/x86/rtc.zig index a2acfb0..0f98bf5 100644 --- a/src/kernel/arch/x86/rtc.zig +++ b/src/kernel/arch/x86/rtc.zig @@ -4,6 +4,7 @@ const is_test = builtin.is_test; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_rtc); const build_options = @import("build_options"); const mock_path = build_options.arch_mock_path; const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig"); @@ -265,8 +266,8 @@ fn enableInterrupts() void { /// Initialise the RTC. /// pub fn init() void { - std.log.info(.rtc, "Init\n", .{}); - defer std.log.info(.rtc, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Register the interrupt handler irq.registerIrq(pic.IRQ_REAL_TIME_CLOCK, rtcHandler) catch |err| switch (err) { @@ -726,7 +727,7 @@ fn rt_init() void { panic(@errorReturnTrace(), "FAILURE: Interrupts not enabled\n", .{}); } - std.log.info(.rtc, "Tested init\n", .{}); + log.info("Tested init\n", .{}); } /// @@ -741,7 +742,7 @@ fn rt_interrupts() void { panic(@errorReturnTrace(), "FAILURE: No interrupt happened\n", .{}); } - std.log.info(.rtc, "Tested interrupts\n", .{}); + log.info("Tested interrupts\n", .{}); } /// diff --git a/src/kernel/arch/x86/syscalls.zig b/src/kernel/arch/x86/syscalls.zig index 69ef1b5..b64d0c8 100644 --- a/src/kernel/arch/x86/syscalls.zig +++ b/src/kernel/arch/x86/syscalls.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const log = std.log.scoped(.x86_syscalls); const builtin = @import("builtin"); const is_test = builtin.is_test; const build_options = @import("build_options"); @@ -57,10 +58,10 @@ fn handle(ctx: *arch.CpuState) u32 { if (handlers[syscall]) |handler| { ctx.eax = handler(ctx, syscallArg(ctx, 0), syscallArg(ctx, 1), syscallArg(ctx, 2), syscallArg(ctx, 3), syscallArg(ctx, 4)); } else { - std.log.warn(.syscall, "Syscall {} triggered but not registered\n", .{syscall}); + log.warn("Syscall {} triggered but not registered\n", .{syscall}); } } else { - std.log.warn(.syscall, "Syscall {} is invalid\n", .{syscall}); + log.warn("Syscall {} is invalid\n", .{syscall}); } return @ptrToInt(ctx); } @@ -242,8 +243,8 @@ inline fn syscallArg(ctx: *arch.CpuState, comptime arg_idx: u32) u32 { /// Initialise syscalls. Registers the isr associated with INTERRUPT. /// pub fn init() void { - std.log.info(.syscall, "Init\n", .{}); - defer std.log.info(.syscall, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); isr.registerIsr(INTERRUPT, handle) catch unreachable; @@ -330,5 +331,5 @@ fn runtimeTests() void { panic(@errorReturnTrace(), "FAILURE syscall5\n", .{}); } - std.log.info(.syscall, "Tested all args\n", .{}); + log.info("Tested all args\n", .{}); } diff --git a/src/kernel/arch/x86/tty.zig b/src/kernel/arch/x86/tty.zig index f9dca60..c9e3813 100644 --- a/src/kernel/arch/x86/tty.zig +++ b/src/kernel/arch/x86/tty.zig @@ -5,6 +5,7 @@ const is_test = builtin.is_test; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; +const log = std.log.scoped(.x86_tty); const build_options = @import("build_options"); const mock_path = build_options.mock_path; const vga = if (is_test) @import("../../" ++ mock_path ++ "vga_mock.zig") else @import("vga.zig"); @@ -380,7 +381,7 @@ pub fn pageUp() void { page_index += 1; // Bounds have been checked, so shouldn't error videoCopy(START_OF_DISPLAYABLE_REGION, pages[page_index][0..TOTAL_CHAR_ON_PAGE], TOTAL_CHAR_ON_PAGE) catch |e| { - std.log.crit(.tty, "Error moving page up. Error: {}\n", .{e}); + log.crit("Error moving page up. Error: {}\n", .{e}); }; vga.disableCursor(); } @@ -396,7 +397,7 @@ pub fn pageDown() void { page_index -= 1; // Bounds have been checked, so shouldn't error videoCopy(START_OF_DISPLAYABLE_REGION, pages[page_index][0..TOTAL_CHAR_ON_PAGE], TOTAL_CHAR_ON_PAGE) catch |e| { - std.log.crit(.tty, "Error moving page down. Error: {}\n", .{e}); + log.crit("Error moving page down. Error: {}\n", .{e}); }; if (page_index == 0) { @@ -416,7 +417,7 @@ pub fn clearScreen() void { // Move all the rows up // This is within bounds, so shouldn't error pagesMoveRowsUp(ROW_TOTAL) catch |e| { - std.log.crit(.tty, "Error moving all pages up. Error: {}\n", .{e}); + log.crit("Error moving all pages up. Error: {}\n", .{e}); }; // Clear the screen @@ -535,13 +536,13 @@ pub fn init() void { // Set the top 7 rows blank setVideoBuffer(blank, START_OF_DISPLAYABLE_REGION) catch |e| { - std.log.crit(.tty, "Error clearing the top 7 rows. Error: {}\n", .{e}); + log.crit("Error clearing the top 7 rows. Error: {}\n", .{e}); }; row += @truncate(u8, row_offset + ROW_MIN); } else { // Clear the screen setVideoBuffer(blank, VIDEO_BUFFER_SIZE) catch |e| { - std.log.crit(.tty, "Error clearing the screen. Error: {}\n", .{e}); + log.crit("Error clearing the screen. Error: {}\n", .{e}); }; // Set the row to below the logo row = ROW_MIN; @@ -900,7 +901,7 @@ test "putEntryAt in displayable region page_index is not 0" { column = @truncate(u8, vga.WIDTH) - @truncate(u8, text.len); row = ROW_MIN - 1; writeString(text) catch |e| { - std.log.crit(.tty, "Unable to print page number, printing out of bounds. Error: {}\n", .{e}); + log.crit("Unable to print page number, printing out of bounds. Error: {}\n", .{e}); }; column = column_temp; row = row_temp; @@ -1558,7 +1559,7 @@ test "pageUp bottom page" { column = @truncate(u8, vga.WIDTH) - @truncate(u8, text.len); row = ROW_MIN - 1; writeString(text) catch |e| { - std.log.crit(.tty, "Unable to print page number, printing out of bounds. Error: {}\n", .{e}); + log.crit("Unable to print page number, printing out of bounds. Error: {}\n", .{e}); }; column = column_temp; row = row_temp; @@ -1638,7 +1639,7 @@ test "pageDown top page" { column = @truncate(u8, vga.WIDTH) - @truncate(u8, text.len); row = ROW_MIN - 1; writeString(text) catch |e| { - std.log.crit(.tty, "Unable to print page number, printing out of bounds. Error: {}\n", .{e}); + log.crit("Unable to print page number, printing out of bounds. Error: {}\n", .{e}); }; column = column_temp; row = row_temp; @@ -2044,7 +2045,7 @@ fn rt_initialisedGlobals() void { panic(@errorReturnTrace(), "Screen all blank, should have logo and page number\n", .{}); } - std.log.info(.tty, "Tested globals\n", .{}); + log.info("Tested globals\n", .{}); } /// @@ -2096,7 +2097,7 @@ fn rt_printString() void { // Clear the text writeString(clear_text) catch |e| panic(@errorReturnTrace(), "Failed to print string to tty: {}\n", .{e}); - std.log.info(.tty, "Tested printing\n", .{}); + log.info("Tested printing\n", .{}); } /// diff --git a/src/kernel/arch/x86/vga.zig b/src/kernel/arch/x86/vga.zig index 571c6d4..1b840b5 100644 --- a/src/kernel/arch/x86/vga.zig +++ b/src/kernel/arch/x86/vga.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const is_test = builtin.is_test; const expectEqual = std.testing.expectEqual; +const log = std.log.scoped(.x86_vga); const build_options = @import("build_options"); const arch = if (is_test) @import(build_options.arch_mock_path ++ "arch_mock.zig") else @import("arch.zig"); const panic = @import("../../panic.zig").panic; @@ -283,8 +284,8 @@ pub fn setCursorShape(shape: CursorShape) void { /// Initialise the VGA text mode. This sets the cursor and underline shape. /// pub fn init() void { - std.log.info(.vga, "Init\n", .{}); - defer std.log.info(.vga, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Set the maximum scan line to 0x0F sendPortData(REG_MAXIMUM_SCAN_LINE, CURSOR_SCANLINE_END); @@ -536,7 +537,7 @@ fn rt_correctMaxScanLine() void { panic(@errorReturnTrace(), "FAILURE: Max scan line not {}, found {}\n", .{ CURSOR_SCANLINE_END, max_scan_line }); } - std.log.info(.vga, "Tested max scan line\n", .{}); + log.info("Tested max scan line\n", .{}); } /// @@ -555,7 +556,7 @@ fn rt_correctCursorShape() void { panic(@errorReturnTrace(), "FAILURE: Cursor scanline are incorrect. Start: {}, end: {}\n", .{ cursor_start, cursor_end }); } - std.log.info(.vga, "Tested cursor shape\n", .{}); + log.info("Tested cursor shape\n", .{}); } /// @@ -587,7 +588,7 @@ fn rt_setCursorGetCursor() void { // Restore the previous x and y updateCursor(prev_x_loc, prev_y_loc); - std.log.info(.vga, "Tested updating cursor\n", .{}); + log.info("Tested updating cursor\n", .{}); } /// diff --git a/src/kernel/filesystem/initrd.zig b/src/kernel/filesystem/initrd.zig index d8aa56b..36f1ac6 100644 --- a/src/kernel/filesystem/initrd.zig +++ b/src/kernel/filesystem/initrd.zig @@ -5,6 +5,7 @@ const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; const expectEqualSlices = std.testing.expectEqualSlices; +const log = std.log.scoped(.initrd); const build_options = @import("build_options"); const mock_path = build_options.mock_path; const Allocator = std.mem.Allocator; @@ -150,8 +151,8 @@ pub const InitrdFS = struct { /// allocated will be freed. /// pub fn init(stream: *std.io.FixedBufferStream([]u8), allocator: *Allocator) (Error || error{EndOfStream} || Allocator.Error)!*InitrdFS { - std.log.info(.initrd, "Init\n", .{}); - defer std.log.info(.initrd, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // First @sizeOf(usize) bytes is the number of files const num_of_files = try stream.reader().readIntNative(usize); @@ -658,7 +659,7 @@ fn rt_openReadClose(allocator: *Allocator) void { error.NoSuchFileOrDir => {}, else => panic(@errorReturnTrace(), "FAILURE: Expected error\n", .{}), }; - std.log.info(.initrd, "Opened, read and closed\n", .{}); + log.info("Opened, read and closed\n", .{}); } /// diff --git a/src/kernel/filesystem/vfs.zig b/src/kernel/filesystem/vfs.zig index e49cc02..5634541 100644 --- a/src/kernel/filesystem/vfs.zig +++ b/src/kernel/filesystem/vfs.zig @@ -446,23 +446,26 @@ const TestFS = struct { } // Form a list containing all directory nodes to check via a breadth-first search // This is inefficient but good for testing as it's clear and easy to modify - var to_check = TailQueue(*TreeNode).init(); - var root_node = try to_check.createNode(&test_fs.tree, test_fs.allocator); + var to_check = TailQueue(*TreeNode){}; + var root_node = try test_fs.allocator.create(TailQueue(*TreeNode).Node); + root_node.* = .{ .data = &test_fs.tree }; to_check.append(root_node); while (to_check.popFirst()) |queue_node| { var tree_node = queue_node.data; - to_check.destroyNode(queue_node, test_fs.allocator); + test_fs.allocator.destroy(queue_node); if ((@TypeOf(node) == *const FileNode and tree_node.val.isFile() and &tree_node.val.File == node) or (@TypeOf(node) == *const DirNode and tree_node.val.isDir() and &tree_node.val.Dir == node) or (@TypeOf(node) == *const Node and &tree_node.val == node)) { // Clean up any unused queue nodes while (to_check.popFirst()) |t_node| { - to_check.destroyNode(t_node, test_fs.allocator); + test_fs.allocator.destroy(t_node); } return tree_node; } for (tree_node.children.items) |child| { // It's not the parent so add its children to the list for checking - to_check.append(try to_check.createNode(child, test_fs.allocator)); + var n = try test_fs.allocator.create(TailQueue(*TreeNode).Node); + n.* = .{ .data = child }; + to_check.append(n); } } return null; diff --git a/src/kernel/heap.zig b/src/kernel/heap.zig index 2a1f5b1..f6699c8 100644 --- a/src/kernel/heap.zig +++ b/src/kernel/heap.zig @@ -1,6 +1,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const testing = std.testing; +const log = std.log.scoped(.heap); const builtin = @import("builtin"); const is_test = builtin.is_test; const build_options = @import("build_options"); @@ -601,8 +602,8 @@ const FreeListAllocator = struct { /// Allocator.Error.OutOfMemory - heap_vmm's allocator didn't have enough memory available to fulfill the request /// pub fn init(comptime vmm_payload: type, heap_vmm: *vmm.VirtualMemoryManager(vmm_payload), attributes: vmm.Attributes, heap_size: usize) (FreeListAllocator.Error || Allocator.Error)!FreeListAllocator { - std.log.info(.heap, "Init\n", .{}); - defer std.log.info(.heap, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); var heap_start = (try heap_vmm.alloc(heap_size / vmm.BLOCK_SIZE, attributes)) orelse panic(null, "Not enough contiguous virtual memory blocks to allocate to kernel heap\n", .{}); // This free call cannot error as it is guaranteed to have been allocated above errdefer heap_vmm.free(heap_start) catch unreachable; diff --git a/src/kernel/kmain.zig b/src/kernel/kmain.zig index 23c44e7..4b858ed 100644 --- a/src/kernel/kmain.zig +++ b/src/kernel/kmain.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const logger = std.log.scoped(.kmain); const builtin = @import("builtin"); const is_test = builtin.is_test; const build_options = @import("build_options"); @@ -78,9 +79,9 @@ export fn kmain(boot_payload: arch.BootPayload) void { panic_root.panic(@errorReturnTrace(), "Failed to initialise kernel VMM: {}", .{e}); }; - std.log.info(.kmain, "Init arch " ++ @tagName(builtin.arch) ++ "\n", .{}); + logger.info("Init arch " ++ @tagName(builtin.arch) ++ "\n", .{}); arch.init(&mem_profile); - std.log.info(.kmain, "Arch init done\n", .{}); + logger.info("Arch init done\n", .{}); // Give the kernel heap 10% of the available memory. This can be fine-tuned as time goes on. var heap_size = mem_profile.mem_kb / 10 * 1024; @@ -133,12 +134,12 @@ export fn kmain(boot_payload: arch.BootPayload) void { } // Initialisation is finished, now does other stuff - std.log.info(.kmain, "Init\n", .{}); + logger.info("Init\n", .{}); // Main initialisation finished so can enable interrupts arch.enableInterrupts(); - std.log.info(.kmain, "Creating init2\n", .{}); + logger.info("Creating init2\n", .{}); // Create a init2 task var idle_task = task.Task.create(initStage2, &kernel_heap.allocator) catch |e| { @@ -173,7 +174,7 @@ fn initStage2() noreturn { switch (build_options.test_mode) { .Initialisation => { - std.log.info(.kmain, "SUCCESS\n", .{}); + logger.info("SUCCESS\n", .{}); }, else => {}, } diff --git a/src/kernel/panic.zig b/src/kernel/panic.zig index 5ba6885..2715173 100644 --- a/src/kernel/panic.zig +++ b/src/kernel/panic.zig @@ -8,6 +8,7 @@ const build_options = @import("build_options"); const ArrayList = std.ArrayList; const Allocator = std.mem.Allocator; const testing = std.testing; +const log = std.log.scoped(.panic); /// The possible errors from panic code const PanicError = error{ @@ -111,7 +112,7 @@ var symbol_map: ?SymbolMap = null; /// fn logTraceAddress(addr: usize) void { const str = if (symbol_map) |syms| syms.search(addr) orelse "?????" else "(no symbols available)"; - std.log.emerg(.panic, "{x}: {}\n", .{ addr, str }); + log.emerg("{x}: {}\n", .{ addr, str }); } /// @@ -278,7 +279,7 @@ fn parseMapEntry(start: *[*]const u8, end: *const u8) (PanicError || std.fmt.Par pub fn panic(trace: ?*builtin.StackTrace, comptime format: []const u8, args: anytype) noreturn { @setCold(true); - std.log.emerg(.panic, "Kernel panic: " ++ format ++ "\n", args); + log.emerg("Kernel panic: " ++ format ++ "\n", args); if (trace) |trc| { var last_addr: u64 = 0; for (trc.instruction_addresses) |ret_addr| { @@ -312,8 +313,8 @@ pub fn panic(trace: ?*builtin.StackTrace, comptime format: []const u8, args: any /// std.fmt.ParseIntError - See parseMapEntry. /// pub fn init(mem_profile: *const mem.MemProfile, allocator: *Allocator) (PanicError || Allocator.Error || std.fmt.ParseIntError)!void { - std.log.info(.panic, "Init\n", .{}); - defer std.log.info(.panic, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Exit if we haven't loaded all debug modules if (mem_profile.modules.len < 1) { diff --git a/src/kernel/pmm.zig b/src/kernel/pmm.zig index 65c24d0..541c7f3 100644 --- a/src/kernel/pmm.zig +++ b/src/kernel/pmm.zig @@ -1,5 +1,6 @@ const is_test = @import("builtin").is_test; const std = @import("std"); +const log = std.log.scoped(.pmm); const build_options = @import("build_options"); const mock_path = build_options.mock_path; const arch = if (is_test) @import(mock_path ++ "arch_mock.zig") else @import("arch.zig").internals; @@ -98,8 +99,8 @@ pub fn blocksFree() usize { /// IN allocator: *Allocator - The allocator to use to allocate the bitmaps. /// pub fn init(mem_profile: *const MemProfile, allocator: *Allocator) void { - std.log.info(.pmm, "Init\n", .{}); - defer std.log.info(.pmm, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); bitmap = PmmBitmap.init(mem_profile.mem_kb * 1024 / BLOCK_SIZE, allocator) catch |e| { panic(@errorReturnTrace(), "Bitmap allocation failed: {}\n", .{e}); @@ -232,5 +233,5 @@ fn runtimeTests(mem_profile: *const MemProfile, allocator: *Allocator) void { panic(@errorReturnTrace(), "FAILURE: Failed freeing allocation in PMM rt test: {}", .{e}); }; } - std.log.info(.pmm, "Tested allocation\n", .{}); + log.info("Tested allocation\n", .{}); } diff --git a/src/kernel/scheduler.zig b/src/kernel/scheduler.zig index 612fd24..8601a25 100644 --- a/src/kernel/scheduler.zig +++ b/src/kernel/scheduler.zig @@ -2,6 +2,7 @@ const std = @import("std"); const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; const assert = std.debug.assert; +const log = std.log.scoped(.scheduler); const builtin = @import("builtin"); const is_test = builtin.is_test; const build_options = @import("build_options"); @@ -98,7 +99,8 @@ pub fn pickNextTask(ctx: *arch.CpuState) usize { /// be freed on return. /// pub fn scheduleTask(new_task: *Task, allocator: *Allocator) Allocator.Error!void { - var task_node = try tasks.createNode(new_task, allocator); + var task_node = try allocator.create(TailQueue(*Task).Node); + task_node.* = .{ .data = new_task }; tasks.prepend(task_node); } @@ -116,11 +118,11 @@ pub fn scheduleTask(new_task: *Task, allocator: *Allocator) Allocator.Error!void /// pub fn init(allocator: *Allocator) Allocator.Error!void { // TODO: Maybe move the task init here? - std.log.info(.scheduler, "Init\n", .{}); - defer std.log.info(.scheduler, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); // Init the task list for round robin - tasks = TailQueue(*Task).init(); + tasks = TailQueue(*Task){}; // Set up the init task to continue execution current_task = try allocator.create(Task); @@ -181,7 +183,7 @@ test "pickNextTask" { var ctx: arch.CpuState = std.mem.zeroes(arch.CpuState); var allocator = std.testing.allocator; - tasks = TailQueue(*Task).init(); + tasks = TailQueue(*Task){}; // Set up a current task current_task = try allocator.create(Task); @@ -229,7 +231,7 @@ test "pickNextTask" { // Free the queue while (tasks.pop()) |elem| { - tasks.destroyNode(elem, allocator); + allocator.destroy(elem); } } @@ -244,7 +246,7 @@ test "createNewTask add new task" { var allocator = std.testing.allocator; // Init the task list - tasks = TailQueue(*Task).init(); + tasks = TailQueue(*Task){}; var test_fn1_task = try Task.create(test_fn1, allocator); defer test_fn1_task.destroy(allocator); @@ -253,7 +255,7 @@ test "createNewTask add new task" { expectEqual(tasks.len, 1); // Free the memory - tasks.destroyNode(tasks.first.?, allocator); + allocator.destroy(tasks.first.?); } test "init" { @@ -276,7 +278,7 @@ test "init" { current_task.destroy(allocator); while (tasks.pop()) |elem| { elem.data.destroy(allocator); - tasks.destroyNode(elem, allocator); + allocator.destroy(elem); } } @@ -288,7 +290,7 @@ var is_set: *volatile bool = undefined; /// The test task function. /// fn task_function() noreturn { - std.log.info(.scheduler, "Switched\n", .{}); + log.info("Switched\n", .{}); is_set.* = false; while (true) {} } @@ -343,7 +345,7 @@ fn rt_variable_preserved(allocator: *Allocator) void { panic(@errorReturnTrace(), "FAILED: z not 3, but: {}\n", .{z}); } - std.log.info(.scheduler, "SUCCESS: Scheduler variables preserved\n", .{}); + log.info("SUCCESS: Scheduler variables preserved\n", .{}); } /// diff --git a/src/kernel/tty.zig b/src/kernel/tty.zig index 38d9919..b788aba 100644 --- a/src/kernel/tty.zig +++ b/src/kernel/tty.zig @@ -1,6 +1,7 @@ const std = @import("std"); const fmt = std.fmt; const Allocator = std.mem.Allocator; +const log = std.log.scoped(.tty); const build_options = @import("build_options"); const arch = @import("arch.zig").internals; const panic = @import("panic.zig").panic; @@ -51,7 +52,7 @@ fn printCallback(ctx: void, str: []const u8) !usize { pub fn print(comptime format: []const u8, args: anytype) void { // Printing can't error because of the scrolling, if it does, we have a big problem fmt.format(OutStream{ .context = {} }, format, args) catch |e| { - std.log.emerg(.tty, "Error printing. Error: {}\n", .{e}); + log.emerg("Error printing. Error: {}\n", .{e}); }; } @@ -101,8 +102,8 @@ pub fn clear() void { /// IN boot_payload: arch.BootPayload - The payload passed to the kernel on boot /// pub fn init(alloc: *Allocator, boot_payload: arch.BootPayload) void { - std.log.info(.tty, "Init\n", .{}); - defer std.log.info(.tty, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); tty = arch.initTTY(boot_payload); allocator = alloc; } diff --git a/src/kernel/vmm.zig b/src/kernel/vmm.zig index 4780bb7..fdca233 100644 --- a/src/kernel/vmm.zig +++ b/src/kernel/vmm.zig @@ -3,6 +3,7 @@ const mock_path = build_options.mock_path; const builtin = @import("builtin"); const is_test = builtin.is_test; const std = @import("std"); +const log = std.log.scoped(.vmm); const bitmap = @import("bitmap.zig"); const pmm = @import("pmm.zig"); const mem = if (is_test) @import(mock_path ++ "mem_mock.zig") else @import("mem.zig"); @@ -418,8 +419,8 @@ pub fn VirtualMemoryManager(comptime Payload: type) type { /// error.OutOfMemory - The allocator cannot allocate the memory required /// pub fn init(mem_profile: *const mem.MemProfile, allocator: *Allocator) Allocator.Error!VirtualMemoryManager(arch.VmmPayload) { - std.log.info(.vmm, "Init\n", .{}); - defer std.log.info(.vmm, "Done\n", .{}); + log.info("Init\n", .{}); + defer log.info("Done\n", .{}); var vmm = try VirtualMemoryManager(arch.VmmPayload).init(@ptrToInt(&KERNEL_ADDR_OFFSET), 0xFFFFFFFF, allocator, arch.VMM_MAPPER, arch.KERNEL_VMM_PAYLOAD); @@ -727,5 +728,5 @@ fn runtimeTests(comptime Payload: type, vmm: VirtualMemoryManager(Payload), mem_ } } - std.log.info(.vmm, "Tested allocations\n", .{}); + log.info("Tested allocations\n", .{}); } diff --git a/test/mock/kernel/mock_framework.zig b/test/mock/kernel/mock_framework.zig index 2508674..0dd6820 100644 --- a/test/mock/kernel/mock_framework.zig +++ b/test/mock/kernel/mock_framework.zig @@ -434,7 +434,7 @@ fn Mock() type { expectEqual(@as(DataElementType, action.data), expect_type); // Free the node - action_list.*.destroyNode(action_node, GlobalAllocator); + GlobalAllocator.destroy(action_node); return ret; } else { @@ -456,7 +456,7 @@ fn Mock() type { pub fn addAction(self: *Self, comptime fun_name: []const u8, data: anytype, action_type: ActionType) void { // Add a new mapping if one doesn't exist. if (!self.named_actions.contains(fun_name)) { - self.named_actions.put(fun_name, TailQueue(Action).init()) catch unreachable; + self.named_actions.put(fun_name, .{}) catch unreachable; } // Get the function mapping to add the parameter to. @@ -466,7 +466,8 @@ fn Mock() type { .action = action_type, .data = createDataElement(data), }; - var a = action_list.createNode(action, GlobalAllocator) catch unreachable; + var a = GlobalAllocator.create(TailQueue(Action).Node) catch unreachable; + a.* = .{ .data = action }; action_list.append(a); // Need to re-assign the value as it isn't updated when we just append actions_kv.value = action_list; @@ -510,7 +511,7 @@ fn Mock() type { expectTest(param_type, param, test_action.data); // Free the node - action_list.destroyNode(test_node, GlobalAllocator); + GlobalAllocator.destroy(test_node); } break :ret expectGetValue(fun_name, &action_list, RetType); }, @@ -550,7 +551,7 @@ fn Mock() type { const actual_function = getDataValue(expected_function, test_element); // Free the node - action_list.destroyNode(test_node, GlobalAllocator); + GlobalAllocator.destroy(test_node); // The data element will contain the function to call const r = switch (params.len) { @@ -649,7 +650,7 @@ fn Mock() type { ActionType.RepeatFunctionCall => { // As this is a repeat action, the function will still be here // So need to free it - action_list.destroyNode(action_node, GlobalAllocator); + GlobalAllocator.destroy(action_node); next.value = action_list; }, } diff --git a/test/runtime_test.zig b/test/runtime_test.zig index 8a7812b..b1d4fe0 100644 --- a/test/runtime_test.zig +++ b/test/runtime_test.zig @@ -255,7 +255,7 @@ pub const RuntimeStep = struct { // put line in the queue var node = self.builder.allocator.create(Node) catch unreachable; - node.* = Node.init(line); + node.* = .{ .next = null, .data = line }; self.msg_queue.put(node); } }