# System Call Reference Complete reference for ModuOS system calls. ## Calling Convention **Registers**: ``` RAX: Syscall number RDI: Argument 1 RSI: Argument 2 RDX: Argument 3 RCX: Argument 4 R8: Argument 5 R9: Argument 6 Return: RAX (0 or positive on success, negative on error) ``` **Trigger**: `int 0x80` ## Process Control ### SYS_EXIT (0) ```c void exit(int status); ``` Terminate current process with exit code. ### SYS_GETPID (5) ```c int getpid(void); ``` Get current process ID. **Returns**: Process ID ### SYS_FORK (6) ```c int fork(void); ``` Create child process (not fully implemented). **Returns**: 0 in child, child PID in parent, negative on error ### SYS_EXEC (7) ```c int exec(const char *path, char **argv); ``` Execute program from file. **Returns**: Negative on error (doesn't return on success) ### SYS_YIELD (11) ```c void yield(void); ``` Yield CPU to other processes. ### SYS_SLEEP (10) ```c int sleep(uint64_t milliseconds); ``` Sleep for specified time. **Returns**: 0 on success ## File I/O ### SYS_OPEN (3) ```c int open(const char *path, int flags); ``` Open file. **Flags**: - `O_RDONLY` (0): Read only - `O_WRONLY` (1): Write only - `O_RDWR` (2): Read/Write **Returns**: File descriptor, or negative on error ### SYS_CLOSE (4) ```c int close(int fd); ``` Close file descriptor. **Returns**: 0 on success, negative on error ### SYS_READ (1) ```c int read(int fd, void *buffer, size_t count); ``` Read from file. **Returns**: Bytes read, or negative on error ### SYS_WRITE (2) ```c int write(int fd, const void *buffer, size_t count); ``` Write to file. **Returns**: Bytes written, or negative on error ## Memory Management ### SYS_BRK (14) ```c void* brk(void *addr); ``` Change data segment size. **Returns**: New break address, or NULL on error ### SYS_MMAP (12) ```c void* mmap(void *addr, size_t length, int prot, int flags); ``` Map memory region. **Returns**: Mapped address, or NULL on error ### SYS_MUNMAP (13) ```c int munmap(void *addr, size_t length); ``` Unmap memory region. **Returns**: 0 on success, negative on error ## Error Codes ```c #define ESUCCESS 0 // Success #define EPERM -1 // Operation not permitted #define ENOENT -2 // No such file or directory #define EBADF -3 // Bad file descriptor #define ENOMEM -4 // Out of memory #define EINVAL -5 // Invalid argument #define ENOSYS -6 // Function not implemented ``` ## Example Usage ```c // Open and read file int fd = open("/file.txt", O_RDONLY); if (fd < 0) { // Error handling return; } char buffer[1024]; int bytes = read(fd, buffer, sizeof(buffer)); close(fd); // Exit program exit(0); ``` ## Next Steps - [System Calls](System-Calls.md) - Implementation details - [Kernel API](Kernel-API.md) - Kernel functions