├── .github ├── ISSUE_TEMPLATE │ └── blank-issue.md ├── dependabot.yml └── workflows │ └── rust.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── SECURITY.md ├── examples └── manpage.rs ├── linux-version ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── build.rs ├── src │ └── lib.rs └── wrapper.h ├── src ├── builder.rs ├── error.rs ├── event.rs ├── lib.rs └── raw.rs ├── tests └── manpage_example.rs └── userfaultfd-sys ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── build.rs ├── src ├── consts.c ├── lib.rs ├── linux4_11.rs ├── linux4_14.rs └── linux5_7.rs └── wrapper.h /.github/ISSUE_TEMPLATE/blank-issue.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Blank Issue 3 | about: Create a blank issue. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "cargo" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | - package-ecosystem: "cargo" # See documentation for possible values 13 | directory: "/userfaultfd-sys" # Location of package manifests 14 | schedule: 15 | interval: "weekly" 16 | 17 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | 12 | # ubuntu-latest runs a recent kernel with /dev/userfaultfd support whereas 13 | # ubuntu-20.04 has a 5.15 kernel. We run the job in both, so we can test 14 | # both paths for creating the file descriptor, i.e. /dev/userfaultfd ioctl 15 | # and userfaultfd syscall. 16 | runs-on: ${{ matrix.runner }} 17 | strategy: 18 | matrix: 19 | runner: [ ubuntu-latest, ubuntu-20.04 ] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | 24 | # Keep this step, so that we can check that the Linux kernel is the one we 25 | # expect, depending on the runner kernel. 26 | - name: Check Linux version 27 | run: uname -r 28 | 29 | # /dev/userfaultfd is only present on ubuntu-latest. 30 | - name: Setup access to /dev/userfaultfd 31 | if: ${{ matrix.runner == 'ubuntu-latest' }} 32 | run: sudo setfacl -m u:${USER}:rw /dev/userfaultfd 33 | 34 | - name: Build 35 | run: cargo build --verbose 36 | 37 | - name: Run tests (Linux 4.11 support) 38 | run: cargo test --verbose 39 | - name: Run tests (Linux 4.14 support) 40 | run: cargo test --verbose --features linux4_14 41 | 42 | - name: Run tests (Linux 5.7 support) 43 | if: ${{ matrix.runner == 'ubuntu-latest' }} 44 | run: cargo test --verbose --features linux5_7 45 | 46 | # On ubuntu-20.04 runner we need to make sure we have the proper kernel 47 | # headers for building the correct bindings 48 | - name: Run tests (Linux 5.7 support) 49 | if: ${{ matrix.runner == 'ubuntu-20.04' }} 50 | run: 51 | sudo apt update && 52 | sudo apt install -y linux-headers-5.11.0-25-generic && 53 | export LINUX_HEADERS=/usr/src/linux-headers-5.11.0-25-generic && 54 | cargo test --verbose --features linux5_7 55 | 56 | audit: 57 | 58 | runs-on: ubuntu-latest 59 | 60 | steps: 61 | - uses: actions/checkout@v2 62 | - name: Install Cargo Audit 63 | run: cargo install cargo-audit 64 | - name: Audit 65 | run: cargo audit 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | **/*.rs.bk 3 | .google.json 4 | TAGS 5 | package 6 | *.pyc 7 | core.* 8 | Cargo.lock -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 0.8.0 (2024-01-12) 2 | 3 | - `IoctlFlags` accepts unknown flags (e.g. due to future kernel changes). 4 | `Error::UnrecognizedIoctls` is removed. 5 | 6 | ### 0.3.1 ~ 0.7.0 7 | 8 | - Added `Uffd::read_events` that can read multiple events from the userfaultfd file descriptor. 9 | - Updated `bitflags` dependency to `2.2.1`. 10 | - Use `/dev/userfaultfd` as the default API for creating userfaultfd file descriptors. 11 | 12 | Since Linux 5.11 a process can select if it wants to handle page faults triggered in kernel space 13 | or not. Under this mechanism, processes that wish to handle those, need to have `CAP_SYS_PTRACE` 14 | capability. `CAP_SYS_PTRACE` allows a process to do much more than create userfault fds, so with 15 | 6.1 Linux introduces `/dev/userfaultfd`, a special character device that allows creating 16 | userfault file descriptors using the `USERFAULTFD_IOC_NEW` `ioctl`. Access to this device is 17 | granted via file system permissions and does not require `CAP_SYS_PTRACE` to handle kernel 18 | triggered page faults. 19 | 20 | We now default to using `/dev/userfaultfd` for creating the descriptors and only if that file is 21 | not present, we fall back to using the syscall. 22 | 23 | ### 0.3.1 (2021-02-17) 24 | 25 | - Added support for the `UFFD_FEATURE_THREAD_ID` flag when compiled with the `linux4_14` Cargo 26 | feature. 27 | 28 | ### 0.3.0 (2021-02-03) 29 | 30 | - Update `bindgen` dependency of `userfaultfd-sys` to `0.57`. Thank you @jgowans 31 | 32 | ### 0.2.1 (2020-11-20) 33 | 34 | - Make `ReadWrite` public. Thank you @electroCutie 35 | 36 | ### 0.2.0 (2020-04-10) 37 | 38 | - Removed the compile-time Linux version check, and replaced it with a Cargo feature. 39 | 40 | The Linux version check was overly restrictive, even on systems that did have the right kernel 41 | version installed but had older headers in `/usr/include/linux`. Beyond that, this check made it 42 | more difficult to compile on a different host than what's targeted. 43 | 44 | There is now a `linux4_14` feature flag on `userfaultfd-sys`, which turns on and tests the extra 45 | constants available in that version. Since `userfaultfd` did not make use of any of those newer 46 | features, it doesn't have a feature flag yet. 47 | 48 | Applications should take care when initializing with `UffdBuilder` to specify the features and 49 | ioctls they require, so that an unsupported version will be detected at runtime. 50 | 51 | 52 | ### 0.1.0 (2020-04-07) 53 | 54 | - Initial public release of userfaultfd-rs. 55 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "userfaultfd" 3 | version = "0.8.1" 4 | authors = ["The Wasmtime Project Developers"] 5 | edition = "2018" 6 | license = "MIT OR Apache-2.0" 7 | description = "Rust bindings for the Linux userfaultfd functionality" 8 | repository = "https://github.com/bytecodealliance/userfaultfd-rs" 9 | readme = "README.md" 10 | 11 | [dependencies] 12 | bitflags = "2.4.0" 13 | cfg-if = "^1.0.0" 14 | libc = "0.2.65" 15 | nix = { version = "0.27", features = ["ioctl"] } 16 | thiserror = "1.0.4" 17 | userfaultfd-sys = { path = "userfaultfd-sys", version = "^0.5.0" } 18 | 19 | [dev-dependencies] 20 | nix = { version = "0.27", features = ["poll", "mman", "feature"] } 21 | 22 | [features] 23 | default = [] 24 | linux4_14 = ["userfaultfd-sys/linux4_14", "nix/process"] 25 | linux5_7 = ["userfaultfd-sys/linux5_7"] 26 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Build](https://github.com/fastly/userfaultfd-rs/workflows/Rust/badge.svg) 2 | 3 | # Userfaultfd-rs 4 | Rust bindings for Linux's userfaultfd functionality. 5 | 6 | ## License 7 | 8 | This software is distributed under the terms of both the MIT license and the Apache License (Version 2.0). 9 | 10 | See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT). 11 | 12 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Report a security issue 2 | 3 | Fastly welcomes security reports and is committed to providing prompt attention to security issues. Security issues should be reported privately via [Fastly’s security issue reporting process](https://www.fastly.com/security/report-security-issue). 4 | 5 | ## Security advisories 6 | 7 | Remediation of security vulnerabilities is prioritized. The project team endeavors to coordinate remediation with third-party stakeholders, and is committed to transparency in the disclosure process. The team announces security issues via release notes as well as the [RustSec advisory database](https://github.com/RustSec/advisory-db) (i.e. `cargo-audit`) on a best-effort basis. 8 | 9 | Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from [Fastly Security Advisories](https://www.fastly.com/security-advisories). 10 | -------------------------------------------------------------------------------- /examples/manpage.rs: -------------------------------------------------------------------------------- 1 | //! Port of the example from the `userfaultfd` manpage. 2 | use libc::{self, c_void}; 3 | use nix::poll::{poll, PollFd, PollFlags}; 4 | use nix::sys::mman::{mmap, MapFlags, ProtFlags}; 5 | use nix::unistd::{sysconf, SysconfVar}; 6 | use std::{convert::TryInto, env}; 7 | use userfaultfd::{Event, Uffd, UffdBuilder}; 8 | 9 | fn fault_handler_thread(uffd: Uffd) { 10 | let page_size = sysconf(SysconfVar::PAGE_SIZE).unwrap().unwrap() as usize; 11 | 12 | // Create a page that will be copied into the faulting region 13 | 14 | let page = unsafe { 15 | mmap( 16 | None, 17 | page_size.try_into().unwrap(), 18 | ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, 19 | MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS, 20 | None::, 21 | 0, 22 | ) 23 | .expect("mmap") 24 | }; 25 | 26 | // Loop, handling incoming events on the userfaultfd file descriptor 27 | 28 | let mut fault_cnt = 0; 29 | loop { 30 | // See what poll() tells us about the userfaultfd 31 | 32 | let mut fds = [PollFd::new(&uffd, PollFlags::POLLIN)]; 33 | let nready = poll(&mut fds, -1).expect("poll"); 34 | let pollfd = fds[0]; 35 | 36 | println!("\nfault_handler_thread():"); 37 | let revents = pollfd.revents().unwrap(); 38 | println!( 39 | " poll() returns: nready = {}; POLLIN = {}; POLLERR = {}", 40 | nready, 41 | revents.contains(PollFlags::POLLIN), 42 | revents.contains(PollFlags::POLLERR), 43 | ); 44 | 45 | // Read an event from the userfaultfd 46 | let event = uffd 47 | .read_event() 48 | .expect("read uffd_msg") 49 | .expect("uffd_msg ready"); 50 | 51 | // We expect only one kind of event; verify that assumption 52 | 53 | if let Event::Pagefault { addr, .. } = event { 54 | // Display info about the page-fault event 55 | 56 | println!(" UFFD_EVENT_PAGEFAULT event: {:?}", event); 57 | 58 | // Copy the page pointed to by 'page' into the faulting region. Vary the contents that are 59 | // copied in, so that it is more obvious that each fault is handled separately. 60 | 61 | for c in unsafe { std::slice::from_raw_parts_mut(page as *mut u8, page_size) } { 62 | *c = b'A' + fault_cnt % 20; 63 | } 64 | fault_cnt += 1; 65 | 66 | let dst = (addr as usize & !(page_size - 1)) as *mut c_void; 67 | let copy = unsafe { uffd.copy(page, dst, page_size, true).expect("uffd copy") }; 68 | 69 | println!(" (uffdio_copy.copy returned {})", copy); 70 | } else { 71 | panic!("Unexpected event on userfaultfd"); 72 | } 73 | } 74 | } 75 | 76 | fn main() { 77 | let num_pages = env::args() 78 | .nth(1) 79 | .expect("Usage: manpage ") 80 | .parse::() 81 | .unwrap(); 82 | 83 | let page_size = sysconf(SysconfVar::PAGE_SIZE).unwrap().unwrap() as usize; 84 | let len = num_pages * page_size; 85 | 86 | // Create and enable userfaultfd object 87 | 88 | let uffd = UffdBuilder::new() 89 | .close_on_exec(true) 90 | .non_blocking(true) 91 | .user_mode_only(true) 92 | .create() 93 | .expect("uffd creation"); 94 | 95 | // Create a private anonymous mapping. The memory will be demand-zero paged--that is, not yet 96 | // allocated. When we actually touch the memory, it will be allocated via the userfaultfd. 97 | 98 | let addr = unsafe { 99 | mmap( 100 | None, 101 | len.try_into().unwrap(), 102 | ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, 103 | MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS, 104 | None::, 105 | 0, 106 | ) 107 | .expect("mmap") 108 | }; 109 | 110 | println!("Address returned by mmap() = {:p}", addr); 111 | 112 | // Register the memory range of the mapping we just created for handling by the userfaultfd 113 | // object. In mode, we request to track missing pages (i.e., pages that have not yet been 114 | // faulted in). 115 | 116 | uffd.register(addr, len).expect("uffd.register()"); 117 | 118 | // Create a thread that will process the userfaultfd events 119 | let _s = std::thread::spawn(move || fault_handler_thread(uffd)); 120 | 121 | // Main thread now touches memory in the mapping, touching locations 1024 bytes apart. This will 122 | // trigger userfaultfd events for all pages in the region. 123 | 124 | // Ensure that faulting address is not on a page boundary, in order to test that we correctly 125 | // handle that case in fault_handling_thread() 126 | let mut l = 0xf; 127 | 128 | while l < len { 129 | let ptr = (addr as usize + l) as *mut u8; 130 | let c = unsafe { *ptr }; 131 | println!("Read address {:p} in main(): {:?}", ptr, c as char); 132 | l += 1024; 133 | std::thread::sleep(std::time::Duration::from_micros(100000)); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /linux-version/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "linux-version" 3 | version = "0.1.2-dev" 4 | authors = ["Adam C. Foltzer "] 5 | edition = "2018" 6 | license = "MIT OR Apache-2.0" 7 | description = "Package for detecting the version of the linux kernel a program is being run on." 8 | repository = "https://github.com/fastly/userfaultfd-rs" 9 | 10 | build = "build.rs" 11 | 12 | [dependencies] 13 | nix = "0.23" 14 | semver = "0.9" 15 | 16 | [build-dependencies] 17 | bindgen = { version = "0.57", default-features = false } -------------------------------------------------------------------------------- /linux-version/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /linux-version/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /linux-version/build.rs: -------------------------------------------------------------------------------- 1 | use bindgen; 2 | use std::env; 3 | use std::path::PathBuf; 4 | 5 | fn main() { 6 | let bindings = bindgen::Builder::default() 7 | .header("wrapper.h") 8 | .whitelist_var("LINUX_VERSION_CODE") 9 | .generate() 10 | .expect("binding generation failed"); 11 | println!("rerun-if-changed=/usr/include/linux/version.h"); 12 | 13 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 14 | bindings 15 | .write_to_file(out_path.join("bindings.rs")) 16 | .expect("binding file couldn't be written"); 17 | } 18 | -------------------------------------------------------------------------------- /linux-version/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub use semver::{SemVerError, Version}; 2 | 3 | mod raw { 4 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 5 | } 6 | 7 | /// Get the version specified in ``. 8 | pub fn linux_headers_version() -> Version { 9 | let version = raw::LINUX_VERSION_CODE as u64; 10 | let major = version >> 16; 11 | let minor = version >> 8 & 0xFF; 12 | let patch = version & 0xFF; 13 | Version::new(major, minor, patch) 14 | } 15 | 16 | /// Get the version specified by `uname -r`. 17 | /// 18 | /// This treats everything after the `major.minor.patch` triple as build metadata. 19 | pub fn linux_kernel_version() -> Result { 20 | let uname = nix::sys::utsname::uname(); 21 | let pre_ver = Version::parse(uname.release())?; 22 | Ok(Version { 23 | pre: vec![], 24 | build: pre_ver.pre.clone(), 25 | ..pre_ver 26 | }) 27 | } 28 | -------------------------------------------------------------------------------- /linux-version/wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | -------------------------------------------------------------------------------- /src/builder.rs: -------------------------------------------------------------------------------- 1 | use crate::error::{Error, Result}; 2 | use crate::raw; 3 | use crate::{IoctlFlags, Uffd}; 4 | use bitflags::bitflags; 5 | use nix::errno::Errno; 6 | use std::fs::{File, OpenOptions}; 7 | use std::io::ErrorKind; 8 | use std::os::fd::AsRawFd; 9 | 10 | const UFFD_DEVICE_PATH: &str = "/dev/userfaultfd"; 11 | 12 | cfg_if::cfg_if! { 13 | if #[cfg(any(feature = "linux5_7", feature = "linux4_14"))] { 14 | bitflags! { 15 | /// Used with `UffdBuilder` to determine which features are available in the current kernel. 16 | #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] 17 | pub struct FeatureFlags: u64 { 18 | const PAGEFAULT_FLAG_WP = raw::UFFD_FEATURE_PAGEFAULT_FLAG_WP; 19 | const EVENT_FORK = raw::UFFD_FEATURE_EVENT_FORK; 20 | const EVENT_REMAP = raw::UFFD_FEATURE_EVENT_REMAP; 21 | const EVENT_REMOVE = raw::UFFD_FEATURE_EVENT_REMOVE; 22 | const MISSING_HUGETLBFS = raw::UFFD_FEATURE_MISSING_HUGETLBFS; 23 | const MISSING_SHMEM = raw::UFFD_FEATURE_MISSING_SHMEM; 24 | const EVENT_UNMAP = raw::UFFD_FEATURE_EVENT_UNMAP; 25 | const SIGBUS = raw::UFFD_FEATURE_SIGBUS; 26 | const THREAD_ID = raw::UFFD_FEATURE_THREAD_ID; 27 | } 28 | } 29 | } else { 30 | bitflags! { 31 | /// Used with `UffdBuilder` to determine which features are available in the current kernel. 32 | #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] 33 | pub struct FeatureFlags: u64 { 34 | const PAGEFAULT_FLAG_WP = raw::UFFD_FEATURE_PAGEFAULT_FLAG_WP; 35 | const EVENT_FORK = raw::UFFD_FEATURE_EVENT_FORK; 36 | const EVENT_REMAP = raw::UFFD_FEATURE_EVENT_REMAP; 37 | const EVENT_REMOVE = raw::UFFD_FEATURE_EVENT_REMOVE; 38 | const MISSING_HUGETLBFS = raw::UFFD_FEATURE_MISSING_HUGETLBFS; 39 | const MISSING_SHMEM = raw::UFFD_FEATURE_MISSING_SHMEM; 40 | const EVENT_UNMAP = raw::UFFD_FEATURE_EVENT_UNMAP; 41 | } 42 | } 43 | } 44 | } 45 | /// A builder for initializing `Uffd` objects. 46 | /// 47 | /// ``` 48 | /// use userfaultfd::UffdBuilder; 49 | /// 50 | /// let uffd = UffdBuilder::new() 51 | /// .close_on_exec(true) 52 | /// .non_blocking(true) 53 | /// .user_mode_only(true) 54 | /// .create(); 55 | /// assert!(uffd.is_ok()); 56 | /// ``` 57 | pub struct UffdBuilder { 58 | close_on_exec: bool, 59 | non_blocking: bool, 60 | user_mode_only: bool, 61 | req_features: FeatureFlags, 62 | req_ioctls: IoctlFlags, 63 | } 64 | 65 | impl UffdBuilder { 66 | /// Create a new builder with no required features or ioctls, `close_on_exec` and 67 | /// `non_blocking` both set to `false`, and `user_mode_only` set to `true`. 68 | pub fn new() -> UffdBuilder { 69 | UffdBuilder { 70 | close_on_exec: false, 71 | non_blocking: false, 72 | user_mode_only: true, 73 | req_features: FeatureFlags::empty(), 74 | req_ioctls: IoctlFlags::empty(), 75 | } 76 | } 77 | 78 | /// Enable the close-on-exec flag for the new userfaultfd object (see the description of 79 | /// `O_CLOEXEC` in [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html)). 80 | pub fn close_on_exec(&mut self, close_on_exec: bool) -> &mut Self { 81 | self.close_on_exec = close_on_exec; 82 | self 83 | } 84 | 85 | /// Enable non-blocking operation for the userfaultfd object. 86 | /// 87 | /// If this is set to `false`, `Uffd::read_event()` will block until an event is available to 88 | /// read. Otherwise, it will immediately return `None` if no event is available. 89 | pub fn non_blocking(&mut self, non_blocking: bool) -> &mut Self { 90 | self.non_blocking = non_blocking; 91 | self 92 | } 93 | 94 | /// Enable user-mode only flag for the userfaultfd object. 95 | /// 96 | /// If set to `false`, the process must have the `CAP_SYS_PTRACE` capability starting with Linux 5.11 97 | /// or object creation will fail with EPERM. When set to `true`, userfaultfd can't be used 98 | /// to handle kernel-mode page faults such as when kernel tries copying data to userspace. 99 | /// 100 | /// When used with kernels older than 5.11, this has no effect; the process doesn't need 101 | /// `CAP_SYS_PTRACE` and can handle kernel-mode page faults. 102 | pub fn user_mode_only(&mut self, user_mode_only: bool) -> &mut Self { 103 | self.user_mode_only = user_mode_only; 104 | self 105 | } 106 | 107 | /// Add a requirement that a particular feature or set of features is available. 108 | /// 109 | /// If a required feature is unavailable, `UffdBuilder.create()` will return an error. 110 | pub fn require_features(&mut self, feature: FeatureFlags) -> &mut Self { 111 | self.req_features |= feature; 112 | self 113 | } 114 | 115 | /// Add a requirement that a particular ioctl or set of ioctls is available. 116 | /// 117 | /// If a required ioctl is unavailable, `UffdBuilder.create()` will return an error. 118 | pub fn require_ioctls(&mut self, ioctls: IoctlFlags) -> &mut Self { 119 | self.req_ioctls |= ioctls; 120 | self 121 | } 122 | 123 | fn uffd_from_dev(&self, file: &mut File, flags: i32) -> Result { 124 | match unsafe { raw::new_uffd(file.as_raw_fd(), flags) } { 125 | Err(err) => Err(err.into()), 126 | Ok(fd) => Ok(Uffd { fd }), 127 | } 128 | } 129 | 130 | fn uffd_from_syscall(&self, flags: i32) -> Result { 131 | let fd = match Errno::result(unsafe { raw::userfaultfd(flags) }) { 132 | Ok(fd) => fd, 133 | // setting the USER_MODE_ONLY flag on kernel pre-5.11 causes it to return EINVAL. 134 | // If the user asks for the flag, we first try with it set, and if kernel gives 135 | // EINVAL we try again without the flag set. 136 | Err(Errno::EINVAL) if self.user_mode_only => Errno::result(unsafe { 137 | raw::userfaultfd(flags & !raw::UFFD_USER_MODE_ONLY as i32) 138 | })?, 139 | Err(e) => return Err(e.into()), 140 | }; 141 | 142 | // Wrap the fd up so that a failure in this function body closes it with the drop. 143 | Ok(Uffd { fd }) 144 | } 145 | 146 | // Try to get a UFFD file descriptor using `/dev/userfaultfd`. If that fails 147 | // fall back to calling the system call. 148 | fn open_file_descriptor(&self, flags: i32) -> Result { 149 | // If `/dev/userfaultfd` exists we'll try to get the file descriptor from it. If the file 150 | // doesn't exist we will fall back to calling the system call. This means, that if the 151 | // device exists but the calling process does not have access rights to it, this will fail, 152 | // i.e. we will not fall back to calling the system call. 153 | match OpenOptions::new() 154 | .read(true) 155 | .write(true) 156 | .open(UFFD_DEVICE_PATH) 157 | { 158 | Ok(mut file) => self.uffd_from_dev(&mut file, flags), 159 | Err(err) if err.kind() == ErrorKind::NotFound => self.uffd_from_syscall(flags), 160 | Err(err) => Err(Error::OpenDevUserfaultfd(err)), 161 | } 162 | } 163 | 164 | /// Create a `Uffd` object with the current settings of this builder. 165 | pub fn create(&self) -> Result { 166 | // first do the syscall to get the file descriptor 167 | let mut flags = 0; 168 | if self.close_on_exec { 169 | flags |= libc::O_CLOEXEC; 170 | } 171 | if self.non_blocking { 172 | flags |= libc::O_NONBLOCK; 173 | } 174 | 175 | if self.user_mode_only { 176 | flags |= raw::UFFD_USER_MODE_ONLY as i32; 177 | } 178 | 179 | let uffd = self.open_file_descriptor(flags)?; 180 | 181 | // then do the UFFDIO_API ioctl to set up and ensure features and other ioctls are available 182 | let mut api = raw::uffdio_api { 183 | api: raw::UFFD_API, 184 | features: self.req_features.bits(), 185 | ioctls: 0, 186 | }; 187 | unsafe { 188 | raw::api(uffd.fd, &mut api as *mut raw::uffdio_api)?; 189 | } 190 | let supported = IoctlFlags::from_bits_retain(api.ioctls); 191 | if !supported.contains(self.req_ioctls) { 192 | Err(Error::UnsupportedIoctls(supported)) 193 | } else { 194 | Ok(uffd) 195 | } 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | 3 | use crate::IoctlFlags; 4 | use nix::errno::Errno; 5 | use thiserror::Error; 6 | 7 | pub type Result = std::result::Result; 8 | 9 | /// Errors for this crate. 10 | /// 11 | /// Several of these errors contain an underlying `Errno` value; see 12 | /// [`userfaultfd(2)`](http://man7.org/linux/man-pages/man2/userfaultfd.2.html) and 13 | /// [`ioctl_userfaultfd(2)`](http://man7.org/linux/man-pages/man2/ioctl_userfaultfd.2.html) for more 14 | /// details on how to interpret these errors. 15 | #[derive(Debug, Error)] 16 | pub enum Error { 17 | /// Copy ioctl failure with `errno` value. 18 | #[error("Copy failed")] 19 | CopyFailed(Errno), 20 | 21 | /// Copy ioctl failure with copied length. 22 | #[error("Copy partially succeeded")] 23 | PartiallyCopied(usize), 24 | 25 | /// Failure to read a full `uffd_msg` struct from the underlying file descriptor. 26 | #[error("Incomplete uffd_msg; read only {read}/{expected} bytes")] 27 | IncompleteMsg { read: usize, expected: usize }, 28 | 29 | /// Generic system error. 30 | #[error("System error")] 31 | SystemError(#[source] nix::Error), 32 | 33 | /// End-of-file was read from the underlying file descriptor. 34 | #[error("EOF when reading file descriptor")] 35 | ReadEof, 36 | 37 | /// An unrecognized event code was found in a `uffd_msg` struct. 38 | #[error("Unrecognized event in uffd_msg: {0}")] 39 | UnrecognizedEvent(u8), 40 | 41 | /// Requested ioctls were not available when initializing the API. 42 | #[error("Requested ioctls unsupported; supported: {0:?}")] 43 | UnsupportedIoctls(IoctlFlags), 44 | 45 | /// Zeropage ioctl failure with `errno` value. 46 | #[error("Zeropage failed: {0}")] 47 | ZeropageFailed(Errno), 48 | 49 | /// Could not open /dev/userfaultfd even though it exists 50 | #[error("Error accessing /dev/userfaultfd: {0}")] 51 | OpenDevUserfaultfd(io::Error), 52 | } 53 | 54 | impl From for Error { 55 | fn from(e: nix::Error) -> Error { 56 | Error::SystemError(e) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/event.rs: -------------------------------------------------------------------------------- 1 | use crate::error::{Error, Result}; 2 | use crate::raw; 3 | use crate::Uffd; 4 | use libc::c_void; 5 | #[cfg(feature = "linux4_14")] 6 | use nix::unistd::Pid; 7 | use std::os::unix::io::{FromRawFd, RawFd}; 8 | 9 | /// Whether a page fault event was for a read or write. 10 | #[derive(Clone, Copy, Debug, PartialEq)] 11 | pub enum ReadWrite { 12 | Read, 13 | Write, 14 | } 15 | 16 | /// The kind of fault for a page fault event. 17 | #[derive(Clone, Copy, Debug, PartialEq)] 18 | pub enum FaultKind { 19 | /// The fault was a read or write on a missing page. 20 | Missing, 21 | /// The fault was a write on a write-protected page. 22 | #[cfg(feature = "linux5_7")] 23 | WriteProtected, 24 | } 25 | 26 | /// Events from the userfaultfd object that are read by `Uffd::read_event()`. 27 | #[derive(Debug)] 28 | pub enum Event { 29 | /// A pagefault event. 30 | Pagefault { 31 | /// The kind of fault. 32 | kind: FaultKind, 33 | /// Whether the fault is on a read or a write. 34 | rw: ReadWrite, 35 | /// The address that triggered the fault. 36 | addr: *mut c_void, 37 | /// The thread that triggered the fault, if [`FeatureFlags::THREAD_ID`] is enabled. 38 | /// 39 | /// If the thread ID feature is not enabled, the value of this field is undefined. It would 40 | /// not be undefined behavior to use it, strictly speaking, but the [`Pid`] will not 41 | /// necessarily point to a real thread. 42 | /// 43 | /// This requires this crate to be compiled with the `linux4_14` feature. 44 | #[cfg(feature = "linux4_14")] 45 | thread_id: Pid, 46 | }, 47 | /// Generated when the faulting process invokes `fork(2)` (or `clone(2)` without the `CLONE_VM` 48 | /// flag). 49 | Fork { 50 | /// The `Uffd` object created for the child by `fork(2)` 51 | uffd: Uffd, 52 | }, 53 | /// Generated when the faulting process invokes `mremap(2)`. 54 | Remap { 55 | /// The original address of the memory range that was remapped. 56 | from: *mut c_void, 57 | /// The new address of the memory range that was remapped. 58 | to: *mut c_void, 59 | /// The original length of the memory range that was remapped. 60 | len: usize, 61 | }, 62 | /// Generated when the faulting process invokes `madvise(2)` with `MADV_DONTNEED` or 63 | /// `MADV_REMOVE` advice. 64 | Remove { 65 | /// The start address of the memory range that was freed. 66 | start: *mut c_void, 67 | /// The end address of the memory range that was freed. 68 | end: *mut c_void, 69 | }, 70 | /// Generated when the faulting process unmaps a meomry range, either explicitly using 71 | /// `munmap(2)` or implicitly during `mmap(2)` or `mremap(2)`. 72 | Unmap { 73 | /// The start address of the memory range that was unmapped. 74 | start: *mut c_void, 75 | /// The end address of the memory range that was unmapped. 76 | end: *mut c_void, 77 | }, 78 | } 79 | 80 | impl Event { 81 | pub(crate) fn from_uffd_msg(msg: &raw::uffd_msg) -> Result { 82 | match msg.event { 83 | raw::UFFD_EVENT_PAGEFAULT => { 84 | let pagefault = unsafe { msg.arg.pagefault }; 85 | cfg_if::cfg_if!( 86 | if #[cfg(feature = "linux5_7")] { 87 | let kind = if pagefault.flags & raw::UFFD_PAGEFAULT_FLAG_WP != 0 { 88 | FaultKind::WriteProtected 89 | } else { 90 | FaultKind::Missing 91 | }; 92 | } else { 93 | let kind = FaultKind::Missing; 94 | } 95 | ); 96 | 97 | let rw = if pagefault.flags & raw::UFFD_PAGEFAULT_FLAG_WRITE == 0 { 98 | ReadWrite::Read 99 | } else { 100 | ReadWrite::Write 101 | }; 102 | // Converting the ptid to i32 is safe because the maximum pid in 103 | // Linux is 2^22, which is about 4 million. 104 | // 105 | // Reference: 106 | // https://github.com/torvalds/linux/blob/2d338201d5311bcd79d42f66df4cecbcbc5f4f2c/include/linux/threads.h 107 | #[cfg(feature = "linux4_14")] 108 | let thread_id = Pid::from_raw(unsafe { pagefault.feat.ptid } as i32); 109 | Ok(Event::Pagefault { 110 | kind, 111 | rw, 112 | addr: pagefault.address as *mut c_void, 113 | #[cfg(feature = "linux4_14")] 114 | thread_id, 115 | }) 116 | } 117 | raw::UFFD_EVENT_FORK => { 118 | let fork = unsafe { msg.arg.fork }; 119 | Ok(Event::Fork { 120 | uffd: unsafe { Uffd::from_raw_fd(fork.ufd as RawFd) }, 121 | }) 122 | } 123 | raw::UFFD_EVENT_REMAP => { 124 | let remap = unsafe { msg.arg.remap }; 125 | Ok(Event::Remap { 126 | from: remap.from as *mut c_void, 127 | to: remap.to as *mut c_void, 128 | len: remap.len as usize, 129 | }) 130 | } 131 | raw::UFFD_EVENT_REMOVE => { 132 | let remove = unsafe { msg.arg.remove }; 133 | Ok(Event::Remove { 134 | start: remove.start as *mut c_void, 135 | end: remove.end as *mut c_void, 136 | }) 137 | } 138 | raw::UFFD_EVENT_UNMAP => { 139 | let remove = unsafe { msg.arg.remove }; 140 | Ok(Event::Unmap { 141 | start: remove.start as *mut c_void, 142 | end: remove.end as *mut c_void, 143 | }) 144 | } 145 | _ => Err(Error::UnrecognizedEvent(msg.event)), 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A Linux mechanism for handling page faults in user space. 2 | //! 3 | //! The main way to interact with this library is to create a `Uffd` object with a `UffdBuilder`, 4 | //! then use the methods of `Uffd` from a worker thread. 5 | //! 6 | //! See [`userfaultfd(2)`](http://man7.org/linux/man-pages/man2/userfaultfd.2.html) and 7 | //! [`ioctl_userfaultfd(2)`](http://man7.org/linux/man-pages/man2/ioctl_userfaultfd.2.html) for more 8 | //! details. 9 | 10 | mod builder; 11 | mod error; 12 | mod event; 13 | mod raw; 14 | 15 | pub use crate::builder::{FeatureFlags, UffdBuilder}; 16 | pub use crate::error::{Error, Result}; 17 | pub use crate::event::{Event, FaultKind, ReadWrite}; 18 | 19 | use bitflags::bitflags; 20 | use libc::{self, c_void}; 21 | use nix::errno::Errno; 22 | use nix::unistd::read; 23 | use std::mem; 24 | use std::os::fd::{AsFd, BorrowedFd}; 25 | use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; 26 | 27 | /// Represents an opaque buffer where userfaultfd events are stored. 28 | /// 29 | /// This is used in conjunction with [`Uffd::read_events`]. 30 | pub struct EventBuffer(Vec); 31 | 32 | impl EventBuffer { 33 | /// Creates a new buffer for `size` number of events. 34 | /// 35 | /// [`Uffd::read_events`] will read up to this many events at a time. 36 | pub fn new(size: usize) -> Self { 37 | Self(vec![unsafe { mem::zeroed() }; size]) 38 | } 39 | } 40 | 41 | /// The userfaultfd object. 42 | /// 43 | /// The userspace representation of the object is a file descriptor, so this type implements 44 | /// `AsRawFd`, `FromRawFd`, and `IntoRawFd`. These methods should be used with caution, but can be 45 | /// essential for using functions like `poll` on a worker thread. 46 | #[derive(Debug)] 47 | pub struct Uffd { 48 | fd: RawFd, 49 | } 50 | 51 | impl Drop for Uffd { 52 | fn drop(&mut self) { 53 | unsafe { libc::close(self.fd) }; 54 | } 55 | } 56 | 57 | impl AsFd for Uffd { 58 | fn as_fd(&self) -> BorrowedFd<'_> { 59 | unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } 60 | } 61 | } 62 | 63 | impl AsRawFd for Uffd { 64 | fn as_raw_fd(&self) -> RawFd { 65 | self.fd 66 | } 67 | } 68 | 69 | impl IntoRawFd for Uffd { 70 | fn into_raw_fd(self) -> RawFd { 71 | self.fd 72 | } 73 | } 74 | 75 | impl FromRawFd for Uffd { 76 | unsafe fn from_raw_fd(fd: RawFd) -> Self { 77 | Uffd { fd } 78 | } 79 | } 80 | 81 | bitflags! { 82 | /// The registration mode used when registering an address range with `Uffd`. 83 | #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] 84 | pub struct RegisterMode: u64 { 85 | /// Registers the range for missing page faults. 86 | const MISSING = raw::UFFDIO_REGISTER_MODE_MISSING; 87 | /// Registers the range for write faults. 88 | #[cfg(feature = "linux5_7")] 89 | const WRITE_PROTECT = raw::UFFDIO_REGISTER_MODE_WP; 90 | } 91 | } 92 | 93 | impl Uffd { 94 | /// Register a memory address range with the userfaultfd object, and returns the `IoctlFlags` 95 | /// that are available for the selected range. 96 | /// 97 | /// This method only registers the given range for missing page faults. 98 | pub fn register(&self, start: *mut c_void, len: usize) -> Result { 99 | self.register_with_mode(start, len, RegisterMode::MISSING) 100 | } 101 | 102 | /// Register a memory address range with the userfaultfd object for the given mode and 103 | /// returns the `IoctlFlags` that are available for the selected range. 104 | pub fn register_with_mode( 105 | &self, 106 | start: *mut c_void, 107 | len: usize, 108 | mode: RegisterMode, 109 | ) -> Result { 110 | let mut register = raw::uffdio_register { 111 | range: raw::uffdio_range { 112 | start: start as u64, 113 | len: len as u64, 114 | }, 115 | mode: mode.bits(), 116 | ioctls: 0, 117 | }; 118 | unsafe { 119 | raw::register(self.as_raw_fd(), &mut register as *mut raw::uffdio_register)?; 120 | } 121 | Ok(IoctlFlags::from_bits_retain(register.ioctls)) 122 | } 123 | 124 | /// Unregister a memory address range from the userfaultfd object. 125 | pub fn unregister(&self, start: *mut c_void, len: usize) -> Result<()> { 126 | let mut range = raw::uffdio_range { 127 | start: start as u64, 128 | len: len as u64, 129 | }; 130 | unsafe { 131 | raw::unregister(self.as_raw_fd(), &mut range as *mut raw::uffdio_range)?; 132 | } 133 | Ok(()) 134 | } 135 | 136 | /// Atomically copy a continuous memory chunk into the userfaultfd-registered range, and return 137 | /// the number of bytes that were successfully copied. 138 | /// 139 | /// If `wake` is `true`, wake up the thread waiting for page fault resolution on the memory 140 | /// range. 141 | pub unsafe fn copy( 142 | &self, 143 | src: *const c_void, 144 | dst: *mut c_void, 145 | len: usize, 146 | wake: bool, 147 | ) -> Result { 148 | let mut copy = raw::uffdio_copy { 149 | src: src as u64, 150 | dst: dst as u64, 151 | len: len as u64, 152 | mode: if wake { 153 | 0 154 | } else { 155 | raw::UFFDIO_COPY_MODE_DONTWAKE 156 | }, 157 | copy: 0, 158 | }; 159 | 160 | let _ = 161 | raw::copy(self.as_raw_fd(), &mut copy as *mut raw::uffdio_copy).map_err(|errno| { 162 | match errno { 163 | Errno::EAGAIN => Error::PartiallyCopied(copy.copy as usize), 164 | _ => Error::CopyFailed(errno), 165 | } 166 | })?; 167 | if copy.copy < 0 { 168 | // shouldn't ever get here, as errno should be caught above 169 | Err(Error::CopyFailed(Errno::from_i32(-copy.copy as i32))) 170 | } else { 171 | Ok(copy.copy as usize) 172 | } 173 | } 174 | 175 | /// Zero out a memory address range registered with userfaultfd, and return the number of bytes 176 | /// that were successfully zeroed. 177 | /// 178 | /// If `wake` is `true`, wake up the thread waiting for page fault resolution on the memory 179 | /// address range. 180 | pub unsafe fn zeropage(&self, start: *mut c_void, len: usize, wake: bool) -> Result { 181 | let mut zeropage = raw::uffdio_zeropage { 182 | range: raw::uffdio_range { 183 | start: start as u64, 184 | len: len as u64, 185 | }, 186 | mode: if wake { 187 | 0 188 | } else { 189 | raw::UFFDIO_ZEROPAGE_MODE_DONTWAKE 190 | }, 191 | zeropage: 0, 192 | }; 193 | 194 | let _ = raw::zeropage(self.as_raw_fd(), &mut zeropage as &mut raw::uffdio_zeropage) 195 | .map_err(Error::ZeropageFailed)?; 196 | if zeropage.zeropage < 0 { 197 | // shouldn't ever get here, as errno should be caught above 198 | Err(Error::ZeropageFailed(Errno::from_i32( 199 | -zeropage.zeropage as i32, 200 | ))) 201 | } else { 202 | Ok(zeropage.zeropage as usize) 203 | } 204 | } 205 | 206 | /// Wake up the thread waiting for page fault resolution on the specified memory address range. 207 | pub fn wake(&self, start: *mut c_void, len: usize) -> Result<()> { 208 | let mut range = raw::uffdio_range { 209 | start: start as u64, 210 | len: len as u64, 211 | }; 212 | unsafe { 213 | raw::wake(self.as_raw_fd(), &mut range as *mut raw::uffdio_range)?; 214 | } 215 | Ok(()) 216 | } 217 | 218 | /// Makes a range write-protected. 219 | #[cfg(feature = "linux5_7")] 220 | pub fn write_protect(&self, start: *mut c_void, len: usize) -> Result<()> { 221 | let mut ioctl = raw::uffdio_writeprotect { 222 | range: raw::uffdio_range { 223 | start: start as u64, 224 | len: len as u64, 225 | }, 226 | mode: raw::UFFDIO_WRITEPROTECT_MODE_WP, 227 | }; 228 | 229 | unsafe { 230 | raw::write_protect( 231 | self.as_raw_fd(), 232 | &mut ioctl as *mut raw::uffdio_writeprotect, 233 | )?; 234 | } 235 | 236 | Ok(()) 237 | } 238 | 239 | /// Removes the write-protection for a range. 240 | /// 241 | /// If `wake` is `true`, wake up the thread waiting for page fault resolution on the memory 242 | /// address range. 243 | #[cfg(feature = "linux5_7")] 244 | pub fn remove_write_protection( 245 | &self, 246 | start: *mut c_void, 247 | len: usize, 248 | wake: bool, 249 | ) -> Result<()> { 250 | let mut ioctl = raw::uffdio_writeprotect { 251 | range: raw::uffdio_range { 252 | start: start as u64, 253 | len: len as u64, 254 | }, 255 | mode: if wake { 256 | 0 257 | } else { 258 | raw::UFFDIO_WRITEPROTECT_MODE_DONTWAKE 259 | }, 260 | }; 261 | 262 | unsafe { 263 | raw::write_protect( 264 | self.as_raw_fd(), 265 | &mut ioctl as *mut raw::uffdio_writeprotect, 266 | )?; 267 | } 268 | 269 | Ok(()) 270 | } 271 | 272 | /// Read an `Event` from the userfaultfd object. 273 | /// 274 | /// If the `Uffd` object was created with `non_blocking` set to `false`, this will block until 275 | /// an event is successfully read (returning `Some(event)`, or an error is returned. 276 | /// 277 | /// If `non_blocking` was `true`, this will immediately return `None` if no event is ready to 278 | /// read. 279 | /// 280 | /// Note that while this method doesn't require a mutable reference to the `Uffd` object, it 281 | /// does consume bytes (thread-safely) from the underlying file descriptor. 282 | /// 283 | /// # Examples 284 | /// 285 | /// ```rust 286 | /// # use userfaultfd::{Uffd, Result}; 287 | /// fn read_event(uffd: &Uffd) -> Result<()> { 288 | /// // Read a single event 289 | /// match uffd.read_event()? { 290 | /// Some(e) => { 291 | /// // Do something with the event 292 | /// }, 293 | /// None => { 294 | /// // This was a non-blocking read and the descriptor was not ready for read 295 | /// }, 296 | /// } 297 | /// Ok(()) 298 | /// } 299 | /// ``` 300 | pub fn read_event(&self) -> Result> { 301 | let mut buf = [unsafe { std::mem::zeroed() }; 1]; 302 | let mut iter = self.read(&mut buf)?; 303 | let event = iter.next().transpose()?; 304 | assert!(iter.next().is_none()); 305 | Ok(event) 306 | } 307 | 308 | /// Read multiple events from the userfaultfd object using the given event buffer. 309 | /// 310 | /// If the `Uffd` object was created with `non_blocking` set to `false`, this will block until 311 | /// an event is successfully read or an error is returned. 312 | /// 313 | /// If `non_blocking` was `true`, this will immediately return an empty iterator if the file 314 | /// descriptor is not ready for reading. 315 | /// 316 | /// # Examples 317 | /// 318 | /// ```rust 319 | /// # use userfaultfd::{Uffd, EventBuffer}; 320 | /// fn read_events(uffd: &Uffd) -> userfaultfd::Result<()> { 321 | /// // Read up to 100 events at a time 322 | /// let mut buf = EventBuffer::new(100); 323 | /// for event in uffd.read_events(&mut buf)? { 324 | /// let event = event?; 325 | /// // Do something with the event... 326 | /// } 327 | /// Ok(()) 328 | /// } 329 | /// ``` 330 | pub fn read_events<'a>( 331 | &self, 332 | buf: &'a mut EventBuffer, 333 | ) -> Result> + 'a> { 334 | self.read(&mut buf.0) 335 | } 336 | 337 | fn read<'a>( 338 | &self, 339 | msgs: &'a mut [raw::uffd_msg], 340 | ) -> Result> + 'a> { 341 | const MSG_SIZE: usize = std::mem::size_of::(); 342 | 343 | let buf = unsafe { 344 | std::slice::from_raw_parts_mut(msgs.as_mut_ptr() as _, msgs.len() * MSG_SIZE) 345 | }; 346 | 347 | let count = match read(self.as_raw_fd(), buf) { 348 | Err(e) if e == Errno::EAGAIN => 0, 349 | Err(e) => return Err(Error::SystemError(e)), 350 | Ok(0) => return Err(Error::ReadEof), 351 | Ok(bytes_read) => { 352 | let remainder = bytes_read % MSG_SIZE; 353 | if remainder != 0 { 354 | return Err(Error::IncompleteMsg { 355 | read: remainder, 356 | expected: MSG_SIZE, 357 | }); 358 | } 359 | 360 | bytes_read / MSG_SIZE 361 | } 362 | }; 363 | 364 | Ok(msgs.iter().take(count).map(|msg| Event::from_uffd_msg(msg))) 365 | } 366 | } 367 | 368 | bitflags! { 369 | /// Used with `UffdBuilder` and `Uffd::register()` to determine which operations are available. 370 | #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] 371 | pub struct IoctlFlags: u64 { 372 | const REGISTER = 1 << raw::_UFFDIO_REGISTER; 373 | const UNREGISTER = 1 << raw::_UFFDIO_UNREGISTER; 374 | const WAKE = 1 << raw::_UFFDIO_WAKE; 375 | const COPY = 1 << raw::_UFFDIO_COPY; 376 | const ZEROPAGE = 1 << raw::_UFFDIO_ZEROPAGE; 377 | #[cfg(feature = "linux5_7")] 378 | const WRITE_PROTECT = 1 << raw::_UFFDIO_WRITEPROTECT; 379 | const API = 1 << raw::_UFFDIO_API; 380 | 381 | /// Unknown ioctls flags are allowed to be robust to future kernel changes. 382 | const _ = !0; 383 | } 384 | } 385 | 386 | #[cfg(test)] 387 | mod test { 388 | use super::*; 389 | use std::ptr; 390 | use std::thread; 391 | 392 | #[test] 393 | fn test_read_event() -> Result<()> { 394 | const PAGE_SIZE: usize = 4096; 395 | 396 | unsafe { 397 | let uffd = UffdBuilder::new().close_on_exec(true).create()?; 398 | 399 | let mapping = libc::mmap( 400 | ptr::null_mut(), 401 | PAGE_SIZE, 402 | libc::PROT_READ | libc::PROT_WRITE, 403 | libc::MAP_PRIVATE | libc::MAP_ANON, 404 | -1, 405 | 0, 406 | ); 407 | 408 | assert!(!mapping.is_null()); 409 | 410 | uffd.register(mapping, PAGE_SIZE)?; 411 | 412 | let ptr = mapping as usize; 413 | let thread = thread::spawn(move || { 414 | let ptr = ptr as *mut u8; 415 | *ptr = 1; 416 | }); 417 | 418 | match uffd.read_event()? { 419 | Some(Event::Pagefault { 420 | rw: ReadWrite::Write, 421 | addr, 422 | .. 423 | }) => { 424 | assert_eq!(addr, mapping); 425 | uffd.zeropage(addr, PAGE_SIZE, true)?; 426 | } 427 | _ => panic!("unexpected event"), 428 | } 429 | 430 | thread.join().expect("failed to join thread"); 431 | 432 | uffd.unregister(mapping, PAGE_SIZE)?; 433 | 434 | assert_eq!(libc::munmap(mapping, PAGE_SIZE), 0); 435 | } 436 | 437 | Ok(()) 438 | } 439 | 440 | #[test] 441 | fn test_nonblocking_read_event() -> Result<()> { 442 | const PAGE_SIZE: usize = 4096; 443 | 444 | unsafe { 445 | let uffd = UffdBuilder::new() 446 | .close_on_exec(true) 447 | .non_blocking(true) 448 | .create()?; 449 | 450 | let mapping = libc::mmap( 451 | ptr::null_mut(), 452 | PAGE_SIZE, 453 | libc::PROT_READ | libc::PROT_WRITE, 454 | libc::MAP_PRIVATE | libc::MAP_ANON, 455 | -1, 456 | 0, 457 | ); 458 | 459 | assert!(!mapping.is_null()); 460 | 461 | uffd.register(mapping, PAGE_SIZE)?; 462 | 463 | assert!(uffd.read_event()?.is_none()); 464 | 465 | let ptr = mapping as usize; 466 | let thread = thread::spawn(move || { 467 | let ptr = ptr as *mut u8; 468 | *ptr = 1; 469 | }); 470 | 471 | loop { 472 | match uffd.read_event()? { 473 | Some(Event::Pagefault { 474 | rw: ReadWrite::Write, 475 | addr, 476 | .. 477 | }) => { 478 | assert_eq!(addr, mapping); 479 | uffd.zeropage(addr, PAGE_SIZE, true)?; 480 | break; 481 | } 482 | Some(_) => panic!("unexpected event"), 483 | None => thread::sleep(std::time::Duration::from_millis(50)), 484 | } 485 | } 486 | 487 | thread.join().expect("failed to join thread"); 488 | 489 | uffd.unregister(mapping, PAGE_SIZE)?; 490 | 491 | assert_eq!(libc::munmap(mapping, PAGE_SIZE), 0); 492 | } 493 | 494 | Ok(()) 495 | } 496 | 497 | #[test] 498 | fn test_read_events() -> Result<()> { 499 | unsafe { 500 | const MAX_THREADS: usize = 5; 501 | const PAGE_SIZE: usize = 4096; 502 | const MEM_SIZE: usize = PAGE_SIZE * MAX_THREADS; 503 | 504 | let uffd = UffdBuilder::new().close_on_exec(true).create()?; 505 | 506 | let mapping = libc::mmap( 507 | ptr::null_mut(), 508 | MEM_SIZE, 509 | libc::PROT_READ | libc::PROT_WRITE, 510 | libc::MAP_PRIVATE | libc::MAP_ANON, 511 | -1, 512 | 0, 513 | ); 514 | 515 | assert!(!mapping.is_null()); 516 | 517 | uffd.register(mapping, MEM_SIZE)?; 518 | 519 | // As accessing the memory will suspend each thread with a page fault event, 520 | // there is no way to signal that the operations the test thread is waiting on to 521 | // complete have been performed. 522 | // 523 | // Therefore, this is inherently racy. The best we can do is simply sleep-wait for 524 | // all threads to have signaled that the operation is *about to be performed*. 525 | let mut seen = [false; MAX_THREADS]; 526 | let mut threads = Vec::new(); 527 | for i in 0..MAX_THREADS { 528 | let seen = &mut seen[i] as *mut _ as usize; 529 | let ptr = (mapping as *mut u8).add(PAGE_SIZE * i) as usize; 530 | threads.push(thread::spawn(move || { 531 | let seen = seen as *mut bool; 532 | let ptr = ptr as *mut u8; 533 | *seen = true; 534 | *ptr = 1; 535 | })); 536 | } 537 | 538 | loop { 539 | // Sleep even if all threads have "signaled", just in case any 540 | // thread is preempted prior to faulting the memory access. 541 | // Still, there's no guarantee that the call to `read_events` below will 542 | // read all the events at once, but this should be "good enough". 543 | let done = seen.iter().all(|b| *b); 544 | thread::sleep(std::time::Duration::from_millis(50)); 545 | if done { 546 | break; 547 | } 548 | } 549 | 550 | // Read all the events at once 551 | let mut buf = EventBuffer::new(MAX_THREADS); 552 | let mut iter = uffd.read_events(&mut buf)?; 553 | 554 | let mut seen = [false; MAX_THREADS]; 555 | for _ in 0..MAX_THREADS { 556 | match iter 557 | .next() 558 | .transpose()? 559 | .expect("failed to read all events; potential race condition was hit") 560 | { 561 | Event::Pagefault { 562 | rw: ReadWrite::Write, 563 | addr, 564 | .. 565 | } => { 566 | let index = (addr as usize - mapping as usize) / PAGE_SIZE; 567 | assert_eq!(seen[index], false); 568 | seen[index] = true; 569 | uffd.zeropage(addr, PAGE_SIZE, true)?; 570 | } 571 | _ => panic!("unexpected event"), 572 | } 573 | } 574 | 575 | assert!(seen.iter().all(|b| *b)); 576 | 577 | for thread in threads { 578 | thread.join().expect("failed to join thread"); 579 | } 580 | 581 | uffd.unregister(mapping, MEM_SIZE)?; 582 | 583 | assert_eq!(libc::munmap(mapping, MEM_SIZE), 0); 584 | } 585 | 586 | Ok(()) 587 | } 588 | 589 | #[cfg(feature = "linux5_7")] 590 | #[test] 591 | fn test_write_protect() -> Result<()> { 592 | const PAGE_SIZE: usize = 4096; 593 | 594 | unsafe { 595 | let uffd = UffdBuilder::new() 596 | .require_features(FeatureFlags::PAGEFAULT_FLAG_WP) 597 | .close_on_exec(true) 598 | .create()?; 599 | 600 | let mapping = libc::mmap( 601 | ptr::null_mut(), 602 | PAGE_SIZE, 603 | libc::PROT_READ | libc::PROT_WRITE, 604 | libc::MAP_PRIVATE | libc::MAP_ANON, 605 | -1, 606 | 0, 607 | ); 608 | 609 | assert!(!mapping.is_null()); 610 | 611 | // This test uses both missing and write-protect modes for a reason. 612 | // The `uffdio_writeprotect` ioctl can only be used on a range *after* 613 | // the missing fault is handled, it seems. This means we either need to 614 | // read/write the page *before* we protect it or handle the missing 615 | // page fault by changing the protection level *after* we zero the page. 616 | assert!(uffd 617 | .register_with_mode( 618 | mapping, 619 | PAGE_SIZE, 620 | RegisterMode::MISSING | RegisterMode::WRITE_PROTECT 621 | )? 622 | .contains(IoctlFlags::WRITE_PROTECT)); 623 | 624 | let ptr = mapping as usize; 625 | let thread = thread::spawn(move || { 626 | let ptr = ptr as *mut u8; 627 | *ptr = 1; 628 | *ptr = 2; 629 | }); 630 | 631 | loop { 632 | match uffd.read_event()? { 633 | Some(Event::Pagefault { 634 | kind, 635 | rw: ReadWrite::Write, 636 | addr, 637 | .. 638 | }) => match kind { 639 | FaultKind::WriteProtected => { 640 | assert_eq!(addr, mapping); 641 | assert_eq!(*(addr as *const u8), 0); 642 | // Remove the protection and wake the page 643 | uffd.remove_write_protection(mapping, PAGE_SIZE, true)?; 644 | break; 645 | } 646 | FaultKind::Missing => { 647 | assert_eq!(addr, mapping); 648 | uffd.zeropage(mapping, PAGE_SIZE, false)?; 649 | 650 | // Technically, we already know it was a write that triggered 651 | // the missing page fault, so there's little point in immediately 652 | // write-protecting the page to cause another fault; in the real 653 | // world, a missing fault with `rw` being `ReadWrite::Write` would 654 | // be enough to mark the page as "dirty". For this test, however, 655 | // we do it this way to ensure a write-protected fault is read. 656 | assert_eq!(*(addr as *const u8), 0); 657 | uffd.write_protect(mapping, PAGE_SIZE)?; 658 | uffd.wake(mapping, PAGE_SIZE)?; 659 | } 660 | }, 661 | _ => panic!("unexpected event"), 662 | } 663 | } 664 | 665 | thread.join().expect("failed to join thread"); 666 | 667 | assert_eq!(*(mapping as *const u8), 2); 668 | 669 | uffd.unregister(mapping, PAGE_SIZE)?; 670 | 671 | assert_eq!(libc::munmap(mapping, PAGE_SIZE), 0); 672 | } 673 | 674 | Ok(()) 675 | } 676 | } 677 | -------------------------------------------------------------------------------- /src/raw.rs: -------------------------------------------------------------------------------- 1 | use libc::{c_int, c_long, syscall, SYS_userfaultfd, INT_MAX}; 2 | pub use userfaultfd_sys::*; 3 | 4 | pub unsafe fn userfaultfd(flags: c_int) -> c_int { 5 | let fd = syscall(SYS_userfaultfd, flags as c_long); 6 | if fd > INT_MAX as c_long { 7 | panic!("fd doesn't fit in a c_int"); 8 | } else { 9 | fd as c_int 10 | } 11 | } 12 | 13 | nix::ioctl_readwrite!(api, UFFDIO, _UFFDIO_API, uffdio_api); 14 | nix::ioctl_readwrite!(register, UFFDIO, _UFFDIO_REGISTER, uffdio_register); 15 | nix::ioctl_read!(unregister, UFFDIO, _UFFDIO_UNREGISTER, uffdio_range); 16 | nix::ioctl_read!(wake, UFFDIO, _UFFDIO_WAKE, uffdio_range); 17 | nix::ioctl_readwrite!(copy, UFFDIO, _UFFDIO_COPY, uffdio_copy); 18 | nix::ioctl_readwrite!(zeropage, UFFDIO, _UFFDIO_ZEROPAGE, uffdio_zeropage); 19 | #[cfg(feature = "linux5_7")] 20 | nix::ioctl_readwrite!( 21 | write_protect, 22 | UFFDIO, 23 | _UFFDIO_WRITEPROTECT, 24 | uffdio_writeprotect 25 | ); 26 | 27 | // ioctls for /dev/userfaultfd 28 | 29 | // This is the `/dev/userfaultfd` ioctl() from creating a new userfault file descriptor. 30 | // It is a "bad" ioctl in the sense that it is defined as an _IOC: 31 | // https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/userfaultfd.h#L17, 32 | // aka `nix::ioctl_none`, however it does receive an integer argument: 33 | // https://elixir.bootlin.com/linux/latest/source/fs/userfaultfd.c#L2186. That is the same argument 34 | // that the userfaultfd() system call receives. 35 | nix::ioctl_write_int_bad!( 36 | /// Create a new userfault file descriptor from the `/dev/userfaultfd` 37 | /// device. This receives the same arguments as the userfaultfd system call. 38 | new_uffd, 39 | nix::request_code_none!(USERFAULTFD_IOC, 0x00) 40 | ); 41 | -------------------------------------------------------------------------------- /tests/manpage_example.rs: -------------------------------------------------------------------------------- 1 | #[test] 2 | fn run_manpage_example() { 3 | let output = std::process::Command::new("cargo") 4 | .args(&["run", "--example", "manpage", "--", "3"]) 5 | .output() 6 | .expect("manpage example failed to start"); 7 | assert!(output.status.success(), "manpage example succeeded"); 8 | } 9 | -------------------------------------------------------------------------------- /userfaultfd-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "userfaultfd-sys" 3 | version = "0.5.0" 4 | authors = ["The Wasmtime Project Developers"] 5 | edition = "2018" 6 | license = "MIT OR Apache-2.0" 7 | description = "Low-level bindings for userfaultfd functionality on Linux." 8 | repository = "https://github.com/bytecodealliance/userfaultfd-rs" 9 | 10 | build = "build.rs" 11 | 12 | [dependencies] 13 | cfg-if = "^1.0.0" 14 | 15 | [build-dependencies] 16 | bindgen = { version = "^0.68.1", default-features = false, features = ["runtime"] } 17 | cc = "1.0" 18 | 19 | [features] 20 | default = [] 21 | linux4_14 = [] 22 | linux5_7 = [] 23 | -------------------------------------------------------------------------------- /userfaultfd-sys/LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | ../LICENSE-APACHE -------------------------------------------------------------------------------- /userfaultfd-sys/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | ../LICENSE-MIT -------------------------------------------------------------------------------- /userfaultfd-sys/build.rs: -------------------------------------------------------------------------------- 1 | use bindgen; 2 | use bindgen::callbacks::{IntKind, ParseCallbacks}; 3 | use cc; 4 | use std::env; 5 | use std::path::PathBuf; 6 | 7 | fn main() { 8 | generate_bindings(); 9 | 10 | cc::Build::new() 11 | .file("src/consts.c") 12 | .compile("userfaultfd_sys_consts"); 13 | } 14 | 15 | fn generate_bindings() { 16 | let mut bindings = bindgen::Builder::default() 17 | .header("wrapper.h") 18 | // filter out stuff from 19 | .blocklist_item("__BITS_PER_LONG") 20 | .blocklist_item("__FD_SETSIZE") 21 | .blocklist_type("__[lb]e.*") 22 | .blocklist_type("__w?sum.*") 23 | .blocklist_type("__kernel_*") 24 | .parse_callbacks(Box::new(Callbacks {})); 25 | 26 | if let Ok(linux_headers) = std::env::var("LINUX_HEADERS") { 27 | let mut incl_dir = PathBuf::from(&linux_headers); 28 | incl_dir.push("include"); 29 | assert!( 30 | incl_dir.exists(), 31 | "LINUX_HEADERS env variable contains an include/ directory" 32 | ); 33 | incl_dir.push("uapi"); 34 | assert!( 35 | incl_dir.exists(), 36 | "LINUX_HEADERS env variable contains an include/uapi/ directory" 37 | ); 38 | bindings = bindings 39 | .clang_arg(format!("-isystem{}/include", linux_headers)) 40 | .clang_arg(format!("-isystem{}/include/uapi", linux_headers)); 41 | } 42 | 43 | let bindings = bindings.generate().expect("binding generation failed"); 44 | 45 | let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); 46 | bindings 47 | .write_to_file(out_path.join("bindings.rs")) 48 | .expect("binding file couldn't be written"); 49 | } 50 | 51 | // all this stuff with callbacks is to give the integer constants the right types 52 | 53 | #[derive(Debug)] 54 | struct Callbacks {} 55 | 56 | impl ParseCallbacks for Callbacks { 57 | fn int_macro(&self, name: &str, _value: i64) -> Option { 58 | for (prefix, kind) in [ 59 | ("_UFFDIO_", IntKind::U64), 60 | ("UFFD_API", IntKind::U64), 61 | ("UFFDIO", IntKind::U8), 62 | ("UFFD_EVENT_", IntKind::U8), 63 | ("UFFD_PAGEFAULT_FLAG_", IntKind::U64), 64 | ("UFFD_FEATURE_", IntKind::U64), 65 | ] 66 | .iter() 67 | { 68 | if name.starts_with(prefix) { 69 | return Some(*kind); 70 | } 71 | } 72 | return None; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /userfaultfd-sys/src/consts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #ifdef UFFD_API 6 | const __u64 _const_UFFD_API = UFFD_API; 7 | #endif 8 | 9 | #ifdef UFFD_API_FEATURES 10 | const __u64 _const_UFFD_API_FEATURES = UFFD_API_FEATURES; 11 | #endif 12 | 13 | #ifdef UFFD_API_IOCTLS 14 | const __u64 _const_UFFD_API_IOCTLS = UFFD_API_IOCTLS; 15 | #endif 16 | 17 | #ifdef UFFD_API_RANGE_IOCTLS 18 | const __u64 _const_UFFD_API_RANGE_IOCTLS = UFFD_API_RANGE_IOCTLS; 19 | #endif 20 | 21 | #ifdef UFFD_API_RANGE_IOCTLS_BASIC 22 | const __u64 _const_UFFD_API_RANGE_IOCTLS_BASIC = UFFD_API_RANGE_IOCTLS_BASIC; 23 | #endif 24 | 25 | #ifdef UFFDIO_REGISTER_MODE_MISSING 26 | const __u64 _const_UFFDIO_REGISTER_MODE_MISSING = UFFDIO_REGISTER_MODE_MISSING; 27 | #endif 28 | 29 | #ifdef UFFDIO_REGISTER_MODE_WP 30 | const __u64 _const_UFFDIO_REGISTER_MODE_WP = UFFDIO_REGISTER_MODE_WP; 31 | #endif 32 | 33 | #ifdef UFFDIO_COPY_MODE_DONTWAKE 34 | const __u64 _const_UFFDIO_COPY_MODE_DONTWAKE = UFFDIO_COPY_MODE_DONTWAKE; 35 | #endif 36 | 37 | #ifdef UFFDIO_COPY_MODE_WP 38 | const __u64 _const_UFFDIO_COPY_MODE_WP = UFFDIO_COPY_MODE_WP; 39 | #endif 40 | 41 | #ifdef UFFDIO_ZEROPAGE_MODE_DONTWAKE 42 | const __u64 _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE = UFFDIO_ZEROPAGE_MODE_DONTWAKE; 43 | #endif 44 | 45 | #ifdef UFFDIO_API 46 | const __u32 _const_UFFDIO_API = UFFDIO_API; 47 | #endif 48 | 49 | #ifdef UFFDIO_REGISTER 50 | const __u32 _const_UFFDIO_REGISTER = UFFDIO_REGISTER; 51 | #endif 52 | 53 | #ifdef UFFDIO_UNREGISTER 54 | const __u32 _const_UFFDIO_UNREGISTER = UFFDIO_UNREGISTER; 55 | #endif 56 | 57 | #ifdef UFFDIO_WAKE 58 | const __u32 _const_UFFDIO_WAKE = UFFDIO_WAKE; 59 | #endif 60 | 61 | #ifdef UFFDIO_COPY 62 | const __u32 _const_UFFDIO_COPY = UFFDIO_COPY; 63 | #endif 64 | 65 | #ifdef UFFDIO_ZEROPAGE 66 | const __u32 _const_UFFDIO_ZEROPAGE = UFFDIO_ZEROPAGE; 67 | #endif 68 | 69 | #ifdef UFFDIO_WRITEPROTECT 70 | const __u32 _const_UFFDIO_WRITEPROTECT = UFFDIO_WRITEPROTECT; 71 | #endif 72 | 73 | #ifdef USERFAULTFD_IOC 74 | const __u32 _const_USERFAULTFD_IOC = USERFAULTFD_IOC; 75 | #endif 76 | -------------------------------------------------------------------------------- /userfaultfd-sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! System bindings to `userfaultfd`. 2 | //! 3 | //! The minimum supported Linux kernel version is 4.11, but additional features from 4.14+ are 4 | //! available by using the `linux4_14` Cargo feature. 5 | 6 | #![allow(non_upper_case_globals)] 7 | #![allow(non_camel_case_types)] 8 | #![allow(non_snake_case)] 9 | 10 | use cfg_if::cfg_if; 11 | 12 | cfg_if! { 13 | if #[cfg(feature = "linux5_7")] { 14 | mod linux5_7; 15 | pub use crate::linux5_7::*; 16 | } 17 | else if #[cfg(feature = "linux4_14")] { 18 | mod linux4_14; 19 | pub use crate::linux4_14::*; 20 | } else { 21 | mod linux4_11; 22 | pub use crate::linux4_11::*; 23 | } 24 | } 25 | 26 | include!(concat!(env!("OUT_DIR"), "/bindings.rs")); 27 | -------------------------------------------------------------------------------- /userfaultfd-sys/src/linux4_11.rs: -------------------------------------------------------------------------------- 1 | use super::*; 2 | 3 | // The following are preprocessor constants that bindgen can't figure out, so we enter them manually 4 | // from , and have tests to make sure they're accurate. 5 | 6 | pub const UFFD_API: u64 = 0xAA; 7 | pub const UFFD_API_FEATURES: u64 = UFFD_FEATURE_EVENT_FORK 8 | | UFFD_FEATURE_EVENT_REMAP 9 | | UFFD_FEATURE_EVENT_REMOVE 10 | | UFFD_FEATURE_EVENT_UNMAP 11 | | UFFD_FEATURE_MISSING_HUGETLBFS 12 | | UFFD_FEATURE_MISSING_SHMEM; 13 | 14 | pub const UFFD_API_IOCTLS: u64 = 1 << _UFFDIO_REGISTER | 1 << _UFFDIO_UNREGISTER | 1 << _UFFDIO_API; 15 | pub const UFFD_API_RANGE_IOCTLS: u64 = 16 | 1 << _UFFDIO_WAKE | 1 << _UFFDIO_COPY | 1 << _UFFDIO_ZEROPAGE; 17 | 18 | pub const UFFDIO_REGISTER_MODE_MISSING: u64 = 1 << 0; 19 | pub const UFFDIO_REGISTER_MODE_WP: u64 = 1 << 1; 20 | 21 | pub const UFFDIO_COPY_MODE_DONTWAKE: u64 = 1 << 0; 22 | pub const UFFDIO_COPY_MODE_WP: u64 = 1 << 1; 23 | 24 | pub const UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64 = 1 << 0; 25 | 26 | pub const UFFDIO_API: u32 = 0xc018aa3f; 27 | pub const UFFDIO_REGISTER: u32 = 0xc020aa00; 28 | pub const UFFDIO_UNREGISTER: u32 = 0x8010aa01; 29 | pub const UFFDIO_WAKE: u32 = 0x8010aa02; 30 | pub const UFFDIO_COPY: u32 = 0xc028aa03; 31 | pub const UFFDIO_ZEROPAGE: u32 = 0xc020aa04; 32 | pub const UFFDIO_WRITEPROTECT: u32 = 0xc018aa06; 33 | 34 | #[cfg(test)] 35 | mod const_tests { 36 | use super::*; 37 | 38 | extern "C" { 39 | static _const_UFFD_API: u64; 40 | static _const_UFFD_API_FEATURES: u64; 41 | static _const_UFFD_API_IOCTLS: u64; 42 | static _const_UFFD_API_RANGE_IOCTLS: u64; 43 | static _const_UFFDIO_REGISTER_MODE_MISSING: u64; 44 | static _const_UFFDIO_REGISTER_MODE_WP: u64; 45 | static _const_UFFDIO_COPY_MODE_DONTWAKE: u64; 46 | static _const_UFFDIO_COPY_MODE_WP: u64; 47 | static _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64; 48 | static _const_UFFDIO_API: u32; 49 | static _const_UFFDIO_REGISTER: u32; 50 | static _const_UFFDIO_UNREGISTER: u32; 51 | static _const_UFFDIO_WAKE: u32; 52 | static _const_UFFDIO_COPY: u32; 53 | static _const_UFFDIO_ZEROPAGE: u32; 54 | static _const_UFFDIO_WRITEPROTECT: u32; 55 | } 56 | 57 | #[test] 58 | fn consts_correct() { 59 | unsafe { 60 | assert_eq!(UFFD_API, _const_UFFD_API, "UFFD_API"); 61 | assert_eq!( 62 | UFFD_API_FEATURES, _const_UFFD_API_FEATURES, 63 | "UFFD_API_FEATURES" 64 | ); 65 | assert_eq!(UFFD_API_IOCTLS, _const_UFFD_API_IOCTLS, "UFFD_API_IOCTLS"); 66 | assert_eq!( 67 | UFFD_API_RANGE_IOCTLS, _const_UFFD_API_RANGE_IOCTLS, 68 | "UFFD_API_RANGE_IOCTLS" 69 | ); 70 | assert_eq!( 71 | UFFDIO_REGISTER_MODE_MISSING, _const_UFFDIO_REGISTER_MODE_MISSING, 72 | "UFFDIO_REGISTER_MODE_MISSING" 73 | ); 74 | assert_eq!( 75 | UFFDIO_REGISTER_MODE_WP, _const_UFFDIO_REGISTER_MODE_WP, 76 | "UFFDIO_REGISTER_MODE_WP" 77 | ); 78 | assert_eq!( 79 | UFFDIO_COPY_MODE_DONTWAKE, _const_UFFDIO_COPY_MODE_DONTWAKE, 80 | "UFFDIO_COPY_MODE_DONTWAKE" 81 | ); 82 | assert_eq!( 83 | UFFDIO_COPY_MODE_WP, _const_UFFDIO_COPY_MODE_WP, 84 | "UFFDIO_COPY_MODE_WP" 85 | ); 86 | assert_eq!( 87 | UFFDIO_ZEROPAGE_MODE_DONTWAKE, _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE, 88 | "UFFDIO_ZEROPAGE_MODE_DONTWAKE" 89 | ); 90 | assert_eq!(UFFDIO_API, _const_UFFDIO_API, "UFFDIO_API"); 91 | assert_eq!(UFFDIO_REGISTER, _const_UFFDIO_REGISTER, "UFFDIO_REGISTER"); 92 | assert_eq!( 93 | UFFDIO_UNREGISTER, _const_UFFDIO_UNREGISTER, 94 | "UFFDIO_UNREGISTER" 95 | ); 96 | assert_eq!(UFFDIO_WAKE, _const_UFFDIO_WAKE, "UFFDIO_WAKE"); 97 | assert_eq!(UFFDIO_COPY, _const_UFFDIO_COPY, "UFFDIO_COPY"); 98 | assert_eq!(UFFDIO_ZEROPAGE, _const_UFFDIO_ZEROPAGE, "UFFDIO_ZEROPAGE"); 99 | assert_eq!( 100 | UFFDIO_WRITEPROTECT, _const_UFFDIO_WRITEPROTECT, 101 | "UFFDIO_WRITEPROTECT" 102 | ); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /userfaultfd-sys/src/linux4_14.rs: -------------------------------------------------------------------------------- 1 | use super::*; 2 | 3 | // The following are preprocessor constants that bindgen can't figure out, so we enter them manually 4 | // from , and have tests to make sure they're accurate. 5 | 6 | pub const UFFD_API: u64 = 0xAA; 7 | 8 | pub const UFFD_API_FEATURES: u64 = UFFD_FEATURE_EVENT_FORK 9 | | UFFD_FEATURE_EVENT_REMAP 10 | | UFFD_FEATURE_EVENT_REMOVE 11 | | UFFD_FEATURE_EVENT_UNMAP 12 | | UFFD_FEATURE_MISSING_HUGETLBFS 13 | | UFFD_FEATURE_MISSING_SHMEM 14 | | UFFD_FEATURE_SIGBUS 15 | | UFFD_FEATURE_THREAD_ID; 16 | pub const UFFD_API_IOCTLS: u64 = 1 << _UFFDIO_REGISTER | 1 << _UFFDIO_UNREGISTER | 1 << _UFFDIO_API; 17 | pub const UFFD_API_RANGE_IOCTLS: u64 = 18 | 1 << _UFFDIO_WAKE | 1 << _UFFDIO_COPY | 1 << _UFFDIO_ZEROPAGE; 19 | pub const UFFD_API_RANGE_IOCTLS_BASIC: u64 = 1 << _UFFDIO_WAKE | 1 << _UFFDIO_COPY; 20 | 21 | pub const UFFDIO_REGISTER_MODE_MISSING: u64 = 1 << 0; 22 | pub const UFFDIO_REGISTER_MODE_WP: u64 = 1 << 1; 23 | 24 | pub const UFFDIO_COPY_MODE_DONTWAKE: u64 = 1 << 0; 25 | pub const UFFDIO_COPY_MODE_WP: u64 = 1 << 1; 26 | 27 | pub const UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64 = 1 << 0; 28 | 29 | pub const UFFDIO_API: u32 = 0xc018aa3f; 30 | pub const UFFDIO_REGISTER: u32 = 0xc020aa00; 31 | pub const UFFDIO_UNREGISTER: u32 = 0x8010aa01; 32 | pub const UFFDIO_WAKE: u32 = 0x8010aa02; 33 | pub const UFFDIO_COPY: u32 = 0xc028aa03; 34 | pub const UFFDIO_ZEROPAGE: u32 = 0xc020aa04; 35 | pub const UFFDIO_WRITEPROTECT: u32 = 0xc018aa06; 36 | 37 | #[cfg(test)] 38 | mod const_tests { 39 | use super::*; 40 | 41 | extern "C" { 42 | static _const_UFFD_API: u64; 43 | static _const_UFFD_API_FEATURES: u64; 44 | static _const_UFFD_API_IOCTLS: u64; 45 | static _const_UFFD_API_RANGE_IOCTLS: u64; 46 | static _const_UFFD_API_RANGE_IOCTLS_BASIC: u64; 47 | static _const_UFFDIO_REGISTER_MODE_MISSING: u64; 48 | static _const_UFFDIO_REGISTER_MODE_WP: u64; 49 | static _const_UFFDIO_COPY_MODE_DONTWAKE: u64; 50 | static _const_UFFDIO_COPY_MODE_WP: u64; 51 | static _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64; 52 | static _const_UFFDIO_API: u32; 53 | static _const_UFFDIO_REGISTER: u32; 54 | static _const_UFFDIO_UNREGISTER: u32; 55 | static _const_UFFDIO_WAKE: u32; 56 | static _const_UFFDIO_COPY: u32; 57 | static _const_UFFDIO_ZEROPAGE: u32; 58 | static _const_UFFDIO_WRITEPROTECT: u32; 59 | } 60 | 61 | #[test] 62 | fn consts_correct() { 63 | unsafe { 64 | assert_eq!(UFFD_API, _const_UFFD_API, "UFFD_API"); 65 | assert_eq!( 66 | UFFD_API_FEATURES, _const_UFFD_API_FEATURES, 67 | "UFFD_API_FEATURES" 68 | ); 69 | assert_eq!(UFFD_API_IOCTLS, _const_UFFD_API_IOCTLS, "UFFD_API_IOCTLS"); 70 | assert_eq!( 71 | UFFD_API_RANGE_IOCTLS, _const_UFFD_API_RANGE_IOCTLS, 72 | "UFFD_API_RANGE_IOCTLS" 73 | ); 74 | assert_eq!( 75 | UFFD_API_RANGE_IOCTLS_BASIC, _const_UFFD_API_RANGE_IOCTLS_BASIC, 76 | "UFFD_API_RANGE_IOCTLS_BASIC" 77 | ); 78 | assert_eq!( 79 | UFFDIO_REGISTER_MODE_MISSING, _const_UFFDIO_REGISTER_MODE_MISSING, 80 | "UFFDIO_REGISTER_MODE_MISSING" 81 | ); 82 | assert_eq!( 83 | UFFDIO_REGISTER_MODE_WP, _const_UFFDIO_REGISTER_MODE_WP, 84 | "UFFDIO_REGISTER_MODE_WP" 85 | ); 86 | assert_eq!( 87 | UFFDIO_COPY_MODE_DONTWAKE, _const_UFFDIO_COPY_MODE_DONTWAKE, 88 | "UFFDIO_COPY_MODE_DONTWAKE" 89 | ); 90 | assert_eq!( 91 | UFFDIO_COPY_MODE_WP, _const_UFFDIO_COPY_MODE_WP, 92 | "UFFDIO_COPY_MODE_WP" 93 | ); 94 | assert_eq!( 95 | UFFDIO_ZEROPAGE_MODE_DONTWAKE, _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE, 96 | "UFFDIO_ZEROPAGE_MODE_DONTWAKE" 97 | ); 98 | assert_eq!(UFFDIO_API, _const_UFFDIO_API, "UFFDIO_API"); 99 | assert_eq!(UFFDIO_REGISTER, _const_UFFDIO_REGISTER, "UFFDIO_REGISTER"); 100 | assert_eq!( 101 | UFFDIO_UNREGISTER, _const_UFFDIO_UNREGISTER, 102 | "UFFDIO_UNREGISTER" 103 | ); 104 | assert_eq!(UFFDIO_WAKE, _const_UFFDIO_WAKE, "UFFDIO_WAKE"); 105 | assert_eq!(UFFDIO_COPY, _const_UFFDIO_COPY, "UFFDIO_COPY"); 106 | assert_eq!(UFFDIO_ZEROPAGE, _const_UFFDIO_ZEROPAGE, "UFFDIO_ZEROPAGE"); 107 | assert_eq!( 108 | UFFDIO_WRITEPROTECT, _const_UFFDIO_WRITEPROTECT, 109 | "UFFDIO_WRITEPROTECT" 110 | ); 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /userfaultfd-sys/src/linux5_7.rs: -------------------------------------------------------------------------------- 1 | use super::*; 2 | 3 | // The following are preprocessor constants that bindgen can't figure out, so we enter them manually 4 | // from , and have tests to make sure they're accurate. 5 | 6 | pub const UFFD_API: u64 = 0xAA; 7 | 8 | pub const UFFD_API_FEATURES: u64 = UFFD_FEATURE_PAGEFAULT_FLAG_WP 9 | | UFFD_FEATURE_EVENT_FORK 10 | | UFFD_FEATURE_EVENT_REMAP 11 | | UFFD_FEATURE_EVENT_REMOVE 12 | | UFFD_FEATURE_EVENT_UNMAP 13 | | UFFD_FEATURE_MISSING_HUGETLBFS 14 | | UFFD_FEATURE_MISSING_SHMEM 15 | | UFFD_FEATURE_SIGBUS 16 | | UFFD_FEATURE_THREAD_ID; 17 | pub const UFFD_API_IOCTLS: u64 = 1 << _UFFDIO_REGISTER | 1 << _UFFDIO_UNREGISTER | 1 << _UFFDIO_API; 18 | 19 | pub const UFFD_API_RANGE_IOCTLS: u64 = 20 | 1 << _UFFDIO_WAKE | 1 << _UFFDIO_COPY | 1 << _UFFDIO_ZEROPAGE | 1 << _UFFDIO_WRITEPROTECT; 21 | 22 | pub const UFFD_API_RANGE_IOCTLS_BASIC: u64 = 1 << _UFFDIO_WAKE | 1 << _UFFDIO_COPY; 23 | 24 | pub const UFFDIO_REGISTER_MODE_MISSING: u64 = 1 << 0; 25 | pub const UFFDIO_REGISTER_MODE_WP: u64 = 1 << 1; 26 | pub const UFFDIO_COPY_MODE_DONTWAKE: u64 = 1 << 0; 27 | pub const UFFDIO_COPY_MODE_WP: u64 = 1 << 1; 28 | pub const UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64 = 1 << 0; 29 | pub const UFFDIO_WRITEPROTECT_MODE_WP: u64 = 1 << 0; 30 | pub const UFFDIO_WRITEPROTECT_MODE_DONTWAKE: u64 = 1 << 1; 31 | 32 | pub const UFFDIO_API: u32 = 0xc018aa3f; 33 | pub const UFFDIO_REGISTER: u32 = 0xc020aa00; 34 | pub const UFFDIO_UNREGISTER: u32 = 0x8010aa01; 35 | pub const UFFDIO_WAKE: u32 = 0x8010aa02; 36 | pub const UFFDIO_COPY: u32 = 0xc028aa03; 37 | pub const UFFDIO_ZEROPAGE: u32 = 0xc020aa04; 38 | pub const UFFDIO_WRITEPROTECT: u32 = 0xc018aa06; 39 | 40 | #[cfg(test)] 41 | mod const_tests { 42 | use super::*; 43 | 44 | extern "C" { 45 | static _const_UFFD_API: u64; 46 | static _const_UFFD_API_FEATURES: u64; 47 | static _const_UFFD_API_IOCTLS: u64; 48 | static _const_UFFD_API_RANGE_IOCTLS: u64; 49 | static _const_UFFD_API_RANGE_IOCTLS_BASIC: u64; 50 | static _const_UFFDIO_REGISTER_MODE_MISSING: u64; 51 | static _const_UFFDIO_REGISTER_MODE_WP: u64; 52 | static _const_UFFDIO_COPY_MODE_DONTWAKE: u64; 53 | static _const_UFFDIO_COPY_MODE_WP: u64; 54 | static _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE: u64; 55 | static _const_UFFDIO_API: u32; 56 | static _const_UFFDIO_REGISTER: u32; 57 | static _const_UFFDIO_UNREGISTER: u32; 58 | static _const_UFFDIO_WAKE: u32; 59 | static _const_UFFDIO_COPY: u32; 60 | static _const_UFFDIO_ZEROPAGE: u32; 61 | static _const_UFFDIO_WRITEPROTECT: u32; 62 | } 63 | 64 | #[test] 65 | fn consts_correct() { 66 | unsafe { 67 | assert_eq!(UFFD_API, _const_UFFD_API, "UFFD_API"); 68 | assert_eq!( 69 | UFFD_API_FEATURES, _const_UFFD_API_FEATURES, 70 | "UFFD_API_FEATURES" 71 | ); 72 | assert_eq!(UFFD_API_IOCTLS, _const_UFFD_API_IOCTLS, "UFFD_API_IOCTLS"); 73 | assert_eq!( 74 | UFFD_API_RANGE_IOCTLS, _const_UFFD_API_RANGE_IOCTLS, 75 | "UFFD_API_RANGE_IOCTLS" 76 | ); 77 | assert_eq!( 78 | UFFD_API_RANGE_IOCTLS_BASIC, _const_UFFD_API_RANGE_IOCTLS_BASIC, 79 | "UFFD_API_RANGE_IOCTLS_BASIC" 80 | ); 81 | assert_eq!( 82 | UFFDIO_REGISTER_MODE_MISSING, _const_UFFDIO_REGISTER_MODE_MISSING, 83 | "UFFDIO_REGISTER_MODE_MISSING" 84 | ); 85 | assert_eq!( 86 | UFFDIO_REGISTER_MODE_WP, _const_UFFDIO_REGISTER_MODE_WP, 87 | "UFFDIO_REGISTER_MODE_WP" 88 | ); 89 | assert_eq!( 90 | UFFDIO_COPY_MODE_DONTWAKE, _const_UFFDIO_COPY_MODE_DONTWAKE, 91 | "UFFDIO_COPY_MODE_DONTWAKE" 92 | ); 93 | assert_eq!( 94 | UFFDIO_COPY_MODE_WP, _const_UFFDIO_COPY_MODE_WP, 95 | "UFFDIO_COPY_MODE_WP" 96 | ); 97 | assert_eq!( 98 | UFFDIO_ZEROPAGE_MODE_DONTWAKE, _const_UFFDIO_ZEROPAGE_MODE_DONTWAKE, 99 | "UFFDIO_ZEROPAGE_MODE_DONTWAKE" 100 | ); 101 | assert_eq!(UFFDIO_API, _const_UFFDIO_API, "UFFDIO_API"); 102 | assert_eq!(UFFDIO_REGISTER, _const_UFFDIO_REGISTER, "UFFDIO_REGISTER"); 103 | assert_eq!( 104 | UFFDIO_UNREGISTER, _const_UFFDIO_UNREGISTER, 105 | "UFFDIO_UNREGISTER" 106 | ); 107 | assert_eq!(UFFDIO_WAKE, _const_UFFDIO_WAKE, "UFFDIO_WAKE"); 108 | assert_eq!(UFFDIO_COPY, _const_UFFDIO_COPY, "UFFDIO_COPY"); 109 | assert_eq!(UFFDIO_ZEROPAGE, _const_UFFDIO_ZEROPAGE, "UFFDIO_ZEROPAGE"); 110 | assert_eq!( 111 | UFFDIO_WRITEPROTECT, _const_UFFDIO_WRITEPROTECT, 112 | "UFFDIO_WRITEPROTECT" 113 | ); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /userfaultfd-sys/wrapper.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef UFFD_USER_MODE_ONLY 3 | // this definition is not available before Linux 5.11. It is provided so 4 | // userfaultfd-sys has the same exports on all kernels 5 | #define UFFD_USER_MODE_ONLY 1 6 | #endif 7 | 8 | 9 | #ifndef USERFAULTFD_IOC 10 | // Similarly, the ioctl() for `/dev/userfaultfd` is introduced with Linux 6.1. 11 | #define USERFAULTFD_IOC 0xAA 12 | #endif 13 | --------------------------------------------------------------------------------