]> granicus.if.org Git - esp-idf/commitdiff
hwcrypto sha: Use spinlocks instead of semaphores for small state changes
authorAngus Gratton <angus@espressif.com>
Fri, 21 Dec 2018 05:16:16 +0000 (16:16 +1100)
committerbot <bot@espressif.com>
Wed, 23 Jan 2019 04:59:44 +0000 (04:59 +0000)
Significant performance improvement and smaller RAM footprint.

components/esp32/hwcrypto/sha.c
components/esp32/include/hwcrypto/sha.h

index ff26fdc94248c4e49d4c05785523abca3a3da27a..fe42997a1d2d7f00b7352d43f2d7ebc6f1a99b97 100644 (file)
@@ -27,7 +27,6 @@
 
 #include <string.h>
 #include <stdio.h>
-#include <sys/lock.h>
 #include <byteswap.h>
 #include <assert.h>
 
@@ -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();
index 921f597fdd7d2b4f385fdbf6df4d8dee8e0d5933..516de6c12724288807f69a800d353f797cd7ec0a 100644 (file)
@@ -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);