@@ -8,10 +8,14 @@ use core::sync::atomic::{AtomicU32, Ordering};
8
8
9
9
use crossbeam_utils:: Backoff ;
10
10
use hermit_sync:: { without_interrupts, * } ;
11
+ #[ cfg( target_arch = "riscv64" ) ]
12
+ use riscv:: register:: sstatus;
11
13
12
14
use crate :: arch;
13
15
use crate :: arch:: core_local:: * ;
14
16
use crate :: arch:: interrupts;
17
+ #[ cfg( target_arch = "riscv64" ) ]
18
+ use crate :: arch:: switch:: switch_to_task;
15
19
#[ cfg( target_arch = "x86_64" ) ]
16
20
use crate :: arch:: switch:: { switch_to_fpu_owner, switch_to_task} ;
17
21
use crate :: kernel:: scheduler:: TaskStacks ;
@@ -289,7 +293,7 @@ impl PerCoreScheduler {
289
293
290
294
/// Returns `true` if a reschedule is required
291
295
#[ inline]
292
- #[ cfg( all( target_arch = "x86_64" , feature = "smp" ) ) ]
296
+ #[ cfg( all( any ( target_arch = "x86_64" , target_arch = "riscv64" ) , feature = "smp" ) ) ]
293
297
pub fn is_scheduling ( & self ) -> bool {
294
298
self . current_task . borrow ( ) . prio < self . ready_queue . get_highest_priority ( )
295
299
}
@@ -422,6 +426,18 @@ impl PerCoreScheduler {
422
426
} )
423
427
}
424
428
429
+ #[ cfg( target_arch = "riscv64" ) ]
430
+ pub fn set_current_kernel_stack ( & self ) {
431
+ let current_task_borrowed = self . current_task . borrow ( ) ;
432
+
433
+ set_kernel_stack (
434
+ ( current_task_borrowed. stacks . get_kernel_stack ( )
435
+ + current_task_borrowed. stacks . get_kernel_stack_size ( )
436
+ - TaskStacks :: MARKER_SIZE )
437
+ . as_u64 ( ) ,
438
+ ) ;
439
+ }
440
+
425
441
/// Save the FPU context for the current FPU owner and restore it for the current task,
426
442
/// which wants to use the FPU now.
427
443
#[ cfg( target_arch = "x86_64" ) ]
@@ -447,7 +463,7 @@ impl PerCoreScheduler {
447
463
}
448
464
}
449
465
450
- #[ cfg( all( target_arch = "x86_64" , feature = "smp" ) ) ]
466
+ #[ cfg( all( any ( target_arch = "x86_64" , target_arch = "riscv64" ) , feature = "smp" ) ) ]
451
467
pub fn check_input ( & mut self ) {
452
468
let mut input_locked = self . input . lock ( ) ;
453
469
@@ -488,6 +504,12 @@ impl PerCoreScheduler {
488
504
} )
489
505
}
490
506
507
+ #[ cfg( target_arch = "riscv64" ) ]
508
+ pub fn reschedule ( & mut self ) {
509
+ // TODO: Align with others
510
+ without_interrupts ( || self . scheduler ( ) ) ;
511
+ }
512
+
491
513
/// Trigger an interrupt to reschedule the system
492
514
#[ cfg( target_arch = "aarch64" ) ]
493
515
pub fn reschedule ( & self ) {
@@ -630,10 +652,26 @@ impl PerCoreScheduler {
630
652
unsafe { * last_stack_pointer } ,
631
653
new_stack_pointer
632
654
) ;
633
- self . current_task = task;
655
+ #[ cfg( not( target_arch = "riscv64" ) ) ]
656
+ {
657
+ self . current_task = task;
658
+ }
634
659
635
660
// Finally return the context of the new task.
661
+ #[ cfg( not( target_arch = "riscv64" ) ) ]
636
662
return Some ( last_stack_pointer) ;
663
+
664
+ #[ cfg( target_arch = "riscv64" ) ]
665
+ {
666
+ if sstatus:: read ( ) . fs ( ) == sstatus:: FS :: Dirty {
667
+ self . current_task . borrow_mut ( ) . last_fpu_state . save ( ) ;
668
+ }
669
+ task. borrow ( ) . last_fpu_state . restore ( ) ;
670
+ self . current_task = task;
671
+ unsafe {
672
+ switch_to_task ( last_stack_pointer, new_stack_pointer. as_usize ( ) ) ;
673
+ }
674
+ }
637
675
}
638
676
}
639
677
0 commit comments