A kernel developed from scratch without relying on any existing software, APIs, or frameworks.
This project demonstrates the development of a basic x86 kernel. It includes setting up the bootloader, initializing the terminal, and printing text to the screen. The kernel is built using a cross-compiler and runs on KVM using QEMU.
KVM (Kernel-based Virtual Machine) is a leading open-source virtualization technology for Linux. It allows physical servers to host multiple, isolated virtual machines (VMs).
Install the necessary dependencies with the following command:
sudo apt-get install -y bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm xorriso mtools
Ensure your system supports KVM by running:
kvm-ok
# Successful output:
# INFO: /dev/kvm exists
# KVM acceleration can be used
A Cross-Compiler is required to compile the kernel as the host and target systems may differ. Follow the cross-compiler setup instructions.
The boot.s
file is the initial assembly code that sets up the processor environment and provides the entry point for the kernel.
/* Declare constants for the multiboot header. */
...
_start:
...
call kernel_main /* Enter high-level kernel */
...
kernel.c
: Contains the main kernel routines including terminal initialization and text display functions.
#include "kernel.h"
...
void kernel_main(void) {
t_terminal terminal;
terminal_init(&terminal);
terminal_putstr("42", &terminal);
}
terminal.c
: Provides functions to handle terminal operations such as printing characters and strings.
#include "kernel.h"
...
void terminal_putstr(const char *data, t_terminal *terminal) {
size_t i = 0;
while (data[i]) {
terminal_putchar(data[i++], terminal);
}
}
The linker.ld
script specifies how the various sections of the kernel should be linked together.
/* Example linker script */
ENTRY(_start)
SECTIONS {
. = 0x100000;
.text : { *(.text) }
.rodata : { *(.rodata) }
.data : { *(.data) }
.bss : { *(.bss) }
}
The grub.cfg
file configures GRUB to load the kernel.
menuentry "kernel-from-scratch" {
multiboot /boot/kernel.bin
}
- The ix86-elf cross-compiler inside the
Dockerfile
from the directoryxcompiler
.
-
Compile the Kernel: Ensure your project structure matches the required directory setup:
├── boot │ └── boot.s ├── grub │ └── grub.cfg ├── include │ └── kernel.h ├── kernel │ ├── kernel.c │ └── terminal.c ├── linker │ └── linker.ld ├── Makefile └── obj
Then run the following command to compile the kernel and create the ISO:
./build.sh
-
Clean Build Artifacts: To clean the object files, kernel binary, and ISO, run:
./build.sh fclean
-
Run the Kernel: Use QEMU to boot the kernel:
qemu-system-i386 -cdrom kernel.iso # Or ./run.sh