1 /*
2 * Copyright (c) 2024 Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 use core::ffi::c_char;
25 use core::ffi::c_uint;
26 use core::ffi::c_void;
27 use core::ptr::{addr_of, addr_of_mut};
28
29 use crate::paddr_t;
30 use crate::status_t;
31
32 pub use crate::sys::vaddr_to_paddr;
33 pub use crate::sys::vmm_alloc_contiguous;
34 pub use crate::sys::vmm_alloc_physical_etc;
35 pub use crate::sys::vmm_aspace_t;
36 pub use crate::sys::vmm_free_region;
37
38 #[inline]
vmm_get_kernel_aspace() -> *mut vmm_aspace_t39 pub fn vmm_get_kernel_aspace() -> *mut vmm_aspace_t {
40 // SAFETY: The returned raw pointer holds the same safety invariants as accessing a `static mut`,
41 // so this `unsafe` is unconditionally sound, and may become safe in edition 2024:
42 // <https://github.com/rust-lang/rust/issues/114447>.
43 unsafe { addr_of_mut!(crate::sys::_kernel_aspace) }
44 }
45
46 /// # Safety
47 ///
48 /// Same as [`vmm_alloc_physical_etc`].
49 #[allow(clippy::too_many_arguments)]
50 #[inline]
vmm_alloc_physical( aspace: *mut vmm_aspace_t, name: *const c_char, size: usize, ptr: *const *mut c_void, align_log2: u8, paddr: paddr_t, vmm_flags: c_uint, arch_mmu_flags: c_uint, ) -> status_t51 pub unsafe fn vmm_alloc_physical(
52 aspace: *mut vmm_aspace_t,
53 name: *const c_char,
54 size: usize,
55 ptr: *const *mut c_void,
56 align_log2: u8,
57 paddr: paddr_t,
58 vmm_flags: c_uint,
59 arch_mmu_flags: c_uint,
60 ) -> status_t {
61 // SAFETY: Delegated.
62 unsafe {
63 vmm_alloc_physical_etc(
64 aspace,
65 name,
66 size,
67 ptr.cast_mut(),
68 align_log2,
69 addr_of!(paddr),
70 1,
71 vmm_flags,
72 arch_mmu_flags,
73 )
74 }
75 }
76