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 /// Creates a new file
39 SysFSCreate = 6,
40 /// Creates a new directory
41 SysFSCreateDir = 7,
42 /// Performs a Sync operation on a given resource
43 ///
44 /// If the resource is a device, the behavior is device specific.
45 ///
46 /// If the resource is a file, writes all pending data to the file.
47 ///
48 /// Otherwise, does nothing or errors with [`NotAFile`]
49 SysIOSync = 16,
50 /// Truncates a file to a given size
51 SysIOTruncate = 17,
52 /// Sends a Command to a given resource that is a device
53 ///
54 /// The behavior is device specific.
55 ///
56 /// Takes 2 arguments: the command (can be as big as size of u16) and the argument (can be as big as size of u64)
57 SysIOCommand = 12,
58 /// Duplicates a given resource, returns a new resource ID pointing to the same resource internally
59 ///
60 /// Succeeds whether the resource is a file, directory, directory iterator or a device
61 SysRDup = 26,
62 // TODO: remove in favor of FAttrs
63 SysFSize = 22,
64 SysFAttrs = 24,
65 SysFGetDirEntry = 23,
66 /// Changes the current working directory to the given path
67 SysPCHDir = 14,
68 /// Gets the current working directory, returns [`crate::errors::ErrorStatus::Generic`] if the given buffer
69 /// is too small to hold the path, always returns the current working directory length whether or not the buffer is small
70 SysPGetCWD = 15,
71 /// Extends the current process's address space by the given amount, amount can be negative to shrink the address space
72 ///
73 /// Basically maps (or unmaps) the given amount of memory
74 /// Returns the new data break (address space end)
75 SysPSbrk = 18,
76 /// Spawns a new process
77 SysPSpawn = 19,
78 /// Spawns a thread inside the current process with the given entry point
79 SysTSpawn = 29,
80 /// 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)
81 SysTExit = 30,
82 /// Sleeps the current thread for the given amount of milliseconds, max is [`u64::MAX`]
83 SysTSleep = 31,
84 /// Waits for a child process with a given PID to exit, cleans it up and returns the exit code
85 SysPWait = 11,
86 /// Waits for a child thread with a given TID to exit
87 SysTWait = 32,
88 /// like [`SysPWait`] without the waiting part, cleans up the given process and returns the exit code
89 ///
90 /// returns [`crate::errors::ErrorStatus::InvalidPid`] if the process doesn't exist
91 ///
92 /// returns [`crate::errors::ErrorStatus::Generic`] if the process exists but hasn't exited yet
93 SysPTryCleanUp = 33,
94 /// Performs a WAIT(addr, val) on the current thread, also takes a timeout
95 SysTFutWait = 34,
96 /// Performs a WAKE(addr, n) on the current thread, wakes n threads waiting on the given address
97 SysTFutWake = 35,
98
99 SysShutdown = 20,
100 SysReboot = 21,
101 /// returns the Uptime of the system in milliseconds
102 SysUptime = 27,
103 /// Maps N pages after a given address to memory that may be shared with a Device or a File
104 ///
105 /// 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
106 /// The address can be null telling the kernel to choose it's own hint
107 ///
108 /// The given reosurce (the resource to map) is ignored unless otherwise specified with the flag [`crate::mem::MemMapFlags::MAP_RESOURCE`]
109 ///
110 /// Returns A Resource that tracks that Memory Mapping and the mappings start address,
111 /// By default the resource is a global resource meaning it lives as long as the process,
112 /// The resource's lifetime can be thread bound with the flag `F_LOCAL`, when the thread exits the resource will be dropped
113 ///
114 /// To Manually Drop the resource aside from relaying on lifetimes use [`SyscallTable::SysRDestroy`],
115 /// To Sync the Memory with the associated File or Device you can either destroy it, let it die or use [`SyscallTable::SysIOSync`]
116 ///
117 /// Other flags include:
118 /// - [crate::mem::MemMapFlags::WRITE]
119 /// - [crate::mem::MemMapFlags::DISABLE_EXEC]
120 SysMemMap = 36,
121}
122
123// 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...
124/// When a new syscall is added, add to this number, and use the old value as the syscall number
125const NEXT_SYSCALL_NUM: u16 = 37;
126
127impl TryFrom<u16> for SyscallTable {
128 type Error = ();
129 fn try_from(value: u16) -> Result<Self, Self::Error> {
130 if value < NEXT_SYSCALL_NUM {
131 Ok(unsafe { core::mem::transmute(value) })
132 } else {
133 Err(())
134 }
135 }
136}