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