From owner-freebsd-emulation@FreeBSD.ORG Mon Oct 8 22:01:11 2007 Return-Path: Delivered-To: freebsd-emulation@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9A32F16A469 for ; Mon, 8 Oct 2007 22:01:11 +0000 (UTC) (envelope-from mihai.dontu@gmail.com) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.190]) by mx1.freebsd.org (Postfix) with ESMTP id 2CC4513C44B for ; Mon, 8 Oct 2007 22:01:10 +0000 (UTC) (envelope-from mihai.dontu@gmail.com) Received: by nf-out-0910.google.com with SMTP id b2so1084394nfb for ; Mon, 08 Oct 2007 15:01:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:from:organization:to:subject:date:user-agent:cc:references:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:message-id; bh=SVQakLXrswognxq9/qXm/VvVzrdz2Q/BG1yWb2BdyiE=; b=RyxXWNjJyeRIJUwgIeJ4g2qNSCb/cmWKhVQdIrFl6XNWR6DQ9u/nFsGhFpg2WaB6eCa+OslQdI6yAWJ8Wy/KtPvhzEERg6U/MExZNyFAEpfSIlI6gooyn6+nAez015jekUcFPaw2i9+xqn6xKPTSmOh/PzfJgBrPnKowrRJbysw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:from:organization:to:subject:date:user-agent:cc:references:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:message-id; b=uXIHjFfG9bjcFZpGrcFNDB2M07K2+ZiBNly3poFctd9YeOMr5B0VCW7eWftrb7u95fzgvmj94r732gl0zmvtqaZpjxAwAVMz3XMxLzst0EvgAeI5JmicJDmTYVctHEwN8VkkBcI8RXiIVVISQsnUCLySXFVGtIbZNwZj3PPYBh4= Received: by 10.86.76.16 with SMTP id y16mr5439388fga.1191880869281; Mon, 08 Oct 2007 15:01:09 -0700 (PDT) Received: from ?192.168.0.3? ( [77.81.70.38]) by mx.google.com with ESMTPS id b17sm7239712fka.2007.10.08.15.01.06 (version=SSLv3 cipher=OTHER); Mon, 08 Oct 2007 15:01:07 -0700 (PDT) From: Mihai =?utf-8?q?Don=C8=9Bu?= Organization: Home To: Jung-uk Kim Date: Tue, 9 Oct 2007 01:00:58 +0300 User-Agent: KMail/1.9.7 References: <200710082135.58099.mihai.dontu@gmail.com> <200710081537.03836.jkim@FreeBSD.org> In-Reply-To: <200710081537.03836.jkim@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Disposition: inline Message-Id: <200710090100.58577.mihai.dontu@gmail.com> Cc: freebsd-emulation@freebsd.org, freebsd-questions@freebsd.org Subject: Re: amd64_set_gsbase() 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: Mon, 08 Oct 2007 22:01:11 -0000 On Monday 08 October 2007, Jung-uk Kim wrote: > 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. But this makes emulation software such as Wine a lost hope, doesn't it? Because Windows apps access the Thread Information Block (TIB) via %gs (%fs on ia32). Anyway, my so called "small" program is actually a Win64 emulator and I need the segment selector to "stay put" across syscalls. It works like a charm on single threaded apps, but as soon as I spawn a thread, all hell breaks loose :) I've managed to come up with something that *kind of* works. It goes like this: void my_handler( int s ) { if ( s == SIGSEGV ) { if ( get_gs() == 0 ) { amd64_set_gsbase(); } else { signal( SIGSEGV, SIG_DFL ); } } } int my_init( void ) { /* alloc TIB memory and initialize */ amd64_set_gsbase( lpTIB ); signal( SIGSEGV, my_handler ); return 0; } but after a series of dlopen()-s, my_handler() is called without %gs being zero and without a valid fault (the handler does not get recalled after signal( SIGSEGV, SIG_DFL ). I'm still working on this aspect ... > 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). I found this: /usr/src/lib/libc/amd64/sys/amd64_set_gsbase.c:32 " int amd64_set_gsbase(void *addr) { return (sysarch(AMD64_SET_GSBASE, &addr)); } " and this (man 2 sysarch()): "The sysarch() system call should never be called directly by user programs. Instead, they should access its functions using the architecture-dependent library." Who am I suppose to believe? :) > However, it only changes the base address via MSR, i.e., %gs > itself has no meaning. Maybe, but the selector loaded in %gs *does* have meaning. Anyway, the thing is I _have_ to make this work. I'll keep you posted ;) -- Mihai Donțu