github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/tls_arm.s (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !windows 6 7 #include "go_asm.h" 8 #include "go_tls.h" 9 #include "funcdata.h" 10 #include "textflag.h" 11 12 // We have to resort to TLS variable to save g(R10). 13 // One reason is that external code might trigger 14 // SIGSEGV, and our runtime.sigtramp don't even know we 15 // are in external code, and will continue to use R10, 16 // this might as well result in another SIGSEGV. 17 // Note: both functions will clobber R0 and R11 and 18 // can be called from 5c ABI code. 19 20 // On android, runtime.tls_g is a normal variable. 21 // TLS offset is computed in x_cgo_inittls. 22 #ifdef GOOS_android 23 #define TLSG_IS_VARIABLE 24 #endif 25 26 // save_g saves the g register into pthread-provided 27 // thread-local memory, so that we can call externally compiled 28 // ARM code that will overwrite those registers. 29 // NOTE: runtime.gogo assumes that R1 is preserved by this function. 30 // runtime.mcall assumes this function only clobbers R0 and R11. 31 // Returns with g in R0. 32 TEXT runtime·save_g(SB),NOSPLIT,$0 33 // If the host does not support MRC the linker will replace it with 34 // a call to runtime.read_tls_fallback which jumps to __kuser_get_tls. 35 // The replacement function saves LR in R11 over the call to read_tls_fallback. 36 // To make stack unwinding work, this function should NOT be marked as NOFRAME, 37 // as it may contain a call, which clobbers LR even just temporarily. 38 MRC 15, 0, R0, C13, C0, 3 // fetch TLS base pointer 39 BIC $3, R0 // Darwin/ARM might return unaligned pointer 40 MOVW runtime·tls_g(SB), R11 41 ADD R11, R0 42 MOVW g, 0(R0) 43 MOVW g, R0 // preserve R0 across call to setg<> 44 RET 45 46 // load_g loads the g register from pthread-provided 47 // thread-local memory, for use after calling externally compiled 48 // ARM code that overwrote those registers. 49 TEXT runtime·load_g(SB),NOSPLIT,$0 50 // See save_g 51 MRC 15, 0, R0, C13, C0, 3 // fetch TLS base pointer 52 BIC $3, R0 // Darwin/ARM might return unaligned pointer 53 MOVW runtime·tls_g(SB), R11 54 ADD R11, R0 55 MOVW 0(R0), g 56 RET 57 58 // This is called from rt0_go, which runs on the system stack 59 // using the initial stack allocated by the OS. 60 // It calls back into standard C using the BL (R4) below. 61 // To do that, the stack pointer must be 8-byte-aligned 62 // on some systems, notably FreeBSD. 63 // The ARM ABI says the stack pointer must be 8-byte-aligned 64 // on entry to any function, but only FreeBSD's C library seems to care. 65 // The caller was 8-byte aligned, but we push an LR. 66 // Declare a dummy word ($4, not $0) to make sure the 67 // frame is 8 bytes and stays 8-byte-aligned. 68 TEXT runtime·_initcgo(SB),NOSPLIT,$4 69 // if there is an _cgo_init, call it. 70 MOVW _cgo_init(SB), R4 71 CMP $0, R4 72 B.EQ nocgo 73 MRC 15, 0, R0, C13, C0, 3 // load TLS base pointer 74 MOVW R0, R3 // arg 3: TLS base pointer 75 #ifdef TLSG_IS_VARIABLE 76 MOVW $runtime·tls_g(SB), R2 // arg 2: &tls_g 77 #else 78 MOVW $0, R2 // arg 2: not used when using platform tls 79 #endif 80 MOVW $setg_gcc<>(SB), R1 // arg 1: setg 81 MOVW g, R0 // arg 0: G 82 BL (R4) // will clobber R0-R3 83 nocgo: 84 RET 85 86 // void setg_gcc(G*); set g called from gcc. 87 TEXT setg_gcc<>(SB),NOSPLIT,$0 88 MOVW R0, g 89 B runtime·save_g(SB) 90 91 #ifdef TLSG_IS_VARIABLE 92 #ifdef GOOS_android 93 // Use the free TLS_SLOT_APP slot #2 on Android Q. 94 // Earlier androids are set up in gcc_android.c. 95 DATA runtime·tls_g+0(SB)/4, $8 96 #endif 97 GLOBL runtime·tls_g+0(SB), NOPTR, $4 98 #else 99 GLOBL runtime·tls_g+0(SB), TLSBSS, $4 100 #endif