Visions of Operating System Development
Here are some snapshots of the system running on Bochs.
Floppy Device (Block)Driver/ Notes
Language and VM Oriented
Entry - 1
Operating System Development Introduction
So, you are about to deploy your Struts/Hibernate application on Tomcat/JBoss and you type ?ant deploy?. Refresh the browser and the initial tests works, repeat?yawn. You think to yourself, ?this is no fun, at all?. If you want some excitement(and I am not sure I can give it to you), why don?t you throw together a quick and dirty operating system?
Here is part 1 of our small series on developing a simple unix/linux style protected mode operating system. We want to look at it from the implementation standpoint as opposed to a lot of the theory on microkernels and monolithic systems. Let us just type ?make? on the host system and ?run?. Or, in this case we want to uses ?bochs?, so we would type bochs and the operating system will boot.
In the intel x86 world, there are two types of systems, the simple real mode operating systems that are 16-bit and operate on interrupt calls to the bios. Then there is the 32-bit protected mode operating systems that allow such things as task switching and hardware memory protection and all that good stuff. As you can probably guess, most modern operating systems use protected mode.
Bochs and OS Development
There is some debate among the hobbyist os developers community on if you should write to ?metal? or use x86 emulation software. Bochs is pretty standard and it will never be able to compete when it comes to testing against a real machine, but it does save a lot of time when testing basic x86 code. If you want to test out the latest features on your graphics card or some hidden intel pentium4, use your machine. Use a garbage test machine. And if you see smoke coming out of your monitor, run!!! Me personally, anything that saves me from waiting 2 minutes for my pentium1 machine to boot up will work. So, I use bochs. Like I said, bochs is emulation software, it claims to mimic everything that an x86 CPU and FPU will do. It even supports some simple graphics cards, VGA, networking and sound. The windows version uses SDL for the VGA aspect. You will need at least Xwindows to run the linux version. It works, you will run into ?timing? issues and strange memory behavior and there is a speed issue on anything less than a 1gig host machine. It is pretty simple to use, edit the bochs config file which basically says where your harddrive image or floppy drive image is located and type ?bochs?. And Bochs will run in an emulation environment based on the machine instructions from your image your provided, simple.
What belongs on the harddrive or floppy image? Well, your operating system. Bochs has been tested with Win95, many small versions of linux, etc, etc.
Your Operating System Boot Sector and Real Mode
At the the head of your floppy disk or hard drive is a 512 byte sector that ends in the byte sequence 0×55, 0xAA, this tells the bios to this device is bootable. In the code I am going to give you, we have a strange system. We are going to use GRUB which is going to eat up the 512 boot sector(with whatever that is), GRUB contains the loader which will load our real 512 boot sector. Here are the first couple of instructions of our boot sector(GCC assembly).
We are going to jump to memory location 0×0000:0×7c00 and starting executing the real mode code.
The realmode code assembly(located in my boot/bootsect.S) is normally the easiest to follow. One, it is in 16 bit words. Two, you aren?t dealing with the task switching and complicated memory constructs of protected mode. Three, you are allowed to use the bios for all your standard interrupt calls. You can use the harddrive, the floppy drive with simple INT calls. In protected mode you are dealing with the complexity of DMA and the TSS and GDT. Anyway, realmode gets you to protected mode. Why procted mode? Well, you can?t use 4GB of memory or play Half-Life 2 or run Tomcat/MySQL with bios calls. Oh by the way, accessing the bios is normally really, really slow, especially with VGA operations.
What have we learned, more of the boot stages?
Nothing. Moving on. The code below is based on the early linux kernel(0.0.11?) It actually was the code Linus used on Minix. So, the code below is an attempt at porting the 15,000 lines of assembly and C from a 1991 GCC to a 2005 version of GCC. The x86 instructions haven?t changed much, but you have to deal with changes in the host operating environment(Linus used Minix, I am using Gentoo 2005). Some of the operating system boot steps are the same as the modern linux kernel. Some of the code is the same as the modern linux kernel. The first boot step includes the 512 real mode code, the next step involves using the bios to copy the kernel to memory. Also, we need to perform the necessary steps to get into 32-bit protected mode. There is a small stage that unzips the kernel from its current memory location and moves it to another spot in memory. There is actually boot code that contains routines for unzipping a chunk of memory. Why? I don?t know. I think the kernel writers wanted most kernels to fit on a floppy, so there is a bit of crunching to get it within the 1.4MB space without drivers or other goodies. A full zipped kernel with all the drivers and other goodies might weight in at 2MB, this slimmed down operating system weighs in at 323KB.
Quick Jump Ahead
I ?might? cover some more of this later, download the code below, type ?make?, this contains a non-working kernel and boot code, code for testing with bochs. I have to do some code and build cleanups, but this should boot from the floppy ?image?. Here are some snapshots of what this little OS does.
Here is an expanded tree of the source code. There is the boot code at the top and bottom. The code 3 files are the real mode boot sector. The bottom contains the second stage boot code including the kernel unzipping routines.
Here is the GRUB bootup sequence. You can use GRUB for just about anything, small or large.
Here is the operating system launching. Most of the output is to test the different drivers in the OS. There is a simple timer driver, a floppy driver, console driver.
I am going to throw some source code at you. There is little documentation. You will probably bang your head trying to compile it, but you can trust me, it compiles on my system.
This makefile needs to be cleaned up, but it builds the small kernel to vmlinux.
This assembly code gets us to protected mode and starts executing the kernel.
GCC C Main Code, the entry point of the ?real? kernel
I have a couple of goals and how it relates to java. I want to add the hard drive and floppy drivers so that I can do block read/writes. I also want to add a filesystem. After that, I want to add a small JavaVM so that it executes java bytecode.
Feel free to email me with patches :)
Entry - 2