Skip to main content

syscall/
call.rs

1use super::{
2    arch::*,
3    data::{Map, Stat, StdFsCallMeta, TimeSpec},
4    error::Result,
5    flag::*,
6    number::*,
7};
8
9use core::mem;
10
11/// Get the current system time
12pub fn clock_gettime(clock: usize, tp: &mut TimeSpec) -> Result<usize> {
13    unsafe { syscall2(SYS_CLOCK_GETTIME, clock, tp as *mut TimeSpec as usize) }
14}
15
16/// Copy and transform a file descriptor into specified fd number
17pub fn dup_into(fd: usize, out: usize, buf: &[u8]) -> Result<usize> {
18    unsafe { syscall4(SYS_DUP_INTO, fd, buf.as_ptr() as usize, buf.len(), out) }
19}
20
21/// Copy and transform a file descriptor
22pub fn dup2(fd: usize, newfd: usize, buf: &[u8]) -> Result<usize> {
23    unsafe { syscall4(SYS_DUP2, fd, newfd, buf.as_ptr() as usize, buf.len()) }
24}
25
26/// Change file ownership
27pub fn fchown(fd: usize, uid: u32, gid: u32) -> Result<usize> {
28    unsafe { syscall3(SYS_FCHOWN, fd, uid as usize, gid as usize) }
29}
30
31/// Change file descriptor flags
32pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result<usize> {
33    unsafe { syscall3(SYS_FCNTL, fd, cmd, arg) }
34}
35
36/// Map a file into memory, but with the ability to set the address to map into, either as a hint
37/// or as a requirement of the map.
38///
39/// # Errors
40/// `EACCES` - the file descriptor was not open for reading
41/// `EBADF` - if the file descriptor was invalid
42/// `ENODEV` - mmapping was not supported
43/// `EINVAL` - invalid combination of flags
44/// `EEXIST` - if [`MapFlags::MAP_FIXED`] was set, and the address specified was already in use.
45///
46pub unsafe fn fmap(fd: usize, map: &Map) -> Result<usize> {
47    syscall3(
48        SYS_FMAP,
49        fd,
50        map as *const Map as usize,
51        mem::size_of::<Map>(),
52    )
53}
54
55/// Unmap whole (or partial) continous memory-mapped files
56pub unsafe fn funmap(addr: usize, len: usize) -> Result<usize> {
57    syscall2(SYS_FUNMAP, addr, len)
58}
59
60/// Retrieve the canonical path of a file
61pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
62    unsafe { syscall3(SYS_FPATH, fd, buf.as_mut_ptr() as usize, buf.len()) }
63}
64
65/// Create a link to a file
66pub fn flink<T: AsRef<str>>(fd: usize, path: T) -> Result<usize> {
67    let path = path.as_ref();
68    unsafe { syscall3(SYS_FLINK, fd, path.as_ptr() as usize, path.len()) }
69}
70
71/// Rename a file
72pub fn frename<T: AsRef<str>>(fd: usize, path: T) -> Result<usize> {
73    let path = path.as_ref();
74    unsafe { syscall3(SYS_FRENAME, fd, path.as_ptr() as usize, path.len()) }
75}
76
77/// Get metadata about a file
78pub fn fstat(fd: usize, stat: &mut Stat) -> Result<usize> {
79    unsafe {
80        syscall3(
81            SYS_FSTAT,
82            fd,
83            stat as *mut Stat as usize,
84            mem::size_of::<Stat>(),
85        )
86    }
87}
88
89/// Sync a file descriptor to its underlying medium
90pub fn fsync(fd: usize) -> Result<usize> {
91    unsafe { syscall1(SYS_FSYNC, fd) }
92}
93
94/// Fast userspace mutex
95pub unsafe fn futex(
96    addr: *mut i32,
97    op: usize,
98    val: i32,
99    val2: usize,
100    addr2: *mut i32,
101) -> Result<usize> {
102    syscall5(
103        SYS_FUTEX,
104        addr as usize,
105        op,
106        (val as isize) as usize,
107        val2,
108        addr2 as usize,
109    )
110}
111
112/// Seek to `offset` bytes in a file descriptor
113pub fn lseek(fd: usize, offset: isize, whence: usize) -> Result<usize> {
114    unsafe { syscall3(SYS_LSEEK, fd, offset as usize, whence) }
115}
116
117/// Change mapping flags
118pub unsafe fn mprotect(addr: usize, size: usize, flags: MapFlags) -> Result<usize> {
119    syscall3(SYS_MPROTECT, addr, size, flags.bits())
120}
121
122/// Sleep for the time specified in `req`
123pub fn nanosleep(req: &TimeSpec, rem: &mut TimeSpec) -> Result<usize> {
124    unsafe {
125        syscall2(
126            SYS_NANOSLEEP,
127            req as *const TimeSpec as usize,
128            rem as *mut TimeSpec as usize,
129        )
130    }
131}
132
133/// Open a file at a specific path into specified fd number
134pub fn openat_into<T: AsRef<str>>(
135    fd: usize,
136    out: usize,
137    path: T,
138    flags: usize,
139    fcntl_flags: usize,
140) -> Result<usize> {
141    let path = path.as_ref();
142    unsafe {
143        syscall6(
144            SYS_OPENAT_INTO,
145            fd,
146            path.as_ptr() as usize,
147            path.len(),
148            flags,
149            fcntl_flags,
150            out,
151        )
152    }
153}
154
155/// Remove a file at at specific path
156pub fn unlinkat<T: AsRef<str>>(fd: usize, path: T, flags: usize) -> Result<usize> {
157    let path = path.as_ref();
158    unsafe { syscall4(SYS_UNLINKAT, fd, path.as_ptr() as usize, path.len(), flags) }
159}
160/// Read from a file descriptor into a buffer
161pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
162    unsafe { syscall3(SYS_READ, fd, buf.as_mut_ptr() as usize, buf.len()) }
163}
164
165/// Write a buffer to a file descriptor
166///
167/// The kernel will attempt to write the bytes in `buf` to the file descriptor `fd`, returning
168/// either an `Err`, explained below, or `Ok(count)` where `count` is the number of bytes which
169/// were written.
170///
171/// # Errors
172///
173/// * `EAGAIN` - the file descriptor was opened with `O_NONBLOCK` and writing would block
174/// * `EBADF` - the file descriptor is not valid or is not open for writing
175/// * `EFAULT` - `buf` does not point to the process's addressible memory
176/// * `EIO` - an I/O error occurred
177/// * `ENOSPC` - the device containing the file descriptor has no room for data
178/// * `EPIPE` - the file descriptor refers to a pipe or socket whose reading end is closed
179pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
180    unsafe { syscall3(SYS_WRITE, fd, buf.as_ptr() as usize, buf.len()) }
181}
182
183/// Yield the process's time slice to the kernel
184///
185/// This function will return Ok(0) on success
186pub fn sched_yield() -> Result<usize> {
187    unsafe { syscall0(SYS_YIELD) }
188}
189
190pub trait Call {
191    unsafe fn raw_call(
192        &self,
193        payload_ptr: *const u8,
194        len: usize,
195        flags: CallFlags,
196        metadata: &[u64],
197    ) -> Result<usize>;
198}
199
200impl Call for usize {
201    unsafe fn raw_call(
202        &self,
203        payload_ptr: *const u8,
204        len: usize,
205        flags: CallFlags,
206        metadata: &[u64],
207    ) -> Result<usize> {
208        unsafe {
209            syscall5(
210                SYS_CALL,
211                *self,
212                payload_ptr as usize,
213                len,
214                metadata.len() | flags.bits(),
215                metadata.as_ptr() as usize,
216            )
217        }
218    }
219}
220
221impl Call for &[usize] {
222    unsafe fn raw_call(
223        &self,
224        payload_ptr: *const u8,
225        len: usize,
226        flags: CallFlags,
227        metadata: &[u64],
228    ) -> Result<usize> {
229        let combined_flags = flags | CallFlags::MULTIPLE_FDS;
230        unsafe {
231            syscall6(
232                SYS_CALL,
233                self.as_ptr() as usize,
234                payload_ptr as usize,
235                len,
236                metadata.len() | combined_flags.bits(),
237                metadata.as_ptr() as usize,
238                self.len() * mem::size_of::<usize>(),
239            )
240        }
241    }
242}
243
244/// SYS_CALL interface, read-only variant
245pub fn call_ro<T: Call>(
246    fd: T,
247    payload: &mut [u8],
248    flags: CallFlags,
249    metadata: &[u64],
250) -> Result<usize> {
251    unsafe {
252        fd.raw_call(
253            payload.as_mut_ptr(),
254            payload.len(),
255            flags | CallFlags::READ,
256            metadata,
257        )
258    }
259}
260/// SYS_CALL interface, write-only variant
261pub fn call_wo<T: Call>(
262    fd: T,
263    payload: &[u8],
264    flags: CallFlags,
265    metadata: &[u64],
266) -> Result<usize> {
267    unsafe {
268        fd.raw_call(
269            payload.as_ptr(),
270            payload.len(),
271            flags | CallFlags::WRITE,
272            metadata,
273        )
274    }
275}
276/// SYS_CALL interface, read-write variant
277pub fn call_rw<T: Call>(
278    fd: T,
279    payload: &mut [u8],
280    flags: CallFlags,
281    metadata: &[u64],
282) -> Result<usize> {
283    unsafe {
284        fd.raw_call(
285            payload.as_mut_ptr(),
286            payload.len(),
287            flags | CallFlags::READ | CallFlags::WRITE,
288            metadata,
289        )
290    }
291}
292
293pub fn std_fs_call<T: Call>(fd: T, payload: &mut [u8], metadata: &StdFsCallMeta) -> Result<usize> {
294    call_rw(fd, payload, CallFlags::STD_FS, metadata)
295}