From 2ad617533b53e7e04ead7cbf22236b5690c9bb06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Audun=20Kvamtr=C3=B8?= Date: Thu, 24 Oct 2024 11:21:33 +0200 Subject: [PATCH] crypto: threading: Ensure mbedtls_mutex functions can be used in TF-M MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -This commit moves checks to ensure we aren't in pre_kernel/ISR into its own function to ensure that this can be compiled in TF-M which will be the case for CRACEN enabled devices. -Adding static function is_pre_kernel_or_isr which is checks if TF-M and MultiThreading is enabled Signed-off-by: Frank Audun Kvamtrø --- .../nrf_security/src/threading/threading_alt.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/subsys/nrf_security/src/threading/threading_alt.c b/subsys/nrf_security/src/threading/threading_alt.c index 7e263a9acffe..baa736094713 100644 --- a/subsys/nrf_security/src/threading/threading_alt.c +++ b/subsys/nrf_security/src/threading/threading_alt.c @@ -32,23 +32,32 @@ NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_asymmetric); NRF_SECURITY_MUTEX_DEFINE(cracen_mutex_symmetric); #endif +static bool inline is_pre_kernel_or_isr(void) +{ +#if defined(CONFIG_MULTITHREADING) && !defined(__NRF_TFM__) + return k_is_pre_kernel() || k_is_in_isr(); +#else + return 0; +#endif +} + static void mbedtls_mutex_init_fn(mbedtls_threading_mutex_t * mutex) { - if(!k_is_pre_kernel() && !k_is_in_isr()) { + if(!is_pre_kernel_or_isr()) { nrf_security_mutex_init(mutex); } } static void mbedtls_mutex_free_fn(mbedtls_threading_mutex_t * mutex) { - if(!k_is_pre_kernel() && !k_is_in_isr()) { + if(!is_pre_kernel_or_isr()) { nrf_security_mutex_free(mutex); } } static int mbedtls_mutex_lock_fn(mbedtls_threading_mutex_t * mutex) { - if(!k_is_pre_kernel() && !k_is_in_isr()) { + if(!is_pre_kernel_or_isr()) { return nrf_security_mutex_lock(mutex); } else { return 0; @@ -57,7 +66,7 @@ static int mbedtls_mutex_lock_fn(mbedtls_threading_mutex_t * mutex) static int mbedtls_mutex_unlock_fn(mbedtls_threading_mutex_t * mutex) { - if(!k_is_pre_kernel() && !k_is_in_isr()) { + if(!is_pre_kernel_or_isr()) { return nrf_security_mutex_unlock(mutex); } else { return 0;