From owner-freebsd-threads@FreeBSD.ORG Tue Jun 20 22:58:37 2006 Return-Path: X-Original-To: freebsd-threads@freebsd.org Delivered-To: freebsd-threads@freebsd.org Received: from localhost.my.domain (localhost [127.0.0.1]) by hub.freebsd.org (Postfix) with ESMTP id 1994016A47A; Tue, 20 Jun 2006 22:58:37 +0000 (UTC) (envelope-from davidxu@freebsd.org) From: David Xu To: freebsd-threads@freebsd.org Date: Wed, 21 Jun 2006 06:58:24 +0800 User-Agent: KMail/1.8.2 References: <20060620120948.GA8288@stud.fit.vutbr.cz> In-Reply-To: <20060620120948.GA8288@stud.fit.vutbr.cz> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200606210658.24741.davidxu@freebsd.org> Cc: threads@freebsd.org, Divacky Roman , hackers@freebsd.org Subject: Re: TLS - implementing linux one in fbsd X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Jun 2006 22:58:37 -0000 On Tuesday 20 June 2006 20:09, Divacky Roman wrote: > 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. > Yes. > 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 > The M:N and 1:1 threading in FreeBSD use different mechanisms to implement TLS, M:N implements it in userland, while 1:1 implements it in kernel. the thr_new or thr_create are used for 1:1 threading, right now libthr uses thr_new to atomically setup a thread, this includes, storing TID, setting TLS, and maybe signal mask( not implemented ) , cpu affinity mask etcs(not implemented), scheduling scope, in one word, it is intended to map most part of pthread_attr into kernel world. > 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?) > If you use thr_new, it is not necessary to use set_thread_area, I am not sure you need to change TLS pointer again after the thread is created, I think only main thread may need this feature, in FreeBSD, setting thread's TLS pointer is via libc function: _set_tp(void *tp). > 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? > Dunno. > 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 > > I think it is used for futex, and the childtid is use to implement pthread_join and garbage collection in thread library, the parent tid pointer (if I recall correctly) is used by parent thread to retrieve child tid. > 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. cpu_set_user_tls is used by thr_new syscall internally to setup TLS pointer before executing user code, the thr_new syscall's only user is libthr.