diff options
| author | qmuntal <quimmuntal@gmail.com> | 2022-09-19 12:19:38 +0200 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2022-11-14 20:43:12 +0000 |
| commit | da564d0006e2cc286fecb3cec94ed143a2667866 (patch) | |
| tree | 1601b9e73eec4bd72c041e368be4a8f1d5143e55 /src/runtime/sys_windows_amd64.s | |
| parent | 0f0aa5d8a6a0253627d58b3aa083b24a1091933f (diff) | |
| download | go-da564d0006e2cc286fecb3cec94ed143a2667866.tar.xz | |
runtime,cmd/internal/obj/x86: use TEB TLS slots on windows/amd64
This CL redesign how we get the TLS pointer on windows/amd64.
We were previously reading it from the [TEB] arbitrary data slot,
located at 0x28(GS), which can only hold 1 TLS pointer.
With this CL, we will read the TLS pointer from the TEB TLS slot array,
located at 0x1480(GS). The TLS slot array can hold multiple
TLS pointers, up to 64, so multiple Go runtimes running on the
same thread can coexists with different TLS.
Each new TLS slot has to be allocated via [TlsAlloc],
which returns the slot index. This index can then be used to get the
slot offset from GS with the following formula: 0x1480 + index*8
The slot index is fixed per Go runtime, so we can store it
in runtime.tls_g and use it latter on to read/update the TLS pointer.
Loading the TLS pointer requires the following asm instructions:
MOVQ runtime.tls_g, AX
MOVQ AX(GS), AX
Notice that this approach is also implemented on windows/arm64.
[TEB]: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block
[TlsAlloc]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc
Updates #22192
Change-Id: Idea7119fd76a3cd083979a4d57ed64b552fa101b
Reviewed-on: https://go-review.googlesource.com/c/go/+/431775
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Diffstat (limited to 'src/runtime/sys_windows_amd64.s')
| -rw-r--r-- | src/runtime/sys_windows_amd64.s | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/src/runtime/sys_windows_amd64.s b/src/runtime/sys_windows_amd64.s index 4e00f64fae..777726f7c1 100644 --- a/src/runtime/sys_windows_amd64.s +++ b/src/runtime/sys_windows_amd64.s @@ -8,6 +8,9 @@ #include "time_windows.h" #include "cgo/abi_amd64.h" +// Offsets into Thread Environment Block (pointer in GS) +#define TEB_TlsSlots 0x1480 + // void runtime·asmstdcall(void *c); TEXT runtime·asmstdcall(SB),NOSPLIT|NOFRAME,$0 // asmcgocall will put first argument into CX. @@ -303,10 +306,10 @@ TEXT runtime·tstart_stdcall(SB),NOSPLIT,$0 MOVQ AX, g_stackguard1(DX) // Set up tls. - LEAQ m_tls(CX), SI - MOVQ SI, 0x28(GS) + LEAQ m_tls(CX), DI MOVQ CX, g_m(DX) - MOVQ DX, g(SI) + MOVQ DX, g(DI) + CALL runtime·settls(SB) // clobbers CX CALL runtime·stackcheck(SB) // clobbers AX,CX CALL runtime·mstart(SB) @@ -318,7 +321,8 @@ TEXT runtime·tstart_stdcall(SB),NOSPLIT,$0 // set tls base to DI TEXT runtime·settls(SB),NOSPLIT,$0 - MOVQ DI, 0x28(GS) + MOVQ runtime·tls_g(SB), CX + MOVQ DI, 0(CX)(GS) RET // Runs on OS stack. @@ -404,3 +408,32 @@ TEXT runtime·osSetupTLS(SB),NOSPLIT,$0-8 LEAQ m_tls(AX), DI CALL runtime·settls(SB) RET + +// This is called from rt0_go, which runs on the system stack +// using the initial stack allocated by the OS. +TEXT runtime·wintls(SB),NOSPLIT|NOFRAME,$0 + // Allocate a TLS slot to hold g across calls to external code + MOVQ SP, AX + ANDQ $~15, SP // alignment as per Windows requirement + SUBQ $48, SP // room for SP and 4 args as per Windows requirement + // plus one extra word to keep stack 16 bytes aligned + MOVQ AX, 32(SP) + MOVQ runtime·_TlsAlloc(SB), AX + CALL AX + MOVQ 32(SP), SP + + MOVQ AX, CX // TLS index + + // Assert that slot is less than 64 so we can use _TEB->TlsSlots + CMPQ CX, $64 + JB ok + CALL runtime·abort(SB) +ok: + // Convert the TLS index at CX into + // an offset from TEB_TlsSlots. + SHLQ $3, CX + + // Save offset from TLS into tls_g. + ADDQ $TEB_TlsSlots, CX + MOVQ CX, runtime·tls_g(SB) + RET |
