How does a system call work on ARM64 Linux?
Short answer: Userspace puts the syscall number in X8 and arguments in X0–X5, then executes SVC. The kernel handles the synchronous exception and returns a result in X0.
MOV X8, #172 // Linux arm64 getpid
SVC #0 // X0 = result
1. Userspace preparation: libc follows the Linux AArch64 syscall ABI: X8 contains the syscall number, X0–X5 contain up to six arguments.
2. Kernel entry: SVC raises a synchronous exception. Hardware saves return state; the kernel entry code saves registers and dispatches the syscall.
3. Return: The kernel places a result in X0 and restores userspace execution. Raw Linux syscall failures are negative error numbers; libc typically converts these into -1 and sets errno.
#include <unistd.h>
pid_t pid = getpid();
Interview takeaway: A syscall is a controlled privilege transition, not an ordinary function call into kernel memory.
Complete example: write(fd, buf, len)
For a raw AArch64 Linux write syscall (number 64 in the standard arm64 ABI):
// X0 = fd, X1 = user buffer address, X2 = byte count
MOV X8, #64
SVC #0
// X0 = bytes written or raw negative errno
At a typical EL0-to-EL1 entry, the CPU selects the lower-EL AArch64 synchronous exception vector. Linux entry code saves the userspace register state, checks the syscall number and invokes the relevant implementation. For write, the VFS resolves the file descriptor to a file and dispatches through the appropriate file operations. The kernel validates user-provided ranges and uses appropriate user-access helpers when copying data. A syscall can block and the scheduler may run another task before it returns. It can return fewer bytes than requested, so userspace handles partial writes.
A raw negative errno is not the same as the libc return convention. The C library generally returns -1 and sets thread-local errno for this interface. Exact Linux function names and entry paths vary by kernel release.