diff --git a/kernel/build.zig b/kernel/build.zig index d468e85..cfc60d5 100644 --- a/kernel/build.zig +++ b/kernel/build.zig @@ -11,6 +11,8 @@ const Target = struct { parent: ?*const Target = null, cpu_arch: ?std.Target.Cpu.Arch = null, cpu_model: ?std.build.Target.CpuModel = null, + extra_features: []const std.Target.Cpu.Feature = &[_] std.Target.Cpu.Feature {}, + forbidden_features: []const std.Target.Cpu.Feature = &[_] std.Target.Cpu.Feature {}, archdir: ?[]const u8 = null, linkscript: ?[]const u8 = null, name: []const u8, @@ -20,9 +22,21 @@ const x86 = Target{ .archdir = "x86", .name = "x86-generic", }; +const x86_64_nosse = std.Target.Cpu.Model { + .name = "x86_64-sse2+soft_float", + .llvm_name = null, + .features = std.Target.x86.featureSet(&[_] std.Target.x86.Feature {std.Target.x86.Feature.@"64bit"}) +}; const x86_64 = Target { .parent = &x86, .cpu_arch = std.Target.Cpu.Arch.x86_64, + .extra_features = &[_] std.Target.Cpu.Feature { + std.Target.x86.all_features[@enumToInt(std.Target.x86.Feature.soft_float)], + }, + .forbidden_features = &[_] std.Target.Cpu.Feature { + std.Target.x86.all_features[@enumToInt(std.Target.x86.Feature.sse)], + std.Target.x86.all_features[@enumToInt(std.Target.x86.Feature.sse2)], + }, .archdir = "x86_64", .name = "x86_64-generic", }; @@ -71,7 +85,6 @@ const Targets = enum { fn getCpuArch(t: Target) std.Target.Cpu.Arch { return t.cpu_arch orelse getCpuArch(t.parent.?.*); } - fn getCpuModel(t: Target) std.build.Target.CpuModel { if(t.cpu_model) |m| { return m; @@ -81,15 +94,35 @@ fn getCpuModel(t: Target) std.build.Target.CpuModel { return std.build.Target.CpuModel.baseline; } } - +fn getExtraFeatures(t: Target) std.Target.Cpu.Feature.Set { + var parentset = std.Target.Cpu.Feature.Set.empty; + if(t.parent) |p| { + parentset = getExtraFeatures(p.*); + } + for (t.extra_features) |bl_ft| { + parentset.addFeature(bl_ft.index); + } + return parentset; +} +fn getForbiddenFeatures(t: Target) std.Target.Cpu.Feature.Set { + var parentset = std.Target.Cpu.Feature.Set.empty; + if(t.parent) |p| { + parentset = getForbiddenFeatures(p.*); + } + for (t.forbidden_features) |bl_ft| { + parentset.addFeature(bl_ft.index); + } + return parentset; +} fn getLinkerScript(t: Target) []const u8 { return t.linkscript orelse getLinkerScript(t.parent.?.*); } - fn getTarget(t: Target) std.zig.CrossTarget { return .{ .cpu_arch = getCpuArch(t), .cpu_model = getCpuModel(t), + .cpu_features_add = getExtraFeatures(t), + .cpu_features_sub = getForbiddenFeatures(t), .os_tag = std.Target.Os.Tag.freestanding, }; } @@ -166,6 +199,7 @@ pub fn build(b: *Builder) !void { // Standard release options allow the person running `zig build` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + // b.setPreferredReleaseMode(std.builtin.Mode.ReleaseSafe); const mode = b.standardReleaseOptions(); const files = try findFiles(ktarget); @@ -174,6 +208,7 @@ pub fn build(b: *Builder) !void { const exename = try std.fmt.allocPrint(alloc, "kernel-{}", .{ktarget.name}); const exe = b.addExecutable(exename, "src/main.zig"); + exe.addIncludeDir("src"); var cfiles = std.ArrayList([]const u8).init(alloc); try cfiles.appendSlice(files.generic_c); try cfiles.appendSlice(files.archspecific_c); @@ -193,11 +228,21 @@ pub fn build(b: *Builder) !void { exe.step.dependOn(&assX.step); exe.addObjectFile(output); } + // const zigobj = b.addObject("zigkalloc", "src/allocator/calloc.zig"); + // zigobj.setTarget(getTarget(ktarget)); + // zigobj.strip = true; + // // zigobj.single_threaded = true; + // // zigobj.code_model = std.builtin.CodeModel.kernel; + // exe.addObject(zigobj); exe.setTarget(getTarget(ktarget)); exe.setBuildMode(mode); + // exe.code_model = std.builtin.CodeModel.kernel; exe.setLinkerScriptPath(getLinkerScript(ktarget)); // exe.setOutputDir("bin"); + // exe.strip = true; + // exe.single_threaded = true; + exe.install(); const run_cmd = exe.run(); diff --git a/kernel/make/x86_64/linker.ld b/kernel/make/x86_64/linker.ld index 0823817..5f394c8 100644 --- a/kernel/make/x86_64/linker.ld +++ b/kernel/make/x86_64/linker.ld @@ -12,22 +12,22 @@ SECTIONS .text ALIGN(4K) : { - KEEP(*(.text*)) + /* KEEP(*(.text*)) */ } .rodata ALIGN(4K) : { - KEEP(*(.rodata*)) + /* KEEP(*(.rodata*)) */ } .data ALIGN(4K) : { - KEEP(*(.data*)) + /* KEEP(*(.data*)) */ } .bss ALIGN(4K) : { - KEEP(*(COMMON)) - KEEP(*(.bss*)) + /* KEEP(*(COMMON)) */ + /* KEEP(*(.bss*)) */ } } diff --git a/kernel/src/allocator/calloc.zig b/kernel/src/allocator/calloc.zig new file mode 100644 index 0000000..8ec4fc9 --- /dev/null +++ b/kernel/src/allocator/calloc.zig @@ -0,0 +1,19 @@ +const std = @import("std"); + +var GlobAlloc: ?std.heap.FixedBufferAllocator = null; + +pub export fn kalloc_init(start: usize, len: usize) void { + // const aligned = std.mem.alignForward(start, 8); + const ptr = @intToPtr([*]u8, start); + const buf = ptr[0..len]; + GlobAlloc = std.heap.FixedBufferAllocator.init(buf); +} + +// export fn kalloc(bytes: usize, a: usize) void { +// var a = GlobAlloc.?.allocator.allocAdvanced(u8, a, bytes, std.mem.Allocator.Exact.at_least); +// return a catch unreachable; +// } + +pub fn kallocNop() void { + +} \ No newline at end of file diff --git a/kernel/src/main.c b/kernel/src/main.c index 318f28c..456bdcf 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -3,6 +3,10 @@ #include "hal/info.h" #include "print/print.h" + +char TESTBUF[128] = "eeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeee - gigakubica"; +char PRINTBUF[128]; + void kmain() { if(!validate_boot_info()) { PANIC("Error: boot_info not entered!"); @@ -13,6 +17,14 @@ void kmain() { printf("Usable memory: %h (at %x)\n", boot_info.usable.len, boot_info.usable.start); printf("Cmdline: %s\n", boot_info.cmdline); + // kalloc_init(boot_info.usable.start); + zigmain(); + + // memcpy(PRINTBUF, TESTBUF, 32); + // puts(PRINTBUF); + + kalloc_init(boot_info.usable.start, boot_info.usable.len); + vga_setcolor(VGA_DARK_GRAY); vga_write_elsewhere("(c) Quinten Kock 2020 (MIT License)", 24, 0); halt_catch_fire(); diff --git a/kernel/src/main.zig b/kernel/src/main.zig index 76005f3..4d24082 100644 --- a/kernel/src/main.zig +++ b/kernel/src/main.zig @@ -1,5 +1,52 @@ const std = @import("std"); +const builtin = @import("builtin"); + +pub const c_alloc = @import("allocator/calloc.zig"); + +const c = @cImport({ + @cInclude("hal/print.h"); + @cInclude("print/print.h"); + @cInclude("hal/ops.h"); + if( builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { + @cInclude("arch/x86/vga.h"); + } +}); + + + +// pub extern fn puts([*:0]const u8) void; + +pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { + @setCold(true); + if(builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { + c.vga_clear(c.VGA_BLUE); + } + + c.printf("Kernel panic: "); + for (msg) |m| { + c.putchar(m); + } + c.putchar('\n'); + if(error_return_trace) |trace| { + // c.panic("ee", "ee", 0); + c.printf("TRACE: %x %x\n", @intCast(usize, trace.*.index), trace.*.instruction_addresses.len); + } + const msgptr = @ptrCast([*c] const u8, msg); + // c.panic(msgptr, "", 0); + c.puts("HELP\n"); + while(true) {} +} + + +export fn zigmain() void { + + const message: [*:0]const u8 = "hello, zig!"; + std.log.info("All your codebase are belong to us.", .{}); + c.puts(message); + c_alloc.kallocNop(); + + var buffer: [32]u8 = undefined; + var alloc = std.heap.FixedBufferAllocator.init(&buffer); + var buf = alloc.allocator.alloc(u8, 40) catch unreachable; -pub fn zigmain() anyerror!void { - std.log.info("All your codebase are belong to us.", .{}); }