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 let queue = self.queues[queue_index].as_mut()?;
200
201 let task = queue.pop_front();
202
203 if queue.is_empty() {
204 *self.prio_bitmap &= !(1 << queue_index as u64);
205 }
206
207 task
208 }
209
210 pub fn pop(&mut self) -> Option<TaskHandle> {
212 let i = msb(self.prio_bitmap.into_inner())?;
213
214 self.pop_from_queue(i as usize)
215 }
216
217 pub fn remove(&mut self, task: TaskHandle) -> bool {
220 let queue_index = task.priority.into() as usize;
221 let mut success = false;
224 if let Some(queue) = &mut self.queues[queue_index] {
225 let mut i = 0;
226 while i != queue.len() {
227 if queue[i].id == task.id {
228 queue.remove(i);
229 success = true;
230 } else {
231 i += 1;
232 }
233 }
234
235 if queue.is_empty() {
236 *self.prio_bitmap &= !(1 << queue_index as u64);
237 }
238 }
239
240 success
241 }
242}
243
244pub(crate) struct PriorityTaskQueue {
246 queues: [LinkedList<Rc<RefCell<Task>>>; NO_PRIORITIES],
247 prio_bitmap: u64,
248}
249
250impl PriorityTaskQueue {
251 pub const fn new() -> PriorityTaskQueue {
253 const EMPTY_LIST: LinkedList<Rc<RefCell<Task>>> = LinkedList::new();
254 PriorityTaskQueue {
255 queues: [EMPTY_LIST; NO_PRIORITIES],
256 prio_bitmap: 0,
257 }
258 }
259
260 pub fn push(&mut self, task: Rc<RefCell<Task>>) {
262 let i = task.borrow().prio.into() as usize;
263 self.prio_bitmap |= (1 << i) as u64;
266 let queue = &mut self.queues[i];
267 queue.push_back(task);
268 }
269
270 fn pop_from_queue(&mut self, queue_index: usize) -> Option<Rc<RefCell<Task>>> {
271 let task = self.queues[queue_index].pop_front();
272 if self.queues[queue_index].is_empty() {
273 self.prio_bitmap &= !(1 << queue_index as u64);
274 }
275
276 task
277 }
278
279 fn remove_from_queue(
282 &mut self,
283 task_index: usize,
284 queue_index: usize,
285 ) -> Option<Rc<RefCell<Task>>> {
286 let queue = &mut self.queues[queue_index];
289 if queue.len() < task_index {
290 return None;
291 }
292
293 let mut split_list = queue.split_off(task_index);
295 let element = split_list.pop_front();
296 queue.append(&mut split_list);
297 if queue.is_empty() {
298 self.prio_bitmap &= !(1 << queue_index as u64);
299 }
300 element
301 }
302
303 pub fn is_empty(&self) -> bool {
305 self.prio_bitmap == 0
306 }
307
308 #[allow(dead_code)]
310 #[inline]
311 pub fn get_priority_bitmap(&self) -> &u64 {
312 &self.prio_bitmap
313 }
314
315 pub fn pop(&mut self) -> Option<Rc<RefCell<Task>>> {
317 let i = msb(self.prio_bitmap)?;
318
319 self.pop_from_queue(i as usize)
320 }
321
322 pub fn pop_with_prio(&mut self, prio: Priority) -> Option<Rc<RefCell<Task>>> {
324 let i = msb(self.prio_bitmap)?;
325
326 if i < u32::from(prio.into()) {
327 return None;
328 }
329
330 self.pop_from_queue(i as usize)
331 }
332
333 #[cfg(all(any(target_arch = "x86_64", target_arch = "riscv64"), feature = "smp"))]
335 pub fn get_highest_priority(&self) -> Priority {
336 let Some(i) = msb(self.prio_bitmap) else {
337 return IDLE_PRIO;
338 };
339
340 Priority::from(i.try_into().unwrap())
341 }
342
343 pub fn set_priority(&mut self, handle: TaskHandle, prio: Priority) -> Result<(), ()> {
345 let old_priority = handle.get_priority().into() as usize;
346 let index = self.queues[old_priority]
347 .iter()
348 .position(|current_task| current_task.borrow().id == handle.id)
349 .ok_or(())?;
350
351 let task = self.remove_from_queue(index, old_priority).ok_or(())?;
352 task.borrow_mut().prio = prio;
353 self.push(task);
354 Ok(())
355 }
356}
357
358#[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))]
360#[cfg_attr(
361 not(any(target_arch = "x86_64", target_arch = "aarch64")),
362 repr(align(64))
363)]
364pub(crate) struct Task {
365 pub id: TaskId,
367 pub status: TaskStatus,
369 pub prio: Priority,
371 pub last_stack_pointer: VirtAddr,
373 pub user_stack_pointer: VirtAddr,
375 pub last_fpu_state: arch::processor::FPUState,
377 pub core_id: CoreId,
379 pub stacks: TaskStacks,
381 pub object_map:
383 Arc<RwSpinLock<HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>>>,
384 #[cfg(not(feature = "common-os"))]
386 pub tls: Option<Tls>,
387 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
389 pub root_page_table: usize,
390}
391
392pub(crate) trait TaskFrame {
393 fn create_stack_frame(&mut self, func: unsafe extern "C" fn(usize), arg: usize);
395}
396
397impl Task {
398 pub fn new(
399 tid: TaskId,
400 core_id: CoreId,
401 task_status: TaskStatus,
402 task_prio: Priority,
403 stacks: TaskStacks,
404 object_map: Arc<
405 RwSpinLock<HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>>,
406 >,
407 ) -> Task {
408 debug!("Creating new task {tid} on core {core_id}");
409
410 Task {
411 id: tid,
412 status: task_status,
413 prio: task_prio,
414 last_stack_pointer: VirtAddr::zero(),
415 user_stack_pointer: VirtAddr::zero(),
416 last_fpu_state: arch::processor::FPUState::new(),
417 core_id,
418 stacks,
419 object_map,
420 #[cfg(not(feature = "common-os"))]
421 tls: None,
422 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
423 root_page_table: arch::create_new_root_page_table(),
424 }
425 }
426
427 pub fn new_idle(tid: TaskId, core_id: CoreId) -> Task {
428 debug!("Creating idle task {tid}");
429
430 static OBJECT_MAP: OnceCell<
432 Arc<
433 RwSpinLock<
434 HashMap<RawFd, Arc<async_lock::RwLock<dyn ObjectInterface>>, RandomState>,
435 >,
436 >,
437 > = OnceCell::new();
438
439 if core_id == 0 {
440 OBJECT_MAP
441 .set(Arc::new(RwSpinLock::new(HashMap::<
442 RawFd,
443 Arc<async_lock::RwLock<dyn ObjectInterface>>,
444 RandomState,
445 >::with_hasher(
446 RandomState::with_seeds(0, 0, 0, 0),
447 ))))
448 .unwrap_or_else(|_| unreachable!());
451 let objmap = OBJECT_MAP.get().unwrap().clone();
452 let mut guard = objmap.write();
453 if env::is_uhyve() {
454 let stdin = Arc::new(async_lock::RwLock::new(UhyveStdin::new()));
455 let stdout = Arc::new(async_lock::RwLock::new(UhyveStdout::new()));
456 let stderr = Arc::new(async_lock::RwLock::new(UhyveStderr::new()));
457 guard.insert(STDIN_FILENO, stdin);
458 guard.insert(STDOUT_FILENO, stdout);
459 guard.insert(STDERR_FILENO, stderr);
460 } else {
461 let stdin = Arc::new(async_lock::RwLock::new(GenericStdin::new()));
462 let stdout = Arc::new(async_lock::RwLock::new(GenericStdout::new()));
463 let stderr = Arc::new(async_lock::RwLock::new(GenericStderr::new()));
464 guard.insert(STDIN_FILENO, stdin);
465 guard.insert(STDOUT_FILENO, stdout);
466 guard.insert(STDERR_FILENO, stderr);
467 }
468 }
469
470 #[cfg(not(feature = "common-os"))]
471 let tls = if cfg!(feature = "instrument-mcount") {
472 Tls::from_env().inspect(Tls::set_thread_ptr)
473 } else {
474 None
475 };
476
477 Task {
478 id: tid,
479 status: TaskStatus::Idle,
480 prio: IDLE_PRIO,
481 last_stack_pointer: VirtAddr::zero(),
482 user_stack_pointer: VirtAddr::zero(),
483 last_fpu_state: arch::processor::FPUState::new(),
484 core_id,
485 stacks: TaskStacks::from_boot_stacks(),
486 object_map: OBJECT_MAP.get().unwrap().clone(),
487 #[cfg(not(feature = "common-os"))]
488 tls,
489 #[cfg(all(target_arch = "x86_64", feature = "common-os"))]
490 root_page_table: *crate::scheduler::BOOT_ROOT_PAGE_TABLE.get().unwrap(),
491 }
492 }
493}
494
495struct BlockedTask {
502 task: Rc<RefCell<Task>>,
503 wakeup_time: Option<u64>,
504}
505
506impl BlockedTask {
507 pub fn new(task: Rc<RefCell<Task>>, wakeup_time: Option<u64>) -> Self {
508 Self { task, wakeup_time }
509 }
510}
511
512pub(crate) struct BlockedTaskQueue {
513 list: LinkedList<BlockedTask>,
514 #[cfg(feature = "net")]
515 network_wakeup_time: Option<u64>,
516}
517
518impl BlockedTaskQueue {
519 pub const fn new() -> Self {
520 Self {
521 list: LinkedList::new(),
522 #[cfg(feature = "net")]
523 network_wakeup_time: None,
524 }
525 }
526
527 fn mark_ready(task: &RefCell<Task>) {
528 let mut borrowed = task.borrow_mut();
529 debug!(
530 "Waking up task {} on core {}",
531 borrowed.id, borrowed.core_id
532 );
533
534 assert!(
535 borrowed.core_id == core_id(),
536 "Try to wake up task {} on the wrong core {} != {}",
537 borrowed.id,
538 borrowed.core_id,
539 core_id()
540 );
541
542 assert!(
543 borrowed.status == TaskStatus::Blocked,
544 "Trying to wake up task {} which is not blocked",
545 borrowed.id
546 );
547 borrowed.status = TaskStatus::Ready;
548 }
549
550 #[cfg(feature = "net")]
551 pub fn add_network_timer(&mut self, wakeup_time: Option<u64>) {
552 self.network_wakeup_time = wakeup_time;
553
554 let next = self.list.front().and_then(|t| t.wakeup_time);
555
556 let time = match (wakeup_time, next) {
557 (Some(a), Some(b)) => Some(a.min(b)),
558 (a, b) => a.or(b),
559 };
560
561 arch::set_oneshot_timer(time);
562 }
563
564 pub fn add(&mut self, task: Rc<RefCell<Task>>, wakeup_time: Option<u64>) {
566 {
567 let mut borrowed = task.borrow_mut();
569 debug!("Blocking task {}", borrowed.id);
570
571 assert_eq!(
572 borrowed.status,
573 TaskStatus::Running,
574 "Trying to block task {} which is not running",
575 borrowed.id
576 );
577 borrowed.status = TaskStatus::Blocked;
578 }
579
580 let new_node = BlockedTask::new(task, wakeup_time);
581
582 if let Some(wt) = wakeup_time {
584 let mut cursor = self.list.cursor_front_mut();
585 let set_oneshot_timer = || {
586 #[cfg(not(feature = "net"))]
587 arch::set_oneshot_timer(wakeup_time);
588 #[cfg(feature = "net")]
589 match self.network_wakeup_time {
590 Some(time) => {
591 if time > wt {
592 arch::set_oneshot_timer(wakeup_time);
593 } else {
594 arch::set_oneshot_timer(self.network_wakeup_time);
595 }
596 }
597 _ => arch::set_oneshot_timer(wakeup_time),
598 }
599 };
600
601 while let Some(node) = cursor.current() {
602 let node_wakeup_time = node.wakeup_time;
603 if node_wakeup_time.is_none() || wt < node_wakeup_time.unwrap() {
604 cursor.insert_before(new_node);
605
606 set_oneshot_timer();
607 return;
608 }
609
610 cursor.move_next();
611 }
612
613 set_oneshot_timer();
614 }
615
616 self.list.push_back(new_node);
617 }
618
619 pub fn custom_wakeup(&mut self, task: TaskHandle) -> Rc<RefCell<Task>> {
621 let mut first_task = true;
622 let mut cursor = self.list.cursor_front_mut();
623
624 #[cfg(feature = "net")]
625 if let Some(wakeup_time) = self.network_wakeup_time
626 && wakeup_time <= arch::processor::get_timer_ticks()
627 {
628 self.network_wakeup_time = None;
629 }
630
631 while let Some(node) = cursor.current() {
633 if node.task.borrow().id == task.get_id() {
634 let task_ref = node.task.clone();
636 cursor.remove_current();
637
638 #[cfg(feature = "net")]
641 if first_task {
642 arch::set_oneshot_timer(cursor.current().map_or_else(
643 || self.network_wakeup_time,
644 |node| match node.wakeup_time {
645 Some(wt) => {
646 if let Some(timer) = self.network_wakeup_time {
647 if wt < timer { Some(wt) } else { Some(timer) }
648 } else {
649 Some(wt)
650 }
651 }
652 None => self.network_wakeup_time,
653 },
654 ));
655 }
656 #[cfg(not(feature = "net"))]
657 if first_task {
658 arch::set_oneshot_timer(
659 cursor
660 .current()
661 .map_or_else(|| None, |node| node.wakeup_time),
662 );
663 }
664
665 Self::mark_ready(&task_ref);
667
668 return task_ref;
669 }
670
671 first_task = false;
672 cursor.move_next();
673 }
674
675 unreachable!();
676 }
677
678 pub fn handle_waiting_tasks(&mut self, ready_queue: &mut PriorityTaskQueue) {
683 let time = arch::processor::get_timer_ticks();
685
686 #[cfg(feature = "net")]
687 if let Some(mut guard) = crate::executor::network::NIC.try_lock()
688 && let crate::executor::network::NetworkState::Initialized(nic) = &mut *guard
689 {
690 let now = crate::executor::network::now();
691 nic.poll_common(now);
692 self.network_wakeup_time = nic.poll_delay(now).map(|d| d.total_micros() + time);
693 }
694
695 let newly_ready_tasks = self.list.extract_if(|blocked_task| {
699 blocked_task
700 .wakeup_time
701 .is_some_and(|wakeup_time| wakeup_time < time)
702 });
703
704 for task in newly_ready_tasks {
705 Self::mark_ready(&task.task);
706 ready_queue.push(task.task);
707 }
708
709 let new_task_wakeup_time = self.list.front().and_then(|task| task.wakeup_time);
710 cfg_if::cfg_if! {
711 if #[cfg(feature = "net")] {
712 let network_wakeup_time = self.network_wakeup_time;
713 } else {
714 let network_wakeup_time = None;
715 }
716 };
717 let timer_wakeup_time = match (new_task_wakeup_time, network_wakeup_time) {
718 (None, None) => None,
719 (None, Some(network_wt)) => Some(network_wt),
720 (Some(task_wt), None) => Some(task_wt),
721 (Some(task_wt), Some(network_wt)) => Some(u64::min(task_wt, network_wt)),
722 };
723
724 arch::set_oneshot_timer(timer_wakeup_time);
725 }
726}