esp-idf/components/esp32
Ivan Grokhotkov cbb84e8f5e esp32s2: fix THREADPTR calculation, re-enable FreeRTOS TLS tests
1. Clarify THREADPTR calculation in FreeRTOS code, explaining where
the constant 0x10 offset comes from.

2. On the ESP32-S2, .flash.rodata section had different default
alignment (8 bytes instead of 16), which resulted in different offset
of the TLS sections. Unfortunately I haven’t found a way to query
section alignment from C code, or to use a constant value to define
section alignment in the linker script. The linker scripts are
modified to force a fixed 16 byte alignment for .flash.rodata on the
ESP32 and ESP32-S2beta. Note that the base address of .flash.rodata
was already 16 byte aligned, so this has not changed the actual
memory layout of the application.

Full explanation of the calculation below.

Assume we have the TLS template section base address
(tls_section_vma), the address of a TLS variable in the template
(address), and the final relocation value (offset). The linker
calculates:
offset = address - tls_section_vma + align_up(TCB_SIZE, alignment).

At run time, the TLS section gets copied from _thread_local_start
(in .rodata) to task_thread_local_start. Let’s assume that an address
of a variable in the runtime TLS section is runtime_address.
Access to this address will happen by calculating THREADPTR + offset.
So, by a series of substitutions:

THREADPTR + offset = runtime_address THREADPTR = runtime_address - offset
THREADPTR = runtime_address - (address - tls_section_vma + align_up(TCB_SIZE, alignment)) THREADPTR = (runtime_address - address) + tls_section_vma - align_up(TCB_SIZE, alignment)

The difference between runtime_address and address is same as the
difference between task_thread_local_start and _thread_local_start.
And tls_section_vma is the address of .rodata section, i.e.
_rodata_start. So we arrive to

THREADPTR = task_thread_local_start - _thread_local_start + _rodata_start - align_up(TCB_SIZE, alignment).

The idea with TCB_SIZE being added to the THREADPTR when computing
the relocation was to let the OS save TCB pointer in the TREADPTR
register. The location of the run-time TLS section was assumed to be
immediately after the TCB, aligned to whatever the section alignment
was. However in our case the problem is that the run-time TLS section
is stored not next to the TCB, but at the top of the stack. Plus,
even if it was stored next to the TCB, the size of a FreeRTOS TCB is
not equal to 8 bytes (TCB_SIZE hardcoded in the linker). So we have
to calculate THREADPTR in a slightly obscure way, to compensate for
these differences.

Closes IDF-1239
2020-01-23 11:29:22 +01:00
..
include clk.h: add extern C guards 2019-11-05 14:56:16 +01:00
ld esp32s2: fix THREADPTR calculation, re-enable FreeRTOS TLS tests 2020-01-23 11:29:22 +01:00
test Merge branch 'refactor/rename_esp32s2beta_to_esp32s2' into 'master' 2020-01-23 09:16:30 +08:00
brownout.c brownout: fix rtc_brownout_isr_handler signature 2019-08-01 16:28:56 +07:00
cache_err_int.c esp32: panic: do digital reset if cache error interrupt is set 2019-12-30 09:49:07 +01:00
cache_sram_mmu.c Spiram: Add option to reserve MMU banks; add himem API to make use of those banks 2018-10-15 14:32:58 +08:00
clk.c spi: move deprecated functions into internal header 2019-10-08 11:51:39 +08:00
CMakeLists.txt build system: Remove some dependencies from esp32 & esp32s2beta 2020-01-08 18:13:12 +11:00
component.mk Revert "C++: add provisions for optional RTTI support" 2019-10-31 10:12:16 +07:00
cpu_start.c Merge branch 'bugfix/coredump_bin_fmt_ver_update' into 'master' 2020-01-10 10:04:17 +08:00
crosscore_int.c tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
dport_access.c esp_wifi: Move esp32 DPORT access wrappers into esp_wifi component 2020-01-08 18:23:29 +11:00
dport_panic_highint_hdl.S esp32: Dis interrupts up to 5 lvl for DPORT 2019-07-16 05:54:45 +00:00
esp_clk_internal.h tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
esp_himem.c tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
esp_timer_esp32.c esp_timer/esp32: Fix case when alarm_reg > counter_reg but FRC_TIMER_INT_STATUS is not set 2020-01-22 14:30:34 +08:00
hw_random.c move esp32 chip specific includes to esp32/xxx.h 2019-03-18 17:14:05 +08:00
int_wdt.c feat(timer): refator timer group driver 2019-11-21 14:14:19 +08:00
intr_alloc.c intr_alloc: fix the issue intr_enable/disable cannot be used in ISR in 2019-12-23 10:23:00 +08:00
Kconfig esp32s2: mac addr allocation 2020-01-14 15:19:38 +08:00
linker.lf esp32: Use FPU for floating point divide, power, complex multiplications 2019-05-29 10:14:31 +10:00
Makefile.projbuild ldgen: fix build issue on make when build dir is a symlink 2019-08-27 13:53:31 +08:00
panic.c esp32: panic: do digital reset if cache error interrupt is set 2019-12-30 09:49:07 +01:00
pm_esp32.c soc/pm: Remove deprecated use of rtc_cpu_freq_t enum 2019-09-13 09:44:07 +10:00
pm_trace.c tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
project_include.cmake components: update with build system changes 2019-05-13 19:59:17 +08:00
reset_reason.c tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
sdkconfig.rename feat(psram): config SPI psram pins based on efuse value 2019-05-28 21:25:16 +08:00
sleep_modes.c rtcio: add hal for driver 2019-11-21 10:40:49 +08:00
spiram_psram.c ci: fix one ut issue when using Wrover-B module with newer ver of PSRAM 2019-11-03 03:07:37 +00:00
spiram_psram.h tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) 2019-08-01 16:28:56 +07:00
spiram.c Merge branch 'master' into feature/esp32s2beta_update 2019-08-08 15:26:58 +10:00
system_api_esp32.c system api: Move common parts into esp_common component 2019-10-30 14:19:22 +11:00
task_wdt.c feat(timer): refator timer group driver 2019-11-21 14:14:19 +08:00