|
| 1 | +/* |
| 2 | +Copyright 2025 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 | +use crate::Result; |
| 18 | + |
| 19 | +use crate::hypervisor::{Hypervisor, InterruptHandle}; |
| 20 | +use crate::mem::memory_region::MemoryRegion; |
| 21 | +use crate::mem::mgr::SandboxMemoryManager; |
| 22 | +use crate::mem::ptr::RawPtr; |
| 23 | +use crate::mem::shared_mem::HostSharedMemory; |
| 24 | +use crate::sandbox::host_funcs::FunctionRegistry; |
| 25 | + |
| 26 | +use log::LevelFilter; |
| 27 | +use std::sync::{Arc, Mutex}; |
| 28 | + |
| 29 | +#[derive(Debug)] |
| 30 | +pub(crate) struct HyperlightVm { |
| 31 | + pub(crate) vm: Box<dyn Hypervisor>, |
| 32 | +} |
| 33 | + |
| 34 | +impl HyperlightVm { |
| 35 | + pub(crate) fn new(inner: Box<dyn Hypervisor>) -> Self { |
| 36 | + Self { vm: inner } |
| 37 | + } |
| 38 | + |
| 39 | + #[allow(clippy::too_many_arguments)] |
| 40 | + pub(crate) fn initialise( |
| 41 | + &mut self, |
| 42 | + peb_addr: RawPtr, |
| 43 | + seed: u64, |
| 44 | + page_size: u32, |
| 45 | + mem_mgr: &mut SandboxMemoryManager<HostSharedMemory>, |
| 46 | + host_funcs: &Arc<Mutex<FunctionRegistry>>, |
| 47 | + guest_max_log_level: Option<LevelFilter>, |
| 48 | + #[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>, |
| 49 | + ) -> Result<()> { |
| 50 | + self.vm.initialise( |
| 51 | + peb_addr, |
| 52 | + seed, |
| 53 | + page_size, |
| 54 | + mem_mgr, |
| 55 | + host_funcs, |
| 56 | + guest_max_log_level, |
| 57 | + #[cfg(gdb)] |
| 58 | + dbg_mem_access_fn, |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + pub(crate) unsafe fn map_region(&mut self, rgn: &MemoryRegion) -> Result<()> { |
| 63 | + unsafe { self.vm.map_region(rgn) } |
| 64 | + } |
| 65 | + |
| 66 | + pub(crate) unsafe fn unmap_region(&mut self, rgn: &MemoryRegion) -> Result<()> { |
| 67 | + unsafe { self.vm.unmap_region(rgn) } |
| 68 | + } |
| 69 | + |
| 70 | + pub(crate) fn get_mapped_regions( |
| 71 | + &self, |
| 72 | + ) -> Box<dyn ExactSizeIterator<Item = &MemoryRegion> + '_> { |
| 73 | + self.vm.get_mapped_regions() |
| 74 | + } |
| 75 | + |
| 76 | + pub(crate) fn dispatch_call_from_host( |
| 77 | + &mut self, |
| 78 | + dispatch_func_addr: RawPtr, |
| 79 | + mem_mgr: &mut SandboxMemoryManager<HostSharedMemory>, |
| 80 | + host_funcs: &Arc<Mutex<FunctionRegistry>>, |
| 81 | + #[cfg(gdb)] dbg_mem_access_fn: Arc<Mutex<SandboxMemoryManager<HostSharedMemory>>>, |
| 82 | + ) -> Result<()> { |
| 83 | + self.vm.dispatch_call_from_host( |
| 84 | + dispatch_func_addr, |
| 85 | + mem_mgr, |
| 86 | + host_funcs, |
| 87 | + #[cfg(gdb)] |
| 88 | + dbg_mem_access_fn, |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + pub(crate) fn interrupt_handle(&self) -> Arc<dyn InterruptHandle> { |
| 93 | + self.vm.interrupt_handle() |
| 94 | + } |
| 95 | + |
| 96 | + pub(crate) fn clear_cancel(&self) { |
| 97 | + self.vm.clear_cancel() |
| 98 | + } |
| 99 | +} |
0 commit comments