How do I make a basic operating system?

I will tell you about my personal experience of writing a x86 64 bit OS from scratch.  Like many others who have answered, I took an OS class last semester.  You can find source code of my OS here: mysterious4 / SBUnix – Bitbucket

Programming OS is one of the most interesting and satisfying experience in terms of learning.

Get yourself familiar with qemu (x86 emulator), gdb (debugging), ctags (Navigating code), git (maintaining repo).

I hope you have sufficient knowledge of C, ASM, x-86 architecture, calling convention for x86 .

Firstly, I think you should really ask yourself what level of complexity you wish to have in your OS. Sketch out the rough design, components you wish to program etc. 

If learning OS concepts is your main aim, I would suggest start with an existing implementation; something like MIT – JOS. 6.828 / Fall 2011 / Overview
It will provide you with skeleton code for memory management, page table setup etc and then you would incrementally add support of various syscalls, interrupt handlers etc.
—————————————————————————————————————–
If you wish to go the hard way and program everything yourself, roll up your sleeves, its going to be exhaustive. I will enumerate steps for you:

Step 1. Writing your own bootloader: Use BOCHS which is a 32bit emulator. Read some theory about where BIOS is loaded, real modes, protected mode and then start implementing. This link might help you : Category:Babystep – OSDev Wiki

Step 2. Write small code for defining your kernel entry point where before calling main, you will setup your stack, GDT tables etc.

Step 3. Now you have task of writing ISR (interrupt service routines). Start from basic stuff. Set up your own GDT, IDT tables. Write your own printf by writing to video memory at 0xB8000. Refer this Printing To Screen 

Step  4. Writing code for PIC (programmable interrupt microcontroller). PIC tells processor what IRQ to call within our IDT (interrupt descriptor table) when interrupt is recieved from device. Refer this : Operating Systems Development Series. PIT (programmable interrupt timer) is your system clock. Write simple timer handler.

Step  5. Write simple device driver for your keyboard by mapping its scan codes. Write this into your keyboard interrupt handler.

Step 6. Now comes part of handling physical memory. Your BIOS e801 map will give you total main memory available. Further when you will come down to paging, it is going to request you physical pages of size 4 Kb. Divide memory into page size and have a bit-map/free list type physical memory manager which will keep track of occupied and free pages.

Step 7. Now arrives the mammoth task of programming virtual page tables. Start by reading first some theory that you won't usually find in OS books like one's which explain when to load cr3 register with physical addr of pml4 in case of 64-bit to enable paging, self referencing page tables etc. Map your kernel to higher memory address.
Write code for heap memory management, kernel malloc etc.

Step 8. Now comes part of process management. Define simple datastructres for your managing your processes – PCB, MM_struct, task_struct (required later on) etc. (refer linux 1.0). Now write procedure for loading simple kernel level process. Once you have this running, go on do define ring permissions and then user level processes.
Write simple round robin scheduler by keeping simple runqueue.

Step 9. Once you have processes running, now you have job of loading processes from memory. Define simple tarfs kind of in memory file system, then procedure for reading elf binary and loading it in your memory.

Step 10. If you want to proceed more, you have task of writing your own file system and later on you can go on to add support for network stack, multi threading etc.

some references for your reading:
1. Your bible – Expanded Main Page
2.
osdever.net
Bran's Kernel Development Tutorial: Getting Started
3. BrokenThorn Entertainment
4. 1.-Environment setup

Answer by Aniket Alshi:

I will tell you about my personal experience of writing a x86 64 bit OS from scratch.  Like many others who have answered, I took an OS class last semester.  You can find source code of my OS here: mysterious4 / SBUnix – Bitbucket

Programming OS is one of the most interesting and satisfying experience in terms of learning.

Get yourself familiar with qemu (x86 emulator), gdb (debugging), ctags (Navigating code), git (maintaining repo).

I hope you have sufficient knowledge of C, ASM, x-86 architecture, calling convention for x86 .

Firstly, I think you should really ask yourself what level of complexity you wish to have in your OS. Sketch out the rough design, components you wish to program etc. 

If learning OS concepts is your main aim, I would suggest start with an existing implementation; something like MIT – JOS. 6.828 / Fall 2011 / Overview
It will provide you with skeleton code for memory management, page table setup etc and then you would incrementally add support of various syscalls, interrupt handlers etc.
—————————————————————————————————————–
If you wish to go the hard way and program everything yourself, roll up your sleeves, its going to be exhaustive. I will enumerate steps for you:

Step 1. Writing your own bootloader: Use BOCHS which is a 32bit emulator. Read some theory about where BIOS is loaded, real modes, protected mode and then start implementing. This link might help you : Category:Babystep – OSDev Wiki

Step 2. Write small code for defining your kernel entry point where before calling main, you will setup your stack, GDT tables etc.

Step 3. Now you have task of writing ISR (interrupt service routines). Start from basic stuff. Set up your own GDT, IDT tables. Write your own printf by writing to video memory at 0xB8000. Refer this Printing To Screen 

Step  4. Writing code for PIC (programmable interrupt microcontroller). PIC tells processor what IRQ to call within our IDT (interrupt descriptor table) when interrupt is recieved from device. Refer this : Operating Systems Development Series. PIT (programmable interrupt timer) is your system clock. Write simple timer handler.

Step  5. Write simple device driver for your keyboard by mapping its scan codes. Write this into your keyboard interrupt handler.

Step 6. Now comes part of handling physical memory. Your BIOS e801 map will give you total main memory available. Further when you will come down to paging, it is going to request you physical pages of size 4 Kb. Divide memory into page size and have a bit-map/free list type physical memory manager which will keep track of occupied and free pages.

Step 7. Now arrives the mammoth task of programming virtual page tables. Start by reading first some theory that you won't usually find in OS books like one's which explain when to load cr3 register with physical addr of pml4 in case of 64-bit to enable paging, self referencing page tables etc. Map your kernel to higher memory address.
Write code for heap memory management, kernel malloc etc.

Step 8. Now comes part of process management. Define simple datastructres for your managing your processes – PCB, MM_struct, task_struct (required later on) etc. (refer linux 1.0). Now write procedure for loading simple kernel level process. Once you have this running, go on do define ring permissions and then user level processes.
Write simple round robin scheduler by keeping simple runqueue.

Step 9. Once you have processes running, now you have job of loading processes from memory. Define simple tarfs kind of in memory file system, then procedure for reading elf binary and loading it in your memory.

Step 10. If you want to proceed more, you have task of writing your own file system and later on you can go on to add support for network stack, multi threading etc.

some references for your reading:
1. Your bible – Expanded Main Page
2. Bran's Kernel Development Tutorial: Getting Started
3. BrokenThorn Entertainment
4. 1.-Environment setup

How do I make a basic operating system?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s