milo

05 — IPC and Syscalls

Milo implements a Linux AArch64 (LP64) system call interface dispatched via svc #0 (kernel/syscalls/SyscallDispatcher.cpp), paired with validated user-space memory transfers (kernel/syscalls/SyscallHelpers.cpp, kernel/mm/AddressSpace.cpp), anonymous pipes (kernel/ipc/Pipe.{hpp,cpp}), and AF_UNIX stream/datagram sockets (kernel/net/af_unix.{hpp,cpp}, kernel/syscalls/SocketSyscalls.cpp).

System Call ABI

Trap Entry & Register Convention (arch/aarch64-linux-gnu/boot.s, arch/aarch64-linux-gnu/trap_aarch64.cpp, arch/aarch64-linux-gnu/cpu.cpp)

  1. Exception & Trap Frame (boot.s, trap_aarch64.cpp): An EL0 or EL1 svc #0 instruction enters vector_table at the synchronous vector and branches to trap_sync_common. trap_sync_common allocates 288 bytes on the stack (sizeof(AArch64CpuContext) == sizeof(HAL::CpuContext)), saves general-purpose registers x0–x30 plus sp_el0, elr_el1, spsr_el1, and tpidr_el0, and calls aarch64_handle_sync_exception(ctx, esr, far).
  2. Dispatch (trap_aarch64.cpp, SyscallDispatcher.cpp): When the exception class EC == 0x15 (SVC in AArch64), aarch64_handle_sync_exception builds a HAL::TrapFrame (type = HAL::TrapType::Syscall, error_code = esr, context = ctx) and calls HAL::get_trap_dispatcher().handle_trap(frame) (SyscallDispatcher::handle_trap).
  3. Syscall Number, Arguments & Return Value (AArch64CpuControl in cpu.cpp):
    • Syscall Number: Read from x8 via get_syscall_number(ctx).
    • Arguments (arg0–arg5): Read from x0–x5 via get_syscall_arg(ctx, index) (index < 6). For sys_ioctl, request is narrowed via static_cast<uint32_t>(request) so sign-extended 32-bit _IOC_READ commands match 64-bit UAPI constants.
    • Return Value: Written to x0 via set_syscall_return(ctx, ret_val), returning non-negative values on success or negated Linux errno constants (ERR_* in kernel/uapi/errno.hpp) on error (sys_execve returns early without overwriting x0 when ret_val == 0 so the new program starts with its prepared x0/x1).
  4. Post-Syscall Signal Delivery (trap_aarch64.cpp, kernel/process/Signal.cpp): After handle_trap returns, aarch64_handle_sync_exception calls Kernel::Process::deliver_pending_signals(*ctx). Fatal default signals exit via exit_current_process(128 + sig); stop signals (SIG_STOP, SIG_TSTP, SIG_TTIN, SIG_TTOU) transition the process to Stopped and notify the parent; catchable signals with a user handler push a 320-byte linux_rt_sigframe onto SP_EL0, set x0 = sig, x1 = 0, x2 = frame_vaddr, and x30 = restorer_lr, and jump ELR_EL1 to sa_handler. Returning from the handler via SYS_RT_SIGRETURN (139) calls restore_signal_frame(ctx) to restore saved registers, SP_EL0, ELR_EL1, SPSR_EL1, TPIDR_EL0, and the signal mask.

Syscall Reference Table

The syscall numbers below are defined in kernel/syscalls/SyscallDispatcher.cpp (SYS_SCHED_YIELD 124 and SYS_GETUID 174 / SYS_GETEUID 175 / SYS_GETGID 176 / SYS_GETEGID 177 are handled inline in SyscallDispatcher::handle_trap; all others delegate to kernel/syscalls/*.cpp):

# Name Arguments (x0..x5) Return Value Source File
17 sys_getcwd x0: uintptr_t buf, x1: uint64_t size Length including NUL, or negative ERR_* (-EINVAL, -ERANGE, -EFAULT) kernel/syscalls/FileSyscalls.cpp
23 sys_dup x0: uint64_t oldfd New FD (>= 0) with FD_CLOEXEC cleared, or -EBADF / -EMFILE kernel/syscalls/FileSyscalls.cpp
24 sys_dup3 x0: uint64_t oldfd, x1: uint64_t newfd, x2: uint64_t flags (O_CLOEXEC) newfd (>= 0), or -EBADF / -EINVAL / -ENOMEM kernel/syscalls/FileSyscalls.cpp
25 sys_fcntl x0: uint64_t fd, x1: uint64_t cmd, x2: uint64_t arg Command result (0, flags/FD, or FD_CLOEXEC_BIT), or negative ERR_* kernel/syscalls/FileSyscalls.cpp
29 sys_ioctl x0: uint64_t fd, x1: uint64_t request (32-bit cmd), x2: uintptr_t arg 0 or positive byte count, or negative ERR_* kernel/syscalls/FileSyscalls.cpp
34 sys_mkdirat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t mode 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
35 sys_unlinkat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t flags (AT_REMOVEDIR) 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
38 sys_renameat x0: int32_t olddirfd, x1: uintptr_t oldpath, x2: int32_t newdirfd, x3: uintptr_t newpath 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
45 sys_truncate x0: uintptr_t pathname, x1: int64_t length 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
46 sys_ftruncate x0: uint64_t fd, x1: int64_t length 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
48 sys_faccessat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t mode, x3: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
49 sys_chdir x0: uintptr_t path 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
52 sys_fchmod x0: uint64_t fd, x1: uint64_t mode 0 or -EBADF / -EROFS kernel/syscalls/FileSyscalls.cpp
53 sys_fchmodat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t mode, x3: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
54 sys_fchownat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint32_t owner, x3: uint32_t group, x4: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
55 sys_fchown x0: uint64_t fd, x1: uint32_t owner, x2: uint32_t group 0 or -EBADF / -EROFS kernel/syscalls/FileSyscalls.cpp
56 sys_openat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t flags, x3: uint64_t mode Allocated FD (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
57 sys_close x0: uint64_t fd 0 or -EBADF kernel/syscalls/FileSyscalls.cpp
59 sys_pipe2 x0: uintptr_t pipefd, x1: uint64_t flags (O_CLOEXEC, O_NONBLOCK) 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
61 sys_getdents64 x0: uint64_t fd, x1: uintptr_t dirp, x2: uint64_t count Bytes written (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
62 sys_lseek x0: uint64_t fd, x1: int64_t offset, x2: uint64_t whence New offset (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
63 sys_read x0: uint64_t fd, x1: uintptr_t buf, x2: uint64_t count Bytes read (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
64 sys_write x0: uint64_t fd, x1: uintptr_t buf, x2: uint64_t count Bytes written (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
66 sys_writev x0: uint64_t fd, x1: uintptr_t iov, x2: uint64_t iovcnt Total bytes written (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
72 sys_pselect6 x0: int32_t nfds, x1: uintptr_t rfds, x2: uintptr_t wfds, x3: uintptr_t efds, x4: uintptr_t timeout, x5: uintptr_t sigpack Ready count (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
73 sys_ppoll x0: uintptr_t fds, x1: uint64_t nfds, x2: uintptr_t tmo_p, x3: uintptr_t sigmask, x4: uint64_t sigsetsize Ready count (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
78 sys_readlinkat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uintptr_t buf, x3: uint64_t bufsiz Bytes copied (>= 0) or negative ERR_* kernel/syscalls/FileSyscalls.cpp
79 sys_newfstatat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uintptr_t statbuf, x3: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
80 sys_fstat x0: uint64_t fd, x1: uintptr_t statbuf 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
82 sys_fsync x0: uint64_t fd (dispatched with datasync = 0) 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
83 sys_fdatasync x0: uint64_t fd (dispatched to sys_fsync with datasync = 1) 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
88 sys_utimensat x0: int32_t dirfd, x1: uintptr_t pathname, x2: uintptr_t times, x3: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
93 sys_exit x0: uint64_t status [[noreturn]] kernel/syscalls/ProcessSyscalls.cpp
94 sys_exit_group x0: uint64_t status (dispatched to sys_exit) [[noreturn]] kernel/syscalls/ProcessSyscalls.cpp
96 sys_set_tid_address x0: uintptr_t tidptr Current PID (>= 0) kernel/syscalls/ProcessSyscalls.cpp
101 sys_nanosleep x0: uintptr_t req, x1: uintptr_t rem 0 or -EINVAL / -EFAULT / -EINTR kernel/syscalls/TimeSyscalls.cpp
113 sys_clock_gettime x0: uint64_t clk_id, x1: uintptr_t tp 0 or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
124 sys_sched_yield No arguments 0 kernel/syscalls/SyscallDispatcher.cpp
129 sys_kill x0: int32_t pid, x1: uint64_t sig 0 or -EINVAL / -ESRCH kernel/syscalls/SignalSyscalls.cpp
134 sys_rt_sigaction x0: uint64_t sig, x1: uintptr_t act, x2: uintptr_t oldact, x3: uint64_t sigsetsize 0 or -EINVAL / -EFAULT kernel/syscalls/SignalSyscalls.cpp
135 sys_rt_sigprocmask x0: uint64_t how, x1: uintptr_t set, x2: uintptr_t oldset, x3: uint64_t sigsetsize 0 or -EINVAL / -EFAULT kernel/syscalls/SignalSyscalls.cpp
139 sys_rt_sigreturn Implicit trap HAL::CpuContext& Restored x0 from linux_rt_sigframe or -1 kernel/syscalls/SignalSyscalls.cpp
153 sys_times x0: uintptr_t buf timer_ticks() or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
154 sys_setpgid x0: int32_t pid, x1: int32_t pgid 0 or -EINVAL / -ESRCH kernel/syscalls/ProcessSyscalls.cpp
155 sys_getpgid x0: int32_t pid PGID (>= 0) or -EINVAL / -ESRCH kernel/syscalls/ProcessSyscalls.cpp
156 sys_getsid x0: int32_t pid SID (>= 0) or -EINVAL / -ESRCH kernel/syscalls/ProcessSyscalls.cpp
157 sys_setsid No arguments New SID (>= 0) or -EINVAL / -EPERM kernel/syscalls/ProcessSyscalls.cpp
160 sys_uname x0: uintptr_t buf 0 or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
165 sys_getrusage x0: int32_t who, x1: uintptr_t usage 0 or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
166 sys_umask x0: uint64_t mask Previous umask (0022 if no current process) kernel/syscalls/ProcessSyscalls.cpp
172 sys_getpid No arguments PID (>= 0) kernel/syscalls/ProcessSyscalls.cpp
173 sys_getppid No arguments PPID (>= 0) kernel/syscalls/ProcessSyscalls.cpp
174 sys_getuid No arguments 0 kernel/syscalls/SyscallDispatcher.cpp
175 sys_geteuid No arguments 0 kernel/syscalls/SyscallDispatcher.cpp
176 sys_getgid No arguments 0 kernel/syscalls/SyscallDispatcher.cpp
177 sys_getegid No arguments 0 kernel/syscalls/SyscallDispatcher.cpp
179 sys_sysinfo x0: uintptr_t info 0 or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
198 sys_socket x0: int32_t domain, x1: int32_t type, x2: int32_t protocol Socket FD (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
199 sys_socketpair x0: int32_t domain, x1: int32_t type, x2: int32_t protocol, x3: uintptr_t sv 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
200 sys_bind x0: uint64_t fd, x1: uintptr_t addr, x2: uint64_t addrlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
201 sys_listen x0: uint64_t fd, x1: int32_t backlog 0 or -EBADF / -ENOTSOCK / -EOPNOTSUPP / -EINVAL kernel/syscalls/SocketSyscalls.cpp
202 sys_accept x0: uint64_t fd, x1: uintptr_t addr, x2: uintptr_t addrlen Accepted FD (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
203 sys_connect x0: uint64_t fd, x1: uintptr_t addr, x2: uint64_t addrlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
204 sys_getsockname x0: uint64_t fd, x1: uintptr_t addr, x2: uintptr_t addrlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
205 sys_getpeername x0: uint64_t fd, x1: uintptr_t addr, x2: uintptr_t addrlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
206 sys_sendto x0: uint64_t fd, x1: uintptr_t buf, x2: uint64_t len, x3: int32_t flags, x4: uintptr_t dest_addr, x5: uint64_t addrlen Bytes sent (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
207 sys_recvfrom x0: uint64_t fd, x1: uintptr_t buf, x2: uint64_t len, x3: int32_t flags, x4: uintptr_t src_addr, x5: uintptr_t addrlen Bytes received (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
208 sys_setsockopt x0: uint64_t fd, x1: int32_t level, x2: int32_t optname, x3: uintptr_t optval, x4: uint64_t optlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
209 sys_getsockopt x0: uint64_t fd, x1: int32_t level, x2: int32_t optname, x3: uintptr_t optval, x4: uintptr_t optlen 0 or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
210 sys_shutdown x0: uint64_t fd, x1: int32_t how (SHUT_RD, SHUT_WR, SHUT_RDWR) 0 or -EBADF / -ENOTSOCK / -EINVAL kernel/syscalls/SocketSyscalls.cpp
211 sys_sendmsg x0: uint64_t fd, x1: uintptr_t msg, x2: int32_t flags Bytes sent (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
212 sys_recvmsg x0: uint64_t fd, x1: uintptr_t msg, x2: int32_t flags Bytes received (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
214 sys_brk x0: uintptr_t addr Current/new break address (int64_t) kernel/syscalls/MemorySyscalls.cpp
215 sys_munmap x0: uintptr_t addr, x1: uint64_t length 0 or -EINVAL / -ENOMEM kernel/syscalls/MemorySyscalls.cpp
220 sys_clone x0: uint64_t flags, x1: uintptr_t child_stack, x2: uintptr_t parent_tid, x3: uint64_t tls, x4: uintptr_t child_tid Child PID (> 0) or -EINVAL / -EAGAIN / -ENOMEM / -EFAULT kernel/syscalls/ProcessSyscalls.cpp
221 sys_execve x0: uintptr_t pathname, x1: uintptr_t argv, x2: uintptr_t envp 0 on success (resumes at new entry/interp), or negative ERR_* kernel/syscalls/ProcessSyscalls.cpp
222 sys_mmap x0: uintptr_t addr, x1: uint64_t length, x2: uint64_t prot, x3: uint64_t flags, x4: uint64_t fd, x5: uint64_t offset Mapped virtual address (> 0) or negative ERR_* kernel/syscalls/MemorySyscalls.cpp
226 sys_mprotect x0: uintptr_t addr, x1: uint64_t length, x2: uint64_t prot 0 or -EINVAL / -ENOMEM kernel/syscalls/MemorySyscalls.cpp
242 sys_accept4 x0: uint64_t fd, x1: uintptr_t addr, x2: uintptr_t addrlen, x3: int32_t flags Accepted FD (>= 0) or negative ERR_* kernel/syscalls/SocketSyscalls.cpp
260 sys_wait4 x0: int32_t pid, x1: uintptr_t wstatus, x2: uint64_t options, x3: uintptr_t rusage Reaped/signaled/continued PID, 0 (WNOHANG), or negative ERR_* kernel/syscalls/ProcessSyscalls.cpp
261 sys_prlimit64 x0: int32_t pid, x1: uint32_t resource, x2: uintptr_t new_rlim, x3: uintptr_t old_rlim 0 or -EINVAL / -ESRCH / -EFAULT / -EPERM kernel/syscalls/ProcessSyscalls.cpp
276 sys_renameat2 x0: int32_t olddirfd, x1: uintptr_t oldpath, x2: int32_t newdirfd, x3: uintptr_t newpath, x4: uint64_t flags (RENAME_NOREPLACE) 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp
278 sys_getrandom x0: uintptr_t buf, x1: uint64_t buflen, x2: uint64_t flags Bytes written (>= 0) or -EINVAL / -EFAULT kernel/syscalls/TimeSyscalls.cpp
439 sys_faccessat2 x0: int32_t dirfd, x1: uintptr_t pathname, x2: uint64_t mode, x3: uint64_t flags 0 or negative ERR_* kernel/syscalls/FileSyscalls.cpp

Inter-Process Communication (IPC) & Sockets

Anonymous Pipes (kernel/ipc/Pipe.hpp, kernel/ipc/Pipe.cpp)

AF_UNIX Sockets (kernel/net/af_unix.hpp, kernel/net/af_unix.cpp, kernel/syscalls/SocketSyscalls.cpp)

sequenceDiagram
    autonumber
    participant Client as EL0 Client (AF_UNIX)
    participant Kern as Kernel Syscalls & VFS/Net
    participant Server as EL0 Server (AF_UNIX)

    Server->>Kern: socket(AF_UNIX, SOCK_STREAM) + bind("/tmp/s.sock") + listen(4)
    Kern->>Kern: vfs_mknod_at(S_IFSOCK) & register_bound_inode(inode, server_sock)
    Client->>Kern: socket(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK) + connect("/tmp/s.sock")
    Kern->>Kern: Lookup bound inode -> enqueue client on listener backlog & wake m_accept_wq
    Server->>Kern: poll() / accept4()
    Kern->>Kern: Dequeue client, allocate 2x 8192B StreamRings & server_ep, mark both Connected
    Kern-->>Server: Return accepted FD
    Client->>Kern: sendto(..., MSG_NOSIGNAL) -> write to shared StreamRing & wake server m_read_wq
    Server->>Kern: recvfrom(...) -> read from StreamRing & wake client m_write_wq