Skip to content

Commit

Permalink
meow
Browse files Browse the repository at this point in the history
Signed-off-by: TalonFloof <[email protected]>
  • Loading branch information
TalonFloof committed Nov 30, 2024
1 parent de90f4a commit e0ed0ad
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions kobold/hal/alarmqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ fn aqCommand(cmd: []const u8, iter: *std.mem.SplitIterator(u8, .sequence)) void
hartID = std.fmt.parseInt(usize, idStr, 0) catch hartID;
}
const hart = hal.hiList.?[hartID];
std.log.debug("Hart #{} AlarmQueue:\n Elapsed {} us\n NextInterval {} us\n", .{ hartID, hart.alarmQueue.timerCounter, hart.alarmQueue.timerNextInterval });
std.log.debug("Hart #{} AlarmQueue:\n Elapsed {} ns\n NextInterval {} ns\n", .{ hartID, hart.alarmQueue.timerCounter, hart.alarmQueue.timerNextInterval });
var n = hart.alarmQueue.list.first;
while (n) |node| {
std.log.debug(" 0x{x}: Deadline {} us Data 0x{x}\n Func 0x{x} ", .{ @intFromPtr(node), node.data.deadline, @intFromPtr(node.data.data), @intFromPtr(node.data.func) });
std.log.debug(" 0x{x}: Deadline {} ns Data 0x{x}\n Func 0x{x} ", .{ @intFromPtr(node), node.data.deadline, @intFromPtr(node.data.data), @intFromPtr(node.data.func) });
hal.debug.file.PrintSymbolName(@intFromPtr(node.data.func));
std.log.debug("\n", .{});
n = node.next;
Expand Down
2 changes: 1 addition & 1 deletion kobold/hal/hal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub const ArchInterface = struct {
getHart: fn () *HartInfo,
intControl: fn (bool) bool,
waitForInt: fn () void,
setTimerDeadline: ?fn (u64) void = null, // In Microseconds, will be ticked if not implemented
setTimerDeadline: ?fn (u64) void = null, // In Nanoseconds, will be ticked if not implemented
getRemainingTime: ?fn () u64 = null,
debugGet: ?fn () u8 = null,
debugDisasm: ?fn (usize, *anyopaque) callconv(.C) usize = null,
Expand Down
2 changes: 1 addition & 1 deletion kobold/hal/x86_64/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ pub const Interface: hal.ArchInterface = .{
.intControl = ArchIntControl,
.waitForInt = ArchWaitForInt,
.setTimerDeadline = timer.setDeadline,
.getRemainingTime = timer.getRemainingUs,
.getRemainingTime = timer.getRemainingNs,
.debugGet = ArchDebugGet,
.debugDisasm = disasm,
.memModel = .{
Expand Down
13 changes: 7 additions & 6 deletions kobold/hal/x86_64/timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn init() void {
apic.hpetTicksPer100NS = 100000000.0 / apic.hpetPeriod;
hpetAddr[2] = 0;
hpetAddr[30] = 0;
hpetAddr[2] = 1;

const hz = 1000000000000000.0 / @as(f64, @floatFromInt(clock));
const interval = @as(usize, @intFromFloat(apic.hpetTicksPer100NS * 10000.0 * 100.0));
Expand All @@ -58,7 +59,7 @@ pub fn init() void {
apic.write(0x380, 0);
apic.write(0x3e0, 0xb);
const duration = hpetAddr[30] + interval;
hpetAddr[2] = 1;

apic.write(0x380, 0xffffffff);
while (hpetAddr[30] < duration) {
std.atomic.spinLoopHint();
Expand All @@ -73,7 +74,7 @@ pub fn init() void {
//timer_log.info("Corrected to {} APIC Ticks/ms", .{@as(u64, @intFromFloat(correctedBase)) * 1000});
//apic.write(0x320, 0x10000);
//const count = 0xffffffff - apic.read(0x390);
ticksPerSecond = @as(u64, @intFromFloat(correctedBase)) * 1000;
ticksPerSecond = @as(u64, @intFromFloat(correctedBase * 1000));
} else {
const freq: f64 = 105000000.0 / 88.0 / 65536.0;
timer_log.info("PIT @ ~{} Hz for APIC Timer Calibration", .{freq});
Expand Down Expand Up @@ -116,16 +117,16 @@ pub fn init() void {
apic.write(0x380, 0);
}

pub fn setDeadline(microsecs: u64) void {
const t: u64 = @as(u64, @intFromFloat(@as(f64, @floatFromInt(ticksPerSecond)) * (@as(f64, @floatFromInt(microsecs)) / 1000000.0 / 4.0)));
pub fn setDeadline(nanosecs: u64) void {
const t: u64 = @as(u64, @intFromFloat(@as(f64, @floatFromInt(ticksPerSecond)) * (@as(f64, @floatFromInt(nanosecs)) / 1000000000.0 / 4.0)));
if (t > 0xffffffff) {
apic.write(0x380, 0xffffffff);
} else {
apic.write(0x380, t);
}
}

pub fn getRemainingUs() u64 {
pub fn getRemainingNs() u64 {
const count = apic.read(0x390);
return @intFromFloat(@as(f64, @floatFromInt(count)) * (@as(f64, @floatFromInt(ticksPerSecond)) / 1000000.0));
return @intFromFloat(@as(f64, @floatFromInt(count)) * (@as(f64, @floatFromInt(ticksPerSecond)) / 1000000000.0));
}
2 changes: 1 addition & 1 deletion kobold/kernel/scheduler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn Schedule(con: ?*hal.arch.Context) noreturn {
}
hart.activeThread = @alignCast(@ptrCast(thr));
hart.activeSyscallStack = @intFromPtr(thr.kstack.ptr) + (thr.kstack.len - 8);
hart.alarmQueue.addAlarm(10000, &hart.scheduleNode);
hart.alarmQueue.addAlarm(10 * 1_000_000, &hart.scheduleNode);
thr.fContext.Load();
thr.gpContext.Enter();
}

0 comments on commit e0ed0ad

Please sign in to comment.