# KFS (Kernel from scratch) Series

## Summary

This documentation presents the key components and core concepts behind building this kernel. Each module is documented and available on GitHub, where versions correspond to each iteration. Aimed at programmers from hobbyist to advanced level, it is not a step-by-step tutorial but rather a holistic overview.

Many well-known OS development resources, while rich in content, tend to be heavily compartmentalized like wikis, lacking a broader perspective. This documentation fills that gap by offering a synthesized, big-picture understanding of the subject.<br>

1. [***Boot sequence primitives***](/thehiddenshape/system-and-networks/kfs-kernel-from-scratch-series/kfs-series-1-primitive-boot-sequences.md)**:** a bootable kernel loaded by GRUB, built on an assembly entry point, with a minimal library providing basic types and utility functions. It writes to VGA for screen output. We also configure and load the GDT and IDT to handle keyboard interrupts.
2. [***Memory***](/thehiddenshape/system-and-networks/kfs-kernel-from-scratch-series/kfs-series-2-memory.md)***:*** the memory subsystem covers the following: pagination, read/write permissions, user/kernel space separation, physical/virtual memory management, and heap allocator helpers (`kmalloc`, `kfree`, `ksize`, `kbrk` for physical, `vmalloc`, `vfree`, `vsize`, `vbrk` for virtual), alongside kernel panic handling.
3. [***Interrupts***](/thehiddenshape/system-and-networks/kfs-kernel-from-scratch-series/kfs-series-3-signals-and-interrupts.md)**:** hardware interrupts, software interrupts, an interrupts descriptor table, signal handling and scheduling, global panic fault handling, stack saving.
4. ***Processes*****:** basic data structure for processes, process interconnection such as kinship, signals, and sockets, process owner, rights on processes, helpers for the following syscalls: fork, wait, \_exit, getuid, signal, kill. Process interruptions, process memory separation, multitasking.
5. ***Filesystem*****:** a complete interface to read/write an IDE, complete interface to read/write/delete an ext2 filesystem, basic file tree (`/sys`, `/var`, `/dev`, `/proc`, `/sys`)
6. ***syscalls, sockets and env*****:** a complete syscall table with a syscall system, complete Unix environment, user accounts with login and password, password protection, inter-process communication socket, a Unix-like filesystem hierarchy
7. ***Modules*****:** registering kernel modules (creation/destruction), loading modules at boot time, implementing functions for communication/callback between the kernel and the modules
8. ***ELF*****:** a complete interface to read, parse, store, and execute ELF files, syscalls to read ELF files and launch a process with them, a kernel module in ELF, ready to be inserted at run time
9. ***The End*****:** fully functional basic binaries (`/bin/*`), libc implementation, POSIX-compliant shell.

## Kernel definition

A **kernel** is the core of an operating system. It's the fundamental program that bridges the gap between hardware and the software you use.

The kernel runs in "privileged mode" (in the *kernel space* region of memory) with full hardware access, while applications run in "user space" with restrictions. When an app needs hardware access, it makes a "system call" to ask the kernel to handle it.

Common examples of kernels include Linux, which is open source and monolithic, Windows NT which is hybrid, and XNU which powers macOS and is based on Mach and BSD.

Below is the definition from the well-known OSDev Wiki, which I find particularly clear and intuitive.

> *The kernel of an operating system is something you will never see. It basically enables any other programs to execute. It handles events generated by hardware (called interrupts) and software (called system calls), and manages access to resources.*
>
> *The hardware event handlers (interrupt handlers) will for instance get the number of the key you just pressed, and convert it to the corresponding character stored in a buffer so some program can retrieve it.*
>
> *The system calls are initiated by user-level programs, for opening files, starting other programs, etc. Each system call handler will have to check whether the arguments passed are valid, then perform the internal operation to complete the request.*
>
> *Most user programs do not directly issue system calls (except for asm programs, for instance), but instead use a standard library which does the ugly job of formatting arguments as required by the kernel and generating the system call. (For example, the C function fopen() eventually calls a kernel function that actually opens the file.)*
>
> *The kernel usually defines a few abstractions like files, processes, sockets, directories, etc. which correspond to an internal state it remembers about last operations, so that a program may issue a session of operation more efficiently.*

## About Unix inspiration

UNIX is both an operating system (such as macOS, and historically systems like BSD or Solaris) and a computer design philosophy.

UNIX manages: files, programs, memory, users, permissions, processes, networking, and the terminal itself.

In a way, it acts as the system’s orchestrator.

What we mostly leverage today is the UNIX philosophy. It is known for simple yet powerful ideas.

One of the most famous principles is: “Everything is a file.”\
In UNIX, directories, disks, keyboards, printers, sockets, and even some processes can often be interacted with through file descriptors or file-like interfaces (the important nuance is that not everything literally *is* a file, but UNIX exposes many resources through a unified file abstraction).

UNIX is heavily CLI oriented, which is why shells such as `bash` or `zsh` are so valued by developers and system administrators.

Some people wonder about the real difference between UNIX and Linux. A simple way to see it is:

<mark style="background-color:$primary;">Original UNIX</mark> = the historical operating system family with official UNIX certification.

<mark style="background-color:$primary;">Linux</mark> = a UNIX-inspired reimplementation built independently.

Technically, Linux is not UNIX, but in practice it behaves very similarly and follows most UNIX principles.

### References

* <https://wiki.osdev.org/Expanded_Main_Page>
* <https://www.kernel.org/doc/html/next/core-api/>
* <https://github.com/redox-os>
* <https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html>
* <https://www.sco.com/developers/devspecs/abi386-4.pdf>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ammons-organization-1.gitbook.io/thehiddenshape/system-and-networks/kfs-kernel-from-scratch-series.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
