наиболее часто локальная память потока используется для организации обработки исключений (to track exception handlers)
а так же хранения значений переменной errno.

http://www.open-std.org/jtc1/sc22/wg21/ … n4324.html
(тут ещё про статистику говорится, но мы не в ядре, поэтому обработка исключений кажется более важным и интересным сценарием использования)

https://stackoverflow.com/questions/518 … nux-kernel

https://lwn.net/Articles/5851/

proper TLS support in compilers (and glibc/pthreads) is a bit problematic on the x86 platform.
There's only 8 general purpose registers available, so on x86 we have to use segments to access the TLS.
...
a per-CPU GDT
...
the kernel keeps a specific per-thread GDT entry that can be set up and modified by each thread the kernel, upon context-switch, modifies this GDT entry to match that of the thread's TLS setting. This way user-space threaded code can access per-thread data via this descriptor - by using the same, constant %gs (or %fs) selector.

asmlinkage int sys_set_thread_area(unsigned int base, unsigned int limit, unsigned int flags)

http://people.redhat.com/drepper/tls.pdf
the address of the thread local storage region is stored in the GS segment register (FS in x86-64), so to access TLS you just do mov eax, GS:ptr.

Отредактировано Лис (2019-03-20 08:24:21)