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