Skip to content

Commit

Permalink
important stuff
Browse files Browse the repository at this point in the history
Signed-off-by: TalonFloof <105325988+TalonFloof@users.noreply.github.com>
  • Loading branch information
TalonFloof committed Nov 17, 2024
1 parent c0882cb commit 8594d76
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ iso: build_pc64
rm -r --force /tmp/kobold_iso

run_pc64: iso
qemu-system-x86_64 -enable-kvm -cpu host,migratable=off -m 8M -serial stdio -device pcie-pci-bridge -cdrom kobold.iso
qemu-system-x86_64 -enable-kvm -cpu host,migratable=off -m 8M -serial stdio -device pcie-pci-bridge -cdrom kobold.iso -smp 2 -no-shutdown -no-reboot

run_rv64: build_rv64
qemu-system-riscv64 -machine virt -m 128M -serial stdio -device ramfb -device virtio-keyboard-device -device virtio-mouse-device -kernel kobold/zig-out/bin/kernel
Expand Down
4 changes: 4 additions & 0 deletions kobold/hal/hal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub const debug = @import("debug/debug.zig");

pub const Writer = std.io.Writer(@TypeOf(.{}), error{}, arch.write);
pub const writer = Writer{ .context = .{} };
const team = @import("root").team;

pub var hiList: ?[]*HartInfo = null;

// #define ALIGN_UP(s, a) (((s) + ((a) - 1)) & ~((a) - 1))
//#define ALIGN_DOWN(s, a) ((s) & ~((a) - 1))
Expand All @@ -33,6 +36,7 @@ pub export fn HALInitialize(stackTop: usize, dtb: *allowzero anyopaque) callconv
arch.init(stackTop, dtb);
if (arch.memModel.layout == .Flat)
@panic("MMUless setups are not supported!");
team.Init();
root.KoboldInit();
@panic("No Command");
}
Expand Down
1 change: 1 addition & 0 deletions kobold/hal/hart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const HartInfo = struct {
};

tss: TSS = TSS{},
apicID: u32 = 0,
},
.riscv32, .riscv64 => struct {},
else => |v| @compileError("Unsupported Architecture " ++ v),
Expand Down
38 changes: 38 additions & 0 deletions kobold/hal/x86_64/hart.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const limine = @import("limine");
const std = @import("std");
const hal = @import("../hal.zig");
const physmem = @import("root").physmem;

export var smp_request: limine.SmpRequest = .{ .flags = 0 };
pub var hartData: usize = 0;

pub fn startSMP() void {
if (smp_request.response) |response| {
hal.hiList = @as([*]*hal.HartInfo, @ptrCast(@alignCast(physmem.AllocateC(response.cpu_count * @sizeOf(usize)))))[0..response.cpu_count];
hal.hiList.?[0] = hal.arch.getHart();
var hartCount: i32 = 1;
std.log.info("{} Hart System (MultiHart Kernel)", .{hal.hiList.?.len});
for (response.cpus()) |hart| {
if (hart.lapic_id != response.bsp_lapic_id) {
var hi: *hal.HartInfo = @ptrCast(@alignCast(physmem.AllocateC(@sizeOf(hal.HartInfo))));
hal.hiList.?[@as(usize, @intCast(hartCount))] = hi;
hi.hartID = @intCast(hartCount);
hi.archData.apicID = hart.lapic_id;
hartData = @intFromPtr(hi);
@as(*align(1) u64, @ptrFromInt(@intFromPtr(hart) + @offsetOf(limine.SmpInfo, "goto_address"))).* = @intFromPtr(&hal.archData._hartstart);
var cycles: usize = 0;
while (hartData != 0) {
cycles += 1;
if (cycles >= 50000000) {
std.log.err("Hart #{} took too long (potential triple fault on hart!)", .{hartCount});
hal.HALOops("X86_64 HAL Initialization Failure");
}
std.atomic.spinLoopHint();
}
hartCount += 1;
}
}
} else {
std.log.info("1 Hart System (MultiHart Kernel)", .{});
}
}
35 changes: 35 additions & 0 deletions kobold/hal/x86_64/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const mem = @import("mem.zig");
const limine = @import("limine");
const elf = @import("root").elf;
const acpi = @import("acpi.zig");
const apic = @import("apic.zig");
const timer = @import("timer.zig");
const hart = @import("hart.zig");
const flanterm = @cImport({
@cInclude("flanterm.h");
@cInclude("backends/fb.h");
Expand All @@ -31,6 +33,23 @@ pub export fn _start() callconv(.Naked) noreturn {
);
}

pub export fn _hartstart() callconv(.Naked) noreturn {
asm volatile (
\\mov %rsp, %rdi
\\push %rax
\\mov %cr0, %rax
\\and $0xfb, %al
\\or $0x22, %al
\\mov %rax, %cr0
\\mov %cr4, %rax
\\or $0x600, %eax
\\mov %rax, %cr4
\\fninit
\\pop %rax
\\jmp HartStart
);
}

export var moduleRequest: limine.ModuleRequest = .{};
export var fbRequest: limine.FramebufferRequest = .{};
var termCtx: ?*flanterm.flanterm_context = null;
Expand Down Expand Up @@ -119,6 +138,7 @@ fn ArchInit(stackTop: usize, limine_header: *allowzero anyopaque) void {
}
acpi.init();
timer.init();
hart.startSMP();

if (moduleRequest.response) |response| {
var len = response.modules().len;
Expand All @@ -141,6 +161,21 @@ fn ArchInit(stackTop: usize, limine_header: *allowzero anyopaque) void {
}
}

pub export fn HartStart(stackTop: usize) callconv(.C) noreturn {
wrmsr(0xC0000102, hart.hartData);
wrmsr(0x277, 0x0107040600070406); // Enable write combining when PAT, PCD, and PWT is set
ArchGetHart().archData.tss.rsp[0] = stackTop;
ArchGetHart().trapStack = stackTop;
gdt.initialize();
idt.fastInit();
apic.setup();
timer.init();
hart.hartData = 0;
while (true) {
std.atomic.spinLoopHint();
}
}

fn ArchWriteString(_: @TypeOf(.{}), string: []const u8) error{}!usize {
var i: isize = 0;
while (i < string.len) : (i += 1) {
Expand Down
1 change: 1 addition & 0 deletions kobold/hal/x86_64/timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn init() void {
std.log.info("{} APIC Ticks/s", .{count * 100});
}
}
apic.write(0x3e0, 0xb);
apic.write(0x320, 0x20);
}

Expand Down
1 change: 1 addition & 0 deletions kobold/kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub const physmem = @import("physmem.zig");
pub const Spinlock = @import("perlib").Spinlock;
pub const elf = @import("elf.zig");
pub const pfn = @import("pfn.zig");
pub const team = @import("team.zig");

pub const kmain_log = std.log.scoped(.KernelMain);

Expand Down
29 changes: 29 additions & 0 deletions kobold/kernel/team.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const thread = @import("thread.zig");
const Spinlock = @import("root").Spinlock;
const hal = @import("root").hal;
const RedBlackTree = @import("perlib").RedBlackTree;
const physmem = @import("root").physmem;

pub const Team = struct {
teamID: i64,
Expand All @@ -18,3 +19,31 @@ const TeamTreeType = RedBlackTree(*Team, struct {
return std.math.order(a.teamID, b.teamID);
}
}.compare);

pub var teams: TeamTreeType = .{};
pub var kteam: ?*Team = null;
pub var teamLock: Spinlock = .unaquired;
pub var nextTeamID: i64 = 1;

pub fn NewTeam(parent: ?*Team, name: []const u8) *Team {
const old = hal.arch.intControl(false);
teamLock.acquire();
var team = @as(*Team, @ptrCast(@alignCast(physmem.AllocateC(@sizeOf(Team)))));

//team.addressSpace = Memory.Paging.NewPageDirectory();
@memcpy(@as([*]u8, @ptrFromInt(@intFromPtr(&team.name))), name);
team.parent = parent;
team.teamID = nextTeamID;
nextTeamID += 1;
const node = @as(*TeamTreeType.Node, @ptrCast(@alignCast(physmem.AllocateC(@sizeOf(TeamTreeType.Node)))));
var entry = teams.getEntryFor(team);
entry.set(node);
teamLock.release();
_ = hal.arch.intControl(old);
return team;
}

pub fn Init() void {
std.log.info("Creating Kernel Team", .{});
kteam = NewTeam(null, "Kernel Team");
}
2 changes: 1 addition & 1 deletion kobold/kernel/thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const Thread = struct {
priority: usize = 16,
kstack: []u8,
gpContext: hal.arch.Context = .{},
fContext: hal.Arch.FloatContext = .{},
fContext: hal.arch.FloatContext = .{},
};

pub const ThreadList = std.DoublyLinkedList(*Thread);
Expand Down

0 comments on commit 8594d76

Please sign in to comment.