Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 29 May 2014 04:38:18 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Peter Jeremy <peter@rulingia.com>
Cc:        freebsd-amd64@freebsd.org
Subject:   Re: i386 Go programs crash on amd64
Message-ID:  <20140529013817.GF3991@kib.kiev.ua>
In-Reply-To: <20140528225542.GA46230@server.rulingia.com>
References:  <20140523225300.GA14433@server.rulingia.com> <537FD853.3000505@wemm.org> <537FD9A2.2010607@wemm.org> <20140523233444.GH74331@kib.kiev.ua> <20140524033944.GA96083@server.rulingia.com> <20140524074101.GI74331@kib.kiev.ua> <20140526113622.GB96083@server.rulingia.com> <20140526134457.GR74331@kib.kiev.ua> <20140528225542.GA46230@server.rulingia.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--Pgaa2uWPnPrfixyx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, May 29, 2014 at 08:55:42AM +1000, Peter Jeremy wrote:
> As I wrote in my initial mail, I am not certain whether this is a problem
> with Go or FreeBSD.  And having done some poking at corefiles with gdb
> (you need gdb7.6 from ports to grok the Go debug information), I have
> found that all my programs are dying at:
>     mov    %gs:0xfffffff8,%ecx
> but it seems to be timing related as to when the offending instruction
> is executed.  I will do some further investigation into how segment
> wraparound is handled.

Hm, I think I know what is going on.  Try this, please.
At least, your test binary worked for me.

diff --git a/sys/amd64/amd64/exception.S b/sys/amd64/amd64/exception.S
index d4e58b1..d51078d 100644
--- a/sys/amd64/amd64/exception.S
+++ b/sys/amd64/amd64/exception.S
@@ -721,21 +721,38 @@ ld_fsbase:
 	pushfq
 	cli
 	movl	$MSR_GSBASE,%ecx
+	/* Save current kernel %gs base into %r12d:%r13d */
 	rdmsr
+	movl	%eax,%r12d
+	movl	%edx,%r13d
 	.globl	ld_gs
 ld_gs:
 	movw	%si,%gs
+	/* Save user %gs base into %r14d:%r15d */
+	rdmsr
+	movl	%eax,%r14d
+	movl	%edx,%r15d
+	/* Restore kernel %gs base */
+	movl	%r12d,%eax
+	movl	%r13d,%edx
 	wrmsr
 	popfq
+	/*
+	 * Restore user %gs base, either from PCB if used for TLS, or
+	 * from the previously saved msr read.
+	 */
+	movl	$MSR_KGSBASE,%ecx
 	cmpw	$KUG32SEL,%si
 	jne	1f
-	movl	$MSR_KGSBASE,%ecx
 	movl	PCB_GSBASE(%r8),%eax
 	movl	PCB_GSBASE+4(%r8),%edx
+	jmp	ld_gsbase
+1:
+	movl	%r14d,%eax
+	movl	%r15d,%edx
 	.globl	ld_gsbase
 ld_gsbase:
-	wrmsr
-1:
+	wrmsr	/* May trap if non-canonical, but only for TLS. */
 	.globl	ld_es
 ld_es:
 	movw	TF_ES(%rsp),%es

--Pgaa2uWPnPrfixyx
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (FreeBSD)

iQIcBAEBAgAGBQJTho+JAAoJEJDCuSvBvK1B0DYP/0+tfp2ARb1YMsrwdwKEAUZ9
zwEXu0K7qsEnXXQGZlTpBFXpEexZSPQsVDOKsMNYSRWOHS4v38v6OlX3ha0UlTPV
jtwbGS5agxUy69p19e/NcUP893wzeVzQB5LW/5R11ERb3hlTj9xOGJNO99h3pE7E
xJ0Hc+BZ8HhRKr4yHMb3ETEy4sVyR+4xeYs7pu0BgQpQK0VtKd9UKDDtZzvGVulH
jydPjv3SPlw7HKeKdDTJMwcOpMCSRm4u0iRWklDL4snqtKB4Yg+BFeHJGKVctu/W
Pth8ESJKqiOa+1L1sKb7oQN5UVh4jmSSkVtEyPniVRLkNgr+XP2tA+/oGbg0ZnI7
EOfz1oD35vveW8cUNTZTmX/GB3mS8PS4yRQzcH34SNS1V1jk8O1db1DMnVjjDbNi
mFfpBC79OxtO8fygyOv6+qNignSDFPnKzmadWzyVZVg2Y9EcS/DKf5JC8b7tB1yf
MpYI4OeaPnx8lRxjokhoFoMzFwl6ManKWNLwZHm2x4YCha/2/POfN+TK5xPluJy/
P4fA/oc3KZgb7s3ykfytviktgZtiQLb1kzumDcGaYBYyVBPjqePn0zhoS9bBcd1l
ku3szAcE+GbdhYMeMViszW6wOgDsl1np2ZEvYd/6BshHFm4zrP3oyr65aYdTwOs4
EdtHdpOWvVTzyVuGhpg6
=XF5w
-----END PGP SIGNATURE-----

--Pgaa2uWPnPrfixyx--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140529013817.GF3991>