Using an emulator to test my OS?

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
michaelg1987
Posts: 4
Joined: Wed Aug 22, 2007 9:39 pm

Using an emulator to test my OS?

Post by michaelg1987 »

I've been reading a lot on making my own (absolutely minimal) operating system, and I've got some code. The code compiles, I have a kernel image file, but the thing is, I don't know how to test it.

I've used QEMU before, but never for this purpose. I don't want to have to reboot my computer all the time in order to test my OS, and I was wondering if someone could explain to me how I can get my image to boot using QEMU - I have checked many resources, and while they all give advice on how to configure QEMU, they don't actually tell me how to get my binary, for instance, into an image file with a bootloader or something. Am I missing some terribly obvious resource? Apologies in advance if I am.
User avatar
JackScott
Member
Member
Posts: 1036
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Mastodon: https://aus.social/@jackscottau
Matrix: @JackScottAU:matrix.org
GitHub: https://github.com/JackScottAU
Contact:

Post by JackScott »

Would you mind telling me (us) the following things? Without them, we are really guessing.
  • Are you using GRUB?
  • Build environment? (Cygwin, UNIX, MinGW, etc)
The gist of the whole idea is that you create a file, mount it on loopback, and then write to the mounted file as if it were a filesystem (but this only works on UNIX, mostly).
The easiest way on Windows, if you are using GRUB, is to download a GRUB disk image off the net, write it to a floppy using rawwrite or similar, then write your kernel to the floppy. From there you can use rawwrite again to create another disk image, or use the floppy directly in QEMU.

http://www.osdev.org/wiki/Loopback_Device
http://www.osdev.org/wiki/RAWRITE

Hope this has been helpful.
michaelg1987
Posts: 4
Joined: Wed Aug 22, 2007 9:39 pm

Post by michaelg1987 »

I'm compiling under Linux, and uh, as for GRUB - I assume I should, it seems like the way to go from everything I read, I just don't know how to use it with my kernel.
User avatar
JackScott
Member
Member
Posts: 1036
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Mastodon: https://aus.social/@jackscottau
Matrix: @JackScottAU:matrix.org
GitHub: https://github.com/JackScottAU
Contact:

Post by JackScott »

If you haven't already, check out http://www.osdev.org/wiki/Bare_bones for information on how to interface GRUB with your kernel. Once you've done that, and created a kernel binary, it's time to write it onto a disk image. If you have no preference otherwise, it's best probably to use the mount/loopback method, which is described in the link I gave in my last post (http://www.osdev.org/wiki/Loopback_Device).

The instructions in part 1.1 are probably the most useful to you, but also remember to copy your kernel onto the image before you unmount. It might also be useful to edit the /boot/grub/menu.lst file before you unmount as well.
michaelg1987
Posts: 4
Joined: Wed Aug 22, 2007 9:39 pm

Post by michaelg1987 »

I figured out how to load it, via floppy image and loopback. My next question, do I HAVE to make it the size of a floppy? I see the next part of the tutorial about formatting bigger images and stuff, but it doesn't explicitly say if it'd be legal to just make the image bigger. Not a big concern right now, my kernel is tiny (12KB I think). Thanks for the links.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Yes - It gets loaded via an emulated floppy-disk-driver, so it has to be the correct size. Bigger images should be used as HDDs and loaded via your own ATA driver. (Or CD-ROMs, if you work that way.)
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Although you can use sparse HD images which only consume as much data as is on the virtual drive (plus some control info, I think).

Cheers,
Adam
User avatar
jerryleecooper
Member
Member
Posts: 233
Joined: Mon Aug 06, 2007 6:32 pm
Location: Canada

Post by jerryleecooper »

You can load your OS as a floppy image using the "-fda" option of qemu even if it's not the same size of a real floppy. Im doing it all the times. But be sure not to read past the limit of your file, or else you get errors. The only emulator that I know that request floppy images to be of the exact size is Virtual PC, but you don't want to use that. My OS text console is all blinking with that emulator, and it gives stranges errors sometimes.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

jerryleecooper wrote:(...) Virtual PC, but you don't want to use that (...) it gives stranges errors sometimes.
VirtualPC is a proper emulator. If it can't run your OS properly, I suggest you search the fault in your code rather than Microsoft's. VPC not working is usually an indication that there are bugs in your code qemu or bochs are unable to detect. So far, many of the bugs I have experienced inside VPC also (would have) occured on real hardware.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
jerryleecooper
Member
Member
Posts: 233
Joined: Mon Aug 06, 2007 6:32 pm
Location: Canada

Post by jerryleecooper »

I had a strange error, it was supposed to work :evil: I have done everything right :) But... :roll: I had my bootloader not working. Happened that my floppy disk was formatted as 1680k at least, I think I didn't copied my kernel correctly. :lol: SO I reformated the diskette, recopied my small kernel (32.5k) and now it's working. But there's the weird blinking, and the cursor is blinking also. But it's a shame my bootloader code doesn't support more than 1440k floppies size, really a shame. It's probably the floppy drive too? It's usb, a Mitsumi. :!:
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Combuster wrote:
jerryleecooper wrote:(...) Virtual PC, but you don't want to use that (...) it gives stranges errors sometimes.
VirtualPC is a proper emulator. If it can't run your OS properly, I suggest you search the fault in your code
Hi,

I've found a couple of exceptions to this (with VPC 2004). Firstly, it doesn't seem to like some methods of using a separate TSS to handle double fault exceptions. At the time, I looked this up on the MS forums and it seems to be an 'official' oversight.

Secondly, I had some problems with being able to set the VME bit in CR4, but did not get the functionality of VME that works on real hardware, Bochs and VirtualBox. At the time, others also seemed to report having noticed the same issue.

Generally, however, I would absolutely agree though - always check your code for errors before assuming it is a problem with VPC.

Cheers,
Adam
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

I use QEMU and sometimes Bochs when I need extra debugging info. Why do I use these? Because they can be invoked by the command line. This makes them simple to run.

My workflow turns into:
- CTRL-S in source file.
- Double click build script.
- Did it compile?
- - Yes - Double click run script.
- - No - Rip hair out.
My OS is Perception.
viki
Posts: 14
Joined: Tue May 08, 2007 11:57 am
Location: Poland

Post by viki »

I use (cygwin-ddd) or (mingw-insight) and grub-gdb-stub and for debugging purpose it works really nice. I've made a tutor about it with a simple kernels in asm and c (in polish) but I'm thinkin to translate it.
Post Reply