Re: How TLS is used in Kernel thread



On Thu, 2007-08-30 at 14:20 -0700, Jin Guojun wrote:
By looking through _pthread_create() code and find it uses a magic
cookie -- TLS -- created
by rtld_allocate_tls(), and passed into kernel by sysarch() via
_tcb_set() / _kcb_set().

The information seems to be set by rtld (ld-elf.so.1) in digest_phdr()
under tag PT_TLS.
But it is very magic for where the TLS object is created and how it is
passed to digest_phdr().

The whole object passed into kernel (as sd.gsbase) looks like this:

TCB: ______________________________
| TLS | TCB |
|______________|_______________|

Can someone give some basic exaplain on following questions?

1) What TLS stand for?

Its used to implement Thread-Local-Storage variables. These are global
variables declared with the '__thread' keyword. Each variable declared
in this way has a per-thread value (i.e. each thread has a private copy
of the variable).

2) Where TLS object is created? (below is the tls assigned, but I couls
not find where ph is from)
case PT_TLS:
obj->tlsindex = 1;
obj->tlssize = ph->p_memsz;
obj->tlsalign = ph->p_align;
obj->tlsinitsize = ph->p_filesz;
obj->tlsinit = (void*) ph->p_vaddr;

Most of the work of dealing with TLS happens in the runtime
linker /libexec/ld-elf.so.1. The runtime linker calculates the TLS size
based on the TLS usage of all loaded libraries. The thread library uses
an internal interface to rtld (_rtld_allocate_tls and _rtld_free_tls)
during thread creation and destruction to allocate and free the TLS
blocks. As you have seen, the memory allocated also includes the
thread-library's control structures (which includes the saved register
set etc.).

Static programs have a slightly simpler mechanism (simpler because there
is only the main program and no dynamic libraries). This is handled in
src/lib/libc/gen/tls.c.


3) Where in kernel the TLS is used for thread operation?

The kernel doesn't use this information at all. It simply provides
support for the thread libraries needs.


_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Assignment of GDT entries
    ... aware of segments, for a rather perverse reason: the TLS stuff. ... Or is it only used in kernel code? ... and obviously we'd like to keep them in the same cacheline too. ...
    (Linux-Kernel)
  • Re: TLS - implementing linux one in fbsd
    ... implement TLS, M:N implements it in userland, while 1:1 implements it in kernel. ... not have to set TLS via syscall later. ... those syscalls to not to setup TLS because linux wants to set it ...
    (freebsd-hackers)
  • Re: Assignment of GDT entries
    ... user code segment number is the same as the kernel code segment +2 (ie ... Or is it only used in kernel code? ... and obviously we'd like to keep them in the same cacheline too. ... - TLS #1 ...
    (Linux-Kernel)
  • Re: how to implement Thread Local Storage (TLS) in kernel
    ... there is no concept of TLS in kernel. ... table on the side using the current thread ID + current IRQL as the lookup, ... TLS doesn't make much sense. ... Please do not send e-mail directly to this alias. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Nvidia, TLS and __thread keyword -- an observation
    ... our thread library needs to create the TLS as defined in the ... there is no reason that all our thread libraries cannot ... I might add that we don't need to follow the spec exactly. ... The spec requires that %gs:0 is the address of a pointer to ...
    (freebsd-current)