TLS - implementing linux one in fbsd

Divacky Roman xdivac02 at stud.fit.vutbr.cz
Tue Jun 20 12:10:01 UTC 2006


Hi

I am student working on SoC project - extending linuxolator, now I am working
on implementing linux TLS in FreeBSD. Here is what I think/know and I like you
to comment on this, thnx. Roman

-------------------------------------------------

Linux and FreeBSD TLS implementation - comparison

Both systems use per-thread setting of where is the tls area stored. This setting is loaded into active threads
GDT and can be accessed via %gs register. This GDT setup is done on every context switch.

Linux uses strict 1:1 threading so every thread is in fact process, so thread creation is done using plain
clone()/fork(). FreeBSD uses M:N (including 1:1) threading. Threads are created via pthread_create() call to
threading library. In kernel there's thr_new() syscall or thread_create() syscall. I didnt find the
connection between threading library and kernel but I assume its using one of the syscalls 

For setting up the GDT for the thread Linux uses syscall set_thread_area() (TODO - how exactly? its unclear what
it does). I dont know how FreeBSD does it but I think it might be done via params to the syscalls (TODO - how is
it done?)

Remaining questions:

clone() - 2.6.x glibc fork() implementation uses clone() syscall. is it supposed to create a thread or just a
process? I think its process but why is the binary (ls, date and probably some other) linked to pthread library?
is it just Linux "strangeness"? I dont see a reason for ls to be threaded... does anyone see?

set/get tid - does it relate to TLS at all? I dont think so but you never know. The tid thing is unclear to me.
The clone() syscall is passed CLONE_CHILD_SETTID & CLONE_CHILD_CLEARTID which should be mutually exclusive. I dont
believe much its a mistake.. but the code is clear:
 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr: NULL;

kostik belousov pointed out that this is used for futexes, so not interesting for this

 
Possible mapping from Linux to FreeBSD:

To me it seems that the the set_thread_area() syscall is used in the process of thread creation to set where the
tls is stored. In FreeBSD we use cpu_set_user_tls() for this. So it might be enough to just wrap call to
cpu_set_user_tls() into the syscall.


More information about the freebsd-threads mailing list