Skip to content

Commit

Permalink
core: assert only normal thread takes a mutex
Browse files Browse the repository at this point in the history
Adds thread_is_in_normal_mode() which returns true if the current thread
is in normal mode. Adds an assert(thread_is_in_normal_mode()) in
__mutex_lock().

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Tested-by: Jens Wiklander <jens.wiklander@linaro.org> (QEMU)
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro authored and jforissier committed Jul 17, 2017
1 parent 4c6699f commit 6289239
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/arch/arm/include/kernel/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ vaddr_t thread_stack_start(void);
/* Returns the stack size for the current thread */
size_t thread_stack_size(void);

bool thread_is_in_normal_mode(void);

/*
* Returns true if previous exeception also was in abort mode.
*
Expand Down
1 change: 1 addition & 0 deletions core/arch/arm/kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static void __mutex_lock(struct mutex *m, const char *fname, int lineno)
{
assert_have_no_spinlock();
assert(thread_get_id_may_fail() != -1);
assert(thread_is_in_normal_mode());

while (true) {
uint32_t old_itr_status;
Expand Down
22 changes: 22 additions & 0 deletions core/arch/arm/kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,28 @@ bool thread_is_from_abort_mode(struct thread_abort_regs __maybe_unused *regs)
#endif
}

#ifdef ARM32
bool thread_is_in_normal_mode(void)
{
return (read_cpsr() & ARM32_CPSR_MODE_MASK) == ARM32_CPSR_MODE_SVC;
}
#endif

#ifdef ARM64
bool thread_is_in_normal_mode(void)
{
uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR);
struct thread_core_local *l = thread_get_core_local();
bool ret;

/* If any bit in l->flags is set we're handling some exception. */
ret = !l->flags;
thread_unmask_exceptions(exceptions);

return ret;
}
#endif

void thread_state_free(void)
{
struct thread_core_local *l = thread_get_core_local();
Expand Down

0 comments on commit 6289239

Please sign in to comment.