System calls
The path of write through libc and ecall, the number in a7, errno, call cost, strace, write(2) vs printf(3).
Why not jal into the kernel
The operating-systems topic put the kernel in a privileged mode that user programs cannot enter by jumping, and the C topic wrote programs that need printing, files, processes and memory. Between them stands a narrow door. A system call is a trap that carries a request number and its arguments into the kernel and brings a result back. The exceptions topic followed the trap through the hardware; this topic follows the request from a C call through libc to ecall, through the kernel’s table and back.
Why not call sys_write with jal like any other function? Three reasons. The code is out of reach: kernel pages are mapped in every process with the bit clear, so fetching them in U-mode raises an instruction page fault (cause 12) answered with SIGSEGV. The mode would not change: even if fetch worked, the code would still run in U-mode and every privileged instruction would fault. Any entry point would do: a jal can target the middle of a function, past the argument checks. A request into the kernel therefore needs an instruction that changes mode and jumps to an address the program does not choose: RISC-V’s ecall, x86-64’s syscall, Arm’s svc. Which service and which arguments is an agreement between kernel and libc — the system-call ABI.
Call path and ABI
The hello-world program of the first topic ends in write(1, buf, 13). On Linux for RISC-V the path is:
maincallswrite(1, buf, 13)with arguments already ina0–a2by the calling convention.- The libc wrapper executes
li a7, 64andecall. - Hardware sets
scausefor an environment call from U-mode and jumps to the handler atstvec. - The kernel saves user registers, advances
sepcby 4 past theecall, readsa7and indexessys_call_table[64]→sys_write. sys_writechecks the fd and that the buffer is readable user memory, copies 13 bytes toward the terminal driver, and leaves 13 ina0.sretreturns to the wrapper. Ifa0 ≥ 0, the wrapper returns it tomain. Ifa0is in [−4095, −1], the wrapper setserrno = −a0and returns −1.
Always test the return value before reading errno. The number 64 is Linux on RISC-V and arm64; on x86-64 Linux write is 1; on macOS it is 4. Tracing the wrong ABI table is a common lab mistake.
Cost, strace, printf
A trap is not a function call. The kernel must save and restore a full register set, validate pointers, dispatch through a table, and often touch different page tables and caches. On an Apple M1, order-of-magnitude timings from the note: an empty function call about 3 ns, getppid about 150 ns, a 1-byte write about 0.5–0.7 µs. Writing 1 MiB as single-byte writes versus 4 KiB writes differs by about 3,700× — amortization is everything.
strace prints each syscall with arguments and results; strace -c summarizes counts and time. Startup of a dynamically linked binary is a storm of open/mmap/mprotect before main. printf(3) formats into a libc buffer (line-buffered on a terminal, fully buffered when redirected) and only then calls write(2). Crashing before flush loses the buffer; fflush or a raw write is the durable path. Next — Linking and loading: how object files become the process that issues these calls.
Limited direct execution is this door in continuous use: almost every user instruction runs natively; only traps enter S-mode. Counting crossings explains why batching I/O, using mmap, and avoiding chatty protocols matter more than micro-optimizing arithmetic once you leave userspace. When a high-level runtime looks “slow,” strace -c often shows the tax is syscalls and page faults, not bytecode dispatch alone — a bridge to the language-runtimes topic.
The ABI also specifies which registers are caller-saved across the trap and how 64-bit values are split on 32-bit ABIs. On RV32 a 64-bit argument may occupy an even/odd register pair; on x86-64 Linux the syscall instruction uses rax for the number and rdi, rsi, rdx, r10, r8, r9 for arguments — note r10 instead of rcx because syscall clobbers rcx and r11. Portability wrappers in libc hide these details; reading glibc’s sysdeps or musl’s arch/*/syscall_arch.h makes them concrete.
Security and correctness meet at the copy. The kernel must not trust user pointers: it uses copy_from_user / copy_to_user (or architecture equivalents) that probe the address and handle page faults mid-copy. Returning a kernel address or following a user-controlled pointer without checks is a class of bug that becomes a CVE. From userspace, passing a stack buffer that another thread unmaps during the syscall races with those probes — another reason multithreaded programs need discipline around fork and lifetime of buffers.
When teaching yourself a new OS surface — io_uring, clone3, openat2 — start from the same template: number, registers, which libc wrapper, what errors mean, what strace prints, and what amortization looks like at 1 B versus 4 KiB versus 1 MiB. The door is stable; the rooms behind it keep growing.
Worked habit: for every new abstraction, write down (1) the kernel object, (2) the creating syscall, (3) what is shared after fork, (4) how EOF or termination is signalled, and (5) one concrete number (size, timeout, default limit). That checklist turns this course’s vocabulary into a debugging script you can run under strace.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.