[Cryptech-Commits] [sw/stm32] 06/06: RSA keygen needs even more stack space than I thought.

git at cryptech.is git at cryptech.is
Mon Jun 13 20:55:47 UTC 2016


This is an automated email from the git hooks/post-receive script.

paul at psgd.org pushed a commit to branch master
in repository sw/stm32.

commit b7f9d44fe7a35c33bd6a5ee7db1cbd9a53e21ee2
Author: Paul Selkirk <paul at psgd.org>
AuthorDate: Mon Jun 13 15:55:56 2016 -0400

    RSA keygen needs even more stack space than I thought.
    
    Also, it turns out the linker wants to include initializers for sdram
    variables in the .elf and .bin files, even though it should handle it like
    bss. So now we manage sdram directly with a pseudo-malloc.
---
 projects/hsm/hsm.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/projects/hsm/hsm.c b/projects/hsm/hsm.c
index e3c1c36..70a7691 100644
--- a/projects/hsm/hsm.c
+++ b/projects/hsm/hsm.c
@@ -77,13 +77,9 @@
 /* Define an absurdly large task stack, because some pkey operation use a
  * lot of stack variables.
  */
-#define TASK_STACK_SIZE 64*1024
+#define TASK_STACK_SIZE 200*1024
 #endif
 
-/* Put the task stack buffers in SDRAM, because ARM RAM is too small.
- */
-__attribute__((section(".sdram1"))) uint8_t stack[NUM_RPC_TASK][TASK_STACK_SIZE];
-
 #ifndef MAX_PKT_SIZE
 /* Another arbitrary number, more or less driven by the 4096-bit RSA
  * keygen test.
@@ -180,6 +176,28 @@ static void dispatch_thread(void const *args)
 }
 osThreadDef_t thread_def[NUM_RPC_TASK];
 
+/* Allocate memory from SDRAM1. There is only malloc, no free, so we don't
+ * worry about fragmentation. */
+static uint8_t *sdram_malloc(size_t size)
+{
+    /* end of variables declared with __attribute__((section(".sdram1"))) */
+    extern uint8_t _esdram1 __asm ("_esdram1");
+    /* end of SDRAM1 section */
+    extern uint8_t __end_sdram1 __asm ("__end_sdram1");
+
+    static uint8_t *sdram_heap = &_esdram1;
+    uint8_t *p = sdram_heap;
+
+#define pad(n) (((n) + 3) & ~3)
+    size = pad(size);
+
+    if (p + size > &__end_sdram1)
+        return NULL;
+
+    sdram_heap += size;
+    return p;
+}
+
 /* The main thread. This does all the setup, and the worker threads handle
  * the rest.
  */
@@ -228,7 +246,9 @@ int main()
         ot->pthread = dispatch_thread;
         ot->tpriority = osPriorityNormal;
         ot->stacksize = TASK_STACK_SIZE;
-        ot->stack_pointer = (uint32_t *)stack[i];
+        ot->stack_pointer = (uint32_t *)(sdram_malloc(TASK_STACK_SIZE));
+        if (ot->stack_pointer == NULL)
+            Error_Handler();
         if (osThreadCreate(ot, (void *)i) == NULL)
             Error_Handler();
     }
@@ -236,5 +256,5 @@ int main()
     /* Start the non-blocking receive */
     HAL_UART_Receive_IT(&huart_user, &c, 1);
 
-    while (1) { ; }
+    return 0;
 }



More information about the Commits mailing list