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