OVMS3-idf/components/freertos/test
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
..
CMakeLists.txt ut: Move tests back from "esp32" subfolder 2020-01-06 17:13:53 +08:00
component.mk ut: Move tests back from "esp32" subfolder 2020-01-06 17:13:53 +08:00
test_freertos_backported_functions.c Fix remaining -Wstrict-prototypes warnings 2019-08-01 16:28:56 +07:00
test_freertos_debug_functions.c unity: separate common and IDF specific functionality 2018-11-19 12:36:31 +08:00
test_freertos_eventgroups.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test_freertos_get_state.c unity: separate common and IDF specific functionality 2018-11-19 12:36:31 +08:00
test_freertos_isinisrcontext.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_freertos_mutex.c freertos: re-enable mutext test for esp32s2beta 2019-12-16 11:55:32 +05:30
test_freertos_scheduling_time.c components/freertos: using the optimized task selection on esp32s2beta 2020-01-14 09:58:14 -03:00
test_freertos_task_delay_until.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test_freertos_task_delete.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_freertos_task_notify.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test_freertos_trace_utilities.c unity: separate common and IDF specific functionality 2018-11-19 12:36:31 +08:00
test_isr_latency.c freertos/tests: fixed wrong header file on isr latency test 2019-12-04 10:41:00 -03:00
test_newlib_reent.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_panic.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_preemption.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_queuesets.c freertos: Add critical sections to queue sets. 2018-07-13 13:58:54 +08:00
test_spinlocks.c freertos: pass unit tests compilation 2019-06-12 17:13:29 +08:00
test_suspend_scheduler.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test_task_priorities.c unity: separate common and IDF specific functionality 2018-11-19 12:36:31 +08:00
test_task_suspend_resume.c global: rename esp32s2beta to esp32s2 2020-01-22 12:14:38 +08:00
test_tasks_snapshot.c test: Fix some unused identifier warnings 2018-12-10 12:34:16 +08:00
test_thread_local.c esp32s2: fix THREADPTR calculation, re-enable FreeRTOS TLS tests 2020-01-23 11:29:22 +01:00
test_timers.c Unit tests: Add FreeRTOS timer tests 2017-05-05 14:38:15 +10:00