@@ -232,6 +232,37 @@ impl<H: AxVCpuHal> Aarch64VCpu<H> {
232232
233233/// Private functions related to vcpu runtime control flow.
234234impl < H : AxVCpuHal > Aarch64VCpu < H > {
235+ // /// Save host context and run guest.
236+ // ///
237+ // /// When a VM-Exit happens when guest's vCpu is running,
238+ // /// the control flow will be redirected to this function through `return_run_guest`.
239+ // #[unsafe(naked)]
240+ // unsafe extern "C" fn run_guest(&mut self) -> usize {
241+ // // Fixes: https://github.com/arceos-hypervisor/arm_vcpu/issues/22
242+ // //
243+ // // The original issue seems to be caused by an unexpected compiler optimization that takes
244+ // // the dummy return value `0` of `run_guest` as the actual return value. By replacing the
245+ // // original `run_guest` with the current naked one, we eliminate the dummy code path of the
246+ // // original version, and ensure that the compiler does not perform any unexpected return
247+ // // value optimization.
248+ // core::arch::naked_asm!(
249+ // // Save host context.
250+ // save_regs_to_stack!(),
251+ // // Save current host stack top to `self.host_stack_top`.
252+ // //
253+ // // 'extern "C"' here specifies the aapcs64 calling convention, according to which
254+ // // the first and only parameter, the pointer of self, should be in x0:
255+ // "mov x9, sp",
256+ // "add x0, x0, {host_stack_top_offset}",
257+ // "str x9, [x0]",
258+ // // Go to `context_vm_entry`.
259+ // "b context_vm_entry",
260+ // // Panic if the control flow comes back here, which should never happen.
261+ // "b {run_guest_panic}",
262+ // host_stack_top_offset = const core::mem::size_of::<TrapFrame>(),
263+ // run_guest_panic = sym Self::run_guest_panic,
264+ // );
265+ // }
235266 /// Save host context and run guest.
236267 ///
237268 /// When a VM-Exit happens when guest's vCpu is running,
@@ -256,14 +287,13 @@ impl<H: AxVCpuHal> Aarch64VCpu<H> {
256287 "add x0, x0, {host_stack_top_offset}" ,
257288 "str x9, [x0]" ,
258289 // Go to `context_vm_entry`.
259- "b context_vm_entry" ,
260- // Panic if the control flow comes back here, which should never happen.
290+ "b {entry}" ,
261291 "b {run_guest_panic}" ,
262292 host_stack_top_offset = const core:: mem:: size_of:: <TrapFrame >( ) ,
293+ entry = sym axcpu:: el2:: enter_guest,
263294 run_guest_panic = sym Self :: run_guest_panic,
264295 ) ;
265296 }
266-
267297 /// This function is called when the control flow comes back to `run_guest`. To provide a error
268298 /// message for debugging purposes.
269299 ///
@@ -325,9 +355,7 @@ impl<H: AxVCpuHal> Aarch64VCpu<H> {
325355
326356 let result = match exit_reason {
327357 TrapKind :: Synchronous => handle_exception_sync ( & mut self . ctx ) ,
328- TrapKind :: Irq => Ok ( AxVCpuExitReason :: ExternalInterrupt {
329- vector : H :: irq_fetch ( ) as _ ,
330- } ) ,
358+ TrapKind :: Irq => Ok ( AxVCpuExitReason :: ExternalInterrupt { vector : 0 } ) ,
331359 _ => panic ! ( "Unhandled exception {:?}" , exit_reason) ,
332360 } ;
333361
0 commit comments