xref: /openbmc/qemu/rust/qemu-api/src/lib.rs (revision cde3c425)
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 
7 #[allow(
8     dead_code,
9     improper_ctypes_definitions,
10     improper_ctypes,
11     non_camel_case_types,
12     non_snake_case,
13     non_upper_case_globals,
14     unsafe_op_in_unsafe_fn,
15     clippy::missing_const_for_fn,
16     clippy::too_many_arguments,
17     clippy::approx_constant,
18     clippy::use_self,
19     clippy::useless_transmute,
20     clippy::missing_safety_doc,
21 )]
22 #[rustfmt::skip]
23 pub mod bindings;
24 
25 unsafe impl Send for bindings::Property {}
26 unsafe impl Sync for bindings::Property {}
27 unsafe impl Sync for bindings::TypeInfo {}
28 unsafe impl Sync for bindings::VMStateDescription {}
29 
30 pub mod definitions;
31 pub mod device_class;
32 
33 use std::alloc::{GlobalAlloc, Layout};
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 ::core::ffi::c_void;
48     fn qemu_vfree(ptr: *mut ::core::ffi::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 ::core::ffi::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