1 // Copyright 2024, Linaro Limited 2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 #![cfg_attr(not(MESON), doc = include_str!("../README.md"))] 6 #![deny(clippy::missing_const_for_fn)] 7 8 #[rustfmt::skip] 9 pub mod bindings; 10 11 // preserve one-item-per-"use" syntax, it is clearer 12 // for prelude-like modules 13 #[rustfmt::skip] 14 pub mod prelude; 15 16 pub mod assertions; 17 pub mod bitops; 18 pub mod callbacks; 19 pub mod cell; 20 pub mod chardev; 21 pub mod errno; 22 pub mod error; 23 pub mod irq; 24 pub mod memory; 25 pub mod module; 26 pub mod qdev; 27 pub mod qom; 28 pub mod sysbus; 29 pub mod timer; 30 pub mod uninit; 31 pub mod vmstate; 32 pub mod zeroable; 33 34 use std::{ 35 alloc::{GlobalAlloc, Layout}, 36 ffi::c_void, 37 }; 38 39 pub use error::{Error, Result}; 40 41 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)] 42 extern "C" { 43 fn g_aligned_alloc0( 44 n_blocks: bindings::gsize, 45 n_block_bytes: bindings::gsize, 46 alignment: bindings::gsize, 47 ) -> bindings::gpointer; 48 fn g_aligned_free(mem: bindings::gpointer); 49 } 50 51 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))] 52 extern "C" { 53 fn qemu_memalign(alignment: usize, size: usize) -> *mut c_void; 54 fn qemu_vfree(ptr: *mut c_void); 55 } 56 57 extern "C" { 58 fn g_malloc0(n_bytes: bindings::gsize) -> bindings::gpointer; 59 fn g_free(mem: bindings::gpointer); 60 } 61 62 /// An allocator that uses the same allocator as QEMU in C. 63 /// 64 /// It is enabled by default with the `allocator` feature. 65 /// 66 /// To set it up manually as a global allocator in your crate: 67 /// 68 /// ```ignore 69 /// use qemu_api::QemuAllocator; 70 /// 71 /// #[global_allocator] 72 /// static GLOBAL: QemuAllocator = QemuAllocator::new(); 73 /// ``` 74 #[derive(Clone, Copy, Debug)] 75 #[repr(C)] 76 pub struct QemuAllocator { 77 _unused: [u8; 0], 78 } 79 80 #[cfg_attr(all(feature = "allocator", not(test)), global_allocator)] 81 pub static GLOBAL: QemuAllocator = QemuAllocator::new(); 82 83 impl QemuAllocator { 84 // From the glibc documentation, on GNU systems, malloc guarantees 16-byte 85 // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See 86 // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html. 87 // This alignment guarantee also applies to Windows and Android. On Darwin 88 // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems. 89 #[cfg(all( 90 target_pointer_width = "32", 91 not(any(target_os = "macos", target_os = "openbsd")) 92 ))] 93 pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(8); 94 #[cfg(all( 95 target_pointer_width = "64", 96 not(any(target_os = "macos", target_os = "openbsd")) 97 ))] 98 pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(16); 99 #[cfg(all( 100 any(target_pointer_width = "32", target_pointer_width = "64"), 101 any(target_os = "macos", target_os = "openbsd") 102 ))] 103 pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = Some(16); 104 #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))] 105 pub const DEFAULT_ALIGNMENT_BYTES: Option<usize> = None; 106 107 pub const fn new() -> Self { 108 Self { _unused: [] } 109 } 110 } 111 112 impl Default for QemuAllocator { 113 fn default() -> Self { 114 Self::new() 115 } 116 } 117 118 // Sanity check. 119 const _: [(); 8] = [(); ::core::mem::size_of::<*mut c_void>()]; 120 121 unsafe impl GlobalAlloc for QemuAllocator { 122 unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 123 if matches!(Self::DEFAULT_ALIGNMENT_BYTES, Some(default) if default.checked_rem(layout.align()) == Some(0)) 124 { 125 // SAFETY: g_malloc0() is safe to call. 126 unsafe { g_malloc0(layout.size().try_into().unwrap()).cast::<u8>() } 127 } else { 128 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)] 129 { 130 // SAFETY: g_aligned_alloc0() is safe to call. 131 unsafe { 132 g_aligned_alloc0( 133 layout.size().try_into().unwrap(), 134 1, 135 layout.align().try_into().unwrap(), 136 ) 137 .cast::<u8>() 138 } 139 } 140 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))] 141 { 142 // SAFETY: qemu_memalign() is safe to call. 143 unsafe { qemu_memalign(layout.align(), layout.size()).cast::<u8>() } 144 } 145 } 146 } 147 148 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { 149 if matches!(Self::DEFAULT_ALIGNMENT_BYTES, Some(default) if default.checked_rem(layout.align()) == Some(0)) 150 { 151 // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid 152 // glib-allocated pointer, so `g_free`ing is safe. 153 unsafe { g_free(ptr.cast::<_>()) } 154 } else { 155 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)] 156 { 157 // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid aligned 158 // glib-allocated pointer, so `g_aligned_free`ing is safe. 159 unsafe { g_aligned_free(ptr.cast::<_>()) } 160 } 161 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))] 162 { 163 // SAFETY: `ptr` must have been allocated by Self::alloc thus a valid aligned 164 // glib-allocated pointer, so `qemu_vfree`ing is safe. 165 unsafe { qemu_vfree(ptr.cast::<_>()) } 166 } 167 } 168 } 169 } 170