![]() |
General Reading »
Hardware & System »
General
Advanced
Debugging and Building Operating SystemsBy S KellerHow to build and test your own Operating System. |
C++, C, ASM, Windows, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Figure 1 - Running other Operating Systems in your current OS with a PC Emulator.
I think that one of the most important ways of being a real professional programmer is to design and build an Operating System. It is not enough to read how an OS works, you have to build one with your own hands.
Now, I would list what you will gain from learning how to develop an Operating System:
If you are not yet convinced, try it out and you would see how much fun it is to program Operating Systems.
This article is the second part of my previous article.
In this article, I would:
INT 13h, move the drive number to the register DL. 
Figure 2 � Bochs in work (Bochs is running my Boot sector from my other article).
For debugging and testing an OS you must have an x86 emulator, we would use Bochs.
Bochs is a portable open source x86 and AMD64 PCs emulator mostly written in C++ and distributed under the GNU Lesser General Public License. It supports emulation of processor(s) (including protected mode), memory, disks, display, Ethernet, BIOS and common hardware peripherals of PCs.
Many guest operating systems can be run using the emulator, including DOS, several versions of Windows, BSDs, and Linux. Bochs can run on many host operating systems, including Windows, Linux, Mac OS X and the Xbox.
Bochs is mostly used for operating system development (when an emulated operating system crashes, it does not crash the host operating system, so the emulated OS can be debugged) and to run other guest operating systems inside already running host operating systems. Some people use it to run old computer games inside their non-compatible computers.
To use Bochs, follow these steps:
lb 0x7c00), dump memory (x /100bx 0x7c00), single stepping (s 1), read registers (r), continue execution (c), dump all CPU registers (dump_cpu) etc.

Figure 3 � Bochs folder, and a simple configuration file.

Figure 4 � a typical mother-board.
All this you can build with Bochs by only writing a simple configuration to a text file. This is why Bochs is so wonderful:
You can configure and build a whole PC, all you need to do is write the PC configuration to some text file (for example, bochsrc.txt) and place it in the Bochs folder. Here is an example of an advanced configuration file:
#=======================================================================
# ROM-IMAGE:
#=======================================================================
romimage: file=$BXSHARE/BIOS-bochs-latest, address=0xf0000
#=======================================================================
# CPU:
#=======================================================================
cpu: count=1, ips=10000000, reset_on_triple_fault=1
#=======================================================================
# Amount of RAM in MB
#=======================================================================
megs: 32
#=======================================================================
# VGA ROM IMAGE
#=======================================================================
vgaromimage: file= $BXSHARE/VGABIOS-lgpl-latest
#=======================================================================
# VGA:
#=======================================================================
vga: extension= vbe
#=======================================================================
# FLOPPY:
#=======================================================================
floppya: 1_44= "MyImg.img" , status=inserted
#=======================================================================
# ATA0, ATA1, ATA2, ATA3 (Set up Hard disk and CD-ROM IRQ's and I/O address)
#=======================================================================
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata2: enabled=0, ioaddr1=0x1e8, ioaddr2=0x3e0, irq=11
ata3: enabled=0, ioaddr1=0x168, ioaddr2=0x360, irq=9
#=======================================================================
# ATA[0-3]-MASTER, ATA[0-3]-SLAVE
#=======================================================================
ata0-master: type=disk, path="MyOS\c.img" , mode=flat,
cylinders=20, heads=16, spt=63, translation=none
ata1-master: type=disk, path="MyOS\d.img" , mode=flat,
cylinders=2, heads=16, spt=63, translation=none
ata0-slave: type=cdrom, path=MyOS\cdrom.iso, status=inserted
ata1-slave: type=cdrom, path=MyOS\E.iso, status=inserted
#=======================================================================
# BOOT from:
#=======================================================================
boot: disk
#=======================================================================
# COM1, COM2, COM3, COM4:
#=======================================================================
Com1: enabled=1, dev="COM1"
#=======================================================================
# Parallel ports
#=======================================================================
parport1: enabled=1, file="LPT1"
#=======================================================================
# SB16:
# This defines the SB16 sound emulation.
#=======================================================================
sb16: midimode=1, midi=/dev/midi00, wavemode=1, wave=/dev/dsp,
loglevel=2, log=sb16.log, dmatimer=600000
#=======================================================================
# VGA_UPDATE_INTERVAL:
#=======================================================================
vga_update_interval: 300000
#=======================================================================
# MOUSE: support:
#=======================================================================
mouse: enabled=0
#=======================================================================
# ne2k: NE2000 compatible ethernet adapter
# (Now you can connect to the Internet):
#=======================================================================
ne2k: ioaddr=0x240, irq=9, mac=b0:c4:20:00:00:01,
ethmod=vnet, ethdev="c:/temp"
#=======================================================================
# I440FXSUPPORT: PCI-Controller:
#=======================================================================
i440fxsupport: enabled=1
#=======================================================================
# USB1:
#=======================================================================
usb1: enabled=1
#=======================================================================
# CMOSIMAGE:
#=======================================================================
#cmosimage: file=cmos.img, rtc_init=time0
#-------------------------
# PCI host device mapping (Inserting a PCI device).
#-------------------------
pcidev: vendor=0x1234, device=0x5678
#=======================================================================
# GDBSTUB: (GDB debugger)
#=======================================================================
#gdbstub: enabled=0, port=1234, text_base=0, data_base=0, bss_base=0
#=======================================================================
# IPS: (Instruction Per seconds)
#=======================================================================
ips: 10000000
#=======================================================================
# FOR MORE INFORMATION !!!!!
#=======================================================================
#For more information see the Bches Documentation
#and "bochsrc-sample.txt" (this is in the Bochs directory).
Have you ever known how easy it is to build a PC, you only have to write a couple of lines and run Bochs, and you have your own PC.
Here are some very useful debug commands:
c (continue executing).
s [count] (execute count instructions, default is 1) - for example, s.
lb addr (set a linear address instruction breakpoint) - for example, lb 0x7C00.
d n (delete a break point) - for example, d 1.
x /nuf addr (dump memory of a physical address) - for example, X /100bx 0x7C00 (dump 100 bytes (b) display in HEX format (X) at address 0x7c00).
r (list of CPU registers and their contents).
info tab (show paging address translation).
dump_cpu (dump complete CPU state).
set reg = expr (change a CPU register to the value of an expression) - for example, set esi = 2*eax+ebx. For more information, check the documentation that comes with Bochs.
Now I am sure you feel like a PC engineer working for some mother-board company. This configuration is a very wonderful thing, because you can build any kind of PC you want, and test your OS on it.
Another very good thing in Bochs is that it comes with source code, so it is like having all the hardware specification from the OS developer point of view. Also, it can help a lot for debugging your OS, because from the source code, you can know exactly what is wrong.
You can get the source code of Bochs (also for compiling with MSVC 6). I needed to do some hacking to get Bochs to compile with MSVC 6 and MSVS 2005. If you have compiled it, how did it compile for you? And what happened if you changed some of the #define identifier values, for example, #define BX_DEBUGGER 1 (in Config.h): did you successfully compile it?
Another good PC emulator (but it can't debug the OS) is Microsoft PC Emulator (you can download it from the Microsoft web site).
If you have any questions or you need help, feel comfortable to send me an e-mail. If you have a subject or a point you want me to write about, please note it in the forums (this would help a lot for future articles).
Here you can download the new source code to my Boot sector program from my first article.
These are the fixes I made in the BOOT Sector:
INT 13h, move the drive number to the register DL. I would continue writing if I see this article interests people.
The next in this series of articles that I would write would go in to the details of developing your own OS, for example, details like OS design, programming the file system, EXE format, setting up for Protected mode, switching to Protected mode, handling interrupts, programming the PIC, writing an ATA/ATAPI driver, writing a floppy driver, DMA, using BIOS32 (a special BIOS interface for Protected mode), writing your kernel with MSVC, driver for graphics display, stage 1 and 2 of loading the OS etc.
These are the tools you would need:
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Nov 2006 Editor: Smitha Vijayan |
Copyright 2006 by S Keller Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |