From f86aa9e1925ef129e92324b3d5e1d4c7f0a03e87 Mon Sep 17 00:00:00 2001 From: Jerome Forissier Date: Thu, 9 Jul 2020 19:46:09 +0200 Subject: core: make thread ID a short int Changes thread_get_id() and thread_get_id_may_fail() to return 'short int' instead of 'int'. That is, 16 bits instead of 32 on all supported architectures which is more than enough since the largest thread ID value is (CFG_NUM_THREADS - 1). Note, struct wait_queue_elem::handle is already a short int. trace_ext_get_thread_id() is not changed (still returns an int) because it is part of the TA API and modifying it would needlessly introduce incompatibilities. Signed-off-by: Jerome Forissier Reviewed-by: Etienne Carriere Reviewed-by: Jens Wiklander --- core/arch/arm/include/kernel/thread.h | 6 +++--- core/arch/arm/kernel/mutex_lockdep.c | 6 +++--- core/arch/arm/kernel/thread.c | 10 ++++++---- core/arch/arm/kernel/thread_a64.S | 2 +- core/include/kernel/tee_ta_manager.h | 2 +- core/kernel/tee_ta_manager.c | 2 +- lib/libutils/ext/ftrace/ftrace.c | 2 +- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/core/arch/arm/include/kernel/thread.h b/core/arch/arm/include/kernel/thread.h index 98abe26a..c0443e5c 100644 --- a/core/arch/arm/include/kernel/thread.h +++ b/core/arch/arm/include/kernel/thread.h @@ -42,7 +42,7 @@ struct thread_core_local { uint64_t x[4]; #endif vaddr_t tmp_stack_va_end; - int curr_thread; + short int curr_thread; uint32_t flags; vaddr_t abt_stack_va_end; #ifdef CFG_TEE_CORE_DEBUG @@ -299,12 +299,12 @@ void thread_clr_boot_thread(void); /* * Returns current thread id. */ -int thread_get_id(void); +short int thread_get_id(void); /* * Returns current thread id, return -1 on failure. */ -int thread_get_id_may_fail(void); +short int thread_get_id_may_fail(void); /* Returns Thread Specific Data (TSD) pointer. */ struct thread_specific_data *thread_get_tsd(void); diff --git a/core/arch/arm/kernel/mutex_lockdep.c b/core/arch/arm/kernel/mutex_lockdep.c index 144e7b8e..2095c862 100644 --- a/core/arch/arm/kernel/mutex_lockdep.c +++ b/core/arch/arm/kernel/mutex_lockdep.c @@ -36,7 +36,7 @@ void mutex_lockdep_init(void) void mutex_lock_check(struct mutex *m) { - int thread = thread_get_id(); + short int thread = thread_get_id(); uint32_t exceptions = 0; exceptions = cpu_spin_lock_xsave(&graph_lock); @@ -46,7 +46,7 @@ void mutex_lock_check(struct mutex *m) void mutex_trylock_check(struct mutex *m) { - int thread = thread_get_id(); + short int thread = thread_get_id(); uint32_t exceptions = 0; exceptions = cpu_spin_lock_xsave(&graph_lock); @@ -56,7 +56,7 @@ void mutex_trylock_check(struct mutex *m) void mutex_unlock_check(struct mutex *m) { - int thread = thread_get_id(); + short int thread = thread_get_id(); uint32_t exceptions = 0; exceptions = cpu_spin_lock_xsave(&graph_lock); diff --git a/core/arch/arm/kernel/thread.c b/core/arch/arm/kernel/thread.c index 3d876aec..21a00ba3 100644 --- a/core/arch/arm/kernel/thread.c +++ b/core/arch/arm/kernel/thread.c @@ -834,23 +834,25 @@ bool thread_init_stack(uint32_t thread_id, vaddr_t sp) return true; } -int thread_get_id_may_fail(void) +short int thread_get_id_may_fail(void) { /* * thread_get_core_local() requires foreign interrupts to be disabled */ uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_FOREIGN_INTR); struct thread_core_local *l = thread_get_core_local(); - int ct = l->curr_thread; + short int ct = l->curr_thread; thread_unmask_exceptions(exceptions); return ct; } -int thread_get_id(void) +short int thread_get_id(void) { - int ct = thread_get_id_may_fail(); + short int ct = thread_get_id_may_fail(); + /* Thread ID has to fit in a short int */ + COMPILE_TIME_ASSERT(CFG_NUM_THREADS <= SHRT_MAX); assert(ct >= 0 && ct < CFG_NUM_THREADS); return ct; } diff --git a/core/arch/arm/kernel/thread_a64.S b/core/arch/arm/kernel/thread_a64.S index d0df9bf8..74512089 100644 --- a/core/arch/arm/kernel/thread_a64.S +++ b/core/arch/arm/kernel/thread_a64.S @@ -16,7 +16,7 @@ #include "thread_private.h" .macro get_thread_ctx core_local, res, tmp0, tmp1 - ldr w\tmp0, [\core_local, \ + ldrh w\tmp0, [\core_local, \ #THREAD_CORE_LOCAL_CURR_THREAD] ldr x\res, =threads mov x\tmp1, #THREAD_CTX_SIZE diff --git a/core/include/kernel/tee_ta_manager.h b/core/include/kernel/tee_ta_manager.h index f8eb7c0f..eb87501b 100644 --- a/core/include/kernel/tee_ta_manager.h +++ b/core/include/kernel/tee_ta_manager.h @@ -105,7 +105,7 @@ struct tee_ta_session { uint32_t ref_count; /* reference counter */ struct condvar refc_cv; /* CV used to wait for ref_count to be 0 */ struct condvar lock_cv; /* CV used to wait for lock */ - int lock_thread; /* Id of thread holding the lock */ + short int lock_thread; /* Id of thread holding the lock */ bool unlink; /* True if session is to be unlinked */ #if defined(CFG_TA_GPROF_SUPPORT) struct sample_buf *sbuf; /* Profiling data (PC sampling) */ diff --git a/core/kernel/tee_ta_manager.c b/core/kernel/tee_ta_manager.c index 82bf2707..deb6754b 100644 --- a/core/kernel/tee_ta_manager.c +++ b/core/kernel/tee_ta_manager.c @@ -39,7 +39,7 @@ struct tee_ta_ctx_head tee_ctxes = TAILQ_HEAD_INITIALIZER(tee_ctxes); #ifndef CFG_CONCURRENT_SINGLE_INSTANCE_TA static struct condvar tee_ta_cv = CONDVAR_INITIALIZER; -static int tee_ta_single_instance_thread = THREAD_ID_INVALID; +static short int tee_ta_single_instance_thread = THREAD_ID_INVALID; static size_t tee_ta_single_instance_count; #endif diff --git a/lib/libutils/ext/ftrace/ftrace.c b/lib/libutils/ext/ftrace/ftrace.c index bf3d968e..dff56d13 100644 --- a/lib/libutils/ext/ftrace/ftrace.c +++ b/lib/libutils/ext/ftrace/ftrace.c @@ -32,7 +32,7 @@ static const char hex_str[] = "0123456789abcdef"; static __noprof struct ftrace_buf *get_fbuf(void) { #if defined(__KERNEL__) - int ct = thread_get_id_may_fail(); + short int ct = thread_get_id_may_fail(); struct tee_ta_session *s = NULL; struct thread_specific_data *tsd = NULL; -- cgit v1.2.3