Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Kbz-8/42_KFS
Browse files Browse the repository at this point in the history
  • Loading branch information
Namonay committed Jul 20, 2024
2 parents d8f30b4 + 687e8ec commit a902877
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Build/
*.a
*.exe
zig-out/
.gdb_*
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
all :
@zig build

debug:
@zig build -Ddebug=true

run : fclean
@zig build run

run-debug : fclean
@zig build run-debug
@zig build run-debug -Ddebug=true

clean:

Expand Down
7 changes: 4 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ const std = @import("std");

pub fn build(b: *std.Build) void
{
const debug_symbols = b.option(bool, "debug", "Add debug symbols") orelse false;

const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/kernel/kmain.zig" } },
.target = b.resolveTargetQuery(.{
.target = b.resolveTargetQuery(.{
.cpu_arch = .x86,
.abi = .none,
.os_tag = .freestanding,
}),
.optimize = .Debug,
// .strip = true,
.strip = !debug_symbols,
.code_model = .kernel,
.pic = false,
.error_tracing = false,
});
kernel.setLinkerScriptPath(.{ .src_path = .{ .owner = b, .sub_path = "linker.ld" } });

Expand Down
4 changes: 2 additions & 2 deletions sources/drivers/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pub fn initDrivers() void
pub fn shutdownDrivers() void
{
@setCold(true);
kernel.logs.klog("[Drivers Manager] unloading drivers...");
kernel.logs.klog("[Drivers Manager] unloaded all drivers");
kernel.logs.klogln("[Drivers Manager] unloading drivers...");
kernel.logs.klogln("[Drivers Manager] unloaded all drivers");
}
1 change: 1 addition & 0 deletions sources/drivers/vga/vga.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_c
updateCursor();
updateNavbar();
kernel.logs.klogln("[VGA Driver] loaded");
kernel.stk.stackTrace(42);
}

fn putEntry(c: u8, color: u8, x: usize, y: usize) void
Expand Down
1 change: 1 addition & 0 deletions sources/kernel/arch/x86/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export fn _start() align(16) linksection(".text.boot") callconv(.Naked) noreturn
asm volatile
(
\\ movl %[stk], %esp
\\ xor %ebp, %ebp
\\ call kmain
:
: [stk] "{ecx}" (@intFromPtr(&kernel_stack) + @sizeOf(@TypeOf(kernel_stack))),
Expand Down
1 change: 1 addition & 0 deletions sources/kernel/kmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const libk = @import("libk");

pub const logs = @import("log.zig");
pub const kpanic = @import("panic.zig").kpanic;
pub const stk = @import("stack_trace.zig");

pub const arch = if(!is_test) switch(builtin.cpu.arch)
{
Expand Down
2 changes: 2 additions & 0 deletions sources/kernel/panic.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const vga = @import("drivers").vga;
const arch = @import("kmain.zig").arch;
const logs = @import("log.zig");
const stk = @import("stack_trace.zig");

pub fn kpanic(message: []const u8) noreturn
{
Expand All @@ -9,6 +10,7 @@ pub fn kpanic(message: []const u8) noreturn
vga.setColor(vga.Color.WHITE, vga.Color.RED);
vga.clear(vga.Color.RED);
vga.putString(logs.getLogBuffer());
stk.stackTrace(42);
vga.putString("\nkernel panic : ");
vga.putString(message);
vga.putString("\n[cannot recover, freezing the system]");
Expand Down
27 changes: 27 additions & 0 deletions sources/kernel/stack_trace.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const libk = @import("libk");

const StackFrame = packed struct
{
esp: ?*StackFrame,
eip: u32
};

pub fn stackTrace(max_frame_stack: usize) void
{
var stk: ?*StackFrame = null;
asm volatile
(
\\ movl %ebp, %eax
: [stk] "={eax}" (stk)
:
: "memory"
);
libk.io.kputs("================= Stack Trace ================= \n");
var frame: usize = 0;
while(stk.?.esp != null and frame < max_frame_stack) : (frame += 1)
{
libk.io.kprintf("fn 0x{} {}()\n", .{ stk.?.eip, "??" });
stk = stk.?.esp;
}
libk.io.kputs("=============== End Stack Trace =============== \n");
}
28 changes: 22 additions & 6 deletions sources/libk/io/out.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn kputs(message: []const u8) void
const ArgTypes = enum
{
Int,
Bool,
Float,
Char,
String,
Expand Down Expand Up @@ -41,7 +42,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
if(args[arg_idx] > 0 and args[arg_idx] < 256)
vga.putChar(args[arg_idx])
else
putNb(args[arg_idx]);
kputNb(args[arg_idx]);
}
else if(@typeInfo(@TypeOf(args[arg_idx])) == .Array and @typeInfo(@TypeOf(args[arg_idx])).Array.child == u8)
kputs(args[arg_idx])
Expand All @@ -59,15 +60,29 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
else switch(@TypeOf(args[arg_idx]))
{
i8, u8, => vga.putChar(args[arg_idx]),
i16, u16, i32, u32, i64, u64, isize, usize => putNb(args[arg_idx]),
i16, u16, i32, u32, i64, u64, isize, usize => kputNb(args[arg_idx]),
f16, f32, f64, comptime_float => {},
bool =>
{
if(args[arg_idx])
kputs("true")
else
kputs("false");
},
else => @compileError("could not manage auto detected type : " ++ @typeName(@TypeOf(args[arg_idx])) ++ "; please add type identifier between brackets"),
}
}
switch(arg_type)
{
.Bool =>
{
if(args[arg_idx])
kputs("true")
else
kputs("false");
},
.Char => vga.putChar(args[arg_idx]),
.Int => putNb(args[arg_idx]),
.Int => kputNb(args[arg_idx]),
.Float => {},
.String => kputs(args[arg_idx]),
.Pointer => { kputs("0x"); kputs(string.toStringBase(@intFromPtr(args[arg_idx]), 16)); },
Expand All @@ -84,6 +99,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
{
switch(c)
{
'b' => arg_type = .Bool,
'c' => arg_type = .Char,
'i' => arg_type = .Int,
'f' => arg_type = .Float,
Expand All @@ -105,7 +121,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
}
}

pub fn putNb(nbr: i64) void
pub fn kputNb(nbr: i64) void
{
if(nbr <= -2147483648)
vga.putString("-2147483648")
Expand All @@ -114,11 +130,11 @@ pub fn putNb(nbr: i64) void
else if(nbr < 0)
{
vga.putChar('-');
putNb(-nbr);
kputNb(-nbr);
}
else if(nbr >= 10)
{
putNb(@divFloor(nbr, 10));
kputNb(@divFloor(nbr, 10));
vga.putChar(@intCast(@mod(nbr, 10) + @as(u8, 48)));
}
else
Expand Down

0 comments on commit a902877

Please sign in to comment.