1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright 2020-2021, Cerno
// Copyright 2024, DorotaC
//
// Licensed under the MIT License
// See the LICENSE file or <http://opensource.org/licenses/MIT>

#![cfg_attr(
    feature = "nightly",
    feature(
        type_privacy_lints,
        non_exhaustive_omitted_patterns_lint,
        strict_provenance
    )
)]
#![cfg_attr(
    feature = "nightly",
    warn(
        fuzzy_provenance_casts,
        lossy_provenance_casts,
        unnameable_types,
        non_exhaustive_omitted_patterns,
        clippy::empty_enum_variants_with_brackets
    )
)]
#![doc = include_str!("../README.md")]

use core::{ffi::c_void, fmt, num::TryFromIntError, ptr, slice};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd};

use log::{debug, warn};
use rustix::{
    fs::fstat,
    mm::{mmap, munmap, MapFlags, ProtFlags},
    param::page_size,
};

mod ioctl;
use ioctl::{
    dma_buf_begin_cpu_read_access, dma_buf_begin_cpu_readwrite_access,
    dma_buf_begin_cpu_write_access, dma_buf_end_cpu_read_access, dma_buf_end_cpu_readwrite_access,
    dma_buf_end_cpu_write_access,
};

/// Error type to map a [`DmaBuf`]
#[non_exhaustive]
#[derive(thiserror::Error, Debug)]
pub enum MapError {
    /// An Error occurred while accessing the buffer file descriptor
    #[error("Could not access the buffer file descriptor: {reason}")]
    FdAccess {
        /// Description of the Error
        reason: String,

        /// Source of the Error
        source: std::io::Error,
    },

    /// An Error occurred while mapping the buffer file descriptor
    #[error("Could not map the buffer file descriptor: {reason}")]
    MappingFailed {
        /// Description of the Error
        reason: String,

        /// Source of the Error
        source: std::io::Error,
    },

    /// An Error occurred while converting between Integer types
    #[error("Integer Conversion Error")]
    IntegerConversionFailed(#[from] TryFromIntError),
}

/// A DMA-Buf buffer
#[derive(Debug)]
pub struct DmaBuf(OwnedFd);

impl DmaBuf {
    fn memory_map(&self) -> Result<(usize, *mut u8), MapError> {
        debug!("Mapping DMA-Buf buffer with File Descriptor {:#?}", self.0);

        let stat = fstat(&self.0).map_err(|e| MapError::FdAccess {
            reason: e.to_string(),
            source: std::io::Error::from(e),
        })?;

        let len = usize::try_from(stat.st_size)?.next_multiple_of(page_size());
        debug!("Valid buffer, size {len}");

        // SAFETY: It's unclear at this point what the exact safety requirements from mmap are, but
        // our fd is valid and the length is aligned, so that's something.
        let mapping_ptr = unsafe {
            mmap(
                ptr::null_mut(),
                len,
                ProtFlags::READ | ProtFlags::WRITE,
                MapFlags::SHARED,
                &self.0,
                0,
            )
        }
        .map(<*mut c_void>::cast::<u8>)
        .map_err(|e| MapError::MappingFailed {
            reason: e.to_string(),
            source: std::io::Error::from(e),
        })?;

        debug!("Memory Mapping Done");
        
        Ok((len, mapping_ptr))
    }

    /// Maps a `DmaBuf` for the CPU to access it read-
    ///
    /// DMA-Buf [requires the user-space](https://docs.kernel.org/driver-api/dma-buf.html#cpu-access-to-dma-buffer-objects) to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. 
    ///
    /// # Panics
    ///
    /// If the buffer size reported by the kernel (`i64`) cannot fit into an `usize`.
    ///
    /// # Errors
    ///
    /// Will return an error if either the Buffer's length can't be retrieved, or if the mmap call
    /// fails.
    pub fn memory_map_ro(&self) -> Result<MappedDmaBufRo<'_>, MapError> {
        let (len, mapping_ptr) = self.memory_map()?;
        MappedDmaBufRo::new(self, len, mapping_ptr)
    }

    /// Maps a `DmaBuf` for the CPU to access it read-write
    ///
    /// DMA-Buf [requires the user-space](https://docs.kernel.org/driver-api/dma-buf.html#cpu-access-to-dma-buffer-objects) to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. 
    ///
    /// # Panics
    ///
    /// If the buffer size reported by the kernel (`i64`) cannot fit into an `usize`.
    ///
    /// # Errors
    ///
    /// Will return an error if either the Buffer's length can't be retrieved, or if the mmap call
    /// fails.
    pub fn memory_map_rw(&mut self) -> Result<MappedDmaBufRw<'_>, MapError> {
        let (len, mapping_ptr) = self.memory_map()?;
        MappedDmaBufRw::new(self, len, mapping_ptr)
    }
    
    /// Maps a `DmaBuf` for the CPU to access it write-only
    ///
    /// DMA-Buf [requires the user-space](https://docs.kernel.org/driver-api/dma-buf.html#cpu-access-to-dma-buffer-objects) to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. 
    ///
    /// # Panics
    ///
    /// If the buffer size reported by the kernel (`i64`) cannot fit into an `usize`.
    ///
    /// # Errors
    ///
    /// Will return an error if either the Buffer's length can't be retrieved, or if the mmap call
    /// fails.
    pub fn memory_map_wo(&mut self) -> Result<MappedDmaBufWo<'_>, MapError> {
        let (len, mapping_ptr) = self.memory_map()?;
        MappedDmaBufWo::new(self, len, mapping_ptr)
    }
}

/// A `DmaBuf` mapped in memory.
///
/// This uses an arbitrary T, even though the only 2 types possible are &DmaBuf and &mut DmaBuf, but Rust doesn't provide a generic over mutability.
///
/// The mutability makes a difference with &DmaBuf: that one can be mapped multiple times in independent places.
struct MappedDmaBuf<T> {
    buf: T,
    len: usize,
    mmap: *mut u8,
}

/// A read-only Dmabuf mapped in memory. The underlying data gets cache-synced on creation and destruction.
#[derive(Debug)]
pub struct MappedDmaBufRo<'a>(MappedDmaBuf<&'a DmaBuf>);

/// A read-write Dmabuf mapped in memory. The underlying data gets cache-synced on creation and destruction.
#[derive(Debug)]
pub struct MappedDmaBufRw<'a>(MappedDmaBuf<&'a mut DmaBuf>);

/// A write-only Dmabuf mapped in memory. The underlying data gets cache-synced on creation and destruction.
#[derive(Debug)]
pub struct MappedDmaBufWo<'a>(MappedDmaBuf<&'a mut DmaBuf>);


impl<T: AsFd> MappedDmaBuf<T> {
    fn as_slice(&self) -> &[u8] {
        // SAFETY: We know that the pointer is valid, and the buffer length is at least equal to
        // self.len bytes. The backing buffer won't be mutated by the kernel, our structure is the
        // sole owner of the pointer, and it won't be mutated in our code either, so we're safe.
        unsafe { slice::from_raw_parts(self.mmap, self.len) }
    }
}

impl MappedDmaBuf<&'_ mut DmaBuf> {
    fn as_slice_mut(&mut self) -> &mut [u8] {
        // SAFETY: We know that the pointer is valid, and the buffer length is at least equal to
        // self.len bytes. The backing buffer won't be mutated by the kernel, our structure is the
        // sole owner of the pointer, and it won't be mutated in our code either, so we're safe.
        unsafe { slice::from_raw_parts_mut(self.mmap, self.len) }
    }
}

impl<'a> MappedDmaBufRo<'a> {
    fn new(buf: &'a DmaBuf, len: usize, mapping_ptr: *mut u8) -> Result<Self, MapError> {
        debug!("Preparing the buffer for read access");

        dma_buf_begin_cpu_read_access(buf.as_fd())?;

        Ok(Self(MappedDmaBuf {
            buf,
            len,
            mmap: mapping_ptr,
        }))
    }
    
    /// Access the underlying data directly
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }
}

// maybe TODO: implement a manual .release(self) -> Result<(), Self> which passes the failure to the caller.
impl Drop for MappedDmaBufRo<'_> {
    fn drop(&mut self) {
        if let Err(e) = dma_buf_end_cpu_read_access(self.0.buf.as_fd()) {
            warn!("End read access failed: {}", e);
        }

        debug!("Buffer access done");
    }
}

impl<'a> MappedDmaBufRw<'a> {
    fn new(buf: &'a mut DmaBuf, len: usize, mapping_ptr: *mut u8) -> Result<Self, MapError> {
        debug!("Preparing the buffer for read/write access");

        dma_buf_begin_cpu_readwrite_access(buf.as_fd())?;

        Ok(Self(MappedDmaBuf {
            buf,
            len,
            mmap: mapping_ptr,
        }))
    }
    
    /// Access the underlying data directly
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_slice()
    }
    
    /// Access the underlying data mutably
    pub fn as_slice_mut(&mut self) -> &mut [u8] {
        self.0.as_slice_mut()
    }
}

impl Drop for MappedDmaBufRw<'_> {
    fn drop(&mut self) {
        if let Err(e) = dma_buf_end_cpu_readwrite_access(self.0.buf.as_fd()) {
            warn!("End read|write access failed: {}", e);
        }

        debug!("Buffer access done");
    }
}


impl<'a> MappedDmaBufWo<'a> {
    fn new(buf: &'a mut DmaBuf, len: usize, mapping_ptr: *mut u8) -> Result<Self, MapError> {
        debug!("Preparing the buffer for write access");

        dma_buf_begin_cpu_write_access(buf.as_fd())?;

        Ok(Self(MappedDmaBuf {
            buf,
            len,
            mmap: mapping_ptr,
        }))
    }
    
    /// Access the underlying data mutably.
    ///
    /// Data in this struct is cache-coherently guarded only for write access, so reading may give unexpected results.
    pub fn as_slice_mut(&mut self) -> &mut [u8] {
        self.0.as_slice_mut()
    }
}

// maybe TODO: implement a manual .release(self) -> Result<(), Self> which passes the failure to the caller.
impl Drop for MappedDmaBufWo<'_> {
    fn drop(&mut self) {
        if let Err(e) = dma_buf_end_cpu_write_access(self.0.buf.as_fd()) {
            warn!("End write access failed: {}", e);
        }

        debug!("Buffer access done");
    }
}


impl From<OwnedFd> for DmaBuf {
    fn from(owned: OwnedFd) -> Self {
        Self(owned)
    }
}

impl AsFd for DmaBuf {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.0.as_fd()
    }
}

impl AsRawFd for DmaBuf {
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}

impl FromRawFd for DmaBuf {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        debug!("Importing DMABuf from File Descriptor {}", fd);

        // SAFETY: We're just forwarding the FromRawFd implementation to our inner OwnerFd type.
        // We're having exactly the same safety guarantees.
        Self(unsafe { OwnedFd::from_raw_fd(fd) })
    }
}

impl<T: fmt::Debug> fmt::Debug for MappedDmaBuf<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MappedDmaBuf")
            .field("DmaBuf", &self.buf)
            .field("len", &self.len)
            .field("address", &self.mmap)
            .finish()
    }
}

impl<T> Drop for MappedDmaBuf<T> {
    fn drop(&mut self) {
        debug!("Unmapping");
        // SAFETY: It's not clear what rustix expects from a safety perspective, but our pointer is
        // valid, and is a void pointer at least.
        if unsafe { munmap(self.mmap.cast::<c_void>(), self.len) }.is_err() {
            warn!("unmap failed!");
        }
    }
}


/// This module exists only to make tests compile.
#[doc(hidden)]
pub mod test {
    use super::*;

    /// Panics. Use for testing compilation.
    pub fn get_dma_buf() -> &'static DmaBuf {
        unimplemented!();
    }
    /// Panics. Use for testing compilation.
    pub fn get_dma_buf_mut() -> &'static mut DmaBuf {
        unimplemented!();
    }
}