DEV Community

Cover image for Peacock OS 1.9: a desktop operating system written entirely in KingCobra
Divyanshu Sinha
Divyanshu Sinha

Posted on

Peacock OS 1.9: a desktop operating system written entirely in KingCobra

I wrote an operating system, and every line of it, from the boot sector to the web browser, is written in one language: KingCobra, a language I also wrote. It's called Peacock OS, version 1.9 is out, and it boots in QEMU on a laptop and on an Android phone.

That includes the bootloader. The MBR, the stage-2 loader that switches the CPU into 64-bit mode, and the kernel are all KingCobra, not just the kernel.

Repo: https://github.com/DivyanshuSinha136/Peacock

Peacock OS desktop

Why build it

A language is only credible once it has carried something large. KingCobra is meant to feel like a scripting language while being able to do systems work, and an OS is the harshest test I could think of: raw memory, interrupts, device drivers, a network stack and a GUI, all at once. Peacock is about 17,000 lines of KingCobra in kernel/, and the bootloader in boot/ is KingCobra too, with its own small x86 instruction emitter instead of an assembler. All of it is compiled by kcc, KingCobra's own compiler.

Try it

You need qemu-system-x86_64 and the KingCobra toolchain (kingcobra and kcc) checked out next to the Peacock folder.

make         # builds peacock.img, a 17 MiB bootable raw disk image
make run     # boots it in QEMU
Enter fullscreen mode Exit fullscreen mode

Or run an image directly:

qemu-system-x86_64 -m 128 -vga std \
  -nic user,model=rtl8139 \
  -usb -device usb-tablet -device usb-kbd \
  -drive file=peacock.img,format=raw
Enter fullscreen mode Exit fullscreen mode
Option Why
-m 128 the kernel heap is 120 MiB (use -m 256 for 2560x1440 and above)
-vga std Bochs/DISPI adapter, so the resolution can be switched live up to 4096x2400
-nic user,model=rtl8139 the supported NIC: DHCP, DNS and TCP through QEMU's user network
-usb -device usb-tablet -device usb-kbd a UHCI controller with an absolute pointer and a keyboard

What's inside

Boots from nothing, in KingCobra

The bootloader is written in KingCobra, not just the kernel. Stage 1 is the 512-byte MBR at LBA 0. Stage 2 (LBA 1-4) enables A20, reads the memory map, switches the CPU into long mode and loads the kernel, and it's KingCobra too. A tiny x86 instruction emitter (boot/x86.kc) takes the place of an assembler, so even the code that runs before any operating system exists is KingCobra.

make boot-selftest rebuilds the original NASM bootloader and checks that the KingCobra one is byte-for-byte identical. The BIOS path handles multiple A20 methods, E820/E801/88h memory-map fallbacks and a checksummed handover block.

A 64-bit rendering pipeline

The back buffer is RGBA16, with 16 bits per channel. Blending and the glass blur happen in 16-bit, and a 4x4 ordered-dither flush writes the result to the 24/32 bpp screen. That means the internal pipeline is 64 bpp even though the output is limited by the display.

A glass desktop

A dock and top bar, translucent panels, damage-rectangle redraw, 9 themes, SVG icons and scalable fonts. The desktop stays uncluttered: Files, Browser, Terminal, Notes and Settings.

Peacock OS launcher

Nine themes in Settings
Nine wallpaper themes, button depth and live resolution switching in Settings.

14 applications

Terminal, Notes 2 (find/replace, selection, undo), Paint, Calculator, System Info, Files, Calendar, Clock, Snake, Minesweeper, System Monitor, Web Browser, App Builder 2 and 3D Studio, plus Settings.

KCFS 2, a crash-safe file system

A two-slot store with CRC32 on every block, atomic two-slot commits, fsck, attributes and a background autosave thread.

Files, the KCFS 2 file manager
Files, the file manager for KCFS 2.

Kernel threads

A cooperative scheduler with mutexes, semaphores and queues. In the terminal, ps, kill and spawn work on them.

A network stack and a browser

An RTL8139 driver, ARP, IPv4, ICMP, UDP, DHCP, DNS, TCP and HTTP. The browser has tabs, bookmarks, find-in-page and an HTML renderer, and I've browsed real HTTP sites with it, including example.com, info.cern.ch, neverssl.com and textfiles.com. The terminal has wget, ping and nslookup.

Peacock OS web browser

Network settings: NIC, DHCP lease and DNS
Settings, Network: the RTL8139 NIC, its DHCP lease and DNS.

USB

A polled UHCI driver with HID boot keyboard and mouse, an absolute tablet, read-only bulk mass storage and a hot-plug scan. There are no hubs yet.

PCI, ACPI and hardware control

PCI enumeration goes through bridges, with BAR sizing and capability walking (PM, MSI, MSI-X, PCIe), device naming and driver binding. There is an ACPI table parser with S5 power-off, a reset register, a CPUID report and a memory map.

Settings, Devices: PCI functions, bound drivers, ACPI and CPU
Settings, Devices: PCI functions, bound drivers, ACPI and CPU.

3D and your own executables

A software 3D rasteriser powers 3D Studio. App Builder 2 compiles a window design into a .pea executable, a native binary format with its own loader, peadump and a command-line compiler, peac.

3D Studio
3D Studio, a software rasteriser.

App Builder 2
App Builder 2: design a window, run it, save it as a .pea executable.

A terminal that does real work

Terminal over a glass backdrop
The terminal over a glass backdrop, with neofetch-style info.

files     ls [-la] cd pwd cat mkdir touch rm [-r] cp [-r] mv echo [> file] stat attr fsck sync
network   ifconfig dhcp ping nslookup wget <url> [file]  browse [url]
system    ver uname date uptime mem cpu neofetch  ps kill <id>  spawn counter|spin|hog [n]
hardware  lspci [-v]  lscpu  lsmem  acpi  usb  usb read <sector>
programs  name | ./name.pea   peac <src> [out]   peadump <prog>   run <name>
Enter fullscreen mode Exit fullscreen mode

How I test an OS without hardware

QEMU boots are slow and awkward to automate, so most of the testing runs on the host. There are 17 host-side suites that run the real kernel code against simulated hardware: disks, PCI buses, UHCI controllers and NICs. That includes about 60 USB checks, crash-recovery tests for the file system, ARP/IP/DHCP/DNS/TCP/HTTP against a simulated NIC, and a boot simulator that boots the real image through a simulated BIOS.

make host-test     # all host suites
make boot-sim      # simulated BIOS boot of the real image
make test          # headless QEMU boot + screendump check (needs QEMU)
Enter fullscreen mode Exit fullscreen mode

The screenshots in the repo are rendered by those test harnesses from the real compositor code, not captured from QEMU.

Honest limits

  • Tested on QEMU only, on a laptop and on Android. It has never booted on real hardware, so real PCI, ACPI, USB and NIC behaviour is unverified.
  • Network: RTL8139 only, and there's no TLS, so https:// sites don't load. The browser has no CSS, JavaScript or images.
  • USB: UHCI only (no OHCI, EHCI or xHCI), no hubs, and mass storage is read-only.
  • Boot: BIOS only (no UEFI), a single CPU and cooperative threads.
  • Output depth: the screen is 24/32 bpp, and the 64-bit pipeline is internal.

What I'd love feedback on

  • Boot results on other VMs and real machines, with the exact setup, whether it works or not.
  • Driver bugs: PCI, UHCI and the NIC driver have only met simulated hardware and QEMU's models.
  • Anything in the code that reads badly, since the point of writing it in KingCobra is that it should be readable.

Repo: https://github.com/DivyanshuSinha136/Peacock ยท The language: https://github.com/DivyanshuSinha136/KingCobra

MIT licensed. A star helps other people find it.

Top comments (0)