System Calls
Overview
System calls provide the interface between user applications and the kernel. They allow applications to request services from the operating system.
Implementation
The system call interface is implemented in kernel/syscalls/:
- syscall.asm: INT 0x80 handler and syscall dispatch
- syscall_table.asm: Syscall table and registration
- syscalls.asm: Basic syscall implementations
Syscall Mechanism
System calls are invoked using the INT 0x80 instruction:
; User code calls syscall: mov eax, SYS_CALL_NUMBER ; Syscall number mov ebx, arg1 ; First argument mov ecx, arg2 ; Second argument mov edx, arg3 ; Third argument int 0x80 ; Invoke syscall ; Return value in EAX
Syscall Handler
The syscall handler (syscall_handler) is registered in the IDT at interrupt 0x80. It:
- Receives syscall number in EAX
- Looks up handler in syscall table
- Calls appropriate handler function
- Returns result in EAX
System Call Categories
1. File Operations
Complete file I/O operations:
- open() - Open file or device
- close() - Close file descriptor
- read() - Read from file
- write() - Write to file
- lseek() - Seek in file
- stat() - Get file status
- fstat() - Get file status by descriptor
- ioctl() - I/O control
2. Directory Operations
Directory management operations:
- mkdir() - Create directory
- rmdir() - Remove directory
- opendir() - Open directory
- closedir() - Close directory
- readdir() - Read directory entry
- getdents() - Get directory entries (Linux-compatible)
3. Process Operations
Process and thread management:
- fork() - Create new process
- execve() - Execute program
- wait() - Wait for child process
- waitpid() - Wait for specific child process
- exit() - Exit process
- getpid() - Get process ID
- getppid() - Get parent process ID
- getuid() - Get user ID
- geteuid() - Get effective user ID
- getgid() - Get group ID
- getegid() - Get effective group ID
- setuid() - Set user ID
- setgid() - Set group ID
- seteuid() - Set effective user ID
- setegid() - Set effective group ID
4. Memory Operations
Memory management operations:
- mmap() - Map memory
- munmap() - Unmap memory
- mprotect() - Change memory protection
- brk() - Change data segment size
- mremap() - Remap memory region
5. Signal Operations
Signal handling operations:
- signal() - Set signal handler (simplified)
- sigaction() - Set signal action (POSIX)
- kill() - Send signal to process
- raise() - Send signal to current process
- sigprocmask() - Set signal mask
- sigpending() - Get pending signals
- sigsuspend() - Suspend until signal received
6. Socket Operations
Network socket operations:
- socket() - Create socket
- bind() - Bind socket to address
- listen() - Listen for connections
- accept() - Accept connection
- connect() - Connect to address
- send() - Send data
- recv() - Receive data
- sendto() - Send data to address
- recvfrom() - Receive data from address
- shutdown() - Shutdown socket
- getsockopt() - Get socket option
- setsockopt() - Set socket option
7. IPC Operations
Inter-process communication:
- Shared Memory: shmget, shmat, shmdt, shmctl
- Message Queues: msgget, msgsnd, msgrcv, msgctl
- Semaphores: semget, semop, semctl
8. System Information
System information operations:
- uname() - Get system information
- sysinfo() - Get system statistics
9. Time Operations
Time and clock operations:
- time() - Get current time
- gettimeofday() - Get time with microseconds
- settimeofday() - Set time
- clock_gettime() - Get clock time
- clock_settime() - Set clock time
- nanosleep() - Sleep for nanoseconds
10. Service Management
Kernel-level service management operations:
- service_manager_init() (204) - Initialize service manager
- service_register() (200) - Register a service
- service_start() (201) - Start a service
- service_stop() (202) - Stop a service
- service_status() (203) - Get service status
11. I/O Operations
I/O control operations:
- ioctl() - I/O control
- fcntl() - File control
File System Calls
sys_read
int sys_read(int fd, void* buf, size_t count);
Parameters:
fd: File descriptorbuf: Buffer to read intocount: Number of bytes to read
Returns: Number of bytes read, or -1 on error
sys_write
int sys_write(int fd, const void* buf, size_t count);
Parameters:
fd: File descriptorbuf: Buffer to write fromcount: Number of bytes to write
Returns: Number of bytes written, or -1 on error
sys_open
int sys_open(const char* path, int flags);
Parameters:
path: File pathflags: Open flags (O_RDONLY, O_WRONLY, O_RDWR, etc.)
Returns: File descriptor, or -1 on error
sys_close
int sys_close(int fd);
Parameters:
fd: File descriptor to close
Returns: 0 on success, -1 on error
sys_exit
void sys_exit(int status);
Parameters:
status: Exit status code
Returns: Never returns (terminates process)
sys_gettime
uint32_t sys_gettime(void);
Returns: System time in milliseconds since boot
sys_sleep
void sys_sleep(uint32_t milliseconds);
Parameters:
milliseconds: Sleep duration in milliseconds
Returns: None (blocks until sleep completes)
sys_execve
int sys_execve(const char* pathname, char* const argv[], char* const envp[]);
Parameters:
pathname: Path to executable fileargv: Array of command-line argument strings (NULL-terminated)envp: Array of environment variable strings (NULL-terminated)
Returns: On success, does not return (process is replaced). On error, returns -1.
Description: Loads and executes an ELF executable, replacing the current process image. Uses the ELF loader and filesystem to load the program.
See execve() System Call for complete documentation.
Syscall Mechanism
System calls are invoked using the INT 0x80 instruction (32-bit) or SYSCALL instruction (64-bit):
; 64-bit user code calls syscall: mov rax, SYS_CALL_NUMBER ; Syscall number mov rdi, arg1 ; First argument mov rsi, arg2 ; Second argument mov rdx, arg3 ; Third argument syscall ; Invoke syscall ; Return value in RAX
System Call Convention
System calls follow standard conventions:
- Function names prefixed with
sys_ - Return values: 0 or positive on success, -1 on error
- Error codes set in errno (future implementation)
Related Documentation
- Service Management System - Kernel-level service management
- execve() System Call - Complete execve() documentation
- ELF Loader - Loading ELF executables
- File System - Reading files from disk
- Process Management - Process creation and management
System Call Numbers
System calls use Linux-compatible numbers for compatibility:
| Category | Syscall Numbers | Examples |
|---|---|---|
| File Operations | 3-6, 12-17 | read, write, open, close, lseek, stat |
| Directory Operations | 83-84, 89 | mkdir, rmdir, readdir |
| Process Operations | 39, 57, 60-61, 110 | getpid, fork, exit, wait, getppid |
| Memory Operations | 9-12 | mmap, munmap, mprotect, brk |
| Signal Operations | 48, 62, 67 | signal, kill, sigaction |
| Socket Operations | 41-43, 49-50 | socket, connect, accept, bind, listen |
| IPC Operations | 29-31, 67 | shmget, shmat, msgget, semget |
| System Info | 63, 99 | uname, sysinfo |
| Time Operations | 96, 201, 227 | gettimeofday, time, clock_gettime |
| Service Management | 200, 201, 202, 203, 204 | service_register, service_start, service_stop, service_status, service_manager_init |
| I/O Operations | 16, 72 | ioctl, fcntl |