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
mod builder;
mod error;
mod event;
mod raw;
pub use crate::builder::{FeatureFlags, UffdBuilder};
pub use crate::error::{Error, Result};
pub use crate::event::{Event, ReadWrite};
use bitflags::bitflags;
use libc::{self, c_void};
use nix::errno::Errno;
use nix::unistd::read;
use std::mem;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[derive(Debug)]
pub struct Uffd {
fd: RawFd,
}
impl Drop for Uffd {
fn drop(&mut self) {
unsafe { libc::close(self.fd) };
}
}
impl AsRawFd for Uffd {
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
impl IntoRawFd for Uffd {
fn into_raw_fd(self) -> RawFd {
self.fd
}
}
impl FromRawFd for Uffd {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Uffd { fd }
}
}
impl Uffd {
pub fn register(&self, start: *mut c_void, len: usize) -> Result<IoctlFlags> {
let mut register = raw::uffdio_register {
range: raw::uffdio_range {
start: start as u64,
len: len as u64,
},
mode: raw::UFFDIO_REGISTER_MODE_MISSING,
ioctls: 0,
};
unsafe {
raw::register(self.as_raw_fd(), &mut register as *mut raw::uffdio_register)?;
}
IoctlFlags::from_bits(register.ioctls).ok_or(Error::UnrecognizedIoctls(register.ioctls))
}
pub fn unregister(&self, start: *mut c_void, len: usize) -> Result<()> {
let mut range = raw::uffdio_range {
start: start as u64,
len: len as u64,
};
unsafe {
raw::unregister(self.as_raw_fd(), &mut range as *mut raw::uffdio_range)?;
}
Ok(())
}
pub unsafe fn copy(
&self,
src: *const c_void,
dst: *mut c_void,
len: usize,
wake: bool,
) -> Result<usize> {
let mut copy = raw::uffdio_copy {
src: src as u64,
dst: dst as u64,
len: len as u64,
mode: if wake {
0
} else {
raw::UFFDIO_COPY_MODE_DONTWAKE
},
copy: 0,
};
let res = raw::copy(self.as_raw_fd(), &mut copy as *mut raw::uffdio_copy);
if let Err(e) = res {
if let Some(errno) = e.as_errno() {
Err(Error::CopyFailed(errno))
} else {
Err(e.into())
}
} else {
if copy.copy < 0 {
Err(Error::CopyFailed(Errno::from_i32(-copy.copy as i32)))
} else {
Ok(copy.copy as usize)
}
}
}
pub unsafe fn zeropage(&self, start: *mut c_void, len: usize, wake: bool) -> Result<usize> {
let mut zeropage = raw::uffdio_zeropage {
range: raw::uffdio_range {
start: start as u64,
len: len as u64,
},
mode: if wake {
0
} else {
raw::UFFDIO_ZEROPAGE_MODE_DONTWAKE
},
zeropage: 0,
};
let res = raw::zeropage(self.as_raw_fd(), &mut zeropage as &mut raw::uffdio_zeropage);
if let Err(e) = res {
if let Some(errno) = e.as_errno() {
Err(Error::ZeropageFailed(errno))
} else {
Err(e.into())
}
} else {
if zeropage.zeropage < 0 {
Err(Error::ZeropageFailed(Errno::from_i32(
-zeropage.zeropage as i32,
)))
} else {
Ok(zeropage.zeropage as usize)
}
}
}
pub fn wake(&self, start: *mut c_void, len: usize) -> Result<()> {
let mut range = raw::uffdio_range {
start: start as u64,
len: len as u64,
};
unsafe {
raw::wake(self.as_raw_fd(), &mut range as *mut raw::uffdio_range)?;
}
Ok(())
}
pub fn read_event(&self) -> Result<Option<Event>> {
const MSG_SIZE: usize = mem::size_of::<raw::uffd_msg>();
let mut buf = [0x00u8; MSG_SIZE];
let res = read(self.as_raw_fd(), &mut buf);
match res {
Err(e) if e.as_errno() == Some(Errno::EAGAIN) => return Ok(None),
Err(e) => Err(e)?,
Ok(nread) => {
if nread == libc::EOF as usize {
return Err(Error::ReadEof);
}
if nread != MSG_SIZE {
return Err(Error::IncompleteMsg {
read: nread,
expected: MSG_SIZE,
});
}
let msg = unsafe { *(buf.as_mut_ptr() as *mut raw::uffd_msg) };
let event = Event::from_uffd_msg(&msg)?;
return Ok(Some(event));
}
}
}
}
bitflags! {
pub struct IoctlFlags: u64 {
const REGISTER = 1 << raw::_UFFDIO_REGISTER;
const UNREGISTER = 1 << raw::_UFFDIO_UNREGISTER;
const WAKE = 1 << raw::_UFFDIO_WAKE;
const COPY = 1 << raw::_UFFDIO_COPY;
const ZEROPAGE = 1 << raw::_UFFDIO_ZEROPAGE;
const API = 1 << raw::_UFFDIO_API;
}
}