From owner-freebsd-emulation@FreeBSD.ORG Fri Jan 26 23:21:15 2007 Return-Path: X-Original-To: freebsd-emulation@FreeBSD.org Delivered-To: freebsd-emulation@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id CBAB016A403 for ; Fri, 26 Jan 2007 23:21:15 +0000 (UTC) (envelope-from jkim@FreeBSD.org) Received: from anuket.mj.niksun.com (gwnew.niksun.com [65.115.46.162]) by mx1.freebsd.org (Postfix) with ESMTP id 8F7C613C4A3 for ; Fri, 26 Jan 2007 23:21:15 +0000 (UTC) (envelope-from jkim@FreeBSD.org) Received: from niksun.com (anuket [10.70.0.5]) by anuket.mj.niksun.com (8.13.6/8.13.6) with ESMTP id l0QNLEXN011972; Fri, 26 Jan 2007 18:21:14 -0500 (EST) (envelope-from jkim@FreeBSD.org) From: Jung-uk Kim To: freebsd-amd64@FreeBSD.org Date: Fri, 26 Jan 2007 18:21:09 -0500 User-Agent: KMail/1.6.2 MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200701261821.12274.jkim@FreeBSD.org> X-Virus-Scanned: ClamAV 0.88.6/2493/Fri Jan 26 07:00:46 2007 on anuket.mj.niksun.com X-Virus-Status: Clean Cc: freebsd-emulation@FreeBSD.org Subject: load_fs() and load_gs() X-BeenThere: freebsd-emulation@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Development of Emulators of other operating systems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Jan 2007 23:21:15 -0000 I have been chasing TLS problem for Linuxulator/amd64. The whole thing actually boils down to the following simulation: ---------------- #include #include #include #include static __thread u_int tls = 0xdeadbeef; int main(void) { #if defined(__amd64__) u_int fs; uint64_t fsbase; fs = rfs(); if (sysarch(AMD64_GET_FSBASE, &fsbase)) return (-1); printf("fsbase = 0x%lx, %%fs: 0x%08x, tls = 0x%x\n", fsbase, fs, tls); /* * glibc does the following two calls. * Note: Actually we don't do anything here * but writing them back. */ if (sysarch(AMD64_SET_FSBASE, &fsbase)) return (-1); load_fs(fs); if (sysarch(AMD64_GET_FSBASE, &fsbase)) return (-1); printf("fsbase = 0x%lx, %%fs: 0x%08x, tls = 0x%x\n", fsbase, rfs(), tls); #elif defined(__i386__) u_int gs; uint32_t gsbase; gs = rgs(); if (sysarch(I386_GET_GSBASE, &gsbase)) return (-1); printf("gsbase = 0x%lx, %%gs: 0x%08x, tls = 0x%x\n", gsbase, gs, tls); /* * glibc does the following two calls. * Note: Actually we don't do anything here * but writing them back. */ if (sysarch(I386_SET_GSBASE, &gsbase)) return (-1); load_gs(gs); if (sysarch(I386_GET_GSBASE, &gsbase)) return (-1); printf("gsbase = 0x%lx, %%gs: 0x%08x, tls = 0x%x\n", gsbase, rgs(), tls); #endif return (0); } ---------------- If you run it on amd64 (both amd64 and i386 binaries), it segfaults at: mov %fs:0x0,%rax (amd64) or mov %gs:0x0,%eax (i386) which is basically reading tls. Why does it segfaults when we just read and write them back? Can anyone enlighten me? Thanks, Jung-uk Kim