xref: /cloud-hypervisor/vm-allocator/src/page_size.rs (revision 3ce0fef7fd546467398c914dbc74d8542e45cf6f)
1 // Copyright 2023 Arm Limited (or its affiliates). All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3 
4 use libc::{sysconf, _SC_PAGESIZE};
5 
6 /// get host page size
7 pub fn get_page_size() -> u64 {
8     // SAFETY: FFI call. Trivially safe.
9     unsafe { sysconf(_SC_PAGESIZE) as u64 }
10 }
11 
12 /// round up address to let it align page size
13 pub fn align_page_size_up(address: u64) -> u64 {
14     let page_size = get_page_size();
15     (address + page_size - 1) & !(page_size - 1)
16 }
17 
18 /// round down address to let it align page size
19 pub fn align_page_size_down(address: u64) -> u64 {
20     let page_size = get_page_size();
21     address & !(page_size - 1)
22 }
23 
24 /// Test if address is 4k aligned
25 pub fn is_4k_aligned(address: u64) -> bool {
26     (address & 0xfff) == 0
27 }
28 
29 /// Test if size is 4k aligned
30 pub fn is_4k_multiple(size: u64) -> bool {
31     (size & 0xfff) == 0
32 }
33 
34 /// Test if address is page size aligned
35 pub fn is_page_size_aligned(address: u64) -> bool {
36     let page_size = get_page_size();
37     address & (page_size - 1) == 0
38 }
39