xref: /openbmc/qemu/rust/qemu-api/src/lib.rs (revision 9f7d4520)
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 definitions;
33 pub mod device_class;
34 pub mod vmstate;
35 pub mod zeroable;
36 
37 use std::{
38     alloc::{GlobalAlloc, Layout},
39     os::raw::c_void,
40 };
41 
42 #[cfg(HAVE_GLIB_WITH_ALIGNED_ALLOC)]
43 extern "C" {
44     fn g_aligned_alloc0(
45         n_blocks: bindings::gsize,
46         n_block_bytes: bindings::gsize,
47         alignment: bindings::gsize,
48     ) -> bindings::gpointer;
49     fn g_aligned_free(mem: bindings::gpointer);
50 }
51 
52 #[cfg(not(HAVE_GLIB_WITH_ALIGNED_ALLOC))]
53 extern "C" {
54     fn qemu_memalign(alignment: usize, size: usize) -> *mut c_void;
55     fn qemu_vfree(ptr: *mut c_void);
56 }
57 
58 extern "C" {
59     fn g_malloc0(n_bytes: bindings::gsize) -> 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 
108     pub const fn new() -> Self {
109         Self { _unused: [] }
110     }
111 }
112 
113 impl Default for QemuAllocator {
114     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 {
123     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 
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