Skip to content

Commit 164f9fa

Browse files
committed
WIP working with lapic to trigger a timer
1 parent d1a3328 commit 164f9fa

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

platform/pc/lapic.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#include <arch/x86.h>
1919
#include <arch/x86/feature.h>
2020
#include <kernel/spinlock.h>
21-
#include "platform_p.h"
21+
#include <platform/time.h>
2222
#include <platform/pc.h>
2323
#include <kernel/vm.h>
2424

25+
#include "platform_p.h"
26+
2527
#define LOCAL_TRACE 1
2628

2729
static bool lapic_present = false;
@@ -69,6 +71,19 @@ enum lapic_regs {
6971
LAPIC_EXT_LVT0 = 0x500,
7072
};
7173

74+
enum lapic_interrupts {
75+
LAPIC_INT_TIMER = 0xf8,
76+
LAPIC_INT_GENERIC,
77+
LAPIC_INT_RESCHEDULE,
78+
};
79+
80+
enum lapic_timer_mode {
81+
LAPIC_TIMER_MODE_ONESHOT = 0,
82+
LAPIC_TIMER_MODE_PERIODIC = 1,
83+
LAPIC_TIMER_MODE_TSC_DEADLINE = 2,
84+
};
85+
86+
7287
static uint32_t lapic_read(enum lapic_regs reg) {
7388
LTRACEF("reg %#x\n", reg);
7489
DEBUG_ASSERT(reg != LAPIC_ICRLO && reg != LAPIC_ICRHI);
@@ -101,6 +116,29 @@ static void lapic_write_icr(uint32_t low, uint32_t apic_id) {
101116
}
102117
}
103118

119+
void lapic_set_oneshot_timer(uint32_t tick) {
120+
LTRACEF("tick %u\n", tick);
121+
122+
// set the initial count, which should trigger the timer
123+
lapic_write(LAPIC_TICR, tick);
124+
}
125+
126+
void lapic_cancel_oneshot_timer(void) {
127+
LTRACE;
128+
129+
// set the counter to 0 which disables it
130+
lapic_write(LAPIC_TICR, 0);
131+
}
132+
133+
enum handler_return lapic_timer_handler(void *arg) {
134+
//PANIC_UNIMPLEMENTED;
135+
136+
// return timer_tick(NULL, current_time());
137+
138+
lapic_set_oneshot_timer(100000000);
139+
140+
return INT_NO_RESCHEDULE;
141+
}
104142

105143
void lapic_init(void) {
106144
// discover the presence of the local apic and map it
@@ -156,6 +194,17 @@ void lapic_init_postvm(uint level) {
156194
if (eas) {
157195
dprintf(INFO, "X86: local apic EAS features %#x\n", lapic_read(LAPIC_EXT_FEATURES));
158196
}
197+
198+
lapic_cancel_oneshot_timer();
199+
200+
// configure the local timer and make sure it is not set to fire
201+
uint32_t val = (LAPIC_TIMER_MODE_ONESHOT << 17) | LAPIC_INT_TIMER;
202+
lapic_write(LAPIC_TIMER, val);
203+
204+
// register the local apic interrupts
205+
register_int_handler_msi(LAPIC_INT_TIMER, &lapic_timer_handler, NULL, false);
206+
207+
lapic_set_oneshot_timer(1000000);
159208
}
160209

161210
LK_INIT_HOOK(lapic, lapic_init_postvm, LK_INIT_LEVEL_VM);

scripts/do-qemux86

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ elif (( $DO_LEGACY )); then
7070
else
7171
QEMU="qemu-system-i386"
7272
PROJECT="pc-x86-test"
73-
CPU=max
73+
CPU=pentium3
7474
MACHINE=pc
7575
fi
7676

0 commit comments

Comments
 (0)