Skip to content

Commit

Permalink
Move usermode halt logic into backing-specific logic
Browse files Browse the repository at this point in the history
  • Loading branch information
smalis-msft committed Oct 21, 2024
1 parent 2e5acb8 commit a217241
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
22 changes: 14 additions & 8 deletions openhcl/virt_mshv_vtl/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ mod private {
stop: &mut StopVp<'_>,
) -> impl Future<Output = Result<(), VpHaltReason<UhRunVpError>>>;

/// Returns true if the VP is ready to run the given VTL, false if it is halted.
/// Process any pending APIC work.
fn poll_apic(
this: &mut UhProcessor<'_, Self>,
vtl: Vtl,
scan_irr: bool,
) -> Result<bool, UhRunVpError>;
) -> Result<(), UhRunVpError>;

/// Requests the VP to exit when an external interrupt is ready to be
/// delivered.
Expand All @@ -235,6 +235,13 @@ mod private {
/// the target VTL that will become active.
fn switch_vtl_state(this: &mut UhProcessor<'_, Self>, target_vtl: Vtl);

/// Returns whether this VP should be put to sleep in usermode, or
/// whether it's ready to proceed into the kernel.
fn halt_in_usermode(this: &mut UhProcessor<'_, Self>, target_vtl: Vtl) -> bool {
let _ = (this, target_vtl);
false
}

fn inspect_extra(_this: &mut UhProcessor<'_, Self>, _resp: &mut inspect::Response<'_>) {}
}
}
Expand Down Expand Up @@ -654,15 +661,13 @@ impl<'p, T: Backing> Processor for UhProcessor<'p, T> {
self.update_synic(Vtl::Vtl0, true);
}

// TODO CVM GUEST VSM: Split ready into two to track per-vtl
let mut ready = false;
for vtl in [Vtl::Vtl1, Vtl::Vtl0] {
// Process interrupts.
if self.hv(vtl).is_some() {
self.update_synic(vtl, false);
}

ready |= T::poll_apic(self, vtl, scan_irr[vtl] || first_scan_irr)
T::poll_apic(self, vtl, scan_irr[vtl] || first_scan_irr)
.map_err(VpHaltReason::Hypervisor)?;
}
first_scan_irr = false;
Expand All @@ -675,11 +680,12 @@ impl<'p, T: Backing> Processor for UhProcessor<'p, T> {
}
}

if ready {
// TODO WHP GUEST VSM: This should be next_vtl
if T::halt_in_usermode(self, Vtl::Vtl0) {
break Poll::Pending;
} else {
return <Result<_, VpHaltReason<_>>>::Ok(()).into();
}

break Poll::Pending;
})
.await?;

Expand Down
11 changes: 3 additions & 8 deletions openhcl/virt_mshv_vtl/src/processor/mshv/apic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ impl UhApicState {

impl UhProcessor<'_, HypervisorBackedX86> {
/// Returns true if the VP is ready to run, false if it is halted.
pub(super) fn poll_apic(&mut self, vtl: Vtl, scan_irr: bool) -> Result<bool, UhRunVpError> {
pub(super) fn poll_apic(&mut self, vtl: Vtl, scan_irr: bool) -> Result<(), UhRunVpError> {
let Some(lapics) = self.backing.lapics.as_mut() else {
return Ok(true);
return Ok(());
};

let lapic = &mut lapics[vtl];
Expand Down Expand Up @@ -304,12 +304,7 @@ impl UhProcessor<'_, HypervisorBackedX86> {
self.handle_sipi(vtl, vector)?;
}

let lapic = &self.backing.lapics.as_ref().unwrap()[vtl];
if lapic.halted || lapic.startup_suspend {
return Ok(false);
}

Ok(true)
Ok(())
}

fn handle_init(&mut self, vtl: Vtl) -> Result<(), UhRunVpError> {
Expand Down
4 changes: 2 additions & 2 deletions openhcl/virt_mshv_vtl/src/processor/mshv/arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ impl BackingPrivate for HypervisorBackedArm64 {
_this: &mut UhProcessor<'_, Self>,
_vtl: Vtl,
_scan_irr: bool,
) -> Result<bool, UhRunVpError> {
Ok(true)
) -> Result<(), UhRunVpError> {
Ok(())
}

fn request_extint_readiness(this: &mut UhProcessor<'_, Self>) {
Expand Down
11 changes: 10 additions & 1 deletion openhcl/virt_mshv_vtl/src/processor/mshv/x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,19 @@ impl BackingPrivate for HypervisorBackedX86 {
this: &mut UhProcessor<'_, Self>,
vtl: Vtl,
scan_irr: bool,
) -> Result<bool, UhRunVpError> {
) -> Result<(), UhRunVpError> {
this.poll_apic(vtl, scan_irr)
}

fn halt_in_usermode(this: &mut UhProcessor<'_, Self>, target_vtl: Vtl) -> bool {
if let Some(lapics) = this.backing.lapics.as_ref() {
if lapics[target_vtl].halted || lapics[target_vtl].startup_suspend {
return true;
}
}
false
}

fn request_extint_readiness(this: &mut UhProcessor<'_, Self>) {
this.backing
.next_deliverability_notifications
Expand Down
7 changes: 2 additions & 5 deletions openhcl/virt_mshv_vtl/src/processor/snp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl BackingPrivate for SnpBacked {
this: &mut UhProcessor<'_, Self>,
vtl: Vtl,
scan_irr: bool,
) -> Result<bool, UhRunVpError> {
) -> Result<(), UhRunVpError> {
// Check for interrupt requests from the host.
// TODO SNP GUEST VSM supporting VTL 1 proxy irrs requires kernel changes
if vtl == Vtl::Vtl0 {
Expand Down Expand Up @@ -388,10 +388,7 @@ impl BackingPrivate for SnpBacked {
}
}

// Return ready even if halted. `run_vp` will wait in the kernel when
// halted, which is necessary so that we are efficiently notified when
// more interrupts arrive.
Ok(true)
Ok(())
}

fn request_extint_readiness(_this: &mut UhProcessor<'_, Self>) {
Expand Down
7 changes: 2 additions & 5 deletions openhcl/virt_mshv_vtl/src/processor/tdx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ impl BackingPrivate for TdxBacked {
this: &mut UhProcessor<'_, Self>,
_vtl: Vtl,
scan_irr: bool,
) -> Result<bool, UhRunVpError> {
) -> Result<(), UhRunVpError> {
if !this.try_poll_apic(scan_irr)? {
tracing::info!("disabling APIC offload due to auto EOI");
let page = zerocopy::transmute_mut!(this.runner.tdx_apic_page_mut());
Expand All @@ -741,10 +741,7 @@ impl BackingPrivate for TdxBacked {
this.try_poll_apic(false)?;
}

// Return ready even if halted. `run_vp` will wait in the kernel when
// halted, which is necessary so that we are efficiently notified when
// more interrupts arrive.
Ok(true)
Ok(())
}

fn request_extint_readiness(_this: &mut UhProcessor<'_, Self>) {
Expand Down

0 comments on commit a217241

Please sign in to comment.