kernel: add standard clock system
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <socks/printk.h>
|
||||
#include <socks/clock.h>
|
||||
#include <socks/vm.h>
|
||||
#include <socks/cpu.h>
|
||||
#include <arch/acpi/io_apic.hpp>
|
||||
@@ -16,6 +17,7 @@
|
||||
#define IA32_APIC_BASE_MSR_ENABLE 0x800
|
||||
|
||||
static int apic_enabled = 0;
|
||||
static unsigned int bsp_id = (unsigned int)-1;
|
||||
|
||||
using namespace arch::acpi;
|
||||
|
||||
@@ -118,12 +120,26 @@ static void ioapic_init(uintptr_t base, unsigned int int_base)
|
||||
queue_push_back(&io_apics, &apic->io_entry);
|
||||
}
|
||||
|
||||
static int lapic_clock_irq(void)
|
||||
{
|
||||
if (this_cpu() == bsp_id) {
|
||||
clock_advance(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct irq_hook lapic_clock_irq_hook = {
|
||||
{},
|
||||
lapic_clock_irq,
|
||||
};
|
||||
|
||||
void local_apic_config_timer(void)
|
||||
{
|
||||
local_apic lapic(lapic_base);
|
||||
lapic.write(local_apic::TIMER_DIV, 0x3);
|
||||
lapic.write(local_apic::TIMER_INITCOUNT, (uint32_t)-1);
|
||||
pit_wait(100);
|
||||
clock_wait(10);
|
||||
lapic.write(local_apic::LVT_TIMER, APIC_LVT_INT_MASKED);
|
||||
|
||||
/* mask IRQ0 to block interrupts from the PIT. */
|
||||
@@ -132,7 +148,8 @@ void local_apic_config_timer(void)
|
||||
|
||||
uint32_t total_ticks = 0xFFFFFFFF - lapic.read(local_apic::TIMER_CURCOUNT);
|
||||
|
||||
total_ticks /= 100;
|
||||
/* convert to LAPIC ticks per kernel clock tick */
|
||||
total_ticks /= 10;
|
||||
|
||||
lapic.write(local_apic::LVT_TIMER, IRQ0 | APIC_LVT_TIMER_MODE_PERIODIC);
|
||||
lapic.write(local_apic::TIMER_DIV, 0x3);
|
||||
@@ -189,8 +206,6 @@ static void init_all_ioapic(struct acpi_madt *madt)
|
||||
|
||||
kern_status_t apic_init(void)
|
||||
{
|
||||
static unsigned int bsp_id = (unsigned int)-1;
|
||||
|
||||
/* the bootstrap processor will be the first one ro call apic_init().
|
||||
it is responsible for initialising the I/O APICs */
|
||||
if (bsp_id == (unsigned int)-1) {
|
||||
@@ -214,8 +229,13 @@ kern_status_t apic_init(void)
|
||||
apic_enabled = 1;
|
||||
irq_enable();
|
||||
|
||||
pit_start(1000);
|
||||
pit_start(HZ);
|
||||
return KERN_OK;
|
||||
|
||||
local_apic_config_timer();
|
||||
pit_stop();
|
||||
|
||||
hook_irq(IRQ0, &lapic_clock_irq_hook);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user