diff --git a/Makefile b/Makefile index ee1b5095e0f71637fa68cd48063a4ec094889cb4..46f8ab23c0bb9417e5372982524c6a80701a0b08 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ pi4_release: QEMU_COMMON_OPTIONS = -machine virt,virtualization=on,gic-version=2\ -m 8g -cpu cortex-a57 -smp 4 -display none -global virtio-mmio.force-legacy=false -QEMU_SERIAL_OPTIONS = -serial stdio #\ +QEMU_SERIAL_OPTIONS = -serial mon:stdio #\ -serial telnet:localhost:12345,server QEMU_NETWORK_OPTIONS = -netdev user,id=n0,hostfwd=tcp::5555-:22 -device virtio-net-device,bus=virtio-mmio-bus.24,netdev=n0 diff --git a/src/board/pi4/platform.rs b/src/board/pi4/platform.rs index 731f147635cd6164508ea80645d5f72289c97a5e..4bf18d3de631b3cccd0dbd0f20ae096a46eb4442 100644 --- a/src/board/pi4/platform.rs +++ b/src/board/pi4/platform.rs @@ -10,7 +10,7 @@ use crate::arch::GicDesc; use crate::arch::SmmuDesc; -use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion, SchedRule}; +use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion, PlatCpuCoreConfig}; use crate::board::SchedRule::RoundRobin; use crate::device::ARM_CORTEX_A57; #[allow(unused_imports)] @@ -68,22 +68,31 @@ pub const DISK_PARTITION_4_SIZE: usize = 11471872; pub static PLAT_DESC: PlatformConfig = PlatformConfig { cpu_desc: PlatCpuConfig { num: 4, - mpidr_list: [0x80000000, 0x80000001, 0x80000002, 0x80000003, 0, 0, 0, 0], - name: [ARM_CORTEX_A57; 8], - sched_list: [ - RoundRobin, - RoundRobin, - RoundRobin, - RoundRobin, - SchedRule::None, - SchedRule::None, - SchedRule::None, - SchedRule::None, + core_list: &[ + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000000, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000001, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000002, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000003, + sched: RoundRobin, + }, ], }, mem_desc: PlatMemoryConfig { - region_num: 4, - regions: [ + regions: &[ PlatMemRegion { base: 0xf0000000, size: 0xc000000, @@ -100,18 +109,6 @@ pub static PLAT_DESC: PlatformConfig = PlatformConfig { base: 0x100000000, size: 0x100000000, }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, ], base: 0xf0000000, }, @@ -146,7 +143,7 @@ pub fn platform_power_on_secondary_cores() { fn _image_start(); } for i in 1..PLAT_DESC.cpu_desc.num { - platform_cpu_on(PLAT_DESC.cpu_desc.mpidr_list[i], _image_start as usize, 0); + platform_cpu_on(PLAT_DESC.cpu_desc.core_list[i].mpidr, _image_start as usize, 0); } } diff --git a/src/board/platform_common.rs b/src/board/platform_common.rs index b1fe79deaeb64a6c1a0c9d86b877b2d5f4e8b6c7..a2f8c204344ea394d23a279de46bc811d52ac95e 100644 --- a/src/board/platform_common.rs +++ b/src/board/platform_common.rs @@ -12,7 +12,6 @@ use crate::arch::GicDesc; use crate::arch::SmmuDesc; pub const PLATFORM_CPU_NUM_MAX: usize = 8; -pub const TOTAL_MEM_REGION_MAX: usize = 16; pub const PLATFORM_VCPU_NUM_MAX: usize = 8; #[repr(C)] @@ -29,17 +28,20 @@ pub struct PlatMemRegion { #[repr(C)] pub struct PlatMemoryConfig { - pub region_num: usize, pub base: usize, - pub regions: [PlatMemRegion; TOTAL_MEM_REGION_MAX], + pub regions: &'static [PlatMemRegion], +} + +pub struct PlatCpuCoreConfig { + pub name: u8, + pub mpidr: usize, + pub sched: SchedRule, } #[repr(C)] pub struct PlatCpuConfig { pub num: usize, - pub name: [u8; PLATFORM_CPU_NUM_MAX], - pub mpidr_list: [usize; PLATFORM_CPU_NUM_MAX], - pub sched_list: [SchedRule; PLATFORM_CPU_NUM_MAX], + pub core_list: &'static [PlatCpuCoreConfig], } #[repr(C)] diff --git a/src/board/qemu/platform.rs b/src/board/qemu/platform.rs index df83e07c7d7be8d11abcb89f2a529c487c017077..c73c9dd92cda6182471c941e4c5432047e8cf8ba 100644 --- a/src/board/qemu/platform.rs +++ b/src/board/qemu/platform.rs @@ -11,8 +11,8 @@ // TODO: move these core name to device use crate::arch::GicDesc; use crate::arch::SmmuDesc; -use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion}; -use crate::board::SchedRule::{self, RoundRobin}; +use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion, PlatCpuCoreConfig}; +use crate::board::SchedRule::RoundRobin; use crate::device::ARM_CORTEX_A57; use crate::driver::{read, write}; @@ -46,22 +46,32 @@ pub const DISK_PARTITION_2_SIZE: usize = 8192000; pub static PLAT_DESC: PlatformConfig = PlatformConfig { cpu_desc: PlatCpuConfig { num: 4, - mpidr_list: [0, 1, 2, 3, 4, 5, 6, 7], - name: [ARM_CORTEX_A57; 8], - sched_list: [ - RoundRobin, - RoundRobin, - RoundRobin, - RoundRobin, - SchedRule::None, - SchedRule::None, - SchedRule::None, - SchedRule::None, + core_list: &[ + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 1, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 2, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 3, + sched: RoundRobin, + }, ], }, mem_desc: PlatMemoryConfig { - region_num: 2, - regions: [ + regions: &[ + // reserve 0x48000000 ~ 0x48100000 for QEMU dtb PlatMemRegion { base: 0x40000000, size: 0x08000000, @@ -70,20 +80,6 @@ pub static PLAT_DESC: PlatformConfig = PlatformConfig { base: 0x50000000, size: 0x1f0000000, }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, ], base: 0x40000000, }, @@ -118,7 +114,7 @@ pub fn platform_power_on_secondary_cores() { fn _image_start(); } for i in 1..PLAT_DESC.cpu_desc.num { - platform_cpu_on(PLAT_DESC.cpu_desc.mpidr_list[i], _image_start as usize, 0); + platform_cpu_on(PLAT_DESC.cpu_desc.core_list[i].mpidr, _image_start as usize, 0); } } diff --git a/src/board/tx2/platform.rs b/src/board/tx2/platform.rs index 542f68e53998f72613e6371669e63ffd2bc7e1b6..be334ecdb74426cb638ed7b29757152ccc6fa8f0 100644 --- a/src/board/tx2/platform.rs +++ b/src/board/tx2/platform.rs @@ -10,7 +10,7 @@ use crate::arch::GicDesc; use crate::arch::SmmuDesc; -use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion, SchedRule}; +use crate::board::{ArchDesc, PlatCpuConfig, PlatformConfig, PlatMemoryConfig, PlatMemRegion, PlatCpuCoreConfig}; use crate::board::SchedRule::RoundRobin; use crate::device::ARM_CORTEX_A57; #[allow(unused_imports)] @@ -64,22 +64,31 @@ pub const SHARE_MEM_BASE: usize = 0xd_0000_0000; pub static PLAT_DESC: PlatformConfig = PlatformConfig { cpu_desc: PlatCpuConfig { num: 4, - mpidr_list: [0x80000100, 0x80000101, 0x80000102, 0x80000103, 0, 0, 0, 0], - name: [ARM_CORTEX_A57; 8], - sched_list: [ - RoundRobin, - RoundRobin, - RoundRobin, - RoundRobin, - SchedRule::None, - SchedRule::None, - SchedRule::None, - SchedRule::None, + core_list: &[ + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000100, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000101, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000102, + sched: RoundRobin, + }, + PlatCpuCoreConfig { + name: ARM_CORTEX_A57, + mpidr: 0x80000103, + sched: RoundRobin, + }, ], }, mem_desc: PlatMemoryConfig { - region_num: 3, - regions: [ + regions: &[ PlatMemRegion { base: 0x80000000, size: 0x10000000, @@ -92,19 +101,6 @@ pub static PLAT_DESC: PlatformConfig = PlatformConfig { base: 0xf0200000, size: 0x185600000, }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, - PlatMemRegion { base: 0, size: 0 }, ], base: 0x80000000, }, @@ -139,7 +135,7 @@ pub fn platform_power_on_secondary_cores() { fn _image_start(); } for i in 1..PLAT_DESC.cpu_desc.num { - platform_cpu_on(PLAT_DESC.cpu_desc.mpidr_list[i], _image_start as usize, 0); + platform_cpu_on(PLAT_DESC.cpu_desc.core_list[i].mpidr, _image_start as usize, 0); } } diff --git a/src/kernel/cpu.rs b/src/kernel/cpu.rs index f4a6ac4bc4f4b638f362c6f62da0d1c869efed44..cd69b77e1b36c2952892bfdf12e717a86370c06b 100644 --- a/src/kernel/cpu.rs +++ b/src/kernel/cpu.rs @@ -249,21 +249,7 @@ impl Cpu { #[no_mangle] #[link_section = ".cpu_private"] -pub static mut CPU: Cpu = Cpu { - id: 0, - cpu_state: CpuState::CpuInv, - active_vcpu: None, - ctx: None, - vcpu_array: VcpuArray::new(), - sched: SchedType::None, - current_irq: 0, - cpu_pt: CpuPt { - lvl1: [0; PTE_PER_PAGE], - lvl2: [0; PTE_PER_PAGE], - lvl3: [0; PTE_PER_PAGE], - }, - stack: [0; CPU_STACK_SIZE], -}; +pub static mut CPU: Cpu = Cpu::default(); pub fn current_cpu() -> &'static mut Cpu { unsafe { &mut CPU } @@ -333,16 +319,7 @@ pub fn cpu_idle() -> ! { } } -pub static mut CPU_LIST: [Cpu; PLATFORM_CPU_NUM_MAX] = [ - Cpu::default(), - Cpu::default(), - Cpu::default(), - Cpu::default(), - Cpu::default(), - Cpu::default(), - Cpu::default(), - Cpu::default(), -]; +pub static mut CPU_LIST: [Cpu; PLATFORM_CPU_NUM_MAX] = [const { Cpu::default() }; PLATFORM_CPU_NUM_MAX]; #[no_mangle] // #[link_section = ".text.boot"] diff --git a/src/kernel/mem.rs b/src/kernel/mem.rs index 4e30e12d0fa53a8b9e564d00e1ee48aafe3c5e55..477653729535d9b76115149cc1693512e4716c69 100644 --- a/src/kernel/mem.rs +++ b/src/kernel/mem.rs @@ -33,7 +33,7 @@ pub fn mem_heap_region_init() { fn _image_end(); } - if PLAT_DESC.mem_desc.region_num == 0 { + if PLAT_DESC.mem_desc.regions.is_empty() { println!("Platform has no memory region!"); } @@ -77,18 +77,16 @@ pub fn mem_heap_region_reserve(base_addr: usize, size: usize) { } fn mem_vm_region_init() { - if PLAT_DESC.mem_desc.region_num - 1 > TOTAL_MEM_REGION_MAX { - panic!("Platform memory regions overrun!"); - } else if PLAT_DESC.mem_desc.region_num == 0 { + if PLAT_DESC.mem_desc.regions.is_empty() { panic!("Platform Vm Memory Regions Overrun!"); } - if PLAT_DESC.mem_desc.region_num <= 1 { + if PLAT_DESC.mem_desc.regions.len() <= 1 { panic!("Platform has no VM memory region!"); } let mut pages: usize = 0; - let vm_region_num = PLAT_DESC.mem_desc.region_num - 1; + let vm_region_num = PLAT_DESC.mem_desc.regions.len() - 1; for i in 0..vm_region_num { let mut mem_region = MemRegion::new(); diff --git a/src/kernel/vcpu_array.rs b/src/kernel/vcpu_array.rs index 4234f59aed364e346b2d86d36b27a1b0501bdc41..a20746986ba5d6ce5d6f88c5dd23d0d96c201c42 100644 --- a/src/kernel/vcpu_array.rs +++ b/src/kernel/vcpu_array.rs @@ -20,7 +20,7 @@ pub struct VcpuArray { impl VcpuArray { pub const fn new() -> Self { Self { - array: [None, None, None, None, None, None, None, None], + array: [const { None }; VM_NUM_MAX], len: 0, } } @@ -93,7 +93,7 @@ impl VcpuArray { // Todo: add config for base slice pub fn cpu_sched_init() { - match PLAT_DESC.cpu_desc.sched_list[current_cpu().id] { + match PLAT_DESC.cpu_desc.core_list[current_cpu().id].sched { SchedRule::RoundRobin => { info!("cpu[{}] init Round Robin Scheduler", current_cpu().id); current_cpu().sched = SchedType::SchedRR(SchedulerRR::new(1)); diff --git a/src/kernel/vm.rs b/src/kernel/vm.rs index 30124b7cc62a48f046cb10b6946ab0ce0485b8fa..0e6a6ba4b08f736af4cedbbd355a8724c9640955 100644 --- a/src/kernel/vm.rs +++ b/src/kernel/vm.rs @@ -32,16 +32,7 @@ use super::vcpu::Vcpu; pub const DIRTY_MEM_THRESHOLD: usize = 0x2000; pub const VM_NUM_MAX: usize = 8; -pub static VM_IF_LIST: [Mutex; VM_NUM_MAX] = [ - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), - Mutex::new(VmInterface::default()), -]; +pub static VM_IF_LIST: [Mutex; VM_NUM_MAX] = [const { Mutex::new(VmInterface::default()) }; VM_NUM_MAX]; pub fn vm_if_reset(vm_id: usize) { let mut vm_if = VM_IF_LIST[vm_id].lock(); diff --git a/src/lib/util.rs b/src/lib/util.rs index f6631e6716564eccb056eb8956cf62938c334cd9..2648a8751808d7db5735ccde04672129d53cf6cd 100644 --- a/src/lib/util.rs +++ b/src/lib/util.rs @@ -9,21 +9,18 @@ // See the Mulan PSL v2 for more details. use core::ptr; - -use spin::Mutex; +use core::sync::atomic::{AtomicBool, Ordering}; use crate::arch::PAGE_SIZE; -pub static TRACE: Mutex = Mutex::new(true); +static TRACE: AtomicBool = AtomicBool::new(true); pub fn set_trace(value: bool) { - let mut trace = TRACE.lock(); - *trace = value; + TRACE.store(value, Ordering::Relaxed); } pub fn trace() -> bool { - let trace = TRACE.lock(); - *trace + TRACE.load(Ordering::Relaxed) } #[inline(always)] diff --git a/src/main.rs b/src/main.rs index 28e4c0e9770ab678b4a2bace848369c509a6d613..7ac67fc27c3bfd3a4d77f619d979605152bc15a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ #![feature(alloc_error_handler)] #![feature(const_btree_new)] #![feature(drain_filter)] +#![feature(inline_const)] #![allow(unused_doc_comments)] #![allow(special_module_name)]