|
| 1 | +use alloc::vec::Vec; |
| 2 | +use anyhow::Result; |
| 3 | +use flatbuffers::{size_prefixed_root, FlatBufferBuilder}; |
| 4 | + |
| 5 | +use crate::flatbuffers::hyperlight::generated::{HyperlightPEB as FbHyperlightPEB, HyperlightPEBArgs as FbHyperlightPEBArgs}; |
| 6 | + |
| 7 | +/// Hyperlight supports 2 primary modes: |
| 8 | +/// (1) Hypervisor mode |
| 9 | +/// (2) In-process mode |
| 10 | +/// |
| 11 | +/// When running in process, there's no hypervisor isolation. |
| 12 | +/// In-process mode is primarily used for debugging and testing. |
| 13 | +#[repr(u64)] |
| 14 | +#[derive(Clone, PartialEq)] |
| 15 | +pub enum RunMode { |
| 16 | + None = 0, |
| 17 | + Hypervisor = 1, |
| 18 | + InProcessWindows = 2, |
| 19 | + InProcessLinux = 3, |
| 20 | + Invalid = 4, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Clone)] |
| 24 | +pub struct HyperlightPEB { |
| 25 | + /// The guest function dispatch pointer is what allows |
| 26 | + /// a host to call "guest functions". The host can |
| 27 | + /// directly set the instruction pointer register to this |
| 28 | + /// before re-entering the guest. |
| 29 | + pub guest_function_dispatch_ptr: Option<u64>, |
| 30 | + |
| 31 | + // Host function details may be used in the guest before |
| 32 | + // issuing a host function call to validate it before |
| 33 | + // ensuing a `VMEXIT`. |
| 34 | + pub host_function_details_ptr: Option<u64>, |
| 35 | + pub host_function_details_size: Option<u64>, |
| 36 | + |
| 37 | + /// Guest error data can be used to pass error information |
| 38 | + /// between host and the guest. |
| 39 | + pub guest_error_data_ptr: Option<u64>, |
| 40 | + pub guest_error_data_size: Option<u64>, |
| 41 | + |
| 42 | + /// On Windows, Hyperlight supports in-process execution. |
| 43 | + /// In-process execution means a guest is running in |
| 44 | + /// Hyperlight, but with no hypervisor isolation. When we |
| 45 | + /// run in-process, we can't rely on the usual mechanism for |
| 46 | + /// host function calls (i.e., `outb`). Instead, we call a |
| 47 | + /// function directly, which is represented by this pointer. |
| 48 | + pub outb_ptr: Option<u64>, |
| 49 | + |
| 50 | + /// Hyperlight supports two primary modes: |
| 51 | + /// (1) Hypervisor mode |
| 52 | + /// (2) In-process mode |
| 53 | + /// |
| 54 | + /// When running in process, there's no hypervisor isolation. |
| 55 | + /// It's a mode primarily used for debugging and testing. |
| 56 | + pub run_mode: Option<RunMode>, |
| 57 | + |
| 58 | + /// The input data pointer is used to pass data from |
| 59 | + /// the host to the guest. |
| 60 | + pub input_data_ptr: Option<u64>, |
| 61 | + pub input_data_size: Option<u64>, |
| 62 | + |
| 63 | + /// The output data pointer is used to pass data from |
| 64 | + /// the guest to the host. |
| 65 | + pub output_data_ptr: Option<u64>, |
| 66 | + pub output_data_size: Option<u64>, |
| 67 | + |
| 68 | + /// The guest panic context pointer can be used to pass |
| 69 | + /// panic context data from the guest to the host. |
| 70 | + pub guest_panic_context_ptr: Option<u64>, |
| 71 | + pub guest_panic_context_size: Option<u64>, |
| 72 | + |
| 73 | + /// The guest heap data pointer points to a region of |
| 74 | + /// memory in the guest that is used for heap allocations. |
| 75 | + pub guest_heap_data_ptr: Option<u64>, |
| 76 | + pub guest_heap_data_size: Option<u64>, |
| 77 | + |
| 78 | + /// The guest stack data pointer points to a region of |
| 79 | + /// memory in the guest that is used for stack allocations. |
| 80 | + pub guest_stack_data_ptr: Option<u64>, |
| 81 | + pub guest_stack_data_size: Option<u64>, |
| 82 | +} |
| 83 | + |
| 84 | +impl TryFrom<&[u8]> for HyperlightPEB { |
| 85 | + type Error = anyhow::Error; |
| 86 | + |
| 87 | + fn try_from(value: &[u8]) -> Result<Self> { |
| 88 | + let peb_fb = size_prefixed_root::<FbHyperlightPEB>(value) |
| 89 | + .map_err(|e| anyhow::anyhow!("Error reading HyperlightPEB buffer: {:?}", e))?; |
| 90 | + |
| 91 | + let run_mode = match peb_fb.run_mode() { |
| 92 | + 0 => Some(RunMode::None), |
| 93 | + 1 => Some(RunMode::Hypervisor), |
| 94 | + 2 => Some(RunMode::InProcessWindows), |
| 95 | + 3 => Some(RunMode::InProcessLinux), |
| 96 | + 4 => Some(RunMode::Invalid), |
| 97 | + _ => None, // Handles unexpected values gracefully |
| 98 | + }; |
| 99 | + |
| 100 | + Ok(Self { |
| 101 | + guest_function_dispatch_ptr: Some(peb_fb.guest_function_dispatch_ptr()).filter(|&v| v != 0), |
| 102 | + host_function_details_ptr: Some(peb_fb.host_function_details_ptr()).filter(|&v| v != 0), |
| 103 | + host_function_details_size: Some(peb_fb.host_function_details_size()).filter(|&v| v != 0), |
| 104 | + guest_error_data_ptr: Some(peb_fb.guest_error_data_ptr()).filter(|&v| v != 0), |
| 105 | + guest_error_data_size: Some(peb_fb.guest_error_data_size()).filter(|&v| v != 0), |
| 106 | + outb_ptr: Some(peb_fb.outb_ptr()).filter(|&v| v != 0), |
| 107 | + run_mode, |
| 108 | + input_data_ptr: Some(peb_fb.input_data_ptr()).filter(|&v| v != 0), |
| 109 | + input_data_size: Some(peb_fb.input_data_size()).filter(|&v| v != 0), |
| 110 | + output_data_ptr: Some(peb_fb.output_data_ptr()).filter(|&v| v != 0), |
| 111 | + output_data_size: Some(peb_fb.output_data_size()).filter(|&v| v != 0), |
| 112 | + guest_panic_context_ptr: Some(peb_fb.guest_panic_context_ptr()).filter(|&v| v != 0), |
| 113 | + guest_panic_context_size: Some(peb_fb.guest_panic_context_size()).filter(|&v| v != 0), |
| 114 | + guest_heap_data_ptr: Some(peb_fb.guest_heap_data_ptr()).filter(|&v| v != 0), |
| 115 | + guest_heap_data_size: Some(peb_fb.guest_heap_data_size()).filter(|&v| v != 0), |
| 116 | + guest_stack_data_ptr: Some(peb_fb.guest_stack_data_ptr()).filter(|&v| v != 0), |
| 117 | + guest_stack_data_size: Some(peb_fb.guest_stack_data_size()).filter(|&v| v != 0), |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl TryFrom<HyperlightPEB> for Vec<u8> { |
| 123 | + type Error = anyhow::Error; |
| 124 | + |
| 125 | + fn try_from(value: HyperlightPEB) -> Result<Vec<u8>> { |
| 126 | + let mut builder = FlatBufferBuilder::new(); |
| 127 | + |
| 128 | + // Convert RunMode into u64 |
| 129 | + let run_mode: u64 = match value.run_mode { |
| 130 | + Some(RunMode::None) => 0, |
| 131 | + Some(RunMode::Hypervisor) => 1, |
| 132 | + Some(RunMode::InProcessWindows) => 2, |
| 133 | + Some(RunMode::InProcessLinux) => 3, |
| 134 | + Some(RunMode::Invalid) => 4, |
| 135 | + None => 0, // Default to None |
| 136 | + }; |
| 137 | + |
| 138 | + let hyperlight_peb = FbHyperlightPEB::create( |
| 139 | + &mut builder, |
| 140 | + &FbHyperlightPEBArgs { |
| 141 | + guest_function_dispatch_ptr: value.guest_function_dispatch_ptr.unwrap_or(0), |
| 142 | + host_function_details_ptr: value.host_function_details_ptr.unwrap_or(0), |
| 143 | + host_function_details_size: value.host_function_details_size.unwrap_or(0), |
| 144 | + guest_error_data_ptr: value.guest_error_data_ptr.unwrap_or(0), |
| 145 | + guest_error_data_size: value.guest_error_data_size.unwrap_or(0), |
| 146 | + outb_ptr: value.outb_ptr.unwrap_or(0), |
| 147 | + run_mode, |
| 148 | + input_data_ptr: value.input_data_ptr.unwrap_or(0), |
| 149 | + input_data_size: value.input_data_size.unwrap_or(0), |
| 150 | + output_data_ptr: value.output_data_ptr.unwrap_or(0), |
| 151 | + output_data_size: value.output_data_size.unwrap_or(0), |
| 152 | + guest_panic_context_ptr: value.guest_panic_context_ptr.unwrap_or(0), |
| 153 | + guest_panic_context_size: value.guest_panic_context_size.unwrap_or(0), |
| 154 | + guest_heap_data_ptr: value.guest_heap_data_ptr.unwrap_or(0), |
| 155 | + guest_heap_data_size: value.guest_heap_data_size.unwrap_or(0), |
| 156 | + guest_stack_data_ptr: value.guest_stack_data_ptr.unwrap_or(0), |
| 157 | + guest_stack_data_size: value.guest_stack_data_size.unwrap_or(0), |
| 158 | + }, |
| 159 | + ); |
| 160 | + |
| 161 | + builder.finish_size_prefixed(hyperlight_peb, None); |
| 162 | + let res = builder.finished_data().to_vec(); |
| 163 | + |
| 164 | + Ok(res) |
| 165 | + } |
| 166 | +} |
0 commit comments