1 // SPDX-License-Identifier: MIT
2 //
3 // This file is based on library/core/src/cell.rs from
4 // Rust 1.82.0.
5 //
6 // Permission is hereby granted, free of charge, to any
7 // person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the
9 // Software without restriction, including without
10 // limitation the rights to use, copy, modify, merge,
11 // publish, distribute, sublicense, and/or sell copies of
12 // the Software, and to permit persons to whom the Software
13 // is furnished to do so, subject to the following
14 // conditions:
15 //
16 // The above copyright notice and this permission notice
17 // shall be included in all copies or substantial portions
18 // of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
21 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
22 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
24 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 // DEALINGS IN THE SOFTWARE.
29
30 //! QEMU-specific mutable containers
31 //!
32 //! Rust memory safety is based on this rule: Given an object `T`, it is only
33 //! possible to have one of the following:
34 //!
35 //! - Having several immutable references (`&T`) to the object (also known as
36 //! **aliasing**).
37 //! - Having one mutable reference (`&mut T`) to the object (also known as
38 //! **mutability**).
39 //!
40 //! This is enforced by the Rust compiler. However, there are situations where
41 //! this rule is not flexible enough. Sometimes it is required to have multiple
42 //! references to an object and yet mutate it. In particular, QEMU objects
43 //! usually have their pointer shared with the "outside world very early in
44 //! their lifetime", for example when they create their
45 //! [`MemoryRegion`s](crate::bindings::MemoryRegion). Therefore, individual
46 //! parts of a device must be made mutable in a controlled manner; this module
47 //! provides the tools to do so.
48 //!
49 //! ## Cell types
50 //!
51 //! [`BqlCell<T>`] and [`BqlRefCell<T>`] allow doing this via the Big QEMU Lock.
52 //! While they are essentially the same single-threaded primitives that are
53 //! available in `std::cell`, the BQL allows them to be used from a
54 //! multi-threaded context and to share references across threads, while
55 //! maintaining Rust's safety guarantees. For this reason, unlike
56 //! their `std::cell` counterparts, `BqlCell` and `BqlRefCell` implement the
57 //! `Sync` trait.
58 //!
59 //! BQL checks are performed in debug builds but can be optimized away in
60 //! release builds, providing runtime safety during development with no overhead
61 //! in production.
62 //!
63 //! The two provide different ways of handling interior mutability.
64 //! `BqlRefCell` is best suited for data that is primarily accessed by the
65 //! device's own methods, where multiple reads and writes can be grouped within
66 //! a single borrow and a mutable reference can be passed around. Instead,
67 //! [`BqlCell`] is a better choice when sharing small pieces of data with
68 //! external code (especially C code), because it provides simple get/set
69 //! operations that can be used one at a time.
70 //!
71 //! Warning: While `BqlCell` and `BqlRefCell` are similar to their `std::cell`
72 //! counterparts, they are not interchangeable. Using `std::cell` types in
73 //! QEMU device implementations is usually incorrect and can lead to
74 //! thread-safety issues.
75 //!
76 //! ### Example
77 //!
78 //! ```
79 //! # use qemu_api::prelude::*;
80 //! # use qemu_api::{c_str, cell::BqlRefCell, irq::InterruptSource, irq::IRQState};
81 //! # use qemu_api::{sysbus::SysBusDevice, qom::Owned, qom::ParentField};
82 //! # const N_GPIOS: usize = 8;
83 //! # struct PL061Registers { /* ... */ }
84 //! # unsafe impl ObjectType for PL061State {
85 //! # type Class = <SysBusDevice as ObjectType>::Class;
86 //! # const TYPE_NAME: &'static std::ffi::CStr = c_str!("pl061");
87 //! # }
88 //! struct PL061State {
89 //! parent_obj: ParentField<SysBusDevice>,
90 //!
91 //! // Configuration is read-only after initialization
92 //! pullups: u32,
93 //! pulldowns: u32,
94 //!
95 //! // Single values shared with C code use BqlCell, in this case via InterruptSource
96 //! out: [InterruptSource; N_GPIOS],
97 //! interrupt: InterruptSource,
98 //!
99 //! // Larger state accessed by device methods uses BqlRefCell or Mutex
100 //! registers: BqlRefCell<PL061Registers>,
101 //! }
102 //! ```
103 //!
104 //! ### `BqlCell<T>`
105 //!
106 //! [`BqlCell<T>`] implements interior mutability by moving values in and out of
107 //! the cell. That is, an `&mut T` to the inner value can never be obtained as
108 //! long as the cell is shared. The value itself cannot be directly obtained
109 //! without copying it, cloning it, or replacing it with something else. This
110 //! type provides the following methods, all of which can be called only while
111 //! the BQL is held:
112 //!
113 //! - For types that implement [`Copy`], the [`get`](BqlCell::get) method
114 //! retrieves the current interior value by duplicating it.
115 //! - For types that implement [`Default`], the [`take`](BqlCell::take) method
116 //! replaces the current interior value with [`Default::default()`] and
117 //! returns the replaced value.
118 //! - All types have:
119 //! - [`replace`](BqlCell::replace): replaces the current interior value and
120 //! returns the replaced value.
121 //! - [`set`](BqlCell::set): this method replaces the interior value,
122 //! dropping the replaced value.
123 //!
124 //! ### `BqlRefCell<T>`
125 //!
126 //! [`BqlRefCell<T>`] uses Rust's lifetimes to implement "dynamic borrowing", a
127 //! process whereby one can claim temporary, exclusive, mutable access to the
128 //! inner value:
129 //!
130 //! ```ignore
131 //! fn clear_interrupts(&self, val: u32) {
132 //! // A mutable borrow gives read-write access to the registers
133 //! let mut regs = self.registers.borrow_mut();
134 //! let old = regs.interrupt_status();
135 //! regs.update_interrupt_status(old & !val);
136 //! }
137 //! ```
138 //!
139 //! Borrows for `BqlRefCell<T>`s are tracked at _runtime_, unlike Rust's native
140 //! reference types which are entirely tracked statically, at compile time.
141 //! Multiple immutable borrows are allowed via [`borrow`](BqlRefCell::borrow),
142 //! or a single mutable borrow via [`borrow_mut`](BqlRefCell::borrow_mut). The
143 //! thread will panic if these rules are violated or if the BQL is not held.
144 //!
145 //! ## Opaque wrappers
146 //!
147 //! The cell types from the previous section are useful at the boundaries
148 //! of code that requires interior mutability. When writing glue code that
149 //! interacts directly with C structs, however, it is useful to operate
150 //! at a lower level.
151 //!
152 //! C functions often violate Rust's fundamental assumptions about memory
153 //! safety by modifying memory even if it is shared. Furthermore, C structs
154 //! often start their life uninitialized and may be populated lazily.
155 //!
156 //! For this reason, this module provides the [`Opaque<T>`] type to opt out
157 //! of Rust's usual guarantees about the wrapped type. Access to the wrapped
158 //! value is always through raw pointers, obtained via methods like
159 //! [`as_mut_ptr()`](Opaque::as_mut_ptr) and [`as_ptr()`](Opaque::as_ptr). These
160 //! pointers can then be passed to C functions or dereferenced; both actions
161 //! require `unsafe` blocks, making it clear where safety guarantees must be
162 //! manually verified. For example
163 //!
164 //! ```ignore
165 //! unsafe {
166 //! let state = Opaque::<MyStruct>::uninit();
167 //! qemu_struct_init(state.as_mut_ptr());
168 //! }
169 //! ```
170 //!
171 //! [`Opaque<T>`] will usually be wrapped one level further, so that
172 //! bridge methods can be added to the wrapper:
173 //!
174 //! ```ignore
175 //! pub struct MyStruct(Opaque<bindings::MyStruct>);
176 //!
177 //! impl MyStruct {
178 //! fn new() -> Pin<Box<MyStruct>> {
179 //! let result = Box::pin(unsafe { Opaque::uninit() });
180 //! unsafe { qemu_struct_init(result.as_mut_ptr()) };
181 //! result
182 //! }
183 //! }
184 //! ```
185 //!
186 //! This pattern of wrapping bindgen-generated types in [`Opaque<T>`] provides
187 //! several advantages:
188 //!
189 //! * The choice of traits to be implemented is not limited by the
190 //! bindgen-generated code. For example, [`Drop`] can be added without
191 //! disabling [`Copy`] on the underlying bindgen type
192 //!
193 //! * [`Send`] and [`Sync`] implementations can be controlled by the wrapper
194 //! type rather than being automatically derived from the C struct's layout
195 //!
196 //! * Methods can be implemented in a separate crate from the bindgen-generated
197 //! bindings
198 //!
199 //! * [`Debug`](std::fmt::Debug) and [`Display`](std::fmt::Display)
200 //! implementations can be customized to be more readable than the raw C
201 //! struct representation
202 //!
203 //! The [`Opaque<T>`] type does not include BQL validation; it is possible to
204 //! assert in the code that the right lock is taken, to use it together
205 //! with a custom lock guard type, or to let C code take the lock, as
206 //! appropriate. It is also possible to use it with non-thread-safe
207 //! types, since by default (unlike [`BqlCell`] and [`BqlRefCell`]
208 //! it is neither `Sync` nor `Send`.
209 //!
210 //! While [`Opaque<T>`] is necessary for C interop, it should be used sparingly
211 //! and only at FFI boundaries. For QEMU-specific types that need interior
212 //! mutability, prefer [`BqlCell`] or [`BqlRefCell`].
213
214 use std::{
215 cell::{Cell, UnsafeCell},
216 cmp::Ordering,
217 fmt,
218 marker::{PhantomData, PhantomPinned},
219 mem::{self, MaybeUninit},
220 ops::{Deref, DerefMut},
221 ptr::NonNull,
222 };
223
224 use crate::bindings;
225
226 /// An internal function that is used by doctests.
bql_start_test()227 pub fn bql_start_test() {
228 if cfg!(MESON) {
229 // SAFETY: integration tests are run with --test-threads=1, while
230 // unit tests and doctests are not multithreaded and do not have
231 // any BQL-protected data. Just set bql_locked to true.
232 unsafe {
233 bindings::rust_bql_mock_lock();
234 }
235 }
236 }
237
bql_locked() -> bool238 pub fn bql_locked() -> bool {
239 // SAFETY: the function does nothing but return a thread-local bool
240 !cfg!(MESON) || unsafe { bindings::bql_locked() }
241 }
242
bql_block_unlock(increase: bool)243 fn bql_block_unlock(increase: bool) {
244 if cfg!(MESON) {
245 // SAFETY: this only adjusts a counter
246 unsafe {
247 bindings::bql_block_unlock(increase);
248 }
249 }
250 }
251
252 /// A mutable memory location that is protected by the Big QEMU Lock.
253 ///
254 /// # Memory layout
255 ///
256 /// `BqlCell<T>` has the same in-memory representation as its inner type `T`.
257 #[repr(transparent)]
258 pub struct BqlCell<T> {
259 value: UnsafeCell<T>,
260 }
261
262 // SAFETY: Same as for std::sync::Mutex. In the end this *is* a Mutex,
263 // except it is stored out-of-line
264 unsafe impl<T: Send> Send for BqlCell<T> {}
265 unsafe impl<T: Send> Sync for BqlCell<T> {}
266
267 impl<T: Copy> Clone for BqlCell<T> {
268 #[inline]
clone(&self) -> BqlCell<T>269 fn clone(&self) -> BqlCell<T> {
270 BqlCell::new(self.get())
271 }
272 }
273
274 impl<T: Default> Default for BqlCell<T> {
275 /// Creates a `BqlCell<T>`, with the `Default` value for T.
276 #[inline]
default() -> BqlCell<T>277 fn default() -> BqlCell<T> {
278 BqlCell::new(Default::default())
279 }
280 }
281
282 impl<T: PartialEq + Copy> PartialEq for BqlCell<T> {
283 #[inline]
eq(&self, other: &BqlCell<T>) -> bool284 fn eq(&self, other: &BqlCell<T>) -> bool {
285 self.get() == other.get()
286 }
287 }
288
289 impl<T: Eq + Copy> Eq for BqlCell<T> {}
290
291 impl<T: PartialOrd + Copy> PartialOrd for BqlCell<T> {
292 #[inline]
partial_cmp(&self, other: &BqlCell<T>) -> Option<Ordering>293 fn partial_cmp(&self, other: &BqlCell<T>) -> Option<Ordering> {
294 self.get().partial_cmp(&other.get())
295 }
296 }
297
298 impl<T: Ord + Copy> Ord for BqlCell<T> {
299 #[inline]
cmp(&self, other: &BqlCell<T>) -> Ordering300 fn cmp(&self, other: &BqlCell<T>) -> Ordering {
301 self.get().cmp(&other.get())
302 }
303 }
304
305 impl<T> From<T> for BqlCell<T> {
306 /// Creates a new `BqlCell<T>` containing the given value.
from(t: T) -> BqlCell<T>307 fn from(t: T) -> BqlCell<T> {
308 BqlCell::new(t)
309 }
310 }
311
312 impl<T: fmt::Debug + Copy> fmt::Debug for BqlCell<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result313 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314 self.get().fmt(f)
315 }
316 }
317
318 impl<T: fmt::Display + Copy> fmt::Display for BqlCell<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result319 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
320 self.get().fmt(f)
321 }
322 }
323
324 impl<T> BqlCell<T> {
325 /// Creates a new `BqlCell` containing the given value.
326 ///
327 /// # Examples
328 ///
329 /// ```
330 /// use qemu_api::cell::BqlCell;
331 /// # qemu_api::cell::bql_start_test();
332 ///
333 /// let c = BqlCell::new(5);
334 /// ```
335 #[inline]
new(value: T) -> BqlCell<T>336 pub const fn new(value: T) -> BqlCell<T> {
337 BqlCell {
338 value: UnsafeCell::new(value),
339 }
340 }
341
342 /// Sets the contained value.
343 ///
344 /// # Examples
345 ///
346 /// ```
347 /// use qemu_api::cell::BqlCell;
348 /// # qemu_api::cell::bql_start_test();
349 ///
350 /// let c = BqlCell::new(5);
351 ///
352 /// c.set(10);
353 /// ```
354 #[inline]
set(&self, val: T)355 pub fn set(&self, val: T) {
356 self.replace(val);
357 }
358
359 /// Replaces the contained value with `val`, and returns the old contained
360 /// value.
361 ///
362 /// # Examples
363 ///
364 /// ```
365 /// use qemu_api::cell::BqlCell;
366 /// # qemu_api::cell::bql_start_test();
367 ///
368 /// let cell = BqlCell::new(5);
369 /// assert_eq!(cell.get(), 5);
370 /// assert_eq!(cell.replace(10), 5);
371 /// assert_eq!(cell.get(), 10);
372 /// ```
373 #[inline]
replace(&self, val: T) -> T374 pub fn replace(&self, val: T) -> T {
375 assert!(bql_locked());
376 // SAFETY: This can cause data races if called from multiple threads,
377 // but it won't happen as long as C code accesses the value
378 // under BQL protection only.
379 mem::replace(unsafe { &mut *self.value.get() }, val)
380 }
381
382 /// Unwraps the value, consuming the cell.
383 ///
384 /// # Examples
385 ///
386 /// ```
387 /// use qemu_api::cell::BqlCell;
388 /// # qemu_api::cell::bql_start_test();
389 ///
390 /// let c = BqlCell::new(5);
391 /// let five = c.into_inner();
392 ///
393 /// assert_eq!(five, 5);
394 /// ```
into_inner(self) -> T395 pub fn into_inner(self) -> T {
396 assert!(bql_locked());
397 self.value.into_inner()
398 }
399 }
400
401 impl<T: Copy> BqlCell<T> {
402 /// Returns a copy of the contained value.
403 ///
404 /// # Examples
405 ///
406 /// ```
407 /// use qemu_api::cell::BqlCell;
408 /// # qemu_api::cell::bql_start_test();
409 ///
410 /// let c = BqlCell::new(5);
411 ///
412 /// let five = c.get();
413 /// ```
414 #[inline]
get(&self) -> T415 pub fn get(&self) -> T {
416 assert!(bql_locked());
417 // SAFETY: This can cause data races if called from multiple threads,
418 // but it won't happen as long as C code accesses the value
419 // under BQL protection only.
420 unsafe { *self.value.get() }
421 }
422 }
423
424 impl<T> BqlCell<T> {
425 /// Returns a raw pointer to the underlying data in this cell.
426 ///
427 /// # Examples
428 ///
429 /// ```
430 /// use qemu_api::cell::BqlCell;
431 /// # qemu_api::cell::bql_start_test();
432 ///
433 /// let c = BqlCell::new(5);
434 ///
435 /// let ptr = c.as_ptr();
436 /// ```
437 #[inline]
as_ptr(&self) -> *mut T438 pub const fn as_ptr(&self) -> *mut T {
439 self.value.get()
440 }
441 }
442
443 impl<T: Default> BqlCell<T> {
444 /// Takes the value of the cell, leaving `Default::default()` in its place.
445 ///
446 /// # Examples
447 ///
448 /// ```
449 /// use qemu_api::cell::BqlCell;
450 /// # qemu_api::cell::bql_start_test();
451 ///
452 /// let c = BqlCell::new(5);
453 /// let five = c.take();
454 ///
455 /// assert_eq!(five, 5);
456 /// assert_eq!(c.into_inner(), 0);
457 /// ```
take(&self) -> T458 pub fn take(&self) -> T {
459 self.replace(Default::default())
460 }
461 }
462
463 /// A mutable memory location with dynamically checked borrow rules,
464 /// protected by the Big QEMU Lock.
465 ///
466 /// See the [module-level documentation](self) for more.
467 ///
468 /// # Memory layout
469 ///
470 /// `BqlRefCell<T>` starts with the same in-memory representation as its
471 /// inner type `T`.
472 #[repr(C)]
473 pub struct BqlRefCell<T> {
474 // It is important that this is the first field (which is not the case
475 // for std::cell::BqlRefCell), so that we can use offset_of! on it.
476 // UnsafeCell and repr(C) both prevent usage of niches.
477 value: UnsafeCell<T>,
478 borrow: Cell<BorrowFlag>,
479 // Stores the location of the earliest currently active borrow.
480 // This gets updated whenever we go from having zero borrows
481 // to having a single borrow. When a borrow occurs, this gets included
482 // in the panic message
483 #[cfg(feature = "debug_cell")]
484 borrowed_at: Cell<Option<&'static std::panic::Location<'static>>>,
485 }
486
487 // Positive values represent the number of `BqlRef` active. Negative values
488 // represent the number of `BqlRefMut` active. Right now QEMU's implementation
489 // does not allow to create `BqlRefMut`s that refer to distinct, nonoverlapping
490 // components of a `BqlRefCell` (e.g., different ranges of a slice).
491 //
492 // `BqlRef` and `BqlRefMut` are both two words in size, and so there will likely
493 // never be enough `BqlRef`s or `BqlRefMut`s in existence to overflow half of
494 // the `usize` range. Thus, a `BorrowFlag` will probably never overflow or
495 // underflow. However, this is not a guarantee, as a pathological program could
496 // repeatedly create and then mem::forget `BqlRef`s or `BqlRefMut`s. Thus, all
497 // code must explicitly check for overflow and underflow in order to avoid
498 // unsafety, or at least behave correctly in the event that overflow or
499 // underflow happens (e.g., see BorrowRef::new).
500 type BorrowFlag = isize;
501 const UNUSED: BorrowFlag = 0;
502
503 #[inline(always)]
is_writing(x: BorrowFlag) -> bool504 const fn is_writing(x: BorrowFlag) -> bool {
505 x < UNUSED
506 }
507
508 #[inline(always)]
is_reading(x: BorrowFlag) -> bool509 const fn is_reading(x: BorrowFlag) -> bool {
510 x > UNUSED
511 }
512
513 impl<T> BqlRefCell<T> {
514 /// Creates a new `BqlRefCell` containing `value`.
515 ///
516 /// # Examples
517 ///
518 /// ```
519 /// use qemu_api::cell::BqlRefCell;
520 ///
521 /// let c = BqlRefCell::new(5);
522 /// ```
523 #[inline]
new(value: T) -> BqlRefCell<T>524 pub const fn new(value: T) -> BqlRefCell<T> {
525 BqlRefCell {
526 value: UnsafeCell::new(value),
527 borrow: Cell::new(UNUSED),
528 #[cfg(feature = "debug_cell")]
529 borrowed_at: Cell::new(None),
530 }
531 }
532 }
533
534 // This ensures the panicking code is outlined from `borrow_mut` for
535 // `BqlRefCell`.
536 #[inline(never)]
537 #[cold]
538 #[cfg(feature = "debug_cell")]
panic_already_borrowed(source: &Cell<Option<&'static std::panic::Location<'static>>>) -> !539 fn panic_already_borrowed(source: &Cell<Option<&'static std::panic::Location<'static>>>) -> ! {
540 // If a borrow occurred, then we must already have an outstanding borrow,
541 // so `borrowed_at` will be `Some`
542 panic!("already borrowed at {:?}", source.take().unwrap())
543 }
544
545 #[inline(never)]
546 #[cold]
547 #[cfg(not(feature = "debug_cell"))]
panic_already_borrowed() -> !548 fn panic_already_borrowed() -> ! {
549 panic!("already borrowed")
550 }
551
552 impl<T> BqlRefCell<T> {
553 #[inline]
554 #[allow(clippy::unused_self)]
panic_already_borrowed(&self) -> !555 fn panic_already_borrowed(&self) -> ! {
556 #[cfg(feature = "debug_cell")]
557 {
558 panic_already_borrowed(&self.borrowed_at)
559 }
560 #[cfg(not(feature = "debug_cell"))]
561 {
562 panic_already_borrowed()
563 }
564 }
565
566 /// Immutably borrows the wrapped value.
567 ///
568 /// The borrow lasts until the returned `BqlRef` exits scope. Multiple
569 /// immutable borrows can be taken out at the same time.
570 ///
571 /// # Panics
572 ///
573 /// Panics if the value is currently mutably borrowed.
574 ///
575 /// # Examples
576 ///
577 /// ```
578 /// use qemu_api::cell::BqlRefCell;
579 /// # qemu_api::cell::bql_start_test();
580 ///
581 /// let c = BqlRefCell::new(5);
582 ///
583 /// let borrowed_five = c.borrow();
584 /// let borrowed_five2 = c.borrow();
585 /// ```
586 ///
587 /// An example of panic:
588 ///
589 /// ```should_panic
590 /// use qemu_api::cell::BqlRefCell;
591 /// # qemu_api::cell::bql_start_test();
592 ///
593 /// let c = BqlRefCell::new(5);
594 ///
595 /// let m = c.borrow_mut();
596 /// let b = c.borrow(); // this causes a panic
597 /// ```
598 #[inline]
599 #[track_caller]
borrow(&self) -> BqlRef<'_, T>600 pub fn borrow(&self) -> BqlRef<'_, T> {
601 if let Some(b) = BorrowRef::new(&self.borrow) {
602 // `borrowed_at` is always the *first* active borrow
603 if b.borrow.get() == 1 {
604 #[cfg(feature = "debug_cell")]
605 self.borrowed_at.set(Some(std::panic::Location::caller()));
606 }
607
608 bql_block_unlock(true);
609
610 // SAFETY: `BorrowRef` ensures that there is only immutable access
611 // to the value while borrowed.
612 let value = unsafe { NonNull::new_unchecked(self.value.get()) };
613 BqlRef { value, borrow: b }
614 } else {
615 self.panic_already_borrowed()
616 }
617 }
618
619 /// Mutably borrows the wrapped value.
620 ///
621 /// The borrow lasts until the returned `BqlRefMut` or all `BqlRefMut`s
622 /// derived from it exit scope. The value cannot be borrowed while this
623 /// borrow is active.
624 ///
625 /// # Panics
626 ///
627 /// Panics if the value is currently borrowed.
628 ///
629 /// # Examples
630 ///
631 /// ```
632 /// use qemu_api::cell::BqlRefCell;
633 /// # qemu_api::cell::bql_start_test();
634 ///
635 /// let c = BqlRefCell::new("hello".to_owned());
636 ///
637 /// *c.borrow_mut() = "bonjour".to_owned();
638 ///
639 /// assert_eq!(&*c.borrow(), "bonjour");
640 /// ```
641 ///
642 /// An example of panic:
643 ///
644 /// ```should_panic
645 /// use qemu_api::cell::BqlRefCell;
646 /// # qemu_api::cell::bql_start_test();
647 ///
648 /// let c = BqlRefCell::new(5);
649 /// let m = c.borrow();
650 ///
651 /// let b = c.borrow_mut(); // this causes a panic
652 /// ```
653 #[inline]
654 #[track_caller]
borrow_mut(&self) -> BqlRefMut<'_, T>655 pub fn borrow_mut(&self) -> BqlRefMut<'_, T> {
656 if let Some(b) = BorrowRefMut::new(&self.borrow) {
657 #[cfg(feature = "debug_cell")]
658 {
659 self.borrowed_at.set(Some(std::panic::Location::caller()));
660 }
661
662 // SAFETY: this only adjusts a counter
663 bql_block_unlock(true);
664
665 // SAFETY: `BorrowRefMut` guarantees unique access.
666 let value = unsafe { NonNull::new_unchecked(self.value.get()) };
667 BqlRefMut {
668 value,
669 _borrow: b,
670 marker: PhantomData,
671 }
672 } else {
673 self.panic_already_borrowed()
674 }
675 }
676
677 /// Returns a raw pointer to the underlying data in this cell.
678 ///
679 /// # Examples
680 ///
681 /// ```
682 /// use qemu_api::cell::BqlRefCell;
683 ///
684 /// let c = BqlRefCell::new(5);
685 ///
686 /// let ptr = c.as_ptr();
687 /// ```
688 #[inline]
as_ptr(&self) -> *mut T689 pub const fn as_ptr(&self) -> *mut T {
690 self.value.get()
691 }
692 }
693
694 // SAFETY: Same as for std::sync::Mutex. In the end this is a Mutex that is
695 // stored out-of-line. Even though BqlRefCell includes Cells, they are
696 // themselves protected by the Big QEMU Lock. Furtheremore, the Big QEMU
697 // Lock cannot be released while any borrows is active.
698 unsafe impl<T> Send for BqlRefCell<T> where T: Send {}
699 unsafe impl<T> Sync for BqlRefCell<T> {}
700
701 impl<T: Clone> Clone for BqlRefCell<T> {
702 /// # Panics
703 ///
704 /// Panics if the value is currently mutably borrowed.
705 #[inline]
706 #[track_caller]
clone(&self) -> BqlRefCell<T>707 fn clone(&self) -> BqlRefCell<T> {
708 BqlRefCell::new(self.borrow().clone())
709 }
710
711 /// # Panics
712 ///
713 /// Panics if `source` is currently mutably borrowed.
714 #[inline]
715 #[track_caller]
clone_from(&mut self, source: &Self)716 fn clone_from(&mut self, source: &Self) {
717 self.value.get_mut().clone_from(&source.borrow())
718 }
719 }
720
721 impl<T: Default> Default for BqlRefCell<T> {
722 /// Creates a `BqlRefCell<T>`, with the `Default` value for T.
723 #[inline]
default() -> BqlRefCell<T>724 fn default() -> BqlRefCell<T> {
725 BqlRefCell::new(Default::default())
726 }
727 }
728
729 impl<T: PartialEq> PartialEq for BqlRefCell<T> {
730 /// # Panics
731 ///
732 /// Panics if the value in either `BqlRefCell` is currently mutably
733 /// borrowed.
734 #[inline]
eq(&self, other: &BqlRefCell<T>) -> bool735 fn eq(&self, other: &BqlRefCell<T>) -> bool {
736 *self.borrow() == *other.borrow()
737 }
738 }
739
740 impl<T: Eq> Eq for BqlRefCell<T> {}
741
742 impl<T: PartialOrd> PartialOrd for BqlRefCell<T> {
743 /// # Panics
744 ///
745 /// Panics if the value in either `BqlRefCell` is currently mutably
746 /// borrowed.
747 #[inline]
partial_cmp(&self, other: &BqlRefCell<T>) -> Option<Ordering>748 fn partial_cmp(&self, other: &BqlRefCell<T>) -> Option<Ordering> {
749 self.borrow().partial_cmp(&*other.borrow())
750 }
751 }
752
753 impl<T: Ord> Ord for BqlRefCell<T> {
754 /// # Panics
755 ///
756 /// Panics if the value in either `BqlRefCell` is currently mutably
757 /// borrowed.
758 #[inline]
cmp(&self, other: &BqlRefCell<T>) -> Ordering759 fn cmp(&self, other: &BqlRefCell<T>) -> Ordering {
760 self.borrow().cmp(&*other.borrow())
761 }
762 }
763
764 impl<T> From<T> for BqlRefCell<T> {
765 /// Creates a new `BqlRefCell<T>` containing the given value.
from(t: T) -> BqlRefCell<T>766 fn from(t: T) -> BqlRefCell<T> {
767 BqlRefCell::new(t)
768 }
769 }
770
771 struct BorrowRef<'b> {
772 borrow: &'b Cell<BorrowFlag>,
773 }
774
775 impl<'b> BorrowRef<'b> {
776 #[inline]
new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>>777 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> {
778 let b = borrow.get().wrapping_add(1);
779 if !is_reading(b) {
780 // Incrementing borrow can result in a non-reading value (<= 0) in these cases:
781 // 1. It was < 0, i.e. there are writing borrows, so we can't allow a read
782 // borrow due to Rust's reference aliasing rules
783 // 2. It was isize::MAX (the max amount of reading borrows) and it overflowed
784 // into isize::MIN (the max amount of writing borrows) so we can't allow an
785 // additional read borrow because isize can't represent so many read borrows
786 // (this can only happen if you mem::forget more than a small constant amount
787 // of `BqlRef`s, which is not good practice)
788 None
789 } else {
790 // Incrementing borrow can result in a reading value (> 0) in these cases:
791 // 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read
792 // borrow
793 // 2. It was > 0 and < isize::MAX, i.e. there were read borrows, and isize is
794 // large enough to represent having one more read borrow
795 borrow.set(b);
796 Some(BorrowRef { borrow })
797 }
798 }
799 }
800
801 impl Drop for BorrowRef<'_> {
802 #[inline]
drop(&mut self)803 fn drop(&mut self) {
804 let borrow = self.borrow.get();
805 debug_assert!(is_reading(borrow));
806 self.borrow.set(borrow - 1);
807 bql_block_unlock(false)
808 }
809 }
810
811 impl Clone for BorrowRef<'_> {
812 #[inline]
clone(&self) -> Self813 fn clone(&self) -> Self {
814 BorrowRef::new(self.borrow).unwrap()
815 }
816 }
817
818 /// Wraps a borrowed reference to a value in a `BqlRefCell` box.
819 /// A wrapper type for an immutably borrowed value from a `BqlRefCell<T>`.
820 ///
821 /// See the [module-level documentation](self) for more.
822 pub struct BqlRef<'b, T: 'b> {
823 // NB: we use a pointer instead of `&'b T` to avoid `noalias` violations, because a
824 // `BqlRef` argument doesn't hold immutability for its whole scope, only until it drops.
825 // `NonNull` is also covariant over `T`, just like we would have with `&T`.
826 value: NonNull<T>,
827 borrow: BorrowRef<'b>,
828 }
829
830 impl<T> Deref for BqlRef<'_, T> {
831 type Target = T;
832
833 #[inline]
deref(&self) -> &T834 fn deref(&self) -> &T {
835 // SAFETY: the value is accessible as long as we hold our borrow.
836 unsafe { self.value.as_ref() }
837 }
838 }
839
840 impl<'b, T> BqlRef<'b, T> {
841 /// Copies a `BqlRef`.
842 ///
843 /// The `BqlRefCell` is already immutably borrowed, so this cannot fail.
844 ///
845 /// This is an associated function that needs to be used as
846 /// `BqlRef::clone(...)`. A `Clone` implementation or a method would
847 /// interfere with the widespread use of `r.borrow().clone()` to clone
848 /// the contents of a `BqlRefCell`.
849 #[must_use]
850 #[inline]
851 #[allow(clippy::should_implement_trait)]
clone(orig: &BqlRef<'b, T>) -> BqlRef<'b, T>852 pub fn clone(orig: &BqlRef<'b, T>) -> BqlRef<'b, T> {
853 BqlRef {
854 value: orig.value,
855 borrow: orig.borrow.clone(),
856 }
857 }
858 }
859
860 impl<T: fmt::Debug> fmt::Debug for BqlRef<'_, T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result861 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
862 (**self).fmt(f)
863 }
864 }
865
866 impl<T: fmt::Display> fmt::Display for BqlRef<'_, T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result867 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
868 (**self).fmt(f)
869 }
870 }
871
872 struct BorrowRefMut<'b> {
873 borrow: &'b Cell<BorrowFlag>,
874 }
875
876 impl<'b> BorrowRefMut<'b> {
877 #[inline]
new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>>878 fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>> {
879 // There must currently be no existing references when borrow_mut() is
880 // called, so we explicitly only allow going from UNUSED to UNUSED - 1.
881 match borrow.get() {
882 UNUSED => {
883 borrow.set(UNUSED - 1);
884 Some(BorrowRefMut { borrow })
885 }
886 _ => None,
887 }
888 }
889 }
890
891 impl Drop for BorrowRefMut<'_> {
892 #[inline]
drop(&mut self)893 fn drop(&mut self) {
894 let borrow = self.borrow.get();
895 debug_assert!(is_writing(borrow));
896 self.borrow.set(borrow + 1);
897 bql_block_unlock(false)
898 }
899 }
900
901 /// A wrapper type for a mutably borrowed value from a `BqlRefCell<T>`.
902 ///
903 /// See the [module-level documentation](self) for more.
904 pub struct BqlRefMut<'b, T: 'b> {
905 // NB: we use a pointer instead of `&'b mut T` to avoid `noalias` violations, because a
906 // `BqlRefMut` argument doesn't hold exclusivity for its whole scope, only until it drops.
907 value: NonNull<T>,
908 _borrow: BorrowRefMut<'b>,
909 // `NonNull` is covariant over `T`, so we need to reintroduce invariance.
910 marker: PhantomData<&'b mut T>,
911 }
912
913 impl<T> Deref for BqlRefMut<'_, T> {
914 type Target = T;
915
916 #[inline]
deref(&self) -> &T917 fn deref(&self) -> &T {
918 // SAFETY: the value is accessible as long as we hold our borrow.
919 unsafe { self.value.as_ref() }
920 }
921 }
922
923 impl<T> DerefMut for BqlRefMut<'_, T> {
924 #[inline]
deref_mut(&mut self) -> &mut T925 fn deref_mut(&mut self) -> &mut T {
926 // SAFETY: the value is accessible as long as we hold our borrow.
927 unsafe { self.value.as_mut() }
928 }
929 }
930
931 impl<T: fmt::Debug> fmt::Debug for BqlRefMut<'_, T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result932 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
933 (**self).fmt(f)
934 }
935 }
936
937 impl<T: fmt::Display> fmt::Display for BqlRefMut<'_, T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result938 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
939 (**self).fmt(f)
940 }
941 }
942
943 /// Stores an opaque value that is shared with C code.
944 ///
945 /// Often, C structs can changed when calling a C function even if they are
946 /// behind a shared Rust reference, or they can be initialized lazily and have
947 /// invalid bit patterns (e.g. `3` for a [`bool`]). This goes against Rust's
948 /// strict aliasing rules, which normally prevent mutation through shared
949 /// references.
950 ///
951 /// Wrapping the struct with `Opaque<T>` ensures that the Rust compiler does not
952 /// assume the usual constraints that Rust structs require, and allows using
953 /// shared references on the Rust side.
954 ///
955 /// `Opaque<T>` is `#[repr(transparent)]`, so that it matches the memory layout
956 /// of `T`.
957 #[repr(transparent)]
958 pub struct Opaque<T> {
959 value: UnsafeCell<MaybeUninit<T>>,
960 // PhantomPinned also allows multiple references to the `Opaque<T>`, i.e.
961 // one `&mut Opaque<T>` can coexist with a `&mut T` or any number of `&T`;
962 // see https://docs.rs/pinned-aliasable/latest/pinned_aliasable/.
963 _pin: PhantomPinned,
964 }
965
966 impl<T> Opaque<T> {
967 /// Creates a new shared reference from a C pointer
968 ///
969 /// # Safety
970 ///
971 /// The pointer must be valid, though it need not point to a valid value.
from_raw<'a>(ptr: *mut T) -> &'a Self972 pub unsafe fn from_raw<'a>(ptr: *mut T) -> &'a Self {
973 let ptr = NonNull::new(ptr).unwrap().cast::<Self>();
974 // SAFETY: Self is a transparent wrapper over T
975 unsafe { ptr.as_ref() }
976 }
977
978 /// Creates a new opaque object with uninitialized contents.
979 ///
980 /// # Safety
981 ///
982 /// Ultimately the pointer to the returned value will be dereferenced
983 /// in another `unsafe` block, for example when passing it to a C function,
984 /// but the functions containing the dereference are usually safe. The
985 /// value returned from `uninit()` must be initialized and pinned before
986 /// calling them.
987 #[allow(clippy::missing_const_for_fn)]
uninit() -> Self988 pub unsafe fn uninit() -> Self {
989 Self {
990 value: UnsafeCell::new(MaybeUninit::uninit()),
991 _pin: PhantomPinned,
992 }
993 }
994
995 /// Creates a new opaque object with zeroed contents.
996 ///
997 /// # Safety
998 ///
999 /// Ultimately the pointer to the returned value will be dereferenced
1000 /// in another `unsafe` block, for example when passing it to a C function,
1001 /// but the functions containing the dereference are usually safe. The
1002 /// value returned from `uninit()` must be pinned (and possibly initialized)
1003 /// before calling them.
1004 #[allow(clippy::missing_const_for_fn)]
zeroed() -> Self1005 pub unsafe fn zeroed() -> Self {
1006 Self {
1007 value: UnsafeCell::new(MaybeUninit::zeroed()),
1008 _pin: PhantomPinned,
1009 }
1010 }
1011
1012 /// Returns a raw mutable pointer to the opaque data.
as_mut_ptr(&self) -> *mut T1013 pub const fn as_mut_ptr(&self) -> *mut T {
1014 UnsafeCell::get(&self.value).cast()
1015 }
1016
1017 /// Returns a raw pointer to the opaque data.
as_ptr(&self) -> *const T1018 pub const fn as_ptr(&self) -> *const T {
1019 self.as_mut_ptr() as *const _
1020 }
1021
1022 /// Returns a raw pointer to the opaque data that can be passed to a
1023 /// C function as `void *`.
as_void_ptr(&self) -> *mut std::ffi::c_void1024 pub const fn as_void_ptr(&self) -> *mut std::ffi::c_void {
1025 UnsafeCell::get(&self.value).cast()
1026 }
1027
1028 /// Converts a raw pointer to the wrapped type.
raw_get(slot: *mut Self) -> *mut T1029 pub const fn raw_get(slot: *mut Self) -> *mut T {
1030 // Compare with Linux's raw_get method, which goes through an UnsafeCell
1031 // because it takes a *const Self instead.
1032 slot.cast()
1033 }
1034 }
1035
1036 impl<T> fmt::Debug for Opaque<T> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result1037 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1038 let mut name: String = "Opaque<".to_string();
1039 name += std::any::type_name::<T>();
1040 name += ">";
1041 f.debug_tuple(&name).field(&self.as_ptr()).finish()
1042 }
1043 }
1044
1045 impl<T: Default> Opaque<T> {
1046 /// Creates a new opaque object with default contents.
1047 ///
1048 /// # Safety
1049 ///
1050 /// Ultimately the pointer to the returned value will be dereferenced
1051 /// in another `unsafe` block, for example when passing it to a C function,
1052 /// but the functions containing the dereference are usually safe. The
1053 /// value returned from `uninit()` must be pinned before calling them.
new() -> Self1054 pub unsafe fn new() -> Self {
1055 Self {
1056 value: UnsafeCell::new(MaybeUninit::new(T::default())),
1057 _pin: PhantomPinned,
1058 }
1059 }
1060 }
1061
1062 /// Annotates [`Self`] as a transparent wrapper for another type.
1063 ///
1064 /// Usually defined via the [`qemu_api_macros::Wrapper`] derive macro.
1065 ///
1066 /// # Examples
1067 ///
1068 /// ```
1069 /// # use std::mem::ManuallyDrop;
1070 /// # use qemu_api::cell::Wrapper;
1071 /// #[repr(transparent)]
1072 /// pub struct Example {
1073 /// inner: ManuallyDrop<String>,
1074 /// }
1075 ///
1076 /// unsafe impl Wrapper for Example {
1077 /// type Wrapped = String;
1078 /// }
1079 /// ```
1080 ///
1081 /// # Safety
1082 ///
1083 /// `Self` must be a `#[repr(transparent)]` wrapper for the `Wrapped` type,
1084 /// whether directly or indirectly.
1085 ///
1086 /// # Methods
1087 ///
1088 /// By convention, types that implement Wrapper also implement the following
1089 /// methods:
1090 ///
1091 /// ```ignore
1092 /// pub const unsafe fn from_raw<'a>(value: *mut Self::Wrapped) -> &'a Self;
1093 /// pub const unsafe fn as_mut_ptr(&self) -> *mut Self::Wrapped;
1094 /// pub const unsafe fn as_ptr(&self) -> *const Self::Wrapped;
1095 /// pub const unsafe fn raw_get(slot: *mut Self) -> *const Self::Wrapped;
1096 /// ```
1097 ///
1098 /// They are not defined here to allow them to be `const`.
1099 pub unsafe trait Wrapper {
1100 type Wrapped;
1101 }
1102
1103 unsafe impl<T> Wrapper for Opaque<T> {
1104 type Wrapped = T;
1105 }
1106