Skip to content

Commit

Permalink
fix virtual wfi inst
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciechozga committed Jan 21, 2025
1 parent 13f3332 commit 565042b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl<'a> ConfidentialFlow<'a> {
self.confidential_hart().confidential_hart_id()
}

fn confidential_hart_mut(&mut self) -> &mut ConfidentialHart {
pub fn confidential_hart_mut(&mut self) -> &mut ConfidentialHart {
self.hardware_hart.confidential_hart_mut()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ impl TimeRequest {
}

pub fn handle(self, confidential_flow: ConfidentialFlow) -> ! {
let time = CSR.time.read();
let addr = 0x200BFF8;
let time = unsafe { (addr as *const u64).read_volatile() } as usize;
// let time = CSR.time.read();
confidential_flow.apply_and_exit_to_confidential_hart(ApplyToConfidentialHart::SbiResponse(SbiResponse::success_with_code(time)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// SPDX-License-Identifier: Apache-2.0
use crate::confidential_flow::{ApplyToConfidentialHart, ConfidentialFlow};
use crate::core::architecture::riscv::specification::WFI_INSTRUCTION;
use crate::core::control_data::ConfidentialHart;
use crate::core::control_data::{ConfidentialHart, HypervisorHart};
use crate::non_confidential_flow::DeclassifyToHypervisor;

/// Handles virtual instruction trap that occured during execution of the confidential hart.
pub struct VirtualInstruction {
Expand All @@ -19,18 +20,27 @@ impl VirtualInstruction {
Self { instruction, instruction_length }
}

pub fn handle(self, confidential_flow: ConfidentialFlow) -> ! {
let transformation = if self.instruction == WFI_INSTRUCTION {
ApplyToConfidentialHart::VirtualInstruction(self)
} else {
// TODO: add support for some CSR manipulation
// TODO: for not supported instructions, inject illegal instruction exception to the guest
panic!("Not supported virtual instruction: {:x}", self.instruction);
};
confidential_flow.apply_and_exit_to_confidential_hart(transformation)
pub fn handle(self, mut confidential_flow: ConfidentialFlow) -> ! {
confidential_flow.confidential_hart_mut().csrs_mut().mepc.add(self.instruction_length);

// let transformation = if self.instruction == WFI_INSTRUCTION {
// ApplyToConfidentialHart::VirtualInstruction(self)
// } else {
// // TODO: add support for some CSR manipulation
// // TODO: for not supported instructions, inject illegal instruction exception to the guest
// panic!("Not supported virtual instruction: {:x}", self.instruction);
// };
confidential_flow.into_non_confidential_flow().declassify_and_exit_to_hypervisor(DeclassifyToHypervisor::VirtualInstruction(self))
}

pub fn apply_to_confidential_hart(&self, confidential_hart: &mut ConfidentialHart) {
confidential_hart.csrs_mut().mepc.add(self.instruction_length);
// confidential_hart.csrs_mut().mepc.add(self.instruction_length);
}

pub fn declassify_to_hypervisor_hart(&self, hypervisor_hart: &mut HypervisorHart) {
use crate::confidential_flow::handlers::sbi::SbiResponse;
use crate::core::architecture::specification::{MIE_SSIP_MASK, SCAUSE_INTERRUPT_MASK};
hypervisor_hart.csrs_mut().scause.write(MIE_SSIP_MASK | SCAUSE_INTERRUPT_MASK);
SbiResponse::success().declassify_to_hypervisor_hart(hypervisor_hart);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::confidential_flow::handlers::interrupts::{ExposeEnabledInterrupts, HandleInterrupt};
use crate::confidential_flow::handlers::mmio::{MmioLoadRequest, MmioStoreRequest};
use crate::confidential_flow::handlers::sbi::{SbiRequest, SbiResponse};
use crate::confidential_flow::handlers::virtual_instructions::VirtualInstruction;

/// Declassifiers that expose part of the confidential VM's hart state to the hypervisor.
pub enum DeclassifyToHypervisor {
Expand All @@ -13,4 +14,5 @@ pub enum DeclassifyToHypervisor {
MmioLoadRequest(MmioLoadRequest),
MmioStoreRequest(MmioStoreRequest),
EnabledInterrupts(ExposeEnabledInterrupts),
VirtualInstruction(VirtualInstruction),
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl<'a> NonConfidentialFlow<'a> {
DeclassifyToHypervisor::MmioLoadRequest(v) => v.declassify_to_hypervisor_hart(self.hypervisor_hart_mut()),
DeclassifyToHypervisor::MmioStoreRequest(v) => v.declassify_to_hypervisor_hart(self.hypervisor_hart_mut()),
DeclassifyToHypervisor::EnabledInterrupts(v) => v.declassify_to_hypervisor_hart(self.hypervisor_hart_mut()),
DeclassifyToHypervisor::VirtualInstruction(v) => v.declassify_to_hypervisor_hart(self.hypervisor_hart_mut()),
}
self
}
Expand Down

0 comments on commit 565042b

Please sign in to comment.