Skip to content

Commit

Permalink
early alarmqueue work
Browse files Browse the repository at this point in the history
  • Loading branch information
TalonFloof authored Nov 13, 2024
1 parent 4882a6d commit a303dd5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions kobold/hal/alarmqueue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const std = @import("std");
const hal = @import("hal.zig");
const Spinlock = @import("perlib").Spinlock;
const physmem = @import("root").physmem;

pub const AlarmQueueNode = struct {
deadline: u64,
Expand All @@ -17,8 +18,21 @@ pub const AlarmQueueNode = struct {
const AlarmQueueList = std.DoublyLinkedList(AlarmQueueNode);
pub const AlarmQueue = struct {
lock: Spinlock = .unaquired,
ticks: u64 = 0,
list: AlarmQueueList = .{},


pub fn addAlarm(timeout: u64, func: *fn(?*anyopaque) callconv(.C) void, data: ?*anyopaque) void {
const node = @ptrCast(@alignCast(physmem.Allocate(@sizeOf(AlarmQueueList.Node),@alignOf(AlarmQueueList.node)).?));
node.data.deadline = hal.arch.getHart().timerCounter + timeout;
node.data.data = data;
node.data.func = func;
}

fn recalibrate() void {
const elapsedTime = hal.arch.getHart().timerNextInterval - hal.arch.getRemainingTime();
var ind = list.first;
while(ind != null) {

ind = ind.?.next;
}
}
};
2 changes: 2 additions & 0 deletions kobold/hal/hart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub const HartInfo = struct {
activeContextStack: usize = 0,
activeSyscallStack: usize = 0,
trapStack: usize = 0,
timerCounter: u64 = 0, // In microseconds
timerNextInterval: u64 = 0, // The inital set time of the timer, in microseconds
archData: ArchData = ArchData{},
hartID: usize = 0,

Expand Down
5 changes: 5 additions & 0 deletions kobold/hal/x86_64/timer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ pub fn setDeadline(microsecs: usize) void {
const t: usize = @intFromFloat(@as(f64, @floatFromInt(ticksPerSecond)) * (@as(f64, @floatFromInt(microsecs)) / 1000000.0));
apic.write(0x380, t);
}

pub fn getRemainingUs() usize {
const count = 0xffffffff - apic.read(0x390);
returns @intFromFloat(@as(f64, @floatFromInt(count)) * (@as(f64, @floatFromInt(ticksPerSecond)) / 1000000.0));
}

0 comments on commit a303dd5

Please sign in to comment.