Skip to content

Commit 5f9281f

Browse files
Replace the SDK's use of ets_intr_lock/unlock with nestable versions
Testing has shown that there are several paths in the SDK that result in nested calls to ets_intr_lock() / ets_intr_unlock() which may be a problem. These functions also do not preserve the enabled interrupt level and may result in code running with interrupts enabled when that is not intended. This issue has recently been fixed in the Arduino code by using xt_rsil() / xt_wsr_ps() but still exists in the Espressif SDK code. This commit is intended to fix that and should be used in addition to the above. The maximum nesting I have seen is 2 and lock/unlock calls appear to be balanced. A max of 7 levels of nesting leaves plenty of room for that to change.
1 parent b6e5830 commit 5f9281f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

cores/esp8266/core_esp8266_main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ static os_event_t s_loop_queue[LOOP_QUEUE_SIZE];
6060
/* Used to implement optimistic_yield */
6161
static uint32_t s_micros_at_task_start;
6262

63+
/* For ets_intr_lock_nest / ets_intr_unlock_nest
64+
* Max nesting seen by SDK so far is 2.
65+
*/
66+
#define ETS_INTR_LOCK_NEST_MAX 7
67+
byte ets_intr_lock_stack[ETS_INTR_LOCK_NEST_MAX];
68+
byte ets_intr_lock_stack_ptr=0;
69+
6370

6471
extern "C" {
6572
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
@@ -121,6 +128,17 @@ extern "C" void optimistic_yield(uint32_t interval_us) {
121128
}
122129
}
123130

131+
extern "C" void ets_intr_lock_nest() {
132+
if (ets_intr_lock_stack_ptr < ETS_INTR_LOCK_NEST_MAX)
133+
ets_intr_lock_stack[ets_intr_lock_stack_ptr++] = xt_rsil(3);
134+
}
135+
136+
extern "C" void ets_intr_unlock_nest() {
137+
if (ets_intr_lock_stack_ptr > 0)
138+
xt_wsr_ps(ets_intr_lock_stack[--ets_intr_lock_stack_ptr]);
139+
}
140+
141+
124142
extern "C" void __loop_end (void)
125143
{
126144
run_scheduled_functions();

tools/sdk/lib/fix_sdk_libs.sh

+7
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ xtensa-lx106-elf-objcopy --redefine-sym default_hostname=wifi_station_default_ho
1616
xtensa-lx106-elf-objcopy --redefine-sym default_hostname=wifi_station_default_hostname eagle_lwip_if.o
1717
xtensa-lx106-elf-ar r libmain.a eagle_lwip_if.o user_interface.o
1818
rm -f eagle_lwip_if.o user_interface.o
19+
20+
# Replace use of ROM ets_intr_(un)lock with nestable ets_intr_(un)lock_nest
21+
for f in libmain.a libpp.a libnet80211.a; do
22+
xtensa-lx106-elf-objcopy --redefine-sym ets_intr_lock=ets_intr_lock_nest $f;
23+
xtensa-lx106-elf-objcopy --redefine-sym ets_intr_unlock=ets_intr_unlock_nest $f;
24+
done
25+

0 commit comments

Comments
 (0)