|
| 1 | +/* |
| 2 | +Copyright 2026 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! Guest-side virtqueue state and initialization. |
| 18 | +//! |
| 19 | +//! Holds the global VirtqProducer instances for G2H and H2G queues. |
| 20 | +//! The producers are created during guest init (from `hyperlight_guest_bin`) |
| 21 | +//! and used by the guest host-call path in `host_comm`. |
| 22 | +
|
| 23 | +use alloc::rc::Rc; |
| 24 | +use core::cell::RefCell; |
| 25 | +use core::num::NonZeroU16; |
| 26 | + |
| 27 | +use hyperlight_common::virtq::{BufferPool, Layout, Notifier, QueueStats, VirtqProducer}; |
| 28 | +use hyperlight_guest::virtq_mem::GuestMemOps; |
| 29 | + |
| 30 | +/// Wrapper to mark types as Sync for single-threaded guest execution. |
| 31 | +struct SyncWrap<T>(T); |
| 32 | + |
| 33 | +// SAFETY: guest execution is single-threaded. |
| 34 | +unsafe impl<T> Sync for SyncWrap<T> {} |
| 35 | + |
| 36 | +/// Guest-side notifier (no-op). |
| 37 | +#[derive(Clone, Copy)] |
| 38 | +pub struct GuestNotifier; |
| 39 | + |
| 40 | +impl Notifier for GuestNotifier { |
| 41 | + fn notify(&self, _stats: QueueStats) {} |
| 42 | +} |
| 43 | + |
| 44 | +/// Type alias for the guest-side producer. |
| 45 | +pub type GuestProducer = VirtqProducer<GuestMemOps, GuestNotifier, Rc<BufferPool>>; |
| 46 | +/// Global G2H producer instance, initialized during guest init. |
| 47 | +static G2H_PRODUCER: SyncWrap<RefCell<Option<GuestProducer>>> = SyncWrap(RefCell::new(None)); |
| 48 | + |
| 49 | +/// Borrow the G2H producer mutably. |
| 50 | +/// |
| 51 | +/// # Panics |
| 52 | +/// |
| 53 | +/// Panics if the G2H producer has not been initialized or is already |
| 54 | +/// borrowed. |
| 55 | +pub fn with_g2h_producer<R>(f: impl FnOnce(&mut GuestProducer) -> R) -> R { |
| 56 | + let mut guard = G2H_PRODUCER.0.borrow_mut(); |
| 57 | + let producer = guard.as_mut().expect("G2H producer not initialized"); |
| 58 | + f(producer) |
| 59 | +} |
| 60 | + |
| 61 | +/// Initialize the G2H producer |
| 62 | +/// |
| 63 | +/// # Safety |
| 64 | +/// |
| 65 | +/// The ring GVA must point to valid, zeroed ring memory of the |
| 66 | +/// appropriate size. The pool GVA must point to valid, zeroed memory. |
| 67 | +pub unsafe fn init_g2h_producer(ring_gva: u64, num_descs: u16, pool_gva: u64, pool_size: usize) { |
| 68 | + let nz = NonZeroU16::new(num_descs).expect("G2H queue depth must be non-zero"); |
| 69 | + let pool = BufferPool::new(pool_gva, pool_size).expect("failed to create G2H buffer pool"); |
| 70 | + |
| 71 | + let layout = unsafe { Layout::from_base(ring_gva, nz) }.expect("invalid G2H ring layout"); |
| 72 | + let producer = VirtqProducer::new(layout, GuestMemOps, GuestNotifier, Rc::new(pool)); |
| 73 | + |
| 74 | + *G2H_PRODUCER.0.borrow_mut() = Some(producer); |
| 75 | +} |
0 commit comments