diff --git a/Cargo.lock b/Cargo.lock index 4b904038..3f218724 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,7 @@ dependencies = [ "log", "loongArch64", "numeric-enum-macro", + "percpu", "psci", "qemu-exit", "raw-cpuid", @@ -251,6 +252,29 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "percpu" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01e56c0c558952222967b592899f98765b48590e7bd7403bfd7075f73afc6ed6" +dependencies = [ + "cfg-if", + "percpu_macros", + "spin 0.9.8", + "x86", +] + +[[package]] +name = "percpu_macros" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9f4cc54a2e471ff72f1499461ba381ad4eae9cbd60d29c258545b995e406e0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.94" @@ -462,6 +486,9 @@ name = "spin" version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] [[package]] name = "spin" diff --git a/Cargo.toml b/Cargo.toml index 20fc9ef3..dc84847b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ cortex-a = "8.1.1" cfg-if = "1.0" bitvec = { version="1.0.1", default-features = false, features = ["atomic", "alloc"] } heapless = { version = "0.8.0 "} +percpu = { package = "percpu", version="0.2", features=["arm-el2"]} [target.'cfg(target_arch = "aarch64")'.dependencies] aarch64-cpu = "9.4.0" diff --git a/platform/aarch64/imx8mp/linker.ld b/platform/aarch64/imx8mp/linker.ld index 0f5e41f3..f8c4a9eb 100644 --- a/platform/aarch64/imx8mp/linker.ld +++ b/platform/aarch64/imx8mp/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x40400000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/ok6254-c/linker.ld b/platform/aarch64/ok6254-c/linker.ld index c3c0b34a..4da975bb 100644 --- a/platform/aarch64/ok6254-c/linker.ld +++ b/platform/aarch64/ok6254-c/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80400000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/phytium-pi/linker.ld b/platform/aarch64/phytium-pi/linker.ld index bc718226..1438cfec 100644 --- a/platform/aarch64/phytium-pi/linker.ld +++ b/platform/aarch64/phytium-pi/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x90100000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/qemu-gicv2/linker.ld b/platform/aarch64/qemu-gicv2/linker.ld index 0f5e41f3..f8c4a9eb 100644 --- a/platform/aarch64/qemu-gicv2/linker.ld +++ b/platform/aarch64/qemu-gicv2/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x40400000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/qemu-gicv3/board.rs b/platform/aarch64/qemu-gicv3/board.rs index d583330b..119c357c 100644 --- a/platform/aarch64/qemu-gicv3/board.rs +++ b/platform/aarch64/qemu-gicv3/board.rs @@ -24,6 +24,7 @@ use crate::{ use crate::pci_dev; +#[allow(unused)] pub const BOARD_NAME: &str = "qemu-gicv3"; pub const BOARD_NCPUS: usize = 4; diff --git a/platform/aarch64/qemu-gicv3/linker.ld b/platform/aarch64/qemu-gicv3/linker.ld index 0f5e41f3..f8c4a9eb 100644 --- a/platform/aarch64/qemu-gicv3/linker.ld +++ b/platform/aarch64/qemu-gicv3/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x40400000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/rk3568/linker.ld b/platform/aarch64/rk3568/linker.ld index 26e135cd..0fe74fbe 100644 --- a/platform/aarch64/rk3568/linker.ld +++ b/platform/aarch64/rk3568/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x60080000; +CPU_NUM = 4; SECTIONS { @@ -42,6 +43,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/aarch64/rk3588/linker.ld b/platform/aarch64/rk3588/linker.ld index 40a2ee2a..b287362b 100644 --- a/platform/aarch64/rk3588/linker.ld +++ b/platform/aarch64/rk3588/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x500000; +CPU_NUM = 8; SECTIONS { @@ -41,6 +42,17 @@ SECTIONS *(.bss .bss.*) *(.sbss .sbss.*) } + + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; . = ALIGN(4K); ebss = .; diff --git a/platform/aarch64/zcu102/linker.ld b/platform/aarch64/zcu102/linker.ld index 0f5e41f3..f8c4a9eb 100644 --- a/platform/aarch64/zcu102/linker.ld +++ b/platform/aarch64/zcu102/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x40400000; +CPU_NUM = 4; SECTIONS { @@ -37,6 +38,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/loongarch64/ls3a5000/linker.ld b/platform/loongarch64/ls3a5000/linker.ld index 6ca259e4..a226494f 100644 --- a/platform/loongarch64/ls3a5000/linker.ld +++ b/platform/loongarch64/ls3a5000/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x90000001f0000000; +CPU_NUM = 4; SECTIONS @@ -46,6 +47,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; diff --git a/platform/loongarch64/ls3a6000/linker.ld b/platform/loongarch64/ls3a6000/linker.ld index 6ca259e4..a226494f 100644 --- a/platform/loongarch64/ls3a6000/linker.ld +++ b/platform/loongarch64/ls3a6000/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x90000001f0000000; +CPU_NUM = 4; SECTIONS @@ -46,6 +47,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; diff --git a/platform/riscv64/hifive-premier-p550/linker.ld b/platform/riscv64/hifive-premier-p550/linker.ld index b1ec2354..6f2d2083 100644 --- a/platform/riscv64/hifive-premier-p550/linker.ld +++ b/platform/riscv64/hifive-premier-p550/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80200000; +CPU_NUM = 4; SECTIONS @@ -38,6 +39,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/riscv64/megrez/linker.ld b/platform/riscv64/megrez/linker.ld index b1ec2354..6f2d2083 100644 --- a/platform/riscv64/megrez/linker.ld +++ b/platform/riscv64/megrez/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80200000; +CPU_NUM = 4; SECTIONS @@ -38,6 +39,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/riscv64/qemu-aia/linker.ld b/platform/riscv64/qemu-aia/linker.ld index b1ec2354..6f2d2083 100644 --- a/platform/riscv64/qemu-aia/linker.ld +++ b/platform/riscv64/qemu-aia/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80200000; +CPU_NUM = 4; SECTIONS @@ -38,6 +39,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/riscv64/qemu-plic/linker.ld b/platform/riscv64/qemu-plic/linker.ld index b1ec2354..6f2d2083 100644 --- a/platform/riscv64/qemu-plic/linker.ld +++ b/platform/riscv64/qemu-plic/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80200000; +CPU_NUM = 4; SECTIONS @@ -38,6 +39,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/riscv64/ur-dp1000/linker.ld b/platform/riscv64/ur-dp1000/linker.ld index eb62b0fe..cb2da6df 100644 --- a/platform/riscv64/ur-dp1000/linker.ld +++ b/platform/riscv64/ur-dp1000/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0x80200000; +CPU_NUM = 8; SECTIONS @@ -38,6 +39,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/x86_64/nuc14mnk/linker.ld b/platform/x86_64/nuc14mnk/linker.ld index 11ae57fd..3fc54b77 100644 --- a/platform/x86_64/nuc14mnk/linker.ld +++ b/platform/x86_64/nuc14mnk/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0xffffff8000200000; +CPU_NUM = 4; SECTIONS { @@ -41,6 +42,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/platform/x86_64/qemu/linker.ld b/platform/x86_64/qemu/linker.ld index 11ae57fd..3fc54b77 100644 --- a/platform/x86_64/qemu/linker.ld +++ b/platform/x86_64/qemu/linker.ld @@ -1,5 +1,6 @@ ENTRY(arch_entry) BASE_ADDRESS = 0xffffff8000200000; +CPU_NUM = 4; SECTIONS { @@ -41,6 +42,17 @@ SECTIONS *(.sbss .sbss.*) } + . = ALIGN(4K); + _percpu_start = .; + _percpu_end = _percpu_start + SIZEOF(.percpu); + .percpu 0x0 (NOLOAD) : AT(_percpu_start) { + _percpu_load_start = .; + *(.percpu .percpu.*) + _percpu_load_end = .; + . = _percpu_load_start + ALIGN(64) * CPU_NUM; + } + . = _percpu_end; + . = ALIGN(4K); ebss = .; ekernel = .; diff --git a/src/arch/aarch64/cpu.rs b/src/arch/aarch64/cpu.rs index 3f7e3676..442a2a29 100644 --- a/src/arch/aarch64/cpu.rs +++ b/src/arch/aarch64/cpu.rs @@ -16,11 +16,11 @@ use crate::{ arch::{mm::new_s2_memory_set, sysreg::write_sysreg}, consts::{MAX_CPU_NUM, PAGE_SIZE, PER_CPU_ARRAY_PTR, PER_CPU_SIZE}, + cpu_data::this_cpu_data, memory::{ addr::PHYS_VIRT_OFFSET, mm::PARKING_MEMORY_SET, GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion, VirtAddr, PARKING_INST_PAGE, }, - percpu::this_cpu_data, platform::BOARD_MPIDR_MAPPINGS, zone::find_zone, }; @@ -265,12 +265,12 @@ pub fn this_cpu_id() -> usize { mpidr_to_cpuid(MPIDR_EL1.get()) as _ } -pub fn store_cpu_pointer_to_reg(pointer: usize) { +pub fn store_cpu_pointer_to_reg(_pointer: usize) { // println!("aarch64 doesn't support store cpu pointer to reg, pointer: {:#x}", pointer); return; } -pub fn get_target_cpu(irq: usize, zone_id: usize) -> usize { +pub fn get_target_cpu(_irq: usize, zone_id: usize) -> usize { find_zone(zone_id) .unwrap() .read() diff --git a/src/arch/aarch64/hypercall.rs b/src/arch/aarch64/hypercall.rs index 91e59485..5a1ec0da 100644 --- a/src/arch/aarch64/hypercall.rs +++ b/src/arch/aarch64/hypercall.rs @@ -16,11 +16,11 @@ use crate::arch::cpu::this_cpu_id; use crate::arch::ivc::{IvcInfo, IVC_INFOS}; -use crate::config::{HvZoneConfig, CONFIG_MAGIC_VERSION}; +use crate::config::CONFIG_MAGIC_VERSION; +use crate::cpu_data::this_zone; use crate::device::virtio_trampoline::MAX_DEVS; use crate::hypercall::HyperCall; use crate::hypercall::HyperCallResult; -use crate::percpu::this_zone; use crate::zone::this_zone_id; impl<'a> HyperCall<'a> { @@ -46,7 +46,7 @@ impl<'a> HyperCall<'a> { HyperCallResult::Ok(0) } - pub fn wait_for_interrupt(&mut self, irq_list: &mut [u64; MAX_DEVS + 1]) { + pub fn wait_for_interrupt(&mut self, _irq_list: &mut [u64; MAX_DEVS + 1]) { trace!("wait_for_interrupt is not need for AArch64"); } @@ -66,6 +66,7 @@ impl<'a> HyperCall<'a> { return config_addr; } + #[allow(unused)] pub fn hv_get_real_list_pa(&mut self, list_addr: u64) -> u64 { // RISC-V does not have a specific prefix for cached memory, so we return the address as is. return list_addr; @@ -76,7 +77,7 @@ impl<'a> HyperCall<'a> { trace!("CPU ID: {} Start Zone", cpuid); } - pub fn hv_virtio_get_irq(&self, virtio_irq: *mut u32) -> HyperCallResult { + pub fn hv_virtio_get_irq(&self, _virtio_irq: *mut u32) -> HyperCallResult { trace!("hv_virtio_get_irq is not need for AArch64"); HyperCallResult::Ok(0) } diff --git a/src/arch/aarch64/iommu.rs b/src/arch/aarch64/iommu.rs index 7e6336d4..58aa75eb 100644 --- a/src/arch/aarch64/iommu.rs +++ b/src/arch/aarch64/iommu.rs @@ -15,9 +15,8 @@ // #![allow(dead_code)] use crate::{ - arch::mm::new_s2_memory_set, consts::{MAX_ZONE_NUM, PAGE_SIZE}, - memory::{Frame, GuestPhysAddr, MemFlags, MemoryRegion, MemorySet, PhysAddr}, + memory::{Frame, PhysAddr}, }; use aarch64_cpu::registers::{Readable, Writeable}; use alloc::vec::Vec; @@ -27,8 +26,6 @@ use tock_registers::{ registers::{ReadOnly, ReadWrite}, }; -use super::Stage2PageTable; - #[allow(dead_code)] const SMMU_BASE_ADDR: PhysAddr = 0x09050000; #[allow(dead_code)] diff --git a/src/arch/aarch64/ipi.rs b/src/arch/aarch64/ipi.rs index a0aa0ab8..974a3013 100644 --- a/src/arch/aarch64/ipi.rs +++ b/src/arch/aarch64/ipi.rs @@ -30,6 +30,6 @@ pub fn arch_check_events(event: Option) { } } -pub fn arch_prepare_send_event(cpu_id: usize, ipi_int_id: usize, event_id: usize) { +pub fn arch_prepare_send_event(_cpu_id: usize, _ipi_int_id: usize, _event_id: usize) { debug!("aarch64 arch_prepare_send_event: do nothing now.") } diff --git a/src/arch/aarch64/mm.rs b/src/arch/aarch64/mm.rs index 36e8e687..23b0b9dd 100644 --- a/src/arch/aarch64/mm.rs +++ b/src/arch/aarch64/mm.rs @@ -53,7 +53,7 @@ pub fn new_s2_memory_set() -> MemorySet { MemorySet::new(if is_s2_pt_level3() { 3 } else { 4 }) } -pub fn arch_post_heap_init(host_dtb: usize) { +pub fn arch_post_heap_init(_host_dtb: usize) { // AArch64 does not need to do some setup work after heap init like x86_64. // This function can be used to set up any architecture-specific parameters if needed. // Currently, it does nothing. diff --git a/src/arch/aarch64/trap.rs b/src/arch/aarch64/trap.rs index e49146e0..acd09f06 100644 --- a/src/arch/aarch64/trap.rs +++ b/src/arch/aarch64/trap.rs @@ -24,11 +24,11 @@ use crate::{ cpu::mpidr_to_cpuid, sysreg::{read_sysreg, write_sysreg}, }, + cpu_data::{get_cpu_data, this_cpu_data, this_zone}, device::irqchip::gic_handle_irq, event::{send_event, IPI_EVENT_SHUTDOWN, IPI_EVENT_WAKEUP}, hypercall::{HyperCall, SGI_IPI_ID}, memory::{mmio_handle_access, MMIOAccess}, - percpu::{get_cpu_data, this_cpu_data, this_zone}, zone::{is_this_root_zone, remove_zone}, }; diff --git a/src/arch/aarch64/zone.rs b/src/arch/aarch64/zone.rs index 207710fc..c4691135 100644 --- a/src/arch/aarch64/zone.rs +++ b/src/arch/aarch64/zone.rs @@ -16,11 +16,10 @@ use core::panic; use crate::{ - arch::Stage2PageTable, config::*, device::virtio_trampoline::mmio_virtio_handler, error::HvResult, - memory::{GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion, MemorySet}, + memory::{GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion}, zone::Zone, }; @@ -118,7 +117,7 @@ impl Zone { Ok(()) } - pub fn arch_zone_post_configuration(&mut self, config: &HvZoneConfig) -> HvResult { + pub fn arch_zone_post_configuration(&mut self, _config: &HvZoneConfig) -> HvResult { Ok(()) } } @@ -132,6 +131,7 @@ pub struct HvArchZoneConfig { #[repr(C, usize)] #[derive(Debug, Clone)] +#[allow(unused)] pub enum GicConfig { Gicv2(Gicv2Config), Gicv3(Gicv3Config), diff --git a/src/arch/loongarch64/cpu.rs b/src/arch/loongarch64/cpu.rs index 625e43c4..5b524dd4 100644 --- a/src/arch/loongarch64/cpu.rs +++ b/src/arch/loongarch64/cpu.rs @@ -17,8 +17,8 @@ use super::ipi::*; use super::zone::ZoneContext; use crate::arch::zone::disable_hwi_through; +use crate::cpu_data::this_cpu_data; use crate::device::common::MMIODerefWrapper; -use crate::percpu::this_cpu_data; use crate::zone::find_zone; use core::arch::asm; use core::fmt::{self, Debug, Formatter}; diff --git a/src/arch/loongarch64/trap.rs b/src/arch/loongarch64/trap.rs index d16e1c81..29397185 100644 --- a/src/arch/loongarch64/trap.rs +++ b/src/arch/loongarch64/trap.rs @@ -20,12 +20,12 @@ use super::zone::ZoneContext; use crate::arch::cpu::this_cpu_id; use crate::arch::ipi::*; use crate::consts::{IPI_EVENT_CLEAR_INJECT_IRQ, MAX_CPU_NUM}; +use crate::cpu_data::this_cpu_data; use crate::device::irqchip::inject_irq; use crate::device::irqchip::ls7a2000::chip::*; use crate::event::{check_events, dump_cpu_events, dump_events}; use crate::hypercall::{SGI_IPI_ID, *}; use crate::memory::{addr, mmio_handle_access, MMIOAccess}; -use crate::percpu::this_cpu_data; use crate::zone::Zone; use crate::PHY_TO_DMW_UNCACHED; use core::arch; diff --git a/src/arch/riscv64/cpu.rs b/src/arch/riscv64/cpu.rs index 35f9a3a4..966ab9b6 100644 --- a/src/arch/riscv64/cpu.rs +++ b/src/arch/riscv64/cpu.rs @@ -14,7 +14,7 @@ // Authors: // use super::csr::*; -use crate::percpu::this_cpu_data; +use crate::cpu_data::this_cpu_data; use crate::platform::{BOARD_HARTID_MAP, BOARD_NCPUS}; use crate::{ arch::mm::new_s2_memory_set, diff --git a/src/arch/riscv64/sbi.rs b/src/arch/riscv64/sbi.rs index 04492318..d922f103 100644 --- a/src/arch/riscv64/sbi.rs +++ b/src/arch/riscv64/sbi.rs @@ -19,9 +19,9 @@ use super::cpu::ArchCpu; use crate::arch::cpu::hartid_to_cpuid; use crate::arch::csr::*; use crate::consts::IPI_EVENT_SEND_IPI; +use crate::cpu_data::{get_cpu_data, this_cpu_data}; use crate::event::{send_event, IPI_EVENT_WAKEUP}; use crate::hypercall::HyperCall; -use crate::percpu::{get_cpu_data, this_cpu_data}; use core::sync::atomic; use riscv::register::sie; use riscv_h::register::hvip; diff --git a/src/arch/x86_64/acpi.rs b/src/arch/x86_64/acpi.rs index 4bfdc530..701b5dff 100644 --- a/src/arch/x86_64/acpi.rs +++ b/src/arch/x86_64/acpi.rs @@ -17,8 +17,8 @@ use crate::{ arch::boot, config::{HvConfigMemoryRegion, HvZoneConfig}, + cpu_data::{this_zone, CpuSet}, error::HvResult, - percpu::{this_zone, CpuSet}, platform::ROOT_PCI_MAX_BUS, }; use acpi::{ diff --git a/src/arch/x86_64/boot.rs b/src/arch/x86_64/boot.rs index 14d92a08..319e62b7 100644 --- a/src/arch/x86_64/boot.rs +++ b/src/arch/x86_64/boot.rs @@ -17,9 +17,9 @@ use crate::{ arch::{zone::HvArchZoneConfig, Stage2PageTable}, config::{root_zone_config, HvPciConfig, HvZoneConfig, MEM_TYPE_RAM}, + cpu_data::this_zone, error::HvResult, memory::{GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion, MemorySet}, - percpu::this_zone, platform::MEM_TYPE_RESERVED, }; use alloc::string::{String, ToString}; diff --git a/src/arch/x86_64/cpu.rs b/src/arch/x86_64/cpu.rs index 25f3653d..5eeaee4f 100644 --- a/src/arch/x86_64/cpu.rs +++ b/src/arch/x86_64/cpu.rs @@ -30,6 +30,7 @@ use crate::{ vmx::*, }, consts::{self, core_end, PER_CPU_SIZE}, + cpu_data::{this_cpu_data, this_zone}, device::irqchip::pic::{check_pending_vectors, clear_vectors, ioapic, lapic::VirtLocalApic}, error::{HvError, HvResult}, memory::{ @@ -38,7 +39,6 @@ use crate::{ Frame, GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion, PhysAddr, PAGE_SIZE, PARKING_INST_PAGE, }, - percpu::{this_cpu_data, this_zone}, platform::ROOT_ZONE_BOOT_STACK, zone::{find_zone, this_zone_id}, }; diff --git a/src/arch/x86_64/hypercall.rs b/src/arch/x86_64/hypercall.rs index cb5517f1..4a228af7 100644 --- a/src/arch/x86_64/hypercall.rs +++ b/src/arch/x86_64/hypercall.rs @@ -17,9 +17,9 @@ use crate::{ arch::cpu::this_cpu_id, config::CONFIG_MAGIC_VERSION, + cpu_data::this_zone, device::virtio_trampoline::MAX_DEVS, hypercall::{HyperCall, HyperCallResult}, - percpu::this_zone, zone::{Zone, ZoneInfo}, }; use spin::RwLock; diff --git a/src/arch/x86_64/ipi.rs b/src/arch/x86_64/ipi.rs index b8c20de5..4c3b6d46 100644 --- a/src/arch/x86_64/ipi.rs +++ b/src/arch/x86_64/ipi.rs @@ -20,11 +20,11 @@ use crate::{ cpu::this_cpu_id, idt::IdtVector, }, + cpu_data::{this_cpu_data, this_zone, CpuSet}, device::irqchip::inject_vector, error::HvResult, event, hypercall::SGI_IPI_ID, - percpu::{this_cpu_data, this_zone, CpuSet}, }; use alloc::{collections::vec_deque::VecDeque, vec::Vec}; use bit_field::BitField; diff --git a/src/arch/x86_64/mmio.rs b/src/arch/x86_64/mmio.rs index 2b7681c4..7e7443c5 100644 --- a/src/arch/x86_64/mmio.rs +++ b/src/arch/x86_64/mmio.rs @@ -19,12 +19,12 @@ use crate::{ s2pt::DescriptorAttr, vmcs::{VmcsGuest16, VmcsGuestNW}, }, + cpu_data::{this_cpu_data, this_zone}, error::HvResult, memory::{ addr::{GuestPhysAddr, GuestVirtAddr, HostPhysAddr}, MMIOAccess, MMIOHandler, }, - percpu::{this_cpu_data, this_zone}, }; use alloc::{sync::Arc, vec::Vec}; use bit_field::BitField; diff --git a/src/arch/x86_64/pci.rs b/src/arch/x86_64/pci.rs index cc05d533..ef173ce8 100644 --- a/src/arch/x86_64/pci.rs +++ b/src/arch/x86_64/pci.rs @@ -16,11 +16,11 @@ use crate::{ arch::{acpi, idt, mmio::MMIoDevice, pio::get_pio_bitmap, zone::HvArchZoneConfig}, + cpu_data::this_zone, error::HvResult, memory::{ mmio_generic_handler, mmio_handle_access, mmio_perform_access, GuestPhysAddr, MMIOAccess, }, - percpu::this_zone, zone::{this_zone_id, Zone}, }; use ::acpi::{mcfg::Mcfg, sdt::Signature}; diff --git a/src/arch/x86_64/s2pt.rs b/src/arch/x86_64/s2pt.rs index 9a4e8e49..43680f66 100644 --- a/src/arch/x86_64/s2pt.rs +++ b/src/arch/x86_64/s2pt.rs @@ -21,12 +21,12 @@ use crate::{ vmcs::*, }, consts::PAGE_SIZE, + cpu_data::this_cpu_data, error::HvResult, memory::{ addr::{GuestPhysAddr, HostPhysAddr, PhysAddr}, MemFlags, }, - percpu::this_cpu_data, zone::this_zone_id, }; use bit_field::BitField; diff --git a/src/arch/x86_64/trap.rs b/src/arch/x86_64/trap.rs index edfbeee9..9621852d 100644 --- a/src/arch/x86_64/trap.rs +++ b/src/arch/x86_64/trap.rs @@ -26,6 +26,7 @@ use crate::{ vmcs::*, vmx::{VmxCrAccessInfo, VmxExitInfo, VmxExitReason, VmxInterruptInfo, VmxIoExitInfo}, }, + cpu_data::{this_cpu_data, this_zone}, device::{ irqchip::{ inject_vector, @@ -36,7 +37,6 @@ use crate::{ error::HvResult, hypercall::HyperCall, memory::{mmio_handle_access, MMIOAccess, MemFlags}, - percpu::{this_cpu_data, this_zone}, zone::this_zone_id, }; use bit_field::BitField; diff --git a/src/arch/x86_64/zone.rs b/src/arch/x86_64/zone.rs index f69f4eac..e0c3a67d 100644 --- a/src/arch/x86_64/zone.rs +++ b/src/arch/x86_64/zone.rs @@ -17,10 +17,10 @@ use crate::{ arch::{acpi, boot, msr::set_msr_bitmap, pio, pio::set_pio_bitmap, Stage2PageTable}, config::*, + cpu_data::get_cpu_data, device::virtio_trampoline::mmio_virtio_handler, error::HvResult, memory::{GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion, MemorySet}, - percpu::get_cpu_data, platform::MEM_TYPE_RESERVED, zone::Zone, }; diff --git a/src/percpu.rs b/src/cpu_data.rs similarity index 100% rename from src/percpu.rs rename to src/cpu_data.rs diff --git a/src/device/irqchip/aia/aplic.rs b/src/device/irqchip/aia/aplic.rs index b92556a3..a42fdb80 100644 --- a/src/device/irqchip/aia/aplic.rs +++ b/src/device/irqchip/aia/aplic.rs @@ -17,7 +17,7 @@ use crate::config::root_zone_config; use crate::device::irqchip::aia::imsic::imsic_trigger; use crate::zone::Zone; -use crate::{arch::cpu::ArchCpu, memory::GuestPhysAddr, percpu::this_cpu_data}; +use crate::{arch::cpu::ArchCpu, cpu_data::this_cpu_data, memory::GuestPhysAddr}; use riscv_decode::Instruction; use spin::Once; use spin::RwLock; diff --git a/src/device/irqchip/aia/mod.rs b/src/device/irqchip/aia/mod.rs index 93b4f5f5..1579ee80 100644 --- a/src/device/irqchip/aia/mod.rs +++ b/src/device/irqchip/aia/mod.rs @@ -24,12 +24,12 @@ use crate::arch::cpu::this_cpu_id; use crate::arch::zone::HvArchZoneConfig; use crate::config::{BitmapWord, HvZoneConfig, CONFIG_INTERRUPTS_BITMAP_BITS_PER_WORD}; use crate::consts::MAX_ZONE_NUM; +use crate::cpu_data::this_cpu_data; use crate::error::HvResult; use crate::memory::HostPhysAddr; use crate::memory::MMIOAccess; use crate::memory::MemFlags; use crate::memory::MemoryRegion; -use crate::percpu::this_cpu_data; use crate::platform::HW_IRQS; use crate::platform::{ BOARD_APLIC_INTERRUPTS_NUM, IMSIC_GUEST_INDEX, IMSIC_GUEST_NUM, IMSIC_S_BASE, diff --git a/src/device/irqchip/aia/vaplic.rs b/src/device/irqchip/aia/vaplic.rs index fdd50405..795b72f1 100644 --- a/src/device/irqchip/aia/vaplic.rs +++ b/src/device/irqchip/aia/vaplic.rs @@ -15,7 +15,7 @@ // use super::*; -use crate::percpu::this_cpu_data; +use crate::cpu_data::this_cpu_data; use alloc::sync::Arc; use alloc::vec::Vec; use bitvec::prelude::*; diff --git a/src/device/irqchip/gicv2/vgic.rs b/src/device/irqchip/gicv2/vgic.rs index 4490ef10..19b18268 100644 --- a/src/device/irqchip/gicv2/vgic.rs +++ b/src/device/irqchip/gicv2/vgic.rs @@ -15,6 +15,7 @@ // Hangqi Ren <2572131118@qq.com> use crate::arch::zone::{GicConfig, Gicv2Config, HvArchZoneConfig}; use crate::config::{BitmapWord, CONFIG_INTERRUPTS_BITMAP_BITS_PER_WORD}; +use crate::cpu_data::this_zone; use crate::device::irqchip::gicv2::gicd::{ get_max_int_num, GICD, GICD_CTRL_REG_OFFSET, GICD_ICACTIVER_REG_OFFSET, GICD_ICENABLER_REG_OFFSET, GICD_ICFGR_REG_OFFSET, GICD_ICPENDR_REG_OFFSET, @@ -28,7 +29,6 @@ use crate::device::irqchip::gicv2::gicd::{ use crate::device::irqchip::gicv2::GICV2; use crate::error::HvResult; use crate::memory::{mmio_perform_access, MMIOAccess, MemFlags, MemoryRegion}; -use crate::percpu::this_zone; /// This file defines and implements the functional functions of virtual gicv2. /// author: ForeverYolo /// reference: diff --git a/src/device/irqchip/gicv3/gits.rs b/src/device/irqchip/gicv3/gits.rs index eb8719f6..65ddc30c 100644 --- a/src/device/irqchip/gicv3/gits.rs +++ b/src/device/irqchip/gicv3/gits.rs @@ -15,13 +15,12 @@ // use core::ptr; -use aarch64_cpu::registers::DAIF::A; use alloc::{sync::Arc, vec::Vec}; use spin::{mutex::Mutex, Once, RwLock}; use crate::{ - consts::MAX_ZONE_NUM, device::irqchip::gicv3::gicr::enable_one_lpi, memory::Frame, - percpu::this_zone, zone::this_zone_id, + consts::MAX_ZONE_NUM, cpu_data::this_zone, device::irqchip::gicv3::gicr::enable_one_lpi, + memory::Frame, }; use super::host_gits_base; @@ -177,7 +176,7 @@ impl Cmdq { self.cbaser_list[zone_id] = value; let gpa_base = value & 0xffffffffff000; unsafe { - let phy_base = match this_zone().read().gpm.page_table_query(gpa_base) { + let _phy_base = match this_zone().read().gpm.page_table_query(gpa_base) { Ok(p) => self.phy_base_list[zone_id] = p.0, _ => {} }; diff --git a/src/device/irqchip/gicv3/vgic.rs b/src/device/irqchip/gicv3/vgic.rs index bd16002b..8ac7dd87 100644 --- a/src/device/irqchip/gicv3/vgic.rs +++ b/src/device/irqchip/gicv3/vgic.rs @@ -18,9 +18,10 @@ use alloc::sync::Arc; use super::{gicd::GICD_LOCK, is_spi}; use crate::platform::BOARD_MPIDR_MAPPINGS; use crate::{ - arch::zone::{GicConfig, Gicv2Config, Gicv3Config, HvArchZoneConfig}, + arch::zone::{GicConfig, HvArchZoneConfig}, config::{BitmapWord, CONFIG_INTERRUPTS_BITMAP_BITS_PER_WORD, CONFIG_MAX_INTERRUPTS}, consts::MAX_CPU_NUM, + cpu_data::{get_cpu_data, this_zone}, device::irqchip::gicv3::{ gicd::*, gicr::*, gits::*, host_gicd_base, host_gicr_base, host_gits_base, MAINTENACE_INTERRUPT, PER_GICR_SIZE, @@ -28,7 +29,6 @@ use crate::{ error::HvResult, hypercall::SGI_IPI_ID, memory::{mmio_perform_access, MMIOAccess}, - percpu::{get_cpu_data, this_zone}, zone::{this_zone_id, Zone}, }; pub fn reg_range(base: usize, n: usize, size: usize) -> core::ops::Range { diff --git a/src/device/irqchip/mod.rs b/src/device/irqchip/mod.rs index 0d74c011..345c4d20 100644 --- a/src/device/irqchip/mod.rs +++ b/src/device/irqchip/mod.rs @@ -72,15 +72,15 @@ pub fn gic_send_event(cpu_id: u64, sgi_num: u64) { } impl Zone { - pub fn virqc_init(&mut self, config: &HvZoneConfig) { + pub fn virqc_init(&mut self, _config: &HvZoneConfig) { #[cfg(all(feature = "plic", target_arch = "riscv64"))] { - self.vplic_init(config); + self.vplic_init(_config); } #[cfg(all(feature = "aia", target_arch = "riscv64"))] { - self.vaplic_init(config); - self.vimsic_init(config); + self.vaplic_init(_config); + self.vimsic_init(_config); } } diff --git a/src/device/irqchip/pic/lapic.rs b/src/device/irqchip/pic/lapic.rs index f5697c3b..8ba43c18 100644 --- a/src/device/irqchip/pic/lapic.rs +++ b/src/device/irqchip/pic/lapic.rs @@ -21,10 +21,10 @@ use crate::{ ipi, msr::Msr::{self, *}, }, + cpu_data::this_cpu_data, device::irqchip::pic::pop_vector, error::HvResult, memory::Frame, - percpu::this_cpu_data, }; use bit_field::BitField; use core::{ops::Range, u32}; diff --git a/src/device/irqchip/plic/mod.rs b/src/device/irqchip/plic/mod.rs index 1add8f67..f2fb304d 100644 --- a/src/device/irqchip/plic/mod.rs +++ b/src/device/irqchip/plic/mod.rs @@ -28,9 +28,9 @@ use crate::arch::zone::HvArchZoneConfig; use crate::config::HvZoneConfig; use crate::config::{BitmapWord, CONFIG_INTERRUPTS_BITMAP_BITS_PER_WORD}; use crate::consts::MAX_CPU_NUM; +use crate::cpu_data::this_cpu_data; use crate::error::HvResult; use crate::memory::mmio::MMIOAccess; -use crate::percpu::this_cpu_data; use crate::platform::*; use crate::zone::Zone; use alloc::collections::BTreeMap; diff --git a/src/device/virtio_trampoline.rs b/src/device/virtio_trampoline.rs index b7896f17..4c8e00c6 100644 --- a/src/device/virtio_trampoline.rs +++ b/src/device/virtio_trampoline.rs @@ -189,7 +189,7 @@ impl VirtioBridgeController { } /// Get req list agent. - pub fn req_agent(&self) -> ReqAgent { + fn req_agent(&self) -> ReqAgent { if !self.is_enable.load(Ordering::Acquire) { panic!("VirtioBridge not enabled"); } diff --git a/src/event.rs b/src/event.rs index 7cec4dee..5bc2e0b7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -19,97 +19,68 @@ use crate::{ consts::{ IPI_EVENT_CLEAR_INJECT_IRQ, IPI_EVENT_SEND_IPI, IPI_EVENT_UPDATE_HART_LINE, MAX_CPU_NUM, }, + cpu_data::this_cpu_data, device::{irqchip::inject_irq, virtio_trampoline::handle_virtio_irq}, - percpu::this_cpu_data, platform::IRQ_WAKEUP_VIRTIO_DEVICE, }; use alloc::{collections::VecDeque, vec::Vec}; -use spin::{Mutex, Once}; +use spin::Mutex; pub const IPI_EVENT_WAKEUP: usize = 0; pub const IPI_EVENT_SHUTDOWN: usize = 1; pub const IPI_EVENT_VIRTIO_INJECT_IRQ: usize = 2; pub const IPI_EVENT_WAKEUP_VIRTIO_DEVICE: usize = 3; -static EVENT_MANAGER: Once = Once::new(); +#[percpu::def_percpu] +static PERCPU_EVENTS: Mutex> = Mutex::new(VecDeque::new()); -struct EventManager { - pub inner: Vec>>, +// The caller ensures the cpu_id is valid +#[inline(always)] +fn get_percpu_events(cpu: usize) -> &'static Mutex> { + unsafe { PERCPU_EVENTS.remote_ref_raw(cpu) } } -impl EventManager { - fn new(max_cpus: usize) -> Self { - let mut vs = vec![]; - for _ in 0..max_cpus { - let v = Mutex::new(VecDeque::new()); - vs.push(v) - } - Self { inner: vs } - } - - fn add_event(&self, cpu: usize, event_id: usize) -> Option<()> { - match self.inner.get(cpu) { - Some(events) => { - let mut e = events.lock(); - if event_id == IPI_EVENT_SHUTDOWN { - e.clear(); - } - e.push_back(event_id); - Some(()) - } - None => None, - } - } - - fn fetch_event(&self, cpu: usize) -> Option { - match self.inner.get(cpu) { - Some(events) => { - let mut e = events.lock(); - e.pop_front() - } - None => None, - } - } - - fn dump(&self) { - for (cpu, events) in self.inner.iter().enumerate() { - let e = events.lock(); - debug!("event manager: cpu: {}, events: {:?}", cpu, e); - } +fn add_event(cpu: usize, event_id: usize) -> Option<()> { + if cpu >= MAX_CPU_NUM { + return None; } - - fn dump_cpu(&self, cpu: usize) -> Vec { - let mut res = Vec::new(); - let e = self.inner[cpu].lock(); - for i in e.iter() { - res.push(*i); - } - res + let mut e = get_percpu_events(cpu).lock(); + if event_id == IPI_EVENT_SHUTDOWN { + // If the event is shutdown, we need to clear all previous events, because shutdown will make cpu idle and won't process any events. + e.clear(); } -} - -fn add_event(cpu: usize, event_id: usize) -> Option<()> { - EVENT_MANAGER.get().unwrap().add_event(cpu, event_id) + e.push_back(event_id); + Some(()) } pub fn fetch_event(cpu: usize) -> Option { - EVENT_MANAGER.get().unwrap().fetch_event(cpu) -} - -pub fn init() { - EVENT_MANAGER.call_once(|| EventManager::new(MAX_CPU_NUM)); + if cpu >= MAX_CPU_NUM { + return None; + } + get_percpu_events(cpu).lock().pop_front() } pub fn dump_events() { - EVENT_MANAGER.get().unwrap().dump(); + for cpu in 0..MAX_CPU_NUM { + let events = get_percpu_events(cpu).lock(); + if !events.is_empty() { + debug!("cpu {} events: {:?}", cpu, *events); + } + } } pub fn dump_cpu_events(cpu: usize) -> Vec { - EVENT_MANAGER.get().unwrap().dump_cpu(cpu) + if cpu >= MAX_CPU_NUM { + return Vec::new(); + } + get_percpu_events(cpu).lock().iter().cloned().collect() } pub fn clear_events(cpu: usize) { - EVENT_MANAGER.get().unwrap().inner[cpu].lock().clear(); + if cpu >= MAX_CPU_NUM { + return; + } + get_percpu_events(cpu).lock().clear(); } pub fn check_events() -> bool { @@ -179,10 +150,3 @@ pub fn send_event(cpu_id: usize, ipi_int_id: usize, event_id: usize) { add_event(cpu_id, event_id); arch_send_event(cpu_id as _, ipi_int_id as _); } - -#[test_case] -fn test_simple_send_event() { - init(); - send_event(0, 0, IPI_EVENT_WAKEUP); - assert_eq!(fetch_event(0), Some(IPI_EVENT_WAKEUP)); -} diff --git a/src/hypercall/mod.rs b/src/hypercall/mod.rs index ec3e3810..55a3b8e1 100644 --- a/src/hypercall/mod.rs +++ b/src/hypercall/mod.rs @@ -19,9 +19,9 @@ use crate::arch::cpu::get_target_cpu; use crate::config::HvZoneConfig; use crate::consts::{INVALID_ADDRESS, MAX_CPU_NUM, MAX_WAIT_TIMES, PAGE_SIZE}; +use crate::cpu_data::{get_cpu_data, PerCpu}; use crate::device::virtio_trampoline::{MAX_DEVS, VIRTIO_BRIDGE, VIRTIO_IRQS}; use crate::error::HvResult; -use crate::percpu::{get_cpu_data, PerCpu}; use crate::zone::{ add_zone, all_zones_info, find_zone, is_this_root_zone, remove_zone, zone_create, ZoneInfo, }; diff --git a/src/logging.rs b/src/logging.rs index 128aeffc..6f8320c0 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -94,6 +94,7 @@ enum ColorCode { BrightWhite = 97, } +#[allow(unused)] fn color_code_to_bgra(code: &ColorCode) -> u32 { match code { ColorCode::Black => 0, @@ -204,7 +205,7 @@ impl Log for SimpleLogger { let level = record.level(); let line = record.line().unwrap_or(0); let target = record.target(); - let cpu_id = crate::percpu::this_cpu_data().id; + let cpu_id = crate::cpu_data::this_cpu_data().id; let level_color = match level { Level::Error => ColorCode::BrightRed, Level::Warn => ColorCode::BrightYellow, diff --git a/src/main.rs b/src/main.rs index b320606a..92e98022 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,12 +52,12 @@ mod logging; mod arch; mod config; mod consts; +mod cpu_data; mod device; mod event; mod hypercall; mod memory; mod panic; -mod percpu; mod platform; mod zone; @@ -72,10 +72,9 @@ use crate::consts::{hv_end, mem_pool_start, MAX_CPU_NUM}; use arch::{cpu::cpu_start, entry::arch_entry}; use config::root_zone_config; use core::sync::atomic::{AtomicI32, AtomicU32, Ordering}; +use cpu_data::PerCpu; #[cfg(feature = "pci")] use pci::pci_config::hvisor_pci_init; -use percpu::PerCpu; -use zone::{add_zone, zone_create}; static INITED_CPUS: AtomicU32 = AtomicU32::new(0); static ENTERED_CPUS: AtomicU32 = AtomicU32::new(0); @@ -128,7 +127,6 @@ fn primary_init_early() { ); memory::frame::init(); memory::frame::test(); - event::init(); arch::stage2_mode_detect(); @@ -147,6 +145,7 @@ fn primary_init_early() { #[cfg(not(test))] { + use zone::{add_zone, zone_create}; let zone = zone_create(root_config).unwrap(); add_zone(zone); } @@ -197,11 +196,13 @@ fn rust_main(cpuid: usize, host_dtb: usize) { if MASTER_CPU.load(Ordering::Acquire) == -1 { MASTER_CPU.store(cpuid as i32, Ordering::Release); is_primary = true; + percpu::init(); memory::heap::init(); memory::heap::test(); arch::time::init_timebase(); arch_post_heap_init(host_dtb); } + percpu::init_percpu_reg(cpuid); let cpu = PerCpu::new(cpuid); diff --git a/src/memory/mmio.rs b/src/memory/mmio.rs index 5c88ac31..e96641c1 100644 --- a/src/memory/mmio.rs +++ b/src/memory/mmio.rs @@ -15,7 +15,7 @@ // use core::{ptr, usize}; -use crate::{error::HvResult, percpu::this_zone, zone::zone_error}; +use crate::{cpu_data::this_zone, error::HvResult, zone::zone_error}; use super::GuestPhysAddr; diff --git a/src/pci/pci_handler.rs b/src/pci/pci_handler.rs index 0d310b8e..bc65d51d 100644 --- a/src/pci/pci_handler.rs +++ b/src/pci/pci_handler.rs @@ -16,10 +16,10 @@ use alloc::string::String; +use crate::cpu_data::this_zone; use crate::error::HvResult; use crate::memory::MMIOAccess; use crate::memory::{GuestPhysAddr, HostPhysAddr, MemFlags, MemoryRegion}; -use crate::percpu::this_zone; use crate::zone::is_this_root_zone; use super::pci_access::{BridgeField, EndpointField, HeaderType, PciField, PciMemType}; diff --git a/src/pci/pci_test.rs b/src/pci/pci_test.rs index 67278065..021ccf3b 100644 --- a/src/pci/pci_test.rs +++ b/src/pci/pci_test.rs @@ -29,14 +29,11 @@ use super::{ use super::{mem_alloc::BaseAllocator, pci_struct::RootComplex}; use crate::{ + cpu_data::this_zone, memory::{mmio_perform_access, MMIOAccess}, pci::{config_accessors::PciConfigMmio, pci_access::EndpointHeader}, - percpu::this_zone, }; -#[cfg(feature = "ecam_pcie")] -use crate::pci::config_accessors::ecam::EcamConfigAccessor; - pub static GLOBAL_PCIE_LIST_TEST: Lazy>> = Lazy::new(|| { let m = BTreeMap::new(); diff --git a/src/pci/vpci_dev/standard.rs b/src/pci/vpci_dev/standard.rs index 7c3f396e..6a7e3bc8 100644 --- a/src/pci/vpci_dev/standard.rs +++ b/src/pci/vpci_dev/standard.rs @@ -22,12 +22,12 @@ use crate::pci::pci_access::{ use crate::pci::pci_struct::{CapabilityType, PciCapability, VirtualPciConfigSpace}; use crate::pci::PciConfigAddress; // use crate::memory::frame::Frame; +use crate::cpu_data::this_zone; use crate::memory::MMIOAccess; use crate::pci::pci_access::PciMemType; use crate::pci::pci_struct::ArcRwLockVirtualPciConfigSpace; use crate::pci::pci_struct::PciCapabilityRegion; use crate::pci::vpci_dev::VirtMsiXCap; -use crate::percpu::this_zone; use alloc::sync::Arc; use spin::RwLock; diff --git a/src/tests.rs b/src/tests.rs index 5d740709..98db70af 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -17,7 +17,6 @@ /// since this is a baremetal program /// all unittests are performed when running hvisor on qemu /// you will need to use `make test` to run the unittests -use core::ptr::write_volatile; use qemu_exit::QEMUExit; #[cfg(target_arch = "riscv64")] diff --git a/src/zone.rs b/src/zone.rs index 9efb5232..6297d1f6 100644 --- a/src/zone.rs +++ b/src/zone.rs @@ -29,10 +29,10 @@ use crate::arch::mm::new_s2_memory_set; use crate::arch::s2pt::Stage2PageTable; use crate::config::{HvZoneConfig, CONFIG_NAME_MAXLEN}; +use crate::cpu_data::{get_cpu_data, this_zone, CpuSet}; use crate::error::HvResult; use crate::memory::addr::GuestPhysAddr; use crate::memory::{MMIOConfig, MMIOHandler, MMIORegion, MemorySet}; -use crate::percpu::{get_cpu_data, this_zone, CpuSet}; use core::panic; #[cfg(feature = "dwc_pcie")]