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