From owner-freebsd-threads@FreeBSD.ORG Mon Mar 29 11:00:25 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D89B316A4CF for ; Mon, 29 Mar 2004 11:00:25 -0800 (PST) Received: from herring.rabson.org (mailgate.nlsystems.com [80.177.232.242]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2208243D1D for ; Mon, 29 Mar 2004 11:00:25 -0800 (PST) (envelope-from dfr@nlsystems.com) Received: from herring.rabson.org (herring.rabson.org [10.0.0.2]) by herring.rabson.org (8.12.11/8.12.11) with ESMTP id i2TJ0Etp011221 for ; Mon, 29 Mar 2004 20:00:14 +0100 (BST) (envelope-from dfr@nlsystems.com) From: Doug Rabson To: freebsd-threads@freebsd.org Date: Mon, 29 Mar 2004 20:00:13 +0100 User-Agent: KMail/1.6.1 MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <200403292000.13794.dfr@nlsystems.com> X-Spam-Status: No, hits=0.0 required=5.0 tests=none autolearn=no version=2.63 X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on herring.rabson.org X-Virus-Scanned: ClamAV version 'clamd / ClamAV version 0.65', clamav-milter version '0.60p' Subject: Thread Local Storage X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Mar 2004 19:00:26 -0000 I've been spending a bit of time recently familiarising myself with this TLS stuff and trying out a few things. I've been playing with rtld and I have a prototype patch which implements enough TLS support to let a non-threaded program which uses static TLS work. With a tiny bit more work I can have limited support for dynamic TLS as well (not for dlopen'ed modules yet though). Is there a p4 tree for this stuff yet? I'd like to check in what I have sometime. I've also been looking at libpthread and I can see some potential problems with it. Currently libpthread on i386 uses %gs to point at a struct kcb which seems to be a per-kse structure. This structure contains a pointer to a per-thread struct tcb and this pointer is managed by the userland context switch code. Other arches are similar, e.g. ia64 uses $tp to point at struct kcb. The problem with TLS is that the i386 ABI needs %gs to point at the TLS storage for the current thread (its a tiny bit more involved than that but that doesn't matter much for the purposed of this discussion). This leads to trouble since it looks like we will end up needing to allocate an LDT segment per thread, leading to an arbitrary limit on the number of threads (~8192). I can think of a couple of possible ways to get around this. One easy way would be to allocate a segment per KSE and call i386_set_ldt from the thread switch. Pretty ugly really and takes a syscall. Another slightly better way would be to lazy-allocate segments when we switch threads and reclaim segments from threads which haven't run recently. This technique would be able to get away with a smaller number of segments which tend to be owned by the threads which run most often. There is a similar issue with libthr but since it already allocates an LDT entry per thread there are no new limitations. Linux has an interesting wrinkle on the libthr solution - they have a GDT per cpu and they pre-allocate three GDT slots for TLS pointers (one for glibc, one for Wine and one spare). The kernel thread switching code fills in these GDT slots on the current cpu with values stored in the pcb-equivalent.