From a7d74f80ce7e4502172d8b9dfa320ad7a43458cc Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Thu, 22 Feb 2024 21:56:37 -0500 Subject: [PATCH] kernel: move spinlock validation to own file Move spin_lock validation outside of thread.c into own file. This code really has nothing to do with the thread code. Signed-off-by: Anas Nashif --- kernel/CMakeLists.txt | 5 +++++ kernel/spinlock_validate.c | 40 ++++++++++++++++++++++++++++++++++++++ kernel/thread.c | 39 ------------------------------------- 3 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 kernel/spinlock_validate.c diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index f519b54556..0ad0bf5d78 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -84,6 +84,11 @@ endif() endif() +if(CONFIG_SPIN_VALIDATE) +list(APPEND kernel_files + spinlock_validate.c) +endif() + if(CONFIG_XIP) list(APPEND kernel_files xip.c) diff --git a/kernel/spinlock_validate.c b/kernel/spinlock_validate.c new file mode 100644 index 0000000000..90da6fac1b --- /dev/null +++ b/kernel/spinlock_validate.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018,2024 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +bool z_spin_lock_valid(struct k_spinlock *l) +{ + uintptr_t thread_cpu = l->thread_cpu; + + if (thread_cpu != 0U) { + if ((thread_cpu & 3U) == _current_cpu->id) { + return false; + } + } + return true; +} + +bool z_spin_unlock_valid(struct k_spinlock *l) +{ + if (l->thread_cpu != (_current_cpu->id | (uintptr_t)_current)) { + return false; + } + l->thread_cpu = 0; + return true; +} + +void z_spin_lock_set_owner(struct k_spinlock *l) +{ + l->thread_cpu = _current_cpu->id | (uintptr_t)_current; +} + +#ifdef CONFIG_KERNEL_COHERENCE +bool z_spin_lock_mem_coherent(struct k_spinlock *l) +{ + return arch_mem_coherent((void *)l); +} +#endif /* CONFIG_KERNEL_COHERENCE */ diff --git a/kernel/thread.c b/kernel/thread.c index 31b92fdb2c..17094798e8 100644 --- a/kernel/thread.c +++ b/kernel/thread.c @@ -945,45 +945,6 @@ FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, #endif } -/* These spinlock assertion predicates are defined here because having - * them in spinlock.h is a giant header ordering headache. - */ -#ifdef CONFIG_SPIN_VALIDATE -bool z_spin_lock_valid(struct k_spinlock *l) -{ - uintptr_t thread_cpu = l->thread_cpu; - - if (thread_cpu != 0U) { - if ((thread_cpu & 3U) == _current_cpu->id) { - return false; - } - } - return true; -} - -bool z_spin_unlock_valid(struct k_spinlock *l) -{ - if (l->thread_cpu != (_current_cpu->id | (uintptr_t)_current)) { - return false; - } - l->thread_cpu = 0; - return true; -} - -void z_spin_lock_set_owner(struct k_spinlock *l) -{ - l->thread_cpu = _current_cpu->id | (uintptr_t)_current; -} - -#ifdef CONFIG_KERNEL_COHERENCE -bool z_spin_lock_mem_coherent(struct k_spinlock *l) -{ - return arch_mem_coherent((void *)l); -} -#endif /* CONFIG_KERNEL_COHERENCE */ - -#endif /* CONFIG_SPIN_VALIDATE */ - int z_impl_k_float_disable(struct k_thread *thread) { #if defined(CONFIG_FPU) && defined(CONFIG_FPU_SHARING)