Skip to content

Commit

Permalink
Start APIC and some little changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Oct 2, 2023
1 parent 1202c43 commit 4d5b2cd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
13 changes: 13 additions & 0 deletions kernel/apic.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const vmm = @import("vmm.zig");

fn read(addr: u32, reg: u32) u32 {
const base: [*]volatile u32 = @ptrFromInt(addr + vmm.higher_half);
base[0] = reg;
return base[4];
}

fn write(addr: u32, reg: u32, value: u32) void {
const base: [*]volatile u32 = @ptrFromInt(addr + vmm.higher_half);
base[0] = reg;
base[4] = value;
}
2 changes: 1 addition & 1 deletion kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn log(
comptime format: []const u8,
args: anytype,
) void {
if (builtin.mode != .Debug) return;
comptime if (builtin.mode != .Debug) return;

const level_txt = comptime switch (level) {
.err => "\x1b[31merror\x1b[m",
Expand Down
2 changes: 2 additions & 0 deletions kernel/gdt.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const log = @import("std").log.scoped(.gdt);
const SpinLock = @import("lock.zig").SpinLock;
const TSS = @import("cpu.zig").TSS;

Expand Down Expand Up @@ -66,6 +67,7 @@ var lock: SpinLock = .{};
pub fn init() void {
gdtr.base = @intFromPtr(&gdt);
reloadGDT();
log.info("init: successfully reloaded GDT", .{});
}

pub fn reloadGDT() void {
Expand Down
8 changes: 6 additions & 2 deletions kernel/idt.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const log = std.log.scoped(.idt);
const cpu = @import("cpu.zig");

// TODO
Expand Down Expand Up @@ -88,6 +89,7 @@ pub fn init() void {

inline for (0..256) |i| {
const handler = comptime makeStubHandler(i);
// log.info("init idt[{}] with {}", .{ i, handler });
idt[i] = IDTEntry.init(@intFromPtr(handler), 0, interrupt_gate);
}

Expand All @@ -96,10 +98,12 @@ pub fn init() void {
// idt[syscall_vector].type_attributes = 0xee;

reloadIDT();
log.info("init: successfully reloaded IDT", .{});
}

pub fn reloadIDT() void {
asm volatile ("lidt (%[idtr])"
asm volatile (
\\lidt (%[idtr])
:
: [idtr] "r" (&idtr),
: "memory"
Expand All @@ -108,7 +112,7 @@ pub fn reloadIDT() void {

pub fn allocateVector() u8 {
const vector = @atomicRmw(u8, &next_vector, .Add, 1, .AcqRel);
if (vector >= 256 - 16) {
if (vector >= 256 - 16) { // TODO - 16 ? also u8 so care about overflows
@panic("IDT exhausted");
}
return vector;
Expand Down
3 changes: 1 addition & 2 deletions kernel/lock.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub const SpinLock = struct {

pub fn unlock(self: *Self) void {
const state = self.state.swap(unlocked, .Release);
std.debug.assert(state != unlocked);
std.debug.assert(state != contended);
std.debug.assert(state == locked);
}
};
5 changes: 3 additions & 2 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const idt = @import("idt.zig");
const pmm = @import("pmm.zig");
const vmm = @import("vmm.zig");
const mem = @import("mem.zig");
const ps2 = @import("ps2.zig");
const time = @import("time.zig");

pub const std_options = struct {
Expand Down Expand Up @@ -69,8 +70,8 @@ fn main() !void {
gdt.init();
idt.init();
// TODO: init events <-- for interrupts
// TODO: interrupt controller (pic or apic)
// TODO: PS/2 -> handle keyboard/mouse <-- extern
// TODO: apic: interrupt controller
// TODO: ps2.init();

try pmm.init();
try vmm.init(); // TODO
Expand Down
4 changes: 2 additions & 2 deletions kernel/ps2.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const arch = @import("arch.zig");
const idt = @import("idt.zig");

pub var keyboard_vector = undefined;
pub var keyboard_vector: u8 = undefined;

// TODO: too complicated ?
pub fn init() void {
Expand All @@ -28,7 +28,7 @@ pub fn init() void {
keyboard_vector = idt.allocateVector();
// TODO
// ioapic.setIRQRedirect(bsp_lapic_id, keyboard_vector, 1, true);
arch.in(u8, 0x60);
_ = arch.in(u8, 0x60);
}

pub fn read() u8 {
Expand Down
7 changes: 1 addition & 6 deletions kernel/serial.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub const Port = enum(u16) {
com4 = 0x2e8,
};

var com1_lock: SpinLock = .{};
const com1_writer = std.io.Writer(void, error{}, com1Write){ .context = {} };

pub fn print(comptime fmt: []const u8, args: anytype) void {
Expand Down Expand Up @@ -55,18 +54,14 @@ inline fn transmitterIsEmpty(port: Port) bool {

inline fn transmitData(port: Port, value: u8) void {
while (!transmitterIsEmpty(port)) {
asm volatile ("pause");
std.atomic.spinLoopHint();
}
arch.out(u8, @intFromEnum(port), value);
}

fn com1Write(_: void, str: []const u8) error{}!usize {
com1_lock.lock();
defer com1_lock.unlock();

for (str) |char| {
transmitData(Port.com1, char);
}

return str.len;
}

0 comments on commit 4d5b2cd

Please sign in to comment.