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