Skip to content

Commit 0637418

Browse files
committed
tmp (x5)
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 59388c2 commit 0637418

File tree

27 files changed

+1085
-1803
lines changed

27 files changed

+1085
-1803
lines changed

src/hyperlight_common/src/flatbuffer_wrappers/hyperlight_peb.rs

Lines changed: 184 additions & 206 deletions
Large diffs are not rendered by default.

src/hyperlight_common/src/flatbuffer_wrappers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub mod guest_log_level;
2525
pub mod host_function_definition;
2626
/// cbindgen:ignore
2727
pub mod host_function_details;
28-
pub mod util;
2928
pub mod hyperlight_peb;
29+
pub mod util;

src/hyperlight_common/src/flatbuffers/hyperlight/generated/hyperlight_peb_generated.rs

Lines changed: 0 additions & 451 deletions
This file was deleted.

src/hyperlight_common/src/flatbuffers/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,5 @@ pub mod hyperlight {
5454
pub use self::log_level_generated::*;
5555
mod guest_log_data_generated;
5656
pub use self::guest_log_data_generated::*;
57-
mod hyperlight_peb_generated;
58-
pub use self::hyperlight_peb_generated::*;
5957
} // generated
6058
} // hyperlight

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
use alloc::vec::Vec;
1716
use core::arch::asm;
1817

19-
use spin::Once;
2018
use hyperlight_common::flatbuffer_wrappers::hyperlight_peb::{HyperlightPEB, RunMode};
21-
use crate::{__security_cookie, HEAP_ALLOCATOR, PEB, RUNNING_MODE};
19+
use spin::Once;
20+
2221
use crate::guest_function_call::dispatch_function;
22+
use crate::{__security_cookie, HEAP_ALLOCATOR, PEB};
2323

2424
#[inline(never)]
2525
pub fn halt() {
2626
unsafe {
27-
if RUNNING_MODE == RunMode::Hypervisor {
27+
if let RunMode::Hypervisor = PEB.clone().unwrap().run_mode {
2828
asm!("hlt", options(nostack))
2929
}
3030
}
@@ -72,50 +72,26 @@ static INIT: Once = Once::new();
7272
#[no_mangle]
7373
pub extern "win64" fn entrypoint(
7474
hyperlight_peb_ptr: u64,
75-
hyperlight_peb_size: u64,
75+
_hyperlight_peb_size: u64,
7676
seed: u64,
7777
_max_log_level: u64,
7878
) {
79-
INIT.call_once(|| {
80-
unsafe {
81-
let peb_slice: &mut [u8] = core::slice::from_raw_parts_mut(
82-
hyperlight_peb_ptr as *mut u8,
83-
hyperlight_peb_size as usize,
84-
);
85-
let mut peb: HyperlightPEB = peb_slice.try_into().unwrap();
86-
let run_mode = peb.run_mode.clone().unwrap();
87-
88-
__security_cookie = seed;
89-
90-
let heap_start = peb.guest_heap_data_ptr.unwrap() as usize;
91-
let heap_size = peb.guest_heap_data_size.unwrap() as usize;
92-
HEAP_ALLOCATOR
93-
.try_lock()
94-
.expect("Failed to access HEAP_ALLOCATOR")
95-
.init(heap_start, heap_size);
96-
97-
match run_mode {
98-
RunMode::Hypervisor => {
99-
RUNNING_MODE = RunMode::Hypervisor;
100-
}
101-
RunMode::InProcessLinux | RunMode::InProcessWindows => {
102-
RUNNING_MODE = run_mode;
103-
}
104-
_ => {
105-
panic!("Invalid runmode in PEB");
106-
}
107-
}
108-
109-
dispatch_function();
110-
111-
peb.guest_function_dispatch_ptr = Some(dispatch_function as usize as u64);
112-
PEB = Some(peb.clone());
113-
let peb_serialized: Vec<u8> = peb.try_into().unwrap();
114-
core::slice::from_raw_parts_mut(hyperlight_peb_ptr as *mut u8, peb_serialized.len())
115-
.copy_from_slice(&peb_serialized);
116-
117-
hyperlight_main();
118-
}
79+
INIT.call_once(|| unsafe {
80+
let peb = hyperlight_peb_ptr as *mut HyperlightPEB;
81+
82+
HEAP_ALLOCATOR
83+
.try_lock()
84+
.expect("Failed to access HEAP_ALLOCATOR")
85+
.init((*peb).guest_heap_data_ptr as usize, (*peb).guest_heap_data_size as usize);
86+
87+
(*peb).set_default_memory_layout();
88+
(*peb).guest_function_dispatch_ptr = dispatch_function as usize as u64;
89+
90+
__security_cookie = seed;
91+
92+
PEB = Some((*peb).clone());
93+
94+
hyperlight_main();
11995
});
12096

12197
halt();
@@ -165,4 +141,4 @@ pub extern "win64" fn entrypoint(
165141
// .init(heap_start, heap_size);
166142
//
167143
//
168-
// reset_error();
144+
// reset_error();

src/hyperlight_guest/src/guest_error.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ pub(crate) fn write_error(error_code: ErrorCode, message: Option<&str>) {
3535
.expect("Invalid guest_error_buffer, could not be converted to a Vec<u8>");
3636

3737
unsafe {
38-
assert!(!peb.guest_error_data_ptr.is_some());
38+
assert!(!peb.guest_error_data_ptr != 0);
3939
let len = guest_error_buffer.len();
40-
if guest_error_buffer.len() > peb.guest_error_data_size.unwrap() as usize {
40+
if guest_error_buffer.len() > peb.guest_error_data_size as usize {
4141
error!(
4242
"Guest error buffer is too small to hold the error message: size {} buffer size {} message may be truncated",
4343
guest_error_buffer.len(),
44-
peb.guest_error_data_size.unwrap() as usize
44+
peb.guest_error_data_size as usize
4545
);
4646
// get the length of the message
4747
let message_len = message.map_or("".to_string(), |m| m.to_string()).len();
4848
// message is too long, truncate it
4949
let truncate_len = message_len
50-
- (guest_error_buffer.len()
51-
- peb.guest_error_data_size.unwrap() as usize);
50+
- (guest_error_buffer.len() - peb.guest_error_data_size as usize);
5251
let truncated_message = message
5352
.map_or("".to_string(), |m| m.to_string())
5453
.chars()
@@ -67,7 +66,7 @@ pub(crate) fn write_error(error_code: ErrorCode, message: Option<&str>) {
6766
// but, because copy_nonoverlapping doesn't return anything, we can't do that.
6867
// Instead, we do the prior asserts/checks to check the destination pointer isn't null
6968
// and that there is enough space in the destination buffer for the copy.
70-
let dest_ptr = peb.guest_error_data_ptr.unwrap() as *mut u8;
69+
let dest_ptr = peb.guest_error_data_ptr as *mut u8;
7170
core::ptr::copy_nonoverlapping(guest_error_buffer.as_ptr(), dest_ptr, len);
7271
}
7372
}
@@ -76,9 +75,9 @@ pub(crate) fn reset_error() {
7675
unsafe {
7776
let peb = PEB.clone().unwrap();
7877
core::ptr::write_bytes(
79-
peb.guest_error_data_ptr.unwrap() as *mut u8,
78+
peb.guest_error_data_ptr as *mut u8,
8079
0,
81-
peb.guest_error_data_size.unwrap() as usize,
80+
peb.guest_error_data_size as usize,
8281
);
8382
}
8483
}

src/hyperlight_guest/src/guest_function_call.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, Functi
2121
use hyperlight_common::flatbuffer_wrappers::function_types::ParameterType;
2222
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2323
use hyperlight_common::input_output::{InputDataSection, OutputDataSection};
24+
2425
use crate::entrypoint::halt;
2526
use crate::error::{HyperlightGuestError, Result};
2627
use crate::guest_error::{reset_error, set_error};
@@ -86,22 +87,27 @@ fn internal_dispatch_function() -> Result<()> {
8687

8788
let peb = unsafe { PEB.clone().unwrap() };
8889

89-
let input_data_ptr = peb.input_data_ptr.unwrap();
90-
let input_data_size = peb.input_data_size.unwrap();
91-
let input_data_section = InputDataSection::new(input_data_ptr as *mut u8, input_data_size as usize);
90+
let input_data_ptr = peb.input_data_ptr;
91+
let input_data_size = peb.input_data_size;
92+
let input_data_section =
93+
InputDataSection::new(input_data_ptr as *mut u8, input_data_size as usize);
9294

93-
let output_data_ptr = peb.output_data_ptr.unwrap();
94-
let output_data_size = peb.output_data_size.unwrap();
95-
let output_data_section = OutputDataSection::new(output_data_ptr as *mut u8, output_data_size as usize);
95+
let output_data_ptr = peb.output_data_ptr;
96+
let output_data_size = peb.output_data_size;
97+
let output_data_section =
98+
OutputDataSection::new(output_data_ptr as *mut u8, output_data_size as usize);
9699

97-
let function_call = input_data_section.try_pop_shared_input_data_into::<FunctionCall>()
100+
let function_call = input_data_section
101+
.try_pop_shared_input_data_into::<FunctionCall>()
98102
.expect("Function call deserialization failed");
99103

100104
let result_vec = call_guest_function(function_call).inspect_err(|e| {
101105
set_error(e.kind.clone(), e.message.as_str());
102106
})?;
103107

104-
Ok(output_data_section.push_shared_output_data(result_vec).unwrap())
108+
Ok(output_data_section
109+
.push_shared_output_data(result_vec)
110+
.unwrap())
105111
}
106112

107113
// This is implemented as a separate function to make sure that epilogue in the internal_dispatch_function is called before the halt()

src/hyperlight_guest/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
// Deps
2020
use buddy_system_allocator::LockedHeap;
2121
use guest_function_register::GuestFunctionRegister;
22-
use hyperlight_common::flatbuffer_wrappers::hyperlight_peb::{HyperlightPEB, RunMode};
22+
use hyperlight_common::flatbuffer_wrappers::hyperlight_peb::HyperlightPEB;
2323

2424
extern crate alloc;
2525

@@ -105,7 +105,6 @@ pub static mut MIN_STACK_ADDRESS: u64 = 0;
105105
// pub(crate) static mut OUTB_PTR_WITH_CONTEXT: Option<
106106
// extern "win64" fn(*mut core::ffi::c_void, u16, u8),
107107
// > = None;
108-
pub static mut RUNNING_MODE: RunMode = RunMode::None;
109108

110109
pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
111110
GuestFunctionRegister::new();

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ impl MultiUseGuestCallContext {
7272
// !Send (and !Sync), we also don't need to worry about
7373
// synchronization
7474

75-
call_function_on_guest(&mut self.sbox.hv_handler, &mut self.sbox.mem_mgr, func_name, func_ret_type, args)
75+
call_function_on_guest(
76+
&mut self.sbox.hv_handler,
77+
&mut self.sbox.mem_mgr,
78+
func_name,
79+
func_ret_type,
80+
args,
81+
)
7682
}
7783

7884
/// Close out the context and get back the internally-stored

src/hyperlight_host/src/func/guest_dispatch.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use tracing::{instrument, Span};
2222

2323
use super::guest_err::check_for_guest_error;
2424
use crate::hypervisor::hypervisor_handler::{HypervisorHandler, HypervisorHandlerAction};
25-
use crate::HyperlightError::GuestExecutionHungOnHostFunctionCall;
26-
use crate::{HyperlightError, Result};
2725
use crate::mem::mgr::SandboxMemoryManager;
2826
use crate::mem::shared_mem::HostSharedMemory;
27+
use crate::HyperlightError::GuestExecutionHungOnHostFunctionCall;
28+
use crate::{HyperlightError, Result};
2929

3030
/// Call a guest function by name, using the given `wrapper_getter`.
3131
#[instrument(
@@ -54,7 +54,9 @@ pub(crate) fn call_function_on_guest(
5454
.try_into()
5555
.map_err(|_| HyperlightError::Error("Failed to serialize FunctionCall".to_string()))?;
5656

57-
mem_mgr.write_guest_function_call(&buffer)?;
57+
let input_data_region = mem_mgr.read_hyperlight_peb()?.get_input_data_region();
58+
59+
mem_mgr.write_guest_function_call(input_data_region, &buffer)?;
5860

5961
match hv_handler.execute_hypervisor_handler_action(
6062
HypervisorHandlerAction::DispatchCallFromHost(function_name.to_string()),
@@ -63,9 +65,7 @@ pub(crate) fn call_function_on_guest(
6365
Err(e) => match e {
6466
HyperlightError::HypervisorHandlerMessageReceiveTimedout() => {
6567
timedout = true;
66-
match hv_handler.terminate_hypervisor_handler_execution_and_reinitialise(
67-
mem_mgr,
68-
)? {
68+
match hv_handler.terminate_hypervisor_handler_execution_and_reinitialise(mem_mgr)? {
6969
HyperlightError::HypervisorHandlerExecutionCancelAttemptOnFinishedExecution() =>
7070
{}
7171
// ^^^ do nothing, we just want to actually get the Flatbuffer return value
@@ -81,8 +81,10 @@ pub(crate) fn call_function_on_guest(
8181
// mem_mgr.check_stack_guard()?; // <- wrapper around mem_mgr `check_for_stack_guard`
8282
check_for_guest_error(mem_mgr)?;
8383

84+
let output_data_region = hv_handler.get_output_data_region();
85+
8486
mem_mgr
85-
.get_guest_function_call_result()
87+
.get_guest_function_call_result(output_data_region)
8688
.map_err(|e| {
8789
if timedout {
8890
// if we timed-out, but still got here

src/hyperlight_host/src/func/guest_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use hyperlight_common::flatbuffer_wrappers::guest_error::{
1919
};
2020

2121
use crate::error::HyperlightError::{GuestError, OutBHandlingError, StackOverflow};
22+
use crate::mem::mgr::SandboxMemoryManager;
2223
use crate::mem::shared_mem::HostSharedMemory;
2324
use crate::sandbox::metrics::SandboxMetric::GuestErrorCount;
2425
use crate::{int_counter_vec_inc, log_then_return, Result};
25-
use crate::mem::mgr::SandboxMemoryManager;
2626

2727
/// Check for a guest error and return an `Err` if one was found,
2828
/// and `Ok` if one was not found.

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use std::fmt::{Debug, Formatter};
3030
use mshv_bindings::hv_message;
3131
use mshv_bindings::{
3232
hv_message_type, hv_message_type_HVMSG_GPA_INTERCEPT, hv_message_type_HVMSG_UNMAPPED_GPA,
33-
hv_message_type_HVMSG_X64_HALT, hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT,
34-
FloatingPointUnit, SegmentRegister, SpecialRegisters, StandardRegisters,
33+
hv_message_type_HVMSG_X64_HALT, hv_message_type_HVMSG_X64_IO_PORT_INTERCEPT, FloatingPointUnit,
34+
SegmentRegister, SpecialRegisters, StandardRegisters,
3535
};
3636
#[cfg(mshv3)]
3737
use mshv_bindings::{
@@ -48,12 +48,12 @@ use super::{
4848
Hypervisor, VirtualCPU, CR0_AM, CR0_ET, CR0_MP, CR0_NE, CR0_PE, CR0_PG, CR0_WP, CR4_OSFXSR,
4949
CR4_OSXMMEXCPT, CR4_PAE, EFER_LMA, EFER_LME, EFER_NX, EFER_SCE,
5050
};
51+
use crate::hypervisor::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
5152
use crate::hypervisor::hypervisor_handler::HypervisorHandler;
5253
use crate::hypervisor::HyperlightExit;
5354
use crate::mem::ptr::{GuestPtr, RawPtr};
5455
use crate::sandbox::sandbox_builder::SandboxMemorySections;
5556
use crate::{log_then_return, Result};
56-
use crate::hypervisor::fpu::{FP_CONTROL_WORD_DEFAULT, FP_TAG_WORD_DEFAULT, MXCSR_DEFAULT};
5757

5858
/// Determine whether the HyperV for Linux hypervisor API is present
5959
/// and functional.

0 commit comments

Comments
 (0)