Skip to main content
System calls (syscalls) are the primary mechanism for user space programs to request services from the kernel. The SerenityOS kernel provides a comprehensive syscall interface defined in Kernel/API/Syscall.h and implemented across Kernel/Syscalls/.

System Call Architecture

Overview

System calls transition execution from user mode to kernel mode, allowing controlled access to privileged operations:

Syscall Numbers

Each system call has a unique number defined by the ENUMERATE_SYSCALLS macro:

Making System Calls

From User Space

Applications invoke syscalls using architecture-specific instructions: x86_64:
AArch64:
RISC-V:
User space code typically doesn’t invoke syscalls directly. Instead, it uses LibC wrapper functions that handle marshalling arguments and error codes.

Syscall Parameters

System calls can accept up to 4 parameters. Complex data structures are passed via parameter structures:

Kernel-Side Handling

Syscall Handler

The main syscall entry point is in Kernel/Syscalls/SyscallHandler.cpp:

Handler Table

Syscalls are dispatched via a function pointer table:

Big Process Lock

Some syscalls require the “big process lock” for thread-safety:
Syscall implementations must document their locking requirements using:
  • VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this) - Lock is held
  • VERIFY_NO_PROCESS_BIG_LOCK(this) - Lock is NOT held

Common Syscalls

Process Management

fork - Create child process
execve - Execute program
exit - Terminate process

File Operations

open - Open file
read - Read from file
write - Write to file

Memory Management

mmap - Map memory
munmap - Unmap memory

Thread Management

create_thread - Create new thread

IPC and Sockets

socket - Create socket
sendmsg/recvmsg - Send/receive messages

Security Features

Pledge

The pledge syscall restricts process capabilities:
After pledging, violating promises terminates the process:

Unveil

The unveil syscall restricts filesystem access:
Pledge and unveil provide defense-in-depth security. Use them early in program initialization to limit attack surface.

Parameter Validation

User Space Pointers

All user space pointers must be validated:

Argument Sanitization

Never trust user space input. Always validate pointers, sizes, flags, and values before using them.

Error Handling

Return Values

Syscalls return ErrorOr<FlatPtr>:
  • Success: Return value (usually 0 or positive)
  • Error: Return Error::from_errno(errno_value)

Error Codes

Common errno values (from Kernel/API/POSIX/errno.h):
  • EINVAL: Invalid argument
  • EBADF: Bad file descriptor
  • ENOMEM: Out of memory
  • EACCES: Permission denied
  • ENOENT: No such file or directory
  • EINTR: Interrupted system call
  • EAGAIN: Resource temporarily unavailable

Performance Considerations

Fast Paths

Optimize common cases:

Avoiding System Calls

User space can avoid syscalls using:
  • vDSO: Virtual dynamic shared object for fast operations
  • Time Page: Shared memory page for reading time
  • Buffering: Reduce syscall frequency via LibC buffering

Debugging Syscalls

Syscall Tracing

Enable ptrace to trace syscalls:

Profiling

The kernel tracks syscall performance:
  • Kernel/API/Syscall.h - Syscall definitions and numbers
  • Kernel/Syscalls/SyscallHandler.cpp - Main syscall dispatcher
  • Kernel/Syscalls/*.cpp - Individual syscall implementations
  • Kernel/API/POSIX/ - POSIX-compatible type definitions
  • Kernel/Tasks/Process.h - Process syscall handlers