Page 1 of 2

Emulator library for OS testing

Posted: Fri Jan 23, 2015 2:27 pm
by madanra
It would be nice to be able to run automated tests for OS development.

Are there any emulator libraries that would be suitable for this?

I had considered writing a wrapper around Bochs, but it doesn't seem like there's a good way of eg. simulating a disk read failure, as Bochs emulates the whole system, so there's no way to hook into what happens when a disk read is requested.

Re: Emulator library for OS testing

Posted: Fri Jan 23, 2015 2:58 pm
by BrightLight
Why would you want to emulate a failure? Besides, it's actually easier to cause a failure than a success! :mrgreen:

Re: Emulator library for OS testing

Posted: Fri Jan 23, 2015 3:42 pm
by Roman
omarrx024 wrote:Why would you want to emulate a failure?
To test error-handling.

Re: Emulator library for OS testing

Posted: Fri Jan 23, 2015 3:44 pm
by BrightLight
Roman wrote:
omarrx024 wrote:Why would you want to emulate a failure?
To test error-handling.
But making a failure is actually too easy! It's actually easier than getting something to work right :mrgreen:

Re: Emulator library for OS testing

Posted: Fri Jan 23, 2015 9:25 pm
by SpyderTL
He wants to run his OS on a virtual machine, like BOCHS, and have the virtual machine "emulate" a hardware error, so that he can test his OS and make sure it handles the hardware error properly.

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 3:46 am
by Combuster
Why not use bochs, set a breakpoint, and mess stuff up from the debugger console? :wink:

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 3:53 am
by madanra
Sure, Bochs is fine for manual testing. I'm not sure that process can be automated, though.

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:06 am
by Combuster
No automation?

Code: Select all

$ cat cmds.txt
s 1000
regs
quit

$ bochsdbg64 -q -f bochsrc.conf < cmds.txt
(...)
Next at t=0
(0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b         ; ea5be000f0
<bochs:1> s 1000
00000000025i[MEM0 ] allocate_block: block=0x0 used 0x1 of 0x20
Next at t=1000
(0) [0x00000000000f05e2] f000:05e2 (unk. ctxt): push bx                   ; 53
<bochs:2> regs
rax: 0x00000000_00000171 rcx: 0x00000000_00000000
rdx: 0x00000000_00000000 rbx: 0x00000000_0000fff8
rsp: 0x00000000_0000ffc8 rbp: 0x00000000_0000ffc8
rsi: 0x00000000_00000000 rdi: 0x00000000_00000500
r8 : 0x00000000_00000000 r9 : 0x00000000_00000000
r10: 0x00000000_00000000 r11: 0x00000000_00000000
r12: 0x00000000_00000000 r13: 0x00000000_00000000
r14: 0x00000000_00000000 r15: 0x00000000_00000000
rip: 0x00000000_000005e2
eflags 0x00000006: id vip vif ac vm rf nt IOPL=0 of df if tf sf zf af PF cf
<bochs:3> quit
00000001000i[     ] dbg: Quit
(...)

$

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:12 am
by alexfru
This is a common testability problem. A common solution to it is to introduce a layer between your code and the underlying stuff (in your case, accesses to BIOS functions, I/O ports, perhaps some other things). This layer can simulate errors either in a controllable way (it has some configurable logic of when to simulate an error and your tests would use it) or random way (it just randomly simulates errors with some low probability, all by itself).

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:18 am
by madanra
@Combuster:
Hmm - I would need to find a way to capture the screen contents in an automated fashion as well, which could be tricky when the test is running on a headless server. I'll look into it...

@alexfru:
When I get on to higher level stuff, that will certainly be possible. But there will always be a few things where that's not possible - one extreme example would be a bootloader, where there's not space for an extra layer.

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:25 am
by Combuster
It's too simple. Just add more stock unix plumbing. Even stdout and stderr are nicely separated in what they render :wink:

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:32 am
by madanra
But stdout/stderr only contain the terminal output, not what's on the guest's screen? Unless you're using the 'term' output library, which is incompatible with debug mode in Bochs.

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:41 am
by Combuster
<bochs:1> help writemem
writemem <filename> <laddr> <len> - dump 'len' bytes of virtual memory starting from the linear address 'laddr' into the file
Tested and also works on video memory.

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:48 am
by alexfru
madanra wrote:When I get on to higher level stuff, that will certainly be possible. But there will always be a few things where that's not possible - one extreme example would be a bootloader, where there's not space for an extra layer.
Sure, there will always be tight places limiting the spectrum of available methods. If your 1st stage bootloader fits into 512 bytes, indeed, there isn't much room for extra stuff. But you can create several variations of it with hardcoded errors and you can test it manually or semi-manually (depending on what information you can extract from the environment, e.g. indications of successful or failed boot).

Re: Emulator library for OS testing

Posted: Sat Jan 24, 2015 4:52 am
by madanra
Combuster wrote:
<bochs:1> help writemem
writemem <filename> <laddr> <len> - dump 'len' bytes of virtual memory starting from the linear address 'laddr' into the file
Tested and also works on video memory.
Fair enough :)

Now to work out how to reliably determine when the code is finished...