Skip to content

Commit

Permalink
Update to zig master and limine 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Aug 28, 2024
1 parent 0742aa6 commit bec5352
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 186 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
zig-cache/
.zig-cache/
zig-out/
*.iso
31 changes: 17 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const std = @import("std");

const arch = "x86_64";

fn concat(b: *std.Build, slices: []const []const u8) []u8 {
return std.mem.concat(b.allocator, u8, slices) catch unreachable;
}
Expand All @@ -25,35 +23,39 @@ fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
query.cpu_features_add.addFeature(@intFromEnum(Feature.soft_float));

const target = b.resolveTargetQuery(query);
const arch = @tagName(target.result.cpu.arch);
const limine = b.dependency("limine", .{});
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .path = "kernel/main.zig" },
.root_source_file = b.path("kernel/main.zig"),
.target = target,
.optimize = optimize,
.code_model = .kernel,
.strip = b.option(bool, "strip", "Strip the kernel") orelse switch (optimize) {
.Debug, .ReleaseSafe => false,
.ReleaseFast, .ReleaseSmall => true,
},
// .omit_frame_pointer = false,
// .pic = true,
});
kernel.root_module.addImport("limine", limine.module("limine"));
kernel.root_module.addImport("ubik", b.createModule(.{ .root_source_file = .{ .path = "lib/ubik.zig" } }));
kernel.root_module.addImport("ubik", b.createModule(.{ .root_source_file = b.path("lib/ubik.zig") }));
kernel.pie = true;
kernel.setLinkerScriptPath(.{
.path = concat(b, &[_][]const u8{ "kernel/linker-", arch, ".ld" }),
});
kernel.root_module.red_zone = false;
// kernel.root_module.stack_check = false;
// kernel.want_lto = false;
kernel.setLinkerScriptPath(b.path(concat(b, &[_][]const u8{ "kernel/linker-", arch, ".ld" })));

return kernel;
}

fn findModules(b: *std.Build) []const u8 {
var modules = std.ArrayList([]const u8).init(b.allocator);
const config = @embedFile("limine.cfg");
const config = @embedFile("limine.conf");
var iter = std.mem.splitAny(u8, config, &std.ascii.whitespace);

while (iter.next()) |line| {
if (std.mem.startsWith(u8, line, "MODULE_PATH=boot://") and
if (std.mem.startsWith(u8, line, "module_path: boot():") and
!std.mem.endsWith(u8, line, ".tar"))
{
const i = std.mem.lastIndexOfScalar(u8, line, '/') orelse unreachable;
Expand All @@ -78,7 +80,7 @@ fn buildImage(b: *std.Build, image_name: []const u8) *std.Build.Step.Run {
"make -C limine && ",
"mkdir -p ", image_dir, " && ",
"tar -cf ", image_dir, "base.tar base && ",
"cp zig-out/bin/kernel.elf limine.cfg limine/limine-bios.sys ",
"cp zig-out/bin/kernel.elf limine.conf limine/limine-bios.sys ",
"limine/limine-bios-cd.bin limine/limine-uefi-cd.bin ",
findModules(b), image_dir, " && ",
"mkdir -p ", image_dir, "EFI/BOOT && ",
Expand All @@ -101,6 +103,7 @@ pub fn build(b: *std.Build) void {
const kernel = buildKernel(b);
b.installArtifact(kernel);

const arch = @tagName(kernel.rootModuleTarget().cpu.arch);
const image_name = concat(b, &[_][]const u8{ "ubik-", arch, ".iso" });
const image_step = b.step("image", "Build the image");
const image_cmd = buildImage(b, image_name);
Expand Down Expand Up @@ -135,8 +138,8 @@ pub fn build(b: *std.Build) void {
const fmt_step = b.step("fmt", "Format all source files");
fmt_step.dependOn(&b.addFmt(.{ .paths = &[_][]const u8{ "kernel", "lib" } }).step);

const clean_step = b.step("clean", "Delete all artifacts created by zig build");
clean_step.dependOn(&b.addRemoveDirTree("zig-cache").step);
clean_step.dependOn(&b.addRemoveDirTree("zig-out").step);
clean_step.dependOn(&b.addRemoveDirTree(image_name).step);
const clean_step = b.step("clean", "Remove build artifacts");
clean_step.dependOn(&b.addRemoveDirTree(b.path(".zig-cache")).step);
clean_step.dependOn(&b.addRemoveDirTree(b.path("zig-out")).step);
clean_step.dependOn(&b.addRemoveDirTree(b.path(image_name)).step);
}
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
.version = "0.1.0",
.dependencies = .{
.limine = .{
.url = "https://github.com/limine-bootloader/limine-zig/archive/master.tar.gz",
.hash = "1220889cec612d653c31a4063d20daa9ec382ac23f76e9cc60a778b0ad17377bafa1",
.url = "https://github.com/48cf/limine-zig/archive/master.tar.gz",
.hash = "1220f946f839eab2ec49dca1c805ce72ac3e3ef9c47b3afcdecd1c05a7b35f66d277",
},
},
.paths = .{
Expand Down
32 changes: 16 additions & 16 deletions kernel/TTY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ pub const Callback = enum(u64) {
};

const Color = enum(u32) {
black = 0x00000000,
red = 0x00aa0000,
green = 0x0000aa00,
brown = 0x00aa5500,
blue = 0x000000aa,
magenta = 0x00aa00aa,
cyan = 0x0000aaaa,
grey = 0x00aaaaaa,
bright_black = 0x00555555,
bright_red = 0x00ff5555,
bright_green = 0x0055ff55,
bright_brown = 0x00ffff55,
bright_blue = 0x005555ff,
bright_magenta = 0x00ff55ff,
bright_cyan = 0x0055ffff,
bright_grey = 0x00ffffff,
black = 0x000000,
red = 0xaa0000,
green = 0x00aa00,
brown = 0xaa5500,
blue = 0x0000aa,
magenta = 0xaa00aa,
cyan = 0x00aaaa,
grey = 0xaaaaaa,
bright_black = 0x555555,
bright_red = 0xff5555,
bright_green = 0x55ff55,
bright_brown = 0xffff55,
bright_blue = 0x5555ff,
bright_magenta = 0xff55ff,
bright_cyan = 0x55ffff,
bright_grey = 0xffffff,
_,
};

Expand Down
4 changes: 2 additions & 2 deletions kernel/arch/x86_64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub const Context = idt.Context;
const mem = @import("x86_64/mem.zig");

comptime {
@export(mem.cpy, .{ .name = "memcpy", .linkage = .Weak, .visibility = .default });
@export(mem.set, .{ .name = "memset", .linkage = .Weak, .visibility = .default });
@export(&mem.cpy, .{ .name = "memcpy", .linkage = .weak, .visibility = .default });
@export(&mem.set, .{ .name = "memset", .linkage = .weak, .visibility = .default });
}

pub fn init() void {
Expand Down
38 changes: 22 additions & 16 deletions kernel/arch/x86_64/idt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ const exceptions = [_][]const u8{
"Reserved",
};

const page_fault_vector = 0x0e;
pub var panic_ipi_vector: u8 = undefined;
var next_vector: u8 = exceptions.len;

// TODO: replace isr with a interrupt dispatcher func?
// -> move all handlers to interrupt.zig?
var isr = [_]InterruptHandler{defaultHandler} ** 256;
var idt: IDT = .{};

var next_vector: u8 = exceptions.len;

pub fn init() void {
idt.descriptor.base = @intFromPtr(&idt.entries);

Expand All @@ -126,7 +126,7 @@ pub fn init() void {
idt.entries[vector] = IDT.Entry.init(@intFromPtr(handler), 0);
}

setIST(0x0e, 2); // page fault uses IST 2
setIST(page_fault_vector, 2); // page fault uses IST 2
panic_ipi_vector = allocVector();
idt.entries[panic_ipi_vector] = IDT.Entry.init(@intFromPtr(&panicHandler), 0);

Expand All @@ -143,7 +143,7 @@ pub fn reload() void {
}

pub fn allocVector() u8 {
const vector = @atomicRmw(u8, &next_vector, .Add, 1, .AcqRel);
const vector = @atomicRmw(u8, &next_vector, .Add, 1, .acq_rel);
// 0xf0 in a non maskable interrupt from APIC
if (vector == 0xf0) {
@panic("IDT exhausted");
Expand Down Expand Up @@ -251,13 +251,22 @@ fn makeHandler(comptime vector: u8) *const fn () callconv(.Naked) void {
}.handler;
}

export fn commonStub() callconv(.Naked) void {
asm volatile (
inline fn swapgs() void {
// if (cs != gdt.kernel_code) -> swapgs
asm volatile (
\\cmpq %[kcode], 0x18(%rsp)
\\je 1f
\\swapgs
\\1:
:
: [kcode] "i" (gdt.kernel_code),
);
}

export fn commonStub() callconv(.Naked) void {
swapgs();

asm volatile (
\\push %r15
\\push %r14
\\push %r13
Expand All @@ -277,10 +286,9 @@ export fn commonStub() callconv(.Naked) void {
\\push %rax
\\mov %ds, %ax
\\push %rax
:
: [kcode] "i" (gdt.kernel_code),
);

// cld?
asm volatile (
\\mov 0x88(%rsp), %rdi
\\imul $8, %rdi
Expand Down Expand Up @@ -311,15 +319,13 @@ export fn commonStub() callconv(.Naked) void {
\\pop %r13
\\pop %r14
\\pop %r15
// if (cs != gdt.kernel_code) -> swapgs
\\cmpq %[kcode], 0x18(%rsp)
\\je 1f
\\swapgs
\\1:
// restore stack
);

swapgs();

// restore stack and return from interrupt
asm volatile (
\\add $16, %rsp
\\iretq
:
: [kcode] "i" (gdt.kernel_code),
);
}
4 changes: 2 additions & 2 deletions kernel/arch/x86_64/mem.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub fn cpy(noalias dst: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

// cld?
asm volatile (
\\cld
\\rep movsb
:
: [_] "{rdi}" (dst),
Expand All @@ -17,8 +17,8 @@ pub fn cpy(noalias dst: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(
pub fn set(dst: ?[*]u8, c: u8, len: usize) callconv(.C) ?[*]u8 {
@setRuntimeSafety(false);

// cld?
asm volatile (
\\cld
\\rep stosb
:
: [_] "{rdi}" (dst),
Expand Down
Loading

0 comments on commit bec5352

Please sign in to comment.