The PIT and VirtualBox

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.
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

The PIT and VirtualBox

Post by -m32 »

Has anyone else had issue with the PIT when using VirtualBox?

No matter what frequency value I use, it seems that the PIT in virtual box takes the same amount of time - about 1 second. This makes for pretty crappy multi-processing. I have to assume then that Windows & other OSes don't use the PIT for their timing... because they run just fine in VirtualBox. :?

The function I've been using since my OS' epoch:

Code: Select all

void timer_set_freq(uint16 freq) {
   uint32 divisor = 1193180 / freq;

   outb(0x43, 0x36);
   outb(0x40, divisor & 0xFF);
   outb(0x40, (divisor >> 8) & 0xFF);
}
It of course works fine on real hardware and in Bochs....
itisiuk
Member
Member
Posts: 98
Joined: Mon Mar 24, 2008 1:46 pm

Re: The PIT and VirtualBox

Post by itisiuk »

thats doing the same as my code so it must be a problem with virtbox although mine is running in qemu.

windows and others use the apic timer instead of the pit so that might be why they run fine.

i wouldnt know what todo about this problem other than contact the ppl who made virtbox and ask about the problem
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: The PIT and VirtualBox

Post by JamesM »

The PIT works fine in virtualbox for me!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: The PIT and VirtualBox

Post by AJ »

Same here - I've never had a problem with it. In fact, Bochs is the only one that ever gave me a problem when I forgot to sync properly.

Cheers,
Adam
User avatar
octa
Member
Member
Posts: 50
Joined: Sat Jun 28, 2008 9:15 am

Re: The PIT and VirtualBox

Post by octa »

i hav this prob in bochs !!!
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: The PIT and VirtualBox

Post by -m32 »

That's weird because I've tried this on several host machines too and I get the same results.

What's weirder is, if (using VirtualBox) I set the PIT to ~1000Hz and I repeatedly query the RTC for the current time, it takes about 35 seconds for one second to pass on the RTC... And if I set the PIT to 100Hz, it takes about 2-4 seconds for one second to pass... If I set it to 10Hz I get almost accurate timing... :shock:

Changing the frequency of the PIT shouln't affect the RTC -- it doesn't on real hardware. I don't get it... :?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: The PIT and VirtualBox

Post by JamesM »

-m32 wrote:That's weird because I've tried this on several host machines too and I get the same results.

What's weirder is, if (using VirtualBox) I set the PIT to ~1000Hz and I repeatedly query the RTC for the current time, it takes about 35 seconds for one second to pass on the RTC... And if I set the PIT to 100Hz, it takes about 2-4 seconds for one second to pass... If I set it to 10Hz I get almost accurate timing... :shock:

Changing the frequency of the PIT shouln't affect the RTC -- it doesn't on real hardware. I don't get it... :?

Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.

Not only that, but emulators run as user space processes and as such it is difficult->impossible to have a clock emulation frequency greater than the host OS's fastest signal/interrupt mechanism, so at a frequency of 1KHz who knows what the emulator is actually doing, because it sure as hell can't get vtimer interrupts at that frequency...
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: The PIT and VirtualBox

Post by -m32 »

Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.
Well my polling loop is only 3 lines, outb, inb and putc. I can't imagine that such a small amount of machine instruction causes such a HUGE delay. Besides, If I use a frequency like 100Hz, there's no reason to have it take 4 seconds for one second to register in the RTC. 100Hz is completely reasonable.
Not only that, but emulators run as user space processes and as such it is difficult->impossible to have a clock emulation frequency greater than the host OS's fastest signal/interrupt mechanism, so at a frequency of 1KHz who knows what the emulator is actually doing, because it sure as hell can't get vtimer interrupts at that frequency...
I understand that, my point was only to demonstrate the major discrepancy in the timing as it is not experienced in any other machine virtualizer.

Thanks for the feedback.
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: The PIT and VirtualBox

Post by suthers »

Code: Select all

void timer_set_freq(uint16 freq) {
   uint32 divisor = 1193180 / freq;

   outb(0x43, 0x36);
   outb(0x40, divisor & 0xFF);
   outb(0x40, (divisor >> 8) & 0xFF);
}
What's the point of doing the '& 0xFF' to pass the top bite of the divisor?
(Just as a side note)
Jules
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: The PIT and VirtualBox

Post by -m32 »

suthers wrote: What's the point of doing the '& 0xFF' to pass the top bite of the divisor?
(Just as a side note)
Jules
None, it's just me being anal ;)
User avatar
suthers
Member
Member
Posts: 672
Joined: Tue Feb 20, 2007 3:00 pm
Location: London UK
Contact:

Re: The PIT and VirtualBox

Post by suthers »

:lol:
But if it is a problem of your polling loop being to long it might help a bit :lol:
Jules
kscguru
Member
Member
Posts: 27
Joined: Sat Jan 19, 2008 12:29 pm

Re: The PIT and VirtualBox

Post by kscguru »

-m32 wrote:
Has it occurred to you that the frequency the RTC updates at isn't changing, but the number of instructions executed per second in your main polling loop will decrease as you increase the frequency of interrupts? Thus, maybe it's just that the emulation isn't fast enough to cope and your RTC polling loop ends up running dog slow.
Well my polling loop is only 3 lines, outb, inb and putc. I can't imagine that such a small amount of machine instruction causes such a HUGE delay. Besides, If I use a frequency like 100Hz, there's no reason to have it take 4 seconds for one second to register in the RTC. 100Hz is completely reasonable.
I understand that, my point was only to demonstrate the major discrepancy in the timing as it is not experienced in any other machine virtualizer.
outb/inb and putc (which I assume writes to the VGA framebuffer internally?) are some of the most expensive instructions to virtualize. A lot of virtualizers will trap on the outb, trap again on inb, get through a few dozen putc instructions and trap on the framebuffer write ... each trap is tens or hundreds of thousands of instructions. It is astonishingly tricky to keep time moving in a VM, and almost impossible with a hard-to-virtualize workload like an I/O-heavy tight loop. To get into too much detail ... I/Os are extremely bad for VT/SVM-based virtualizers, so VIrtualBox (which is VT-based KVM + qemu internally) will do badly but VMware (which can run in other modes) and Bochs (which is all emulation) are likely to do better.

Put more work in the loop, or a "sti; hlt;" (which is often used to "catch up" time) and wait for interrupt delivery from the PIC.
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: The PIT and VirtualBox

Post by -m32 »

Anyway, this kind of deviated from my original point.. setting the PIT in VirtualBox doesn't seem to have an effect on the frequency of interrupts fired. 10Hz, 20Hz, 100Hz, 1000Hz, whatever, it still takes at least 1 second to get an interrupt. - And that's with no code in my int handler except putting a period (.) on the screen. No matter how much machine instruction there is, still 1 second for the PIT interval.

Like I said it doesn't happen on any other virtualizer or hardware.

Sigh.... maybe I should just submit a bug report. :roll:
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: The PIT and VirtualBox

Post by JamesM »

Possibly, but as I said I don't get the same behaviour. Is your host kernel compiled tickless, by any chance?
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: The PIT and VirtualBox

Post by -m32 »

JamesM wrote:Possibly, but as I said I don't get the same behaviour. Is your host kernel compiled tickless, by any chance?
Nope ;)

Tried it on various hosts on different boxes. As for OSes: XP, Vista x64, Fedora 8. Same thing.
Post Reply