From 94ed7b82988500a0716d7be3610eaa6fe31232c5 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 21 Dec 2018 16:16:16 +1100 Subject: [PATCH] hwcrypto sha: Use spinlocks instead of semaphores for small state changes Significant performance improvement and smaller RAM footprint. --- components/esp32/hwcrypto/sha.c | 27 +++++++++++++++---------- components/esp32/include/hwcrypto/sha.h | 4 ++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/components/esp32/hwcrypto/sha.c b/components/esp32/hwcrypto/sha.c index ff26fdc94..fe42997a1 100644 --- a/components/esp32/hwcrypto/sha.c +++ b/components/esp32/hwcrypto/sha.c @@ -27,7 +27,6 @@ #include #include -#include #include #include @@ -56,9 +55,9 @@ inline static uint32_t SHA_CONTINUE_REG(esp_sha_type sha_type) { return SHA_1_CONTINUE_REG + sha_type * 0x10; } -/* Single lock for SHA engine memory block +/* Single spinlock for SHA engine memory block */ -static _lock_t memory_block_lock; +static portMUX_TYPE memory_block_lock = portMUX_INITIALIZER_UNLOCKED; /* Binary semaphore managing the state of each concurrent SHA engine. @@ -75,9 +74,9 @@ static SemaphoreHandle_t engine_states[3]; static uint8_t engines_in_use; -/* Lock for engines_in_use counter +/* Spinlock for engines_in_use counter */ -static _lock_t engines_in_use_lock; +static portMUX_TYPE engines_in_use_lock = portMUX_INITIALIZER_UNLOCKED; /* Index into the engine_states array */ inline static size_t sha_engine_index(esp_sha_type type) { @@ -123,12 +122,12 @@ inline static size_t block_length(esp_sha_type type) { void esp_sha_lock_memory_block(void) { - _lock_acquire(&memory_block_lock); + portENTER_CRITICAL(&memory_block_lock); } void esp_sha_unlock_memory_block(void) { - _lock_release(&memory_block_lock); + portEXIT_CRITICAL(&memory_block_lock); } static SemaphoreHandle_t sha_get_engine_state(esp_sha_type sha_type) @@ -177,7 +176,7 @@ static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_t return false; } - _lock_acquire(&engines_in_use_lock); + portENTER_CRITICAL(&engines_in_use_lock); if (engines_in_use == 0) { /* Just locked first engine, @@ -191,7 +190,7 @@ static bool esp_sha_lock_engine_common(esp_sha_type sha_type, TickType_t ticks_t engines_in_use++; assert(engines_in_use <= 3); - _lock_release(&engines_in_use_lock); + portEXIT_CRITICAL(&engines_in_use_lock); return true; } @@ -201,7 +200,7 @@ void esp_sha_unlock_engine(esp_sha_type sha_type) { SemaphoreHandle_t *engine_state = sha_get_engine_state(sha_type); - _lock_acquire(&engines_in_use_lock); + portENTER_CRITICAL(&engines_in_use_lock); engines_in_use--; @@ -211,7 +210,7 @@ void esp_sha_unlock_engine(esp_sha_type sha_type) periph_module_disable(PERIPH_SHA_MODULE); } - _lock_release(&engines_in_use_lock); + portEXIT_CRITICAL(&engines_in_use_lock); xSemaphoreGive(engine_state); } @@ -238,6 +237,9 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state) } #endif + // preemptively do this before entering the critical section, then re-check once in it + esp_sha_wait_idle(); + esp_sha_lock_memory_block(); esp_sha_wait_idle(); @@ -270,6 +272,9 @@ void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_ } #endif + // preemptively do this before entering the critical section, then re-check once in it + esp_sha_wait_idle(); + esp_sha_lock_memory_block(); esp_sha_wait_idle(); diff --git a/components/esp32/include/hwcrypto/sha.h b/components/esp32/include/hwcrypto/sha.h index 921f597fd..516de6c12 100644 --- a/components/esp32/include/hwcrypto/sha.h +++ b/components/esp32/include/hwcrypto/sha.h @@ -166,6 +166,8 @@ void esp_sha_unlock_engine(esp_sha_type sha_type); * while it is in use by the SHA engine. Caller should use esp_sha_wait_idle() * to ensure the SHA engine is not reading from the memory block in hardware. * + * @note This function enters a critical section. Do not block while holding this lock. + * * @note You do not need to lock the memory block before calling esp_sha_block() or esp_sha_read_digest_state(), these functions handle memory block locking internally. * * Call esp_sha_unlock_memory_block() when done. @@ -177,6 +179,8 @@ void esp_sha_lock_memory_block(void); * * Caller should have already locked a SHA engine before calling this function. * + * This function releases the critical section entered by esp_sha_lock_memory_block(). + * * Call following esp_sha_lock_memory_block(). */ void esp_sha_unlock_memory_block(void);