Skip to content

Syscall Reference

NtinosTheGamer2324 edited this page Dec 11, 2025 · 1 revision

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)

void exit(int status);

Terminate current process with exit code.

SYS_GETPID (5)

int getpid(void);

Get current process ID.

Returns: Process ID

SYS_FORK (6)

int fork(void);

Create child process (not fully implemented).

Returns: 0 in child, child PID in parent, negative on error

SYS_EXEC (7)

int exec(const char *path, char **argv);

Execute program from file.

Returns: Negative on error (doesn't return on success)

SYS_YIELD (11)

void yield(void);

Yield CPU to other processes.

SYS_SLEEP (10)

int sleep(uint64_t milliseconds);

Sleep for specified time.

Returns: 0 on success

File I/O

SYS_OPEN (3)

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)

int close(int fd);

Close file descriptor.

Returns: 0 on success, negative on error

SYS_READ (1)

int read(int fd, void *buffer, size_t count);

Read from file.

Returns: Bytes read, or negative on error

SYS_WRITE (2)

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)

void* brk(void *addr);

Change data segment size.

Returns: New break address, or NULL on error

SYS_MMAP (12)

void* mmap(void *addr, size_t length, int prot, int flags);

Map memory region.

Returns: Mapped address, or NULL on error

SYS_MUNMAP (13)

int munmap(void *addr, size_t length);

Unmap memory region.

Returns: 0 on success, negative on error

Error Codes

#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

// 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

Clone this wiki locally