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