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
12// needed by std options
13pub const page_size = struct {
14 pub const min = 4 << 10;
15 pub const max = 4 << 10;
16 pub fn get() usize {
17 return 4 << 10;
18 }
19};
20
21pub var per_cpu_init_data: PerCpuInitData = .{};
22
23const PerCpuInitData = struct {
24 const StandardGdt = structures.gdt.StandardGdt;
25 const Tss = structures.tss.Tss;
26
27 gdt_buf: []StandardGdt = undefined,
28 tss_buf: []Tss = undefined,
29 // Physical ptr
30 stack_buf: usize = undefined,
31
32 const stack_size = std.heap.page_size_max;
33
34 const Self = @This();
35 pub fn init(self: *Self, cpu_count: u64) void {
36 // 1. Allocate stack space for every core
37 self.stack_buf = common.init_data.bootmem.allocPhys(stack_size * cpu_count) catch |err| {
38 std.log.err("init PerCpuInitData: failed to allocate stack! {}", .{err});
39 @panic("stack_buf");
40 };
41
42 // 2. Allocate space for GDT and TSS data
43 const gdt_size = @sizeOf(StandardGdt);
44 const tss_size = @sizeOf(Tss);
45
46 const total_required_size = gdt_size * cpu_count + tss_size * cpu_count;
47 const buf: [*]u8 = @ptrFromInt(common.init_data.bootmem.allocMem(total_required_size) catch |err| {
48 std.log.err("init PerCpuInitData: GDT/TSS alloc failed: {}", .{err});
49 @panic("gdt_tss_buf");
50 });
51
52 // 3. Transmute and fill out the structure
53 const gdt_buf: [*]StandardGdt = @ptrCast(@alignCast(buf[0 .. gdt_size * cpu_count]));
54 const tss_buf: [*]Tss = @ptrCast(@alignCast(buf[gdt_size * cpu_count ..][0 .. tss_size * cpu_count]));
55 self.gdt_buf = gdt_buf[0..cpu_count];
56 self.tss_buf = tss_buf[0..cpu_count];
57 }
58
59 // returns a pointer to the TOP of the stack!
60 pub fn getStackPhys(self: *Self, core_num: usize) usize {
61 return self.stack_buf + (core_num + 1) * stack_size;
62 }
63};