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