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