From owner-freebsd-threads@FreeBSD.ORG Sun Sep 2 08:33:29 2007 Return-Path: Delivered-To: threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C27D816A419; Sun, 2 Sep 2007 08:33:29 +0000 (UTC) (envelope-from dfr@rabson.org) Received: from itchy.rabson.org (unknown [IPv6:2001:618:400::50b1:e8f2]) by mx1.freebsd.org (Postfix) with ESMTP id 2206B13C468; Sun, 2 Sep 2007 08:33:28 +0000 (UTC) (envelope-from dfr@rabson.org) Received: from [80.177.232.250] (herring.rabson.org [80.177.232.250]) by itchy.rabson.org (8.13.3/8.13.3) with ESMTP id l828XP7W018386; Sun, 2 Sep 2007 09:33:26 +0100 (BST) (envelope-from dfr@rabson.org) From: Doug Rabson To: Jin Guojun In-Reply-To: <46D734A1.2090700@george.lbl.gov> References: <46B245D5.1050606@george.lbl.gov> <20070803090530.GH2738@deviant.kiev.zoral.com.ua> <46D734A1.2090700@george.lbl.gov> Content-Type: text/plain Date: Sun, 02 Sep 2007 09:33:25 +0100 Message-Id: <1188722005.1058.32.camel@herring.rabson.org> Mime-Version: 1.0 X-Mailer: Evolution 2.10.2 FreeBSD GNOME Team Port Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV 0.87.1/4128/Sun Sep 2 06:54:56 2007 on itchy.rabson.org X-Virus-Status: Clean Cc: threads@freebsd.org, hackers@freebsd.org Subject: Re: How TLS is used in Kernel thread 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: Sun, 02 Sep 2007 08:33:29 -0000 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.