The RUNA owl
RUNAoperating system

Articles

How musl runs on RUNA

RUNA's kernel is not Linux, yet its whole userland is built on musl, a C library written for Linux. Here is how the port works, why it is small, and what it took to get threads right.

November 2026

The question from 2019

In August 2019 a developer asked on the musl mailing list how to port musl to a microkernel of their own: the musl site mentions the possibility, but there was no guide. Rich Felker, the author of musl, answered in some detail.

His points, in short. There is no ready framework for porting musl to a kernel other than Linux. The simple parts, such as reading and writing files, map easily onto generic Unix system calls. The hard parts are the ones where musl does real work in user space on top of a careful set of kernel primitives, threads above all. Any abstraction layer there is either wrong or no thinner than the pthread API itself. So the preferred route is to “provide an ABI-compatible or at least API-compatible syscall interface”: then all of musl's portable code keeps working.

RUNA followed that route. Seven years after the question, here is what it looks like in practice.

What the port is

RUNA does not pretend to be Linux. It has its own system call numbers and its own model of processes and threads, and it cannot run Linux binaries. But at the level musl actually sees, it is what musl expects:

Because of that, RUNA does not even need an architecture of its own inside musl. It reuses musl's x86_64 and aarch64 directories and changes only what really differs.

1.2.6the musl release, as published
4upstream files changed, all of them headers
16files of RUNA's own, in one directory
0lines changed in musl's Makefile
Layers: programs, upstream musl, RUNA's call table and own files, the system call instruction, the RUNA kernel Programs BusyBox, ps, showhw, tests, your own C code musl 1.2.6, upstream source stdio, malloc, strings, pthreads, the dynamic loader: not changed bits/syscall.h.in which calls exist, under RUNA's numbers src/runa/ RUNA's own versions: 16 files in all syscall / svc 0: the same registers and error convention RUNA kernel: POSIX semantics, its own numbers

Step one: a table of numbers

Each architecture in musl has a file, bits/syscall.h.in, that lists every system call by name and number. On x86_64 musl knows about 384 of them. RUNA answers 81.

RUNA's version of the file works in two passes. First it declares every name musl knows as “not implemented”: a number the kernel always answers with ENOSYS. Then a second block gives the real RUNA number to the names RUNA does answer:

/* Every name musl knows: "not implemented" unless RUNA answers it. */
#define RUNA_NR_UNSUPPORTED     127
#define __NR_read               RUNA_NR_UNSUPPORTED
#define __NR_open               RUNA_NR_UNSUPPORTED
#define __NR_openat             RUNA_NR_UNSUPPORTED
#define __NR_getrlimit          RUNA_NR_UNSUPPORTED
#define __NR_prlimit64          RUNA_NR_UNSUPPORTED
/* ... about 380 lines ... */

/* RUNA's calls, by the names musl asks for them. */
#undef  __NR_read
#define __NR_read               0
#undef  __NR_openat
#define __NR_openat             2
#undef  __NR_open               /* no number at all: musl takes openat */
#undef  __NR_getrlimit
#define __NR_getrlimit          82

The nice part is how much musl already knows how to do on its own. It was written to run on old and new kernels alike, so many functions have two paths. If SYS_open is not defined, musl's open() calls openat in the current directory. If prlimit64 answers ENOSYS, getrlimit() falls back to the older call. RUNA did not patch any of these functions: it leaves a name out, and musl's own code takes the other path.

The two passes also make the table safe. A name RUNA does not answer has no number of its own, so when RUNA adds a new call later, no old name can reach it by accident.

Step two: our own files in our own directory

A handful of functions RUNA implements differently. The tempting way is to edit musl's files in place. The trouble comes later: the next musl release touches the same files, and nobody remembers which lines were ours.

So RUNA's versions live in a directory of their own, src/runa/, and musl's originals stay exactly as released. A short makefile rule tells musl's build to take RUNA's file instead of upstream's file with the same name:

# src/runa/runa.mk: a file in src/runa stands for upstream's file of the same name.
RUNA_BASE      = $(filter src/runa/%,$(BASE_OBJS))
RUNA_ARCH      = $(filter src/runa/$(ARCH)/%,$(ARCH_OBJS))
UPSTREAM_OBJS  = $(filter-out src/runa/%,$(BASE_OBJS) $(ARCH_OBJS))
REPLACED_OBJS += $(filter $(addprefix %/,$(notdir $(RUNA_BASE))),$(UPSTREAM_OBJS))
REPLACED_OBJS += $(filter $(addprefix %/$(ARCH)/,$(notdir $(RUNA_ARCH))),$(UPSTREAM_OBJS))

The rule is plugged in through config.mak, the file musl's Makefile already reads for local settings. Our build script adds one line to it, and the Makefile itself stays untouched:

echo 'include $(srcdir)/src/runa/runa.mk' >> "$BUILD_DIR/config.mak"

Updating musl now means unpacking a new release. Conflicts can only appear in four headers, and anyone who reads the port sees at a glance which code is RUNA's.

Step three: threads, the hard part

This is where Rich Felker expected trouble, and rightly so. musl's pthreads are a delicate piece of work, and they lean on exact kernel behaviour. Here is what musl needs from a kernel:

RUNA's kernel provides each of these as a call of its own, with the behaviour musl expects. The only translation left is the function that starts a thread. musl calls __clone() with Linux's set of arguments; RUNA's small assembly version packs them into a declared, versioned “thread creation block” and hands it to the kernel:

struct runa_thread_req_v1 {
	uint32_t size, version, flags, sched_class;
	uint64_t stack, tls, tid_ptr, clear_tid_ptr;
	uint32_t priority, weight, cpu;
	uint32_t reserved;
	char     name[16];
};

Everything above that, pthread_create, mutexes, condition variables, thread-local storage, is musl's code, unchanged. The test program below runs on RUNA and checks the result from several sides: four threads count to 8000 under a mutex; a thread waiting on a condition variable is not given a single turn on the processor while it waits; pthread_kill reaches exactly the thread it was sent to; an exec from a worker thread leaves exactly one thread. An exit status of 0 means every check passed.

RUNA console: the threads test program prints its checks and exits with status 0
musl's pthreads on RUNA. Screenshot of the RUNA console in QEMU.

Where RUNA turned out simpler

Sometimes the port got smaller rather than bigger, because RUNA's kernel keeps things where POSIX says they belong.

Take setuid() in a program with threads. On Linux the kernel keeps user and group ids for each thread separately, while POSIX says they belong to the process. To bridge the gap, musl's __synccall stops every thread with a signal and has each one change its own ids, a careful and fairly heavy procedure. In RUNA the ids belong to the process, and one write changes them for every thread. RUNA's __synccall is therefore just a call:

void __synccall(void (*func)(void *), void *ctx)
{
	func(ctx);
}

vfork() is another example. In RUNA the child always gets its own copy of the address space, so nothing it does can spoil the parent's memory, and vfork can simply be fork, written in plain C instead of assembly:

pid_t vfork(void)
{
	return syscall(SYS_fork);
}

And the answer of stat has a single layout on every architecture, so stat, lstat and fstat are one short function with no older call to try first.

Honest differences

RUNA's kernel has no separate dup and dup2 calls: a descriptor is copied by fcntl(F_DUPFD), which gives the lowest free number from the one asked for. So dup2 on RUNA is two steps, close and copy, not POSIX's single step. If another thread grabs the number in between, dup2 says EBUSY instead of quietly putting the copy somewhere else.

And a call RUNA does not have yet answers ENOSYS. A program sees “not implemented”, not a crash, and musl's fallbacks get their chance first.

The result

All of RUNA's userland, BusyBox, the system utilities and the test programs, is built against this musl with an ordinary cross-compiler wrapper. BusyBox, for example, is linked dynamically and loaded by musl's own loader, as on any musl system:

RUNA console: uname, the libraries in /lib, the musl loader reporting version 1.2.6, and the libraries of busybox
The kernel's name, the libraries in /lib, musl's dynamic loader reporting its version, and the libraries BusyBox is linked with. Screenshot of the RUNA console in QEMU.

If you are porting musl too

  1. Keep the calling convention of the architecture musl already supports. Then you need no architecture of your own in musl, only a table of numbers.
  2. Default every name to “not implemented” and give numbers only to the calls you really answer. Unknown calls fail cleanly, and new calls cannot be reached by old names.
  3. Let musl's fallbacks work for you. Leaving a name undefined is often enough to send musl down a path you do support.
  4. Put your own versions in a directory of your own and plug them in through config.mak. Do not edit upstream files in place.
  5. Give threads real kernel primitives rather than emulating them in the library: a thread start with a stack and a thread pointer, a cleared and woken word at exit, a futex, a signal to one thread.
  6. Compare against the release tarball, not against your own first commit. Ours turned out to carry old edits nobody remembered.

← All articles