Put timing information in cpu structure

This commit is contained in:
Thomas Lovén 2022-01-20 21:51:55 +01:00
parent 7e3febe699
commit 9ef3c195d0
4 changed files with 21 additions and 5 deletions

View File

@ -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,
},
}

View File

@ -2,6 +2,7 @@
#include <sched.h>
#include <mittos/graphics.h>
#include <memory.h>
#include <cpu.h>
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));

View File

@ -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);
}

View File

@ -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();