Skip to content

Commit

Permalink
fix(time): fix clock_nanosleep bug and signal cause zombie state trap…
Browse files Browse the repository at this point in the history
… return to user bug
  • Loading branch information
ChenRuiwei committed Jul 26, 2024
1 parent 43d4b8a commit 460a3f6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TEST_DIR := ./testcase/$(TEST)
export STRACE :=
export SMP :=
export PREEMPT :=
export DEBUG :=

# Args
DISASM_ARGS = -d
Expand Down
4 changes: 4 additions & 0 deletions arch/src/riscv64/sstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Sstatus {
}
}

pub fn sie(&mut self) -> bool {
self.bits.get_bit(1)
}

pub fn set_spie(&mut self, val: bool) {
self.bits.set_bit(5, val);
}
Expand Down
3 changes: 3 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ endif
ifneq ($(PREEMPT), )
FEATURES += preempt
endif
ifneq ($(DEBUG), )
FEATURES += debug
endif

CARGO_BUILD_ARGS :=
ifeq ($(MODE), release)
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/syscall/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Syscall<'_> {
return Ok(0);
}
let sleep = req - current;
task.suspend_timeout(req).await
task.suspend_timeout(sleep).await
} else {
task.suspend_timeout(req).await
};
Expand Down
6 changes: 6 additions & 0 deletions kernel/src/task/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ impl<F: Future<Output = ()> + Send + 'static> Future for KernelTaskFuture<F> {
pub async fn task_loop(task: Arc<Task>) {
*task.waker() = Some(get_waker().await);
loop {
match task.state() {
Zombie => break,
Stopped => suspend_now().await,
_ => {}
}

trap::user_trap::trap_return(&task);

// task may be set to zombie by other task, e.g. execve will kill other tasks in
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/trap/user_trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ pub fn trap_return(task: &Arc<Task>) {
// 2. This task encounter a signal handler
task.trap_context_mut().user_fx.restore();
task.trap_context_mut().sstatus.set_fs(FS::Clean);
assert!(!task.trap_context_mut().sstatus.sie());
assert!(!task.is_zombie());
unsafe {
__return_to_user(task.trap_context_mut());
// NOTE: next time when user traps into kernel, it will come back here
Expand Down

0 comments on commit 460a3f6

Please sign in to comment.