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