hermit/scheduler/
mod.rs

1#![allow(clippy::type_complexity)]
2
3use alloc::boxed::Box;
4use alloc::collections::{BTreeMap, VecDeque};
5use alloc::rc::Rc;
6use alloc::sync::Arc;
7#[cfg(feature = "smp")]
8use alloc::vec::Vec;
9use core::cell::RefCell;
10use core::ptr;
11#[cfg(all(target_arch = "x86_64", feature = "smp"))]
12use core::sync::atomic::AtomicBool;
13use core::sync::atomic::{AtomicI32, AtomicU32, Ordering};
14
15use ahash::RandomState;
16use crossbeam_utils::Backoff;
17use hashbrown::{HashMap, hash_map};
18use hermit_sync::*;
19#[cfg(target_arch = "riscv64")]
20use riscv::register::sstatus;
21
22use crate::arch::core_local::*;
23#[cfg(target_arch = "riscv64")]
24use crate::arch::switch::switch_to_task;
25#[cfg(target_arch = "x86_64")]
26use crate::arch::switch::{switch_to_fpu_owner, switch_to_task};
27use crate::arch::{get_processor_count, interrupts};
28use crate::errno::Errno;
29use crate::fd::{FileDescriptor, ObjectInterface};
30use crate::kernel::scheduler::TaskStacks;
31use crate::scheduler::task::*;
32use crate::{arch, io};
33
34pub mod task;
35
36static NO_TASKS: AtomicU32 = AtomicU32::new(0);
37/// Map between Core ID and per-core scheduler
38#[cfg(feature = "smp")]
39static SCHEDULER_INPUTS: SpinMutex<Vec<&InterruptTicketMutex<SchedulerInput>>> =
40	SpinMutex::new(Vec::new());
41#[cfg(all(target_arch = "x86_64", feature = "smp"))]
42static CORE_HLT_STATE: SpinMutex<Vec<&AtomicBool>> = SpinMutex::new(Vec::new());
43/// Map between Task ID and Queue of waiting tasks
44static WAITING_TASKS: InterruptTicketMutex<BTreeMap<TaskId, VecDeque<TaskHandle>>> =
45	InterruptTicketMutex::new(BTreeMap::new());
46/// Map between Task ID and TaskHandle
47static TASKS: InterruptTicketMutex<BTreeMap<TaskId, TaskHandle>> =
48	InterruptTicketMutex::new(BTreeMap::new());
49
50/// Unique identifier for a core.
51pub type CoreId = u32;
52
53#[cfg(feature = "smp")]
54pub(crate) struct SchedulerInput {
55	/// Queue of new tasks
56	new_tasks: VecDeque<NewTask>,
57	/// Queue of task, which are wakeup by another core
58	wakeup_tasks: VecDeque<TaskHandle>,
59}
60
61#[cfg(feature = "smp")]
62impl SchedulerInput {
63	pub fn new() -> Self {
64		Self {
65			new_tasks: VecDeque::new(),
66			wakeup_tasks: VecDeque::new(),
67		}
68	}
69}
70
71#[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))]
72#[cfg_attr(
73	not(any(target_arch = "x86_64", target_arch = "aarch64")),
74	repr(align(64))
75)]
76pub(crate) struct PerCoreScheduler {
77	/// Core ID of this per-core scheduler
78	#[cfg(feature = "smp")]
79	core_id: CoreId,
80	/// Task which is currently running
81	current_task: Rc<RefCell<Task>>,
82	/// Idle Task
83	idle_task: Rc<RefCell<Task>>,
84	/// Task that currently owns the FPU
85	#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
86	fpu_owner: Rc<RefCell<Task>>,
87	/// Queue of tasks, which are ready
88	ready_queue: PriorityTaskQueue,
89	/// Queue of tasks, which are finished and can be released
90	finished_tasks: VecDeque<Rc<RefCell<Task>>>,
91	/// Queue of blocked tasks, sorted by wakeup time.
92	blocked_tasks: BlockedTaskQueue,
93}
94
95pub(crate) trait PerCoreSchedulerExt {
96	/// Triggers the scheduler to reschedule the tasks.
97	/// Interrupt flag will be cleared during the reschedule
98	fn reschedule(self);
99
100	#[cfg(feature = "net")]
101	fn add_network_timer(self, wakeup_time: Option<u64>);
102
103	/// Terminate the current task on the current core.
104	fn exit(self, exit_code: i32) -> !;
105}
106
107impl PerCoreSchedulerExt for &mut PerCoreScheduler {
108	#[cfg(target_arch = "x86_64")]
109	fn reschedule(self) {
110		without_interrupts(|| {
111			if let Some(last_stack_pointer) = self.scheduler() {
112				let (new_stack_pointer, is_idle) = {
113					let borrowed = self.current_task.borrow();
114					(
115						borrowed.last_stack_pointer,
116						borrowed.status == TaskStatus::Idle,
117					)
118				};
119
120				if is_idle || Rc::ptr_eq(&self.current_task, &self.fpu_owner) {
121					unsafe {
122						switch_to_fpu_owner(
123							last_stack_pointer,
124							new_stack_pointer.as_u64() as usize,
125						);
126					}
127				} else {
128					unsafe {
129						switch_to_task(last_stack_pointer, new_stack_pointer.as_u64() as usize);
130					}
131				}
132			}
133		});
134	}
135
136	/// Trigger an interrupt to reschedule the system
137	#[cfg(target_arch = "aarch64")]
138	fn reschedule(self) {
139		use core::arch::asm;
140
141		use arm_gic::IntId;
142		use arm_gic::gicv3::{GicV3, SgiTarget, SgiTargetGroup};
143
144		use crate::interrupts::SGI_RESCHED;
145
146		unsafe {
147			asm!("dsb nsh", "isb", options(nostack, nomem, preserves_flags));
148		}
149
150		let reschedid = IntId::sgi(SGI_RESCHED.into());
151		#[cfg(feature = "smp")]
152		let core_id = self.core_id;
153		#[cfg(not(feature = "smp"))]
154		let core_id = 0;
155
156		GicV3::send_sgi(
157			reschedid,
158			SgiTarget::List {
159				affinity3: 0,
160				affinity2: 0,
161				affinity1: 0,
162				target_list: 1 << core_id,
163			},
164			SgiTargetGroup::CurrentGroup1,
165		);
166
167		interrupts::enable();
168	}
169
170	#[cfg(target_arch = "riscv64")]
171	fn reschedule(self) {
172		without_interrupts(|| self.scheduler());
173	}
174
175	#[cfg(feature = "net")]
176	fn add_network_timer(self, wakeup_time: Option<u64>) {
177		without_interrupts(|| {
178			self.blocked_tasks.add_network_timer(wakeup_time);
179		});
180	}
181
182	fn exit(self, exit_code: i32) -> ! {
183		without_interrupts(|| {
184			// Get the current task.
185			let mut current_task_borrowed = self.current_task.borrow_mut();
186			assert_ne!(
187				current_task_borrowed.status,
188				TaskStatus::Idle,
189				"Trying to terminate the idle task"
190			);
191
192			// Finish the task and reschedule.
193			debug!(
194				"Finishing task {} with exit code {}",
195				current_task_borrowed.id, exit_code
196			);
197			current_task_borrowed.status = TaskStatus::Finished;
198			NO_TASKS.fetch_sub(1, Ordering::SeqCst);
199
200			let current_id = current_task_borrowed.id;
201			drop(current_task_borrowed);
202
203			// wakeup tasks, which are waiting for task with the identifier id
204			if let Some(mut queue) = WAITING_TASKS.lock().remove(&current_id) {
205				while let Some(task) = queue.pop_front() {
206					self.custom_wakeup(task);
207				}
208			}
209		});
210
211		self.reschedule();
212		unreachable!()
213	}
214}
215
216struct NewTask {
217	tid: TaskId,
218	func: unsafe extern "C" fn(usize),
219	arg: usize,
220	prio: Priority,
221	core_id: CoreId,
222	stacks: TaskStacks,
223	object_map: Arc<
224		RwSpinLock<
225			HashMap<FileDescriptor, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>,
226		>,
227	>,
228}
229
230impl From<NewTask> for Task {
231	fn from(value: NewTask) -> Self {
232		let NewTask {
233			tid,
234			func,
235			arg,
236			prio,
237			core_id,
238			stacks,
239			object_map,
240		} = value;
241		let mut task = Self::new(tid, core_id, TaskStatus::Ready, prio, stacks, object_map);
242		task.create_stack_frame(func, arg);
243		task
244	}
245}
246
247impl PerCoreScheduler {
248	/// Spawn a new task.
249	pub unsafe fn spawn(
250		func: unsafe extern "C" fn(usize),
251		arg: usize,
252		prio: Priority,
253		core_id: CoreId,
254		stack_size: usize,
255	) -> TaskId {
256		// Create the new task.
257		let tid = get_tid();
258		let stacks = TaskStacks::new(stack_size);
259		let new_task = NewTask {
260			tid,
261			func,
262			arg,
263			prio,
264			core_id,
265			stacks,
266			object_map: core_scheduler().get_current_task_object_map(),
267		};
268
269		// Add it to the task lists.
270		let wakeup = {
271			#[cfg(feature = "smp")]
272			let mut input_locked = get_scheduler_input(core_id).lock();
273			WAITING_TASKS.lock().insert(tid, VecDeque::with_capacity(1));
274			TASKS.lock().insert(
275				tid,
276				TaskHandle::new(
277					tid,
278					prio,
279					#[cfg(feature = "smp")]
280					core_id,
281				),
282			);
283			NO_TASKS.fetch_add(1, Ordering::SeqCst);
284
285			#[cfg(feature = "smp")]
286			if core_id == core_scheduler().core_id {
287				let task = Rc::new(RefCell::new(Task::from(new_task)));
288				core_scheduler().ready_queue.push(task);
289				false
290			} else {
291				input_locked.new_tasks.push_back(new_task);
292				true
293			}
294			#[cfg(not(feature = "smp"))]
295			if core_id == 0 {
296				let task = Rc::new(RefCell::new(Task::from(new_task)));
297				core_scheduler().ready_queue.push(task);
298				false
299			} else {
300				panic!("Invalid core_id {}!", core_id)
301			}
302		};
303
304		debug!("Creating task {tid} with priority {prio} on core {core_id}");
305
306		if wakeup {
307			arch::wakeup_core(core_id);
308		}
309
310		tid
311	}
312
313	#[cfg(feature = "newlib")]
314	fn clone_impl(&self, func: extern "C" fn(usize), arg: usize) -> TaskId {
315		static NEXT_CORE_ID: AtomicU32 = AtomicU32::new(1);
316
317		// Get the Core ID of the next CPU.
318		let core_id: CoreId = {
319			// Increase the CPU number by 1.
320			let id = NEXT_CORE_ID.fetch_add(1, Ordering::SeqCst);
321
322			// Check for overflow.
323			if id == arch::get_processor_count() {
324				NEXT_CORE_ID.store(0, Ordering::SeqCst);
325				0
326			} else {
327				id
328			}
329		};
330
331		// Get the current task.
332		let current_task_borrowed = self.current_task.borrow();
333
334		// Clone the current task.
335		let tid = get_tid();
336		let clone_task = NewTask {
337			tid,
338			func,
339			arg,
340			prio: current_task_borrowed.prio,
341			core_id,
342			stacks: TaskStacks::new(current_task_borrowed.stacks.get_user_stack_size()),
343			object_map: current_task_borrowed.object_map.clone(),
344		};
345
346		// Add it to the task lists.
347		let wakeup = {
348			#[cfg(feature = "smp")]
349			let mut input_locked = get_scheduler_input(core_id).lock();
350			WAITING_TASKS.lock().insert(tid, VecDeque::with_capacity(1));
351			TASKS.lock().insert(
352				tid,
353				TaskHandle::new(
354					tid,
355					current_task_borrowed.prio,
356					#[cfg(feature = "smp")]
357					core_id,
358				),
359			);
360			NO_TASKS.fetch_add(1, Ordering::SeqCst);
361			#[cfg(feature = "smp")]
362			if core_id == core_scheduler().core_id {
363				let clone_task = Rc::new(RefCell::new(Task::from(clone_task)));
364				core_scheduler().ready_queue.push(clone_task);
365				false
366			} else {
367				input_locked.new_tasks.push_back(clone_task);
368				true
369			}
370			#[cfg(not(feature = "smp"))]
371			if core_id == 0 {
372				let clone_task = Rc::new(RefCell::new(Task::from(clone_task)));
373				core_scheduler().ready_queue.push(clone_task);
374				false
375			} else {
376				panic!("Invalid core_id {}!", core_id);
377			}
378		};
379
380		// Wake up the CPU
381		if wakeup {
382			arch::wakeup_core(core_id);
383		}
384
385		tid
386	}
387
388	#[cfg(feature = "newlib")]
389	pub fn clone(&self, func: extern "C" fn(usize), arg: usize) -> TaskId {
390		without_interrupts(|| self.clone_impl(func, arg))
391	}
392
393	/// Returns `true` if a reschedule is required
394	#[inline]
395	#[cfg(all(any(target_arch = "x86_64", target_arch = "riscv64"), feature = "smp"))]
396	pub fn is_scheduling(&self) -> bool {
397		self.current_task.borrow().prio < self.ready_queue.get_highest_priority()
398	}
399
400	#[inline]
401	pub fn handle_waiting_tasks(&mut self) {
402		without_interrupts(|| {
403			crate::executor::run();
404			self.blocked_tasks
405				.handle_waiting_tasks(&mut self.ready_queue);
406		});
407	}
408
409	#[cfg(not(feature = "smp"))]
410	pub fn custom_wakeup(&mut self, task: TaskHandle) {
411		without_interrupts(|| {
412			let task = self.blocked_tasks.custom_wakeup(task);
413			self.ready_queue.push(task);
414		});
415	}
416
417	#[cfg(feature = "smp")]
418	pub fn custom_wakeup(&mut self, task: TaskHandle) {
419		if task.get_core_id() == self.core_id {
420			without_interrupts(|| {
421				let task = self.blocked_tasks.custom_wakeup(task);
422				self.ready_queue.push(task);
423			});
424		} else {
425			get_scheduler_input(task.get_core_id())
426				.lock()
427				.wakeup_tasks
428				.push_back(task);
429			// Wake up the CPU
430			arch::wakeup_core(task.get_core_id());
431		}
432	}
433
434	#[inline]
435	pub fn block_current_task(&mut self, wakeup_time: Option<u64>) {
436		without_interrupts(|| {
437			self.blocked_tasks
438				.add(self.current_task.clone(), wakeup_time);
439		});
440	}
441
442	#[inline]
443	pub fn get_current_task_handle(&self) -> TaskHandle {
444		without_interrupts(|| {
445			let current_task_borrowed = self.current_task.borrow();
446
447			TaskHandle::new(
448				current_task_borrowed.id,
449				current_task_borrowed.prio,
450				#[cfg(feature = "smp")]
451				current_task_borrowed.core_id,
452			)
453		})
454	}
455
456	#[inline]
457	pub fn get_current_task_id(&self) -> TaskId {
458		without_interrupts(|| self.current_task.borrow().id)
459	}
460
461	#[inline]
462	pub fn get_current_task_object_map(
463		&self,
464	) -> Arc<
465		RwSpinLock<
466			HashMap<FileDescriptor, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>,
467		>,
468	> {
469		without_interrupts(|| self.current_task.borrow().object_map.clone())
470	}
471
472	/// Map a file descriptor to their IO interface and returns
473	/// the shared reference
474	#[inline]
475	pub fn get_object(
476		&self,
477		fd: FileDescriptor,
478	) -> io::Result<Arc<async_lock::RwLock<dyn ObjectInterface>>> {
479		without_interrupts(|| {
480			let current_task = self.current_task.borrow();
481			let object_map = current_task.object_map.read();
482			object_map.get(&fd).cloned().ok_or(Errno::Badf)
483		})
484	}
485
486	/// Creates a new map between file descriptor and their IO interface and
487	/// clone the standard descriptors.
488	#[cfg(feature = "common-os")]
489	#[cfg_attr(not(target_arch = "x86_64"), expect(dead_code))]
490	pub fn recreate_objmap(&self) -> io::Result<()> {
491		let mut map = HashMap::<
492			FileDescriptor,
493			Arc<async_lock::RwLock<dyn ObjectInterface>>,
494			RandomState,
495		>::with_hasher(RandomState::with_seeds(0, 0, 0, 0));
496
497		without_interrupts(|| {
498			let mut current_task = self.current_task.borrow_mut();
499			let object_map = current_task.object_map.read();
500
501			// clone standard file descriptors
502			for i in 0..3 {
503				if let Some(obj) = object_map.get(&i) {
504					map.insert(i, obj.clone());
505				}
506			}
507
508			drop(object_map);
509			current_task.object_map = Arc::new(RwSpinLock::new(map));
510		});
511
512		Ok(())
513	}
514
515	/// Insert a new IO interface and returns a file descriptor as
516	/// identifier to this object
517	pub fn insert_object(
518		&self,
519		obj: Arc<async_lock::RwLock<dyn ObjectInterface>>,
520	) -> io::Result<FileDescriptor> {
521		without_interrupts(|| {
522			let current_task = self.current_task.borrow();
523			let mut object_map = current_task.object_map.write();
524
525			let new_fd = || -> io::Result<FileDescriptor> {
526				let mut fd: FileDescriptor = 0;
527				loop {
528					if !object_map.contains_key(&fd) {
529						break Ok(fd);
530					} else if fd == FileDescriptor::MAX {
531						break Err(Errno::Overflow);
532					}
533
534					fd = fd.saturating_add(1);
535				}
536			};
537
538			let fd = new_fd()?;
539			let _ = object_map.insert(fd, obj.clone());
540			Ok(fd)
541		})
542	}
543
544	/// Duplicate a IO interface and returns a new file descriptor as
545	/// identifier to the new copy
546	pub fn dup_object(&self, fd: FileDescriptor) -> io::Result<FileDescriptor> {
547		without_interrupts(|| {
548			let current_task = self.current_task.borrow();
549			let mut object_map = current_task.object_map.write();
550
551			let obj = (*(object_map.get(&fd).ok_or(Errno::Inval)?)).clone();
552
553			let new_fd = || -> io::Result<FileDescriptor> {
554				let mut fd: FileDescriptor = 0;
555				loop {
556					if !object_map.contains_key(&fd) {
557						break Ok(fd);
558					} else if fd == FileDescriptor::MAX {
559						break Err(Errno::Overflow);
560					}
561
562					fd = fd.saturating_add(1);
563				}
564			};
565
566			let fd = new_fd()?;
567			match object_map.entry(fd) {
568				hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile),
569				hash_map::Entry::Vacant(vacant_entry) => {
570					vacant_entry.insert(obj);
571					Ok(fd)
572				}
573			}
574		})
575	}
576
577	pub fn dup_object2(
578		&self,
579		fd1: FileDescriptor,
580		fd2: FileDescriptor,
581	) -> io::Result<FileDescriptor> {
582		without_interrupts(|| {
583			let current_task = self.current_task.borrow();
584			let mut object_map = current_task.object_map.write();
585
586			let obj = object_map.get(&fd1).cloned().ok_or(Errno::Badf)?;
587
588			match object_map.entry(fd2) {
589				hash_map::Entry::Occupied(_occupied_entry) => Err(Errno::Mfile),
590				hash_map::Entry::Vacant(vacant_entry) => {
591					vacant_entry.insert(obj);
592					Ok(fd2)
593				}
594			}
595		})
596	}
597
598	/// Remove a IO interface, which is named by the file descriptor
599	pub fn remove_object(
600		&self,
601		fd: FileDescriptor,
602	) -> io::Result<Arc<async_lock::RwLock<dyn ObjectInterface>>> {
603		without_interrupts(|| {
604			let current_task = self.current_task.borrow();
605			let mut object_map = current_task.object_map.write();
606
607			object_map.remove(&fd).ok_or(Errno::Badf)
608		})
609	}
610
611	#[inline]
612	pub fn get_current_task_prio(&self) -> Priority {
613		without_interrupts(|| self.current_task.borrow().prio)
614	}
615
616	/// Returns reference to prio_bitmap
617	#[allow(dead_code)]
618	#[inline]
619	pub fn get_priority_bitmap(&self) -> &u64 {
620		self.ready_queue.get_priority_bitmap()
621	}
622
623	#[cfg(target_arch = "x86_64")]
624	pub fn set_current_kernel_stack(&self) {
625		let current_task_borrowed = self.current_task.borrow();
626		let tss = unsafe { &mut *CoreLocal::get().tss.get() };
627
628		let rsp = current_task_borrowed.stacks.get_kernel_stack()
629			+ current_task_borrowed.stacks.get_kernel_stack_size() as u64
630			- TaskStacks::MARKER_SIZE as u64;
631		tss.privilege_stack_table[0] = rsp.into();
632		CoreLocal::get().kernel_stack.set(rsp.as_mut_ptr());
633		let ist_start = current_task_borrowed.stacks.get_interrupt_stack()
634			+ current_task_borrowed.stacks.get_interrupt_stack_size() as u64
635			- TaskStacks::MARKER_SIZE as u64;
636		tss.interrupt_stack_table[0] = ist_start.into();
637	}
638
639	pub fn set_current_task_priority(&mut self, prio: Priority) {
640		without_interrupts(|| {
641			trace!("Change priority of the current task");
642			self.current_task.borrow_mut().prio = prio;
643		});
644	}
645
646	pub fn set_priority(&mut self, id: TaskId, prio: Priority) -> Result<(), ()> {
647		trace!("Change priority of task {id} to priority {prio}");
648
649		without_interrupts(|| {
650			let task = get_task_handle(id).ok_or(())?;
651			#[cfg(feature = "smp")]
652			let other_core = task.get_core_id() != self.core_id;
653			#[cfg(not(feature = "smp"))]
654			let other_core = false;
655
656			if other_core {
657				warn!("Have to change the priority on another core");
658			} else if self.current_task.borrow().id == task.get_id() {
659				self.current_task.borrow_mut().prio = prio;
660			} else {
661				self.ready_queue
662					.set_priority(task, prio)
663					.expect("Do not find valid task in ready queue");
664			}
665
666			Ok(())
667		})
668	}
669
670	#[cfg(target_arch = "riscv64")]
671	pub fn set_current_kernel_stack(&self) {
672		let current_task_borrowed = self.current_task.borrow();
673
674		let stack = (current_task_borrowed.stacks.get_kernel_stack()
675			+ current_task_borrowed.stacks.get_kernel_stack_size() as u64
676			- TaskStacks::MARKER_SIZE as u64)
677			.as_u64();
678		CoreLocal::get().kernel_stack.set(stack);
679	}
680
681	/// Save the FPU context for the current FPU owner and restore it for the current task,
682	/// which wants to use the FPU now.
683	#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
684	pub fn fpu_switch(&mut self) {
685		if !Rc::ptr_eq(&self.current_task, &self.fpu_owner) {
686			debug!(
687				"Switching FPU owner from task {} to {}",
688				self.fpu_owner.borrow().id,
689				self.current_task.borrow().id
690			);
691
692			self.fpu_owner.borrow_mut().last_fpu_state.save();
693			self.current_task.borrow().last_fpu_state.restore();
694			self.fpu_owner = self.current_task.clone();
695		}
696	}
697
698	/// Check if a finished task could be deleted.
699	fn cleanup_tasks(&mut self) {
700		// Pop the first finished task and remove it from the TASKS list, which implicitly deallocates all associated memory.
701		while let Some(finished_task) = self.finished_tasks.pop_front() {
702			debug!("Cleaning up task {}", finished_task.borrow().id);
703		}
704	}
705
706	#[cfg(feature = "smp")]
707	pub fn check_input(&mut self) {
708		let mut input_locked = CoreLocal::get().scheduler_input.lock();
709
710		while let Some(task) = input_locked.wakeup_tasks.pop_front() {
711			let task = self.blocked_tasks.custom_wakeup(task);
712			self.ready_queue.push(task);
713		}
714
715		while let Some(new_task) = input_locked.new_tasks.pop_front() {
716			let task = Rc::new(RefCell::new(Task::from(new_task)));
717			self.ready_queue.push(task.clone());
718		}
719	}
720
721	/// Only the idle task should call this function.
722	/// Set the idle task to halt state if not another
723	/// available.
724	pub fn run() -> ! {
725		let backoff = Backoff::new();
726
727		loop {
728			let core_scheduler = core_scheduler();
729			interrupts::disable();
730
731			// run async tasks
732			crate::executor::run();
733
734			// do housekeeping
735			#[cfg(feature = "smp")]
736			core_scheduler.check_input();
737			core_scheduler.cleanup_tasks();
738
739			if core_scheduler.ready_queue.is_empty() {
740				if backoff.is_completed() {
741					interrupts::enable_and_wait();
742					backoff.reset();
743				} else {
744					interrupts::enable();
745					backoff.snooze();
746				}
747			} else {
748				interrupts::enable();
749				core_scheduler.reschedule();
750				backoff.reset();
751			}
752		}
753	}
754
755	#[inline]
756	#[cfg(target_arch = "aarch64")]
757	pub fn get_last_stack_pointer(&self) -> memory_addresses::VirtAddr {
758		self.current_task.borrow().last_stack_pointer
759	}
760
761	/// Triggers the scheduler to reschedule the tasks.
762	/// Interrupt flag must be cleared before calling this function.
763	pub fn scheduler(&mut self) -> Option<*mut usize> {
764		// run background tasks
765		crate::executor::run();
766
767		// Someone wants to give up the CPU
768		// => we have time to cleanup the system
769		self.cleanup_tasks();
770
771		// Get information about the current task.
772		let (id, last_stack_pointer, prio, status) = {
773			let mut borrowed = self.current_task.borrow_mut();
774			(
775				borrowed.id,
776				ptr::from_mut(&mut borrowed.last_stack_pointer).cast::<usize>(),
777				borrowed.prio,
778				borrowed.status,
779			)
780		};
781
782		let mut new_task = None;
783
784		if status == TaskStatus::Running {
785			// A task is currently running.
786			// Check if a task with a equal or higher priority is available.
787			if let Some(task) = self.ready_queue.pop_with_prio(prio) {
788				new_task = Some(task);
789			}
790		} else {
791			if status == TaskStatus::Finished {
792				// Mark the finished task as invalid and add it to the finished tasks for a later cleanup.
793				self.current_task.borrow_mut().status = TaskStatus::Invalid;
794				self.finished_tasks.push_back(self.current_task.clone());
795			}
796
797			// No task is currently running.
798			// Check if there is any available task and get the one with the highest priority.
799			if let Some(task) = self.ready_queue.pop() {
800				// This available task becomes the new task.
801				debug!("Task is available.");
802				new_task = Some(task);
803			} else if status != TaskStatus::Idle {
804				// The Idle task becomes the new task.
805				debug!("Only Idle Task is available.");
806				new_task = Some(self.idle_task.clone());
807			}
808		}
809
810		if let Some(task) = new_task {
811			// There is a new task we want to switch to.
812
813			// Handle the current task.
814			if status == TaskStatus::Running {
815				// Mark the running task as ready again and add it back to the queue.
816				self.current_task.borrow_mut().status = TaskStatus::Ready;
817				self.ready_queue.push(self.current_task.clone());
818			}
819
820			// Handle the new task and get information about it.
821			let (new_id, new_stack_pointer) = {
822				let mut borrowed = task.borrow_mut();
823				if borrowed.status != TaskStatus::Idle {
824					// Mark the new task as running.
825					borrowed.status = TaskStatus::Running;
826				}
827
828				(borrowed.id, borrowed.last_stack_pointer)
829			};
830
831			if id != new_id {
832				// Tell the scheduler about the new task.
833				debug!(
834					"Switching task from {} to {} (stack {:#X} => {:p})",
835					id,
836					new_id,
837					unsafe { *last_stack_pointer },
838					new_stack_pointer
839				);
840				#[cfg(not(target_arch = "riscv64"))]
841				{
842					self.current_task = task;
843				}
844
845				// Finally return the context of the new task.
846				#[cfg(not(target_arch = "riscv64"))]
847				return Some(last_stack_pointer);
848
849				#[cfg(target_arch = "riscv64")]
850				{
851					if sstatus::read().fs() == sstatus::FS::Dirty {
852						self.current_task.borrow_mut().last_fpu_state.save();
853					}
854					task.borrow().last_fpu_state.restore();
855					self.current_task = task;
856					unsafe {
857						switch_to_task(last_stack_pointer, new_stack_pointer.as_usize());
858					}
859				}
860			}
861		}
862
863		None
864	}
865}
866
867fn get_tid() -> TaskId {
868	static TID_COUNTER: AtomicI32 = AtomicI32::new(0);
869	let guard = TASKS.lock();
870
871	loop {
872		let id = TaskId::from(TID_COUNTER.fetch_add(1, Ordering::SeqCst));
873		if !guard.contains_key(&id) {
874			return id;
875		}
876	}
877}
878
879#[inline]
880pub(crate) fn abort() -> ! {
881	core_scheduler().exit(-1)
882}
883
884/// Add a per-core scheduler for the current core.
885pub(crate) fn add_current_core() {
886	// Create an idle task for this core.
887	let core_id = core_id();
888	let tid = get_tid();
889	let idle_task = Rc::new(RefCell::new(Task::new_idle(tid, core_id)));
890
891	// Add the ID -> Task mapping.
892	WAITING_TASKS.lock().insert(tid, VecDeque::with_capacity(1));
893	TASKS.lock().insert(
894		tid,
895		TaskHandle::new(
896			tid,
897			IDLE_PRIO,
898			#[cfg(feature = "smp")]
899			core_id,
900		),
901	);
902	// Initialize a scheduler for this core.
903	debug!("Initializing scheduler for core {core_id} with idle task {tid}");
904	let boxed_scheduler = Box::new(PerCoreScheduler {
905		#[cfg(feature = "smp")]
906		core_id,
907		current_task: idle_task.clone(),
908		#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
909		fpu_owner: idle_task.clone(),
910		idle_task,
911		ready_queue: PriorityTaskQueue::new(),
912		finished_tasks: VecDeque::new(),
913		blocked_tasks: BlockedTaskQueue::new(),
914	});
915
916	let scheduler = Box::into_raw(boxed_scheduler);
917	set_core_scheduler(scheduler);
918	#[cfg(feature = "smp")]
919	{
920		SCHEDULER_INPUTS.lock().insert(
921			core_id.try_into().unwrap(),
922			&CoreLocal::get().scheduler_input,
923		);
924		#[cfg(target_arch = "x86_64")]
925		CORE_HLT_STATE
926			.lock()
927			.insert(core_id.try_into().unwrap(), &CoreLocal::get().hlt);
928	}
929}
930
931#[inline]
932#[cfg(all(target_arch = "x86_64", feature = "smp", not(feature = "idle-poll")))]
933pub(crate) fn take_core_hlt_state(core_id: CoreId) -> bool {
934	CORE_HLT_STATE.lock()[usize::try_from(core_id).unwrap()].swap(false, Ordering::Acquire)
935}
936
937#[inline]
938#[cfg(feature = "smp")]
939fn get_scheduler_input(core_id: CoreId) -> &'static InterruptTicketMutex<SchedulerInput> {
940	SCHEDULER_INPUTS.lock()[usize::try_from(core_id).unwrap()]
941}
942
943pub unsafe fn spawn(
944	func: unsafe extern "C" fn(usize),
945	arg: usize,
946	prio: Priority,
947	stack_size: usize,
948	selector: isize,
949) -> TaskId {
950	static CORE_COUNTER: AtomicU32 = AtomicU32::new(1);
951
952	let core_id = if selector < 0 {
953		// use Round Robin to schedule the cores
954		CORE_COUNTER.fetch_add(1, Ordering::SeqCst) % get_processor_count()
955	} else {
956		selector as u32
957	};
958
959	unsafe { PerCoreScheduler::spawn(func, arg, prio, core_id, stack_size) }
960}
961
962#[allow(clippy::result_unit_err)]
963pub fn join(id: TaskId) -> Result<(), ()> {
964	let core_scheduler = core_scheduler();
965
966	debug!(
967		"Task {} is waiting for task {}",
968		core_scheduler.get_current_task_id(),
969		id
970	);
971
972	loop {
973		let mut waiting_tasks_guard = WAITING_TASKS.lock();
974
975		if let Some(queue) = waiting_tasks_guard.get_mut(&id) {
976			queue.push_back(core_scheduler.get_current_task_handle());
977			core_scheduler.block_current_task(None);
978
979			// Switch to the next task.
980			drop(waiting_tasks_guard);
981			core_scheduler.reschedule();
982		} else {
983			return Ok(());
984		}
985	}
986}
987
988pub fn shutdown(arg: i32) -> ! {
989	crate::syscalls::shutdown(arg)
990}
991
992fn get_task_handle(id: TaskId) -> Option<TaskHandle> {
993	TASKS.lock().get(&id).copied()
994}
995
996#[cfg(all(target_arch = "x86_64", feature = "common-os"))]
997pub(crate) static BOOT_ROOT_PAGE_TABLE: OnceCell<usize> = OnceCell::new();
998
999#[cfg(all(target_arch = "x86_64", feature = "common-os"))]
1000pub(crate) fn get_root_page_table() -> usize {
1001	let current_task_borrowed = core_scheduler().current_task.borrow_mut();
1002	current_task_borrowed.root_page_table
1003}