Skip to content

Commit

Permalink
adding elf32 support base, adding multiboot infos, adding functions n…
Browse files Browse the repository at this point in the history
…ames to stack trace un debug
  • Loading branch information
Kbz-8 committed Jul 21, 2024
1 parent b01b34f commit 6a04a77
Show file tree
Hide file tree
Showing 17 changed files with 305 additions and 99 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
all :
@zig build
@zig build -Ddebug=false

debug:
@zig build -Ddebug=true

run : fclean
@zig build run
@zig build run -Ddebug=false

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

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

clean:
@rm -rf .zig_cache

fclean : clean
fclean:
@rm -rf zig-out

.PHONY : all run
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn build(b: *std.Build) void
.cpu_arch = .x86,
.abi = .gnu,
.os_tag = .freestanding,
.ofmt = .elf,
}),
.optimize = .Debug,
.strip = !debug_symbols,
Expand Down
33 changes: 14 additions & 19 deletions linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,35 @@ SECTIONS
{
. = 2M;

.text : ALIGN(4K)
.text : ALIGN(0x1000)
{
KEEP(*(.multiboot))
*(.text)
}

.rodata : ALIGN(4K)
.rodata : ALIGN(0x1000)
{
*(.rodata)
__debug_info_start = .;
KEEP(*(.debug_info))
__debug_info_end = .;
__debug_abbrev_start = .;
KEEP(*(.debug_abbrev))
__debug_abbrev_end = .;
__debug_str_start = .;
KEEP(*(.debug_str))
__debug_str_end = .;
__debug_line_start = .;
KEEP(*(.debug_line))
__debug_line_end = .;
__debug_ranges_start = .;
KEEP(*(.debug_ranges))
__debug_ranges_end = .;
}

.data : ALIGN(4K)
.data : ALIGN(0x1000)
{
*(.data)
}

.bss : ALIGN(4K)
.bss : ALIGN(0x1000)
{
*(COMMON)
*(.bss)
}

.strtab : ALIGN(0x1000)
{
*(.strtab)
}

.shstrtab : ALIGN(0x1000)
{
*(.shstrtab)
}
}
2 changes: 1 addition & 1 deletion sources/drivers/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn initDrivers() void
kernel.logs.beginSection();
kb.init();
vga.init("RatiOS 0.2", vga.computeColor(vga.Color.BLACK, vga.Color.LIGHT_GREY), vga.computeColor(vga.Color.WHITE, vga.Color.DARK_GREY), vga.computeColor(vga.Color.LIGHT_BLUE, vga.Color.DARK_GREY));
//power.init();
power.init();
kernel.logs.endSection();
kernel.logs.klogln("[Drivers Manager] loaded drivers");
}
Expand Down
2 changes: 1 addition & 1 deletion sources/drivers/power/acpi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub fn init() bool
var dsdt_length: u32 = (@as(*const u32, @ptrFromInt(@intFromPtr(facp.DSDT) + 1))).* - 36;
while(dsdt_length > 0)
{
if(kernel.memory.memcmp(S5_addr, "_S5_", 4) == 0)
if(libk.memory.memcmp(S5_addr, "_S5_", 4) == 0)
break;
S5_addr += 1;
dsdt_length -= 1;
Expand Down
14 changes: 7 additions & 7 deletions sources/drivers/power/xsdt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ pub const XSDP = struct

pub fn checkXSDP(ptr: *u32) !*u32
{
kernel.logs.klogln("caca");
kernel.logs.klogNb(@sizeOf(rsdt.RSDP));
kernel.logs.klog("\n");
kernel.logs.klogNb(@sizeOf(XSDP));
kernel.logs.klog("\n");
// kernel.logs.klogln("caca");
// kernel.logs.klogNb(@sizeOf(rsdt.RSDP));
// kernel.logs.klog("\n");
// kernel.logs.klogNb(@sizeOf(XSDP));
// kernel.logs.klog("\n");
const xsdp: *XSDP = @as(*XSDP, @ptrCast(ptr));
var check: u32 = 0;
// Check checksum of xsdp
const bptr: [*]u8 = @ptrCast(ptr);
for(@sizeOf(rsdt.RSDP)..@sizeOf(XSDP)) |i|
check += bptr[i]; // Possible kernel panic if overflow here
kernel.logs.klogNb(check);
kernel.logs.klog("caca\n");
// kernel.logs.klogNb(check);
// kernel.logs.klog("caca\n");
if(@as(u8, @truncate(check)) == 0)
return @ptrFromInt(xsdp.xsdt_address);
return error.XSDPNotFound;
Expand Down
47 changes: 23 additions & 24 deletions sources/drivers/vga/vga.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Screen = struct
curr_bg : Color = Color.BLACK,
curr_fg : Color = Color.WHITE,
color: u8 = computeColor(Color.WHITE, Color.BLACK),
pointer: u16 = 0,
pointer: u16 = 0,
buffer : [8000]u16 = [_]u16{getVal(' ', computeColor(Color.WHITE, Color.BLACK))} ** 8000,
};

Expand Down Expand Up @@ -107,41 +107,41 @@ pub fn changeScreen(targetScreen: u8) void
fn updateNavbar() void
{
vga.color = vga.nav_color;
const values = [_]u8{'1','2','3','4','5','6','7','L'};
var i : u32 = 63;
const values = [_]u8{'1','2','3','4','5','6','7','L'};
var i : u32 = 63;
for(values) |c|
{
putCharAt(' ', i, 0);
i += 1;
i += 1;
if(@as(u8, @truncate((i - 63) / 2)) == vga.currentScreen)
{
vga.color = vga.nav_triggered_color;
putCharAt(c, i, 0);
vga.color = vga.nav_color;
}
else
putCharAt(c, i, 0);
i += 1;
else
putCharAt(c, i, 0);
i += 1;
}
putCharAt(' ', i, 0);
putCharAt(' ', i, 0);
vga.color = computeColor(vga.curr_fg, vga.curr_bg);
}

pub fn moveCursor(dir : u8) void
{
if (dir == 75 and vga.x > 0)
vga.x -= 1;
if (dir == 72 and vga.y > 1)
vga.y -= 1;
if (dir == 77 and vga.x < vga.width - 1)
vga.x += 1;
if (dir == 80 and vga.y < vga.height)
vga.y += 1;
if (dir == 72 and vga.y == 1)
reverseScroll();
if (dir == 80 and vga.y == vga.height)
scroll();
updateCursor();
if (dir == 75 and vga.x > 0)
vga.x -= 1;
if (dir == 72 and vga.y > 1)
vga.y -= 1;
if (dir == 77 and vga.x < vga.width - 1)
vga.x += 1;
if (dir == 80 and vga.y < vga.height)
vga.y += 1;
if (dir == 72 and vga.y == 1)
reverseScroll();
if (dir == 80 and vga.y == vga.height)
scroll();
updateCursor();
}

pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_color : u8) void
Expand All @@ -168,12 +168,11 @@ pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_c
vga.nav_triggered_color = triggered_color;
vga.color = computeColor(vga.curr_fg, vga.curr_bg);
vga.y = 1;
for (80..1999) |i|
vga.buffer[i] = getVal(' ', computeColor(Color.WHITE, Color.BLACK));
for (80..1999) |i|
vga.buffer[i] = getVal(' ', computeColor(Color.WHITE, Color.BLACK));
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
6 changes: 0 additions & 6 deletions sources/kernel/arch/x86/arch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,3 @@ pub fn disableInts() void
{
asm volatile("cli");
}

pub fn init() void
{
idt.idtInit();
gdt.gdtInit();
}
28 changes: 25 additions & 3 deletions sources/kernel/arch/x86/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,44 @@ comptime
);
}

const multiboot = @import("multiboot.zig");
const boot = @import("../../boot.zig");

pub export var kernel_stack: [32 * 1024]u8 align(16) linksection(".bss") = undefined;
pub export var user_stack: [64 * 1024]u8 align(16) linksection(".bss") = undefined;

extern fn kmain() void;
var multiboot_info_addr: u32 = 0;

export fn _start() align(16) linksection(".text.boot") callconv(.Naked) noreturn
{
// Setup the stack and call kernel
// Get multiboot info address
multiboot_info_addr = asm
(
\\ mov %%ebx, %[res]
: [res] "=r" (-> u32)
);
// Setup the stack and call x86 init
asm volatile
(
\\ movl %[stk], %esp
\\ xor %ebp, %ebp
\\ call kmain
\\ call x86Init
:
: [stk] "{ecx}" (@intFromPtr(&kernel_stack) + @sizeOf(@TypeOf(kernel_stack))),
);
while(true)
asm volatile("hlt");
}

const arch = @import("arch.zig");

extern fn kmain() void;

export fn x86Init() void
{
@setCold(true);
arch.idt.idtInit();
arch.gdt.gdtInit();
multiboot.populateBootData(&boot.kboot_data, @ptrFromInt(multiboot_info_addr));
kmain();
}
68 changes: 68 additions & 0 deletions sources/kernel/arch/x86/multiboot.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const boot = @import("../../boot.zig");
const elf32 = @import("../../sys/elf32.zig");

const MULTIBOOT_INFO_ELF_SHDR = 0x00000020;
const MULTIBOOT_INFO_AOUT_SYMS = 0x00000010;

const AoutSymbolTable = extern struct
{
tabsize: u32,
strsize: u32,
addr: u32,
reserved: u32,
};

const ElfSectionHeaderTable = extern struct
{
num: u32,
size: u32,
addr: u32,
shndx: u32,
};

const MultibootInfo = extern struct
{
flags: u32,
mem_lower: u32,
mem_upper: u32,
boot_device: u32,
cmdline: u32,
mods_count: u32,
mods_addr: u32,

u: extern union
{
aout_sym: AoutSymbolTable,
elf_sec: ElfSectionHeaderTable,
},

mmap_length: u32,
mmap_addr: u32,
drives_length: u32,
drives_addr: u32,
config_table: u32,
boot_loader_name: u32,
};

pub fn populateBootData(boot_data: *boot.Boot, info: *MultibootInfo) void
{
boot_data.cmdline = @ptrFromInt(info.cmdline);
boot_data.total_mem = info.mem_lower + info.mem_upper;
if((info.flags & MULTIBOOT_INFO_ELF_SHDR) != 0)
{
boot_data.shdr = @ptrFromInt(info.u.elf_sec.addr);
boot_data.shdr_num = info.u.elf_sec.num;

for(0..boot_data.shdr_num) |i|
{
const shdr: *elf32.SectionHeader = &boot_data.shdr.?[i];
if(shdr.sh_type == elf32.SHT_SYMTAB)
{
boot_data.symtab = @ptrCast(shdr);
boot_data.symtab_num = shdr.sh_size / @sizeOf(elf32.Symbol);
}
if(shdr.sh_type == elf32.SHT_STRTAB and boot_data.strtab == null)
boot_data.strtab = shdr;
}
}
}
17 changes: 17 additions & 0 deletions sources/kernel/boot.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const elf32 = @import("sys/elf32.zig");

pub const Boot = struct
{
cmdline: [*:0]const u8 = undefined,
total_mem: usize = 0,

shdr: ?[*]elf32.SectionHeader = null,
shdr_num: usize = 0,

symtab: ?*elf32.SectionHeader = null,
symtab_num: usize = 0,

strtab: ?*elf32.SectionHeader = null,
};

pub var kboot_data: Boot = .{};
Loading

0 comments on commit 6a04a77

Please sign in to comment.