1#![allow(clippy::type_complexity)]
2
3#[cfg(not(feature = "common-os"))]
4pub(crate) mod tls;
5
6use alloc::collections::{LinkedList, VecDeque};
7use alloc::rc::Rc;
8use alloc::sync::Arc;
9use core::cell::RefCell;
10use core::num::NonZeroU64;
11use core::{cmp, fmt};
12
13use ahash::RandomState;
14use crossbeam_utils::CachePadded;
15use hashbrown::HashMap;
16use hermit_sync::{OnceCell, RwSpinLock};
17use memory_addresses::VirtAddr;
18
19#[cfg(not(feature = "common-os"))]
20use self::tls::Tls;
21use crate::arch::core_local::*;
22use crate::arch::scheduler::TaskStacks;
23use crate::fd::stdio::*;
24use crate::fd::{ObjectInterface, RawFd, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
25use crate::scheduler::CoreId;
26use crate::{arch, env};
27
28#[inline]
38fn msb(n: u64) -> Option<u32> {
39 NonZeroU64::new(n).map(|n| u64::BITS - 1 - n.leading_zeros())
40}
41
42#[derive(Copy, Clone, Debug, Eq, PartialEq)]
44pub(crate) enum TaskStatus {
45 Invalid,
46 Ready,
47 Running,
48 Blocked,
49 Finished,
50 Idle,
51}
52
53#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
55pub struct TaskId(i32);
56
57impl TaskId {
58 pub const fn into(self) -> i32 {
59 self.0
60 }
61
62 pub const fn from(x: i32) -> Self {
63 TaskId(x)
64 }
65}
66
67impl fmt::Display for TaskId {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(f, "{}", self.0)
70 }
71}
72
73#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
75pub struct Priority(u8);
76
77impl Priority {
78 pub const fn into(self) -> u8 {
79 self.0
80 }
81
82 pub const fn from(x: u8) -> Self {
83 Priority(x)
84 }
85}
86
87impl fmt::Display for Priority {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 write!(f, "{}", self.0)
90 }
91}
92
93#[allow(dead_code)]
94pub const HIGH_PRIO: Priority = Priority::from(3);
95pub const NORMAL_PRIO: Priority = Priority::from(2);
96#[allow(dead_code)]
97pub const LOW_PRIO: Priority = Priority::from(1);
98pub const IDLE_PRIO: Priority = Priority::from(0);
99
100pub const NO_PRIORITIES: usize = 31;
102
103#[derive(Copy, Clone, Debug)]
104pub(crate) struct TaskHandle {
105 id: TaskId,
106 priority: Priority,
107 #[cfg(feature = "smp")]
108 core_id: CoreId,
109}
110
111impl TaskHandle {
112 pub fn new(id: TaskId, priority: Priority, #[cfg(feature = "smp")] core_id: CoreId) -> Self {
113 Self {
114 id,
115 priority,
116 #[cfg(feature = "smp")]
117 core_id,
118 }
119 }
120
121 #[cfg(feature = "smp")]
122 pub fn get_core_id(&self) -> CoreId {
123 self.core_id
124 }
125
126 pub fn get_id(&self) -> TaskId {
127 self.id
128 }
129
130 pub fn get_priority(&self) -> Priority {
131 self.priority
132 }
133}
134
135impl Ord for TaskHandle {
136 fn cmp(&self, other: &Self) -> cmp::Ordering {
137 self.id.cmp(&other.id)
138 }
139}
140
141impl PartialOrd for TaskHandle {
142 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
143 Some(self.cmp(other))
144 }
145}
146
147impl PartialEq for TaskHandle {
148 fn eq(&self, other: &Self) -> bool {
149 self.id == other.id
150 }
151}
152
153impl Eq for TaskHandle {}
154
155#[derive(Default)]
157pub(crate) struct TaskHandlePriorityQueue {
158 queues: [Option<VecDeque<TaskHandle>>; NO_PRIORITIES],
159 prio_bitmap: CachePadded<u64>,
160}
161
162impl TaskHandlePriorityQueue {
163 pub const fn new() -> Self {
165 Self {
166 queues: [const { None }; NO_PRIORITIES],
167 prio_bitmap: CachePadded::new(0),
168 }
169 }
170
171 pub fn is_empty(&self) -> bool {
173 self.prio_bitmap.into_inner() == 0
174 }
175
176 pub fn contains(&self, task: TaskHandle) -> bool {
179 matches!(self.queues[task.priority.into() as usize]
180 .as_ref(), Some(queue) if queue.iter().any(|queued| queued.id == task.id))
181 }
182
183 pub fn push(&mut self, task: TaskHandle) {
185 let i = task.priority.into() as usize;
186 *self.prio_bitmap |= (1 << i) as u64;
189 if let Some(queue) = &mut self.queues[i] {
190 queue.push_back(task);
191 } else {
192 let mut queue = VecDeque::new();
193 queue.push_back(task);
194 self.queues[i] = Some(queue);
195 }
196 }
197
198 fn pop_from_queue(&mut self, queue_index: usize) -> Option<TaskHandle> {
199 if let Some(queue) = &mut self.queues[queue_index] {
200 let task = queue.pop_front();
201
202 if queue.is_empty() {
203 *self.prio_bitmap &= !(1 << queue_index as u64);
204 }
205
206 task
207 } else {
208 None
209 }
210 }
211
212 pub fn pop(&mut self) -> Option<TaskHandle> {
214 if let Some(i) = msb(self.prio_bitmap.into_inner()) {
215 return self.pop_from_queue(i as usize);
216 }
217
218 None
219 }
220
221 pub fn remove(&mut self, task: TaskHandle) -> bool {
224 let queue_index = task.priority.into() as usize;
225 let mut success = false;
228 if let Some(queue) = &mut self.queues[queue_index] {
229 let mut i = 0;
230 while i != queue.len() {
231 if queue[i].id == task.id {
232 queue.remove(i);
233 success = true;
234 } else {
235 i += 1;
236 }
237 }
238
239 if queue.is_empty() {
240 *self.prio_bitmap &= !(1 << queue_index as u64);
241 }
242 }
243
244 success
245 }
246}
247
248pub(crate) struct PriorityTaskQueue {
250 queues: [LinkedList<Rc<RefCell<Task>>>; NO_PRIORITIES],
251 prio_bitmap: u64,
252}
253
254impl PriorityTaskQueue {
255 pub const fn new() -> PriorityTaskQueue {
257 const EMPTY_LIST: LinkedList<Rc<RefCell<Task>>> = LinkedList::new();
258 PriorityTaskQueue {
259 queues: [EMPTY_LIST; NO_PRIORITIES],
260 prio_bitmap: 0,
261 }
262 }
263
264 pub fn push(&mut self, task: Rc<RefCell<Task>>) {
266 let i = task.borrow().prio.into() as usize;
267 self.prio_bitmap |= (1 << i) as u64;
270 let queue = &mut self.queues[i];
271 queue.push_back(task);
272 }
273
274 fn pop_from_queue(&mut self, queue_index: usize) -> Option<Rc<RefCell<Task>>> {
275 let task = self.queues[queue_index].pop_front();
276 if self.queues[queue_index].is_empty() {
277 self.prio_bitmap &= !(1 << queue_index as u64);
278 }
279
280 task
281 }
282
283 fn remove_from_queue(
286 &mut self,
287 task_index: usize,
288 queue_index: usize,
289 ) -> Option<Rc<RefCell<Task>>> {
290 let queue = &mut self.queues[queue_index];
293 if task_index <= queue.len() {
294 let mut split_list = queue.split_off(task_index);
296 let element = split_list.pop_front();
297 queue.append(&mut split_list);
298 if queue.is_empty() {
299 self.prio_bitmap &= !(1 << queue_index as u64);
300 }
301 element
302 } else {
303 None
304 }
305 }
306
307 pub fn is_empty(&self) -> bool {
309 self.prio_bitmap == 0
310 }
311
312 #[allow(dead_code)]
314 #[inline]
315 pub fn get_priority_bitmap(&self) -> &u64 {
316 &self.prio_bitmap
317 }
318
319 pub fn pop(&mut self) -> Option<Rc<RefCell<Task>>> {
321 if let Some(i) = msb(self.prio_bitmap) {
322 return self.pop_from_queue(i as usize);
323 }
324
325 None
326 }
327
328 pub fn pop_with_prio(&mut self, prio: Priority) -> Option<Rc<RefCell<Task>>> {
330 if let Some(i) = msb(self.prio_bitmap)
331 && i >= u32::from(prio.into())
332 {
333 return self.pop_from_queue(i as usize);
334 }
335
336 None
337 }
338
339 #[cfg(all(any(target_arch = "x86_64", target_arch = "riscv64"), feature = "smp"))]
341 pub fn get_highest_priority(&self) -> Priority {
342 if let Some(i) = msb(self.prio_bitmap) {
343 Priority::from(i.try_into().unwrap())
344 } else {
345 IDLE_PRIO
346 }
347 }
348
349 pub fn set_priority(&mut self, handle: TaskHandle, prio: Priority) -> Result<(), ()> {
351 let old_priority = handle.get_priority().into() as usize;
352 if let Some(index) = self.queues[old_priority]
353 .iter()
354 .position(|current_task| current_task.borrow().id == handle.id)
355 {
356 let Some(task) = self.remove_from_queue(index, old_priority) else {
357 return Err(());
358 };
359 task.borrow_mut().prio = prio;
360 self.push(task);
361 return Ok(());
362 }
363
364 Err(())
365 }
366}
367
368#[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))]
370#[cfg_attr(
371 not(any(target_arch = "x86_64", target_arch = "aarch64")),
372 repr(align(64))
373)]
374pub(crate) struct Task {
375 pub id: TaskId,
377 pub status: TaskStatus,
379 pub prio: Priority,
381 pub last_stack_pointer: VirtAddr,
383 pub user_stack_pointer: VirtAddr,
385 pub last_fpu_state: arch::processor::FPUState,
387 pub core_id: CoreId,
389 pub stacks: TaskStacks,
391 pub object_map:
393 Arc<RwSpinLock<HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>>>,
394 #[cfg(not(feature = "common-os"))]
396 pub tls: Option<Tls>,
397 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
399 pub root_page_table: usize,
400}
401
402pub(crate) trait TaskFrame {
403 fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize);
405}
406
407impl Task {
408 pub fn new(
409 tid: TaskId,
410 core_id: CoreId,
411 task_status: TaskStatus,
412 task_prio: Priority,
413 stacks: TaskStacks,
414 object_map: Arc<
415 RwSpinLock<HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>>,
416 >,
417 ) -> Task {
418 debug!("Creating new task {tid} on core {core_id}");
419
420 Task {
421 id: tid,
422 status: task_status,
423 prio: task_prio,
424 last_stack_pointer: VirtAddr::zero(),
425 user_stack_pointer: VirtAddr::zero(),
426 last_fpu_state: arch::processor::FPUState::new(),
427 core_id,
428 stacks,
429 object_map,
430 #[cfg(not(feature = "common-os"))]
431 tls: None,
432 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
433 root_page_table: arch::create_new_root_page_table(),
434 }
435 }
436
437 pub fn new_idle(tid: TaskId, core_id: CoreId) -> Task {
438 debug!("Creating idle task {tid}");
439
440 static OBJECT_MAP: OnceCell<
442 Arc<
443 RwSpinLock<
444 HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>,
445 >,
446 >,
447 > = OnceCell::new();
448
449 if core_id == 0 {
450 OBJECT_MAP
451 .set(Arc::new(RwSpinLock::new(HashMap::<
452 RawFd,
453 Arc<async_lock::RwLock<dyn ObjectInterface>>,
454 RandomState,
455 >::with_hasher(
456 RandomState::with_seeds(0, 0, 0, 0),
457 ))))
458 .unwrap_or_else(|_| unreachable!());
461 let objmap = OBJECT_MAP.get().unwrap().clone();
462 let mut guard = objmap.write();
463 if env::is_uhyve() {
464 let stdin = Arc::new(async_lock::RwLock::new(UhyveStdin::new()));
465 let stdout = Arc::new(async_lock::RwLock::new(UhyveStdout::new()));
466 let stderr = Arc::new(async_lock::RwLock::new(UhyveStderr::new()));
467 guard.insert(STDIN_FILENO, stdin);
468 guard.insert(STDOUT_FILENO, stdout);
469 guard.insert(STDERR_FILENO, stderr);
470 } else {
471 let stdin = Arc::new(async_lock::RwLock::new(GenericStdin::new()));
472 let stdout = Arc::new(async_lock::RwLock::new(GenericStdout::new()));
473 let stderr = Arc::new(async_lock::RwLock::new(GenericStderr::new()));
474 guard.insert(STDIN_FILENO, stdin);
475 guard.insert(STDOUT_FILENO, stdout);
476 guard.insert(STDERR_FILENO, stderr);
477 }
478 }
479
480 Task {
481 id: tid,
482 status: TaskStatus::Idle,
483 prio: IDLE_PRIO,
484 last_stack_pointer: VirtAddr::zero(),
485 user_stack_pointer: VirtAddr::zero(),
486 last_fpu_state: arch::processor::FPUState::new(),
487 core_id,
488 stacks: TaskStacks::from_boot_stacks(),
489 object_map: OBJECT_MAP.get().unwrap().clone(),
490 #[cfg(not(feature = "common-os"))]
491 tls: None,
492 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
493 root_page_table: *crate::scheduler::BOOT_ROOT_PAGE_TABLE.get().unwrap(),
494 }
495 }
496}
497
498struct BlockedTask {
505 task: Rc<RefCell<Task>>,
506 wakeup_time: Option<u64>,
507}
508
509impl BlockedTask {
510 pub fn new(task: Rc<RefCell<Task>>, wakeup_time: Option<u64>) -> Self {
511 Self { task, wakeup_time }
512 }
513}
514
515pub(crate) struct BlockedTaskQueue {
516 list: LinkedList<BlockedTask>,
517 #[cfg(feature = "net")]
518 network_wakeup_time: Option<u64>,
519}
520
521impl BlockedTaskQueue {
522 pub const fn new() -> Self {
523 Self {
524 list: LinkedList::new(),
525 #[cfg(feature = "net")]
526 network_wakeup_time: None,
527 }
528 }
529
530 fn mark_ready(task: &RefCell<Task>) {
531 let mut borrowed = task.borrow_mut();
532 debug!(
533 "Waking up task {} on core {}",
534 borrowed.id, borrowed.core_id
535 );
536
537 assert!(
538 borrowed.core_id == core_id(),
539 "Try to wake up task {} on the wrong core {} != {}",
540 borrowed.id,
541 borrowed.core_id,
542 core_id()
543 );
544
545 assert!(
546 borrowed.status == TaskStatus::Blocked,
547 "Trying to wake up task {} which is not blocked",
548 borrowed.id
549 );
550 borrowed.status = TaskStatus::Ready;
551 }
552
553 #[cfg(feature = "net")]
554 pub fn add_network_timer(&mut self, wakeup_time: Option<u64>) {
555 self.network_wakeup_time = wakeup_time;
556
557 let next = self.list.front().and_then(|t| t.wakeup_time);
558
559 let time = match (wakeup_time, next) {
560 (Some(a), Some(b)) => Some(a.min(b)),
561 (a, b) => a.or(b),
562 };
563
564 arch::set_oneshot_timer(time);
565 }
566
567 pub fn add(&mut self, task: Rc<RefCell<Task>>, wakeup_time: Option<u64>) {
569 {
570 let mut borrowed = task.borrow_mut();
572 debug!("Blocking task {}", borrowed.id);
573
574 assert_eq!(
575 borrowed.status,
576 TaskStatus::Running,
577 "Trying to block task {} which is not running",
578 borrowed.id
579 );
580 borrowed.status = TaskStatus::Blocked;
581 }
582
583 let new_node = BlockedTask::new(task, wakeup_time);
584
585 if let Some(wt) = wakeup_time {
587 let mut cursor = self.list.cursor_front_mut();
588 let set_oneshot_timer = || {
589 #[cfg(not(feature = "net"))]
590 arch::set_oneshot_timer(wakeup_time);
591 #[cfg(feature = "net")]
592 match self.network_wakeup_time {
593 Some(time) => {
594 if time > wt {
595 arch::set_oneshot_timer(wakeup_time);
596 } else {
597 arch::set_oneshot_timer(self.network_wakeup_time);
598 }
599 }
600 _ => arch::set_oneshot_timer(wakeup_time),
601 }
602 };
603
604 while let Some(node) = cursor.current() {
605 let node_wakeup_time = node.wakeup_time;
606 if node_wakeup_time.is_none() || wt < node_wakeup_time.unwrap() {
607 cursor.insert_before(new_node);
608
609 set_oneshot_timer();
610 return;
611 }
612
613 cursor.move_next();
614 }
615
616 set_oneshot_timer();
617 }
618
619 self.list.push_back(new_node);
620 }
621
622 pub fn custom_wakeup(&mut self, task: TaskHandle) -> Rc<RefCell<Task>> {
624 let mut first_task = true;
625 let mut cursor = self.list.cursor_front_mut();
626
627 #[cfg(feature = "net")]
628 if let Some(wakeup_time) = self.network_wakeup_time
629 && wakeup_time <= arch::processor::get_timer_ticks()
630 {
631 self.network_wakeup_time = None;
632 }
633
634 while let Some(node) = cursor.current() {
636 if node.task.borrow().id == task.get_id() {
637 let task_ref = node.task.clone();
639 cursor.remove_current();
640
641 #[cfg(feature = "net")]
644 if first_task {
645 arch::set_oneshot_timer(cursor.current().map_or_else(
646 || self.network_wakeup_time,
647 |node| match node.wakeup_time {
648 Some(wt) => {
649 if let Some(timer) = self.network_wakeup_time {
650 if wt < timer { Some(wt) } else { Some(timer) }
651 } else {
652 Some(wt)
653 }
654 }
655 None => self.network_wakeup_time,
656 },
657 ));
658 }
659 #[cfg(not(feature = "net"))]
660 if first_task {
661 arch::set_oneshot_timer(
662 cursor
663 .current()
664 .map_or_else(|| None, |node| node.wakeup_time),
665 );
666 }
667
668 Self::mark_ready(&task_ref);
670
671 return task_ref;
672 }
673
674 first_task = false;
675 cursor.move_next();
676 }
677
678 unreachable!();
679 }
680
681 pub fn handle_waiting_tasks(&mut self, ready_queue: &mut PriorityTaskQueue) {
686 let time = arch::processor::get_timer_ticks();
688
689 #[cfg(feature = "net")]
690 if let Some(mut guard) = crate::executor::network::NIC.try_lock()
691 && let crate::executor::network::NetworkState::Initialized(nic) = &mut *guard
692 {
693 let now = crate::executor::network::now();
694 nic.poll_common(now);
695 self.network_wakeup_time = nic.poll_delay(now).map(|d| d.total_micros() + time);
696 }
697
698 let newly_ready_tasks = self.list.extract_if(|blocked_task| {
702 blocked_task
703 .wakeup_time
704 .is_some_and(|wakeup_time| wakeup_time < time)
705 });
706
707 for task in newly_ready_tasks {
708 Self::mark_ready(&task.task);
709 ready_queue.push(task.task);
710 }
711
712 let new_task_wakeup_time = self.list.front().and_then(|task| task.wakeup_time);
713 cfg_if::cfg_if! {
714 if #[cfg(feature = "net")] {
715 let network_wakeup_time = self.network_wakeup_time;
716 } else {
717 let network_wakeup_time = None;
718 }
719 };
720 let timer_wakeup_time = match (new_task_wakeup_time, network_wakeup_time) {
721 (None, None) => None,
722 (None, Some(network_wt)) => Some(network_wt),
723 (Some(task_wt), None) => Some(task_wt),
724 (Some(task_wt), Some(network_wt)) => Some(u64::min(task_wt, network_wt)),
725 };
726
727 arch::set_oneshot_timer(timer_wakeup_time);
728 }
729}