Page 1 of 1

[SOLVED] A question about APIC Timer

Posted: Thu Jul 25, 2013 8:22 am
by nbdd0121
I am a high school student from China who loves programming and developing. Now I am trying to develop a OS by my own. The first several steps are very easy, I successfully entered protected mode, loaded GDT, IDT and TSS, enabled paging, and initialized 8259A and PIT timer to support multi-tasks.
I want something challenge, so I started to include the support of APIC. I just read through the APIC Timer part in the Intel Manual briefly and wrote some codes. The timer did not run as I expected.
/* Initialize */
apic_write(APIC_SVR, 0x130);// 0x30 vector's handler does nothing: Only a iretd, apic_write is as same as that of Linux.
apic_write(APIC_TIMER_DCR, 0x0);// DCR=Divide Configuration Register
apic_write(APIC_TIMER_ICR, 242880);// ICR=Initial Count Register
apic_write(APIC_LVT_TIMER, 0x20020);// 0x20 is the timer interrupt handler
/* End */
In the timer interrupt handler, I just save all registers, "apic_write(APIC_EOI_REG,0);" and schedule the tasks. I expected the timer to run periodically, but it only runs once. However, when I add "apic_write(APIC_TIMER_ICR, 242880);" into my timer handler, the timer can run normally. I got really confused by this problem. Where is wrong? I think I did not miss any essential steps.

Re: A question about APIC Timer

Posted: Thu Jul 25, 2013 10:36 am
by Owen
The APIC timer has multiple modes. My guess is it was left in one shot mode.

Re: A question about APIC Timer

Posted: Fri Jul 26, 2013 1:16 am
by nbdd0121
Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
I set the LVT Timer to be 0x20020, which means the interrupt vector is 0x20 and because the bit 18:17 is 01, the timer is in periodic mode. So I think the problem is not because the wrong configuration.

Re: A question about APIC Timer

Posted: Fri Aug 16, 2013 12:54 pm
by AlexMode
nbdd0121 wrote:
Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
I set the LVT Timer to be 0x20020, which means the interrupt vector is 0x20 and because the bit 18:17 is 01, the timer is in periodic mode. So I think the problem is not because the wrong configuration.
Are you using Bochs? There is a bug in old Bochs versions (2.4.6 at least), that cause APIC timer to malfunction (there are some other APIC-related bugs). I spend a few hours with same problem (APIC timer in periodic mode fires only once in Bochs, but OK in Qemu and real HW) before I found bugreport. The only solution is to upgrade to recent Bochs version (2.6.2 works fairy well). It was first exception from rule "1) Bochs has no errors. 2) If you think there are error in Bochs, see 1 and try to review your code." in my experience :)

Re: A question about APIC Timer

Posted: Sat Aug 17, 2013 1:31 am
by nbdd0121
AlexMode wrote:
nbdd0121 wrote:
Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
I set the LVT Timer to be 0x20020, which means the interrupt vector is 0x20 and because the bit 18:17 is 01, the timer is in periodic mode. So I think the problem is not because the wrong configuration.
Are you using Bochs? There is a bug in old Bochs versions (2.4.6 at least), that cause APIC timer to malfunction (there are some other APIC-related bugs). I spend a few hours with same problem (APIC timer in periodic mode fires only once in Bochs, but OK in Qemu and real HW) before I found bugreport. The only solution is to upgrade to recent Bochs version (2.6.2 works fairy well). It was first exception from rule "1) Bochs has no errors. 2) If you think there are error in Bochs, see 1 and try to review your code." in my experience :)
I really appreciate your notice. I figured out my code worked fine on VMware.. Maybe it is caused by Bochs.. I also thought that bochs would not be wrong...

Re: A question about APIC Timer

Posted: Sat Aug 17, 2013 5:42 am
by stlw
There is much more chance that Bochs won't be wrong if you using latest version rather than archaic one.

Sranislav