|
27 | 27 | #include <stdint.h>
|
28 | 28 | #include <stdlib.h>
|
29 | 29 | #include <stdio.h>
|
30 |
| -#include "pgmspace.h" |
| 30 | + |
31 | 31 | #include "debug.h"
|
32 | 32 | #include "StackThunk.h"
|
| 33 | + |
| 34 | +#include <pgmspace.h> |
33 | 35 | #include <ets_sys.h>
|
| 36 | + |
34 | 37 | #include <umm_malloc/umm_malloc.h>
|
35 | 38 | #include <umm_malloc/umm_heap_select.h>
|
36 | 39 |
|
37 | 40 | extern "C" {
|
38 | 41 |
|
| 42 | +extern void optimistic_yield(uint32_t); |
| 43 | + |
39 | 44 | uint32_t *stack_thunk_ptr = NULL;
|
40 | 45 | uint32_t *stack_thunk_top = NULL;
|
| 46 | + |
41 | 47 | uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
|
| 48 | +uint32_t *stack_thunk_yield_save = NULL; /* Saved A1 when yielding from within BearSSL */ |
| 49 | + |
42 | 50 | uint32_t stack_thunk_refcnt = 0;
|
43 | 51 |
|
44 | 52 | /* Largest stack usage seen in the wild at 6120 */
|
@@ -150,4 +158,37 @@ void stack_thunk_fatal_smashing()
|
150 | 158 | __stack_chk_fail();
|
151 | 159 | }
|
152 | 160 |
|
| 161 | +/* Called within bearssl code instead of optimistic_yield(...) */ |
| 162 | +void stack_thunk_yield(); |
| 163 | +asm( |
| 164 | + ".section .text.stack_thunk_yield,\"ax\",@progbits\n\t" |
| 165 | + ".literal_position\n\t" |
| 166 | + ".align 4\n\t" |
| 167 | + ".global stack_thunk_yield\n\t" |
| 168 | + ".type stack_thunk_yield, @function\n\t" |
| 169 | + "\n" |
| 170 | +"stack_thunk_yield:\n\t" |
| 171 | +/* Keep the original caller */ |
| 172 | + "addi a1, a1, -16\n\t" |
| 173 | + "s32i.n a0, a1, 12\n\t" |
| 174 | +/* Swap bearssl <-> cont stacks */ |
| 175 | + "movi a2, stack_thunk_yield_save\n\t" |
| 176 | + "s32i.n a1, a2, 0\n\t" |
| 177 | + "movi a2, stack_thunk_save\n\t" |
| 178 | + "l32i.n a1, a2, 0\n\t" |
| 179 | +/* optimistic_yield(10000) without extra l32r */ |
| 180 | + "movi a2, 0x10\n\t" |
| 181 | + "addmi a2, a2, 0x2700\n\t" |
| 182 | + "call0 optimistic_yield\n\t" |
| 183 | +/* Swap bearssl <-> cont stacks, again */ |
| 184 | + "movi a2, stack_thunk_yield_save\n\t" |
| 185 | + "l32i.n a1, a2, 0\n\t" |
| 186 | + "\n" |
| 187 | +/* Restore caller */ |
| 188 | + "l32i.n a0, a1, 12\n\t" |
| 189 | + "addi a1, a1, 16\n\t" |
| 190 | + "ret.n\n\t" |
| 191 | + ".size stack_thunk_yield, .-stack_thunk_yield\n\t" |
| 192 | +); |
| 193 | + |
153 | 194 | }
|
0 commit comments