Skip to content

Commit 733f137

Browse files
committed
更新依赖项并移除不必要的异常处理代码
1 parent 5cb7922 commit 733f137

File tree

5 files changed

+40
-217
lines changed

5 files changed

+40
-217
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tock-registers = "0.9"
2626

2727
axerrno = "0.1.0"
2828
percpu = {version = "0.2.0", features = ["arm-el2"]}
29+
axcpu = {path="../axcpu", features = ["arm-el2"]}
2930

3031
axaddrspace = {git = "https://github.com/arceos-hypervisor/axaddrspace.git"}
3132
axdevice_base = {git = "https://github.com/arceos-hypervisor/axdevice_crates.git"}

src/exception.S

Lines changed: 0 additions & 140 deletions
This file was deleted.

src/exception.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ pub enum TrapKind {
2828
}
2929
}
3030

31-
/// Equals to [`TrapKind::Synchronous`], used in exception.S.
32-
const EXCEPTION_SYNC: usize = TrapKind::Synchronous as usize;
33-
/// Equals to [`TrapKind::Irq`], used in exception.S.
34-
const EXCEPTION_IRQ: usize = TrapKind::Irq as usize;
35-
3631
#[repr(u8)]
3732
#[derive(Debug)]
3833
#[allow(unused)]
@@ -43,12 +38,6 @@ enum TrapSource {
4338
LowerAArch32 = 3,
4439
}
4540

46-
core::arch::global_asm!(
47-
include_str!("exception.S"),
48-
exception_sync = const EXCEPTION_SYNC,
49-
exception_irq = const EXCEPTION_IRQ,
50-
);
51-
5241
/// Handles synchronous exceptions that occur during the execution of a guest VM.
5342
///
5443
/// This function examines the exception class (EC) to determine the cause of the exception
@@ -276,33 +265,6 @@ fn handle_smc64_exception(ctx: &mut TrapFrame) -> AxResult<AxVCpuExitReason> {
276265
}
277266
}
278267

279-
/// Handles IRQ exceptions that occur from the current exception level.
280-
/// Dispatches IRQs to the appropriate handler provided by the underlying host OS,
281-
/// which is registered at [`crate::pcpu::IRQ_HANDLER`] during `Aarch64PerCpu::new()`.
282-
#[unsafe(no_mangle)]
283-
fn current_el_irq_handler(_tf: &mut TrapFrame) {
284-
unsafe { crate::pcpu::IRQ_HANDLER.current_ref_raw() }
285-
.get()
286-
.unwrap()()
287-
}
288-
289-
/// Handles synchronous exceptions that occur from the current exception level.
290-
#[unsafe(no_mangle)]
291-
fn current_el_sync_handler(tf: &mut TrapFrame) {
292-
let esr = ESR_EL2.extract();
293-
let ec = ESR_EL2.read(ESR_EL2::EC);
294-
let iss = ESR_EL2.read(ESR_EL2::ISS);
295-
296-
error!("ESR_EL2: {:#x}", esr.get());
297-
error!("Exception Class: {ec:#x}");
298-
error!("Instruction Specific Syndrome: {iss:#x}");
299-
300-
panic!(
301-
"Unhandled synchronous exception from current EL: {:#x?}",
302-
tf
303-
);
304-
}
305-
306268
/// A trampoline function for sp switching during handling VM exits,
307269
/// when **there is a active VCPU running**, which means that the host context is stored
308270
/// into host stack in `run_guest` function.
@@ -340,7 +302,7 @@ fn current_el_sync_handler(tf: &mut TrapFrame) {
340302
/// invoked as part of the low-level hypervisor or VM exit handling routines.
341303
#[unsafe(naked)]
342304
#[unsafe(no_mangle)]
343-
unsafe extern "C" fn vmexit_trampoline() -> ! {
305+
unsafe extern "C" fn handle_vmexit() -> ! {
344306
core::arch::naked_asm!(
345307
// Curretly `sp` points to the base address of `Aarch64VCpu.ctx`, which stores guest's `TrapFrame`.
346308
"add x9, sp, 34 * 8", // Skip the exception frame.
@@ -351,12 +313,3 @@ unsafe extern "C" fn vmexit_trampoline() -> ! {
351313
"ret", // Control flow is handed back to Aarch64VCpu.run(), simulating the normal return of the `run_guest` function.
352314
)
353315
}
354-
355-
/// Deal with invalid aarch64 exception.
356-
#[unsafe(no_mangle)]
357-
fn invalid_exception_el2(tf: &mut TrapFrame, kind: TrapKind, source: TrapSource) {
358-
panic!(
359-
"Invalid exception {:?} from {:?}:\n{:#x?}",
360-
kind, source, tf
361-
);
362-
}

src/pcpu.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{cell::OnceCell, marker::PhantomData};
1+
use core::marker::PhantomData;
22

33
use aarch64_cpu::registers::*;
44
use axerrno::AxResult;
@@ -14,27 +14,8 @@ pub struct Aarch64PerCpu<H: AxVCpuHal> {
1414
_phantom: PhantomData<H>,
1515
}
1616

17-
#[percpu::def_percpu]
18-
static ORI_EXCEPTION_VECTOR_BASE: usize = 0;
19-
20-
/// IRQ handler registered by underlying host OS during per-cpu initialization,
21-
/// for dispatching IRQs to the host OS.
22-
///
23-
/// Set `IRQ_HANDLER` as per-cpu variable to avoid the need of `OnceLock`.
24-
#[percpu::def_percpu]
25-
pub static IRQ_HANDLER: OnceCell<&(dyn Fn() + Send + Sync)> = OnceCell::new();
26-
27-
unsafe extern "C" {
28-
fn exception_vector_base_vcpu();
29-
}
30-
3117
impl<H: AxVCpuHal> AxArchPerCpu for Aarch64PerCpu<H> {
3218
fn new(cpu_id: usize) -> AxResult<Self> {
33-
// Register IRQ handler for this CPU.
34-
let _ = unsafe { IRQ_HANDLER.current_ref_mut_raw() }
35-
.set(&|| H::irq_hanlder())
36-
.map(|_| {});
37-
3819
Ok(Self {
3920
cpu_id,
4021
_phantom: PhantomData,
@@ -49,11 +30,11 @@ impl<H: AxVCpuHal> AxArchPerCpu for Aarch64PerCpu<H> {
4930
// First we save origin `exception_vector_base`.
5031
// Safety:
5132
// Todo: take care of `preemption`
52-
unsafe { ORI_EXCEPTION_VECTOR_BASE.write_current_raw(VBAR_EL2.get() as usize) }
33+
// unsafe { ORI_EXCEPTION_VECTOR_BASE.write_current_raw(VBAR_EL2.get() as usize) }
5334

5435
// Set current `VBAR_EL2` to `exception_vector_base_vcpu`
5536
// defined in this crate.
56-
VBAR_EL2.set(exception_vector_base_vcpu as usize as _);
37+
// VBAR_EL2.set(exception_vector_base_vcpu as usize as _);
5738

5839
HCR_EL2.modify(
5940
HCR_EL2::VM::Enable
@@ -83,7 +64,7 @@ impl<H: AxVCpuHal> AxArchPerCpu for Aarch64PerCpu<H> {
8364
// Reset `VBAR_EL2` into previous value.
8465
// Safety:
8566
// Todo: take care of `preemption`
86-
VBAR_EL2.set(unsafe { ORI_EXCEPTION_VECTOR_BASE.read_current_raw() } as _);
67+
// VBAR_EL2.set(unsafe { ORI_EXCEPTION_VECTOR_BASE.read_current_raw() } as _);
8768

8869
HCR_EL2.set(HCR_EL2::VM::Disable.into());
8970
Ok(())

src/vcpu.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,37 @@ impl<H: AxVCpuHal> Aarch64VCpu<H> {
232232

233233
/// Private functions related to vcpu runtime control flow.
234234
impl<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

Comments
 (0)