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
// Copyright 2020-2021, Cerno
// Licensed under the MIT License
// See the LICENSE file or <http://opensource.org/licenses/MIT>
//
//! # DMA-Buf Helper Library
//!
//! The DMA-Buf mechanism in Linux is aimed at providing a way for the user-space to efficiently
//! share memory buffers between multiple devices, without any copy.
//!
//! This library provides a safe abstraction over this interface for Rust.

#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(clippy::nursery)]
#![deny(clippy::cargo)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_sign_loss)]

use std::{convert::TryInto, num::TryFromIntError, os::unix::io::RawFd, slice};

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,
};
use log::debug;
use mmap::{MapOption, MemoryMap};
use nix::sys::stat::fstat;

mod ioctl;

/// Error Type for dma-buf
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// An Error occured in the closure
    #[error("Closure Error")]
    Closure,

    /// An Error occured when casting an integer
    #[error("Integer Conversion Error")]
    IntegerCast(#[from] TryFromIntError),

    /// An Error happened when allocating a buffer
    #[error("System Error")]
    System(#[from] nix::Error),

    /// An Error occured when mapping the buffer
    #[error("mmap Error")]
    MMap(#[from] mmap::MapError),
}

/// A DMA-Buf buffer
#[derive(Debug)]
pub struct DmaBuf {
    fd: RawFd,
}

impl DmaBuf {
    /// Maps a `DmaBuf` for the CPU to access it
    ///
    /// # 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(self) -> Result<MappedDmaBuf, Error> {
        debug!("Mapping DMA-Buf buffer with File Descriptor {}", self.fd);

        let stat = fstat(self.fd)?;
        let len = stat.st_size.try_into()?;
        debug!("Valid buffer, size {}", len);

        let mmap = MemoryMap::new(
            len,
            &[
                MapOption::MapFd(self.fd),
                MapOption::MapOffset(0),
                MapOption::MapNonStandardFlags(libc::MAP_SHARED),
                MapOption::MapReadable,
                MapOption::MapWritable,
            ],
        )?;

        debug!("Memory Mapping Done");

        Ok(MappedDmaBuf {
            buf: self,
            len,
            mmap,
        })
    }
}

/// A `DmaBuf` mapped in memory
pub struct MappedDmaBuf {
    buf: DmaBuf,
    len: usize,
    mmap: MemoryMap,
}

impl MappedDmaBuf {
    /// Calls a closure to read the buffer content
    ///
    /// DMA-Buf requires the user-space to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. The closure will be run
    /// with those primitives called for a read access from the CPU.
    ///
    /// The result of the closure will be returned on success. On failure, the closure must return
    /// `Error::Closure`
    ///
    /// # Errors
    ///
    /// Will return [Error] if the underlying ioctl or the closure fails
    pub fn read<F, R>(&self, f: F) -> Result<R, Error>
    where
        F: Fn(&[u8]) -> Result<R, Error>,
    {
        let slice = unsafe { slice::from_raw_parts(self.mmap.data(), self.len) };

        debug!("Preparing the buffer for read access");

        dma_buf_begin_cpu_read_access(self.buf.fd)?;

        debug!("Accessing the buffer");

        let ret = f(slice);

        if ret.is_ok() {
            debug!("Closure done without error");
        } else {
            debug!("Closure encountered an error")
        }

        dma_buf_end_cpu_read_access(self.buf.fd)?;

        debug!("Buffer access done");

        ret
    }

    /// Calls a closure to read from and write to the buffer content
    ///
    /// DMA-Buf requires the user-space to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. The closure will be run
    /// with those primitives called for a read and write access from the CPU.
    ///
    /// The result of the closure will be returned on success. On failure, the closure must return
    /// `Error::Closure`
    ///
    /// # Errors
    ///
    /// Will return [Error] if the underlying ioctl or the closure fails
    pub fn readwrite<A, F, R>(&self, f: F, arg: Option<A>) -> Result<R, Error>
    where
        F: Fn(&mut [u8], Option<A>) -> Result<R, Error>,
    {
        let slice = unsafe { slice::from_raw_parts_mut(self.mmap.data(), self.len) };

        debug!("Preparing the buffer for read/write access");

        dma_buf_begin_cpu_readwrite_access(self.buf.fd)?;

        debug!("Accessing the buffer");

        let ret = f(slice, arg);

        if ret.is_ok() {
            debug!("Closure done without error");
        } else {
            debug!("Closure encountered an error")
        }

        dma_buf_end_cpu_readwrite_access(self.buf.fd)?;

        debug!("Buffer access done");

        ret
    }

    /// Calls a closure to read from and write to the buffer content
    ///
    /// DMA-Buf requires the user-space to call the `DMA_BUF_IOCTL_SYNC` ioctl before and after any
    /// CPU access to a buffer in order to maintain the cache coherency. The closure will be run
    /// with those primitives called for a read and write access from the CPU.
    ///
    /// The closure must return () on success. On failure, the closure must return `Error::Closure`.
    ///
    /// # Errors
    ///
    /// Will return [Error] if the underlying ioctl or the closure fails
    pub fn write<A, F>(&self, f: F, arg: Option<A>) -> Result<(), Error>
    where
        F: Fn(&mut [u8], Option<A>) -> Result<(), Error>,
    {
        let slice = unsafe { slice::from_raw_parts_mut(self.mmap.data(), self.len) };

        debug!("Preparing the buffer for write access");

        dma_buf_begin_cpu_write_access(self.buf.fd)?;

        debug!("Accessing the buffer");

        let ret = f(slice, arg);

        if ret.is_ok() {
            debug!("Closure done without error");
        } else {
            debug!("Closure encountered an error")
        }

        dma_buf_end_cpu_write_access(self.buf.fd)?;

        debug!("Buffer access done");

        ret
    }
}

impl std::os::unix::io::AsRawFd for DmaBuf {
    fn as_raw_fd(&self) -> RawFd {
        self.fd
    }
}

impl std::os::unix::io::AsRawFd for MappedDmaBuf {
    fn as_raw_fd(&self) -> RawFd {
        self.buf.fd
    }
}

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

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

impl Drop for DmaBuf {
    fn drop(&mut self) {
        debug!("Closing buffer {}", self.fd);
        nix::unistd::close(self.fd).unwrap();
    }
}