Skip to content

Commit

Permalink
Update to zig master and switch ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Sep 24, 2024
1 parent 31c3b6e commit 4b39e6f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
submodules: true

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
uses: mlugg/setup-zig@v1
with:
version: master

Expand All @@ -45,9 +45,9 @@ jobs:
uses: actions/checkout@v4

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
uses: mlugg/setup-zig@v1
with:
version: master

- name: Lint
run: zig fmt --check kernel/*.zig lib/*.zig
run: zig fmt --check kernel/*.zig lib/*.zig build.zig
22 changes: 21 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

fn concat(b: *std.Build, slices: []const []const u8) []u8 {
return std.mem.concat(b.allocator, u8, slices) catch unreachable;
Expand Down Expand Up @@ -89,6 +90,7 @@ fn findModules(b: *std.Build) []const u8 {
fn buildImage(b: *std.Build, image_name: []const u8) *std.Build.Step.Run {
const image_dir = b.cache_root.join(b.allocator, &[_][]const u8{"image_root/"}) catch unreachable;

// zig fmt: off
const image_params = &[_][]const u8{
"/bin/sh", "-c",
concat(b, &[_][]const u8{
Expand All @@ -110,11 +112,22 @@ fn buildImage(b: *std.Build, image_name: []const u8) *std.Build.Step.Run {
"rm -rf ", image_dir,
})
};
// zig fmt: on

return b.addSystemCommand(image_params);
}

pub fn build(b: *std.Build) void {
comptime {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.14.0-dev.1637+8c232922b") catch unreachable;
if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint(
\\Your zig version ({}) does not meet the minimum required version ({})
, .{ current_zig, min_zig }));
}
}

const kernel = buildKernel(b);
b.installArtifact(kernel);

Expand All @@ -127,6 +140,7 @@ pub fn build(b: *std.Build) void {

const run_step = b.step("run", "Run the image with qemu");
const nodisplay = b.option(bool, "nodisplay", "Disable display for qemu") orelse false;
// zig fmt: off
const run_cmd = b.addSystemCommand(&[_][]const u8{
concat(b, &[_][]const u8{ "qemu-system-", arch }),
"-no-reboot",
Expand All @@ -135,11 +149,13 @@ pub fn build(b: *std.Build) void {
"-m", "1G",
"-smp", "4",
// "-d", "int,guest_errors",
// "-s", "-S",
"-boot", "d",
"-vga", "std",
"-display", if (nodisplay) "none" else "gtk",
"-cdrom", image_name
});
// zig fmt: on
run_cmd.step.dependOn(image_step);
run_step.dependOn(&run_cmd.step);

Expand All @@ -151,7 +167,11 @@ pub fn build(b: *std.Build) void {
}).step);

const fmt_step = b.step("fmt", "Format all source files");
fmt_step.dependOn(&b.addFmt(.{ .paths = &[_][]const u8{ "kernel", "lib" } }).step);
fmt_step.dependOn(&b.addFmt(.{ .paths = &[_][]const u8{
"kernel",
"lib",
"build.zig",
} }).step);

const clean_step = b.step("clean", "Remove build artifacts");
clean_step.dependOn(&b.addRemoveDirTree(b.path(".zig-cache")).step);
Expand Down
5 changes: 1 addition & 4 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ fn callback(tty: *TTY, cb: TTY.Callback, arg1: u64, arg2: u64, arg3: u64) void {
export fn _start() noreturn {
arch.disableInterrupts();

if (!base_revision.is_supported()) {
unreachable;
}

std.debug.assert(base_revision.is_supported());
serial.init();
arch.init();
pmm.init();
Expand Down
30 changes: 15 additions & 15 deletions kernel/mm/vmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ pub const PTE = packed struct(u64) {
protection_key: u4,
execute_disable: bool,

const present: u64 = 1 << 0;
const writable: u64 = 1 << 1;
const user: u64 = 1 << 2;
const noexec: u64 = 1 << 63;
const PRESENT: u64 = 1 << 0;
const WRITABLE: u64 = 1 << 1;
const USER: u64 = 1 << 2;
const NOEXEC: u64 = 1 << 63;

pub inline fn getAddress(self: PTE) u64 {
return @as(u64, self.address) << 12;
Expand All @@ -68,7 +68,7 @@ pub const PTE = packed struct(u64) {
}

const new_page_table = pmm.alloc(1, true) orelse return error.OutOfMemory;
self.* = @bitCast(new_page_table | present | writable | user);
self.* = @bitCast(new_page_table | PRESENT | WRITABLE | USER);
return @ptrFromInt(new_page_table + hhdm_offset);
}
};
Expand Down Expand Up @@ -475,15 +475,15 @@ pub fn init() void {
_ = kaddr_space.pml4[i].getNextLevel(true) catch unreachable;
}

kaddr_space.mapSection("text", PTE.present);
kaddr_space.mapSection("rodata", PTE.present | PTE.noexec);
kaddr_space.mapSection("data", PTE.present | PTE.writable | PTE.noexec);
kaddr_space.mapSection("text", PTE.PRESENT);
kaddr_space.mapSection("rodata", PTE.PRESENT | PTE.NOEXEC);
kaddr_space.mapSection("data", PTE.PRESENT | PTE.WRITABLE | PTE.NOEXEC);

// map the first 4 GiB
var addr: u64 = 0x1000;
while (addr < 0x100000000) : (addr += page_size) {
kaddr_space.mapPage(addr, addr, PTE.present | PTE.writable) catch unreachable;
kaddr_space.mapPage(addr + hhdm_offset, addr, PTE.present | PTE.writable | PTE.noexec) catch unreachable;
kaddr_space.mapPage(addr, addr, PTE.PRESENT | PTE.WRITABLE) catch unreachable;
kaddr_space.mapPage(addr + hhdm_offset, addr, PTE.PRESENT | PTE.WRITABLE | PTE.NOEXEC) catch unreachable;
}

// map the rest of the memory map
Expand All @@ -499,8 +499,8 @@ pub fn init() void {
while (i < top) : (i += page_size) {
if (i < 0x100000000) continue;

kaddr_space.mapPage(i, i, PTE.present | PTE.writable) catch unreachable;
kaddr_space.mapPage(i + hhdm_offset, i, PTE.present | PTE.writable | PTE.noexec) catch unreachable;
kaddr_space.mapPage(i, i, PTE.PRESENT | PTE.WRITABLE) catch unreachable;
kaddr_space.mapPage(i + hhdm_offset, i, PTE.PRESENT | PTE.WRITABLE | PTE.NOEXEC) catch unreachable;
}
}

Expand Down Expand Up @@ -533,12 +533,12 @@ pub fn handlePageFault(cr2: u64, reason: PageFaultError) !void {
}

fn mmapPageInRange(global: *MMapRangeGlobal, vaddr: u64, paddr: u64, prot: i32) !void {
var flags = PTE.present | PTE.user;
var flags = PTE.PRESENT | PTE.USER;
if ((prot & ubik.PROT.WRITE) != 0) {
flags |= PTE.writable;
flags |= PTE.WRITABLE;
}
if ((prot & ubik.PROT.EXEC) == 0) {
flags |= PTE.noexec;
flags |= PTE.NOEXEC;
}

try global.shadow_addr_space.mapPage(vaddr, paddr, flags);
Expand Down
6 changes: 3 additions & 3 deletions kernel/vfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ pub const Node = struct {
return self.vtable.truncate(self);
}

pub inline fn stat(self: *Node, statbuf: *ubik.Stat) StatError!void {
return self.vtable.stat(self, statbuf);
}
// pub inline fn stat(self: *Node, statbuf: *ubik.Stat) StatError!void {
// return self.vtable.stat(self, statbuf);
// }

pub fn writePath(self: *Node, writer: anytype) !void {
if (self.parent) |parent| {
Expand Down

0 comments on commit 4b39e6f

Please sign in to comment.