]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] runtime/cgo: call setg_gcc in crosscall_amd64
authorCherry Zhang <cherryyz@google.com>
Tue, 2 Feb 2021 22:26:57 +0000 (17:26 -0500)
committerCherry Zhang <cherryyz@google.com>
Fri, 5 Feb 2021 16:09:50 +0000 (16:09 +0000)
Currently, when using cgo, the g pointer is set via a separate
call to setg_gcc or with inline assembly in threadentry. This CL
changes it to call setg_gcc in crosscall_amd64, like other g-
register platforms. When we have an actual g register on AMD64,
we'll need to set the register immediately before calling into
Go.

Change-Id: Ib1171e05cd0dabba3b7d12e072084d141051cf3d
Reviewed-on: https://go-review.googlesource.com/c/go/+/289192
Trust: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/runtime/cgo/gcc_amd64.S
src/runtime/cgo/gcc_darwin_amd64.c
src/runtime/cgo/gcc_dragonfly_amd64.c
src/runtime/cgo/gcc_freebsd_amd64.c
src/runtime/cgo/gcc_linux_amd64.c
src/runtime/cgo/gcc_netbsd_amd64.c
src/runtime/cgo/gcc_openbsd_amd64.c
src/runtime/cgo/gcc_solaris_amd64.c
src/runtime/cgo/gcc_windows_amd64.c
src/runtime/cgo/libcgo.h

index 17d9d47ef40cbc7d3f4e641b012c5cd6d6fd63ab..d75f8646663ab497a7ef5bcea498291f7301772b 100644 (file)
@@ -30,9 +30,14 @@ EXT(crosscall_amd64):
        pushq %r15
 
 #if defined(_WIN64)
+       movq %r8, %rdi  /* arg of setg_gcc */
+       call *%rdx      /* setg_gcc */
        call *%rcx      /* fn */
 #else
-       call *%rdi      /* fn */
+       movq %rdi, %rbx
+       movq %rdx, %rdi /* arg of setg_gcc */
+       call *%rsi      /* setg_gcc */
+       call *%rbx      /* fn */
 #endif
 
        popq %r15
index 51410d50269dafd1d5ee4c34294d08b1984a48eb..d5b7fd8fd802bf21d734db635e2a5455fb83403f 100644 (file)
@@ -9,13 +9,16 @@
 #include "libcgo_unix.h"
 
 static void* threadentry(void*);
+static void (*setg_gcc)(void*);
 
 void
-x_cgo_init(G *g)
+x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
 {
        pthread_attr_t attr;
        size_t size;
 
+       setg_gcc = setg;
+
        pthread_attr_init(&attr);
        pthread_attr_getstacksize(&attr, &size);
        g->stacklo = (uintptr)&attr - size + 4096;
@@ -57,10 +60,6 @@ threadentry(void *v)
        ts = *(ThreadStart*)v;
        free(v);
 
-       // Move the g pointer into the slot reserved in thread local storage.
-       // Constant must match the one in cmd/link/internal/ld/sym.go.
-       asm volatile("movq %0, %%gs:0x30" :: "r"(ts.g));
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index d25db91900071aea15e9ceaa4c46a245f73bbb43..0003414bf8665aba986a510192f2e7918fc62a55 100644 (file)
@@ -61,11 +61,6 @@ threadentry(void *v)
        ts = *(ThreadStart*)v;
        free(v);
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index 514a2f8a235ee78c2c1f2e5e75e9ecc01ea49eac..6071ec39093783c469b99079dc770b0f19f896d7 100644 (file)
@@ -69,11 +69,6 @@ threadentry(void *v)
        free(v);
        _cgo_tsan_release();
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index f2bf6482cb5f77689e4446e8ee0a8a87410700df..c25e7e769ba23ddae032e510b1c062dd776f1f6c 100644 (file)
@@ -89,11 +89,6 @@ threadentry(void *v)
        free(v);
        _cgo_tsan_release();
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index dc966fc45b4ba4f3a9b723aace2de527297ea450..9f4b031a08954de8cba3ef6b63e6cd6ff17b0600 100644 (file)
@@ -62,11 +62,6 @@ threadentry(void *v)
        ts = *(ThreadStart*)v;
        free(v);
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
        // On NetBSD, a new thread inherits the signal stack of the
        // creating thread. That confuses minit, so we remove that
        // signal stack here before calling the regular mstart. It's
@@ -78,6 +73,6 @@ threadentry(void *v)
        ss.ss_flags = SS_DISABLE;
        sigaltstack(&ss, nil);
 
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index 34319fb0b80e3b642ba8f6ce5a51b8bef9609816..09d2750f3ad8f96c7b31d69262fed414381e4be9 100644 (file)
@@ -60,11 +60,6 @@ threadentry(void *v)
        ts = *(ThreadStart*)v;
        free(v);
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index 079bd12898e25652bc791cafd5acef38b6e8fc5a..e89e844b1e0ad30ba76d3af59695391e7ceb0eff 100644 (file)
@@ -72,11 +72,6 @@ threadentry(void *v)
        ts = *(ThreadStart*)v;
        free(v);
 
-       /*
-        * Set specific keys.
-        */
-       setg_gcc((void*)ts.g);
-
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
        return nil;
 }
index 0f8c817f0e4d138fd7d0efd396d26f16435bd6b3..25cfd086ddc3331997d59e7cb24a061b42d64e15 100644 (file)
 #include "libcgo_windows.h"
 
 static void threadentry(void*);
+static void (*setg_gcc)(void*);
 
 void
-x_cgo_init(G *g)
+x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
 {
+       setg_gcc = setg;
 }
 
 
@@ -46,10 +48,8 @@ threadentry(void *v)
         */
        asm volatile (
          "movq %0, %%gs:0x28\n"        // MOVL tls0, 0x28(GS)
-         "movq %%gs:0x28, %%rax\n" // MOVQ 0x28(GS), tmp
-         "movq %1, 0(%%rax)\n" // MOVQ g, 0(GS)
-         :: "r"(ts.tls), "r"(ts.g) : "%rax"
+         :: "r"(ts.tls)
        );
 
-       crosscall_amd64(ts.fn);
+       crosscall_amd64(ts.fn, setg_gcc, (void*)ts.g);
 }
index aba500a301c66a0c1864c3fd9c61ebfc56e21a5f..af4960e7e952206a0923d4495137bb8eafeac9b4 100644 (file)
@@ -66,7 +66,7 @@ uintptr_t _cgo_wait_runtime_init_done(void);
 /*
  * Call fn in the 6c world.
  */
-void crosscall_amd64(void (*fn)(void));
+void crosscall_amd64(void (*fn)(void), void (*setg_gcc)(void*), void *g);
 
 /*
  * Call fn in the 8c world.