zigk: Implement debug printing

This commit is contained in:
Quinten Kock 2020-12-21 13:42:32 +01:00
parent e743d602a6
commit 1d8f06e8cf
5 changed files with 130 additions and 45 deletions

View File

@ -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

View File

@ -15,5 +15,5 @@ pub export fn kalloc_init(start: usize, len: usize) void {
// }
pub fn kallocNop() void {
std.log.err("calling kallocNop", .{});
}

View File

@ -1,10 +0,0 @@
#include "vga.h"
#include <hal/ops.h>
#include <print/print.h>
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();
}

103
kernel/src/debug.zig Normal file
View File

@ -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};

View File

@ -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");
}