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 theENUMERATE_SYSCALLS macro:
Making System Calls
From User Space
Applications invoke syscalls using architecture-specific instructions: x86_64: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 inKernel/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:Common Syscalls
Process Management
fork - Create child processFile Operations
open - Open fileMemory Management
mmap - Map memoryThread Management
create_thread - Create new threadIPC and Sockets
socket - Create socketSecurity Features
Pledge
Thepledge syscall restricts process capabilities:
Unveil
Theunveil 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
Error Handling
Return Values
Syscalls returnErrorOr<FlatPtr>:
- Success: Return value (usually 0 or positive)
- Error: Return
Error::from_errno(errno_value)
Error Codes
Common errno values (fromKernel/API/POSIX/errno.h):
EINVAL: Invalid argumentEBADF: Bad file descriptorENOMEM: Out of memoryEACCES: Permission deniedENOENT: No such file or directoryEINTR: Interrupted system callEAGAIN: 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:Related Files
Kernel/API/Syscall.h- Syscall definitions and numbersKernel/Syscalls/SyscallHandler.cpp- Main syscall dispatcherKernel/Syscalls/*.cpp- Individual syscall implementationsKernel/API/POSIX/- POSIX-compatible type definitionsKernel/Tasks/Process.h- Process syscall handlers
