|
16 | 16 |
|
17 | 17 | use crate::{ |
18 | 18 | bindings, |
19 | | - error::{to_result, Result}, |
| 19 | + error::{code::EINVAL, to_result, Result}, |
20 | 20 | mm::MmWithUser, |
21 | 21 | page::Page, |
22 | 22 | types::Opaque, |
@@ -198,6 +198,190 @@ impl VmaMixedMap { |
198 | 198 | } |
199 | 199 | } |
200 | 200 |
|
| 201 | +/// A configuration object for setting up a VMA in an `f_ops->mmap()` hook. |
| 202 | +/// |
| 203 | +/// The `f_ops->mmap()` hook is called when a new VMA is being created, and the hook is able to |
| 204 | +/// configure the VMA in various ways to fit the driver that owns it. Using `VmaNew` indicates that |
| 205 | +/// you are allowed to perform operations on the VMA that can only be performed before the VMA is |
| 206 | +/// fully initialized. |
| 207 | +/// |
| 208 | +/// # Invariants |
| 209 | +/// |
| 210 | +/// For the duration of 'a, the referenced vma must be undergoing initialization in an |
| 211 | +/// `f_ops->mmap()` hook. |
| 212 | +pub struct VmaNew { |
| 213 | + vma: VmaRef, |
| 214 | +} |
| 215 | + |
| 216 | +// Make all `VmaRef` methods available on `VmaNew`. |
| 217 | +impl Deref for VmaNew { |
| 218 | + type Target = VmaRef; |
| 219 | + |
| 220 | + #[inline] |
| 221 | + fn deref(&self) -> &VmaRef { |
| 222 | + &self.vma |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +impl VmaNew { |
| 227 | + /// Access a virtual memory area given a raw pointer. |
| 228 | + /// |
| 229 | + /// # Safety |
| 230 | + /// |
| 231 | + /// Callers must ensure that `vma` is undergoing initial vma setup for the duration of 'a. |
| 232 | + #[inline] |
| 233 | + pub unsafe fn from_raw<'a>(vma: *mut bindings::vm_area_struct) -> &'a Self { |
| 234 | + // SAFETY: The caller ensures that the invariants are satisfied for the duration of 'a. |
| 235 | + unsafe { &*vma.cast() } |
| 236 | + } |
| 237 | + |
| 238 | + /// Internal method for updating the vma flags. |
| 239 | + /// |
| 240 | + /// # Safety |
| 241 | + /// |
| 242 | + /// This must not be used to set the flags to an invalid value. |
| 243 | + #[inline] |
| 244 | + unsafe fn update_flags(&self, set: vm_flags_t, unset: vm_flags_t) { |
| 245 | + let mut flags = self.flags(); |
| 246 | + flags |= set; |
| 247 | + flags &= !unset; |
| 248 | + |
| 249 | + // SAFETY: This is not a data race: the vma is undergoing initial setup, so it's not yet |
| 250 | + // shared. Additionally, `VmaNew` is `!Sync`, so it cannot be used to write in parallel. |
| 251 | + // The caller promises that this does not set the flags to an invalid value. |
| 252 | + unsafe { (*self.as_ptr()).__bindgen_anon_2.__vm_flags = flags }; |
| 253 | + } |
| 254 | + |
| 255 | + /// Set the `VM_MIXEDMAP` flag on this vma. |
| 256 | + /// |
| 257 | + /// This enables the vma to contain both `struct page` and pure PFN pages. Returns a reference |
| 258 | + /// that can be used to call `vm_insert_page` on the vma. |
| 259 | + #[inline] |
| 260 | + pub fn set_mixedmap(&self) -> &VmaMixedMap { |
| 261 | + // SAFETY: We don't yet provide a way to set VM_PFNMAP, so this cannot put the flags in an |
| 262 | + // invalid state. |
| 263 | + unsafe { self.update_flags(flags::MIXEDMAP, 0) }; |
| 264 | + |
| 265 | + // SAFETY: We just set `VM_MIXEDMAP` on the vma. |
| 266 | + unsafe { VmaMixedMap::from_raw(self.vma.as_ptr()) } |
| 267 | + } |
| 268 | + |
| 269 | + /// Set the `VM_IO` flag on this vma. |
| 270 | + /// |
| 271 | + /// This is used for memory mapped IO and similar. The flag tells other parts of the kernel to |
| 272 | + /// avoid looking at the pages. For memory mapped IO this is useful as accesses to the pages |
| 273 | + /// could have side effects. |
| 274 | + #[inline] |
| 275 | + pub fn set_io(&self) { |
| 276 | + // SAFETY: Setting the VM_IO flag is always okay. |
| 277 | + unsafe { self.update_flags(flags::IO, 0) }; |
| 278 | + } |
| 279 | + |
| 280 | + /// Set the `VM_DONTEXPAND` flag on this vma. |
| 281 | + /// |
| 282 | + /// This prevents the vma from being expanded with `mremap()`. |
| 283 | + #[inline] |
| 284 | + pub fn set_dontexpand(&self) { |
| 285 | + // SAFETY: Setting the VM_DONTEXPAND flag is always okay. |
| 286 | + unsafe { self.update_flags(flags::DONTEXPAND, 0) }; |
| 287 | + } |
| 288 | + |
| 289 | + /// Set the `VM_DONTCOPY` flag on this vma. |
| 290 | + /// |
| 291 | + /// This prevents the vma from being copied on fork. This option is only permanent if `VM_IO` |
| 292 | + /// is set. |
| 293 | + #[inline] |
| 294 | + pub fn set_dontcopy(&self) { |
| 295 | + // SAFETY: Setting the VM_DONTCOPY flag is always okay. |
| 296 | + unsafe { self.update_flags(flags::DONTCOPY, 0) }; |
| 297 | + } |
| 298 | + |
| 299 | + /// Set the `VM_DONTDUMP` flag on this vma. |
| 300 | + /// |
| 301 | + /// This prevents the vma from being included in core dumps. This option is only permanent if |
| 302 | + /// `VM_IO` is set. |
| 303 | + #[inline] |
| 304 | + pub fn set_dontdump(&self) { |
| 305 | + // SAFETY: Setting the VM_DONTDUMP flag is always okay. |
| 306 | + unsafe { self.update_flags(flags::DONTDUMP, 0) }; |
| 307 | + } |
| 308 | + |
| 309 | + /// Returns whether `VM_READ` is set. |
| 310 | + /// |
| 311 | + /// This flag indicates whether userspace is mapping this vma as readable. |
| 312 | + #[inline] |
| 313 | + pub fn readable(&self) -> bool { |
| 314 | + (self.flags() & flags::READ) != 0 |
| 315 | + } |
| 316 | + |
| 317 | + /// Try to clear the `VM_MAYREAD` flag, failing if `VM_READ` is set. |
| 318 | + /// |
| 319 | + /// This flag indicates whether userspace is allowed to make this vma readable with |
| 320 | + /// `mprotect()`. |
| 321 | + /// |
| 322 | + /// Note that this operation is irreversible. Once `VM_MAYREAD` has been cleared, it can never |
| 323 | + /// be set again. |
| 324 | + #[inline] |
| 325 | + pub fn try_clear_mayread(&self) -> Result { |
| 326 | + if self.readable() { |
| 327 | + return Err(EINVAL); |
| 328 | + } |
| 329 | + // SAFETY: Clearing `VM_MAYREAD` is okay when `VM_READ` is not set. |
| 330 | + unsafe { self.update_flags(0, flags::MAYREAD) }; |
| 331 | + Ok(()) |
| 332 | + } |
| 333 | + |
| 334 | + /// Returns whether `VM_WRITE` is set. |
| 335 | + /// |
| 336 | + /// This flag indicates whether userspace is mapping this vma as writable. |
| 337 | + #[inline] |
| 338 | + pub fn writable(&self) -> bool { |
| 339 | + (self.flags() & flags::WRITE) != 0 |
| 340 | + } |
| 341 | + |
| 342 | + /// Try to clear the `VM_MAYWRITE` flag, failing if `VM_WRITE` is set. |
| 343 | + /// |
| 344 | + /// This flag indicates whether userspace is allowed to make this vma writable with |
| 345 | + /// `mprotect()`. |
| 346 | + /// |
| 347 | + /// Note that this operation is irreversible. Once `VM_MAYWRITE` has been cleared, it can never |
| 348 | + /// be set again. |
| 349 | + #[inline] |
| 350 | + pub fn try_clear_maywrite(&self) -> Result { |
| 351 | + if self.writable() { |
| 352 | + return Err(EINVAL); |
| 353 | + } |
| 354 | + // SAFETY: Clearing `VM_MAYWRITE` is okay when `VM_WRITE` is not set. |
| 355 | + unsafe { self.update_flags(0, flags::MAYWRITE) }; |
| 356 | + Ok(()) |
| 357 | + } |
| 358 | + |
| 359 | + /// Returns whether `VM_EXEC` is set. |
| 360 | + /// |
| 361 | + /// This flag indicates whether userspace is mapping this vma as executable. |
| 362 | + #[inline] |
| 363 | + pub fn executable(&self) -> bool { |
| 364 | + (self.flags() & flags::EXEC) != 0 |
| 365 | + } |
| 366 | + |
| 367 | + /// Try to clear the `VM_MAYEXEC` flag, failing if `VM_EXEC` is set. |
| 368 | + /// |
| 369 | + /// This flag indicates whether userspace is allowed to make this vma executable with |
| 370 | + /// `mprotect()`. |
| 371 | + /// |
| 372 | + /// Note that this operation is irreversible. Once `VM_MAYEXEC` has been cleared, it can never |
| 373 | + /// be set again. |
| 374 | + #[inline] |
| 375 | + pub fn try_clear_mayexec(&self) -> Result { |
| 376 | + if self.executable() { |
| 377 | + return Err(EINVAL); |
| 378 | + } |
| 379 | + // SAFETY: Clearing `VM_MAYEXEC` is okay when `VM_EXEC` is not set. |
| 380 | + unsafe { self.update_flags(0, flags::MAYEXEC) }; |
| 381 | + Ok(()) |
| 382 | + } |
| 383 | +} |
| 384 | + |
201 | 385 | /// The integer type used for vma flags. |
202 | 386 | #[doc(inline)] |
203 | 387 | pub use bindings::vm_flags_t; |
|
0 commit comments