[SOLVED] A question about APIC Timer

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
nbdd0121
Member
Member
Posts: 60
Joined: Thu Jul 25, 2013 8:10 am

[SOLVED] A question about APIC Timer

Post 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.
Last edited by nbdd0121 on Wed Aug 21, 2013 5:35 pm, edited 1 time in total.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: A question about APIC Timer

Post by Owen »

The APIC timer has multiple modes. My guess is it was left in one shot mode.
nbdd0121
Member
Member
Posts: 60
Joined: Thu Jul 25, 2013 8:10 am

Re: A question about APIC Timer

Post 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.
AlexMode
Posts: 3
Joined: Wed Aug 14, 2013 3:00 am

Re: A question about APIC Timer

Post 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 :)
nbdd0121
Member
Member
Posts: 60
Joined: Thu Jul 25, 2013 8:10 am

Re: A question about APIC Timer

Post 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...
stlw
Member
Member
Posts: 357
Joined: Fri Apr 04, 2008 6:43 am
Contact:

Re: A question about APIC Timer

Post by stlw »

There is much more chance that Bochs won't be wrong if you using latest version rather than archaic one.

Sranislav
Post Reply