← Interview Prep

Linux Kernel — System Calls

The user→kernel boundary: the syscall instruction & mode switch, the syscall number/table & handler, user-pointer validation (copy_from/to_user), negative-errno returns, why syscalls cost more than function calls (KPTI, batching, io_uring), the vDSO, per-arch ABI, and strace/seccomp.

The controlled doorway from user space into the kernel: how a syscall is invoked, why it's costlier than a function call, and how the kernel returns a result safely. Original, interview-focused notes.

A system call is the only way userspace asks the kernel to do a privileged operation (I/O, memory, processes). It's a mode switch (user → kernel ring), not a jump — the CPU raises privilege, the kernel runs the requested service, then returns to user mode.

The mechanism

  1. The program (usually via a libc wrapper) puts the syscall number in a register (rax on x86-64) and arguments in registers (rdi, rsi, rdx, r10, r8, r9), then executes the syscall instruction (legacy: int 0x80).
  2. The CPU switches to kernel mode, jumps to a fixed entry point, and the kernel uses the number to index the system-call table (sys_call_table) → the handler (sys_read, sys_openat, …).
  3. The handler runs on the task's kernel stack, validating every user pointer (copy_from_user/copy_to_user — never trust a userspace address).
  4. The return value goes back in rax; on error it's a negative errno, which the libc wrapper turns into -1 + errno.

Why it's expensive (vs a function call)

vDSO — the syscall that isn't

Some "syscalls" are read-only and hot (gettimeofday, clock_gettime). The kernel maps a tiny shared page — the vDSO — into every process so those run entirely in user space, no mode switch. (The old fixed-address vsyscall is its deprecated predecessor.)

Numbers, ABI & tools

$ strace -f -e trace=openat,read,write -c ./app
% time     seconds  usecs/call     calls    errors syscall
 61.2    0.004131          4        980           read
 27.4    0.001849          3        612        12 openat
 ...

Likely interview questions

Kernel Internals series. Related: Processes · Linux Basics.