SchOS/docs/architecture/syscalls.md
Nexoniarz 9e6d8dd4e3 sockets: a descriptor can cross between processes
sendmsg/recvmsg with SCM_RIGHTS. Item 1 of docs/plan/foundations.md and
mechanism 1 of docs/plan/hybrid.md, which are the same primitive seen from two
directions: Wayland's wl_shm_create_pool passes a file descriptor with no
integer-shaped alternative, and handing a driver process the device it is
allowed to touch is the same act. Both plans stopped here.

Linux's structures byte for byte -- msghdr, cmsghdr, the CMSG_ macros -- because
the programs that need this are not ours. libwayland and toybox both call
recvmsg with Linux's headers and would not notice a friendlier door.

The delivery rule is the whole design. A batch of descriptors is pinned to the
stream position of the first byte of the message that carried it, and that has
two halves: a reader arriving at that position is handed them, and a reader that
has not reached it is stopped SHORT of it. Leave out the second half and a
program asking for 4096 bytes swallows three messages and is handed one
message's descriptors, with nothing in the result saying which. socktest checks
exactly that: two one-byte messages, an eight-byte read, one byte back.

The kernel holds a reference to every descriptor in flight, so a sender may
close its own copy the moment sendmsg returns. Giving that reference back is the
part that leaks if it is not written down, and there are three ways a message
ends:

  - received with room            installed in the receiver's table
  - received without room, or by
    plain read(2)                 closed; MSG_CTRUNC for the first case
  - never received, socket closed closed by sockclose

The middle row looks like a choice and is not: holding kernel references for a
receiver that has proved it is not looking is a leak with extra steps.

No garbage collector. A socket descriptor sent over that same socket and never
received holds a reference to the socket that holds the reference to it, and
neither close reaches zero. Linux carries several hundred lines for this.
Nothing on either plan passes a socket over a socket -- Wayland passes memory,
the driver work passes devices -- so it is a bounded leak that is documented
rather than a silent one.

Fourteen checks in cmd/socktest, and every one moves data through the descriptor
rather than inspecting it. The descriptor passed is always a pipe end, because a
pipe announces its own lifetime: a read of a pipe whose writers are all gone
returns 0, which turns "was the kernel's reference released" -- otherwise
invisible from userspace -- into a value a test can compare.

Two bugs fell out, neither in the new code:

  - sockpair() never set the socket's domain, so every socketpair() socket
    claimed domain 0. Invisible while the data paths asked only whether a socket
    was AF_INET or AF_NETLINK, since zero answers "neither" correctly.
    SCM_RIGHTS asks "is this AF_UNIX", and zero is wrong for that one.

  - pipe_poll() reports POLLHUP but not POLLIN at end of file, so a non-blocking
    read of a pipe whose writers had all gone returned EAGAIN forever instead of
    0 -- a program that polls and reads spins waiting for an end that already
    happened. sockreadable() always had the equivalent condition; the pipe path
    did not. Found because the test helper for "is this descriptor closed" is a
    non-blocking read of a pipe.

projects/schcom/README.md said SCM_RIGHTS was done for a long time while nothing
in the kernel implemented it, and an entire GUI plan was drawn on that line.
It is true now, which is not the same as it having been right, and the README
says so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-13 19:49:37 +02:00

5.9 KiB

Syscalls, and the Linux personality

Two things happen at this boundary. SchOS has its own syscall numbering, and it also answers Linux's — which is why musl, toybox and mksh run unmodified.


One table, everything generated

Syscalls are declared in exactly one place: kernel/sys/table.inc. One line each.

SYSCALL(nr, linux_nr, name)     // implemented as sys_<name>()
SYSCALL_ALIAS(linux_nr, name)   // a second Linux number for the same handler

Three things are generated from it by macro expansion, and none is hand-maintained:

Generated Where
The SYS_* constants include/syscall.h
The dispatch table kernel/sys/syscall.c
The Linux translation map kernel/sys/linux_abi.c

Adding a syscall is therefore two edits: a line here, and a body written with SYSCALL_DEFINE(<name>) in one of the kernel/sys/sys_*.c files.

This replaced a two-place scheme and the reason is worth keeping. The Linux number used to live in a second hand-written table. Adding a syscall meant three edits across two files, and forgetting the second one left the call unreachable from every musl-linked program while looking perfectly registered in the first. A syscall can no longer be half-registered.

How a Linux binary runs

The translation is a flat array indexed by Linux syscall number, whose value is the native SYS_* number — or 0, meaning not implemented, which dispatches to -ENOSYS.

It is enabled per process, and selected automatically by exec():

// musl binaries have entry >= 0x100000; SchOS-native ones are linked at 0x4000
if (elf.entry >= LINUX_ABI_ENTRY_THRESHOLD)
    myproc()->syscall_map = linux_syscall_map;

A link address is a crude discriminator and it is honest about being one. It works because the two classes of binary in this system are linked by two different linker scripts at two very different addresses.

There is also enable_linux_abi() as an explicit call, which is why cmd/schinit/main.c carries a warning not to use it: doing so mid-process would renumber the syscalls the caller still has to make.

Native vs. Linux numbering

Some calls have no Linux equivalent and are marked NO_LINUX: sleep, uptime, reboot, shutdown, getmouse, vga_sync, schfs_index, schfs_query. These are reachable only from first-party programs linked against lib/.

They all land on the same scratch index in the translation array and overwrite each other there, which is harmless — no real Linux syscall uses that number, so the slot is never dispatched.

Where the two disagree on a name

shutdown is the one collision. SchOS has a native shutdown() that powers the machine off, and Linux number 48 is the socket call. Two syscalls cannot share a sys_ function name, so Linux 48 maps to sockshutdown.

What is implemented

Roughly 190 native numbers covering:

Area
Process fork, exec, exit, wait/waitpid/wait4/waitid, clone, kill, sessions, process groups
Identity the full real/effective/saved set for both uid and gid, groups, umask
Memory mmap, munmap, mprotect, brk/sbrk, madvise, System V shared memory
Filesystem the ordinary set, the whole *at() family, statfs, xattr, mount/umount2
I/O read/write, readv/writev, pread/pwrite, sendfile, poll/select/ppoll/pselect6, epoll, ioctl, fcntl
Sockets socket, bind, listen, accept, connect, sendto, recvfrom, socketpair, setsockopt, getsockopt
Signals rt_sigaction, rt_sigreturn, rt_sigsuspend, rt_sigpending, rt_sigtimedwait, sigaltstack
Time gettimeofday, clock_gettime, clock_getres, nanosleep, clock_nanosleep, alarm, timers
Notification eventfd2, timerfd_create/settime/gettime, signalfd4

Argument fetching

fetchint, fetchaddr, fetchstr and argptr validate a user pointer through the page tables and nothing else. They previously rejected any address >= proc->sz, on the assumption that every valid user pointer lives in the heap — which is wrong for stack addresses and for every mmap'd buffer, i.e. for most pointers real programs pass.

uva2ka() translates a user virtual address and returns the in-page offset, which it did not originally do. Every caller that read or wrote through a non-page-aligned pointer — TIOCGPGRP, TCGETS, getcwd, signal masks, times(), sysinfo() — was touching the wrong address whenever the pointer was not page-aligned, i.e. almost always.

uva2ka() guarantees only the page containing that address. A structure copied in bulk through one translation runs off the end of the mapped page when the pointer lands near a boundary — and P2V is a flat offset, so "the next page" in kernel space has nothing to do with the user's next page. That bug corrupted unrelated kernel memory and crashed every freshly exec'd shell. Copy field by field, each through its own translation, or use copyin/copyout.


What it does not do

  • sendmsg/recvmsg carry SCM_RIGHTS and nothing else. SCM_CREDENTIALS is refused rather than ignored. See networking.md.
  • No memfd_create.
  • No getsockname or getpeername, and accept() does not report an AF_INET peer's address.
  • setsockopt handles six options and answers ENOPROTOOPT for everything else. SO_RCVTIMEO/SO_SNDTIMEO are among the refused.
  • clone does not share an address space, so it is fork with extra steps.
  • sched_setaffinity is accepted and ignored.
  • getrusage does not distinguish RUSAGE_CHILDREN from RUSAGE_SELF, and fills in only the calling process's own tick count.
  • The ABI is chosen by link address. It works, and it is a heuristic rather than a fact about the binary.
  • /proc/<pid>/exe is a regular file, not a symlink, so readlink() on it fails and ps -o comm prints [init] rather than init. Default ps and top output is unaffected.