From owner-freebsd-questions@FreeBSD.ORG Mon Oct 8 19:59:49 2007 Return-Path: Delivered-To: freebsd-questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 852BE16A420 for ; Mon, 8 Oct 2007 19:59:49 +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 446F313C468 for ; Mon, 8 Oct 2007 19:59:49 +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 l98JbBKg001756; Mon, 8 Oct 2007 15:37:11 -0400 (EDT) (envelope-from jkim@FreeBSD.org) From: Jung-uk Kim To: freebsd-emulation@FreeBSD.org Date: Mon, 8 Oct 2007 15:37:01 -0400 User-Agent: KMail/1.6.2 References: <200710082135.58099.mihai.dontu@gmail.com> In-Reply-To: <200710082135.58099.mihai.dontu@gmail.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <200710081537.03836.jkim@FreeBSD.org> X-Virus-Scanned: ClamAV 0.90.2/4507/Mon Oct 8 14:42:59 2007 on anuket.mj.niksun.com X-Virus-Status: Clean Cc: freebsd-questions@FreeBSD.org, Mihai =?utf-8?q?Don=C8=9Bu?= Subject: Re: amd64_set_gsbase() X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Oct 2007 19:59:49 -0000 On Monday 08 October 2007 02:35 pm, Mihai Donțu wrote: > Hi, > > I have a "small" amd64 program that makes havy use of LDT (%GS to > be more specific). The trouble is, in a multithreaded environment, > the selector value gets lost (or reset?). > > The code *always* segfaults with this stack: > 4 LWP 100126 0x0000000800dec07c in select () from /lib/libc.so.6 > * 3 Thread 0x517000 (runnable) 0x000000080055cfbc in ?? () > 2 Thread 0x517400 (LWP 100125) 0x0000000800c0d85c in > pthread_testcancel () from /lib/libpthread.so.2 1 Thread 0x517800 > (runnable) 0x0000000800d5d000 in makecontext () from > /lib/libc.so.6 > > at this instruction: > 0x000000080055cfbc: mov %gs:0x10,%r11 > > (gdb) p $gs > $1 = 0 > > I've been reading on the net something about the kernel not > preserving the GS across syscalls (or stmh). Is this true? and if > so, is there a known workaround? > > I'm on a FreeBSD 6.2-STABLE-200706 (AMD64) machine. Yes, you are correct. A short version is "don't do that". A long version goes like this. %fs and %gs are not preserved while context switching on amd64. In fact, you should not use amd64_set_gsbase() directly. If you *really* have to mess up with base addresses, you have to use sysarch(2) syscall, i.e., sysarch(AMD64_SET_GSBASE, args). However, it only changes the base address via MSR, i.e., %gs itself has no meaning. Jung-uk Kim