From 1d8f06e8cfadb9458f4adf1485353f0ef980b4d6 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Mon, 21 Dec 2020 13:42:32 +0100 Subject: [PATCH] zigk: Implement debug printing --- Makefile | 1 + .../src/allocator/{calloc.zig => kalloc.zig} | 2 +- kernel/src/arch/x86/panic.c | 10 -- kernel/src/debug.zig | 103 ++++++++++++++++++ kernel/src/main.zig | 59 +++++----- 5 files changed, 130 insertions(+), 45 deletions(-) rename kernel/src/allocator/{calloc.zig => kalloc.zig} (92%) delete mode 100644 kernel/src/arch/x86/panic.c create mode 100644 kernel/src/debug.zig diff --git a/Makefile b/Makefile index 001071c..6f25b2b 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ run: $(KERNEL_HDD) -drive file=$(KERNEL_HDD),format=raw \ -smp 2 \ -enable-kvm \ + -debugcon stdio \ $(QEMUFLAGS) bochs: $(KERNEL_HDD) ext/bochsrc diff --git a/kernel/src/allocator/calloc.zig b/kernel/src/allocator/kalloc.zig similarity index 92% rename from kernel/src/allocator/calloc.zig rename to kernel/src/allocator/kalloc.zig index 8ec4fc9..3b894b5 100644 --- a/kernel/src/allocator/calloc.zig +++ b/kernel/src/allocator/kalloc.zig @@ -15,5 +15,5 @@ pub export fn kalloc_init(start: usize, len: usize) void { // } pub fn kallocNop() void { - + std.log.err("calling kallocNop", .{}); } \ No newline at end of file diff --git a/kernel/src/arch/x86/panic.c b/kernel/src/arch/x86/panic.c deleted file mode 100644 index 345de60..0000000 --- a/kernel/src/arch/x86/panic.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "vga.h" -#include -#include - -void panic(const char *message, const char *filename, int line) { - vga_clear(VGA_BLUE); - printf("KernOS kernel panic:\n%s\n", message); - printf("at %s:%d", filename, line); - halt_catch_fire(); -} diff --git a/kernel/src/debug.zig b/kernel/src/debug.zig new file mode 100644 index 0000000..4ea29c4 --- /dev/null +++ b/kernel/src/debug.zig @@ -0,0 +1,103 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const root = @import("root"); + +const c = @cImport({ + @cInclude("hal/debug.h"); + @cInclude("hal/print.h"); + @cInclude("print/print.h"); + @cInclude("hal/ops.h"); + if( builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { + @cInclude("arch/x86/vga.h"); + } +}); + +pub fn panicMultiline(msgs: comptime [][]const u8) noreturn { + @setCold(true); + if( builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { + // TODO: make framebuffer API more neutral + c.vga_clear(c.VGA_BLUE); + c.vga_setcolor(c.VGA_WHITE); + } + // TODO: implement Zig printing + AWriter.writeAll( + \\/---------------------\ + \\| KORNOS KERNEL PANIC | + \\\---------------------/ + \\ + ) catch unreachable; + for(msgs) |msg, i| { + // for(msg) |ch| { + // c.putchar(ch); + // } + // AWriter.print("TYPE: {}", @typeName(msg)); + try AWriter.writeAll(msg); + try AWriter.writeByte('\n'); + // c.putchar('\n'); + } + c.halt_catch_fire(); + while(true) {} +} + +pub fn zigpanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { + @setCold(true); + // TODO: parse error_return_trace + panicMultiline(&[_][]const u8 {msg, "\nat: (TODO implement zig stacktraces)"}); +} + +var panicbuf: [64]u8 = undefined; +export fn panic(msg: [*:0]const u8, file: [*:0]const u8, line: usize) noreturn { + const len = std.mem.len(msg); + // @panic(msg[0..len]); + // const loc = std.fmt.bufPrint(&panicbuf, "at {}:{}", .{file, line}) catch "error displaying location"; + const loc = file[0..std.mem.len(file)]; + panicMultiline(&[_][]const u8 {msg[0..len], "", loc}); +} + +pub fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + // const scope_prefix = "(" ++ @tagName(scope) ++ "): "; + // const prefix = "[" ++ @tagName(level) ++ "] " ++ scope_prefix; + // nosuspend CWriter.print(prefix ++ format ++ "\n", args) catch return; + // @panic(prefix ++ format ++ "\n"); + + const level_txt = "[" ++ switch (level) { + .emerg => "emergency", + .alert => "alert", + .crit => "critical", + .err => "error", + .warn => "warning", + .notice => "notice", + .info => "info", + .debug => "debug", + } ++ "]"; + const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; + const writer = if(@enumToInt(level) <= @enumToInt(std.log.Level.notice)) AWriter else DWriter; + nosuspend writer.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return; + +} + +fn putcharWrite(context: void, data: []const u8) error{}!usize { + for(data) |d| c.putchar(d); + return data.len; +} +fn debugWrite(context: void, data: []const u8) error{}!usize { + const port: u16 = 0xE9; + for(data) |d| { + asm volatile ("outb %%al, %%dx" :: [d] "{al}" (d), [port] "{dx}" (port) : "memory"); + } + return data.len; +} +fn logWrite(context: void, data: []const u8) error{}!usize { + _ = try putcharWrite(context, data); + _ = try debugWrite(context, data); + return data.len; +} + +const CWriter = std.io.Writer(void, error{}, putcharWrite){.context = undefined}; +const DWriter = std.io.Writer(void, error{}, debugWrite){.context = undefined}; +const AWriter = std.io.Writer(void, error{}, logWrite){.context = undefined}; \ No newline at end of file diff --git a/kernel/src/main.zig b/kernel/src/main.zig index 4d24082..f5951a4 100644 --- a/kernel/src/main.zig +++ b/kernel/src/main.zig @@ -1,9 +1,28 @@ const std = @import("std"); const builtin = @import("builtin"); +const debug = @import("debug.zig"); +const kalloc = @import("allocator/kalloc.zig"); -pub const c_alloc = @import("allocator/calloc.zig"); +pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { + debug.zigpanic(msg, error_return_trace); +} + +pub const log_level = .debug; +pub fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, +) void { + return debug.log(level, scope, format, args); +} + +comptime { + _ = debug; +} const c = @cImport({ + @cInclude("hal/debug.h"); @cInclude("hal/print.h"); @cInclude("print/print.h"); @cInclude("hal/ops.h"); @@ -12,41 +31,13 @@ const c = @cImport({ } }); - - -// pub extern fn puts([*:0]const u8) void; - -pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { - @setCold(true); - if(builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { - c.vga_clear(c.VGA_BLUE); - } - - c.printf("Kernel panic: "); - for (msg) |m| { - c.putchar(m); - } - c.putchar('\n'); - if(error_return_trace) |trace| { - // c.panic("ee", "ee", 0); - c.printf("TRACE: %x %x\n", @intCast(usize, trace.*.index), trace.*.instruction_addresses.len); - } - const msgptr = @ptrCast([*c] const u8, msg); - // c.panic(msgptr, "", 0); - c.puts("HELP\n"); - while(true) {} -} - - export fn zigmain() void { - - const message: [*:0]const u8 = "hello, zig!"; + // // const message: [*:0]const u8 = "hello, zig!\n"; std.log.info("All your codebase are belong to us.", .{}); - c.puts(message); - c_alloc.kallocNop(); + c.puts("zig ee\n"); - var buffer: [32]u8 = undefined; - var alloc = std.heap.FixedBufferAllocator.init(&buffer); - var buf = alloc.allocator.alloc(u8, 40) catch unreachable; + kalloc.kallocNop(); + + // @panic("ee"); }