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
30 gdt_buf: []StandardGdt = undefined,
31 tss_buf: []Tss = undefined,
32
33 const Self = @This();
34 pub fn init(self: *Self, cpu_count: u64) void {
35 // 1. Allocate space for GDT and TSS data
36 const gdt_size = @sizeOf(StandardGdt);
37 const tss_size = @sizeOf(Tss);
38
39 const total_required_size = gdt_size * cpu_count + tss_size * cpu_count;
40 const buf: [*]u8 = @ptrFromInt(common.init_data.bootmem.allocMem(total_required_size) catch |err| {
41 std.log.err("init PerCpuInitData: GDT/TSS alloc failed: {}", .{err});
42 @panic("rip bozo");
43 });
44
45 // 2. Transmute and fill out the structure
46 const gdt_buf: [*]StandardGdt = @ptrCast(@alignCast(buf[0 .. gdt_size * cpu_count]));
47 const tss_buf: [*]Tss = @ptrCast(@alignCast(buf[gdt_size * cpu_count ..][0 .. tss_size * cpu_count]));
48 self.gdt_buf = gdt_buf[0..cpu_count];
49 self.tss_buf = tss_buf[0..cpu_count];
50 }
51};
52
53comptime {
54 // Entry point (_start)
55 @export(&boot.bsp_init, .{ .name = "_start", .linkage = .strong });
56}