Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
I don't know what are those changes to elf.zig.
  • Loading branch information
Ratakor committed Oct 26, 2024
1 parent 02262be commit b07b45a
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 251 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
A kernel

# TODO
- Rework VMM -> rework sched -> work on VFS -> work on ELF
- Add a checklist/roadmap
- Move tty and drivers out of kernel space
- Replace json with zon
Expand All @@ -10,7 +11,7 @@ A kernel
- Support RISC-V64, aarch64 and x86_64
- Replace @import("root") with @import("main.zig") to allow for testing
- Replace limine with a custom bootloader?
- write core in zig and the rest in nov?
- write core in zig and the rest in nov

# Clone, build and run
Make sure to have `zig master`, `xorriso` and `qemu-system-x86` then run
Expand All @@ -22,7 +23,7 @@ Make sure to have `zig master`, `xorriso` and `qemu-system-x86` then run

# File structure
This shouldn't be in readme.
TODO: move init function at the end of file?
TODO: move init function at the end of file (or top?)

1. imports
2. type definitions
Expand Down
20 changes: 8 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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;
return std.mem.concat(b.allocator, u8, slices) catch @panic("OOM");
}

fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
Expand Down Expand Up @@ -40,7 +40,8 @@ fn buildKernel(b: *std.Build) *std.Build.Step.Compile {

const target = b.resolveTargetQuery(target_query);
const optimize = b.standardOptimizeOption(.{});
const limine = b.dependency("limine", .{});
const limine = b.dependency("limine", .{}).module("limine");
const ubik = b.createModule(.{ .root_source_file = b.path("lib/ubik.zig") });
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = b.path("kernel/main.zig"),
Expand All @@ -54,8 +55,8 @@ fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
// .omit_frame_pointer = false,
// .pic = true,
});
kernel.root_module.addImport("limine", limine.module("limine"));
kernel.root_module.addImport("ubik", b.createModule(.{ .root_source_file = b.path("lib/ubik.zig") }));
kernel.root_module.addImport("limine", limine);
kernel.root_module.addImport("ubik", ubik);
kernel.pie = true;
kernel.root_module.red_zone = false;
kernel.root_module.stack_check = false;
Expand All @@ -79,16 +80,11 @@ fn findModules(b: *std.Build) []const u8 {
}
}

var modules_str: []const u8 = "";
for (modules.items) |module| {
modules_str = concat(b, &[_][]const u8{ modules_str, module, " " });
}

return modules_str;
return std.mem.join(b.allocator, " ", modules.items) catch @panic("OOM");
}

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;
const image_dir = ".zig-cache/image_root/";

// zig fmt: off
const image_params = &[_][]const u8{
Expand All @@ -110,7 +106,7 @@ fn buildImage(b: *std.Build, image_name: []const u8) *std.Build.Step.Run {
image_dir, " -o ", image_name, " && ",
"./limine/limine bios-install ", image_name, " && ",
"rm -rf ", image_dir,
})
}),
};
// zig fmt: on

Expand Down
2 changes: 1 addition & 1 deletion kernel/TTY.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ saved_state_current_charset: usize,
saved_state_current_fg: ?usize,
saved_state_current_bg: ?usize,

// TODO: lock? also don't forget to init it
// TODO: lock?
// read_lock: SpinLock,
// write_lock: SpinLock,

Expand Down
1 change: 0 additions & 1 deletion kernel/acpi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ pub fn shutdown() noreturn {
unreachable;
}

// TODO: does this get optimized away correctly?
inline fn parseInt(s5_addr: []const u8, value: *u64) usize {
switch (s5_addr[0]) {
0x0 => {
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/x86_64/idt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ export fn commonStub() callconv(.Naked) void {
\\push %rax
);

// cld?
asm volatile (
\\cld
\\mov 0x88(%rsp), %rdi
\\imul $8, %rdi
\\add %rdi, %rax
Expand Down
10 changes: 3 additions & 7 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ const source_files = [_][]const u8{
"fs/zero.zig",
"lib/lock.zig",
"lib/term.zig",
"lib/tree.zig",
"mm/pmm.zig",
"mm/vmm.zig",
"acpi.zig",
Expand All @@ -195,17 +194,14 @@ const source_files = [_][]const u8{

// TODO: get source files from filesystem instead + zig std lib files
fn printLineFromFile(writer: anytype, source_location: std.debug.SourceLocation) !void {
var contents: []const u8 = undefined;

inline for (source_files) |src_path| {
const content = inline for (source_files) |src_path| {
if (std.mem.endsWith(u8, source_location.file_name, src_path)) {
contents = @embedFile(src_path);
break;
break @embedFile(src_path);
}
} else return error.FileNotFound;

var line: usize = 1;
for (contents) |byte| {
for (content) |byte| {
if (line == source_location.line) {
try writer.writeByte(byte);
if (byte == '\n') return;
Expand Down
51 changes: 31 additions & 20 deletions kernel/elf.zig
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
const std = @import("std");
const root = @import("root");
const arch = @import("arch.zig");
const pmm = root.pmm;
const vmm = root.vmm;
const page_size = std.mem.page_size;

// TODO: could std.elf be useful?

pub const AuxVal = struct {
entry: u64,
phdr: u64,
phent: u64,
phnum: u64,
};

pub fn load(elf_addr: u64, addr_space: *vmm.AddressSpace, load_base: u64, ld_path: *[]u8) !AuxVal {
pub fn load(elf_addr: u64, addr_space: *vmm.AddressSpace, load_base: u64) !AuxVal {
const header = try std.elf.Header.parse(@ptrFromInt(elf_addr));

if (!header.is_64 or header.endian != .Little or header.machine != .X86_64) {
if (!header.is_64 or header.endian != arch.endian or header.machine != .X86_64) {
return error.InvalidElf;
}

var auxv: AuxVal = undefined;
var iter = header.program_header_iterator();
while (try iter.next()) |phdr| switch (phdr.p_type) {
// var iter = header.program_header_iterator();
// while (try iter.next()) |phdr| switch (phdr.p_type) {
const ph_tbl = @as([*]std.elf.Elf64_Phdr, @ptrFromInt(elf_addr + header.phoff))[0..header.phnum];
for (ph_tbl) |phdr| switch (phdr.p_type) {
std.elf.PT_LOAD => {
var prot: i32 = vmm.PROT.READ;
if (phdr.p_flags & std.elf.PF_W) {
prot |= vmm.PROT.WRITE;
var prot: i32 = std.os.PROT.READ;
if (phdr.p_flags & std.elf.PF_W != 0) {
prot |= std.os.PROT.WRITE;
}
if (phdr.p_flags & std.elf.PF_X) {
prot |= vmm.PROT.EXEC;
if (phdr.p_flags & std.elf.PF_X != 0) {
prot |= std.os.PROT.EXEC;
}

const misalign = phdr.p_vaddr & (page_size - 1);
const page_count = std.math.divCeil(phdr.p_memsz + misalign, page_size);
std.log.debug("a {} {}", .{ misalign, phdr.p_vaddr });
const page_count = try std.math.divCeil(usize, phdr.p_memsz + misalign, page_size);

const paddr = pmm.alloc(page_count, true) orelse return error.OutOfMemory;
errdefer pmm.free(paddr, page_count);
Expand All @@ -40,25 +46,30 @@ pub fn load(elf_addr: u64, addr_space: *vmm.AddressSpace, load_base: u64, ld_pat
paddr,
page_count * page_size,
prot,
vmm.MAP.ANONYMOUS,
std.os.MAP.ANONYMOUS,
);

const dst: [*]u8 = @ptrFromInt(phdr.p_vaddr);
const src: [*]u8 = @ptrFromInt(elf_addr + phdr.p_offset);
@memcpy(dst[0..phdr.p_filesz], src[0..phdr.p_filesz]);
@memset(dst[phdr.p_filesz..phdr.p_memsz], 0);

// TODO: read + map file
// try res.read(null, phys + misalign + VMM_HIGHER_HALF, phdr.p_offset, phdr.p_filesz);
},
std.elf.PT_PHDR => {
auxv.phdr = phdr.p_vaddr + load_base;
},
std.elf.PT_INTERP => {
if (ld_path) |ldp| {
root.allocator.allocSentinel(u8, phdr.p_filesz, 0);
const path = try root.allocator.alloc(u8, phdr.p_filesz + 1);
errdefer root.allocator.free(path);
// TODO
// try res.read(null, @intFromPtr(path), phdr.p_offset, phdr.p_filesz);
ldp = path;
@panic("Not Implemented");
}
// if (ld_path) |ldp| {
// root.allocator.allocSentinel(u8, phdr.p_filesz, 0);
// const path = try root.allocator.alloc(u8, phdr.p_filesz + 1);
// errdefer root.allocator.free(path);
// // TODO
// // try res.read(null, @intFromPtr(path), phdr.p_offset, phdr.p_filesz);
// ldp = path;
// @panic("Not Implemented");
// }
},
else => {},
};
Expand Down
1 change: 0 additions & 1 deletion kernel/lib.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub const SpinLock = @import("lib/lock.zig").SpinLock;
pub const Tree = @import("lib/tree.zig").Tree;
pub const term = @import("lib/term.zig");
Loading

0 comments on commit b07b45a

Please sign in to comment.