exmap/sys/
mod.rs

1#![allow(
2    non_camel_case_types,
3    non_upper_case_globals,
4    dead_code,
5    non_snake_case,
6    unused_qualifications
7)]
8
9use rustix::{fd::AsRawFd, io};
10use std::ffi::{c_int, c_void};
11
12include!("sys.rs");
13
14fn to_result(ret: c_int) -> io::Result<c_int> {
15    if ret >= 0 {
16        Ok(ret)
17    } else {
18        Err(rustix::io::Errno::from_raw_os_error(-ret))
19    }
20}
21
22#[repr(C)]
23#[derive(Debug)]
24pub(crate) struct iovec {
25    pub(crate) iov_base: *mut c_void,
26    pub(crate) iov_len: usize,
27}
28
29#[repr(transparent)]
30pub(crate) struct exmap_iov_wrapper(ExmapIov);
31
32#[repr(C)]
33pub(crate) struct user_interface {}
34
35/// Generates the mmap offset for an interface
36///
37/// Manually taken from exmap header because C macros
38/// are not generated with bindgen
39pub const fn EXMAP_OFF_INTERFACE(n: i64) -> i64 {
40    EXMAP_OFF_INTERFACE_BASE | (n << 12)
41}
42
43pub(crate) unsafe fn exmap_setup<Fd: AsRawFd>(
44    fd: &Fd,
45    params: &exmap_ioctl_setup,
46) -> io::Result<()> {
47    to_result(unsafe {
48        sc::syscall3(
49            sc::nr::IOCTL,
50            fd.as_raw_fd() as usize,
51            Fix753_EXMAP_IOCTL_SETUP as usize,
52            params as *const _ as usize,
53        )
54    } as _)
55    .map(|_| ())
56}
57
58// SAFETY:
59// The file descriptor is a valid exmap fd. The ioctl request has the correct corresponding
60// argument
61pub(crate) unsafe fn exmap_ioctl<Fd: AsRawFd>(
62    fd: &Fd,
63    params: &exmap_action_params,
64) -> io::Result<c_int> {
65    to_result(unsafe {
66        sc::syscall3(
67            sc::nr::IOCTL as usize,
68            fd.as_raw_fd() as usize,
69            Fix753_EXMAP_IOCTL_ACTION as usize,
70            params as *const _ as usize,
71        )
72    } as _)
73}