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