|
| 1 | +/* |
| 2 | + BearSSLThunks.c - Allow use second stack for BearSSL calls |
| 3 | +
|
| 4 | + BearSSL uses a significant amount of stack space, much larger than |
| 5 | + the default Arduino core stack. These routines handle swapping |
| 6 | + between a secondary, user-allocated stack on the heap and the real |
| 7 | + stack. |
| 8 | +
|
| 9 | + Copyright (c) 2017 Earle F. Philhower, III. All rights reserved. |
| 10 | +
|
| 11 | + This library is free software; you can redistribute it and/or |
| 12 | + modify it under the terms of the GNU Lesser General Public |
| 13 | + License as published by the Free Software Foundation; either |
| 14 | + version 2.1 of the License, or (at your option) any later version. |
| 15 | +
|
| 16 | + This library is distributed in the hope that it will be useful, |
| 17 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | + Lesser General Public License for more details. |
| 20 | +
|
| 21 | + You should have received a copy of the GNU Lesser General Public |
| 22 | + License along with this library; if not, write to the Free Software |
| 23 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | + Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling) |
| 25 | +*/ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdlib.h> |
| 29 | + |
| 30 | +static uint32_t *_stackPtr = NULL; |
| 31 | +static uint32_t *_stackTop = NULL; |
| 32 | +static uint32_t _refcnt = 0; |
| 33 | + |
| 34 | +#define _stackSize (4500/4) |
| 35 | +#define _stackPaint 0xdeadbeef |
| 36 | + |
| 37 | +/* Add a reference, and allocate the stack if necessary */ |
| 38 | +void br_thunk_add_ref() |
| 39 | +{ |
| 40 | + _refcnt++; |
| 41 | + if (_refcnt == 1) { |
| 42 | + _stackPtr = (uint32_t *)malloc(_stackSize * sizeof(uint32_t)); |
| 43 | + _stackTop = _stackPtr + _stackSize - 1; |
| 44 | + for (int i=0; i < _stackSize; i++) { |
| 45 | + _stackPtr[i] = _stackPaint; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/* Drop a reference, and free stack if no more in use */ |
| 51 | +void br_thunk_del_ref() |
| 52 | +{ |
| 53 | + if (_refcnt == 0) { |
| 54 | + /* Error! */ |
| 55 | + return; |
| 56 | + } |
| 57 | + _refcnt--; |
| 58 | + if (!_refcnt) { |
| 59 | + free(_stackPtr); |
| 60 | + _stackPtr = NULL; |
| 61 | + _stackTop = NULL; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/* Return the number of bytes ever used since the stack was created */ |
| 66 | +uint32_t br_thunk_get_max_usage() |
| 67 | +{ |
| 68 | + uint32_t cnt = 0; |
| 69 | + |
| 70 | + /* No stack == no usage by definition! */ |
| 71 | + if (!_stackPtr) { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + for (cnt=0; (cnt < _stackSize) && (_stackPtr[cnt] == _stackPaint); cnt++) { |
| 76 | + /* Noop, all work done in for() */ |
| 77 | + } |
| 78 | + return 4 * (_stackSize - cnt); |
| 79 | +} |
| 80 | + |
| 81 | +__asm("\n\ |
| 82 | +.data\n\ |
| 83 | +.align 4\n\ |
| 84 | +_saveStack: .word 0x00000000\n\ |
| 85 | +\n\ |
| 86 | +.text\n\ |
| 87 | +.literal_position\n\ |
| 88 | +\n\ |
| 89 | +.macro thunk fcnName\n\ |
| 90 | + .text\n\ |
| 91 | + .global thunk_\\fcnName\n\ |
| 92 | + .type thunk_\\fcnName, @function\n\ |
| 93 | + .align 4\n\ |
| 94 | + thunk_\\fcnName:\n\ |
| 95 | + addi a1, a1, -16 /* Allocate space for saved registers on stack */\n\ |
| 96 | + s32i a0, a1, 12 /* Store A0, trounced by calls */\n\ |
| 97 | + s32i a15, a1, 8 /* Store A15 (our temporary one) */\n\ |
| 98 | + movi a15, _saveStack /* Store A1(SP) in temp space */\n\ |
| 99 | + s32i a1, a15, 0\n\ |
| 100 | + movi a15, _stackTop /* Load A1(SP) with thunk stack */\n\ |
| 101 | + l32i.n a1, a15, 0\n\ |
| 102 | + call0 \\fcnName /* Do the call */\n\ |
| 103 | + movi a15, _saveStack /* Restore A1(SP) */\n\ |
| 104 | + l32i.n a1, a15, 0\n\ |
| 105 | + l32i.n a15, a1, 8 /* Restore the saved registers */\n\ |
| 106 | + l32i.n a0, a1, 12\n\ |
| 107 | + addi a1, a1, 16 /* Free up stack and return to caller */\n\ |
| 108 | + ret\n\ |
| 109 | + .size thunk_\\fcnName, . - thunk_\\fcnName\n\ |
| 110 | +.endm\n\ |
| 111 | +\n\ |
| 112 | +thunk br_ssl_engine_recvapp_ack\n\ |
| 113 | +thunk br_ssl_engine_recvapp_buf\n\ |
| 114 | +thunk br_ssl_engine_recvrec_ack\n\ |
| 115 | +thunk br_ssl_engine_recvrec_buf\n\ |
| 116 | +thunk br_ssl_engine_sendapp_ack\n\ |
| 117 | +thunk br_ssl_engine_sendapp_buf\n\ |
| 118 | +thunk br_ssl_engine_sendrec_ack\n\ |
| 119 | +thunk br_ssl_engine_sendrec_buf"); |
0 commit comments