Friday, July 08, 2005

Loading the kernel

After POST [], the BIOS looks at the first sector of the primary boot device. If boot code is written in the first sector, it is loaded into the memory at address 0x7C00 by the BIOS and the system jumps to that code. Now, how does the BIOS identify the boot loader code? Since, this boot code is the first to get loaded into memory, we call it as Primary Bootloader, which can only be a maximum of 512 bytes. The last 2 bytes of a boot loader has, 0xAA55. This is the standard signature for any primary boot loader. Since, the primary loader needs to be less than 512 bytes, it should be written in x86 assembly language.

The primary loader loads either secondary bootloader or kernel into memory. The choice is left to you. The secondary loader does not have the constraint of being less than 512 bytes. It can be of any size and it can be written in a high-level language, such as C, C++, etc. The primary job of a secondary loader needs to set up the environment for kernel, i.e., GDT, IDT, etc, load the kernel and jump to it.


Types of kernel

Since, I am running out of time today, I will explain you the types of kernel shortly. Basically, there are three types of kernel:

  • Monolithic Kernel
  • Micro Kernel
  • ExoKernel

As the name itself suggests, the kernel has every services like, FS Management, MM, Process Management, etc. in the kernel space. It does not run as a seperate process. So, as you guess, there is no context switching, when you ask for a service. But, the probability of a monolithic kernel getting struck is more. Because, if there is a bug in the kernel itself, nothing can rescue it. Linux and Windows are good examples of Monolithic kernel. Linux, being a monolithic kernel, you can insert modules into the kernel dynamically using insmod command.

Micro Kernel:
Micro kernel runs all the services as a daemon in the user space. So, if a problem occurs in any of the service, the kernel will be able to decide what to do next. But, you pay-off the time to switch to a service in this type of kernel. Micro kernels are some what difficult to design and build than the monolithic kernel. There are always a discussion over the internet, talking about the advantage and disadvantages of monolithic and micro kernel.

Exo Kernel:
Exo kernel is not yet stabilized. It's under design and research. The user mode processes running in this type of kernel has the ability to access kernel resources like process tables, etc directly.

Requirements of OS Deving

Requirements for writing an operating system:
Ok. Let me get into the topic straight away. Today, we shall see, what are the software requirements for developing an OS.
First, decide which platform are you gonna use for your os deving. You can either choose Windows or Linux. Wide variety of tools are available in both platforms.
* Text editor - (I would recommend ConTEXT for windows users, good syntax highlighting and easy to use)
* Assembler NASM (Netwide ASeMbler), etc.If you are very well used to any other assemblers, you can use it. MASM, TASM and FASM are some other assemblers to mention. You will be required to write some part of your OS in assembly language, such as primary bootloader, which when written in other langages will take more than 512 bytes to fit in.
* C Compiler - GCC, etc.You can choose to write OS as a whole in x86 assembly language or by mixing some high level languages such as C and C++ with ASM. How to mix asm and C? That, I will explain you latter.
* Linker - ld, jloc, etc.If you are a good designer, you will be creating multiple files for your OS and compile it individually. At last, you will need a single file by linking all the compiled files. There's where a linker is used. A linker takes multiple input object files and finally gives you a single linked file. Linkers like ld, gives you the full freedom to specify code, data, etc. segments in the linked file through a special file called linker.ld.
* Cygwin or DJGPP [If you are deving in Windows]If you choose to use Linux command and other stuffs like gcc, ld in Windows, you will surely need Cygwin or DJGPP installed.
* partcopyFor windows users only. Linux users, don't worry. We have dd :-). Your primary loader needs to be written to the first 512 bytes of the floppy or harddisk image.
* Virtual floppy drive - VFD for windows users.Again linux users, dont' bother. I will explain you later how to mount your floppy or hard disk image using linux loop devices. When you coding hard, you will be required to write boot loader or your kernel file to your physical floppy drive to test it. This method has two disadvantages. First thing is, the number of floppies you are gonna spend. The second is, the time. Since disk drive accesses take more time, you have to wait some till it gets writed into the drive.
* And at last, an x86 machine simulator, if you choose x86 processor -BOCHS, etc.
Operating systems cannot be run like other application programs. You have to reboot your system everytime to see the results. Don't worry. Your fellow devers has made it easy. Simply, get the image of the floppy drive or hard disk (in which your files are residing) and give it to the bochs. Bochs will do the rest. It will show you the results that you will see in the real hardware. But, we cannot expect 100% accuracy. My suggestion would be to test your OS in the real system, at some regular check points, i.e., when you see a new functionality working in the BOCHS.

Note: Please, see the links for the above mentioned tools in the LINKS section of the LEXOS - main blog page.

Though, I have just now started my way in OS deving, I have given you the basic softwares that you will require. Finally, apart from software requirements, you will need lots of time for debugging itself and persistance, when you see your code doesn't work for a long time :-) Hope this will be helpful for beginners. Comments welcome.