From owner-p4-projects@FreeBSD.ORG Fri Aug 13 22:07:45 2004 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 0F08716A4D0; Fri, 13 Aug 2004 22:07:45 +0000 (GMT) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id DCC7716A4CE for ; Fri, 13 Aug 2004 22:07:44 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id B623343D2D for ; Fri, 13 Aug 2004 22:07:44 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.12.11/8.12.11) with ESMTP id i7DM7imA006467 for ; Fri, 13 Aug 2004 22:07:44 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.12.11/8.12.11/Submit) id i7DM7iHt006464 for perforce@freebsd.org; Fri, 13 Aug 2004 22:07:44 GMT (envelope-from davidxu@freebsd.org) Date: Fri, 13 Aug 2004 22:07:44 GMT Message-Id: <200408132207.i7DM7iHt006464@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to davidxu@freebsd.org using -f From: David Xu To: Perforce Change Reviews Subject: PERFORCE change 59595 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Aug 2004 22:07:45 -0000 http://perforce.freebsd.org/chv.cgi?CH=59595 Change 59595 by davidxu@davidxu_alona on 2004/08/13 22:07:09 Fix ldt entry leak bug, remember ldt index, so we can free idt entry in retire_thread. Allow a thread allocated a ltd entry to set its gs. Affected files ... .. //depot/projects/davidxu_ksedbg/src/lib/libthr/arch/i386/i386/_setcurthread.c#2 edit Differences ... ==== //depot/projects/davidxu_ksedbg/src/lib/libthr/arch/i386/i386/_setcurthread.c#2 (text+ko) ==== @@ -41,12 +41,15 @@ #define MAXTHR 8192 -#define LDT_INDEX(x) (((long)(x) - (long)ldt_entries) / sizeof(ldt_entries[0])) +struct ldt_entry { + void *ptr; + int ldt; +}; -void **ldt_free = NULL; -void *ldt_entries[MAXTHR]; -static int ldt_inited = 0; -static spinlock_t ldt_lock = _SPINLOCK_INITIALIZER; +struct ldt_entry *ldt_free = NULL; +struct ldt_entry ldt_entries[MAXTHR]; +static int ldt_inited = 0; +static spinlock_t ldt_lock = _SPINLOCK_INITIALIZER; static void ldt_init(void); @@ -62,25 +65,28 @@ { int i; - ldt_free = &ldt_entries[NLDT]; - - for (i = 0; i < MAXTHR - 1; i++) - ldt_entries[i] = (void *)&ldt_entries[i + 1]; - - ldt_entries[MAXTHR - 1] = NULL; - + ldt_free = &ldt_entries[0]; + for (i = 0; i < MAXTHR - 1; i++) { + ldt_entries[i].ptr = &ldt_entries[i + 1]; + ldt_entries[i].ldt = -1; + } + ldt_entries[MAXTHR - 1].ptr = NULL; + ldt_entries[MAXTHR - 1].ldt = -1; ldt_inited = 1; } void _retire_thread(void *entry) { + struct ldt_entry *le = (struct ldt_entry *)entry; + _spinlock(&ldt_lock); - if (ldt_free == NULL) - *(void **)entry = NULL; - else - *(void **)entry = *ldt_free; - ldt_free = entry; + le->ptr = ldt_free; + ldt_free = le; + if (le->ldt != -1) { + i386_set_ldt(le->ldt, NULL, 1); + le->ldt = -1; + } _spinunlock(&ldt_lock); } @@ -88,11 +94,16 @@ _set_curthread(ucontext_t *uc, struct pthread *thr, int *err) { union descriptor desc; - void **ldt_entry; + struct ldt_entry *ldt_entry; int ldt_index; *err = 0; + if (thr->arch_id != NULL) { + ldt_entry = thr->arch_id; + ldt_index = ldt_entry->ldt; + goto set_seg; + } /* * If we are setting up the initial thread, the gs register * won't be setup for the current thread. In any case, we @@ -117,7 +128,7 @@ * Pull one off of the free list and update the free list pointer. */ ldt_entry = ldt_free; - ldt_free = (void **)*ldt_entry; + ldt_free = ldt_entry->ptr; if (thr != _thread_initial) _SPINUNLOCK(&ldt_lock); @@ -126,7 +137,7 @@ * Cache the address of the thread structure here. This is * what the gs register will point to. */ - *ldt_entry = (void *)thr; + ldt_entry->ptr = (void *)thr; bzero(&desc, sizeof(desc)); @@ -149,7 +160,9 @@ ldt_index = i386_set_ldt(LDT_AUTO_ALLOC, &desc, 1); if (ldt_index == -1) abort(); + ldt_entry->ldt = ldt_index; +set_seg: /* * Set up our gs with the index into the ldt for this entry. */