hn.today

A minimal kernel in Swift, running in QEMU

carette.xyz30 points4 comments
Screenshot of A minimal kernel in Swift, running in QEMU

A developer built a tiny bare-metal kernel in Swift and ran it inside QEMU's aarch64 "virt" machine to explore what it takes to run Swift without an operating system. Using Embedded Swift from a development toolchain and targeting aarch64-none-none-elf, the work replaces normal startup and runtime plumbing: a small assembly entry (boot.S) defines _start, sets up a stack for core 0, parks other cores, and calls a C-compatible Swift entry function annotated with @c. The build uses custom toolset flags (enable Embedded and Volatile, -no-allocations, -nostdlib, lld) and a linker script to avoid default ELF metadata placement. Missing C runtime routines were implemented in Swift: a putchar that writes to QEMU's PL011 UART at 0x0900_0000 and a memmove that handles overlapping copies. After fixing linker and section placement issues the result is a valid AArch64 ELF that prints "Hello, Embedded Swift" and stays running.

The experiment then added input handling by polling the UART flag register (0x0900_0018) for RXFE and echoing characters back to create a simple prompt. Interaction is synchronous and inefficient (polling), but demonstrates serial I/O from Swift on bare metal. Key limitations and next steps listed include clearing .bss at boot, hiding UART access behind Swift abstractions, exploring allocation support in Embedded Swift, implementing interrupts/timers, and moving from an infinite loop to scheduling or drivers (framebuffer, keyboard). An update notes using @c instead of the older @_cdecl approach.

Read on carette.xyz4 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.