Skip to content

Latest commit

 

History

History
387 lines (321 loc) · 15.9 KB

File metadata and controls

387 lines (321 loc) · 15.9 KB

⚙️ PromptOS Syscall ABI v1

Version: 1.0
Status: 📝 Draft
Architecture: x86_64


📖 1. Overview

PromptOS uses the SYSCALL/SYSRET mechanism for User → Kernel transitions. The ABI follows the System V AMD64 convention with adaptations.

🧠 Design Philosophy (ASSUMPTION: E1 = Hybrid)

  • Internal: Handle-based system (not file descriptor numbers)
  • External: POSIX compatibility shim in libc
  • No fork(): Instead spawn() for new processes
  • Signals present: POSIX-like; event-based IPC can be added optionally

📞 2. Calling Convention

Register Allocation

Register Usage
RAX Syscall number (Input) / Return Value (Output)
RDI Argument 1
RSI Argument 2
RDX Argument 3
R10 Argument 4 (not RCX, as SYSCALL overwrites RCX)
R8 Argument 5
R9 Argument 6

Caller-Saved Registers

The following registers may be modified by the kernel:

  • RAX, RCX, R11 (always modified by SYSCALL)
  • RDI, RSI, RDX, R10, R8, R9 (Arguments)

Callee-Saved Registers

The following registers are preserved by the kernel:

  • RBX, RBP, R12, R13, R14, R15, RSP

Return Values

RAX Value Meaning
>= 0 Success (Return value is syscall-specific)
< 0 Error (negative errno value)

3. Error Codes

#define EOK         0       // No error
#define EPERM       1       // Operation not permitted
#define ENOENT      2       // No such file or directory
#define ESRCH       3       // No such process
#define EINTR       4       // Interrupted system call
#define EIO         5       // I/O error
#define ENXIO       6       // No such device or address
#define E2BIG       7       // Argument list too long
#define ENOEXEC     8       // Exec format error
#define EBADF       9       // Bad file number
#define ECHILD      10      // No child processes
#define EAGAIN      11      // Try again (EWOULDBLOCK)
#define ENOMEM      12      // Out of memory
#define EACCES      13      // Permission denied
#define EFAULT      14      // Bad address
#define EBUSY       16      // Device or resource busy
#define EEXIST      17      // File exists
#define EXDEV       18      // Cross-device link
#define ENODEV      19      // No such device
#define ENOTDIR     20      // Not a directory
#define EISDIR      21      // Is a directory
#define EINVAL      22      // Invalid argument
#define ENFILE      23      // File table overflow
#define EMFILE      24      // Too many open files
#define ENOTTY      25      // Not a typewriter
#define EFBIG       27      // File too large
#define ENOSPC      28      // No space left on device
#define ESPIPE      29      // Illegal seek
#define EROFS       30      // Read-only file system
#define EMLINK      31      // Too many links
#define EPIPE       32      // Broken pipe
#define EDOM        33      // Math argument out of domain
#define ERANGE      34      // Math result not representable
#define ENOSYS      38      // Function not implemented
#define ENOTEMPTY   39      // Directory not empty
#define ELOOP       40      // Too many symbolic links
#define ENOMSG      42      // No message of desired type
#define ENODATA     61      // No data available
#define ETIME       62      // Timer expired
#define EOVERFLOW   75      // Value too large
#define EILSEQ      84      // Illegal byte sequence
#define ENOTSOCK    88      // Socket operation on non-socket
#define EMSGSIZE    90      // Message too long
#define EPROTOTYPE  91      // Protocol wrong type
#define ENOPROTOOPT 92      // Protocol not available
#define EPROTONOSUPPORT 93  // Protocol not supported
#define EOPNOTSUPP  95      // Operation not supported
#define EAFNOSUPPORT 97     // Address family not supported
#define EADDRINUSE  98      // Address already in use
#define EADDRNOTAVAIL 99    // Cannot assign requested address
#define ENETDOWN    100     // Network is down
#define ENETUNREACH 101     // Network is unreachable
#define ECONNABORTED 103    // Software caused connection abort
#define ECONNRESET  104     // Connection reset by peer
#define ENOBUFS     105     // No buffer space available
#define EISCONN     106     // Transport endpoint connected
#define ENOTCONN    107     // Transport endpoint not connected
#define ETIMEDOUT   110     // Connection timed out
#define ECONNREFUSED 111    // Connection refused
#define EHOSTUNREACH 113    // No route to host
#define EALREADY    114     // Operation already in progress
#define EINPROGRESS 115     // Operation now in progress
#define ECANCELED   125     // Operation Canceled

4. Syscall Table

4.1 Process Management (0-9)

No Name Arguments Return Description
0 sys_exit int status - Exit process
1 sys_spawn const char *path, char **argv, char **envp, SpawnFlags flags pid_t Create new process
2 sys_exec const char *path, char **argv, char **envp int Replace current program
3 sys_waitpid pid_t pid, int *status, int options pid_t Wait for child process
4 sys_getpid - pid_t Own PID
5 sys_getppid - pid_t Parent PID
6 sys_kill pid_t pid, int event int Send event to process
7 sys_yield - int Voluntarily yield CPU
8 sys_getuid - uid_t User ID
9 sys_setuid uid_t uid int Set User ID

4.2 File Operations (10-29)

No Name Arguments Return Description
10 sys_open const char *path, int flags, mode_t mode handle_t Open file
11 sys_close handle_t handle int Close handle
12 sys_read handle_t handle, void *buf, size_t count ssize_t Read
13 sys_write handle_t handle, const void *buf, size_t count ssize_t Write
14 sys_seek handle_t handle, off_t offset, int whence off_t Change position
15 sys_stat const char *path, struct stat *buf int File info (Path)
16 sys_fstat handle_t handle, struct stat *buf int File info (Handle)
17 sys_mkdir const char *path, mode_t mode int Create directory
18 sys_rmdir const char *path int Delete directory
19 sys_unlink const char *path int Delete file
20 sys_rename const char *old, const char *new int Rename
21 sys_readdir handle_t handle, struct dirent *buf, size_t count int Read directory
22 sys_getcwd char *buf, size_t size int Working directory
23 sys_chdir const char *path int Change directory
24 sys_link const char *old, const char *new int Create hardlink
25 sys_symlink const char *target, const char *link int Create symlink
26 sys_readlink const char *path, char *buf, size_t size ssize_t Read symlink
27 sys_truncate const char *path, off_t length int Truncate file
28 sys_ftruncate handle_t handle, off_t length int Truncate file (Handle)
29 sys_chmod const char *path, mode_t mode int Change permissions

4.3 Memory Management (30-39)

No Name Arguments Return Description
30 sys_mmap void *addr, size_t len, int prot, int flags, handle_t h, off_t off void* Map memory
31 sys_munmap void *addr, size_t len int Remove mapping
32 sys_mprotect void *addr, size_t len, int prot int Change protection
33 sys_brk void *addr void* Expand heap
34 sys_shmget int key, size_t size, int flags handle_t Create shared memory
35 sys_shmat handle_t shm, void *addr, int flags void* Attach shared memory
36 sys_shmdt void *addr int Detach shared memory

4.4 IPC (40-49)

No Name Arguments Return Description
40 sys_pipe handle_t handles[2], int flags int Create pipe
41 sys_dup handle_t old handle_t Duplicate handle
42 sys_dup2 handle_t old, handle_t new handle_t Duplicate handle to target
43 sys_poll struct pollfd *fds, size_t nfds, int timeout int I/O multiplexing
44 sys_futex int *uaddr, int op, int val, ... int Futex operations
45 sys_eventfd unsigned int initval, int flags handle_t Event handle
46 sys_msgqueue_create const char *name, int flags handle_t Message queue
47 sys_msgqueue_send handle_t mq, const void *buf, size_t len, int prio int Send message
48 sys_msgqueue_recv handle_t mq, void *buf, size_t len, int *prio ssize_t Receive message
49 sys_msgqueue_close handle_t mq int Close queue

4.5 Network (50-59)

No Name Arguments Return Description
50 sys_socket int domain, int type, int protocol handle_t Create socket
51 sys_bind handle_t s, const struct sockaddr *addr, size_t len int Bind address
52 sys_listen handle_t s, int backlog int Listen for connections
53 sys_accept handle_t s, struct sockaddr *addr, size_t *len handle_t Accept connection
54 sys_connect handle_t s, const struct sockaddr *addr, size_t len int Connect
55 sys_send handle_t s, const void *buf, size_t len, int flags ssize_t Send data
56 sys_recv handle_t s, void *buf, size_t len, int flags ssize_t Receive data
57 sys_sendto handle_t s, const void *buf, size_t len, int fl, sockaddr*, sz ssize_t Send UDP
58 sys_recvfrom handle_t s, void *buf, size_t len, int fl, sockaddr*, sz* ssize_t Receive UDP
59 sys_setsockopt handle_t s, int level, int opt, const void *val, size_t len int Set socket option

4.6 Time (60-64)

No Name Arguments Return Description
60 sys_clock_gettime int clock_id, struct timespec *tp int Read time
61 sys_clock_settime int clock_id, const struct timespec *tp int Set time
62 sys_nanosleep const struct timespec *req, struct timespec *rem int Sleep
63 sys_gettimeofday struct timeval *tv, void *tz int Time (µs resolution)
64 sys_timer_create int clock, struct sigevent *evp, handle_t *tid int Create timer

4.7 System (70-79)

No Name Arguments Return Description
70 sys_ioctl handle_t handle, unsigned long request, void *arg int Device control
71 sys_mount const char *src, const char *dst, const char *fs, int fl, void *data int Mount
72 sys_umount const char *target, int flags int Unmount
73 sys_reboot int cmd int Reboot/Shutdown
74 sys_sysinfo struct sysinfo *info int System info
75 sys_uname struct utsname *buf int OS info
76 sys_syslog int type, char *buf, int len int Kernel log
77 sys_getrlimit int resource, struct rlimit *lim int Read limit
78 sys_setrlimit int resource, const struct rlimit *lim int Set limit

4.8 Thread Management (80-89)

No Name Arguments Return Description
80 sys_thread_create void (*fn)(void*), void *arg, void *stack, size_t stack_size tid_t Create thread
81 sys_thread_exit void *retval - Exit thread
82 sys_thread_join tid_t tid, void **retval int Wait for thread
83 sys_thread_detach tid_t tid int Detach thread
84 sys_gettid - tid_t Thread ID
85 sys_tls_set void *ptr int Set TLS pointer
86 sys_tls_get - void* Read TLS pointer

4.9 GUI/Display (90-99)

No Name Arguments Return Description
90 sys_display_connect const char *name handle_t Display connection
91 sys_surface_create handle_t disp, int w, int h, int format handle_t Create surface
92 sys_surface_destroy handle_t surface int Destroy surface
93 sys_surface_buffer handle_t surface, void **buf, int *stride int Get buffer address
94 sys_surface_commit handle_t surface, int x, int y, int w, int h int Submit changes
95 sys_input_poll handle_t disp, InputEvent *events, int max int Poll input events

5. Types and Structures

Handle Types

typedef int64_t handle_t;       // Universal handle
typedef int32_t pid_t;          // Process ID
typedef int32_t tid_t;          // Thread ID
typedef int64_t off_t;          // File offset
typedef uint32_t mode_t;        // File mode
typedef uint32_t uid_t;         // User ID
typedef uint32_t gid_t;         // Group ID

struct stat

struct stat {
    dev_t       st_dev;         // Device ID
    ino_t       st_ino;         // Inode number
    mode_t      st_mode;        // File mode
    uint32_t    st_nlink;       // Number of hard links
    uid_t       st_uid;         // User ID
    gid_t       st_gid;         // Group ID
    dev_t       st_rdev;        // Device ID (if special file)
    off_t       st_size;        // Total size in bytes
    int64_t     st_atime;       // Last access time
    int64_t     st_mtime;       // Last modification time
    int64_t     st_ctime;       // Last status change time
    uint64_t    st_blksize;     // Block size for I/O
    uint64_t    st_blocks;      // Number of 512B blocks
};

struct dirent

struct dirent {
    ino_t       d_ino;          // Inode number
    off_t       d_off;          // Offset to next entry
    uint16_t    d_reclen;       // Record length
    uint8_t     d_type;         // File type
    char        d_name[256];    // File name
};

struct pollfd

struct pollfd {
    handle_t    fd;             // Handle
    int16_t     events;         // Requested events
    int16_t     revents;        // Returned events
};

#define POLLIN      0x0001      // Data available
#define POLLOUT     0x0004      // Writing possible
#define POLLERR     0x0008      // Error condition
#define POLLHUP     0x0010      // Hung up

SpawnFlags

typedef enum {
    SPAWN_NONE          = 0,
    SPAWN_DETACHED      = (1 << 0),     // Don't wait for child
    SPAWN_NEW_SESSION   = (1 << 1),     // Create new session
    SPAWN_SHARE_CWD     = (1 << 2),     // Share working directory
} SpawnFlags;

6. Example: Hello World in Assembly

; sys_write(stdout, "Hello, World!\n", 14)
section .data
    msg: db "Hello, World!", 10
    msg_len equ $ - msg

section .text
global _start

_start:
    ; sys_write(1, msg, msg_len)
    mov rax, 13         ; sys_write
    mov rdi, 1          ; stdout handle
    lea rsi, [rel msg]  ; buffer
    mov rdx, msg_len    ; length
    syscall
    
    ; sys_exit(0)
    mov rax, 0          ; sys_exit
    xor rdi, rdi        ; status = 0
    syscall

7. POSIX Compatibility (libc Shim)

The libc maps POSIX functions to native syscalls:

POSIX PromptOS
fork() Not supported (Error ENOSYS)
fork() + exec() spawn()
signal() Event-based IPC
sigaction() Event-based IPC
file descriptors Handles (transparent via libc)

8. Capability Checks

Certain syscalls require capabilities:

Syscall Capability
sys_mount CAP_FS_MOUNT
sys_reboot CAP_SYS_ADMIN
sys_setuid CAP_SYS_ADMIN
sys_bind (port < 1024) CAP_NET_BIND
sys_ioctl (raw hardware) CAP_HW_RAW

Syscall ABI v1 - PromptOS