summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/time.c
blob: f4fa744ce169fed97255703f7f70ec9a8b80af24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <kernel/arch/amd64/boot.h>
#include <kernel/arch/amd64/interrupts.h>
#include <kernel/arch/amd64/time.h>
#include <kernel/panic.h>
#include <kernel/proc.h>

static uint64_t uptime = 0, goal = ~0;
static Proc *scheduled = NULL;

uint64_t uptime_ms(void) { return uptime; }

static void update_goal(void) {
	goal = scheduled ? scheduled->waits4timer.goal : ~(uint64_t)0;
}

void pit_irq(void) {
	// TODO inefficient - getting here executes a lot of code which could just be a few lines of asm
	uptime++;
	if (uptime < goal) return;

	Proc *p = scheduled;
	assert(p);
	scheduled = p->waits4timer.next;
	proc_setstate(p, PS_RUNNING);
	update_goal();
}

void timer_schedule(Proc *p, uint64_t time) {
	proc_setstate(p, PS_WAITS4TIMER);
	p->waits4timer.goal = time;

	Proc **slot = &scheduled;
	while (*slot && (*slot)->waits4timer.goal <= time) {
		assert((*slot)->state == PS_WAITS4TIMER);
		slot = &(*slot)->waits4timer.next;
	}
	p->waits4timer.next = *slot;
	*slot = p;
	update_goal();
}

void timer_deschedule(Proc *p) {
	assert(p->state == PS_WAITS4TIMER);

	Proc **slot = &scheduled;
	while (*slot && *slot != p)
		slot = &(*slot)->waits4timer.next;
	assert(*slot);
	*slot = p->waits4timer.next;

	proc_setstate(p, PS_RUNNING);
	update_goal();
}

void timer_init(void) {
	irq_fn[IRQ_PIT] = pit_irq;
}