safa_abi/
syscalls.rs

1/// defines Syscall numbers
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u16)]
4pub enum SyscallTable {
5    SysPExit = 0,
6    /// Yields execution to the next thread in the current CPU
7    SysTYield = 1,
8    /// Opens a file or directory with all permissions
9    SysFSOpenAll = 2,
10    /// Opens a file or directory with given mode (permissions and flags)
11    SysFSOpen = 25,
12    /// Deletes a path
13    SysFSRemovePath = 28,
14    /// Given a Directory resource, opens a Directory Iterator
15    SysFDirIterOpen = 8,
16    /// Destroys (closes) an open resource whether it is a file, directory, directory iterator, or any other resource
17    SysRDestroy = 5,
18    /// Legacy system call to close a directory iterator, use [`SysRDestroy`] instead
19    SysDirIterClose = 9,
20    /// Given a Directory Iterator Resource, returns the next DirEntry in the directory
21    SysDirIterNext = 10,
22    /// Performs a write operation on a given resource
23    ///
24    /// If the resource is a file, writes the given buffer to the file, the writes are pending until [SysIOSync] is performed.
25    ///
26    /// If the resource is a device, the behavior is device specific.
27    ///
28    /// Otherwise, errors with [`NotAFile`]
29    SysIOWrite = 3,
30    /// Performs a read operation on a given resource
31    ///
32    /// If the resource is a file, reads the given buffer from the file.
33    ///
34    /// If the resource is a device, the behavior is device specific.
35    ///
36    /// Otherwise, errors with [`NotAFile`]
37    SysIORead = 4,
38    /// Given a set of resources, waits for any of them to become ready for I/O (with specified events), returns the events that occurred causing the thread to wake up.
39    /// A single poll entry's layout is defined in [`crate::poll::PollEntry`].
40    SysIOPoll = 45,
41    /// Creates a new file
42    SysFSCreate = 6,
43    /// Creates a new directory
44    SysFSCreateDir = 7,
45    /// Performs a Sync operation on a given resource
46    ///
47    /// If the resource is a device, the behavior is device specific.
48    ///
49    /// If the resource is a file, writes all pending data to the file.
50    ///
51    /// Otherwise, does nothing or errors with [`NotAFile`]
52    SysIOSync = 16,
53    /// Truncates a file to a given size
54    SysIOTruncate = 17,
55    /// Sends a Command to a given resource that is a device
56    ///
57    /// The behavior is device specific.
58    ///
59    /// Takes 2 arguments: the command (can be as big as size of u16) and the argument (can be as big as size of u64)
60    SysIOCommand = 12,
61    /// Duplicates a given resource, returns a new resource ID pointing to the same resource internally
62    ///
63    /// Succeeds whether the resource is a file, directory, directory iterator or a device
64    SysRClone = 26,
65    // TODO: remove in favor of FAttrs
66    SysFSize = 22,
67    SysFAttrs = 24,
68    SysFGetDirEntry = 23,
69    /// Changes the current working directory to the given path
70    SysPCHDir = 14,
71    /// Gets the current working directory, returns [`crate::errors::ErrorStatus::Generic`] if the given buffer
72    /// is too small to hold the path, always returns the current working directory length whether or not the buffer is small
73    SysPGetCWD = 15,
74    /// Extends the current process's address space by the given amount, amount can be negative to shrink the address space
75    ///
76    /// Basically maps (or unmaps) the given amount of memory
77    /// Returns the new data break (address space end)
78    SysPSbrk = 18,
79    /// Spawns a new process
80    SysPSpawn = 19,
81    /// Spawns a thread inside the current process with the given entry point
82    SysTSpawn = 29,
83    /// Exits the current thread, takes an exit code so that it can act as [SysPExit] if it's the last thread in the process (otherwise it is unused)
84    SysTExit = 30,
85    /// Sleeps the current thread for the given amount of milliseconds, max is [`u64::MAX`]
86    SysTSleep = 31,
87    /// Waits for a child process with a given PID to exit, cleans it up and returns the exit code
88    SysPWait = 11,
89    /// Waits for a child thread with a given TID to exit
90    SysTWait = 32,
91    /// like [`SysPWait`] without the waiting part, cleans up the given process and returns the exit code
92    ///
93    /// returns [`crate::errors::ErrorStatus::InvalidPid`] if the process doesn't exist
94    ///
95    /// returns [`crate::errors::ErrorStatus::Generic`] if the process exists but hasn't exited yet
96    SysPTryCleanUp = 33,
97    /// Performs a WAIT(addr, val) on the current thread, also takes a timeout
98    SysTFutWait = 34,
99    /// Performs a WAKE(addr, n) on the current thread, wakes n threads waiting on the given address
100    SysTFutWake = 35,
101
102    SysShutdown = 20,
103    SysReboot = 21,
104    /// returns the Uptime of the system in milliseconds
105    SysUptime = 27,
106    /// Maps N pages after a given address to memory that may be shared with a Device or a File
107    ///
108    /// The given address is a just a hint unless specified (not yet implemented) with the flag [`crate::mem::MemMapFlags::FIXED`], in that case unlike mmap in linux you cannot map colliding regions
109    /// The address can be null telling the kernel to choose it's own hint
110    ///
111    /// The given Resource (the Resource to map) is ignored unless otherwise specified with the flag [`crate::mem::MemMapFlags::MAP_RESOURCE`]
112    ///
113    /// Returns A Resource that tracks that Memory Mapping and the mappings start address,
114    /// By default the resource is a global resource meaning it lives as long as the process,
115    /// The resource's lifetime can be thread bound with the flag `F_LOCAL`, when the thread exits the resource will be dropped
116    ///
117    /// To Manually Drop the resource aside from relaying on lifetimes use [`SyscallTable::SysRDestroy`],
118    /// To Sync the Memory with the associated File or Device you can either destroy it, let it die or use [`SyscallTable::SysIOSync`]
119    ///
120    /// Other flags include:
121    /// - [crate::mem::MemMapFlags::WRITE]
122    /// - [crate::mem::MemMapFlags::DISABLE_EXEC]
123    SysMemMap = 36,
124    /// Create a Shared Memory Descriptor, returning a key that points to it,
125    /// The life time of that descriptor is bound to the calling process or the thread if a flag was specified.
126    ///
127    /// The returned Key can then be opened from another process using [`SyscallTable::SysMemShmOpen`] and then [`SyscallTable::SysMemMap`]ped,
128    /// instead of calling [`SysMemShmOpen`] afterwards this returns an Optional Resource ID that can be mapped directly using [`SysMemMap`] from the calling process,
129    /// but the desired Process to communicate with, should use [`SyscallTable::SysMemShmOpen`] to get it's own copy.
130    ///
131    /// The lifetime of the key is extended for each [`SyscallTable::SysMemShmOpen`], so that it isn't dropped until all the threads/processes that owns it are dropped.
132    SysMemShmCreate = 42,
133    /// Creates a Resource that can be [`SyscallTable::SysMemMap`]ped to a Shared Memory Descriptor,
134    /// Takes in a key that was created using [`SyscallTable::SysMemShmCreate`].
135    ///
136    /// The lifetime of the Resource is bound to the process or a single thread if a flag was specified
137    SysMemShmOpen = 43,
138    // Sockets
139    /// Creates a Socket Descriptor resource
140    ///
141    /// That resource just describes properties of the socket, and then can be upgraded using a [`SyscallTable::SysSocketBind`] to a Server Socket,
142    /// or can be used to connect to one
143    SysSockCreate = 37,
144    /// Binds a Server Socket or binds and upgrades a Socket Descriptor created [`SyscallTable::SysSockCreate`] to a Server Socket Resource
145    /// and binds it to an address
146    SysSockBind = 38,
147    /// Configures a Server Socket's, binded with [`SyscallTable::SysSockBind`], listening queue, by default the socket cannot hold any pending connections before calling this
148    SysSockListen = 39,
149    /// Accepts a pending connection request from the listening queue configured with [`SyscallTable::SysSockListen`] takes in a Server Socket,
150    /// the pending connection request has been made using [`SyscallTable::SysSockConnect`], returns a Resource describing the server's end of the connection
151    SysSockAccept = 40,
152    /// Takes in a Socket Descriptor created with [`SyscallTable::SysSockCreate`], and a Server Socket address
153    ///
154    /// and attempts to connect to a Server Socket binded using [`SyscallTable::SysSockBind`],
155    /// returns a Resource describing the client's end of the connection
156    SysSockConnect = 41,
157    /// Given a socket sends data to another socket's address using the given socket with given flags,
158    /// sends data to a connected socket if the target address is None
159    SysSockSendTo = 46,
160    /// Given a socket, receive data from it with given flags then get the address that we received from if possible.
161    SysSockRecvFrom = 47,
162    /// Allocates a single new pair of Mother VTTY interface and a child VTTY Interface.
163    /// TODO: Write VTTY docs.
164    SysVTTYAlloc = 44,
165}
166
167// sadly we cannot use any proc macros here because this crate is used by the libstd port and more, they don't happen to like proc macros...
168/// When a new syscall is added, add to this number, and use the old value as the syscall number
169const NEXT_SYSCALL_NUM: u16 = 48;
170
171impl TryFrom<u16> for SyscallTable {
172    type Error = ();
173    fn try_from(value: u16) -> Result<Self, Self::Error> {
174        if value < NEXT_SYSCALL_NUM {
175            Ok(unsafe { core::mem::transmute(value) })
176        } else {
177            Err(())
178        }
179    }
180}