diff --git a/base-hw/src/core/kernel.cc b/base-hw/src/core/kernel.cc index e7c2fe5c51..3391405e87 100644 --- a/base-hw/src/core/kernel.cc +++ b/base-hw/src/core/kernel.cc @@ -380,13 +380,19 @@ namespace Kernel */ void do_resume_thread(Thread * const user) { - /* get targeted thread */ + /* lookup thread */ Thread * const t = Thread::pool()->object(user->user_arg_1()); - assert(t); - + if (!t) { + PERR("unknown thread"); + user->user_arg_0(-1); + return; + } /* check permissions */ - assert(user->pd_id() == core_id() || user->pd_id() == t->pd_id()); - + if (user->pd_id() != core_id() && user->pd_id() != t->pd_id()) { + PERR("not entitled to resume thread"); + user->user_arg_0(-1); + return; + } /* resume targeted thread */ user->user_arg_0(t->resume()); } @@ -397,22 +403,21 @@ namespace Kernel */ void do_resume_faulter(Thread * const user) { - /* get targeted thread */ + /* lookup thread */ Thread * const t = Thread::pool()->object(user->user_arg_1()); - assert(t); - + if (!t) { + PERR("unknown thread"); + user->user_arg_0(-1); + return; + } /* check permissions */ - assert(user->pd_id() == core_id() || user->pd_id() == t->pd_id()); - - /* - * Writeback the TLB entry that resolves the fault. - * This is a substitution for write-through-flagging - * the memory that holds the TLB data, because the latter - * is not feasible in core space. - */ + if (user->pd_id() != core_id() && user->pd_id() != t->pd_id()) { + PERR("not entitled to resume thread"); + user->user_arg_0(-1); + return; + } + /* writeback translation table and resume faulter */ Cpu::tlb_insertions(); - - /* resume targeted thread */ t->resume(); } @@ -422,11 +427,8 @@ namespace Kernel */ void do_yield_thread(Thread * const user) { - /* get targeted thread */ Thread * const t = Thread::pool()->object(user->user_arg_1()); - - /* invoke kernel object */ - if (t) t->resume(); + if (t) { t->receive_yielded_cpu(); } cpu_scheduler()->yield(); } diff --git a/base-hw/src/core/kernel/thread.h b/base-hw/src/core/kernel/thread.h index 2c84d37efa..45800c320b 100644 --- a/base-hw/src/core/kernel/thread.h +++ b/base-hw/src/core/kernel/thread.h @@ -488,6 +488,15 @@ class Kernel::Thread */ unsigned id() const { return Object::id(); } + /** + * Notice that another thread yielded the CPU to us + */ + void receive_yielded_cpu() + { + if (_state == AWAIT_RESUME) { _schedule(); } + else { PERR("failed to receive yielded CPU"); } + } + /*********************** ** Execution_context **