Page 1 of 1

Bochs PIT broken?

Posted: Thu Jul 23, 2015 12:28 pm
by tkausl
Hello,

I've tried setting up the PIT with the following code:

Code: Select all

  uint32_t divisor = 1193182 / 50;
  outb(0x43, 0x36);
  u8int l = (u8int)(divisor & 0xFF);
  u8int h = (u8int)( (divisor>>8) & 0xFF );
  outb(0x40, l);
  for(int i = 0; i < 1000000; i++); // Does neither work with nor without this loop
  outb(0x40, h);
For some Reason, Bochs spams me with timer-interrupts, a lot more than i requested.My first and only process sleeps for 50 ticks, prints something and goes back to sleeping. In Bochs, this spams my terminal like hell, if i start it in VirtualBox, i get one print (approx.) every second (its like every ~.9 seconds or so). Am i doing something wrong here?

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 1:54 pm
by bradbobak
I had this issue before..i think adding the following to the bochsrc.txt may solve the problem.

Code: Select all

clock: sync=slowdown

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 2:57 pm
by tkausl
bradbobak wrote:I had this issue before..i think adding the following to the bochsrc.txt may solve the problem.

Code: Select all

clock: sync=slowdown
It helps, but now everything is loading like hours :mrgreen:

Code: Select all

clock: sync=realtime
seems to help too, i've to test this tomorrow.

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 3:08 pm
by Kazinsal
Bochs is a terribly old emulator from the 90s. It has barely changed since then, as you can probably tell by the near complete lack of anything resembling a sane interface and configuration structure.

Don't use Bochs. Use a sane virtual machine that can use hardware virtualization and dynamic translation like QEMU or VirtualBox.

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 4:13 pm
by Nable
Kazinsal wrote:Bochs is a terribly old emulator from the 90s. It has barely changed since then, as you can probably tell by the near complete lack of anything resembling a sane interface and configuration structure.

Don't use Bochs. Use a sane virtual machine that can use hardware virtualization and dynamic translation like QEMU or VirtualBox.
Hey, please, cool down a bit. Bochs is a simulator, it's really accurate and provides unique information when something goes wrong with your custom software.

QEmu and VirtualBox are quick-and-dirty solutions (OK, QEmu-KVM isn't dirty but it's still not very useful while debugging some obscure cases that you have while writing your own OS or VMM) for running common products (Windows/*nix) that you aren't going to debug.

Bochs can be compared to AMD SimNow or, possibly, Simics.
It's a great tool but as most good tools it's neither versatile nor trivial to use.

Btw, it's funny when one says "sane" about VMM that can happily run broken code that doesn't work on real hardware (and in Bochs, yes). Isn't it the real insanity?

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 4:23 pm
by Brendan
Hi,

For emulators, there's 3 very different use cases:
  • You want it to run as fast as possible while maintaining "virtual time = real time". In this case it's impossible to make it deterministic. This is the "sync=realtime" option in Bochs.
  • You want it to be deterministic while maintaining "virtual time = real time". In this case it can't run as fast as possible. This is the "sync=slowdown" option in Bochs.
  • You want it to be deterministic and run as fast as possible. In this case it can't ensure "virtual time = real time". This is the "sync=none" option in Bochs.
For "sync=slowdown"; the "IPS" setting determines how fast the virtual machine should try to run. If Bochs determines that the virtual machine is running faster than IPS says it should then Bochs will slow the virtual machine down; and if Bochs determines that the virtual machine is running slower than IPS says it should then there's nothing Bochs can do about it (IPS is set wrong) and "virtual time = real time" gets broken. This means that for this case you probably want to set "IPS" to about "90% of as fast as possible" so that Bochs doesn't deliberately slow it down too much, but there's no risk of "virtual time = real time" getting broken.

For "sync=none"; the "IPS" setting is used to determine how much virtual time has passed (where virtual time has nothing to do with real time at all). E.g. if you set IPS to 100000 then Bochs does 100000 instructions per virtual second (where 1 virtual second might be 10 ms of real time, or 10 seconds of real time, or anything else). In this case, you can set the IPS setting to anything you like, but might want to set IPS so that virtual time is close to real time.

Most emulators aren't as flexible as Bochs and force you to use the equivalent of "sync=realtime" without giving you any choice. This is fine for most things, but "non-deterministic" makes it extremely hard to debug things like race conditions (that are effected by different timing).


Cheers,

Brendan

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 4:57 pm
by tkausl
Thanks Brendan for the in-depth information.

Normally i wouldn't care, but i've implemented multithreading now and i was testing my sleep() function, i want/need to precisely know tick-speed and if my 11931 divisor just ticks like hell, there is nothing i can do about :( But in VirtualBox at least it looks like 100 ticks are 1 second, might be little bit more or less.

Re: Bochs PIT broken?

Posted: Thu Jul 23, 2015 5:01 pm
by Peterbjornx
In my experience QEmu's PIT isn't to be trusted either: my kernel used it as its timebase and although it kept accurate time on real hardware it was unable to do so in QEmu: a single second took over half a minute!