Skip to content

Commit b1f7d4c

Browse files
committed
Make interrupt retry delay configurable
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 661de86 commit b1f7d4c

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use super::{
6565
use crate::hypervisor::HyperlightExit;
6666
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
6767
use crate::mem::ptr::{GuestPtr, RawPtr};
68+
use crate::sandbox::SandboxConfiguration;
6869
#[cfg(gdb)]
6970
use crate::HyperlightError;
7071
use crate::{log_then_return, new_error, Result};
@@ -318,6 +319,7 @@ impl HypervLinuxDriver {
318319
entrypoint_ptr: GuestPtr,
319320
rsp_ptr: GuestPtr,
320321
pml4_ptr: GuestPtr,
322+
config: &SandboxConfiguration,
321323
#[cfg(gdb)] gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
322324
) -> Result<Self> {
323325
let mshv = Mshv::new()?;
@@ -397,6 +399,7 @@ impl HypervLinuxDriver {
397399
running: AtomicBool::new(false),
398400
cancel_requested: AtomicBool::new(false),
399401
tid: AtomicU64::new(unsafe { libc::pthread_self() }),
402+
retry_delay: config.get_interrupt_retry_delay(),
400403
dropped: AtomicBool::new(false),
401404
}),
402405

@@ -853,11 +856,14 @@ mod tests {
853856
MemoryRegionFlags::READ | MemoryRegionFlags::WRITE | MemoryRegionFlags::EXECUTE,
854857
crate::mem::memory_region::MemoryRegionType::Code,
855858
);
859+
let config: SandboxConfiguration = Default::default();
860+
856861
super::HypervLinuxDriver::new(
857862
regions.build(),
858863
entrypoint_ptr,
859864
rsp_ptr,
860865
pml4_ptr,
866+
&config,
861867
#[cfg(gdb)]
862868
None,
863869
)

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use super::{
4040
};
4141
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
4242
use crate::mem::ptr::{GuestPtr, RawPtr};
43+
use crate::sandbox::SandboxConfiguration;
4344
#[cfg(gdb)]
4445
use crate::HyperlightError;
4546
use crate::{log_then_return, new_error, Result};
@@ -301,6 +302,7 @@ impl KVMDriver {
301302
pml4_addr: u64,
302303
entrypoint: u64,
303304
rsp: u64,
305+
config: &SandboxConfiguration,
304306
#[cfg(gdb)] gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
305307
) -> Result<Self> {
306308
let kvm = Kvm::new()?;
@@ -352,6 +354,7 @@ impl KVMDriver {
352354
running: AtomicBool::new(false),
353355
cancel_requested: AtomicBool::new(false),
354356
tid: AtomicU64::new(unsafe { libc::pthread_self() }),
357+
retry_delay: config.get_interrupt_retry_delay(),
355358
dropped: AtomicBool::new(false),
356359
}),
357360

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ use std::str::FromStr;
6464
#[cfg(any(kvm, mshv))]
6565
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
6666
use std::sync::{Arc, Mutex};
67+
#[cfg(any(kvm, mshv))]
68+
use std::time::Duration;
6769

6870
#[cfg(gdb)]
6971
use gdb::VcpuStopReason;
@@ -353,6 +355,8 @@ pub(super) struct LinuxInterruptHandle {
353355
cancel_requested: AtomicBool,
354356
/// Whether the corresponding vm is dropped
355357
dropped: AtomicBool,
358+
/// Retry delay between signals sent to the vcpu thread
359+
retry_delay: Duration,
356360
}
357361

358362
#[cfg(any(kvm, mshv))]
@@ -369,7 +373,7 @@ impl InterruptHandle for LinuxInterruptHandle {
369373
unsafe {
370374
libc::pthread_kill(self.tid.load(Ordering::Relaxed) as _, signal_number);
371375
}
372-
std::thread::sleep(std::time::Duration::from_micros(50));
376+
std::thread::sleep(self.retry_delay);
373377
}
374378

375379
sent_signal
@@ -391,7 +395,7 @@ pub(crate) mod tests {
391395
use crate::mem::ptr::RawPtr;
392396
use crate::sandbox::uninitialized::GuestBinary;
393397
use crate::sandbox::uninitialized_evolve::set_up_hypervisor_partition;
394-
use crate::sandbox::UninitializedSandbox;
398+
use crate::sandbox::{SandboxConfiguration, UninitializedSandbox};
395399
use crate::{is_hypervisor_present, new_error, Result};
396400

397401
#[cfg(gdb)]
@@ -432,13 +436,11 @@ pub(crate) mod tests {
432436

433437
let filename = dummy_guest_as_string().map_err(|e| new_error!("{}", e))?;
434438

435-
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), None)?;
439+
let config: SandboxConfiguration = Default::default();
440+
let sandbox =
441+
UninitializedSandbox::new(GuestBinary::FilePath(filename.clone()), Some(config))?;
436442
let (_hshm, mut gshm) = sandbox.mgr.build();
437-
let mut vm = set_up_hypervisor_partition(
438-
&mut gshm,
439-
#[cfg(gdb)]
440-
&sandbox.debug_info,
441-
)?;
443+
let mut vm = set_up_hypervisor_partition(&mut gshm, &config)?;
442444
vm.initialise(
443445
RawPtr::from(0x230000),
444446
1234567890,

src/hyperlight_host/src/sandbox/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
use std::cmp::max;
18+
use std::time::Duration;
1819

1920
use tracing::{instrument, Span};
2021

@@ -55,6 +56,8 @@ pub struct SandboxConfiguration {
5556
/// field should be represented as an `Option`, that type is not
5657
/// FFI-safe, so it cannot be.
5758
heap_size_override: u64,
59+
/// Interrupt retry delay
60+
interrupt_retry_delay: Duration,
5861
}
5962

6063
impl SandboxConfiguration {
@@ -66,6 +69,8 @@ impl SandboxConfiguration {
6669
pub const DEFAULT_OUTPUT_SIZE: usize = 0x4000;
6770
/// The minimum size of output data
6871
pub const MIN_OUTPUT_SIZE: usize = 0x2000;
72+
/// The default interrupt retry delay
73+
pub const DEFAULT_INTERRUPT_RETRY_DELAY: Duration = Duration::from_micros(500);
6974

7075
#[allow(clippy::too_many_arguments)]
7176
/// Create a new configuration for a sandbox with the given sizes.
@@ -75,13 +80,15 @@ impl SandboxConfiguration {
7580
output_data_size: usize,
7681
stack_size_override: Option<u64>,
7782
heap_size_override: Option<u64>,
83+
interrutp_retry_delay: Duration,
7884
#[cfg(gdb)] guest_debug_info: Option<DebugInfo>,
7985
) -> Self {
8086
Self {
8187
input_data_size: max(input_data_size, Self::MIN_INPUT_SIZE),
8288
output_data_size: max(output_data_size, Self::MIN_OUTPUT_SIZE),
8389
stack_size_override: stack_size_override.unwrap_or(0),
8490
heap_size_override: heap_size_override.unwrap_or(0),
91+
interrupt_retry_delay: interrutp_retry_delay,
8592

8693
#[cfg(gdb)]
8794
guest_debug_info,
@@ -114,6 +121,16 @@ impl SandboxConfiguration {
114121
self.heap_size_override = heap_size;
115122
}
116123

124+
/// Sets the interrupt retry delay
125+
pub fn set_interrupt_retry_delay(&mut self, delay: Duration) {
126+
self.interrupt_retry_delay = delay;
127+
}
128+
129+
/// Get the delay between retries for interrupts
130+
pub fn get_interrupt_retry_delay(&self) -> Duration {
131+
self.interrupt_retry_delay
132+
}
133+
117134
/// Sets the configuration for the guest debug
118135
#[cfg(gdb)]
119136
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
@@ -172,6 +189,7 @@ impl Default for SandboxConfiguration {
172189
Self::DEFAULT_OUTPUT_SIZE,
173190
None,
174191
None,
192+
Self::DEFAULT_INTERRUPT_RETRY_DELAY,
175193
#[cfg(gdb)]
176194
None,
177195
)
@@ -194,6 +212,7 @@ mod tests {
194212
OUTPUT_DATA_SIZE_OVERRIDE,
195213
Some(STACK_SIZE_OVERRIDE),
196214
Some(HEAP_SIZE_OVERRIDE),
215+
SandboxConfiguration::DEFAULT_INTERRUPT_RETRY_DELAY,
197216
#[cfg(gdb)]
198217
None,
199218
);
@@ -219,6 +238,7 @@ mod tests {
219238
SandboxConfiguration::MIN_OUTPUT_SIZE - 1,
220239
None,
221240
None,
241+
SandboxConfiguration::DEFAULT_INTERRUPT_RETRY_DELAY,
222242
#[cfg(gdb)]
223243
None,
224244
);

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use std::sync::{Arc, Mutex};
2222
use log::LevelFilter;
2323
use tracing::{instrument, Span};
2424

25-
#[cfg(gdb)]
26-
use super::config::DebugInfo;
2725
use super::host_funcs::{default_writer_func, FunctionRegistry};
2826
use super::mem_mgr::MemMgrWrapper;
2927
use super::uninitialized_evolve::evolve_impl_multi_use;
@@ -67,8 +65,7 @@ pub struct UninitializedSandbox {
6765
/// The memory manager for the sandbox.
6866
pub(crate) mgr: MemMgrWrapper<ExclusiveSharedMemory>,
6967
pub(crate) max_guest_log_level: Option<LevelFilter>,
70-
#[cfg(gdb)]
71-
pub(crate) debug_info: Option<DebugInfo>,
68+
pub(crate) config: SandboxConfiguration,
7269
}
7370

7471
impl crate::sandbox_state::sandbox::UninitializedSandbox for UninitializedSandbox {
@@ -160,8 +157,6 @@ impl UninitializedSandbox {
160157

161158
let sandbox_cfg = cfg.unwrap_or_default();
162159

163-
#[cfg(gdb)]
164-
let debug_info = sandbox_cfg.get_guest_debug_info();
165160
let mut mem_mgr_wrapper = {
166161
let mut mgr = UninitializedSandbox::load_guest_binary(sandbox_cfg, &guest_binary)?;
167162
let stack_guard = Self::create_stack_guard();
@@ -177,8 +172,7 @@ impl UninitializedSandbox {
177172
host_funcs,
178173
mgr: mem_mgr_wrapper,
179174
max_guest_log_level: None,
180-
#[cfg(gdb)]
181-
debug_info,
175+
config: sandbox_cfg,
182176
};
183177

184178
// If we were passed a writer for host print register it otherwise use the default.

src/hyperlight_host/src/sandbox/uninitialized_evolve.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use tracing::{instrument, Span};
2222
use super::hypervisor::{get_available_hypervisor, HypervisorType};
2323
#[cfg(gdb)]
2424
use super::mem_access::dbg_mem_access_handler_wrapper;
25+
use super::SandboxConfiguration;
2526
use crate::hypervisor::handlers::{MemAccessHandlerCaller, OutBHandlerCaller};
2627
use crate::hypervisor::Hypervisor;
2728
use crate::mem::layout::SandboxMemoryLayout;
@@ -68,11 +69,7 @@ where
6869
) -> Result<ResSandbox>,
6970
{
7071
let (hshm, mut gshm) = u_sbox.mgr.build();
71-
let mut vm = set_up_hypervisor_partition(
72-
&mut gshm,
73-
#[cfg(gdb)]
74-
&u_sbox.debug_info,
75-
)?;
72+
let mut vm = set_up_hypervisor_partition(&mut gshm, &u_sbox.config)?;
7673
let outb_hdl = outb_handler_wrapper(hshm.clone(), u_sbox.host_funcs.clone());
7774

7875
let seed = {
@@ -143,7 +140,7 @@ pub(super) fn evolve_impl_multi_use(u_sbox: UninitializedSandbox) -> Result<Mult
143140

144141
pub(crate) fn set_up_hypervisor_partition(
145142
mgr: &mut SandboxMemoryManager<GuestSharedMemory>,
146-
#[cfg(gdb)] debug_info: &Option<DebugInfo>,
143+
#[cfg_attr(target_os = "windows", allow(unused_variables))] config: &SandboxConfiguration,
147144
) -> Result<Box<dyn Hypervisor>> {
148145
let mem_size = u64::try_from(mgr.shared_mem.mem_size())?;
149146
let mut regions = mgr.layout.get_memory_regions(&mgr.shared_mem)?;
@@ -179,10 +176,10 @@ pub(crate) fn set_up_hypervisor_partition(
179176

180177
// Create gdb thread if gdb is enabled and the configuration is provided
181178
#[cfg(gdb)]
182-
let gdb_conn = if let Some(DebugInfo { port }) = debug_info {
179+
let gdb_conn = if let Some(DebugInfo { port }) = config.get_guest_debug_info() {
183180
use crate::hypervisor::gdb::create_gdb_thread;
184181

185-
let gdb_conn = create_gdb_thread(*port, unsafe { libc::pthread_self() });
182+
let gdb_conn = create_gdb_thread(port, unsafe { libc::pthread_self() });
186183

187184
// in case the gdb thread creation fails, we still want to continue
188185
// without gdb
@@ -206,6 +203,7 @@ pub(crate) fn set_up_hypervisor_partition(
206203
entrypoint_ptr,
207204
rsp_ptr,
208205
pml4_ptr,
206+
config,
209207
#[cfg(gdb)]
210208
gdb_conn,
211209
)?;
@@ -219,6 +217,7 @@ pub(crate) fn set_up_hypervisor_partition(
219217
pml4_ptr.absolute()?,
220218
entrypoint_ptr.absolute()?,
221219
rsp_ptr.absolute()?,
220+
config,
222221
#[cfg(gdb)]
223222
gdb_conn,
224223
)?;

0 commit comments

Comments
 (0)