syscall/
call.rs

1use super::{
2    arch::*,
3    data::{Map, Stat, StatVfs, TimeSpec},
4    error::Result,
5    flag::*,
6    number::*,
7};
8
9use core::mem;
10
11/// Close a file
12pub fn close(fd: usize) -> Result<usize> {
13    unsafe { syscall1(SYS_CLOSE, fd) }
14}
15
16/// Get the current system time
17pub fn clock_gettime(clock: usize, tp: &mut TimeSpec) -> Result<usize> {
18    unsafe { syscall2(SYS_CLOCK_GETTIME, clock, tp as *mut TimeSpec as usize) }
19}
20
21/// Copy and transform a file descriptor
22pub fn dup(fd: usize, buf: &[u8]) -> Result<usize> {
23    unsafe { syscall3(SYS_DUP, fd, buf.as_ptr() as usize, buf.len()) }
24}
25
26/// Copy and transform a file descriptor
27pub fn dup2(fd: usize, newfd: usize, buf: &[u8]) -> Result<usize> {
28    unsafe { syscall4(SYS_DUP2, fd, newfd, buf.as_ptr() as usize, buf.len()) }
29}
30
31/// Change file permissions
32pub fn fchmod(fd: usize, mode: u16) -> Result<usize> {
33    unsafe { syscall2(SYS_FCHMOD, fd, mode as usize) }
34}
35
36/// Change file ownership
37pub fn fchown(fd: usize, uid: u32, gid: u32) -> Result<usize> {
38    unsafe { syscall3(SYS_FCHOWN, fd, uid as usize, gid as usize) }
39}
40
41/// Change file descriptor flags
42pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result<usize> {
43    unsafe { syscall3(SYS_FCNTL, fd, cmd, arg) }
44}
45
46/// Map a file into memory, but with the ability to set the address to map into, either as a hint
47/// or as a requirement of the map.
48///
49/// # Errors
50/// `EACCES` - the file descriptor was not open for reading
51/// `EBADF` - if the file descriptor was invalid
52/// `ENODEV` - mmapping was not supported
53/// `EINVAL` - invalid combination of flags
54/// `EEXIST` - if [`MapFlags::MAP_FIXED`] was set, and the address specified was already in use.
55///
56pub unsafe fn fmap(fd: usize, map: &Map) -> Result<usize> {
57    syscall3(
58        SYS_FMAP,
59        fd,
60        map as *const Map as usize,
61        mem::size_of::<Map>(),
62    )
63}
64
65/// Unmap whole (or partial) continous memory-mapped files
66pub unsafe fn funmap(addr: usize, len: usize) -> Result<usize> {
67    syscall2(SYS_FUNMAP, addr, len)
68}
69
70/// Retrieve the canonical path of a file
71pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
72    unsafe { syscall3(SYS_FPATH, fd, buf.as_mut_ptr() as usize, buf.len()) }
73}
74
75/// Create a link to a file
76pub fn flink<T: AsRef<str>>(fd: usize, path: T) -> Result<usize> {
77    let path = path.as_ref();
78    unsafe { syscall3(SYS_FLINK, fd, path.as_ptr() as usize, path.len()) }
79}
80
81/// Rename a file
82pub fn frename<T: AsRef<str>>(fd: usize, path: T) -> Result<usize> {
83    let path = path.as_ref();
84    unsafe { syscall3(SYS_FRENAME, fd, path.as_ptr() as usize, path.len()) }
85}
86
87/// Get metadata about a file
88pub fn fstat(fd: usize, stat: &mut Stat) -> Result<usize> {
89    unsafe {
90        syscall3(
91            SYS_FSTAT,
92            fd,
93            stat as *mut Stat as usize,
94            mem::size_of::<Stat>(),
95        )
96    }
97}
98
99/// Get metadata about a filesystem
100pub fn fstatvfs(fd: usize, stat: &mut StatVfs) -> Result<usize> {
101    unsafe {
102        syscall3(
103            SYS_FSTATVFS,
104            fd,
105            stat as *mut StatVfs as usize,
106            mem::size_of::<StatVfs>(),
107        )
108    }
109}
110
111/// Sync a file descriptor to its underlying medium
112pub fn fsync(fd: usize) -> Result<usize> {
113    unsafe { syscall1(SYS_FSYNC, fd) }
114}
115
116/// Truncate or extend a file to a specified length
117pub fn ftruncate(fd: usize, len: usize) -> Result<usize> {
118    unsafe { syscall2(SYS_FTRUNCATE, fd, len) }
119}
120
121// Change modify and/or access times
122pub fn futimens(fd: usize, times: &[TimeSpec]) -> Result<usize> {
123    unsafe {
124        syscall3(
125            SYS_FUTIMENS,
126            fd,
127            times.as_ptr() as usize,
128            times.len() * mem::size_of::<TimeSpec>(),
129        )
130    }
131}
132
133/// Fast userspace mutex
134pub unsafe fn futex(
135    addr: *mut i32,
136    op: usize,
137    val: i32,
138    val2: usize,
139    addr2: *mut i32,
140) -> Result<usize> {
141    syscall5(
142        SYS_FUTEX,
143        addr as usize,
144        op,
145        (val as isize) as usize,
146        val2,
147        addr2 as usize,
148    )
149}
150
151/// Seek to `offset` bytes in a file descriptor
152pub fn lseek(fd: usize, offset: isize, whence: usize) -> Result<usize> {
153    unsafe { syscall3(SYS_LSEEK, fd, offset as usize, whence) }
154}
155
156/// Make a new scheme namespace
157pub fn mkns(schemes: &[[usize; 2]]) -> Result<usize> {
158    unsafe { syscall2(SYS_MKNS, schemes.as_ptr() as usize, schemes.len()) }
159}
160
161/// Change mapping flags
162pub unsafe fn mprotect(addr: usize, size: usize, flags: MapFlags) -> Result<usize> {
163    syscall3(SYS_MPROTECT, addr, size, flags.bits())
164}
165
166/// Sleep for the time specified in `req`
167pub fn nanosleep(req: &TimeSpec, rem: &mut TimeSpec) -> Result<usize> {
168    unsafe {
169        syscall2(
170            SYS_NANOSLEEP,
171            req as *const TimeSpec as usize,
172            rem as *mut TimeSpec as usize,
173        )
174    }
175}
176
177/// Open a file
178pub fn open<T: AsRef<str>>(path: T, flags: usize) -> Result<usize> {
179    let path = path.as_ref();
180    unsafe { syscall3(SYS_OPEN, path.as_ptr() as usize, path.len(), flags) }
181}
182
183/// Open a file at a specific path
184pub fn openat<T: AsRef<str>>(fd: usize, path: T, flags: usize) -> Result<usize> {
185    let path = path.as_ref();
186    unsafe { syscall4(SYS_OPENAT, fd, path.as_ptr() as usize, path.len(), flags) }
187}
188
189/// Read from a file descriptor into a buffer
190pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
191    unsafe { syscall3(SYS_READ, fd, buf.as_mut_ptr() as usize, buf.len()) }
192}
193
194/// Remove a directory
195pub fn rmdir<T: AsRef<str>>(path: T) -> Result<usize> {
196    let path = path.as_ref();
197    unsafe { syscall2(SYS_RMDIR, path.as_ptr() as usize, path.len()) }
198}
199
200/// Remove a file
201pub fn unlink<T: AsRef<str>>(path: T) -> Result<usize> {
202    let path = path.as_ref();
203    unsafe { syscall2(SYS_UNLINK, path.as_ptr() as usize, path.len()) }
204}
205
206/// Write a buffer to a file descriptor
207///
208/// The kernel will attempt to write the bytes in `buf` to the file descriptor `fd`, returning
209/// either an `Err`, explained below, or `Ok(count)` where `count` is the number of bytes which
210/// were written.
211///
212/// # Errors
213///
214/// * `EAGAIN` - the file descriptor was opened with `O_NONBLOCK` and writing would block
215/// * `EBADF` - the file descriptor is not valid or is not open for writing
216/// * `EFAULT` - `buf` does not point to the process's addressible memory
217/// * `EIO` - an I/O error occurred
218/// * `ENOSPC` - the device containing the file descriptor has no room for data
219/// * `EPIPE` - the file descriptor refers to a pipe or socket whose reading end is closed
220pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
221    unsafe { syscall3(SYS_WRITE, fd, buf.as_ptr() as usize, buf.len()) }
222}
223
224/// Yield the process's time slice to the kernel
225///
226/// This function will return Ok(0) on success
227pub fn sched_yield() -> Result<usize> {
228    unsafe { syscall0(SYS_YIELD) }
229}
230
231/// Send a file descriptor `fd`, handled by the scheme providing `receiver_socket`. `flags` is
232/// currently unused (must be zero), and `arg` is included in the scheme call.
233///
234/// The scheme can return an arbitrary value.
235pub fn sendfd(receiver_socket: usize, fd: usize, flags: usize, arg: u64) -> Result<usize> {
236    #[cfg(target_pointer_width = "32")]
237    unsafe {
238        syscall5(
239            SYS_SENDFD,
240            receiver_socket,
241            fd,
242            flags,
243            arg as u32 as usize,
244            (arg >> 32) as u32 as usize,
245        )
246    }
247
248    #[cfg(target_pointer_width = "64")]
249    unsafe {
250        syscall4(SYS_SENDFD, receiver_socket, fd, flags, arg as usize)
251    }
252}