diff --git a/.vscode/settings.json b/.vscode/settings.json index 5b13e7a..5a08ad4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,4 +12,10 @@ "C_Cpp.default.includePath": ["src/kernel/include", "sysroot/usr/include"], "C_Cpp.default.cStandard": "c17", + "files.associations": { + "cpu.h": "c" + }, + "[c]": { + "editor.tabSize": 2, + }, } \ No newline at end of file diff --git a/src/kernel/boot/TEMP-test_scheduler.c b/src/kernel/boot/TEMP-test_scheduler.c index 480750a..15d1606 100644 --- a/src/kernel/boot/TEMP-test_scheduler.c +++ b/src/kernel/boot/TEMP-test_scheduler.c @@ -2,6 +2,7 @@ #include #include #include +#include extern gfx_context *term_fb; extern gfx_context kernel_fb; @@ -18,6 +19,12 @@ void thread1() { RGB(255,255,0), RGB(0,0,0), '0'+(a++%10) ); + int s = (current_cpu->timer_ticks/1000)%10; + putCharacter(term_fb, + 0, 0, + RGB(255,255,0), RGB(0,0,0), + '0'+(s%10) + ); flip(term_fb); draw_line(ctx, 0, 100, 50, ((a-1)%100), RGB(0,0,0)); draw_line(ctx, 0, 100, 50, (a%100), RGB(255,0,0)); diff --git a/src/kernel/cpu/timer.c b/src/kernel/cpu/timer.c index 9e9585e..5acb253 100644 --- a/src/kernel/cpu/timer.c +++ b/src/kernel/cpu/timer.c @@ -55,23 +55,22 @@ static uint64_t calibrate_apic_timer() { void reset_apic_timer(uint64_t us) { APIC_REG(APIC_TIMER_DIVIDER) = 0xB; - APIC_REG(APIC_TIMER_INIT) = us*apic_ticks_per_us; + APIC_REG(APIC_TIMER_INIT) = us*current_cpu->timer_freq; APIC_REG(APIC_LVT(LVT_TIMER)) = 0x0 | IRQ_INTERRUPT(IRQ_TIMER); } volatile int ctr = 0; registers *apic_timer_handler(registers *r) { - - ctr++; - __asm__("cli"); + current_cpu->timer_ticks++; reset_apic_timer(1000); irq_ack(); return r; } void timer_init() { - apic_ticks_per_us = calibrate_apic_timer(); + current_cpu->timer_ticks = 0; + current_cpu->timer_freq = calibrate_apic_timer(); bind_interrupt(IRQ_INTERRUPT(IRQ_TIMER), apic_timer_handler); reset_apic_timer(1000); } \ No newline at end of file diff --git a/src/kernel/include/cpu.h b/src/kernel/include/cpu.h index 5363111..546ed62 100644 --- a/src/kernel/include/cpu.h +++ b/src/kernel/include/cpu.h @@ -55,6 +55,8 @@ struct cpu { uint8_t id; uint8_t apic_id; uint32_t flags; + uint64_t timer_ticks; + uint64_t timer_freq; // in ticks per microsecond }; struct ioapic { uint8_t id; @@ -66,6 +68,8 @@ extern struct cpu *cpus[MAX_CPUS]; extern struct ioapic ioapic; extern uint8_t irq_redirects[MAX_IRQS]; +#define current_cpu (cpus[0]) + // cpu/cpu.c void early_cpu_init(); void cpu_init();