esp-idf/components/freertos
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/freertos global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test esp32s2: fix THREADPTR calculation, re-enable FreeRTOS TLS tests 2020-01-23 11:29:22 +01:00
CMakeLists.txt freertos: added a FreeRTOS property returning "original" include path 2019-12-06 12:16:54 +01:00
component.mk freertos: fix defining uxTopUsedPriority for esp32s2beta 2019-10-29 16:38:48 +01:00
croutine.c Add UNTESTED_FUNCTION() call to untested functions, make Kconfig option to enable that to map to assert() 2016-09-27 11:36:30 +08:00
event_groups.c freertos: removed likely macros from non-port specifics parts of freertos 2020-01-03 14:42:57 -03:00
FreeRTOS-openocd.c freertos: fix defining uxTopUsedPriority for esp32s2beta 2019-10-29 16:38:48 +01:00
Kconfig spin_lock: added new spinlock interface and decoupled it from RTOS 2020-01-22 06:20:34 +08:00
license.txt Initial public version 2016-08-17 23:08:22 +08:00
linker.lf ldgen: update component linker fragment files 2019-04-04 15:57:34 +08:00
list.c freertos: removed likely macros from non-port specifics parts of freertos 2020-01-03 14:42:57 -03:00
port.c esp32s2: fix THREADPTR calculation, re-enable FreeRTOS TLS tests 2020-01-23 11:29:22 +01:00
portasm.S pm: support for tracing using GPIOs 2017-10-18 14:19:18 +08:00
portmacro_priv.h global: move the soc component out of the common list 2019-04-16 13:21:15 +08:00
queue.c freertos: removed likely macros from non-port specifics parts of freertos 2020-01-03 14:42:57 -03:00
readme_xtensa.txt Initial public version 2016-08-17 23:08:22 +08:00
sdkconfig.rename Rename Kconfig options (components/freertos) 2019-05-21 09:09:01 +02:00
stdint.readme Initial public version 2016-08-17 23:08:22 +08:00
tasks.c spin_lock: added new spinlock interface and decoupled it from RTOS 2020-01-22 06:20:34 +08:00
timers.c freertos: removed likely macros from non-port specifics parts of freertos 2020-01-03 14:42:57 -03:00
xt_asm_utils.h components/freertos: fixed typos and licence placement on external code 2019-12-04 10:39:22 -03:00
xtensa_context.S freertos: save/restore PS and EPC1 around window spilling 2019-12-27 11:27:01 +01:00
xtensa_init.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
xtensa_intr_asm.S Replace non-multicore-aware rom funcs with multicore-aware funcs, ESP_LOG->ESP_EARLY_LOG fix, reserve ints used in wireless libs. Fixes WiFi crashing 2016-12-13 13:24:01 +08:00
xtensa_intr.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
xtensa_overlay_os_hook.c Initial public version 2016-08-17 23:08:22 +08:00
xtensa_vector_defaults.S misc adjustment of esp32 component 2019-04-03 19:57:46 +08:00
xtensa_vectors.S all: Using xxx_periph.h 2019-06-03 14:15:08 +08:00