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