M3 Operating System Development

A chronicle of the development of the M3 operating system

M3’s Boot Sector/Loader – Design & Goals

leave a comment »

Now that we’ve figured out how to build a boot sector and boot a virtual PC with it, it’s time to start thinking about fleshing it out into a boot sector that actually does something.

Since a boot sector is limited to 512 bytes, there isn’t a whole lot that we can do. The primary job of the boot sector is to load the kernel into memory and hand control to it. Since I’m planning to write the entire kernel in C, I’d also like to get the processor into protected mode before handing control to the kernel. I think that the C compiler will spit out binaries that expect protected mode.

What’s protected mode? 

There are two different modes that a CPU can operate in – real mode and protected mode. In real mode, the currently executing code has complete access to all the instructions of the CPU. In protected mode, a limited set of CPU functionality is accessible by the currently running code. If code running in protected mode needs some feature of the CPU that is protected, it can trigger an interrupt, which will hand over control to the kernel. Then the kernel can decide how best to service the request.

Protected mode allows the kernel to have exclusive control of critical functions of the CPU, such as those that manage memory and hardware. It will allow the CPU to help us with things like paging, virtual memory, and multitasking – all desirable features of modern operating systems.

How do we get the CPU into protected mode? Well, there are several steps that must be taken, including setting up a Global Descriptor Table (GDT), initializing some segments, etc. Intel’s IA-32 Intel Architecture Software Developer’s Manual, Volume 3 goes into the steps in some detail, and is a necessary resource for programming IA-32 CPUs. Actually, all the volumes are necessary reads, IMO. Here’s Intel’s page where you can download these manuals for free.

A20 gate

The other important thing that the boot sector needs to do is to enable the A20 gate, which is needed to enable protected mode. What is the A20 gate, you ask? Well, it’s a logic gate attached to the A20 line, a part of the system bus, and it’s typically disabled by the BIOS before our boot sector starts executing. For a good description of the A20 gate/line, read the Wikipedia article. Why try to explain it myself when others have already done such a good job?

The M3 boot sector – Goals

So here’s a summary of what we’re trying to accomplish with the M3 boot sector:

  1. Enable the A20 gate
  2. Load the C kernel to the proper location in memory (TBD)
  3. Get into 32-bit protected mode
  4. Hand over control to the kernel

Along the way, we’ll print relevant status messages, if we have the space. I’m not really sure how much code is going to be involved to do all these things, but I do know that 512 bytes is NOT a lot of space. I am fully expecting to have to break this functionality into two separate entities – the boot sector, and a boot loader. This is often called a multistage boot. The boot sector performs some initialization, loads the boot loader into memory, and then transfers control to the boot loader.

The boot loader will not have a 512 byte size restriction, and so it will likely handle the heavy lifting – getting the processor into pmode, loading up the kernel, and handing over control. We’ll see if I can get everything to fit in 512 bytes, but I’m not that proficient in assembly yet, so my code is probably more verbose than it needs to be.

At any rate, I’m looking forward to the challenge.

Advertisements

Written by m3os

March 4, 2009 at 9:08 am

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

%d bloggers like this: