|
| 1 | +use core::fmt::Arguments; |
| 2 | + |
1 | 3 | use aarch64_cpu::registers::*; |
2 | 4 | use axerrno::AxResult; |
3 | 5 | use axvm_types::{ |
@@ -88,23 +90,43 @@ impl Aarch64VCpu { |
88 | 90 | } |
89 | 91 |
|
90 | 92 | pub fn set_dtb_addr(&mut self, dtb_addr: GuestPhysAddr) -> AxResult { |
91 | | - debug!("set vcpu dtb addr:{dtb_addr:?}"); |
| 93 | + debug!("vCPU{} set vcpu dtb addr:{dtb_addr:?}", self.mpidr); |
92 | 94 | self.ctx.set_argument(dtb_addr.as_usize()); |
93 | 95 | Ok(()) |
94 | 96 | } |
95 | 97 |
|
96 | 98 | pub fn set_entry(&mut self, entry: GuestPhysAddr) -> AxResult { |
97 | | - debug!("set vcpu entry:{entry:?}"); |
| 99 | + debug!("vCPU{} set vcpu entry:{entry:?}", self.mpidr); |
98 | 100 | self.set_elr(entry.as_usize()); |
99 | 101 | Ok(()) |
100 | 102 | } |
101 | 103 |
|
102 | 104 | pub fn set_ept_root(&mut self, ept_root: HostPhysAddr) -> AxResult { |
103 | | - debug!("set vcpu ept root:{ept_root:#x}"); |
| 105 | + debug!("vCPU{} set vcpu ept root:{ept_root:#x}", self.mpidr); |
104 | 106 | self.guest_system_regs.vttbr_el2 = ept_root.as_usize() as u64; |
105 | 107 | Ok(()) |
106 | 108 | } |
107 | 109 |
|
| 110 | + pub fn setup_current_cpu(&mut self, vmid: usize) -> AxResult { |
| 111 | + // Set VMID then invalidate stage-2 TLB for this VMID to avoid stale translations. |
| 112 | + let vmid_mask: u64 = 0xffff << 48; |
| 113 | + let mut vttbr = self.guest_system_regs.vttbr_el2; |
| 114 | + vttbr = (vttbr & !vmid_mask) | ((vmid as u64 & 0xffff) << 48); |
| 115 | + VTTBR_EL2.set(vttbr); |
| 116 | + |
| 117 | + unsafe { |
| 118 | + core::arch::asm!( |
| 119 | + "dsb ishst", // ensure VTTBR write visible before TLB invalidation |
| 120 | + "tlbi vmalls12e1is", // invalidate stage-2 by VMID (inner-shareable) |
| 121 | + "dsb ish", // ensure completion of invalidation |
| 122 | + "isb", // sync context |
| 123 | + options(nostack, preserves_flags) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | + |
108 | 130 | pub fn run(&mut self) -> AxResult<AxVCpuExitReason> { |
109 | 131 | // Run guest. |
110 | 132 | let exit_reson = unsafe { |
|
0 commit comments