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 #[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_t50 pub unsafe fn vmm_alloc_physical(
51 aspace: *mut vmm_aspace_t,
52 name: *const c_char,
53 size: usize,
54 ptr: *const *mut c_void,
55 align_log2: u8,
56 paddr: paddr_t,
57 vmm_flags: c_uint,
58 arch_mmu_flags: c_uint,
59 ) -> status_t {
60 // SAFETY: Delegated.
61 unsafe {
62 vmm_alloc_physical_etc(
63 aspace,
64 name,
65 size,
66 ptr.cast_mut(),
67 align_log2,
68 addr_of!(paddr),
69 1,
70 vmm_flags,
71 arch_mmu_flags,
72 )
73 }
74 }
75