</>EmbeddedInterview

What is a system call and how does it work?

MediumOperating System Concepts

Short answer: A system call is the controlled entry point from a user program into the kernel, used whenever the program needs something only the kernel may do (I/O, memory, creating processes). The C library puts the syscall number and arguments in registers and executes a trap instruction (svc #0 on ARM64, syscall on x86-64). The CPU switches to kernel mode at a fixed entry point; the kernel checks the arguments, runs the handler from the syscall table and returns the result. Negative return values become errno in libc.

ARM64 example (what write(1, "hi\n", 3) becomes)

mov  x8, #64          // __NR_write on arm64
mov  x0, #1           // fd
adr  x1, msg          // buffer
mov  x2, #3           // count
svc  #0               // trap to EL1
// x0 now holds the result (bytes written, or -errno)

Inside the kernel (ARM64): the SVC exception goes to the EL1 vector el0_sync → el0_svc → invoke_syscall(), which indexes sys_call_table[x8] and calls, e.g., __arm64_sys_write(). User pointers are never dereferenced directly: the kernel uses copy_from_user()/copy_to_user(), which handle faults safely.

Why system calls exist (the rationale):

  • Protection: user code runs at EL0 / ring 3 and cannot touch hardware or other processes' memory. The only way in is through checked entry points.
  • Abstraction: programs call read() on any file, pipe, socket or device; the kernel handles the details.
  • Resource management: the kernel arbitrates between programs (scheduling, quotas, permissions).

Effect on scheduling: a system call is a point where the kernel may switch tasks: blocking calls put the caller to sleep and run another task; on the way out, if a higher-priority task became ready, the kernel reschedules before returning.

Cost and how Linux avoids it

  • A syscall costs on the order of 100 ns to a few µs (mode switch, mitigations).
  • vDSO: clock_gettime() and gettimeofday() run entirely in user space by reading kernel-maintained data.
  • Batching interfaces: readv/writev, sendmmsg, io_uring (submission/completion rings with few or no syscalls).

Tools: strace -f ./prog lists every syscall with arguments and results; strace -c gives counts and times; ltrace shows library calls.