Skip to content

A kernel from scratch, without any existing software, API, or such.

Notifications You must be signed in to change notification settings

AndreIglesias/Kernel-From-Scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kernel-From-Scratch

A kernel developed from scratch without relying on any existing software, APIs, or frameworks.

Kernel-From-Scratch

Table of Contents

  1. Project Overview
  2. Setup
  3. Kernel Essentials
  4. Compilation and Running

Project Overview

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.

KFS

Setup

KVM

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).

KVM

Dependencies Installation

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

Check for Virtualization Support

Ensure your system supports KVM by running:

kvm-ok
# Successful output:
# INFO: /dev/kvm exists
# KVM acceleration can be used

Cross-Compiler

A Cross-Compiler is required to compile the kernel as the host and target systems may differ. Follow the cross-compiler setup instructions.

GCC compiler

Kernel Essentials

Entry Point (boot.s)

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 Code (kernel.c and terminal.c)

  • 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);
    }
}

Linker Script (linker.ld)

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) }
}

Configuration (grub.cfg)

The grub.cfg file configures GRUB to load the kernel.

menuentry "kernel-from-scratch" {
    multiboot /boot/kernel.bin
}

Compilation and Running

Requirements

  • The ix86-elf cross-compiler inside the Dockerfile from the directory xcompiler.

Steps

  1. 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
  2. Clean Build Artifacts: To clean the object files, kernel binary, and ISO, run:

    ./build.sh fclean
  3. Run the Kernel: Use QEMU to boot the kernel:

    qemu-system-i386 -cdrom kernel.iso
    # Or
    ./run.sh

About

A kernel from scratch, without any existing software, API, or such.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published