Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack thunk yield() fixes #9224

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion cores/esp8266/StackThunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "pgmspace.h"

#include "debug.h"
#include "StackThunk.h"

#include <pgmspace.h>
#include <ets_sys.h>

#include <umm_malloc/umm_malloc.h>
#include <umm_malloc/umm_heap_select.h>

extern "C" {

extern void optimistic_yield(uint32_t);

uint32_t *stack_thunk_ptr = NULL;
uint32_t *stack_thunk_top = NULL;

uint32_t *stack_thunk_save = NULL; /* Saved A1 while in BearSSL */
uint32_t *stack_thunk_yield_save = NULL; /* Saved A1 when yielding from within BearSSL */

uint32_t stack_thunk_refcnt = 0;

/* Largest stack usage seen in the wild at 6120 */
Expand Down Expand Up @@ -150,4 +158,37 @@ void stack_thunk_fatal_smashing()
__stack_chk_fail();
}

/* Called within bearssl code instead of optimistic_yield(...) */
void stack_thunk_yield();
asm(
".section .text.stack_thunk_yield,\"ax\",@progbits\n\t"
".literal_position\n\t"
".align 4\n\t"
".global stack_thunk_yield\n\t"
".type stack_thunk_yield, @function\n\t"
"\n"
"stack_thunk_yield:\n\t"
/* Keep the original caller */
"addi a1, a1, -16\n\t"
"s32i.n a0, a1, 12\n\t"
/* Swap bearssl <-> cont stacks */
"movi a2, stack_thunk_yield_save\n\t"
"s32i.n a1, a2, 0\n\t"
"movi a2, stack_thunk_save\n\t"
"l32i.n a1, a2, 0\n\t"
/* optimistic_yield(10000) without extra l32r */
"movi a2, 0x10\n\t"
"addmi a2, a2, 0x2700\n\t"
"call0 optimistic_yield\n\t"
/* Swap bearssl <-> cont stacks, again */
"movi a2, stack_thunk_yield_save\n\t"
"l32i.n a1, a2, 0\n\t"
"\n"
/* Restore caller */
"l32i.n a0, a1, 12\n\t"
"addi a1, a1, 16\n\t"
"ret.n\n\t"
".size stack_thunk_yield, .-stack_thunk_yield\n\t"
);

}
2 changes: 2 additions & 0 deletions cores/esp8266/StackThunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
extern "C" {
#endif

extern void stack_thunk_yield(void);

extern void stack_thunk_add_ref();
extern void stack_thunk_del_ref();
extern void stack_thunk_repaint();
Expand Down
2 changes: 1 addition & 1 deletion tools/sdk/include/bearssl/bearssl_git.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Do not edit -- Automatically generated by tools/sdk/ssl/bearssl/Makefile
#define BEARSSL_GIT b024386
#define BEARSSL_GIT 5166f2b
Binary file modified tools/sdk/lib/libbearssl.a
Binary file not shown.
Loading