Microkernel thing OS experiment (Zig ⚡)
1pub const boot = @import("boot.zig"); 2pub const instructions = @import("instructions/root.zig"); 3pub const interrupts = @import("interrupts/root.zig"); 4pub const mm = @import("mm/root.zig"); 5pub const port = @import("port.zig"); 6pub const structures = @import("structures/root.zig"); 7pub const tsc = @import("tsc.zig"); 8pub const registers = @import("registers.zig"); 9const common = @import("common"); 10const std = @import("std"); 11 12fn pageSize() usize { 13 return 4 << 10; 14} 15 16pub const std_options: std.Options = .{ 17 .logFn = common.aux.logFn, 18 .page_size_min = 4 << 10, 19 .page_size_max = 4 << 10, 20 .queryPageSize = pageSize, 21}; 22pub const panic = std.debug.FullPanic(common.aux.panic); 23 24pub var per_cpu_init_data: PerCpuInitData = .{}; 25 26const PerCpuInitData = struct { 27 const StandardGdt = structures.gdt.StandardGdt; 28 const Tss = structures.tss.Tss; 29 const Idt = structures.Idt; 30 31 gdt_buf: []StandardGdt = undefined, 32 tss_buf: []Tss = undefined, 33 idt: *Idt = undefined, 34 35 const Self = @This(); 36 pub fn init(self: *Self, cpu_count: u64) void { 37 // 1. Allocate an IDT 38 const idt_addr = common.init_data.bootmem.allocMem(@sizeOf(Idt)) catch |err| { 39 std.log.err("init PerCpuInitData: IDT alloc failed: {}", .{err}); 40 @panic("rip bozo"); 41 }; 42 self.idt = @ptrFromInt(idt_addr); 43 44 // 2. Allocate space for GDT and TSS data 45 const gdt_size = @sizeOf(StandardGdt); 46 const tss_size = @sizeOf(Tss); 47 48 const total_required_size = gdt_size * cpu_count + tss_size * cpu_count; 49 const buf: [*]u8 = @ptrFromInt(common.init_data.bootmem.allocMem(total_required_size) catch |err| { 50 std.log.err("init PerCpuInitData: GDT/TSS alloc failed: {}", .{err}); 51 @panic("rip bozo"); 52 }); 53 54 // 3. Transmute and fill out the structure 55 const gdt_buf: [*]StandardGdt = @ptrCast(@alignCast(buf[0 .. gdt_size * cpu_count])); 56 const tss_buf: [*]Tss = @ptrCast(@alignCast(buf[gdt_size * cpu_count ..][0 .. tss_size * cpu_count])); 57 self.gdt_buf = gdt_buf[0..cpu_count]; 58 self.tss_buf = tss_buf[0..cpu_count]; 59 } 60}; 61 62comptime { 63 // Entry point (_start) 64 @export(&boot.bsp_init, .{ .name = "_start", .linkage = .strong }); 65}