Archive for the ‘OS Development’ Category

Well, this is something I’ve always wanted to learn more about; how operating systems work and how to write one of my own, and after a little light reading of Tanenbaum’s “Modern Operating System” I decided to give it a try. It’s pretty much the hardest thing I’ve ever attempted to do, and I haven’t even got an OS per se, just a program which can print out text to the screen after the BIOS has booted. Here’s a boring screenshot of it running in QEMU:

This Windows-killer took me the better half of a week to figure out, I’m sure I’ll have a fully functioning GUI, Nvdia drivers and a TCP stack by next Tuesday ;)

I’ve had to pick up a bit of x86 assembler on the way, with hopefully a lot more coming. A few of the functions look like this:

; Initial entry point
[extern kmain]
[global load]
load:
    mov esp, stack_end
    push eax
    push ebx
    call kmain
    cli

hang:
    hlt
    jmp hang

; Returns CPU details
[global get_cpuid]
get_cpuid:
    push ebp				; save the base pointer
    mov ebp, esp			; move the current stack pointer to ebp
    mov eax, [ebp+8]		; get first parameter (mode)
    mov ebx, [ebp+12]		; get second parameter (pointer to string)
    push ebx
    cpuid
    pop eax
    mov [eax], ebx
    mov [eax + 4], edx
    mov [eax + 8], ecx
    leave
    ret

[global is_in_protected_mode]
is_in_protected_mode:
	xor eax, eax
	smsw ax
	and ax, 1
	ret

call kmain transfers control over to a C function which inits the rest of the (insignificant) kernel.

void kmain(void * mbd, unsigned int magic)
{
	gfx_init();
    gfx_clear();

    if(magic != 0x2badb002) {
		oops("Invalid magic number: %d", magic);
    }

    kprintf("Booting ZoofOS...\n");

    char cpuid[20];
    get_cpuid(0, cpuid);

	int mode = is_in_protected_mode();

    kprintf("CPU is a %s, and is running in %s mode.\n", cpuid, (mode) ? "protected" : "real" );
}

The most interesting thing with kernel development is that you have to do everything from scratch, no standard libraries, no nothing.

ZoofOS isn’t much right now and never will be, but it will be an incredible learning experience.