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