Skip to content

Commit

Permalink
Update to latest zig
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Feb 19, 2024
1 parent 5bdac6a commit a66c319
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
39 changes: 20 additions & 19 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
}

fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
const optimize = b.standardOptimizeOption(.{});
var target: std.zig.CrossTarget = .{
var query: std.Target.Query = .{
.cpu_arch = .x86_64,
.os_tag = .freestanding,
.abi = .none,
Expand All @@ -15,31 +17,32 @@ fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
// Disable CPU features that require additional initialization
// like MMX, SSE/2 and AVX. That requires us to enable the soft-float feature.
const Feature = std.Target.x86.Feature;
target.cpu_features_sub.addFeature(@intFromEnum(Feature.mmx));
target.cpu_features_sub.addFeature(@intFromEnum(Feature.sse));
target.cpu_features_sub.addFeature(@intFromEnum(Feature.sse2));
target.cpu_features_sub.addFeature(@intFromEnum(Feature.avx));
target.cpu_features_sub.addFeature(@intFromEnum(Feature.avx2));
target.cpu_features_add.addFeature(@intFromEnum(Feature.soft_float));

query.cpu_features_sub.addFeature(@intFromEnum(Feature.mmx));
query.cpu_features_sub.addFeature(@intFromEnum(Feature.sse));
query.cpu_features_sub.addFeature(@intFromEnum(Feature.sse2));
query.cpu_features_sub.addFeature(@intFromEnum(Feature.avx));
query.cpu_features_sub.addFeature(@intFromEnum(Feature.avx2));
query.cpu_features_add.addFeature(@intFromEnum(Feature.soft_float));

const target = b.resolveTargetQuery(query);
const limine = b.dependency("limine", .{});
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .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,
},
});
kernel.code_model = .kernel;
kernel.addModule("limine", limine.module("limine"));
kernel.addModule("ubik", b.createModule(.{ .source_file = .{ .path = "lib/ubik.zig" } }));
kernel.root_module.addImport("limine", limine.module("limine"));
kernel.root_module.addImport("ubik", b.createModule(.{ .root_source_file = .{ .path = "lib/ubik.zig" } }));
kernel.pie = true;
kernel.setLinkerScriptPath(.{
.path = concat(b, &[_][]const u8{ "kernel/linker-", @tagName(target.cpu_arch.?), ".ld" }),
.path = concat(b, &[_][]const u8{ "kernel/linker-", arch, ".ld" }),
});
kernel.pie = true;
kernel.strip = b.option(bool, "strip", "Strip the kernel") orelse switch (optimize) {
.Debug, .ReleaseSafe => false,
.ReleaseFast, .ReleaseSmall => true,
};

return kernel;
}
Expand Down Expand Up @@ -98,9 +101,7 @@ pub fn build(b: *std.Build) void {
const kernel = buildKernel(b);
b.installArtifact(kernel);

const arch = @tagName(kernel.target.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);
image_cmd.step.dependOn(b.getInstallStep());
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.dependencies = .{
.limine = .{
.url = "https://github.com/limine-bootloader/limine-zig/archive/master.tar.gz",
.hash = "12202809180bab2c7ae3382781b2fc65395e74b49d99ff2595f3fea9f7cf66cfa963",
.hash = "1220889cec612d653c31a4063d20daa9ec382ac23f76e9cc60a778b0ad17377bafa1",
},
},
.paths = .{
Expand Down
6 changes: 3 additions & 3 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const lib = @import("lib.zig");
pub usingnamespace lib;

pub const panic = debug.panic;
pub const std_options = struct {
pub const log_level = if (builtin.mode == .Debug) .debug else .info;
pub const logFn = debug.log;
pub const std_options: std.Options = .{
.log_level = if (builtin.mode == .Debug) .debug else .info,
.logFn = debug.log,
};

pub const os = struct {
Expand Down
11 changes: 5 additions & 6 deletions kernel/mm/vmm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const log = std.log.scoped(.vmm);
const alignBackward = std.mem.alignBackward;
const alignForward = std.mem.alignForward;
const page_size = std.mem.page_size;
const MAP = std.os.MAP;

// TODO: mapRange/unmapRange?
// TODO: move paging code to paging.zig
Expand Down Expand Up @@ -106,7 +105,7 @@ const MMapRangeLocal = struct {
length: usize,
offset: std.os.off_t,
prot: i32,
flags: i32,
flags: std.os.MAP,
};

// TODO: improve
Expand Down Expand Up @@ -194,7 +193,7 @@ pub const AddressSpace = struct {

try new_addr_space.mmap_ranges.append(root.allocator, new_local_range);

if (local_range.flags & MAP.SHARED != 0) {
if (local_range.flags.TYPE == std.os.system.MAP_TYPE.SHARED) {
try global_range.locals.append(root.allocator, new_local_range);
var i = local_range.base;
while (i < local_range.base + local_range.length) : (i += page_size) {
Expand All @@ -215,7 +214,7 @@ pub const AddressSpace = struct {
try new_global_range.locals.append(root.allocator, new_local_range);
errdefer new_global_range.locals.deinit(root.allocator);

if (local_range.flags & MAP.ANONYMOUS == 0) {
if (local_range.flags.ANONYMOUS == false) {
@panic("Non anonymous fork");
}

Expand Down Expand Up @@ -436,7 +435,7 @@ pub const AddressSpace = struct {
self.lock.unlock();

if (snip_size == local_range.length and global_range.locals.items.len == 1) {
if (local_range.flags & MAP.ANONYMOUS != 0) {
if (local_range.flags.ANONYMOUS) {
var k = global_range.base;
while (k < global_range.base + global_range.length) : (k += page_size) {
const paddr = global_range.shadow_addr_space.virt2phys(j) catch continue;
Expand Down Expand Up @@ -524,7 +523,7 @@ pub fn handlePageFault(cr2: u64, reason: PageFaultError) !void {
// TODO: enable interrupts?

const local_range = range.range;
const page = if (local_range.flags & MAP.ANONYMOUS != 0)
const page = if (local_range.flags.ANONYMOUS)
pmm.alloc(1, true) orelse return error.OutOfMemory
else
@panic("TODO: map files");
Expand Down
4 changes: 2 additions & 2 deletions kernel/sched.zig
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ pub const Thread = struct {
}
};

/// reschedule every 5ms
const quantum = 5_000;
/// reschedule every 50ms
const quantum = 50_000;

pub var kernel_process: *Process = undefined;
pub var processes: std.ArrayListUnmanaged(*Process) = .{}; // TODO: hashmap with pid?
Expand Down
1 change: 1 addition & 0 deletions lib/ubik.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub const T = linux.T;
pub const S = linux.S;
pub const E = linux.E;
pub const PROT = linux.PROT;
pub const MAP_TYPE = linux.MAP_TYPE;
pub const MAP = linux.MAP;
pub const DT = linux.DT;
pub const O = linux.O;
Expand Down

0 comments on commit a66c319

Please sign in to comment.