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>>(
185    fd: usize,
186    path: T,
187    flags: usize,
188    fcntl_flags: usize,
189) -> Result<usize> {
190    let path = path.as_ref();
191    unsafe {
192        syscall5(
193            SYS_OPENAT,
194            fd,
195            path.as_ptr() as usize,
196            path.len(),
197            flags,
198            fcntl_flags,
199        )
200    }
201}
202/// Open a file at a specific path with filter
203pub fn openat_with_filter<T: AsRef<str>>(
204    fd: usize,
205    path: T,
206    flags: usize,
207    fcntl_flags: usize,
208    euid: u32,
209    egid: u32,
210) -> Result<usize> {
211    let path = path.as_ref();
212    unsafe {
213        syscall6(
214            SYS_OPENAT_WITH_FILTER,
215            fd,
216            path.as_ptr() as usize,
217            path.len(),
218            flags | fcntl_flags,
219            // NOTE: Short-term solution to allow namespace management.
220            // In the long term, we need to figure out how we should best handle
221            // Unix permissions using capabilities.
222            euid as usize,
223            egid as usize,
224        )
225    }
226}
227
228/// Remove a file at at specific path
229pub fn unlinkat<T: AsRef<str>>(fd: usize, path: T, flags: usize) -> Result<usize> {
230    let path = path.as_ref();
231    unsafe { syscall4(SYS_UNLINKAT, fd, path.as_ptr() as usize, path.len(), flags) }
232}
233/// Remove a file at at specific path with filter
234pub fn unlinkat_with_filter<T: AsRef<str>>(
235    fd: usize,
236    path: T,
237    flags: usize,
238    euid: u32,
239    egid: u32,
240) -> Result<usize> {
241    let path = path.as_ref();
242    unsafe {
243        syscall6(
244            SYS_UNLINKAT_WITH_FILTER,
245            fd,
246            path.as_ptr() as usize,
247            path.len(),
248            flags,
249            // NOTE: Short-term solution to allow namespace management.
250            // In the long term, we need to figure out how we should best handle
251            // Unix permissions using capabilities.
252            euid as usize,
253            egid as usize,
254        )
255    }
256}
257
258/// Read from a file descriptor into a buffer
259pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
260    unsafe { syscall3(SYS_READ, fd, buf.as_mut_ptr() as usize, buf.len()) }
261}
262
263/// Write a buffer to a file descriptor
264///
265/// The kernel will attempt to write the bytes in `buf` to the file descriptor `fd`, returning
266/// either an `Err`, explained below, or `Ok(count)` where `count` is the number of bytes which
267/// were written.
268///
269/// # Errors
270///
271/// * `EAGAIN` - the file descriptor was opened with `O_NONBLOCK` and writing would block
272/// * `EBADF` - the file descriptor is not valid or is not open for writing
273/// * `EFAULT` - `buf` does not point to the process's addressible memory
274/// * `EIO` - an I/O error occurred
275/// * `ENOSPC` - the device containing the file descriptor has no room for data
276/// * `EPIPE` - the file descriptor refers to a pipe or socket whose reading end is closed
277pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
278    unsafe { syscall3(SYS_WRITE, fd, buf.as_ptr() as usize, buf.len()) }
279}
280
281/// Yield the process's time slice to the kernel
282///
283/// This function will return Ok(0) on success
284pub fn sched_yield() -> Result<usize> {
285    unsafe { syscall0(SYS_YIELD) }
286}
287
288/// Send a file descriptor `fd`, handled by the scheme providing `receiver_socket`. `flags` is
289/// currently unused (must be zero), and `arg` is included in the scheme call.
290///
291/// The scheme can return an arbitrary value.
292pub fn sendfd(receiver_socket: usize, fd: usize, flags: usize, arg: u64) -> Result<usize> {
293    #[cfg(target_pointer_width = "32")]
294    unsafe {
295        syscall5(
296            SYS_SENDFD,
297            receiver_socket,
298            fd,
299            flags,
300            arg as u32 as usize,
301            (arg >> 32) as u32 as usize,
302        )
303    }
304
305    #[cfg(target_pointer_width = "64")]
306    unsafe {
307        syscall4(SYS_SENDFD, receiver_socket, fd, flags, arg as usize)
308    }
309}
310
311/// SYS_CALL interface, read-only variant
312pub fn call_ro(fd: usize, payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
313    let combined_flags = flags | CallFlags::READ;
314    unsafe {
315        syscall5(
316            SYS_CALL,
317            fd,
318            payload.as_mut_ptr() as usize,
319            payload.len(),
320            metadata.len() | combined_flags.bits(),
321            metadata.as_ptr() as usize,
322        )
323    }
324}
325/// SYS_CALL interface, write-only variant
326pub fn call_wo(fd: usize, payload: &[u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
327    let combined_flags = flags | CallFlags::WRITE;
328    unsafe {
329        syscall5(
330            SYS_CALL,
331            fd,
332            payload.as_ptr() as *mut u8 as usize,
333            payload.len(),
334            metadata.len() | combined_flags.bits(),
335            metadata.as_ptr() as usize,
336        )
337    }
338}
339/// SYS_CALL interface, read-write variant
340pub fn call_rw(fd: usize, payload: &mut [u8], flags: CallFlags, metadata: &[u64]) -> Result<usize> {
341    let combined_flags = flags | CallFlags::READ | CallFlags::WRITE;
342    unsafe {
343        syscall5(
344            SYS_CALL,
345            fd,
346            payload.as_mut_ptr() as usize,
347            payload.len(),
348            metadata.len() | combined_flags.bits(),
349            metadata.as_ptr() as usize,
350        )
351    }
352}