xref: /DragonOS/kernel/src/sched/syscall.rs (revision 406099704eb939ae23b18f0cfb3ed36c534c1c84)
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             let next_pcb = pcb.unwrap();
27             let current_pcb = ProcessManager::current_pcb();
28             // kdebug!("sched: current_pcb: {:?}, next_pcb: {:?}\n", current_pcb, next_pcb);
29             if current_pcb.pid() != next_pcb.pid() {
30                 CPU_EXECUTING.set(smp_get_processor_id(), next_pcb.pid());
31                 unsafe { ProcessManager::switch_process(current_pcb, next_pcb) };
32             }
33         }
34         drop(irq_guard);
35         return Ok(0);
36     }
37 }
38