1bacd691cSlogin use core::{ 2bacd691cSlogin fmt::Debug, 3bacd691cSlogin intrinsics::unlikely, 4bacd691cSlogin mem::{self, MaybeUninit}, 5bacd691cSlogin ptr::null_mut, 6bacd691cSlogin sync::atomic::{compiler_fence, Ordering}, 7bacd691cSlogin }; 862e46139SGou Ngai 9bacd691cSlogin use alloc::{boxed::Box, sync::Arc}; 10bacd691cSlogin use num_traits::FromPrimitive; 1162e46139SGou Ngai 1262e46139SGou Ngai use crate::{ 13bacd691cSlogin arch::{ 14bacd691cSlogin asm::{ 15bacd691cSlogin current::current_pcb, 16bacd691cSlogin irqflags::{local_irq_restore, local_irq_save}, 17bacd691cSlogin }, 18bacd691cSlogin interrupt::{cli, sti}, 19bacd691cSlogin }, 20bacd691cSlogin include::bindings::bindings::MAX_CPU_NUM, 21bacd691cSlogin kdebug, kinfo, 22bacd691cSlogin libs::rwlock::RwLock, 23bacd691cSlogin smp::core::smp_get_processor_id, 24bacd691cSlogin syscall::SystemError, 25bacd691cSlogin time::timer::clock, 2662e46139SGou Ngai }; 2762e46139SGou Ngai 2862e46139SGou Ngai const MAX_SOFTIRQ_NUM: u64 = 64; 29bacd691cSlogin const MAX_SOFTIRQ_RESTART: i32 = 20; 30bacd691cSlogin 31bacd691cSlogin static mut __CPU_PENDING: Option<Box<[VecStatus; MAX_CPU_NUM as usize]>> = None; 32bacd691cSlogin static mut __SORTIRQ_VECTORS: *mut Softirq = null_mut(); 33bacd691cSlogin 34bacd691cSlogin #[no_mangle] 35bacd691cSlogin pub extern "C" fn rs_softirq_init() { 36bacd691cSlogin softirq_init().expect("softirq_init failed"); 37bacd691cSlogin } 38bacd691cSlogin 39bacd691cSlogin pub fn softirq_init() -> Result<(), SystemError> { 40bacd691cSlogin kinfo!("Initializing softirq..."); 41bacd691cSlogin unsafe { 42bacd691cSlogin __SORTIRQ_VECTORS = Box::leak(Box::new(Softirq::new())); 43bacd691cSlogin __CPU_PENDING = Some(Box::new([VecStatus::default(); MAX_CPU_NUM as usize])); 44bacd691cSlogin let cpu_pending = __CPU_PENDING.as_mut().unwrap(); 45bacd691cSlogin for i in 0..MAX_CPU_NUM { 46bacd691cSlogin cpu_pending[i as usize] = VecStatus::default(); 47bacd691cSlogin } 48bacd691cSlogin } 49bacd691cSlogin return Ok(()); 50bacd691cSlogin } 51bacd691cSlogin 52bacd691cSlogin #[inline(always)] 53bacd691cSlogin pub fn softirq_vectors() -> &'static mut Softirq { 54bacd691cSlogin unsafe { 55bacd691cSlogin return __SORTIRQ_VECTORS.as_mut().unwrap(); 56bacd691cSlogin } 57bacd691cSlogin } 58bacd691cSlogin 59bacd691cSlogin #[inline(always)] 60bacd691cSlogin fn cpu_pending(cpu_id: usize) -> &'static mut VecStatus { 61bacd691cSlogin unsafe { 62bacd691cSlogin return &mut __CPU_PENDING.as_mut().unwrap()[cpu_id]; 63bacd691cSlogin } 64bacd691cSlogin } 6562e46139SGou Ngai 6662e46139SGou Ngai /// 软中断向量号码 6762e46139SGou Ngai #[allow(dead_code)] 6862e46139SGou Ngai #[repr(u8)] 69bacd691cSlogin #[derive(FromPrimitive, Copy, Clone, Debug, PartialEq, Eq)] 7062e46139SGou Ngai pub enum SoftirqNumber { 71bacd691cSlogin /// 时钟软中断信号 72bacd691cSlogin TIMER = 0, 7362e46139SGou Ngai VideoRefresh = 1, //帧缓冲区刷新软中断 7462e46139SGou Ngai } 7562e46139SGou Ngai 76bacd691cSlogin impl From<u64> for SoftirqNumber { 77bacd691cSlogin fn from(value: u64) -> Self { 78bacd691cSlogin return <Self as FromPrimitive>::from_u64(value).unwrap(); 7962e46139SGou Ngai } 8062e46139SGou Ngai } 8162e46139SGou Ngai 82bacd691cSlogin bitflags! { 83bacd691cSlogin #[derive(Default)] 84bacd691cSlogin pub struct VecStatus: u64 { 85bacd691cSlogin const TIMER = 1 << 0; 86bacd691cSlogin const VIDEO_REFRESH = 1 << 1; 87bacd691cSlogin } 88bacd691cSlogin } 89bacd691cSlogin 90bacd691cSlogin impl From<SoftirqNumber> for VecStatus { 91bacd691cSlogin fn from(value: SoftirqNumber) -> Self { 92bacd691cSlogin return Self::from_bits_truncate(1 << (value as u64)); 93bacd691cSlogin } 94bacd691cSlogin } 95bacd691cSlogin 96bacd691cSlogin pub trait SoftirqVec: Send + Sync + Debug { 97bacd691cSlogin fn run(&self); 98bacd691cSlogin } 99bacd691cSlogin 100bacd691cSlogin #[derive(Debug)] 10162e46139SGou Ngai pub struct Softirq { 102bacd691cSlogin table: RwLock<[Option<Arc<dyn SoftirqVec>>; MAX_SOFTIRQ_NUM as usize]>, 10362e46139SGou Ngai } 10462e46139SGou Ngai impl Softirq { 105bacd691cSlogin fn new() -> Softirq { 106bacd691cSlogin let mut data: [MaybeUninit<Option<Arc<dyn SoftirqVec>>>; MAX_SOFTIRQ_NUM as usize] = 107bacd691cSlogin unsafe { MaybeUninit::uninit().assume_init() }; 108bacd691cSlogin 109bacd691cSlogin for i in 0..MAX_SOFTIRQ_NUM { 110bacd691cSlogin data[i as usize] = MaybeUninit::new(None); 11162e46139SGou Ngai } 11262e46139SGou Ngai 113bacd691cSlogin let data: [Option<Arc<dyn SoftirqVec>>; MAX_SOFTIRQ_NUM as usize] = unsafe { 114bacd691cSlogin mem::transmute::<_, [Option<Arc<dyn SoftirqVec>>; MAX_SOFTIRQ_NUM as usize]>(data) 115bacd691cSlogin }; 11662e46139SGou Ngai 117bacd691cSlogin return Softirq { 118bacd691cSlogin table: RwLock::new(data), 119bacd691cSlogin }; 12062e46139SGou Ngai } 12162e46139SGou Ngai 12262e46139SGou Ngai /// @brief 注册软中断向量 123bacd691cSlogin /// 124bacd691cSlogin /// @param softirq_num 中断向量号 125bacd691cSlogin /// 126bacd691cSlogin /// @param hanlder 中断函数对应的结构体 12762e46139SGou Ngai pub fn register_softirq( 128bacd691cSlogin &self, 129bacd691cSlogin softirq_num: SoftirqNumber, 130bacd691cSlogin handler: Arc<dyn SoftirqVec>, 131bacd691cSlogin ) -> Result<i32, SystemError> { 132bacd691cSlogin // kdebug!("register_softirq softirq_num = {:?}", softirq_num as u64); 13362e46139SGou Ngai 134bacd691cSlogin // let self = &mut SOFTIRQ_VECTORS.lock(); 135bacd691cSlogin // 判断该软中断向量是否已经被注册 136bacd691cSlogin let mut table_guard = self.table.write(); 137bacd691cSlogin if table_guard[softirq_num as usize].is_some() { 138bacd691cSlogin // kdebug!("register_softirq failed"); 139bacd691cSlogin 140bacd691cSlogin return Err(SystemError::EINVAL); 14162e46139SGou Ngai } 142bacd691cSlogin table_guard[softirq_num as usize] = Some(handler); 143bacd691cSlogin drop(table_guard); 144bacd691cSlogin 145bacd691cSlogin // kdebug!( 146bacd691cSlogin // "register_softirq successfully, softirq_num = {:?}", 147bacd691cSlogin // softirq_num as u64 148bacd691cSlogin // ); 149bacd691cSlogin compiler_fence(Ordering::SeqCst); 150bacd691cSlogin return Ok(0); 15162e46139SGou Ngai } 15262e46139SGou Ngai 15362e46139SGou Ngai /// @brief 解注册软中断向量 154bacd691cSlogin /// 15562e46139SGou Ngai /// @param irq_num 中断向量号码 156bacd691cSlogin pub fn unregister_softirq(&self, softirq_num: SoftirqNumber) { 157bacd691cSlogin // kdebug!("unregister_softirq softirq_num = {:?}", softirq_num as u64); 158bacd691cSlogin let table_guard = &mut self.table.write(); 159bacd691cSlogin // 将软中断向量清空 160bacd691cSlogin table_guard[softirq_num as usize] = None; 161bacd691cSlogin drop(table_guard); 162bacd691cSlogin // 将对应位置的pending和runing都置0 163bacd691cSlogin // self.running.lock().set(VecStatus::from(softirq_num), false); 164bacd691cSlogin // 将对应CPU的pending置0 165bacd691cSlogin compiler_fence(Ordering::SeqCst); 166bacd691cSlogin cpu_pending(smp_get_processor_id() as usize).set(VecStatus::from(softirq_num), false); 167bacd691cSlogin compiler_fence(Ordering::SeqCst); 16862e46139SGou Ngai } 169bacd691cSlogin 170bacd691cSlogin pub fn do_softirq(&self) { 171bacd691cSlogin // TODO pcb的flags未修改 172bacd691cSlogin // todo: 是否需要判断在当前cpu上面,该函数的嵌套层数?(防止爆栈) 173bacd691cSlogin let end = clock() + 500 * 2; 174bacd691cSlogin let cpu_id = smp_get_processor_id(); 175bacd691cSlogin let mut max_restart = MAX_SOFTIRQ_RESTART; 176bacd691cSlogin loop { 177bacd691cSlogin compiler_fence(Ordering::SeqCst); 178bacd691cSlogin let pending = cpu_pending(cpu_id as usize).bits; 179bacd691cSlogin cpu_pending(cpu_id as usize).bits = 0; 180bacd691cSlogin compiler_fence(Ordering::SeqCst); 181bacd691cSlogin 182bacd691cSlogin sti(); 183bacd691cSlogin if pending != 0 { 184bacd691cSlogin for i in 0..MAX_SOFTIRQ_NUM { 185bacd691cSlogin if pending & (1 << i) == 0 { 18662e46139SGou Ngai continue; 18762e46139SGou Ngai } 188bacd691cSlogin 189bacd691cSlogin let table_guard = self.table.read(); 190bacd691cSlogin let softirq_func = table_guard[i as usize].clone(); 191bacd691cSlogin drop(table_guard); 192bacd691cSlogin if softirq_func.is_none() { 193bacd691cSlogin continue; 194bacd691cSlogin } 195bacd691cSlogin 196bacd691cSlogin let prev_count = current_pcb().preempt_count; 197bacd691cSlogin 198bacd691cSlogin softirq_func.as_ref().unwrap().run(); 199bacd691cSlogin if unlikely(prev_count != current_pcb().preempt_count) { 200bacd691cSlogin kdebug!( 201bacd691cSlogin "entered softirq {:?} with preempt_count {:?},exited with {:?}", 202bacd691cSlogin i, 203bacd691cSlogin prev_count, 204bacd691cSlogin current_pcb().preempt_count 205bacd691cSlogin ); 206bacd691cSlogin current_pcb().preempt_count = prev_count; 207bacd691cSlogin } 208bacd691cSlogin } 209bacd691cSlogin } 210bacd691cSlogin cli(); 211bacd691cSlogin max_restart -= 1; 212bacd691cSlogin compiler_fence(Ordering::SeqCst); 213bacd691cSlogin if cpu_pending(cpu_id as usize).is_empty() { 214bacd691cSlogin compiler_fence(Ordering::SeqCst); 215bacd691cSlogin if clock() < end && max_restart > 0 { 216bacd691cSlogin continue; 217bacd691cSlogin } else { 218bacd691cSlogin break; 219bacd691cSlogin } 220bacd691cSlogin } else { 221bacd691cSlogin // TODO:当有softirqd时 唤醒它 22262e46139SGou Ngai break; 22362e46139SGou Ngai } 22462e46139SGou Ngai } 22562e46139SGou Ngai } 22662e46139SGou Ngai 227bacd691cSlogin pub fn raise_softirq(&self, softirq_num: SoftirqNumber) { 228bacd691cSlogin let mut flags = 0; 229bacd691cSlogin local_irq_save(&mut flags); 230bacd691cSlogin let processor_id = smp_get_processor_id() as usize; 231bacd691cSlogin 232bacd691cSlogin cpu_pending(processor_id).insert(VecStatus::from(softirq_num)); 233bacd691cSlogin 234bacd691cSlogin compiler_fence(Ordering::SeqCst); 235bacd691cSlogin 236bacd691cSlogin local_irq_restore(&flags); 237bacd691cSlogin // kdebug!("raise_softirq exited"); 23862e46139SGou Ngai } 239*8fd71f27Shoumkh pub unsafe fn clear_softirq_pending(&self, softirq_num: SoftirqNumber) { 240bacd691cSlogin compiler_fence(Ordering::SeqCst); 241bacd691cSlogin cpu_pending(smp_get_processor_id() as usize).remove(VecStatus::from(softirq_num)); 242bacd691cSlogin compiler_fence(Ordering::SeqCst); 24362e46139SGou Ngai } 24462e46139SGou Ngai } 245bacd691cSlogin 246bacd691cSlogin // ======= 以下为给C提供的接口 ======= 247bacd691cSlogin #[no_mangle] 248bacd691cSlogin pub extern "C" fn rs_raise_softirq(softirq_num: u32) { 249bacd691cSlogin softirq_vectors().raise_softirq(SoftirqNumber::from(softirq_num as u64)); 25062e46139SGou Ngai } 251bacd691cSlogin 252bacd691cSlogin #[no_mangle] 253bacd691cSlogin pub extern "C" fn rs_unregister_softirq(softirq_num: u32) { 254bacd691cSlogin softirq_vectors().unregister_softirq(SoftirqNumber::from(softirq_num as u64)); 25562e46139SGou Ngai } 256bacd691cSlogin 257bacd691cSlogin #[no_mangle] 258bacd691cSlogin pub extern "C" fn rs_do_softirq() { 259bacd691cSlogin softirq_vectors().do_softirq(); 260bacd691cSlogin } 261bacd691cSlogin 262bacd691cSlogin #[no_mangle] 263bacd691cSlogin pub extern "C" fn rs_clear_softirq_pending(softirq_num: u32) { 264*8fd71f27Shoumkh unsafe { 265bacd691cSlogin softirq_vectors().clear_softirq_pending(SoftirqNumber::from(softirq_num as u64)); 26662e46139SGou Ngai } 267*8fd71f27Shoumkh } 268