Microkernel thing OS experiment (Zig ⚡)

tsc_deadline: fix detection bug

pci.express 3ba56657 d3f49685

verified
Changed files
+22 -14
components
ukernel
arch
amd64
instructions
interrupts
+2 -5
components/ukernel/arch/amd64/boot.zig
···
const std = @import("std");
const arch = @import("root.zig");
const common = @import("common");
-
const console = @import("console");
const flanterm = @import("flanterm");
const log = std.log.scoped(.amd64_init);
const StandardGdt = arch.structures.gdt.StandardGdt;
···
break :blk .{ .xapic = apic_base };
},
};
-
// Set up the spurious vector and the TPR
arch.interrupts.apic.init.initialSetup();
-
-
// Calibrate the APIC timer
-
arch.interrupts.apic.init.calibrateTimer();
// Enable one-shot interrupts
arch.interrupts.apic.init.enableOneshotInterrupt();
···
const std = @import("std");
const arch = @import("root.zig");
const common = @import("common");
const flanterm = @import("flanterm");
const log = std.log.scoped(.amd64_init);
const StandardGdt = arch.structures.gdt.StandardGdt;
···
break :blk .{ .xapic = apic_base };
},
};
+
// Set up the spurious vector, TPR, and
+
// calibrate the timer
arch.interrupts.apic.init.initialSetup();
// Enable one-shot interrupts
arch.interrupts.apic.init.enableOneshotInterrupt();
+2 -2
components/ukernel/arch/amd64/instructions/cpuid.zig
···
}
const FeaturesEcx = packed struct(u32) {
-
_reserved0: u23,
tsc_deadline: bool,
-
_reserved1: u8,
};
pub inline fn cpuid(leaf: u32, sub: u32) DefaultResults {
···
}
const FeaturesEcx = packed struct(u32) {
+
_reserved0: u24,
tsc_deadline: bool,
+
_reserved1: u7,
};
pub inline fn cpuid(leaf: u32, sub: u32) DefaultResults {
+3 -1
components/ukernel/arch/amd64/interrupts/apic.zig
···
// });
arch.interrupts.idt.add_handler(.{ .interrupt = 0xFF }, spurious_interrupt_handler, 3, 0);
arch.interrupts.idt.add_handler(.{ .interrupt = 48 }, periodic_handler, 3, 0);
}
-
pub fn calibrateTimer() void {
singleton.setDivideConfigurationRegister(.div2);
singleton.setLVTTimerRegister(.{
.idt_entry = 0x69,
···
// });
arch.interrupts.idt.add_handler(.{ .interrupt = 0xFF }, spurious_interrupt_handler, 3, 0);
arch.interrupts.idt.add_handler(.{ .interrupt = 48 }, periodic_handler, 3, 0);
+
+
calibrateTimer();
}
+
fn calibrateTimer() void {
singleton.setDivideConfigurationRegister(.div2);
singleton.setLVTTimerRegister(.{
.idt_entry = 0x69,
+15 -6
components/ukernel/arch/amd64/interrupts/root.zig
···
asm volatile ("cli");
}
pub fn unhandled_interrupt(frame: *idt.InterruptFrame(u64)) callconv(.{ .x86_64_sysv = .{} }) void {
if (std.enums.tagName(idt.Exception, frame.int_num.exception)) |exception_name| {
std.log.err("Unhandled interrupt (0x{x} : {s})!!!", .{ frame.int_num.interrupt, exception_name });
} else {
std.log.err("Unhandled interrupt (0x{x})!!!", .{frame.int_num.interrupt});
}
-
std.log.err("CR3: 0x{x:0>16}", .{frame.cr3});
-
std.log.err("RAX: 0x{x:0>16}, RBX: 0x{x:0>16}, RCX: 0x{x:0>16}, RDX: 0x{x:0>16}", .{ frame.regs.rax, frame.regs.rbx, frame.regs.rcx, frame.regs.rdx });
-
std.log.err("RSI: 0x{x:0>16}, RDI: 0x{x:0>16}, RBP: 0x{x:0>16}, RSP: 0x{x:0>16}", .{ frame.regs.rsi, frame.regs.rdi, frame.regs.rbp, frame.rsp });
-
std.log.err("R8: 0x{x:0>16}, R9: 0x{x:0>16}, R10: 0x{x:0>16}, R11: 0x{x:0>16}", .{ frame.regs.r8, frame.regs.r9, frame.regs.r10, frame.regs.r11 });
-
std.log.err("R12: 0x{x:0>16}, R13: 0x{x:0>16}, R14: 0x{x:0>16}, R15: 0x{x:0>16}", .{ frame.regs.r12, frame.regs.r13, frame.regs.r14, frame.regs.r15 });
-
std.log.err("RFL: 0x{x:0>16}, RIP: 0x{x:0>16}, CS: 0x{x:0>16}, SS: 0x{x:0>16}", .{ frame.eflags, frame.rip, frame.cs, frame.ss });
arch.interrupts.disable();
arch.instructions.die();
}
···
pub fn double_fault(stack_frame: *idt.InterruptFrame(u64)) callconv(.{ .x86_64_sysv = .{} }) void {
std.log.err("Double fault @ 0x{x}, dying!!!", .{stack_frame.rip});
arch.instructions.die();
}
···
std.log.warn("LDT selector: 0x{x}", .{ldt_sel});
},
}
arch.instructions.die();
}
···
asm volatile ("cli");
}
+
fn print_regs(frame: *idt.InterruptFrame(u64)) void {
+
std.log.err("CR3: 0x{x:0>16}", .{frame.cr3});
+
std.log.err("RAX: 0x{x:0>16}, RBX: 0x{x:0>16}, RCX: 0x{x:0>16}, RDX: 0x{x:0>16}", .{ frame.regs.rax, frame.regs.rbx, frame.regs.rcx, frame.regs.rdx });
+
std.log.err("RSI: 0x{x:0>16}, RDI: 0x{x:0>16}, RBP: 0x{x:0>16}, RSP: 0x{x:0>16}", .{ frame.regs.rsi, frame.regs.rdi, frame.regs.rbp, frame.rsp });
+
std.log.err("R8: 0x{x:0>16}, R9: 0x{x:0>16}, R10: 0x{x:0>16}, R11: 0x{x:0>16}", .{ frame.regs.r8, frame.regs.r9, frame.regs.r10, frame.regs.r11 });
+
std.log.err("R12: 0x{x:0>16}, R13: 0x{x:0>16}, R14: 0x{x:0>16}, R15: 0x{x:0>16}", .{ frame.regs.r12, frame.regs.r13, frame.regs.r14, frame.regs.r15 });
+
std.log.err("RFL: 0x{x:0>16}, RIP: 0x{x:0>16}, CS: 0x{x:0>16}, SS: 0x{x:0>16}", .{ frame.eflags, frame.rip, frame.cs, frame.ss });
+
}
+
pub fn unhandled_interrupt(frame: *idt.InterruptFrame(u64)) callconv(.{ .x86_64_sysv = .{} }) void {
if (std.enums.tagName(idt.Exception, frame.int_num.exception)) |exception_name| {
std.log.err("Unhandled interrupt (0x{x} : {s})!!!", .{ frame.int_num.interrupt, exception_name });
} else {
std.log.err("Unhandled interrupt (0x{x})!!!", .{frame.int_num.interrupt});
}
+
+
print_regs(frame);
+
arch.interrupts.disable();
arch.instructions.die();
}
···
pub fn double_fault(stack_frame: *idt.InterruptFrame(u64)) callconv(.{ .x86_64_sysv = .{} }) void {
std.log.err("Double fault @ 0x{x}, dying!!!", .{stack_frame.rip});
+
print_regs(stack_frame);
+
arch.interrupts.disable();
arch.instructions.die();
}
···
std.log.warn("LDT selector: 0x{x}", .{ldt_sel});
},
}
+
print_regs(@ptrCast(stack_frame));
arch.instructions.die();
}