User:Zesterer/Bare Bones

From OSDev.wiki
Revision as of 02:53, 29 March 2017 by Zesterer (talk | contribs)
Jump to navigation Jump to search
Difficulty level

Beginner
Kernel Designs
Models
Other Concepts

This is an introductory tutorial for kernel development on the x86 architecture. It is a minimal example only, and does not show you how to structure a serious project. However, completing this project is a good first step for aspiring kernel developers.

By the end of this tutorial, you will have the following:

  • A simple kernel written in C and x86 Assembly that is capable of displaying a message and can be run from an emulator or on real hardware.
  • A rudimentary understanding of the x86 and x86 assembly.

What This Is Not

  • A 'make your own operating system' tutorial. Developing a complete operating system requires years of work and, in most cases, decades of experience.
  • A one-size-fits-all introduction. There are many languages, architectures and approaches to operating system development. However, this is the most common route taken by beginners.

Requirements

Before proceeding, make sure you have the basics ready.

Required Knowledge

It is important to make sure that you have enough basic knowledge. Kernel development has an extremely steep learning curve.

  • A basic understanding of the C programming language. You should be comfortable with functions, pointers, casting and flow-control.
  • A basic understanding of computer architecture. You should know the difference between ROM and RAM, you should know what CPU registers are, and what machine code & executables are.
  • A basic understanding of computational logic and mathematics. You should understand how hexadecimal & binary work, as well as what bitwise operators and the 'stack' is.
  • Some basic command-line skills. You should understand how to run simple commands, navigate your filesystem and manipulate files.

Required Tools

You will also require some software tools to develop this project.

  • A UNIX-like operating system that supports operating system development well such as Linux or a UNIX-like environment (MinGW or Cygwin if you are using Windows).
  • A text editor of some sort, preferably with syntax highlighting (it will make your life easier). I recommend the cross-platform Atom editor.

Some Background Knowledge

First, a little background information.

We will be developing our kernel for the x86 architecture. The x86 is a family of computer architectures first introduced in the 1970s. Most modern PCs are backwards-compatible with x86 however, so we should have no problem running our finished kernel on real hardware.

The x86 is a CISC (Complex Instruction Set Computer) architecture. This means that it has a large instruction set that we can manipulate in order to execute programs.

We will be compiling our kernel with our current operating system, with the intention of it running 'free-standing' (i.e: independent of any other OS). For this reason, we will be using a 'cross-compiler' to create our final executable. Cross-compilation is the first stage of writing a new operating system. After a huge amount of work, it is possible to make an operating system 'self-hosting'. This means that it is capable of compiling itself rather than relying on an existing OS. However, that is a long way down the road. For the foreseeable future, you will be cross-compiling your operating system.

We'll be writing the kernel in x86 Assembly and the C programming language. When the x86 first loads up our kernel, it won't yet be in a fit state to run C code. This is why we must use Assembly to first set up a basic C environment. Once this is done, we can write (most) of the rest of the kernel in C.

The x86 is a complex architecture with various different CPU states and modes. To avoid having to deal with them right now, we'll be using the GRUB bootloader to load our kernel into memory and set up a stable 32-bit environment.

To test our operating system, we'll be running the kernel in QEMU. QEMU is an emulator and will allow us to test our operating system without rebooting real hardware after every change.

Building A Cross-Compiler

Main article: GCC Cross-Compiler, Why do I need a Cross-Compiler?

TODO

The Code

Please remember, this is a minimal setup. A more advanced kernel project will have a more complex code structure, as well as an automated build system.

For now, we'll be using 3 files. They are:

start.s     - This file will contain our x86 Assembly code that starts our kernel and sets up the x86
kernel.c    - This file will contain the majority of our kernel, written in C
linker.ld   - This file will give the compiler information about how it should construct our kernel executable and link the previous files together

First, we create

start.s

. To best understand the code, I recommend typing it out yourself so you can understand each part in detail.

// TODO