Compare commits

...

5 Commits

9 changed files with 158 additions and 71 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

@ -1,6 +1,6 @@
.PHONY: x86_64 i386 clean
x86_64 i386:
zig build -Dtarget="$@" --prefix .
zig build -Dtarget="$@" --prefix . $(ZIGFLAGS)
clean:
rm -rf bin zig-cache
rm -rf bin zig-cache

View File

@ -200,8 +200,8 @@ pub fn build(b: *Builder) !void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
// b.setPreferredReleaseMode(std.builtin.Mode.ReleaseSafe);
const mode = b.standardReleaseOptions();
// const mode = b.standardReleaseOptions();
const mode = .ReleaseFast;
const files = try findFiles(ktarget);
@ -241,7 +241,9 @@ pub fn build(b: *Builder) !void {
exe.code_model = std.builtin.CodeModel.kernel;
exe.setLinkerScriptPath(getLinkerScript(ktarget));
// exe.setOutputDir("bin");
// exe.strip = true;
if(mode != .Debug) {
exe.strip = true;
}
// exe.single_threaded = true;
exe.install();

View File

@ -5,7 +5,7 @@ SECTIONS
kernel_phys_offset = 0xffffffff80000000;
. = kernel_phys_offset + 1M;
.stivale2hdr ALIGN(4K) :
.stivale2hdr ALIGN(1K) :
{
KEEP(*(.stivale2hdr))
}

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();
}

View File

@ -26,15 +26,15 @@ void print_stivale2(struct stivale2_struct *info) {
case STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID: {
struct stivale2_struct_tag_framebuffer *f = (struct stivale2_struct_tag_framebuffer *)tag;
printf("Framebuffer tag: %dx%d at %x\n", f->framebuffer_width, f->framebuffer_height, f->framebuffer_addr);
printf("\tPitch: %d\n", f->framebuffer_pitch);
printf("\tBPP: %d\n", f->framebuffer_bpp);
printf("\tMemory model: %d\n", f->memory_model);
printf("\tRed mask size: %d\n", f->red_mask_size);
printf("\tRed mask size: %d\n", f->red_mask_shift);
printf("\tGreen mask size: %d\n", f->green_mask_size);
printf("\tGreen mask size: %d\n", f->green_mask_shift);
printf("\tBlue mask size: %d\n", f->blue_mask_size);
printf("\tBlue mask size: %d\n", f->blue_mask_shift);
// printf("\tPitch: %d\n", f->framebuffer_pitch);
// printf("\tBPP: %d\n", f->framebuffer_bpp);
// printf("\tMemory model: %d\n", f->memory_model);
// printf("\tRed mask size: %d\n", f->red_mask_size);
// printf("\tRed mask size: %d\n", f->red_mask_shift);
// printf("\tGreen mask size: %d\n", f->green_mask_size);
// printf("\tGreen mask size: %d\n", f->green_mask_shift);
// printf("\tBlue mask size: %d\n", f->blue_mask_size);
// printf("\tBlue mask size: %d\n", f->blue_mask_shift);
break;
}
case STIVALE2_STRUCT_TAG_MODULES_ID: {
@ -104,16 +104,16 @@ void parse_stivale2(struct stivale2_struct *info) {
}
case STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID: {
struct stivale2_struct_tag_framebuffer *f = (struct stivale2_struct_tag_framebuffer *)tag;
printf("FRAMEBUFFER (%dx%d at %x)\n", f->framebuffer_width, f->framebuffer_height, f->framebuffer_addr);
printf("\tPitch: %d\n", f->framebuffer_pitch);
printf("\tBPP: %d\n", f->framebuffer_bpp);
printf("\tMemory model: %d\n", f->memory_model);
printf("\tRed mask size: %d\n", f->red_mask_size);
printf("\tRed mask size: %d\n", f->red_mask_shift);
printf("\tGreen mask size: %d\n", f->green_mask_size);
printf("\tGreen mask size: %d\n", f->green_mask_shift);
printf("\tBlue mask size: %d\n", f->blue_mask_size);
printf("\tBlue mask size: %d\n", f->blue_mask_shift);
printf("FB (%dx%d at %x)\n", f->framebuffer_width, f->framebuffer_height, f->framebuffer_addr);
// printf("\tPitch: %d\n", f->framebuffer_pitch);
// printf("\tBPP: %d\n", f->framebuffer_bpp);
// printf("\tMemory model: %d\n", f->memory_model);
// printf("\tRed mask size: %d\n", f->red_mask_size);
// printf("\tRed mask size: %d\n", f->red_mask_shift);
// printf("\tGreen mask size: %d\n", f->green_mask_size);
// printf("\tGreen mask size: %d\n", f->green_mask_shift);
// printf("\tBlue mask size: %d\n", f->blue_mask_size);
// printf("\tBlue mask size: %d\n", f->blue_mask_shift);
break;
}
case STIVALE2_STRUCT_TAG_MODULES_ID: {
@ -163,7 +163,7 @@ void print_stivale2_memmap(struct stivale2_struct *info) {
printf("Memmap (%d entries):\n", m->entries);
for (size_t i = 0; i < m->entries; i++) {
struct stivale2_mmap_entry me = m->memmap[i];
printf("\t[%x+%h] %x\n", (size_t)me.base, (size_t)me.length, me.type);
printf("\t[%x(%h)+%h] %x\n", (size_t)me.base, (size_t)me.base, (size_t)me.length, me.type);
}
break;
}

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