Sunday, July 31, 2011

OSDEV Series - Episode 2.1: Short recap

Rings

Rings offer a protection layer for programs. They allow certain levels of resource access to processes, so it keeps bad programs from messing things up. There are a total of 4 rings in most common architectures. However, many architectures have only two rings (e.g. x86_64), corresponding to ring 0 and 3 in this description.

Ring 0:
This is kernel mode or supervisor mode. This level has the least protection, and the most access to resources. When starting up, the OS runs in this mode unless it switches out. Interrupt handlers run in this mode.

Rings 1 and 2:
These rings are mostly used for device drivers. They offer more protection, but not as much as ring 3.

Ring 3:
This is the ring that most OS's use for applications. This ring is also called Userland, or Userspace. It has the most protection and the least resource access.

Most operating systems use only Ring 0 and 3. This is because rings 1 and 2 are unneeded, as device drivers can run in either ring.
Sometimes applications need access to resources that their ring wont allow. If they try to access them, a General Protection Fault (int 13) will be triggered, and the application shutdown. The application must interface with the kernel somehow, and mostly this is done with System Calls.



Kernel designs

Microkernels:
A microkernel only implements the basic services needed for applications, such as memory management, or multitasking. Other services implemented in user space, also known as servers, offer additional functionality that the kernel would offer in other designs. Such services may include networking, file systems etc.

Pros: Lightweight kernel, easy to maintain.
Cons: A large number of system calls is required for servers, and this costs a lot of performance.


Monolithic kernels:
All the services are run in kernel space

Pros:
Rich and powerful hardware access
Easier to implement than other designs

Cons:
All the services must run smoothly, otherwise the entire system could crash
Harder to maintain, since the kernel is pretty large

Examples of kernels:
UNIX (and UNIX-like)
DOS


Other kernel designs:

  • Hybrid kernels: A compromise between the two big designs. The most popular example of a hybrid kernel is Windows NT.
  • Nanokernels: Very small microkernels.
  • Exokernels: As little abstraction as possible. This gives very much power to applications. However, it is not a very deeply researched design, and implementation can be very difficult.



Abstraction

In computer science, abstraction is the process by which data and programs are defined with a representation similar to its meaning (semantics), while hiding away the implementation details. In other words, it is the method of hiding away ugly stuff behind really nice functions that programmers can use.

To exemplify, let's look at a typical file read operation. The application reads a file from the computer, by calling an fread() function from the system library. The fread() function itself, makes a system call, asking the kernel to read the file. The kernel goes through the virtual file system to find the file. First it finds the device, such as the hard disk or memory card, then reads the actual sectors, and then parses the file system. The sector read function may be calling the firmware present in the device.

All these steps are in fact abstraction layers. A read operation starts at top level, where the application is situated, and goes all the way down to hardware level.

OSDEV Series - Episode 2.1 - Rings and kernel designs

Section 2: The basics
Episode 1: 
Rings and kernel designs


Covered topics:

Theory: Rings, kernel designs, abstraction
Practice: Coding the entry point in assembly, creating linker and shell scripts for compiling

Project files: http://dl.dropbox.com/u/24832466/OSDEV/MyOS-2.1.zip