@@ -15,15 +15,19 @@ use crate::non_confidential_flow::handlers::cove_host_extension::{
1515 DestroyConfidentialVm , GetSecurityMonitorInfo , PromoteToConfidentialVm , RunConfidentialHart ,
1616} ;
1717use crate :: non_confidential_flow:: handlers:: nested_acceleration_extension:: { NaclProbeFeature , NaclSetupSharedMemory } ;
18- use crate :: non_confidential_flow:: handlers:: opensbi:: { DelegateToOpensbi , ProbeSbiExtension } ;
19- use crate :: non_confidential_flow:: handlers:: supervisor_binary_interface:: InvalidCall ;
18+ use crate :: non_confidential_flow:: handlers:: supervisor_binary_interface:: { InvalidCall , ProbeSbiExtension } ;
2019use crate :: non_confidential_flow:: { ApplyToHypervisorHart , DeclassifyToHypervisor } ;
20+ use opensbi_sys:: sbi_trap_regs;
2121
2222extern "C" {
2323 /// To ensure safety, specify all possible valid states that KVM expects to see and prove that security monitor
2424 /// never returns to KVM with other state. For example, only a subset of exceptions/interrupts can be handled by KVM.
2525 /// KVM kill the vcpu if it receives unexpected exception because it does not know what to do with it.
2626 fn exit_to_hypervisor_asm ( ) -> !;
27+
28+ /// Currently, we rely on OpenSBI to handle some of the interrupts or exceptions. Below function is the entry point
29+ /// to OpenSBI trap handler.
30+ fn sbi_trap_handler ( regs : * mut sbi_trap_regs ) -> * mut sbi_trap_regs ;
2731}
2832
2933/// Represents the non-confidential part of the finite state machine (FSM), implementing router and exit nodes. It encapsulates the
@@ -55,15 +59,9 @@ impl<'a> NonConfidentialFlow<'a> {
5559 // hardware hart's dump area in main memory. This area in main memory is exclusively owned by the physical hart executing this code.
5660 // Specifically, every physical hart has its own are in the main memory and its `mscratch` register stores the address. See the
5761 // `initialization` procedure for more details.
58- let flow = unsafe { Self :: create ( hart_ptr. as_mut ( ) . expect ( Self :: CTX_SWITCH_ERROR_MSG ) ) } ;
62+ let mut flow = unsafe { Self :: create ( hart_ptr. as_mut ( ) . expect ( Self :: CTX_SWITCH_ERROR_MSG ) ) } ;
5963 match TrapCause :: from_hart_architectural_state ( flow. hypervisor_hart ( ) . hypervisor_hart_state ( ) ) {
60- Interrupt => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
61- IllegalInstruction => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
62- LoadAddressMisaligned => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
63- LoadAccessFault => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
64- StoreAddressMisaligned => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
65- StoreAccessFault => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
66- HsEcall ( Base ( ProbeExtension ) ) => ProbeSbiExtension :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
64+ HsEcall ( Base ( ProbeExtension ) ) => ProbeSbiExtension :: from_hypervisor_hart ( flow. hypervisor_hart_mut ( ) ) . handle ( flow) ,
6765 HsEcall ( Covh ( TsmGetInfo ) ) => GetSecurityMonitorInfo :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
6866 HsEcall ( Covh ( PromoteToTvm ) ) => PromoteToConfidentialVm :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
6967 HsEcall ( Covh ( TvmVcpuRun ) ) => RunConfidentialHart :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
@@ -72,12 +70,21 @@ impl<'a> NonConfidentialFlow<'a> {
7270 HsEcall ( Nacl ( ProbeFeature ) ) => NaclProbeFeature :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
7371 HsEcall ( Nacl ( SetupSharedMemory ) ) => NaclSetupSharedMemory :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
7472 HsEcall ( Nacl ( _) ) => InvalidCall :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
75- HsEcall ( _) => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
76- MachineEcall => DelegateToOpensbi :: from_hypervisor_hart ( flow. hypervisor_hart ( ) ) . handle ( flow) ,
77- trap_reason => panic ! ( "Bug: Incorrect interrupt delegation configuration: {:?}" , trap_reason) ,
73+ _ => flow. delegate_to_opensbi ( ) ,
7874 }
7975 }
8076
77+ pub fn delegate_to_opensbi ( self ) -> ! {
78+ // Safety: We play with fire here. We must statically make sure that OpenSBI's input structure is bitwise same as ACE's hart state.
79+ let trap_regs = self . hardware_hart . hypervisor_hart_mut ( ) . hypervisor_hart_state_mut ( ) as * mut _ as * mut sbi_trap_regs ;
80+ let _ = self . hardware_hart . opensbi_context ( || {
81+ Ok ( unsafe {
82+ sbi_trap_handler ( trap_regs) ;
83+ } )
84+ } ) ;
85+ unsafe { exit_to_hypervisor_asm ( ) }
86+ }
87+
8188 /// Tries to traverse to confidential flow of the finite state machine (FSM). Returns error if the identifier of a confidential VM or
8289 /// hart are incorrect or cannot be scheduled for execution.
8390 pub fn into_confidential_flow (
@@ -113,18 +120,11 @@ impl<'a> NonConfidentialFlow<'a> {
113120 pub ( super ) fn apply_and_exit_to_hypervisor ( mut self , transformation : ApplyToHypervisorHart ) -> ! {
114121 match transformation {
115122 ApplyToHypervisorHart :: SbiResponse ( v) => v. apply_to_hypervisor_hart ( self . hypervisor_hart_mut ( ) ) ,
116- ApplyToHypervisorHart :: OpenSbiResponse ( v) => v. apply_to_hypervisor_hart ( self . hypervisor_hart_mut ( ) ) ,
117123 ApplyToHypervisorHart :: SetSharedMemory ( v) => v. apply_to_hypervisor_hart ( self . hypervisor_hart_mut ( ) ) ,
118124 }
119125 unsafe { exit_to_hypervisor_asm ( ) }
120126 }
121127
122- /// Swaps the mscratch register value with the original mascratch value used by OpenSBI. This function must be
123- /// called before executing any OpenSBI function. We can remove this once we get rid of the OpenSBI firmware.
124- pub fn swap_mscratch ( & mut self ) {
125- self . hardware_hart . swap_mscratch ( )
126- }
127-
128128 pub fn shared_memory ( & self ) -> & NaclSharedMemory {
129129 self . hypervisor_hart ( ) . shared_memory ( )
130130 }
0 commit comments