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.
[SOLVED] A question about APIC Timer
[SOLVED] A question about APIC Timer
Last edited by nbdd0121 on Wed Aug 21, 2013 5:35 pm, edited 1 time in total.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: A question about APIC Timer
The APIC timer has multiple modes. My guess is it was left in one shot mode.
Re: A question about APIC Timer
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.Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
Re: A question about APIC Timer
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 experiencenbdd0121 wrote: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.Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
Re: A question about APIC Timer
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...AlexMode wrote: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 experiencenbdd0121 wrote: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.Owen wrote:The APIC timer has multiple modes. My guess is it was left in one shot mode.
Re: A question about APIC Timer
There is much more chance that Bochs won't be wrong if you using latest version rather than archaic one.
Sranislav
Sranislav