Skip to content
MokiMeowPublic

About

A 64-bit x86 OS kernel written from scratch in C and assembly that boots into its own interactive shell

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Nutshell

A 64-bit x86 operating-system kernel, written from scratch in C and assembly, that boots into its own interactive shell.

x86_64 C + Assembly Multiboot2 v1.0.0 MIT

Nutshell booting and running shell commands


What this is

Nutshell is a small operating-system kernel built the hard way: no libc, no framework, no training wheels. GRUB drops us into 32-bit protected mode; from there the kernel sets up its own page tables, enters 64-bit long mode, installs interrupt handlers, drives the keyboard, manages physical memory, and finally boots into a shell running on the bare metal: no OS underneath it, because this is the OS.

It is deliberately scoped: enough to be real, small enough for one person to understand end to end.

How it boots

GRUB loads the Multiboot2 image, the 32-bit entry creates the first page tables and enables long mode, and a far jump hands control to the 64-bit C kernel. The kernel then brings up its runtime GDT/TSS, allocators, interrupts, timer, and keyboard before entering the shell. The complete annotated path is in docs/03-boot-process.md.

+-----------+   GRUB    +------------------+  long mode  +----------------+
|  BIOS /   | --------> |  boot.asm (32b)  | ----------> |  kernel (C,64b)|
|  GRUB     | multiboot2|  paging + GDT +  |  far jump   |  VGA, IDT, PIC |
+-----------+           |  long-mode enter |             |  keyboard, mem |
                        +------------------+             |  -> shell      |
                                                         +----------------+

Why it is interesting (the depth on show)

  • The 32 → 64-bit transition, by hand: CPUID feature detection, building 4-level page tables, enabling PAE + EFER.LME + paging, and far-jumping through a 64-bit GDT. (docs/03-boot-process.md)
  • Interrupts from the silicon up: a 64-bit IDT, remapping the legacy 8259 PIC, exception and IRQ handlers, and the PIT timer. (docs/06-interrupts.md)
  • Your own memory manager: a physical-frame allocator over the multiboot memory map and a kernel heap, so malloc means your malloc. (docs/05-memory-management.md)
  • A real driver: PS/2 keyboard on IRQ1, scancode-set-1 decoding with Shift and Caps Lock, and an input ring buffer feeding the shell. (docs/07-keyboard-and-drivers.md)
  • A shell on bare metal: a REPL with built-in commands, running with nothing beneath it. (docs/08-the-shell.md)

Fault diagnostics

The panic built-in deliberately raises a divide error so the exception path can report the vector and register state instead of silently rebooting.

Nutshell exception report

Quick start (WSL2 / Linux, $0, no accounts)

./scripts/setup-wsl.sh   # one-time: nasm, gcc, binutils, grub, qemu
make run                 # build the ISO and boot Nutshell in QEMU
make test-keymap         # test keyboard decoding without QEMU
make test                # run PMM, heap, and scripted-shell tests in QEMU
make test-host           # fast memory-map, serial, and PIC regressions, GCC only
make test-interactive    # real PS/2 typing, editing, recovery, and fault tests

You should see Nutshell boot into 64-bit long mode and print its banner. At the prompt, use help for commands, memfrag to inspect contiguous free frames, irqstats for interrupt counts, and serialstats for serial output drops. Serial output is mirrored to your terminal. See docs/01-getting-started.md for details, and docs/09-testing-and-debugging.md for the QEMU + GDB workflow.

Requirements

Nutshell builds on Ubuntu or WSL2 with nasm, GCC, GNU binutils, make, xorriso, GRUB rescue tools, mtools, and qemu-system-x86_64. The host must be x86-64. No graphical display is required: the build and self-test workflows use QEMU serial output as their source of truth.

Reproducible BIOS ISOs

To reproduce the release and test ISO bytes, use the same source revision and BIOS toolchain in two fresh build directories. SOURCE_DATE_EPOCH fixes the ISO volume and file timestamps with GNU date, GRUB and xorriso. The Make targets select /usr/lib/grub/i386-pc explicitly so installed EFI modules cannot change the BIOS image. Set GRUB_BIOS_DIR if your BIOS modules live elsewhere. The build and boot workflow uses the commit time and compares both images. Find its kernel-serial-logs artifact on the build workflow run for the commit under Actions. The workflow runs on pushes to main and pull requests targeting main. While retained, the artifact supplies serial logs, SHA256SUMS, and iso-build-info.txt for checking a local rebuild. Build the ISOs locally to obtain the images. If the comparison fails, the iso-reproducibility-diagnostics artifact preserves both image pairs. The build replaces an existing ISO only after the packager succeeds.

export SOURCE_DATE_EPOCH=$(git show -s --format=%ct HEAD)
make iso test-iso BUILD=build/repro-a
make iso test-iso BUILD=build/repro-b
cmp build/repro-a/nutshell.iso build/repro-b/nutshell.iso
cmp build/repro-a/nutshell-test.iso build/repro-b/nutshell-test.iso

Test the Nutvisor boot handoff

Run these commands from Nutshell with the updated Nutvisor checkout beside it. The test boots the Nutshell self-test ELF through Nutvisor's exported Multiboot2 state in QEMU TCG.

make build/test/kernel.bin
make -C ../nutvisor test-nutshell-handoff

See docs/12-nutshell-handoff.md in the Nutvisor checkout for prerequisites, artifact verification, and commands for checkout paths containing spaces.

Status

Nutshell v1.0.0 is complete. The kernel boots to nutshell> , all built-in commands work, and CI smoke-boots both the interactive and self-test images.

# Milestone State
0 Boot to 64-bit long mode ✅ done
1 Screen + serial I/O (kprintf) ✅ done
2 Global Descriptor Table ✅ done
3 Interrupts (IDT + PIC + timer) ✅ done
4 PS/2 keyboard driver ✅ done
5 Physical memory + kernel heap ✅ done
6 The shell ✅ done
7 Polish, tests, portfolio pass ✅ done

Limitations

Nutshell currently targets BIOS boot through GRUB on x86-64. It uses VGA text mode, PS/2 keyboard input, identity-mapped low memory, and a single CPU core. There is no userspace, privilege separation, filesystem, networking, USB, ACPI enumeration, or SMP support. These boundaries keep the complete boot, interrupt, memory, driver, and shell path small enough to understand end to end.

Repository layout

nutshell/
├── boot/         # assembly: multiboot header, 32->64-bit bring-up
├── src/          # C kernel sources (auto-discovered by the Makefile)
├── include/      # kernel headers
├── assets/       # real QEMU boot GIF and fault screenshot
├── grub/         # GRUB config baked into the ISO
├── scripts/      # toolchain setup and helpers
├── tests/        # headless QEMU self-test runner
├── docs/         # architecture, boot process, roadmap, milestones, ADRs
├── linker.ld     # places the multiboot header first; loads kernel at 1 MiB
└── Makefile      # build / iso / run / debug / clean

Documentation

Everything is in docs/. Start with docs/00-overview.md, or jump to the roadmap to see how each piece gets built.

License

MIT: see LICENSE.

About

A 64-bit x86 OS kernel written from scratch in C and assembly that boots into its own interactive shell

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages