summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kc/core/Makefile2
-rw-r--r--kc/core/pit8253.c12
-rw-r--r--kc/core/pit8253.h1
-rw-r--r--kc/core/task.c21
-rw-r--r--kc/core/task.h1
-rw-r--r--kc/core/timer.c8
-rw-r--r--kc/core/timer.h6
7 files changed, 45 insertions, 6 deletions
diff --git a/kc/core/Makefile b/kc/core/Makefile
index 6674318..49083bc 100644
--- a/kc/core/Makefile
+++ b/kc/core/Makefile
@@ -13,7 +13,7 @@ SOBJS :=
GOBJS := entry_x86_64.o reloc_x86_64.o kprint.o serial.o port.o \
kc_main.o memory.o memset.o memcpy.o memmove.o memcmp.o cpu.o \
vm_tree.o exceptions.o panic.o msr.o task.o cpu_task.o \
- kcc_memory.o string.o pic8259.o pic8259_isr.o pit8253.o
+ kcc_memory.o string.o pic8259.o pic8259_isr.o pit8253.o timer.o
IOBJS :=
# __attribute__((interrupt)) requires -mgeneral-regs-only
diff --git a/kc/core/pit8253.c b/kc/core/pit8253.c
index 8be2872..0cf5232 100644
--- a/kc/core/pit8253.c
+++ b/kc/core/pit8253.c
@@ -13,6 +13,8 @@
#define CONFIG_CHANNEL_0 (SEL_CHANNEL0|ACC_LOHI|MODE_RATE|BIN_COUNT)
+#define PIT_HZ 1193182U
+
static uint64_t count = 0;
int pit8253_callback(uint8_t irq)
@@ -22,7 +24,7 @@ int pit8253_callback(uint8_t irq)
if (!(count % (185)))
{
- kprintf("it's been about %lu seconds\n", count / 18);
+ kprintf("it's been about %lu seconds since starting\n", count / 18);
}
return 0;
@@ -30,6 +32,9 @@ int pit8253_callback(uint8_t irq)
void pit8253_init(void)
{
+ kprintf(
+ "initializing timer to an approximate frequency of %uHz\n",
+ PIT_HZ / 0xffff);
outb(PIT8253_CMD, CONFIG_CHANNEL_0);
outb(PIT8253_DATA0, 0xff);
outb(PIT8253_DATA0, 0xff);
@@ -46,6 +51,11 @@ void pit8253_stop(void)
{
}
+uint64_t pit8253_count(void)
+{
+ return count;
+}
+
void pit8253_set_hz(uint16_t hz)
{
(void)hz;
diff --git a/kc/core/pit8253.h b/kc/core/pit8253.h
index 81374a3..25861f7 100644
--- a/kc/core/pit8253.h
+++ b/kc/core/pit8253.h
@@ -5,5 +5,6 @@
void pit8253_init(void);
void pit8253_start(void);
void pit8253_stop(void);
+uint64_t pit8253_count(void);
void pit8253_set_hz(uint16_t hz);
diff --git a/kc/core/task.c b/kc/core/task.c
index b381793..09156d4 100644
--- a/kc/core/task.c
+++ b/kc/core/task.c
@@ -1,4 +1,5 @@
#include "task.h"
+#include "timer.h"
#include "memory.h"
#include "panic.h"
#include "cpu.h"
@@ -16,6 +17,15 @@ static void set_thread_status(enum kc_thread_status status);
static void idle_task_thread(void);
extern uint64_t *get_tss_rsp0(void);
+static uint64_t last_count = 0;
+
+void update_time_used(void)
+{
+ uint64_t current_count = timer_ticks();
+ uint64_t elapsed = current_count - last_count;
+ last_count = current_count;
+ current_task->time_elapsed += elapsed;
+}
void task_set_thread(struct kc_thread *task)
{
@@ -26,6 +36,8 @@ void task_set_thread(struct kc_thread *task)
void task_schedule(void)
{
+ kprintf("task_schedule() called @%lu ticks\n", last_count);
+ update_time_used();
if (first_ready_task)
{
struct kc_thread *task = first_ready_task;
@@ -34,6 +46,7 @@ void task_schedule(void)
}
}
+
static void lock_scheduler(void)
{
__asm__ volatile ("cli;");
@@ -64,6 +77,7 @@ static void unblock_thread(struct kc_thread *thread)
first_ready_task->next = thread;
first_ready_task = thread;
}
+ unlock_scheduler();
}
static void set_thread_status(enum kc_thread_status status)
@@ -80,11 +94,10 @@ static void idle_task_thread(void)
while (1)
{
__asm__ volatile ("hlt;");
+ lock_scheduler();
+ task_schedule();
+ unlock_scheduler();
}
- kputs("scheduling\n");
- lock_scheduler();
- task_schedule();
- unlock_scheduler();
}
noreturn void task_init(void)
diff --git a/kc/core/task.h b/kc/core/task.h
index e9ddaaf..346630a 100644
--- a/kc/core/task.h
+++ b/kc/core/task.h
@@ -22,6 +22,7 @@ struct kc_thread
{
struct kc_thread *prev;
struct kc_thread *next;
+ uint64_t time_elapsed;
enum kc_thread_status status;
struct kc_thread_state state;
};
diff --git a/kc/core/timer.c b/kc/core/timer.c
new file mode 100644
index 0000000..2b7d5f6
--- /dev/null
+++ b/kc/core/timer.c
@@ -0,0 +1,8 @@
+#include "timer.h"
+#include "pit8253.h"
+
+uint64_t timer_ticks(void)
+{
+ return pit8253_count();
+}
+
diff --git a/kc/core/timer.h b/kc/core/timer.h
new file mode 100644
index 0000000..702d7b6
--- /dev/null
+++ b/kc/core/timer.h
@@ -0,0 +1,6 @@
+#pragma once
+#include <stdint.h>
+
+uint64_t timer_ticks(void);
+
+