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().

Tested-by: Jens Wiklander <jens.wiklander@linaro.org> (QEMU)
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro committed Jul 7, 2017
1 parent 5d8aaa0 commit 1ff8250
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);

/*
* Adds a mutex to the list of held mutexes for current thread
* Requires foreign interrupts to be disabled.
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 @@ -642,6 +642,28 @@ size_t thread_stack_size(void)
return STACK_THREAD_SIZE;
}

#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 1ff8250

Please sign in to comment.