xref: /DragonOS/kernel/src/sched/syscall.rs (revision 1496ba7b24a5e6954291ca9643b9f3cec567479a)
1 use crate::{
2     arch::CurrentIrqArch,
3     exception::InterruptArch,
4     process::ProcessManager,
5     smp::core::smp_get_processor_id,
6     syscall::{Syscall, SystemError},
7 };
8 
9 use super::core::{do_sched, CPU_EXECUTING};
10 
11 impl Syscall {
12     /// @brief 让系统立即运行调度器的系统调用
13     /// 请注意,该系统调用不能由ring3的程序发起
14     #[inline(always)]
15     pub fn sched(from_user: bool) -> Result<usize, SystemError> {
16         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
17 
18         // 进行权限校验,拒绝用户态发起调度
19         if from_user {
20             return Err(SystemError::EPERM);
21         }
22         // 根据调度结果统一进行切换
23         let pcb = do_sched();
24 
25         if pcb.is_some() {
26             CPU_EXECUTING.set(smp_get_processor_id(), pcb.as_ref().unwrap().pid());
27             unsafe { ProcessManager::switch_process(ProcessManager::current_pcb(), pcb.unwrap()) };
28         }
29         drop(irq_guard);
30         return Ok(0);
31     }
32 }
33