Skip to content

Commit 896b686

Browse files
committed
critical code protection (wip)
1 parent aa4f87e commit 896b686

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

Diff for: cores/esp8266/Arduino.h

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ void ets_intr_unlock();
165165
#define interrupts() xt_rsil(0)
166166
#define noInterrupts() xt_rsil(15)
167167

168+
#define lockEnterVar(varSave, level) __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (varSave))
169+
#define lockLeaveVar(varSave) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (varSave) : "memory")
170+
171+
#define lockDeclM() uint32_t intrState
172+
#define lockEnterM() lockEnterVar(intrState, 0)
173+
#define lockLeaveM() lockLeaveVar(intrState)
174+
175+
#define lockEnter() lockDeclM(); lockEnterM()
176+
#define lockLeave() lockLeaveM()
177+
168178
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
169179
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
170180
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )

Diff for: cores/esp8266/Schedule.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static scheduled_fn_t* sFirstUnused = 0;
1717

1818
static int sCount = 0;
1919

20+
IRAM_ATTR
2021
static scheduled_fn_t* get_fn() {
2122
scheduled_fn_t* result = NULL;
2223
// try to get an item from unused items list
@@ -39,24 +40,28 @@ static void recycle_fn(scheduled_fn_t* fn)
3940
sFirstUnused = fn;
4041
}
4142

43+
IRAM_ATTR
4244
bool schedule_function_us(mFuncT fn, uint32_t repeat_us)
4345
{
46+
lockEnter();
47+
4448
scheduled_fn_t* item = get_fn();
4549
if (!item) {
50+
lockLeave();
4651
return false;
4752
}
4853
item->mFunc = fn;
49-
50-
uint32_t savedPS = xt_rsil(0); // noInterrupts();
5154
item->mNext = sFirst;
5255
sFirst = item;
53-
xt_wsr_ps(savedPS); // interrupts();
5456

5557
if (repeat_us)
5658
item->callNow.reset(repeat_us);
59+
60+
lockLeave();
5761
return true;
5862
}
5963

64+
IRAM_ATTR
6065
bool schedule_function(std::function<void(void)> fn)
6166
{
6267
return schedule_function_us([&fn](){ fn(); return false; }, 0);
@@ -70,10 +75,10 @@ void run_scheduled_functions()
7075
toCall = item->mNext;
7176
if (item->callNow && !item->mFunc())
7277
{
73-
uint32_t savedPS = xt_rsil(0); // noInterrupts();
78+
lockEnter();
7479
if (sFirst == item)
7580
sFirst = item->mNext;
76-
xt_wsr_ps(savedPS); // interrupts();
81+
lockLeave();
7782

7883
item->mFunc = mFuncT();
7984
recycle_fn(item);

Diff for: tests/host/common/c_types.h

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ typedef enum {
102102
#define ICACHE_RODATA_ATTR
103103
#endif /* ICACHE_FLASH */
104104

105+
// counterpart https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp8266-compat.h
106+
#define IRAM_ATTR ICACHE_RAM_ATTR
107+
105108
#define STORE_ATTR __attribute__((aligned(4)))
106109

107110
#ifndef __cplusplus

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

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ typedef enum {
9393
#define ICACHE_RAM_ATTR
9494
#endif /* ICACHE_FLASH */
9595

96+
// counterpart https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp8266-compat.h
97+
#define IRAM_ATTR ICACHE_RAM_ATTR
98+
9699
#define STORE_ATTR __attribute__((aligned(4)))
97100

98101
#ifndef __cplusplus

0 commit comments

Comments
 (0)