[−][src]Module sunrise_kernel::process::thread_local_storage
TLS manager
Abstract
For each thread of a process, the kernel allocates a 0x200-bytes "Thread Local Storage" memory region in UserLand. In this region resides the 0x100-bytes IPC command buffer, which is used by the user for passing IPC arguments, and a pointer to the user-controlled "thread context", which will likely be used for holding userspace thread local variables.
Each thread in a process has its own private TLS, and from userspace its address can be found out
at anytime by reading an architecture-specific register (aarch64 uses tpidrro_el0
, x86 uses the
gs
segment selector).
Location
The TLS content is defined by the TLS structure. It is a 0x200-bytes memory area that leaves in UserLand so it can be accessed and modified by the user. The user is allowed to access and modify the TLS of other thread from its process if it manages to find the location of their TLS, but this is not advised, as it serves little purpose.
Kernel-side, each thread holds a raw pointer to its TLS (*mut TLS
) in its ThreadStruct.
This pointer is used by the kernel to get the thread's ipc_command_buffer
address,
and is restored as part of hardware context on every context-switch.
Allocation
Each process holds a TLSManager in its ProcessStruct, which manages the TLSs for this process, keeps track of which ones are in-use and which ones are free, and try to re-use free TLSs when spawning a thread.
When a thread is being created, it asks its process's TLSManager
via allocate_tls to get a pointer
to its TLS, and saves it in the ThreadStruct
.
When a thread dies, it notifies its process's TLSManager
via free_tls, so its TLS can be re-used.
TLSs are only 0x200 bytes, so the TLSManager
groups them together to fit inside a page,
and will allocate a new page every time it is full and cannot satisfy a TLS allocation.
Structs
TLSManager | TLS allocator |
TLSPage | Manages a page containing 8 TLS |