Skip to content

Commit 392bfa6

Browse files
committed
feat(virtq): remove unused stack based io path
Signed-off-by: Tomasz Andrzejak <andreiltd@gmail.com>
1 parent 49d5a53 commit 392bfa6

15 files changed

Lines changed: 188 additions & 1114 deletions

File tree

src/hyperlight_guest/src/guest_handle/host_comm.rs

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,13 @@ use alloc::format;
1818
use alloc::string::ToString;
1919
use alloc::vec::Vec;
2020

21-
use flatbuffers::FlatBufferBuilder;
22-
use hyperlight_common::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
23-
use hyperlight_common::flatbuffer_wrappers::function_types::{
24-
FunctionCallResult, ParameterValue, ReturnType, ReturnValue,
25-
};
2621
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2722
use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
2823
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
29-
use hyperlight_common::flatbuffer_wrappers::util::estimate_flatbuffer_capacity;
30-
use hyperlight_common::outb::OutBAction;
3124
use tracing::instrument;
3225

3326
use super::handle::GuestHandle;
3427
use crate::error::{HyperlightGuestError, Result};
35-
use crate::exit::out32;
3628

3729
impl GuestHandle {
3830
/// Get user memory region as bytes.
@@ -59,99 +51,6 @@ impl GuestHandle {
5951
}
6052
}
6153

62-
/// Get a return value from a host function call.
63-
/// This usually requires a host function to be called first using
64-
/// `call_host_function_internal`.
65-
///
66-
/// When calling `call_host_function<T>`, this function is called
67-
/// internally to get the return value.
68-
#[instrument(skip_all, level = "Trace")]
69-
pub fn get_host_return_value<T: TryFrom<ReturnValue>>(&self) -> Result<T> {
70-
let inner = self
71-
.try_pop_shared_input_data_into::<FunctionCallResult>()
72-
.expect("Unable to deserialize a return value from host")
73-
.into_inner();
74-
75-
match inner {
76-
Ok(ret) => T::try_from(ret).map_err(|_| {
77-
let expected = core::any::type_name::<T>();
78-
HyperlightGuestError::new(
79-
ErrorCode::UnsupportedParameterType,
80-
format!("Host return value could not be converted to expected {expected}",),
81-
)
82-
}),
83-
Err(e) => Err(HyperlightGuestError {
84-
kind: e.code,
85-
message: e.message,
86-
}),
87-
}
88-
}
89-
90-
pub fn get_host_return_raw(&self) -> Result<ReturnValue> {
91-
let inner = self
92-
.try_pop_shared_input_data_into::<FunctionCallResult>()
93-
.expect("Unable to deserialize a return value from host")
94-
.into_inner();
95-
96-
match inner {
97-
Ok(ret) => Ok(ret),
98-
Err(e) => Err(HyperlightGuestError {
99-
kind: e.code,
100-
message: e.message,
101-
}),
102-
}
103-
}
104-
105-
/// Call a host function without reading its return value from shared mem.
106-
/// This is used by both the Rust and C APIs to reduce code duplication.
107-
///
108-
/// Note: The function return value must be obtained by calling
109-
/// `get_host_return_value`.
110-
#[instrument(skip_all, level = "Trace")]
111-
pub fn call_host_function_without_returning_result(
112-
&self,
113-
function_name: &str,
114-
parameters: Option<Vec<ParameterValue>>,
115-
return_type: ReturnType,
116-
) -> Result<()> {
117-
let estimated_capacity =
118-
estimate_flatbuffer_capacity(function_name, parameters.as_deref().unwrap_or(&[]));
119-
120-
let host_function_call = FunctionCall::new(
121-
function_name.to_string(),
122-
parameters,
123-
FunctionCallType::Host,
124-
return_type,
125-
);
126-
127-
let mut builder = FlatBufferBuilder::with_capacity(estimated_capacity);
128-
129-
let host_function_call_buffer = host_function_call.encode(&mut builder);
130-
self.push_shared_output_data(host_function_call_buffer)?;
131-
132-
unsafe {
133-
out32(OutBAction::CallFunction as u16, 0);
134-
}
135-
136-
Ok(())
137-
}
138-
139-
/// Call a host function with the given parameters and return type.
140-
/// This function serializes the function call and its parameters,
141-
/// sends it to the host, and then retrieves the return value.
142-
///
143-
/// The return value is deserialized into the specified type `T`.
144-
#[instrument(skip_all, level = "Info")]
145-
pub fn call_host_function<T: TryFrom<ReturnValue>>(
146-
&self,
147-
function_name: &str,
148-
parameters: Option<Vec<ParameterValue>>,
149-
return_type: ReturnType,
150-
) -> Result<T> {
151-
self.call_host_function_without_returning_result(function_name, parameters, return_type)?;
152-
self.get_host_return_value::<T>()
153-
}
154-
15554
/// Log a message with the specified log level, source, caller, source file, and line number.
15655
pub fn log_message(
15756
&self,

src/hyperlight_guest/src/guest_handle/io.rs

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

src/hyperlight_guest/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ pub mod virtq;
3030
pub mod guest_handle {
3131
pub mod handle;
3232
pub mod host_comm;
33-
pub mod io;
3433
}

src/hyperlight_guest/src/virtq/context.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub struct GuestContext {
7272
g2h_producer: G2hProducer,
7373
h2g_producer: H2gProducer,
7474
generation: u16,
75+
last_host_return: Option<ReturnValue>,
7576
}
7677

7778
impl GuestContext {
@@ -100,6 +101,7 @@ impl GuestContext {
100101
g2h_producer,
101102
h2g_producer,
102103
generation,
104+
last_host_return: None,
103105
};
104106

105107
ctx.prefill_h2g();
@@ -343,4 +345,27 @@ impl GuestContext {
343345
entry.write_all(payload)?;
344346
self.g2h_producer.submit(entry)
345347
}
348+
349+
/// Stash a host function return value for later retrieval.
350+
///
351+
/// Used by the C API's two-step calling convention where
352+
/// `hl_call_host_function` and `hl_get_host_return_value_as_*`
353+
/// are separate calls.
354+
pub fn stash_host_return(&mut self, value: ReturnValue) {
355+
self.last_host_return = Some(value);
356+
}
357+
358+
/// Take the stashed host return value.
359+
///
360+
/// Panics if no value was stashed or if the type conversion fails.
361+
pub fn take_host_return<T: TryFrom<ReturnValue>>(&mut self) -> T {
362+
let rv = self
363+
.last_host_return
364+
.take()
365+
.expect("No host return value available");
366+
match T::try_from(rv) {
367+
Ok(v) => v,
368+
Err(_) => panic!("Host return value type mismatch"),
369+
}
370+
}
346371
}

0 commit comments

Comments
 (0)