Skip to content

Commit 2d5f12b

Browse files
committed
Move continuation stack from .bss onto sys stack
1 parent 491c9b8 commit 2d5f12b

9 files changed

+70
-69
lines changed

Diff for: cores/esp8266/cont_util.c

+3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
#include "cont.h"
2222
#include <stddef.h>
23+
#include <string.h>
2324
#include "ets_sys.h"
2425

2526

2627
#define CONT_STACKGUARD 0xfeefeffe
2728

2829
void cont_init(cont_t* cont) {
30+
memset(cont, 0, sizeof(cont_t));
31+
2932
cont->stack_guard1 = CONT_STACKGUARD;
3033
cont->stack_guard2 = CONT_STACKGUARD;
3134
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4);

Diff for: cores/esp8266/core_esp8266_main.cpp

+46-27
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,30 @@ extern "C" {
3737

3838
#define LOOP_TASK_PRIORITY 1
3939
#define LOOP_QUEUE_SIZE 1
40-
4140
#define OPTIMISTIC_YIELD_TIME_US 16000
4241

42+
extern "C" void call_user_start();
43+
extern void loop();
44+
extern void setup();
45+
extern void (*__init_array_start)(void);
46+
extern void (*__init_array_end)(void);
47+
48+
/* Not static, used in Esp.cpp */
4349
struct rst_info resetInfo;
4450

51+
/* Not static, used in core_esp8266_postmortem.c.
52+
* Placed into noinit section because we assign value to this variable
53+
* before .bss is zero-filled, and need to preserve the value.
54+
*/
55+
cont_t* g_pcont __attribute__((section(".noinit")));
56+
57+
/* Event queue used by the main (arduino) task */
58+
static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
59+
60+
/* Used to implement optimistic_yield */
61+
static uint32_t s_micros_at_task_start;
62+
63+
4564
extern "C" {
4665
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
4766
const char* core_release =
@@ -52,19 +71,10 @@ const char* core_release =
5271
#endif
5372
} // extern "C"
5473

55-
int atexit(void (*func)()) {
56-
(void) func;
57-
return 0;
58-
}
59-
60-
extern "C" void ets_update_cpu_frequency(int freqmhz);
6174
void initVariant() __attribute__((weak));
6275
void initVariant() {
6376
}
6477

65-
extern void loop();
66-
extern void setup();
67-
6878
void preloop_update_frequency() __attribute__((weak));
6979
void preloop_update_frequency() {
7080
#if defined(F_CPU) && (F_CPU == 160000000L)
@@ -73,17 +83,10 @@ void preloop_update_frequency() {
7383
#endif
7484
}
7585

76-
extern void (*__init_array_start)(void);
77-
extern void (*__init_array_end)(void);
78-
79-
cont_t g_cont __attribute__ ((aligned (16)));
80-
static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
81-
82-
static uint32_t g_micros_at_task_start;
8386

8487
extern "C" void esp_yield() {
85-
if (cont_can_yield(&g_cont)) {
86-
cont_yield(&g_cont);
88+
if (cont_can_yield(g_pcont)) {
89+
cont_yield(g_pcont);
8790
}
8891
}
8992

@@ -92,7 +95,7 @@ extern "C" void esp_schedule() {
9295
}
9396

9497
extern "C" void __yield() {
95-
if (cont_can_yield(&g_cont)) {
98+
if (cont_can_yield(g_pcont)) {
9699
esp_schedule();
97100
esp_yield();
98101
}
@@ -104,8 +107,8 @@ extern "C" void __yield() {
104107
extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
105108

106109
extern "C" void optimistic_yield(uint32_t interval_us) {
107-
if (cont_can_yield(&g_cont) &&
108-
(system_get_time() - g_micros_at_task_start) > interval_us)
110+
if (cont_can_yield(g_pcont) &&
111+
(system_get_time() - s_micros_at_task_start) > interval_us)
109112
{
110113
yield();
111114
}
@@ -125,9 +128,9 @@ static void loop_wrapper() {
125128

126129
static void loop_task(os_event_t *events) {
127130
(void) events;
128-
g_micros_at_task_start = system_get_time();
129-
cont_run(&g_cont, &loop_wrapper);
130-
if (cont_check(&g_cont) != 0) {
131+
s_micros_at_task_start = system_get_time();
132+
cont_run(g_pcont, &loop_wrapper);
133+
if (cont_check(g_pcont) != 0) {
131134
panic();
132135
}
133136
}
@@ -145,6 +148,22 @@ void init_done() {
145148
esp_schedule();
146149
}
147150

151+
/* This is the entry point of the application.
152+
* It gets called on the default stack, which grows down from the top
153+
* of DRAM area.
154+
* .bss has not been zeroed out yet, but .data and .rodata are in place.
155+
* Cache is not enabled, so only ROM and IRAM functions can be called.
156+
* Peripherals (except for SPI0 and UART0) are not initialized.
157+
* This function does not return.
158+
*/
159+
extern "C" void ICACHE_RAM_ATTR app_entry(void)
160+
{
161+
/* Allocate continuation context on this stack, and save pointer to it. */
162+
cont_t s_cont __attribute__((aligned(16)));
163+
g_pcont = &s_cont;
164+
/* Call the entry point of the SDK code. */
165+
call_user_start();
166+
}
148167

149168
extern "C" void user_init(void) {
150169
struct rst_info *rtc_info_ptr = system_get_rst_info();
@@ -156,10 +175,10 @@ extern "C" void user_init(void) {
156175

157176
initVariant();
158177

159-
cont_init(&g_cont);
178+
cont_init(g_pcont);
160179

161180
ets_task(loop_task,
162-
LOOP_TASK_PRIORITY, g_loop_queue,
181+
LOOP_TASK_PRIORITY, s_loop_queue,
163182
LOOP_QUEUE_SIZE);
164183

165184
system_init_done_cb(&init_done);

Diff for: cores/esp8266/core_esp8266_postmortem.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
extern void __real_system_restart_local();
3434

35-
extern cont_t g_cont;
35+
extern cont_t* g_pcont;
3636

3737
// These will be pointers to PROGMEM const strings
3838
static const char* s_panic_file = 0;
@@ -131,8 +131,8 @@ void __wrap_system_restart_local() {
131131
ets_printf_P("\nSoft WDT reset\n");
132132
}
133133

134-
uint32_t cont_stack_start = (uint32_t) &(g_cont.stack);
135-
uint32_t cont_stack_end = (uint32_t) g_cont.stack_end;
134+
uint32_t cont_stack_start = (uint32_t) &(g_pcont->stack);
135+
uint32_t cont_stack_end = (uint32_t) g_pcont->stack_end;
136136
uint32_t stack_end;
137137

138138
// amount of stack taken by interrupt or exception handler

Diff for: cores/esp8266/libc_replacements.c

+3-35
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,7 @@ void _exit(int status) {
122122
abort();
123123
}
124124

125-
#if 0
126-
127-
int ICACHE_RAM_ATTR printf(const char* format, ...) {
128-
va_list arglist;
129-
va_start(arglist, format);
130-
int ret = ets_vprintf(ets_putc, format, arglist);
131-
va_end(arglist);
132-
return ret;
133-
}
134-
135-
int ICACHE_RAM_ATTR sprintf(char* buffer, const char* format, ...) {
136-
int ret;
137-
va_list arglist;
138-
va_start(arglist, format);
139-
ret = ets_vsprintf(buffer, format, arglist);
140-
va_end(arglist);
141-
return ret;
142-
}
143-
144-
int ICACHE_RAM_ATTR snprintf(char* buffer, size_t size, const char* format, ...) {
145-
int ret;
146-
va_list arglist;
147-
va_start(arglist, format);
148-
ret = ets_vsnprintf(buffer, size, format, arglist);
149-
va_end(arglist);
150-
return ret;
151-
}
152-
153-
int ICACHE_RAM_ATTR vprintf(const char * format, va_list arg) {
154-
return ets_vprintf(ets_putc, format, arg);
155-
}
156-
157-
int ICACHE_RAM_ATTR vsnprintf(char * buffer, size_t size, const char * format, va_list arg) {
158-
return ets_vsnprintf(buffer, size, format, arg);
125+
int atexit(void (*func)()) {
126+
(void) func;
127+
return 0;
159128
}
160-
#endif

Diff for: platform.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ compiler.c.flags=-c {compiler.warning_flags} -Os -g -Wpointer-arith -Wno-implici
3737
compiler.S.cmd=xtensa-lx106-elf-gcc
3838
compiler.S.flags=-c -g -x assembler-with-cpp -MMD -mlongcalls
3939

40-
compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u call_user_start {build.float} -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read
40+
compiler.c.elf.flags=-g {compiler.warning_flags} -Os -nostdlib -Wl,--no-check-sections -u app_entry {build.float} -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-L{compiler.libc.path}/lib" "-T{build.flash_ld}" -Wl,--gc-sections -Wl,-wrap,system_restart_local -Wl,-wrap,spi_flash_read
4141

4242
compiler.c.elf.cmd=xtensa-lx106-elf-gcc
4343
compiler.c.elf.libs=-lhal -lphy -lpp -lnet80211 {build.lwip_lib} -lwpa -lcrypto -lmain -lwps -laxtls -lespnow -lsmartconfig -lairkiss -lwpa2 -lstdc++ -lm -lc -lgcc

Diff for: tools/platformio-build.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def scons_patched_match_splitext(path, suffixes=None):
8080
],
8181
LINKFLAGS=[
8282
"-Wl,-wrap,system_restart_local",
83-
"-Wl,-wrap,spi_flash_read"
83+
"-Wl,-wrap,spi_flash_read",
84+
"-u,app_entry"
8485
]
8586
)
8687

Diff for: tools/sdk/include/ets_sys.h

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ int ets_vprintf(int (*print_function)(int), const char * format, va_list arg) __
215215
int ets_putc(int);
216216
bool ets_task(ETSTask task, uint8 prio, ETSEvent *queue, uint8 qlen);
217217
bool ets_post(uint8 prio, ETSSignal sig, ETSParam par);
218+
void ets_update_cpu_frequency(uint32_t ticks_per_us);
218219

219220

220221
#ifdef __cplusplus

Diff for: tools/sdk/ld/eagle.app.v6.common.ld

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ PHDRS
99
irom0_0_phdr PT_LOAD;
1010
}
1111
/* Default entry point: */
12-
ENTRY(call_user_start)
12+
ENTRY(app_entry)
1313
EXTERN(_DebugExceptionVector)
1414
EXTERN(_DoubleExceptionVector)
1515
EXTERN(_KernelExceptionVector)
@@ -75,6 +75,10 @@ SECTIONS
7575
_Pri_3_HandlerAddress = ABSOLUTE(.);
7676
_data_end = ABSOLUTE(.);
7777
} >dram0_0_seg :dram0_0_phdr
78+
.noinit : ALIGN(4)
79+
{
80+
*(.noinit)
81+
} >dram0_0_seg :dram0_0_phdr
7882
.irom0.text : ALIGN(4)
7983
{
8084
_irom0_text_start = ABSOLUTE(.);

Diff for: tools/sdk/ld/eagle.app.v6.common.ld.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ PHDRS
1212

1313

1414
/* Default entry point: */
15-
ENTRY(call_user_start)
15+
ENTRY(app_entry)
1616
EXTERN(_DebugExceptionVector)
1717
EXTERN(_DoubleExceptionVector)
1818
EXTERN(_KernelExceptionVector)
@@ -83,6 +83,11 @@ SECTIONS
8383
_Pri_3_HandlerAddress = ABSOLUTE(.);
8484
_data_end = ABSOLUTE(.);
8585
} >dram0_0_seg :dram0_0_phdr
86+
87+
.noinit : ALIGN(4)
88+
{
89+
*(.noinit)
90+
} >dram0_0_seg :dram0_0_phdr
8691

8792
#ifdef VTABLES_IN_DRAM
8893
#include "eagle.app.v6.common.ld.vtables.h"

0 commit comments

Comments
 (0)