Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/arch/riscv64/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
addr::PHYS_VIRT_OFFSET, mm::PARKING_MEMORY_SET, GuestPhysAddr, HostPhysAddr, MemFlags,
MemoryRegion, VirtAddr, PARKING_INST_PAGE,
},
zone::find_zone,
};
use core::ptr::addr_of;

Expand Down Expand Up @@ -306,12 +305,3 @@ pub fn store_cpu_pointer_to_reg(pointer: usize) {
// println!("Stored CPU pointer to CSR_SSCRATCH: {:#x}", pointer);
return;
}

pub fn get_target_cpu(_irq: usize, zone_id: usize) -> usize {
find_zone(zone_id)
.unwrap()
.read()
.cpu_set()
.first_cpu()
.unwrap()
}
5 changes: 0 additions & 5 deletions src/arch/riscv64/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// ForeverYolo <2572131118@qq.com>

use crate::config::CONFIG_MAGIC_VERSION;
use crate::device::virtio_trampoline::MAX_DEVS;
use crate::hypercall::HyperCall;
use crate::hypercall::HyperCallResult;

Expand All @@ -29,10 +28,6 @@ impl<'a> HyperCall<'a> {
HyperCallResult::Ok(0)
}

pub fn wait_for_interrupt(&mut self, _irq_list: &mut [u64; MAX_DEVS + 1]) {
trace!("wait_for_interrupt is not need for RISC-V");
}

pub fn hv_zone_config_check(&self, magic_version: *mut u64) -> HyperCallResult {
unsafe {
*magic_version = CONFIG_MAGIC_VERSION as _;
Expand Down
4 changes: 0 additions & 4 deletions src/arch/x86_64/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,3 @@ pub fn store_cpu_pointer_to_reg(pointer: usize) {
// println!("x86_64 doesn't support store cpu pointer to reg, pointer: {:#x}", pointer);
return;
}

pub fn get_target_cpu(irq: usize, zone_id: usize) -> usize {
ioapic::get_irq_cpu(irq, zone_id)
}
5 changes: 0 additions & 5 deletions src/arch/x86_64/hypercall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use crate::{
config::CONFIG_MAGIC_VERSION,
cpu_data::this_zone,
device::virtio_trampoline::MAX_DEVS,
hypercall::{HyperCall, HyperCallResult},
zone::{Zone, ZoneInfo},
};
Expand All @@ -41,10 +40,6 @@ impl<'a> HyperCall<'a> {
HyperCallResult::Ok(0)
}

pub fn wait_for_interrupt(&mut self, irq_list: &mut [u64; MAX_DEVS + 1]) {
trace!("wait_for_interrupt is not need for x86_64");
}

pub fn hv_get_real_pa(&mut self, config_addr: u64) -> u64 {
unsafe {
this_zone()
Expand Down
19 changes: 13 additions & 6 deletions src/arch/x86_64/ipi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

use crate::{
arch::{
acpi::{get_apic_id, get_cpu_id},
acpi::{get_apic_id, try_get_cpu_id},
cpu::this_cpu_id,
idt::IdtVector,
},
cpu_data::{this_cpu_data, this_zone, CpuSet},
device::irqchip::inject_vector,
device::irqchip::post_guest_vector,
error::HvResult,
event,
hypercall::SGI_IPI_ID,
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn send_ipi(value: u64) -> HvResult {
let vector = value.get_bits(0..=7) as u8;
let delivery_mode: u8 = value.get_bits(8..=10) as u8;
let dest_shorthand = value.get_bits(18..=19) as u8;
let dest = get_cpu_id(value.get_bits(32..=39) as usize);
let dest_apic_id = value.get_bits(32..=39) as usize;
let cnt = value.get_bits(40..=63) as u32;

let mut cpu_set = this_zone().cpu_set();
Expand All @@ -97,7 +97,14 @@ pub fn send_ipi(value: u64) -> HvResult {

match dest_shorthand {
IpiDestShorthand::NO_SHORTHAND => {
dest_set.set_bit(dest);
// The guest picks the APIC ID; only CPUs of its own zone are valid.
match try_get_cpu_id(dest_apic_id).filter(|dest| cpu_set.contains_cpu(*dest)) {
Some(dest) => dest_set.set_bit(dest),
None => warn!(
"drop IPI from CPU{} to APIC ID {:#x} outside its zone",
cpu_id, dest_apic_id
),
}
}
IpiDestShorthand::SELF => {
dest_set.set_bit(cpu_id);
Expand All @@ -116,10 +123,10 @@ pub fn send_ipi(value: u64) -> HvResult {
match delivery_mode {
IpiDeliveryMode::FIXED => {
// info!("dest: {:x}, vector: {:x}", dest, vector);
inject_vector(dest, vector, None, false, false);
post_guest_vector(dest, vector, None, false, false);
}
IpiDeliveryMode::NMI => {
inject_vector(dest, 2, None, false, false);
post_guest_vector(dest, 2, None, false, false);
}
IpiDeliveryMode::INIT => {}
IpiDeliveryMode::START_UP => {
Expand Down
4 changes: 2 additions & 2 deletions src/arch/x86_64/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use crate::{
cpu_data::{this_cpu_data, this_zone},
device::{
irqchip::{
inject_vector,
pic::{ioapic::irqs, lapic::VirtLocalApic},
post_guest_vector,
},
uart::{virt_console_io_read, virt_console_io_write, UartReg},
},
Expand Down Expand Up @@ -140,7 +140,7 @@ fn handle_irq(vector: u8, path: IrqPath) {
IdtVector::APIC_SPURIOUS_VECTOR | IdtVector::APIC_ERROR_VECTOR if !is_timer => {}
_ => {
if vector >= 0x20 && this_cpu_data().vcpu.state.is_running() {
inject_vector(this_cpu_id(), vector, None, false, false);
post_guest_vector(this_cpu_id(), vector, None, false, false);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const GUEST_IRQ_NR: usize = 256;
pub const GUEST_LINES: &[usize] = &[];

// Architecture-private vCPU state placeholder.
// Guest interrupts still go through `PENDING_VECTORS` via `inject_vector()`.
// Guest interrupts still go through `PENDING_VECTORS` via `post_guest_vector()`.
pub struct ArchVcpu;

impl ArchVcpu {
Expand Down
146 changes: 64 additions & 82 deletions src/device/irqchip/aia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ use crate::platform::{
BOARD_APLIC_INTERRUPTS_NUM, IMSIC_GUEST_INDEX, IMSIC_GUEST_NUM, IMSIC_S_BASE,
};
use crate::root_zone_config;
use crate::zone::Zone;
use crate::zone::{find_zone, this_zone_id, IrqLifecycleGuard, IrqPostGuard, Zone};
use alloc::sync::Arc;
pub use aplic::*;
use heapless::FnvIndexMap;
pub use imsic::*;
use spin::Once;
use spin::{Mutex, Once};
pub use vaplic::*;
pub use vimsic::*;

// Physical APLIC
pub static APLIC: Once<Aplic> = Once::new();
// The MAX_ZONE_NUM should be the power of 2.
static mut VAPLIC_MAP: Option<FnvIndexMap<usize, VirtualAPLIC, MAX_ZONE_NUM>> = None;
static VAPLIC_MAP: Mutex<FnvIndexMap<usize, Arc<VirtualAPLIC>, MAX_ZONE_NUM>> =
Mutex::new(FnvIndexMap::new());
/// Per-zone IOMMU MSI page table (one 4 KiB frame, 16-byte PTEs; invalid entries are all-zero).
static mut MSI_PT_MAP: Option<FnvIndexMap<usize, Frame, MAX_ZONE_NUM>> = None;
static MSI_PT_MAP: Mutex<FnvIndexMap<usize, Frame, MAX_ZONE_NUM>> = Mutex::new(FnvIndexMap::new());

/// Physical base of the MSI page-table frame for `zone_id` (`vm_id`), after `vimsic_init`.
pub fn msi_pt_paddr_for_zone(zone_id: usize) -> Option<PhysAddr> {
// Once the MSI_PT for one zone is initialized, it wouldn't change.
// Here don't use lock, because it's read-only operation here.
unsafe { MSI_PT_MAP.as_ref()?.get(&zone_id).map(|f| f.start_paddr()) }
MSI_PT_MAP.lock().get(&zone_id).map(|f| f.start_paddr())
}

pub fn init_aplic(aplic_base: usize) {
Expand All @@ -71,11 +71,6 @@ pub fn primary_init_early() {
let root_config = root_zone_config();
init_aplic(root_config.arch_config.aplic_base as usize);
host_aplic().init_global(this_cpu_id(), BOARD_APLIC_INTERRUPTS_NUM);

unsafe {
VAPLIC_MAP = Some(FnvIndexMap::new());
MSI_PT_MAP = Some(FnvIndexMap::new());
}
}

pub fn primary_init_late() {
Expand Down Expand Up @@ -110,59 +105,63 @@ pub fn inject_irq(irq: usize, is_hardware: bool) {
}
// For hvisor on qemu-aia, only software interrupt will be injected.
// Hardware interrupt will be transferred to IMSIC's interrupt file directly.
let target = this_cpu_data()
.zone
.as_ref()
.unwrap()
.get_vaplic()
.vaplic_get_target(irq);
let mut hart = (target >> 18) & 0x3FFF;
let irq_post = IrqPostGuard::lock();
vaplic_pulse(&irq_post, this_zone_id(), irq);
}

/// Pulse software interrupt `irq` in zone `zone_id`'s vAPLIC; callable from any CPU.
pub fn vaplic_pulse(_irq_post: &IrqPostGuard, zone_id: usize, irq: usize) -> bool {
let Some(target) = VAPLIC_MAP
.lock()
.get(&zone_id)
.map(|vaplic| vaplic.vaplic_get_target(irq))
else {
return false;
};
let Some(cpu_set) = find_zone(zone_id).map(|zone| zone.read().cpu_set()) else {
return false;
};
let Some(first_cpu) = cpu_set.first_cpu() else {
return false;
};
let vhart = ((target >> 18) & 0x3FFF) as usize;
// let guest = (target >> 12) & 0x3F;
let eiid = target & 0x3FF;
// Transfer vhart_id to phart_id.
hart = hart
+ this_cpu_data()
.zone
.as_ref()
.unwrap()
.read()
.cpu_set()
.first_cpu()
.unwrap() as u32;
imsic_trigger(hart, IMSIC_GUEST_INDEX as u32, eiid);
// Transfer vhart_id to phart_id. The guest writes the target, so the hart
// must stay inside its zone.
let hart = vhart + first_cpu;
if !cpu_set.contains_cpu(hart) {
warn!(
"drop vAPLIC IRQ {} of zone {} to hart {} outside the zone",
irq, zone_id, hart
);
return false;
}
imsic_trigger(hart as u32, IMSIC_GUEST_INDEX as u32, eiid);
true
}

/// Print all keys in the VAPLIC_MAP for debugging purposes.
fn print_keys() {
info!("VAPLIC_MAP keys:");
unsafe {
if let Some(map) = &VAPLIC_MAP {
for (&key, _) in map.iter() {
info!("\tZone {}'s vaplic in VAPLIC_MAP", key);
}
}
for (&key, _) in VAPLIC_MAP.lock().iter() {
info!("\tZone {}'s vaplic in VAPLIC_MAP", key);
}
}

impl Zone {
/// Initial the virtual APLIC related to thiz Zone.
pub fn vaplic_init(&mut self, config: &HvZoneConfig) {
// Create a new VirtualAPLIC for this Zone.
unsafe {
if let Some(map) = &mut VAPLIC_MAP {
if map.contains_key(&self.id()) {
panic!("VirtualAPLIC for Zone {} already exists!", self.id());
}
let vaplic = vaplic::VirtualAPLIC::new(
config.arch_config.aplic_base,
BOARD_APLIC_INTERRUPTS_NUM,
);
// Insert into Map <zone_id, vplic>
let _ = map.insert(self.id(), vaplic);
} else {
panic!("VAPLIC_MAP is not initialized!");
}
let mut map = VAPLIC_MAP.lock();
if map.contains_key(&self.id()) {
panic!("VirtualAPLIC for Zone {} already exists!", self.id());
}
let vaplic =
vaplic::VirtualAPLIC::new(config.arch_config.aplic_base, BOARD_APLIC_INTERRUPTS_NUM);
// Insert into Map <zone_id, vplic>
let _ = map.insert(self.id(), Arc::new(vaplic));
drop(map);
info!(
"VirtualAPLIC for Zone {} initialized successfully",
self.id()
Expand All @@ -174,31 +173,24 @@ impl Zone {
pub fn vimsic_init(&self, _config: &HvZoneConfig) {
info!("Zone {} vIMSIC init", self.id());
let msi_pt = vimsic::vimsic_init(self, IMSIC_S_BASE, IMSIC_GUEST_NUM);
unsafe {
if let Some(map) = &mut MSI_PT_MAP {
if map.contains_key(&self.id()) {
panic!("MSI page table for Zone {} already exists!", self.id());
}
if map.insert(self.id(), msi_pt).is_err() {
panic!("MSI_PT_MAP full");
}
} else {
panic!("MSI_PT_MAP is not initialized!");
}
let mut map = MSI_PT_MAP.lock();
if map.contains_key(&self.id()) {
panic!("MSI page table for Zone {} already exists!", self.id());
}
if map.insert(self.id(), msi_pt).is_err() {
panic!("MSI_PT_MAP full");
}
}

pub fn get_vaplic(&self) -> &VirtualAPLIC {
unsafe {
VAPLIC_MAP
.as_ref()
.expect("VAPLIC_MAP is not initialized!")
.get(&self.id())
.expect("VirtualAPLIC for this Zone does not exist!")
}
pub fn get_vaplic(&self) -> Arc<VirtualAPLIC> {
VAPLIC_MAP
.lock()
.get(&self.id())
.cloned()
.expect("VirtualAPLIC for this Zone does not exist!")
}

pub fn arch_irqchip_reset(&self) {
pub fn arch_irqchip_reset(&self, _irq_lifecycle: &IrqLifecycleGuard) {
// We should make sure only one cpu to do this.
// This func will only be called by one root zone's cpu.
let host_aplic = host_aplic();
Expand Down Expand Up @@ -226,18 +218,8 @@ impl Zone {
crate::event::clear_events(cpuid);
});

unsafe {
if let Some(map) = &mut VAPLIC_MAP {
map.remove(&self.id());
} else {
panic!("VAPLIC_MAP is not initialized!");
}
if let Some(map) = &mut MSI_PT_MAP {
let _ = map.remove(&self.id());
} else {
panic!("MSI_PT_MAP is not initialized!");
}
}
VAPLIC_MAP.lock().remove(&self.id());
MSI_PT_MAP.lock().remove(&self.id());
print_keys();
}

Expand Down
7 changes: 1 addition & 6 deletions src/device/irqchip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use core::sync::atomic::{fence, Ordering};
#[cfg(target_arch = "aarch64")]
pub(crate) const SGIS_NUM: usize = SGI_RANGE.end;

#[cfg(any(target_arch = "riscv64", target_arch = "x86_64"))]
pub fn handle_virtio_irq_event() {
crate::device::virtio_trampoline::handle_virtio_irq();
}

/// What a `post_guest_irq()` call does to the guest IRQ.
//
// Each IRQ number has a fixed trigger type: numbers in `GUEST_LINES` are
Expand Down Expand Up @@ -328,4 +323,4 @@ pub mod pic;
pub use ls7a2000::{percpu_init, primary_init_early, primary_init_late};

#[cfg(target_arch = "x86_64")]
pub use pic::{inject_irq, inject_vector, percpu_init, primary_init_early, primary_init_late};
pub use pic::{inject_irq, percpu_init, post_guest_vector, primary_init_early, primary_init_late};
Loading
Loading